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

Chris Lattner sabre at nondot.org
Fri Jul 20 23:41:57 PDT 2007


Author: lattner
Date: Sat Jul 21 01:41:57 2007
New Revision: 40281

URL: http://llvm.org/viewvc/llvm-project?rev=40281&view=rev
Log:
Add support for reusing macroid's with negative physical loc deltas. This
keeps the MacroInfo table more compact.

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

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

==============================================================================
--- cfe/trunk/Basic/SourceManager.cpp (original)
+++ cfe/trunk/Basic/SourceManager.cpp Sat Jul 21 01:41:57 2007
@@ -180,20 +180,19 @@
   // 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);
+      if (SourceLocation::isValidMacroPhysOffs(PhysDelta))
+        return SourceLocation::getMacroLoc(MacroIDs.size()-1, PhysDelta, 0);
     }
   }
   
  
   MacroIDs.push_back(MacroIDInfo::get(InstantLoc, PhysLoc));
-  
   return SourceLocation::getMacroLoc(MacroIDs.size()-1, 0, 0);
 }
 

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

==============================================================================
--- cfe/trunk/include/clang/Basic/SourceLocation.h (original)
+++ cfe/trunk/include/clang/Basic/SourceLocation.h Sat Jul 21 01:41:57 2007
@@ -28,9 +28,9 @@
     FileIDBits  = 14,
     FilePosBits = 32-1-FileIDBits,
     
-    MacroIDBits       = 19,
+    MacroIDBits       = 20,
     MacroPhysOffsBits = 9,
-    MacroLogOffBits   = 3
+    MacroLogOffBits   = 2
   };
 
   SourceLocation() : ID(0) {}  // 0 is an invalid FileID.
@@ -56,16 +56,24 @@
     return L;
   }
   
-  static SourceLocation getMacroLoc(unsigned MacroID, unsigned PhysOffs,
+  static bool isValidMacroPhysOffs(int Val) {
+    if (Val >= 0)
+      return Val < (1 << (MacroPhysOffsBits-1));
+    return -Val < (1 << (MacroPhysOffsBits-1));
+  }
+  
+  static SourceLocation getMacroLoc(unsigned MacroID, int PhysOffs,
                                     unsigned LogOffs) {
-    SourceLocation L;
-    
     assert(MacroID < (1 << MacroIDBits) && "Too many macros!");
-    assert(PhysOffs < (1 << MacroPhysOffsBits) && "Physoffs too large!");
+    assert(isValidMacroPhysOffs(PhysOffs) && "Physoffs too large!");
     assert(LogOffs  < (1 << MacroLogOffBits) && "Logical offs too large!");
     
+    PhysOffs &= (1 << MacroPhysOffsBits)-1;
+    
+    SourceLocation L;
     L.ID = (1 << 31) | (MacroID << (MacroPhysOffsBits+MacroLogOffBits)) |
-           (PhysOffs << MacroLogOffBits) | LogOffs;
+           (PhysOffs << MacroLogOffBits) |
+           LogOffs;
     return L;
   }
   
@@ -99,9 +107,12 @@
     return (ID >> (MacroPhysOffsBits+MacroLogOffBits)) & ((1 << MacroIDBits)-1);
   }
   
-  unsigned getMacroPhysOffs() const {
+  int getMacroPhysOffs() const {
     assert(isMacroID() && "Is not a macro id!");
-    return (ID >> MacroLogOffBits) & ((1 << MacroPhysOffsBits)-1);
+    int Val = (ID >> MacroLogOffBits) & ((1 << MacroPhysOffsBits)-1);
+    // Sign extend it properly.
+    unsigned ShAmt = sizeof(int)*8 - MacroPhysOffsBits;
+    return (Val << ShAmt) >> ShAmt;
   }
   
   unsigned getMacroLogOffs() const {
@@ -111,7 +122,7 @@
   
   /// getFileLocWithOffset - Return a source location with the specified offset
   /// from this file SourceLocation.
-  SourceLocation getFileLocWithOffset(unsigned Offset) const {
+  SourceLocation getFileLocWithOffset(int Offset) const {
     return getFileLoc(getFileID(), getRawFilePos()+Offset);
   }
   





More information about the cfe-commits mailing list