From 3144749f8bf9bbf7c027f2161a930bff80ad6f72 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Thu, 3 May 2012 17:50:32 +0000 Subject: [PATCH] In StringLiteralParser::init, make sure we emit an error when failing to lex the string, as suggested by Eli. Part of rdar://11305263. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@156081 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticLexKinds.td | 1 + include/clang/Lex/LiteralSupport.h | 1 + lib/Lex/LiteralSupport.cpp | 27 +++++++++++------------ 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/include/clang/Basic/DiagnosticLexKinds.td b/include/clang/Basic/DiagnosticLexKinds.td index 670283ef1a..3bad7b9012 100644 --- a/include/clang/Basic/DiagnosticLexKinds.td +++ b/include/clang/Basic/DiagnosticLexKinds.td @@ -177,6 +177,7 @@ def err_bad_character_encoding : Error< def warn_bad_character_encoding : ExtWarn< "illegal character encoding in character literal">, InGroup>; +def err_lexing_string : Error<"failure when lexing a string">; //===----------------------------------------------------------------------===// diff --git a/include/clang/Lex/LiteralSupport.h b/include/clang/Lex/LiteralSupport.h index 7e7f82f05f..bbce62d1d4 100644 --- a/include/clang/Lex/LiteralSupport.h +++ b/include/clang/Lex/LiteralSupport.h @@ -232,6 +232,7 @@ private: void init(const Token *StringToks, unsigned NumStringToks); bool CopyStringFragment(StringRef Fragment); bool DiagnoseBadString(const Token& Tok); + void DiagnoseLexingError(SourceLocation Loc); }; } // end namespace clang diff --git a/lib/Lex/LiteralSupport.cpp b/lib/Lex/LiteralSupport.cpp index b01fc1f8c6..c7120f2bef 100644 --- a/lib/Lex/LiteralSupport.cpp +++ b/lib/Lex/LiteralSupport.cpp @@ -1037,10 +1037,8 @@ StringLiteralParser(const Token *StringToks, unsigned NumStringToks, void StringLiteralParser::init(const Token *StringToks, unsigned NumStringToks){ // The literal token may have come from an invalid source location (e.g. due // to a PCH error), in which case the token length will be 0. - if (NumStringToks == 0 || StringToks[0].getLength() < 2) { - hadError = true; - return; - } + if (NumStringToks == 0 || StringToks[0].getLength() < 2) + return DiagnoseLexingError(SourceLocation()); // Scan all of the string portions, remember the max individual token length, // computing a bound on the concatenated string length, and see whether any @@ -1057,10 +1055,8 @@ void StringLiteralParser::init(const Token *StringToks, unsigned NumStringToks){ // Implement Translation Phase #6: concatenation of string literals /// (C99 5.1.1.2p1). The common case is only one string fragment. for (unsigned i = 1; i != NumStringToks; ++i) { - if (StringToks[i].getLength() < 2) { - hadError = true; - return; - } + if (StringToks[i].getLength() < 2) + return DiagnoseLexingError(StringToks[i].getLocation()); // The string could be shorter than this if it needs cleaning, but this is a // reasonable bound, which is all we need. @@ -1123,10 +1119,8 @@ void StringLiteralParser::init(const Token *StringToks, unsigned NumStringToks){ unsigned ThisTokLen = Lexer::getSpelling(StringToks[i], ThisTokBuf, SM, Features, &StringInvalid); - if (StringInvalid) { - hadError = true; - continue; - } + if (StringInvalid) + return DiagnoseLexingError(StringToks[i].getLocation()); const char *ThisTokBegin = ThisTokBuf; const char *ThisTokEnd = ThisTokBuf+ThisTokLen; @@ -1195,8 +1189,7 @@ void StringLiteralParser::init(const Token *StringToks, unsigned NumStringToks){ if (ThisTokBuf[0] != '"') { // The file may have come from PCH and then changed after loading the // PCH; Fail gracefully. - hadError = true; - continue; + return DiagnoseLexingError(StringToks[i].getLocation()); } ++ThisTokBuf; // skip " @@ -1354,6 +1347,12 @@ bool StringLiteralParser::DiagnoseBadString(const Token &Tok) { return !NoErrorOnBadEncoding; } +void StringLiteralParser::DiagnoseLexingError(SourceLocation Loc) { + hadError = true; + if (Diags) + Diags->Report(Loc, diag::err_lexing_string); +} + /// getOffsetOfStringByte - This function returns the offset of the /// specified byte of the string data represented by Token. This handles /// advancing over escape sequences in the string. -- 2.40.0