From 0833dd0675c25cbb35671c7a2006d511d5c77ce3 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 17 Nov 2010 07:21:13 +0000 Subject: [PATCH] propagate preprocessor out of StringLiteralParser. It is now possible to create one without a preprocessor. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@119476 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Lex/LiteralSupport.h | 12 +++++++++++- lib/Lex/LiteralSupport.cpp | 28 ++++++++++++++++++---------- lib/Sema/SemaChecking.cpp | 4 ++-- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/include/clang/Lex/LiteralSupport.h b/include/clang/Lex/LiteralSupport.h index 69a66657cc..52e027fffc 100644 --- a/include/clang/Lex/LiteralSupport.h +++ b/include/clang/Lex/LiteralSupport.h @@ -140,7 +140,6 @@ public: /// wide string analysis and Translation Phase #6 (concatenation of string /// literals) (C99 5.1.1.2p1). class StringLiteralParser { - Preprocessor &PP; const SourceManager &SM; const LangOptions &Features; const TargetInfo &Target; @@ -154,6 +153,14 @@ class StringLiteralParser { public: StringLiteralParser(const Token *StringToks, unsigned NumStringToks, Preprocessor &PP, bool Complain = true); + StringLiteralParser(const Token *StringToks, unsigned NumStringToks, + const SourceManager &sm, const LangOptions &features, + const TargetInfo &target, Diagnostic *diags = 0) + : SM(sm), Features(features), Target(target), Diags(diags) { + init(StringToks, NumStringToks); + } + + bool hadError; bool AnyWide; bool Pascal; @@ -173,6 +180,9 @@ public: /// If the Diagnostics pointer is non-null, then this will do semantic /// checking of the string literal and emit errors and warnings. unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo) const; + +private: + void init(const Token *StringToks, unsigned NumStringToks); }; } // end namespace clang diff --git a/lib/Lex/LiteralSupport.cpp b/lib/Lex/LiteralSupport.cpp index 6f0584b182..26bef2fdcc 100644 --- a/lib/Lex/LiteralSupport.cpp +++ b/lib/Lex/LiteralSupport.cpp @@ -827,9 +827,13 @@ CharLiteralParser::CharLiteralParser(const char *begin, const char *end, /// StringLiteralParser:: StringLiteralParser(const Token *StringToks, unsigned NumStringToks, - Preprocessor &pp, bool Complain) - : PP(pp), SM(PP.getSourceManager()), Features(PP.getLangOptions()), + Preprocessor &PP, bool Complain) + : SM(PP.getSourceManager()), Features(PP.getLangOptions()), Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() : 0) { + init(StringToks, NumStringToks); +} + +void StringLiteralParser::init(const Token *StringToks, unsigned NumStringToks){ // Scan all of the string portions, remember the max individual token length, // computing a bound on the concatenated string length, and see whether any // piece is a wide-string. If any of the string portions is a wide-string @@ -893,8 +897,9 @@ StringLiteralParser(const Token *StringToks, unsigned NumStringToks, // that ThisTokBuf points to a buffer that is big enough for the whole token // and 'spelled' tokens can only shrink. bool StringInvalid = false; - unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf, - &StringInvalid); + unsigned ThisTokLen = + Preprocessor::getSpelling(StringToks[i], ThisTokBuf, SM, Features, + &StringInvalid); if (StringInvalid) { hadError = 1; continue; @@ -979,19 +984,22 @@ StringLiteralParser(const Token *StringToks, unsigned NumStringToks, ResultBuf[0] /= wchar_tByteWidth; // Verify that pascal strings aren't too large. - if (GetStringLength() > 256 && Complain) { - PP.Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long) - << SourceRange(StringToks[0].getLocation(), - StringToks[NumStringToks-1].getLocation()); + if (GetStringLength() > 256) { + if (Diags) + Diags->Report(FullSourceLoc(StringToks[0].getLocation(), SM), + diag::err_pascal_string_too_long) + << SourceRange(StringToks[0].getLocation(), + StringToks[NumStringToks-1].getLocation()); hadError = 1; return; } - } else if (Complain) { + } else if (Diags) { // Complain if this string literal has too many characters. unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509; if (GetNumStringChars() > MaxChars) - PP.Diag(StringToks[0].getLocation(), diag::ext_string_too_long) + Diags->Report(FullSourceLoc(StringToks[0].getLocation(), SM), + diag::ext_string_too_long) << GetNumStringChars() << MaxChars << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0) << SourceRange(StringToks[0].getLocation(), diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index fb231731b9..0fa1fc18e3 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -83,8 +83,8 @@ SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL, TheLexer.LexFromRawLexer(TheTok); // Use the StringLiteralParser to compute the length of the string in bytes. - StringLiteralParser SLP(&TheTok, 1, PP, /*Complain=*/false); - // PP.getSourceManager(), PP.getLangOptions(), PP.getTargetInfo()); + StringLiteralParser SLP(&TheTok, 1, PP.getSourceManager(), + PP.getLangOptions(), PP.getTargetInfo()); unsigned TokNumBytes = SLP.GetStringLength(); // If the byte is in this token, return the location of the byte. -- 2.40.0