Add an overload of Preprocessor::getSpelling which takes a SmallVector and
returns a StringRef. Use it to simplify some repetitive code.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@97322 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp
index 4803c5a..976c94e 100644
--- a/lib/Lex/PPDirectives.cpp
+++ b/lib/Lex/PPDirectives.cpp
@@ -1024,13 +1024,9 @@
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/lib/Lex/PPExpressions.cpp b/lib/Lex/PPExpressions.cpp
index 3b620d0..6c92126 100644
--- a/lib/Lex/PPExpressions.cpp
+++ b/lib/Lex/PPExpressions.cpp
@@ -218,10 +218,9 @@
}
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/lib/Lex/PPMacroExpansion.cpp b/lib/Lex/PPMacroExpansion.cpp
index fd67f4c..d60cf08 100644
--- a/lib/Lex/PPMacroExpansion.cpp
+++ b/lib/Lex/PPMacroExpansion.cpp
@@ -541,13 +541,9 @@
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/lib/Lex/Pragma.cpp b/lib/Lex/Pragma.cpp
index 63b23b6..654d460 100644
--- a/lib/Lex/Pragma.cpp
+++ b/lib/Lex/Pragma.cpp
@@ -287,11 +287,8 @@
// 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/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp
index df0e702..84ce062 100644
--- a/lib/Lex/Preprocessor.cpp
+++ b/lib/Lex/Preprocessor.cpp
@@ -503,10 +503,8 @@
} 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;