diff --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp --- a/clang/lib/Sema/SemaAttr.cpp +++ b/clang/lib/Sema/SemaAttr.cpp @@ -810,10 +810,23 @@ return; } - DeclContext *DC = ND->getDeclContext(); - if (getLangOpts().CPlusPlus && !DC->isExternCContext()) { - Diag(Loc, diag::err_pragma_alloc_text_c_linkage); - return; + if (getLangOpts().CPlusPlus) { + bool IsInExternCContext = false; + Decl *D = ND; + while (D != nullptr) { + if (auto *FD = dyn_cast(D)) { + if (FD->isInExternCContext()) { + IsInExternCContext = true; + break; + } + } + D = D->getPreviousDecl(); + } + + if (!IsInExternCContext) { + Diag(Loc, diag::err_pragma_alloc_text_c_linkage); + return; + } } FunctionToSectionMap[II->getName()] = std::make_tuple(Section, Loc); diff --git a/clang/test/Sema/pragma-ms-alloc-text.cpp b/clang/test/Sema/pragma-ms-alloc-text.cpp --- a/clang/test/Sema/pragma-ms-alloc-text.cpp +++ b/clang/test/Sema/pragma-ms-alloc-text.cpp @@ -40,3 +40,10 @@ #pragma alloc_text(c, foo6) // no-warning void foo6() {} } + +extern "C" { +static void foo7(); +} +static void foo7(); +#pragma alloc_text(c, foo7) // no-warning +void foo7() {}