/// if an internal buffer is returned.
unsigned getSpelling(const Token &Tok, const char *&Buffer) const;
+ /// getSpelling - This method is used to get the spelling of a token into a
+ /// SmallVector. Note that the returned StringRef may not point to the
+ /// supplied buffer if a copy can be avoided.
+ llvm::StringRef getSpelling(const Token &Tok,
+ llvm::SmallVectorImpl<char> &Buffer) const {
+ // Try the fast path.
+ if (const IdentifierInfo *II = Tok.getIdentifierInfo())
+ return II->getName();
+
+ // Resize the buffer if we need to copy into it.
+ if (!Tok.needsCleaning())
+ Buffer.resize(Tok.getLength());
+
+ const char *Ptr = Buffer.data();
+ unsigned Len = getSpelling(Tok, Ptr);
+ return llvm::StringRef(Ptr, Len);
+ }
+
/// getSpellingOfSingleCharacterNumericConstant - Tok is a numeric constant
/// with length 1, return the character.
char getSpellingOfSingleCharacterNumericConstant(const Token &Tok) const {
if (I->hasLeadingSpace())
OS << ' ';
- // Make sure we have enough space in the spelling buffer.
- if (I->getLength() > SpellingBuffer.size())
- SpellingBuffer.resize(I->getLength());
- const char *Buffer = SpellingBuffer.data();
- unsigned SpellingLen = PP.getSpelling(*I, Buffer);
- OS.write(Buffer, SpellingLen);
+ OS << PP.getSpelling(*I, SpellingBuffer);
}
}
return;
case tok::angle_string_literal:
- case tok::string_literal: {
- FilenameBuffer.resize(FilenameTok.getLength());
- const char *FilenameStart = &FilenameBuffer[0];
- unsigned Len = getSpelling(FilenameTok, FilenameStart);
- Filename = llvm::StringRef(FilenameStart, Len);
+ case tok::string_literal:
+ Filename = getSpelling(FilenameTok, FilenameBuffer);
break;
- }
case tok::less:
// This could be a <foo/bar.h> file coming from a macro expansion. In this
}
case tok::char_constant: { // 'x'
llvm::SmallString<32> CharBuffer;
- CharBuffer.resize(PeekTok.getLength());
- const char *ThisTokBegin = &CharBuffer[0];
- unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
- CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
+ llvm::StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer);
+
+ CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
PeekTok.getLocation(), PP);
if (Literal.hadError())
return true; // A diagnostic was already emitted.
return false;
case tok::angle_string_literal:
- case tok::string_literal: {
- FilenameBuffer.resize(Tok.getLength());
- const char *FilenameStart = &FilenameBuffer[0];
- unsigned Len = PP.getSpelling(Tok, FilenameStart);
- Filename = llvm::StringRef(FilenameStart, Len);
+ case tok::string_literal:
+ Filename = PP.getSpelling(Tok, FilenameBuffer);
break;
- }
case tok::less:
// This could be a <foo/bar.h> file coming from a macro expansion. In this
// Reserve a buffer to get the spelling.
llvm::SmallString<128> FilenameBuffer;
- FilenameBuffer.resize(FilenameTok.getLength());
+ llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer);
- const char *FilenameStart = &FilenameBuffer[0];
- unsigned Len = getSpelling(FilenameTok, FilenameStart);
- llvm::StringRef Filename(FilenameStart, Len);
bool isAngled =
GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
// If GetIncludeFilenameSpelling set the start ptr to null, there was an
} else {
// Cleaning needed, alloca a buffer, clean into it, then use the buffer.
llvm::SmallVector<char, 64> IdentifierBuffer;
- IdentifierBuffer.resize(Identifier.getLength());
- const char *TmpBuf = &IdentifierBuffer[0];
- unsigned Size = getSpelling(Identifier, TmpBuf);
- II = getIdentifierInfo(llvm::StringRef(TmpBuf, Size));
+ llvm::StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer);
+ II = getIdentifierInfo(CleanedStr);
}
Identifier.setIdentifierInfo(II);
return II;
assert(Tok.is(tok::string_literal) && "Not a string literal!");
llvm::SmallVector<char, 8> LangBuffer;
// LangBuffer is guaranteed to be big enough.
- LangBuffer.resize(Tok.getLength());
- const char *LangBufPtr = &LangBuffer[0];
- unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
+ llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer);
SourceLocation Loc = ConsumeStringToken();
DeclPtrTy LinkageSpec
= Actions.ActOnStartLinkageSpecification(CurScope,
/*FIXME: */SourceLocation(),
- Loc, LangBufPtr, StrSize,
+ Loc, Lang.data(), Lang.size(),
Tok.is(tok::l_brace)? Tok.getLocation()
: SourceLocation());
Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
llvm::SmallString<16> CharBuffer;
- CharBuffer.resize(Tok.getLength());
- const char *ThisTokBegin = &CharBuffer[0];
- unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
+ llvm::StringRef ThisTok = PP.getSpelling(Tok, CharBuffer);
- CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
- Tok.getLocation(), PP);
+ CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(),
+ PP);
if (Literal.hadError())
return ExprError();