From: Argyrios Kyrtzidis Date: Wed, 11 Aug 2010 22:55:12 +0000 (+0000) Subject: -Make TokenID of IdentifierInfo read-only, remove setTokenID(). X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=646395bbcaa849c94bc2a3246c71d809ca719f01;p=clang -Make TokenID of IdentifierInfo read-only, remove setTokenID(). -There are 2 instances that change the TokenID for GNU libstdc++ 4.2 compatibility. To handler those cases introduce a RevertedTokenID bitfield, RevertTokenIDToIdentifier() and hasRevertedTokenIDToIdentifier() methods. Store the bitfield in PCH. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@110868 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/IdentifierTable.h b/include/clang/Basic/IdentifierTable.h index c8642a1ac5..cbc2d01202 100644 --- a/include/clang/Basic/IdentifierTable.h +++ b/include/clang/Basic/IdentifierTable.h @@ -61,7 +61,9 @@ class IdentifierInfo { bool NeedsHandleIdentifier : 1; // See "RecomputeNeedsHandleIdentifier". bool IsFromPCH : 1; // True if identfier first appeared in a PCH // and wasn't modified since. - // 8 bits left in 32-bit word. + bool RevertedTokenID : 1; // True if RevertTokenIDToIdentifier was + // called. + // 7 bits left in 32-bit word. void *FETokenInfo; // Managed by the language front-end. llvm::StringMapEntry *Entry; @@ -130,11 +132,21 @@ public: IsFromPCH = false; } - /// get/setTokenID - If this is a source-language token (e.g. 'for'), this API + /// getTokenID - If this is a source-language token (e.g. 'for'), this API /// can be used to cause the lexer to map identifiers to source-language /// tokens. tok::TokenKind getTokenID() const { return (tok::TokenKind)TokenID; } - void setTokenID(tok::TokenKind ID) { TokenID = ID; } + + /// \brief True if RevertTokenIDToIdentifier() was called. + bool hasRevertedTokenIDToIdentifier() const { return RevertedTokenID; } + + /// \brief Revert TokenID to tok::identifier; used for GNU libstdc++ 4.2 + /// compatibility. + void RevertTokenIDToIdentifier() { + assert(TokenID != tok::identifier && "Already at tok::identifier"); + TokenID = tok::identifier; + RevertedTokenID = true; + } /// getPPKeywordID - Return the preprocessor keyword ID for this identifier. /// For example, "define" will return tok::pp_define. @@ -323,6 +335,12 @@ public: return *II; } + IdentifierInfo &get(llvm::StringRef Name, tok::TokenKind TokenCode) { + IdentifierInfo &II = get(Name); + II.TokenID = TokenCode; + return II; + } + IdentifierInfo &get(const char *NameStart, const char *NameEnd) { return get(llvm::StringRef(NameStart, NameEnd-NameStart)); } diff --git a/lib/Basic/IdentifierTable.cpp b/lib/Basic/IdentifierTable.cpp index 1f0c52bdc1..c7b16c99c9 100644 --- a/lib/Basic/IdentifierTable.cpp +++ b/lib/Basic/IdentifierTable.cpp @@ -35,6 +35,7 @@ IdentifierInfo::IdentifierInfo() { IsCPPOperatorKeyword = false; NeedsHandleIdentifier = false; IsFromPCH = false; + RevertedTokenID = false; FETokenInfo = 0; Entry = 0; } @@ -101,8 +102,7 @@ static void AddKeyword(llvm::StringRef Keyword, // Don't add this keyword if disabled in this language. if (AddResult == 0) return; - IdentifierInfo &Info = Table.get(Keyword); - Info.setTokenID(TokenCode); + IdentifierInfo &Info = Table.get(Keyword, TokenCode); Info.setIsExtensionToken(AddResult == 1); } @@ -111,8 +111,7 @@ static void AddKeyword(llvm::StringRef Keyword, static void AddCXXOperatorKeyword(llvm::StringRef Keyword, tok::TokenKind TokenCode, IdentifierTable &Table) { - IdentifierInfo &Info = Table.get(Keyword); - Info.setTokenID(TokenCode); + IdentifierInfo &Info = Table.get(Keyword, TokenCode); Info.setIsCPlusPlusOperatorKeyword(); } diff --git a/lib/Frontend/PCHReader.cpp b/lib/Frontend/PCHReader.cpp index 68acbb2faf..a09b89deea 100644 --- a/lib/Frontend/PCHReader.cpp +++ b/lib/Frontend/PCHReader.cpp @@ -657,6 +657,8 @@ public: unsigned Bits = ReadUnalignedLE16(d); bool CPlusPlusOperatorKeyword = Bits & 0x01; Bits >>= 1; + bool HasRevertedTokenIDToIdentifier = Bits & 0x01; + Bits >>= 1; bool Poisoned = Bits & 0x01; Bits >>= 1; bool ExtensionToken = Bits & 0x01; @@ -677,7 +679,9 @@ public: Reader.SetIdentifierInfo(ID, II); // Set or check the various bits in the IdentifierInfo structure. - // FIXME: Load token IDs lazily, too? + // Token IDs are read-only. + if (HasRevertedTokenIDToIdentifier) + II->RevertTokenIDToIdentifier(); II->setObjCOrBuiltinID(ObjCOrBuiltinID); assert(II->isExtensionToken() == ExtensionToken && "Incorrect extension token flag"); diff --git a/lib/Frontend/PCHWriter.cpp b/lib/Frontend/PCHWriter.cpp index 0eed4ccd71..6712122682 100644 --- a/lib/Frontend/PCHWriter.cpp +++ b/lib/Frontend/PCHWriter.cpp @@ -1847,6 +1847,7 @@ public: Bits = (Bits << 1) | unsigned(hasMacroDefinition); Bits = (Bits << 1) | unsigned(II->isExtensionToken()); Bits = (Bits << 1) | unsigned(II->isPoisoned()); + Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier()); Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword()); clang::io::Emit16(Out, Bits); diff --git a/lib/Parse/ParseDeclCXX.cpp b/lib/Parse/ParseDeclCXX.cpp index 55e677dbaa..5f404ac430 100644 --- a/lib/Parse/ParseDeclCXX.cpp +++ b/lib/Parse/ParseDeclCXX.cpp @@ -661,7 +661,7 @@ void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, // token sequence "struct __is_pod", make __is_pod into a normal // identifier rather than a keyword, to allow libstdc++ 4.2 to work // properly. - Tok.getIdentifierInfo()->setTokenID(tok::identifier); + Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); Tok.setKind(tok::identifier); } @@ -671,7 +671,7 @@ void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, // token sequence "struct __is_empty", make __is_empty into a normal // identifier rather than a keyword, to allow libstdc++ 4.2 to work // properly. - Tok.getIdentifierInfo()->setTokenID(tok::identifier); + Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); Tok.setKind(tok::identifier); } diff --git a/test/PCH/cxx-traits.cpp b/test/PCH/cxx-traits.cpp new file mode 100644 index 0000000000..69c64758ae --- /dev/null +++ b/test/PCH/cxx-traits.cpp @@ -0,0 +1,8 @@ +// Test this without pch. +// RUN: %clang_cc1 -include %S/cxx-traits.h -fsyntax-only -verify %s + +// RUN: %clang_cc1 -x c++-header -emit-pch -o %t %S/cxx-traits.h +// RUN: %clang_cc1 -include-pch %t -fsyntax-only -verify %s + +bool _Is_pod_comparator = __is_pod::__value; +bool _Is_empty_check = __is_empty::__value; diff --git a/test/PCH/cxx-traits.h b/test/PCH/cxx-traits.h new file mode 100644 index 0000000000..62722ab179 --- /dev/null +++ b/test/PCH/cxx-traits.h @@ -0,0 +1,11 @@ +// Header for PCH test cxx-traits.cpp + +template +struct __is_pod { + enum { __value }; +}; + +template +struct __is_empty { + enum { __value }; +};