]> granicus.if.org Git - clang/commitdiff
PCH support for string literals
authorDouglas Gregor <dgregor@apple.com>
Wed, 15 Apr 2009 16:35:07 +0000 (16:35 +0000)
committerDouglas Gregor <dgregor@apple.com>
Wed, 15 Apr 2009 16:35:07 +0000 (16:35 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@69172 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/Expr.h
include/clang/Frontend/PCHBitCodes.h
lib/AST/Expr.cpp
lib/Frontend/PCHReader.cpp
lib/Frontend/PCHWriter.cpp
test/PCH/exprs.c
test/PCH/exprs.h

index 8e5117b63a2d10b1ac8105e7699828d89ed9acf5..7dd78342067818b6d24fe51814be409bd0f3c2b8 100644 (file)
@@ -581,12 +581,21 @@ public:
     return Create(C, StrData, ByteLength, Wide, Ty, &Loc, 1);
   }
 
+  /// \brief Construct an empty string literal.
+  static StringLiteral *CreateEmpty(ASTContext &C, unsigned NumStrs);
+
   StringLiteral* Clone(ASTContext &C) const;
   void Destroy(ASTContext &C);
   
   const char *getStrData() const { return StrData; }
   unsigned getByteLength() const { return ByteLength; }
+
+  /// \brief Sets the string data to the given string data.
+  void setStrData(ASTContext &C, const char *Str, unsigned Len);
+
   bool isWide() const { return IsWide; }
+  void setWide(bool W) { IsWide = W; }
+
   bool containsNonAsciiOrNull() const {
     for (unsigned i = 0; i < getByteLength(); ++i)
       if (!isascii(getStrData()[i]) || !getStrData()[i])
@@ -601,7 +610,11 @@ public:
     assert(TokNum < NumConcatenated && "Invalid tok number");
     return TokLocs[TokNum];
   }
-  
+  void setStrTokenLoc(unsigned TokNum, SourceLocation L) { 
+    assert(TokNum < NumConcatenated && "Invalid tok number");
+    TokLocs[TokNum] = L;
+  }
+
   typedef const SourceLocation *tokloc_iterator;
   tokloc_iterator tokloc_begin() const { return TokLocs; }
   tokloc_iterator tokloc_end() const { return TokLocs+NumConcatenated; }
index faec40be2df154cc4a31f0a7d7168ef73f54d21c..279e4fc2dfaeb559c4d6782d28939c372f47a3f0 100644 (file)
@@ -381,6 +381,8 @@ namespace clang {
       EXPR_INTEGER_LITERAL,
       /// \brief A FloatingLiteral record.
       EXPR_FLOATING_LITERAL,
+      /// \brief A StringLiteral record.
+      EXPR_STRING_LITERAL,
       /// \brief A CharacterLiteral record.
       EXPR_CHARACTER_LITERAL,
       /// \brief A ParenExpr record.
index e2dd64a7036cd89e786d98c9286d2bdc7302aa68..53633977e98b383cca8ff10909f3ebc1741f63c8 100644 (file)
@@ -68,6 +68,17 @@ StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
   return SL;
 }
 
+StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
+  void *Mem = C.Allocate(sizeof(StringLiteral)+
+                         sizeof(SourceLocation)*(NumStrs-1),
+                         llvm::alignof<StringLiteral>());
+  StringLiteral *SL = new (Mem) StringLiteral(QualType());
+  SL->StrData = 0;
+  SL->ByteLength = 0;
+  SL->NumConcatenated = NumStrs;
+  return SL;
+}
+
 StringLiteral* StringLiteral::Clone(ASTContext &C) const {
   return Create(C, StrData, ByteLength, IsWide, getType(),
                 TokLocs, NumConcatenated);
@@ -79,6 +90,16 @@ void StringLiteral::Destroy(ASTContext &C) {
   C.Deallocate(this);
 }
 
+void StringLiteral::setStrData(ASTContext &C, const char *Str, unsigned Len) {
+  if (StrData)
+    C.Deallocate(const_cast<char*>(StrData));
+
+  char *AStrData = new (C, 1) char[Len];
+  memcpy(AStrData, Str, Len);
+  StrData = AStrData;
+  ByteLength = Len;
+}
+
 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
 /// corresponds to, e.g. "sizeof" or "[pre]++".
 const char *UnaryOperator::getOpcodeStr(Opcode Op) {
index 96c383b49f31f88bb37ac94f6c45156225082ff7..2da21023c7747e823e129369a19dc89dccc88e92 100644 (file)
@@ -226,6 +226,10 @@ namespace {
                   unsigned &Idx, llvm::SmallVectorImpl<Expr *> &ExprStack)
       : Reader(Reader), Record(Record), Idx(Idx), ExprStack(ExprStack) { }
 
+    /// \brief The number of record fields required for the Expr class
+    /// itself.
+    static const unsigned NumExprFields = 3;
+
     // Each of the Visit* functions reads in part of the expression
     // from the given record and the current expression stack, then
     // return the total number of operands that it read from the
@@ -236,6 +240,7 @@ namespace {
     unsigned VisitDeclRefExpr(DeclRefExpr *E);
     unsigned VisitIntegerLiteral(IntegerLiteral *E);
     unsigned VisitFloatingLiteral(FloatingLiteral *E);
+    unsigned VisitStringLiteral(StringLiteral *E);
     unsigned VisitCharacterLiteral(CharacterLiteral *E);
     unsigned VisitParenExpr(ParenExpr *E);
     unsigned VisitUnaryOperator(UnaryOperator *E);
@@ -252,6 +257,7 @@ unsigned PCHStmtReader::VisitExpr(Expr *E) {
   E->setType(Reader.GetType(Record[Idx++]));
   E->setTypeDependent(Record[Idx++]);
   E->setValueDependent(Record[Idx++]);
+  assert(Idx == NumExprFields && "Incorrect expression field count");
   return 0;
 }
 
@@ -284,6 +290,26 @@ unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
   return 0;
 }
 
+unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
+  VisitExpr(E);
+  unsigned Len = Record[Idx++];
+  assert(Record[Idx] == E->getNumConcatenated() && 
+         "Wrong number of concatenated tokens!");
+  ++Idx;
+  E->setWide(Record[Idx++]);
+
+  // Read string data  
+  llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
+  E->setStrData(Reader.getContext(), &Str[0], Len);
+  Idx += Len;
+
+  // Read source locations
+  for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
+    E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
+
+  return 0;
+}
+
 unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
   VisitExpr(E);
   E->setValue(Record[Idx++]);
@@ -1656,6 +1682,11 @@ Expr *PCHReader::ReadExpr() {
       E = new (Context) FloatingLiteral(Empty);
       break;
       
+    case pch::EXPR_STRING_LITERAL:
+      E = StringLiteral::CreateEmpty(Context, 
+                                     Record[PCHStmtReader::NumExprFields + 1]);
+      break;
+
     case pch::EXPR_CHARACTER_LITERAL:
       E = new (Context) CharacterLiteral(Empty);
       break;
index dc23fb9d81659f0941ba8ba8fae8b47f1b02a888..69b070f79c07c1bcca53445227ad2f7ce39b79d9 100644 (file)
@@ -448,6 +448,7 @@ namespace {
     void VisitDeclRefExpr(DeclRefExpr *E);
     void VisitIntegerLiteral(IntegerLiteral *E);
     void VisitFloatingLiteral(FloatingLiteral *E);
+    void VisitStringLiteral(StringLiteral *E);
     void VisitCharacterLiteral(CharacterLiteral *E);
     void VisitParenExpr(ParenExpr *E);
     void VisitUnaryOperator(UnaryOperator *E);
@@ -495,6 +496,22 @@ void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
   Code = pch::EXPR_FLOATING_LITERAL;
 }
 
+void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
+  VisitExpr(E);
+  Record.push_back(E->getByteLength());
+  Record.push_back(E->getNumConcatenated());
+  Record.push_back(E->isWide());
+  // FIXME: String data should be stored as a blob at the end of the
+  // StringLiteral. However, we can't do so now because we have no
+  // provision for coping with abbreviations when we're jumping around
+  // the PCH file during deserialization.
+  Record.insert(Record.end(), 
+                E->getStrData(), E->getStrData() + E->getByteLength());
+  for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
+    Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
+  Code = pch::EXPR_STRING_LITERAL;
+}
+
 void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
   VisitExpr(E);
   Record.push_back(E->getValue());
index 94625deb4c657cc9453ffcd177e735b62722a1be..e41c93f21da3e8da80fb423559e654f82a4a862c 100644 (file)
@@ -20,6 +20,11 @@ long_literal *long_ptr1 = &long_integer;
 // FloatingLiteral + ParenExpr
 floating_literal *double_ptr = &floating;
 
+// StringLiteral
+const char* printHello() {
+  return hello;
+}
+
 // CharacterLiteral
 char_literal *int_ptr3 = &integer;
 
index 49966a3c46fe5ff2ffb27221deb2ba9db939777f..73e15fb5a9f6ab18e76438bce078bbb39d92d83f 100644 (file)
@@ -13,6 +13,9 @@ typedef typeof(17l) long_literal;
 // FloatingLiteral and ParenExpr
 typedef typeof((42.5)) floating_literal;
 
+// StringLiteral
+const char *hello = "Hello" "PCH" "World";
+
 // CharacterLiteral
 typedef typeof('a') char_literal;