aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/clang/Index/ASTLocation.h4
-rw-r--r--tools/CIndex/CIndex.cpp16
-rw-r--r--tools/c-index-test/c-index-test.c4
3 files changed, 19 insertions, 5 deletions
diff --git a/include/clang/Index/ASTLocation.h b/include/clang/Index/ASTLocation.h
index 9620ec5dbd..0fec84152b 100644
--- a/include/clang/Index/ASTLocation.h
+++ b/include/clang/Index/ASTLocation.h
@@ -124,8 +124,8 @@ public:
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();
}
diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp
index 133a269afd..d0638d0772 100644
--- a/tools/CIndex/CIndex.cpp
+++ b/tools/CIndex/CIndex.cpp
@@ -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 @@ const char *clang_getCursorSource(CXCursor C)
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,
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
index bfc8e620ac..73764950c3 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -63,7 +63,9 @@ static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
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);