Provide and use a safe Token::getRawIdentifier() accessor

llvm-svn: 209061
This commit is contained in:
Alp Toker 2014-05-17 04:53:25 +00:00
parent ec2748a8ad
commit 2d57cea256
8 changed files with 28 additions and 33 deletions

View File

@ -18,6 +18,7 @@
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/TemplateKinds.h"
#include "clang/Basic/TokenKinds.h"
#include "llvm/ADT/StringRef.h"
#include <cstdlib>
namespace clang {
@ -167,12 +168,12 @@ public:
PtrData = (void*) II;
}
/// getRawIdentifierData - For a raw identifier token (i.e., an identifier
/// lexed in raw mode), returns a pointer to the start of it in the text
/// buffer if known, null otherwise.
const char *getRawIdentifierData() const {
/// getRawIdentifier - For a raw identifier token (i.e., an identifier
/// lexed in raw mode), returns a reference to the text substring in the
/// buffer if known.
StringRef getRawIdentifier() const {
assert(is(tok::raw_identifier));
return reinterpret_cast<const char*>(PtrData);
return StringRef(reinterpret_cast<const char *>(PtrData), getLength());
}
void setRawIdentifierData(const char *Ptr) {
assert(is(tok::raw_identifier));

View File

@ -413,8 +413,7 @@ bool MigrationContext::rewritePropertyAttribute(StringRef fromAttr,
if (tok.isNot(tok::at)) return false;
lexer.LexFromRawLexer(tok);
if (tok.isNot(tok::raw_identifier)) return false;
if (StringRef(tok.getRawIdentifierData(), tok.getLength())
!= "property")
if (tok.getRawIdentifier() != "property")
return false;
lexer.LexFromRawLexer(tok);
if (tok.isNot(tok::l_paren)) return false;
@ -430,8 +429,7 @@ bool MigrationContext::rewritePropertyAttribute(StringRef fromAttr,
while (1) {
if (tok.isNot(tok::raw_identifier)) return false;
StringRef ident(tok.getRawIdentifierData(), tok.getLength());
if (ident == fromAttr) {
if (tok.getRawIdentifier() == fromAttr) {
if (!toAttr.empty()) {
Pass.TA.replaceText(tok.getLocation(), fromAttr, toAttr);
return true;
@ -496,8 +494,7 @@ bool MigrationContext::addPropertyAttribute(StringRef attr,
if (tok.isNot(tok::at)) return false;
lexer.LexFromRawLexer(tok);
if (tok.isNot(tok::raw_identifier)) return false;
if (StringRef(tok.getRawIdentifierData(), tok.getLength())
!= "property")
if (tok.getRawIdentifier() != "property")
return false;
lexer.LexFromRawLexer(tok);

View File

@ -382,7 +382,7 @@ unsigned Lexer::getSpelling(const Token &Tok, const char *&Buffer,
const char *TokStart = 0;
// NOTE: this has to be checked *before* testing for an IdentifierInfo.
if (Tok.is(tok::raw_identifier))
TokStart = Tok.getRawIdentifierData();
TokStart = Tok.getRawIdentifier().data();
else if (!Tok.hasUCN()) {
if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
// Just return the string from the identifier table, which is very quick.
@ -637,8 +637,7 @@ Lexer::ComputePreamble(const llvm::MemoryBuffer *Buffer,
// the raw identifier to recognize and categorize preprocessor directives.
TheLexer.LexFromRawLexer(TheTok);
if (TheTok.getKind() == tok::raw_identifier && !TheTok.needsCleaning()) {
StringRef Keyword(TheTok.getRawIdentifierData(),
TheTok.getLength());
StringRef Keyword = TheTok.getRawIdentifier();
PreambleDirectiveKind PDK
= llvm::StringSwitch<PreambleDirectiveKind>(Keyword)
.Case("include", PDK_Skipped)

View File

@ -1055,10 +1055,11 @@ retry:
L.LexFromRawLexer(LToken);
Tok.Location = LToken.getLocation().getRawEncoding();
switch (LToken.getKind()) {
case tok::raw_identifier:
Tok.StringData = LToken.getRawIdentifierData();
Tok.StringLength = LToken.getLength();
Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString())
case tok::raw_identifier: {
StringRef RI = LToken.getRawIdentifier();
Tok.StringData = RI.data();
Tok.StringLength = RI.size();
Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(RI)
.Case("config_macros", MMToken::ConfigMacros)
.Case("conflict", MMToken::Conflict)
.Case("exclude", MMToken::ExcludeKeyword)
@ -1075,6 +1076,7 @@ retry:
.Case("use", MMToken::UseKeyword)
.Default(MMToken::Identifier);
break;
}
case tok::comma:
Tok.Kind = MMToken::Comma;

View File

@ -310,9 +310,9 @@ void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
// to spell an i/e in a strange way that is another letter. Skipping this
// allows us to avoid looking up the identifier info for #define/#undef and
// other common directives.
const char *RawCharData = Tok.getRawIdentifierData();
StringRef RI = Tok.getRawIdentifier();
char FirstChar = RawCharData[0];
char FirstChar = RI[0];
if (FirstChar >= 'a' && FirstChar <= 'z' &&
FirstChar != 'i' && FirstChar != 'e') {
CurPPLexer->ParsingPreprocessorDirective = false;
@ -326,8 +326,8 @@ void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
// when skipping.
char DirectiveBuf[20];
StringRef Directive;
if (!Tok.needsCleaning() && Tok.getLength() < 20) {
Directive = StringRef(RawCharData, Tok.getLength());
if (!Tok.needsCleaning() && RI.size() < 20) {
Directive = RI;
} else {
std::string DirectiveStr = getSpelling(Tok);
unsigned IdLen = DirectiveStr.size();

View File

@ -501,14 +501,13 @@ void Preprocessor::EndSourceFile() {
/// identifier information for the token and install it into the token,
/// updating the token kind accordingly.
IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier) const {
assert(Identifier.getRawIdentifierData() != 0 && "No raw identifier data!");
assert(!Identifier.getRawIdentifier().empty() && "No raw identifier data!");
// Look up this token, see if it is a macro, or if it is a language keyword.
IdentifierInfo *II;
if (!Identifier.needsCleaning() && !Identifier.hasUCN()) {
// No cleaning needed, just use the characters from the lexed buffer.
II = getIdentifierInfo(StringRef(Identifier.getRawIdentifierData(),
Identifier.getLength()));
II = getIdentifierInfo(Identifier.getRawIdentifier());
} else {
// Cleaning needed, alloca a buffer, clean into it, then use the buffer.
SmallString<64> IdentifierBuffer;

View File

@ -292,8 +292,7 @@ static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Token Tok;
do {
lexer.LexFromRawLexer(Tok);
if (Tok.is(tok::raw_identifier) &&
StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) {
if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Loc = Tok.getLocation();
return true;
}

View File

@ -5694,14 +5694,13 @@ static void annotatePreprocessorTokens(CXTranslationUnit TU,
break;
MacroInfo *MI = 0;
if (Tok.is(tok::raw_identifier) &&
StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == "define") {
if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "define") {
if (lexNext(Lex, Tok, NextIdx, NumTokens))
break;
if (Tok.is(tok::raw_identifier)) {
StringRef Name(Tok.getRawIdentifierData(), Tok.getLength());
IdentifierInfo &II = PP.getIdentifierTable().get(Name);
IdentifierInfo &II =
PP.getIdentifierTable().get(Tok.getRawIdentifier());
SourceLocation MappedTokLoc =
CXXUnit->mapLocationToPreamble(Tok.getLocation());
MI = getMacroInfo(II, MappedTokLoc, TU);
@ -6819,8 +6818,7 @@ MacroDefinition *cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI,
if (!PPRec)
return 0;
StringRef Name(Tok.getRawIdentifierData(), Tok.getLength());
IdentifierInfo &II = PP.getIdentifierTable().get(Name);
IdentifierInfo &II = PP.getIdentifierTable().get(Tok.getRawIdentifier());
if (!II.hadMacroDefinition())
return 0;