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