[cfe-commits] r94575 - in /cfe/trunk: include/clang-c/Index.h include/clang/Basic/SourceManager.h test/Index/cindex-test-inclusions.c test/Index/include_test.h test/Index/include_test_2.h tools/CIndex/CIndex.exports tools/CIndex/CIndexInclusionStack.cpp tools/CIndex/CMakeLists.txt tools/c-index-test/c-index-test.c

Ted Kremenek kremenek at apple.com
Tue Jan 26 11:31:51 PST 2010


Author: kremenek
Date: Tue Jan 26 13:31:51 2010
New Revision: 94575

URL: http://llvm.org/viewvc/llvm-project?rev=94575&view=rev
Log:
Implement 'clang_getInclusions()' in CIndex.  This API allows clients to walk the set of files included in a translation unit via the C API.

Added:
    cfe/trunk/test/Index/cindex-test-inclusions.c
    cfe/trunk/test/Index/include_test.h
    cfe/trunk/test/Index/include_test_2.h
    cfe/trunk/tools/CIndex/CIndexInclusionStack.cpp
Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/include/clang/Basic/SourceManager.h
    cfe/trunk/tools/CIndex/CIndex.exports
    cfe/trunk/tools/CIndex/CMakeLists.txt
    cfe/trunk/tools/c-index-test/c-index-test.c

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

==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Tue Jan 26 13:31:51 2010
@@ -1403,6 +1403,38 @@
 CINDEX_LINKAGE const char *clang_getClangVersion();
 
 /**
+ * \brief Return a version string, suitable for showing to a user, but not
+ *        intended to be parsed (the format is not guaranteed to be stable).
+ */
+ 
+ 
+ /**
+  * \brief Visitor invoked for each file in a translation unit 
+  *        (used with clang_getInclusions()).
+  *
+  * This visitor function will be invoked by clang_getInclusions() for each
+  * file included (either at the top-level or by #include directives) within
+  * a translation unit.  The first argument is the file being included, and
+  * the second and third arguments provide the inclusion stack.  The
+  * array is sorted in order of immediate inclusion.  For example,
+  * the first element refers to the location that included 'included_file'.
+  */
+typedef void (*CXInclusionVisitor)(CXFile included_file,
+                                   CXSourceLocation* inclusion_stack,
+                                   unsigned include_len,
+                                   CXClientData client_data);
+
+/**
+ * \brief Visit the set of preprocessor inclusions in a translation unit.
+ *   The visitor function is called with the provided data for every included
+ *   file.  This does not include headers included by the PCH file (unless one
+ *   is inspecting the inclusions in the PCH file itself).
+ */
+CINDEX_LINKAGE void clang_getInclusions(CXTranslationUnit tu,
+                                        CXInclusionVisitor visitor,
+                                        CXClientData client_data);
+
+/**
  * @}
  */
 

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

==============================================================================
--- cfe/trunk/include/clang/Basic/SourceManager.h (original)
+++ cfe/trunk/include/clang/Basic/SourceManager.h Tue Jan 26 13:31:51 2010
@@ -675,6 +675,11 @@
   void PrintStats() const;
 
   unsigned sloc_entry_size() const { return SLocEntryTable.size(); }
+  
+  // FIXME: Exposing this is a little gross; what we want is a good way
+  //  to iterate the entries that were not defined in a PCH file (or
+  //  any other external source).
+  unsigned sloc_loaded_entry_size() const { return SLocEntryLoaded.size(); }
 
   const SrcMgr::SLocEntry &getSLocEntry(unsigned ID) const {
     assert(ID < SLocEntryTable.size() && "Invalid id");

Added: cfe/trunk/test/Index/cindex-test-inclusions.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/cindex-test-inclusions.c?rev=94575&view=auto

==============================================================================
--- cfe/trunk/test/Index/cindex-test-inclusions.c (added)
+++ cfe/trunk/test/Index/cindex-test-inclusions.c Tue Jan 26 13:31:51 2010
@@ -0,0 +1,13 @@
+// RUN: c-index-test -test-inclusion-stack-source %s 2>&1 | FileCheck %s
+
+#include "include_test.h"
+
+// CHECK: cindex-test-inclusions.c
+// CHECK: included by:
+// CHECK: include_test.h
+// CHECK: included by:
+// CHECK: cindex-test-inclusions.c:3:10
+// CHECK: include_test_2.h
+// CHECK: included by:
+// CHECK: include_test.h:1:10
+// CHECK: cindex-test-inclusions.c:3:10

Added: cfe/trunk/test/Index/include_test.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/include_test.h?rev=94575&view=auto

==============================================================================
--- cfe/trunk/test/Index/include_test.h (added)
+++ cfe/trunk/test/Index/include_test.h Tue Jan 26 13:31:51 2010
@@ -0,0 +1 @@
+#include "include_test_2.h"

Added: cfe/trunk/test/Index/include_test_2.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/include_test_2.h?rev=94575&view=auto

==============================================================================
    (empty)

Modified: cfe/trunk/tools/CIndex/CIndex.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.exports?rev=94575&r1=94574&r2=94575&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.exports (original)
+++ cfe/trunk/tools/CIndex/CIndex.exports Tue Jan 26 13:31:51 2010
@@ -28,6 +28,7 @@
 _clang_getFile
 _clang_getFileName
 _clang_getFileTime
+_clang_getInclusions
 _clang_getInstantiationLocation
 _clang_getLocation
 _clang_getNullCursor

Added: cfe/trunk/tools/CIndex/CIndexInclusionStack.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndexInclusionStack.cpp?rev=94575&view=auto

==============================================================================
--- cfe/trunk/tools/CIndex/CIndexInclusionStack.cpp (added)
+++ cfe/trunk/tools/CIndex/CIndexInclusionStack.cpp Tue Jan 26 13:31:51 2010
@@ -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

Modified: cfe/trunk/tools/CIndex/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CMakeLists.txt?rev=94575&r1=94574&r2=94575&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CMakeLists.txt (original)
+++ cfe/trunk/tools/CIndex/CMakeLists.txt Tue Jan 26 13:31:51 2010
@@ -21,6 +21,7 @@
 add_clang_library(CIndex
   CIndex.cpp
   CIndexCodeCompletion.cpp
+  CIndexInclusionStack.cpp
   CIndexUSRs.cpp
   CIndexer.cpp
   CXCursor.cpp

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=94575&r1=94574&r2=94575&view=diff

==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Tue Jan 26 13:31:51 2010
@@ -303,6 +303,29 @@
 }
 
 /******************************************************************************/
+/* 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 @@
     "       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 @@
                              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;
 }





More information about the cfe-commits mailing list