Skip to content

Commit 9fc3b4a

Browse files
committed
[clang] Add a namespace for interesting identifiers.
Differential Revision: https://reviews.llvm.org/D146148
1 parent 9ab964e commit 9fc3b4a

File tree

6 files changed

+85
-11
lines changed

6 files changed

+85
-11
lines changed

clang/include/clang/Basic/IdentifierTable.h

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ enum { IdentifierInfoAlignment = 8 };
7676

7777
static constexpr int ObjCOrBuiltinIDBits = 16;
7878

79+
/// The "layout" of ObjCOrBuiltinID is:
80+
/// - The first value (0) represents "not a special identifier".
81+
/// - The next (NUM_OBJC_KEYWORDS - 1) values represent ObjCKeywordKinds (not
82+
/// including objc_not_keyword).
83+
/// - The next (NUM_INTERESTING_IDENTIFIERS - 1) values represent
84+
/// InterestingIdentifierKinds (not including not_interesting).
85+
/// - The rest of the values represent builtin IDs (not including NotBuiltin).
86+
static constexpr int FirstObjCKeywordID = 1;
87+
static constexpr int LastObjCKeywordID =
88+
FirstObjCKeywordID + tok::NUM_OBJC_KEYWORDS - 2;
89+
static constexpr int FirstInterestingIdentifierID = LastObjCKeywordID + 1;
90+
static constexpr int LastInterestingIdentifierID =
91+
FirstInterestingIdentifierID + tok::NUM_INTERESTING_IDENTIFIERS - 2;
92+
static constexpr int FirstBuiltinID = LastInterestingIdentifierID + 1;
93+
7994
/// One of these records is kept for each identifier that
8095
/// is lexed. This contains information about whether the token was \#define'd,
8196
/// is a language keyword, or if it is a front-end token of some sort (e.g. a
@@ -290,7 +305,9 @@ class alignas(IdentifierInfoAlignment) IdentifierInfo {
290305
///
291306
/// For example, 'class' will return tok::objc_class if ObjC is enabled.
292307
tok::ObjCKeywordKind getObjCKeywordID() const {
293-
if (ObjCOrBuiltinID < tok::NUM_OBJC_KEYWORDS)
308+
static_assert(FirstObjCKeywordID == 1,
309+
"hard-coding this assumption to simplify code");
310+
if (ObjCOrBuiltinID <= LastObjCKeywordID)
294311
return tok::ObjCKeywordKind(ObjCOrBuiltinID);
295312
else
296313
return tok::objc_not_keyword;
@@ -301,15 +318,30 @@ class alignas(IdentifierInfoAlignment) IdentifierInfo {
301318
///
302319
/// 0 is not-built-in. 1+ are specific builtin functions.
303320
unsigned getBuiltinID() const {
304-
if (ObjCOrBuiltinID >= tok::NUM_OBJC_KEYWORDS)
305-
return ObjCOrBuiltinID - tok::NUM_OBJC_KEYWORDS;
321+
if (ObjCOrBuiltinID >= FirstBuiltinID)
322+
return 1 + (ObjCOrBuiltinID - FirstBuiltinID);
306323
else
307324
return 0;
308325
}
309326
void setBuiltinID(unsigned ID) {
310-
ObjCOrBuiltinID = ID + tok::NUM_OBJC_KEYWORDS;
311-
assert(ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID
312-
&& "ID too large for field!");
327+
assert(ID != 0);
328+
ObjCOrBuiltinID = FirstBuiltinID + (ID - 1);
329+
assert(getBuiltinID() == ID && "ID too large for field!");
330+
}
331+
void clearBuiltinID() { ObjCOrBuiltinID = 0; }
332+
333+
tok::InterestingIdentifierKind getInterestingIdentifierID() const {
334+
if (ObjCOrBuiltinID >= FirstInterestingIdentifierID &&
335+
ObjCOrBuiltinID <= LastInterestingIdentifierID)
336+
return tok::InterestingIdentifierKind(
337+
1 + (ObjCOrBuiltinID - FirstInterestingIdentifierID));
338+
else
339+
return tok::not_interesting;
340+
}
341+
void setInterestingIdentifierID(unsigned ID) {
342+
assert(ID != tok::not_interesting);
343+
ObjCOrBuiltinID = FirstInterestingIdentifierID + (ID - 1);
344+
assert(getInterestingIdentifierID() == ID && "ID too large for field!");
313345
}
314346

315347
unsigned getObjCOrBuiltinID() const { return ObjCOrBuiltinID; }

clang/include/clang/Basic/TokenKinds.def

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@
8585
#ifndef PRAGMA_ANNOTATION
8686
#define PRAGMA_ANNOTATION(X) ANNOTATION(X)
8787
#endif
88+
#ifndef INTERESTING_IDENTIFIER
89+
#define INTERESTING_IDENTIFIER(X)
90+
#endif
8891

8992
//===----------------------------------------------------------------------===//
9093
// Preprocessor keywords.
@@ -794,6 +797,15 @@ OBJC_AT_KEYWORD(dynamic)
794797
OBJC_AT_KEYWORD(import)
795798
OBJC_AT_KEYWORD(available)
796799

800+
//===----------------------------------------------------------------------===//
801+
// Interesting idenitifiers.
802+
//===----------------------------------------------------------------------===//
803+
INTERESTING_IDENTIFIER(not_interesting)
804+
INTERESTING_IDENTIFIER(FILE)
805+
INTERESTING_IDENTIFIER(jmp_buf)
806+
INTERESTING_IDENTIFIER(sigjmp_buf)
807+
INTERESTING_IDENTIFIER(ucontext_t)
808+
797809
// TODO: What to do about context-sensitive keywords like:
798810
// bycopy/byref/in/inout/oneway/out?
799811

@@ -974,3 +986,4 @@ ANNOTATION(repl_input_end)
974986
#undef TOK
975987
#undef C99_KEYWORD
976988
#undef C2X_KEYWORD
989+
#undef INTERESTING_IDENTIFIER

clang/include/clang/Basic/TokenKinds.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ enum ObjCKeywordKind {
4444
NUM_OBJC_KEYWORDS
4545
};
4646

47+
/// Provides a namespace for interesting identifers such as float_t and
48+
/// double_t.
49+
enum InterestingIdentifierKind {
50+
#define INTERESTING_IDENTIFIER(X) X,
51+
#include "clang/Basic/TokenKinds.def"
52+
NUM_INTERESTING_IDENTIFIERS
53+
};
54+
4755
/// Defines the possible values of an on-off-switch (C99 6.10.6p2).
4856
enum OnOffSwitch {
4957
OOS_ON, OOS_OFF, OOS_DEFAULT

clang/lib/Basic/Builtins.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
151151
unsigned ID = NameIt->second->getBuiltinID();
152152
if (ID != Builtin::NotBuiltin && isPredefinedLibFunction(ID) &&
153153
isInStdNamespace(ID) == InStdNamespace) {
154-
Table.get(Name).setBuiltinID(Builtin::NotBuiltin);
154+
NameIt->second->clearBuiltinID();
155155
}
156156
}
157157
}

clang/lib/Basic/IdentifierTable.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,16 @@ static void AddObjCKeyword(StringRef Name,
279279
Table.get(Name).setObjCKeywordID(ObjCID);
280280
}
281281

282+
static void AddInterestingIdentifier(StringRef Name,
283+
tok::InterestingIdentifierKind BTID,
284+
IdentifierTable &Table) {
285+
// Don't add 'not_interesting' identifier.
286+
if (BTID != tok::not_interesting) {
287+
IdentifierInfo &Info = Table.get(Name, tok::identifier);
288+
Info.setInterestingIdentifierID(BTID);
289+
}
290+
}
291+
282292
/// AddKeywords - Add all keywords to the symbol table.
283293
///
284294
void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
@@ -295,6 +305,9 @@ void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
295305
#define OBJC_AT_KEYWORD(NAME) \
296306
if (LangOpts.ObjC) \
297307
AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
308+
#define INTERESTING_IDENTIFIER(NAME) \
309+
AddInterestingIdentifier(StringRef(#NAME), tok::NAME, *this);
310+
298311
#define TESTING_KEYWORD(NAME, FLAGS)
299312
#include "clang/Basic/TokenKinds.def"
300313

clang/lib/Sema/SemaDecl.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6764,14 +6764,22 @@ Sema::ActOnTypedefNameDecl(Scope *S, DeclContext *DC, TypedefNameDecl *NewTD,
67646764
if (IdentifierInfo *II = NewTD->getIdentifier())
67656765
if (!NewTD->isInvalidDecl() &&
67666766
NewTD->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
6767-
if (II->isStr("FILE"))
6767+
switch (II->getInterestingIdentifierID()) {
6768+
case tok::InterestingIdentifierKind::FILE:
67686769
Context.setFILEDecl(NewTD);
6769-
else if (II->isStr("jmp_buf"))
6770+
break;
6771+
case tok::InterestingIdentifierKind::jmp_buf:
67706772
Context.setjmp_bufDecl(NewTD);
6771-
else if (II->isStr("sigjmp_buf"))
6773+
break;
6774+
case tok::InterestingIdentifierKind::sigjmp_buf:
67726775
Context.setsigjmp_bufDecl(NewTD);
6773-
else if (II->isStr("ucontext_t"))
6776+
break;
6777+
case tok::InterestingIdentifierKind::ucontext_t:
67746778
Context.setucontext_tDecl(NewTD);
6779+
break;
6780+
default:
6781+
break;
6782+
}
67756783
}
67766784

67776785
return NewTD;

0 commit comments

Comments
 (0)