diff options
author | Steve Naroff <snaroff@apple.com> | 2009-09-15 20:25:34 +0000 |
---|---|---|
committer | Steve Naroff <snaroff@apple.com> | 2009-09-15 20:25:34 +0000 |
commit | 77128ddd3077fc045751a55bb3226802b15d5510 (patch) | |
tree | 1c3b342c0ce5cf90fba48540b61e389d83bdeede | |
parent | 1fdd89ba1d14ee11fda5b9e1c171db428b1acb23 (diff) |
- clang_getCursor(): Replace asserts with error codes (CXCursor_InvalidFile, CXCursor_NoDeclFound).
- Add predicate clang_isInvalid().
- Implement clang_getCursorFromDecl().
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@81908 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/clang-c/Index.h | 12 | ||||
-rw-r--r-- | tools/CIndex/CIndex.cpp | 37 | ||||
-rw-r--r-- | tools/CIndex/CIndex.exports | 1 | ||||
-rw-r--r-- | tools/c-index-test/c-index-test.c | 11 |
4 files changed, 45 insertions, 16 deletions
diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index 9af3169157..030dfae985 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -37,8 +37,6 @@ typedef void *CXDecl; /* A specific declaration within a translation unit. */ /* Cursors represent declarations, definitions, and references. */ enum CXCursorKind { - CXCursor_Invalid = 0, - /* Declarations */ CXCursor_FirstDecl = 1, CXCursor_TypedefDecl = 2, @@ -76,7 +74,14 @@ enum CXCursorKind { CXCursor_ObjCMessageRef = 42, CXCursor_ObjCSelectorRef = 43, CXCursor_ObjCClassRef = 44, - CXCursor_LastRef = 44 + CXCursor_LastRef = 44, + + /* Error conditions */ + CXCursor_FirstInvalid = 70, + CXCursor_InvalidFile = 70, + CXCursor_NoDeclFound = 71, + CXCursor_NotImplemented = 72, + CXCursor_LastInvalid = 72 }; /* A cursor into the CXTranslationUnit. */ @@ -172,6 +177,7 @@ enum CXCursorKind clang_getCursorKind(CXCursor); unsigned clang_isDeclaration(enum CXCursorKind); unsigned clang_isReference(enum CXCursorKind); unsigned clang_isDefinition(enum CXCursorKind); +unsigned clang_isInvalid(enum CXCursorKind); unsigned clang_getCursorLine(CXCursor); unsigned clang_getCursorColumn(CXCursor); diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp index de9c0a370f..1efb50fab5 100644 --- a/tools/CIndex/CIndex.cpp +++ b/tools/CIndex/CIndex.cpp @@ -265,10 +265,6 @@ CXEntity clang_getEntity(const char *URI) // // CXDecl Operations. // -CXCursor clang_getCursorFromDecl(CXDecl) -{ - return CXCursor(); -} CXEntity clang_getEntityFromDecl(CXDecl) { return 0; @@ -347,6 +343,9 @@ const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef"; case CXCursor_ObjCClassRef: return "ObjCClassRef"; + case CXCursor_InvalidFile: return "InvalidFile"; + case CXCursor_NoDeclFound: return "NoDeclFound"; + case CXCursor_NotImplemented: return "NotImplemented"; default: return "<not implemented>"; } } @@ -370,7 +369,7 @@ static enum CXCursorKind TranslateKind(Decl *D) { } default: break; } - return CXCursor_Invalid; + return CXCursor_NotImplemented; } // // CXCursor Operations. @@ -383,21 +382,39 @@ CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name, FileManager &FMgr = CXXUnit->getFileManager(); const FileEntry *File = FMgr.getFile(source_name, - source_name+strlen(source_name)); - assert(File && "clang_getCursor(): FileManager returned 0"); - + source_name+strlen(source_name)); + if (!File) { + CXCursor C = { CXCursor_InvalidFile, 0 }; + return C; + } SourceLocation SLoc = CXXUnit->getSourceManager().getLocation(File, line, column); ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc); Decl *Dcl = ALoc.getDecl(); - assert(Dcl && "clang_getCursor(): ASTLocation has a null decl"); + if (Dcl) { + CXCursor C = { TranslateKind(Dcl), Dcl }; + return C; + } + CXCursor C = { CXCursor_NoDeclFound, 0 }; + return C; +} + +CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) +{ + assert(AnonDecl && "Passed null CXDecl"); + NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); - CXCursor C = { TranslateKind(Dcl), Dcl }; + CXCursor C = { TranslateKind(ND), ND }; return C; } +unsigned clang_isInvalid(enum CXCursorKind K) +{ + return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; +} + unsigned clang_isDeclaration(enum CXCursorKind K) { return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; diff --git a/tools/CIndex/CIndex.exports b/tools/CIndex/CIndex.exports index 7c9660b4d0..17aba04493 100644 --- a/tools/CIndex/CIndex.exports +++ b/tools/CIndex/CIndex.exports @@ -17,6 +17,7 @@ _clang_createTranslationUnit _clang_isDeclaration _clang_isReference _clang_isDefinition +_clang_isInvalid _clang_getCursorSpelling _clang_getCursorKindSpelling _clang_getTranslationUnitSpelling diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c index 101d71223a..e7043c1482 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -5,8 +5,11 @@ #include <string.h> static void PrintCursor(CXCursor Cursor) { - printf("%s => %s ", clang_getCursorKindSpelling(Cursor.kind), - clang_getCursorSpelling(Cursor)); + if (clang_isInvalid(Cursor.kind)) + printf("Invalid Cursor => %s\n", clang_getCursorKindSpelling(Cursor.kind)); + else + printf("%s => %s ", clang_getCursorKindSpelling(Cursor.kind), + clang_getCursorSpelling(Cursor)); } static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) @@ -47,8 +50,10 @@ int main(int argc, char **argv) { /* methodSignature - returns a cursor of type ObjCInstanceMethodDecl */ C = clang_getCursor(TU, "/System/Library/Frameworks/Foundation.framework/Headers/NSInvocation.h", 22, 1); PrintCursor(C); + C = clang_getCursor(TU, "Large.m", 5, 18); + PrintCursor(C); } else if (argc == 3) { - enum CXCursorKind K = CXCursor_Invalid; + enum CXCursorKind K = CXCursor_NotImplemented; if (!strcmp(argv[2], "all")) { clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0); |