[cfe-commits] r84275 - in /cfe/trunk: include/clang/Index/ASTLocation.h tools/CIndex/CIndex.cpp tools/c-index-test/c-index-test.c

Douglas Gregor dgregor at apple.com
Fri Oct 16 14:24:32 PDT 2009


Author: dgregor
Date: Fri Oct 16 16:24:31 2009
New Revision: 84275

URL: http://llvm.org/viewvc/llvm-project?rev=84275&view=rev
Log:
Make CIndex and c-index-test a little bit more robust. The only
substantive change is that clang_getCursorSource() now returns the
file in which a macro was instantiated when the cursor points into a
macro instantiation, rather than crashing.


Modified:
    cfe/trunk/include/clang/Index/ASTLocation.h
    cfe/trunk/tools/CIndex/CIndex.cpp
    cfe/trunk/tools/c-index-test/c-index-test.c

Modified: cfe/trunk/include/clang/Index/ASTLocation.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Index/ASTLocation.h?rev=84275&r1=84274&r2=84275&view=diff

==============================================================================
--- cfe/trunk/include/clang/Index/ASTLocation.h (original)
+++ cfe/trunk/include/clang/Index/ASTLocation.h Fri Oct 16 16:24:31 2009
@@ -124,8 +124,8 @@
     return TypeLoc(QualType::getFromOpaquePtr(Ty.TyPtr), Ty.Data);
   }
 
-  Decl *dyn_AsDecl() const { return getKind() == N_Decl ? D : 0; }
-  Stmt *dyn_AsStmt() const { return getKind() == N_Stmt ? Stm : 0; }
+  Decl *dyn_AsDecl() const { return isValid() && getKind() == N_Decl ? D : 0; }
+  Stmt *dyn_AsStmt() const { return isValid() && getKind() == N_Stmt ? Stm : 0; }
   NamedRef dyn_AsNamedRef() const {
     return getKind() == N_Type ? AsNamedRef() : NamedRef();
   }

Modified: cfe/trunk/tools/CIndex/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.cpp?rev=84275&r1=84274&r2=84275&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Fri Oct 16 16:24:31 2009
@@ -22,11 +22,12 @@
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Frontend/ASTUnit.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/System/Path.h"
 #include <cstdio>
 #include <dlfcn.h>
 #include <sys/wait.h>
 #include <vector>
-#include "llvm/System/Path.h"
 
 using namespace clang;
 using namespace idx;
@@ -823,7 +824,18 @@
   SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
   
   SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
-  return SourceMgr.getBufferName(SLoc);
+  if (SLoc.isFileID())
+    return SourceMgr.getBufferName(SLoc);
+
+  // Retrieve the file in which the macro was instantiated, then provide that
+  // buffer name.
+  // FIXME: Do we want to give specific macro-instantiation information?
+  const llvm::MemoryBuffer *Buffer 
+    = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
+  if (!Buffer)
+    return 0;
+
+  return Buffer->getBufferIdentifier();
 }
 
 void clang_getDefinitionSpellingAndExtent(CXCursor C, 

Modified: cfe/trunk/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=84275&r1=84274&r2=84275&view=diff

==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Fri Oct 16 16:24:31 2009
@@ -63,7 +63,9 @@
 
           Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
                                 curLine, curColumn);
-          if (Ref.kind != CXCursor_FunctionDecl) {
+          if (Ref.kind == CXCursor_NoDeclFound) {
+            // Nothing found here; that's fine.
+          } else if (Ref.kind != CXCursor_FunctionDecl) {
             printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Ref)),
                                              curLine, curColumn);
             PrintCursor(Ref);





More information about the cfe-commits mailing list