[cfe-commits] r46335 - /cfe/trunk/Lex/Preprocessor.cpp

Chris Lattner sabre at nondot.org
Thu Jan 24 16:00:31 PST 2008


Author: lattner
Date: Thu Jan 24 18:00:30 2008
New Revision: 46335

URL: http://llvm.org/viewvc/llvm-project?rev=46335&view=rev
Log:
Change the location we return for the EOF token to actually be on the last
line of the file, in the common case where a file ends with a newline.

Modified:
    cfe/trunk/Lex/Preprocessor.cpp

Modified: cfe/trunk/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Lex/Preprocessor.cpp?rev=46335&r1=46334&r2=46335&view=diff

==============================================================================
--- cfe/trunk/Lex/Preprocessor.cpp (original)
+++ cfe/trunk/Lex/Preprocessor.cpp Thu Jan 24 18:00:30 2008
@@ -1296,10 +1296,26 @@
     // Client should lex another token.
     return false;
   }
+
+  // If the file ends with a newline, form the EOF token on the newline itself,
+  // rather than "on the line following it", which doesn't exist.  This makes
+  // diagnostics relating to the end of file include the last file that the user
+  // actually typed, which is goodness.
+  const char *EndPos = CurLexer->BufferEnd;
+  if (EndPos != CurLexer->BufferStart && 
+      (EndPos[-1] == '\n' || EndPos[-1] == '\r')) {
+    --EndPos;
+    
+    // Handle \n\r and \r\n:
+    if (EndPos != CurLexer->BufferStart && 
+        (EndPos[-1] == '\n' || EndPos[-1] == '\r') &&
+        EndPos[-1] != EndPos[0])
+      --EndPos;
+  }
   
   Result.startToken();
-  CurLexer->BufferPtr = CurLexer->BufferEnd;
-  CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
+  CurLexer->BufferPtr = EndPos;
+  CurLexer->FormTokenWithChars(Result, EndPos);
   Result.setKind(tok::eof);
   
   // We're done with the #included file.





More information about the cfe-commits mailing list