Repository: llvm/llvm-project
File: clang/lib/Sema/SemaLambda.cpp (C++)

Code before:
    NonInlineInModulePurview
  } Kind = Normal;

  bool IsInNonspecializedTemplate =
      inTemplateInstantiation() || CurContext->isDependentContext();

  // Default arguments of member function parameters that appear in a class
  // definition, as well as the initializers of data members, receive special
  // treatment. Identify them.
  Kind = [&]() {
    // See discussion in https://github.com/itanium-cxx-abi/cxx-abi/issues/186
    //
    // zygoloid:
    //    Yeah, I think the only cases left where lambdas don't need a
    //    mangling are when they have (effectively) internal linkage or appear
    //    in a non-inline function in a non-module translation unit.

    Decl *ManglingContextDeclForModule = [&]() {
      if (!ManglingContextDecl || [&]() {
            // If we must allocate mangling numbers but the
            // `ManglingContextDecl` is a local variable, use the `DeclContext`
            // containing the lambda expression instead.
            VarDecl *Var = dyn_cast<VarDecl>(ManglingContextDecl);
            return Var && Var->isLocalVarDecl();
          }())
        return const_cast<Decl *>(cast<Decl>(DC));

      return ManglingContextDecl;
    }();

    if (auto *ND = dyn_cast<NamedDecl>(ManglingContextDeclForModule);
        ND && (ND->isInNamedModule() || ND->isFromGlobalModule()) &&
        ND->isExternallyVisible()) {
      ManglingContextDecl = ND;
      return NonInlineInModulePurview;
    }

    if (!ManglingContextDecl)
      return Normal;

    if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(ManglingContextDecl)) {
      if (const DeclContext *LexicalDC
          = Param->getDeclContext()->getLexicalParent())
        if (LexicalDC->isRecord())
          return DefaultArgument;
    } else if (VarDecl *Var = dyn_cast<VarDecl>(ManglingContextDecl)) {
      if (Var->getMostRecentDecl()->isInline())
        return InlineVariable;

      if (Var->getDeclContext()->isRecord() && IsInNonspecializedTemplate)
        return TemplatedVariable;

Review comment (suggestion):
```suggestion
if (auto *VD = dyn_cast_if_present<VarDecl>(ManglingContextDecl); VD && VD->isLocalVarDecl())
   ...
```

Code after:
  //    mangling are when they have (effectively) internal linkage or
  //    appear in a non-inline function in a non-module translation unit.
  static constexpr auto IsExternallyVisibleInModulePurview =
      [](const NamedDecl *ND) -> bool {
    return (ND->isInNamedModule() || ND->isFromGlobalModule()) &&
           ND->isExternallyVisible();
  };

  // Default arguments of member function parameters that appear in a class
  // definition, as well as the initializers of data members, receive special
  // treatment. Identify them.
  Kind = [&]() {
    if (!ManglingContextDecl)
      return Normal;

    if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(ManglingContextDecl)) {
      if (const DeclContext *LexicalDC
          = Param->getDeclContext()->getLexicalParent())
        if (LexicalDC->isRecord())
          return DefaultArgument;
    } else if (VarDecl *Var = dyn_cast<VarDecl>(ManglingContextDecl)) {
      if (Var->getMostRecentDecl()->isInline())
        return InlineVariable;

      if (IsExternallyVisibleInModulePurview(Var))
        return ExternallyVisibleVariableInModulePurview;

      if (Var->getDeclContext()->isRecord() && IsInNonspecializedTemplate)
        return TemplatedVariable;

      if (Var->getDescribedVarTemplate())
        return TemplatedVariable;

      if (auto *VTS = dyn_cast<VarTemplateSpecializationDecl>(Var)) {
        if (!VTS->isExplicitSpecialization())
          return TemplatedVariable;
      }
    } else if (isa<FieldDecl>(ManglingContextDecl)) {
      return DataMember;
    } else if (isa<ImplicitConceptSpecializationDecl>(ManglingContextDecl)) {
      return Concept;
    }

    return Normal;
  }();

  // Determine whether the given context is or is enclosed in a function that
  // requires Decl's inside to be mangled, so either:
  // - an inline function
  // - or a function in a module purview that is externally visible
  static constexpr auto IsInFunctionThatRequiresMangling =