[clang] b8290ff - Fix -Wreserved-identifier in presence of system macro

via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 31 02:16:41 PST 2022


Author: serge-sans-paille
Date: 2022-01-31T11:16:28+01:00
New Revision: b8290ffa9fd935b8a0e99634fccfae9ed87ad9b5

URL: https://github.com/llvm/llvm-project/commit/b8290ffa9fd935b8a0e99634fccfae9ed87ad9b5
DIFF: https://github.com/llvm/llvm-project/commit/b8290ffa9fd935b8a0e99634fccfae9ed87ad9b5.diff

LOG: Fix -Wreserved-identifier in presence of system macro

Do not warn on reserved identifiers resulting from expansion of system macros.
Also properly test -Wreserved-identifier wrt. system headers.

Should fix #49592

Differential Revision: https://reviews.llvm.org/D118532

Added: 
    clang/test/Sema/Inputs/reserved-identifier.h

Modified: 
    clang/lib/Sema/SemaDecl.cpp
    clang/test/Sema/reserved-identifier.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 3252671991b70..e747ffc6f2ac1 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -5703,6 +5703,13 @@ static bool RebuildDeclaratorInCurrentInstantiation(Sema &S, Declarator &D,
   return false;
 }
 
+/// Returns true if the declaration is declared in a system header or from a
+/// system macro.
+static bool isFromSystemHeader(SourceManager &SM, const Decl *D) {
+  return SM.isInSystemHeader(D->getLocation()) ||
+         SM.isInSystemMacro(D->getLocation());
+}
+
 void Sema::warnOnReservedIdentifier(const NamedDecl *D) {
   // Avoid warning twice on the same identifier, and don't warn on redeclaration
   // of system decl.
@@ -5710,9 +5717,10 @@ void Sema::warnOnReservedIdentifier(const NamedDecl *D) {
     return;
   ReservedIdentifierStatus Status = D->isReserved(getLangOpts());
   if (Status != ReservedIdentifierStatus::NotReserved &&
-      !Context.getSourceManager().isInSystemHeader(D->getLocation()))
+      !isFromSystemHeader(Context.getSourceManager(), D)) {
     Diag(D->getLocation(), diag::warn_reserved_extern_symbol)
         << D << static_cast<int>(Status);
+  }
 }
 
 Decl *Sema::ActOnDeclarator(Scope *S, Declarator &D) {

diff  --git a/clang/test/Sema/Inputs/reserved-identifier.h b/clang/test/Sema/Inputs/reserved-identifier.h
new file mode 100644
index 0000000000000..83b681f239c3b
--- /dev/null
+++ b/clang/test/Sema/Inputs/reserved-identifier.h
@@ -0,0 +1,4 @@
+int __i_come_from_a_system_header; // no-warning
+#define __I_AM_A_SYSTEM_MACRO()    // no-warning
+
+#define SOME_SYSTEM_MACRO() int __i_come_from_a_system_macro

diff  --git a/clang/test/Sema/reserved-identifier.c b/clang/test/Sema/reserved-identifier.c
index 746b4775b0fe1..9296e7125e2c8 100644
--- a/clang/test/Sema/reserved-identifier.c
+++ b/clang/test/Sema/reserved-identifier.c
@@ -1,4 +1,12 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -Wreserved-identifier -Wno-visibility %s
+// RUN: %clang_cc1 -isystem %S/Inputs -fsyntax-only -verify -Wreserved-identifier -Wno-visibility %s
+
+#include <reserved-identifier.h>
+
+__I_AM_A_SYSTEM_MACRO() // no-warning
+
+void test_system_macro_expansion() {
+  SOME_SYSTEM_MACRO(); // no-warning
+}
 
 #define __oof foo__ // expected-warning {{macro name is a reserved identifier}}
 
@@ -58,7 +66,7 @@ void func(struct _preserved { int a; } r) {} // expected-warning {{identifier '_
 
 extern char *_strdup(const char *); // expected-warning {{identifier '_strdup' is reserved because it starts with '_' at global scope}}
 
-// Don't warn on redecleration
+// Don't warn on redeclaration
 extern char *_strdup(const char *); // no-warning
 
 void ok() {


        


More information about the cfe-commits mailing list