- clang_getCursor(): Replace asserts with error codes (CXCursor_InvalidFile, CXCursor_NoDeclFound).

- Add predicate clang_isInvalid().
- Implement clang_getCursorFromDecl().

llvm-svn: 81908
This commit is contained in:
Steve Naroff 2009-09-15 20:25:34 +00:00
parent 4fb9cde8ef
commit 54f22fb1de
4 changed files with 46 additions and 17 deletions

View File

@ -37,8 +37,6 @@ typedef void *CXDecl; /* A specific declaration within a translation unit. */
/* Cursors represent declarations, definitions, and references. */ /* Cursors represent declarations, definitions, and references. */
enum CXCursorKind { enum CXCursorKind {
CXCursor_Invalid = 0,
/* Declarations */ /* Declarations */
CXCursor_FirstDecl = 1, CXCursor_FirstDecl = 1,
CXCursor_TypedefDecl = 2, CXCursor_TypedefDecl = 2,
@ -76,7 +74,14 @@ enum CXCursorKind {
CXCursor_ObjCMessageRef = 42, CXCursor_ObjCMessageRef = 42,
CXCursor_ObjCSelectorRef = 43, CXCursor_ObjCSelectorRef = 43,
CXCursor_ObjCClassRef = 44, 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. */ /* A cursor into the CXTranslationUnit. */
@ -172,6 +177,7 @@ enum CXCursorKind clang_getCursorKind(CXCursor);
unsigned clang_isDeclaration(enum CXCursorKind); unsigned clang_isDeclaration(enum CXCursorKind);
unsigned clang_isReference(enum CXCursorKind); unsigned clang_isReference(enum CXCursorKind);
unsigned clang_isDefinition(enum CXCursorKind); unsigned clang_isDefinition(enum CXCursorKind);
unsigned clang_isInvalid(enum CXCursorKind);
unsigned clang_getCursorLine(CXCursor); unsigned clang_getCursorLine(CXCursor);
unsigned clang_getCursorColumn(CXCursor); unsigned clang_getCursorColumn(CXCursor);

View File

@ -265,10 +265,6 @@ CXEntity clang_getEntity(const char *URI)
// //
// CXDecl Operations. // CXDecl Operations.
// //
CXCursor clang_getCursorFromDecl(CXDecl)
{
return CXCursor();
}
CXEntity clang_getEntityFromDecl(CXDecl) CXEntity clang_getEntityFromDecl(CXDecl)
{ {
return 0; return 0;
@ -347,6 +343,9 @@ const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef"; case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
case CXCursor_ObjCClassRef: return "ObjCClassRef"; case CXCursor_ObjCClassRef: return "ObjCClassRef";
case CXCursor_InvalidFile: return "InvalidFile";
case CXCursor_NoDeclFound: return "NoDeclFound";
case CXCursor_NotImplemented: return "NotImplemented";
default: return "<not implemented>"; default: return "<not implemented>";
} }
} }
@ -370,7 +369,7 @@ static enum CXCursorKind TranslateKind(Decl *D) {
} }
default: break; default: break;
} }
return CXCursor_Invalid; return CXCursor_NotImplemented;
} }
// //
// CXCursor Operations. // CXCursor Operations.
@ -384,20 +383,38 @@ CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
FileManager &FMgr = CXXUnit->getFileManager(); FileManager &FMgr = CXXUnit->getFileManager();
const FileEntry *File = FMgr.getFile(source_name, const FileEntry *File = FMgr.getFile(source_name,
source_name+strlen(source_name)); source_name+strlen(source_name));
assert(File && "clang_getCursor(): FileManager returned 0"); if (!File) {
CXCursor C = { CXCursor_InvalidFile, 0 };
return C;
}
SourceLocation SLoc = SourceLocation SLoc =
CXXUnit->getSourceManager().getLocation(File, line, column); CXXUnit->getSourceManager().getLocation(File, line, column);
ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc); ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
Decl *Dcl = ALoc.getDecl(); Decl *Dcl = ALoc.getDecl();
assert(Dcl && "clang_getCursor(): ASTLocation has a null decl"); if (Dcl) {
CXCursor C = { TranslateKind(Dcl), Dcl };
CXCursor C = { TranslateKind(Dcl), Dcl }; return C;
}
CXCursor C = { CXCursor_NoDeclFound, 0 };
return C; return C;
} }
CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
{
assert(AnonDecl && "Passed null CXDecl");
NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
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) unsigned clang_isDeclaration(enum CXCursorKind K)
{ {
return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;

View File

@ -17,6 +17,7 @@ _clang_createTranslationUnit
_clang_isDeclaration _clang_isDeclaration
_clang_isReference _clang_isReference
_clang_isDefinition _clang_isDefinition
_clang_isInvalid
_clang_getCursorSpelling _clang_getCursorSpelling
_clang_getCursorKindSpelling _clang_getCursorKindSpelling
_clang_getTranslationUnitSpelling _clang_getTranslationUnitSpelling

View File

@ -5,8 +5,11 @@
#include <string.h> #include <string.h>
static void PrintCursor(CXCursor Cursor) { static void PrintCursor(CXCursor Cursor) {
printf("%s => %s ", clang_getCursorKindSpelling(Cursor.kind), if (clang_isInvalid(Cursor.kind))
clang_getCursorSpelling(Cursor)); 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) 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 */ /* methodSignature - returns a cursor of type ObjCInstanceMethodDecl */
C = clang_getCursor(TU, "/System/Library/Frameworks/Foundation.framework/Headers/NSInvocation.h", 22, 1); C = clang_getCursor(TU, "/System/Library/Frameworks/Foundation.framework/Headers/NSInvocation.h", 22, 1);
PrintCursor(C); PrintCursor(C);
C = clang_getCursor(TU, "Large.m", 5, 18);
PrintCursor(C);
} else if (argc == 3) { } else if (argc == 3) {
enum CXCursorKind K = CXCursor_Invalid; enum CXCursorKind K = CXCursor_NotImplemented;
if (!strcmp(argv[2], "all")) { if (!strcmp(argv[2], "all")) {
clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0); clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);