From: Benjamin Kramer Date: Sun, 29 Mar 2015 19:05:27 +0000 (+0000) Subject: [lex] Provide a valid token when __has_include is found outside of a pp directive X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5d9ea6db7d20ccd7c178fbc3eb86e37a43f09b3a;p=clang [lex] Provide a valid token when __has_include is found outside of a pp directive ExpandBuiltinMacro would strip the identifier and downstream users crash when they encounter an identifier token with nullptr identifier info. Found by afl-fuzz. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@233497 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Lex/PPMacroExpansion.cpp b/lib/Lex/PPMacroExpansion.cpp index f2baddd0b8..3ceba05401 100644 --- a/lib/Lex/PPMacroExpansion.cpp +++ b/lib/Lex/PPMacroExpansion.cpp @@ -1073,6 +1073,9 @@ static bool EvaluateHasIncludeCommon(Token &Tok, // These expressions are only allowed within a preprocessor directive. if (!PP.isParsingIfOrElifDirective()) { PP.Diag(LParenLoc, diag::err_pp_directive_required) << II->getName(); + // Return a valid identifier token. + assert(Tok.is(tok::identifier)); + Tok.setIdentifierInfo(II); return false; } diff --git a/test/SemaCXX/typo-correction.cpp b/test/SemaCXX/typo-correction.cpp index 324900150c..174b1403e2 100644 --- a/test/SemaCXX/typo-correction.cpp +++ b/test/SemaCXX/typo-correction.cpp @@ -634,3 +634,9 @@ namespace testArraySubscriptIndex { } }; } + +namespace crash_has_include { +int has_include(int); // expected-note {{'has_include' declared here}} +// expected-error@+1 {{__has_include must be used within a preprocessing directive}} +int foo = __has_include(42); // expected-error {{use of undeclared identifier '__has_include'; did you mean 'has_include'?}} +}