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