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