steplong created this revision. Herald added a project: All. steplong requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
MSVC's pragma alloc_text accepts a function that was redeclared in a non extern-C context if the previous declaration was in an extern-C context. i.e. extern "C" { static void f(); } static void f(); Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D128649 Files: clang/lib/Sema/SemaAttr.cpp clang/test/Sema/pragma-ms-alloc-text.cpp Index: clang/test/Sema/pragma-ms-alloc-text.cpp =================================================================== --- clang/test/Sema/pragma-ms-alloc-text.cpp +++ 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() {} Index: clang/lib/Sema/SemaAttr.cpp =================================================================== --- clang/lib/Sema/SemaAttr.cpp +++ 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<FunctionDecl>(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);
Index: clang/test/Sema/pragma-ms-alloc-text.cpp =================================================================== --- clang/test/Sema/pragma-ms-alloc-text.cpp +++ 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() {} Index: clang/lib/Sema/SemaAttr.cpp =================================================================== --- clang/lib/Sema/SemaAttr.cpp +++ 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<FunctionDecl>(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);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits