diff --git a/clang/Lex/Lexer.cpp b/clang/Lex/Lexer.cpp index 7f50f350e50d..a9905f339160 100644 --- a/clang/Lex/Lexer.cpp +++ b/clang/Lex/Lexer.cpp @@ -904,23 +904,32 @@ bool Lexer::LexEndOfFile(LexerToken &Result, const char *CurPtr) { return true; // Have a token. } - // If we aren't in raw mode, issue diagnostics. If we are in raw mode, let the - // code that put us into raw mode do this: there are multiple possible reasons - // for raw mode, and not all want these diagnostics. - if (!LexingRawMode) { - // If we are in a #if directive, emit an error. - while (!ConditionalStack.empty()) { - PP.Diag(ConditionalStack.back().IfLoc, - diag::err_pp_unterminated_conditional); - ConditionalStack.pop_back(); - } - - // If the file was empty or didn't end in a newline, issue a pedwarn. - if (CurPtr[-1] != '\n' && CurPtr[-1] != '\r') - Diag(BufferEnd, diag::ext_no_newline_eof); + // If we are in raw mode, return this event as an EOF token. Let the caller + // that put us in raw mode handle the event. + if (LexingRawMode) { + Result.StartToken(); + BufferPtr = BufferEnd; + FormTokenWithChars(Result, BufferEnd); + Result.SetKind(tok::eof); + return true; } + // Otherwise, issue diagnostics for unterminated #if and missing newline. + + // If we are in a #if directive, emit an error. + while (!ConditionalStack.empty()) { + PP.Diag(ConditionalStack.back().IfLoc, + diag::err_pp_unterminated_conditional); + ConditionalStack.pop_back(); + } + + // If the file was empty or didn't end in a newline, issue a pedwarn. + if (CurPtr[-1] != '\n' && CurPtr[-1] != '\r') + Diag(BufferEnd, diag::ext_no_newline_eof); + BufferPtr = CurPtr; + + // Finally, let the preprocessor handle this. return PP.HandleEndOfFile(Result); } diff --git a/clang/Lex/Preprocessor.cpp b/clang/Lex/Preprocessor.cpp index 932fae74f8fb..f302b6770977 100644 --- a/clang/Lex/Preprocessor.cpp +++ b/clang/Lex/Preprocessor.cpp @@ -1009,19 +1009,6 @@ bool Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) { assert(!CurMacroExpander && "Ending a file when currently in a macro!"); - // If we are in a #if 0 block skipping tokens, and we see the end of the file, - // this is an error condition. Just return the EOF token up to - // SkipExcludedConditionalBlock. The code that enabled skipping will issue - // errors for the unterminated #if's on the conditional stack if it is - // interested. - if (isSkipping()) { - Result.StartToken(); - CurLexer->BufferPtr = CurLexer->BufferEnd; - CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd); - Result.SetKind(tok::eof); - return true; - } - // See if this file had a controlling macro. if (CurLexer) { // Not ending a macro, ignore it. if (const IdentifierInfo *ControllingMacro = diff --git a/clang/test/Preprocessor/macro_paste_bcpl_comment.c b/clang/test/Preprocessor/macro_paste_bcpl_comment.c new file mode 100644 index 000000000000..9a864d520cc5 --- /dev/null +++ b/clang/test/Preprocessor/macro_paste_bcpl_comment.c @@ -0,0 +1,5 @@ +// RUN: clang %s -Eonly 2>&1 | grep error + +#define COMM1 / ## / +COMM1 +