[cfe-commits] r143373 - /cfe/trunk/tools/libclang/CXSourceLocation.cpp

Ted Kremenek kremenek at apple.com
Mon Oct 31 15:23:52 PDT 2011


Author: kremenek
Date: Mon Oct 31 17:23:51 2011
New Revision: 143373

URL: http://llvm.org/viewvc/llvm-project?rev=143373&view=rev
Log:
[libclang] Tweak internals of CXSourceLocation to allow an alternate implementation if the lowest bit of ptr_data[0] is not 0.  This
is prep for work on serialized diagnostics.

Modified:
    cfe/trunk/tools/libclang/CXSourceLocation.cpp

Modified: cfe/trunk/tools/libclang/CXSourceLocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXSourceLocation.cpp?rev=143373&r1=143372&r2=143373&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXSourceLocation.cpp (original)
+++ cfe/trunk/tools/libclang/CXSourceLocation.cpp Mon Oct 31 17:23:51 2011
@@ -22,6 +22,16 @@
 using namespace clang::cxstring;
 
 //===----------------------------------------------------------------------===//
+// Internal predicates on CXSourceLocations.
+//===----------------------------------------------------------------------===//
+
+static bool isASTUnitSourceLocation(const CXSourceLocation &L) {
+  // If the lowest bit is clear then the first ptr_data entry is a SourceManager
+  // pointer, or the CXSourceLocation is a null location.
+  return ((uintptr_t)L.ptr_data[0] & 0x1) == 0;
+}
+
+//===----------------------------------------------------------------------===//
 // Basic construction and comparison of CXSourceLocations and CXSourceRanges.
 //===----------------------------------------------------------------------===//
 
@@ -138,7 +148,6 @@
 // of their origin.
 //===----------------------------------------------------------------------===//
 
-
 static void createNullLocation(CXFile *file, unsigned *line,
                                unsigned *column, unsigned *offset) {
   if (file)
@@ -152,6 +161,19 @@
   return;
 }
 
+static void createNullLocation(CXString *filename, unsigned *line,
+                               unsigned *column, unsigned *offset = 0) {
+  if (filename)
+    *filename = createCXString("");
+  if (line)
+    *line = 0;
+  if (column)
+    *column = 0;
+  if (offset)
+    *offset = 0;
+  return;
+}
+
 extern "C" {
 
 void clang_getExpansionLocation(CXSourceLocation location,
@@ -159,63 +181,70 @@
                                 unsigned *line,
                                 unsigned *column,
                                 unsigned *offset) {
-  SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
   
-  if (!location.ptr_data[0] || Loc.isInvalid()) {
-    createNullLocation(file, line, column, offset);
-    return;
-  }
+  if (isASTUnitSourceLocation(location)) {  
+    SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
   
-  const SourceManager &SM =
-  *static_cast<const SourceManager*>(location.ptr_data[0]);
-  SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc);
-  
-  // Check that the FileID is invalid on the expansion location.
-  // This can manifest in invalid code.
-  FileID fileID = SM.getFileID(ExpansionLoc);
-  bool Invalid = false;
-  const SrcMgr::SLocEntry &sloc = SM.getSLocEntry(fileID, &Invalid);
-  if (Invalid || !sloc.isFile()) {
-    createNullLocation(file, line, column, offset);
-    return;
+    if (!location.ptr_data[0] || Loc.isInvalid()) {
+      createNullLocation(file, line, column, offset);
+      return;
+    }
+  
+    const SourceManager &SM =
+    *static_cast<const SourceManager*>(location.ptr_data[0]);
+    SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc);
+    
+    // Check that the FileID is invalid on the expansion location.
+    // This can manifest in invalid code.
+    FileID fileID = SM.getFileID(ExpansionLoc);
+    bool Invalid = false;
+    const SrcMgr::SLocEntry &sloc = SM.getSLocEntry(fileID, &Invalid);
+    if (Invalid || !sloc.isFile()) {
+      createNullLocation(file, line, column, offset);
+      return;
+    }
+    
+    if (file)
+      *file = (void *)SM.getFileEntryForSLocEntry(sloc);
+    if (line)
+      *line = SM.getExpansionLineNumber(ExpansionLoc);
+    if (column)
+      *column = SM.getExpansionColumnNumber(ExpansionLoc);
+    if (offset)
+      *offset = SM.getDecomposedLoc(ExpansionLoc).second;
   }
   
-  if (file)
-    *file = (void *)SM.getFileEntryForSLocEntry(sloc);
-  if (line)
-    *line = SM.getExpansionLineNumber(ExpansionLoc);
-  if (column)
-    *column = SM.getExpansionColumnNumber(ExpansionLoc);
-  if (offset)
-    *offset = SM.getDecomposedLoc(ExpansionLoc).second;
+  // FIXME:
+  createNullLocation(file, line, column, offset);
 }
 
 void clang_getPresumedLocation(CXSourceLocation location,
                                CXString *filename,
                                unsigned *line,
                                unsigned *column) {
-  SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
   
-  if (!location.ptr_data[0] || Loc.isInvalid()) {
-    if (filename)
-      *filename = createCXString("");
-    if (line)
-      *line = 0;
-    if (column)
-      *column = 0;
-  }
-  else {
-    const SourceManager &SM =
-    *static_cast<const SourceManager*>(location.ptr_data[0]);
-    PresumedLoc PreLoc = SM.getPresumedLoc(Loc);
-    
-    if (filename)
-      *filename = createCXString(PreLoc.getFilename());
-    if (line)
-      *line = PreLoc.getLine();
-    if (column)
-      *column = PreLoc.getColumn();
+  if (isASTUnitSourceLocation(location)) {
+    SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
+  
+    if (!location.ptr_data[0] || Loc.isInvalid())
+      createNullLocation(filename, line, column);
+    else {
+      const SourceManager &SM =
+      *static_cast<const SourceManager*>(location.ptr_data[0]);
+      PresumedLoc PreLoc = SM.getPresumedLoc(Loc);
+      
+      if (filename)
+        *filename = createCXString(PreLoc.getFilename());
+      if (line)
+        *line = PreLoc.getLine();
+      if (column)
+        *column = PreLoc.getColumn();
+    }
+    return;
   }
+  
+  // FIXME:
+  createNullLocation(filename, line, column);
 }
 
 void clang_getInstantiationLocation(CXSourceLocation location,
@@ -232,38 +261,45 @@
                                unsigned *line,
                                unsigned *column,
                                unsigned *offset) {
-  SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
   
-  if (!location.ptr_data[0] || Loc.isInvalid())
-    return createNullLocation(file, line, column, offset);
-  
-  const SourceManager &SM =
-  *static_cast<const SourceManager*>(location.ptr_data[0]);
-  SourceLocation SpellLoc = Loc;
-  if (SpellLoc.isMacroID()) {
-    SourceLocation SimpleSpellingLoc = SM.getImmediateSpellingLoc(SpellLoc);
-    if (SimpleSpellingLoc.isFileID() &&
-        SM.getFileEntryForID(SM.getDecomposedLoc(SimpleSpellingLoc).first))
-      SpellLoc = SimpleSpellingLoc;
-    else
-      SpellLoc = SM.getExpansionLoc(SpellLoc);
+  if (isASTUnitSourceLocation(location)) {
+    SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
+    
+    if (!location.ptr_data[0] || Loc.isInvalid())
+      return createNullLocation(file, line, column, offset);
+    
+    const SourceManager &SM =
+    *static_cast<const SourceManager*>(location.ptr_data[0]);
+    SourceLocation SpellLoc = Loc;
+    if (SpellLoc.isMacroID()) {
+      SourceLocation SimpleSpellingLoc = SM.getImmediateSpellingLoc(SpellLoc);
+      if (SimpleSpellingLoc.isFileID() &&
+          SM.getFileEntryForID(SM.getDecomposedLoc(SimpleSpellingLoc).first))
+        SpellLoc = SimpleSpellingLoc;
+      else
+        SpellLoc = SM.getExpansionLoc(SpellLoc);
+    }
+    
+    std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellLoc);
+    FileID FID = LocInfo.first;
+    unsigned FileOffset = LocInfo.second;
+    
+    if (FID.isInvalid())
+      return createNullLocation(file, line, column, offset);
+    
+    if (file)
+      *file = (void *)SM.getFileEntryForID(FID);
+    if (line)
+      *line = SM.getLineNumber(FID, FileOffset);
+    if (column)
+      *column = SM.getColumnNumber(FID, FileOffset);
+    if (offset)
+      *offset = FileOffset;
+    return;
   }
-  
-  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellLoc);
-  FileID FID = LocInfo.first;
-  unsigned FileOffset = LocInfo.second;
-  
-  if (FID.isInvalid())
-    return createNullLocation(file, line, column, offset);
-  
-  if (file)
-    *file = (void *)SM.getFileEntryForID(FID);
-  if (line)
-    *line = SM.getLineNumber(FID, FileOffset);
-  if (column)
-    *column = SM.getColumnNumber(FID, FileOffset);
-  if (offset)
-    *offset = FileOffset;
+
+  // FIXME:
+  createNullLocation(file, line, column, offset);
 }
 
 } // end extern "C"





More information about the cfe-commits mailing list