[cfe-commits] r80921 - in /cfe/trunk: include/clang-c/Index.h tools/CIndex/CIndex.cpp tools/CIndex/CIndex.exports tools/c-index-test/c-index-test.c
Steve Naroff
snaroff at apple.com
Thu Sep 3 08:49:00 PDT 2009
Author: snaroff
Date: Thu Sep 3 10:49:00 2009
New Revision: 80921
URL: http://llvm.org/viewvc/llvm-project?rev=80921&view=rev
Log:
- Add back some harmless code that part of a reverted commit (r80859). I'll investigate the lifetime snafu (with ASTUnit) separately.
- Traverse category methods, add a "class ref" and make the little test harness a bit more flexible.
Modified:
cfe/trunk/include/clang-c/Index.h
cfe/trunk/tools/CIndex/CIndex.cpp
cfe/trunk/tools/CIndex/CIndex.exports
cfe/trunk/tools/c-index-test/c-index-test.c
Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=80921&r1=80920&r2=80921&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Thu Sep 3 10:49:00 2009
@@ -75,7 +75,8 @@
CXCursor_ObjCProtocolRef = 41,
CXCursor_ObjCMessageRef = 42,
CXCursor_ObjCSelectorRef = 43,
- CXCursor_LastRef = 43
+ CXCursor_ObjCClassRef = 44,
+ CXCursor_LastRef = 44
};
/* A cursor into the CXTranslationUnit. */
@@ -91,6 +92,8 @@
CXIndex clang_createIndex();
+const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit);
+
CXTranslationUnit clang_createTranslationUnit(
CXIndex, const char *ast_filename
);
Modified: cfe/trunk/tools/CIndex/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.cpp?rev=80921&r1=80920&r2=80921&view=diff
==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Thu Sep 3 10:49:00 2009
@@ -67,6 +67,9 @@
break;
}
}
+ void VisitVarDecl(VarDecl *ND) {
+ Call(CXCursor_VarDecl, ND);
+ }
void VisitFunctionDecl(FunctionDecl *ND) {
Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
: CXCursor_FunctionDecl, ND);
@@ -95,6 +98,9 @@
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);
}
@@ -102,11 +108,18 @@
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) {
@@ -129,17 +142,28 @@
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);
@@ -167,6 +191,13 @@
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,
@@ -259,6 +290,12 @@
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>";
}
@@ -292,6 +329,7 @@
case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
+ case CXCursor_ObjCClassRef: return "ObjCClassRef";
default: return "<not implemented>";
}
}
Modified: cfe/trunk/tools/CIndex/CIndex.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.exports?rev=80921&r1=80920&r2=80921&view=diff
==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.exports (original)
+++ cfe/trunk/tools/CIndex/CIndex.exports Thu Sep 3 10:49:00 2009
@@ -19,3 +19,4 @@
_clang_isDefinition
_clang_getCursorSpelling
_clang_getCursorKindSpelling
+_clang_getTranslationUnitSpelling
Modified: cfe/trunk/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=80921&r1=80920&r2=80921&view=diff
==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Thu Sep 3 10:49:00 2009
@@ -2,26 +2,32 @@
#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);
}
@@ -34,6 +40,18 @@
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;
}
More information about the cfe-commits
mailing list