r224238 - Preprocessor: Recover instead of mutating a token in ExpandBuiltinMacro

David Majnemer david.majnemer at gmail.com
Mon Dec 15 01:03:59 PST 2014


Author: majnemer
Date: Mon Dec 15 03:03:58 2014
New Revision: 224238

URL: http://llvm.org/viewvc/llvm-project?rev=224238&view=rev
Log:
Preprocessor: Recover instead of mutating a token in ExpandBuiltinMacro

We would CreateString on arbitrary garbage instead of just skipping to
the end of the builtin macro.  Eventually, this would cause us to crash
because we would end up replacing the contents of a character token with
a numeric literal.

This fixes PR21825.

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

Modified: cfe/trunk/lib/Lex/PPMacroExpansion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPMacroExpansion.cpp?rev=224238&r1=224237&r2=224238&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPMacroExpansion.cpp (original)
+++ cfe/trunk/lib/Lex/PPMacroExpansion.cpp Mon Dec 15 03:03:58 2014
@@ -1415,6 +1415,10 @@ void Preprocessor::ExpandBuiltinMacro(To
         if (IsScopeValid && Tok.is(tok::r_paren))
           IsValid = true;
       }
+      // Eat tokens until ')'.
+      while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eod) &&
+             Tok.isNot(tok::eof))
+        LexUnexpandedToken(Tok);
     }
 
     int Value = 0;
@@ -1441,9 +1445,10 @@ void Preprocessor::ExpandBuiltinMacro(To
       Value = HasFeature(*this, FeatureII);
     }
 
+    if (!IsValid)
+      return;
     OS << Value;
-    if (IsValid)
-      Tok.setKind(tok::numeric_constant);
+    Tok.setKind(tok::numeric_constant);
   } else if (II == Ident__has_include ||
              II == Ident__has_include_next) {
     // The argument to these two builtins should be a parenthesized
@@ -1507,9 +1512,10 @@ void Preprocessor::ExpandBuiltinMacro(To
                               WarningName.substr(2), Diags);
     } while (false);
 
+    if (!IsValid)
+      return;
     OS << (int)Value;
-    if (IsValid)
-      Tok.setKind(tok::numeric_constant);
+    Tok.setKind(tok::numeric_constant);
   } else if (II == Ident__building_module) {
     // The argument to this builtin should be an identifier. The
     // builtin evaluates to 1 when that identifier names the module we are

Modified: cfe/trunk/test/Preprocessor/feature_tests.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/feature_tests.c?rev=224238&r1=224237&r2=224238&view=diff
==============================================================================
--- cfe/trunk/test/Preprocessor/feature_tests.c (original)
+++ cfe/trunk/test/Preprocessor/feature_tests.c Mon Dec 15 03:03:58 2014
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 %s -triple=i686-apple-darwin9
+// RUN: %clang_cc1 %s -triple=i686-apple-darwin9 -verify -DVERIFY
 // RUN: %clang_cc1 %s -E -triple=i686-apple-darwin9
 #ifndef __has_feature
 #error Should have __has_feature
@@ -53,3 +53,10 @@
 #if !HAS_BUILTIN(MY_ALIAS_BUILTIN) || !HAS_FEATURE(MY_ALIAS_FEATURE)
 #error Expansion should have occurred
 #endif
+
+#ifdef VERIFY
+// expected-error at +2 {{builtin feature check macro requires a parenthesized identifier}}
+// expected-error at +1 {{expected value in expression}}
+#if __has_feature('x')
+#endif
+#endif





More information about the cfe-commits mailing list