<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">Committed in r176806, thanks!<div><br><div><div>On Mar 10, 2013, at 5:55 AM, "Benyei, Guy" <<a href="mailto:guy.benyei@intel.com">guy.benyei@intel.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">Hi Argyrios,<br>The file-includes test you've added fails in Windows, since it doesn't expect backslash characters in the paths. Also, the freeing of a 'const char **' in c-index-test.c triggers a warning in windows.<br>Attached a fix for both issues - please review.<br><br>Thanks<br>   Guy<br><br><br>-----Original Message-----<br>From: <a href="mailto:cfe-commits-bounces@cs.uiuc.edu">cfe-commits-bounces@cs.uiuc.edu</a> [<a href="mailto:cfe-commits-bounces@cs.uiuc.edu">mailto:cfe-commits-bounces@cs.uiuc.edu</a>] On Behalf Of Argyrios Kyrtzidis<br>Sent: Friday, March 08, 2013 04:33<br>To: <a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>Subject: r176682 - [libclang] Introduce clang_findIncludesInFile, that can be used to retrieve all #import/#include directives in a specific file.<br><br>Author: akirtzidis<br>Date: Thu Mar  7 20:32:34 2013<br>New Revision: 176682<br><br>URL: <a href="http://llvm.org/viewvc/llvm-project?rev=176682&view=rev">http://llvm.org/viewvc/llvm-project?rev=176682&view=rev</a><br>Log:<br>[libclang] Introduce clang_findIncludesInFile, that can be used to retrieve all #import/#include directives in a specific file.<br><br>It passes to the visitor, that the caller provides, CXCursor_InclusionDirective cursors for all the include directives in a particular file.<br><br>Added:<br>   cfe/trunk/test/Index/file-includes.c<br>Modified:<br>   cfe/trunk/include/clang-c/Index.h<br>   cfe/trunk/lib/Serialization/ASTReader.cpp<br>   cfe/trunk/tools/c-index-test/c-index-test.c<br>   cfe/trunk/tools/libclang/CIndexHigh.cpp<br>   cfe/trunk/tools/libclang/libclang.exports<br><br>Modified: cfe/trunk/include/clang-c/Index.h<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=176682&r1=176681&r2=176682&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=176682&r1=176681&r2=176682&view=diff</a><br>==============================================================================<br>--- cfe/trunk/include/clang-c/Index.h (original)<br>+++ cfe/trunk/include/clang-c/Index.h Thu Mar  7 20:32:34 2013<br>@@ -32,7 +32,7 @@<br> * compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable.<br> */<br>#define CINDEX_VERSION_MAJOR 0<br>-#define CINDEX_VERSION_MINOR 12<br>+#define CINDEX_VERSION_MINOR 13<br><br>#define CINDEX_VERSION_ENCODE(major, minor) ( \<br>      ((major) * 10000)                       \<br>@@ -5021,6 +5021,19 @@ typedef struct {<br>CINDEX_LINKAGE void clang_findReferencesInFile(CXCursor cursor, CXFile file,<br>                                               CXCursorAndRangeVisitor visitor);<br><br>+/**<br>+ * \brief Find #import/#include directives in a specific file.<br>+ *<br>+ * \param TU translation unit containing the file to query.<br>+ *<br>+ * \param file to search for #import/#include directives.<br>+ *<br>+ * \param visitor callback that will receive pairs of<span class="Apple-converted-space"> </span><br>+CXCursor/CXSourceRange for<br>+ * each directive found.<br>+ */<br>+CINDEX_LINKAGE void clang_findIncludesInFile(CXTranslationUnit TU, CXFile file,<br>+                                             CXCursorAndRangeVisitor<span class="Apple-converted-space"> </span><br>+visitor);<br>+<br>#ifdef __has_feature<br>#  if __has_feature(blocks)<br><br>@@ -5031,6 +5044,10 @@ CINDEX_LINKAGE<br>void clang_findReferencesInFileWithBlock(CXCursor, CXFile,<br>                                         CXCursorAndRangeVisitorBlock);<br><br>+CINDEX_LINKAGE<br>+void clang_findIncludesInFileWithBlock(CXTranslationUnit, CXFile,<br>+                                       CXCursorAndRangeVisitorBlock);<br>+<br>#  endif<br>#endif<br><br><br>Modified: cfe/trunk/lib/Serialization/ASTReader.cpp<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=176682&r1=176681&r2=176682&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=176682&r1=176681&r2=176682&view=diff</a><br>==============================================================================<br>--- cfe/trunk/lib/Serialization/ASTReader.cpp (original)<br>+++ cfe/trunk/lib/Serialization/ASTReader.cpp Thu Mar  7 20:32:34 2013<br>@@ -4000,7 +4000,7 @@ ASTReader::findBeginPreprocessedEntity(S<br><br>  GlobalSLocOffsetMapType::const_iterator<br>    SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -<br>-                                        BLoc.getOffset());<br>+                                        BLoc.getOffset() - 1);<br>  assert(SLocMapI != GlobalSLocOffsetMap.end() &&<br>         "Corrupted global sloc offset map");<br><br>@@ -4048,7 +4048,7 @@ ASTReader::findEndPreprocessedEntity(Sou<br><br>  GlobalSLocOffsetMapType::const_iterator<br>    SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -<br>-                                        ELoc.getOffset());<br>+                                        ELoc.getOffset() - 1);<br>  assert(SLocMapI != GlobalSLocOffsetMap.end() &&<br>         "Corrupted global sloc offset map");<br><br><br>Added: cfe/trunk/test/Index/file-includes.c<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/file-includes.c?rev=176682&view=auto">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/file-includes.c?rev=176682&view=auto</a><br>==============================================================================<br>--- cfe/trunk/test/Index/file-includes.c (added)<br>+++ cfe/trunk/test/Index/file-includes.c Thu Mar  7 20:32:34 2013<br>@@ -0,0 +1,24 @@<br>+<br>+#include "targeted-top.h"<br>+#include "targeted-preamble.h"<br>+<br>+extern int LocalVar;<br>+int LocalVar;<br>+<br>+// RUN: c-index-test -write-pch %t.h.pch %S/targeted-top.h -Xclang<span class="Apple-converted-space"> </span><br>+-detailed-preprocessing-record<br>+<br>+// RUN: c-index-test -file-includes-in=%s %s | FileCheck %s<span class="Apple-converted-space"> </span><br>+-check-prefix=LOCAL // RUN: env CINDEXTEST_EDITING=1 c-index-test<span class="Apple-converted-space"> </span><br>+-file-includes-in=%s %s | FileCheck %s -check-prefix=LOCAL // RUN:<span class="Apple-converted-space"> </span><br>+c-index-test -file-includes-in=%s %s -include %t.h | FileCheck %s<span class="Apple-converted-space"> </span><br>+-check-prefix=LOCAL // RUN: env CINDEXTEST_EDITING=1 c-index-test<span class="Apple-converted-space"> </span><br>+-file-includes-in=%s %s -include %t.h | FileCheck %s<span class="Apple-converted-space"> </span><br>+-check-prefix=LOCAL<br>+<br>+// LOCAL: inclusion directive=targeted-top.h<span class="Apple-converted-space"> </span><br>+({{.*}}/test/Index/targeted-top.h) {{.*}}=[2:1 - 2:2] // LOCAL:<span class="Apple-converted-space"> </span><br>+inclusion directive=targeted-preamble.h<span class="Apple-converted-space"> </span><br>+({{.*}}/test/Index/targeted-preamble.h) =[3:1 - 3:2]<br>+<br>+// RUN: c-index-test -file-includes-in=%S/targeted-top.h %s | FileCheck<span class="Apple-converted-space"> </span><br>+%s -check-prefix=TOP // RUN: env CINDEXTEST_EDITING=1 c-index-test<span class="Apple-converted-space"> </span><br>+-file-includes-in=%S/targeted-top.h %s | FileCheck %s -check-prefix=TOP<span class="Apple-converted-space"> </span><br>+// RUN: c-index-test -file-includes-in=%S/targeted-top.h %s -include<span class="Apple-converted-space"> </span><br>+%t.h | FileCheck %s -check-prefix=TOP // RUN: env CINDEXTEST_EDITING=1<span class="Apple-converted-space"> </span><br>+c-index-test -file-includes-in=%S/targeted-top.h %s -include %t.h |<span class="Apple-converted-space"> </span><br>+FileCheck %s -check-prefix=TOP<br>+<br>+// TOP: inclusion directive=targeted-nested1.h<span class="Apple-converted-space"> </span><br>+({{.*}}/test/Index/targeted-nested1.h) =[5:1 - 5:2] // TOP: inclusion<span class="Apple-converted-space"> </span><br>+directive=targeted-fields.h ({{.*}}/test/Index/targeted-fields.h)<span class="Apple-converted-space"> </span><br>+=[16:1 - 16:2]<br><br>Modified: cfe/trunk/tools/c-index-test/c-index-test.c<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=176682&r1=176681&r2=176682&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=176682&r1=176681&r2=176682&view=diff</a><br>==============================================================================<br>--- cfe/trunk/tools/c-index-test/c-index-test.c (original)<br>+++ cfe/trunk/tools/c-index-test/c-index-test.c Thu Mar  7 20:32:34 2013<br>@@ -2135,6 +2135,99 @@ static int find_file_refs_at(int argc, c<br>  return 0;<br>}<br><br>+static enum CXVisitorResult findFileIncludesVisit(void *context,<br>+                                         CXCursor cursor, CXSourceRange<span class="Apple-converted-space"> </span><br>+range) {<br>+  PrintCursor(cursor, NULL);<br>+  PrintRange(range, "");<br>+  printf("\n");<br>+  return CXVisit_Continue;<br>+}<br>+<br>+static int find_file_includes_in(int argc, const char **argv) {<br>+  CXIndex CIdx;<br>+  struct CXUnsavedFile *unsaved_files = 0;<br>+  int num_unsaved_files = 0;<br>+  CXTranslationUnit TU;<br>+  const char **Filenames = 0;<br>+  unsigned NumFilenames = 0;<br>+  unsigned Repeats = 1;<br>+  unsigned I, FI;<br>+<br>+  /* Count the number of locations. */<br>+  while (strstr(argv[NumFilenames+1], "-file-includes-in=") == argv[NumFilenames+1])<br>+    ++NumFilenames;<br>+<br>+  /* Parse the locations. */<br>+  assert(NumFilenames > 0 && "Unable to count filenames?");  Filenames<span class="Apple-converted-space"> </span><br>+ = (const char **)malloc(NumFilenames * sizeof(const char *));  for (I<span class="Apple-converted-space"> </span><br>+ = 0; I < NumFilenames; ++I) {<br>+    const char *input = argv[I + 1] + strlen("-file-includes-in=");<br>+    /* Copy the file name. */<br>+    Filenames[I] = input;<br>+  }<br>+<br>+  if (parse_remapped_files(argc, argv, NumFilenames + 1, &unsaved_files,<br>+                           &num_unsaved_files))<br>+    return -1;<br>+<br>+  if (getenv("CINDEXTEST_EDITING"))<br>+    Repeats = 2;<br>+<br>+  /* Parse the translation unit. When we're testing clang_getCursor() after<br>+     reparsing, don't remap unsaved files until the second parse. */  <br>+ CIdx = clang_createIndex(1, 1);  TU = clang_parseTranslationUnit(CIdx,<span class="Apple-converted-space"> </span><br>+ argv[argc - 1],<br>+                                  argv + num_unsaved_files + 1 + NumFilenames,<br>+                                  argc - num_unsaved_files - 2 - NumFilenames,<br>+                                  unsaved_files,<br>+                                  Repeats > 1? 0 : num_unsaved_files,<br>+                                  getDefaultParsingOptions());<br>+<br>+  if (!TU) {<br>+    fprintf(stderr, "unable to parse input\n");<br>+    return -1;<br>+  }<br>+<br>+  if (checkForErrors(TU) != 0)<br>+    return -1;<br>+<br>+  for (I = 0; I != Repeats; ++I) {<br>+    if (Repeats > 1 &&<br>+        clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,<br>+                                     clang_defaultReparseOptions(TU))) {<br>+      clang_disposeTranslationUnit(TU);<br>+      return 1;<br>+    }<br>+<br>+    if (checkForErrors(TU) != 0)<br>+      return -1;<br>+<br>+    for (FI = 0; FI < NumFilenames; ++FI) {<br>+      CXFile file = clang_getFile(TU, Filenames[FI]);<br>+      if (!file)<br>+        continue;<br>+<br>+      if (checkForErrors(TU) != 0)<br>+        return -1;<br>+<br>+      if (I + 1 == Repeats) {<br>+        CXCursorAndRangeVisitor visitor = { 0, findFileIncludesVisit };<br>+        clang_findIncludesInFile(TU, file, visitor);<br>+<br>+        if (checkForErrors(TU) != 0)<br>+          return -1;<br>+      }<br>+    }<br>+  }<br>+<br>+  PrintDiagnostics(TU);<br>+  clang_disposeTranslationUnit(TU);<br>+  clang_disposeIndex(CIdx);<br>+  free(Filenames);<br>+  free_remapped_files(unsaved_files, num_unsaved_files);<br>+  return 0;<br>+}<br>+<br>#define MAX_IMPORTED_ASTFILES 200<br><br>typedef struct {<br>@@ -3520,7 +3613,8 @@ static void print_usage(void) {<br>    "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"<br>    "       c-index-test -code-completion-timing=<site> <compiler arguments>\n"<br>    "       c-index-test -cursor-at=<site> <compiler arguments>\n"<br>-    "       c-index-test -file-refs-at=<site> <compiler arguments>\n");<br>+    "       c-index-test -file-refs-at=<site> <compiler arguments>\n"<br>+    "       c-index-test -file-includes-in=<filename> <compiler arguments>\n");<br>  fprintf(stderr,<br>    "       c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"<br>    "       c-index-test -index-file-full [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"<br>@@ -3582,6 +3676,8 @@ int cindextest_main(int argc, const char<br>    return inspect_cursor_at(argc, argv);<br>  if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])<br>    return find_file_refs_at(argc, argv);<br>+  if (argc > 2 && strstr(argv[1], "-file-includes-in=") == argv[1])<br>+    return find_file_includes_in(argc, argv);<br>  if (argc > 2 && strcmp(argv[1], "-index-file") == 0)<br>    return index_file(argc - 2, argv + 2, /*full=*/0);<br>  if (argc > 2 && strcmp(argv[1], "-index-file-full") == 0)<br><br>Modified: cfe/trunk/tools/libclang/CIndexHigh.cpp<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexHigh.cpp?rev=176682&r1=176681&r2=176682&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexHigh.cpp?rev=176682&r1=176681&r2=176682&view=diff</a><br>==============================================================================<br>--- cfe/trunk/tools/libclang/CIndexHigh.cpp (original)<br>+++ cfe/trunk/tools/libclang/CIndexHigh.cpp Thu Mar  7 20:32:34 2013<br>@@ -337,6 +337,72 @@ static void findMacroRefsInFile(CXTransl<br>  FindMacroRefsVisitor.visitPreprocessedEntitiesInRegion();<br>}<br><br>+namespace {<br>+<br>+struct FindFileIncludesVisitor {<br>+  ASTUnit &Unit;<br>+  const FileEntry *File;<br>+  CXCursorAndRangeVisitor visitor;<br>+<br>+  FindFileIncludesVisitor(ASTUnit &Unit, const FileEntry *File,<br>+                          CXCursorAndRangeVisitor visitor)<br>+    : Unit(Unit), File(File), visitor(visitor) { }<br>+<br>+  ASTContext &getASTContext() const {<br>+    return Unit.getASTContext();<br>+  }<br>+<br>+  enum CXChildVisitResult visit(CXCursor cursor, CXCursor parent) {<br>+    if (cursor.kind != CXCursor_InclusionDirective)<br>+      return CXChildVisit_Continue;<br>+<br>+    SourceLocation<br>+      Loc =<span class="Apple-converted-space"> </span><br>+ cxloc::translateSourceLocation(clang_getCursorLocation(cursor));<br>+<br>+    ASTContext &Ctx = getASTContext();<br>+    SourceManager &SM = Ctx.getSourceManager();<br>+<br>+    // We are looking for includes in a specific file.<br>+    std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);<br>+    if (SM.getFileEntryForID(LocInfo.first) != File)<br>+      return CXChildVisit_Continue;<br>+<br>+    if (visitor.visit(visitor.context, cursor,<br>+                      cxloc::translateSourceRange(Ctx, Loc)) == CXVisit_Break)<br>+      return CXChildVisit_Break;<br>+    return CXChildVisit_Continue;<br>+  }<br>+<br>+  static enum CXChildVisitResult visit(CXCursor cursor, CXCursor parent,<br>+                                       CXClientData client_data) {<br>+    return static_cast<FindFileIncludesVisitor*>(client_data)-><br>+                                                          visit(cursor,<span class="Apple-converted-space"> </span><br>+parent);<br>+  }<br>+};<br>+<br>+} // anonymous namespace<br>+<br>+static void findIncludesInFile(CXTranslationUnit TU, const FileEntry *File,<br>+                               CXCursorAndRangeVisitor Visitor) {<br>+  assert(TU && File && Visitor.visit);<br>+<br>+  ASTUnit *Unit = cxtu::getASTUnit(TU);  SourceManager &SM =<span class="Apple-converted-space"> </span><br>+ Unit->getSourceManager();<br>+<br>+  FileID FID = SM.translateFile(File);<br>+<br>+  FindFileIncludesVisitor IncludesVisitor(*Unit, File, Visitor);<br>+<br>+  SourceRange Range(SM.getLocForStartOfFile(FID),<span class="Apple-converted-space"> </span><br>+SM.getLocForEndOfFile(FID));<br>+  CursorVisitor InclusionCursorsVisitor(TU,<br>+                                        FindFileIncludesVisitor::visit,<br>+                                        &IncludesVisitor,<br>+                                        /*VisitPreprocessorLast=*/false,<br>+                                        /*VisitIncludedEntities=*/false,<br>+                                        Range);<br>+  InclusionCursorsVisitor.visitPreprocessedEntitiesInRegion();<br>+}<br>+<br><br>//===----------------------------------------------------------------------===//<br>// libclang public APIs.<br>@@ -410,6 +476,38 @@ void clang_findReferencesInFile(CXCursor<br>                   visitor);<br>}<br><br>+void clang_findIncludesInFile(CXTranslationUnit TU, CXFile file,<br>+                              CXCursorAndRangeVisitor visitor) {<br>+  LogRef Log = Logger::make(LLVM_FUNCTION_NAME);<br>+<br>+  if (!TU) {<br>+    if (Log)<br>+      *Log << "Null CXTranslationUnit";<br>+    return;<br>+  }<br>+  if (!file) {<br>+    if (Log)<br>+      *Log << "Null file";<br>+    return;<br>+  }<br>+  if (!visitor.visit) {<br>+    if (Log)<br>+      *Log << "Null visitor";<br>+    return;<br>+  }<br>+<br>+  if (Log)<br>+    *Log << TU << " @" << static_cast<const FileEntry *>(file);<br>+<br>+  ASTUnit *CXXUnit = cxtu::getASTUnit(TU);  if (!CXXUnit)<br>+    return;<br>+<br>+  ASTUnit::ConcurrencyCheck Check(*CXXUnit);<br>+<br>+  findIncludesInFile(TU, static_cast<const FileEntry *>(file),<span class="Apple-converted-space"> </span><br>+visitor); }<br>+<br>static enum CXVisitorResult _visitCursorAndRange(void *context,<br>                                                 CXCursor cursor,<br>                                                 CXSourceRange range) { @@ -425,5 +523,13 @@ void clang_findReferencesInFileWithBlock<br>  return clang_findReferencesInFile(cursor, file, visitor);  }<br><br>+void clang_findIncludesInFileWithBlock(CXTranslationUnit TU,<br>+                                       CXFile file,<br>+                                       CXCursorAndRangeVisitorBlock<span class="Apple-converted-space"> </span><br>+block) {<br>+  CXCursorAndRangeVisitor visitor = { block,<br>+                                      block ? _visitCursorAndRange : 0<span class="Apple-converted-space"> </span><br>+};<br>+  return clang_findIncludesInFile(TU, file, visitor); }<br>+<br>} // end: extern "C"<br><br><br>Modified: cfe/trunk/tools/libclang/libclang.exports<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=176682&r1=176681&r2=176682&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=176682&r1=176681&r2=176682&view=diff</a><br>==============================================================================<br>--- cfe/trunk/tools/libclang/libclang.exports (original)<br>+++ cfe/trunk/tools/libclang/libclang.exports Thu Mar  7 20:32:34 2013<br>@@ -98,6 +98,8 @@ clang_equalLocations<br>clang_equalRanges<br>clang_equalTypes<br>clang_executeOnThread<br>+clang_findIncludesInFile<br>+clang_findIncludesInFileWithBlock<br>clang_findReferencesInFile<br>clang_findReferencesInFileWithBlock<br>clang_formatDiagnostic<br><br><br>_______________________________________________<br>cfe-commits mailing list<br><a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits<br>---------------------------------------------------------------------<br>Intel Israel (74) Limited<br><br>This e-mail and any attachments may contain confidential material for<br>the sole use of the intended recipient(s). Any review or distribution<br>by others is strictly prohibited. If you are not the intended<br>recipient, please contact the sender and delete all copies.<br><span><cindex_win.patch></span></div></blockquote></div><br></div></body></html>