diff options
author | Serge Pavlov <sepavloff@gmail.com> | 2014-12-02 11:06:09 +0000 |
---|---|---|
committer | Serge Pavlov <sepavloff@gmail.com> | 2014-12-02 11:06:09 +0000 |
commit | d04e6fdd403fcd39dc96075df858cd942c3ceab5 (patch) | |
tree | bfb6268d3c7431aa8d44500ae098d0ca4847e304 /lib/Basic/IdentifierTable.cpp | |
parent | 85fd1028d5358138fcfae7adb9c09a386a91c332 (diff) | |
download | clang-d04e6fdd403fcd39dc96075df858cd942c3ceab5.tar.gz |
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
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@223114 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Basic/IdentifierTable.cpp')
-rw-r--r-- | lib/Basic/IdentifierTable.cpp | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/lib/Basic/IdentifierTable.cpp b/lib/Basic/IdentifierTable.cpp index dd097047dc..613b43fce9 100644 --- a/lib/Basic/IdentifierTable.cpp +++ b/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 |