From f591982bb2908a80be2aebc52c98d9befc9b5937 Mon Sep 17 00:00:00 2001 From: Andy Gibbs Date: Sat, 17 Nov 2012 19:14:53 +0000 Subject: [PATCH] Fix handling of invalid uses of the __has_warning builtin macro llvm-svn: 168265 --- clang/lib/Lex/PPMacroExpansion.cpp | 8 ++++++-- clang/test/Preprocessor/invalid-__has_warning1.c | 5 +++++ clang/test/Preprocessor/invalid-__has_warning2.c | 5 +++++ clang/test/Preprocessor/warning_tests.c | 4 +++- 4 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 clang/test/Preprocessor/invalid-__has_warning1.c create mode 100644 clang/test/Preprocessor/invalid-__has_warning2.c diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index de98d50..1f07a70 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -1291,7 +1291,10 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { StartLoc = Tok.getLocation(); IsValid = false; // Eat tokens until ')'. - do Lex(Tok); while (!(Tok.is(tok::r_paren) || Tok.is(tok::eod))); + while (Tok.isNot(tok::r_paren) + && Tok.isNot(tok::eod) + && Tok.isNot(tok::eof)) + Lex(Tok); break; } @@ -1342,7 +1345,8 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { Diag(StartLoc, diag::err_warning_check_malformed); OS << (int)Value; - Tok.setKind(tok::numeric_constant); + if (IsValid) + 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 diff --git a/clang/test/Preprocessor/invalid-__has_warning1.c b/clang/test/Preprocessor/invalid-__has_warning1.c new file mode 100644 index 0000000..40491d4 --- /dev/null +++ b/clang/test/Preprocessor/invalid-__has_warning1.c @@ -0,0 +1,5 @@ +// RUN: %clang_cc1 -verify %s + +// These must be the last lines in this test. +// expected-error@+1{{requires a parenthesized string}} expected-error@+1 2{{expected}} +int i = __has_warning( diff --git a/clang/test/Preprocessor/invalid-__has_warning2.c b/clang/test/Preprocessor/invalid-__has_warning2.c new file mode 100644 index 0000000..7d85e537 --- /dev/null +++ b/clang/test/Preprocessor/invalid-__has_warning2.c @@ -0,0 +1,5 @@ +// RUN: %clang_cc1 -verify %s + +// These must be the last lines in this test. +// expected-error@+1{{requires a parenthesized string}} expected-error@+1{{expected}} +int i = __has_warning(); diff --git a/clang/test/Preprocessor/warning_tests.c b/clang/test/Preprocessor/warning_tests.c index 3f2865c..ac0b37b 100644 --- a/clang/test/Preprocessor/warning_tests.c +++ b/clang/test/Preprocessor/warning_tests.c @@ -11,7 +11,9 @@ #warning Should have -Wparentheses #endif -#if __has_warning(-Wfoo) // expected-error {{builtin warning check macro requires a parenthesized string}} +// expected-error@+2 {{builtin warning check macro requires a parenthesized string}} +// expected-error@+1 {{expected value in expression}} +#if __has_warning(-Wfoo) #endif // expected-warning@+3 {{Not a valid warning flag}} -- 2.7.4