Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 1 | //===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Clang-C Source Indexing library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang-c/Index.h" |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 15 | #include "clang/Index/Program.h" |
| 16 | #include "clang/Index/Indexer.h" |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 17 | #include "clang/Index/ASTLocation.h" |
| 18 | #include "clang/Index/Utils.h" |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclVisitor.h" |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtVisitor.h" |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
Benjamin Kramer | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 22 | #include "clang/Basic/FileManager.h" |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Benjamin Kramer | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/ASTUnit.h" |
| 25 | #include <cstdio> |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 26 | #include <dlfcn.h> |
Ted Kremenek | 8c4195e | 2009-10-15 22:10:56 +0000 | [diff] [blame] | 27 | #include <sys/wait.h> |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 28 | #include <vector> |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 29 | #include "llvm/System/Path.h" |
| 30 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | using namespace idx; |
| 33 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 36 | static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) |
| 37 | { |
| 38 | NamedDecl *D = DRE->getDecl(); |
| 39 | if (isa<VarDecl>(D)) |
| 40 | return CXCursor_VarRef; |
| 41 | else if (isa<FunctionDecl>(D)) |
| 42 | return CXCursor_FunctionRef; |
| 43 | else if (isa<EnumConstantDecl>(D)) |
| 44 | return CXCursor_EnumConstantRef; |
| 45 | else |
| 46 | return CXCursor_NotImplemented; |
| 47 | } |
| 48 | |
| 49 | #if 0 |
| 50 | // Will be useful one day. |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 51 | class CRefVisitor : public StmtVisitor<CRefVisitor> { |
| 52 | CXDecl CDecl; |
| 53 | CXDeclIterator Callback; |
| 54 | CXClientData CData; |
| 55 | |
| 56 | void Call(enum CXCursorKind CK, Stmt *SRef) { |
| 57 | CXCursor C = { CK, CDecl, SRef }; |
| 58 | Callback(CDecl, C, CData); |
| 59 | } |
| 60 | |
| 61 | public: |
| 62 | CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) : |
| 63 | CDecl(C), Callback(cback), CData(D) {} |
| 64 | |
| 65 | void VisitStmt(Stmt *S) { |
| 66 | for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end(); |
| 67 | C != CEnd; ++C) |
| 68 | Visit(*C); |
| 69 | } |
| 70 | void VisitDeclRefExpr(DeclRefExpr *Node) { |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 71 | Call(TranslateDeclRefExpr(Node), Node); |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 72 | } |
| 73 | void VisitMemberExpr(MemberExpr *Node) { |
| 74 | Call(CXCursor_MemberRef, Node); |
| 75 | } |
| 76 | void VisitObjCMessageExpr(ObjCMessageExpr *Node) { |
| 77 | Call(CXCursor_ObjCSelectorRef, Node); |
| 78 | } |
| 79 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { |
| 80 | Call(CXCursor_ObjCIvarRef, Node); |
| 81 | } |
| 82 | }; |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 83 | #endif |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 84 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 85 | // Translation Unit Visitor. |
| 86 | class TUVisitor : public DeclVisitor<TUVisitor> { |
| 87 | CXTranslationUnit TUnit; |
| 88 | CXTranslationUnitIterator Callback; |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 89 | CXClientData CData; |
| 90 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame^] | 91 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 92 | // to the visitor. Declarations with a PCH level greater than this value will |
| 93 | // be suppressed. |
| 94 | unsigned MaxPCHLevel; |
| 95 | |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 96 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame^] | 97 | // Filter any declarations that have a PCH level greater than what we allow. |
| 98 | if (ND->getPCHLevel() > MaxPCHLevel) |
| 99 | return; |
| 100 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 101 | CXCursor C = { CK, ND, 0 }; |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 102 | Callback(TUnit, C, CData); |
| 103 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 104 | public: |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 105 | TUVisitor(CXTranslationUnit CTU, |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame^] | 106 | CXTranslationUnitIterator cback, CXClientData D, |
| 107 | unsigned MaxPCHLevel) : |
| 108 | TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {} |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 109 | |
| 110 | void VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 111 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 112 | } |
| 113 | void VisitDeclContext(DeclContext *DC) { |
| 114 | for (DeclContext::decl_iterator |
| 115 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 116 | Visit(*I); |
| 117 | } |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 118 | void VisitTypedefDecl(TypedefDecl *ND) { |
| 119 | Call(CXCursor_TypedefDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 120 | } |
| 121 | void VisitTagDecl(TagDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 122 | switch (ND->getTagKind()) { |
| 123 | case TagDecl::TK_struct: |
| 124 | Call(CXCursor_StructDecl, ND); |
| 125 | break; |
| 126 | case TagDecl::TK_class: |
| 127 | Call(CXCursor_ClassDecl, ND); |
| 128 | break; |
| 129 | case TagDecl::TK_union: |
| 130 | Call(CXCursor_UnionDecl, ND); |
| 131 | break; |
| 132 | case TagDecl::TK_enum: |
| 133 | Call(CXCursor_EnumDecl, ND); |
| 134 | break; |
| 135 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 136 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 137 | void VisitVarDecl(VarDecl *ND) { |
| 138 | Call(CXCursor_VarDecl, ND); |
| 139 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 140 | void VisitFunctionDecl(FunctionDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 141 | Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn |
| 142 | : CXCursor_FunctionDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 143 | } |
| 144 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) { |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 145 | Call(CXCursor_ObjCInterfaceDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 146 | } |
| 147 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 148 | Call(CXCursor_ObjCCategoryDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 149 | } |
| 150 | void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) { |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 151 | Call(CXCursor_ObjCProtocolDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 152 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 153 | void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) { |
| 154 | Call(CXCursor_ObjCClassDefn, ND); |
| 155 | } |
| 156 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) { |
| 157 | Call(CXCursor_ObjCCategoryDefn, ND); |
| 158 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 161 | // Declaration visitor. |
| 162 | class CDeclVisitor : public DeclVisitor<CDeclVisitor> { |
| 163 | CXDecl CDecl; |
| 164 | CXDeclIterator Callback; |
| 165 | CXClientData CData; |
| 166 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame^] | 167 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 168 | // to the visitor. Declarations with a PCH level greater than this value will |
| 169 | // be suppressed. |
| 170 | unsigned MaxPCHLevel; |
| 171 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 172 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 173 | // Disable the callback when the context is equal to the visiting decl. |
| 174 | if (CDecl == ND && !clang_isReference(CK)) |
| 175 | return; |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame^] | 176 | |
| 177 | // Filter any declarations that have a PCH level greater than what we allow. |
| 178 | if (ND->getPCHLevel() > MaxPCHLevel) |
| 179 | return; |
| 180 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 181 | CXCursor C = { CK, ND, 0 }; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 182 | Callback(CDecl, C, CData); |
| 183 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 184 | public: |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame^] | 185 | CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D, |
| 186 | unsigned MaxPCHLevel) : |
| 187 | CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {} |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 188 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 189 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 190 | // Issue callbacks for the containing class. |
| 191 | Call(CXCursor_ObjCClassRef, ND); |
| 192 | // FIXME: Issue callbacks for protocol refs. |
| 193 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
| 194 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 195 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 196 | // Issue callbacks for super class. |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 197 | if (D->getSuperClass()) |
| 198 | Call(CXCursor_ObjCSuperClassRef, D); |
| 199 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 200 | for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(), |
| 201 | E = D->protocol_end(); I != E; ++I) |
| 202 | Call(CXCursor_ObjCProtocolRef, *I); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 203 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 204 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 205 | void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 206 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 207 | E = PID->protocol_end(); I != E; ++I) |
| 208 | Call(CXCursor_ObjCProtocolRef, *I); |
| 209 | |
| 210 | VisitDeclContext(dyn_cast<DeclContext>(PID)); |
| 211 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 212 | void VisitTagDecl(TagDecl *D) { |
| 213 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 214 | } |
| 215 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 216 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 217 | } |
| 218 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 219 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 220 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 221 | void VisitDeclContext(DeclContext *DC) { |
| 222 | for (DeclContext::decl_iterator |
| 223 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 224 | Visit(*I); |
| 225 | } |
| 226 | void VisitEnumConstantDecl(EnumConstantDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 227 | Call(CXCursor_EnumConstantDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 228 | } |
| 229 | void VisitFieldDecl(FieldDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 230 | Call(CXCursor_FieldDecl, ND); |
| 231 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 232 | void VisitVarDecl(VarDecl *ND) { |
| 233 | Call(CXCursor_VarDecl, ND); |
| 234 | } |
| 235 | void VisitParmVarDecl(ParmVarDecl *ND) { |
| 236 | Call(CXCursor_ParmDecl, ND); |
| 237 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 238 | void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) { |
| 239 | Call(CXCursor_ObjCPropertyDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 240 | } |
| 241 | void VisitObjCIvarDecl(ObjCIvarDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 242 | Call(CXCursor_ObjCIvarDecl, ND); |
| 243 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 244 | void VisitFunctionDecl(FunctionDecl *ND) { |
| 245 | if (ND->isThisDeclarationADefinition()) { |
| 246 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 247 | #if 0 |
| 248 | // Not currently needed. |
| 249 | CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody()); |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 250 | CRefVisitor RVisit(CDecl, Callback, CData); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 251 | RVisit.Visit(Body); |
| 252 | #endif |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 255 | void VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
| 256 | if (ND->getBody()) { |
| 257 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn |
| 258 | : CXCursor_ObjCClassMethodDefn, ND); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 259 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 260 | } else |
| 261 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl |
| 262 | : CXCursor_ObjCClassMethodDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 263 | } |
| 264 | }; |
| 265 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame^] | 266 | class CIndexer : public Indexer { |
| 267 | public: |
| 268 | explicit CIndexer(Program &prog) : Indexer(prog), OnlyLocalDecls(false) { } |
| 269 | |
| 270 | /// \brief Whether we only want to see "local" declarations (that did not |
| 271 | /// come from a previous precompiled header). If false, we want to see all |
| 272 | /// declarations. |
| 273 | bool getOnlyLocalDecls() const { return OnlyLocalDecls; } |
| 274 | void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; } |
| 275 | |
| 276 | private: |
| 277 | bool OnlyLocalDecls; |
| 278 | }; |
| 279 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 282 | extern "C" { |
Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 283 | |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 284 | static const char *clangPath; |
| 285 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 286 | CXIndex clang_createIndex() |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 287 | { |
Daniel Dunbar | a390759 | 2009-09-21 03:03:22 +0000 | [diff] [blame] | 288 | // FIXME: Program is leaked. |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 289 | |
| 290 | // Find the location where this library lives (libCIndex.dylib). |
| 291 | // We do the lookup here to avoid poking dladdr too many times. |
| 292 | // This silly cast below avoids a C++ warning. |
| 293 | Dl_info info; |
| 294 | if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0) |
| 295 | assert(0 && "Call to dladdr() failed"); |
| 296 | |
| 297 | llvm::sys::Path CIndexPath(info.dli_fname); |
| 298 | std::string CIndexDir = CIndexPath.getDirname(); |
| 299 | |
| 300 | // We now have the CIndex directory, locate clang relative to it. |
| 301 | std::string ClangPath = CIndexDir + "/../bin/clang"; |
| 302 | |
| 303 | clangPath = ClangPath.c_str(); |
| 304 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame^] | 305 | return new CIndexer(*new Program()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 308 | void clang_disposeIndex(CXIndex CIdx) |
| 309 | { |
| 310 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame^] | 311 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 314 | // FIXME: need to pass back error info. |
| 315 | CXTranslationUnit clang_createTranslationUnit( |
| 316 | CXIndex CIdx, const char *ast_filename) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 317 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 318 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame^] | 319 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 320 | std::string astName(ast_filename); |
| 321 | std::string ErrMsg; |
| 322 | |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 323 | return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(), |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame^] | 324 | CXXIdx->getFileManager(), &ErrMsg, |
| 325 | CXXIdx->getOnlyLocalDecls()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 328 | CXTranslationUnit clang_createTranslationUnitFromSourceFile( |
| 329 | CXIndex CIdx, |
| 330 | const char *source_filename, |
| 331 | int num_command_line_args, const char **command_line_args) |
| 332 | { |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 333 | // Build up the arguments for involing clang. |
| 334 | std::vector<const char *> argv; |
| 335 | argv.push_back(clangPath); |
| 336 | argv.push_back("-emit-ast"); |
| 337 | argv.push_back(source_filename); |
| 338 | argv.push_back("-o"); |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 339 | // Generate a temporary name for the AST file. |
| 340 | char astTmpFile[L_tmpnam]; |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 341 | argv.push_back(tmpnam(astTmpFile)); |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 342 | for (int i = num_command_line_args; i < num_command_line_args; i++) |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 343 | argv.push_back(command_line_args[i]); |
| 344 | argv.push_back(NULL); |
| 345 | |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 346 | // Generate the AST file in a separate process. |
| 347 | pid_t child_pid = fork(); |
| 348 | if (child_pid == 0) { // Child process |
| 349 | |
| 350 | // Execute the command, passing the appropriate arguments. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 351 | execv(argv[0], (char *const *)&argv[0]); |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 352 | |
| 353 | // If execv returns, it failed. |
| 354 | assert(0 && "execv() failed"); |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 355 | } |
| 356 | // This is run by the parent. |
| 357 | int child_status; |
| 358 | pid_t tpid; |
| 359 | do { // Wait for the child to terminate. |
| 360 | tpid = wait(&child_status); |
| 361 | } while (tpid != child_pid); |
| 362 | |
| 363 | // Finally, we create the translation unit from the ast file. |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 364 | ASTUnit *ATU = static_cast<ASTUnit *>( |
| 365 | clang_createTranslationUnit(CIdx, astTmpFile)); |
| 366 | ATU->unlinkTemporaryFile(); |
| 367 | return ATU; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 370 | void clang_disposeTranslationUnit( |
| 371 | CXTranslationUnit CTUnit) |
| 372 | { |
| 373 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 374 | delete static_cast<ASTUnit *>(CTUnit); |
| 375 | } |
| 376 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame^] | 377 | void clang_wantOnlyLocalDeclarations(CXIndex CIdx) { |
| 378 | static_cast<CIndexer *>(CIdx)->setOnlyLocalDecls(true); |
| 379 | } |
| 380 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 381 | const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) |
| 382 | { |
| 383 | assert(CTUnit && "Passed null CXTranslationUnit"); |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 384 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 385 | return CXXUnit->getOriginalSourceFileName().c_str(); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 386 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 387 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 388 | void clang_loadTranslationUnit(CXTranslationUnit CTUnit, |
| 389 | CXTranslationUnitIterator callback, |
| 390 | CXClientData CData) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 391 | { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 392 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 393 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 394 | ASTContext &Ctx = CXXUnit->getASTContext(); |
| 395 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame^] | 396 | TUVisitor DVisit(CTUnit, callback, CData, |
| 397 | CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel); |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 398 | DVisit.Visit(Ctx.getTranslationUnitDecl()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 401 | void clang_loadDeclaration(CXDecl Dcl, |
| 402 | CXDeclIterator callback, |
| 403 | CXClientData CData) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 404 | { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 405 | assert(Dcl && "Passed null CXDecl"); |
| 406 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame^] | 407 | CDeclVisitor DVisit(Dcl, callback, CData, |
| 408 | static_cast<Decl *>(Dcl)->getPCHLevel()); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 409 | DVisit.Visit(static_cast<Decl *>(Dcl)); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Steve Naroff | 7e8f818 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 412 | // Some notes on CXEntity: |
| 413 | // |
| 414 | // - Since the 'ordinary' namespace includes functions, data, typedefs, |
| 415 | // ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one |
| 416 | // entity for 2 different types). For example: |
| 417 | // |
| 418 | // module1.m: @interface Foo @end Foo *x; |
| 419 | // module2.m: void Foo(int); |
| 420 | // |
| 421 | // - Since the unique name spans translation units, static data/functions |
| 422 | // within a CXTranslationUnit are *not* currently represented by entities. |
| 423 | // As a result, there will be no entity for the following: |
| 424 | // |
| 425 | // module.m: static void Foo() { } |
| 426 | // |
| 427 | |
| 428 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 429 | const char *clang_getDeclarationName(CXEntity) |
| 430 | { |
| 431 | return ""; |
| 432 | } |
| 433 | const char *clang_getURI(CXEntity) |
| 434 | { |
| 435 | return ""; |
| 436 | } |
| 437 | |
| 438 | CXEntity clang_getEntity(const char *URI) |
| 439 | { |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | // |
| 444 | // CXDecl Operations. |
| 445 | // |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 446 | CXEntity clang_getEntityFromDecl(CXDecl) |
| 447 | { |
| 448 | return 0; |
| 449 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 450 | const char *clang_getDeclSpelling(CXDecl AnonDecl) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 451 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 452 | assert(AnonDecl && "Passed null CXDecl"); |
| 453 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 454 | |
| 455 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) { |
| 456 | return OMD->getSelector().getAsString().c_str(); |
| 457 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 458 | if (ND->getIdentifier()) |
| 459 | return ND->getIdentifier()->getName(); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 460 | else |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 461 | return ""; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 462 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 463 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 464 | unsigned clang_getDeclLine(CXDecl AnonDecl) |
| 465 | { |
| 466 | assert(AnonDecl && "Passed null CXDecl"); |
| 467 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 468 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
| 469 | return SourceMgr.getSpellingLineNumber(ND->getLocation()); |
| 470 | } |
| 471 | |
| 472 | unsigned clang_getDeclColumn(CXDecl AnonDecl) |
| 473 | { |
| 474 | assert(AnonDecl && "Passed null CXDecl"); |
| 475 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 476 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 7416524 | 2009-09-25 22:15:54 +0000 | [diff] [blame] | 477 | return SourceMgr.getSpellingColumnNumber(ND->getLocation()); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 480 | const char *clang_getDeclSource(CXDecl AnonDecl) |
| 481 | { |
| 482 | assert(AnonDecl && "Passed null CXDecl"); |
| 483 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 484 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
| 485 | return SourceMgr.getBufferName(ND->getLocation()); |
| 486 | } |
| 487 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 488 | const char *clang_getCursorSpelling(CXCursor C) |
| 489 | { |
| 490 | assert(C.decl && "CXCursor has null decl"); |
| 491 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 492 | |
| 493 | if (clang_isReference(C.kind)) { |
| 494 | switch (C.kind) { |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 495 | case CXCursor_ObjCSuperClassRef: |
| 496 | { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 497 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 498 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
| 499 | return OID->getSuperClass()->getIdentifier()->getName(); |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 500 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 501 | case CXCursor_ObjCClassRef: |
| 502 | { |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 503 | if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) { |
| 504 | return OID->getIdentifier()->getName(); |
| 505 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 506 | ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND); |
| 507 | assert(OID && "clang_getCursorLine(): Missing category decl"); |
| 508 | return OID->getClassInterface()->getIdentifier()->getName(); |
| 509 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 510 | case CXCursor_ObjCProtocolRef: |
| 511 | { |
| 512 | ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND); |
| 513 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
| 514 | return OID->getIdentifier()->getName(); |
| 515 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 516 | case CXCursor_ObjCSelectorRef: |
| 517 | { |
| 518 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>( |
| 519 | static_cast<Stmt *>(C.stmt)); |
| 520 | assert(OME && "clang_getCursorLine(): Missing message expr"); |
| 521 | return OME->getSelector().getAsString().c_str(); |
| 522 | } |
| 523 | case CXCursor_VarRef: |
| 524 | case CXCursor_FunctionRef: |
| 525 | case CXCursor_EnumConstantRef: |
| 526 | { |
| 527 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>( |
| 528 | static_cast<Stmt *>(C.stmt)); |
| 529 | assert(DRE && "clang_getCursorLine(): Missing decl ref expr"); |
| 530 | return DRE->getDecl()->getIdentifier()->getName(); |
| 531 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 532 | default: |
| 533 | return "<not implemented>"; |
| 534 | } |
| 535 | } |
| 536 | return clang_getDeclSpelling(C.decl); |
| 537 | } |
| 538 | |
| 539 | const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 540 | { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 541 | switch (Kind) { |
| 542 | case CXCursor_FunctionDecl: return "FunctionDecl"; |
| 543 | case CXCursor_TypedefDecl: return "TypedefDecl"; |
| 544 | case CXCursor_EnumDecl: return "EnumDecl"; |
| 545 | case CXCursor_EnumConstantDecl: return "EnumConstantDecl"; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 546 | case CXCursor_StructDecl: return "StructDecl"; |
| 547 | case CXCursor_UnionDecl: return "UnionDecl"; |
| 548 | case CXCursor_ClassDecl: return "ClassDecl"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 549 | case CXCursor_FieldDecl: return "FieldDecl"; |
| 550 | case CXCursor_VarDecl: return "VarDecl"; |
| 551 | case CXCursor_ParmDecl: return "ParmDecl"; |
| 552 | case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl"; |
| 553 | case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl"; |
| 554 | case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl"; |
| 555 | case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl"; |
| 556 | case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl"; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 557 | case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl"; |
| 558 | case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl"; |
| 559 | case CXCursor_FunctionDefn: return "FunctionDefn"; |
| 560 | case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn"; |
| 561 | case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn"; |
| 562 | case CXCursor_ObjCClassDefn: return "ObjCClassDefn"; |
| 563 | case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn"; |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 564 | case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 565 | case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef"; |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 566 | case CXCursor_ObjCClassRef: return "ObjCClassRef"; |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 567 | case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef"; |
| 568 | |
| 569 | case CXCursor_VarRef: return "VarRef"; |
| 570 | case CXCursor_FunctionRef: return "FunctionRef"; |
| 571 | case CXCursor_EnumConstantRef: return "EnumConstantRef"; |
| 572 | case CXCursor_MemberRef: return "MemberRef"; |
| 573 | |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 574 | case CXCursor_InvalidFile: return "InvalidFile"; |
| 575 | case CXCursor_NoDeclFound: return "NoDeclFound"; |
| 576 | case CXCursor_NotImplemented: return "NotImplemented"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 577 | default: return "<not implemented>"; |
| 578 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 579 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 580 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 581 | static enum CXCursorKind TranslateKind(Decl *D) { |
| 582 | switch (D->getKind()) { |
| 583 | case Decl::Function: return CXCursor_FunctionDecl; |
| 584 | case Decl::Typedef: return CXCursor_TypedefDecl; |
| 585 | case Decl::Enum: return CXCursor_EnumDecl; |
| 586 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 587 | case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class |
| 588 | case Decl::Field: return CXCursor_FieldDecl; |
| 589 | case Decl::Var: return CXCursor_VarDecl; |
| 590 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 591 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 592 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 593 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 594 | case Decl::ObjCMethod: { |
| 595 | ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D); |
| 596 | if (MD->isInstanceMethod()) |
| 597 | return CXCursor_ObjCInstanceMethodDecl; |
| 598 | return CXCursor_ObjCClassMethodDecl; |
| 599 | } |
| 600 | default: break; |
| 601 | } |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 602 | return CXCursor_NotImplemented; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 603 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 604 | // |
| 605 | // CXCursor Operations. |
| 606 | // |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 607 | CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name, |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 608 | unsigned line, unsigned column) |
| 609 | { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 610 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 611 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 612 | |
| 613 | FileManager &FMgr = CXXUnit->getFileManager(); |
| 614 | const FileEntry *File = FMgr.getFile(source_name, |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 615 | source_name+strlen(source_name)); |
| 616 | if (!File) { |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 617 | CXCursor C = { CXCursor_InvalidFile, 0, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 618 | return C; |
| 619 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 620 | SourceLocation SLoc = |
| 621 | CXXUnit->getSourceManager().getLocation(File, line, column); |
| 622 | |
| 623 | ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc); |
| 624 | |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 625 | Decl *Dcl = ALoc.getParentDecl(); |
Argyrios Kyrtzidis | 05a7651 | 2009-09-29 19:45:58 +0000 | [diff] [blame] | 626 | if (ALoc.isNamedRef()) |
| 627 | Dcl = ALoc.AsNamedRef().ND; |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 628 | Stmt *Stm = ALoc.dyn_AsStmt(); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 629 | if (Dcl) { |
| 630 | if (Stm) { |
| 631 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) { |
| 632 | CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm }; |
| 633 | return C; |
| 634 | } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) { |
| 635 | CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp }; |
| 636 | return C; |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 637 | } |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 638 | // Fall through...treat as a decl, not a ref. |
| 639 | } |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 640 | if (ALoc.isNamedRef()) { |
| 641 | if (isa<ObjCInterfaceDecl>(Dcl)) { |
| 642 | CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() }; |
| 643 | return C; |
| 644 | } |
| 645 | if (isa<ObjCProtocolDecl>(Dcl)) { |
| 646 | CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() }; |
| 647 | return C; |
| 648 | } |
| 649 | } |
| 650 | CXCursor C = { TranslateKind(Dcl), Dcl, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 651 | return C; |
| 652 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 653 | CXCursor C = { CXCursor_NoDeclFound, 0, 0 }; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 654 | return C; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 657 | CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) |
| 658 | { |
| 659 | assert(AnonDecl && "Passed null CXDecl"); |
| 660 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 661 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 662 | CXCursor C = { TranslateKind(ND), ND, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 663 | return C; |
| 664 | } |
| 665 | |
| 666 | unsigned clang_isInvalid(enum CXCursorKind K) |
| 667 | { |
| 668 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 669 | } |
| 670 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 671 | unsigned clang_isDeclaration(enum CXCursorKind K) |
| 672 | { |
| 673 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 674 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 675 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 676 | unsigned clang_isReference(enum CXCursorKind K) |
| 677 | { |
| 678 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 679 | } |
| 680 | |
| 681 | unsigned clang_isDefinition(enum CXCursorKind K) |
| 682 | { |
| 683 | return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn; |
| 684 | } |
| 685 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 686 | CXCursorKind clang_getCursorKind(CXCursor C) |
| 687 | { |
| 688 | return C.kind; |
| 689 | } |
| 690 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 691 | static Decl *getDeclFromExpr(Stmt *E) { |
| 692 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 693 | return RefExpr->getDecl(); |
| 694 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 695 | return ME->getMemberDecl(); |
| 696 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 697 | return RE->getDecl(); |
| 698 | |
| 699 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 700 | return getDeclFromExpr(CE->getCallee()); |
| 701 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) |
| 702 | return getDeclFromExpr(CE->getSubExpr()); |
| 703 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 704 | return OME->getMethodDecl(); |
| 705 | |
| 706 | return 0; |
| 707 | } |
| 708 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 709 | CXDecl clang_getCursorDecl(CXCursor C) |
| 710 | { |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 711 | if (clang_isDeclaration(C.kind)) |
| 712 | return C.decl; |
| 713 | |
| 714 | if (clang_isReference(C.kind)) { |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 715 | if (C.stmt) { |
Steve Naroff | f9adf8f | 2009-10-05 17:58:19 +0000 | [diff] [blame] | 716 | if (C.kind == CXCursor_ObjCClassRef || |
| 717 | C.kind == CXCursor_ObjCProtocolRef) |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 718 | return static_cast<Stmt *>(C.stmt); |
| 719 | else |
| 720 | return getDeclFromExpr(static_cast<Stmt *>(C.stmt)); |
| 721 | } else |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 722 | return C.decl; |
| 723 | } |
| 724 | return 0; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 727 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 728 | static SourceLocation getLocationFromCursor(CXCursor C, |
| 729 | SourceManager &SourceMgr, |
| 730 | NamedDecl *ND) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 731 | if (clang_isReference(C.kind)) { |
| 732 | switch (C.kind) { |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 733 | case CXCursor_ObjCClassRef: |
| 734 | { |
| 735 | if (isa<ObjCInterfaceDecl>(ND)) { |
| 736 | // FIXME: This is a hack (storing the parent decl in the stmt slot). |
| 737 | NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt); |
| 738 | return parentDecl->getLocation(); |
| 739 | } |
| 740 | ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND); |
| 741 | assert(OID && "clang_getCursorLine(): Missing category decl"); |
| 742 | return OID->getClassInterface()->getLocation(); |
| 743 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 744 | case CXCursor_ObjCSuperClassRef: |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 745 | { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 746 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 747 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 748 | return OID->getSuperClassLoc(); |
| 749 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 750 | case CXCursor_ObjCProtocolRef: |
| 751 | { |
| 752 | ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND); |
| 753 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
| 754 | return OID->getLocation(); |
| 755 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 756 | case CXCursor_ObjCSelectorRef: |
| 757 | { |
| 758 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>( |
| 759 | static_cast<Stmt *>(C.stmt)); |
| 760 | assert(OME && "clang_getCursorLine(): Missing message expr"); |
| 761 | return OME->getLeftLoc(); /* FIXME: should be a range */ |
| 762 | } |
| 763 | case CXCursor_VarRef: |
| 764 | case CXCursor_FunctionRef: |
| 765 | case CXCursor_EnumConstantRef: |
| 766 | { |
| 767 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>( |
| 768 | static_cast<Stmt *>(C.stmt)); |
| 769 | assert(DRE && "clang_getCursorLine(): Missing decl ref expr"); |
| 770 | return DRE->getLocation(); |
| 771 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 772 | default: |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 773 | return SourceLocation(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 774 | } |
| 775 | } else { // We have a declaration or a definition. |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 776 | SourceLocation SLoc; |
| 777 | switch (ND->getKind()) { |
| 778 | case Decl::ObjCInterface: |
| 779 | { |
| 780 | SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc(); |
| 781 | break; |
| 782 | } |
| 783 | case Decl::ObjCProtocol: |
| 784 | { |
| 785 | SLoc = ND->getLocation(); /* FIXME: need to get the name location. */ |
| 786 | break; |
| 787 | } |
| 788 | default: |
| 789 | { |
| 790 | SLoc = ND->getLocation(); |
| 791 | break; |
| 792 | } |
| 793 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 794 | if (SLoc.isInvalid()) |
| 795 | return SourceLocation(); |
Steve Naroff | 1164d85 | 2009-09-02 18:58:52 +0000 | [diff] [blame] | 796 | return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations. |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 797 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 800 | unsigned clang_getCursorLine(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 801 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 802 | assert(C.decl && "CXCursor has null decl"); |
| 803 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 804 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 805 | |
| 806 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 807 | return SourceMgr.getSpellingLineNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 808 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 809 | |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 810 | unsigned clang_getCursorColumn(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 811 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 812 | assert(C.decl && "CXCursor has null decl"); |
| 813 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 814 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 815 | |
| 816 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 817 | return SourceMgr.getSpellingColumnNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 818 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 819 | const char *clang_getCursorSource(CXCursor C) |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 820 | { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 821 | assert(C.decl && "CXCursor has null decl"); |
| 822 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 823 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 824 | |
| 825 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 826 | return SourceMgr.getBufferName(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 829 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
| 830 | const char **startBuf, |
| 831 | const char **endBuf, |
| 832 | unsigned *startLine, |
| 833 | unsigned *startColumn, |
| 834 | unsigned *endLine, |
| 835 | unsigned *endColumn) |
| 836 | { |
| 837 | assert(C.decl && "CXCursor has null decl"); |
| 838 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 839 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 840 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
| 841 | |
| 842 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 843 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 844 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 845 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 846 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 847 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 848 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 849 | } |
| 850 | |
| 851 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 852 | } // end extern "C" |