From: Chris Lattner Date: Fri, 17 Apr 2009 23:30:53 +0000 (+0000) Subject: #line is allowed to have macros that expand to nothing after them. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ab82f41b217ce588a9456c0b4411f219d3ed0df8;p=clang #line is allowed to have macros that expand to nothing after them. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@69401 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Lex/Preprocessor.h b/include/clang/Lex/Preprocessor.h index 9e4278515b..2184acf0e3 100644 --- a/include/clang/Lex/Preprocessor.h +++ b/include/clang/Lex/Preprocessor.h @@ -580,8 +580,9 @@ public: void HandleDirective(Token &Result); /// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If - /// not, emit a diagnostic and consume up until the eom. - void CheckEndOfDirective(const char *Directive); + /// not, emit a diagnostic and consume up until the eom. If EnableMacros is + /// true, then we consider macros that expand to zero tokens as being ok. + void CheckEndOfDirective(const char *Directive, bool EnableMacros = false); /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the /// current line until the tok::eom token is found. diff --git a/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp index 742076956d..ce86d0edca 100644 --- a/lib/Lex/PPDirectives.cpp +++ b/lib/Lex/PPDirectives.cpp @@ -101,12 +101,17 @@ void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) { } /// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If -/// not, emit a diagnostic and consume up until the eom. -void Preprocessor::CheckEndOfDirective(const char *DirType) { +/// not, emit a diagnostic and consume up until the eom. If EnableMacros is +/// true, then we consider macros that expand to zero tokens as being ok. +void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) { Token Tmp; - // Lex unexpanded tokens: macros might expand to zero tokens, causing us to - // miss diagnosing invalid lines. - LexUnexpandedToken(Tmp); + // Lex unexpanded tokens for most directives: macros might expand to zero + // tokens, causing us to miss diagnosing invalid lines. Some directives (like + // #line) allow empty macros. + if (EnableMacros) + Lex(Tmp); + else + LexUnexpandedToken(Tmp); // There should be no tokens after the directive, but we allow them as an // extension. @@ -694,8 +699,9 @@ void Preprocessor::HandleLineDirective(Token &Tok) { FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString(), Literal.GetStringLength()); - // Verify that there is nothing after the string, other than EOM. - CheckEndOfDirective("line"); + // Verify that there is nothing after the string, other than EOM. Because + // of C99 6.10.4p5, macros that expand to empty tokens are ok. + CheckEndOfDirective("line", true); } SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID); diff --git a/test/Preprocessor/line-directive.c b/test/Preprocessor/line-directive.c index 66efae94d5..1dd8b292d2 100644 --- a/test/Preprocessor/line-directive.c +++ b/test/Preprocessor/line-directive.c @@ -60,3 +60,11 @@ typedef int z1; // ok typedef int w; // expected-note {{previous definition is here}} typedef int w; // expected-error {{redefinition of typedef 'w' is invalid in C}} + + +// This should not produce an "extra tokens at end of #line directive" warning, +// because #line is allowed to contain expanded tokens. +#define EMPTY() +#line 2 "foo.c" EMPTY( ) +#line 2 "foo.c" NONEMPTY( ) // expected-warning{{extra tokens at end of #line directive}} +