[cfe-commits] r69401 - in /cfe/trunk: include/clang/Lex/Preprocessor.h lib/Lex/PPDirectives.cpp test/Preprocessor/line-directive.c

Chris Lattner sabre at nondot.org
Fri Apr 17 16:30:53 PDT 2009


Author: lattner
Date: Fri Apr 17 18:30:53 2009
New Revision: 69401

URL: http://llvm.org/viewvc/llvm-project?rev=69401&view=rev
Log:
#line is allowed to have macros that expand to nothing after them.

Modified:
    cfe/trunk/include/clang/Lex/Preprocessor.h
    cfe/trunk/lib/Lex/PPDirectives.cpp
    cfe/trunk/test/Preprocessor/line-directive.c

Modified: cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=69401&r1=69400&r2=69401&view=diff

==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Fri Apr 17 18:30:53 2009
@@ -580,8 +580,9 @@
   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.

Modified: cfe/trunk/lib/Lex/PPDirectives.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPDirectives.cpp?rev=69401&r1=69400&r2=69401&view=diff

==============================================================================
--- cfe/trunk/lib/Lex/PPDirectives.cpp (original)
+++ cfe/trunk/lib/Lex/PPDirectives.cpp Fri Apr 17 18:30:53 2009
@@ -101,12 +101,17 @@
 }
 
 /// 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 @@
     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);

Modified: cfe/trunk/test/Preprocessor/line-directive.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/line-directive.c?rev=69401&r1=69400&r2=69401&view=diff

==============================================================================
--- cfe/trunk/test/Preprocessor/line-directive.c (original)
+++ cfe/trunk/test/Preprocessor/line-directive.c Fri Apr 17 18:30:53 2009
@@ -60,3 +60,11 @@
 
 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}}
+





More information about the cfe-commits mailing list