]> granicus.if.org Git - clang/commitdiff
Add support for Pascal strings.
authorAnders Carlsson <andersca@mac.com>
Mon, 15 Oct 2007 02:50:23 +0000 (02:50 +0000)
committerAnders Carlsson <andersca@mac.com>
Mon, 15 Oct 2007 02:50:23 +0000 (02:50 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42974 91177308-0d34-0410-b5e6-96231b3b80d8

AST/StmtPrinter.cpp
Driver/clang.cpp
Lex/LiteralSupport.cpp
Sema/SemaExpr.cpp
clang.xcodeproj/project.pbxproj
include/clang/Basic/DiagnosticKinds.def
include/clang/Basic/LangOptions.h
include/clang/Lex/LiteralSupport.h

index 15582547c27a321444e0472bf6d503a19fbce9b8..ff4b54bd8d1d6bd39429b0238a4b7a18df8bb61c 100644 (file)
@@ -419,7 +419,7 @@ void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
 void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
   if (Str->isWide()) OS << 'L';
   OS << '"';
-  
+
   // FIXME: this doesn't print wstrings right.
   for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
     switch (Str->getStrData()[i]) {
index 6d9b6b0e4662fa9271acdc66e1fd11a228b04007..6e8dd9e967287aa7c373634e171e0a5a390c496b 100644 (file)
@@ -271,10 +271,15 @@ NoOperatorNames("fno-operator-names",
                 llvm::cl::desc("Do not treat C++ operator name keywords as "
                                "synonyms for operators"));
 
+static llvm::cl::opt<bool>
+PascalStrings("fpascal-strings",
+              llvm::cl::desc("Recognize and construct Pascal-style "
+                             "string literals"));
 // FIXME: add:
 //   -ansi
 //   -trigraphs
 //   -fdollars-in-identifiers
+//   -fpascal-strings
 static void InitializeLanguageStandard(LangOptions &Options) {
   if (LangStd == lang_unspecified) {
     // Based on the base language, pick one.
@@ -325,6 +330,7 @@ static void InitializeLanguageStandard(LangOptions &Options) {
   
   Options.Trigraphs = 1; // -trigraphs or -ansi
   Options.DollarIdents = 1;  // FIXME: Really a target property.
+  Options.PascalStrings = PascalStrings;
 }
 
 //===----------------------------------------------------------------------===//
index 90a05f68b45082be720561e8e1b8c319a9c47159..c0027f266cac42375cedcba7ff437199daeeb268 100644 (file)
@@ -599,6 +599,8 @@ StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
   // wide strings as appropriate.
   ResultPtr = &ResultBuf[0];   // Next byte to fill in.
   
+  Pascal = false;
+  
   for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
     const char *ThisTokBuf = &TokenBuf[0];
     // Get the spelling of the token, which eliminates trigraphs, etc.  We know
@@ -619,6 +621,19 @@ StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
     assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
     ++ThisTokBuf;
     
+    // Check if this is a pascal string
+    if (pp.getLangOptions().PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
+        ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
+      
+      // If the \p sequence is found in the first token, we have a pascal string
+      // Otherwise, if we already have a pascal string, ignore the first \p
+      if (i == 0) {
+        ++ThisTokBuf;
+        Pascal = true;
+      } else if (Pascal)
+        ThisTokBuf += 2;
+    }
+      
     while (ThisTokBuf != ThisTokEnd) {
       // Is this a span of non-escape characters?
       if (ThisTokBuf[0] != '\\') {
@@ -665,4 +680,7 @@ StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
     for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
     *ResultPtr++ = 0;
   }
+    
+  if (Pascal) 
+    ResultBuf[0] = ResultPtr-&ResultBuf[0]-1;
 }
index 904230f82c2868d8634504e66db0f91fdd822572..471312baf06cb6c02a8432b57b2a78f1c13d3103 100644 (file)
@@ -46,11 +46,22 @@ Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
     StringTokLocs.push_back(StringToks[i].getLocation());
   
   // FIXME: handle wchar_t
-  QualType t = Context.getPointerType(Context.CharTy);
+  QualType t;
+  
+  if (Literal.Pascal) 
+    t = Context.getPointerType(Context.UnsignedCharTy);
+  else
+    t = Context.getPointerType(Context.CharTy);
+  
+  if (Literal.Pascal && Literal.GetStringLength() > 256)
+    return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long,
+                SourceRange(StringToks[0].getLocation(),
+                            StringToks[NumStringToks-1].getLocation()));
   
   // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
   return new StringLiteral(Literal.GetString(), Literal.GetStringLength(), 
-                           Literal.AnyWide, t, StringToks[0].getLocation(),
+                           Literal.AnyWide, t, 
+                           StringToks[0].getLocation(),
                            StringToks[NumStringToks-1].getLocation());
 }
 
index b88c381faa0b21298a15082c3f3667bb0e3dacc7..75d938806b0068e9faf478a9935977559b88f772 100644 (file)
                08FB7793FE84155DC02AAC07 /* Project object */ = {
                        isa = PBXProject;
                        buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */;
+                       compatibilityVersion = "Xcode 2.4";
                        hasScannedForEncodings = 1;
                        mainGroup = 08FB7794FE84155DC02AAC07 /* clang */;
                        projectDirPath = "";
index 2a8900c02578199f0d51aeea2dcf8b4a429a2bf6..e2b055b783ffaa94640233b40ce67ae042d9e5a5 100644 (file)
@@ -771,6 +771,8 @@ DIAG(err_typecheck_choose_expr_requires_constant, ERROR,
      "'__builtin_choose_expr' requires a constant expression")
 DIAG(warn_unused_expr, WARNING,
      "expression result unused")
+DIAG(err_pascal_string_too_long, ERROR,
+    "Pascal string is too long")
 
 // CHECK: printf format string errors
 DIAG(warn_printf_not_string_constant, WARNING,
index a2d07f0e448e678566f42504b140b1541f25e25b..afd91e2405a0d4e5213442ccf18b5cf6d778803b 100644 (file)
@@ -34,10 +34,12 @@ struct LangOptions {
   unsigned ObjC1             : 1;  // Objective C 1 support enabled.
   unsigned ObjC2             : 1;  // Objective C 2 support enabled.
   
+  unsigned PascalStrings     : 1;  // Allow Pascal strings
+  
   LangOptions() {
     Trigraphs = BCPLComment = DollarIdents = Digraphs = ObjC1 = ObjC2 = 0;
     C99 = Microsoft = CPlusPlus = CPlusPlus0x = NoExtensions = 0;
-    CXXOperatorNames = 0;
+    CXXOperatorNames = PascalStrings = 0;
   }
 };
 
index dde2d9197630d44c552a4711a2924396cda644df..e8650fc8fa70139e1355329ce4a4729cc458feb6 100644 (file)
@@ -150,6 +150,7 @@ public:
                       Preprocessor &PP, TargetInfo &T);
   bool hadError;
   bool AnyWide;
+  bool Pascal;
   
   const char *GetString() { return &ResultBuf[0]; }
   unsigned GetStringLength() { return ResultPtr-&ResultBuf[0]; }