Index: include/clang/Basic/DiagnosticGroups.td =================================================================== --- include/clang/Basic/DiagnosticGroups.td +++ include/clang/Basic/DiagnosticGroups.td @@ -336,6 +336,7 @@ // Preprocessor warnings. def AmbiguousMacro : DiagGroup<"ambiguous-macro">; +def ReservedIdAsMacro : DiagGroup<"reserved-id-macro">; // Just silence warnings about -Wstrict-aliasing for now. def : DiagGroup<"strict-aliasing=0">; Index: include/clang/Basic/DiagnosticLexKinds.td =================================================================== --- include/clang/Basic/DiagnosticLexKinds.td +++ include/clang/Basic/DiagnosticLexKinds.td @@ -290,6 +290,14 @@ "expanding this definition of %0">; 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">, + InGroup; +def warn_pp_macro_is_reserved_id : Warning< + "macro name is a keyword or reserved identifier">, InGroup; +def warn_pp_defundef_reserved_ident : Warning< + "reserved identifier is used as macro name">, DefaultIgnore, + InGroup; def pp_invalid_string_literal : Warning< "invalid string literal, ignoring final '\\'">; Index: include/clang/Basic/IdentifierTable.h =================================================================== --- include/clang/Basic/IdentifierTable.h +++ include/clang/Basic/IdentifierTable.h @@ -249,6 +249,9 @@ } bool isCPlusPlusOperatorKeyword() const { return IsCPPOperatorKeyword; } + /// \brief Return true if this token is a keyword in the specified language. + bool isKeyword(const LangOptions &LangOpts); + /// getFETokenInfo/setFETokenInfo - The language front-end is allowed to /// associate arbitrary metadata with this token. template Index: lib/Basic/IdentifierTable.cpp =================================================================== --- lib/Basic/IdentifierTable.cpp +++ lib/Basic/IdentifierTable.cpp @@ -209,6 +209,31 @@ LangOpts, *this); } +/// \brief Checks if the specified token kind represents a keyword in the +/// specified language. +/// \returns Status of the keyword in the language. +static KeywordStatus GetKeywordStatus(const LangOptions &LangOpts, + tok::TokenKind K) { + switch (K) { +#define KEYWORD(NAME, FLAGS) \ + case tok::kw_##NAME: return GetKeywordStatus(LangOpts, FLAGS); +#include "clang/Basic/TokenKinds.def" + default: return KS_Disabled; + } +} + +/// \brief Returns true if the identifier represents a keyword in the +/// specified language. +bool IdentifierInfo::isKeyword(const LangOptions &LangOpts) { + switch (GetKeywordStatus(LangOpts, getTokenID())) { + case KS_Enabled: + case KS_Extension: + return true; + default: + return false; + } +} + tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const { // We use a perfect hash function here involving the length of the keyword, // the first and third character. For preprocessor ID's there are no Index: lib/Lex/PPDirectives.cpp =================================================================== --- lib/Lex/PPDirectives.cpp +++ lib/Lex/PPDirectives.cpp @@ -100,6 +100,55 @@ } while (Tmp.isNot(tok::eod)); } +/// \brief Enumerates possible cases of #define/#undef a reserved identifier. +enum MacroDiag { + MD_NoWarn, //> Not a reserved identifier + MD_Keyword, //> Macro hides keyword + MD_Warn, //> Warn, on by default + MD_WarnStrict //> Warn, off by default +}; + +static MacroDiag ShouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) { + StringRef Text = II->getName(); + // C++ [macro.globals], C11 7.1.3: All identifiers that begin with an + // underscore and either an uppercase letter or another underscore are always + // reserved for any use. + if (Text.size() >= 2 && Text[0] == '_' && + ((Text[1] >= 'A' && Text[1] <= 'Z') || Text[1] == '_')) + return MD_WarnStrict; + if (PP.getLangOpts().CPlusPlus) { + if (Text.find("__") != StringRef::npos) + return MD_WarnStrict; + } + if (II->isKeyword(PP.getLangOpts())) + return MD_Keyword; + // See C++11 [macro.names]. + if (PP.getLangOpts().CPlusPlus) { + if (Text.equals("override") || Text.equals("final")) + return MD_Keyword; + } + return MD_NoWarn; +} + +static MacroDiag ShouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) { + if (II->isKeyword(PP.getLangOpts())) + return MD_Warn; + StringRef Text = II->getName(); + // C++11 [macro.names] + if (PP.getLangOpts().CPlusPlus) { + if (Text.equals("override") || Text.equals("final")) + return MD_Warn; + } + if (Text.size() >= 2 && Text[0] == '_' && + ((Text[1] >= 'A' && Text[1] <= 'Z') || Text[1] == '_')) + return MD_WarnStrict; + if (PP.getLangOpts().CPlusPlus) { + if (Text.find("__") != StringRef::npos) + return MD_WarnStrict; + } + return MD_NoWarn; +} + bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef) { // Missing macro name? if (MacroNameTok.is(tok::eod)) @@ -140,6 +189,27 @@ Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro); } + // Warn if defining/undefining reserved identifier including keywords. + SourceLocation MacroNameLoc = MacroNameTok.getLocation(); + bool WarnOnReserved = !SourceMgr.isInSystemHeader(MacroNameLoc); + if (WarnOnReserved) { + PresumedLoc PLoc = getSourceManager().getPresumedLoc(MacroNameLoc, false); + WarnOnReserved = (strcmp(PLoc.getFilename(), "") != 0); + } + if (WarnOnReserved) { + MacroDiag D = MD_NoWarn; + if (isDefineUndef == MU_Define) + D = ShouldWarnOnMacroDef(*this, II); + else if (isDefineUndef == MU_Undef) + D = ShouldWarnOnMacroUndef(*this, II); + if (D == MD_Keyword) + Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword); + if (D == MD_Warn) + 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. return false; } Index: test/PCH/single-token-macro.c =================================================================== --- test/PCH/single-token-macro.c +++ test/PCH/single-token-macro.c @@ -12,6 +12,8 @@ #ifndef HEADER #define HEADER +#pragma clang diagnostic ignored "-Wreserved-id-macro" + #ifdef __stdcall // __stdcall is defined as __attribute__((__stdcall__)) for targeting mingw32. #undef __stdcall Index: test/Preprocessor/cxx_oper_keyword_ms_compat.cpp =================================================================== --- test/Preprocessor/cxx_oper_keyword_ms_compat.cpp +++ test/Preprocessor/cxx_oper_keyword_ms_compat.cpp @@ -1,6 +1,8 @@ // RUN: %clang_cc1 %s -E -verify -fms-extensions // expected-no-diagnostics +#pragma clang diagnostic ignored "-Wreserved-id-macro" + bool f() { // Check that operators still work before redefining them. #if compl 0 bitand 1 Index: test/Preprocessor/macro-reserved.c =================================================================== --- /dev/null +++ test/Preprocessor/macro-reserved.c @@ -0,0 +1,29 @@ +// RUN: %clang_cc1 -fsyntax-only %s -verify + +#define for 0 // expected-warning {{keyword or reserved identifier is hidden by macro definition}} +#define final 1 +#define __HAVE_X 0 +#define _HAVE_X 0 +#define X__Y + +#undef inline // expected-warning {{macro name is a keyword or reserved identifier}} +#undef override +#undef __cplusplus +#undef _HAVE_X +#undef X__Y + +#pragma clang diagnostic warning "-Wreserved-id-macro" + +#define switch if // expected-warning {{keyword or reserved identifier is hidden by macro definition}} +#define final 1 +#define __HAVE_X 0 // expected-warning {{reserved identifier is used as macro name}} +#define _HAVE_X 0 // expected-warning {{reserved identifier is used as macro name}} +#define X__Y + +#undef inline // expected-warning {{macro name is a keyword or reserved identifier}} +#undef override +#undef __cplusplus // expected-warning {{reserved identifier is used as macro name}} +#undef _HAVE_X // expected-warning {{reserved identifier is used as macro name}} +#undef X__Y + +int x; Index: test/Preprocessor/macro-reserved.cpp =================================================================== --- /dev/null +++ test/Preprocessor/macro-reserved.cpp @@ -0,0 +1,29 @@ +// RUN: %clang_cc1 -fsyntax-only %s -verify + +#define for 0 // expected-warning {{keyword or reserved identifier is hidden by macro definition}} +#define final 1 // expected-warning {{keyword or reserved identifier is hidden by macro definition}} +#define __HAVE_X 0 +#define _HAVE_X 0 +#define X__Y + +#undef inline // expected-warning {{macro name is a keyword or reserved identifier}} +#undef override // expected-warning {{macro name is a keyword or reserved identifier}} +#undef __cplusplus +#undef _HAVE_X +#undef X__Y + +#pragma clang diagnostic warning "-Wreserved-id-macro" + +#define switch if // expected-warning {{keyword or reserved identifier is hidden by macro definition}} +#define final 1 // expected-warning {{keyword or reserved identifier is hidden by macro definition}} +#define __HAVE_X 0 // expected-warning {{reserved identifier is used as macro name}} +#define _HAVE_X 0 // expected-warning {{reserved identifier is used as macro name}} +#define X__Y // expected-warning {{reserved identifier is used as macro name}} + +#undef inline // expected-warning {{macro name is a keyword or reserved identifier}} +#undef override // expected-warning {{macro name is a keyword or reserved identifier}} +#undef __cplusplus // expected-warning {{reserved identifier is used as macro name}} +#undef _HAVE_X // expected-warning {{reserved identifier is used as macro name}} +#undef X__Y // expected-warning {{reserved identifier is used as macro name}} + +int x; Index: test/Sema/thread-specifier.c =================================================================== --- test/Sema/thread-specifier.c +++ test/Sema/thread-specifier.c @@ -5,6 +5,8 @@ // 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 "-Wreserved-id-macro" + #ifdef __cplusplus // In C++, we define __private_extern__ to extern. #undef __private_extern__