summaryrefslogtreecommitdiffstats
path: root/clang/lib/Lex/PPDirectives.cpp
diff options
context:
space:
mode:
authorSerge Pavlov <sepavloff@gmail.com>2014-12-02 11:06:09 +0000
committerSerge Pavlov <sepavloff@gmail.com>2014-12-02 11:06:09 +0000
commit1ecb41c09b7d81b6ad0d9934ede8464a5c142b2c (patch)
tree83e7f2a8ab343d7570fbf20054402eb00643e8fa /clang/lib/Lex/PPDirectives.cpp
parenta0199b9a594151cd8889665de57ab2b3f69ee137 (diff)
downloadbcm5719-llvm-1ecb41c09b7d81b6ad0d9934ede8464a5c142b2c.tar.gz
bcm5719-llvm-1ecb41c09b7d81b6ad0d9934ede8464a5c142b2c.zip
Emit warning if define or undef reserved identifier or keyword.
Summary: This change implements warnings if macro name is identical to a keyword or reserved identifier. The warnings are different depending on the "danger" of the operation. Defining macro that replaces a keyword is on by default. Other cases produce warning that is off by default but can be turned on using option -Wreserved-id-macro. This change fixes PR11488. Reviewers: rnk Reviewed By: rnk Subscribers: rnk, cfe-commits Differential Revision: http://reviews.llvm.org/D6194 llvm-svn: 223114
Diffstat (limited to 'clang/lib/Lex/PPDirectives.cpp')
-rw-r--r--clang/lib/Lex/PPDirectives.cpp69
1 files changed, 69 insertions, 0 deletions
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 0a46663d85d..16b86b7a305 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -100,6 +100,58 @@ void Preprocessor::DiscardUntilEndOfDirective() {
} while (Tmp.isNot(tok::eod));
}
+/// \brief Enumerates possible cases of #define/#undef a reserved identifier.
+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
+};
+
+/// \brief Checks if the specified identifier is reserved in the specified
+/// language.
+/// This function does not check if the identifier is a keyword.
+static bool isReservedId(StringRef Text, const LangOptions &Lang) {
+ // C++ [macro.names], 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] == '_' &&
+ (isUppercase(Text[1]) || Text[1] == '_'))
+ return true;
+ // C++ [global.names]
+ // Each name that contains a double underscore ... is reserved to the
+ // implementation for any use.
+ if (Lang.CPlusPlus) {
+ if (Text.find("__") != StringRef::npos)
+ return true;
+ }
+ return false;
+}
+
+static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
+ const LangOptions &Lang = PP.getLangOpts();
+ StringRef Text = II->getName();
+ if (isReservedId(Text, Lang))
+ return MD_WarnStrict;
+ if (II->isKeyword(Lang))
+ return MD_KeywordDef;
+ if (Lang.CPlusPlus && (Text.equals("override") || Text.equals("final")))
+ return MD_KeywordDef;
+ return MD_NoWarn;
+}
+
+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;
+ if (isReservedId(Text, Lang))
+ return MD_WarnStrict;
+ return MD_NoWarn;
+}
+
bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef) {
// Missing macro name?
if (MacroNameTok.is(tok::eod))
@@ -140,6 +192,23 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef) {
Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
}
+ // Warn if defining/undefining reserved identifier including keywords.
+ SourceLocation MacroNameLoc = MacroNameTok.getLocation();
+ if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
+ (strcmp(SourceMgr.getBufferName(MacroNameLoc), "<built-in>") != 0)) {
+ MacroDiag D = MD_NoWarn;
+ if (isDefineUndef == MU_Define)
+ D = shouldWarnOnMacroDef(*this, II);
+ else if (isDefineUndef == MU_Undef)
+ D = shouldWarnOnMacroUndef(*this, II);
+ if (D == MD_KeywordDef)
+ Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
+ if (D == MD_KeywordUndef)
+ 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;
}
OpenPOWER on IntegriCloud