[cfe-commits] r40108 - in /cfe/trunk: Basic/SourceManager.cpp include/clang/Basic/SourceLocation.h include/clang/Basic/SourceManager.h

Chris Lattner sabre at nondot.org
Fri Jul 20 11:00:12 PDT 2007


Author: lattner
Date: Fri Jul 20 13:00:12 2007
New Revision: 40108

URL: http://llvm.org/viewvc/llvm-project?rev=40108&view=rev
Log:
improve comments, implement a trivial single-entry cache in 
SourceManager::getInstantiationLoc.  With this change, every token
expanded from a macro doesn't get its own MacroID.  :)

This reduces # macro IDs in carbon.h from 16805 to 9197


Modified:
    cfe/trunk/Basic/SourceManager.cpp
    cfe/trunk/include/clang/Basic/SourceLocation.h
    cfe/trunk/include/clang/Basic/SourceManager.h

Modified: cfe/trunk/Basic/SourceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Basic/SourceManager.cpp?rev=40108&r1=40107&r2=40108&view=diff

==============================================================================
--- cfe/trunk/Basic/SourceManager.cpp (original)
+++ cfe/trunk/Basic/SourceManager.cpp Fri Jul 20 13:00:12 2007
@@ -165,16 +165,35 @@
 /// getInstantiationLoc - Return a new SourceLocation that encodes the fact
 /// that a token from physloc PhysLoc should actually be referenced from
 /// InstantiationLoc.
-SourceLocation SourceManager::getInstantiationLoc(SourceLocation VirtLoc,
+SourceLocation SourceManager::getInstantiationLoc(SourceLocation PhysLoc,
                                                   SourceLocation InstantLoc) {
   // The specified source location may be a mapped location, due to a macro
   // instantiation or #line directive.  Strip off this information to find out
   // where the characters are actually located.
-  SourceLocation PhysLoc = getPhysicalLoc(VirtLoc);
+  PhysLoc = getPhysicalLoc(PhysLoc);
   
   // Resolve InstantLoc down to a real logical location.
   InstantLoc = getLogicalLoc(InstantLoc);
   
+  
+  // If the last macro id is close to the currently requested location, try to
+  // reuse it.  This implements a single-entry cache.
+  if (!MacroIDs.empty()) {
+    MacroIDInfo &LastOne = MacroIDs.back();
+    if (LastOne.getInstantiationLoc() == InstantLoc &&
+        LastOne.getPhysicalLoc().getFileID() == PhysLoc.getFileID()) {
+      
+      int PhysDelta = PhysLoc.getRawFilePos() -
+                      LastOne.getPhysicalLoc().getRawFilePos();
+      if (unsigned(PhysDelta) < (1 << SourceLocation::MacroPhysOffsBits))
+        return SourceLocation::getMacroLoc(MacroIDs.size()-1,
+                                           (unsigned)PhysDelta, 0);
+
+    }
+  }
+  
+  
+  
   // FIXME: intelligently cache macroid's.
   MacroIDs.push_back(MacroIDInfo::get(InstantLoc, PhysLoc));
   

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

==============================================================================
--- cfe/trunk/include/clang/Basic/SourceLocation.h (original)
+++ cfe/trunk/include/clang/Basic/SourceLocation.h Fri Jul 20 13:00:12 2007
@@ -28,8 +28,8 @@
     FileIDBits  = 14,
     FilePosBits = 32-1-FileIDBits,
     
-    MacroIDBits       = 23,
-    MacroPhysOffsBits = 5,
+    MacroIDBits       = 19,
+    MacroPhysOffsBits = 9,
     MacroLogOffBits   = 3
   };
 
@@ -106,7 +106,7 @@
   
   unsigned getMacroLogOffs() const {
     assert(isMacroID() && "Is not a macro id!");
-    return ID & ((1 << MacroPhysOffsBits)-1);
+    return ID & ((1 << MacroLogOffBits)-1);
   }
   
   /// getFileLocWithOffset - Return a source location with the specified offset

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

==============================================================================
--- cfe/trunk/include/clang/Basic/SourceManager.h (original)
+++ cfe/trunk/include/clang/Basic/SourceManager.h Fri Jul 20 13:00:12 2007
@@ -53,8 +53,8 @@
   typedef std::pair<const FileEntry * const, FileInfo> InfoRec;
 
   /// FileIDInfo - Information about a FileID, basically just the logical file
-  /// that it represents and include stack information.  A SourceLocation is a
-  /// byte offset from the start of this.
+  /// that it represents and include stack information.  A File SourceLocation
+  /// is a byte offset from the start of this.
   ///
   /// FileID's are used to compute the location of a character in memory as well
   /// as the logical source location, which can be differ from the physical
@@ -63,16 +63,12 @@
   ///
   /// Each FileID has include stack information, indicating where it came from.
   /// For the primary translation unit, it comes from SourceLocation() aka 0.
+  /// This information encodes the #include chain that a token was instantiated
+  /// from.
   ///
-  /// There are three types of FileID's:
-  ///   1. Normal MemoryBuffer (file).  These are represented by a "InfoRec *",
-  ///      describing the source file, and a Chunk number, which factors into
-  ///      the SourceLocation's offset from the start of the buffer.
-  ///   2. Macro Expansions.  These indicate that the logical location is
-  ///      totally different than the physical location.  The logical source
-  ///      location is specified by the IncludeLoc.  The physical location is
-  ///      the FilePos of the token's SourceLocation combined with the FileID
-  ///      from MacroTokenFileID.
+  /// FileIDInfos contain a "InfoRec *", describing the source file, and a Chunk
+  /// number, which allows a SourceLocation to index into very large files
+  /// (those which there are not enough FilePosBits to address).
   ///
   struct FileIDInfo {
   private:
@@ -104,6 +100,11 @@
     const InfoRec *getInfo() const { return Info; }
   };
   
+  /// MacroIDInfo - Macro SourceLocations refer to these records by their ID.
+  /// Each MacroIDInfo encodes the Instantiation location - where the macro was
+  /// instantiated, and the PhysicalLoc - where the actual character data for
+  /// the token came from.  An actual macro SourceLocation stores deltas from
+  /// these positions.
   class MacroIDInfo {
     SourceLocation InstantiationLoc, PhysicalLoc;
   public:
@@ -153,13 +154,8 @@
   /// MacroIDs - Information about each MacroID.
   std::vector<SrcMgr::MacroIDInfo> MacroIDs;
   
-  /// LastInstantiationLoc_* - Cache the last instantiation request for fast
-  /// lookup.  Macros often want many tokens instantated at the same location.
-  SourceLocation LastInstantiationLoc_InstantLoc;
-  unsigned       LastInstantiationLoc_MacroFID;
-  unsigned       LastInstantiationLoc_Result;
 public:
-  SourceManager() { LastInstantiationLoc_MacroFID = ~0U; }
+  SourceManager() {}
   ~SourceManager();
   
   /// createFileID - Create a new FileID that represents the specified file
@@ -249,8 +245,8 @@
     // File locations are both physical and logical.
     if (Loc.isFileID()) return Loc;
     
-    SourceLocation ILoc = MacroIDs[Loc.getMacroID()].getPhysicalLoc();
-    return ILoc.getFileLocWithOffset(Loc.getMacroPhysOffs());
+    SourceLocation PLoc = MacroIDs[Loc.getMacroID()].getPhysicalLoc();
+    return PLoc.getFileLocWithOffset(Loc.getMacroPhysOffs());
   }
 
   /// getFileEntryForLoc - Return the FileEntry record for the physloc of the 





More information about the cfe-commits mailing list