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. |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 7 | // |
Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 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" |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 19 | #include "clang/Sema/CodeCompleteConsumer.h" |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclVisitor.h" |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtVisitor.h" |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 22 | #include "clang/AST/Decl.h" |
Benjamin Kramer | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 23 | #include "clang/Basic/FileManager.h" |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Benjamin Kramer | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/ASTUnit.h" |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringExtras.h" |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 27 | #include "llvm/Config/config.h" |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 29 | #include "llvm/Support/MemoryBuffer.h" |
| 30 | #include "llvm/System/Path.h" |
Benjamin Kramer | 0829a83 | 2009-10-18 11:19:36 +0000 | [diff] [blame] | 31 | #include "llvm/System/Program.h" |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 33 | |
Benjamin Kramer | d01a0bc | 2009-08-29 12:56:35 +0000 | [diff] [blame] | 34 | #include <cstdio> |
Ted Kremenek | 49358d8 | 2009-10-19 21:17:25 +0000 | [diff] [blame] | 35 | #include <vector> |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 36 | #include <sstream> |
Ted Kremenek | 49358d8 | 2009-10-19 21:17:25 +0000 | [diff] [blame] | 37 | |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 38 | #ifdef LLVM_ON_WIN32 |
| 39 | #define WIN32_LEAN_AND_MEAN |
| 40 | #include <windows.h> |
| 41 | #else |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 42 | #include <dlfcn.h> |
Daniel Dunbar | a47dd19 | 2009-10-17 23:53:11 +0000 | [diff] [blame] | 43 | #endif |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 44 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 45 | using namespace clang; |
| 46 | using namespace idx; |
| 47 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 48 | namespace { |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 49 | static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 50 | { |
| 51 | NamedDecl *D = DRE->getDecl(); |
| 52 | if (isa<VarDecl>(D)) |
| 53 | return CXCursor_VarRef; |
| 54 | else if (isa<FunctionDecl>(D)) |
| 55 | return CXCursor_FunctionRef; |
| 56 | else if (isa<EnumConstantDecl>(D)) |
| 57 | return CXCursor_EnumConstantRef; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 58 | else |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 59 | return CXCursor_NotImplemented; |
| 60 | } |
| 61 | |
| 62 | #if 0 |
| 63 | // Will be useful one day. |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 64 | class CRefVisitor : public StmtVisitor<CRefVisitor> { |
| 65 | CXDecl CDecl; |
| 66 | CXDeclIterator Callback; |
| 67 | CXClientData CData; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 68 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 69 | void Call(enum CXCursorKind CK, Stmt *SRef) { |
| 70 | CXCursor C = { CK, CDecl, SRef }; |
| 71 | Callback(CDecl, C, CData); |
| 72 | } |
| 73 | |
| 74 | public: |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 75 | CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) : |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 76 | CDecl(C), Callback(cback), CData(D) {} |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 77 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 78 | void VisitStmt(Stmt *S) { |
| 79 | for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end(); |
| 80 | C != CEnd; ++C) |
| 81 | Visit(*C); |
| 82 | } |
| 83 | void VisitDeclRefExpr(DeclRefExpr *Node) { |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 84 | Call(TranslateDeclRefExpr(Node), Node); |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 85 | } |
| 86 | void VisitMemberExpr(MemberExpr *Node) { |
| 87 | Call(CXCursor_MemberRef, Node); |
| 88 | } |
| 89 | void VisitObjCMessageExpr(ObjCMessageExpr *Node) { |
| 90 | Call(CXCursor_ObjCSelectorRef, Node); |
| 91 | } |
| 92 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) { |
| 93 | Call(CXCursor_ObjCIvarRef, Node); |
| 94 | } |
| 95 | }; |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 96 | #endif |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 98 | /// IgnoreDiagnosticsClient - A DiagnosticsClient that just ignores emitted |
| 99 | /// warnings and errors. |
| 100 | class VISIBILITY_HIDDEN IgnoreDiagnosticsClient : public DiagnosticClient { |
| 101 | public: |
| 102 | virtual ~IgnoreDiagnosticsClient() {} |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 103 | virtual void HandleDiagnostic(Diagnostic::Level, const DiagnosticInfo &) {} |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 104 | }; |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 105 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 106 | // Translation Unit Visitor. |
| 107 | class TUVisitor : public DeclVisitor<TUVisitor> { |
| 108 | CXTranslationUnit TUnit; |
| 109 | CXTranslationUnitIterator Callback; |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 110 | CXClientData CData; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 111 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 112 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 113 | // to the visitor. Declarations with a PCH level greater than this value will |
| 114 | // be suppressed. |
| 115 | unsigned MaxPCHLevel; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 116 | |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 117 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 118 | // Filter any declarations that have a PCH level greater than what we allow. |
| 119 | if (ND->getPCHLevel() > MaxPCHLevel) |
| 120 | return; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 121 | |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 122 | // Filter any implicit declarations (since the source info will be bogus). |
| 123 | if (ND->isImplicit()) |
| 124 | return; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 125 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 126 | CXCursor C = { CK, ND, 0 }; |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 127 | Callback(TUnit, C, CData); |
| 128 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 129 | public: |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 130 | TUVisitor(CXTranslationUnit CTU, |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 131 | CXTranslationUnitIterator cback, CXClientData D, |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 132 | unsigned MaxPCHLevel) : |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 133 | TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {} |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 134 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 135 | void VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 136 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 137 | } |
| 138 | void VisitDeclContext(DeclContext *DC) { |
| 139 | for (DeclContext::decl_iterator |
| 140 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 141 | Visit(*I); |
| 142 | } |
Ted Kremenek | ef7fdc6 | 2009-11-17 07:02:15 +0000 | [diff] [blame] | 143 | |
| 144 | void VisitFunctionDecl(FunctionDecl *ND) { |
| 145 | Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn |
| 146 | : CXCursor_FunctionDecl, ND); |
| 147 | } |
| 148 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 149 | Call(CXCursor_ObjCCategoryDecl, ND); |
| 150 | } |
| 151 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) { |
| 152 | Call(CXCursor_ObjCCategoryDefn, ND); |
| 153 | } |
| 154 | void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) { |
| 155 | Call(CXCursor_ObjCClassDefn, ND); |
| 156 | } |
| 157 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) { |
| 158 | Call(CXCursor_ObjCInterfaceDecl, ND); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 159 | } |
Ted Kremenek | ef7fdc6 | 2009-11-17 07:02:15 +0000 | [diff] [blame] | 160 | void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) { |
| 161 | Call(CXCursor_ObjCProtocolDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 162 | } |
| 163 | void VisitTagDecl(TagDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 164 | switch (ND->getTagKind()) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 165 | case TagDecl::TK_struct: |
| 166 | Call(CXCursor_StructDecl, ND); |
| 167 | break; |
| 168 | case TagDecl::TK_class: |
| 169 | Call(CXCursor_ClassDecl, ND); |
| 170 | break; |
| 171 | case TagDecl::TK_union: |
| 172 | Call(CXCursor_UnionDecl, ND); |
| 173 | break; |
| 174 | case TagDecl::TK_enum: |
| 175 | Call(CXCursor_EnumDecl, ND); |
| 176 | break; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 177 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 178 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 179 | void VisitTypedefDecl(TypedefDecl *ND) { |
| 180 | Call(CXCursor_TypedefDecl, ND); |
| 181 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 182 | void VisitVarDecl(VarDecl *ND) { |
| 183 | Call(CXCursor_VarDecl, ND); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 184 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 185 | }; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 186 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 187 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 188 | // Declaration visitor. |
| 189 | class CDeclVisitor : public DeclVisitor<CDeclVisitor> { |
| 190 | CXDecl CDecl; |
| 191 | CXDeclIterator Callback; |
| 192 | CXClientData CData; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 193 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 194 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 195 | // to the visitor. Declarations with a PCH level greater than this value will |
| 196 | // be suppressed. |
| 197 | unsigned MaxPCHLevel; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 198 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 199 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 200 | // Disable the callback when the context is equal to the visiting decl. |
| 201 | if (CDecl == ND && !clang_isReference(CK)) |
| 202 | return; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 203 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 204 | // Filter any declarations that have a PCH level greater than what we allow. |
| 205 | if (ND->getPCHLevel() > MaxPCHLevel) |
| 206 | return; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 207 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 208 | CXCursor C = { CK, ND, 0 }; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 209 | Callback(CDecl, C, CData); |
| 210 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 211 | public: |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 212 | CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D, |
| 213 | unsigned MaxPCHLevel) : |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 214 | CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {} |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 215 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 216 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 217 | // Issue callbacks for the containing class. |
| 218 | Call(CXCursor_ObjCClassRef, ND); |
| 219 | // FIXME: Issue callbacks for protocol refs. |
| 220 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
| 221 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 222 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 223 | // Issue callbacks for super class. |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 224 | if (D->getSuperClass()) |
| 225 | Call(CXCursor_ObjCSuperClassRef, D); |
| 226 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 227 | for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(), |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 228 | E = D->protocol_end(); I != E; ++I) |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 229 | Call(CXCursor_ObjCProtocolRef, *I); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 230 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 231 | } |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 232 | void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 233 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 234 | E = PID->protocol_end(); I != E; ++I) |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 235 | Call(CXCursor_ObjCProtocolRef, *I); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 236 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 237 | VisitDeclContext(dyn_cast<DeclContext>(PID)); |
| 238 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 239 | void VisitTagDecl(TagDecl *D) { |
| 240 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 241 | } |
| 242 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 243 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 244 | } |
| 245 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 246 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 247 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 248 | void VisitDeclContext(DeclContext *DC) { |
| 249 | for (DeclContext::decl_iterator |
| 250 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 251 | Visit(*I); |
| 252 | } |
| 253 | void VisitEnumConstantDecl(EnumConstantDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 254 | Call(CXCursor_EnumConstantDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 255 | } |
| 256 | void VisitFieldDecl(FieldDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 257 | Call(CXCursor_FieldDecl, ND); |
| 258 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 259 | void VisitVarDecl(VarDecl *ND) { |
| 260 | Call(CXCursor_VarDecl, ND); |
| 261 | } |
| 262 | void VisitParmVarDecl(ParmVarDecl *ND) { |
| 263 | Call(CXCursor_ParmDecl, ND); |
| 264 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 265 | void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) { |
| 266 | Call(CXCursor_ObjCPropertyDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 267 | } |
| 268 | void VisitObjCIvarDecl(ObjCIvarDecl *ND) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 269 | Call(CXCursor_ObjCIvarDecl, ND); |
| 270 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 271 | void VisitFunctionDecl(FunctionDecl *ND) { |
| 272 | if (ND->isThisDeclarationADefinition()) { |
| 273 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 274 | #if 0 |
| 275 | // Not currently needed. |
| 276 | CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody()); |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 277 | CRefVisitor RVisit(CDecl, Callback, CData); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 278 | RVisit.Visit(Body); |
| 279 | #endif |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 282 | void VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
| 283 | if (ND->getBody()) { |
| 284 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn |
| 285 | : CXCursor_ObjCClassMethodDefn, ND); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 286 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 287 | } else |
| 288 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl |
| 289 | : CXCursor_ObjCClassMethodDecl, ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 290 | } |
| 291 | }; |
| 292 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 293 | class CIndexer : public Indexer { |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 294 | public: |
| 295 | explicit CIndexer(Program *prog) : Indexer(*prog), |
| 296 | OnlyLocalDecls(false), |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 297 | DisplayDiagnostics(false) {} |
Ted Kremenek | dff7689 | 2009-10-17 06:21:47 +0000 | [diff] [blame] | 298 | |
| 299 | virtual ~CIndexer() { delete &getProgram(); } |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 300 | |
| 301 | /// \brief Whether we only want to see "local" declarations (that did not |
| 302 | /// come from a previous precompiled header). If false, we want to see all |
| 303 | /// declarations. |
| 304 | bool getOnlyLocalDecls() const { return OnlyLocalDecls; } |
| 305 | void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; } |
Benjamin Kramer | 9670762 | 2009-10-18 11:10:55 +0000 | [diff] [blame] | 306 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 307 | void setDisplayDiagnostics(bool Display = true) { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 308 | DisplayDiagnostics = Display; |
| 309 | } |
| 310 | bool getDisplayDiagnostics() const { return DisplayDiagnostics; } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 311 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 312 | /// \brief Get the path of the clang binary. |
Benjamin Kramer | c5a9e95 | 2009-10-19 10:20:24 +0000 | [diff] [blame] | 313 | const llvm::sys::Path& getClangPath(); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 314 | private: |
| 315 | bool OnlyLocalDecls; |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 316 | bool DisplayDiagnostics; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 317 | |
Benjamin Kramer | c5a9e95 | 2009-10-19 10:20:24 +0000 | [diff] [blame] | 318 | llvm::sys::Path ClangPath; |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 319 | }; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 320 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 321 | const llvm::sys::Path& CIndexer::getClangPath() { |
| 322 | // Did we already compute the path? |
| 323 | if (!ClangPath.empty()) |
| 324 | return ClangPath; |
| 325 | |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 326 | // Find the location where this library lives (libCIndex.dylib). |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 327 | #ifdef LLVM_ON_WIN32 |
| 328 | MEMORY_BASIC_INFORMATION mbi; |
| 329 | char path[MAX_PATH]; |
Benjamin Kramer | edcd828 | 2009-10-18 16:20:58 +0000 | [diff] [blame] | 330 | VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi, |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 331 | sizeof(mbi)); |
| 332 | GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH); |
| 333 | |
| 334 | llvm::sys::Path CIndexPath(path); |
John Thompson | 6a6742a | 2009-11-11 23:11:14 +0000 | [diff] [blame] | 335 | |
| 336 | CIndexPath.eraseComponent(); |
| 337 | CIndexPath.appendComponent("clang"); |
| 338 | CIndexPath.appendSuffix("exe"); |
| 339 | CIndexPath.makeAbsolute(); |
Benjamin Kramer | 20d7581 | 2009-10-18 16:13:48 +0000 | [diff] [blame] | 340 | #else |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 341 | // This silly cast below avoids a C++ warning. |
| 342 | Dl_info info; |
| 343 | if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0) |
| 344 | assert(0 && "Call to dladdr() failed"); |
| 345 | |
| 346 | llvm::sys::Path CIndexPath(info.dli_fname); |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 347 | |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 348 | // We now have the CIndex directory, locate clang relative to it. |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 349 | CIndexPath.eraseComponent(); |
| 350 | CIndexPath.eraseComponent(); |
| 351 | CIndexPath.appendComponent("bin"); |
| 352 | CIndexPath.appendComponent("clang"); |
John Thompson | 6a6742a | 2009-11-11 23:11:14 +0000 | [diff] [blame] | 353 | #endif |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 354 | |
| 355 | // Cache our result. |
| 356 | ClangPath = CIndexPath; |
| 357 | return ClangPath; |
| 358 | } |
| 359 | |
| 360 | } |
| 361 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 362 | static SourceLocation getLocationFromCursor(CXCursor C, |
Daniel Dunbar | c619033 | 2009-11-08 04:13:53 +0000 | [diff] [blame] | 363 | SourceManager &SourceMgr, |
| 364 | NamedDecl *ND) { |
| 365 | if (clang_isReference(C.kind)) { |
| 366 | switch (C.kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 367 | case CXCursor_ObjCClassRef: { |
| 368 | if (isa<ObjCInterfaceDecl>(ND)) { |
| 369 | // FIXME: This is a hack (storing the parent decl in the stmt slot). |
| 370 | NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt); |
| 371 | return parentDecl->getLocation(); |
Daniel Dunbar | c619033 | 2009-11-08 04:13:53 +0000 | [diff] [blame] | 372 | } |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 373 | ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND); |
| 374 | assert(OID && "clang_getCursorLine(): Missing category decl"); |
| 375 | return OID->getClassInterface()->getLocation(); |
| 376 | } |
| 377 | case CXCursor_ObjCSuperClassRef: { |
| 378 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 379 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
| 380 | return OID->getSuperClassLoc(); |
| 381 | } |
| 382 | case CXCursor_ObjCProtocolRef: { |
| 383 | ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND); |
| 384 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
| 385 | return OID->getLocation(); |
| 386 | } |
| 387 | case CXCursor_ObjCSelectorRef: { |
| 388 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>( |
| 389 | static_cast<Stmt *>(C.stmt)); |
| 390 | assert(OME && "clang_getCursorLine(): Missing message expr"); |
| 391 | return OME->getLeftLoc(); /* FIXME: should be a range */ |
| 392 | } |
| 393 | case CXCursor_VarRef: |
| 394 | case CXCursor_FunctionRef: |
| 395 | case CXCursor_EnumConstantRef: { |
| 396 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>( |
| 397 | static_cast<Stmt *>(C.stmt)); |
| 398 | assert(DRE && "clang_getCursorLine(): Missing decl ref expr"); |
| 399 | return DRE->getLocation(); |
| 400 | } |
| 401 | default: |
| 402 | return SourceLocation(); |
Daniel Dunbar | c619033 | 2009-11-08 04:13:53 +0000 | [diff] [blame] | 403 | } |
| 404 | } else { // We have a declaration or a definition. |
| 405 | SourceLocation SLoc; |
| 406 | switch (ND->getKind()) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 407 | case Decl::ObjCInterface: { |
| 408 | SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc(); |
| 409 | break; |
| 410 | } |
| 411 | case Decl::ObjCProtocol: { |
| 412 | SLoc = ND->getLocation(); /* FIXME: need to get the name location. */ |
| 413 | break; |
| 414 | } |
| 415 | default: { |
| 416 | SLoc = ND->getLocation(); |
| 417 | break; |
| 418 | } |
Daniel Dunbar | c619033 | 2009-11-08 04:13:53 +0000 | [diff] [blame] | 419 | } |
| 420 | if (SLoc.isInvalid()) |
| 421 | return SourceLocation(); |
| 422 | return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations. |
| 423 | } |
| 424 | } |
| 425 | |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 426 | static CXString createCXString(const char *String, bool DupString = false) { |
| 427 | CXString Str; |
| 428 | if (DupString) { |
| 429 | Str.Spelling = strdup(String); |
| 430 | Str.MustFreeString = 1; |
| 431 | } else { |
| 432 | Str.Spelling = String; |
| 433 | Str.MustFreeString = 0; |
| 434 | } |
| 435 | return Str; |
| 436 | } |
| 437 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 438 | extern "C" { |
| 439 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 440 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 441 | int displayDiagnostics) { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 442 | CIndexer *CIdxr = new CIndexer(new Program()); |
| 443 | if (excludeDeclarationsFromPCH) |
| 444 | CIdxr->setOnlyLocalDecls(); |
| 445 | if (displayDiagnostics) |
| 446 | CIdxr->setDisplayDiagnostics(); |
| 447 | return CIdxr; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 450 | void clang_disposeIndex(CXIndex CIdx) { |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 451 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 452 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 455 | // FIXME: need to pass back error info. |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 456 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
| 457 | const char *ast_filename) { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 458 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 459 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 460 | std::string astName(ast_filename); |
| 461 | std::string ErrMsg; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 462 | |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 463 | CXTranslationUnit TU = |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 464 | ASTUnit::LoadFromPCHFile(astName, &ErrMsg, |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 465 | CXXIdx->getDisplayDiagnostics() ? |
| 466 | NULL : new IgnoreDiagnosticsClient(), |
| 467 | CXXIdx->getOnlyLocalDecls(), |
| 468 | /* UseBumpAllocator = */ true); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 469 | |
Ted Kremenek | 0854d70 | 2009-11-10 19:18:52 +0000 | [diff] [blame] | 470 | if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) |
Ted Kremenek | 779e5f4 | 2009-10-26 22:08:39 +0000 | [diff] [blame] | 471 | llvm::errs() << "clang_createTranslationUnit: " << ErrMsg << '\n'; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 472 | |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 473 | return TU; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 476 | CXTranslationUnit |
| 477 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 478 | const char *source_filename, |
| 479 | int num_command_line_args, |
| 480 | const char **command_line_args) { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 481 | assert(CIdx && "Passed null CXIndex"); |
| 482 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 483 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 484 | // Build up the arguments for invoking 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 485 | std::vector<const char *> argv; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 486 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 487 | // First add the complete path to the 'clang' executable. |
| 488 | llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath(); |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 489 | argv.push_back(ClangPath.c_str()); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 490 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 491 | // Add the '-emit-ast' option as our execution mode for 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 492 | argv.push_back("-emit-ast"); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 493 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 494 | // The 'source_filename' argument is optional. If the caller does not |
| 495 | // specify it then it is assumed that the source file is specified |
| 496 | // in the actual argument list. |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 497 | if (source_filename) |
| 498 | argv.push_back(source_filename); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 499 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 500 | // Generate a temporary name for the AST file. |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 501 | argv.push_back("-o"); |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 502 | char astTmpFile[L_tmpnam]; |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 503 | argv.push_back(tmpnam(astTmpFile)); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 504 | |
| 505 | // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. |
| 506 | for (int i = 0; i < num_command_line_args; ++i) |
| 507 | if (const char *arg = command_line_args[i]) { |
| 508 | if (strcmp(arg, "-o") == 0) { |
| 509 | ++i; // Also skip the matching argument. |
| 510 | continue; |
| 511 | } |
| 512 | if (strcmp(arg, "-emit-ast") == 0 || |
| 513 | strcmp(arg, "-c") == 0 || |
| 514 | strcmp(arg, "-fsyntax-only") == 0) { |
| 515 | continue; |
| 516 | } |
| 517 | |
| 518 | // Keep the argument. |
| 519 | argv.push_back(arg); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 520 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 521 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 522 | // Add the null terminator. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 523 | argv.push_back(NULL); |
| 524 | |
Ted Kremenek | feb15e3 | 2009-10-26 22:14:08 +0000 | [diff] [blame] | 525 | // Invoke 'clang'. |
| 526 | llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null |
| 527 | // on Unix or NUL (Windows). |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 528 | std::string ErrMsg; |
Ted Kremenek | c46e463 | 2009-10-19 22:27:32 +0000 | [diff] [blame] | 529 | const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL }; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 530 | llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, |
| 531 | /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL, |
| 532 | /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 533 | |
Ted Kremenek | 0854d70 | 2009-11-10 19:18:52 +0000 | [diff] [blame] | 534 | if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) { |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 535 | llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 536 | << '\n' << "Arguments: \n"; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 537 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
Ted Kremenek | 779e5f4 | 2009-10-26 22:08:39 +0000 | [diff] [blame] | 538 | I!=E; ++I) { |
| 539 | if (*I) |
| 540 | llvm::errs() << ' ' << *I << '\n'; |
| 541 | } |
| 542 | llvm::errs() << '\n'; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 543 | } |
Benjamin Kramer | 0829a83 | 2009-10-18 11:19:36 +0000 | [diff] [blame] | 544 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 545 | // Finally, we create the translation unit from the ast file. |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 546 | ASTUnit *ATU = static_cast<ASTUnit *>( |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 547 | clang_createTranslationUnit(CIdx, astTmpFile)); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 548 | if (ATU) |
| 549 | ATU->unlinkTemporaryFile(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 550 | return ATU; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 553 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 554 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 555 | delete static_cast<ASTUnit *>(CTUnit); |
| 556 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 557 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 558 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 559 | assert(CTUnit && "Passed null CXTranslationUnit"); |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 560 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 561 | return createCXString(CXXUnit->getOriginalSourceFileName().c_str(), true); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 562 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 563 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 564 | void clang_loadTranslationUnit(CXTranslationUnit CTUnit, |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 565 | CXTranslationUnitIterator callback, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 566 | CXClientData CData) { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 567 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 568 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 569 | ASTContext &Ctx = CXXUnit->getASTContext(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 570 | |
| 571 | TUVisitor DVisit(CTUnit, callback, CData, |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 572 | CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel); |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 573 | DVisit.Visit(Ctx.getTranslationUnitDecl()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 576 | void clang_loadDeclaration(CXDecl Dcl, |
| 577 | CXDeclIterator callback, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 578 | CXClientData CData) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 579 | assert(Dcl && "Passed null CXDecl"); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 580 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 581 | CDeclVisitor DVisit(Dcl, callback, CData, |
| 582 | static_cast<Decl *>(Dcl)->getPCHLevel()); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 583 | DVisit.Visit(static_cast<Decl *>(Dcl)); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Steve Naroff | 7e8f818 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 586 | // Some notes on CXEntity: |
| 587 | // |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 588 | // - Since the 'ordinary' namespace includes functions, data, typedefs, |
| 589 | // ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one |
Steve Naroff | 7e8f818 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 590 | // entity for 2 different types). For example: |
| 591 | // |
| 592 | // module1.m: @interface Foo @end Foo *x; |
| 593 | // module2.m: void Foo(int); |
| 594 | // |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 595 | // - Since the unique name spans translation units, static data/functions |
Steve Naroff | 7e8f818 | 2009-08-28 12:07:44 +0000 | [diff] [blame] | 596 | // within a CXTranslationUnit are *not* currently represented by entities. |
| 597 | // As a result, there will be no entity for the following: |
| 598 | // |
| 599 | // module.m: static void Foo() { } |
| 600 | // |
| 601 | |
| 602 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 603 | const char *clang_getDeclarationName(CXEntity) { |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 604 | return ""; |
| 605 | } |
| 606 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 607 | const char *clang_getURI(CXEntity) { |
| 608 | return ""; |
| 609 | } |
| 610 | |
| 611 | CXEntity clang_getEntity(const char *URI) { |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | // |
| 616 | // CXDecl Operations. |
| 617 | // |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 618 | |
| 619 | CXEntity clang_getEntityFromDecl(CXDecl) { |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 620 | return 0; |
| 621 | } |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 622 | |
| 623 | CXString clang_getDeclSpelling(CXDecl AnonDecl) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 624 | assert(AnonDecl && "Passed null CXDecl"); |
| 625 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 626 | |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 627 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
| 628 | return createCXString(OMD->getSelector().getAsString().c_str(), true); |
| 629 | |
| 630 | if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
Steve Naroff | 0d69b8c | 2009-10-29 21:11:04 +0000 | [diff] [blame] | 631 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 632 | // and returns different names. NamedDecl returns the class name and |
| 633 | // ObjCCategoryImplDecl returns the category name. |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 634 | return createCXString(CIMP->getIdentifier()->getNameStart()); |
| 635 | |
| 636 | if (ND->getIdentifier()) |
| 637 | return createCXString(ND->getIdentifier()->getNameStart()); |
| 638 | |
| 639 | return createCXString(""); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 640 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 641 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 642 | unsigned clang_getDeclLine(CXDecl AnonDecl) { |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 643 | assert(AnonDecl && "Passed null CXDecl"); |
| 644 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 645 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
| 646 | return SourceMgr.getSpellingLineNumber(ND->getLocation()); |
| 647 | } |
| 648 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 649 | unsigned clang_getDeclColumn(CXDecl AnonDecl) { |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 650 | assert(AnonDecl && "Passed null CXDecl"); |
| 651 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 652 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 7416524 | 2009-09-25 22:15:54 +0000 | [diff] [blame] | 653 | return SourceMgr.getSpellingColumnNumber(ND->getLocation()); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 656 | const char *clang_getDeclSource(CXDecl AnonDecl) { |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 657 | assert(AnonDecl && "Passed null CXDecl"); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 658 | FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl)); |
| 659 | assert (FEnt && "Cannot find FileEntry for Decl"); |
| 660 | return clang_getFileName(FEnt); |
| 661 | } |
| 662 | |
| 663 | static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 664 | SourceLocation SLoc) { |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 665 | FileID FID; |
| 666 | if (SLoc.isFileID()) |
| 667 | FID = SMgr.getFileID(SLoc); |
| 668 | else |
| 669 | FID = SMgr.getDecomposedSpellingLoc(SLoc).first; |
| 670 | return SMgr.getFileEntryForID(FID); |
| 671 | } |
| 672 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 673 | CXFile clang_getDeclSourceFile(CXDecl AnonDecl) { |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 674 | assert(AnonDecl && "Passed null CXDecl"); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 675 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 676 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 677 | return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation()); |
| 678 | } |
| 679 | |
| 680 | const char *clang_getFileName(CXFile SFile) { |
| 681 | assert(SFile && "Passed null CXFile"); |
| 682 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 683 | return FEnt->getName(); |
| 684 | } |
| 685 | |
| 686 | time_t clang_getFileTime(CXFile SFile) { |
| 687 | assert(SFile && "Passed null CXFile"); |
| 688 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 689 | return FEnt->getModificationTime(); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 692 | CXString clang_getCursorSpelling(CXCursor C) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 693 | assert(C.decl && "CXCursor has null decl"); |
| 694 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 695 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 696 | if (clang_isReference(C.kind)) { |
| 697 | switch (C.kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 698 | case CXCursor_ObjCSuperClassRef: { |
| 699 | ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND); |
| 700 | assert(OID && "clang_getCursorLine(): Missing interface decl"); |
| 701 | return createCXString(OID->getSuperClass()->getIdentifier() |
| 702 | ->getNameStart()); |
| 703 | } |
| 704 | case CXCursor_ObjCClassRef: { |
| 705 | if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 706 | return createCXString(OID->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 707 | |
| 708 | ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(ND); |
| 709 | assert(OCD && "clang_getCursorLine(): Missing category decl"); |
| 710 | return createCXString(OCD->getClassInterface()->getIdentifier() |
| 711 | ->getNameStart()); |
| 712 | } |
| 713 | case CXCursor_ObjCProtocolRef: { |
| 714 | ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND); |
| 715 | assert(OID && "clang_getCursorLine(): Missing protocol decl"); |
| 716 | return createCXString(OID->getIdentifier()->getNameStart()); |
| 717 | } |
| 718 | case CXCursor_ObjCSelectorRef: { |
| 719 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>( |
| 720 | static_cast<Stmt *>(C.stmt)); |
| 721 | assert(OME && "clang_getCursorLine(): Missing message expr"); |
| 722 | return createCXString(OME->getSelector().getAsString().c_str(), true); |
| 723 | } |
| 724 | case CXCursor_VarRef: |
| 725 | case CXCursor_FunctionRef: |
| 726 | case CXCursor_EnumConstantRef: { |
| 727 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>( |
| 728 | static_cast<Stmt *>(C.stmt)); |
| 729 | assert(DRE && "clang_getCursorLine(): Missing decl ref expr"); |
| 730 | return createCXString(DRE->getDecl()->getIdentifier()->getNameStart()); |
| 731 | } |
| 732 | default: |
| 733 | return createCXString("<not implemented>"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | return clang_getDeclSpelling(C.decl); |
| 737 | } |
| 738 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 739 | const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 740 | switch (Kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 741 | case CXCursor_FunctionDecl: return "FunctionDecl"; |
| 742 | case CXCursor_TypedefDecl: return "TypedefDecl"; |
| 743 | case CXCursor_EnumDecl: return "EnumDecl"; |
| 744 | case CXCursor_EnumConstantDecl: return "EnumConstantDecl"; |
| 745 | case CXCursor_StructDecl: return "StructDecl"; |
| 746 | case CXCursor_UnionDecl: return "UnionDecl"; |
| 747 | case CXCursor_ClassDecl: return "ClassDecl"; |
| 748 | case CXCursor_FieldDecl: return "FieldDecl"; |
| 749 | case CXCursor_VarDecl: return "VarDecl"; |
| 750 | case CXCursor_ParmDecl: return "ParmDecl"; |
| 751 | case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl"; |
| 752 | case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl"; |
| 753 | case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl"; |
| 754 | case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl"; |
| 755 | case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl"; |
| 756 | case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl"; |
| 757 | case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl"; |
| 758 | case CXCursor_FunctionDefn: return "FunctionDefn"; |
| 759 | case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn"; |
| 760 | case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn"; |
| 761 | case CXCursor_ObjCClassDefn: return "ObjCClassDefn"; |
| 762 | case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn"; |
| 763 | case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; |
| 764 | case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef"; |
| 765 | case CXCursor_ObjCClassRef: return "ObjCClassRef"; |
| 766 | case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef"; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 767 | |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 768 | case CXCursor_VarRef: return "VarRef"; |
| 769 | case CXCursor_FunctionRef: return "FunctionRef"; |
| 770 | case CXCursor_EnumConstantRef: return "EnumConstantRef"; |
| 771 | case CXCursor_MemberRef: return "MemberRef"; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 772 | |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 773 | case CXCursor_InvalidFile: return "InvalidFile"; |
| 774 | case CXCursor_NoDeclFound: return "NoDeclFound"; |
| 775 | case CXCursor_NotImplemented: return "NotImplemented"; |
| 776 | default: return "<not implemented>"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 777 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 778 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 779 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 780 | static enum CXCursorKind TranslateKind(Decl *D) { |
| 781 | switch (D->getKind()) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 782 | case Decl::Function: return CXCursor_FunctionDecl; |
| 783 | case Decl::Typedef: return CXCursor_TypedefDecl; |
| 784 | case Decl::Enum: return CXCursor_EnumDecl; |
| 785 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; |
| 786 | case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class |
| 787 | case Decl::Field: return CXCursor_FieldDecl; |
| 788 | case Decl::Var: return CXCursor_VarDecl; |
| 789 | case Decl::ParmVar: return CXCursor_ParmDecl; |
| 790 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; |
| 791 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; |
| 792 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; |
| 793 | case Decl::ObjCMethod: { |
| 794 | ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D); |
| 795 | if (MD->isInstanceMethod()) |
| 796 | return CXCursor_ObjCInstanceMethodDecl; |
| 797 | return CXCursor_ObjCClassMethodDecl; |
| 798 | } |
| 799 | default: break; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 800 | } |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 801 | return CXCursor_NotImplemented; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 802 | } |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 803 | // |
| 804 | // CXCursor Operations. |
| 805 | // |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 806 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 807 | CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 808 | unsigned line, unsigned column) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 809 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 810 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 811 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 812 | FileManager &FMgr = CXXUnit->getFileManager(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 813 | const FileEntry *File = FMgr.getFile(source_name, |
| 814 | source_name+strlen(source_name)); |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 815 | if (!File) { |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 816 | CXCursor C = { CXCursor_InvalidFile, 0, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 817 | return C; |
| 818 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 819 | SourceLocation SLoc = |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 820 | CXXUnit->getSourceManager().getLocation(File, line, column); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 821 | |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 822 | ASTLocation LastLoc = CXXUnit->getLastASTLocation(); |
| 823 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 824 | ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc, |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 825 | &LastLoc); |
| 826 | if (ALoc.isValid()) |
| 827 | CXXUnit->setLastASTLocation(ALoc); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 828 | |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 829 | Decl *Dcl = ALoc.getParentDecl(); |
Argyrios Kyrtzidis | 05a7651 | 2009-09-29 19:45:58 +0000 | [diff] [blame] | 830 | if (ALoc.isNamedRef()) |
| 831 | Dcl = ALoc.AsNamedRef().ND; |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 832 | Stmt *Stm = ALoc.dyn_AsStmt(); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 833 | if (Dcl) { |
| 834 | if (Stm) { |
| 835 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) { |
| 836 | CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm }; |
| 837 | return C; |
| 838 | } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) { |
| 839 | CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp }; |
| 840 | return C; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 841 | } |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 842 | // Fall through...treat as a decl, not a ref. |
| 843 | } |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 844 | if (ALoc.isNamedRef()) { |
| 845 | if (isa<ObjCInterfaceDecl>(Dcl)) { |
| 846 | CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() }; |
| 847 | return C; |
| 848 | } |
| 849 | if (isa<ObjCProtocolDecl>(Dcl)) { |
| 850 | CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() }; |
| 851 | return C; |
| 852 | } |
| 853 | } |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 854 | CXCursor C = { TranslateKind(Dcl), Dcl, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 855 | return C; |
| 856 | } |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 857 | CXCursor C = { CXCursor_NoDeclFound, 0, 0 }; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 858 | return C; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 861 | CXCursor clang_getNullCursor(void) { |
| 862 | CXCursor C; |
| 863 | C.kind = CXCursor_InvalidFile; |
| 864 | C.decl = NULL; |
| 865 | C.stmt = NULL; |
| 866 | return C; |
| 867 | } |
| 868 | |
| 869 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
| 870 | return X.kind == Y.kind && X.decl == Y.decl && X.stmt == Y.stmt; |
| 871 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 872 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 873 | CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 874 | assert(AnonDecl && "Passed null CXDecl"); |
| 875 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 876 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 877 | CXCursor C = { TranslateKind(ND), ND, 0 }; |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 878 | return C; |
| 879 | } |
| 880 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 881 | unsigned clang_isInvalid(enum CXCursorKind K) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 882 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 883 | } |
| 884 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 885 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 886 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 887 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 888 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 889 | unsigned clang_isReference(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 890 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 891 | } |
| 892 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 893 | unsigned clang_isDefinition(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 894 | return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn; |
| 895 | } |
| 896 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 897 | CXCursorKind clang_getCursorKind(CXCursor C) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 898 | return C.kind; |
| 899 | } |
| 900 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 901 | static Decl *getDeclFromExpr(Stmt *E) { |
| 902 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 903 | return RefExpr->getDecl(); |
| 904 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 905 | return ME->getMemberDecl(); |
| 906 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 907 | return RE->getDecl(); |
| 908 | |
| 909 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 910 | return getDeclFromExpr(CE->getCallee()); |
| 911 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) |
| 912 | return getDeclFromExpr(CE->getSubExpr()); |
| 913 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 914 | return OME->getMethodDecl(); |
| 915 | |
| 916 | return 0; |
| 917 | } |
| 918 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 919 | CXDecl clang_getCursorDecl(CXCursor C) { |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 920 | if (clang_isDeclaration(C.kind)) |
| 921 | return C.decl; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 922 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 923 | if (clang_isReference(C.kind)) { |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 924 | if (C.stmt) { |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 925 | if (C.kind == CXCursor_ObjCClassRef || |
Steve Naroff | f9adf8f | 2009-10-05 17:58:19 +0000 | [diff] [blame] | 926 | C.kind == CXCursor_ObjCProtocolRef) |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 927 | return static_cast<Stmt *>(C.stmt); |
| 928 | else |
| 929 | return getDeclFromExpr(static_cast<Stmt *>(C.stmt)); |
| 930 | } else |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 931 | return C.decl; |
| 932 | } |
| 933 | return 0; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 936 | unsigned clang_getCursorLine(CXCursor C) { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 937 | assert(C.decl && "CXCursor has null decl"); |
| 938 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 939 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 940 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 941 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 942 | return SourceMgr.getSpellingLineNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 943 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 944 | |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 945 | const char *clang_getCString(CXString string) { |
| 946 | return string.Spelling; |
| 947 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 948 | |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 949 | void clang_disposeString(CXString string) { |
Benjamin Kramer | 858e5de | 2009-11-09 18:24:53 +0000 | [diff] [blame] | 950 | if (string.MustFreeString) |
| 951 | free((void*)string.Spelling); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 954 | unsigned clang_getCursorColumn(CXCursor C) { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 955 | assert(C.decl && "CXCursor has null decl"); |
| 956 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 957 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 958 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 959 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 960 | return SourceMgr.getSpellingColumnNumber(SLoc); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 961 | } |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 962 | |
| 963 | const char *clang_getCursorSource(CXCursor C) { |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 964 | assert(C.decl && "CXCursor has null decl"); |
| 965 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 966 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 967 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 968 | SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 969 | |
Ted Kremenek | 9298cfc | 2009-11-17 05:31:58 +0000 | [diff] [blame] | 970 | if (SLoc.isFileID()) { |
| 971 | const char *bufferName = SourceMgr.getBufferName(SLoc); |
| 972 | return bufferName[0] == '<' ? NULL : bufferName; |
| 973 | } |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 974 | |
| 975 | // Retrieve the file in which the macro was instantiated, then provide that |
| 976 | // buffer name. |
| 977 | // FIXME: Do we want to give specific macro-instantiation information? |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 978 | const llvm::MemoryBuffer *Buffer |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 979 | = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first); |
| 980 | if (!Buffer) |
| 981 | return 0; |
| 982 | |
| 983 | return Buffer->getBufferIdentifier(); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 986 | CXFile clang_getCursorSourceFile(CXCursor C) { |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 987 | assert(C.decl && "CXCursor has null decl"); |
| 988 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 989 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 990 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 991 | return (void *)getFileEntryFromSourceLocation(SourceMgr, |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 992 | getLocationFromCursor(C,SourceMgr, ND)); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 995 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 996 | const char **startBuf, |
| 997 | const char **endBuf, |
| 998 | unsigned *startLine, |
| 999 | unsigned *startColumn, |
| 1000 | unsigned *endLine, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1001 | unsigned *endColumn) { |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1002 | assert(C.decl && "CXCursor has null decl"); |
| 1003 | NamedDecl *ND = static_cast<NamedDecl *>(C.decl); |
| 1004 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 1005 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1006 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1007 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 1008 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 1009 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 1010 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 1011 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 1012 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 1013 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 1014 | } |
| 1015 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1016 | enum CXCompletionChunkKind |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1017 | clang_getCompletionChunkKind(CXCompletionString completion_string, |
| 1018 | unsigned chunk_number) { |
| 1019 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 1020 | if (!CCStr || chunk_number >= CCStr->size()) |
| 1021 | return CXCompletionChunk_Text; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1022 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1023 | switch ((*CCStr)[chunk_number].Kind) { |
| 1024 | case CodeCompletionString::CK_TypedText: |
| 1025 | return CXCompletionChunk_TypedText; |
| 1026 | case CodeCompletionString::CK_Text: |
| 1027 | return CXCompletionChunk_Text; |
| 1028 | case CodeCompletionString::CK_Optional: |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1029 | return CXCompletionChunk_Optional; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1030 | case CodeCompletionString::CK_Placeholder: |
| 1031 | return CXCompletionChunk_Placeholder; |
| 1032 | case CodeCompletionString::CK_Informative: |
| 1033 | return CXCompletionChunk_Informative; |
| 1034 | case CodeCompletionString::CK_CurrentParameter: |
| 1035 | return CXCompletionChunk_CurrentParameter; |
| 1036 | case CodeCompletionString::CK_LeftParen: |
| 1037 | return CXCompletionChunk_LeftParen; |
| 1038 | case CodeCompletionString::CK_RightParen: |
| 1039 | return CXCompletionChunk_RightParen; |
| 1040 | case CodeCompletionString::CK_LeftBracket: |
| 1041 | return CXCompletionChunk_LeftBracket; |
| 1042 | case CodeCompletionString::CK_RightBracket: |
| 1043 | return CXCompletionChunk_RightBracket; |
| 1044 | case CodeCompletionString::CK_LeftBrace: |
| 1045 | return CXCompletionChunk_LeftBrace; |
| 1046 | case CodeCompletionString::CK_RightBrace: |
| 1047 | return CXCompletionChunk_RightBrace; |
| 1048 | case CodeCompletionString::CK_LeftAngle: |
| 1049 | return CXCompletionChunk_LeftAngle; |
| 1050 | case CodeCompletionString::CK_RightAngle: |
| 1051 | return CXCompletionChunk_RightAngle; |
| 1052 | case CodeCompletionString::CK_Comma: |
| 1053 | return CXCompletionChunk_Comma; |
| 1054 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1055 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1056 | // Should be unreachable, but let's be careful. |
| 1057 | return CXCompletionChunk_Text; |
| 1058 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1059 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1060 | const char *clang_getCompletionChunkText(CXCompletionString completion_string, |
| 1061 | unsigned chunk_number) { |
| 1062 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 1063 | if (!CCStr || chunk_number >= CCStr->size()) |
| 1064 | return 0; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1065 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1066 | switch ((*CCStr)[chunk_number].Kind) { |
| 1067 | case CodeCompletionString::CK_TypedText: |
| 1068 | case CodeCompletionString::CK_Text: |
| 1069 | case CodeCompletionString::CK_Placeholder: |
| 1070 | case CodeCompletionString::CK_CurrentParameter: |
| 1071 | case CodeCompletionString::CK_Informative: |
| 1072 | case CodeCompletionString::CK_LeftParen: |
| 1073 | case CodeCompletionString::CK_RightParen: |
| 1074 | case CodeCompletionString::CK_LeftBracket: |
| 1075 | case CodeCompletionString::CK_RightBracket: |
| 1076 | case CodeCompletionString::CK_LeftBrace: |
| 1077 | case CodeCompletionString::CK_RightBrace: |
| 1078 | case CodeCompletionString::CK_LeftAngle: |
| 1079 | case CodeCompletionString::CK_RightAngle: |
| 1080 | case CodeCompletionString::CK_Comma: |
| 1081 | return (*CCStr)[chunk_number].Text; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1082 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1083 | case CodeCompletionString::CK_Optional: |
| 1084 | // Note: treated as an empty text block. |
| 1085 | return 0; |
| 1086 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1087 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1088 | // Should be unreachable, but let's be careful. |
| 1089 | return 0; |
| 1090 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1091 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1092 | CXCompletionString |
| 1093 | clang_getCompletionChunkCompletionString(CXCompletionString completion_string, |
| 1094 | unsigned chunk_number) { |
| 1095 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 1096 | if (!CCStr || chunk_number >= CCStr->size()) |
| 1097 | return 0; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1098 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1099 | switch ((*CCStr)[chunk_number].Kind) { |
| 1100 | case CodeCompletionString::CK_TypedText: |
| 1101 | case CodeCompletionString::CK_Text: |
| 1102 | case CodeCompletionString::CK_Placeholder: |
| 1103 | case CodeCompletionString::CK_CurrentParameter: |
| 1104 | case CodeCompletionString::CK_Informative: |
| 1105 | case CodeCompletionString::CK_LeftParen: |
| 1106 | case CodeCompletionString::CK_RightParen: |
| 1107 | case CodeCompletionString::CK_LeftBracket: |
| 1108 | case CodeCompletionString::CK_RightBracket: |
| 1109 | case CodeCompletionString::CK_LeftBrace: |
| 1110 | case CodeCompletionString::CK_RightBrace: |
| 1111 | case CodeCompletionString::CK_LeftAngle: |
| 1112 | case CodeCompletionString::CK_RightAngle: |
| 1113 | case CodeCompletionString::CK_Comma: |
| 1114 | return 0; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1115 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1116 | case CodeCompletionString::CK_Optional: |
| 1117 | // Note: treated as an empty text block. |
| 1118 | return (*CCStr)[chunk_number].Optional; |
| 1119 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1120 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1121 | // Should be unreachable, but let's be careful. |
| 1122 | return 0; |
| 1123 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1124 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1125 | unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) { |
| 1126 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 1127 | return CCStr? CCStr->size() : 0; |
| 1128 | } |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1129 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 1130 | static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd, |
| 1131 | unsigned &Value) { |
| 1132 | if (Memory + sizeof(unsigned) > MemoryEnd) |
| 1133 | return true; |
| 1134 | |
| 1135 | memmove(&Value, Memory, sizeof(unsigned)); |
| 1136 | Memory += sizeof(unsigned); |
| 1137 | return false; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1138 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1139 | |
| 1140 | void clang_codeComplete(CXIndex CIdx, |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1141 | const char *source_filename, |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1142 | int num_command_line_args, |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1143 | const char **command_line_args, |
| 1144 | const char *complete_filename, |
| 1145 | unsigned complete_line, |
| 1146 | unsigned complete_column, |
| 1147 | CXCompletionIterator completion_iterator, |
| 1148 | CXClientData client_data) { |
| 1149 | // The indexer, which is mainly used to determine where diagnostics go. |
| 1150 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1151 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1152 | // Build up the arguments for invoking 'clang'. |
| 1153 | std::vector<const char *> argv; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1154 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1155 | // First add the complete path to the 'clang' executable. |
| 1156 | llvm::sys::Path ClangPath = CXXIdx->getClangPath(); |
| 1157 | argv.push_back(ClangPath.c_str()); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1158 | |
| 1159 | // Add the '-fsyntax-only' argument so that we only perform a basic |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1160 | // syntax check of the code. |
| 1161 | argv.push_back("-fsyntax-only"); |
| 1162 | |
| 1163 | // Add the appropriate '-code-completion-at=file:line:column' argument |
| 1164 | // to perform code completion, with an "-Xclang" preceding it. |
| 1165 | std::string code_complete_at; |
| 1166 | code_complete_at += "-code-completion-at="; |
| 1167 | code_complete_at += complete_filename; |
| 1168 | code_complete_at += ":"; |
| 1169 | code_complete_at += llvm::utostr(complete_line); |
| 1170 | code_complete_at += ":"; |
| 1171 | code_complete_at += llvm::utostr(complete_column); |
| 1172 | argv.push_back("-Xclang"); |
| 1173 | argv.push_back(code_complete_at.c_str()); |
| 1174 | argv.push_back("-Xclang"); |
Daniel Dunbar | 4db166b | 2009-11-19 05:32:09 +0000 | [diff] [blame] | 1175 | argv.push_back("-no-code-completion-debug-printer"); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1176 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1177 | // Add the source file name (FIXME: later, we'll want to build temporary |
| 1178 | // file from the buffer, or just feed the source text via standard input). |
Ted Kremenek | 4633d1b | 2009-11-17 18:18:02 +0000 | [diff] [blame] | 1179 | if (source_filename) |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1180 | argv.push_back(source_filename); |
| 1181 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1182 | // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. |
| 1183 | for (int i = 0; i < num_command_line_args; ++i) |
| 1184 | if (const char *arg = command_line_args[i]) { |
| 1185 | if (strcmp(arg, "-o") == 0) { |
| 1186 | ++i; // Also skip the matching argument. |
| 1187 | continue; |
| 1188 | } |
| 1189 | if (strcmp(arg, "-emit-ast") == 0 || |
| 1190 | strcmp(arg, "-c") == 0 || |
| 1191 | strcmp(arg, "-fsyntax-only") == 0) { |
| 1192 | continue; |
| 1193 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1194 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1195 | // Keep the argument. |
| 1196 | argv.push_back(arg); |
| 1197 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1198 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1199 | // Add the null terminator. |
| 1200 | argv.push_back(NULL); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1201 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1202 | // Generate a temporary name for the AST file. |
| 1203 | char tmpFile[L_tmpnam]; |
| 1204 | char *tmpFileName = tmpnam(tmpFile); |
| 1205 | llvm::sys::Path ResultsFile(tmpFileName); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1206 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1207 | // Invoke 'clang'. |
| 1208 | llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null |
| 1209 | // on Unix or NUL (Windows). |
| 1210 | std::string ErrMsg; |
| 1211 | const llvm::sys::Path *Redirects[] = { &DevNull, &ResultsFile, &DevNull, 0 }; |
| 1212 | llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, |
| 1213 | /* redirects */ &Redirects[0], |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1214 | /* secondsToWait */ 0, |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1215 | /* memoryLimits */ 0, &ErrMsg); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1216 | |
Ted Kremenek | 0854d70 | 2009-11-10 19:18:52 +0000 | [diff] [blame] | 1217 | if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) { |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1218 | llvm::errs() << "clang_codeComplete: " << ErrMsg |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 1219 | << '\n' << "Arguments: \n"; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1220 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
| 1221 | I!=E; ++I) { |
| 1222 | if (*I) |
| 1223 | llvm::errs() << ' ' << *I << '\n'; |
| 1224 | } |
| 1225 | llvm::errs() << '\n'; |
| 1226 | } |
| 1227 | |
| 1228 | // Parse the resulting source file to find code-completion results. |
| 1229 | using llvm::MemoryBuffer; |
| 1230 | using llvm::StringRef; |
| 1231 | if (MemoryBuffer *F = MemoryBuffer::getFile(ResultsFile.c_str())) { |
| 1232 | StringRef Buffer = F->getBuffer(); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 1233 | for (const char *Str = Buffer.data(), *StrEnd = Str + Buffer.size(); |
| 1234 | Str < StrEnd;) { |
| 1235 | unsigned KindValue; |
| 1236 | if (ReadUnsigned(Str, StrEnd, KindValue)) |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1237 | break; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1238 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 1239 | CodeCompletionString *CCStr |
| 1240 | = CodeCompletionString::Deserialize(Str, StrEnd); |
| 1241 | if (!CCStr) |
| 1242 | continue; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1243 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 1244 | if (!CCStr->empty()) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1245 | // Vend the code-completion result to the caller. |
| 1246 | CXCompletionResult Result; |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 1247 | Result.CursorKind = (CXCursorKind)KindValue; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1248 | Result.CompletionString = CCStr; |
| 1249 | if (completion_iterator) |
| 1250 | completion_iterator(&Result, client_data); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1251 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1252 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1253 | delete CCStr; |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 1254 | }; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1255 | delete F; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1258 | ResultsFile.eraseFromDisk(); |
| 1259 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1260 | |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 1261 | } // end extern "C" |