From: Richard Smith Date: Thu, 21 Jun 2012 00:35:03 +0000 (+0000) Subject: Do not complain about junk on the end of a #endif in a skipped block. Such junk X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=986f317944658ecb1c8c279c1c8a0079396d42bf;p=clang Do not complain about junk on the end of a #endif in a skipped block. Such junk is permitted by all relevant language standards. Patch by Andy Gibbs! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@158883 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp index 5c03c97b7c..8f79796c58 100644 --- a/lib/Lex/PPDirectives.cpp +++ b/lib/Lex/PPDirectives.cpp @@ -317,7 +317,6 @@ void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, } else if (Directive[0] == 'e') { StringRef Sub = Directive.substr(1); if (Sub == "ndif") { // "endif" - CheckEndOfDirective("endif"); PPConditionalInfo CondInfo; CondInfo.WasSkipping = true; // Silence bogus warning. bool InCond = CurPPLexer->popConditionalLevel(CondInfo); @@ -326,9 +325,12 @@ void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, // If we popped the outermost skipping block, we're done skipping! if (!CondInfo.WasSkipping) { + CheckEndOfDirective("endif"); if (Callbacks) Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc); break; + } else { + DiscardUntilEndOfDirective(); } } else if (Sub == "lse") { // "else". // #else directive in a skipping conditional. If not in some other diff --git a/test/Preprocessor/disabled-cond-diags2.c b/test/Preprocessor/disabled-cond-diags2.c new file mode 100644 index 0000000000..d0629aee4f --- /dev/null +++ b/test/Preprocessor/disabled-cond-diags2.c @@ -0,0 +1,27 @@ +// RUN: %clang_cc1 -Eonly -verify %s + +#if 0 +#if 1 +#endif junk // shouldn't produce diagnostics +#endif + +#if 0 +#endif junk // expected-warning{{extra tokens at end of #endif directive}} + +#if 1 junk // expected-error{{token is not a valid binary operator in a preprocessor subexpression}} +#X // shouldn't produce diagnostics (block #if condition not valid, so skipped) +#else +#X // expected-error{{invalid preprocessing directive}} +#endif + +#if 0 +// diagnostics should not be produced until final #endif +#X +#include +#if 1 junk +#else junk +#endif junk +#line -2 +#error +#warning +#endif junk // expected-warning{{extra tokens at end of #endif directive}}