aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/CIndex/CIndex.exports1
-rw-r--r--tools/CIndex/CIndexInclusionStack.cpp65
-rw-r--r--tools/CIndex/CMakeLists.txt1
-rw-r--r--tools/c-index-test/c-index-test.c34
4 files changed, 100 insertions, 1 deletions
diff --git a/tools/CIndex/CIndex.exports b/tools/CIndex/CIndex.exports
index ef77fede53..12e2788e4a 100644
--- a/tools/CIndex/CIndex.exports
+++ b/tools/CIndex/CIndex.exports
@@ -28,6 +28,7 @@ _clang_getDefinitionSpellingAndExtent
_clang_getFile
_clang_getFileName
_clang_getFileTime
+_clang_getInclusions
_clang_getInstantiationLocation
_clang_getLocation
_clang_getNullCursor
diff --git a/tools/CIndex/CIndexInclusionStack.cpp b/tools/CIndex/CIndexInclusionStack.cpp
new file mode 100644
index 0000000000..4a884e5884
--- /dev/null
+++ b/tools/CIndex/CIndexInclusionStack.cpp
@@ -0,0 +1,65 @@
+//===- CIndexInclusionStack.cpp - Clang-C Source Indexing Library ---------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines a callback mechanism for clients to get the inclusion
+// stack from a translation unit.
+//
+//===----------------------------------------------------------------------===//
+
+#include "CIndexer.h"
+#include "CXSourceLocation.h"
+#include "clang/AST/DeclVisitor.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/raw_ostream.h"
+
+extern "C" {
+void clang_getInclusions(CXTranslationUnit TU, CXInclusionVisitor CB,
+ CXClientData clientData) {
+
+ ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
+ SourceManager &SM = CXXUnit->getSourceManager();
+ ASTContext &Ctx = CXXUnit->getASTContext();
+
+ llvm::SmallVector<CXSourceLocation, 10> InclusionStack;
+ unsigned i = SM.sloc_loaded_entry_size();
+ unsigned n = SM.sloc_entry_size();
+
+ // In the case where all the SLocEntries are in an external source, traverse
+ // those SLocEntries as well. This is the case where we are looking
+ // at the inclusion stack of an AST/PCH file.
+ if (i >= n)
+ i = 0;
+
+ for ( ; i < n ; ++i) {
+
+ const SrcMgr::SLocEntry &SL = SM.getSLocEntry(i);
+
+ if (!SL.isFile())
+ continue;
+
+ const SrcMgr::FileInfo &FI = SL.getFile();
+ if (!FI.getContentCache()->Entry)
+ continue;
+
+ // Build the inclusion stack.
+ SourceLocation L = FI.getIncludeLoc();
+ InclusionStack.clear();
+ while (L.isValid()) {
+ PresumedLoc PLoc = SM.getPresumedLoc(L);
+ InclusionStack.push_back(cxloc::translateSourceLocation(Ctx, L));
+ L = PLoc.getIncludeLoc();
+ }
+
+ // Callback to the client.
+ // FIXME: We should have a function to construct CXFiles.
+ CB((CXFile) FI.getContentCache()->Entry,
+ InclusionStack.data(), InclusionStack.size(), clientData);
+ }
+}
+} // end extern C
diff --git a/tools/CIndex/CMakeLists.txt b/tools/CIndex/CMakeLists.txt
index 730eaafb5a..f4d2225310 100644
--- a/tools/CIndex/CMakeLists.txt
+++ b/tools/CIndex/CMakeLists.txt
@@ -21,6 +21,7 @@ set( LLVM_LINK_COMPONENTS
add_clang_library(CIndex
CIndex.cpp
CIndexCodeCompletion.cpp
+ CIndexInclusionStack.cpp
CIndexUSRs.cpp
CIndexer.cpp
CXCursor.cpp
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
index 7250cc7257..2ad9da12bb 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -303,6 +303,29 @@ enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
}
/******************************************************************************/
+/* Inclusion stack testing. */
+/******************************************************************************/
+
+void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
+ unsigned includeStackLen, CXClientData data) {
+
+ unsigned i;
+ printf("file: %s\nincluded by:\n", clang_getFileName(includedFile));
+ for (i = 0; i < includeStackLen; ++i) {
+ CXFile includingFile;
+ unsigned line, column;
+ clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
+ &column, 0);
+ printf(" %s:%d:%d\n", clang_getFileName(includingFile), line, column);
+ }
+ printf("\n");
+}
+
+void PrintInclusionStack(CXTranslationUnit TU) {
+ clang_getInclusions(TU, InclusionVisitor, NULL);
+}
+
+/******************************************************************************/
/* Loading ASTs/source. */
/******************************************************************************/
@@ -853,7 +876,9 @@ static void print_usage(void) {
" c-index-test -test-load-source <symbol filter> {<args>}*\n"
" c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n");
fprintf(stderr,
- " c-index-test -test-annotate-tokens=<range> {<args>}* \n\n"
+ " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
+ " c-index-test -test-inclusion-stack-source {<args>}*\n"
+ " c-index-test -test-inclusion-stack-tu <AST file>\n\n"
" <symbol filter> values:\n%s",
" all - load all symbols, including those from PCH\n"
" local - load all symbols except those in PCH\n"
@@ -886,6 +911,13 @@ int main(int argc, const char **argv) {
argc >= 5 ? argv[4] : 0);
else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
return perform_token_annotation(argc, argv);
+ else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
+ return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
+ PrintInclusionStack);
+ else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
+ return perform_test_load_tu(argv[2], "all", NULL, NULL,
+ PrintInclusionStack);
+
print_usage();
return 1;
}