r176716 - [libclang] Modify clang_findReferencesInFile and clang_findIncludesInFile to return a value.

Argyrios Kyrtzidis akyrtzi at gmail.com
Fri Mar 8 12:42:33 PST 2013


Author: akirtzidis
Date: Fri Mar  8 14:42:33 2013
New Revision: 176716

URL: http://llvm.org/viewvc/llvm-project?rev=176716&view=rev
Log:
[libclang] Modify clang_findReferencesInFile and clang_findIncludesInFile to return a value.

Possible values are:
 1  : if a parameter was invalid
 -1 : if the callback returned CXVisit_Break,
 otherwise returns 0.

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/tools/libclang/CIndex.cpp
    cfe/trunk/tools/libclang/CIndexHigh.cpp
    cfe/trunk/tools/libclang/CursorVisitor.h

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=176716&r1=176715&r2=176716&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Fri Mar  8 14:42:33 2013
@@ -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_findReferences
  *
  * \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

Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=176716&r1=176715&r2=176716&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Fri Mar  8 14:42:33 2013
@@ -223,9 +223,9 @@ static bool visitPreprocessedEntitiesInR
                                            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,
   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::visitDeclsFromFileRe
     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::visitDeclsFromFileRe
     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::visitDeclsFromFileRe
     }
 
     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::visitDeclsFromFileRe
       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() {

Modified: cfe/trunk/tools/libclang/CIndexHigh.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexHigh.cpp?rev=176716&r1=176715&r2=176716&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndexHigh.cpp (original)
+++ cfe/trunk/tools/libclang/CIndexHigh.cpp Fri Mar  8 14:42:33 2013
@@ -212,25 +212,24 @@ static enum CXChildVisitResult findFileI
   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(CXTranslati
                                   /*VisitIncludedEntities=*/false,
                                   Range,
                                   /*VisitDeclsOnly=*/true);
-  FindIdRefsVisitor.visitFileRegion();
+  return FindIdRefsVisitor.visitFileRegion();
 }
 
 namespace {
@@ -307,12 +306,12 @@ static enum CXChildVisitResult findFileM
   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(CXTransl
   else
     Macro = getCursorMacroExpansion(Cursor).getName();
   if (!Macro)
-    return;
+    return false;
 
   FindFileMacroRefVisitData data(*Unit, File, Macro, Visitor);
 
@@ -334,7 +333,7 @@ static void findMacroRefsInFile(CXTransl
                                   /*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(CXTransla
                                         /*VisitPreprocessorLast=*/false,
                                         /*VisitIncludedEntities=*/false,
                                         Range);
-  InclusionCursorsVisitor.visitPreprocessedEntitiesInRegion();
+  return InclusionCursorsVisitor.visitPreprocessedEntitiesInRegion();
 }
 
 
@@ -410,29 +409,29 @@ static void findIncludesInFile(CXTransla
 
 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
 
   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
   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(CXTranslat
 
   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 _visitCursor
   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);

Modified: cfe/trunk/tools/libclang/CursorVisitor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CursorVisitor.h?rev=176716&r1=176715&r2=176716&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CursorVisitor.h (original)
+++ cfe/trunk/tools/libclang/CursorVisitor.h Fri Mar  8 14:42:33 2013
@@ -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();
 





More information about the cfe-commits mailing list