summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@googlemail.com>2010-02-27 13:44:12 +0000
committerBenjamin Kramer <benny.kra@googlemail.com>2010-02-27 13:44:12 +0000
commit0a1abd4088b3d8d42567301fda17a37b1cd7b8d6 (patch)
treeddf0f69ad36c259b8901d7925f66bf5bb8707561
parent09597b461d806e7853aee6506a7bb5303dd5f24c (diff)
downloadbcm5719-llvm-0a1abd4088b3d8d42567301fda17a37b1cd7b8d6.tar.gz
bcm5719-llvm-0a1abd4088b3d8d42567301fda17a37b1cd7b8d6.zip
Add an overload of Preprocessor::getSpelling which takes a SmallVector and
returns a StringRef. Use it to simplify some repetitive code. llvm-svn: 97322
-rw-r--r--clang/include/clang/Lex/Preprocessor.h18
-rw-r--r--clang/lib/Frontend/PrintPreprocessedOutput.cpp7
-rw-r--r--clang/lib/Lex/PPDirectives.cpp8
-rw-r--r--clang/lib/Lex/PPExpressions.cpp7
-rw-r--r--clang/lib/Lex/PPMacroExpansion.cpp8
-rw-r--r--clang/lib/Lex/Pragma.cpp5
-rw-r--r--clang/lib/Lex/Preprocessor.cpp6
-rw-r--r--clang/lib/Parse/ParseDeclCXX.cpp6
-rw-r--r--clang/lib/Sema/SemaExpr.cpp8
9 files changed, 34 insertions, 39 deletions
diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h
index dedbbd868a9..f7e83d587fe 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -570,6 +570,24 @@ public:
/// 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 {
diff --git a/clang/lib/Frontend/PrintPreprocessedOutput.cpp b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
index 43deaee8c1d..774372c8693 100644
--- a/clang/lib/Frontend/PrintPreprocessedOutput.cpp
+++ b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
@@ -67,12 +67,7 @@ static void PrintMacroDefinition(const IdentifierInfo &II, const MacroInfo &MI,
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);
}
}
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 4803c5ab85d..976c94eda36 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -1024,13 +1024,9 @@ void Preprocessor::HandleIncludeDirective(Token &IncludeTok,
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
diff --git a/clang/lib/Lex/PPExpressions.cpp b/clang/lib/Lex/PPExpressions.cpp
index 3b620d09345..6c9212632f1 100644
--- a/clang/lib/Lex/PPExpressions.cpp
+++ b/clang/lib/Lex/PPExpressions.cpp
@@ -218,10 +218,9 @@ static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
}
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.
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
index fd67f4c9124..d60cf0804f5 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -541,13 +541,9 @@ static bool EvaluateHasIncludeCommon(bool &Result, Token &Tok,
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
diff --git a/clang/lib/Lex/Pragma.cpp b/clang/lib/Lex/Pragma.cpp
index 63b23b6d5c4..654d4606a95 100644
--- a/clang/lib/Lex/Pragma.cpp
+++ b/clang/lib/Lex/Pragma.cpp
@@ -287,11 +287,8 @@ void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
// 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
diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp
index df0e702ab44..84ce062a6ab 100644
--- a/clang/lib/Lex/Preprocessor.cpp
+++ b/clang/lib/Lex/Preprocessor.cpp
@@ -503,10 +503,8 @@ IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
} 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;
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index dbbfb9518b3..cb0e5a7f876 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -167,9 +167,7 @@ Parser::DeclPtrTy Parser::ParseLinkage(ParsingDeclSpec &DS,
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();
@@ -177,7 +175,7 @@ Parser::DeclPtrTy Parser::ParseLinkage(ParsingDeclSpec &DS,
DeclPtrTy LinkageSpec
= Actions.ActOnStartLinkageSpecification(CurScope,
/*FIXME: */SourceLocation(),
- Loc, LangBufPtr, StrSize,
+ Loc, Lang.data(), Lang.size(),
Tok.is(tok::l_brace)? Tok.getLocation()
: SourceLocation());
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index c25d1195d8b..6c1377f3cb0 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -1664,12 +1664,10 @@ Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc,
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();
OpenPOWER on IntegriCloud