]> granicus.if.org Git - clang/commitdiff
Introduce local -> global mapping for preprocessed entity IDs. This is
authorDouglas Gregor <dgregor@apple.com>
Thu, 4 Aug 2011 18:56:47 +0000 (18:56 +0000)
committerDouglas Gregor <dgregor@apple.com>
Thu, 4 Aug 2011 18:56:47 +0000 (18:56 +0000)
the last of the ID/offset/index mappings that I know
of. Unfortunately, the "gap" method of testing doesn't work here due
to the way the preprocessing record performs iteration. We'll do more
testing once multi-AST loading is possible.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@136902 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Serialization/ASTBitCodes.h
include/clang/Serialization/ASTReader.h
include/clang/Serialization/ContinuousRangeMap.h
lib/Serialization/ASTReader.cpp
lib/Serialization/ASTWriter.cpp

index fbaf587ad5e231c5c1b03f50e629853bfe228867..55d0ddbbc50aaab938b6edd2d72794285e7744aa 100644 (file)
@@ -141,7 +141,10 @@ namespace clang {
     /// \brief An ID number that refers to an entity in the detailed
     /// preprocessing record.
     typedef uint32_t PreprocessedEntityID;
-    
+
+    /// \brief The number of predefined preprocessed entity IDs.
+    const unsigned int NUM_PREDEF_PP_ENTITY_IDS = 1;
+
     /// \brief Describes the various kinds of blocks that occur within
     /// an AST file.
     enum BlockIDs {
index d862501c4f3da5765696d4688eb1d83634d0146b..ceb0d2837b46da9b25fa73ed19b2b3298f15f76a 100644 (file)
@@ -290,6 +290,9 @@ public:
   /// this module.
   serialization::PreprocessedEntityID BasePreprocessedEntityID;
   
+  /// \brief Remapping table for preprocessed entity IDs in this module.
+  ContinuousRangeMap<uint32_t, int, 2> PreprocessedEntityRemap;
+
   /// \brief The number of macro definitions in this file.
   unsigned LocalNumMacroDefinitions;
   
index 42b8954b54a74a73afd85cc08469d33c9faa402b..7f7832041072e655f556aeae2ecc9211961a9f2a 100644 (file)
@@ -61,6 +61,9 @@ private:
 
 public:
   void insert(const value_type &Val) {
+    if (!Rep.empty() && Rep.back() == Val)
+      return;
+
     assert((Rep.empty() || Rep.back().first < Val.first) &&
            "Must insert keys in order.");
     Rep.push_back(Val);
index 62ce2f8c8ae650f3754d09fe1a262aec217d0e85..2606676b691b17d7f395fab585b10279da63a248 100644 (file)
@@ -1643,8 +1643,12 @@ PreprocessedEntity *ASTReader::LoadPreprocessedEntity(Module &F) {
 
 PreprocessedEntityID 
 ASTReader::getGlobalPreprocessedEntityID(Module &M, unsigned LocalID) {
-  // FIXME: Local-to-global mapping
-  return LocalID;
+  ContinuousRangeMap<uint32_t, int, 2>::iterator 
+    I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
+  assert(I != M.PreprocessedEntityRemap.end() 
+         && "Invalid index into preprocessed entity index remap");
+  
+  return LocalID + I->second;
 }
 
 namespace {
@@ -2322,6 +2326,8 @@ ASTReader::ReadASTBlock(Module &F) {
       ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
       ContinuousRangeMap<uint32_t, int, 2>::Builder 
         IdentifierRemap(F.IdentifierRemap);
+      ContinuousRangeMap<uint32_t, int, 2>::Builder 
+        PreprocessedEntityRemap(F.PreprocessedEntityRemap);
       ContinuousRangeMap<uint32_t, int, 2>::Builder 
         MacroDefinitionRemap(F.MacroDefinitionRemap);
       ContinuousRangeMap<uint32_t, int, 2>::Builder 
@@ -2350,12 +2356,12 @@ ASTReader::ReadASTBlock(Module &F) {
         // Source location offset is mapped to OM->SLocEntryBaseOffset.
         SLocRemap.insert(std::make_pair(SLocOffset,
           static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
-        
-        // FIXME: Map other locations
         IdentifierRemap.insert(
           std::make_pair(IdentifierIDOffset, 
                          OM->BaseIdentifierID - IdentifierIDOffset));
-        (void)PreprocessedEntityIDOffset;
+        PreprocessedEntityRemap.insert(
+          std::make_pair(PreprocessedEntityIDOffset, 
+            OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
         MacroDefinitionRemap.insert(
           std::make_pair(MacroDefinitionIDOffset,
                          OM->BaseMacroDefinitionID - MacroDefinitionIDOffset));
@@ -2485,11 +2491,10 @@ ASTReader::ReadASTBlock(Module &F) {
     case MACRO_DEFINITION_OFFSETS: {
       F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
       F.NumPreallocatedPreprocessingEntities = Record[0];
-      F.LocalNumMacroDefinitions = Record[1];
-      unsigned LocalBaseMacroID = Record[2];
+      unsigned LocalBasePreprocessedEntityID = Record[1];
+      F.LocalNumMacroDefinitions = Record[2];
+      unsigned LocalBaseMacroID = Record[3];
       
-      // Introduce the global -> local mapping for preprocessed entities within 
-      // this AST file.
       unsigned StartingID;
       if (PP) {
         if (!PP->getPreprocessingRecord())
@@ -2502,13 +2507,25 @@ ASTReader::ReadASTBlock(Module &F) {
       } else {
         // FIXME: We'll eventually want to kill this path, since it assumes
         // a particular allocation strategy in the preprocessing record.
-        StartingID = getTotalNumPreprocessedEntities();
+        StartingID = getTotalNumPreprocessedEntities() 
+                   - F.NumPreallocatedPreprocessingEntities;
       }
-      GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
-      
       F.BaseMacroDefinitionID = getTotalNumMacroDefinitions();
       F.BasePreprocessedEntityID = StartingID;
 
+      if (F.NumPreallocatedPreprocessingEntities > 0) {
+        // Introduce the global -> local mapping for preprocessed entities in
+        // this module.
+        GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
+       
+        // Introduce the local -> global mapping for preprocessed entities in
+        // this module.
+        F.PreprocessedEntityRemap.insert(
+          std::make_pair(LocalBasePreprocessedEntityID,
+            F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
+      }
+      
+
       if (F.LocalNumMacroDefinitions > 0) {
         // Introduce the global -> local mapping for macro definitions within 
         // this module.
@@ -5606,8 +5623,13 @@ void Module::dump() {
                << '\n';
   dumpLocalRemap("Source location offset map", SLocRemap);
   llvm::errs() << "  Base identifier ID: " << BaseIdentifierID << '\n'
-               << "Number of identifiers: " << LocalNumIdentifiers << '\n';
+               << "  Number of identifiers: " << LocalNumIdentifiers << '\n';
   dumpLocalRemap("Identifier ID map", IdentifierRemap);
+  llvm::errs() << "  Base preprocessed entity ID: " << BasePreprocessedEntityID
+               << '\n'  
+               << "Number of preprocessed entities: " 
+               << NumPreallocatedPreprocessingEntities << '\n';
+  dumpLocalRemap("Preprocessed entity ID map", PreprocessedEntityRemap);
   llvm::errs() << "  Base type index: " << BaseTypeIndex << '\n'
                << "  Number of types: " << LocalNumTypes << '\n';
   dumpLocalRemap("Type index map", TypeRemap);
index 8d2fb9b2b7719fec9390c9774fadf15cf094868b..f23a0cdce9af75ba35a024a2a0bdcc20c7ae4bfd 100644 (file)
@@ -1796,8 +1796,10 @@ void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
     InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
   }
   
-  unsigned IndexBase = Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0;
-  unsigned NextPreprocessorEntityID = IndexBase + 1;
+  unsigned FirstPreprocessorEntityID 
+    = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0) 
+    + NUM_PREDEF_PP_ENTITY_IDS;
+  unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
   RecordData Record;
   uint64_t BitsInChain = Chain? Chain->TotalModulesSizeInBits : 0;
   for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
@@ -1879,6 +1881,7 @@ void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
     BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
     Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
+    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first macro def
     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
@@ -1887,6 +1890,7 @@ void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
     Record.clear();
     Record.push_back(MACRO_DEFINITION_OFFSETS);
     Record.push_back(NumPreprocessingRecords);
+    Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
     Record.push_back(MacroDefinitionOffsets.size());
     Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
     Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,