From 4e270380c150b3086875edd10e72f677b95f7e14 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Sat, 17 Nov 2012 20:25:54 +0000 Subject: [PATCH] Fix crash on end-of-file after \ in a char literal, fixes PR14369. This makes LexCharConstant() look more like LexStringLiteral(), which doesn't have this bug. Add tests for eof after \ for several other cases. llvm-svn: 168269 --- clang/lib/Lex/Lexer.cpp | 14 ++++++++------ clang/test/Lexer/eof-char.c | 8 ++++++++ clang/test/Lexer/eof-file.c | 8 ++++++++ clang/test/Lexer/eof-string.c | 8 ++++++++ 4 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 clang/test/Lexer/eof-char.c create mode 100644 clang/test/Lexer/eof-file.c create mode 100644 clang/test/Lexer/eof-string.c diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp index 354a44d..4698e28 100644 --- a/clang/lib/Lex/Lexer.cpp +++ b/clang/lib/Lex/Lexer.cpp @@ -1823,16 +1823,18 @@ void Lexer::LexCharConstant(Token &Result, const char *CurPtr, while (C != '\'') { // Skip escaped characters. - if (C == '\\') { - // Skip the escaped character. - getAndAdvanceChar(CurPtr, Result); - } else if (C == '\n' || C == '\r' || // Newline. - (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. + if (C == '\\') + C = getAndAdvanceChar(CurPtr, Result); + + if (C == '\n' || C == '\r' || // Newline. + (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. if (!isLexingRawMode() && !LangOpts.AsmPreprocessor) Diag(BufferPtr, diag::ext_unterminated_char); FormTokenWithChars(Result, CurPtr-1, tok::unknown); return; - } else if (C == 0) { + } + + if (C == 0) { if (isCodeCompletionPoint(CurPtr-1)) { PP->CodeCompleteNaturalLanguage(); FormTokenWithChars(Result, CurPtr-1, tok::unknown); diff --git a/clang/test/Lexer/eof-char.c b/clang/test/Lexer/eof-char.c new file mode 100644 index 0000000..42ee6ea --- /dev/null +++ b/clang/test/Lexer/eof-char.c @@ -0,0 +1,8 @@ +// RUN: %clang_cc1 %s -verify -fsyntax-only +// vim: set binary noeol: + +// This file intentionally ends without a \n on the last line. Make sure your +// editor doesn't add one. + +// expected-warning@+1{{missing terminating ' character}} expected-error@+1{{expected expression}} expected-error@+1{{expected ';'}} +char c = '\ \ No newline at end of file diff --git a/clang/test/Lexer/eof-file.c b/clang/test/Lexer/eof-file.c new file mode 100644 index 0000000..43c300c --- /dev/null +++ b/clang/test/Lexer/eof-file.c @@ -0,0 +1,8 @@ +// RUN: %clang_cc1 %s -verify -fsyntax-only +// vim: set binary noeol: + +// This file intentionally ends without a \n on the last line. Make sure your +// editor doesn't add one. + +// expected-error@+1{{expected expression}} expected-error@+1{{expected ';'}} +char c = \ \ No newline at end of file diff --git a/clang/test/Lexer/eof-string.c b/clang/test/Lexer/eof-string.c new file mode 100644 index 0000000..e1a60a4 --- /dev/null +++ b/clang/test/Lexer/eof-string.c @@ -0,0 +1,8 @@ +// RUN: %clang_cc1 %s -verify -fsyntax-only +// vim: set binary noeol: + +// This file intentionally ends without a \n on the last line. Make sure your +// editor doesn't add one. + +// expected-warning@+1{{missing terminating '"' character}} expected-error@+1{{expected expression}} expected-error@+1{{expected ';'}} +char c = "\ \ No newline at end of file -- 2.7.4