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 | |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Version.h" |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclVisitor.h" |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeLocVisitor.h" |
Ted Kremenek | d821065 | 2010-01-06 23:43:31 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Lexer.h" |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MemoryBuffer.h" |
Benjamin Kramer | 0829a83 | 2009-10-18 11:19:36 +0000 | [diff] [blame] | 25 | #include "llvm/System/Program.h" |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 26 | |
Ted Kremenek | db3d0da | 2010-01-05 20:55:39 +0000 | [diff] [blame] | 27 | // Needed to define L_TMPNAM on some systems. |
| 28 | #include <cstdio> |
| 29 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 30 | using namespace clang; |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 31 | using namespace clang::cxcursor; |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 32 | using namespace idx; |
| 33 | |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | // Crash Reporting. |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | |
| 38 | #ifdef __APPLE__ |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 39 | #ifndef NDEBUG |
| 40 | #define USE_CRASHTRACER |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 41 | #include "clang/Analysis/Support/SaveAndRestore.h" |
| 42 | // Integrate with crash reporter. |
| 43 | extern "C" const char *__crashreporter_info__; |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 44 | #define NUM_CRASH_STRINGS 16 |
| 45 | static unsigned crashtracer_counter = 0; |
Ted Kremenek | 254ba7c | 2010-01-07 23:13:53 +0000 | [diff] [blame] | 46 | static unsigned crashtracer_counter_id[NUM_CRASH_STRINGS] = { 0 }; |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 47 | static const char *crashtracer_strings[NUM_CRASH_STRINGS] = { 0 }; |
| 48 | static const char *agg_crashtracer_strings[NUM_CRASH_STRINGS] = { 0 }; |
| 49 | |
| 50 | static unsigned SetCrashTracerInfo(const char *str, |
| 51 | llvm::SmallString<1024> &AggStr) { |
| 52 | |
Ted Kremenek | 254ba7c | 2010-01-07 23:13:53 +0000 | [diff] [blame] | 53 | unsigned slot = 0; |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 54 | while (crashtracer_strings[slot]) { |
| 55 | if (++slot == NUM_CRASH_STRINGS) |
| 56 | slot = 0; |
| 57 | } |
| 58 | crashtracer_strings[slot] = str; |
Ted Kremenek | 254ba7c | 2010-01-07 23:13:53 +0000 | [diff] [blame] | 59 | crashtracer_counter_id[slot] = ++crashtracer_counter; |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 60 | |
| 61 | // We need to create an aggregate string because multiple threads |
| 62 | // may be in this method at one time. The crash reporter string |
| 63 | // will attempt to overapproximate the set of in-flight invocations |
| 64 | // of this function. Race conditions can still cause this goal |
| 65 | // to not be achieved. |
| 66 | { |
| 67 | llvm::raw_svector_ostream Out(AggStr); |
| 68 | for (unsigned i = 0; i < NUM_CRASH_STRINGS; ++i) |
| 69 | if (crashtracer_strings[i]) Out << crashtracer_strings[i] << '\n'; |
| 70 | } |
| 71 | __crashreporter_info__ = agg_crashtracer_strings[slot] = AggStr.c_str(); |
| 72 | return slot; |
| 73 | } |
| 74 | |
| 75 | static void ResetCrashTracerInfo(unsigned slot) { |
Ted Kremenek | 254ba7c | 2010-01-07 23:13:53 +0000 | [diff] [blame] | 76 | unsigned max_slot = 0; |
| 77 | unsigned max_value = 0; |
| 78 | |
| 79 | crashtracer_strings[slot] = agg_crashtracer_strings[slot] = 0; |
| 80 | |
| 81 | for (unsigned i = 0 ; i < NUM_CRASH_STRINGS; ++i) |
| 82 | if (agg_crashtracer_strings[i] && |
| 83 | crashtracer_counter_id[i] > max_value) { |
| 84 | max_slot = i; |
| 85 | max_value = crashtracer_counter_id[i]; |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 86 | } |
Ted Kremenek | 254ba7c | 2010-01-07 23:13:53 +0000 | [diff] [blame] | 87 | |
| 88 | __crashreporter_info__ = agg_crashtracer_strings[max_slot]; |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | namespace { |
| 92 | class ArgsCrashTracerInfo { |
| 93 | llvm::SmallString<1024> CrashString; |
| 94 | llvm::SmallString<1024> AggregateString; |
| 95 | unsigned crashtracerSlot; |
| 96 | public: |
| 97 | ArgsCrashTracerInfo(llvm::SmallVectorImpl<const char*> &Args) |
| 98 | : crashtracerSlot(0) |
| 99 | { |
| 100 | { |
| 101 | llvm::raw_svector_ostream Out(CrashString); |
| 102 | Out << "ClangCIndex [createTranslationUnitFromSourceFile]: clang"; |
| 103 | for (llvm::SmallVectorImpl<const char*>::iterator I=Args.begin(), |
| 104 | E=Args.end(); I!=E; ++I) |
| 105 | Out << ' ' << *I; |
| 106 | } |
| 107 | crashtracerSlot = SetCrashTracerInfo(CrashString.c_str(), |
| 108 | AggregateString); |
| 109 | } |
| 110 | |
| 111 | ~ArgsCrashTracerInfo() { |
| 112 | ResetCrashTracerInfo(crashtracerSlot); |
| 113 | } |
| 114 | }; |
| 115 | } |
| 116 | #endif |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 117 | #endif |
| 118 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 119 | typedef llvm::PointerIntPair<ASTContext *, 1, bool> CXSourceLocationPtr; |
| 120 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 121 | /// \brief Translate a Clang source location into a CIndex source location. |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 122 | static CXSourceLocation translateSourceLocation(ASTContext &Context, |
| 123 | SourceLocation Loc, |
| 124 | bool AtEnd = false) { |
| 125 | CXSourceLocationPtr Ptr(&Context, AtEnd); |
| 126 | CXSourceLocation Result = { Ptr.getOpaqueValue(), Loc.getRawEncoding() }; |
| 127 | return Result; |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 130 | /// \brief Translate a Clang source range into a CIndex source range. |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 131 | static CXSourceRange translateSourceRange(ASTContext &Context, SourceRange R) { |
| 132 | CXSourceRange Result = { &Context, |
| 133 | R.getBegin().getRawEncoding(), |
| 134 | R.getEnd().getRawEncoding() }; |
| 135 | return Result; |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 138 | static SourceLocation translateSourceLocation(CXSourceLocation L) { |
| 139 | return SourceLocation::getFromRawEncoding(L.int_data); |
| 140 | } |
| 141 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 142 | static SourceRange translateSourceRange(CXSourceRange R) { |
| 143 | return SourceRange(SourceLocation::getFromRawEncoding(R.begin_int_data), |
| 144 | SourceLocation::getFromRawEncoding(R.end_int_data)); |
| 145 | } |
| 146 | |
| 147 | /// \brief The result of comparing two source ranges. |
| 148 | enum RangeComparisonResult { |
| 149 | /// \brief Either the ranges overlap or one of the ranges is invalid. |
| 150 | RangeOverlap, |
| 151 | |
| 152 | /// \brief The first range ends before the second range starts. |
| 153 | RangeBefore, |
| 154 | |
| 155 | /// \brief The first range starts after the second range ends. |
| 156 | RangeAfter |
| 157 | }; |
| 158 | |
| 159 | /// \brief Compare two source ranges to determine their relative position in |
| 160 | /// the translation unit. |
| 161 | static RangeComparisonResult RangeCompare(SourceManager &SM, |
| 162 | SourceRange R1, |
| 163 | SourceRange R2) { |
| 164 | assert(R1.isValid() && "First range is invalid?"); |
| 165 | assert(R2.isValid() && "Second range is invalid?"); |
| 166 | if (SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin())) |
| 167 | return RangeBefore; |
| 168 | if (SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin())) |
| 169 | return RangeAfter; |
| 170 | return RangeOverlap; |
| 171 | } |
| 172 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 173 | |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 174 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 175 | // Cursor visitor. |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 176 | //===----------------------------------------------------------------------===// |
| 177 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 178 | namespace { |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 179 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 180 | // Cursor visitor. |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 181 | class CursorVisitor : public DeclVisitor<CursorVisitor, bool>, |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 182 | public TypeLocVisitor<CursorVisitor, bool>, |
| 183 | public StmtVisitor<CursorVisitor, bool> |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 184 | { |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 185 | /// \brief The translation unit we are traversing. |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 186 | ASTUnit *TU; |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 187 | |
| 188 | /// \brief The parent cursor whose children we are traversing. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 189 | CXCursor Parent; |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 190 | |
| 191 | /// \brief The declaration that serves at the parent of any statement or |
| 192 | /// expression nodes. |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 193 | Decl *StmtParent; |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 194 | |
| 195 | /// \brief The visitor function. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 196 | CXCursorVisitor Visitor; |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 197 | |
| 198 | /// \brief The opaque client data, to be passed along to the visitor. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 199 | CXClientData ClientData; |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 200 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 201 | // MaxPCHLevel - the maximum PCH level of declarations that we will pass on |
| 202 | // to the visitor. Declarations with a PCH level greater than this value will |
| 203 | // be suppressed. |
| 204 | unsigned MaxPCHLevel; |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 205 | |
| 206 | /// \brief When valid, a source range to which the cursor should restrict |
| 207 | /// its search. |
| 208 | SourceRange RegionOfInterest; |
| 209 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 210 | using DeclVisitor<CursorVisitor, bool>::Visit; |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 211 | using TypeLocVisitor<CursorVisitor, bool>::Visit; |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 212 | using StmtVisitor<CursorVisitor, bool>::Visit; |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 213 | |
| 214 | /// \brief Determine whether this particular source range comes before, comes |
| 215 | /// after, or overlaps the region of interest. |
| 216 | /// |
| 217 | /// \param R a source range retrieved from the abstract syntax tree. |
| 218 | RangeComparisonResult CompareRegionOfInterest(SourceRange R); |
| 219 | |
| 220 | /// \brief Determine whether this particular source range comes before, comes |
| 221 | /// after, or overlaps the region of interest. |
| 222 | /// |
| 223 | /// \param CXR a source range retrieved from a cursor. |
| 224 | RangeComparisonResult CompareRegionOfInterest(CXSourceRange CXR); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 225 | |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 226 | public: |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 227 | CursorVisitor(ASTUnit *TU, CXCursorVisitor Visitor, CXClientData ClientData, |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 228 | unsigned MaxPCHLevel, |
| 229 | SourceRange RegionOfInterest = SourceRange()) |
| 230 | : TU(TU), Visitor(Visitor), ClientData(ClientData), |
| 231 | MaxPCHLevel(MaxPCHLevel), RegionOfInterest(RegionOfInterest) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 232 | { |
| 233 | Parent.kind = CXCursor_NoDeclFound; |
| 234 | Parent.data[0] = 0; |
| 235 | Parent.data[1] = 0; |
| 236 | Parent.data[2] = 0; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 237 | StmtParent = 0; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 240 | bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 241 | bool VisitChildren(CXCursor Parent); |
| 242 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 243 | // Declaration visitors |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 244 | bool VisitDeclContext(DeclContext *DC); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 245 | bool VisitTranslationUnitDecl(TranslationUnitDecl *D); |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 246 | bool VisitTypedefDecl(TypedefDecl *D); |
| 247 | bool VisitTagDecl(TagDecl *D); |
| 248 | bool VisitEnumConstantDecl(EnumConstantDecl *D); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 249 | bool VisitDeclaratorDecl(DeclaratorDecl *DD); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 250 | bool VisitFunctionDecl(FunctionDecl *ND); |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 251 | bool VisitFieldDecl(FieldDecl *D); |
| 252 | bool VisitVarDecl(VarDecl *); |
| 253 | bool VisitObjCMethodDecl(ObjCMethodDecl *ND); |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 254 | bool VisitObjCContainerDecl(ObjCContainerDecl *D); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 255 | bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 256 | bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID); |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 257 | bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| 258 | bool VisitObjCImplDecl(ObjCImplDecl *D); |
| 259 | bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 260 | bool VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
| 261 | // FIXME: ObjCPropertyDecl requires TypeSourceInfo, getter/setter locations, |
| 262 | // etc. |
| 263 | // FIXME: ObjCCompatibleAliasDecl requires aliased-class locations. |
| 264 | bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); |
| 265 | bool VisitObjCClassDecl(ObjCClassDecl *D); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 266 | |
| 267 | // Type visitors |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 268 | // FIXME: QualifiedTypeLoc doesn't provide any location information |
| 269 | bool VisitBuiltinTypeLoc(BuiltinTypeLoc TL); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 270 | bool VisitTypedefTypeLoc(TypedefTypeLoc TL); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 271 | bool VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL); |
| 272 | bool VisitTagTypeLoc(TagTypeLoc TL); |
| 273 | // FIXME: TemplateTypeParmTypeLoc doesn't provide any location information |
| 274 | bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL); |
| 275 | bool VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL); |
| 276 | bool VisitPointerTypeLoc(PointerTypeLoc TL); |
| 277 | bool VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL); |
| 278 | bool VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL); |
| 279 | bool VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL); |
| 280 | bool VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL); |
| 281 | bool VisitFunctionTypeLoc(FunctionTypeLoc TL); |
| 282 | bool VisitArrayTypeLoc(ArrayTypeLoc TL); |
Douglas Gregor | 2332c11 | 2010-01-21 20:48:56 +0000 | [diff] [blame] | 283 | // FIXME: Implement for TemplateSpecializationTypeLoc |
| 284 | // FIXME: Implement visitors here when the unimplemented TypeLocs get |
| 285 | // implemented |
| 286 | bool VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL); |
| 287 | bool VisitTypeOfTypeLoc(TypeOfTypeLoc TL); |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 288 | |
| 289 | // Statement visitors |
| 290 | bool VisitStmt(Stmt *S); |
| 291 | bool VisitDeclStmt(DeclStmt *S); |
Douglas Gregor | f5bab41 | 2010-01-22 01:00:11 +0000 | [diff] [blame] | 292 | // FIXME: LabelStmt label? |
| 293 | bool VisitIfStmt(IfStmt *S); |
| 294 | bool VisitSwitchStmt(SwitchStmt *S); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 295 | }; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 296 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 297 | } // end anonymous namespace |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 298 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 299 | RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { |
| 300 | assert(RegionOfInterest.isValid() && "RangeCompare called with invalid range"); |
| 301 | if (R.isInvalid()) |
| 302 | return RangeOverlap; |
| 303 | |
| 304 | // Move the end of the input range to the end of the last token in that |
| 305 | // range. |
| 306 | R.setEnd(TU->getPreprocessor().getLocForEndOfToken(R.getEnd(), 1)); |
| 307 | return RangeCompare(TU->getSourceManager(), R, RegionOfInterest); |
| 308 | } |
| 309 | |
| 310 | RangeComparisonResult CursorVisitor::CompareRegionOfInterest(CXSourceRange CXR) { |
| 311 | return CompareRegionOfInterest(translateSourceRange(CXR)); |
| 312 | } |
| 313 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 314 | /// \brief Visit the given cursor and, if requested by the visitor, |
| 315 | /// its children. |
| 316 | /// |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 317 | /// \param Cursor the cursor to visit. |
| 318 | /// |
| 319 | /// \param CheckRegionOfInterest if true, then the caller already checked that |
| 320 | /// this cursor is within the region of interest. |
| 321 | /// |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 322 | /// \returns true if the visitation should be aborted, false if it |
| 323 | /// should continue. |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 324 | bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 325 | if (clang_isInvalid(Cursor.kind)) |
| 326 | return false; |
| 327 | |
| 328 | if (clang_isDeclaration(Cursor.kind)) { |
| 329 | Decl *D = getCursorDecl(Cursor); |
| 330 | assert(D && "Invalid declaration cursor"); |
| 331 | if (D->getPCHLevel() > MaxPCHLevel) |
| 332 | return false; |
| 333 | |
| 334 | if (D->isImplicit()) |
| 335 | return false; |
| 336 | } |
| 337 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 338 | // If we have a range of interest, and this cursor doesn't intersect with it, |
| 339 | // we're done. |
| 340 | if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { |
| 341 | CXSourceRange Range = clang_getCursorExtent(Cursor); |
| 342 | if (translateSourceRange(Range).isInvalid() || |
| 343 | CompareRegionOfInterest(Range)) |
| 344 | return false; |
| 345 | } |
| 346 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 347 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 348 | case CXChildVisit_Break: |
| 349 | return true; |
| 350 | |
| 351 | case CXChildVisit_Continue: |
| 352 | return false; |
| 353 | |
| 354 | case CXChildVisit_Recurse: |
| 355 | return VisitChildren(Cursor); |
| 356 | } |
| 357 | |
| 358 | llvm_unreachable("Silly GCC, we can't get here"); |
| 359 | } |
| 360 | |
| 361 | /// \brief Visit the children of the given cursor. |
| 362 | /// |
| 363 | /// \returns true if the visitation should be aborted, false if it |
| 364 | /// should continue. |
| 365 | bool CursorVisitor::VisitChildren(CXCursor Cursor) { |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 366 | if (clang_isReference(Cursor.kind)) { |
| 367 | // By definition, references have no children. |
| 368 | return false; |
| 369 | } |
| 370 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 371 | // Set the Parent field to Cursor, then back to its old value once we're |
| 372 | // done. |
| 373 | class SetParentRAII { |
| 374 | CXCursor &Parent; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 375 | Decl *&StmtParent; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 376 | CXCursor OldParent; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 377 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 378 | public: |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 379 | SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent) |
| 380 | : Parent(Parent), StmtParent(StmtParent), OldParent(Parent) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 381 | { |
| 382 | Parent = NewParent; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 383 | if (clang_isDeclaration(Parent.kind)) |
| 384 | StmtParent = getCursorDecl(Parent); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | ~SetParentRAII() { |
| 388 | Parent = OldParent; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 389 | if (clang_isDeclaration(Parent.kind)) |
| 390 | StmtParent = getCursorDecl(Parent); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 391 | } |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 392 | } SetParent(Parent, StmtParent, Cursor); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 393 | |
| 394 | if (clang_isDeclaration(Cursor.kind)) { |
| 395 | Decl *D = getCursorDecl(Cursor); |
| 396 | assert(D && "Invalid declaration cursor"); |
| 397 | return Visit(D); |
| 398 | } |
| 399 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 400 | if (clang_isStatement(Cursor.kind)) |
| 401 | return Visit(getCursorStmt(Cursor)); |
| 402 | if (clang_isExpression(Cursor.kind)) |
| 403 | return Visit(getCursorExpr(Cursor)); |
| 404 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 405 | if (clang_isTranslationUnit(Cursor.kind)) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 406 | ASTUnit *CXXUnit = getCursorASTUnit(Cursor); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 407 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && |
| 408 | RegionOfInterest.isInvalid()) { |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 409 | const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls(); |
| 410 | for (std::vector<Decl*>::const_iterator it = TLDs.begin(), |
| 411 | ie = TLDs.end(); it != ie; ++it) { |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 412 | if (Visit(MakeCXCursor(*it, CXXUnit), true)) |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 413 | return true; |
| 414 | } |
| 415 | } else { |
| 416 | return VisitDeclContext( |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 417 | CXXUnit->getASTContext().getTranslationUnitDecl()); |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | return false; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 421 | } |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 422 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 423 | // Nothing to visit at the moment. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 424 | return false; |
| 425 | } |
| 426 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 427 | bool CursorVisitor::VisitDeclContext(DeclContext *DC) { |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 428 | for (DeclContext::decl_iterator |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 429 | I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) { |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 430 | if (RegionOfInterest.isValid()) { |
| 431 | SourceRange R = (*I)->getSourceRange(); |
| 432 | if (R.isInvalid()) |
| 433 | continue; |
| 434 | |
| 435 | switch (CompareRegionOfInterest(R)) { |
| 436 | case RangeBefore: |
| 437 | // This declaration comes before the region of interest; skip it. |
| 438 | continue; |
| 439 | |
| 440 | case RangeAfter: |
| 441 | // This declaration comes after the region of interest; we're done. |
| 442 | return false; |
| 443 | |
| 444 | case RangeOverlap: |
| 445 | // This declaration overlaps the region of interest; visit it. |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | if (Visit(MakeCXCursor(*I, TU), true)) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 451 | return true; |
| 452 | } |
| 453 | |
| 454 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 457 | bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 458 | llvm_unreachable("Translation units are visited directly by Visit()"); |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { |
| 463 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 464 | return Visit(TSInfo->getTypeLoc()); |
| 465 | |
| 466 | return false; |
| 467 | } |
| 468 | |
| 469 | bool CursorVisitor::VisitTagDecl(TagDecl *D) { |
| 470 | return VisitDeclContext(D); |
| 471 | } |
| 472 | |
| 473 | bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 474 | if (Expr *Init = D->getInitExpr()) |
| 475 | return Visit(MakeCXCursor(Init, StmtParent, TU)); |
| 476 | return false; |
| 477 | } |
| 478 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 479 | bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { |
| 480 | if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) |
| 481 | if (Visit(TSInfo->getTypeLoc())) |
| 482 | return true; |
| 483 | |
| 484 | return false; |
| 485 | } |
| 486 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 487 | bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 488 | if (VisitDeclaratorDecl(ND)) |
| 489 | return true; |
| 490 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 491 | if (ND->isThisDeclarationADefinition() && |
| 492 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU))) |
| 493 | return true; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 494 | |
| 495 | return false; |
| 496 | } |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 497 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 498 | bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { |
| 499 | if (VisitDeclaratorDecl(D)) |
| 500 | return true; |
| 501 | |
| 502 | if (Expr *BitWidth = D->getBitWidth()) |
| 503 | return Visit(MakeCXCursor(BitWidth, StmtParent, TU)); |
| 504 | |
| 505 | return false; |
| 506 | } |
| 507 | |
| 508 | bool CursorVisitor::VisitVarDecl(VarDecl *D) { |
| 509 | if (VisitDeclaratorDecl(D)) |
| 510 | return true; |
| 511 | |
| 512 | if (Expr *Init = D->getInit()) |
| 513 | return Visit(MakeCXCursor(Init, StmtParent, TU)); |
| 514 | |
| 515 | return false; |
| 516 | } |
| 517 | |
| 518 | bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
| 519 | // FIXME: We really need a TypeLoc covering Objective-C method declarations. |
| 520 | // At the moment, we don't have information about locations in the return |
| 521 | // type. |
| 522 | for (ObjCMethodDecl::param_iterator P = ND->param_begin(), |
| 523 | PEnd = ND->param_end(); |
| 524 | P != PEnd; ++P) { |
| 525 | if (Visit(MakeCXCursor(*P, TU))) |
| 526 | return true; |
| 527 | } |
| 528 | |
| 529 | if (ND->isThisDeclarationADefinition() && |
| 530 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU))) |
| 531 | return true; |
| 532 | |
| 533 | return false; |
| 534 | } |
| 535 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 536 | bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
| 537 | return VisitDeclContext(D); |
| 538 | } |
| 539 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 540 | bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 541 | if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), |
| 542 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 543 | return true; |
| 544 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 545 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 546 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 547 | E = ND->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 548 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 549 | return true; |
| 550 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 551 | return VisitObjCContainerDecl(ND); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 554 | bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 555 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
| 556 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 557 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 558 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 559 | return true; |
| 560 | |
| 561 | return VisitObjCContainerDecl(PID); |
| 562 | } |
| 563 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 564 | bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 565 | // Issue callbacks for super class. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 566 | if (D->getSuperClass() && |
| 567 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 568 | D->getSuperClassLoc(), |
| 569 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 570 | return true; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 571 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 572 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 573 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 574 | E = D->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 575 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 576 | return true; |
| 577 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 578 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 581 | bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 582 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 585 | bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 586 | if (Visit(MakeCursorObjCClassRef(D->getCategoryDecl()->getClassInterface(), |
| 587 | D->getLocation(), TU))) |
| 588 | return true; |
| 589 | |
| 590 | return VisitObjCImplDecl(D); |
| 591 | } |
| 592 | |
| 593 | bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 594 | #if 0 |
| 595 | // Issue callbacks for super class. |
| 596 | // FIXME: No source location information! |
| 597 | if (D->getSuperClass() && |
| 598 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
| 599 | D->getSuperClassLoc(), |
| 600 | TU))) |
| 601 | return true; |
| 602 | #endif |
| 603 | |
| 604 | return VisitObjCImplDecl(D); |
| 605 | } |
| 606 | |
| 607 | bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 608 | ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 609 | for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(), |
| 610 | E = D->protocol_end(); |
| 611 | I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 612 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 613 | return true; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 614 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 615 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 618 | bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) { |
| 619 | for (ObjCClassDecl::iterator C = D->begin(), CEnd = D->end(); C != CEnd; ++C) |
| 620 | if (Visit(MakeCursorObjCClassRef(C->getInterface(), C->getLocation(), TU))) |
| 621 | return true; |
| 622 | |
| 623 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 626 | bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
| 627 | ASTContext &Context = TU->getASTContext(); |
| 628 | |
| 629 | // Some builtin types (such as Objective-C's "id", "sel", and |
| 630 | // "Class") have associated declarations. Create cursors for those. |
| 631 | QualType VisitType; |
| 632 | switch (TL.getType()->getAs<BuiltinType>()->getKind()) { |
| 633 | case BuiltinType::Void: |
| 634 | case BuiltinType::Bool: |
| 635 | case BuiltinType::Char_U: |
| 636 | case BuiltinType::UChar: |
| 637 | case BuiltinType::Char16: |
| 638 | case BuiltinType::Char32: |
| 639 | case BuiltinType::UShort: |
| 640 | case BuiltinType::UInt: |
| 641 | case BuiltinType::ULong: |
| 642 | case BuiltinType::ULongLong: |
| 643 | case BuiltinType::UInt128: |
| 644 | case BuiltinType::Char_S: |
| 645 | case BuiltinType::SChar: |
| 646 | case BuiltinType::WChar: |
| 647 | case BuiltinType::Short: |
| 648 | case BuiltinType::Int: |
| 649 | case BuiltinType::Long: |
| 650 | case BuiltinType::LongLong: |
| 651 | case BuiltinType::Int128: |
| 652 | case BuiltinType::Float: |
| 653 | case BuiltinType::Double: |
| 654 | case BuiltinType::LongDouble: |
| 655 | case BuiltinType::NullPtr: |
| 656 | case BuiltinType::Overload: |
| 657 | case BuiltinType::Dependent: |
| 658 | break; |
| 659 | |
| 660 | case BuiltinType::UndeducedAuto: // FIXME: Deserves a cursor? |
| 661 | break; |
| 662 | |
| 663 | case BuiltinType::ObjCId: |
| 664 | VisitType = Context.getObjCIdType(); |
| 665 | break; |
| 666 | |
| 667 | case BuiltinType::ObjCClass: |
| 668 | VisitType = Context.getObjCClassType(); |
| 669 | break; |
| 670 | |
| 671 | case BuiltinType::ObjCSel: |
| 672 | VisitType = Context.getObjCSelType(); |
| 673 | break; |
| 674 | } |
| 675 | |
| 676 | if (!VisitType.isNull()) { |
| 677 | if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) |
| 678 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), |
| 679 | TU)); |
| 680 | } |
| 681 | |
| 682 | return false; |
| 683 | } |
| 684 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 685 | bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 686 | return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU)); |
| 687 | } |
| 688 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 689 | bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 690 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 691 | } |
| 692 | |
| 693 | bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { |
| 694 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 695 | } |
| 696 | |
| 697 | bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 698 | if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU))) |
| 699 | return true; |
| 700 | |
| 701 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 702 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 703 | TU))) |
| 704 | return true; |
| 705 | } |
| 706 | |
| 707 | return false; |
| 708 | } |
| 709 | |
| 710 | bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 711 | if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseTypeLoc())) |
| 712 | return true; |
| 713 | |
| 714 | if (TL.hasProtocolsAsWritten()) { |
| 715 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 716 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), |
| 717 | TL.getProtocolLoc(I), |
| 718 | TU))) |
| 719 | return true; |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | return false; |
| 724 | } |
| 725 | |
| 726 | bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 727 | return Visit(TL.getPointeeLoc()); |
| 728 | } |
| 729 | |
| 730 | bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 731 | return Visit(TL.getPointeeLoc()); |
| 732 | } |
| 733 | |
| 734 | bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 735 | return Visit(TL.getPointeeLoc()); |
| 736 | } |
| 737 | |
| 738 | bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 739 | return Visit(TL.getPointeeLoc()); |
| 740 | } |
| 741 | |
| 742 | bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 743 | return Visit(TL.getPointeeLoc()); |
| 744 | } |
| 745 | |
| 746 | bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL) { |
| 747 | if (Visit(TL.getResultLoc())) |
| 748 | return true; |
| 749 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 750 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 751 | if (Visit(MakeCXCursor(TL.getArg(I), TU))) |
| 752 | return true; |
| 753 | |
| 754 | return false; |
| 755 | } |
| 756 | |
| 757 | bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 758 | if (Visit(TL.getElementLoc())) |
| 759 | return true; |
| 760 | |
| 761 | if (Expr *Size = TL.getSizeExpr()) |
| 762 | return Visit(MakeCXCursor(Size, StmtParent, TU)); |
| 763 | |
| 764 | return false; |
| 765 | } |
| 766 | |
Douglas Gregor | 2332c11 | 2010-01-21 20:48:56 +0000 | [diff] [blame] | 767 | bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 768 | return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); |
| 769 | } |
| 770 | |
| 771 | bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 772 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 773 | return Visit(TSInfo->getTypeLoc()); |
| 774 | |
| 775 | return false; |
| 776 | } |
| 777 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 778 | bool CursorVisitor::VisitStmt(Stmt *S) { |
| 779 | for (Stmt::child_iterator Child = S->child_begin(), ChildEnd = S->child_end(); |
| 780 | Child != ChildEnd; ++Child) { |
| 781 | if (Visit(MakeCXCursor(*Child, StmtParent, TU))) |
| 782 | return true; |
| 783 | } |
| 784 | |
| 785 | return false; |
| 786 | } |
| 787 | |
| 788 | bool CursorVisitor::VisitDeclStmt(DeclStmt *S) { |
| 789 | for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); |
| 790 | D != DEnd; ++D) { |
| 791 | if (Visit(MakeCXCursor(*D, TU))) |
| 792 | return true; |
| 793 | } |
| 794 | |
| 795 | return false; |
| 796 | } |
| 797 | |
Douglas Gregor | f5bab41 | 2010-01-22 01:00:11 +0000 | [diff] [blame] | 798 | bool CursorVisitor::VisitIfStmt(IfStmt *S) { |
| 799 | if (VarDecl *Var = S->getConditionVariable()) { |
| 800 | if (Visit(MakeCXCursor(Var, TU))) |
| 801 | return true; |
| 802 | } else if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU))) |
| 803 | return true; |
| 804 | |
| 805 | if (S->getThen() && Visit(MakeCXCursor(S->getThen(), StmtParent, TU))) |
| 806 | return true; |
| 807 | |
| 808 | if (S->getElse() && Visit(MakeCXCursor(S->getElse(), StmtParent, TU))) |
| 809 | return true; |
| 810 | |
| 811 | return false; |
| 812 | } |
| 813 | |
| 814 | bool CursorVisitor::VisitSwitchStmt(SwitchStmt *S) { |
| 815 | if (VarDecl *Var = S->getConditionVariable()) { |
| 816 | if (Visit(MakeCXCursor(Var, TU))) |
| 817 | return true; |
| 818 | } else if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU))) |
| 819 | return true; |
| 820 | |
| 821 | if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU))) |
| 822 | return true; |
| 823 | |
| 824 | return false; |
| 825 | } |
| 826 | |
Daniel Dunbar | 140fce2 | 2010-01-12 02:34:07 +0000 | [diff] [blame] | 827 | CXString CIndexer::createCXString(const char *String, bool DupString){ |
Benjamin Kramer | 62cf322 | 2009-11-09 19:13:48 +0000 | [diff] [blame] | 828 | CXString Str; |
| 829 | if (DupString) { |
| 830 | Str.Spelling = strdup(String); |
| 831 | Str.MustFreeString = 1; |
| 832 | } else { |
| 833 | Str.Spelling = String; |
| 834 | Str.MustFreeString = 0; |
| 835 | } |
| 836 | return Str; |
| 837 | } |
| 838 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 839 | extern "C" { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 840 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 841 | int displayDiagnostics) { |
Douglas Gregor | a030b7c | 2010-01-22 20:35:53 +0000 | [diff] [blame] | 842 | CIndexer *CIdxr = new CIndexer(); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 843 | if (excludeDeclarationsFromPCH) |
| 844 | CIdxr->setOnlyLocalDecls(); |
| 845 | if (displayDiagnostics) |
| 846 | CIdxr->setDisplayDiagnostics(); |
| 847 | return CIdxr; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 850 | void clang_disposeIndex(CXIndex CIdx) { |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 851 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 852 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 855 | void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) { |
| 856 | assert(CIdx && "Passed null CXIndex"); |
| 857 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 858 | CXXIdx->setUseExternalASTGeneration(value); |
| 859 | } |
| 860 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 861 | // FIXME: need to pass back error info. |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 862 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
| 863 | const char *ast_filename) { |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 864 | assert(CIdx && "Passed null CXIndex"); |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 865 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 866 | |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 867 | return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(), |
| 868 | CXXIdx->getOnlyLocalDecls(), |
| 869 | /* UseBumpAllocator = */ true); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 872 | CXTranslationUnit |
| 873 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 874 | const char *source_filename, |
| 875 | int num_command_line_args, |
| 876 | const char **command_line_args) { |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 877 | assert(CIdx && "Passed null CXIndex"); |
| 878 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 879 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 880 | if (!CXXIdx->getUseExternalASTGeneration()) { |
| 881 | llvm::SmallVector<const char *, 16> Args; |
| 882 | |
| 883 | // The 'source_filename' argument is optional. If the caller does not |
| 884 | // specify it then it is assumed that the source file is specified |
| 885 | // in the actual argument list. |
| 886 | if (source_filename) |
| 887 | Args.push_back(source_filename); |
| 888 | Args.insert(Args.end(), command_line_args, |
| 889 | command_line_args + num_command_line_args); |
| 890 | |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 891 | unsigned NumErrors = CXXIdx->getDiags().getNumErrors(); |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 892 | |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 893 | #ifdef USE_CRASHTRACER |
| 894 | ArgsCrashTracerInfo ACTI(Args); |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 895 | #endif |
| 896 | |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 897 | llvm::OwningPtr<ASTUnit> Unit( |
| 898 | ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(), |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 899 | CXXIdx->getDiags(), |
| 900 | CXXIdx->getClangResourcesPath(), |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 901 | CXXIdx->getOnlyLocalDecls(), |
| 902 | /* UseBumpAllocator = */ true)); |
Ted Kremenek | 29b7284 | 2010-01-07 22:49:05 +0000 | [diff] [blame] | 903 | |
Daniel Dunbar | 9422097 | 2009-12-05 02:17:18 +0000 | [diff] [blame] | 904 | // FIXME: Until we have broader testing, just drop the entire AST if we |
| 905 | // encountered an error. |
| 906 | if (NumErrors != CXXIdx->getDiags().getNumErrors()) |
| 907 | return 0; |
| 908 | |
| 909 | return Unit.take(); |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 912 | // Build up the arguments for invoking 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 913 | std::vector<const char *> argv; |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 914 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 915 | // First add the complete path to the 'clang' executable. |
| 916 | llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath(); |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 917 | argv.push_back(ClangPath.c_str()); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 918 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 919 | // Add the '-emit-ast' option as our execution mode for 'clang'. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 920 | argv.push_back("-emit-ast"); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 921 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 922 | // The 'source_filename' argument is optional. If the caller does not |
| 923 | // specify it then it is assumed that the source file is specified |
| 924 | // in the actual argument list. |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 925 | if (source_filename) |
| 926 | argv.push_back(source_filename); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 927 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 928 | // Generate a temporary name for the AST file. |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 929 | argv.push_back("-o"); |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 930 | char astTmpFile[L_tmpnam]; |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 931 | argv.push_back(tmpnam(astTmpFile)); |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 932 | |
| 933 | // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. |
| 934 | for (int i = 0; i < num_command_line_args; ++i) |
| 935 | if (const char *arg = command_line_args[i]) { |
| 936 | if (strcmp(arg, "-o") == 0) { |
| 937 | ++i; // Also skip the matching argument. |
| 938 | continue; |
| 939 | } |
| 940 | if (strcmp(arg, "-emit-ast") == 0 || |
| 941 | strcmp(arg, "-c") == 0 || |
| 942 | strcmp(arg, "-fsyntax-only") == 0) { |
| 943 | continue; |
| 944 | } |
| 945 | |
| 946 | // Keep the argument. |
| 947 | argv.push_back(arg); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 948 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 949 | |
Ted Kremenek | 139ba86 | 2009-10-22 00:03:57 +0000 | [diff] [blame] | 950 | // Add the null terminator. |
Ted Kremenek | 74cd069 | 2009-10-15 23:21:22 +0000 | [diff] [blame] | 951 | argv.push_back(NULL); |
| 952 | |
Ted Kremenek | feb15e3 | 2009-10-26 22:14:08 +0000 | [diff] [blame] | 953 | // Invoke 'clang'. |
| 954 | llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null |
| 955 | // on Unix or NUL (Windows). |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 956 | std::string ErrMsg; |
Ted Kremenek | c46e463 | 2009-10-19 22:27:32 +0000 | [diff] [blame] | 957 | const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL }; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 958 | llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, |
| 959 | /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL, |
| 960 | /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 961 | |
Ted Kremenek | 0854d70 | 2009-11-10 19:18:52 +0000 | [diff] [blame] | 962 | if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) { |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 963 | llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 964 | << '\n' << "Arguments: \n"; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 965 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
Ted Kremenek | 779e5f4 | 2009-10-26 22:08:39 +0000 | [diff] [blame] | 966 | I!=E; ++I) { |
| 967 | if (*I) |
| 968 | llvm::errs() << ' ' << *I << '\n'; |
| 969 | } |
| 970 | llvm::errs() << '\n'; |
Ted Kremenek | 379afec | 2009-10-22 03:24:01 +0000 | [diff] [blame] | 971 | } |
Benjamin Kramer | 0829a83 | 2009-10-18 11:19:36 +0000 | [diff] [blame] | 972 | |
Steve Naroff | 37b5ac2 | 2009-10-15 20:50:09 +0000 | [diff] [blame] | 973 | // Finally, we create the translation unit from the ast file. |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 974 | ASTUnit *ATU = static_cast<ASTUnit *>( |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 975 | clang_createTranslationUnit(CIdx, astTmpFile)); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 976 | if (ATU) |
| 977 | ATU->unlinkTemporaryFile(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 978 | return ATU; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 981 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 982 | assert(CTUnit && "Passed null CXTranslationUnit"); |
| 983 | delete static_cast<ASTUnit *>(CTUnit); |
| 984 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 985 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 986 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 987 | assert(CTUnit && "Passed null CXTranslationUnit"); |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 988 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 989 | return CIndexer::createCXString(CXXUnit->getOriginalSourceFileName().c_str(), |
| 990 | true); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 991 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 992 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 993 | CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 994 | CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } }; |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 995 | return Result; |
| 996 | } |
| 997 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 998 | } // end: extern "C" |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 999 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 1000 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1001 | // CXSourceLocation and CXSourceRange Operations. |
| 1002 | //===----------------------------------------------------------------------===// |
| 1003 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1004 | extern "C" { |
| 1005 | CXSourceLocation clang_getNullLocation() { |
| 1006 | CXSourceLocation Result = { 0, 0 }; |
| 1007 | return Result; |
| 1008 | } |
| 1009 | |
| 1010 | unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) { |
| 1011 | return loc1.ptr_data == loc2.ptr_data && loc1.int_data == loc2.int_data; |
| 1012 | } |
| 1013 | |
| 1014 | CXSourceLocation clang_getLocation(CXTranslationUnit tu, |
| 1015 | CXFile file, |
| 1016 | unsigned line, |
| 1017 | unsigned column) { |
| 1018 | if (!tu) |
| 1019 | return clang_getNullLocation(); |
| 1020 | |
| 1021 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu); |
| 1022 | SourceLocation SLoc |
| 1023 | = CXXUnit->getSourceManager().getLocation( |
| 1024 | static_cast<const FileEntry *>(file), |
| 1025 | line, column); |
| 1026 | |
| 1027 | return translateSourceLocation(CXXUnit->getASTContext(), SLoc, false); |
| 1028 | } |
| 1029 | |
| 1030 | CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) { |
| 1031 | if (begin.ptr_data != end.ptr_data) { |
| 1032 | CXSourceRange Result = { 0, 0, 0 }; |
| 1033 | return Result; |
| 1034 | } |
| 1035 | |
| 1036 | CXSourceRange Result = { begin.ptr_data, begin.int_data, end.int_data }; |
| 1037 | return Result; |
| 1038 | } |
| 1039 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1040 | void clang_getInstantiationLocation(CXSourceLocation location, |
| 1041 | CXFile *file, |
| 1042 | unsigned *line, |
| 1043 | unsigned *column) { |
| 1044 | CXSourceLocationPtr Ptr |
| 1045 | = CXSourceLocationPtr::getFromOpaqueValue(location.ptr_data); |
| 1046 | SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); |
| 1047 | |
| 1048 | if (!Ptr.getPointer() || Loc.isInvalid()) { |
| 1049 | if (file) |
| 1050 | *file = 0; |
| 1051 | if (line) |
| 1052 | *line = 0; |
| 1053 | if (column) |
| 1054 | *column = 0; |
| 1055 | return; |
| 1056 | } |
| 1057 | |
| 1058 | // FIXME: This is largely copy-paste from |
| 1059 | ///TextDiagnosticPrinter::HighlightRange. When it is clear that this is |
| 1060 | // what we want the two routines should be refactored. |
| 1061 | ASTContext &Context = *Ptr.getPointer(); |
| 1062 | SourceManager &SM = Context.getSourceManager(); |
| 1063 | SourceLocation InstLoc = SM.getInstantiationLoc(Loc); |
| 1064 | |
| 1065 | if (Ptr.getInt()) { |
| 1066 | // We want the last character in this location, so we will adjust |
| 1067 | // the instantiation location accordingly. |
| 1068 | |
| 1069 | // If the location is from a macro instantiation, get the end of |
| 1070 | // the instantiation range. |
| 1071 | if (Loc.isMacroID()) |
| 1072 | InstLoc = SM.getInstantiationRange(Loc).second; |
| 1073 | |
| 1074 | // Measure the length token we're pointing at, so we can adjust |
| 1075 | // the physical location in the file to point at the last |
| 1076 | // character. |
| 1077 | // FIXME: This won't cope with trigraphs or escaped newlines |
| 1078 | // well. For that, we actually need a preprocessor, which isn't |
| 1079 | // currently available here. Eventually, we'll switch the pointer |
| 1080 | // data of CXSourceLocation/CXSourceRange to a translation unit |
| 1081 | // (CXXUnit), so that the preprocessor will be available here. At |
| 1082 | // that point, we can use Preprocessor::getLocForEndOfToken(). |
| 1083 | unsigned Length = Lexer::MeasureTokenLength(InstLoc, SM, |
| 1084 | Context.getLangOptions()); |
| 1085 | if (Length > 0) |
| 1086 | InstLoc = InstLoc.getFileLocWithOffset(Length - 1); |
| 1087 | } |
| 1088 | |
| 1089 | if (file) |
| 1090 | *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc)); |
| 1091 | if (line) |
| 1092 | *line = SM.getInstantiationLineNumber(InstLoc); |
| 1093 | if (column) |
| 1094 | *column = SM.getInstantiationColumnNumber(InstLoc); |
| 1095 | } |
| 1096 | |
| 1097 | CXSourceLocation clang_getRangeStart(CXSourceRange range) { |
| 1098 | CXSourceLocation Result = { range.ptr_data, range.begin_int_data }; |
| 1099 | return Result; |
| 1100 | } |
| 1101 | |
| 1102 | CXSourceLocation clang_getRangeEnd(CXSourceRange range) { |
| 1103 | llvm::PointerIntPair<ASTContext *, 1, bool> Ptr; |
| 1104 | Ptr.setPointer(static_cast<ASTContext *>(range.ptr_data)); |
| 1105 | Ptr.setInt(true); |
| 1106 | CXSourceLocation Result = { Ptr.getOpaqueValue(), range.end_int_data }; |
| 1107 | return Result; |
| 1108 | } |
| 1109 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1110 | } // end: extern "C" |
| 1111 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1112 | //===----------------------------------------------------------------------===// |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 1113 | // CXFile Operations. |
| 1114 | //===----------------------------------------------------------------------===// |
| 1115 | |
| 1116 | extern "C" { |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 1117 | const char *clang_getFileName(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1118 | if (!SFile) |
| 1119 | return 0; |
| 1120 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 1121 | assert(SFile && "Passed null CXFile"); |
| 1122 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 1123 | return FEnt->getName(); |
| 1124 | } |
| 1125 | |
| 1126 | time_t clang_getFileTime(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1127 | if (!SFile) |
| 1128 | return 0; |
| 1129 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 1130 | assert(SFile && "Passed null CXFile"); |
| 1131 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 1132 | return FEnt->getModificationTime(); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 1133 | } |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1134 | |
| 1135 | CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) { |
| 1136 | if (!tu) |
| 1137 | return 0; |
| 1138 | |
| 1139 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu); |
| 1140 | |
| 1141 | FileManager &FMgr = CXXUnit->getFileManager(); |
| 1142 | const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name)); |
| 1143 | return const_cast<FileEntry *>(File); |
| 1144 | } |
| 1145 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 1146 | } // end: extern "C" |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 1147 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 1148 | //===----------------------------------------------------------------------===// |
| 1149 | // CXCursor Operations. |
| 1150 | //===----------------------------------------------------------------------===// |
| 1151 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 1152 | static Decl *getDeclFromExpr(Stmt *E) { |
| 1153 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 1154 | return RefExpr->getDecl(); |
| 1155 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 1156 | return ME->getMemberDecl(); |
| 1157 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 1158 | return RE->getDecl(); |
| 1159 | |
| 1160 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 1161 | return getDeclFromExpr(CE->getCallee()); |
| 1162 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) |
| 1163 | return getDeclFromExpr(CE->getSubExpr()); |
| 1164 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 1165 | return OME->getMethodDecl(); |
| 1166 | |
| 1167 | return 0; |
| 1168 | } |
| 1169 | |
| 1170 | extern "C" { |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1171 | |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1172 | unsigned clang_visitChildren(CXCursor parent, |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1173 | CXCursorVisitor visitor, |
| 1174 | CXClientData client_data) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1175 | ASTUnit *CXXUnit = getCursorASTUnit(parent); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1176 | |
| 1177 | unsigned PCHLevel = Decl::MaxPCHLevel; |
| 1178 | |
| 1179 | // Set the PCHLevel to filter out unwanted decls if requested. |
| 1180 | if (CXXUnit->getOnlyLocalDecls()) { |
| 1181 | PCHLevel = 0; |
| 1182 | |
| 1183 | // If the main input was an AST, bump the level. |
| 1184 | if (CXXUnit->isMainFileAST()) |
| 1185 | ++PCHLevel; |
| 1186 | } |
| 1187 | |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1188 | CursorVisitor CursorVis(CXXUnit, visitor, client_data, PCHLevel); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1189 | return CursorVis.VisitChildren(parent); |
| 1190 | } |
| 1191 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 1192 | static CXString getDeclSpelling(Decl *D) { |
| 1193 | NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D); |
| 1194 | if (!ND) |
| 1195 | return CIndexer::createCXString(""); |
| 1196 | |
| 1197 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
| 1198 | return CIndexer::createCXString(OMD->getSelector().getAsString().c_str(), |
| 1199 | true); |
| 1200 | |
| 1201 | if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
| 1202 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 1203 | // and returns different names. NamedDecl returns the class name and |
| 1204 | // ObjCCategoryImplDecl returns the category name. |
| 1205 | return CIndexer::createCXString(CIMP->getIdentifier()->getNameStart()); |
| 1206 | |
| 1207 | if (ND->getIdentifier()) |
| 1208 | return CIndexer::createCXString(ND->getIdentifier()->getNameStart()); |
| 1209 | |
| 1210 | return CIndexer::createCXString(""); |
| 1211 | } |
| 1212 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1213 | CXString clang_getCursorSpelling(CXCursor C) { |
Daniel Dunbar | c81d718 | 2010-01-18 17:52:42 +0000 | [diff] [blame] | 1214 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 1215 | if (clang_isTranslationUnit(C.kind)) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1216 | return clang_getTranslationUnitSpelling(C.data[2]); |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 1217 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 1218 | if (clang_isReference(C.kind)) { |
| 1219 | switch (C.kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 1220 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 1221 | ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
| 1222 | return CIndexer::createCXString(Super->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 1223 | } |
| 1224 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 1225 | ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
| 1226 | return CIndexer::createCXString(Class->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 1227 | } |
| 1228 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 1229 | ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 1230 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 1231 | return CIndexer::createCXString(OID->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 1232 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1233 | case CXCursor_TypeRef: { |
| 1234 | TypeDecl *Type = getCursorTypeRef(C).first; |
| 1235 | assert(Type && "Missing type decl"); |
| 1236 | |
| 1237 | return CIndexer::createCXString( |
| 1238 | getCursorContext(C).getTypeDeclType(Type).getAsString().c_str(), |
| 1239 | true); |
| 1240 | } |
| 1241 | |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 1242 | default: |
Ted Kremenek | 4b333d2 | 2010-01-12 00:36:38 +0000 | [diff] [blame] | 1243 | return CIndexer::createCXString("<not implemented>"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 1244 | } |
| 1245 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 1246 | |
| 1247 | if (clang_isExpression(C.kind)) { |
| 1248 | Decl *D = getDeclFromExpr(getCursorExpr(C)); |
| 1249 | if (D) |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 1250 | return getDeclSpelling(D); |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 1251 | return CIndexer::createCXString(""); |
| 1252 | } |
| 1253 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 1254 | return getDeclSpelling(getCursorDecl(C)); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1257 | const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 1258 | switch (Kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 1259 | case CXCursor_FunctionDecl: return "FunctionDecl"; |
| 1260 | case CXCursor_TypedefDecl: return "TypedefDecl"; |
| 1261 | case CXCursor_EnumDecl: return "EnumDecl"; |
| 1262 | case CXCursor_EnumConstantDecl: return "EnumConstantDecl"; |
| 1263 | case CXCursor_StructDecl: return "StructDecl"; |
| 1264 | case CXCursor_UnionDecl: return "UnionDecl"; |
| 1265 | case CXCursor_ClassDecl: return "ClassDecl"; |
| 1266 | case CXCursor_FieldDecl: return "FieldDecl"; |
| 1267 | case CXCursor_VarDecl: return "VarDecl"; |
| 1268 | case CXCursor_ParmDecl: return "ParmDecl"; |
| 1269 | case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl"; |
| 1270 | case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl"; |
| 1271 | case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl"; |
| 1272 | case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl"; |
| 1273 | case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl"; |
| 1274 | case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl"; |
| 1275 | case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl"; |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1276 | case CXCursor_ObjCImplementationDecl: return "ObjCImplementationDecl"; |
| 1277 | case CXCursor_ObjCCategoryImplDecl: return "ObjCCategoryImplDecl"; |
Douglas Gregor | 3012213 | 2010-01-19 22:07:56 +0000 | [diff] [blame] | 1278 | case CXCursor_UnexposedDecl: return "UnexposedDecl"; |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 1279 | case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; |
| 1280 | case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef"; |
| 1281 | case CXCursor_ObjCClassRef: return "ObjCClassRef"; |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1282 | case CXCursor_TypeRef: return "TypeRef"; |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 1283 | case CXCursor_UnexposedExpr: return "UnexposedExpr"; |
| 1284 | case CXCursor_DeclRefExpr: return "DeclRefExpr"; |
| 1285 | case CXCursor_MemberRefExpr: return "MemberRefExpr"; |
| 1286 | case CXCursor_CallExpr: return "CallExpr"; |
| 1287 | case CXCursor_ObjCMessageExpr: return "ObjCMessageExpr"; |
| 1288 | case CXCursor_UnexposedStmt: return "UnexposedStmt"; |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 1289 | case CXCursor_InvalidFile: return "InvalidFile"; |
| 1290 | case CXCursor_NoDeclFound: return "NoDeclFound"; |
| 1291 | case CXCursor_NotImplemented: return "NotImplemented"; |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 1292 | case CXCursor_TranslationUnit: return "TranslationUnit"; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 1293 | } |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 1294 | |
| 1295 | llvm_unreachable("Unhandled CXCursorKind"); |
| 1296 | return NULL; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 1297 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 1298 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 1299 | enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, |
| 1300 | CXCursor parent, |
| 1301 | CXClientData client_data) { |
| 1302 | CXCursor *BestCursor = static_cast<CXCursor *>(client_data); |
| 1303 | *BestCursor = cursor; |
| 1304 | return CXChildVisit_Recurse; |
| 1305 | } |
| 1306 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1307 | CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { |
| 1308 | if (!TU) |
Ted Kremenek | f462989 | 2010-01-14 01:51:23 +0000 | [diff] [blame] | 1309 | return clang_getNullCursor(); |
Ted Kremenek | f462989 | 2010-01-14 01:51:23 +0000 | [diff] [blame] | 1310 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1311 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU); |
| 1312 | |
| 1313 | SourceLocation SLoc = translateSourceLocation(Loc); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 1314 | CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 1315 | if (SLoc.isValid()) { |
| 1316 | SourceRange RegionOfInterest(SLoc, |
| 1317 | CXXUnit->getPreprocessor().getLocForEndOfToken(SLoc, 1)); |
| 1318 | |
| 1319 | // FIXME: Would be great to have a "hint" cursor, then walk from that |
| 1320 | // hint cursor upward until we find a cursor whose source range encloses |
| 1321 | // the region of interest, rather than starting from the translation unit. |
| 1322 | CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit); |
| 1323 | CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result, |
| 1324 | Decl::MaxPCHLevel, RegionOfInterest); |
| 1325 | CursorVis.VisitChildren(Parent); |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 1326 | } |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 1327 | return Result; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 1330 | CXCursor clang_getNullCursor(void) { |
Douglas Gregor | 5bfb8c1 | 2010-01-20 23:34:41 +0000 | [diff] [blame] | 1331 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 1335 | return X == Y; |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 1336 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1337 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1338 | unsigned clang_isInvalid(enum CXCursorKind K) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 1339 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 1340 | } |
| 1341 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1342 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 1343 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 1344 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 1345 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1346 | unsigned clang_isReference(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 1347 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 1348 | } |
| 1349 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 1350 | unsigned clang_isExpression(enum CXCursorKind K) { |
| 1351 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; |
| 1352 | } |
| 1353 | |
| 1354 | unsigned clang_isStatement(enum CXCursorKind K) { |
| 1355 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; |
| 1356 | } |
| 1357 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 1358 | unsigned clang_isTranslationUnit(enum CXCursorKind K) { |
| 1359 | return K == CXCursor_TranslationUnit; |
| 1360 | } |
| 1361 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1362 | CXCursorKind clang_getCursorKind(CXCursor C) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 1363 | return C.kind; |
| 1364 | } |
| 1365 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 1366 | static SourceLocation getLocationFromExpr(Expr *E) { |
| 1367 | if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
| 1368 | return /*FIXME:*/Msg->getLeftLoc(); |
| 1369 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 1370 | return DRE->getLocation(); |
| 1371 | if (MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
| 1372 | return Member->getMemberLoc(); |
| 1373 | if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
| 1374 | return Ivar->getLocation(); |
| 1375 | return E->getLocStart(); |
| 1376 | } |
| 1377 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1378 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 1379 | if (clang_isReference(C.kind)) { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 1380 | switch (C.kind) { |
| 1381 | case CXCursor_ObjCSuperClassRef: { |
| 1382 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 1383 | = getCursorObjCSuperClassRef(C); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1384 | return translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | case CXCursor_ObjCProtocolRef: { |
| 1388 | std::pair<ObjCProtocolDecl *, SourceLocation> P |
| 1389 | = getCursorObjCProtocolRef(C); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1390 | return translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | case CXCursor_ObjCClassRef: { |
| 1394 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 1395 | = getCursorObjCClassRef(C); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1396 | return translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 1397 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1398 | |
| 1399 | case CXCursor_TypeRef: { |
| 1400 | std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C); |
| 1401 | return translateSourceLocation(P.first->getASTContext(), P.second); |
| 1402 | } |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 1403 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 1404 | default: |
| 1405 | // FIXME: Need a way to enumerate all non-reference cases. |
| 1406 | llvm_unreachable("Missed a reference kind"); |
| 1407 | } |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1408 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 1409 | |
| 1410 | if (clang_isExpression(C.kind)) |
| 1411 | return translateSourceLocation(getCursorContext(C), |
| 1412 | getLocationFromExpr(getCursorExpr(C))); |
| 1413 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1414 | if (!getCursorDecl(C)) { |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1415 | CXSourceLocation empty = { 0, 0 }; |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1416 | return empty; |
| 1417 | } |
| 1418 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 1419 | Decl *D = getCursorDecl(C); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 1420 | SourceLocation Loc = D->getLocation(); |
| 1421 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D)) |
| 1422 | Loc = Class->getClassLoc(); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1423 | return translateSourceLocation(D->getASTContext(), Loc); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1424 | } |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 1425 | |
| 1426 | CXSourceRange clang_getCursorExtent(CXCursor C) { |
| 1427 | if (clang_isReference(C.kind)) { |
| 1428 | switch (C.kind) { |
| 1429 | case CXCursor_ObjCSuperClassRef: { |
| 1430 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 1431 | = getCursorObjCSuperClassRef(C); |
| 1432 | return translateSourceRange(P.first->getASTContext(), P.second); |
| 1433 | } |
| 1434 | |
| 1435 | case CXCursor_ObjCProtocolRef: { |
| 1436 | std::pair<ObjCProtocolDecl *, SourceLocation> P |
| 1437 | = getCursorObjCProtocolRef(C); |
| 1438 | return translateSourceRange(P.first->getASTContext(), P.second); |
| 1439 | } |
| 1440 | |
| 1441 | case CXCursor_ObjCClassRef: { |
| 1442 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 1443 | = getCursorObjCClassRef(C); |
| 1444 | |
| 1445 | return translateSourceRange(P.first->getASTContext(), P.second); |
| 1446 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1447 | |
| 1448 | case CXCursor_TypeRef: { |
| 1449 | std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C); |
| 1450 | return translateSourceRange(P.first->getASTContext(), P.second); |
| 1451 | } |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 1452 | |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 1453 | default: |
| 1454 | // FIXME: Need a way to enumerate all non-reference cases. |
| 1455 | llvm_unreachable("Missed a reference kind"); |
| 1456 | } |
| 1457 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 1458 | |
| 1459 | if (clang_isExpression(C.kind)) |
| 1460 | return translateSourceRange(getCursorContext(C), |
| 1461 | getCursorExpr(C)->getSourceRange()); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 1462 | |
| 1463 | if (clang_isStatement(C.kind)) |
| 1464 | return translateSourceRange(getCursorContext(C), |
| 1465 | getCursorStmt(C)->getSourceRange()); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 1466 | |
| 1467 | if (!getCursorDecl(C)) { |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1468 | CXSourceRange empty = { 0, 0, 0 }; |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 1469 | return empty; |
| 1470 | } |
| 1471 | |
| 1472 | Decl *D = getCursorDecl(C); |
| 1473 | return translateSourceRange(D->getASTContext(), D->getSourceRange()); |
| 1474 | } |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 1475 | |
| 1476 | CXCursor clang_getCursorReferenced(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1477 | if (clang_isInvalid(C.kind)) |
| 1478 | return clang_getNullCursor(); |
| 1479 | |
| 1480 | ASTUnit *CXXUnit = getCursorASTUnit(C); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1481 | if (clang_isDeclaration(C.kind)) |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 1482 | return C; |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1483 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 1484 | if (clang_isExpression(C.kind)) { |
| 1485 | Decl *D = getDeclFromExpr(getCursorExpr(C)); |
| 1486 | if (D) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1487 | return MakeCXCursor(D, CXXUnit); |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 1488 | return clang_getNullCursor(); |
| 1489 | } |
| 1490 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 1491 | if (!clang_isReference(C.kind)) |
| 1492 | return clang_getNullCursor(); |
| 1493 | |
| 1494 | switch (C.kind) { |
| 1495 | case CXCursor_ObjCSuperClassRef: |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1496 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit); |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 1497 | |
| 1498 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1499 | return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit); |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 1500 | |
| 1501 | case CXCursor_ObjCClassRef: |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1502 | return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1503 | |
| 1504 | case CXCursor_TypeRef: |
| 1505 | return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit); |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 1506 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 1507 | default: |
| 1508 | // We would prefer to enumerate all non-reference cursor kinds here. |
| 1509 | llvm_unreachable("Unhandled reference cursor kind"); |
| 1510 | break; |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | return clang_getNullCursor(); |
| 1515 | } |
| 1516 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1517 | CXCursor clang_getCursorDefinition(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1518 | if (clang_isInvalid(C.kind)) |
| 1519 | return clang_getNullCursor(); |
| 1520 | |
| 1521 | ASTUnit *CXXUnit = getCursorASTUnit(C); |
| 1522 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1523 | bool WasReference = false; |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 1524 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1525 | C = clang_getCursorReferenced(C); |
| 1526 | WasReference = true; |
| 1527 | } |
| 1528 | |
| 1529 | if (!clang_isDeclaration(C.kind)) |
| 1530 | return clang_getNullCursor(); |
| 1531 | |
| 1532 | Decl *D = getCursorDecl(C); |
| 1533 | if (!D) |
| 1534 | return clang_getNullCursor(); |
| 1535 | |
| 1536 | switch (D->getKind()) { |
| 1537 | // Declaration kinds that don't really separate the notions of |
| 1538 | // declaration and definition. |
| 1539 | case Decl::Namespace: |
| 1540 | case Decl::Typedef: |
| 1541 | case Decl::TemplateTypeParm: |
| 1542 | case Decl::EnumConstant: |
| 1543 | case Decl::Field: |
| 1544 | case Decl::ObjCIvar: |
| 1545 | case Decl::ObjCAtDefsField: |
| 1546 | case Decl::ImplicitParam: |
| 1547 | case Decl::ParmVar: |
| 1548 | case Decl::NonTypeTemplateParm: |
| 1549 | case Decl::TemplateTemplateParm: |
| 1550 | case Decl::ObjCCategoryImpl: |
| 1551 | case Decl::ObjCImplementation: |
| 1552 | case Decl::LinkageSpec: |
| 1553 | case Decl::ObjCPropertyImpl: |
| 1554 | case Decl::FileScopeAsm: |
| 1555 | case Decl::StaticAssert: |
| 1556 | case Decl::Block: |
| 1557 | return C; |
| 1558 | |
| 1559 | // Declaration kinds that don't make any sense here, but are |
| 1560 | // nonetheless harmless. |
| 1561 | case Decl::TranslationUnit: |
| 1562 | case Decl::Template: |
| 1563 | case Decl::ObjCContainer: |
| 1564 | break; |
| 1565 | |
| 1566 | // Declaration kinds for which the definition is not resolvable. |
| 1567 | case Decl::UnresolvedUsingTypename: |
| 1568 | case Decl::UnresolvedUsingValue: |
| 1569 | break; |
| 1570 | |
| 1571 | case Decl::UsingDirective: |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1572 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), |
| 1573 | CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1574 | |
| 1575 | case Decl::NamespaceAlias: |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1576 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1577 | |
| 1578 | case Decl::Enum: |
| 1579 | case Decl::Record: |
| 1580 | case Decl::CXXRecord: |
| 1581 | case Decl::ClassTemplateSpecialization: |
| 1582 | case Decl::ClassTemplatePartialSpecialization: |
| 1583 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition(D->getASTContext())) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1584 | return MakeCXCursor(Def, CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1585 | return clang_getNullCursor(); |
| 1586 | |
| 1587 | case Decl::Function: |
| 1588 | case Decl::CXXMethod: |
| 1589 | case Decl::CXXConstructor: |
| 1590 | case Decl::CXXDestructor: |
| 1591 | case Decl::CXXConversion: { |
| 1592 | const FunctionDecl *Def = 0; |
| 1593 | if (cast<FunctionDecl>(D)->getBody(Def)) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1594 | return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1595 | return clang_getNullCursor(); |
| 1596 | } |
| 1597 | |
| 1598 | case Decl::Var: { |
| 1599 | VarDecl *Var = cast<VarDecl>(D); |
| 1600 | |
| 1601 | // Variables with initializers have definitions. |
| 1602 | const VarDecl *Def = 0; |
| 1603 | if (Var->getDefinition(Def)) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1604 | return MakeCXCursor(const_cast<VarDecl *>(Def), CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1605 | |
| 1606 | // extern and private_extern variables are not definitions. |
| 1607 | if (Var->hasExternalStorage()) |
| 1608 | return clang_getNullCursor(); |
| 1609 | |
| 1610 | // In-line static data members do not have definitions. |
| 1611 | if (Var->isStaticDataMember() && !Var->isOutOfLine()) |
| 1612 | return clang_getNullCursor(); |
| 1613 | |
| 1614 | // All other variables are themselves definitions. |
| 1615 | return C; |
| 1616 | } |
| 1617 | |
| 1618 | case Decl::FunctionTemplate: { |
| 1619 | const FunctionDecl *Def = 0; |
| 1620 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1621 | return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1622 | return clang_getNullCursor(); |
| 1623 | } |
| 1624 | |
| 1625 | case Decl::ClassTemplate: { |
| 1626 | if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() |
| 1627 | ->getDefinition(D->getASTContext())) |
| 1628 | return MakeCXCursor( |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1629 | cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), |
| 1630 | CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1631 | return clang_getNullCursor(); |
| 1632 | } |
| 1633 | |
| 1634 | case Decl::Using: { |
| 1635 | UsingDecl *Using = cast<UsingDecl>(D); |
| 1636 | CXCursor Def = clang_getNullCursor(); |
| 1637 | for (UsingDecl::shadow_iterator S = Using->shadow_begin(), |
| 1638 | SEnd = Using->shadow_end(); |
| 1639 | S != SEnd; ++S) { |
| 1640 | if (Def != clang_getNullCursor()) { |
| 1641 | // FIXME: We have no way to return multiple results. |
| 1642 | return clang_getNullCursor(); |
| 1643 | } |
| 1644 | |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1645 | Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(), |
| 1646 | CXXUnit)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | return Def; |
| 1650 | } |
| 1651 | |
| 1652 | case Decl::UsingShadow: |
| 1653 | return clang_getCursorDefinition( |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1654 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), |
| 1655 | CXXUnit)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1656 | |
| 1657 | case Decl::ObjCMethod: { |
| 1658 | ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); |
| 1659 | if (Method->isThisDeclarationADefinition()) |
| 1660 | return C; |
| 1661 | |
| 1662 | // Dig out the method definition in the associated |
| 1663 | // @implementation, if we have it. |
| 1664 | // FIXME: The ASTs should make finding the definition easier. |
| 1665 | if (ObjCInterfaceDecl *Class |
| 1666 | = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) |
| 1667 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) |
| 1668 | if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), |
| 1669 | Method->isInstanceMethod())) |
| 1670 | if (Def->isThisDeclarationADefinition()) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1671 | return MakeCXCursor(Def, CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1672 | |
| 1673 | return clang_getNullCursor(); |
| 1674 | } |
| 1675 | |
| 1676 | case Decl::ObjCCategory: |
| 1677 | if (ObjCCategoryImplDecl *Impl |
| 1678 | = cast<ObjCCategoryDecl>(D)->getImplementation()) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1679 | return MakeCXCursor(Impl, CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1680 | return clang_getNullCursor(); |
| 1681 | |
| 1682 | case Decl::ObjCProtocol: |
| 1683 | if (!cast<ObjCProtocolDecl>(D)->isForwardDecl()) |
| 1684 | return C; |
| 1685 | return clang_getNullCursor(); |
| 1686 | |
| 1687 | case Decl::ObjCInterface: |
| 1688 | // There are two notions of a "definition" for an Objective-C |
| 1689 | // class: the interface and its implementation. When we resolved a |
| 1690 | // reference to an Objective-C class, produce the @interface as |
| 1691 | // the definition; when we were provided with the interface, |
| 1692 | // produce the @implementation as the definition. |
| 1693 | if (WasReference) { |
| 1694 | if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl()) |
| 1695 | return C; |
| 1696 | } else if (ObjCImplementationDecl *Impl |
| 1697 | = cast<ObjCInterfaceDecl>(D)->getImplementation()) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1698 | return MakeCXCursor(Impl, CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1699 | return clang_getNullCursor(); |
| 1700 | |
| 1701 | case Decl::ObjCProperty: |
| 1702 | // FIXME: We don't really know where to find the |
| 1703 | // ObjCPropertyImplDecls that implement this property. |
| 1704 | return clang_getNullCursor(); |
| 1705 | |
| 1706 | case Decl::ObjCCompatibleAlias: |
| 1707 | if (ObjCInterfaceDecl *Class |
| 1708 | = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) |
| 1709 | if (!Class->isForwardDecl()) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1710 | return MakeCXCursor(Class, CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1711 | |
| 1712 | return clang_getNullCursor(); |
| 1713 | |
| 1714 | case Decl::ObjCForwardProtocol: { |
| 1715 | ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D); |
| 1716 | if (Forward->protocol_size() == 1) |
| 1717 | return clang_getCursorDefinition( |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1718 | MakeCXCursor(*Forward->protocol_begin(), |
| 1719 | CXXUnit)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1720 | |
| 1721 | // FIXME: Cannot return multiple definitions. |
| 1722 | return clang_getNullCursor(); |
| 1723 | } |
| 1724 | |
| 1725 | case Decl::ObjCClass: { |
| 1726 | ObjCClassDecl *Class = cast<ObjCClassDecl>(D); |
| 1727 | if (Class->size() == 1) { |
| 1728 | ObjCInterfaceDecl *IFace = Class->begin()->getInterface(); |
| 1729 | if (!IFace->isForwardDecl()) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1730 | return MakeCXCursor(IFace, CXXUnit); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1731 | return clang_getNullCursor(); |
| 1732 | } |
| 1733 | |
| 1734 | // FIXME: Cannot return multiple definitions. |
| 1735 | return clang_getNullCursor(); |
| 1736 | } |
| 1737 | |
| 1738 | case Decl::Friend: |
| 1739 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1740 | return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1741 | return clang_getNullCursor(); |
| 1742 | |
| 1743 | case Decl::FriendTemplate: |
| 1744 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1745 | return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1746 | return clang_getNullCursor(); |
| 1747 | } |
| 1748 | |
| 1749 | return clang_getNullCursor(); |
| 1750 | } |
| 1751 | |
| 1752 | unsigned clang_isCursorDefinition(CXCursor C) { |
| 1753 | if (!clang_isDeclaration(C.kind)) |
| 1754 | return 0; |
| 1755 | |
| 1756 | return clang_getCursorDefinition(C) == C; |
| 1757 | } |
| 1758 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 1759 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1760 | const char **startBuf, |
| 1761 | const char **endBuf, |
| 1762 | unsigned *startLine, |
| 1763 | unsigned *startColumn, |
| 1764 | unsigned *endLine, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 1765 | unsigned *endColumn) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 1766 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
| 1767 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1768 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 1769 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 1770 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1771 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 1772 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 1773 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 1774 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 1775 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 1776 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 1777 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 1778 | } |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 1779 | |
| 1780 | } // end: extern "C" |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 1781 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 1782 | //===----------------------------------------------------------------------===// |
| 1783 | // CXString Operations. |
| 1784 | //===----------------------------------------------------------------------===// |
| 1785 | |
| 1786 | extern "C" { |
| 1787 | const char *clang_getCString(CXString string) { |
| 1788 | return string.Spelling; |
| 1789 | } |
| 1790 | |
| 1791 | void clang_disposeString(CXString string) { |
| 1792 | if (string.MustFreeString && string.Spelling) |
| 1793 | free((void*)string.Spelling); |
| 1794 | } |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 1795 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 1796 | } // end: extern "C" |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 1797 | |
| 1798 | //===----------------------------------------------------------------------===// |
| 1799 | // Misc. utility functions. |
| 1800 | //===----------------------------------------------------------------------===// |
| 1801 | |
| 1802 | extern "C" { |
| 1803 | |
| 1804 | const char *clang_getClangVersion() { |
| 1805 | return getClangFullVendorVersion(); |
| 1806 | } |
| 1807 | |
| 1808 | } // end: extern "C" |
| 1809 | |