aboutsummaryrefslogtreecommitdiff
path: root/Lex/Preprocessor.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2008-01-25 00:00:30 +0000
committerChris Lattner <sabre@nondot.org>2008-01-25 00:00:30 +0000
commit09cf90f6d6d87c32f0611b4d6ca563ed1ab05c15 (patch)
tree0d0354e3619afa64f2da57c54122f205943554bb /Lex/Preprocessor.cpp
parent13099923a2209899034e2ea0ca0e88f27fb7a8b2 (diff)
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@46335 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'Lex/Preprocessor.cpp')
-rw-r--r--Lex/Preprocessor.cpp20
1 files changed, 18 insertions, 2 deletions
diff --git a/Lex/Preprocessor.cpp b/Lex/Preprocessor.cpp
index d831093268..34b5fe2183 100644
--- a/Lex/Preprocessor.cpp
+++ b/Lex/Preprocessor.cpp
@@ -1296,10 +1296,26 @@ bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
// 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.