[cfe-commits] r82564 - in /cfe/trunk: include/clang-c/Index.h tools/CIndex/CIndex.cpp
Steve Naroff
snaroff at apple.com
Tue Sep 22 12:25:29 PDT 2009
Author: snaroff
Date: Tue Sep 22 14:25:29 2009
New Revision: 82564
URL: http://llvm.org/viewvc/llvm-project?rev=82564&view=rev
Log:
- Implement support for various types of "refs" (initially to help test clang_getCursor()).
- Add missing prototypes for dispose functions.
Modified:
cfe/trunk/include/clang-c/Index.h
cfe/trunk/tools/CIndex/CIndex.cpp
Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=82564&r1=82563&r2=82564&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Tue Sep 22 14:25:29 2009
@@ -34,6 +34,7 @@
typedef void *CXTranslationUnit; /* A translation unit instance. */
typedef void *CXDecl; /* A specific declaration within a translation unit. */
+typedef void *CXStmt; /* A specific statement within a function/method */
/* Cursors represent declarations, definitions, and references. */
enum CXCursorKind {
@@ -68,13 +69,18 @@
CXCursor_LastDefn = 36,
/* References */
- CXCursor_FirstRef = 40,
+ CXCursor_FirstRef = 40, /* Decl references */
CXCursor_ObjCSuperClassRef = 40,
CXCursor_ObjCProtocolRef = 41,
- CXCursor_ObjCMessageRef = 42,
- CXCursor_ObjCSelectorRef = 43,
- CXCursor_ObjCClassRef = 44,
- CXCursor_LastRef = 44,
+ CXCursor_ObjCClassRef = 42,
+
+ CXCursor_ObjCSelectorRef = 43, /* Expression references */
+ CXCursor_ObjCIvarRef = 44,
+ CXCursor_VarRef = 45,
+ CXCursor_FunctionRef = 46,
+ CXCursor_EnumConstantRef = 47,
+ CXCursor_MemberRef = 48,
+ CXCursor_LastRef = 48,
/* Error conditions */
CXCursor_FirstInvalid = 70,
@@ -85,23 +91,25 @@
};
/* A cursor into the CXTranslationUnit. */
+
typedef struct {
enum CXCursorKind kind;
CXDecl decl;
-
- /* FIXME: Handle references. */
+ CXStmt stmt; /* expression reference */
} CXCursor;
/* A unique token for looking up "visible" CXDecls from a CXTranslationUnit. */
typedef void *CXEntity;
CXIndex clang_createIndex();
+void clang_disposeIndex(CXIndex);
const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit);
CXTranslationUnit clang_createTranslationUnit(
CXIndex, const char *ast_filename
);
+void clang_disposeTranslationUnit(CXTranslationUnit);
/*
Usage: clang_loadTranslationUnit(). Will load the toplevel declarations
Modified: cfe/trunk/tools/CIndex/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.cpp?rev=82564&r1=82563&r2=82564&view=diff
==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Tue Sep 22 14:25:29 2009
@@ -17,6 +17,7 @@
#include "clang/Index/ASTLocation.h"
#include "clang/Index/Utils.h"
#include "clang/AST/DeclVisitor.h"
+#include "clang/AST/StmtVisitor.h"
#include "clang/AST/Decl.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
@@ -27,6 +28,45 @@
namespace {
+class CRefVisitor : public StmtVisitor<CRefVisitor> {
+ CXDecl CDecl;
+ CXDeclIterator Callback;
+ CXClientData CData;
+
+ void Call(enum CXCursorKind CK, Stmt *SRef) {
+ CXCursor C = { CK, CDecl, SRef };
+ Callback(CDecl, C, CData);
+ }
+
+public:
+ CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
+ CDecl(C), Callback(cback), CData(D) {}
+
+ void VisitStmt(Stmt *S) {
+ for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
+ C != CEnd; ++C)
+ Visit(*C);
+ }
+ void VisitDeclRefExpr(DeclRefExpr *Node) {
+ NamedDecl *D = Node->getDecl();
+ if (isa<VarDecl>(D))
+ Call(CXCursor_VarRef, Node);
+ else if (isa<FunctionDecl>(D))
+ Call(CXCursor_FunctionRef, Node);
+ else if (isa<EnumConstantDecl>(D))
+ Call(CXCursor_EnumConstantRef, Node);
+ }
+ void VisitMemberExpr(MemberExpr *Node) {
+ Call(CXCursor_MemberRef, Node);
+ }
+ void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
+ Call(CXCursor_ObjCSelectorRef, Node);
+ }
+ void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
+ Call(CXCursor_ObjCIvarRef, Node);
+ }
+};
+
// Translation Unit Visitor.
class TUVisitor : public DeclVisitor<TUVisitor> {
CXTranslationUnit TUnit;
@@ -34,7 +74,7 @@
CXClientData CData;
void Call(enum CXCursorKind CK, NamedDecl *ND) {
- CXCursor C = { CK, ND };
+ CXCursor C = { CK, ND, 0 };
Callback(TUnit, C, CData);
}
public:
@@ -103,7 +143,7 @@
// Disable the callback when the context is equal to the visiting decl.
if (CDecl == ND && !clang_isReference(CK))
return;
- CXCursor C = { CK, ND };
+ CXCursor C = { CK, ND, 0 };
Callback(CDecl, C, CData);
}
public:
@@ -168,6 +208,9 @@
void VisitFunctionDecl(FunctionDecl *ND) {
if (ND->isThisDeclarationADefinition()) {
VisitDeclContext(dyn_cast<DeclContext>(ND));
+
+ CRefVisitor RVisit(CDecl, Callback, CData);
+ RVisit.Visit(ND->getBody());
}
}
void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
@@ -323,6 +366,22 @@
assert(OID && "clang_getCursorLine(): Missing protocol decl");
return OID->getIdentifier()->getName();
}
+ case CXCursor_ObjCSelectorRef:
+ {
+ ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
+ static_cast<Stmt *>(C.stmt));
+ assert(OME && "clang_getCursorLine(): Missing message expr");
+ return OME->getSelector().getAsString().c_str();
+ }
+ case CXCursor_VarRef:
+ case CXCursor_FunctionRef:
+ case CXCursor_EnumConstantRef:
+ {
+ DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
+ static_cast<Stmt *>(C.stmt));
+ assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
+ return DRE->getDecl()->getIdentifier()->getName();
+ }
default:
return "<not implemented>";
}
@@ -358,6 +417,13 @@
case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
case CXCursor_ObjCClassRef: return "ObjCClassRef";
+ case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
+
+ case CXCursor_VarRef: return "VarRef";
+ case CXCursor_FunctionRef: return "FunctionRef";
+ case CXCursor_EnumConstantRef: return "EnumConstantRef";
+ case CXCursor_MemberRef: return "MemberRef";
+
case CXCursor_InvalidFile: return "InvalidFile";
case CXCursor_NoDeclFound: return "NoDeclFound";
case CXCursor_NotImplemented: return "NotImplemented";
@@ -376,6 +442,8 @@
case Decl::Var: return CXCursor_VarDecl;
case Decl::ParmVar: return CXCursor_ParmDecl;
case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
+ case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
+ case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
case Decl::ObjCMethod: {
ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
if (MD->isInstanceMethod())
@@ -399,7 +467,7 @@
const FileEntry *File = FMgr.getFile(source_name,
source_name+strlen(source_name));
if (!File) {
- CXCursor C = { CXCursor_InvalidFile, 0 };
+ CXCursor C = { CXCursor_InvalidFile, 0, 0 };
return C;
}
SourceLocation SLoc =
@@ -409,10 +477,10 @@
Decl *Dcl = ALoc.getDecl();
if (Dcl) {
- CXCursor C = { TranslateKind(Dcl), Dcl };
+ CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
return C;
}
- CXCursor C = { CXCursor_NoDeclFound, 0 };
+ CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
return C;
}
@@ -421,7 +489,7 @@
assert(AnonDecl && "Passed null CXDecl");
NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
- CXCursor C = { TranslateKind(ND), ND };
+ CXCursor C = { TranslateKind(ND), ND, 0 };
return C;
}
@@ -472,6 +540,22 @@
assert(OID && "clang_getCursorLine(): Missing protocol decl");
return OID->getLocation();
}
+ case CXCursor_ObjCSelectorRef:
+ {
+ ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
+ static_cast<Stmt *>(C.stmt));
+ assert(OME && "clang_getCursorLine(): Missing message expr");
+ return OME->getLeftLoc(); /* FIXME: should be a range */
+ }
+ case CXCursor_VarRef:
+ case CXCursor_FunctionRef:
+ case CXCursor_EnumConstantRef:
+ {
+ DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
+ static_cast<Stmt *>(C.stmt));
+ assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
+ return DRE->getLocation();
+ }
default:
return SourceLocation();
}
More information about the cfe-commits
mailing list