]> granicus.if.org Git - clang/commitdiff
Fix the root cause of PR2750 instead of the side effect.
authorChris Lattner <sabre@nondot.org>
Mon, 29 Sep 2008 23:12:31 +0000 (23:12 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 29 Sep 2008 23:12:31 +0000 (23:12 +0000)
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

lib/Lex/LiteralSupport.cpp
lib/Lex/PPMacroExpansion.cpp

index 8c2f2aff47478cbec7fcd00d406523451140925c..a66aaa2010a3a10da0c200209c115c167c7c22bb 100644 (file)
@@ -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;
index fb9b613f5c8e7f0b528042791f0a83185e60bc2b..43df71a2cc8c8aa8a98e9d9512eb699a24fa6f22 100644 (file)
@@ -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!");
   }