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