steplong updated this revision to Diff 440268.
steplong added a comment.

- Use `getCanonicalDecl()`. This does make us reject (MSVC rejects it already 
because of conflicting declarations):

  static void f();
  extern "C" { static void f(); }
  static void f() {}



- Error if not function decl

I think we have to use `isInExternCContext()` to accept the following (MSVC 
accepts this):

  extern "C" { static void f(); }
  static void f() {}


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D128649/new/

https://reviews.llvm.org/D128649

Files:
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  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,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}}
Index: clang/lib/Sema/SemaAttr.cpp
===================================================================
--- clang/lib/Sema/SemaAttr.cpp
+++ 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<Decl>(ND)->getCanonicalDecl();
+
+      if (auto *FD = dyn_cast<FunctionDecl>(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);
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ 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'">,


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,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}}
Index: clang/lib/Sema/SemaAttr.cpp
===================================================================
--- clang/lib/Sema/SemaAttr.cpp
+++ 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<Decl>(ND)->getCanonicalDecl();
+
+      if (auto *FD = dyn_cast<FunctionDecl>(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);
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ 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'">,
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to