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

Ted Kremenek kremenek at apple.com
Mon Apr 14 14:04:25 PDT 2008


Author: kremenek
Date: Mon Apr 14 16:04:18 2008
New Revision: 49682

URL: http://llvm.org/viewvc/llvm-project?rev=49682&view=rev
Log:
Added "getCanonicalID()", "isFromSameFile", and "isFromMainFile" to compare
the files of different SourceLocations.  These methods correctly handle the
case where a file may have multiple FileIDs due to it being large enough
to be spread across several chunks.

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

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

==============================================================================
--- cfe/trunk/include/clang/Basic/SourceLocation.h (original)
+++ cfe/trunk/include/clang/Basic/SourceLocation.h Mon Apr 14 16:04:18 2008
@@ -249,6 +249,8 @@
   
   bool isFileID() const { return Loc.isFileID(); }
   
+  unsigned getCanonicalFileID() const;
+  
   bool operator==(const FullSourceLoc& RHS) const {
     return SrcMgr == RHS.SrcMgr && Loc == RHS.Loc;
   }

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

==============================================================================
--- cfe/trunk/include/clang/Basic/SourceManager.h (original)
+++ cfe/trunk/include/clang/Basic/SourceManager.h Mon Apr 14 16:04:18 2008
@@ -371,7 +371,15 @@
   const FileEntry* getFileEntryForID(unsigned id) const {
     return getContentCache(id)->Entry;
   }
-    
+  
+  /// getCanonicalFileID - Return the canonical FileID for a SourceLocation.
+  ///  A file can have multiple FileIDs if it is large enough to be broken
+  ///  into multiple chunks.  This method returns the unique FileID without
+  ///  chunk information for a given SourceLocation.  Use this method when
+  ///  you want to compare FileIDs across SourceLocations.
+  unsigned getCanonicalFileID(SourceLocation PhysLoc) const {
+    return getDecomposedFileLoc(PhysLoc).first;
+  }    
   
   /// getDecomposedFileLoc - Decompose the specified file location into a raw
   /// FileID + Offset pair.  The first element is the FileID, the second is the
@@ -399,6 +407,18 @@
     return getDecomposedFileLoc(PhysLoc).second;
   }
   
+  /// isFromSameFile - Returns true if both SourceLocations correspond to
+  ///  the same file.
+  bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
+    return getCanonicalFileID(Loc1) == getCanonicalFileID(Loc2);
+  }
+  
+  /// isFromMainFile - Returns true if the file of provided SourceLocation is
+  ///   the main file.
+  bool isFromMainFile(SourceLocation Loc) const {
+    return getCanonicalFileID(Loc) == getMainFileID();
+  } 
+  
   /// PrintStats - Print statistics to stderr.
   ///
   void PrintStats() const;

Modified: cfe/trunk/lib/Basic/SourceLocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/SourceLocation.cpp?rev=49682&r1=49681&r2=49682&view=diff

==============================================================================
--- cfe/trunk/lib/Basic/SourceLocation.cpp (original)
+++ cfe/trunk/lib/Basic/SourceLocation.cpp Mon Apr 14 16:04:18 2008
@@ -88,3 +88,7 @@
   assert (isValid());
   return SrcMgr->getBuffer(Loc.getFileID());
 }
+
+unsigned FullSourceLoc::getCanonicalFileID() const {
+  return SrcMgr->getCanonicalFileID(Loc);
+}





More information about the cfe-commits mailing list