From: Chris Lattner Date: Mon, 29 Sep 2008 23:12:31 +0000 (+0000) Subject: Fix the root cause of PR2750 instead of the side effect. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4411f46050216a139ab2fc7ff145ec384d11ec7f;p=clang Fix the root cause of PR2750 instead of the side effect. NumericLiteral parser is not careful about overrun because it should never be possible. It implicitly expects that its input matched the regex for pp-constant. Because of this, it knows it can't be pointing to a prefix of something that looks like a number. This is all fine, except that __LINE__ does not prevent implicit concatenation from happening. Fix __LINE__ to not do this. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@56818 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Lex/LiteralSupport.cpp b/lib/Lex/LiteralSupport.cpp index 8c2f2aff47..a66aaa2010 100644 --- a/lib/Lex/LiteralSupport.cpp +++ b/lib/Lex/LiteralSupport.cpp @@ -224,7 +224,7 @@ NumericLiteralParser(const char *begin, const char *end, saw_period = true; s = SkipDigits(s); } - if (s != ThisTokEnd && (*s == 'e' || *s == 'E')) { // exponent + if ((*s == 'e' || *s == 'E')) { // exponent const char *Exponent = s; s++; saw_exponent = true; diff --git a/lib/Lex/PPMacroExpansion.cpp b/lib/Lex/PPMacroExpansion.cpp index fb9b613f5c..43df71a2cc 100644 --- a/lib/Lex/PPMacroExpansion.cpp +++ b/lib/Lex/PPMacroExpansion.cpp @@ -445,12 +445,15 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { Tok.clearFlag(Token::NeedsCleaning); if (II == Ident__LINE__) { - // __LINE__ expands to a simple numeric value. - sprintf(TmpBuffer, "%u", SourceMgr.getLogicalLineNumber(Tok.getLocation())); - unsigned Length = strlen(TmpBuffer); + // __LINE__ expands to a simple numeric value. Add a space after it so that + // it will tokenize as a number (and not run into stuff after it in the temp + // buffer). + sprintf(TmpBuffer, "%u ", + SourceMgr.getLogicalLineNumber(Tok.getLocation())); + unsigned Length = strlen(TmpBuffer)-1; Tok.setKind(tok::numeric_constant); Tok.setLength(Length); - Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation())); + Tok.setLocation(CreateString(TmpBuffer, Length+1, Tok.getLocation())); } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) { SourceLocation Loc = Tok.getLocation(); if (II == Ident__BASE_FILE__) { @@ -489,9 +492,11 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { for (; Loc.isValid(); ++Depth) Loc = SourceMgr.getIncludeLoc(Loc); - // __INCLUDE_LEVEL__ expands to a simple numeric value. - sprintf(TmpBuffer, "%u", Depth); - unsigned Length = strlen(TmpBuffer); + // __INCLUDE_LEVEL__ expands to a simple numeric value. Add a space after + // it so that it will tokenize as a number (and not run into stuff after it + // in the temp buffer). + sprintf(TmpBuffer, "%u ", Depth); + unsigned Length = strlen(TmpBuffer)-1; Tok.setKind(tok::numeric_constant); Tok.setLength(Length); Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation())); @@ -523,7 +528,7 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { TmpBuffer[Len-1] = '"'; // Replace the newline with a quote. Tok.setKind(tok::string_literal); Tok.setLength(Len); - Tok.setLocation(CreateString(TmpBuffer, Len, Tok.getLocation())); + Tok.setLocation(CreateString(TmpBuffer, Len+1, Tok.getLocation())); } else { assert(0 && "Unknown identifier!"); }