[cfe-commits] r166438 - in /cfe/trunk: lib/Lex/PPMacroExpansion.cpp test/Preprocessor/has_include.c

Richard Trieu rtrieu at google.com
Mon Oct 22 13:28:49 PDT 2012


Author: rtrieu
Date: Mon Oct 22 15:28:48 2012
New Revision: 166438

URL: http://llvm.org/viewvc/llvm-project?rev=166438&view=rev
Log:
Fix for PR13334.  This prevents crashes that result from badly formed
expressions involving __has_include

Modified:
    cfe/trunk/lib/Lex/PPMacroExpansion.cpp
    cfe/trunk/test/Preprocessor/has_include.c

Modified: cfe/trunk/lib/Lex/PPMacroExpansion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPMacroExpansion.cpp?rev=166438&r1=166437&r2=166438&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPMacroExpansion.cpp (original)
+++ cfe/trunk/lib/Lex/PPMacroExpansion.cpp Mon Oct 22 15:28:48 2012
@@ -925,22 +925,30 @@
 static bool EvaluateHasIncludeCommon(Token &Tok,
                                      IdentifierInfo *II, Preprocessor &PP,
                                      const DirectoryLookup *LookupFrom) {
-  SourceLocation LParenLoc;
+  // Save the location of the current token.  If a '(' is later found, use
+  // that location.  If no, use the end of this location instead.
+  SourceLocation LParenLoc = Tok.getLocation();
 
   // Get '('.
   PP.LexNonComment(Tok);
 
   // Ensure we have a '('.
   if (Tok.isNot(tok::l_paren)) {
-    PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName();
-    return false;
-  }
-
-  // Save '(' location for possible missing ')' message.
-  LParenLoc = Tok.getLocation();
+    // No '(', use end of last token.
+    LParenLoc = PP.getLocForEndOfToken(LParenLoc);
+    PP.Diag(LParenLoc, diag::err_pp_missing_lparen) << II->getName();
+    // If the next token looks like a filename or the start of one,
+    // assume it is and process it as such.
+    if (!Tok.is(tok::angle_string_literal) && !Tok.is(tok::string_literal) &&
+        !Tok.is(tok::less))
+      return false;
+  } else {
+    // Save '(' location for possible missing ')' message.
+    LParenLoc = Tok.getLocation();
 
-  // Get the file name.
-  PP.getCurrentLexer()->LexIncludeFilename(Tok);
+    // Get the file name.
+    PP.getCurrentLexer()->LexIncludeFilename(Tok);
+  }
 
   // Reserve a buffer to get the spelling.
   SmallString<128> FilenameBuffer;
@@ -965,8 +973,11 @@
     // This could be a <foo/bar.h> file coming from a macro expansion.  In this
     // case, glue the tokens together into FilenameBuffer and interpret those.
     FilenameBuffer.push_back('<');
-    if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc))
+    if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) {
+      // Let the caller know a <eod> was found by changing the Token kind.
+      Tok.setKind(tok::eod);
       return false;   // Found <eod> but no ">"?  Diagnostic already emitted.
+    }
     Filename = FilenameBuffer.str();
     break;
   default:
@@ -974,12 +985,15 @@
     return false;
   }
 
+  SourceLocation FilenameLoc = Tok.getLocation();
+
   // Get ')'.
   PP.LexNonComment(Tok);
 
   // Ensure we have a trailing ).
   if (Tok.isNot(tok::r_paren)) {
-    PP.Diag(Tok.getLocation(), diag::err_pp_missing_rparen) << II->getName();
+    PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_missing_rparen)
+        << II->getName();
     PP.Diag(LParenLoc, diag::note_matching) << "(";
     return false;
   }
@@ -1252,7 +1266,8 @@
     else
       Value = EvaluateHasIncludeNext(Tok, II, *this);
     OS << (int)Value;
-    Tok.setKind(tok::numeric_constant);
+    if (Tok.is(tok::r_paren))
+      Tok.setKind(tok::numeric_constant);
   } else if (II == Ident__has_warning) {
     // The argument should be a parenthesized string literal.
     // The argument to these builtins should be a parenthesized identifier.

Modified: cfe/trunk/test/Preprocessor/has_include.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/has_include.c?rev=166438&r1=166437&r2=166438&view=diff
==============================================================================
--- cfe/trunk/test/Preprocessor/has_include.c (original)
+++ cfe/trunk/test/Preprocessor/has_include.c Mon Oct 22 15:28:48 2012
@@ -67,7 +67,7 @@
 // Try badly formed expressions.
 // FIXME: We can recover better in almost all of these cases. (PR13335)
 
-// expected-error at +1 {{missing '(' after '__has_include'}} expected-error at +1 {{expected end of line}}
+// expected-error at +1 {{missing '(' after '__has_include'}}
 #if __has_include "stdint.h")
 #endif
 
@@ -83,11 +83,11 @@
 #if __has_include)
 #endif
 
-// expected-error at +1 {{missing '(' after '__has_include'}} expected-error at +1 {{token is not a valid binary operator in a preprocessor subexpression}}
+// expected-error at +1 {{missing '(' after '__has_include'}}
 #if __has_include<stdint.h>)
 #endif
 
-// expected-error at +1 {{expected "FILENAME" or <FILENAME>}} expected-warning at +1 {{missing terminating '"' character}}
+// expected-error at +1 {{expected "FILENAME" or <FILENAME>}} expected-warning at +1 {{missing terminating '"' character}}  expected-error at +1 {{invalid token at start of a preprocessor expression}}
 #if __has_include("stdint.h)
 #endif
 
@@ -99,11 +99,25 @@
 #if __has_include(stdint.h>)
 #endif
 
+// expected-error at +1 {{missing '(' after '__has_include'}}
+__has_include
+
+// expected-error at +1 {{missing ')' after '__has_include'}} // expected-error at +1 {{expected value in expression}}  // expected-note at +1 {{to match this '('}}
+#if __has_include("stdint.h"
+#endif
 
-// FIXME: These test cases cause the compiler to crash. (PR13334)
-//#if __has_include("stdint.h"
-//#if __has_include(
-//#if __has_include
-//#if __has_include(<stdint.h>
-//#if __has_include(<stdint.h)
+// expected-error at +1 {{expected "FILENAME" or <FILENAME>}} // expected-error at +1 {{expected value in expression}}
+#if __has_include(
+#endif
 
+// expected-error at +1 {{missing '(' after '__has_include'}} // expected-error at +1 {{expected value in expression}}
+#if __has_include
+#endif
+
+// expected-error at +1 {{missing ')' after '__has_include'}}  // expected-error at +1 {{expected value in expression}}  // expected-note at +1 {{to match this '('}}
+#if __has_include(<stdint.h>
+#endif
+
+// expected-error at +1 {{expected "FILENAME" or <FILENAME>}} // expected-error at +1 {{expected value in expression}}
+#if __has_include(<stdint.h)
+#endif





More information about the cfe-commits mailing list