diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -992,6 +992,8 @@ "'#pragma %0' can only appear at file scope">; def err_pragma_alloc_text_c_linkage: Error< "'#pragma alloc_text' is applicable only to functions with C linkage">; +def err_pragma_alloc_text_not_function: Error< + "'#pragma alloc_text' is applicable only to functions">; def warn_pragma_unused_undeclared_var : Warning< "undeclared variable %0 used as an argument for '#pragma unused'">, 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,18 @@ return; } - DeclContext *DC = ND->getDeclContext(); - if (getLangOpts().CPlusPlus && !DC->isExternCContext()) { - Diag(Loc, diag::err_pragma_alloc_text_c_linkage); - return; + if (getLangOpts().CPlusPlus) { + Decl *D = cast(ND)->getCanonicalDecl(); + + if (auto *FD = dyn_cast(D)) { + if (!FD->isInExternCContext()) { + Diag(Loc, diag::err_pragma_alloc_text_c_linkage); + return; + } + } else { + Diag(Loc, diag::err_pragma_alloc_text_not_function); + 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,20 @@ #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() {} + +static void foo8(); +extern "C" { +static void foo8(); +} +#pragma alloc_text(c, foo8) // expected-error {{'#pragma alloc_text' is applicable only to functions with C linkage}} +void foo7() {} + +enum foo9 { A, B, C }; +#pragma alloc_text(c, foo9) // expected-error {{'#pragma alloc_text' is applicable only to functions}}