[PATCH] D128649: [clang-cl] Accept a pragma alloc_text corner case accepted by MSVC

Stephen Long via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 27 07:38:32 PDT 2022


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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128649.440222.patch
Type: text/x-patch
Size: 1355 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220627/6028ede1/attachment.bin>


More information about the cfe-commits mailing list