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