break;
}
}
+ void VisitVarDecl(VarDecl *ND) {
+ Call(CXCursor_VarDecl, ND);
+ }
void VisitFunctionDecl(FunctionDecl *ND) {
Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
: CXCursor_FunctionDecl, ND);
CXClientData CData;
void Call(enum CXCursorKind CK, NamedDecl *ND) {
+ // Disable the callback when the context is equal to the visiting decl.
+ if (CDecl == ND && !clang_isReference(CK))
+ return;
CXCursor C = { CK, ND };
Callback(CDecl, C, CData);
}
CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
CDecl(C), Callback(cback), CData(D) {}
+ void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
+ // Issue callbacks for the containing class.
+ Call(CXCursor_ObjCClassRef, ND);
+ // FIXME: Issue callbacks for protocol refs.
+ VisitDeclContext(dyn_cast<DeclContext>(ND));
+ }
void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
- // Issue callbacks for super class and protocols.
+ // Issue callbacks for super class.
if (D->getSuperClass())
Call(CXCursor_ObjCSuperClassRef, D);
+ // FIXME: Issue callbacks for protocol refs.
VisitDeclContext(dyn_cast<DeclContext>(D));
}
void VisitTagDecl(TagDecl *D) {
void VisitFieldDecl(FieldDecl *ND) {
Call(CXCursor_FieldDecl, ND);
}
+ void VisitVarDecl(VarDecl *ND) {
+ Call(CXCursor_VarDecl, ND);
+ }
+ void VisitParmVarDecl(ParmVarDecl *ND) {
+ Call(CXCursor_ParmDecl, ND);
+ }
void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
Call(CXCursor_ObjCPropertyDecl, ND);
}
void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Call(CXCursor_ObjCIvarDecl, ND);
}
+ void VisitFunctionDecl(FunctionDecl *ND) {
+ if (ND->isThisDeclarationADefinition()) {
+ VisitDeclContext(dyn_cast<DeclContext>(ND));
+ }
+ }
void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
if (ND->getBody()) {
Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
: CXCursor_ObjCClassMethodDefn, ND);
- // FIXME: load body.
+ VisitDeclContext(dyn_cast<DeclContext>(ND));
} else
Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
: CXCursor_ObjCClassMethodDecl, ND);
return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getFileManager(), &ErrMsg);
}
+const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
+{
+ assert(CTUnit && "Passed null CXTranslationUnit");
+ //ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
+ //return CXXUnit->getOriginalSourceFileName().c_str();
+ return "<unimplemented>";
+}
void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
CXTranslationUnitIterator callback,
assert(OID && "clang_getCursorLine(): Missing interface decl");
return OID->getSuperClass()->getIdentifier()->getName();
}
+ case CXCursor_ObjCClassRef:
+ {
+ ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
+ assert(OID && "clang_getCursorLine(): Missing category decl");
+ return OID->getClassInterface()->getIdentifier()->getName();
+ }
default:
return "<not implemented>";
}
case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
+ case CXCursor_ObjCClassRef: return "ObjCClassRef";
default: return "<not implemented>";
}
}
#include "clang-c/Index.h"
#include <stdio.h>
+#include <string.h>
+
+static void PrintCursor(CXCursor Cursor) {
+ printf("%s => %s\n", clang_getCursorKindSpelling(Cursor.kind),
+ clang_getCursorSpelling(Cursor));
+}
static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
{
if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
- printf("%s => %s", clang_getCursorKindSpelling(Cursor.kind),
- clang_getCursorSpelling(Cursor));
- printf(" (%s,%d:%d)\n", clang_getCursorSource(Cursor),
- clang_getCursorLine(Cursor),
- clang_getCursorColumn(Cursor));
+ PrintCursor(Cursor);
+ printf(" Context: %s\n", clang_getDeclSpelling(Dcl));
+ printf(" Source: %s (%d:%d)\n", clang_getCursorSource(Cursor),
+ clang_getCursorLine(Cursor),
+ clang_getCursorColumn(Cursor));
}
}
static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
CXClientData Filter)
{
if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
- printf("%s => %s", clang_getCursorKindSpelling(Cursor.kind),
- clang_getCursorSpelling(Cursor));
- printf(" (%s,%d:%d)\n", clang_getCursorSource(Cursor),
- clang_getCursorLine(Cursor),
- clang_getCursorColumn(Cursor));
+ PrintCursor(Cursor);
+ printf(" Context: %s\n", clang_getTranslationUnitSpelling(Unit));
+ printf(" Source: %s (%d:%d)\n", clang_getCursorSource(Cursor),
+ clang_getCursorLine(Cursor),
+ clang_getCursorColumn(Cursor));
clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
}
CXIndex Idx = clang_createIndex();
CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]);
- clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
+ if (argc == 2)
+ clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
+ else if (argc == 3) {
+ enum CXCursorKind K = CXCursor_Invalid;
+
+ if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl;
+ else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl;
+ else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl;
+ else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl;
+ else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl;
+
+ clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K);
+ }
return 1;
}