[cfe-commits] r42974 - in /cfe/trunk: 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

Anders Carlsson andersca at mac.com
Sun Oct 14 19:50:24 PDT 2007


Author: andersca
Date: Sun Oct 14 21:50:23 2007
New Revision: 42974

URL: http://llvm.org/viewvc/llvm-project?rev=42974&view=rev
Log:
Add support for Pascal strings.

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

Modified: cfe/trunk/AST/StmtPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/StmtPrinter.cpp?rev=42974&r1=42973&r2=42974&view=diff

==============================================================================
--- cfe/trunk/AST/StmtPrinter.cpp (original)
+++ cfe/trunk/AST/StmtPrinter.cpp Sun Oct 14 21:50:23 2007
@@ -419,7 +419,7 @@
 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]) {

Modified: cfe/trunk/Driver/clang.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/clang.cpp?rev=42974&r1=42973&r2=42974&view=diff

==============================================================================
--- cfe/trunk/Driver/clang.cpp (original)
+++ cfe/trunk/Driver/clang.cpp Sun Oct 14 21:50:23 2007
@@ -271,10 +271,15 @@
                 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 @@
   
   Options.Trigraphs = 1; // -trigraphs or -ansi
   Options.DollarIdents = 1;  // FIXME: Really a target property.
+  Options.PascalStrings = PascalStrings;
 }
 
 //===----------------------------------------------------------------------===//

Modified: cfe/trunk/Lex/LiteralSupport.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Lex/LiteralSupport.cpp?rev=42974&r1=42973&r2=42974&view=diff

==============================================================================
--- cfe/trunk/Lex/LiteralSupport.cpp (original)
+++ cfe/trunk/Lex/LiteralSupport.cpp Sun Oct 14 21:50:23 2007
@@ -599,6 +599,8 @@
   // 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 @@
     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 @@
     for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
     *ResultPtr++ = 0;
   }
+    
+  if (Pascal) 
+    ResultBuf[0] = ResultPtr-&ResultBuf[0]-1;
 }

Modified: cfe/trunk/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaExpr.cpp?rev=42974&r1=42973&r2=42974&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Sun Oct 14 21:50:23 2007
@@ -46,11 +46,22 @@
     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());
 }
 

Modified: cfe/trunk/clang.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/clang.xcodeproj/project.pbxproj?rev=42974&r1=42973&r2=42974&view=diff

==============================================================================
--- cfe/trunk/clang.xcodeproj/project.pbxproj (original)
+++ cfe/trunk/clang.xcodeproj/project.pbxproj Sun Oct 14 21:50:23 2007
@@ -742,6 +742,7 @@
 		08FB7793FE84155DC02AAC07 /* Project object */ = {
 			isa = PBXProject;
 			buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */;
+			compatibilityVersion = "Xcode 2.4";
 			hasScannedForEncodings = 1;
 			mainGroup = 08FB7794FE84155DC02AAC07 /* clang */;
 			projectDirPath = "";

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=42974&r1=42973&r2=42974&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Sun Oct 14 21:50:23 2007
@@ -771,6 +771,8 @@
      "'__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,

Modified: cfe/trunk/include/clang/Basic/LangOptions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/LangOptions.h?rev=42974&r1=42973&r2=42974&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/LangOptions.h (original)
+++ cfe/trunk/include/clang/Basic/LangOptions.h Sun Oct 14 21:50:23 2007
@@ -34,10 +34,12 @@
   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;
   }
 };
 

Modified: cfe/trunk/include/clang/Lex/LiteralSupport.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/LiteralSupport.h?rev=42974&r1=42973&r2=42974&view=diff

==============================================================================
--- cfe/trunk/include/clang/Lex/LiteralSupport.h (original)
+++ cfe/trunk/include/clang/Lex/LiteralSupport.h Sun Oct 14 21:50:23 2007
@@ -150,6 +150,7 @@
                       Preprocessor &PP, TargetInfo &T);
   bool hadError;
   bool AnyWide;
+  bool Pascal;
   
   const char *GetString() { return &ResultBuf[0]; }
   unsigned GetStringLength() { return ResultPtr-&ResultBuf[0]; }





More information about the cfe-commits mailing list