r224100 - Do not warn on keyword undef

Serge Pavlov sepavloff at gmail.com
Thu Dec 11 22:37:56 PST 2014


Author: sepavloff
Date: Fri Dec 12 00:37:55 2014
New Revision: 224100

URL: http://llvm.org/viewvc/llvm-project?rev=224100&view=rev
Log:
Do not warn on keyword undef

#undef a keyword is generally harmless but used often in configuration scripts.
Also added tests that I forgot to include to commit in r223114.


Added:
    cfe/trunk/test/Preprocessor/macro-reserved.c
    cfe/trunk/test/Preprocessor/macro-reserved.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td
    cfe/trunk/lib/Lex/PPDirectives.cpp
    cfe/trunk/test/PCH/single-token-macro.c
    cfe/trunk/test/Sema/thread-specifier.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td?rev=224100&r1=224099&r2=224100&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td Fri Dec 12 00:37:55 2014
@@ -291,12 +291,10 @@ def note_pp_ambiguous_macro_chosen : Not
 def note_pp_ambiguous_macro_other : Note<
   "other definition of %0">;
 def warn_pp_macro_hides_keyword : Warning<
-  "keyword or reserved identifier is hidden by macro definition">,
+  "keyword is hidden by macro definition">,
   InGroup<KeywordAsMacro>;
 def warn_pp_macro_is_reserved_id : Warning<
-  "macro name is a keyword or reserved identifier">, InGroup<KeywordAsMacro>;
-def warn_pp_defundef_reserved_ident : Warning<
-  "reserved identifier is used as macro name">, DefaultIgnore,
+  "macro name is a reserved identifier">, DefaultIgnore,
   InGroup<ReservedIdAsMacro>;
 
 def pp_invalid_string_literal : Warning<

Modified: cfe/trunk/lib/Lex/PPDirectives.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPDirectives.cpp?rev=224100&r1=224099&r2=224100&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPDirectives.cpp (original)
+++ cfe/trunk/lib/Lex/PPDirectives.cpp Fri Dec 12 00:37:55 2014
@@ -104,8 +104,7 @@ void Preprocessor::DiscardUntilEndOfDire
 enum MacroDiag {
   MD_NoWarn,        //> Not a reserved identifier
   MD_KeywordDef,    //> Macro hides keyword, enabled by default
-  MD_KeywordUndef,  //> #undef keyword, enabled by default
-  MD_WarnStrict     //> Other reserved id, disabled by default
+  MD_ReservedMacro  //> #define of #undef reserved id, disabled by default
 };
 
 /// \brief Checks if the specified identifier is reserved in the specified
@@ -132,7 +131,8 @@ static MacroDiag shouldWarnOnMacroDef(Pr
   const LangOptions &Lang = PP.getLangOpts();
   StringRef Text = II->getName();
   if (isReservedId(Text, Lang))
-    return MD_WarnStrict;
+    return MD_ReservedMacro;
+  // Do not warn on keyword undef.  It is generally harmless and widely used.
   if (II->isKeyword(Lang))
     return MD_KeywordDef;
   if (Lang.CPlusPlus && (Text.equals("override") || Text.equals("final")))
@@ -142,13 +142,10 @@ static MacroDiag shouldWarnOnMacroDef(Pr
 
 static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
   const LangOptions &Lang = PP.getLangOpts();
-  if (II->isKeyword(Lang))
-    return MD_KeywordUndef;
   StringRef Text = II->getName();
-  if (Lang.CPlusPlus && (Text.equals("override") || Text.equals("final")))
-    return MD_KeywordUndef;
+  // Do not warn on keyword undef.  It is generally harmless and widely used.
   if (isReservedId(Text, Lang))
-    return MD_WarnStrict;
+    return MD_ReservedMacro;
   return MD_NoWarn;
 }
 
@@ -203,10 +200,8 @@ bool Preprocessor::CheckMacroName(Token
       D = shouldWarnOnMacroUndef(*this, II);
     if (D == MD_KeywordDef)
       Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
-    if (D == MD_KeywordUndef)
+    if (D == MD_ReservedMacro)
       Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
-    else if (D == MD_WarnStrict)
-      Diag(MacroNameTok, diag::warn_pp_defundef_reserved_ident);
   }
 
   // Okay, we got a good identifier.

Modified: cfe/trunk/test/PCH/single-token-macro.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/single-token-macro.c?rev=224100&r1=224099&r2=224100&view=diff
==============================================================================
--- cfe/trunk/test/PCH/single-token-macro.c (original)
+++ cfe/trunk/test/PCH/single-token-macro.c Fri Dec 12 00:37:55 2014
@@ -12,8 +12,6 @@
 #ifndef HEADER
 #define HEADER
 
-#pragma clang diagnostic ignored "-Wreserved-id-macro"
-
 #ifdef __stdcall
 // __stdcall is defined as __attribute__((__stdcall__)) for targeting mingw32.
 #undef __stdcall

Added: cfe/trunk/test/Preprocessor/macro-reserved.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/macro-reserved.c?rev=224100&view=auto
==============================================================================
--- cfe/trunk/test/Preprocessor/macro-reserved.c (added)
+++ cfe/trunk/test/Preprocessor/macro-reserved.c Fri Dec 12 00:37:55 2014
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 -fsyntax-only %s -verify
+
+#define for 0    // expected-warning {{keyword is hidden by macro definition}}
+#define final 1
+#define __HAVE_X 0
+#define _HAVE_X 0
+#define X__Y
+
+#undef __cplusplus
+#undef _HAVE_X
+#undef X__Y
+
+#pragma clang diagnostic warning "-Wreserved-id-macro"
+
+#define switch if  // expected-warning {{keyword is hidden by macro definition}}
+#define final 1
+#define __HAVE_X 0 // expected-warning {{macro name is a reserved identifier}}
+#define _HAVE_X 0  // expected-warning {{macro name is a reserved identifier}}
+#define X__Y
+
+#undef __cplusplus // expected-warning {{macro name is a reserved identifier}}
+#undef _HAVE_X     // expected-warning {{macro name is a reserved identifier}}
+#undef X__Y
+
+int x;

Added: cfe/trunk/test/Preprocessor/macro-reserved.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/macro-reserved.cpp?rev=224100&view=auto
==============================================================================
--- cfe/trunk/test/Preprocessor/macro-reserved.cpp (added)
+++ cfe/trunk/test/Preprocessor/macro-reserved.cpp Fri Dec 12 00:37:55 2014
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 -fsyntax-only %s -verify
+
+#define for 0    // expected-warning {{keyword is hidden by macro definition}}
+#define final 1  // expected-warning {{keyword is hidden by macro definition}}
+#define __HAVE_X 0
+#define _HAVE_X 0
+#define X__Y
+
+#undef __cplusplus
+#undef _HAVE_X
+#undef X__Y
+
+#pragma clang diagnostic warning "-Wreserved-id-macro"
+
+#define switch if  // expected-warning {{keyword is hidden by macro definition}}
+#define final 1    // expected-warning {{keyword is hidden by macro definition}}
+#define __HAVE_X 0 // expected-warning {{macro name is a reserved identifier}}
+#define _HAVE_X 0  // expected-warning {{macro name is a reserved identifier}}
+#define X__Y       // expected-warning {{macro name is a reserved identifier}}
+
+#undef __cplusplus // expected-warning {{macro name is a reserved identifier}}
+#undef _HAVE_X     // expected-warning {{macro name is a reserved identifier}}
+#undef X__Y        // expected-warning {{macro name is a reserved identifier}}
+
+int x;

Modified: cfe/trunk/test/Sema/thread-specifier.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/thread-specifier.c?rev=224100&r1=224099&r2=224100&view=diff
==============================================================================
--- cfe/trunk/test/Sema/thread-specifier.c (original)
+++ cfe/trunk/test/Sema/thread-specifier.c Fri Dec 12 00:37:55 2014
@@ -5,8 +5,6 @@
 // RUN: %clang_cc1 -triple i686-pc-linux-gnu -fsyntax-only -Wno-private-extern -verify -pedantic -x c++ %s -DCXX11 -D__thread=thread_local -std=c++11 -Wno-deprecated
 // RUN: %clang_cc1 -triple i686-pc-linux-gnu -fsyntax-only -Wno-private-extern -verify -pedantic -x c++ %s -DC11 -D__thread=_Thread_local -std=c++11 -Wno-deprecated
 
-#pragma clang diagnostic ignored "-Wkeyword-macro"
-
 #ifdef __cplusplus
 // In C++, we define __private_extern__ to extern.
 #undef __private_extern__





More information about the cfe-commits mailing list