[cfe-commits] r68882 - in /cfe/trunk: include/clang/Frontend/PCHBitCodes.h include/clang/Frontend/PCHReader.h lib/Frontend/PCHReader.cpp lib/Frontend/PCHWriter.cpp test/PCH/variables.c test/PCH/variables.h

Chris Lattner sabre at nondot.org
Sat Apr 11 14:15:38 PDT 2009


Author: lattner
Date: Sat Apr 11 16:15:38 2009
New Revision: 68882

URL: http://llvm.org/viewvc/llvm-project?rev=68882&view=rev
Log:
now that we have an identifier table in the PCH file, finish hooking up
macro deserialization.  We now correctly install II's in tokens, handle
function-like macros, etc.

Modified:
    cfe/trunk/include/clang/Frontend/PCHBitCodes.h
    cfe/trunk/include/clang/Frontend/PCHReader.h
    cfe/trunk/lib/Frontend/PCHReader.cpp
    cfe/trunk/lib/Frontend/PCHWriter.cpp
    cfe/trunk/test/PCH/variables.c
    cfe/trunk/test/PCH/variables.h

Modified: cfe/trunk/include/clang/Frontend/PCHBitCodes.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHBitCodes.h?rev=68882&r1=68881&r2=68882&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHBitCodes.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHBitCodes.h Sat Apr 11 16:15:38 2009
@@ -155,9 +155,6 @@
       // The macros in the PP section are a PP_MACRO_* instance followed by a
       // list of PP_TOKEN instances for each token in the definition.
 
-      // FIXME: TEMP HACK UNTIL WE HAVE IDENTIFIER INFO IDs.
-      PP_MACRO_NAME = 4,
-
       /// \brief An object-like macro definition.
       /// [PP_MACRO_OBJECT_LIKE, IdentInfoID, SLoc, IsUsed]
       PP_MACRO_OBJECT_LIKE = 1,

Modified: cfe/trunk/include/clang/Frontend/PCHReader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHReader.h?rev=68882&r1=68881&r2=68882&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHReader.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHReader.h Sat Apr 11 16:15:38 2009
@@ -137,9 +137,9 @@
   typedef llvm::SmallVector<uint64_t, 64> RecordData;
 
   PCHReader(Preprocessor &PP, ASTContext &Context) 
-    : PP(PP), Context(Context), Buffer() { }
+    : PP(PP), Context(Context), IdentifierTable(0) { }
 
-  ~PCHReader();
+  ~PCHReader() {}
 
   PCHReadResult ReadPCH(const std::string &FileName);
 
@@ -195,8 +195,11 @@
   /// \brief Report a diagnostic.
   DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
 
-  const IdentifierInfo *GetIdentifierInfo(const RecordData &Record, 
-                                          unsigned &Idx);
+  IdentifierInfo *DecodeIdentifierInfo(unsigned Idx);
+  
+  IdentifierInfo *GetIdentifierInfo(const RecordData &Record, unsigned &Idx) {
+    return DecodeIdentifierInfo(Record[Idx++]);
+  }
   DeclarationName ReadDeclarationName(const RecordData &Record, unsigned &Idx);
 };
 

Modified: cfe/trunk/lib/Frontend/PCHReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReader.cpp?rev=68882&r1=68881&r2=68882&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHReader.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReader.cpp Sat Apr 11 16:15:38 2009
@@ -286,7 +286,6 @@
   if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
     return Error("Malformed preprocessor block record");
   
-  std::string CurName; // FIXME: HACK.
   RecordData Record;
   llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
   MacroInfo *LastMacro = 0;
@@ -320,21 +319,11 @@
     default:  // Default behavior: ignore unknown records.
       break;
         
-    case pch::PP_MACRO_NAME:
-      // Set CurName.  FIXME: This is a hack and should be removed when we have
-      // identifier id's.
-      CurName.clear();
-      for (unsigned i = 0, e = Record.size(); i != e; ++i)
-        CurName += (char)Record[i];
-      break;
-        
     case pch::PP_MACRO_OBJECT_LIKE:
     case pch::PP_MACRO_FUNCTION_LIKE: {
-      unsigned IdentInfo = Record[0];
-      IdentInfo = IdentInfo; // FIXME: Decode into identifier info*.
-      assert(!CurName.empty());
-      IdentifierInfo *II = PP.getIdentifierInfo(CurName.c_str());
-      
+      IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
+      if (II == 0)
+        return Error("Macro must have a name");
       SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
       bool isUsed = Record[2];
       
@@ -348,7 +337,7 @@
         MacroArgs.clear();
         unsigned NumArgs = Record[5];
         for (unsigned i = 0; i != NumArgs; ++i)
-          ; // FIXME: Decode macro arg names: MacroArgs.push_back(Record[6+i]);
+          MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
 
         // Install function-like macro info.
         MI->setIsFunctionLike();
@@ -376,8 +365,8 @@
       Tok.startToken();
       Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
       Tok.setLength(Record[1]);
-      unsigned IdentInfo = Record[2];
-      IdentInfo = IdentInfo; // FIXME: Handle this right.
+      if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
+        Tok.setIdentifierInfo(II);
       Tok.setKind((tok::TokenKind)Record[3]);
       Tok.setFlag((Token::TokenFlags)Record[4]);
       LastMacro->AddTokenToBody(Tok);
@@ -393,15 +382,29 @@
     return Failure;
   }
 
+  uint64_t PreprocessorBlockBit = 0;
+  
   // Read all of the records and blocks for the PCH file.
   RecordData Record;
   while (!Stream.AtEndOfStream()) {
     unsigned Code = Stream.ReadCode();
     if (Code == llvm::bitc::END_BLOCK) {
+      // If we saw the preprocessor block, read it now.
+      if (PreprocessorBlockBit) {
+        uint64_t SavedPos = Stream.GetCurrentBitNo();
+        Stream.JumpToBit(PreprocessorBlockBit);
+        if (ReadPreprocessorBlock()) {
+          Error("Malformed preprocessor block");
+          return Failure;
+        }
+        Stream.JumpToBit(SavedPos);
+      }        
+      
       if (Stream.ReadBlockEnd()) {
         Error("Error at end of module block");
         return Failure;
       }
+
       return Success;
     }
 
@@ -416,6 +419,20 @@
         }
         break;
 
+      case pch::PREPROCESSOR_BLOCK_ID:
+        // Skip the preprocessor block for now, but remember where it is.  We
+        // want to read it in after the identifier table.
+        if (PreprocessorBlockBit) {
+          Error("Multiple preprocessor blocks found.");
+          return Failure;
+        }
+        PreprocessorBlockBit = Stream.GetCurrentBitNo();
+        if (Stream.SkipBlock()) {
+          Error("Malformed block record");
+          return Failure;
+        }
+        break;
+          
       case pch::SOURCE_MANAGER_BLOCK_ID:
         switch (ReadSourceManagerBlock()) {
         case Success:
@@ -429,13 +446,6 @@
           return IgnorePCH;
         }
         break;
-          
-      case pch::PREPROCESSOR_BLOCK_ID:
-        if (ReadPreprocessorBlock()) {
-          Error("Malformed preprocessor block");
-          return Failure;
-        }
-        break;
       }
       continue;
     }
@@ -514,8 +524,6 @@
   return Failure;
 }
 
-PCHReader::~PCHReader() { }
-
 PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
   // Set the PCH file name.
   this->FileName = FileName;
@@ -946,24 +954,22 @@
   std::fprintf(stderr, "\n");
 }
 
-const IdentifierInfo *PCHReader::GetIdentifierInfo(const RecordData &Record, 
-                                                   unsigned &Idx) {
-  pch::IdentID ID = Record[Idx++];
+IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
   if (ID == 0)
     return 0;
-
+  
   if (!IdentifierTable || IdentifierData.empty()) {
     Error("No identifier table in PCH file");
     return 0;
   }
-
+  
   if (IdentifierData[ID - 1] & 0x01) {
     uint64_t Offset = IdentifierData[ID - 1];
     IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
-                               &Context.Idents.get(IdentifierTable + Offset));
+                                                        &Context.Idents.get(IdentifierTable + Offset));
   }
-
-  return reinterpret_cast<const IdentifierInfo *>(IdentifierData[ID - 1]);
+  
+  return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
 }
 
 DeclarationName 

Modified: cfe/trunk/lib/Frontend/PCHWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriter.cpp?rev=68882&r1=68881&r2=68882&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriter.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriter.cpp Sat Apr 11 16:15:38 2009
@@ -582,17 +582,7 @@
     if (MI->isBuiltinMacro())
       continue;
 
-    IdentifierInfo *II = I->first;
-    
-    // FIXME: Emit a PP_MACRO_NAME for testing.  This should be removed when we
-    // have identifierinfo id's.
-    for (unsigned i = 0, e = II->getLength(); i != e; ++i)
-      Record.push_back(II->getName()[i]);
-    S.EmitRecord(pch::PP_MACRO_NAME, Record);
-    Record.clear();
-    
-    // FIXME: Output the identifier Info ID #!
-    Record.push_back((intptr_t)II); 
+    AddIdentifierRef(I->first, Record);
     Record.push_back(MI->getDefinitionLoc().getRawEncoding());
     Record.push_back(MI->isUsed());
     
@@ -607,8 +597,7 @@
       Record.push_back(MI->getNumArgs());
       for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
            I != E; ++I)
-        // FIXME: Output the identifier Info ID #!
-        Record.push_back((intptr_t)II); 
+        AddIdentifierRef(*I, Record);
     }
     S.EmitRecord(Code, Record);
     Record.clear();
@@ -623,10 +612,9 @@
       Record.push_back(Tok.getLocation().getRawEncoding());
       Record.push_back(Tok.getLength());
 
-      // FIXME: Output the identifier Info ID #!
       // FIXME: When reading literal tokens, reconstruct the literal pointer if
       // it is needed.
-      Record.push_back((intptr_t)Tok.getIdentifierInfo());
+      AddIdentifierRef(Tok.getIdentifierInfo(), Record);
       
       // FIXME: Should translate token kind to a stable encoding.
       Record.push_back(Tok.getKind());

Modified: cfe/trunk/test/PCH/variables.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/variables.c?rev=68882&r1=68881&r2=68882&view=diff

==============================================================================
--- cfe/trunk/test/PCH/variables.c (original)
+++ cfe/trunk/test/PCH/variables.c Sat Apr 11 16:15:38 2009
@@ -15,3 +15,4 @@
 
 int Q = A_MACRO_IN_THE_PCH;
 
+int R = FUNCLIKE_MACRO(A_MACRO_, IN_THE_PCH);
\ No newline at end of file

Modified: cfe/trunk/test/PCH/variables.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/variables.h?rev=68882&r1=68881&r2=68882&view=diff

==============================================================================
--- cfe/trunk/test/PCH/variables.h (original)
+++ cfe/trunk/test/PCH/variables.h Sat Apr 11 16:15:38 2009
@@ -15,4 +15,4 @@
 int MAKE_HAPPY(Very);
 
 #define A_MACRO_IN_THE_PCH 492
-#define FUNCLIKE_MACRO(X, Y) X ## Y
\ No newline at end of file
+#define FUNCLIKE_MACRO(X, Y) X ## Y





More information about the cfe-commits mailing list