Lexer::ReadToEndOfLine: Only build the string if it's actually used and do so in a less malloc-intensive way.

llvm-svn: 157064
This commit is contained in:
Benjamin Kramer 2012-05-18 19:32:16 +00:00
parent 4b63d2ae1d
commit e5fbc6c85d
3 changed files with 13 additions and 14 deletions

View File

@ -200,7 +200,7 @@ public:
/// ReadToEndOfLine - Read the rest of the current preprocessor line as an /// ReadToEndOfLine - Read the rest of the current preprocessor line as an
/// uninterpreted string. This switches the lexer out of directive mode. /// uninterpreted string. This switches the lexer out of directive mode.
std::string ReadToEndOfLine(); void ReadToEndOfLine(SmallVectorImpl<char> *Result = 0);
/// Diag - Forwarding function for diagnostics. This translate a source /// Diag - Forwarding function for diagnostics. This translate a source

View File

@ -2286,10 +2286,9 @@ bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
/// ReadToEndOfLine - Read the rest of the current preprocessor line as an /// ReadToEndOfLine - Read the rest of the current preprocessor line as an
/// uninterpreted string. This switches the lexer out of directive mode. /// uninterpreted string. This switches the lexer out of directive mode.
std::string Lexer::ReadToEndOfLine() { void Lexer::ReadToEndOfLine(SmallVectorImpl<char> *Result) {
assert(ParsingPreprocessorDirective && ParsingFilename == false && assert(ParsingPreprocessorDirective && ParsingFilename == false &&
"Must be in a preprocessing directive!"); "Must be in a preprocessing directive!");
std::string Result;
Token Tmp; Token Tmp;
// CurPtr - Cache BufferPtr in an automatic variable. // CurPtr - Cache BufferPtr in an automatic variable.
@ -2298,7 +2297,8 @@ std::string Lexer::ReadToEndOfLine() {
char Char = getAndAdvanceChar(CurPtr, Tmp); char Char = getAndAdvanceChar(CurPtr, Tmp);
switch (Char) { switch (Char) {
default: default:
Result += Char; if (Result)
Result->push_back(Char);
break; break;
case 0: // Null. case 0: // Null.
// Found end of file? // Found end of file?
@ -2306,11 +2306,12 @@ std::string Lexer::ReadToEndOfLine() {
if (isCodeCompletionPoint(CurPtr-1)) { if (isCodeCompletionPoint(CurPtr-1)) {
PP->CodeCompleteNaturalLanguage(); PP->CodeCompleteNaturalLanguage();
cutOffLexing(); cutOffLexing();
return Result; return;
} }
// Nope, normal character, continue. // Nope, normal character, continue.
Result += Char; if (Result)
Result->push_back(Char);
break; break;
} }
// FALL THROUGH. // FALL THROUGH.
@ -2329,8 +2330,8 @@ std::string Lexer::ReadToEndOfLine() {
} }
assert(Tmp.is(tok::eod) && "Unexpected token!"); assert(Tmp.is(tok::eod) && "Unexpected token!");
// Finally, we're done, return the string we found. // Finally, we're done;
return Result; return;
} }
} }
} }

View File

@ -1018,15 +1018,13 @@ void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
// tokens. For example, this is allowed: "#warning ` 'foo". GCC does // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
// collapse multiple consequtive white space between tokens, but this isn't // collapse multiple consequtive white space between tokens, but this isn't
// specified by the standard. // specified by the standard.
std::string Message = CurLexer->ReadToEndOfLine(); SmallString<128> Message;
CurLexer->ReadToEndOfLine(&Message);
// Find the first non-whitespace character, so that we can make the // Find the first non-whitespace character, so that we can make the
// diagnostic more succinct. // diagnostic more succinct.
StringRef Msg(Message); StringRef Msg = Message.str().ltrim(" ");
size_t i = Msg.find_first_not_of(' ');
if (i < Msg.size())
Msg = Msg.substr(i);
if (isWarning) if (isWarning)
Diag(Tok, diag::pp_hash_warning) << Msg; Diag(Tok, diag::pp_hash_warning) << Msg;
else else