[libclang] Modify clang_findReferencesInFile and clang_findIncludesInFile to return...
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Fri, 8 Mar 2013 20:42:33 +0000 (20:42 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Fri, 8 Mar 2013 20:42:33 +0000 (20:42 +0000)
Possible values are:
 1  : if a parameter was invalid
 -1 : if the callback returned CXVisit_Break,
 otherwise returns 0.

llvm-svn: 176716

clang/include/clang-c/Index.h
clang/tools/libclang/CIndex.cpp
clang/tools/libclang/CIndexHigh.cpp
clang/tools/libclang/CursorVisitor.h

index b9b4f8e..df7fa40 100644 (file)
@@ -32,7 +32,7 @@
  * compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable.
  */
 #define CINDEX_VERSION_MAJOR 0
-#define CINDEX_VERSION_MINOR 13
+#define CINDEX_VERSION_MINOR 14
 
 #define CINDEX_VERSION_ENCODE(major, minor) ( \
       ((major) * 10000)                       \
@@ -5017,9 +5017,14 @@ typedef struct {
  * each reference found.
  * The CXSourceRange will point inside the file; if the reference is inside
  * a macro (and not a macro argument) the CXSourceRange will be invalid.
+ *
+ * \returns
+ * 1  : if a parameter was invalid
+ * -1 : if the callback returned CXVisit_Break,
+ * otherwise returns 0.
  */
-CINDEX_LINKAGE void clang_findReferencesInFile(CXCursor cursor, CXFile file,
-                                               CXCursorAndRangeVisitor visitor);
+CINDEX_LINKAGE int clang_findReferencesInFile(CXCursor cursor, CXFile file,
+                                              CXCursorAndRangeVisitor visitor);
 
 /**
  * \brief Find #import/#include directives in a specific file.
@@ -5030,9 +5035,14 @@ CINDEX_LINKAGE void clang_findReferencesInFile(CXCursor cursor, CXFile file,
  *
  * \param visitor callback that will receive pairs of CXCursor/CXSourceRange for
  * each directive found.
+ *
+ * \returns
+ * 1  : if a parameter was invalid
+ * -1 : if the callback returned CXVisit_Break,
+ * otherwise returns 0.
  */
-CINDEX_LINKAGE void clang_findIncludesInFile(CXTranslationUnit TU, CXFile file,
-                                             CXCursorAndRangeVisitor visitor);
+CINDEX_LINKAGE int clang_findIncludesInFile(CXTranslationUnit TU, CXFile file,
+                                            CXCursorAndRangeVisitor visitor);
 
 #ifdef __has_feature
 #  if __has_feature(blocks)
@@ -5041,12 +5051,12 @@ typedef enum CXVisitorResult
     (^CXCursorAndRangeVisitorBlock)(CXCursor, CXSourceRange);
 
 CINDEX_LINKAGE
-void clang_findReferencesInFileWithBlock(CXCursor, CXFile,
-                                         CXCursorAndRangeVisitorBlock);
+int clang_findReferencesInFileWithBlock(CXCursor, CXFile,
+                                        CXCursorAndRangeVisitorBlock);
 
 CINDEX_LINKAGE
-void clang_findIncludesInFileWithBlock(CXTranslationUnit, CXFile,
-                                       CXCursorAndRangeVisitorBlock);
+int clang_findIncludesInFileWithBlock(CXTranslationUnit, CXFile,
+                                      CXCursorAndRangeVisitorBlock);
 
 #  endif
 #endif
index 9e8b2f4..a2449bd 100644 (file)
@@ -223,9 +223,9 @@ static bool visitPreprocessedEntitiesInRange(SourceRange R,
                                            PPRec, FID);
 }
 
-void CursorVisitor::visitFileRegion() {
+bool CursorVisitor::visitFileRegion() {
   if (RegionOfInterest.isInvalid())
-    return;
+    return false;
 
   ASTUnit *Unit = cxtu::getASTUnit(TU);
   SourceManager &SM = Unit->getSourceManager();
@@ -243,7 +243,7 @@ void CursorVisitor::visitFileRegion() {
 
   assert(Begin.first == End.first);
   if (Begin.second > End.second)
-    return;
+    return false;
   
   FileID File = Begin.first;
   unsigned Offset = Begin.second;
@@ -251,12 +251,15 @@ void CursorVisitor::visitFileRegion() {
 
   if (!VisitDeclsOnly && !VisitPreprocessorLast)
     if (visitPreprocessedEntitiesInRegion())
-      return; // visitation break.
+      return true; // visitation break.
 
-  visitDeclsFromFileRegion(File, Offset, Length);
+  if (visitDeclsFromFileRegion(File, Offset, Length))
+    return true; // visitation break.
 
   if (!VisitDeclsOnly && VisitPreprocessorLast)
-    visitPreprocessedEntitiesInRegion();
+    return visitPreprocessedEntitiesInRegion();
+
+  return false;
 }
 
 static bool isInLexicalContext(Decl *D, DeclContext *DC) {
@@ -271,7 +274,7 @@ static bool isInLexicalContext(Decl *D, DeclContext *DC) {
   return false;
 }
 
-void CursorVisitor::visitDeclsFromFileRegion(FileID File,
+bool CursorVisitor::visitDeclsFromFileRegion(FileID File,
                                              unsigned Offset, unsigned Length) {
   ASTUnit *Unit = cxtu::getASTUnit(TU);
   SourceManager &SM = Unit->getSourceManager();
@@ -286,7 +289,7 @@ void CursorVisitor::visitDeclsFromFileRegion(FileID File,
     bool Invalid = false;
     const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid);
     if (Invalid)
-      return;
+      return false;
 
     SourceLocation Outer;
     if (SLEntry.isFile())
@@ -294,7 +297,7 @@ void CursorVisitor::visitDeclsFromFileRegion(FileID File,
     else
       Outer = SLEntry.getExpansion().getExpansionLocStart();
     if (Outer.isInvalid())
-      return;
+      return false;
 
     llvm::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer);
     Length = 0;
@@ -337,11 +340,11 @@ void CursorVisitor::visitDeclsFromFileRegion(FileID File,
     }
 
     if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true))
-      break;
+      return true; // visitation break.
   }
 
   if (VisitedAtLeastOnce)
-    return;
+    return false;
 
   // No Decls overlapped with the range. Move up the lexical context until there
   // is a context that contains the range or we reach the translation unit
@@ -356,12 +359,14 @@ void CursorVisitor::visitDeclsFromFileRegion(FileID File,
       break;
 
     if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) {
-      Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true);
-      break;
+      if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true))
+        return true; // visitation break.
     }
 
     DC = D->getLexicalDeclContext();
   }
+
+  return false;
 }
 
 bool CursorVisitor::visitPreprocessedEntitiesInRegion() {
index 6af55e8..0457a32 100644 (file)
@@ -212,25 +212,24 @@ static enum CXChildVisitResult findFileIdRefVisit(CXCursor cursor,
   return CXChildVisit_Recurse;
 }
 
-static void findIdRefsInFile(CXTranslationUnit TU, CXCursor declCursor,
-                           const FileEntry *File,
-                           CXCursorAndRangeVisitor Visitor) {
+static bool findIdRefsInFile(CXTranslationUnit TU, CXCursor declCursor,
+                             const FileEntry *File,
+                             CXCursorAndRangeVisitor Visitor) {
   assert(clang_isDeclaration(declCursor.kind));
   SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager();
 
   FileID FID = SM.translateFile(File);
   const Decl *Dcl = cxcursor::getCursorDecl(declCursor);
   if (!Dcl)
-    return;
+    return false;
 
   FindFileIdRefVisitData data(TU, FID, Dcl,
                               cxcursor::getSelectorIdentifierIndex(declCursor),
                               Visitor);
 
   if (const DeclContext *DC = Dcl->getParentFunctionOrMethod()) {
-    clang_visitChildren(cxcursor::MakeCXCursor(cast<Decl>(DC), TU),
-                        findFileIdRefVisit, &data);
-    return;
+    return clang_visitChildren(cxcursor::MakeCXCursor(cast<Decl>(DC), TU),
+                               findFileIdRefVisit, &data);
   }
 
   SourceRange Range(SM.getLocForStartOfFile(FID), SM.getLocForEndOfFile(FID));
@@ -240,7 +239,7 @@ static void findIdRefsInFile(CXTranslationUnit TU, CXCursor declCursor,
                                   /*VisitIncludedEntities=*/false,
                                   Range,
                                   /*VisitDeclsOnly=*/true);
-  FindIdRefsVisitor.visitFileRegion();
+  return FindIdRefsVisitor.visitFileRegion();
 }
 
 namespace {
@@ -307,12 +306,12 @@ static enum CXChildVisitResult findFileMacroRefVisit(CXCursor cursor,
   return CXChildVisit_Continue;
 }
 
-static void findMacroRefsInFile(CXTranslationUnit TU, CXCursor Cursor,
+static bool findMacroRefsInFile(CXTranslationUnit TU, CXCursor Cursor,
                                 const FileEntry *File,
                                 CXCursorAndRangeVisitor Visitor) {
   if (Cursor.kind != CXCursor_MacroDefinition &&
       Cursor.kind != CXCursor_MacroExpansion)
-    return;
+    return false;
 
   ASTUnit *Unit = cxtu::getASTUnit(TU);
   SourceManager &SM = Unit->getSourceManager();
@@ -324,7 +323,7 @@ static void findMacroRefsInFile(CXTranslationUnit TU, CXCursor Cursor,
   else
     Macro = getCursorMacroExpansion(Cursor).getName();
   if (!Macro)
-    return;
+    return false;
 
   FindFileMacroRefVisitData data(*Unit, File, Macro, Visitor);
 
@@ -334,7 +333,7 @@ static void findMacroRefsInFile(CXTranslationUnit TU, CXCursor Cursor,
                                   /*VisitPreprocessorLast=*/false,
                                   /*VisitIncludedEntities=*/false,
                                   Range);
-  FindMacroRefsVisitor.visitPreprocessedEntitiesInRegion();
+  return FindMacroRefsVisitor.visitPreprocessedEntitiesInRegion();
 }
 
 namespace {
@@ -382,7 +381,7 @@ struct FindFileIncludesVisitor {
 
 } // anonymous namespace
 
-static void findIncludesInFile(CXTranslationUnit TU, const FileEntry *File,
+static bool findIncludesInFile(CXTranslationUnit TU, const FileEntry *File,
                                CXCursorAndRangeVisitor Visitor) {
   assert(TU && File && Visitor.visit);
 
@@ -400,7 +399,7 @@ static void findIncludesInFile(CXTranslationUnit TU, const FileEntry *File,
                                         /*VisitPreprocessorLast=*/false,
                                         /*VisitIncludedEntities=*/false,
                                         Range);
-  InclusionCursorsVisitor.visitPreprocessedEntitiesInRegion();
+  return InclusionCursorsVisitor.visitPreprocessedEntitiesInRegion();
 }
 
 
@@ -410,29 +409,29 @@ static void findIncludesInFile(CXTranslationUnit TU, const FileEntry *File,
 
 extern "C" {
 
-void clang_findReferencesInFile(CXCursor cursor, CXFile file,
-                                CXCursorAndRangeVisitor visitor) {
+int clang_findReferencesInFile(CXCursor cursor, CXFile file,
+                               CXCursorAndRangeVisitor visitor) {
   LogRef Log = Logger::make(LLVM_FUNCTION_NAME);
 
   if (clang_Cursor_isNull(cursor)) {
     if (Log)
       *Log << "Null cursor";
-    return;
+    return 1;
   }
   if (cursor.kind == CXCursor_NoDeclFound) {
     if (Log)
       *Log << "Got CXCursor_NoDeclFound";
-    return;
+    return 1;
   }
   if (!file) {
     if (Log)
       *Log << "Null file";
-    return;
+    return 1;
   }
   if (!visitor.visit) {
     if (Log)
       *Log << "Null visitor";
-    return;
+    return 1;
   }
 
   if (Log)
@@ -440,17 +439,18 @@ void clang_findReferencesInFile(CXCursor cursor, CXFile file,
 
   ASTUnit *CXXUnit = cxcursor::getCursorASTUnit(cursor);
   if (!CXXUnit)
-    return;
+    return 1;
 
   ASTUnit::ConcurrencyCheck Check(*CXXUnit);
 
   if (cursor.kind == CXCursor_MacroDefinition ||
       cursor.kind == CXCursor_MacroExpansion) {
-    findMacroRefsInFile(cxcursor::getCursorTU(cursor),
-                        cursor,
-                        static_cast<const FileEntry *>(file),
-                        visitor);
-    return;
+    if (findMacroRefsInFile(cxcursor::getCursorTU(cursor),
+                            cursor,
+                            static_cast<const FileEntry *>(file),
+                            visitor))
+      return -1; // visitation break;
+    return 0;
   }
 
   // We are interested in semantics of identifiers so for C++ constructor exprs
@@ -467,33 +467,35 @@ void clang_findReferencesInFile(CXCursor cursor, CXFile file,
   if (!clang_isDeclaration(refCursor.kind)) {
     if (Log)
       *Log << "cursor is not referencing a declaration";
-    return;
+    return 1;
   }
 
-  findIdRefsInFile(cxcursor::getCursorTU(cursor),
-                   refCursor,
-                   static_cast<const FileEntry *>(file),
-                   visitor);
+  if (findIdRefsInFile(cxcursor::getCursorTU(cursor),
+                       refCursor,
+                       static_cast<const FileEntry *>(file),
+                       visitor))
+    return -1; //visitation break;
+  return 0;
 }
 
-void clang_findIncludesInFile(CXTranslationUnit TU, CXFile file,
-                              CXCursorAndRangeVisitor visitor) {
+int clang_findIncludesInFile(CXTranslationUnit TU, CXFile file,
+                             CXCursorAndRangeVisitor visitor) {
   LogRef Log = Logger::make(LLVM_FUNCTION_NAME);
 
   if (!TU) {
     if (Log)
       *Log << "Null CXTranslationUnit";
-    return;
+    return 1;
   }
   if (!file) {
     if (Log)
       *Log << "Null file";
-    return;
+    return 1;
   }
   if (!visitor.visit) {
     if (Log)
       *Log << "Null visitor";
-    return;
+    return 1;
   }
 
   if (Log)
@@ -501,11 +503,13 @@ void clang_findIncludesInFile(CXTranslationUnit TU, CXFile file,
 
   ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
   if (!CXXUnit)
-    return;
+    return 1;
 
   ASTUnit::ConcurrencyCheck Check(*CXXUnit);
 
-  findIncludesInFile(TU, static_cast<const FileEntry *>(file), visitor);
+  if (findIncludesInFile(TU, static_cast<const FileEntry *>(file), visitor))
+    return -1; //visitation break;
+  return 0;
 }
 
 static enum CXVisitorResult _visitCursorAndRange(void *context,
@@ -515,17 +519,17 @@ static enum CXVisitorResult _visitCursorAndRange(void *context,
   return INVOKE_BLOCK2(block, cursor, range);
 }
 
-void clang_findReferencesInFileWithBlock(CXCursor cursor,
-                                         CXFile file,
-                                         CXCursorAndRangeVisitorBlock block) {
+int clang_findReferencesInFileWithBlock(CXCursor cursor,
+                                        CXFile file,
+                                        CXCursorAndRangeVisitorBlock block) {
   CXCursorAndRangeVisitor visitor = { block,
                                       block ? _visitCursorAndRange : 0 };
   return clang_findReferencesInFile(cursor, file, visitor);
 }
 
-void clang_findIncludesInFileWithBlock(CXTranslationUnit TU,
-                                       CXFile file,
-                                       CXCursorAndRangeVisitorBlock block) {
+int clang_findIncludesInFileWithBlock(CXTranslationUnit TU,
+                                      CXFile file,
+                                      CXCursorAndRangeVisitorBlock block) {
   CXCursorAndRangeVisitor visitor = { block,
                                       block ? _visitCursorAndRange : 0 };
   return clang_findIncludesInFile(TU, file, visitor);
index 38b7f33..53d864d 100644 (file)
@@ -116,7 +116,7 @@ private:
   /// \param R a half-open source range retrieved from the abstract syntax tree.
   RangeComparisonResult CompareRegionOfInterest(SourceRange R);
 
-  void visitDeclsFromFileRegion(FileID File, unsigned Offset, unsigned Length);
+  bool visitDeclsFromFileRegion(FileID File, unsigned Offset, unsigned Length);
 
   class SetParentRAII {
     CXCursor &Parent;
@@ -179,7 +179,7 @@ public:
 
   /// \brief Visit declarations and preprocessed entities for the file region
   /// designated by \see RegionOfInterest.
-  void visitFileRegion();
+  bool visitFileRegion();
   
   bool visitPreprocessedEntitiesInRegion();