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 | // |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 10 | // This file implements the main API hooks in the Clang-C Source Indexing |
| 11 | // library. |
Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 15 | #include "CIndexer.h" |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 16 | #include "CXCursor.h" |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 17 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclVisitor.h" |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 19 | #include "clang/AST/StmtVisitor.h" |
Ted Kremenek | d821065 | 2010-01-06 23:43:31 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Lexer.h" |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" |
Benjamin Kramer | 0829a83 | 2009-10-18 11:19:36 +0000 | [diff] [blame] | 22 | #include "llvm/System/Program.h" |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 23 | |
Ted Kremenek | db3d0da | 2010-01-05 20:55:39 +0000 | [diff] [blame] | 24 | // Needed to define L_TMPNAM on some systems. |
| 25 | #include <cstdio> |
| 26 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 27 | using namespace clang; |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 28 | using namespace clang::cxcursor; |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 29 | using namespace idx; |
| 30 | |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Crash Reporting. |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
| 35 | #ifdef __APPLE__ |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 36 | #ifndef NDEBUG |
| 37 | #define USE_CRASHTRACER |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 38 | #include "clang/Analysis/Support/SaveAndRestore.h" |
| 39 | // Integrate with crash reporter. |
| 40 | extern "C" const char *__crashreporter_info__; |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 41 | #define NUM_CRASH_STRINGS 16 |
| 42 | static unsigned crashtracer_counter = 0; |
Ted Kremenek | 254ba7c | 2010-01-07 23:13:53 +0000 | [diff] [blame] | 43 | static unsigned crashtracer_counter_id[NUM_CRASH_STRINGS] = { 0 }; |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 44 | static const char *crashtracer_strings[NUM_CRASH_STRINGS] = { 0 }; |
| 45 | static const char *agg_crashtracer_strings[NUM_CRASH_STRINGS] = { 0 }; |
| 46 | |
| 47 | static unsigned SetCrashTracerInfo(const char *str, |
| 48 | llvm::SmallString<1024> &AggStr) { |
| 49 | |
Ted Kremenek | 254ba7c | 2010-01-07 23:13:53 +0000 | [diff] [blame] | 50 | unsigned slot = 0; |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 51 | while (crashtracer_strings[slot]) { |
| 52 | if (++slot == NUM_CRASH_STRINGS) |
| 53 | slot = 0; |
| 54 | } |
| 55 | crashtracer_strings[slot] = str; |
Ted Kremenek | 254ba7c | 2010-01-07 23:13:53 +0000 | [diff] [blame] | 56 | crashtracer_counter_id[slot] = ++crashtracer_counter; |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 57 | |
| 58 | // We need to create an aggregate string because multiple threads |
| 59 | // may be in this method at one time. The crash reporter string |
| 60 | // will attempt to overapproximate the set of in-flight invocations |
| 61 | // of this function. Race conditions can still cause this goal |
| 62 | // to not be achieved. |
| 63 | { |
| 64 | llvm::raw_svector_ostream Out(AggStr); |
| 65 | for (unsigned i = 0; i < NUM_CRASH_STRINGS; ++i) |
| 66 | if (crashtracer_strings[i]) Out << crashtracer_strings[i] << '\n'; |
| 67 | } |
| 68 | __crashreporter_info__ = agg_crashtracer_strings[slot] = AggStr.c_str(); |
| 69 | return slot; |
| 70 | } |
| 71 | |
| 72 | static void ResetCrashTracerInfo(unsigned slot) { |
Ted Kremenek | 254ba7c | 2010-01-07 23:13:53 +0000 | [diff] [blame] | 73 | unsigned max_slot = 0; |
| 74 | unsigned max_value = 0; |
| 75 | |
| 76 | crashtracer_strings[slot] = agg_crashtracer_strings[slot] = 0; |
| 77 | |
| 78 | for (unsigned i = 0 ; i < NUM_CRASH_STRINGS; ++i) |
| 79 | if (agg_crashtracer_strings[i] && |
| 80 | crashtracer_counter_id[i] > max_value) { |
| 81 | max_slot = i; |
| 82 | max_value = crashtracer_counter_id[i]; |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 83 | } |
Ted Kremenek | 254ba7c | 2010-01-07 23:13:53 +0000 | [diff] [blame] | 84 | |
| 85 | __crashreporter_info__ = agg_crashtracer_strings[max_slot]; |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | namespace { |
| 89 | class ArgsCrashTracerInfo { |
| 90 | llvm::SmallString<1024> CrashString; |
| 91 | llvm::SmallString<1024> AggregateString; |
| 92 | unsigned crashtracerSlot; |
| 93 | public: |
| 94 | ArgsCrashTracerInfo(llvm::SmallVectorImpl<const char*> &Args) |
| 95 | : crashtracerSlot(0) |
| 96 | { |
| 97 | { |
| 98 | llvm::raw_svector_ostream Out(CrashString); |
| 99 | Out << "ClangCIndex [createTranslationUnitFromSourceFile]: clang"; |
| 100 | for (llvm::SmallVectorImpl<const char*>::iterator I=Args.begin(), |
| 101 | E=Args.end(); I!=E; ++I) |
| 102 | Out << ' ' << *I; |
| 103 | } |
| 104 | crashtracerSlot = SetCrashTracerInfo(CrashString.c_str(), |
| 105 | AggregateString); |
| 106 | } |
| 107 | |
| 108 | ~ArgsCrashTracerInfo() { |
| 109 | ResetCrashTracerInfo(crashtracerSlot); |
| 110 | } |
| 111 | }; |
| 112 | } |
| 113 | #endif |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 114 | #endif |
| 115 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 116 | /// \brief Translate a Clang source location into a CIndex source location. |
| 117 | static CXSourceLocation translateSourceLocation(SourceManager &SourceMgr, |
| 118 | SourceLocation Loc) { |
| 119 | SourceLocation InstLoc = SourceMgr.getInstantiationLoc(Loc); |
| 120 | if (InstLoc.isInvalid()) { |
| 121 | CXSourceLocation Loc = { 0, 0, 0 }; |
| 122 | return Loc; |
| 123 | } |
| 124 | |
| 125 | CXSourceLocation Result; |
| 126 | Result.file |
| 127 | = (void*)SourceMgr.getFileEntryForID(SourceMgr.getFileID(InstLoc)); |
| 128 | Result.line = SourceMgr.getInstantiationLineNumber(InstLoc); |
| 129 | Result.column = SourceMgr.getInstantiationColumnNumber(InstLoc); |
| 130 | return Result; |
| 131 | } |
| 132 | |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 133 | //===----------------------------------------------------------------------===// |
| 134 | // Visitors. |
| 135 | //===----------------------------------------------------------------------===// |
| 136 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 137 | namespace { |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 138 | static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) { |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 139 | NamedDecl *D = DRE->getDecl(); |
| 140 | if (isa<VarDecl>(D)) |
| 141 | return CXCursor_VarRef; |
| 142 | else if (isa<FunctionDecl>(D)) |
| 143 | return CXCursor_FunctionRef; |
| 144 | else if (isa<EnumConstantDecl>(D)) |
| 145 | return CXCursor_EnumConstantRef; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 146 | else |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 147 | return CXCursor_NotImplemented; |
| 148 | } |
| 149 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 150 | // Translation Unit Visitor. |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 151 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 152 | class TUVisitor : public DeclVisitor<TUVisitor> { |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 153 | public: |
| 154 | typedef void (*Iterator)(void *, CXCursor, CXClientData); |
| 155 | private: |
| 156 | void *Root; // CXDecl or CXTranslationUnit |
| 157 | Iterator Callback; // CXTranslationUnitIterator or CXDeclIterator. |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 158 | CXClientData CData; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 159 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 160 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 161 | // to the visitor. Declarations with a PCH level greater than this value will |
| 162 | // be suppressed. |
| 163 | unsigned MaxPCHLevel; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 164 | |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 165 | void Call(const CXCursor &C) { |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 166 | if (clang_isInvalid(C.kind)) |
| 167 | return; |
| 168 | |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 169 | if (const Decl *D = getCursorDecl(C)) { |
| 170 | // Filter any declarations that have a PCH level greater than what |
| 171 | // we allow. |
| 172 | if (D->getPCHLevel() > MaxPCHLevel) |
| 173 | return; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 174 | |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 175 | // Filter any implicit declarations (since the source info will be bogus). |
| 176 | if (D->isImplicit()) |
| 177 | return; |
| 178 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 179 | |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 180 | Callback(Root, C, CData); |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 181 | } |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 182 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 183 | public: |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 184 | TUVisitor(void *root, Iterator cback, CXClientData D, unsigned MaxPCHLevel) : |
| 185 | Root(root), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {} |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 186 | |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 187 | void VisitDecl(Decl *D); |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 188 | void VisitDeclContext(DeclContext *DC); |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 189 | void VisitTranslationUnitDecl(TranslationUnitDecl *D); |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 190 | }; |
Ted Kremenek | ef7fdc6 | 2009-11-17 07:02:15 +0000 | [diff] [blame] | 191 | |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 192 | void TUVisitor::VisitDecl(Decl *D) { |
| 193 | Call(MakeCXCursor(D)); |
| 194 | } |
| 195 | |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 196 | void TUVisitor::VisitDeclContext(DeclContext *DC) { |
| 197 | for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 198 | I != E; ++I) |
| 199 | Visit(*I); |
| 200 | } |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 201 | |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 202 | void TUVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 203 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 204 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 205 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 206 | // Declaration visitor. |
| 207 | class CDeclVisitor : public DeclVisitor<CDeclVisitor> { |
| 208 | CXDecl CDecl; |
| 209 | CXDeclIterator Callback; |
| 210 | CXClientData CData; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 211 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 212 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 213 | // to the visitor. Declarations with a PCH level greater than this value will |
| 214 | // be suppressed. |
| 215 | unsigned MaxPCHLevel; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 216 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 217 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 218 | // Disable the callback when the context is equal to the visiting decl. |
| 219 | if (CDecl == ND && !clang_isReference(CK)) |
| 220 | return; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 221 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 222 | // Filter any declarations that have a PCH level greater than what we allow. |
| 223 | if (ND->getPCHLevel() > MaxPCHLevel) |
| 224 | return; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 225 | |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 226 | CXCursor C = { CK, { ND, 0, 0 } }; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 227 | Callback(CDecl, C, CData); |
| 228 | } |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 229 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 230 | public: |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 231 | CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D, |
| 232 | unsigned MaxPCHLevel) : |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 233 | CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {} |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 234 | |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 235 | void VisitDeclContext(DeclContext *DC); |
| 236 | void VisitEnumConstantDecl(EnumConstantDecl *ND); |
| 237 | void VisitFieldDecl(FieldDecl *ND); |
| 238 | void VisitFunctionDecl(FunctionDecl *ND); |
| 239 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND); |
| 240 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 241 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
| 242 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| 243 | void VisitObjCIvarDecl(ObjCIvarDecl *ND); |
| 244 | void VisitObjCMethodDecl(ObjCMethodDecl *ND); |
| 245 | void VisitObjCPropertyDecl(ObjCPropertyDecl *ND); |
| 246 | void VisitObjCProtocolDecl(ObjCProtocolDecl *PID); |
| 247 | void VisitParmVarDecl(ParmVarDecl *ND); |
| 248 | void VisitTagDecl(TagDecl *D); |
| 249 | void VisitVarDecl(VarDecl *ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 250 | }; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 251 | } // end anonymous namespace |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 252 | |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 253 | void CDeclVisitor::VisitDeclContext(DeclContext *DC) { |
| 254 | for (DeclContext::decl_iterator |
| 255 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 256 | Visit(*I); |
| 257 | } |
| 258 | |
| 259 | void CDeclVisitor::VisitEnumConstantDecl(EnumConstantDecl *ND) { |
| 260 | Call(CXCursor_EnumConstantDecl, ND); |
| 261 | } |
| 262 | |
| 263 | void CDeclVisitor::VisitFieldDecl(FieldDecl *ND) { |
| 264 | Call(CXCursor_FieldDecl, ND); |
| 265 | } |
| 266 | |
| 267 | void CDeclVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
| 268 | if (ND->isThisDeclarationADefinition()) { |
| 269 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
| 270 | #if 0 |
| 271 | // Not currently needed. |
| 272 | CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody()); |
| 273 | CRefVisitor RVisit(CDecl, Callback, CData); |
| 274 | RVisit.Visit(Body); |
| 275 | #endif |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | void CDeclVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 280 | // Issue callbacks for the containing class. |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 281 | Callback(CDecl, |
| 282 | MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation()), |
| 283 | CData); |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 284 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 285 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 286 | E = ND->protocol_end(); I != E; ++I, ++PL) |
| 287 | Callback(CDecl, MakeCursorObjCProtocolRef(*I, *PL), CData); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 288 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
| 289 | } |
| 290 | |
| 291 | void CDeclVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 292 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 293 | } |
| 294 | |
| 295 | void CDeclVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 296 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 297 | } |
| 298 | |
| 299 | void CDeclVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 300 | // Issue callbacks for super class. |
| 301 | if (D->getSuperClass()) |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 302 | Callback(CDecl, |
| 303 | MakeCursorObjCSuperClassRef(D->getSuperClass(), |
| 304 | D->getSuperClassLoc()), |
| 305 | CData); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 306 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 307 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 308 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 309 | E = D->protocol_end(); I != E; ++I, ++PL) |
| 310 | Callback(CDecl, MakeCursorObjCProtocolRef(*I, *PL), CData); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 311 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 312 | } |
| 313 | |
| 314 | void CDeclVisitor::VisitObjCIvarDecl(ObjCIvarDecl *ND) { |
| 315 | Call(CXCursor_ObjCIvarDecl, ND); |
| 316 | } |
| 317 | |
| 318 | void CDeclVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
| 319 | if (ND->getBody()) { |
| 320 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn |
| 321 | : CXCursor_ObjCClassMethodDefn, ND); |
| 322 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
| 323 | } else |
| 324 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl |
| 325 | : CXCursor_ObjCClassMethodDecl, ND); |
| 326 | } |
| 327 | |
| 328 | void CDeclVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *ND) { |
| 329 | Call(CXCursor_ObjCPropertyDecl, ND); |
| 330 | } |
| 331 | |
| 332 | void CDeclVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 333 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 334 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 335 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 336 | Callback(CDecl, MakeCursorObjCProtocolRef(*I, *PL), CData); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 337 | |
| 338 | VisitDeclContext(dyn_cast<DeclContext>(PID)); |
| 339 | } |
| 340 | |
| 341 | void CDeclVisitor::VisitParmVarDecl(ParmVarDecl *ND) { |
| 342 | Call(CXCursor_ParmDecl, ND); |
| 343 | } |
| 344 | |
| 345 | void CDeclVisitor::VisitTagDecl(TagDecl *D) { |
| 346 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 347 | } |
| 348 | |
| 349 | void CDeclVisitor::VisitVarDecl(VarDecl *ND) { |
| 350 | Call(CXCursor_VarDecl, ND); |
| 351 | } |
| 352 | |
Daniel Dunbar | 140fce2 | 2010-01-12 02:34:07 +0000 | [diff] [blame] | 353 | CXString CIndexer::createCXString(const char *String, bool DupString){ |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 354 | CXString Str; |
| 355 | if (DupString) { |
| 356 | Str.Spelling = strdup(String); |
| 357 | Str.MustFreeString = 1; |
| 358 | } else { |
| 359 | Str.Spelling = String; |
| 360 | Str.MustFreeString = 0; |
| 361 | } |
| 362 | return Str; |
| 363 | } |
| 364 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 365 | extern "C" { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 366 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 367 | int displayDiagnostics) { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 368 | CIndexer *CIdxr = new CIndexer(new Program()); |
| 369 | if (excludeDeclarationsFromPCH) |
| 370 | CIdxr->setOnlyLocalDecls(); |
| 371 | if (displayDiagnostics) |
| 372 | CIdxr->setDisplayDiagnostics(); |
| 373 | return CIdxr; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 376 | void clang_disposeIndex(CXIndex CIdx) { |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 377 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 378 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 381 | void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) { |
| 382 | assert(CIdx && "Passed null CXIndex"); |
| 383 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 384 | CXXIdx->setUseExternalASTGeneration(value); |
| 385 | } |
| 386 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 387 | // FIXME: need to pass back error info. |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 388 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
| 389 | const char *ast_filename) { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 390 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 391 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 392 | |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 393 | return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(), |
| 394 | CXXIdx->getOnlyLocalDecls(), |
| 395 | /* UseBumpAllocator = */ true); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 398 | CXTranslationUnit |
| 399 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 400 | const char *source_filename, |
| 401 | int num_command_line_args, |
| 402 | const char **command_line_args) { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 403 | assert(CIdx && "Passed null CXIndex"); |
| 404 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 405 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 406 | if (!CXXIdx->getUseExternalASTGeneration()) { |
| 407 | llvm::SmallVector<const char *, 16> Args; |
| 408 | |
| 409 | // The 'source_filename' argument is optional. If the caller does not |
| 410 | // specify it then it is assumed that the source file is specified |
| 411 | // in the actual argument list. |
| 412 | if (source_filename) |
| 413 | Args.push_back(source_filename); |
| 414 | Args.insert(Args.end(), command_line_args, |
| 415 | command_line_args + num_command_line_args); |
| 416 | |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 417 | unsigned NumErrors = CXXIdx->getDiags().getNumErrors(); |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 418 | |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 419 | #ifdef USE_CRASHTRACER |
| 420 | ArgsCrashTracerInfo ACTI(Args); |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 421 | #endif |
| 422 | |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 423 | llvm::OwningPtr<ASTUnit> Unit( |
| 424 | ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(), |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 425 | CXXIdx->getDiags(), |
| 426 | CXXIdx->getClangResourcesPath(), |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 427 | CXXIdx->getOnlyLocalDecls(), |
| 428 | /* UseBumpAllocator = */ true)); |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 429 | |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 430 | // FIXME: Until we have broader testing, just drop the entire AST if we |
| 431 | // encountered an error. |
| 432 | if (NumErrors != CXXIdx->getDiags().getNumErrors()) |
| 433 | return 0; |
| 434 | |
| 435 | return Unit.take(); |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 438 | // Build up the arguments for invoking 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 439 | std::vector<const char *> argv; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 440 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 441 | // First add the complete path to the 'clang' executable. |
| 442 | llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath(); |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 443 | argv.push_back(ClangPath.c_str()); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 444 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 445 | // Add the '-emit-ast' option as our execution mode for 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 446 | argv.push_back("-emit-ast"); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 447 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 448 | // The 'source_filename' argument is optional. If the caller does not |
| 449 | // specify it then it is assumed that the source file is specified |
| 450 | // in the actual argument list. |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 451 | if (source_filename) |
| 452 | argv.push_back(source_filename); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 453 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 454 | // Generate a temporary name for the AST file. |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 455 | argv.push_back("-o"); |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 456 | char astTmpFile[L_tmpnam]; |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 457 | argv.push_back(tmpnam(astTmpFile)); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 458 | |
| 459 | // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. |
| 460 | for (int i = 0; i < num_command_line_args; ++i) |
| 461 | if (const char *arg = command_line_args[i]) { |
| 462 | if (strcmp(arg, "-o") == 0) { |
| 463 | ++i; // Also skip the matching argument. |
| 464 | continue; |
| 465 | } |
| 466 | if (strcmp(arg, "-emit-ast") == 0 || |
| 467 | strcmp(arg, "-c") == 0 || |
| 468 | strcmp(arg, "-fsyntax-only") == 0) { |
| 469 | continue; |
| 470 | } |
| 471 | |
| 472 | // Keep the argument. |
| 473 | argv.push_back(arg); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 474 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 475 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 476 | // Add the null terminator. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 477 | argv.push_back(NULL); |
| 478 | |
Ted Kremenek | feb15e3 | 2009-10-26 22:14:08 +0000 | [diff] [blame] | 479 | // Invoke 'clang'. |
| 480 | llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null |
| 481 | // on Unix or NUL (Windows). |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 482 | std::string ErrMsg; |
Ted Kremenek | c46e463 | 2009-10-19 22:27:32 +0000 | [diff] [blame] | 483 | const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL }; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 484 | llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, |
| 485 | /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL, |
| 486 | /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 487 | |
Ted Kremenek | 0854d70 | 2009-11-10 19:18:52 +0000 | [diff] [blame] | 488 | if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) { |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 489 | llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 490 | << '\n' << "Arguments: \n"; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 491 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
Ted Kremenek | 779e5f4 | 2009-10-26 22:08:39 +0000 | [diff] [blame] | 492 | I!=E; ++I) { |
| 493 | if (*I) |
| 494 | llvm::errs() << ' ' << *I << '\n'; |
| 495 | } |
| 496 | llvm::errs() << '\n'; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 497 | } |
Benjamin Kramer | 0829a83 | 2009-10-18 11:19:36 +0000 | [diff] [blame] | 498 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 499 | // Finally, we create the translation unit from the ast file. |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 500 | ASTUnit *ATU = static_cast<ASTUnit *>( |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 501 | clang_createTranslationUnit(CIdx, astTmpFile)); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 502 | if (ATU) |
| 503 | ATU->unlinkTemporaryFile(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 504 | return ATU; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 507 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 508 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 509 | delete static_cast<ASTUnit *>(CTUnit); |
| 510 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 511 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 512 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 513 | assert(CTUnit && "Passed null CXTranslationUnit"); |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 514 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 515 | return CIndexer::createCXString(CXXUnit->getOriginalSourceFileName().c_str(), |
| 516 | true); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 517 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 518 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 519 | void clang_loadTranslationUnit(CXTranslationUnit CTUnit, |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 520 | CXTranslationUnitIterator callback, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 521 | CXClientData CData) { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 522 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 523 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 524 | ASTContext &Ctx = CXXUnit->getASTContext(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 525 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 526 | unsigned PCHLevel = Decl::MaxPCHLevel; |
| 527 | |
| 528 | // Set the PCHLevel to filter out unwanted decls if requested. |
| 529 | if (CXXUnit->getOnlyLocalDecls()) { |
| 530 | PCHLevel = 0; |
| 531 | |
| 532 | // If the main input was an AST, bump the level. |
| 533 | if (CXXUnit->isMainFileAST()) |
| 534 | ++PCHLevel; |
| 535 | } |
| 536 | |
| 537 | TUVisitor DVisit(CTUnit, callback, CData, PCHLevel); |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 538 | |
| 539 | // If using a non-AST based ASTUnit, iterate over the stored list of top-level |
| 540 | // decls. |
| 541 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) { |
| 542 | const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls(); |
| 543 | for (std::vector<Decl*>::const_iterator it = TLDs.begin(), |
| 544 | ie = TLDs.end(); it != ie; ++it) { |
| 545 | DVisit.Visit(*it); |
| 546 | } |
| 547 | } else |
| 548 | DVisit.Visit(Ctx.getTranslationUnitDecl()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 551 | void clang_loadDeclaration(CXDecl Dcl, |
| 552 | CXDeclIterator callback, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 553 | CXClientData CData) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 554 | assert(Dcl && "Passed null CXDecl"); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 555 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 556 | CDeclVisitor DVisit(Dcl, callback, CData, |
| 557 | static_cast<Decl *>(Dcl)->getPCHLevel()); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 558 | DVisit.Visit(static_cast<Decl *>(Dcl)); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 559 | } |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 560 | } // end: extern "C" |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 561 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 562 | //===----------------------------------------------------------------------===// |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 563 | // CXDecl Operations. |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 564 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 565 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 566 | static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr, |
| 567 | SourceLocation SLoc) { |
| 568 | FileID FID; |
| 569 | if (SLoc.isFileID()) |
| 570 | FID = SMgr.getFileID(SLoc); |
| 571 | else |
| 572 | FID = SMgr.getDecomposedSpellingLoc(SLoc).first; |
| 573 | return SMgr.getFileEntryForID(FID); |
| 574 | } |
| 575 | |
| 576 | extern "C" { |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 577 | CXString clang_getDeclSpelling(CXDecl AnonDecl) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 578 | assert(AnonDecl && "Passed null CXDecl"); |
| 579 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 580 | |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 581 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 582 | return CIndexer::createCXString(OMD->getSelector().getAsString().c_str(), |
| 583 | true); |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 584 | |
| 585 | if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
Steve Naroff | 0d69b8c | 2009-10-29 21:11:04 +0000 | [diff] [blame] | 586 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 587 | // and returns different names. NamedDecl returns the class name and |
| 588 | // ObjCCategoryImplDecl returns the category name. |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 589 | return CIndexer::createCXString(CIMP->getIdentifier()->getNameStart()); |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 590 | |
| 591 | if (ND->getIdentifier()) |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 592 | return CIndexer::createCXString(ND->getIdentifier()->getNameStart()); |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 593 | |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 594 | return CIndexer::createCXString(""); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 595 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 596 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 597 | unsigned clang_getDeclLine(CXDecl AnonDecl) { |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 598 | assert(AnonDecl && "Passed null CXDecl"); |
| 599 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 600 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
| 601 | return SourceMgr.getSpellingLineNumber(ND->getLocation()); |
| 602 | } |
| 603 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 604 | unsigned clang_getDeclColumn(CXDecl AnonDecl) { |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 605 | assert(AnonDecl && "Passed null CXDecl"); |
| 606 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 607 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 7416524 | 2009-09-25 22:15:54 +0000 | [diff] [blame] | 608 | return SourceMgr.getSpellingColumnNumber(ND->getLocation()); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 609 | } |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 610 | |
Douglas Gregor | 3c7313d | 2010-01-18 22:13:09 +0000 | [diff] [blame] | 611 | CXSourceRange clang_getDeclExtent(CXDecl AnonDecl) { |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 612 | assert(AnonDecl && "Passed null CXDecl"); |
| 613 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
Ted Kremenek | d821065 | 2010-01-06 23:43:31 +0000 | [diff] [blame] | 614 | SourceManager &SM = ND->getASTContext().getSourceManager(); |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 615 | SourceRange R = ND->getSourceRange(); |
| 616 | |
Ted Kremenek | d821065 | 2010-01-06 23:43:31 +0000 | [diff] [blame] | 617 | SourceLocation Begin = SM.getInstantiationLoc(R.getBegin()); |
| 618 | SourceLocation End = SM.getInstantiationLoc(R.getEnd()); |
| 619 | |
| 620 | if (!Begin.isValid()) { |
Douglas Gregor | 3c7313d | 2010-01-18 22:13:09 +0000 | [diff] [blame] | 621 | CXSourceRange extent = { { 0, 0, 0 }, { 0, 0, 0 } }; |
Ted Kremenek | d821065 | 2010-01-06 23:43:31 +0000 | [diff] [blame] | 622 | return extent; |
| 623 | } |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 624 | |
Ted Kremenek | d821065 | 2010-01-06 23:43:31 +0000 | [diff] [blame] | 625 | // FIXME: This is largely copy-paste from |
| 626 | ///TextDiagnosticPrinter::HighlightRange. When it is clear that this is |
| 627 | // what we want the two routines should be refactored. |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 628 | |
Ted Kremenek | d821065 | 2010-01-06 23:43:31 +0000 | [diff] [blame] | 629 | // If the End location and the start location are the same and are a macro |
| 630 | // location, then the range was something that came from a macro expansion |
| 631 | // or _Pragma. If this is an object-like macro, the best we can do is to |
| 632 | // get the range. If this is a function-like macro, we'd also like to |
| 633 | // get the arguments. |
| 634 | if (Begin == End && R.getEnd().isMacroID()) |
| 635 | End = SM.getInstantiationRange(R.getEnd()).second; |
| 636 | |
| 637 | assert(SM.getFileID(Begin) == SM.getFileID(End)); |
| 638 | unsigned StartLineNo = SM.getInstantiationLineNumber(Begin); |
| 639 | unsigned EndLineNo = SM.getInstantiationLineNumber(End); |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 640 | |
Ted Kremenek | d821065 | 2010-01-06 23:43:31 +0000 | [diff] [blame] | 641 | // Compute the column number of the start. Keep the column based at 1. |
| 642 | unsigned StartColNo = SM.getInstantiationColumnNumber(Begin); |
| 643 | |
| 644 | // Compute the column number of the end. |
| 645 | unsigned EndColNo = SM.getInstantiationColumnNumber(End); |
| 646 | if (EndColNo) { |
| 647 | // Offset the end column by 1 so that we point to the last character |
| 648 | // in the last token. |
| 649 | --EndColNo; |
| 650 | |
| 651 | // Add in the length of the token, so that we cover multi-char tokens. |
| 652 | ASTContext &Ctx = ND->getTranslationUnitDecl()->getASTContext(); |
| 653 | const LangOptions &LOpts = Ctx.getLangOptions(); |
| 654 | |
| 655 | EndColNo += Lexer::MeasureTokenLength(End, SM, LOpts); |
| 656 | } |
| 657 | |
| 658 | // Package up the line/column data and return to the caller. |
Douglas Gregor | 3c7313d | 2010-01-18 22:13:09 +0000 | [diff] [blame] | 659 | const FileEntry *FEntry = SM.getFileEntryForID(SM.getFileID(Begin)); |
| 660 | CXSourceRange extent = { { (void *)FEntry, StartLineNo, StartColNo }, |
| 661 | { (void *)FEntry, EndLineNo, EndColNo } }; |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 662 | return extent; |
| 663 | } |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 664 | |
Ted Kremenek | 6ab9db1 | 2010-01-08 17:11:32 +0000 | [diff] [blame] | 665 | const char *clang_getDeclSource(CXDecl AnonDecl) { |
| 666 | assert(AnonDecl && "Passed null CXDecl"); |
| 667 | FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl)); |
| 668 | assert(FEnt && "Cannot find FileEntry for Decl"); |
| 669 | return clang_getFileName(FEnt); |
| 670 | } |
| 671 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 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 | } |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 679 | } // end: extern "C" |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 680 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 681 | //===----------------------------------------------------------------------===// |
| 682 | // CXFile Operations. |
| 683 | //===----------------------------------------------------------------------===// |
| 684 | |
| 685 | extern "C" { |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 686 | const char *clang_getFileName(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 687 | if (!SFile) |
| 688 | return 0; |
| 689 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 690 | assert(SFile && "Passed null CXFile"); |
| 691 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 692 | return FEnt->getName(); |
| 693 | } |
| 694 | |
| 695 | time_t clang_getFileTime(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 696 | if (!SFile) |
| 697 | return 0; |
| 698 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 699 | assert(SFile && "Passed null CXFile"); |
| 700 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 701 | return FEnt->getModificationTime(); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 702 | } |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 703 | } // end: extern "C" |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 704 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 705 | //===----------------------------------------------------------------------===// |
| 706 | // CXCursor Operations. |
| 707 | //===----------------------------------------------------------------------===// |
| 708 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 709 | static Decl *getDeclFromExpr(Stmt *E) { |
| 710 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 711 | return RefExpr->getDecl(); |
| 712 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 713 | return ME->getMemberDecl(); |
| 714 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 715 | return RE->getDecl(); |
| 716 | |
| 717 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 718 | return getDeclFromExpr(CE->getCallee()); |
| 719 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) |
| 720 | return getDeclFromExpr(CE->getSubExpr()); |
| 721 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 722 | return OME->getMethodDecl(); |
| 723 | |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | extern "C" { |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 728 | CXString clang_getCursorSpelling(CXCursor C) { |
Daniel Dunbar | c81d718 | 2010-01-18 17:52:42 +0000 | [diff] [blame] | 729 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 730 | if (clang_isReference(C.kind)) { |
| 731 | switch (C.kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 732 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 733 | ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
| 734 | return CIndexer::createCXString(Super->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 735 | } |
| 736 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 737 | ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
| 738 | return CIndexer::createCXString(Class->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 739 | } |
| 740 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 741 | ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame^] | 742 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 743 | return CIndexer::createCXString(OID->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 744 | } |
| 745 | case CXCursor_ObjCSelectorRef: { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 746 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(getCursorStmt(C)); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame^] | 747 | assert(OME && "getCursorSpelling(): Missing message expr"); |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 748 | return CIndexer::createCXString(OME->getSelector().getAsString().c_str(), |
| 749 | true); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 750 | } |
| 751 | case CXCursor_VarRef: |
| 752 | case CXCursor_FunctionRef: |
| 753 | case CXCursor_EnumConstantRef: { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 754 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(getCursorStmt(C)); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame^] | 755 | assert(DRE && "getCursorSpelling(): Missing decl ref expr"); |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 756 | return CIndexer::createCXString(DRE->getDecl()->getIdentifier() |
| 757 | ->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 758 | } |
| 759 | default: |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 760 | return CIndexer::createCXString("<not implemented>"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 761 | } |
| 762 | } |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 763 | return clang_getDeclSpelling(getCursorDecl(C)); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 766 | const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 767 | switch (Kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 768 | case CXCursor_FunctionDecl: return "FunctionDecl"; |
| 769 | case CXCursor_TypedefDecl: return "TypedefDecl"; |
| 770 | case CXCursor_EnumDecl: return "EnumDecl"; |
| 771 | case CXCursor_EnumConstantDecl: return "EnumConstantDecl"; |
| 772 | case CXCursor_StructDecl: return "StructDecl"; |
| 773 | case CXCursor_UnionDecl: return "UnionDecl"; |
| 774 | case CXCursor_ClassDecl: return "ClassDecl"; |
| 775 | case CXCursor_FieldDecl: return "FieldDecl"; |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 776 | case CXCursor_FunctionDefn: return "FunctionDefn"; |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 777 | case CXCursor_VarDecl: return "VarDecl"; |
| 778 | case CXCursor_ParmDecl: return "ParmDecl"; |
| 779 | case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl"; |
| 780 | case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl"; |
| 781 | case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl"; |
| 782 | case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl"; |
| 783 | case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl"; |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 784 | case CXCursor_ObjCIvarRef: return "ObjCIvarRef"; |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 785 | case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl"; |
| 786 | case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl"; |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 787 | case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn"; |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 788 | case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn"; |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 789 | case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn"; |
| 790 | case CXCursor_ObjCClassDefn: return "ObjCClassDefn"; |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 791 | case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; |
| 792 | case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef"; |
| 793 | case CXCursor_ObjCClassRef: return "ObjCClassRef"; |
| 794 | case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef"; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 795 | |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 796 | case CXCursor_VarRef: return "VarRef"; |
| 797 | case CXCursor_FunctionRef: return "FunctionRef"; |
| 798 | case CXCursor_EnumConstantRef: return "EnumConstantRef"; |
| 799 | case CXCursor_MemberRef: return "MemberRef"; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 800 | |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 801 | case CXCursor_InvalidFile: return "InvalidFile"; |
| 802 | case CXCursor_NoDeclFound: return "NoDeclFound"; |
| 803 | case CXCursor_NotImplemented: return "NotImplemented"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 804 | } |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 805 | |
| 806 | llvm_unreachable("Unhandled CXCursorKind"); |
| 807 | return NULL; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 808 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 809 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 810 | CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 811 | unsigned line, unsigned column) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 812 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 813 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 814 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 815 | FileManager &FMgr = CXXUnit->getFileManager(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 816 | const FileEntry *File = FMgr.getFile(source_name, |
| 817 | source_name+strlen(source_name)); |
Ted Kremenek | f462989 | 2010-01-14 01:51:23 +0000 | [diff] [blame] | 818 | if (!File) |
| 819 | return clang_getNullCursor(); |
| 820 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 821 | SourceLocation SLoc = |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 822 | CXXUnit->getSourceManager().getLocation(File, line, column); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 823 | |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 824 | ASTLocation LastLoc = CXXUnit->getLastASTLocation(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 825 | ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc, |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 826 | &LastLoc); |
Ted Kremenek | f462989 | 2010-01-14 01:51:23 +0000 | [diff] [blame] | 827 | |
| 828 | // FIXME: This doesn't look thread-safe. |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 829 | if (ALoc.isValid()) |
| 830 | CXXUnit->setLastASTLocation(ALoc); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 831 | |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 832 | Decl *Dcl = ALoc.getParentDecl(); |
Argyrios Kyrtzidis | 05a7651 | 2009-09-29 19:45:58 +0000 | [diff] [blame] | 833 | if (ALoc.isNamedRef()) |
| 834 | Dcl = ALoc.AsNamedRef().ND; |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 835 | Stmt *Stm = ALoc.dyn_AsStmt(); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 836 | if (Dcl) { |
| 837 | if (Stm) { |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 838 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame^] | 839 | return MakeCXCursor(TranslateDeclRefExpr(DRE), Dcl, Stm, |
| 840 | CXXUnit->getASTContext()); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 841 | else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame^] | 842 | return MakeCXCursor(CXCursor_ObjCSelectorRef, Dcl, MExp, |
| 843 | CXXUnit->getASTContext()); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 844 | // Fall through...treat as a decl, not a ref. |
| 845 | } |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 846 | if (ALoc.isNamedRef()) { |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 847 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(Dcl)) |
| 848 | return MakeCursorObjCClassRef(Class, ALoc.AsNamedRef().Loc); |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 849 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(Dcl)) |
| 850 | return MakeCursorObjCProtocolRef(Proto, ALoc.AsNamedRef().Loc); |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 851 | } |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 852 | return MakeCXCursor(Dcl); |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 853 | } |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 854 | return MakeCXCursor(CXCursor_NoDeclFound, 0); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 857 | CXCursor clang_getNullCursor(void) { |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 858 | return MakeCXCursor(CXCursor_InvalidFile, 0); |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 862 | return X == Y; |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 863 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 864 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 865 | CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 866 | assert(AnonDecl && "Passed null CXDecl"); |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 867 | return MakeCXCursor(static_cast<NamedDecl *>(AnonDecl)); |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 870 | unsigned clang_isInvalid(enum CXCursorKind K) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 871 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 872 | } |
| 873 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 874 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 875 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 876 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 877 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 878 | unsigned clang_isReference(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 879 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 880 | } |
| 881 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 882 | unsigned clang_isDefinition(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 883 | return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn; |
| 884 | } |
| 885 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 886 | CXCursorKind clang_getCursorKind(CXCursor C) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 887 | return C.kind; |
| 888 | } |
| 889 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 890 | CXDecl clang_getCursorDecl(CXCursor C) { |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 891 | if (clang_isDeclaration(C.kind) || clang_isDefinition(C.kind)) |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 892 | return getCursorDecl(C); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 893 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 894 | if (clang_isReference(C.kind)) { |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 895 | if (getCursorStmt(C)) |
| 896 | return getDeclFromExpr(getCursorStmt(C)); |
| 897 | |
| 898 | return getCursorDecl(C); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 899 | } |
| 900 | return 0; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 903 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 904 | if (clang_isReference(C.kind)) { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame^] | 905 | switch (C.kind) { |
| 906 | case CXCursor_ObjCSuperClassRef: { |
| 907 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 908 | = getCursorObjCSuperClassRef(C); |
| 909 | SourceManager &SM = P.first->getASTContext().getSourceManager(); |
| 910 | return translateSourceLocation(SM, P.second); |
| 911 | } |
| 912 | |
| 913 | case CXCursor_ObjCProtocolRef: { |
| 914 | std::pair<ObjCProtocolDecl *, SourceLocation> P |
| 915 | = getCursorObjCProtocolRef(C); |
| 916 | SourceManager &SM = P.first->getASTContext().getSourceManager(); |
| 917 | return translateSourceLocation(SM, P.second); |
| 918 | } |
| 919 | |
| 920 | case CXCursor_ObjCClassRef: { |
| 921 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 922 | = getCursorObjCClassRef(C); |
| 923 | SourceManager &SM = P.first->getASTContext().getSourceManager(); |
| 924 | return translateSourceLocation(SM, P.second); |
| 925 | } |
| 926 | |
| 927 | case CXCursor_ObjCSelectorRef: |
| 928 | case CXCursor_ObjCIvarRef: |
| 929 | case CXCursor_VarRef: |
| 930 | case CXCursor_FunctionRef: |
| 931 | case CXCursor_EnumConstantRef: |
| 932 | case CXCursor_MemberRef: { |
| 933 | Expr *E = getCursorExpr(C); |
| 934 | SourceManager &SM = getCursorContext(C).getSourceManager(); |
| 935 | if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
| 936 | return translateSourceLocation(SM, /*FIXME:*/Msg->getLeftLoc()); |
| 937 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 938 | return translateSourceLocation(SM, DRE->getLocation()); |
| 939 | if (MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
| 940 | return translateSourceLocation(SM, Member->getMemberLoc()); |
| 941 | if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
| 942 | return translateSourceLocation(SM, Ivar->getLocation()); |
| 943 | return translateSourceLocation(SM, E->getLocStart()); |
| 944 | } |
| 945 | |
| 946 | default: |
| 947 | // FIXME: Need a way to enumerate all non-reference cases. |
| 948 | llvm_unreachable("Missed a reference kind"); |
| 949 | } |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | if (!getCursorDecl(C)) { |
| 953 | CXSourceLocation empty = { 0, 0, 0 }; |
| 954 | return empty; |
| 955 | } |
| 956 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame^] | 957 | Decl *D = getCursorDecl(C); |
| 958 | SourceManager &SM = D->getASTContext().getSourceManager(); |
| 959 | SourceLocation Loc = D->getLocation(); |
| 960 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D)) |
| 961 | Loc = Class->getClassLoc(); |
| 962 | return translateSourceLocation(SM, Loc); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 965 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 966 | const char **startBuf, |
| 967 | const char **endBuf, |
| 968 | unsigned *startLine, |
| 969 | unsigned *startColumn, |
| 970 | unsigned *endLine, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 971 | unsigned *endColumn) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 972 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
| 973 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 974 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 975 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 976 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 977 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 978 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 979 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 980 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 981 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 982 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 983 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 984 | } |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 985 | |
| 986 | } // end: extern "C" |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 987 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 988 | //===----------------------------------------------------------------------===// |
| 989 | // CXString Operations. |
| 990 | //===----------------------------------------------------------------------===// |
| 991 | |
| 992 | extern "C" { |
| 993 | const char *clang_getCString(CXString string) { |
| 994 | return string.Spelling; |
| 995 | } |
| 996 | |
| 997 | void clang_disposeString(CXString string) { |
| 998 | if (string.MustFreeString && string.Spelling) |
| 999 | free((void*)string.Spelling); |
| 1000 | } |
| 1001 | } // end: extern "C" |