aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Basic/IdentifierTable.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/Basic/IdentifierTable.cpp
parenta0199b9a594151cd8889665de57ab2b3f69ee137 (diff)
downloadllvm-1ecb41c09b7d81b6ad0d9934ede8464a5c142b2c.zip
llvm-1ecb41c09b7d81b6ad0d9934ede8464a5c142b2c.tar.gz
llvm-1ecb41c09b7d81b6ad0d9934ede8464a5c142b2c.tar.bz2
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/Basic/IdentifierTable.cpp')
-rw-r--r--clang/lib/Basic/IdentifierTable.cpp29
1 files changed, 27 insertions, 2 deletions
diff --git a/clang/lib/Basic/IdentifierTable.cpp b/clang/lib/Basic/IdentifierTable.cpp
index dd09704..613b43f 100644
--- a/clang/lib/Basic/IdentifierTable.cpp
+++ b/clang/lib/Basic/IdentifierTable.cpp
@@ -122,7 +122,7 @@ namespace {
/// \brief Translates flags as specified in TokenKinds.def into keyword status
/// in the given language standard.
-static KeywordStatus GetKeywordStatus(const LangOptions &LangOpts,
+static KeywordStatus getKeywordStatus(const LangOptions &LangOpts,
unsigned Flags) {
if (Flags == KEYALL) return KS_Enabled;
if (LangOpts.CPlusPlus && (Flags & KEYCXX)) return KS_Enabled;
@@ -151,7 +151,7 @@ static KeywordStatus GetKeywordStatus(const LangOptions &LangOpts,
static void AddKeyword(StringRef Keyword,
tok::TokenKind TokenCode, unsigned Flags,
const LangOptions &LangOpts, IdentifierTable &Table) {
- KeywordStatus AddResult = GetKeywordStatus(LangOpts, Flags);
+ KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags);
// Don't add this keyword under MSVCCompat.
if (LangOpts.MSVCCompat && (Flags & KEYNOMS))
@@ -209,6 +209,31 @@ void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
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 getTokenKwStatus(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 (getTokenKwStatus(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