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