From e3739cdb5fe9e51b464c8defce598d290466a712 Mon Sep 17 00:00:00 2001 From: Alp Toker Date: Fri, 13 Dec 2013 17:04:55 +0000 Subject: [PATCH] Fix raw lex crash and -frewrite-includes noeol-at-eof failure Raw lexers don't have a preprocessor so we need to null check. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@197245 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Lex/Lexer.cpp | 3 ++- lib/Rewrite/Frontend/InclusionRewriter.cpp | 10 +++++----- test/Frontend/rewrite-includes-eof.c | 4 ++++ 3 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 test/Frontend/rewrite-includes-eof.c diff --git a/lib/Lex/Lexer.cpp b/lib/Lex/Lexer.cpp index c071455da6..009f18ad23 100644 --- a/lib/Lex/Lexer.cpp +++ b/lib/Lex/Lexer.cpp @@ -2460,7 +2460,8 @@ bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) { FormTokenWithChars(Result, CurPtr, tok::eod); // Restore comment saving mode, in case it was disabled for directive. - resetExtendedTokenMode(); + if (PP) + resetExtendedTokenMode(); return true; // Have a token. } diff --git a/lib/Rewrite/Frontend/InclusionRewriter.cpp b/lib/Rewrite/Frontend/InclusionRewriter.cpp index a2e0db6961..0e3e53498b 100644 --- a/lib/Rewrite/Frontend/InclusionRewriter.cpp +++ b/lib/Rewrite/Frontend/InclusionRewriter.cpp @@ -77,7 +77,7 @@ private: void OutputContentUpTo(const MemoryBuffer &FromFile, unsigned &WriteFrom, unsigned WriteTo, StringRef EOL, int &lines, - bool EnsureNewline = false); + bool EnsureNewline); void CommentOutDirective(Lexer &DirectivesLex, const Token &StartToken, const MemoryBuffer &FromFile, StringRef EOL, unsigned &NextToWrite, int &Lines); @@ -250,7 +250,7 @@ void InclusionRewriter::CommentOutDirective(Lexer &DirectiveLex, StringRef EOL, unsigned &NextToWrite, int &Line) { OutputContentUpTo(FromFile, NextToWrite, - SM.getFileOffset(StartToken.getLocation()), EOL, Line); + SM.getFileOffset(StartToken.getLocation()), EOL, Line, false); Token DirectiveToken; do { DirectiveLex.LexFromRawLexer(DirectiveToken); @@ -258,7 +258,7 @@ void InclusionRewriter::CommentOutDirective(Lexer &DirectiveLex, OS << "#if 0 /* expanded by -frewrite-includes */" << EOL; OutputContentUpTo(FromFile, NextToWrite, SM.getFileOffset(DirectiveToken.getLocation()) + DirectiveToken.getLength(), - EOL, Line); + EOL, Line, true); OS << "#endif /* expanded by -frewrite-includes */" << EOL; } @@ -462,12 +462,12 @@ bool InclusionRewriter::Process(FileID FileId, // Replace the macro with (0) or (1), followed by the commented // out macro for reference. OutputContentUpTo(FromFile, NextToWrite, SM.getFileOffset(Loc), - EOL, Line); + EOL, Line, false); OS << '(' << (int) HasFile << ")/*"; OutputContentUpTo(FromFile, NextToWrite, SM.getFileOffset(RawToken.getLocation()) + RawToken.getLength(), - EOL, Line); + EOL, Line, false); OS << "*/"; } } while (RawToken.isNot(tok::eod)); diff --git a/test/Frontend/rewrite-includes-eof.c b/test/Frontend/rewrite-includes-eof.c new file mode 100644 index 0000000000..af7fd89e3d --- /dev/null +++ b/test/Frontend/rewrite-includes-eof.c @@ -0,0 +1,4 @@ +// RUN: %clang_cc1 -E -frewrite-includes -I %S/Inputs %s +// expected-no-diagnostics +// Note: there's no newline at the end of this C file. +#include "rewrite-includes-bom.h" \ No newline at end of file -- 2.50.1