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