diff options
author | Douglas Gregor <dgregor@apple.com> | 2010-03-16 22:30:13 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2010-03-16 22:30:13 +0000 |
commit | 453091cc2082e207ea2c2dda645a9bc01b37fb0c (patch) | |
tree | 96026d218bae429d6706d3783cd94e86b578dc47 /lib/Lex/MacroArgs.cpp | |
parent | a98c27ba83d25d878473ed8c6a34b40b27d323fd (diff) |
Audit all Preprocessor::getSpelling() callers, improving failure
recovery for those that need it.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@98689 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Lex/MacroArgs.cpp')
-rw-r--r-- | lib/Lex/MacroArgs.cpp | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/lib/Lex/MacroArgs.cpp b/lib/Lex/MacroArgs.cpp index 2f1a34c832..89f6368a27 100644 --- a/lib/Lex/MacroArgs.cpp +++ b/lib/Lex/MacroArgs.cpp @@ -208,24 +208,31 @@ Token MacroArgs::StringifyArgument(const Token *ArgToks, if (Tok.is(tok::string_literal) || // "foo" Tok.is(tok::wide_string_literal) || // L"foo" Tok.is(tok::char_constant)) { // 'x' and L'x'. - std::string Str = Lexer::Stringify(PP.getSpelling(Tok)); - Result.append(Str.begin(), Str.end()); + bool Invalid = false; + std::string TokStr = PP.getSpelling(Tok, &Invalid); + if (!Invalid) { + std::string Str = Lexer::Stringify(TokStr); + Result.append(Str.begin(), Str.end()); + } } else { // Otherwise, just append the token. Do some gymnastics to get the token // in place and avoid copies where possible. unsigned CurStrLen = Result.size(); Result.resize(CurStrLen+Tok.getLength()); const char *BufPtr = &Result[CurStrLen]; - unsigned ActualTokLen = PP.getSpelling(Tok, BufPtr); - - // If getSpelling returned a pointer to an already uniqued version of the - // string instead of filling in BufPtr, memcpy it onto our string. - if (BufPtr != &Result[CurStrLen]) - memcpy(&Result[CurStrLen], BufPtr, ActualTokLen); - - // If the token was dirty, the spelling may be shorter than the token. - if (ActualTokLen != Tok.getLength()) - Result.resize(CurStrLen+ActualTokLen); + bool Invalid = false; + unsigned ActualTokLen = PP.getSpelling(Tok, BufPtr, &Invalid); + + if (!Invalid) { + // If getSpelling returned a pointer to an already uniqued version of + // the string instead of filling in BufPtr, memcpy it onto our string. + if (BufPtr != &Result[CurStrLen]) + memcpy(&Result[CurStrLen], BufPtr, ActualTokLen); + + // If the token was dirty, the spelling may be shorter than the token. + if (ActualTokLen != Tok.getLength()) + Result.resize(CurStrLen+ActualTokLen); + } } } |