]> granicus.if.org Git - clang/commitdiff
propagate preprocessor out of StringLiteralParser. It is now
authorChris Lattner <sabre@nondot.org>
Wed, 17 Nov 2010 07:21:13 +0000 (07:21 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 17 Nov 2010 07:21:13 +0000 (07:21 +0000)
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
lib/Lex/LiteralSupport.cpp
lib/Sema/SemaChecking.cpp

index 69a66657ccc50931fbe5005468605689c149f18f..52e027fffcb733fd528a07e1ec439f8ff41523a5 100644 (file)
@@ -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
index 6f0584b1822f1743338aca2428e7e96622cf180c..26bef2fdcc4ec3bb723d187b20669e69c3feb726 100644 (file)
@@ -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(),
index fb231731b9805b5b4bcd136bb58d4a12494a2713..0fa1fc18e309ddd8c18f4cfcc5e0757e61ab8795 100644 (file)
@@ -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.