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 | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 116 | typedef llvm::PointerIntPair<ASTContext *, 1, bool> CXSourceLocationPtr; |
| 117 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 118 | /// \brief Translate a Clang source location into a CIndex source location. |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 119 | static CXSourceLocation translateSourceLocation(ASTContext &Context, |
| 120 | SourceLocation Loc, |
| 121 | bool AtEnd = false) { |
| 122 | CXSourceLocationPtr Ptr(&Context, AtEnd); |
| 123 | CXSourceLocation Result = { Ptr.getOpaqueValue(), Loc.getRawEncoding() }; |
| 124 | return Result; |
| 125 | |
| 126 | #if 0 |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 127 | SourceLocation InstLoc = SourceMgr.getInstantiationLoc(Loc); |
| 128 | if (InstLoc.isInvalid()) { |
| 129 | CXSourceLocation Loc = { 0, 0, 0 }; |
| 130 | return Loc; |
| 131 | } |
| 132 | |
| 133 | CXSourceLocation Result; |
| 134 | Result.file |
| 135 | = (void*)SourceMgr.getFileEntryForID(SourceMgr.getFileID(InstLoc)); |
| 136 | Result.line = SourceMgr.getInstantiationLineNumber(InstLoc); |
| 137 | Result.column = SourceMgr.getInstantiationColumnNumber(InstLoc); |
| 138 | return Result; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 139 | #endif |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 142 | /// \brief Translate a Clang source range into a CIndex source range. |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 143 | static CXSourceRange translateSourceRange(ASTContext &Context, SourceRange R) { |
| 144 | CXSourceRange Result = { &Context, |
| 145 | R.getBegin().getRawEncoding(), |
| 146 | R.getEnd().getRawEncoding() }; |
| 147 | return Result; |
| 148 | #if 0 |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 149 | if (R.isInvalid()) { |
| 150 | CXSourceRange extent = { { 0, 0, 0 }, { 0, 0, 0 } }; |
| 151 | return extent; |
| 152 | } |
| 153 | |
| 154 | // FIXME: This is largely copy-paste from |
| 155 | ///TextDiagnosticPrinter::HighlightRange. When it is clear that this is |
| 156 | // what we want the two routines should be refactored. |
| 157 | |
| 158 | SourceManager &SM = Context.getSourceManager(); |
| 159 | SourceLocation Begin = SM.getInstantiationLoc(R.getBegin()); |
| 160 | SourceLocation End = SM.getInstantiationLoc(R.getEnd()); |
| 161 | |
| 162 | // If the End location and the start location are the same and are a macro |
| 163 | // location, then the range was something that came from a macro expansion |
| 164 | // or _Pragma. If this is an object-like macro, the best we can do is to |
| 165 | // get the range. If this is a function-like macro, we'd also like to |
| 166 | // get the arguments. |
| 167 | if (Begin == End && R.getEnd().isMacroID()) |
| 168 | End = SM.getInstantiationRange(R.getEnd()).second; |
| 169 | |
| 170 | unsigned StartLineNo = SM.getInstantiationLineNumber(Begin); |
| 171 | unsigned EndLineNo = SM.getInstantiationLineNumber(End); |
| 172 | |
| 173 | // Compute the column number of the start. Keep the column based at 1. |
| 174 | unsigned StartColNo = SM.getInstantiationColumnNumber(Begin); |
| 175 | |
| 176 | // Compute the column number of the end. |
| 177 | unsigned EndColNo = SM.getInstantiationColumnNumber(End); |
| 178 | if (EndColNo) { |
| 179 | // Offset the end column by 1 so that we point to the last character |
| 180 | // in the last token. |
| 181 | --EndColNo; |
| 182 | |
| 183 | // Add in the length of the token, so that we cover multi-char tokens. |
| 184 | EndColNo += Lexer::MeasureTokenLength(End, SM, Context.getLangOptions()); |
| 185 | } |
| 186 | |
| 187 | // Package up the line/column data and return to the caller. |
| 188 | const FileEntry *BeginFile = SM.getFileEntryForID(SM.getFileID(Begin)); |
| 189 | const FileEntry *EndFile = SM.getFileEntryForID(SM.getFileID(End)); |
| 190 | CXSourceRange extent = { { (void *)BeginFile, StartLineNo, StartColNo }, |
| 191 | { (void *)EndFile, EndLineNo, EndColNo } }; |
| 192 | return extent; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 193 | #endif |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 196 | |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 197 | //===----------------------------------------------------------------------===// |
| 198 | // Visitors. |
| 199 | //===----------------------------------------------------------------------===// |
| 200 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 201 | namespace { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 202 | // Translation Unit Visitor. |
| 203 | class TUVisitor : public DeclVisitor<TUVisitor> { |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 204 | public: |
| 205 | typedef void (*Iterator)(void *, CXCursor, CXClientData); |
| 206 | private: |
| 207 | void *Root; // CXDecl or CXTranslationUnit |
| 208 | Iterator Callback; // CXTranslationUnitIterator or CXDeclIterator. |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 209 | CXClientData CData; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 210 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 211 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 212 | // to the visitor. Declarations with a PCH level greater than this value will |
| 213 | // be suppressed. |
| 214 | unsigned MaxPCHLevel; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 215 | |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 216 | void Call(const CXCursor &C) { |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 217 | if (clang_isInvalid(C.kind)) |
| 218 | return; |
| 219 | |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 220 | if (const Decl *D = getCursorDecl(C)) { |
| 221 | // Filter any declarations that have a PCH level greater than what |
| 222 | // we allow. |
| 223 | if (D->getPCHLevel() > MaxPCHLevel) |
| 224 | return; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 225 | |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 226 | // Filter any implicit declarations (since the source info will be bogus). |
| 227 | if (D->isImplicit()) |
| 228 | return; |
| 229 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 230 | |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 231 | Callback(Root, C, CData); |
Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 232 | } |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 233 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 234 | public: |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 235 | TUVisitor(void *root, Iterator cback, CXClientData D, unsigned MaxPCHLevel) : |
| 236 | Root(root), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {} |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 237 | |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 238 | void VisitDecl(Decl *D); |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 239 | void VisitDeclContext(DeclContext *DC); |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 240 | void VisitTranslationUnitDecl(TranslationUnitDecl *D); |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 241 | }; |
Ted Kremenek | ef7fdc6 | 2009-11-17 07:02:15 +0000 | [diff] [blame] | 242 | |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 243 | void TUVisitor::VisitDecl(Decl *D) { |
| 244 | Call(MakeCXCursor(D)); |
| 245 | } |
| 246 | |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 247 | void TUVisitor::VisitDeclContext(DeclContext *DC) { |
| 248 | for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 249 | I != E; ++I) |
| 250 | Visit(*I); |
| 251 | } |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 252 | |
Ted Kremenek | f128618 | 2010-01-13 00:13:47 +0000 | [diff] [blame] | 253 | void TUVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 254 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 255 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 256 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 257 | // Declaration visitor. |
| 258 | class CDeclVisitor : public DeclVisitor<CDeclVisitor> { |
| 259 | CXDecl CDecl; |
| 260 | CXDeclIterator Callback; |
| 261 | CXClientData CData; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 262 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 263 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 264 | // to the visitor. Declarations with a PCH level greater than this value will |
| 265 | // be suppressed. |
| 266 | unsigned MaxPCHLevel; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 267 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 268 | void Call(enum CXCursorKind CK, NamedDecl *ND) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 269 | // Disable the callback when the context is equal to the visiting decl. |
| 270 | if (CDecl == ND && !clang_isReference(CK)) |
| 271 | return; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 272 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 273 | // Filter any declarations that have a PCH level greater than what we allow. |
| 274 | if (ND->getPCHLevel() > MaxPCHLevel) |
| 275 | return; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 276 | |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 277 | CXCursor C = { CK, { ND, 0, 0 } }; |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 278 | Callback(CDecl, C, CData); |
| 279 | } |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 280 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 281 | public: |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 282 | CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D, |
| 283 | unsigned MaxPCHLevel) : |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 284 | CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {} |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 285 | |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 286 | void VisitDeclContext(DeclContext *DC); |
| 287 | void VisitEnumConstantDecl(EnumConstantDecl *ND); |
| 288 | void VisitFieldDecl(FieldDecl *ND); |
| 289 | void VisitFunctionDecl(FunctionDecl *ND); |
| 290 | void VisitObjCCategoryDecl(ObjCCategoryDecl *ND); |
| 291 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 292 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
| 293 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| 294 | void VisitObjCIvarDecl(ObjCIvarDecl *ND); |
| 295 | void VisitObjCMethodDecl(ObjCMethodDecl *ND); |
| 296 | void VisitObjCPropertyDecl(ObjCPropertyDecl *ND); |
| 297 | void VisitObjCProtocolDecl(ObjCProtocolDecl *PID); |
| 298 | void VisitParmVarDecl(ParmVarDecl *ND); |
| 299 | void VisitTagDecl(TagDecl *D); |
| 300 | void VisitVarDecl(VarDecl *ND); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 301 | }; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 302 | } // end anonymous namespace |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 303 | |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 304 | void CDeclVisitor::VisitDeclContext(DeclContext *DC) { |
| 305 | for (DeclContext::decl_iterator |
| 306 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) |
| 307 | Visit(*I); |
| 308 | } |
| 309 | |
| 310 | void CDeclVisitor::VisitEnumConstantDecl(EnumConstantDecl *ND) { |
| 311 | Call(CXCursor_EnumConstantDecl, ND); |
| 312 | } |
| 313 | |
| 314 | void CDeclVisitor::VisitFieldDecl(FieldDecl *ND) { |
| 315 | Call(CXCursor_FieldDecl, ND); |
| 316 | } |
| 317 | |
| 318 | void CDeclVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
| 319 | if (ND->isThisDeclarationADefinition()) { |
| 320 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
| 321 | #if 0 |
| 322 | // Not currently needed. |
| 323 | CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody()); |
| 324 | CRefVisitor RVisit(CDecl, Callback, CData); |
| 325 | RVisit.Visit(Body); |
| 326 | #endif |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | void CDeclVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 331 | // Issue callbacks for the containing class. |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 332 | Callback(CDecl, |
| 333 | MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation()), |
| 334 | CData); |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 335 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 336 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 337 | E = ND->protocol_end(); I != E; ++I, ++PL) |
| 338 | Callback(CDecl, MakeCursorObjCProtocolRef(*I, *PL), CData); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 339 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
| 340 | } |
| 341 | |
| 342 | void CDeclVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 343 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 344 | } |
| 345 | |
| 346 | void CDeclVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 347 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 348 | } |
| 349 | |
| 350 | void CDeclVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 351 | // Issue callbacks for super class. |
| 352 | if (D->getSuperClass()) |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 353 | Callback(CDecl, |
| 354 | MakeCursorObjCSuperClassRef(D->getSuperClass(), |
| 355 | D->getSuperClassLoc()), |
| 356 | CData); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 357 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 358 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 359 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 360 | E = D->protocol_end(); I != E; ++I, ++PL) |
| 361 | Callback(CDecl, MakeCursorObjCProtocolRef(*I, *PL), CData); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 362 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 363 | } |
| 364 | |
| 365 | void CDeclVisitor::VisitObjCIvarDecl(ObjCIvarDecl *ND) { |
| 366 | Call(CXCursor_ObjCIvarDecl, ND); |
| 367 | } |
| 368 | |
| 369 | void CDeclVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 370 | Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl |
| 371 | : CXCursor_ObjCClassMethodDecl, ND); |
| 372 | |
| 373 | if (ND->getBody()) |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 374 | VisitDeclContext(dyn_cast<DeclContext>(ND)); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | void CDeclVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *ND) { |
| 378 | Call(CXCursor_ObjCPropertyDecl, ND); |
| 379 | } |
| 380 | |
| 381 | void CDeclVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 382 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 383 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 384 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 385 | Callback(CDecl, MakeCursorObjCProtocolRef(*I, *PL), CData); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 386 | |
| 387 | VisitDeclContext(dyn_cast<DeclContext>(PID)); |
| 388 | } |
| 389 | |
| 390 | void CDeclVisitor::VisitParmVarDecl(ParmVarDecl *ND) { |
| 391 | Call(CXCursor_ParmDecl, ND); |
| 392 | } |
| 393 | |
| 394 | void CDeclVisitor::VisitTagDecl(TagDecl *D) { |
| 395 | VisitDeclContext(dyn_cast<DeclContext>(D)); |
| 396 | } |
| 397 | |
| 398 | void CDeclVisitor::VisitVarDecl(VarDecl *ND) { |
| 399 | Call(CXCursor_VarDecl, ND); |
| 400 | } |
| 401 | |
Daniel Dunbar | 140fce2 | 2010-01-12 02:34:07 +0000 | [diff] [blame] | 402 | CXString CIndexer::createCXString(const char *String, bool DupString){ |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 403 | CXString Str; |
| 404 | if (DupString) { |
| 405 | Str.Spelling = strdup(String); |
| 406 | Str.MustFreeString = 1; |
| 407 | } else { |
| 408 | Str.Spelling = String; |
| 409 | Str.MustFreeString = 0; |
| 410 | } |
| 411 | return Str; |
| 412 | } |
| 413 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 414 | extern "C" { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 415 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 416 | int displayDiagnostics) { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 417 | CIndexer *CIdxr = new CIndexer(new Program()); |
| 418 | if (excludeDeclarationsFromPCH) |
| 419 | CIdxr->setOnlyLocalDecls(); |
| 420 | if (displayDiagnostics) |
| 421 | CIdxr->setDisplayDiagnostics(); |
| 422 | return CIdxr; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 425 | void clang_disposeIndex(CXIndex CIdx) { |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 426 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 427 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 430 | void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) { |
| 431 | assert(CIdx && "Passed null CXIndex"); |
| 432 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 433 | CXXIdx->setUseExternalASTGeneration(value); |
| 434 | } |
| 435 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 436 | // FIXME: need to pass back error info. |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 437 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
| 438 | const char *ast_filename) { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 439 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 440 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 441 | |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 442 | return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(), |
| 443 | CXXIdx->getOnlyLocalDecls(), |
| 444 | /* UseBumpAllocator = */ true); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 447 | CXTranslationUnit |
| 448 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 449 | const char *source_filename, |
| 450 | int num_command_line_args, |
| 451 | const char **command_line_args) { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 452 | assert(CIdx && "Passed null CXIndex"); |
| 453 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 454 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 455 | if (!CXXIdx->getUseExternalASTGeneration()) { |
| 456 | llvm::SmallVector<const char *, 16> Args; |
| 457 | |
| 458 | // The 'source_filename' argument is optional. If the caller does not |
| 459 | // specify it then it is assumed that the source file is specified |
| 460 | // in the actual argument list. |
| 461 | if (source_filename) |
| 462 | Args.push_back(source_filename); |
| 463 | Args.insert(Args.end(), command_line_args, |
| 464 | command_line_args + num_command_line_args); |
| 465 | |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 466 | unsigned NumErrors = CXXIdx->getDiags().getNumErrors(); |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 467 | |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 468 | #ifdef USE_CRASHTRACER |
| 469 | ArgsCrashTracerInfo ACTI(Args); |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 470 | #endif |
| 471 | |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 472 | llvm::OwningPtr<ASTUnit> Unit( |
| 473 | ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(), |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 474 | CXXIdx->getDiags(), |
| 475 | CXXIdx->getClangResourcesPath(), |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 476 | CXXIdx->getOnlyLocalDecls(), |
| 477 | /* UseBumpAllocator = */ true)); |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 478 | |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 479 | // FIXME: Until we have broader testing, just drop the entire AST if we |
| 480 | // encountered an error. |
| 481 | if (NumErrors != CXXIdx->getDiags().getNumErrors()) |
| 482 | return 0; |
| 483 | |
| 484 | return Unit.take(); |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 487 | // Build up the arguments for invoking 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 488 | std::vector<const char *> argv; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 489 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 490 | // First add the complete path to the 'clang' executable. |
| 491 | llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath(); |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 492 | argv.push_back(ClangPath.c_str()); |
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 | // Add the '-emit-ast' option as our execution mode for 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 495 | argv.push_back("-emit-ast"); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 496 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 497 | // The 'source_filename' argument is optional. If the caller does not |
| 498 | // specify it then it is assumed that the source file is specified |
| 499 | // in the actual argument list. |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 500 | if (source_filename) |
| 501 | argv.push_back(source_filename); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 502 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 503 | // Generate a temporary name for the AST file. |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 504 | argv.push_back("-o"); |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 505 | char astTmpFile[L_tmpnam]; |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 506 | argv.push_back(tmpnam(astTmpFile)); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 507 | |
| 508 | // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. |
| 509 | for (int i = 0; i < num_command_line_args; ++i) |
| 510 | if (const char *arg = command_line_args[i]) { |
| 511 | if (strcmp(arg, "-o") == 0) { |
| 512 | ++i; // Also skip the matching argument. |
| 513 | continue; |
| 514 | } |
| 515 | if (strcmp(arg, "-emit-ast") == 0 || |
| 516 | strcmp(arg, "-c") == 0 || |
| 517 | strcmp(arg, "-fsyntax-only") == 0) { |
| 518 | continue; |
| 519 | } |
| 520 | |
| 521 | // Keep the argument. |
| 522 | argv.push_back(arg); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 523 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 524 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 525 | // Add the null terminator. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 526 | argv.push_back(NULL); |
| 527 | |
Ted Kremenek | feb15e3 | 2009-10-26 22:14:08 +0000 | [diff] [blame] | 528 | // Invoke 'clang'. |
| 529 | llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null |
| 530 | // on Unix or NUL (Windows). |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 531 | std::string ErrMsg; |
Ted Kremenek | c46e463 | 2009-10-19 22:27:32 +0000 | [diff] [blame] | 532 | const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL }; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 533 | llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, |
| 534 | /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL, |
| 535 | /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 536 | |
Ted Kremenek | 0854d70 | 2009-11-10 19:18:52 +0000 | [diff] [blame] | 537 | if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) { |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 538 | llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 539 | << '\n' << "Arguments: \n"; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 540 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
Ted Kremenek | 779e5f4 | 2009-10-26 22:08:39 +0000 | [diff] [blame] | 541 | I!=E; ++I) { |
| 542 | if (*I) |
| 543 | llvm::errs() << ' ' << *I << '\n'; |
| 544 | } |
| 545 | llvm::errs() << '\n'; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 546 | } |
Benjamin Kramer | 0829a83 | 2009-10-18 11:19:36 +0000 | [diff] [blame] | 547 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 548 | // Finally, we create the translation unit from the ast file. |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 549 | ASTUnit *ATU = static_cast<ASTUnit *>( |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 550 | clang_createTranslationUnit(CIdx, astTmpFile)); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 551 | if (ATU) |
| 552 | ATU->unlinkTemporaryFile(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 553 | return ATU; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 556 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 557 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 558 | delete static_cast<ASTUnit *>(CTUnit); |
| 559 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 560 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 561 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 562 | assert(CTUnit && "Passed null CXTranslationUnit"); |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 563 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 564 | return CIndexer::createCXString(CXXUnit->getOriginalSourceFileName().c_str(), |
| 565 | true); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 566 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 567 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 568 | void clang_loadTranslationUnit(CXTranslationUnit CTUnit, |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 569 | CXTranslationUnitIterator callback, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 570 | CXClientData CData) { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 571 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 572 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
| 573 | ASTContext &Ctx = CXXUnit->getASTContext(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 574 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 575 | unsigned PCHLevel = Decl::MaxPCHLevel; |
| 576 | |
| 577 | // Set the PCHLevel to filter out unwanted decls if requested. |
| 578 | if (CXXUnit->getOnlyLocalDecls()) { |
| 579 | PCHLevel = 0; |
| 580 | |
| 581 | // If the main input was an AST, bump the level. |
| 582 | if (CXXUnit->isMainFileAST()) |
| 583 | ++PCHLevel; |
| 584 | } |
| 585 | |
| 586 | TUVisitor DVisit(CTUnit, callback, CData, PCHLevel); |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 587 | |
| 588 | // If using a non-AST based ASTUnit, iterate over the stored list of top-level |
| 589 | // decls. |
| 590 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) { |
| 591 | const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls(); |
| 592 | for (std::vector<Decl*>::const_iterator it = TLDs.begin(), |
| 593 | ie = TLDs.end(); it != ie; ++it) { |
| 594 | DVisit.Visit(*it); |
| 595 | } |
| 596 | } else |
| 597 | DVisit.Visit(Ctx.getTranslationUnitDecl()); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 600 | void clang_loadDeclaration(CXDecl Dcl, |
| 601 | CXDeclIterator callback, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 602 | CXClientData CData) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 603 | assert(Dcl && "Passed null CXDecl"); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 604 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 605 | CDeclVisitor DVisit(Dcl, callback, CData, |
| 606 | static_cast<Decl *>(Dcl)->getPCHLevel()); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 607 | DVisit.Visit(static_cast<Decl *>(Dcl)); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 608 | } |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 609 | } // end: extern "C" |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 610 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 611 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 612 | // CXSourceLocation and CXSourceRange Operations. |
| 613 | //===----------------------------------------------------------------------===// |
| 614 | |
| 615 | void clang_getInstantiationLocation(CXSourceLocation location, |
| 616 | CXFile *file, |
| 617 | unsigned *line, |
| 618 | unsigned *column) { |
| 619 | CXSourceLocationPtr Ptr |
| 620 | = CXSourceLocationPtr::getFromOpaqueValue(location.ptr_data); |
| 621 | SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); |
| 622 | |
| 623 | if (!Ptr.getPointer() || Loc.isInvalid()) { |
| 624 | if (file) |
| 625 | *file = 0; |
| 626 | if (line) |
| 627 | *line = 0; |
| 628 | if (column) |
| 629 | *column = 0; |
| 630 | return; |
| 631 | } |
| 632 | |
| 633 | // FIXME: This is largely copy-paste from |
| 634 | ///TextDiagnosticPrinter::HighlightRange. When it is clear that this is |
| 635 | // what we want the two routines should be refactored. |
| 636 | ASTContext &Context = *Ptr.getPointer(); |
| 637 | SourceManager &SM = Context.getSourceManager(); |
| 638 | SourceLocation InstLoc = SM.getInstantiationLoc(Loc); |
| 639 | |
| 640 | if (Ptr.getInt()) { |
| 641 | // We want the last character in this location, so we will adjust |
| 642 | // the instantiation location accordingly. |
| 643 | |
| 644 | // If the location is from a macro instantiation, get the end of |
| 645 | // the instantiation range. |
| 646 | if (Loc.isMacroID()) |
| 647 | InstLoc = SM.getInstantiationRange(Loc).second; |
| 648 | |
| 649 | // Measure the length token we're pointing at, so we can adjust |
| 650 | // the physical location in the file to point at the last |
| 651 | // character. |
| 652 | // FIXME: This won't cope with trigraphs or escaped newlines |
| 653 | // well. For that, we actually need a preprocessor, which isn't |
| 654 | // currently available here. Eventually, we'll switch the pointer |
| 655 | // data of CXSourceLocation/CXSourceRange to a translation unit |
| 656 | // (CXXUnit), so that the preprocessor will be available here. At |
| 657 | // that point, we can use Preprocessor::getLocForEndOfToken(). |
| 658 | unsigned Length = Lexer::MeasureTokenLength(InstLoc, SM, |
| 659 | Context.getLangOptions()); |
| 660 | if (Length > 0) |
| 661 | InstLoc = InstLoc.getFileLocWithOffset(Length - 1); |
| 662 | } |
| 663 | |
| 664 | if (file) |
| 665 | *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc)); |
| 666 | if (line) |
| 667 | *line = SM.getInstantiationLineNumber(InstLoc); |
| 668 | if (column) |
| 669 | *column = SM.getInstantiationColumnNumber(InstLoc); |
| 670 | } |
| 671 | |
| 672 | CXSourceLocation clang_getRangeStart(CXSourceRange range) { |
| 673 | CXSourceLocation Result = { range.ptr_data, range.begin_int_data }; |
| 674 | return Result; |
| 675 | } |
| 676 | |
| 677 | CXSourceLocation clang_getRangeEnd(CXSourceRange range) { |
| 678 | llvm::PointerIntPair<ASTContext *, 1, bool> Ptr; |
| 679 | Ptr.setPointer(static_cast<ASTContext *>(range.ptr_data)); |
| 680 | Ptr.setInt(true); |
| 681 | CXSourceLocation Result = { Ptr.getOpaqueValue(), range.end_int_data }; |
| 682 | return Result; |
| 683 | } |
| 684 | |
| 685 | //===----------------------------------------------------------------------===// |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 686 | // CXDecl Operations. |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 687 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 688 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 689 | static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr, |
| 690 | SourceLocation SLoc) { |
| 691 | FileID FID; |
| 692 | if (SLoc.isFileID()) |
| 693 | FID = SMgr.getFileID(SLoc); |
| 694 | else |
| 695 | FID = SMgr.getDecomposedSpellingLoc(SLoc).first; |
| 696 | return SMgr.getFileEntryForID(FID); |
| 697 | } |
| 698 | |
| 699 | extern "C" { |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 700 | CXString clang_getDeclSpelling(CXDecl AnonDecl) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 701 | assert(AnonDecl && "Passed null CXDecl"); |
Douglas Gregor | 3012213 | 2010-01-19 22:07:56 +0000 | [diff] [blame] | 702 | Decl *D = static_cast<Decl *>(AnonDecl); |
| 703 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 704 | if (!ND) |
| 705 | return CIndexer::createCXString(""); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 706 | |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 707 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 708 | return CIndexer::createCXString(OMD->getSelector().getAsString().c_str(), |
| 709 | true); |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 710 | |
| 711 | if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
Steve Naroff | 0d69b8c | 2009-10-29 21:11:04 +0000 | [diff] [blame] | 712 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 713 | // and returns different names. NamedDecl returns the class name and |
| 714 | // ObjCCategoryImplDecl returns the category name. |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 715 | return CIndexer::createCXString(CIMP->getIdentifier()->getNameStart()); |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 716 | |
| 717 | if (ND->getIdentifier()) |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 718 | return CIndexer::createCXString(ND->getIdentifier()->getNameStart()); |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 719 | |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 720 | return CIndexer::createCXString(""); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 721 | } |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 722 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 723 | unsigned clang_getDeclLine(CXDecl AnonDecl) { |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 724 | assert(AnonDecl && "Passed null CXDecl"); |
| 725 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 726 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
| 727 | return SourceMgr.getSpellingLineNumber(ND->getLocation()); |
| 728 | } |
| 729 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 730 | unsigned clang_getDeclColumn(CXDecl AnonDecl) { |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 731 | assert(AnonDecl && "Passed null CXDecl"); |
| 732 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 733 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 7416524 | 2009-09-25 22:15:54 +0000 | [diff] [blame] | 734 | return SourceMgr.getSpellingColumnNumber(ND->getLocation()); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 735 | } |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 736 | |
Douglas Gregor | 3c7313d | 2010-01-18 22:13:09 +0000 | [diff] [blame] | 737 | CXSourceRange clang_getDeclExtent(CXDecl AnonDecl) { |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 738 | return clang_getCursorExtent(clang_getCursorFromDecl(AnonDecl)); |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 739 | } |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 740 | |
Ted Kremenek | 6ab9db1 | 2010-01-08 17:11:32 +0000 | [diff] [blame] | 741 | const char *clang_getDeclSource(CXDecl AnonDecl) { |
| 742 | assert(AnonDecl && "Passed null CXDecl"); |
| 743 | FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl)); |
| 744 | assert(FEnt && "Cannot find FileEntry for Decl"); |
| 745 | return clang_getFileName(FEnt); |
| 746 | } |
| 747 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 748 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 749 | CXFile clang_getDeclSourceFile(CXDecl AnonDecl) { |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 750 | assert(AnonDecl && "Passed null CXDecl"); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 751 | NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); |
| 752 | SourceManager &SourceMgr = ND->getASTContext().getSourceManager(); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 753 | return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation()); |
| 754 | } |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 755 | } // end: extern "C" |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 756 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 757 | //===----------------------------------------------------------------------===// |
| 758 | // CXFile Operations. |
| 759 | //===----------------------------------------------------------------------===// |
| 760 | |
| 761 | extern "C" { |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 762 | const char *clang_getFileName(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 763 | if (!SFile) |
| 764 | return 0; |
| 765 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 766 | assert(SFile && "Passed null CXFile"); |
| 767 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 768 | return FEnt->getName(); |
| 769 | } |
| 770 | |
| 771 | time_t clang_getFileTime(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 772 | if (!SFile) |
| 773 | return 0; |
| 774 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 775 | assert(SFile && "Passed null CXFile"); |
| 776 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 777 | return FEnt->getModificationTime(); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 778 | } |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 779 | } // end: extern "C" |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 780 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 781 | //===----------------------------------------------------------------------===// |
| 782 | // CXCursor Operations. |
| 783 | //===----------------------------------------------------------------------===// |
| 784 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 785 | static Decl *getDeclFromExpr(Stmt *E) { |
| 786 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 787 | return RefExpr->getDecl(); |
| 788 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 789 | return ME->getMemberDecl(); |
| 790 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 791 | return RE->getDecl(); |
| 792 | |
| 793 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 794 | return getDeclFromExpr(CE->getCallee()); |
| 795 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) |
| 796 | return getDeclFromExpr(CE->getSubExpr()); |
| 797 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 798 | return OME->getMethodDecl(); |
| 799 | |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | extern "C" { |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 804 | CXString clang_getCursorSpelling(CXCursor C) { |
Daniel Dunbar | c81d718 | 2010-01-18 17:52:42 +0000 | [diff] [blame] | 805 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 806 | if (clang_isReference(C.kind)) { |
| 807 | switch (C.kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 808 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 809 | ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
| 810 | return CIndexer::createCXString(Super->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 811 | } |
| 812 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 813 | ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
| 814 | return CIndexer::createCXString(Class->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 815 | } |
| 816 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 817 | ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 818 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 819 | return CIndexer::createCXString(OID->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 820 | } |
| 821 | case CXCursor_ObjCSelectorRef: { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 822 | ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(getCursorStmt(C)); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 823 | assert(OME && "getCursorSpelling(): Missing message expr"); |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 824 | return CIndexer::createCXString(OME->getSelector().getAsString().c_str(), |
| 825 | true); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 826 | } |
| 827 | case CXCursor_VarRef: |
| 828 | case CXCursor_FunctionRef: |
| 829 | case CXCursor_EnumConstantRef: { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 830 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(getCursorStmt(C)); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 831 | assert(DRE && "getCursorSpelling(): Missing decl ref expr"); |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 832 | return CIndexer::createCXString(DRE->getDecl()->getIdentifier() |
| 833 | ->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 834 | } |
| 835 | default: |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 836 | return CIndexer::createCXString("<not implemented>"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 837 | } |
| 838 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame^] | 839 | |
| 840 | if (clang_isExpression(C.kind)) { |
| 841 | Decl *D = getDeclFromExpr(getCursorExpr(C)); |
| 842 | if (D) |
| 843 | return clang_getDeclSpelling(D); |
| 844 | return CIndexer::createCXString(""); |
| 845 | } |
| 846 | |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 847 | return clang_getDeclSpelling(getCursorDecl(C)); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 850 | const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 851 | switch (Kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 852 | case CXCursor_FunctionDecl: return "FunctionDecl"; |
| 853 | case CXCursor_TypedefDecl: return "TypedefDecl"; |
| 854 | case CXCursor_EnumDecl: return "EnumDecl"; |
| 855 | case CXCursor_EnumConstantDecl: return "EnumConstantDecl"; |
| 856 | case CXCursor_StructDecl: return "StructDecl"; |
| 857 | case CXCursor_UnionDecl: return "UnionDecl"; |
| 858 | case CXCursor_ClassDecl: return "ClassDecl"; |
| 859 | case CXCursor_FieldDecl: return "FieldDecl"; |
| 860 | case CXCursor_VarDecl: return "VarDecl"; |
| 861 | case CXCursor_ParmDecl: return "ParmDecl"; |
| 862 | case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl"; |
| 863 | case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl"; |
| 864 | case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl"; |
| 865 | case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl"; |
| 866 | case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl"; |
| 867 | case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl"; |
| 868 | case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl"; |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 869 | case CXCursor_ObjCImplementationDecl: return "ObjCImplementationDecl"; |
| 870 | case CXCursor_ObjCCategoryImplDecl: return "ObjCCategoryImplDecl"; |
Douglas Gregor | 3012213 | 2010-01-19 22:07:56 +0000 | [diff] [blame] | 871 | case CXCursor_UnexposedDecl: return "UnexposedDecl"; |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 872 | case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; |
| 873 | case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef"; |
| 874 | case CXCursor_ObjCClassRef: return "ObjCClassRef"; |
| 875 | case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef"; |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 876 | case CXCursor_VarRef: return "VarRef"; |
| 877 | case CXCursor_FunctionRef: return "FunctionRef"; |
| 878 | case CXCursor_EnumConstantRef: return "EnumConstantRef"; |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame^] | 879 | case CXCursor_UnexposedExpr: return "UnexposedExpr"; |
| 880 | case CXCursor_DeclRefExpr: return "DeclRefExpr"; |
| 881 | case CXCursor_MemberRefExpr: return "MemberRefExpr"; |
| 882 | case CXCursor_CallExpr: return "CallExpr"; |
| 883 | case CXCursor_ObjCMessageExpr: return "ObjCMessageExpr"; |
| 884 | case CXCursor_UnexposedStmt: return "UnexposedStmt"; |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 885 | case CXCursor_InvalidFile: return "InvalidFile"; |
| 886 | case CXCursor_NoDeclFound: return "NoDeclFound"; |
| 887 | case CXCursor_NotImplemented: return "NotImplemented"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 888 | } |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 889 | |
| 890 | llvm_unreachable("Unhandled CXCursorKind"); |
| 891 | return NULL; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 892 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 893 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 894 | CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 895 | unsigned line, unsigned column) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 896 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 897 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 898 | |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 899 | FileManager &FMgr = CXXUnit->getFileManager(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 900 | const FileEntry *File = FMgr.getFile(source_name, |
| 901 | source_name+strlen(source_name)); |
Ted Kremenek | f462989 | 2010-01-14 01:51:23 +0000 | [diff] [blame] | 902 | if (!File) |
| 903 | return clang_getNullCursor(); |
| 904 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 905 | SourceLocation SLoc = |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 906 | CXXUnit->getSourceManager().getLocation(File, line, column); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 907 | |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 908 | ASTLocation LastLoc = CXXUnit->getLastASTLocation(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 909 | ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc, |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 910 | &LastLoc); |
Ted Kremenek | f462989 | 2010-01-14 01:51:23 +0000 | [diff] [blame] | 911 | |
| 912 | // FIXME: This doesn't look thread-safe. |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 913 | if (ALoc.isValid()) |
| 914 | CXXUnit->setLastASTLocation(ALoc); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 915 | |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 916 | Decl *Dcl = ALoc.getParentDecl(); |
Argyrios Kyrtzidis | 05a7651 | 2009-09-29 19:45:58 +0000 | [diff] [blame] | 917 | if (ALoc.isNamedRef()) |
| 918 | Dcl = ALoc.AsNamedRef().ND; |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 919 | Stmt *Stm = ALoc.dyn_AsStmt(); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 920 | if (Dcl) { |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame^] | 921 | if (Stm) |
| 922 | return MakeCXCursor(Stm, Dcl); |
| 923 | |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 924 | if (ALoc.isNamedRef()) { |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 925 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(Dcl)) |
| 926 | return MakeCursorObjCClassRef(Class, ALoc.AsNamedRef().Loc); |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 927 | if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(Dcl)) |
| 928 | return MakeCursorObjCProtocolRef(Proto, ALoc.AsNamedRef().Loc); |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 929 | } |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 930 | return MakeCXCursor(Dcl); |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 931 | } |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 932 | return MakeCXCursor(CXCursor_NoDeclFound, 0); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 935 | CXCursor clang_getNullCursor(void) { |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 936 | return MakeCXCursor(CXCursor_InvalidFile, 0); |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 940 | return X == Y; |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 941 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 942 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 943 | CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 944 | assert(AnonDecl && "Passed null CXDecl"); |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 945 | return MakeCXCursor(static_cast<NamedDecl *>(AnonDecl)); |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 948 | unsigned clang_isInvalid(enum CXCursorKind K) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 949 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 950 | } |
| 951 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 952 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 953 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 954 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 955 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 956 | unsigned clang_isReference(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 957 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 958 | } |
| 959 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame^] | 960 | unsigned clang_isExpression(enum CXCursorKind K) { |
| 961 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; |
| 962 | } |
| 963 | |
| 964 | unsigned clang_isStatement(enum CXCursorKind K) { |
| 965 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; |
| 966 | } |
| 967 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 968 | CXCursorKind clang_getCursorKind(CXCursor C) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 969 | return C.kind; |
| 970 | } |
| 971 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 972 | CXDecl clang_getCursorDecl(CXCursor C) { |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 973 | if (clang_isDeclaration(C.kind)) |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 974 | return getCursorDecl(C); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 975 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 976 | if (clang_isReference(C.kind)) { |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 977 | if (getCursorStmt(C)) |
| 978 | return getDeclFromExpr(getCursorStmt(C)); |
| 979 | |
| 980 | return getCursorDecl(C); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 981 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame^] | 982 | |
| 983 | if (clang_isExpression(C.kind)) |
| 984 | return getDeclFromExpr(getCursorStmt(C)); |
| 985 | |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 986 | return 0; |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame^] | 989 | static SourceLocation getLocationFromExpr(Expr *E) { |
| 990 | if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
| 991 | return /*FIXME:*/Msg->getLeftLoc(); |
| 992 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 993 | return DRE->getLocation(); |
| 994 | if (MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
| 995 | return Member->getMemberLoc(); |
| 996 | if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
| 997 | return Ivar->getLocation(); |
| 998 | return E->getLocStart(); |
| 999 | } |
| 1000 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1001 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 1002 | if (clang_isReference(C.kind)) { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 1003 | switch (C.kind) { |
| 1004 | case CXCursor_ObjCSuperClassRef: { |
| 1005 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 1006 | = getCursorObjCSuperClassRef(C); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1007 | return translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | case CXCursor_ObjCProtocolRef: { |
| 1011 | std::pair<ObjCProtocolDecl *, SourceLocation> P |
| 1012 | = getCursorObjCProtocolRef(C); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1013 | return translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | case CXCursor_ObjCClassRef: { |
| 1017 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 1018 | = getCursorObjCClassRef(C); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1019 | return translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | case CXCursor_ObjCSelectorRef: |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 1023 | case CXCursor_VarRef: |
| 1024 | case CXCursor_FunctionRef: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame^] | 1025 | case CXCursor_EnumConstantRef: |
| 1026 | return translateSourceLocation(getCursorContext(C), |
| 1027 | getLocationFromExpr(getCursorExpr(C))); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 1028 | |
| 1029 | default: |
| 1030 | // FIXME: Need a way to enumerate all non-reference cases. |
| 1031 | llvm_unreachable("Missed a reference kind"); |
| 1032 | } |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1033 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame^] | 1034 | |
| 1035 | if (clang_isExpression(C.kind)) |
| 1036 | return translateSourceLocation(getCursorContext(C), |
| 1037 | getLocationFromExpr(getCursorExpr(C))); |
| 1038 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1039 | if (!getCursorDecl(C)) { |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1040 | CXSourceLocation empty = { 0, 0 }; |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1041 | return empty; |
| 1042 | } |
| 1043 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 1044 | Decl *D = getCursorDecl(C); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 1045 | SourceLocation Loc = D->getLocation(); |
| 1046 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D)) |
| 1047 | Loc = Class->getClassLoc(); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1048 | return translateSourceLocation(D->getASTContext(), Loc); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1049 | } |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 1050 | |
| 1051 | CXSourceRange clang_getCursorExtent(CXCursor C) { |
| 1052 | if (clang_isReference(C.kind)) { |
| 1053 | switch (C.kind) { |
| 1054 | case CXCursor_ObjCSuperClassRef: { |
| 1055 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 1056 | = getCursorObjCSuperClassRef(C); |
| 1057 | return translateSourceRange(P.first->getASTContext(), P.second); |
| 1058 | } |
| 1059 | |
| 1060 | case CXCursor_ObjCProtocolRef: { |
| 1061 | std::pair<ObjCProtocolDecl *, SourceLocation> P |
| 1062 | = getCursorObjCProtocolRef(C); |
| 1063 | return translateSourceRange(P.first->getASTContext(), P.second); |
| 1064 | } |
| 1065 | |
| 1066 | case CXCursor_ObjCClassRef: { |
| 1067 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 1068 | = getCursorObjCClassRef(C); |
| 1069 | |
| 1070 | return translateSourceRange(P.first->getASTContext(), P.second); |
| 1071 | } |
| 1072 | |
| 1073 | case CXCursor_ObjCSelectorRef: |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 1074 | case CXCursor_VarRef: |
| 1075 | case CXCursor_FunctionRef: |
| 1076 | case CXCursor_EnumConstantRef: |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 1077 | return translateSourceRange(getCursorContext(C), |
| 1078 | getCursorExpr(C)->getSourceRange()); |
| 1079 | |
| 1080 | default: |
| 1081 | // FIXME: Need a way to enumerate all non-reference cases. |
| 1082 | llvm_unreachable("Missed a reference kind"); |
| 1083 | } |
| 1084 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame^] | 1085 | |
| 1086 | if (clang_isExpression(C.kind)) |
| 1087 | return translateSourceRange(getCursorContext(C), |
| 1088 | getCursorExpr(C)->getSourceRange()); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 1089 | |
| 1090 | if (!getCursorDecl(C)) { |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1091 | CXSourceRange empty = { 0, 0, 0 }; |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 1092 | return empty; |
| 1093 | } |
| 1094 | |
| 1095 | Decl *D = getCursorDecl(C); |
| 1096 | return translateSourceRange(D->getASTContext(), D->getSourceRange()); |
| 1097 | } |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 1098 | |
| 1099 | CXCursor clang_getCursorReferenced(CXCursor C) { |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1100 | if (clang_isDeclaration(C.kind)) |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 1101 | return C; |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1102 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame^] | 1103 | if (clang_isExpression(C.kind)) { |
| 1104 | Decl *D = getDeclFromExpr(getCursorExpr(C)); |
| 1105 | if (D) |
| 1106 | return MakeCXCursor(D); |
| 1107 | return clang_getNullCursor(); |
| 1108 | } |
| 1109 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 1110 | if (!clang_isReference(C.kind)) |
| 1111 | return clang_getNullCursor(); |
| 1112 | |
| 1113 | switch (C.kind) { |
| 1114 | case CXCursor_ObjCSuperClassRef: |
| 1115 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first); |
| 1116 | |
| 1117 | case CXCursor_ObjCProtocolRef: { |
| 1118 | return MakeCXCursor(getCursorObjCProtocolRef(C).first); |
| 1119 | |
| 1120 | case CXCursor_ObjCClassRef: |
| 1121 | return MakeCXCursor(getCursorObjCClassRef(C).first); |
| 1122 | |
| 1123 | case CXCursor_ObjCSelectorRef: |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 1124 | case CXCursor_VarRef: |
| 1125 | case CXCursor_FunctionRef: |
Douglas Gregor | e8c7043 | 2010-01-19 22:15:34 +0000 | [diff] [blame] | 1126 | case CXCursor_EnumConstantRef: { |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 1127 | Decl *D = getDeclFromExpr(getCursorExpr(C)); |
| 1128 | if (D) |
| 1129 | return MakeCXCursor(D); |
| 1130 | break; |
| 1131 | } |
| 1132 | |
| 1133 | default: |
| 1134 | // We would prefer to enumerate all non-reference cursor kinds here. |
| 1135 | llvm_unreachable("Unhandled reference cursor kind"); |
| 1136 | break; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | return clang_getNullCursor(); |
| 1141 | } |
| 1142 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1143 | CXCursor clang_getCursorDefinition(CXCursor C) { |
| 1144 | bool WasReference = false; |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame^] | 1145 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1146 | C = clang_getCursorReferenced(C); |
| 1147 | WasReference = true; |
| 1148 | } |
| 1149 | |
| 1150 | if (!clang_isDeclaration(C.kind)) |
| 1151 | return clang_getNullCursor(); |
| 1152 | |
| 1153 | Decl *D = getCursorDecl(C); |
| 1154 | if (!D) |
| 1155 | return clang_getNullCursor(); |
| 1156 | |
| 1157 | switch (D->getKind()) { |
| 1158 | // Declaration kinds that don't really separate the notions of |
| 1159 | // declaration and definition. |
| 1160 | case Decl::Namespace: |
| 1161 | case Decl::Typedef: |
| 1162 | case Decl::TemplateTypeParm: |
| 1163 | case Decl::EnumConstant: |
| 1164 | case Decl::Field: |
| 1165 | case Decl::ObjCIvar: |
| 1166 | case Decl::ObjCAtDefsField: |
| 1167 | case Decl::ImplicitParam: |
| 1168 | case Decl::ParmVar: |
| 1169 | case Decl::NonTypeTemplateParm: |
| 1170 | case Decl::TemplateTemplateParm: |
| 1171 | case Decl::ObjCCategoryImpl: |
| 1172 | case Decl::ObjCImplementation: |
| 1173 | case Decl::LinkageSpec: |
| 1174 | case Decl::ObjCPropertyImpl: |
| 1175 | case Decl::FileScopeAsm: |
| 1176 | case Decl::StaticAssert: |
| 1177 | case Decl::Block: |
| 1178 | return C; |
| 1179 | |
| 1180 | // Declaration kinds that don't make any sense here, but are |
| 1181 | // nonetheless harmless. |
| 1182 | case Decl::TranslationUnit: |
| 1183 | case Decl::Template: |
| 1184 | case Decl::ObjCContainer: |
| 1185 | break; |
| 1186 | |
| 1187 | // Declaration kinds for which the definition is not resolvable. |
| 1188 | case Decl::UnresolvedUsingTypename: |
| 1189 | case Decl::UnresolvedUsingValue: |
| 1190 | break; |
| 1191 | |
| 1192 | case Decl::UsingDirective: |
| 1193 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace()); |
| 1194 | |
| 1195 | case Decl::NamespaceAlias: |
| 1196 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace()); |
| 1197 | |
| 1198 | case Decl::Enum: |
| 1199 | case Decl::Record: |
| 1200 | case Decl::CXXRecord: |
| 1201 | case Decl::ClassTemplateSpecialization: |
| 1202 | case Decl::ClassTemplatePartialSpecialization: |
| 1203 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition(D->getASTContext())) |
| 1204 | return MakeCXCursor(Def); |
| 1205 | return clang_getNullCursor(); |
| 1206 | |
| 1207 | case Decl::Function: |
| 1208 | case Decl::CXXMethod: |
| 1209 | case Decl::CXXConstructor: |
| 1210 | case Decl::CXXDestructor: |
| 1211 | case Decl::CXXConversion: { |
| 1212 | const FunctionDecl *Def = 0; |
| 1213 | if (cast<FunctionDecl>(D)->getBody(Def)) |
| 1214 | return MakeCXCursor(const_cast<FunctionDecl *>(Def)); |
| 1215 | return clang_getNullCursor(); |
| 1216 | } |
| 1217 | |
| 1218 | case Decl::Var: { |
| 1219 | VarDecl *Var = cast<VarDecl>(D); |
| 1220 | |
| 1221 | // Variables with initializers have definitions. |
| 1222 | const VarDecl *Def = 0; |
| 1223 | if (Var->getDefinition(Def)) |
| 1224 | return MakeCXCursor(const_cast<VarDecl *>(Def)); |
| 1225 | |
| 1226 | // extern and private_extern variables are not definitions. |
| 1227 | if (Var->hasExternalStorage()) |
| 1228 | return clang_getNullCursor(); |
| 1229 | |
| 1230 | // In-line static data members do not have definitions. |
| 1231 | if (Var->isStaticDataMember() && !Var->isOutOfLine()) |
| 1232 | return clang_getNullCursor(); |
| 1233 | |
| 1234 | // All other variables are themselves definitions. |
| 1235 | return C; |
| 1236 | } |
| 1237 | |
| 1238 | case Decl::FunctionTemplate: { |
| 1239 | const FunctionDecl *Def = 0; |
| 1240 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) |
| 1241 | return MakeCXCursor(Def->getDescribedFunctionTemplate()); |
| 1242 | return clang_getNullCursor(); |
| 1243 | } |
| 1244 | |
| 1245 | case Decl::ClassTemplate: { |
| 1246 | if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() |
| 1247 | ->getDefinition(D->getASTContext())) |
| 1248 | return MakeCXCursor( |
| 1249 | cast<CXXRecordDecl>(Def)->getDescribedClassTemplate()); |
| 1250 | return clang_getNullCursor(); |
| 1251 | } |
| 1252 | |
| 1253 | case Decl::Using: { |
| 1254 | UsingDecl *Using = cast<UsingDecl>(D); |
| 1255 | CXCursor Def = clang_getNullCursor(); |
| 1256 | for (UsingDecl::shadow_iterator S = Using->shadow_begin(), |
| 1257 | SEnd = Using->shadow_end(); |
| 1258 | S != SEnd; ++S) { |
| 1259 | if (Def != clang_getNullCursor()) { |
| 1260 | // FIXME: We have no way to return multiple results. |
| 1261 | return clang_getNullCursor(); |
| 1262 | } |
| 1263 | |
| 1264 | Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl())); |
| 1265 | } |
| 1266 | |
| 1267 | return Def; |
| 1268 | } |
| 1269 | |
| 1270 | case Decl::UsingShadow: |
| 1271 | return clang_getCursorDefinition( |
| 1272 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl())); |
| 1273 | |
| 1274 | case Decl::ObjCMethod: { |
| 1275 | ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); |
| 1276 | if (Method->isThisDeclarationADefinition()) |
| 1277 | return C; |
| 1278 | |
| 1279 | // Dig out the method definition in the associated |
| 1280 | // @implementation, if we have it. |
| 1281 | // FIXME: The ASTs should make finding the definition easier. |
| 1282 | if (ObjCInterfaceDecl *Class |
| 1283 | = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) |
| 1284 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) |
| 1285 | if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), |
| 1286 | Method->isInstanceMethod())) |
| 1287 | if (Def->isThisDeclarationADefinition()) |
| 1288 | return MakeCXCursor(Def); |
| 1289 | |
| 1290 | return clang_getNullCursor(); |
| 1291 | } |
| 1292 | |
| 1293 | case Decl::ObjCCategory: |
| 1294 | if (ObjCCategoryImplDecl *Impl |
| 1295 | = cast<ObjCCategoryDecl>(D)->getImplementation()) |
| 1296 | return MakeCXCursor(Impl); |
| 1297 | return clang_getNullCursor(); |
| 1298 | |
| 1299 | case Decl::ObjCProtocol: |
| 1300 | if (!cast<ObjCProtocolDecl>(D)->isForwardDecl()) |
| 1301 | return C; |
| 1302 | return clang_getNullCursor(); |
| 1303 | |
| 1304 | case Decl::ObjCInterface: |
| 1305 | // There are two notions of a "definition" for an Objective-C |
| 1306 | // class: the interface and its implementation. When we resolved a |
| 1307 | // reference to an Objective-C class, produce the @interface as |
| 1308 | // the definition; when we were provided with the interface, |
| 1309 | // produce the @implementation as the definition. |
| 1310 | if (WasReference) { |
| 1311 | if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl()) |
| 1312 | return C; |
| 1313 | } else if (ObjCImplementationDecl *Impl |
| 1314 | = cast<ObjCInterfaceDecl>(D)->getImplementation()) |
| 1315 | return MakeCXCursor(Impl); |
| 1316 | return clang_getNullCursor(); |
| 1317 | |
| 1318 | case Decl::ObjCProperty: |
| 1319 | // FIXME: We don't really know where to find the |
| 1320 | // ObjCPropertyImplDecls that implement this property. |
| 1321 | return clang_getNullCursor(); |
| 1322 | |
| 1323 | case Decl::ObjCCompatibleAlias: |
| 1324 | if (ObjCInterfaceDecl *Class |
| 1325 | = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) |
| 1326 | if (!Class->isForwardDecl()) |
| 1327 | return MakeCXCursor(Class); |
| 1328 | |
| 1329 | return clang_getNullCursor(); |
| 1330 | |
| 1331 | case Decl::ObjCForwardProtocol: { |
| 1332 | ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D); |
| 1333 | if (Forward->protocol_size() == 1) |
| 1334 | return clang_getCursorDefinition( |
| 1335 | MakeCXCursor(*Forward->protocol_begin())); |
| 1336 | |
| 1337 | // FIXME: Cannot return multiple definitions. |
| 1338 | return clang_getNullCursor(); |
| 1339 | } |
| 1340 | |
| 1341 | case Decl::ObjCClass: { |
| 1342 | ObjCClassDecl *Class = cast<ObjCClassDecl>(D); |
| 1343 | if (Class->size() == 1) { |
| 1344 | ObjCInterfaceDecl *IFace = Class->begin()->getInterface(); |
| 1345 | if (!IFace->isForwardDecl()) |
| 1346 | return MakeCXCursor(IFace); |
| 1347 | return clang_getNullCursor(); |
| 1348 | } |
| 1349 | |
| 1350 | // FIXME: Cannot return multiple definitions. |
| 1351 | return clang_getNullCursor(); |
| 1352 | } |
| 1353 | |
| 1354 | case Decl::Friend: |
| 1355 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) |
| 1356 | return clang_getCursorDefinition(MakeCXCursor(Friend)); |
| 1357 | return clang_getNullCursor(); |
| 1358 | |
| 1359 | case Decl::FriendTemplate: |
| 1360 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) |
| 1361 | return clang_getCursorDefinition(MakeCXCursor(Friend)); |
| 1362 | return clang_getNullCursor(); |
| 1363 | } |
| 1364 | |
| 1365 | return clang_getNullCursor(); |
| 1366 | } |
| 1367 | |
| 1368 | unsigned clang_isCursorDefinition(CXCursor C) { |
| 1369 | if (!clang_isDeclaration(C.kind)) |
| 1370 | return 0; |
| 1371 | |
| 1372 | return clang_getCursorDefinition(C) == C; |
| 1373 | } |
| 1374 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1375 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1376 | const char **startBuf, |
| 1377 | const char **endBuf, |
| 1378 | unsigned *startLine, |
| 1379 | unsigned *startColumn, |
| 1380 | unsigned *endLine, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1381 | unsigned *endColumn) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 1382 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
| 1383 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1384 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 1385 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 1386 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1387 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 1388 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 1389 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 1390 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 1391 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 1392 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 1393 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 1394 | } |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 1395 | |
| 1396 | } // end: extern "C" |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1397 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 1398 | //===----------------------------------------------------------------------===// |
| 1399 | // CXString Operations. |
| 1400 | //===----------------------------------------------------------------------===// |
| 1401 | |
| 1402 | extern "C" { |
| 1403 | const char *clang_getCString(CXString string) { |
| 1404 | return string.Spelling; |
| 1405 | } |
| 1406 | |
| 1407 | void clang_disposeString(CXString string) { |
| 1408 | if (string.MustFreeString && string.Spelling) |
| 1409 | free((void*)string.Spelling); |
| 1410 | } |
| 1411 | } // end: extern "C" |