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 | 0a90d32 | 2010-11-17 23:24:11 +0000 | [diff] [blame] | 17 | #include "CXTranslationUnit.h" |
Ted Kremenek | ed12273 | 2010-11-16 01:56:27 +0000 | [diff] [blame] | 18 | #include "CXString.h" |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 19 | #include "CXType.h" |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 20 | #include "CXSourceLocation.h" |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 21 | #include "CIndexDiagnostic.h" |
Argyrios Kyrtzidis | e397bf1 | 2011-11-03 19:02:34 +0000 | [diff] [blame] | 22 | #include "CursorVisitor.h" |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 23 | |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 24 | #include "clang/Basic/Version.h" |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 25 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 26 | #include "clang/AST/StmtVisitor.h" |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 27 | #include "clang/Basic/Diagnostic.h" |
| 28 | #include "clang/Frontend/ASTUnit.h" |
| 29 | #include "clang/Frontend/CompilerInstance.h" |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 30 | #include "clang/Frontend/FrontendDiagnostic.h" |
Ted Kremenek | d821065 | 2010-01-06 23:43:31 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Lexer.h" |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 32 | #include "clang/Lex/HeaderSearch.h" |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 33 | #include "clang/Lex/PreprocessingRecord.h" |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 34 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/STLExtras.h" |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/Optional.h" |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/StringSwitch.h" |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 38 | #include "clang/Analysis/Support/SaveAndRestore.h" |
Daniel Dunbar | c7df4f3 | 2010-08-18 18:43:14 +0000 | [diff] [blame] | 39 | #include "llvm/Support/CrashRecoveryContext.h" |
Daniel Dunbar | 48615ff | 2010-10-08 19:30:33 +0000 | [diff] [blame] | 40 | #include "llvm/Support/PrettyStackTrace.h" |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 41 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 42 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 7a07fcb | 2010-08-09 21:00:09 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Timer.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Mutex.h" |
| 45 | #include "llvm/Support/Program.h" |
| 46 | #include "llvm/Support/Signals.h" |
| 47 | #include "llvm/Support/Threading.h" |
Ted Kremenek | 37f1ea0 | 2010-11-15 23:11:54 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 49 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 50 | using namespace clang; |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 51 | using namespace clang::cxcursor; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 52 | using namespace clang::cxstring; |
Argyrios Kyrtzidis | 9049cf6 | 2011-10-12 07:07:33 +0000 | [diff] [blame] | 53 | using namespace clang::cxtu; |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 54 | |
Argyrios Kyrtzidis | 9049cf6 | 2011-10-12 07:07:33 +0000 | [diff] [blame] | 55 | CXTranslationUnit cxtu::MakeCXTranslationUnit(ASTUnit *TU) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 56 | if (!TU) |
| 57 | return 0; |
| 58 | CXTranslationUnit D = new CXTranslationUnitImpl(); |
| 59 | D->TUData = TU; |
| 60 | D->StringPool = createCXStringPool(); |
| 61 | return D; |
| 62 | } |
| 63 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 64 | cxtu::CXTUOwner::~CXTUOwner() { |
| 65 | if (TU) |
| 66 | clang_disposeTranslationUnit(TU); |
| 67 | } |
| 68 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 69 | /// \brief Compare two source ranges to determine their relative position in |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 70 | /// the translation unit. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 71 | static RangeComparisonResult RangeCompare(SourceManager &SM, |
| 72 | SourceRange R1, |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 73 | SourceRange R2) { |
| 74 | assert(R1.isValid() && "First range is invalid?"); |
| 75 | assert(R2.isValid() && "Second range is invalid?"); |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 76 | if (R1.getEnd() != R2.getBegin() && |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 77 | SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin())) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 78 | return RangeBefore; |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 79 | if (R2.getEnd() != R1.getBegin() && |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 80 | SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin())) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 81 | return RangeAfter; |
| 82 | return RangeOverlap; |
| 83 | } |
| 84 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 85 | /// \brief Determine if a source location falls within, before, or after a |
| 86 | /// a given source range. |
| 87 | static RangeComparisonResult LocationCompare(SourceManager &SM, |
| 88 | SourceLocation L, SourceRange R) { |
| 89 | assert(R.isValid() && "First range is invalid?"); |
| 90 | assert(L.isValid() && "Second range is invalid?"); |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 91 | if (L == R.getBegin() || L == R.getEnd()) |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 92 | return RangeOverlap; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 93 | if (SM.isBeforeInTranslationUnit(L, R.getBegin())) |
| 94 | return RangeBefore; |
| 95 | if (SM.isBeforeInTranslationUnit(R.getEnd(), L)) |
| 96 | return RangeAfter; |
| 97 | return RangeOverlap; |
| 98 | } |
| 99 | |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 100 | /// \brief Translate a Clang source range into a CIndex source range. |
| 101 | /// |
| 102 | /// Clang internally represents ranges where the end location points to the |
| 103 | /// start of the token at the end. However, for external clients it is more |
| 104 | /// useful to have a CXSourceRange be a proper half-open interval. This routine |
| 105 | /// does the appropriate translation. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 106 | CXSourceRange cxloc::translateSourceRange(const SourceManager &SM, |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 107 | const LangOptions &LangOpts, |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 108 | const CharSourceRange &R) { |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 109 | // We want the last character in this location, so we will adjust the |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 110 | // location accordingly. |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 111 | SourceLocation EndLoc = R.getEnd(); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 112 | if (EndLoc.isValid() && EndLoc.isMacroID()) |
Chandler Carruth | edc3dcc | 2011-07-25 16:56:02 +0000 | [diff] [blame] | 113 | EndLoc = SM.getExpansionRange(EndLoc).second; |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 114 | if (R.isTokenRange() && !EndLoc.isInvalid() && EndLoc.isFileID()) { |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 115 | unsigned Length = Lexer::MeasureTokenLength(EndLoc, SM, LangOpts); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 116 | EndLoc = EndLoc.getLocWithOffset(Length); |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts }, |
| 120 | R.getBegin().getRawEncoding(), |
| 121 | EndLoc.getRawEncoding() }; |
| 122 | return Result; |
| 123 | } |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 124 | |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 125 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 126 | // Cursor visitor. |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 127 | //===----------------------------------------------------------------------===// |
| 128 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 129 | static SourceRange getRawCursorExtent(CXCursor C); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 130 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr); |
| 131 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 132 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 133 | RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 134 | return RangeCompare(AU->getSourceManager(), R, RegionOfInterest); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 137 | /// \brief Visit the given cursor and, if requested by the visitor, |
| 138 | /// its children. |
| 139 | /// |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 140 | /// \param Cursor the cursor to visit. |
| 141 | /// |
| 142 | /// \param CheckRegionOfInterest if true, then the caller already checked that |
| 143 | /// this cursor is within the region of interest. |
| 144 | /// |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 145 | /// \returns true if the visitation should be aborted, false if it |
| 146 | /// should continue. |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 147 | bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 148 | if (clang_isInvalid(Cursor.kind)) |
| 149 | return false; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 150 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 151 | if (clang_isDeclaration(Cursor.kind)) { |
| 152 | Decl *D = getCursorDecl(Cursor); |
| 153 | assert(D && "Invalid declaration cursor"); |
Argyrios Kyrtzidis | 65ab907 | 2011-09-26 19:05:37 +0000 | [diff] [blame] | 154 | // Ignore implicit declarations, unless it's an objc method because |
| 155 | // currently we should report implicit methods for properties when indexing. |
| 156 | if (D->isImplicit() && !isa<ObjCMethodDecl>(D)) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 160 | // If we have a range of interest, and this cursor doesn't intersect with it, |
| 161 | // we're done. |
| 162 | if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 163 | SourceRange Range = getRawCursorExtent(Cursor); |
Daniel Dunbar | f408f32 | 2010-02-14 08:32:05 +0000 | [diff] [blame] | 164 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 165 | return false; |
| 166 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 167 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 168 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 169 | case CXChildVisit_Break: |
| 170 | return true; |
| 171 | |
| 172 | case CXChildVisit_Continue: |
| 173 | return false; |
| 174 | |
| 175 | case CXChildVisit_Recurse: |
| 176 | return VisitChildren(Cursor); |
| 177 | } |
| 178 | |
Douglas Gregor | fd64377 | 2010-01-25 16:45:46 +0000 | [diff] [blame] | 179 | return false; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 182 | static bool visitPreprocessedEntitiesInRange(SourceRange R, |
| 183 | PreprocessingRecord &PPRec, |
| 184 | CursorVisitor &Visitor) { |
| 185 | SourceManager &SM = Visitor.getASTUnit()->getSourceManager(); |
| 186 | FileID FID; |
| 187 | |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 188 | if (!Visitor.shouldVisitIncludedEntities()) { |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 189 | // If the begin/end of the range lie in the same FileID, do the optimization |
| 190 | // where we skip preprocessed entities that do not come from the same FileID. |
| 191 | FID = SM.getFileID(R.getBegin()); |
| 192 | if (FID != SM.getFileID(R.getEnd())) |
| 193 | FID = FileID(); |
| 194 | } |
| 195 | |
| 196 | std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> |
| 197 | Entities = PPRec.getPreprocessedEntitiesInRange(R); |
| 198 | return Visitor.visitPreprocessedEntities(Entities.first, Entities.second, |
| 199 | PPRec, FID); |
| 200 | } |
| 201 | |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 202 | void CursorVisitor::visitFileRegion() { |
| 203 | if (RegionOfInterest.isInvalid()) |
| 204 | return; |
| 205 | |
| 206 | ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData); |
| 207 | SourceManager &SM = Unit->getSourceManager(); |
| 208 | |
| 209 | std::pair<FileID, unsigned> |
| 210 | Begin = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getBegin())), |
| 211 | End = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getEnd())); |
| 212 | |
| 213 | if (End.first != Begin.first) { |
| 214 | // If the end does not reside in the same file, try to recover by |
| 215 | // picking the end of the file of begin location. |
| 216 | End.first = Begin.first; |
| 217 | End.second = SM.getFileIDSize(Begin.first); |
| 218 | } |
| 219 | |
| 220 | assert(Begin.first == End.first); |
| 221 | if (Begin.second > End.second) |
| 222 | return; |
| 223 | |
| 224 | FileID File = Begin.first; |
| 225 | unsigned Offset = Begin.second; |
| 226 | unsigned Length = End.second - Begin.second; |
| 227 | |
| 228 | if (!VisitPreprocessorLast && |
| 229 | Unit->getPreprocessor().getPreprocessingRecord()) |
| 230 | visitPreprocessedEntitiesInRegion(); |
| 231 | |
| 232 | visitDeclsFromFileRegion(File, Offset, Length); |
| 233 | |
| 234 | if (VisitPreprocessorLast && |
| 235 | Unit->getPreprocessor().getPreprocessingRecord()) |
| 236 | visitPreprocessedEntitiesInRegion(); |
| 237 | } |
| 238 | |
| 239 | void CursorVisitor::visitDeclsFromFileRegion(FileID File, |
| 240 | unsigned Offset, unsigned Length) { |
| 241 | ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData); |
| 242 | SourceManager &SM = Unit->getSourceManager(); |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 243 | SourceRange Range = RegionOfInterest; |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 244 | |
| 245 | SmallVector<Decl *, 16> Decls; |
| 246 | Unit->findFileRegionDecls(File, Offset, Length, Decls); |
| 247 | |
| 248 | // If we didn't find any file level decls for the file, try looking at the |
| 249 | // file that it was included from. |
| 250 | while (Decls.empty()) { |
| 251 | bool Invalid = false; |
| 252 | const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid); |
| 253 | if (Invalid) |
| 254 | return; |
| 255 | |
| 256 | SourceLocation Outer; |
| 257 | if (SLEntry.isFile()) |
| 258 | Outer = SLEntry.getFile().getIncludeLoc(); |
| 259 | else |
| 260 | Outer = SLEntry.getExpansion().getExpansionLocStart(); |
| 261 | if (Outer.isInvalid()) |
| 262 | return; |
| 263 | |
| 264 | llvm::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer); |
| 265 | Length = 0; |
| 266 | Unit->findFileRegionDecls(File, Offset, Length, Decls); |
| 267 | } |
| 268 | |
| 269 | assert(!Decls.empty()); |
| 270 | |
| 271 | bool VisitedAtLeastOnce = false; |
| 272 | SmallVector<Decl *, 16>::iterator DIt = Decls.begin(); |
| 273 | for (SmallVector<Decl *, 16>::iterator DE = Decls.end(); DIt != DE; ++DIt) { |
| 274 | Decl *D = *DIt; |
| 275 | |
| 276 | // We handle forward decls via ObjCClassDecl. |
| 277 | if (ObjCInterfaceDecl *InterD = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 278 | if (InterD->isForwardDecl()) |
| 279 | continue; |
| 280 | // An interface that started as a forward decl may have changed location |
| 281 | // because its @interface was parsed. |
| 282 | if (InterD->isInitiallyForwardDecl() && |
| 283 | !SM.isInFileID(SM.getFileLoc(InterD->getLocation()), File)) |
| 284 | continue; |
| 285 | } |
| 286 | |
| 287 | RangeComparisonResult CompRes = RangeCompare(SM, D->getSourceRange(),Range); |
| 288 | if (CompRes == RangeBefore) |
| 289 | continue; |
| 290 | if (CompRes == RangeAfter) |
| 291 | break; |
| 292 | |
| 293 | assert(CompRes == RangeOverlap); |
| 294 | VisitedAtLeastOnce = true; |
Argyrios Kyrtzidis | ba98617 | 2011-11-03 19:02:28 +0000 | [diff] [blame] | 295 | if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true)) |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 296 | break; |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | if (VisitedAtLeastOnce) |
| 300 | return; |
| 301 | |
| 302 | // No Decls overlapped with the range. Move up the lexical context until there |
| 303 | // is a context that contains the range or we reach the translation unit |
| 304 | // level. |
| 305 | DeclContext *DC = DIt == Decls.begin() ? (*DIt)->getLexicalDeclContext() |
| 306 | : (*(DIt-1))->getLexicalDeclContext(); |
| 307 | |
| 308 | while (DC && !DC->isTranslationUnit()) { |
| 309 | Decl *D = cast<Decl>(DC); |
| 310 | SourceRange CurDeclRange = D->getSourceRange(); |
| 311 | if (CurDeclRange.isInvalid()) |
| 312 | break; |
| 313 | |
| 314 | if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) { |
Argyrios Kyrtzidis | ba98617 | 2011-11-03 19:02:28 +0000 | [diff] [blame] | 315 | Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true); |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 316 | break; |
| 317 | } |
| 318 | |
| 319 | DC = D->getLexicalDeclContext(); |
| 320 | } |
| 321 | } |
| 322 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 323 | bool CursorVisitor::visitPreprocessedEntitiesInRegion() { |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 324 | PreprocessingRecord &PPRec |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 325 | = *AU->getPreprocessor().getPreprocessingRecord(); |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 326 | SourceManager &SM = AU->getSourceManager(); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 327 | |
Argyrios Kyrtzidis | 92ddef1 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 328 | if (RegionOfInterest.isValid()) { |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 329 | SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest); |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 330 | SourceLocation B = MappedRange.getBegin(); |
| 331 | SourceLocation E = MappedRange.getEnd(); |
| 332 | |
| 333 | if (AU->isInPreambleFileID(B)) { |
| 334 | if (SM.isLoadedSourceLocation(E)) |
| 335 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), |
| 336 | PPRec, *this); |
| 337 | |
| 338 | // Beginning of range lies in the preamble but it also extends beyond |
| 339 | // it into the main file. Split the range into 2 parts, one covering |
| 340 | // the preamble and another covering the main file. This allows subsequent |
| 341 | // calls to visitPreprocessedEntitiesInRange to accept a source range that |
| 342 | // lies in the same FileID, allowing it to skip preprocessed entities that |
| 343 | // do not come from the same FileID. |
| 344 | bool breaked = |
| 345 | visitPreprocessedEntitiesInRange( |
| 346 | SourceRange(B, AU->getEndOfPreambleFileID()), |
| 347 | PPRec, *this); |
| 348 | if (breaked) return true; |
| 349 | return visitPreprocessedEntitiesInRange( |
| 350 | SourceRange(AU->getStartOfMainFileID(), E), |
| 351 | PPRec, *this); |
| 352 | } |
| 353 | |
| 354 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this); |
Argyrios Kyrtzidis | 92ddef1 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 357 | bool OnlyLocalDecls |
Douglas Gregor | 32038bb | 2010-12-21 19:07:48 +0000 | [diff] [blame] | 358 | = !AU->isMainFileAST() && AU->getOnlyLocalDecls(); |
| 359 | |
Argyrios Kyrtzidis | 92ddef1 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 360 | if (OnlyLocalDecls) |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 361 | return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(), |
| 362 | PPRec); |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 363 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 364 | return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec); |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | template<typename InputIterator> |
| 368 | bool CursorVisitor::visitPreprocessedEntities(InputIterator First, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 369 | InputIterator Last, |
| 370 | PreprocessingRecord &PPRec, |
| 371 | FileID FID) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 372 | for (; First != Last; ++First) { |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 373 | if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID)) |
| 374 | continue; |
| 375 | |
| 376 | PreprocessedEntity *PPE = *First; |
| 377 | if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 378 | if (Visit(MakeMacroExpansionCursor(ME, TU))) |
| 379 | return true; |
| 380 | |
| 381 | continue; |
| 382 | } |
| 383 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 384 | if (MacroDefinition *MD = dyn_cast<MacroDefinition>(PPE)) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 385 | if (Visit(MakeMacroDefinitionCursor(MD, TU))) |
| 386 | return true; |
| 387 | |
| 388 | continue; |
| 389 | } |
| 390 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 391 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 392 | if (Visit(MakeInclusionDirectiveCursor(ID, TU))) |
| 393 | return true; |
| 394 | |
| 395 | continue; |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 399 | return false; |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 402 | /// \brief Visit the children of the given cursor. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 403 | /// |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 404 | /// \returns true if the visitation should be aborted, false if it |
| 405 | /// should continue. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 406 | bool CursorVisitor::VisitChildren(CXCursor Cursor) { |
Douglas Gregor | c314aa4 | 2011-03-02 19:17:03 +0000 | [diff] [blame] | 407 | if (clang_isReference(Cursor.kind) && |
| 408 | Cursor.kind != CXCursor_CXXBaseSpecifier) { |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 409 | // By definition, references have no children. |
| 410 | return false; |
| 411 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 412 | |
| 413 | // Set the Parent field to Cursor, then back to its old value once we're |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 414 | // done. |
Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 415 | SetParentRAII SetParent(Parent, StmtParent, Cursor); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 416 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 417 | if (clang_isDeclaration(Cursor.kind)) { |
| 418 | Decl *D = getCursorDecl(Cursor); |
Douglas Gregor | 06d9b1a | 2011-04-14 21:41:34 +0000 | [diff] [blame] | 419 | if (!D) |
| 420 | return false; |
| 421 | |
Ted Kremenek | 539311e | 2010-02-18 18:47:01 +0000 | [diff] [blame] | 422 | return VisitAttributes(D) || Visit(D); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 423 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 424 | |
Douglas Gregor | 06d9b1a | 2011-04-14 21:41:34 +0000 | [diff] [blame] | 425 | if (clang_isStatement(Cursor.kind)) { |
| 426 | if (Stmt *S = getCursorStmt(Cursor)) |
| 427 | return Visit(S); |
| 428 | |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | if (clang_isExpression(Cursor.kind)) { |
| 433 | if (Expr *E = getCursorExpr(Cursor)) |
| 434 | return Visit(E); |
| 435 | |
| 436 | return false; |
| 437 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 438 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 439 | if (clang_isTranslationUnit(Cursor.kind)) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 440 | CXTranslationUnit tu = getCursorTU(Cursor); |
| 441 | ASTUnit *CXXUnit = static_cast<ASTUnit*>(tu->TUData); |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 442 | |
| 443 | int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast }; |
| 444 | for (unsigned I = 0; I != 2; ++I) { |
| 445 | if (VisitOrder[I]) { |
| 446 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && |
| 447 | RegionOfInterest.isInvalid()) { |
| 448 | for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(), |
| 449 | TLEnd = CXXUnit->top_level_end(); |
| 450 | TL != TLEnd; ++TL) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 451 | if (Visit(MakeCXCursor(*TL, tu, RegionOfInterest), true)) |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 452 | return true; |
| 453 | } |
| 454 | } else if (VisitDeclContext( |
| 455 | CXXUnit->getASTContext().getTranslationUnitDecl())) |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 456 | return true; |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 457 | continue; |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 458 | } |
Bob Wilson | 3178cb6 | 2010-03-19 03:57:57 +0000 | [diff] [blame] | 459 | |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 460 | // Walk the preprocessing record. |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 461 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) |
| 462 | visitPreprocessedEntitiesInRegion(); |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 463 | } |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 464 | |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 465 | return false; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 466 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 467 | |
Douglas Gregor | c314aa4 | 2011-03-02 19:17:03 +0000 | [diff] [blame] | 468 | if (Cursor.kind == CXCursor_CXXBaseSpecifier) { |
| 469 | if (CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) { |
| 470 | if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) { |
| 471 | return Visit(BaseTSInfo->getTypeLoc()); |
| 472 | } |
| 473 | } |
| 474 | } |
Argyrios Kyrtzidis | 221d5a5 | 2011-09-13 18:49:56 +0000 | [diff] [blame] | 475 | |
| 476 | if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { |
| 477 | IBOutletCollectionAttr *A = |
| 478 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor)); |
| 479 | if (const ObjCInterfaceType *InterT = A->getInterface()->getAs<ObjCInterfaceType>()) |
| 480 | return Visit(cxcursor::MakeCursorObjCClassRef(InterT->getInterface(), |
| 481 | A->getInterfaceLoc(), TU)); |
| 482 | } |
| 483 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 484 | // Nothing to visit at the moment. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 485 | return false; |
| 486 | } |
| 487 | |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 488 | bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { |
Douglas Gregor | 13c8ccb | 2011-04-22 23:49:24 +0000 | [diff] [blame] | 489 | if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten()) |
| 490 | if (Visit(TSInfo->getTypeLoc())) |
| 491 | return true; |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 492 | |
Ted Kremenek | 664cffd | 2010-07-22 11:30:19 +0000 | [diff] [blame] | 493 | if (Stmt *Body = B->getBody()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 494 | return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest)); |
Ted Kremenek | 664cffd | 2010-07-22 11:30:19 +0000 | [diff] [blame] | 495 | |
| 496 | return false; |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 499 | llvm::Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) { |
| 500 | if (RegionOfInterest.isValid()) { |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 501 | SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager()); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 502 | if (Range.isInvalid()) |
| 503 | return llvm::Optional<bool>(); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 504 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 505 | switch (CompareRegionOfInterest(Range)) { |
| 506 | case RangeBefore: |
| 507 | // This declaration comes before the region of interest; skip it. |
| 508 | return llvm::Optional<bool>(); |
| 509 | |
| 510 | case RangeAfter: |
| 511 | // This declaration comes after the region of interest; we're done. |
| 512 | return false; |
| 513 | |
| 514 | case RangeOverlap: |
| 515 | // This declaration overlaps the region of interest; visit it. |
| 516 | break; |
| 517 | } |
| 518 | } |
| 519 | return true; |
| 520 | } |
| 521 | |
| 522 | bool CursorVisitor::VisitDeclContext(DeclContext *DC) { |
| 523 | DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 524 | |
| 525 | // FIXME: Eventually remove. This part of a hack to support proper |
| 526 | // iteration over all Decls contained lexically within an ObjC container. |
| 527 | SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I); |
| 528 | SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E); |
| 529 | |
| 530 | for ( ; I != E; ++I) { |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 531 | Decl *D = *I; |
| 532 | if (D->getLexicalDeclContext() != DC) |
| 533 | continue; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 534 | CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 535 | const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); |
| 536 | if (!V.hasValue()) |
| 537 | continue; |
| 538 | if (!V.getValue()) |
| 539 | return false; |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 540 | if (Visit(Cursor, true)) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 541 | return true; |
| 542 | } |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 543 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 546 | bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 547 | llvm_unreachable("Translation units are visited directly by Visit()"); |
| 548 | return false; |
| 549 | } |
| 550 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 551 | bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 552 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 553 | return Visit(TSInfo->getTypeLoc()); |
| 554 | |
| 555 | return false; |
| 556 | } |
| 557 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 558 | bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { |
| 559 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 560 | return Visit(TSInfo->getTypeLoc()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 561 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 562 | return false; |
| 563 | } |
| 564 | |
| 565 | bool CursorVisitor::VisitTagDecl(TagDecl *D) { |
| 566 | return VisitDeclContext(D); |
| 567 | } |
| 568 | |
Douglas Gregor | 0ab1e9f | 2010-09-01 17:32:36 +0000 | [diff] [blame] | 569 | bool CursorVisitor::VisitClassTemplateSpecializationDecl( |
| 570 | ClassTemplateSpecializationDecl *D) { |
| 571 | bool ShouldVisitBody = false; |
| 572 | switch (D->getSpecializationKind()) { |
| 573 | case TSK_Undeclared: |
| 574 | case TSK_ImplicitInstantiation: |
| 575 | // Nothing to visit |
| 576 | return false; |
| 577 | |
| 578 | case TSK_ExplicitInstantiationDeclaration: |
| 579 | case TSK_ExplicitInstantiationDefinition: |
| 580 | break; |
| 581 | |
| 582 | case TSK_ExplicitSpecialization: |
| 583 | ShouldVisitBody = true; |
| 584 | break; |
| 585 | } |
| 586 | |
| 587 | // Visit the template arguments used in the specialization. |
| 588 | if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { |
| 589 | TypeLoc TL = SpecType->getTypeLoc(); |
| 590 | if (TemplateSpecializationTypeLoc *TSTLoc |
| 591 | = dyn_cast<TemplateSpecializationTypeLoc>(&TL)) { |
| 592 | for (unsigned I = 0, N = TSTLoc->getNumArgs(); I != N; ++I) |
| 593 | if (VisitTemplateArgumentLoc(TSTLoc->getArgLoc(I))) |
| 594 | return true; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | if (ShouldVisitBody && VisitCXXRecordDecl(D)) |
| 599 | return true; |
| 600 | |
| 601 | return false; |
| 602 | } |
| 603 | |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 604 | bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( |
| 605 | ClassTemplatePartialSpecializationDecl *D) { |
| 606 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 607 | // before visiting these template parameters. |
| 608 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 609 | return true; |
| 610 | |
| 611 | // Visit the partial specialization arguments. |
| 612 | const TemplateArgumentLoc *TemplateArgs = D->getTemplateArgsAsWritten(); |
| 613 | for (unsigned I = 0, N = D->getNumTemplateArgsAsWritten(); I != N; ++I) |
| 614 | if (VisitTemplateArgumentLoc(TemplateArgs[I])) |
| 615 | return true; |
| 616 | |
| 617 | return VisitCXXRecordDecl(D); |
| 618 | } |
| 619 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 620 | bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 621 | // Visit the default argument. |
| 622 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 623 | if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) |
| 624 | if (Visit(DefArg->getTypeLoc())) |
| 625 | return true; |
| 626 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 627 | return false; |
| 628 | } |
| 629 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 630 | bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 631 | if (Expr *Init = D->getInitExpr()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 632 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 633 | return false; |
| 634 | } |
| 635 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 636 | bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { |
| 637 | if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) |
| 638 | if (Visit(TSInfo->getTypeLoc())) |
| 639 | return true; |
| 640 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 641 | // Visit the nested-name-specifier, if present. |
| 642 | if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc()) |
| 643 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 644 | return true; |
| 645 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 646 | return false; |
| 647 | } |
| 648 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 649 | /// \brief Compare two base or member initializers based on their source order. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 650 | static int CompareCXXCtorInitializers(const void* Xp, const void *Yp) { |
| 651 | CXXCtorInitializer const * const *X |
| 652 | = static_cast<CXXCtorInitializer const * const *>(Xp); |
| 653 | CXXCtorInitializer const * const *Y |
| 654 | = static_cast<CXXCtorInitializer const * const *>(Yp); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 655 | |
| 656 | if ((*X)->getSourceOrder() < (*Y)->getSourceOrder()) |
| 657 | return -1; |
| 658 | else if ((*X)->getSourceOrder() > (*Y)->getSourceOrder()) |
| 659 | return 1; |
| 660 | else |
| 661 | return 0; |
| 662 | } |
| 663 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 664 | bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 665 | if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { |
| 666 | // Visit the function declaration's syntactic components in the order |
| 667 | // written. This requires a bit of work. |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 668 | TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 669 | FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL); |
| 670 | |
| 671 | // If we have a function declared directly (without the use of a typedef), |
| 672 | // visit just the return type. Otherwise, just visit the function's type |
| 673 | // now. |
| 674 | if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL->getResultLoc())) || |
| 675 | (!FTL && Visit(TL))) |
| 676 | return true; |
| 677 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 678 | // Visit the nested-name-specifier, if present. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 679 | if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc()) |
| 680 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 681 | return true; |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 682 | |
| 683 | // Visit the declaration name. |
| 684 | if (VisitDeclarationNameInfo(ND->getNameInfo())) |
| 685 | return true; |
| 686 | |
| 687 | // FIXME: Visit explicitly-specified template arguments! |
| 688 | |
| 689 | // Visit the function parameters, if we have a function type. |
| 690 | if (FTL && VisitFunctionTypeLoc(*FTL, true)) |
| 691 | return true; |
| 692 | |
| 693 | // FIXME: Attributes? |
| 694 | } |
| 695 | |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 696 | if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) { |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 697 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) { |
| 698 | // Find the initializers that were written in the source. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 699 | SmallVector<CXXCtorInitializer *, 4> WrittenInits; |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 700 | for (CXXConstructorDecl::init_iterator I = Constructor->init_begin(), |
| 701 | IEnd = Constructor->init_end(); |
| 702 | I != IEnd; ++I) { |
| 703 | if (!(*I)->isWritten()) |
| 704 | continue; |
| 705 | |
| 706 | WrittenInits.push_back(*I); |
| 707 | } |
| 708 | |
| 709 | // Sort the initializers in source order |
| 710 | llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 711 | &CompareCXXCtorInitializers); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 712 | |
| 713 | // Visit the initializers in source order |
| 714 | for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 715 | CXXCtorInitializer *Init = WrittenInits[I]; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 716 | if (Init->isAnyMemberInitializer()) { |
| 717 | if (Visit(MakeCursorMemberRef(Init->getAnyMember(), |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 718 | Init->getMemberLocation(), TU))) |
| 719 | return true; |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 720 | } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) { |
| 721 | if (Visit(TInfo->getTypeLoc())) |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 722 | return true; |
| 723 | } |
| 724 | |
| 725 | // Visit the initializer value. |
| 726 | if (Expr *Initializer = Init->getInit()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 727 | if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest))) |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 728 | return true; |
| 729 | } |
| 730 | } |
| 731 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 732 | if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 733 | return true; |
| 734 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 735 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 736 | return false; |
| 737 | } |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 738 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 739 | bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { |
| 740 | if (VisitDeclaratorDecl(D)) |
| 741 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 742 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 743 | if (Expr *BitWidth = D->getBitWidth()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 744 | return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 745 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 746 | return false; |
| 747 | } |
| 748 | |
| 749 | bool CursorVisitor::VisitVarDecl(VarDecl *D) { |
| 750 | if (VisitDeclaratorDecl(D)) |
| 751 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 752 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 753 | if (Expr *Init = D->getInit()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 754 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 755 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 756 | return false; |
| 757 | } |
| 758 | |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 759 | bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 760 | if (VisitDeclaratorDecl(D)) |
| 761 | return true; |
| 762 | |
| 763 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 764 | if (Expr *DefArg = D->getDefaultArgument()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 765 | return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 766 | |
| 767 | return false; |
| 768 | } |
| 769 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 770 | bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 771 | // FIXME: Visit the "outer" template parameter lists on the FunctionDecl |
| 772 | // before visiting these template parameters. |
| 773 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 774 | return true; |
| 775 | |
| 776 | return VisitFunctionDecl(D->getTemplatedDecl()); |
| 777 | } |
| 778 | |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 779 | bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 780 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 781 | // before visiting these template parameters. |
| 782 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 783 | return true; |
| 784 | |
| 785 | return VisitCXXRecordDecl(D->getTemplatedDecl()); |
| 786 | } |
| 787 | |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 788 | bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 789 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 790 | return true; |
| 791 | |
| 792 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && |
| 793 | VisitTemplateArgumentLoc(D->getDefaultArgument())) |
| 794 | return true; |
| 795 | |
| 796 | return false; |
| 797 | } |
| 798 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 799 | bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 800 | if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo()) |
| 801 | if (Visit(TSInfo->getTypeLoc())) |
| 802 | return true; |
| 803 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 804 | for (ObjCMethodDecl::param_iterator P = ND->param_begin(), |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 805 | PEnd = ND->param_end(); |
| 806 | P != PEnd; ++P) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 807 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 808 | return true; |
| 809 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 810 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 811 | if (ND->isThisDeclarationADefinition() && |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 812 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 813 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 814 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 815 | return false; |
| 816 | } |
| 817 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 818 | namespace { |
| 819 | struct ContainerDeclsSort { |
| 820 | SourceManager &SM; |
| 821 | ContainerDeclsSort(SourceManager &sm) : SM(sm) {} |
| 822 | bool operator()(Decl *A, Decl *B) { |
| 823 | SourceLocation L_A = A->getLocStart(); |
| 824 | SourceLocation L_B = B->getLocStart(); |
| 825 | assert(L_A.isValid() && L_B.isValid()); |
| 826 | return SM.isBeforeInTranslationUnit(L_A, L_B); |
| 827 | } |
| 828 | }; |
| 829 | } |
| 830 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 831 | bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 832 | // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially |
| 833 | // an @implementation can lexically contain Decls that are not properly |
| 834 | // nested in the AST. When we identify such cases, we need to retrofit |
| 835 | // this nesting here. |
| 836 | if (!DI_current) |
| 837 | return VisitDeclContext(D); |
| 838 | |
| 839 | // Scan the Decls that immediately come after the container |
| 840 | // in the current DeclContext. If any fall within the |
| 841 | // container's lexical region, stash them into a vector |
| 842 | // for later processing. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 843 | SmallVector<Decl *, 24> DeclsInContainer; |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 844 | SourceLocation EndLoc = D->getSourceRange().getEnd(); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 845 | SourceManager &SM = AU->getSourceManager(); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 846 | if (EndLoc.isValid()) { |
| 847 | DeclContext::decl_iterator next = *DI_current; |
| 848 | while (++next != DE_current) { |
| 849 | Decl *D_next = *next; |
| 850 | if (!D_next) |
| 851 | break; |
| 852 | SourceLocation L = D_next->getLocStart(); |
| 853 | if (!L.isValid()) |
| 854 | break; |
| 855 | if (SM.isBeforeInTranslationUnit(L, EndLoc)) { |
| 856 | *DI_current = next; |
| 857 | DeclsInContainer.push_back(D_next); |
| 858 | continue; |
| 859 | } |
| 860 | break; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | // The common case. |
| 865 | if (DeclsInContainer.empty()) |
| 866 | return VisitDeclContext(D); |
| 867 | |
| 868 | // Get all the Decls in the DeclContext, and sort them with the |
| 869 | // additional ones we've collected. Then visit them. |
| 870 | for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end(); |
| 871 | I!=E; ++I) { |
| 872 | Decl *subDecl = *I; |
Ted Kremenek | 0582c89 | 2010-11-02 23:17:51 +0000 | [diff] [blame] | 873 | if (!subDecl || subDecl->getLexicalDeclContext() != D || |
| 874 | subDecl->getLocStart().isInvalid()) |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 875 | continue; |
| 876 | DeclsInContainer.push_back(subDecl); |
| 877 | } |
| 878 | |
| 879 | // Now sort the Decls so that they appear in lexical order. |
| 880 | std::sort(DeclsInContainer.begin(), DeclsInContainer.end(), |
| 881 | ContainerDeclsSort(SM)); |
| 882 | |
| 883 | // Now visit the decls. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 884 | for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(), |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 885 | E = DeclsInContainer.end(); I != E; ++I) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 886 | CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 887 | const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); |
| 888 | if (!V.hasValue()) |
| 889 | continue; |
| 890 | if (!V.getValue()) |
| 891 | return false; |
| 892 | if (Visit(Cursor, true)) |
| 893 | return true; |
| 894 | } |
| 895 | return false; |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 898 | bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 899 | if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), |
| 900 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 901 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 902 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 903 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 904 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 905 | E = ND->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 906 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 907 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 908 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 909 | return VisitObjCContainerDecl(ND); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 912 | bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 913 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
| 914 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 915 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 916 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 917 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 918 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 919 | return VisitObjCContainerDecl(PID); |
| 920 | } |
| 921 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 922 | bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 923 | if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc())) |
John McCall | fc92920 | 2010-06-04 22:33:30 +0000 | [diff] [blame] | 924 | return true; |
| 925 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 926 | // FIXME: This implements a workaround with @property declarations also being |
| 927 | // installed in the DeclContext for the @interface. Eventually this code |
| 928 | // should be removed. |
| 929 | ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); |
| 930 | if (!CDecl || !CDecl->IsClassExtension()) |
| 931 | return false; |
| 932 | |
| 933 | ObjCInterfaceDecl *ID = CDecl->getClassInterface(); |
| 934 | if (!ID) |
| 935 | return false; |
| 936 | |
| 937 | IdentifierInfo *PropertyId = PD->getIdentifier(); |
| 938 | ObjCPropertyDecl *prevDecl = |
| 939 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId); |
| 940 | |
| 941 | if (!prevDecl) |
| 942 | return false; |
| 943 | |
| 944 | // Visit synthesized methods since they will be skipped when visiting |
| 945 | // the @interface. |
| 946 | if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 947 | if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 948 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 949 | return true; |
| 950 | |
| 951 | if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 952 | if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 953 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 954 | return true; |
| 955 | |
| 956 | return false; |
| 957 | } |
| 958 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 959 | bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 960 | // Issue callbacks for super class. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 961 | if (D->getSuperClass() && |
| 962 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 963 | D->getSuperClassLoc(), |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 964 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 965 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 966 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 967 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 968 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 969 | E = D->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 970 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 971 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 972 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 973 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 976 | bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 977 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 980 | bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 981 | // 'ID' could be null when dealing with invalid code. |
| 982 | if (ObjCInterfaceDecl *ID = D->getClassInterface()) |
| 983 | if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) |
| 984 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 985 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 986 | return VisitObjCImplDecl(D); |
| 987 | } |
| 988 | |
| 989 | bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 990 | #if 0 |
| 991 | // Issue callbacks for super class. |
| 992 | // FIXME: No source location information! |
| 993 | if (D->getSuperClass() && |
| 994 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 995 | D->getSuperClassLoc(), |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 996 | TU))) |
| 997 | return true; |
| 998 | #endif |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 999 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1000 | return VisitObjCImplDecl(D); |
| 1001 | } |
| 1002 | |
| 1003 | bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 1004 | ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1005 | for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(), |
| 1006 | E = D->protocol_end(); |
| 1007 | I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1008 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1009 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1010 | |
| 1011 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1014 | bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) { |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 1015 | if (Visit(MakeCursorObjCClassRef(D->getForwardInterfaceDecl(), |
| 1016 | D->getForwardDecl()->getLocation(), TU))) |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1017 | return true; |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1018 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1021 | bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) { |
| 1022 | if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl()) |
| 1023 | return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU)); |
| 1024 | |
| 1025 | return false; |
| 1026 | } |
| 1027 | |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 1028 | bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { |
| 1029 | return VisitDeclContext(D); |
| 1030 | } |
| 1031 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 1032 | bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1033 | // Visit nested-name-specifier. |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 1034 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1035 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1036 | return true; |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 1037 | |
| 1038 | return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), |
| 1039 | D->getTargetNameLoc(), TU)); |
| 1040 | } |
| 1041 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1042 | bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1043 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1044 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1045 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1046 | return true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1047 | } |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1048 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1049 | if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU))) |
| 1050 | return true; |
| 1051 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1052 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1053 | } |
| 1054 | |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1055 | bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1056 | // Visit nested-name-specifier. |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 1057 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1058 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1059 | return true; |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1060 | |
| 1061 | return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), |
| 1062 | D->getIdentLocation(), TU)); |
| 1063 | } |
| 1064 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1065 | bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1066 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1067 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1068 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1069 | return true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1070 | } |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1071 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1072 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1073 | } |
| 1074 | |
| 1075 | bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( |
| 1076 | UnresolvedUsingTypenameDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1077 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1078 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1079 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1080 | return true; |
| 1081 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1082 | return false; |
| 1083 | } |
| 1084 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1085 | bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { |
| 1086 | switch (Name.getName().getNameKind()) { |
| 1087 | case clang::DeclarationName::Identifier: |
| 1088 | case clang::DeclarationName::CXXLiteralOperatorName: |
| 1089 | case clang::DeclarationName::CXXOperatorName: |
| 1090 | case clang::DeclarationName::CXXUsingDirective: |
| 1091 | return false; |
| 1092 | |
| 1093 | case clang::DeclarationName::CXXConstructorName: |
| 1094 | case clang::DeclarationName::CXXDestructorName: |
| 1095 | case clang::DeclarationName::CXXConversionFunctionName: |
| 1096 | if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) |
| 1097 | return Visit(TSInfo->getTypeLoc()); |
| 1098 | return false; |
| 1099 | |
| 1100 | case clang::DeclarationName::ObjCZeroArgSelector: |
| 1101 | case clang::DeclarationName::ObjCOneArgSelector: |
| 1102 | case clang::DeclarationName::ObjCMultiArgSelector: |
| 1103 | // FIXME: Per-identifier location info? |
| 1104 | return false; |
| 1105 | } |
| 1106 | |
| 1107 | return false; |
| 1108 | } |
| 1109 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1110 | bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, |
| 1111 | SourceRange Range) { |
| 1112 | // FIXME: This whole routine is a hack to work around the lack of proper |
| 1113 | // source information in nested-name-specifiers (PR5791). Since we do have |
| 1114 | // a beginning source location, we can visit the first component of the |
| 1115 | // nested-name-specifier, if it's a single-token component. |
| 1116 | if (!NNS) |
| 1117 | return false; |
| 1118 | |
| 1119 | // Get the first component in the nested-name-specifier. |
| 1120 | while (NestedNameSpecifier *Prefix = NNS->getPrefix()) |
| 1121 | NNS = Prefix; |
| 1122 | |
| 1123 | switch (NNS->getKind()) { |
| 1124 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1125 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), |
| 1126 | TU)); |
| 1127 | |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 1128 | case NestedNameSpecifier::NamespaceAlias: |
| 1129 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1130 | Range.getBegin(), TU)); |
| 1131 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1132 | case NestedNameSpecifier::TypeSpec: { |
| 1133 | // If the type has a form where we know that the beginning of the source |
| 1134 | // range matches up with a reference cursor. Visit the appropriate reference |
| 1135 | // cursor. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1136 | const Type *T = NNS->getAsType(); |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1137 | if (const TypedefType *Typedef = dyn_cast<TypedefType>(T)) |
| 1138 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); |
| 1139 | if (const TagType *Tag = dyn_cast<TagType>(T)) |
| 1140 | return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); |
| 1141 | if (const TemplateSpecializationType *TST |
| 1142 | = dyn_cast<TemplateSpecializationType>(T)) |
| 1143 | return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); |
| 1144 | break; |
| 1145 | } |
| 1146 | |
| 1147 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1148 | case NestedNameSpecifier::Global: |
| 1149 | case NestedNameSpecifier::Identifier: |
| 1150 | break; |
| 1151 | } |
| 1152 | |
| 1153 | return false; |
| 1154 | } |
| 1155 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1156 | bool |
| 1157 | CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1158 | SmallVector<NestedNameSpecifierLoc, 4> Qualifiers; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1159 | for (; Qualifier; Qualifier = Qualifier.getPrefix()) |
| 1160 | Qualifiers.push_back(Qualifier); |
| 1161 | |
| 1162 | while (!Qualifiers.empty()) { |
| 1163 | NestedNameSpecifierLoc Q = Qualifiers.pop_back_val(); |
| 1164 | NestedNameSpecifier *NNS = Q.getNestedNameSpecifier(); |
| 1165 | switch (NNS->getKind()) { |
| 1166 | case NestedNameSpecifier::Namespace: |
| 1167 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1168 | Q.getLocalBeginLoc(), |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1169 | TU))) |
| 1170 | return true; |
| 1171 | |
| 1172 | break; |
| 1173 | |
| 1174 | case NestedNameSpecifier::NamespaceAlias: |
| 1175 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1176 | Q.getLocalBeginLoc(), |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1177 | TU))) |
| 1178 | return true; |
| 1179 | |
| 1180 | break; |
| 1181 | |
| 1182 | case NestedNameSpecifier::TypeSpec: |
| 1183 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1184 | if (Visit(Q.getTypeLoc())) |
| 1185 | return true; |
| 1186 | |
| 1187 | break; |
| 1188 | |
| 1189 | case NestedNameSpecifier::Global: |
| 1190 | case NestedNameSpecifier::Identifier: |
| 1191 | break; |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | return false; |
| 1196 | } |
| 1197 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1198 | bool CursorVisitor::VisitTemplateParameters( |
| 1199 | const TemplateParameterList *Params) { |
| 1200 | if (!Params) |
| 1201 | return false; |
| 1202 | |
| 1203 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 1204 | PEnd = Params->end(); |
| 1205 | P != PEnd; ++P) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1206 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1207 | return true; |
| 1208 | } |
| 1209 | |
| 1210 | return false; |
| 1211 | } |
| 1212 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1213 | bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { |
| 1214 | switch (Name.getKind()) { |
| 1215 | case TemplateName::Template: |
| 1216 | return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); |
| 1217 | |
| 1218 | case TemplateName::OverloadedTemplate: |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1219 | // Visit the overloaded template set. |
| 1220 | if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU))) |
| 1221 | return true; |
| 1222 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1223 | return false; |
| 1224 | |
| 1225 | case TemplateName::DependentTemplate: |
| 1226 | // FIXME: Visit nested-name-specifier. |
| 1227 | return false; |
| 1228 | |
| 1229 | case TemplateName::QualifiedTemplate: |
| 1230 | // FIXME: Visit nested-name-specifier. |
| 1231 | return Visit(MakeCursorTemplateRef( |
| 1232 | Name.getAsQualifiedTemplateName()->getDecl(), |
| 1233 | Loc, TU)); |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1234 | |
| 1235 | case TemplateName::SubstTemplateTemplateParm: |
| 1236 | return Visit(MakeCursorTemplateRef( |
| 1237 | Name.getAsSubstTemplateTemplateParm()->getParameter(), |
| 1238 | Loc, TU)); |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 1239 | |
| 1240 | case TemplateName::SubstTemplateTemplateParmPack: |
| 1241 | return Visit(MakeCursorTemplateRef( |
| 1242 | Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), |
| 1243 | Loc, TU)); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | return false; |
| 1247 | } |
| 1248 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1249 | bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { |
| 1250 | switch (TAL.getArgument().getKind()) { |
| 1251 | case TemplateArgument::Null: |
| 1252 | case TemplateArgument::Integral: |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 1253 | case TemplateArgument::Pack: |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1254 | return false; |
| 1255 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1256 | case TemplateArgument::Type: |
| 1257 | if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) |
| 1258 | return Visit(TSInfo->getTypeLoc()); |
| 1259 | return false; |
| 1260 | |
| 1261 | case TemplateArgument::Declaration: |
| 1262 | if (Expr *E = TAL.getSourceDeclExpression()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1263 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1264 | return false; |
| 1265 | |
| 1266 | case TemplateArgument::Expression: |
| 1267 | if (Expr *E = TAL.getSourceExpression()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1268 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1269 | return false; |
| 1270 | |
| 1271 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1272 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 1273 | if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc())) |
| 1274 | return true; |
| 1275 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1276 | return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1277 | TAL.getTemplateNameLoc()); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | return false; |
| 1281 | } |
| 1282 | |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 1283 | bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 1284 | return VisitDeclContext(D); |
| 1285 | } |
| 1286 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1287 | bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1288 | return Visit(TL.getUnqualifiedLoc()); |
| 1289 | } |
| 1290 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1291 | bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 1292 | ASTContext &Context = AU->getASTContext(); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1293 | |
| 1294 | // Some builtin types (such as Objective-C's "id", "sel", and |
| 1295 | // "Class") have associated declarations. Create cursors for those. |
| 1296 | QualType VisitType; |
John McCall | e0a22d0 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 1297 | switch (TL.getTypePtr()->getKind()) { |
John McCall | 2dde35b | 2011-10-18 22:28:37 +0000 | [diff] [blame] | 1298 | |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1299 | case BuiltinType::Void: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1300 | case BuiltinType::NullPtr: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1301 | case BuiltinType::Dependent: |
John McCall | 2dde35b | 2011-10-18 22:28:37 +0000 | [diff] [blame] | 1302 | #define BUILTIN_TYPE(Id, SingletonId) |
| 1303 | #define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1304 | #define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1305 | #define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1306 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1307 | #include "clang/AST/BuiltinTypes.def" |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1308 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1309 | |
Ted Kremenek | c4174cc | 2010-02-18 18:52:18 +0000 | [diff] [blame] | 1310 | case BuiltinType::ObjCId: |
| 1311 | VisitType = Context.getObjCIdType(); |
| 1312 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1313 | |
| 1314 | case BuiltinType::ObjCClass: |
| 1315 | VisitType = Context.getObjCClassType(); |
| 1316 | break; |
| 1317 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1318 | case BuiltinType::ObjCSel: |
| 1319 | VisitType = Context.getObjCSelType(); |
| 1320 | break; |
| 1321 | } |
| 1322 | |
| 1323 | if (!VisitType.isNull()) { |
| 1324 | if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1325 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1326 | TU)); |
| 1327 | } |
| 1328 | |
| 1329 | return false; |
| 1330 | } |
| 1331 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1332 | bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1333 | return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU)); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1336 | bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 1337 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1338 | } |
| 1339 | |
| 1340 | bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { |
Argyrios Kyrtzidis | 6f155de | 2011-08-25 22:24:47 +0000 | [diff] [blame] | 1341 | if (TL.isDefinition()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1342 | return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest)); |
Argyrios Kyrtzidis | 6f155de | 2011-08-25 22:24:47 +0000 | [diff] [blame] | 1343 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1344 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1345 | } |
| 1346 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1347 | bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
Chandler Carruth | 960d13d | 2011-05-01 09:53:37 +0000 | [diff] [blame] | 1348 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1351 | bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 1352 | if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU))) |
| 1353 | return true; |
| 1354 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1355 | return false; |
| 1356 | } |
| 1357 | |
| 1358 | bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 1359 | if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) |
| 1360 | return true; |
| 1361 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1362 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1363 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1364 | TU))) |
| 1365 | return true; |
| 1366 | } |
| 1367 | |
| 1368 | return false; |
| 1369 | } |
| 1370 | |
| 1371 | bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1372 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1375 | bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) { |
| 1376 | return Visit(TL.getInnerLoc()); |
| 1377 | } |
| 1378 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1379 | bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1380 | return Visit(TL.getPointeeLoc()); |
| 1381 | } |
| 1382 | |
| 1383 | bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1384 | return Visit(TL.getPointeeLoc()); |
| 1385 | } |
| 1386 | |
| 1387 | bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1388 | return Visit(TL.getPointeeLoc()); |
| 1389 | } |
| 1390 | |
| 1391 | bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1392 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1396 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
Argyrios Kyrtzidis | 3422fbc | 2011-08-15 18:44:43 +0000 | [diff] [blame] | 1399 | bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 1400 | return Visit(TL.getModifiedLoc()); |
| 1401 | } |
| 1402 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1403 | bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, |
| 1404 | bool SkipResultType) { |
| 1405 | if (!SkipResultType && Visit(TL.getResultLoc())) |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1406 | return true; |
| 1407 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1408 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
Ted Kremenek | 5dbacb4 | 2010-04-07 00:27:13 +0000 | [diff] [blame] | 1409 | if (Decl *D = TL.getArg(I)) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1410 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest))) |
Ted Kremenek | 5dbacb4 | 2010-04-07 00:27:13 +0000 | [diff] [blame] | 1411 | return true; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1412 | |
| 1413 | return false; |
| 1414 | } |
| 1415 | |
| 1416 | bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1417 | if (Visit(TL.getElementLoc())) |
| 1418 | return true; |
| 1419 | |
| 1420 | if (Expr *Size = TL.getSizeExpr()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1421 | return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1422 | |
| 1423 | return false; |
| 1424 | } |
| 1425 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1426 | bool CursorVisitor::VisitTemplateSpecializationTypeLoc( |
| 1427 | TemplateSpecializationTypeLoc TL) { |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1428 | // Visit the template name. |
| 1429 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1430 | TL.getTemplateNameLoc())) |
| 1431 | return true; |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1432 | |
| 1433 | // Visit the template arguments. |
| 1434 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1435 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1436 | return true; |
| 1437 | |
| 1438 | return false; |
| 1439 | } |
| 1440 | |
Douglas Gregor | 2332c11 | 2010-01-21 20:48:56 +0000 | [diff] [blame] | 1441 | bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 1442 | return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); |
| 1443 | } |
| 1444 | |
| 1445 | bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 1446 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1447 | return Visit(TSInfo->getTypeLoc()); |
| 1448 | |
| 1449 | return false; |
| 1450 | } |
| 1451 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1452 | bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 1453 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1454 | return Visit(TSInfo->getTypeLoc()); |
| 1455 | |
| 1456 | return false; |
| 1457 | } |
| 1458 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 1459 | bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
| 1460 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1461 | return true; |
| 1462 | |
| 1463 | return false; |
| 1464 | } |
| 1465 | |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 1466 | bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc( |
| 1467 | DependentTemplateSpecializationTypeLoc TL) { |
| 1468 | // Visit the nested-name-specifier, if there is one. |
| 1469 | if (TL.getQualifierLoc() && |
| 1470 | VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1471 | return true; |
| 1472 | |
| 1473 | // Visit the template arguments. |
| 1474 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1475 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1476 | return true; |
| 1477 | |
| 1478 | return false; |
| 1479 | } |
| 1480 | |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1481 | bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 1482 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1483 | return true; |
| 1484 | |
| 1485 | return Visit(TL.getNamedTypeLoc()); |
| 1486 | } |
| 1487 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 1488 | bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { |
| 1489 | return Visit(TL.getPatternLoc()); |
| 1490 | } |
| 1491 | |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 1492 | bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 1493 | if (Expr *E = TL.getUnderlyingExpr()) |
| 1494 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1495 | |
| 1496 | return false; |
| 1497 | } |
| 1498 | |
| 1499 | bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 1500 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1501 | } |
| 1502 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1503 | bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) { |
| 1504 | return Visit(TL.getValueLoc()); |
| 1505 | } |
| 1506 | |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 1507 | #define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \ |
| 1508 | bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \ |
| 1509 | return Visit##PARENT##Loc(TL); \ |
| 1510 | } |
| 1511 | |
| 1512 | DEFAULT_TYPELOC_IMPL(Complex, Type) |
| 1513 | DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType) |
| 1514 | DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType) |
| 1515 | DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType) |
| 1516 | DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType) |
| 1517 | DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type) |
| 1518 | DEFAULT_TYPELOC_IMPL(Vector, Type) |
| 1519 | DEFAULT_TYPELOC_IMPL(ExtVector, VectorType) |
| 1520 | DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType) |
| 1521 | DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType) |
| 1522 | DEFAULT_TYPELOC_IMPL(Record, TagType) |
| 1523 | DEFAULT_TYPELOC_IMPL(Enum, TagType) |
| 1524 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type) |
| 1525 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type) |
| 1526 | DEFAULT_TYPELOC_IMPL(Auto, Type) |
| 1527 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 1528 | bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1529 | // Visit the nested-name-specifier, if present. |
| 1530 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1531 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1532 | return true; |
| 1533 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 1534 | if (D->isCompleteDefinition()) { |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 1535 | for (CXXRecordDecl::base_class_iterator I = D->bases_begin(), |
| 1536 | E = D->bases_end(); I != E; ++I) { |
| 1537 | if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU))) |
| 1538 | return true; |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | return VisitTagDecl(D); |
| 1543 | } |
| 1544 | |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1545 | bool CursorVisitor::VisitAttributes(Decl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1546 | for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end(); |
| 1547 | i != e; ++i) |
| 1548 | if (Visit(MakeCXCursor(*i, D, TU))) |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1549 | return true; |
| 1550 | |
| 1551 | return false; |
| 1552 | } |
| 1553 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1554 | //===----------------------------------------------------------------------===// |
| 1555 | // Data-recursive visitor methods. |
| 1556 | //===----------------------------------------------------------------------===// |
| 1557 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1558 | namespace { |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1559 | #define DEF_JOB(NAME, DATA, KIND)\ |
| 1560 | class NAME : public VisitorJob {\ |
| 1561 | public:\ |
| 1562 | NAME(DATA *d, CXCursor parent) : VisitorJob(parent, VisitorJob::KIND, d) {} \ |
| 1563 | static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\ |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1564 | DATA *get() const { return static_cast<DATA*>(data[0]); }\ |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1565 | }; |
| 1566 | |
| 1567 | DEF_JOB(StmtVisit, Stmt, StmtVisitKind) |
| 1568 | DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind) |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1569 | DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1570 | DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind) |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1571 | DEF_JOB(ExplicitTemplateArgsVisit, ASTTemplateArgumentListInfo, |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1572 | ExplicitTemplateArgsVisitKind) |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 1573 | DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1574 | #undef DEF_JOB |
| 1575 | |
| 1576 | class DeclVisit : public VisitorJob { |
| 1577 | public: |
| 1578 | DeclVisit(Decl *d, CXCursor parent, bool isFirst) : |
| 1579 | VisitorJob(parent, VisitorJob::DeclVisitKind, |
| 1580 | d, isFirst ? (void*) 1 : (void*) 0) {} |
| 1581 | static bool classof(const VisitorJob *VJ) { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 1582 | return VJ->getKind() == DeclVisitKind; |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1583 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1584 | Decl *get() const { return static_cast<Decl*>(data[0]); } |
| 1585 | bool isFirst() const { return data[1] ? true : false; } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1586 | }; |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1587 | class TypeLocVisit : public VisitorJob { |
| 1588 | public: |
| 1589 | TypeLocVisit(TypeLoc tl, CXCursor parent) : |
| 1590 | VisitorJob(parent, VisitorJob::TypeLocVisitKind, |
| 1591 | tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {} |
| 1592 | |
| 1593 | static bool classof(const VisitorJob *VJ) { |
| 1594 | return VJ->getKind() == TypeLocVisitKind; |
| 1595 | } |
| 1596 | |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 1597 | TypeLoc get() const { |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1598 | QualType T = QualType::getFromOpaquePtr(data[0]); |
| 1599 | return TypeLoc(T, data[1]); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1600 | } |
| 1601 | }; |
| 1602 | |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1603 | class LabelRefVisit : public VisitorJob { |
| 1604 | public: |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1605 | LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent) |
| 1606 | : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD, |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1607 | labelLoc.getPtrEncoding()) {} |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1608 | |
| 1609 | static bool classof(const VisitorJob *VJ) { |
| 1610 | return VJ->getKind() == VisitorJob::LabelRefVisitKind; |
| 1611 | } |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1612 | LabelDecl *get() const { return static_cast<LabelDecl*>(data[0]); } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1613 | SourceLocation getLoc() const { |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1614 | return SourceLocation::getFromPtrEncoding(data[1]); } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1615 | }; |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1616 | |
| 1617 | class NestedNameSpecifierLocVisit : public VisitorJob { |
| 1618 | public: |
| 1619 | NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent) |
| 1620 | : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind, |
| 1621 | Qualifier.getNestedNameSpecifier(), |
| 1622 | Qualifier.getOpaqueData()) { } |
| 1623 | |
| 1624 | static bool classof(const VisitorJob *VJ) { |
| 1625 | return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind; |
| 1626 | } |
| 1627 | |
| 1628 | NestedNameSpecifierLoc get() const { |
| 1629 | return NestedNameSpecifierLoc(static_cast<NestedNameSpecifier*>(data[0]), |
| 1630 | data[1]); |
| 1631 | } |
| 1632 | }; |
| 1633 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1634 | class DeclarationNameInfoVisit : public VisitorJob { |
| 1635 | public: |
| 1636 | DeclarationNameInfoVisit(Stmt *S, CXCursor parent) |
| 1637 | : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {} |
| 1638 | static bool classof(const VisitorJob *VJ) { |
| 1639 | return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind; |
| 1640 | } |
| 1641 | DeclarationNameInfo get() const { |
| 1642 | Stmt *S = static_cast<Stmt*>(data[0]); |
| 1643 | switch (S->getStmtClass()) { |
| 1644 | default: |
| 1645 | llvm_unreachable("Unhandled Stmt"); |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 1646 | case clang::Stmt::MSDependentExistsStmtClass: |
| 1647 | return cast<MSDependentExistsStmt>(S)->getNameInfo(); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1648 | case Stmt::CXXDependentScopeMemberExprClass: |
| 1649 | return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo(); |
| 1650 | case Stmt::DependentScopeDeclRefExprClass: |
| 1651 | return cast<DependentScopeDeclRefExpr>(S)->getNameInfo(); |
| 1652 | } |
| 1653 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1654 | }; |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1655 | class MemberRefVisit : public VisitorJob { |
| 1656 | public: |
| 1657 | MemberRefVisit(FieldDecl *D, SourceLocation L, CXCursor parent) |
| 1658 | : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D, |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1659 | L.getPtrEncoding()) {} |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1660 | static bool classof(const VisitorJob *VJ) { |
| 1661 | return VJ->getKind() == VisitorJob::MemberRefVisitKind; |
| 1662 | } |
| 1663 | FieldDecl *get() const { |
| 1664 | return static_cast<FieldDecl*>(data[0]); |
| 1665 | } |
| 1666 | SourceLocation getLoc() const { |
| 1667 | return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]); |
| 1668 | } |
| 1669 | }; |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1670 | class EnqueueVisitor : public StmtVisitor<EnqueueVisitor, void> { |
| 1671 | VisitorWorkList &WL; |
| 1672 | CXCursor Parent; |
| 1673 | public: |
| 1674 | EnqueueVisitor(VisitorWorkList &wl, CXCursor parent) |
| 1675 | : WL(wl), Parent(parent) {} |
| 1676 | |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1677 | void VisitAddrLabelExpr(AddrLabelExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1678 | void VisitBlockExpr(BlockExpr *B); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1679 | void VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
Ted Kremenek | 083c7e2 | 2010-11-13 05:38:03 +0000 | [diff] [blame] | 1680 | void VisitCompoundStmt(CompoundStmt *S); |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1681 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { /* Do nothing. */ } |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 1682 | void VisitMSDependentExistsStmt(MSDependentExistsStmt *S); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1683 | void VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1684 | void VisitCXXNewExpr(CXXNewExpr *E); |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 1685 | void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1686 | void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1687 | void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1688 | void VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); |
Ted Kremenek | b8dd1ca | 2010-11-17 00:50:41 +0000 | [diff] [blame] | 1689 | void VisitCXXTypeidExpr(CXXTypeidExpr *E); |
Ted Kremenek | 55b933a | 2010-11-17 00:50:36 +0000 | [diff] [blame] | 1690 | void VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); |
Ted Kremenek | 1e7e877 | 2010-11-17 00:50:52 +0000 | [diff] [blame] | 1691 | void VisitCXXUuidofExpr(CXXUuidofExpr *E); |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1692 | void VisitDeclRefExpr(DeclRefExpr *D); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1693 | void VisitDeclStmt(DeclStmt *S); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1694 | void VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1695 | void VisitDesignatedInitExpr(DesignatedInitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1696 | void VisitExplicitCastExpr(ExplicitCastExpr *E); |
| 1697 | void VisitForStmt(ForStmt *FS); |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1698 | void VisitGotoStmt(GotoStmt *GS); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1699 | void VisitIfStmt(IfStmt *If); |
| 1700 | void VisitInitListExpr(InitListExpr *IE); |
| 1701 | void VisitMemberExpr(MemberExpr *M); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1702 | void VisitOffsetOfExpr(OffsetOfExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1703 | void VisitObjCEncodeExpr(ObjCEncodeExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1704 | void VisitObjCMessageExpr(ObjCMessageExpr *M); |
| 1705 | void VisitOverloadExpr(OverloadExpr *E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1706 | void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1707 | void VisitStmt(Stmt *S); |
| 1708 | void VisitSwitchStmt(SwitchStmt *S); |
| 1709 | void VisitWhileStmt(WhileStmt *W); |
Ted Kremenek | 2939b6f | 2010-11-17 00:50:50 +0000 | [diff] [blame] | 1710 | void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 1711 | void VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E); |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 1712 | void VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 1713 | void VisitExpressionTraitExpr(ExpressionTraitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1714 | void VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U); |
Ted Kremenek | 9d3bf79 | 2010-11-17 00:50:43 +0000 | [diff] [blame] | 1715 | void VisitVAArgExpr(VAArgExpr *E); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 1716 | void VisitSizeOfPackExpr(SizeOfPackExpr *E); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1717 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1718 | private: |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1719 | void AddDeclarationNameInfo(Stmt *S); |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1720 | void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier); |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1721 | void AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1722 | void AddMemberRef(FieldDecl *D, SourceLocation L); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1723 | void AddStmt(Stmt *S); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1724 | void AddDecl(Decl *D, bool isFirst = true); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1725 | void AddTypeLoc(TypeSourceInfo *TI); |
| 1726 | void EnqueueChildren(Stmt *S); |
| 1727 | }; |
| 1728 | } // end anonyous namespace |
| 1729 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1730 | void EnqueueVisitor::AddDeclarationNameInfo(Stmt *S) { |
| 1731 | // 'S' should always be non-null, since it comes from the |
| 1732 | // statement we are visiting. |
| 1733 | WL.push_back(DeclarationNameInfoVisit(S, Parent)); |
| 1734 | } |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1735 | |
| 1736 | void |
| 1737 | EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 1738 | if (Qualifier) |
| 1739 | WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent)); |
| 1740 | } |
| 1741 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1742 | void EnqueueVisitor::AddStmt(Stmt *S) { |
| 1743 | if (S) |
| 1744 | WL.push_back(StmtVisit(S, Parent)); |
| 1745 | } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1746 | void EnqueueVisitor::AddDecl(Decl *D, bool isFirst) { |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1747 | if (D) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1748 | WL.push_back(DeclVisit(D, Parent, isFirst)); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1749 | } |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1750 | void EnqueueVisitor:: |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1751 | AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1752 | if (A) |
| 1753 | WL.push_back(ExplicitTemplateArgsVisit( |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1754 | const_cast<ASTTemplateArgumentListInfo*>(A), Parent)); |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1755 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1756 | void EnqueueVisitor::AddMemberRef(FieldDecl *D, SourceLocation L) { |
| 1757 | if (D) |
| 1758 | WL.push_back(MemberRefVisit(D, L, Parent)); |
| 1759 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1760 | void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) { |
| 1761 | if (TI) |
| 1762 | WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent)); |
| 1763 | } |
| 1764 | void EnqueueVisitor::EnqueueChildren(Stmt *S) { |
Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 1765 | unsigned size = WL.size(); |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1766 | for (Stmt::child_range Child = S->children(); Child; ++Child) { |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1767 | AddStmt(*Child); |
Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 1768 | } |
| 1769 | if (size == WL.size()) |
| 1770 | return; |
| 1771 | // Now reverse the entries we just added. This will match the DFS |
| 1772 | // ordering performed by the worklist. |
| 1773 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 1774 | std::reverse(I, E); |
| 1775 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1776 | void EnqueueVisitor::VisitAddrLabelExpr(AddrLabelExpr *E) { |
| 1777 | WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent)); |
| 1778 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1779 | void EnqueueVisitor::VisitBlockExpr(BlockExpr *B) { |
| 1780 | AddDecl(B->getBlockDecl()); |
| 1781 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1782 | void EnqueueVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 1783 | EnqueueChildren(E); |
| 1784 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1785 | } |
Ted Kremenek | 083c7e2 | 2010-11-13 05:38:03 +0000 | [diff] [blame] | 1786 | void EnqueueVisitor::VisitCompoundStmt(CompoundStmt *S) { |
| 1787 | for (CompoundStmt::reverse_body_iterator I = S->body_rbegin(), |
| 1788 | E = S->body_rend(); I != E; ++I) { |
| 1789 | AddStmt(*I); |
| 1790 | } |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1791 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1792 | void EnqueueVisitor:: |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 1793 | VisitMSDependentExistsStmt(MSDependentExistsStmt *S) { |
| 1794 | AddStmt(S->getSubStmt()); |
| 1795 | AddDeclarationNameInfo(S); |
| 1796 | if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc()) |
| 1797 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 1798 | } |
| 1799 | |
| 1800 | void EnqueueVisitor:: |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1801 | VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) { |
| 1802 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
| 1803 | AddDeclarationNameInfo(E); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1804 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 1805 | AddNestedNameSpecifierLoc(QualifierLoc); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1806 | if (!E->isImplicitAccess()) |
| 1807 | AddStmt(E->getBase()); |
| 1808 | } |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1809 | void EnqueueVisitor::VisitCXXNewExpr(CXXNewExpr *E) { |
| 1810 | // Enqueue the initializer or constructor arguments. |
| 1811 | for (unsigned I = E->getNumConstructorArgs(); I > 0; --I) |
| 1812 | AddStmt(E->getConstructorArg(I-1)); |
| 1813 | // Enqueue the array size, if any. |
| 1814 | AddStmt(E->getArraySize()); |
| 1815 | // Enqueue the allocated type. |
| 1816 | AddTypeLoc(E->getAllocatedTypeSourceInfo()); |
| 1817 | // Enqueue the placement arguments. |
| 1818 | for (unsigned I = E->getNumPlacementArgs(); I > 0; --I) |
| 1819 | AddStmt(E->getPlacementArg(I-1)); |
| 1820 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1821 | void EnqueueVisitor::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *CE) { |
Ted Kremenek | 8b8d8c9 | 2010-11-13 05:55:56 +0000 | [diff] [blame] | 1822 | for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I) |
| 1823 | AddStmt(CE->getArg(I-1)); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1824 | AddStmt(CE->getCallee()); |
| 1825 | AddStmt(CE->getArg(0)); |
| 1826 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1827 | void EnqueueVisitor::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { |
| 1828 | // Visit the name of the type being destroyed. |
| 1829 | AddTypeLoc(E->getDestroyedTypeInfo()); |
| 1830 | // Visit the scope type that looks disturbingly like the nested-name-specifier |
| 1831 | // but isn't. |
| 1832 | AddTypeLoc(E->getScopeTypeInfo()); |
| 1833 | // Visit the nested-name-specifier. |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1834 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 1835 | AddNestedNameSpecifierLoc(QualifierLoc); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1836 | // Visit base expression. |
| 1837 | AddStmt(E->getBase()); |
| 1838 | } |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 1839 | void EnqueueVisitor::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
| 1840 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1841 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1842 | void EnqueueVisitor::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { |
| 1843 | EnqueueChildren(E); |
| 1844 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1845 | } |
Ted Kremenek | b8dd1ca | 2010-11-17 00:50:41 +0000 | [diff] [blame] | 1846 | void EnqueueVisitor::VisitCXXTypeidExpr(CXXTypeidExpr *E) { |
| 1847 | EnqueueChildren(E); |
| 1848 | if (E->isTypeOperand()) |
| 1849 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 1850 | } |
Ted Kremenek | 55b933a | 2010-11-17 00:50:36 +0000 | [diff] [blame] | 1851 | |
| 1852 | void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr |
| 1853 | *E) { |
| 1854 | EnqueueChildren(E); |
| 1855 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1856 | } |
Ted Kremenek | 1e7e877 | 2010-11-17 00:50:52 +0000 | [diff] [blame] | 1857 | void EnqueueVisitor::VisitCXXUuidofExpr(CXXUuidofExpr *E) { |
| 1858 | EnqueueChildren(E); |
| 1859 | if (E->isTypeOperand()) |
| 1860 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 1861 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1862 | void EnqueueVisitor::VisitDeclRefExpr(DeclRefExpr *DR) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1863 | if (DR->hasExplicitTemplateArgs()) { |
| 1864 | AddExplicitTemplateArgs(&DR->getExplicitTemplateArgs()); |
| 1865 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1866 | WL.push_back(DeclRefExprParts(DR, Parent)); |
| 1867 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1868 | void EnqueueVisitor::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { |
| 1869 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
| 1870 | AddDeclarationNameInfo(E); |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 1871 | AddNestedNameSpecifierLoc(E->getQualifierLoc()); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1872 | } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1873 | void EnqueueVisitor::VisitDeclStmt(DeclStmt *S) { |
| 1874 | unsigned size = WL.size(); |
| 1875 | bool isFirst = true; |
| 1876 | for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); |
| 1877 | D != DEnd; ++D) { |
| 1878 | AddDecl(*D, isFirst); |
| 1879 | isFirst = false; |
| 1880 | } |
| 1881 | if (size == WL.size()) |
| 1882 | return; |
| 1883 | // Now reverse the entries we just added. This will match the DFS |
| 1884 | // ordering performed by the worklist. |
| 1885 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 1886 | std::reverse(I, E); |
| 1887 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1888 | void EnqueueVisitor::VisitDesignatedInitExpr(DesignatedInitExpr *E) { |
| 1889 | AddStmt(E->getInit()); |
| 1890 | typedef DesignatedInitExpr::Designator Designator; |
| 1891 | for (DesignatedInitExpr::reverse_designators_iterator |
| 1892 | D = E->designators_rbegin(), DEnd = E->designators_rend(); |
| 1893 | D != DEnd; ++D) { |
| 1894 | if (D->isFieldDesignator()) { |
| 1895 | if (FieldDecl *Field = D->getField()) |
| 1896 | AddMemberRef(Field, D->getFieldLoc()); |
| 1897 | continue; |
| 1898 | } |
| 1899 | if (D->isArrayDesignator()) { |
| 1900 | AddStmt(E->getArrayIndex(*D)); |
| 1901 | continue; |
| 1902 | } |
| 1903 | assert(D->isArrayRangeDesignator() && "Unknown designator kind"); |
| 1904 | AddStmt(E->getArrayRangeEnd(*D)); |
| 1905 | AddStmt(E->getArrayRangeStart(*D)); |
| 1906 | } |
| 1907 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1908 | void EnqueueVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) { |
| 1909 | EnqueueChildren(E); |
| 1910 | AddTypeLoc(E->getTypeInfoAsWritten()); |
| 1911 | } |
| 1912 | void EnqueueVisitor::VisitForStmt(ForStmt *FS) { |
| 1913 | AddStmt(FS->getBody()); |
| 1914 | AddStmt(FS->getInc()); |
| 1915 | AddStmt(FS->getCond()); |
| 1916 | AddDecl(FS->getConditionVariable()); |
| 1917 | AddStmt(FS->getInit()); |
| 1918 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1919 | void EnqueueVisitor::VisitGotoStmt(GotoStmt *GS) { |
| 1920 | WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent)); |
| 1921 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1922 | void EnqueueVisitor::VisitIfStmt(IfStmt *If) { |
| 1923 | AddStmt(If->getElse()); |
| 1924 | AddStmt(If->getThen()); |
| 1925 | AddStmt(If->getCond()); |
| 1926 | AddDecl(If->getConditionVariable()); |
| 1927 | } |
| 1928 | void EnqueueVisitor::VisitInitListExpr(InitListExpr *IE) { |
| 1929 | // We care about the syntactic form of the initializer list, only. |
| 1930 | if (InitListExpr *Syntactic = IE->getSyntacticForm()) |
| 1931 | IE = Syntactic; |
| 1932 | EnqueueChildren(IE); |
| 1933 | } |
| 1934 | void EnqueueVisitor::VisitMemberExpr(MemberExpr *M) { |
Douglas Gregor | 89629a7 | 2010-11-17 17:15:08 +0000 | [diff] [blame] | 1935 | WL.push_back(MemberExprParts(M, Parent)); |
| 1936 | |
| 1937 | // If the base of the member access expression is an implicit 'this', don't |
| 1938 | // visit it. |
| 1939 | // FIXME: If we ever want to show these implicit accesses, this will be |
| 1940 | // unfortunate. However, clang_getCursor() relies on this behavior. |
Douglas Gregor | 75e8504 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 1941 | if (!M->isImplicitAccess()) |
| 1942 | AddStmt(M->getBase()); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1943 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1944 | void EnqueueVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) { |
| 1945 | AddTypeLoc(E->getEncodedTypeSourceInfo()); |
| 1946 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1947 | void EnqueueVisitor::VisitObjCMessageExpr(ObjCMessageExpr *M) { |
| 1948 | EnqueueChildren(M); |
| 1949 | AddTypeLoc(M->getClassReceiverTypeInfo()); |
| 1950 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1951 | void EnqueueVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) { |
| 1952 | // Visit the components of the offsetof expression. |
| 1953 | for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) { |
| 1954 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 1955 | const OffsetOfNode &Node = E->getComponent(I-1); |
| 1956 | switch (Node.getKind()) { |
| 1957 | case OffsetOfNode::Array: |
| 1958 | AddStmt(E->getIndexExpr(Node.getArrayExprIndex())); |
| 1959 | break; |
| 1960 | case OffsetOfNode::Field: |
Abramo Bagnara | 06dec89 | 2011-03-12 09:45:03 +0000 | [diff] [blame] | 1961 | AddMemberRef(Node.getField(), Node.getSourceRange().getEnd()); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1962 | break; |
| 1963 | case OffsetOfNode::Identifier: |
| 1964 | case OffsetOfNode::Base: |
| 1965 | continue; |
| 1966 | } |
| 1967 | } |
| 1968 | // Visit the type into which we're computing the offset. |
| 1969 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1970 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1971 | void EnqueueVisitor::VisitOverloadExpr(OverloadExpr *E) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1972 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 1973 | WL.push_back(OverloadExprParts(E, Parent)); |
| 1974 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1975 | void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr( |
| 1976 | UnaryExprOrTypeTraitExpr *E) { |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 1977 | EnqueueChildren(E); |
| 1978 | if (E->isArgumentType()) |
| 1979 | AddTypeLoc(E->getArgumentTypeInfo()); |
| 1980 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1981 | void EnqueueVisitor::VisitStmt(Stmt *S) { |
| 1982 | EnqueueChildren(S); |
| 1983 | } |
| 1984 | void EnqueueVisitor::VisitSwitchStmt(SwitchStmt *S) { |
| 1985 | AddStmt(S->getBody()); |
| 1986 | AddStmt(S->getCond()); |
| 1987 | AddDecl(S->getConditionVariable()); |
| 1988 | } |
Ted Kremenek | fafa75a | 2010-11-17 00:50:39 +0000 | [diff] [blame] | 1989 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1990 | void EnqueueVisitor::VisitWhileStmt(WhileStmt *W) { |
| 1991 | AddStmt(W->getBody()); |
| 1992 | AddStmt(W->getCond()); |
| 1993 | AddDecl(W->getConditionVariable()); |
| 1994 | } |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 1995 | |
Ted Kremenek | 2939b6f | 2010-11-17 00:50:50 +0000 | [diff] [blame] | 1996 | void EnqueueVisitor::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { |
| 1997 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 1998 | } |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 1999 | |
| 2000 | void EnqueueVisitor::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) { |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2001 | AddTypeLoc(E->getRhsTypeSourceInfo()); |
Francois Pichet | 0a03a3f | 2010-12-08 09:11:05 +0000 | [diff] [blame] | 2002 | AddTypeLoc(E->getLhsTypeSourceInfo()); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2005 | void EnqueueVisitor::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { |
| 2006 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2007 | } |
| 2008 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 2009 | void EnqueueVisitor::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { |
| 2010 | EnqueueChildren(E); |
| 2011 | } |
| 2012 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2013 | void EnqueueVisitor::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U) { |
| 2014 | VisitOverloadExpr(U); |
| 2015 | if (!U->isImplicitAccess()) |
| 2016 | AddStmt(U->getBase()); |
| 2017 | } |
Ted Kremenek | 9d3bf79 | 2010-11-17 00:50:43 +0000 | [diff] [blame] | 2018 | void EnqueueVisitor::VisitVAArgExpr(VAArgExpr *E) { |
| 2019 | AddStmt(E->getSubExpr()); |
| 2020 | AddTypeLoc(E->getWrittenTypeInfo()); |
| 2021 | } |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2022 | void EnqueueVisitor::VisitSizeOfPackExpr(SizeOfPackExpr *E) { |
| 2023 | WL.push_back(SizeOfPackExprParts(E, Parent)); |
| 2024 | } |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2025 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2026 | void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, Stmt *S) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2027 | EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S); |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { |
| 2031 | if (RegionOfInterest.isValid()) { |
| 2032 | SourceRange Range = getRawCursorExtent(C); |
| 2033 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 2034 | return false; |
| 2035 | } |
| 2036 | return true; |
| 2037 | } |
| 2038 | |
| 2039 | bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { |
| 2040 | while (!WL.empty()) { |
| 2041 | // Dequeue the worklist item. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2042 | VisitorJob LI = WL.back(); |
| 2043 | WL.pop_back(); |
| 2044 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2045 | // Set the Parent field, then back to its old value once we're done. |
| 2046 | SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); |
| 2047 | |
| 2048 | switch (LI.getKind()) { |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2049 | case VisitorJob::DeclVisitKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2050 | Decl *D = cast<DeclVisit>(&LI)->get(); |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2051 | if (!D) |
| 2052 | continue; |
| 2053 | |
| 2054 | // For now, perform default visitation for Decls. |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2055 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest, |
| 2056 | cast<DeclVisit>(&LI)->isFirst()))) |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2057 | return true; |
| 2058 | |
| 2059 | continue; |
| 2060 | } |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2061 | case VisitorJob::ExplicitTemplateArgsVisitKind: { |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 2062 | const ASTTemplateArgumentListInfo *ArgList = |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2063 | cast<ExplicitTemplateArgsVisit>(&LI)->get(); |
| 2064 | for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(), |
| 2065 | *ArgEnd = Arg + ArgList->NumTemplateArgs; |
| 2066 | Arg != ArgEnd; ++Arg) { |
| 2067 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2068 | return true; |
| 2069 | } |
| 2070 | continue; |
| 2071 | } |
Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 2072 | case VisitorJob::TypeLocVisitKind: { |
| 2073 | // Perform default visitation for TypeLocs. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2074 | if (Visit(cast<TypeLocVisit>(&LI)->get())) |
Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 2075 | return true; |
| 2076 | continue; |
| 2077 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2078 | case VisitorJob::LabelRefVisitKind: { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2079 | LabelDecl *LS = cast<LabelRefVisit>(&LI)->get(); |
Ted Kremenek | e745501 | 2011-02-23 04:54:51 +0000 | [diff] [blame] | 2080 | if (LabelStmt *stmt = LS->getStmt()) { |
| 2081 | if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(), |
| 2082 | TU))) { |
| 2083 | return true; |
| 2084 | } |
| 2085 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2086 | continue; |
| 2087 | } |
Ted Kremenek | 47695c8 | 2011-08-18 22:25:21 +0000 | [diff] [blame] | 2088 | |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 2089 | case VisitorJob::NestedNameSpecifierLocVisitKind: { |
| 2090 | NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI); |
| 2091 | if (VisitNestedNameSpecifierLoc(V->get())) |
| 2092 | return true; |
| 2093 | continue; |
| 2094 | } |
| 2095 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 2096 | case VisitorJob::DeclarationNameInfoVisitKind: { |
| 2097 | if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI) |
| 2098 | ->get())) |
| 2099 | return true; |
| 2100 | continue; |
| 2101 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2102 | case VisitorJob::MemberRefVisitKind: { |
| 2103 | MemberRefVisit *V = cast<MemberRefVisit>(&LI); |
| 2104 | if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU))) |
| 2105 | return true; |
| 2106 | continue; |
| 2107 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2108 | case VisitorJob::StmtVisitKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2109 | Stmt *S = cast<StmtVisit>(&LI)->get(); |
Ted Kremenek | 8c269ac | 2010-11-11 23:11:43 +0000 | [diff] [blame] | 2110 | if (!S) |
| 2111 | continue; |
| 2112 | |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2113 | // Update the current cursor. |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2114 | CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2115 | if (!IsInRegionOfInterest(Cursor)) |
| 2116 | continue; |
| 2117 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 2118 | case CXChildVisit_Break: return true; |
| 2119 | case CXChildVisit_Continue: break; |
| 2120 | case CXChildVisit_Recurse: |
| 2121 | EnqueueWorkList(WL, S); |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2122 | break; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2123 | } |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2124 | continue; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2125 | } |
| 2126 | case VisitorJob::MemberExprPartsKind: { |
| 2127 | // Handle the other pieces in the MemberExpr besides the base. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2128 | MemberExpr *M = cast<MemberExprParts>(&LI)->get(); |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2129 | |
| 2130 | // Visit the nested-name-specifier |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2131 | if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc()) |
| 2132 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2133 | return true; |
| 2134 | |
| 2135 | // Visit the declaration name. |
| 2136 | if (VisitDeclarationNameInfo(M->getMemberNameInfo())) |
| 2137 | return true; |
| 2138 | |
| 2139 | // Visit the explicitly-specified template arguments, if any. |
| 2140 | if (M->hasExplicitTemplateArgs()) { |
| 2141 | for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), |
| 2142 | *ArgEnd = Arg + M->getNumTemplateArgs(); |
| 2143 | Arg != ArgEnd; ++Arg) { |
| 2144 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2145 | return true; |
| 2146 | } |
| 2147 | } |
| 2148 | continue; |
| 2149 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2150 | case VisitorJob::DeclRefExprPartsKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2151 | DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get(); |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2152 | // Visit nested-name-specifier, if present. |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2153 | if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc()) |
| 2154 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2155 | return true; |
| 2156 | // Visit declaration name. |
| 2157 | if (VisitDeclarationNameInfo(DR->getNameInfo())) |
| 2158 | return true; |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2159 | continue; |
| 2160 | } |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2161 | case VisitorJob::OverloadExprPartsKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2162 | OverloadExpr *O = cast<OverloadExprParts>(&LI)->get(); |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2163 | // Visit the nested-name-specifier. |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2164 | if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc()) |
| 2165 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2166 | return true; |
| 2167 | // Visit the declaration name. |
| 2168 | if (VisitDeclarationNameInfo(O->getNameInfo())) |
| 2169 | return true; |
| 2170 | // Visit the overloaded declaration reference. |
| 2171 | if (Visit(MakeCursorOverloadedDeclRef(O, TU))) |
| 2172 | return true; |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2173 | continue; |
| 2174 | } |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2175 | case VisitorJob::SizeOfPackExprPartsKind: { |
| 2176 | SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get(); |
| 2177 | NamedDecl *Pack = E->getPack(); |
| 2178 | if (isa<TemplateTypeParmDecl>(Pack)) { |
| 2179 | if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack), |
| 2180 | E->getPackLoc(), TU))) |
| 2181 | return true; |
| 2182 | |
| 2183 | continue; |
| 2184 | } |
| 2185 | |
| 2186 | if (isa<TemplateTemplateParmDecl>(Pack)) { |
| 2187 | if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack), |
| 2188 | E->getPackLoc(), TU))) |
| 2189 | return true; |
| 2190 | |
| 2191 | continue; |
| 2192 | } |
| 2193 | |
| 2194 | // Non-type template parameter packs and function parameter packs are |
| 2195 | // treated like DeclRefExpr cursors. |
| 2196 | continue; |
| 2197 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2198 | } |
| 2199 | } |
| 2200 | return false; |
| 2201 | } |
| 2202 | |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2203 | bool CursorVisitor::Visit(Stmt *S) { |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 2204 | VisitorWorkList *WL = 0; |
| 2205 | if (!WorkListFreeList.empty()) { |
| 2206 | WL = WorkListFreeList.back(); |
| 2207 | WL->clear(); |
| 2208 | WorkListFreeList.pop_back(); |
| 2209 | } |
| 2210 | else { |
| 2211 | WL = new VisitorWorkList(); |
| 2212 | WorkListCache.push_back(WL); |
| 2213 | } |
| 2214 | EnqueueWorkList(*WL, S); |
| 2215 | bool result = RunVisitorWorkList(*WL); |
| 2216 | WorkListFreeList.push_back(WL); |
| 2217 | return result; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2218 | } |
| 2219 | |
Francois Pichet | 48a8d14 | 2011-07-25 22:00:44 +0000 | [diff] [blame] | 2220 | namespace { |
| 2221 | typedef llvm::SmallVector<SourceRange, 4> RefNamePieces; |
| 2222 | RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr, |
| 2223 | const DeclarationNameInfo &NI, |
| 2224 | const SourceRange &QLoc, |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 2225 | const ASTTemplateArgumentListInfo *TemplateArgs = 0){ |
Francois Pichet | 48a8d14 | 2011-07-25 22:00:44 +0000 | [diff] [blame] | 2226 | const bool WantQualifier = NameFlags & CXNameRange_WantQualifier; |
| 2227 | const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs; |
| 2228 | const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece; |
| 2229 | |
| 2230 | const DeclarationName::NameKind Kind = NI.getName().getNameKind(); |
| 2231 | |
| 2232 | RefNamePieces Pieces; |
| 2233 | |
| 2234 | if (WantQualifier && QLoc.isValid()) |
| 2235 | Pieces.push_back(QLoc); |
| 2236 | |
| 2237 | if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr) |
| 2238 | Pieces.push_back(NI.getLoc()); |
| 2239 | |
| 2240 | if (WantTemplateArgs && TemplateArgs) |
| 2241 | Pieces.push_back(SourceRange(TemplateArgs->LAngleLoc, |
| 2242 | TemplateArgs->RAngleLoc)); |
| 2243 | |
| 2244 | if (Kind == DeclarationName::CXXOperatorName) { |
| 2245 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 2246 | NI.getInfo().CXXOperatorName.BeginOpNameLoc)); |
| 2247 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 2248 | NI.getInfo().CXXOperatorName.EndOpNameLoc)); |
| 2249 | } |
| 2250 | |
| 2251 | if (WantSinglePiece) { |
| 2252 | SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd()); |
| 2253 | Pieces.clear(); |
| 2254 | Pieces.push_back(R); |
| 2255 | } |
| 2256 | |
| 2257 | return Pieces; |
| 2258 | } |
| 2259 | } |
| 2260 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2261 | //===----------------------------------------------------------------------===// |
| 2262 | // Misc. API hooks. |
| 2263 | //===----------------------------------------------------------------------===// |
| 2264 | |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2265 | static llvm::sys::Mutex EnableMultithreadingMutex; |
| 2266 | static bool EnabledMultithreading; |
| 2267 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 2268 | extern "C" { |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2269 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 2270 | int displayDiagnostics) { |
Daniel Dunbar | 48615ff | 2010-10-08 19:30:33 +0000 | [diff] [blame] | 2271 | // Disable pretty stack trace functionality, which will otherwise be a very |
| 2272 | // poor citizen of the world and set up all sorts of signal handlers. |
| 2273 | llvm::DisablePrettyStackTrace = true; |
| 2274 | |
Daniel Dunbar | c7df4f3 | 2010-08-18 18:43:14 +0000 | [diff] [blame] | 2275 | // We use crash recovery to make some of our APIs more reliable, implicitly |
| 2276 | // enable it. |
| 2277 | llvm::CrashRecoveryContext::Enable(); |
| 2278 | |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2279 | // Enable support for multithreading in LLVM. |
| 2280 | { |
| 2281 | llvm::sys::ScopedLock L(EnableMultithreadingMutex); |
| 2282 | if (!EnabledMultithreading) { |
| 2283 | llvm::llvm_start_multithreaded(); |
| 2284 | EnabledMultithreading = true; |
| 2285 | } |
| 2286 | } |
| 2287 | |
Douglas Gregor | a030b7c | 2010-01-22 20:35:53 +0000 | [diff] [blame] | 2288 | CIndexer *CIdxr = new CIndexer(); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2289 | if (excludeDeclarationsFromPCH) |
| 2290 | CIdxr->setOnlyLocalDecls(); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2291 | if (displayDiagnostics) |
| 2292 | CIdxr->setDisplayDiagnostics(); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2293 | return CIdxr; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2296 | void clang_disposeIndex(CXIndex CIdx) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2297 | if (CIdx) |
| 2298 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
Ted Kremenek | d2427dd | 2011-03-18 23:05:39 +0000 | [diff] [blame] | 2301 | void clang_toggleCrashRecovery(unsigned isEnabled) { |
| 2302 | if (isEnabled) |
| 2303 | llvm::CrashRecoveryContext::Enable(); |
| 2304 | else |
| 2305 | llvm::CrashRecoveryContext::Disable(); |
| 2306 | } |
| 2307 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2308 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2309 | const char *ast_filename) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2310 | if (!CIdx) |
| 2311 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2312 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 2313 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 2314 | FileSystemOptions FileSystemOpts; |
| 2315 | FileSystemOpts.WorkingDir = CXXIdx->getWorkingDirectory(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2316 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2317 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags; |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2318 | ASTUnit *TU = ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2319 | CXXIdx->getOnlyLocalDecls(), |
| 2320 | 0, 0, true); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2321 | return MakeCXTranslationUnit(TU); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2322 | } |
| 2323 | |
Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 2324 | unsigned clang_defaultEditingTranslationUnitOptions() { |
Douglas Gregor | 2a2c50b | 2010-09-27 05:49:58 +0000 | [diff] [blame] | 2325 | return CXTranslationUnit_PrecompiledPreamble | |
Douglas Gregor | b5af843 | 2011-08-25 22:54:01 +0000 | [diff] [blame] | 2326 | CXTranslationUnit_CacheCompletionResults; |
Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2329 | CXTranslationUnit |
| 2330 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 2331 | const char *source_filename, |
| 2332 | int num_command_line_args, |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2333 | const char * const *command_line_args, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2334 | unsigned num_unsaved_files, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2335 | struct CXUnsavedFile *unsaved_files) { |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 2336 | unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord | |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2337 | CXTranslationUnit_NestedMacroExpansions; |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2338 | return clang_parseTranslationUnit(CIdx, source_filename, |
| 2339 | command_line_args, num_command_line_args, |
| 2340 | unsaved_files, num_unsaved_files, |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 2341 | Options); |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2342 | } |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2343 | |
| 2344 | struct ParseTranslationUnitInfo { |
| 2345 | CXIndex CIdx; |
| 2346 | const char *source_filename; |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2347 | const char *const *command_line_args; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2348 | int num_command_line_args; |
| 2349 | struct CXUnsavedFile *unsaved_files; |
| 2350 | unsigned num_unsaved_files; |
| 2351 | unsigned options; |
| 2352 | CXTranslationUnit result; |
| 2353 | }; |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2354 | static void clang_parseTranslationUnit_Impl(void *UserData) { |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2355 | ParseTranslationUnitInfo *PTUI = |
| 2356 | static_cast<ParseTranslationUnitInfo*>(UserData); |
| 2357 | CXIndex CIdx = PTUI->CIdx; |
| 2358 | const char *source_filename = PTUI->source_filename; |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2359 | const char * const *command_line_args = PTUI->command_line_args; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2360 | int num_command_line_args = PTUI->num_command_line_args; |
| 2361 | struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files; |
| 2362 | unsigned num_unsaved_files = PTUI->num_unsaved_files; |
| 2363 | unsigned options = PTUI->options; |
| 2364 | PTUI->result = 0; |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2365 | |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2366 | if (!CIdx) |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2367 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2368 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2369 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 2370 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2371 | bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 2372 | // FIXME: Add a flag for modules. |
| 2373 | TranslationUnitKind TUKind |
| 2374 | = (options & CXTranslationUnit_Incomplete)? TU_Prefix : TU_Complete; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2375 | bool CacheCodeCompetionResults |
| 2376 | = options & CXTranslationUnit_CacheCompletionResults; |
| 2377 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2378 | // Configure the diagnostics. |
| 2379 | DiagnosticOptions DiagOpts; |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2380 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2381 | Diags(CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args, |
| 2382 | command_line_args)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2383 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2384 | // Recover resources if we crash before exiting this function. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2385 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 2386 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2387 | DiagCleanup(Diags.getPtr()); |
| 2388 | |
| 2389 | llvm::OwningPtr<std::vector<ASTUnit::RemappedFile> > |
| 2390 | RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); |
| 2391 | |
| 2392 | // Recover resources if we crash before exiting this function. |
| 2393 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 2394 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 2395 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2396 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2397 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2398 | const llvm::MemoryBuffer *Buffer |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 2399 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2400 | RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, |
| 2401 | Buffer)); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2402 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2403 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2404 | llvm::OwningPtr<std::vector<const char *> > |
| 2405 | Args(new std::vector<const char*>()); |
| 2406 | |
| 2407 | // Recover resources if we crash before exiting this method. |
| 2408 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > |
| 2409 | ArgsCleanup(Args.get()); |
| 2410 | |
Douglas Gregor | 52ddc5d | 2010-07-09 18:39:07 +0000 | [diff] [blame] | 2411 | // Since the Clang C library is primarily used by batch tools dealing with |
| 2412 | // (often very broken) source code, where spell-checking can have a |
| 2413 | // significant negative impact on performance (particularly when |
| 2414 | // precompiled headers are involved), we disable it by default. |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2415 | // Only do this if we haven't found a spell-checking-related argument. |
| 2416 | bool FoundSpellCheckingArgument = false; |
| 2417 | for (int I = 0; I != num_command_line_args; ++I) { |
| 2418 | if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || |
| 2419 | strcmp(command_line_args[I], "-fspell-checking") == 0) { |
| 2420 | FoundSpellCheckingArgument = true; |
| 2421 | break; |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2422 | } |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2423 | } |
| 2424 | if (!FoundSpellCheckingArgument) |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2425 | Args->push_back("-fno-spell-checking"); |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2426 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2427 | Args->insert(Args->end(), command_line_args, |
| 2428 | command_line_args + num_command_line_args); |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 2429 | |
Argyrios Kyrtzidis | c842955 | 2011-03-20 18:17:52 +0000 | [diff] [blame] | 2430 | // The 'source_filename' argument is optional. If the caller does not |
| 2431 | // specify it then it is assumed that the source file is specified |
| 2432 | // in the actual argument list. |
| 2433 | // Put the source file after command_line_args otherwise if '-x' flag is |
| 2434 | // present it will be unused. |
| 2435 | if (source_filename) |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2436 | Args->push_back(source_filename); |
Argyrios Kyrtzidis | c842955 | 2011-03-20 18:17:52 +0000 | [diff] [blame] | 2437 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2438 | // Do we need the detailed preprocessing record? |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2439 | bool NestedMacroExpansions = false; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2440 | if (options & CXTranslationUnit_DetailedPreprocessingRecord) { |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2441 | Args->push_back("-Xclang"); |
| 2442 | Args->push_back("-detailed-preprocessing-record"); |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2443 | NestedMacroExpansions |
| 2444 | = (options & CXTranslationUnit_NestedMacroExpansions); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
Argyrios Kyrtzidis | 026f691 | 2010-11-18 21:47:04 +0000 | [diff] [blame] | 2447 | unsigned NumErrors = Diags->getClient()->getNumErrors(); |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2448 | llvm::OwningPtr<ASTUnit> Unit( |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2449 | ASTUnit::LoadFromCommandLine(Args->size() ? &(*Args)[0] : 0 |
| 2450 | /* vector::data() not portable */, |
| 2451 | Args->size() ? (&(*Args)[0] + Args->size()) :0, |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2452 | Diags, |
| 2453 | CXXIdx->getClangResourcesPath(), |
| 2454 | CXXIdx->getOnlyLocalDecls(), |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 2455 | /*CaptureDiagnostics=*/true, |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2456 | RemappedFiles->size() ? &(*RemappedFiles)[0]:0, |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2457 | RemappedFiles->size(), |
Argyrios Kyrtzidis | 299a4a9 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 2458 | /*RemappedFilesKeepOriginalName=*/true, |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2459 | PrecompilePreamble, |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 2460 | TUKind, |
Douglas Gregor | 99ba202 | 2010-10-27 17:24:53 +0000 | [diff] [blame] | 2461 | CacheCodeCompetionResults, |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2462 | NestedMacroExpansions)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2463 | |
Argyrios Kyrtzidis | 026f691 | 2010-11-18 21:47:04 +0000 | [diff] [blame] | 2464 | if (NumErrors != Diags->getClient()->getNumErrors()) { |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2465 | // Make sure to check that 'Unit' is non-NULL. |
| 2466 | if (CXXIdx->getDisplayDiagnostics() && Unit.get()) { |
| 2467 | for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), |
| 2468 | DEnd = Unit->stored_diag_end(); |
| 2469 | D != DEnd; ++D) { |
| 2470 | CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions()); |
| 2471 | CXString Msg = clang_formatDiagnostic(&Diag, |
| 2472 | clang_defaultDiagnosticDisplayOptions()); |
| 2473 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 2474 | clang_disposeString(Msg); |
| 2475 | } |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 2476 | #ifdef LLVM_ON_WIN32 |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2477 | // On Windows, force a flush, since there may be multiple copies of |
| 2478 | // stderr and stdout in the file system, all with different buffers |
| 2479 | // but writing to the same device. |
| 2480 | fflush(stderr); |
| 2481 | #endif |
| 2482 | } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2483 | } |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 2484 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2485 | PTUI->result = MakeCXTranslationUnit(Unit.take()); |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2486 | } |
| 2487 | CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, |
| 2488 | const char *source_filename, |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2489 | const char * const *command_line_args, |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2490 | int num_command_line_args, |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2491 | struct CXUnsavedFile *unsaved_files, |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2492 | unsigned num_unsaved_files, |
| 2493 | unsigned options) { |
| 2494 | ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args, |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2495 | num_command_line_args, unsaved_files, |
| 2496 | num_unsaved_files, options, 0 }; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2497 | llvm::CrashRecoveryContext CRC; |
| 2498 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 2499 | if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) { |
Daniel Dunbar | 60a4543 | 2010-08-23 22:35:34 +0000 | [diff] [blame] | 2500 | fprintf(stderr, "libclang: crash detected during parsing: {\n"); |
| 2501 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 2502 | fprintf(stderr, " 'command_line_args' : ["); |
| 2503 | for (int i = 0; i != num_command_line_args; ++i) { |
| 2504 | if (i) |
| 2505 | fprintf(stderr, ", "); |
| 2506 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 2507 | } |
| 2508 | fprintf(stderr, "],\n"); |
| 2509 | fprintf(stderr, " 'unsaved_files' : ["); |
| 2510 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 2511 | if (i) |
| 2512 | fprintf(stderr, ", "); |
| 2513 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 2514 | unsaved_files[i].Length); |
| 2515 | } |
| 2516 | fprintf(stderr, "],\n"); |
| 2517 | fprintf(stderr, " 'options' : %d,\n", options); |
| 2518 | fprintf(stderr, "}\n"); |
| 2519 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2520 | return 0; |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2521 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
| 2522 | PrintLibclangResourceUsage(PTUI.result); |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2523 | } |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2524 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2525 | return PTUI.result; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
Douglas Gregor | 1999844 | 2010-08-13 15:35:05 +0000 | [diff] [blame] | 2528 | unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { |
| 2529 | return CXSaveTranslationUnit_None; |
| 2530 | } |
| 2531 | |
| 2532 | int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, |
| 2533 | unsigned options) { |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2534 | if (!TU) |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 2535 | return CXSaveError_InvalidTU; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2536 | |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 2537 | CXSaveError result = static_cast<ASTUnit *>(TU->TUData)->Save(FileName); |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2538 | if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 2539 | PrintLibclangResourceUsage(TU); |
| 2540 | return result; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2541 | } |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2542 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2543 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2544 | if (CTUnit) { |
| 2545 | // If the translation unit has been marked as unsafe to free, just discard |
| 2546 | // it. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2547 | if (static_cast<ASTUnit *>(CTUnit->TUData)->isUnsafeToFree()) |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2548 | return; |
| 2549 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2550 | delete static_cast<ASTUnit *>(CTUnit->TUData); |
| 2551 | disposeCXStringPool(CTUnit->StringPool); |
| 2552 | delete CTUnit; |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2553 | } |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 2554 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2555 | |
Douglas Gregor | e1e13bf | 2010-08-11 15:58:42 +0000 | [diff] [blame] | 2556 | unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { |
| 2557 | return CXReparse_None; |
| 2558 | } |
| 2559 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2560 | struct ReparseTranslationUnitInfo { |
| 2561 | CXTranslationUnit TU; |
| 2562 | unsigned num_unsaved_files; |
| 2563 | struct CXUnsavedFile *unsaved_files; |
| 2564 | unsigned options; |
| 2565 | int result; |
| 2566 | }; |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2567 | |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2568 | static void clang_reparseTranslationUnit_Impl(void *UserData) { |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2569 | ReparseTranslationUnitInfo *RTUI = |
| 2570 | static_cast<ReparseTranslationUnitInfo*>(UserData); |
| 2571 | CXTranslationUnit TU = RTUI->TU; |
| 2572 | unsigned num_unsaved_files = RTUI->num_unsaved_files; |
| 2573 | struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files; |
| 2574 | unsigned options = RTUI->options; |
| 2575 | (void) options; |
| 2576 | RTUI->result = 1; |
| 2577 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2578 | if (!TU) |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2579 | return; |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2580 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2581 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2582 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2583 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2584 | llvm::OwningPtr<std::vector<ASTUnit::RemappedFile> > |
| 2585 | RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); |
| 2586 | |
| 2587 | // Recover resources if we crash before exiting this function. |
| 2588 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 2589 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 2590 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2591 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2592 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2593 | const llvm::MemoryBuffer *Buffer |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2594 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2595 | RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, |
| 2596 | Buffer)); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2599 | if (!CXXUnit->Reparse(RemappedFiles->size() ? &(*RemappedFiles)[0] : 0, |
| 2600 | RemappedFiles->size())) |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2601 | RTUI->result = 0; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2602 | } |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2603 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2604 | int clang_reparseTranslationUnit(CXTranslationUnit TU, |
| 2605 | unsigned num_unsaved_files, |
| 2606 | struct CXUnsavedFile *unsaved_files, |
| 2607 | unsigned options) { |
| 2608 | ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files, |
| 2609 | options, 0 }; |
Argyrios Kyrtzidis | 8c4b47e | 2011-10-28 22:54:33 +0000 | [diff] [blame] | 2610 | |
Argyrios Kyrtzidis | e7de9b4 | 2011-10-29 19:32:39 +0000 | [diff] [blame] | 2611 | if (getenv("LIBCLANG_NOTHREADS")) { |
Argyrios Kyrtzidis | 8c4b47e | 2011-10-28 22:54:33 +0000 | [diff] [blame] | 2612 | clang_reparseTranslationUnit_Impl(&RTUI); |
| 2613 | return RTUI.result; |
| 2614 | } |
| 2615 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2616 | llvm::CrashRecoveryContext CRC; |
| 2617 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 2618 | if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) { |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2619 | fprintf(stderr, "libclang: crash detected during reparsing\n"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2620 | static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true); |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2621 | return 1; |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2622 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 2623 | PrintLibclangResourceUsage(TU); |
Ted Kremenek | 1dfb26a | 2010-10-29 01:06:50 +0000 | [diff] [blame] | 2624 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2625 | return RTUI.result; |
| 2626 | } |
| 2627 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2628 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2629 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2630 | if (!CTUnit) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2631 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2632 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2633 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit->TUData); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2634 | return createCXString(CXXUnit->getOriginalSourceFileName(), true); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 2635 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 2636 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2637 | CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2638 | CXCursor Result = { CXCursor_TranslationUnit, 0, { 0, 0, TU } }; |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2639 | return Result; |
| 2640 | } |
| 2641 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2642 | } // end: extern "C" |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2643 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2644 | //===----------------------------------------------------------------------===// |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2645 | // CXFile Operations. |
| 2646 | //===----------------------------------------------------------------------===// |
| 2647 | |
| 2648 | extern "C" { |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2649 | CXString clang_getFileName(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2650 | if (!SFile) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2651 | return createCXString((const char*)NULL); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2652 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2653 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2654 | return createCXString(FEnt->getName()); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | time_t clang_getFileTime(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2658 | if (!SFile) |
| 2659 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2660 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2661 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 2662 | return FEnt->getModificationTime(); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2663 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2664 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2665 | CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) { |
| 2666 | if (!tu) |
| 2667 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2668 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2669 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2670 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2671 | FileManager &FMgr = CXXUnit->getFileManager(); |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 2672 | return const_cast<FileEntry *>(FMgr.getFile(file_name)); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2673 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2674 | |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 2675 | unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit tu, CXFile file) { |
| 2676 | if (!tu || !file) |
| 2677 | return 0; |
| 2678 | |
| 2679 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
| 2680 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
| 2681 | return CXXUnit->getPreprocessor().getHeaderSearchInfo() |
| 2682 | .isFileMultipleIncludeGuarded(FEnt); |
| 2683 | } |
| 2684 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2685 | } // end: extern "C" |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2686 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2687 | //===----------------------------------------------------------------------===// |
| 2688 | // CXCursor Operations. |
| 2689 | //===----------------------------------------------------------------------===// |
| 2690 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2691 | static Decl *getDeclFromExpr(Stmt *E) { |
Argyrios Kyrtzidis | c295461 | 2011-09-12 22:17:26 +0000 | [diff] [blame] | 2692 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2693 | return getDeclFromExpr(CE->getSubExpr()); |
| 2694 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2695 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 2696 | return RefExpr->getDecl(); |
Douglas Gregor | 38f28c1 | 2010-10-22 22:24:08 +0000 | [diff] [blame] | 2697 | if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) |
| 2698 | return RefExpr->getDecl(); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2699 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 2700 | return ME->getMemberDecl(); |
| 2701 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 2702 | return RE->getDecl(); |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2703 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 2704 | return PRE->isExplicitProperty() ? PRE->getExplicitProperty() : 0; |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2705 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2706 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 2707 | return getDeclFromExpr(CE->getCallee()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2708 | if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 2709 | if (!CE->isElidable()) |
| 2710 | return CE->getConstructor(); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2711 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 2712 | return OME->getMethodDecl(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2713 | |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2714 | if (ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) |
| 2715 | return PE->getProtocol(); |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 2716 | if (SubstNonTypeTemplateParmPackExpr *NTTP |
| 2717 | = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E)) |
| 2718 | return NTTP->getParameterPack(); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2719 | if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
| 2720 | if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || |
| 2721 | isa<ParmVarDecl>(SizeOfPack->getPack())) |
| 2722 | return SizeOfPack->getPack(); |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2723 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2724 | return 0; |
| 2725 | } |
| 2726 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2727 | static SourceLocation getLocationFromExpr(Expr *E) { |
Argyrios Kyrtzidis | c295461 | 2011-09-12 22:17:26 +0000 | [diff] [blame] | 2728 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
| 2729 | return getLocationFromExpr(CE->getSubExpr()); |
| 2730 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2731 | if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
| 2732 | return /*FIXME:*/Msg->getLeftLoc(); |
| 2733 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 2734 | return DRE->getLocation(); |
Douglas Gregor | 38f28c1 | 2010-10-22 22:24:08 +0000 | [diff] [blame] | 2735 | if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) |
| 2736 | return RefExpr->getLocation(); |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2737 | if (MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
| 2738 | return Member->getMemberLoc(); |
| 2739 | if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
| 2740 | return Ivar->getLocation(); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2741 | if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
| 2742 | return SizeOfPack->getPackLoc(); |
| 2743 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2744 | return E->getLocStart(); |
| 2745 | } |
| 2746 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2747 | extern "C" { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2748 | |
| 2749 | unsigned clang_visitChildren(CXCursor parent, |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 2750 | CXCursorVisitor visitor, |
| 2751 | CXClientData client_data) { |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 2752 | CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, |
| 2753 | /*VisitPreprocessorLast=*/false); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 2754 | return CursorVis.VisitChildren(parent); |
| 2755 | } |
| 2756 | |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 2757 | #ifndef __has_feature |
| 2758 | #define __has_feature(x) 0 |
| 2759 | #endif |
| 2760 | #if __has_feature(blocks) |
| 2761 | typedef enum CXChildVisitResult |
| 2762 | (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent); |
| 2763 | |
| 2764 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 2765 | CXClientData client_data) { |
| 2766 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 2767 | return block(cursor, parent); |
| 2768 | } |
| 2769 | #else |
| 2770 | // If we are compiled with a compiler that doesn't have native blocks support, |
| 2771 | // define and call the block manually, so the |
| 2772 | typedef struct _CXChildVisitResult |
| 2773 | { |
| 2774 | void *isa; |
| 2775 | int flags; |
| 2776 | int reserved; |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2777 | enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, |
| 2778 | CXCursor); |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 2779 | } *CXCursorVisitorBlock; |
| 2780 | |
| 2781 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 2782 | CXClientData client_data) { |
| 2783 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 2784 | return block->invoke(block, cursor, parent); |
| 2785 | } |
| 2786 | #endif |
| 2787 | |
| 2788 | |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2789 | unsigned clang_visitChildrenWithBlock(CXCursor parent, |
| 2790 | CXCursorVisitorBlock block) { |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 2791 | return clang_visitChildren(parent, visitWithBlock, block); |
| 2792 | } |
| 2793 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2794 | static CXString getDeclSpelling(Decl *D) { |
| 2795 | NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 2796 | if (!ND) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2797 | if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 2798 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 2799 | return createCXString(Property->getIdentifier()->getName()); |
| 2800 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2801 | return createCXString(""); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2804 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2805 | return createCXString(OMD->getSelector().getAsString()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2806 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2807 | if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
| 2808 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 2809 | // and returns different names. NamedDecl returns the class name and |
| 2810 | // ObjCCategoryImplDecl returns the category name. |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2811 | return createCXString(CIMP->getIdentifier()->getNameStart()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2812 | |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 2813 | if (isa<UsingDirectiveDecl>(D)) |
| 2814 | return createCXString(""); |
| 2815 | |
Ted Kremenek | 50aa6ac | 2010-05-19 21:51:10 +0000 | [diff] [blame] | 2816 | llvm::SmallString<1024> S; |
| 2817 | llvm::raw_svector_ostream os(S); |
| 2818 | ND->printName(os); |
| 2819 | |
| 2820 | return createCXString(os.str()); |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2821 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2822 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2823 | CXString clang_getCursorSpelling(CXCursor C) { |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2824 | if (clang_isTranslationUnit(C.kind)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2825 | return clang_getTranslationUnitSpelling( |
| 2826 | static_cast<CXTranslationUnit>(C.data[2])); |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2827 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 2828 | if (clang_isReference(C.kind)) { |
| 2829 | switch (C.kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2830 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 2831 | ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2832 | return createCXString(Super->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2833 | } |
| 2834 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 2835 | ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2836 | return createCXString(Class->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2837 | } |
| 2838 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 2839 | ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 2840 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2841 | return createCXString(OID->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2842 | } |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 2843 | case CXCursor_CXXBaseSpecifier: { |
| 2844 | CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); |
| 2845 | return createCXString(B->getType().getAsString()); |
| 2846 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2847 | case CXCursor_TypeRef: { |
| 2848 | TypeDecl *Type = getCursorTypeRef(C).first; |
| 2849 | assert(Type && "Missing type decl"); |
| 2850 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2851 | return createCXString(getCursorContext(C).getTypeDeclType(Type). |
| 2852 | getAsString()); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2853 | } |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 2854 | case CXCursor_TemplateRef: { |
| 2855 | TemplateDecl *Template = getCursorTemplateRef(C).first; |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 2856 | assert(Template && "Missing template decl"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 2857 | |
| 2858 | return createCXString(Template->getNameAsString()); |
| 2859 | } |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 2860 | |
| 2861 | case CXCursor_NamespaceRef: { |
| 2862 | NamedDecl *NS = getCursorNamespaceRef(C).first; |
| 2863 | assert(NS && "Missing namespace decl"); |
| 2864 | |
| 2865 | return createCXString(NS->getNameAsString()); |
| 2866 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2867 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 2868 | case CXCursor_MemberRef: { |
| 2869 | FieldDecl *Field = getCursorMemberRef(C).first; |
| 2870 | assert(Field && "Missing member decl"); |
| 2871 | |
| 2872 | return createCXString(Field->getNameAsString()); |
| 2873 | } |
| 2874 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 2875 | case CXCursor_LabelRef: { |
| 2876 | LabelStmt *Label = getCursorLabelRef(C).first; |
| 2877 | assert(Label && "Missing label"); |
| 2878 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2879 | return createCXString(Label->getName()); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 2882 | case CXCursor_OverloadedDeclRef: { |
| 2883 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
| 2884 | if (Decl *D = Storage.dyn_cast<Decl *>()) { |
| 2885 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 2886 | return createCXString(ND->getNameAsString()); |
| 2887 | return createCXString(""); |
| 2888 | } |
| 2889 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
| 2890 | return createCXString(E->getName().getAsString()); |
| 2891 | OverloadedTemplateStorage *Ovl |
| 2892 | = Storage.get<OverloadedTemplateStorage*>(); |
| 2893 | if (Ovl->size() == 0) |
| 2894 | return createCXString(""); |
| 2895 | return createCXString((*Ovl->begin())->getNameAsString()); |
| 2896 | } |
| 2897 | |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2898 | default: |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2899 | return createCXString("<not implemented>"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 2900 | } |
| 2901 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 2902 | |
| 2903 | if (clang_isExpression(C.kind)) { |
| 2904 | Decl *D = getDeclFromExpr(getCursorExpr(C)); |
| 2905 | if (D) |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2906 | return getDeclSpelling(D); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2907 | return createCXString(""); |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 2910 | if (clang_isStatement(C.kind)) { |
| 2911 | Stmt *S = getCursorStmt(C); |
| 2912 | if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2913 | return createCXString(Label->getName()); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 2914 | |
| 2915 | return createCXString(""); |
| 2916 | } |
| 2917 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 2918 | if (C.kind == CXCursor_MacroExpansion) |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 2919 | return createCXString(getCursorMacroExpansion(C)->getName() |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 2920 | ->getNameStart()); |
| 2921 | |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 2922 | if (C.kind == CXCursor_MacroDefinition) |
| 2923 | return createCXString(getCursorMacroDefinition(C)->getName() |
| 2924 | ->getNameStart()); |
| 2925 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 2926 | if (C.kind == CXCursor_InclusionDirective) |
| 2927 | return createCXString(getCursorInclusionDirective(C)->getFileName()); |
| 2928 | |
Douglas Gregor | 60cbfac | 2010-01-25 16:56:17 +0000 | [diff] [blame] | 2929 | if (clang_isDeclaration(C.kind)) |
| 2930 | return getDeclSpelling(getCursorDecl(C)); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2931 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 2932 | if (C.kind == CXCursor_AnnotateAttr) { |
| 2933 | AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C)); |
| 2934 | return createCXString(AA->getAnnotation()); |
| 2935 | } |
| 2936 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2937 | return createCXString(""); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 2940 | CXString clang_getCursorDisplayName(CXCursor C) { |
| 2941 | if (!clang_isDeclaration(C.kind)) |
| 2942 | return clang_getCursorSpelling(C); |
| 2943 | |
| 2944 | Decl *D = getCursorDecl(C); |
| 2945 | if (!D) |
| 2946 | return createCXString(""); |
| 2947 | |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2948 | PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy(); |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 2949 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
| 2950 | D = FunTmpl->getTemplatedDecl(); |
| 2951 | |
| 2952 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
| 2953 | llvm::SmallString<64> Str; |
| 2954 | llvm::raw_svector_ostream OS(Str); |
| 2955 | OS << Function->getNameAsString(); |
| 2956 | if (Function->getPrimaryTemplate()) |
| 2957 | OS << "<>"; |
| 2958 | OS << "("; |
| 2959 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { |
| 2960 | if (I) |
| 2961 | OS << ", "; |
| 2962 | OS << Function->getParamDecl(I)->getType().getAsString(Policy); |
| 2963 | } |
| 2964 | |
| 2965 | if (Function->isVariadic()) { |
| 2966 | if (Function->getNumParams()) |
| 2967 | OS << ", "; |
| 2968 | OS << "..."; |
| 2969 | } |
| 2970 | OS << ")"; |
| 2971 | return createCXString(OS.str()); |
| 2972 | } |
| 2973 | |
| 2974 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { |
| 2975 | llvm::SmallString<64> Str; |
| 2976 | llvm::raw_svector_ostream OS(Str); |
| 2977 | OS << ClassTemplate->getNameAsString(); |
| 2978 | OS << "<"; |
| 2979 | TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); |
| 2980 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 2981 | if (I) |
| 2982 | OS << ", "; |
| 2983 | |
| 2984 | NamedDecl *Param = Params->getParam(I); |
| 2985 | if (Param->getIdentifier()) { |
| 2986 | OS << Param->getIdentifier()->getName(); |
| 2987 | continue; |
| 2988 | } |
| 2989 | |
| 2990 | // There is no parameter name, which makes this tricky. Try to come up |
| 2991 | // with something useful that isn't too long. |
| 2992 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 2993 | OS << (TTP->wasDeclaredWithTypename()? "typename" : "class"); |
| 2994 | else if (NonTypeTemplateParmDecl *NTTP |
| 2995 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 2996 | OS << NTTP->getType().getAsString(Policy); |
| 2997 | else |
| 2998 | OS << "template<...> class"; |
| 2999 | } |
| 3000 | |
| 3001 | OS << ">"; |
| 3002 | return createCXString(OS.str()); |
| 3003 | } |
| 3004 | |
| 3005 | if (ClassTemplateSpecializationDecl *ClassSpec |
| 3006 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 3007 | // If the type was explicitly written, use that. |
| 3008 | if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) |
| 3009 | return createCXString(TSInfo->getType().getAsString(Policy)); |
| 3010 | |
| 3011 | llvm::SmallString<64> Str; |
| 3012 | llvm::raw_svector_ostream OS(Str); |
| 3013 | OS << ClassSpec->getNameAsString(); |
| 3014 | OS << TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3015 | ClassSpec->getTemplateArgs().data(), |
| 3016 | ClassSpec->getTemplateArgs().size(), |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3017 | Policy); |
| 3018 | return createCXString(OS.str()); |
| 3019 | } |
| 3020 | |
| 3021 | return clang_getCursorSpelling(C); |
| 3022 | } |
| 3023 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3024 | CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3025 | switch (Kind) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3026 | case CXCursor_FunctionDecl: |
| 3027 | return createCXString("FunctionDecl"); |
| 3028 | case CXCursor_TypedefDecl: |
| 3029 | return createCXString("TypedefDecl"); |
| 3030 | case CXCursor_EnumDecl: |
| 3031 | return createCXString("EnumDecl"); |
| 3032 | case CXCursor_EnumConstantDecl: |
| 3033 | return createCXString("EnumConstantDecl"); |
| 3034 | case CXCursor_StructDecl: |
| 3035 | return createCXString("StructDecl"); |
| 3036 | case CXCursor_UnionDecl: |
| 3037 | return createCXString("UnionDecl"); |
| 3038 | case CXCursor_ClassDecl: |
| 3039 | return createCXString("ClassDecl"); |
| 3040 | case CXCursor_FieldDecl: |
| 3041 | return createCXString("FieldDecl"); |
| 3042 | case CXCursor_VarDecl: |
| 3043 | return createCXString("VarDecl"); |
| 3044 | case CXCursor_ParmDecl: |
| 3045 | return createCXString("ParmDecl"); |
| 3046 | case CXCursor_ObjCInterfaceDecl: |
| 3047 | return createCXString("ObjCInterfaceDecl"); |
| 3048 | case CXCursor_ObjCCategoryDecl: |
| 3049 | return createCXString("ObjCCategoryDecl"); |
| 3050 | case CXCursor_ObjCProtocolDecl: |
| 3051 | return createCXString("ObjCProtocolDecl"); |
| 3052 | case CXCursor_ObjCPropertyDecl: |
| 3053 | return createCXString("ObjCPropertyDecl"); |
| 3054 | case CXCursor_ObjCIvarDecl: |
| 3055 | return createCXString("ObjCIvarDecl"); |
| 3056 | case CXCursor_ObjCInstanceMethodDecl: |
| 3057 | return createCXString("ObjCInstanceMethodDecl"); |
| 3058 | case CXCursor_ObjCClassMethodDecl: |
| 3059 | return createCXString("ObjCClassMethodDecl"); |
| 3060 | case CXCursor_ObjCImplementationDecl: |
| 3061 | return createCXString("ObjCImplementationDecl"); |
| 3062 | case CXCursor_ObjCCategoryImplDecl: |
| 3063 | return createCXString("ObjCCategoryImplDecl"); |
Ted Kremenek | 8bd5a69 | 2010-04-13 23:39:06 +0000 | [diff] [blame] | 3064 | case CXCursor_CXXMethod: |
| 3065 | return createCXString("CXXMethod"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3066 | case CXCursor_UnexposedDecl: |
| 3067 | return createCXString("UnexposedDecl"); |
| 3068 | case CXCursor_ObjCSuperClassRef: |
| 3069 | return createCXString("ObjCSuperClassRef"); |
| 3070 | case CXCursor_ObjCProtocolRef: |
| 3071 | return createCXString("ObjCProtocolRef"); |
| 3072 | case CXCursor_ObjCClassRef: |
| 3073 | return createCXString("ObjCClassRef"); |
| 3074 | case CXCursor_TypeRef: |
| 3075 | return createCXString("TypeRef"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3076 | case CXCursor_TemplateRef: |
| 3077 | return createCXString("TemplateRef"); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3078 | case CXCursor_NamespaceRef: |
| 3079 | return createCXString("NamespaceRef"); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3080 | case CXCursor_MemberRef: |
| 3081 | return createCXString("MemberRef"); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3082 | case CXCursor_LabelRef: |
| 3083 | return createCXString("LabelRef"); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3084 | case CXCursor_OverloadedDeclRef: |
| 3085 | return createCXString("OverloadedDeclRef"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3086 | case CXCursor_IntegerLiteral: |
| 3087 | return createCXString("IntegerLiteral"); |
| 3088 | case CXCursor_FloatingLiteral: |
| 3089 | return createCXString("FloatingLiteral"); |
| 3090 | case CXCursor_ImaginaryLiteral: |
| 3091 | return createCXString("ImaginaryLiteral"); |
| 3092 | case CXCursor_StringLiteral: |
| 3093 | return createCXString("StringLiteral"); |
| 3094 | case CXCursor_CharacterLiteral: |
| 3095 | return createCXString("CharacterLiteral"); |
| 3096 | case CXCursor_ParenExpr: |
| 3097 | return createCXString("ParenExpr"); |
| 3098 | case CXCursor_UnaryOperator: |
| 3099 | return createCXString("UnaryOperator"); |
| 3100 | case CXCursor_ArraySubscriptExpr: |
| 3101 | return createCXString("ArraySubscriptExpr"); |
| 3102 | case CXCursor_BinaryOperator: |
| 3103 | return createCXString("BinaryOperator"); |
| 3104 | case CXCursor_CompoundAssignOperator: |
| 3105 | return createCXString("CompoundAssignOperator"); |
| 3106 | case CXCursor_ConditionalOperator: |
| 3107 | return createCXString("ConditionalOperator"); |
| 3108 | case CXCursor_CStyleCastExpr: |
| 3109 | return createCXString("CStyleCastExpr"); |
| 3110 | case CXCursor_CompoundLiteralExpr: |
| 3111 | return createCXString("CompoundLiteralExpr"); |
| 3112 | case CXCursor_InitListExpr: |
| 3113 | return createCXString("InitListExpr"); |
| 3114 | case CXCursor_AddrLabelExpr: |
| 3115 | return createCXString("AddrLabelExpr"); |
| 3116 | case CXCursor_StmtExpr: |
| 3117 | return createCXString("StmtExpr"); |
| 3118 | case CXCursor_GenericSelectionExpr: |
| 3119 | return createCXString("GenericSelectionExpr"); |
| 3120 | case CXCursor_GNUNullExpr: |
| 3121 | return createCXString("GNUNullExpr"); |
| 3122 | case CXCursor_CXXStaticCastExpr: |
| 3123 | return createCXString("CXXStaticCastExpr"); |
| 3124 | case CXCursor_CXXDynamicCastExpr: |
| 3125 | return createCXString("CXXDynamicCastExpr"); |
| 3126 | case CXCursor_CXXReinterpretCastExpr: |
| 3127 | return createCXString("CXXReinterpretCastExpr"); |
| 3128 | case CXCursor_CXXConstCastExpr: |
| 3129 | return createCXString("CXXConstCastExpr"); |
| 3130 | case CXCursor_CXXFunctionalCastExpr: |
| 3131 | return createCXString("CXXFunctionalCastExpr"); |
| 3132 | case CXCursor_CXXTypeidExpr: |
| 3133 | return createCXString("CXXTypeidExpr"); |
| 3134 | case CXCursor_CXXBoolLiteralExpr: |
| 3135 | return createCXString("CXXBoolLiteralExpr"); |
| 3136 | case CXCursor_CXXNullPtrLiteralExpr: |
| 3137 | return createCXString("CXXNullPtrLiteralExpr"); |
| 3138 | case CXCursor_CXXThisExpr: |
| 3139 | return createCXString("CXXThisExpr"); |
| 3140 | case CXCursor_CXXThrowExpr: |
| 3141 | return createCXString("CXXThrowExpr"); |
| 3142 | case CXCursor_CXXNewExpr: |
| 3143 | return createCXString("CXXNewExpr"); |
| 3144 | case CXCursor_CXXDeleteExpr: |
| 3145 | return createCXString("CXXDeleteExpr"); |
| 3146 | case CXCursor_UnaryExpr: |
| 3147 | return createCXString("UnaryExpr"); |
| 3148 | case CXCursor_ObjCStringLiteral: |
| 3149 | return createCXString("ObjCStringLiteral"); |
| 3150 | case CXCursor_ObjCEncodeExpr: |
| 3151 | return createCXString("ObjCEncodeExpr"); |
| 3152 | case CXCursor_ObjCSelectorExpr: |
| 3153 | return createCXString("ObjCSelectorExpr"); |
| 3154 | case CXCursor_ObjCProtocolExpr: |
| 3155 | return createCXString("ObjCProtocolExpr"); |
| 3156 | case CXCursor_ObjCBridgedCastExpr: |
| 3157 | return createCXString("ObjCBridgedCastExpr"); |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 3158 | case CXCursor_BlockExpr: |
| 3159 | return createCXString("BlockExpr"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3160 | case CXCursor_PackExpansionExpr: |
| 3161 | return createCXString("PackExpansionExpr"); |
| 3162 | case CXCursor_SizeOfPackExpr: |
| 3163 | return createCXString("SizeOfPackExpr"); |
| 3164 | case CXCursor_UnexposedExpr: |
| 3165 | return createCXString("UnexposedExpr"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3166 | case CXCursor_DeclRefExpr: |
| 3167 | return createCXString("DeclRefExpr"); |
| 3168 | case CXCursor_MemberRefExpr: |
| 3169 | return createCXString("MemberRefExpr"); |
| 3170 | case CXCursor_CallExpr: |
| 3171 | return createCXString("CallExpr"); |
| 3172 | case CXCursor_ObjCMessageExpr: |
| 3173 | return createCXString("ObjCMessageExpr"); |
| 3174 | case CXCursor_UnexposedStmt: |
| 3175 | return createCXString("UnexposedStmt"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3176 | case CXCursor_DeclStmt: |
| 3177 | return createCXString("DeclStmt"); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3178 | case CXCursor_LabelStmt: |
| 3179 | return createCXString("LabelStmt"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3180 | case CXCursor_CompoundStmt: |
| 3181 | return createCXString("CompoundStmt"); |
| 3182 | case CXCursor_CaseStmt: |
| 3183 | return createCXString("CaseStmt"); |
| 3184 | case CXCursor_DefaultStmt: |
| 3185 | return createCXString("DefaultStmt"); |
| 3186 | case CXCursor_IfStmt: |
| 3187 | return createCXString("IfStmt"); |
| 3188 | case CXCursor_SwitchStmt: |
| 3189 | return createCXString("SwitchStmt"); |
| 3190 | case CXCursor_WhileStmt: |
| 3191 | return createCXString("WhileStmt"); |
| 3192 | case CXCursor_DoStmt: |
| 3193 | return createCXString("DoStmt"); |
| 3194 | case CXCursor_ForStmt: |
| 3195 | return createCXString("ForStmt"); |
| 3196 | case CXCursor_GotoStmt: |
| 3197 | return createCXString("GotoStmt"); |
| 3198 | case CXCursor_IndirectGotoStmt: |
| 3199 | return createCXString("IndirectGotoStmt"); |
| 3200 | case CXCursor_ContinueStmt: |
| 3201 | return createCXString("ContinueStmt"); |
| 3202 | case CXCursor_BreakStmt: |
| 3203 | return createCXString("BreakStmt"); |
| 3204 | case CXCursor_ReturnStmt: |
| 3205 | return createCXString("ReturnStmt"); |
| 3206 | case CXCursor_AsmStmt: |
| 3207 | return createCXString("AsmStmt"); |
| 3208 | case CXCursor_ObjCAtTryStmt: |
| 3209 | return createCXString("ObjCAtTryStmt"); |
| 3210 | case CXCursor_ObjCAtCatchStmt: |
| 3211 | return createCXString("ObjCAtCatchStmt"); |
| 3212 | case CXCursor_ObjCAtFinallyStmt: |
| 3213 | return createCXString("ObjCAtFinallyStmt"); |
| 3214 | case CXCursor_ObjCAtThrowStmt: |
| 3215 | return createCXString("ObjCAtThrowStmt"); |
| 3216 | case CXCursor_ObjCAtSynchronizedStmt: |
| 3217 | return createCXString("ObjCAtSynchronizedStmt"); |
| 3218 | case CXCursor_ObjCAutoreleasePoolStmt: |
| 3219 | return createCXString("ObjCAutoreleasePoolStmt"); |
| 3220 | case CXCursor_ObjCForCollectionStmt: |
| 3221 | return createCXString("ObjCForCollectionStmt"); |
| 3222 | case CXCursor_CXXCatchStmt: |
| 3223 | return createCXString("CXXCatchStmt"); |
| 3224 | case CXCursor_CXXTryStmt: |
| 3225 | return createCXString("CXXTryStmt"); |
| 3226 | case CXCursor_CXXForRangeStmt: |
| 3227 | return createCXString("CXXForRangeStmt"); |
| 3228 | case CXCursor_SEHTryStmt: |
| 3229 | return createCXString("SEHTryStmt"); |
| 3230 | case CXCursor_SEHExceptStmt: |
| 3231 | return createCXString("SEHExceptStmt"); |
| 3232 | case CXCursor_SEHFinallyStmt: |
| 3233 | return createCXString("SEHFinallyStmt"); |
| 3234 | case CXCursor_NullStmt: |
| 3235 | return createCXString("NullStmt"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3236 | case CXCursor_InvalidFile: |
| 3237 | return createCXString("InvalidFile"); |
Ted Kremenek | 292db64 | 2010-03-19 20:39:05 +0000 | [diff] [blame] | 3238 | case CXCursor_InvalidCode: |
| 3239 | return createCXString("InvalidCode"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3240 | case CXCursor_NoDeclFound: |
| 3241 | return createCXString("NoDeclFound"); |
| 3242 | case CXCursor_NotImplemented: |
| 3243 | return createCXString("NotImplemented"); |
| 3244 | case CXCursor_TranslationUnit: |
| 3245 | return createCXString("TranslationUnit"); |
Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 3246 | case CXCursor_UnexposedAttr: |
| 3247 | return createCXString("UnexposedAttr"); |
| 3248 | case CXCursor_IBActionAttr: |
| 3249 | return createCXString("attribute(ibaction)"); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3250 | case CXCursor_IBOutletAttr: |
| 3251 | return createCXString("attribute(iboutlet)"); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 3252 | case CXCursor_IBOutletCollectionAttr: |
| 3253 | return createCXString("attribute(iboutletcollection)"); |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 3254 | case CXCursor_CXXFinalAttr: |
| 3255 | return createCXString("attribute(final)"); |
| 3256 | case CXCursor_CXXOverrideAttr: |
| 3257 | return createCXString("attribute(override)"); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 3258 | case CXCursor_AnnotateAttr: |
| 3259 | return createCXString("attribute(annotate)"); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3260 | case CXCursor_PreprocessingDirective: |
| 3261 | return createCXString("preprocessing directive"); |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3262 | case CXCursor_MacroDefinition: |
| 3263 | return createCXString("macro definition"); |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3264 | case CXCursor_MacroExpansion: |
| 3265 | return createCXString("macro expansion"); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3266 | case CXCursor_InclusionDirective: |
| 3267 | return createCXString("inclusion directive"); |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 3268 | case CXCursor_Namespace: |
| 3269 | return createCXString("Namespace"); |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 3270 | case CXCursor_LinkageSpec: |
| 3271 | return createCXString("LinkageSpec"); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3272 | case CXCursor_CXXBaseSpecifier: |
| 3273 | return createCXString("C++ base class specifier"); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 3274 | case CXCursor_Constructor: |
| 3275 | return createCXString("CXXConstructor"); |
| 3276 | case CXCursor_Destructor: |
| 3277 | return createCXString("CXXDestructor"); |
| 3278 | case CXCursor_ConversionFunction: |
| 3279 | return createCXString("CXXConversion"); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 3280 | case CXCursor_TemplateTypeParameter: |
| 3281 | return createCXString("TemplateTypeParameter"); |
| 3282 | case CXCursor_NonTypeTemplateParameter: |
| 3283 | return createCXString("NonTypeTemplateParameter"); |
| 3284 | case CXCursor_TemplateTemplateParameter: |
| 3285 | return createCXString("TemplateTemplateParameter"); |
| 3286 | case CXCursor_FunctionTemplate: |
| 3287 | return createCXString("FunctionTemplate"); |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 3288 | case CXCursor_ClassTemplate: |
| 3289 | return createCXString("ClassTemplate"); |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 3290 | case CXCursor_ClassTemplatePartialSpecialization: |
| 3291 | return createCXString("ClassTemplatePartialSpecialization"); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3292 | case CXCursor_NamespaceAlias: |
| 3293 | return createCXString("NamespaceAlias"); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 3294 | case CXCursor_UsingDirective: |
| 3295 | return createCXString("UsingDirective"); |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 3296 | case CXCursor_UsingDeclaration: |
| 3297 | return createCXString("UsingDeclaration"); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3298 | case CXCursor_TypeAliasDecl: |
Douglas Gregor | 352697a | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3299 | return createCXString("TypeAliasDecl"); |
| 3300 | case CXCursor_ObjCSynthesizeDecl: |
| 3301 | return createCXString("ObjCSynthesizeDecl"); |
| 3302 | case CXCursor_ObjCDynamicDecl: |
| 3303 | return createCXString("ObjCDynamicDecl"); |
Argyrios Kyrtzidis | 2dfdb94 | 2011-09-30 17:58:23 +0000 | [diff] [blame] | 3304 | case CXCursor_CXXAccessSpecifier: |
| 3305 | return createCXString("CXXAccessSpecifier"); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3306 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3307 | |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 3308 | llvm_unreachable("Unhandled CXCursorKind"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3309 | return createCXString((const char*) 0); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 3310 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3311 | |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3312 | struct GetCursorData { |
| 3313 | SourceLocation TokenBeginLoc; |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3314 | bool PointsAtMacroArgExpansion; |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3315 | CXCursor &BestCursor; |
| 3316 | |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3317 | GetCursorData(SourceManager &SM, |
| 3318 | SourceLocation tokenBegin, CXCursor &outputCursor) |
| 3319 | : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) { |
| 3320 | PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin); |
| 3321 | } |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3322 | }; |
| 3323 | |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3324 | static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, |
| 3325 | CXCursor parent, |
| 3326 | CXClientData client_data) { |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3327 | GetCursorData *Data = static_cast<GetCursorData *>(client_data); |
| 3328 | CXCursor *BestCursor = &Data->BestCursor; |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3329 | |
| 3330 | // If we point inside a macro argument we should provide info of what the |
| 3331 | // token is so use the actual cursor, don't replace it with a macro expansion |
| 3332 | // cursor. |
| 3333 | if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion) |
| 3334 | return CXChildVisit_Recurse; |
Argyrios Kyrtzidis | 65ab907 | 2011-09-26 19:05:37 +0000 | [diff] [blame] | 3335 | |
| 3336 | if (clang_isDeclaration(cursor.kind)) { |
| 3337 | // Avoid having the implicit methods override the property decls. |
| 3338 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(getCursorDecl(cursor))) |
| 3339 | if (MD->isImplicit()) |
| 3340 | return CXChildVisit_Break; |
| 3341 | } |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3342 | |
| 3343 | if (clang_isExpression(cursor.kind) && |
| 3344 | clang_isDeclaration(BestCursor->kind)) { |
| 3345 | Decl *D = getCursorDecl(*BestCursor); |
| 3346 | |
| 3347 | // Avoid having the cursor of an expression replace the declaration cursor |
| 3348 | // when the expression source range overlaps the declaration range. |
| 3349 | // This can happen for C++ constructor expressions whose range generally |
| 3350 | // include the variable declaration, e.g.: |
| 3351 | // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor. |
| 3352 | if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() && |
| 3353 | D->getLocation() == Data->TokenBeginLoc) |
| 3354 | return CXChildVisit_Break; |
| 3355 | } |
| 3356 | |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3357 | // If our current best cursor is the construction of a temporary object, |
| 3358 | // don't replace that cursor with a type reference, because we want |
| 3359 | // clang_getCursor() to point at the constructor. |
| 3360 | if (clang_isExpression(BestCursor->kind) && |
| 3361 | isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 3362 | cursor.kind == CXCursor_TypeRef) { |
| 3363 | // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it |
| 3364 | // as having the actual point on the type reference. |
| 3365 | *BestCursor = getTypeRefedCallExprCursor(*BestCursor); |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3366 | return CXChildVisit_Recurse; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 3367 | } |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3368 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3369 | *BestCursor = cursor; |
| 3370 | return CXChildVisit_Recurse; |
| 3371 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3372 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 3373 | CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { |
| 3374 | if (!TU) |
Ted Kremenek | f462989 | 2010-01-14 01:51:23 +0000 | [diff] [blame] | 3375 | return clang_getNullCursor(); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3376 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3377 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 3378 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 3379 | |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3380 | SourceLocation SLoc = cxloc::translateSourceLocation(Loc); |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3381 | CXCursor Result = cxcursor::getCursor(TU, SLoc); |
Ted Kremenek | a629ea4 | 2010-07-29 00:52:07 +0000 | [diff] [blame] | 3382 | |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3383 | bool Logging = getenv("LIBCLANG_LOGGING"); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3384 | if (Logging) { |
| 3385 | CXFile SearchFile; |
| 3386 | unsigned SearchLine, SearchColumn; |
| 3387 | CXFile ResultFile; |
| 3388 | unsigned ResultLine, ResultColumn; |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3389 | CXString SearchFileName, ResultFileName, KindSpelling, USR; |
| 3390 | const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : ""; |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3391 | CXSourceLocation ResultLoc = clang_getCursorLocation(Result); |
| 3392 | |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 3393 | clang_getExpansionLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, 0); |
| 3394 | clang_getExpansionLocation(ResultLoc, &ResultFile, &ResultLine, |
| 3395 | &ResultColumn, 0); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3396 | SearchFileName = clang_getFileName(SearchFile); |
| 3397 | ResultFileName = clang_getFileName(ResultFile); |
| 3398 | KindSpelling = clang_getCursorKindSpelling(Result.kind); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3399 | USR = clang_getCursorUSR(Result); |
| 3400 | fprintf(stderr, "clang_getCursor(%s:%d:%d) = %s(%s:%d:%d):%s%s\n", |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3401 | clang_getCString(SearchFileName), SearchLine, SearchColumn, |
| 3402 | clang_getCString(KindSpelling), |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3403 | clang_getCString(ResultFileName), ResultLine, ResultColumn, |
| 3404 | clang_getCString(USR), IsDef); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3405 | clang_disposeString(SearchFileName); |
| 3406 | clang_disposeString(ResultFileName); |
| 3407 | clang_disposeString(KindSpelling); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3408 | clang_disposeString(USR); |
Douglas Gregor | 0aefbd8 | 2010-12-10 01:45:00 +0000 | [diff] [blame] | 3409 | |
| 3410 | CXCursor Definition = clang_getCursorDefinition(Result); |
| 3411 | if (!clang_equalCursors(Definition, clang_getNullCursor())) { |
| 3412 | CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition); |
| 3413 | CXString DefinitionKindSpelling |
| 3414 | = clang_getCursorKindSpelling(Definition.kind); |
| 3415 | CXFile DefinitionFile; |
| 3416 | unsigned DefinitionLine, DefinitionColumn; |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 3417 | clang_getExpansionLocation(DefinitionLoc, &DefinitionFile, |
| 3418 | &DefinitionLine, &DefinitionColumn, 0); |
Douglas Gregor | 0aefbd8 | 2010-12-10 01:45:00 +0000 | [diff] [blame] | 3419 | CXString DefinitionFileName = clang_getFileName(DefinitionFile); |
| 3420 | fprintf(stderr, " -> %s(%s:%d:%d)\n", |
| 3421 | clang_getCString(DefinitionKindSpelling), |
| 3422 | clang_getCString(DefinitionFileName), |
| 3423 | DefinitionLine, DefinitionColumn); |
| 3424 | clang_disposeString(DefinitionFileName); |
| 3425 | clang_disposeString(DefinitionKindSpelling); |
| 3426 | } |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3427 | } |
| 3428 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3429 | return Result; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3432 | CXCursor clang_getNullCursor(void) { |
Douglas Gregor | 5bfb8c1 | 2010-01-20 23:34:41 +0000 | [diff] [blame] | 3433 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3434 | } |
| 3435 | |
| 3436 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 3437 | return X == Y; |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3438 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 3439 | |
Douglas Gregor | 9ce5584 | 2010-11-20 00:09:34 +0000 | [diff] [blame] | 3440 | unsigned clang_hashCursor(CXCursor C) { |
| 3441 | unsigned Index = 0; |
| 3442 | if (clang_isExpression(C.kind) || clang_isStatement(C.kind)) |
| 3443 | Index = 1; |
| 3444 | |
| 3445 | return llvm::DenseMapInfo<std::pair<unsigned, void*> >::getHashValue( |
| 3446 | std::make_pair(C.kind, C.data[Index])); |
| 3447 | } |
| 3448 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3449 | unsigned clang_isInvalid(enum CXCursorKind K) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 3450 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 3451 | } |
| 3452 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3453 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3454 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 3455 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 3456 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3457 | unsigned clang_isReference(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3458 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 3459 | } |
| 3460 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3461 | unsigned clang_isExpression(enum CXCursorKind K) { |
| 3462 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; |
| 3463 | } |
| 3464 | |
| 3465 | unsigned clang_isStatement(enum CXCursorKind K) { |
| 3466 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; |
| 3467 | } |
| 3468 | |
Douglas Gregor | 8be80e1 | 2011-07-06 03:00:34 +0000 | [diff] [blame] | 3469 | unsigned clang_isAttribute(enum CXCursorKind K) { |
| 3470 | return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr; |
| 3471 | } |
| 3472 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3473 | unsigned clang_isTranslationUnit(enum CXCursorKind K) { |
| 3474 | return K == CXCursor_TranslationUnit; |
| 3475 | } |
| 3476 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3477 | unsigned clang_isPreprocessing(enum CXCursorKind K) { |
| 3478 | return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; |
| 3479 | } |
| 3480 | |
Ted Kremenek | ad6eff6 | 2010-03-08 21:17:29 +0000 | [diff] [blame] | 3481 | unsigned clang_isUnexposed(enum CXCursorKind K) { |
| 3482 | switch (K) { |
| 3483 | case CXCursor_UnexposedDecl: |
| 3484 | case CXCursor_UnexposedExpr: |
| 3485 | case CXCursor_UnexposedStmt: |
| 3486 | case CXCursor_UnexposedAttr: |
| 3487 | return true; |
| 3488 | default: |
| 3489 | return false; |
| 3490 | } |
| 3491 | } |
| 3492 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3493 | CXCursorKind clang_getCursorKind(CXCursor C) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 3494 | return C.kind; |
| 3495 | } |
| 3496 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3497 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 3498 | if (clang_isReference(C.kind)) { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3499 | switch (C.kind) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3500 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3501 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 3502 | = getCursorObjCSuperClassRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3503 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3504 | } |
| 3505 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3506 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3507 | std::pair<ObjCProtocolDecl *, SourceLocation> P |
| 3508 | = getCursorObjCProtocolRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3509 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3510 | } |
| 3511 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3512 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3513 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 3514 | = getCursorObjCClassRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3515 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3516 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3517 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3518 | case CXCursor_TypeRef: { |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3519 | std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3520 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3521 | } |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3522 | |
| 3523 | case CXCursor_TemplateRef: { |
| 3524 | std::pair<TemplateDecl *, SourceLocation> P = getCursorTemplateRef(C); |
| 3525 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3526 | } |
| 3527 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3528 | case CXCursor_NamespaceRef: { |
| 3529 | std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); |
| 3530 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3531 | } |
| 3532 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3533 | case CXCursor_MemberRef: { |
| 3534 | std::pair<FieldDecl *, SourceLocation> P = getCursorMemberRef(C); |
| 3535 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3536 | } |
| 3537 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3538 | case CXCursor_CXXBaseSpecifier: { |
Douglas Gregor | 1b0f7af | 2010-10-02 19:51:13 +0000 | [diff] [blame] | 3539 | CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); |
| 3540 | if (!BaseSpec) |
| 3541 | return clang_getNullLocation(); |
| 3542 | |
| 3543 | if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) |
| 3544 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3545 | TSInfo->getTypeLoc().getBeginLoc()); |
| 3546 | |
| 3547 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3548 | BaseSpec->getSourceRange().getBegin()); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3549 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3550 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3551 | case CXCursor_LabelRef: { |
| 3552 | std::pair<LabelStmt *, SourceLocation> P = getCursorLabelRef(C); |
| 3553 | return cxloc::translateSourceLocation(getCursorContext(C), P.second); |
| 3554 | } |
| 3555 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3556 | case CXCursor_OverloadedDeclRef: |
| 3557 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3558 | getCursorOverloadedDeclRef(C).second); |
| 3559 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3560 | default: |
| 3561 | // FIXME: Need a way to enumerate all non-reference cases. |
| 3562 | llvm_unreachable("Missed a reference kind"); |
| 3563 | } |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3564 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3565 | |
| 3566 | if (clang_isExpression(C.kind)) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3567 | return cxloc::translateSourceLocation(getCursorContext(C), |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3568 | getLocationFromExpr(getCursorExpr(C))); |
| 3569 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3570 | if (clang_isStatement(C.kind)) |
| 3571 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3572 | getCursorStmt(C)->getLocStart()); |
| 3573 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3574 | if (C.kind == CXCursor_PreprocessingDirective) { |
| 3575 | SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); |
| 3576 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3577 | } |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3578 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3579 | if (C.kind == CXCursor_MacroExpansion) { |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 3580 | SourceLocation L |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3581 | = cxcursor::getCursorMacroExpansion(C)->getSourceRange().getBegin(); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3582 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3583 | } |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3584 | |
| 3585 | if (C.kind == CXCursor_MacroDefinition) { |
| 3586 | SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); |
| 3587 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3588 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3589 | |
| 3590 | if (C.kind == CXCursor_InclusionDirective) { |
| 3591 | SourceLocation L |
| 3592 | = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); |
| 3593 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3594 | } |
| 3595 | |
Ted Kremenek | 9a700d2 | 2010-05-12 06:16:13 +0000 | [diff] [blame] | 3596 | if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 3597 | return clang_getNullLocation(); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3598 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3599 | Decl *D = getCursorDecl(C); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3600 | SourceLocation Loc = D->getLocation(); |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 3601 | // FIXME: Multiple variables declared in a single declaration |
| 3602 | // currently lack the information needed to correctly determine their |
| 3603 | // ranges when accounting for the type-specifier. We use context |
| 3604 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3605 | // and if so, whether it is the first decl. |
| 3606 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3607 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3608 | Loc = VD->getLocation(); |
| 3609 | } |
| 3610 | |
Douglas Gregor | 2ca54fe | 2010-03-22 15:53:50 +0000 | [diff] [blame] | 3611 | return cxloc::translateSourceLocation(getCursorContext(C), Loc); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3612 | } |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3613 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3614 | } // end extern "C" |
| 3615 | |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3616 | CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) { |
| 3617 | assert(TU); |
| 3618 | |
| 3619 | // Guard against an invalid SourceLocation, or we may assert in one |
| 3620 | // of the following calls. |
| 3621 | if (SLoc.isInvalid()) |
| 3622 | return clang_getNullCursor(); |
| 3623 | |
| 3624 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 3625 | |
| 3626 | // Translate the given source location to make it point at the beginning of |
| 3627 | // the token under the cursor. |
| 3628 | SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), |
| 3629 | CXXUnit->getASTContext().getLangOptions()); |
| 3630 | |
| 3631 | CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 3632 | if (SLoc.isValid()) { |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3633 | GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result); |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3634 | CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData, |
| 3635 | /*VisitPreprocessorLast=*/true, |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 3636 | /*VisitIncludedEntities=*/false, |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3637 | SourceLocation(SLoc)); |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 3638 | CursorVis.visitFileRegion(); |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | return Result; |
| 3642 | } |
| 3643 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3644 | static SourceRange getRawCursorExtent(CXCursor C) { |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3645 | if (clang_isReference(C.kind)) { |
| 3646 | switch (C.kind) { |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3647 | case CXCursor_ObjCSuperClassRef: |
| 3648 | return getCursorObjCSuperClassRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3649 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3650 | case CXCursor_ObjCProtocolRef: |
| 3651 | return getCursorObjCProtocolRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3652 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3653 | case CXCursor_ObjCClassRef: |
| 3654 | return getCursorObjCClassRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3655 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3656 | case CXCursor_TypeRef: |
| 3657 | return getCursorTypeRef(C).second; |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3658 | |
| 3659 | case CXCursor_TemplateRef: |
| 3660 | return getCursorTemplateRef(C).second; |
| 3661 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3662 | case CXCursor_NamespaceRef: |
| 3663 | return getCursorNamespaceRef(C).second; |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3664 | |
| 3665 | case CXCursor_MemberRef: |
| 3666 | return getCursorMemberRef(C).second; |
| 3667 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3668 | case CXCursor_CXXBaseSpecifier: |
Douglas Gregor | 1b0f7af | 2010-10-02 19:51:13 +0000 | [diff] [blame] | 3669 | return getCursorCXXBaseSpecifier(C)->getSourceRange(); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3670 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3671 | case CXCursor_LabelRef: |
| 3672 | return getCursorLabelRef(C).second; |
| 3673 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3674 | case CXCursor_OverloadedDeclRef: |
| 3675 | return getCursorOverloadedDeclRef(C).second; |
| 3676 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3677 | default: |
| 3678 | // FIXME: Need a way to enumerate all non-reference cases. |
| 3679 | llvm_unreachable("Missed a reference kind"); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3680 | } |
| 3681 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3682 | |
| 3683 | if (clang_isExpression(C.kind)) |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3684 | return getCursorExpr(C)->getSourceRange(); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3685 | |
| 3686 | if (clang_isStatement(C.kind)) |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3687 | return getCursorStmt(C)->getSourceRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3688 | |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 3689 | if (clang_isAttribute(C.kind)) |
| 3690 | return getCursorAttr(C)->getRange(); |
| 3691 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3692 | if (C.kind == CXCursor_PreprocessingDirective) |
| 3693 | return cxcursor::getCursorPreprocessingDirective(C); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3694 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 3695 | if (C.kind == CXCursor_MacroExpansion) { |
| 3696 | ASTUnit *TU = getCursorASTUnit(C); |
| 3697 | SourceRange Range = cxcursor::getCursorMacroExpansion(C)->getSourceRange(); |
| 3698 | return TU->mapRangeFromPreamble(Range); |
| 3699 | } |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3700 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 3701 | if (C.kind == CXCursor_MacroDefinition) { |
| 3702 | ASTUnit *TU = getCursorASTUnit(C); |
| 3703 | SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange(); |
| 3704 | return TU->mapRangeFromPreamble(Range); |
| 3705 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3706 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 3707 | if (C.kind == CXCursor_InclusionDirective) { |
| 3708 | ASTUnit *TU = getCursorASTUnit(C); |
| 3709 | SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange(); |
| 3710 | return TU->mapRangeFromPreamble(Range); |
| 3711 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3712 | |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 3713 | if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { |
| 3714 | Decl *D = cxcursor::getCursorDecl(C); |
| 3715 | SourceRange R = D->getSourceRange(); |
| 3716 | // FIXME: Multiple variables declared in a single declaration |
| 3717 | // currently lack the information needed to correctly determine their |
| 3718 | // ranges when accounting for the type-specifier. We use context |
| 3719 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3720 | // and if so, whether it is the first decl. |
| 3721 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3722 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3723 | R.setBegin(VD->getLocation()); |
| 3724 | } |
| 3725 | return R; |
| 3726 | } |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3727 | return SourceRange(); |
| 3728 | } |
| 3729 | |
| 3730 | /// \brief Retrieves the "raw" cursor extent, which is then extended to include |
| 3731 | /// the decl-specifier-seq for declarations. |
| 3732 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) { |
| 3733 | if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { |
| 3734 | Decl *D = cxcursor::getCursorDecl(C); |
| 3735 | SourceRange R = D->getSourceRange(); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3736 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 3737 | // Adjust the start of the location for declarations preceded by |
| 3738 | // declaration specifiers. |
| 3739 | SourceLocation StartLoc; |
| 3740 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
| 3741 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
| 3742 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 3743 | } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
| 3744 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
| 3745 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 3746 | } |
| 3747 | |
| 3748 | if (StartLoc.isValid() && R.getBegin().isValid() && |
| 3749 | SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin())) |
| 3750 | R.setBegin(StartLoc); |
| 3751 | |
| 3752 | // FIXME: Multiple variables declared in a single declaration |
| 3753 | // currently lack the information needed to correctly determine their |
| 3754 | // ranges when accounting for the type-specifier. We use context |
| 3755 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3756 | // and if so, whether it is the first decl. |
| 3757 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3758 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3759 | R.setBegin(VD->getLocation()); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3760 | } |
| 3761 | |
| 3762 | return R; |
| 3763 | } |
| 3764 | |
| 3765 | return getRawCursorExtent(C); |
| 3766 | } |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3767 | |
| 3768 | extern "C" { |
| 3769 | |
| 3770 | CXSourceRange clang_getCursorExtent(CXCursor C) { |
| 3771 | SourceRange R = getRawCursorExtent(C); |
| 3772 | if (R.isInvalid()) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 3773 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3774 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3775 | return cxloc::translateSourceRange(getCursorContext(C), R); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3776 | } |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3777 | |
| 3778 | CXCursor clang_getCursorReferenced(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3779 | if (clang_isInvalid(C.kind)) |
| 3780 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3781 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3782 | CXTranslationUnit tu = getCursorTU(C); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3783 | if (clang_isDeclaration(C.kind)) { |
| 3784 | Decl *D = getCursorDecl(C); |
| 3785 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3786 | return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3787 | if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3788 | return MakeCursorOverloadedDeclRef(Classes, D->getLocation(), tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3789 | if (ObjCForwardProtocolDecl *Protocols |
| 3790 | = dyn_cast<ObjCForwardProtocolDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3791 | return MakeCursorOverloadedDeclRef(Protocols, D->getLocation(), tu); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3792 | if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3793 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 3794 | return MakeCXCursor(Property, tu); |
| 3795 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3796 | return C; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3797 | } |
| 3798 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3799 | if (clang_isExpression(C.kind)) { |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3800 | Expr *E = getCursorExpr(C); |
| 3801 | Decl *D = getDeclFromExpr(E); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 3802 | if (D) { |
| 3803 | CXCursor declCursor = MakeCXCursor(D, tu); |
| 3804 | declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C), |
| 3805 | declCursor); |
| 3806 | return declCursor; |
| 3807 | } |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3808 | |
| 3809 | if (OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3810 | return MakeCursorOverloadedDeclRef(Ovl, tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3811 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3812 | return clang_getNullCursor(); |
| 3813 | } |
| 3814 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3815 | if (clang_isStatement(C.kind)) { |
| 3816 | Stmt *S = getCursorStmt(C); |
| 3817 | if (GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) |
Ted Kremenek | 37c2e96 | 2011-03-15 23:47:49 +0000 | [diff] [blame] | 3818 | if (LabelDecl *label = Goto->getLabel()) |
| 3819 | if (LabelStmt *labelS = label->getStmt()) |
| 3820 | return MakeCXCursor(labelS, getCursorDecl(C), tu); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3821 | |
| 3822 | return clang_getNullCursor(); |
| 3823 | } |
| 3824 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3825 | if (C.kind == CXCursor_MacroExpansion) { |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3826 | if (MacroDefinition *Def = getCursorMacroExpansion(C)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3827 | return MakeMacroDefinitionCursor(Def, tu); |
Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 3828 | } |
| 3829 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3830 | if (!clang_isReference(C.kind)) |
| 3831 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3832 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3833 | switch (C.kind) { |
| 3834 | case CXCursor_ObjCSuperClassRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3835 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3836 | |
| 3837 | case CXCursor_ObjCProtocolRef: { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3838 | return MakeCXCursor(getCursorObjCProtocolRef(C).first, tu); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3839 | |
| 3840 | case CXCursor_ObjCClassRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3841 | return MakeCXCursor(getCursorObjCClassRef(C).first, tu ); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3842 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3843 | case CXCursor_TypeRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3844 | return MakeCXCursor(getCursorTypeRef(C).first, tu ); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3845 | |
| 3846 | case CXCursor_TemplateRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3847 | return MakeCXCursor(getCursorTemplateRef(C).first, tu ); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3848 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3849 | case CXCursor_NamespaceRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3850 | return MakeCXCursor(getCursorNamespaceRef(C).first, tu ); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3851 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3852 | case CXCursor_MemberRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3853 | return MakeCXCursor(getCursorMemberRef(C).first, tu ); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3854 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3855 | case CXCursor_CXXBaseSpecifier: { |
| 3856 | CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); |
| 3857 | return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3858 | tu )); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3859 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3860 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3861 | case CXCursor_LabelRef: |
| 3862 | // FIXME: We end up faking the "parent" declaration here because we |
| 3863 | // don't want to make CXCursor larger. |
| 3864 | return MakeCXCursor(getCursorLabelRef(C).first, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3865 | static_cast<ASTUnit*>(tu->TUData)->getASTContext() |
| 3866 | .getTranslationUnitDecl(), |
| 3867 | tu); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3868 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3869 | case CXCursor_OverloadedDeclRef: |
| 3870 | return C; |
| 3871 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3872 | default: |
| 3873 | // We would prefer to enumerate all non-reference cursor kinds here. |
| 3874 | llvm_unreachable("Unhandled reference cursor kind"); |
| 3875 | break; |
| 3876 | } |
| 3877 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3878 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3879 | return clang_getNullCursor(); |
| 3880 | } |
| 3881 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3882 | CXCursor clang_getCursorDefinition(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3883 | if (clang_isInvalid(C.kind)) |
| 3884 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3885 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3886 | CXTranslationUnit TU = getCursorTU(C); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3887 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3888 | bool WasReference = false; |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3889 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3890 | C = clang_getCursorReferenced(C); |
| 3891 | WasReference = true; |
| 3892 | } |
| 3893 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3894 | if (C.kind == CXCursor_MacroExpansion) |
Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 3895 | return clang_getCursorReferenced(C); |
| 3896 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3897 | if (!clang_isDeclaration(C.kind)) |
| 3898 | return clang_getNullCursor(); |
| 3899 | |
| 3900 | Decl *D = getCursorDecl(C); |
| 3901 | if (!D) |
| 3902 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3903 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3904 | switch (D->getKind()) { |
| 3905 | // Declaration kinds that don't really separate the notions of |
| 3906 | // declaration and definition. |
| 3907 | case Decl::Namespace: |
| 3908 | case Decl::Typedef: |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3909 | case Decl::TypeAlias: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3910 | case Decl::TypeAliasTemplate: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3911 | case Decl::TemplateTypeParm: |
| 3912 | case Decl::EnumConstant: |
| 3913 | case Decl::Field: |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 3914 | case Decl::IndirectField: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3915 | case Decl::ObjCIvar: |
| 3916 | case Decl::ObjCAtDefsField: |
| 3917 | case Decl::ImplicitParam: |
| 3918 | case Decl::ParmVar: |
| 3919 | case Decl::NonTypeTemplateParm: |
| 3920 | case Decl::TemplateTemplateParm: |
| 3921 | case Decl::ObjCCategoryImpl: |
| 3922 | case Decl::ObjCImplementation: |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 3923 | case Decl::AccessSpec: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3924 | case Decl::LinkageSpec: |
| 3925 | case Decl::ObjCPropertyImpl: |
| 3926 | case Decl::FileScopeAsm: |
| 3927 | case Decl::StaticAssert: |
| 3928 | case Decl::Block: |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3929 | case Decl::Label: // FIXME: Is this right?? |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3930 | case Decl::ClassScopeFunctionSpecialization: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3931 | return C; |
| 3932 | |
| 3933 | // Declaration kinds that don't make any sense here, but are |
| 3934 | // nonetheless harmless. |
| 3935 | case Decl::TranslationUnit: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3936 | break; |
| 3937 | |
| 3938 | // Declaration kinds for which the definition is not resolvable. |
| 3939 | case Decl::UnresolvedUsingTypename: |
| 3940 | case Decl::UnresolvedUsingValue: |
| 3941 | break; |
| 3942 | |
| 3943 | case Decl::UsingDirective: |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3944 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3945 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3946 | |
| 3947 | case Decl::NamespaceAlias: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3948 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3949 | |
| 3950 | case Decl::Enum: |
| 3951 | case Decl::Record: |
| 3952 | case Decl::CXXRecord: |
| 3953 | case Decl::ClassTemplateSpecialization: |
| 3954 | case Decl::ClassTemplatePartialSpecialization: |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 3955 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3956 | return MakeCXCursor(Def, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3957 | return clang_getNullCursor(); |
| 3958 | |
| 3959 | case Decl::Function: |
| 3960 | case Decl::CXXMethod: |
| 3961 | case Decl::CXXConstructor: |
| 3962 | case Decl::CXXDestructor: |
| 3963 | case Decl::CXXConversion: { |
| 3964 | const FunctionDecl *Def = 0; |
| 3965 | if (cast<FunctionDecl>(D)->getBody(Def)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3966 | return MakeCXCursor(const_cast<FunctionDecl *>(Def), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3967 | return clang_getNullCursor(); |
| 3968 | } |
| 3969 | |
| 3970 | case Decl::Var: { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 3971 | // Ask the variable if it has a definition. |
| 3972 | if (VarDecl *Def = cast<VarDecl>(D)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3973 | return MakeCXCursor(Def, TU); |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 3974 | return clang_getNullCursor(); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3975 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3976 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3977 | case Decl::FunctionTemplate: { |
| 3978 | const FunctionDecl *Def = 0; |
| 3979 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3980 | return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3981 | return clang_getNullCursor(); |
| 3982 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3983 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3984 | case Decl::ClassTemplate: { |
| 3985 | if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 3986 | ->getDefinition()) |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3987 | return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3988 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3989 | return clang_getNullCursor(); |
| 3990 | } |
| 3991 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3992 | case Decl::Using: |
| 3993 | return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3994 | D->getLocation(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3995 | |
| 3996 | case Decl::UsingShadow: |
| 3997 | return clang_getCursorDefinition( |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3998 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3999 | TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4000 | |
| 4001 | case Decl::ObjCMethod: { |
| 4002 | ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); |
| 4003 | if (Method->isThisDeclarationADefinition()) |
| 4004 | return C; |
| 4005 | |
| 4006 | // Dig out the method definition in the associated |
| 4007 | // @implementation, if we have it. |
| 4008 | // FIXME: The ASTs should make finding the definition easier. |
| 4009 | if (ObjCInterfaceDecl *Class |
| 4010 | = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) |
| 4011 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) |
| 4012 | if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), |
| 4013 | Method->isInstanceMethod())) |
| 4014 | if (Def->isThisDeclarationADefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4015 | return MakeCXCursor(Def, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4016 | |
| 4017 | return clang_getNullCursor(); |
| 4018 | } |
| 4019 | |
| 4020 | case Decl::ObjCCategory: |
| 4021 | if (ObjCCategoryImplDecl *Impl |
| 4022 | = cast<ObjCCategoryDecl>(D)->getImplementation()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4023 | return MakeCXCursor(Impl, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4024 | return clang_getNullCursor(); |
| 4025 | |
| 4026 | case Decl::ObjCProtocol: |
| 4027 | if (!cast<ObjCProtocolDecl>(D)->isForwardDecl()) |
| 4028 | return C; |
| 4029 | return clang_getNullCursor(); |
| 4030 | |
| 4031 | case Decl::ObjCInterface: |
| 4032 | // There are two notions of a "definition" for an Objective-C |
| 4033 | // class: the interface and its implementation. When we resolved a |
| 4034 | // reference to an Objective-C class, produce the @interface as |
| 4035 | // the definition; when we were provided with the interface, |
| 4036 | // produce the @implementation as the definition. |
| 4037 | if (WasReference) { |
| 4038 | if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl()) |
| 4039 | return C; |
| 4040 | } else if (ObjCImplementationDecl *Impl |
| 4041 | = cast<ObjCInterfaceDecl>(D)->getImplementation()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4042 | return MakeCXCursor(Impl, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4043 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4044 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4045 | case Decl::ObjCProperty: |
| 4046 | // FIXME: We don't really know where to find the |
| 4047 | // ObjCPropertyImplDecls that implement this property. |
| 4048 | return clang_getNullCursor(); |
| 4049 | |
| 4050 | case Decl::ObjCCompatibleAlias: |
| 4051 | if (ObjCInterfaceDecl *Class |
| 4052 | = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) |
| 4053 | if (!Class->isForwardDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4054 | return MakeCXCursor(Class, TU); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4055 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4056 | return clang_getNullCursor(); |
| 4057 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4058 | case Decl::ObjCForwardProtocol: |
| 4059 | return MakeCursorOverloadedDeclRef(cast<ObjCForwardProtocolDecl>(D), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4060 | D->getLocation(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4061 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4062 | case Decl::ObjCClass: |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 4063 | return MakeCursorOverloadedDeclRef(cast<ObjCClassDecl>(D), D->getLocation(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4064 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4065 | |
| 4066 | case Decl::Friend: |
| 4067 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4068 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4069 | return clang_getNullCursor(); |
| 4070 | |
| 4071 | case Decl::FriendTemplate: |
| 4072 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4073 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4074 | return clang_getNullCursor(); |
| 4075 | } |
| 4076 | |
| 4077 | return clang_getNullCursor(); |
| 4078 | } |
| 4079 | |
| 4080 | unsigned clang_isCursorDefinition(CXCursor C) { |
| 4081 | if (!clang_isDeclaration(C.kind)) |
| 4082 | return 0; |
| 4083 | |
| 4084 | return clang_getCursorDefinition(C) == C; |
| 4085 | } |
| 4086 | |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4087 | CXCursor clang_getCanonicalCursor(CXCursor C) { |
| 4088 | if (!clang_isDeclaration(C.kind)) |
| 4089 | return C; |
| 4090 | |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4091 | if (Decl *D = getCursorDecl(C)) { |
Argyrios Kyrtzidis | debb00f | 2011-07-15 22:37:58 +0000 | [diff] [blame] | 4092 | if (ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 4093 | if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl()) |
| 4094 | return MakeCXCursor(CatD, getCursorTU(C)); |
| 4095 | |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4096 | if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) |
| 4097 | if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
| 4098 | return MakeCXCursor(IFD, getCursorTU(C)); |
| 4099 | |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4100 | return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C)); |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4101 | } |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4102 | |
| 4103 | return C; |
| 4104 | } |
| 4105 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4106 | unsigned clang_getNumOverloadedDecls(CXCursor C) { |
Douglas Gregor | 7c432dd | 2010-09-16 13:54:00 +0000 | [diff] [blame] | 4107 | if (C.kind != CXCursor_OverloadedDeclRef) |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4108 | return 0; |
| 4109 | |
| 4110 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
| 4111 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
| 4112 | return E->getNumDecls(); |
| 4113 | |
| 4114 | if (OverloadedTemplateStorage *S |
| 4115 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 4116 | return S->size(); |
| 4117 | |
| 4118 | Decl *D = Storage.get<Decl*>(); |
| 4119 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Argyrios Kyrtzidis | 826faa2 | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 4120 | return Using->shadow_size(); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 4121 | if (isa<ObjCClassDecl>(D)) |
| 4122 | return 1; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4123 | if (ObjCForwardProtocolDecl *Protocols =dyn_cast<ObjCForwardProtocolDecl>(D)) |
| 4124 | return Protocols->protocol_size(); |
| 4125 | |
| 4126 | return 0; |
| 4127 | } |
| 4128 | |
| 4129 | CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { |
Douglas Gregor | 7c432dd | 2010-09-16 13:54:00 +0000 | [diff] [blame] | 4130 | if (cursor.kind != CXCursor_OverloadedDeclRef) |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4131 | return clang_getNullCursor(); |
| 4132 | |
| 4133 | if (index >= clang_getNumOverloadedDecls(cursor)) |
| 4134 | return clang_getNullCursor(); |
| 4135 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4136 | CXTranslationUnit TU = getCursorTU(cursor); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4137 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first; |
| 4138 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4139 | return MakeCXCursor(E->decls_begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4140 | |
| 4141 | if (OverloadedTemplateStorage *S |
| 4142 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4143 | return MakeCXCursor(S->begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4144 | |
| 4145 | Decl *D = Storage.get<Decl*>(); |
| 4146 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) { |
| 4147 | // FIXME: This is, unfortunately, linear time. |
| 4148 | UsingDecl::shadow_iterator Pos = Using->shadow_begin(); |
| 4149 | std::advance(Pos, index); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4150 | return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4151 | } |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4152 | if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 4153 | return MakeCXCursor(Classes->getForwardInterfaceDecl(), TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4154 | if (ObjCForwardProtocolDecl *Protocols = dyn_cast<ObjCForwardProtocolDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4155 | return MakeCXCursor(Protocols->protocol_begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4156 | |
| 4157 | return clang_getNullCursor(); |
| 4158 | } |
| 4159 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 4160 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4161 | const char **startBuf, |
| 4162 | const char **endBuf, |
| 4163 | unsigned *startLine, |
| 4164 | unsigned *startColumn, |
| 4165 | unsigned *endLine, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 4166 | unsigned *endColumn) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 4167 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
| 4168 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4169 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 4170 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4171 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4172 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 4173 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 4174 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 4175 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 4176 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 4177 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 4178 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 4179 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4180 | |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 4181 | |
| 4182 | CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags, |
| 4183 | unsigned PieceIndex) { |
| 4184 | RefNamePieces Pieces; |
| 4185 | |
| 4186 | switch (C.kind) { |
| 4187 | case CXCursor_MemberRefExpr: |
| 4188 | if (MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C))) |
| 4189 | Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(), |
| 4190 | E->getQualifierLoc().getSourceRange()); |
| 4191 | break; |
| 4192 | |
| 4193 | case CXCursor_DeclRefExpr: |
| 4194 | if (DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C))) |
| 4195 | Pieces = buildPieces(NameFlags, false, E->getNameInfo(), |
| 4196 | E->getQualifierLoc().getSourceRange(), |
| 4197 | E->getExplicitTemplateArgsOpt()); |
| 4198 | break; |
| 4199 | |
| 4200 | case CXCursor_CallExpr: |
| 4201 | if (CXXOperatorCallExpr *OCE = |
| 4202 | dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) { |
| 4203 | Expr *Callee = OCE->getCallee(); |
| 4204 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) |
| 4205 | Callee = ICE->getSubExpr(); |
| 4206 | |
| 4207 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) |
| 4208 | Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(), |
| 4209 | DRE->getQualifierLoc().getSourceRange()); |
| 4210 | } |
| 4211 | break; |
| 4212 | |
| 4213 | default: |
| 4214 | break; |
| 4215 | } |
| 4216 | |
| 4217 | if (Pieces.empty()) { |
| 4218 | if (PieceIndex == 0) |
| 4219 | return clang_getCursorExtent(C); |
| 4220 | } else if (PieceIndex < Pieces.size()) { |
| 4221 | SourceRange R = Pieces[PieceIndex]; |
| 4222 | if (R.isValid()) |
| 4223 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 4224 | } |
| 4225 | |
| 4226 | return clang_getNullRange(); |
| 4227 | } |
| 4228 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 4229 | void clang_enableStackTraces(void) { |
| 4230 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 4231 | } |
| 4232 | |
Daniel Dunbar | 995aaf9 | 2010-11-04 01:26:29 +0000 | [diff] [blame] | 4233 | void clang_executeOnThread(void (*fn)(void*), void *user_data, |
| 4234 | unsigned stack_size) { |
| 4235 | llvm::llvm_execute_on_thread(fn, user_data, stack_size); |
| 4236 | } |
| 4237 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 4238 | } // end: extern "C" |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4239 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 4240 | //===----------------------------------------------------------------------===// |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4241 | // Token-based Operations. |
| 4242 | //===----------------------------------------------------------------------===// |
| 4243 | |
| 4244 | /* CXToken layout: |
| 4245 | * int_data[0]: a CXTokenKind |
| 4246 | * int_data[1]: starting token location |
| 4247 | * int_data[2]: token length |
| 4248 | * int_data[3]: reserved |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4249 | * ptr_data: for identifiers and keywords, an IdentifierInfo*. |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4250 | * otherwise unused. |
| 4251 | */ |
| 4252 | extern "C" { |
| 4253 | |
| 4254 | CXTokenKind clang_getTokenKind(CXToken CXTok) { |
| 4255 | return static_cast<CXTokenKind>(CXTok.int_data[0]); |
| 4256 | } |
| 4257 | |
| 4258 | CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { |
| 4259 | switch (clang_getTokenKind(CXTok)) { |
| 4260 | case CXToken_Identifier: |
| 4261 | case CXToken_Keyword: |
| 4262 | // We know we have an IdentifierInfo*, so use that. |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 4263 | return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data) |
| 4264 | ->getNameStart()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4265 | |
| 4266 | case CXToken_Literal: { |
| 4267 | // We have stashed the starting pointer in the ptr_data field. Use it. |
| 4268 | const char *Text = static_cast<const char *>(CXTok.ptr_data); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4269 | return createCXString(StringRef(Text, CXTok.int_data[2])); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4270 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4271 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4272 | case CXToken_Punctuation: |
| 4273 | case CXToken_Comment: |
| 4274 | break; |
| 4275 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4276 | |
| 4277 | // We have to find the starting buffer pointer the hard way, by |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4278 | // deconstructing the source location. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4279 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4280 | if (!CXXUnit) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 4281 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4282 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4283 | SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); |
| 4284 | std::pair<FileID, unsigned> LocInfo |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4285 | = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4286 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4287 | StringRef Buffer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4288 | = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); |
| 4289 | if (Invalid) |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4290 | return createCXString(""); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4291 | |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4292 | return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2])); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4293 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4294 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4295 | CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4296 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4297 | if (!CXXUnit) |
| 4298 | return clang_getNullLocation(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4299 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4300 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), |
| 4301 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 4302 | } |
| 4303 | |
| 4304 | CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4305 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 4306 | if (!CXXUnit) |
| 4307 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4308 | |
| 4309 | return cxloc::translateSourceRange(CXXUnit->getASTContext(), |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4310 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 4311 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4312 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4313 | static void getTokens(ASTUnit *CXXUnit, SourceRange Range, |
| 4314 | SmallVectorImpl<CXToken> &CXTokens) { |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4315 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 4316 | std::pair<FileID, unsigned> BeginLocInfo |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4317 | = SourceMgr.getDecomposedLoc(Range.getBegin()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4318 | std::pair<FileID, unsigned> EndLocInfo |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4319 | = SourceMgr.getDecomposedLoc(Range.getEnd()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4320 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4321 | // Cannot tokenize across files. |
| 4322 | if (BeginLocInfo.first != EndLocInfo.first) |
| 4323 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4324 | |
| 4325 | // Create a lexer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4326 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4327 | StringRef Buffer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4328 | = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
Douglas Gregor | 47a3fcd | 2010-03-16 20:26:15 +0000 | [diff] [blame] | 4329 | if (Invalid) |
| 4330 | return; |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4331 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4332 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 4333 | CXXUnit->getASTContext().getLangOptions(), |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4334 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4335 | Lex.SetCommentRetentionState(true); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4336 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4337 | // Lex tokens until we hit the end of the range. |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4338 | const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4339 | Token Tok; |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4340 | bool previousWasAt = false; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4341 | do { |
| 4342 | // Lex the next token |
| 4343 | Lex.LexFromRawLexer(Tok); |
| 4344 | if (Tok.is(tok::eof)) |
| 4345 | break; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4346 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4347 | // Initialize the CXToken. |
| 4348 | CXToken CXTok; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4349 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4350 | // - Common fields |
| 4351 | CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); |
| 4352 | CXTok.int_data[2] = Tok.getLength(); |
| 4353 | CXTok.int_data[3] = 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4354 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4355 | // - Kind-specific fields |
| 4356 | if (Tok.isLiteral()) { |
| 4357 | CXTok.int_data[0] = CXToken_Literal; |
| 4358 | CXTok.ptr_data = (void *)Tok.getLiteralData(); |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4359 | } else if (Tok.is(tok::raw_identifier)) { |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4360 | // Lookup the identifier to determine whether we have a keyword. |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4361 | IdentifierInfo *II |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4362 | = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok); |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4363 | |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4364 | if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4365 | CXTok.int_data[0] = CXToken_Keyword; |
| 4366 | } |
| 4367 | else { |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4368 | CXTok.int_data[0] = Tok.is(tok::identifier) |
| 4369 | ? CXToken_Identifier |
| 4370 | : CXToken_Keyword; |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4371 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4372 | CXTok.ptr_data = II; |
| 4373 | } else if (Tok.is(tok::comment)) { |
| 4374 | CXTok.int_data[0] = CXToken_Comment; |
| 4375 | CXTok.ptr_data = 0; |
| 4376 | } else { |
| 4377 | CXTok.int_data[0] = CXToken_Punctuation; |
| 4378 | CXTok.ptr_data = 0; |
| 4379 | } |
| 4380 | CXTokens.push_back(CXTok); |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4381 | previousWasAt = Tok.is(tok::at); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4382 | } while (Lex.getBufferLocation() <= EffectiveBufferEnd); |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4383 | } |
| 4384 | |
| 4385 | void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, |
| 4386 | CXToken **Tokens, unsigned *NumTokens) { |
| 4387 | if (Tokens) |
| 4388 | *Tokens = 0; |
| 4389 | if (NumTokens) |
| 4390 | *NumTokens = 0; |
| 4391 | |
| 4392 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 4393 | if (!CXXUnit || !Tokens || !NumTokens) |
| 4394 | return; |
| 4395 | |
| 4396 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 4397 | |
| 4398 | SourceRange R = cxloc::translateCXSourceRange(Range); |
| 4399 | if (R.isInvalid()) |
| 4400 | return; |
| 4401 | |
| 4402 | SmallVector<CXToken, 32> CXTokens; |
| 4403 | getTokens(CXXUnit, R, CXTokens); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4404 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4405 | if (CXTokens.empty()) |
| 4406 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4407 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4408 | *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size()); |
| 4409 | memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); |
| 4410 | *NumTokens = CXTokens.size(); |
| 4411 | } |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4412 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4413 | void clang_disposeTokens(CXTranslationUnit TU, |
| 4414 | CXToken *Tokens, unsigned NumTokens) { |
| 4415 | free(Tokens); |
| 4416 | } |
| 4417 | |
| 4418 | } // end: extern "C" |
| 4419 | |
| 4420 | //===----------------------------------------------------------------------===// |
| 4421 | // Token annotation APIs. |
| 4422 | //===----------------------------------------------------------------------===// |
| 4423 | |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4424 | typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4425 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 4426 | CXCursor parent, |
| 4427 | CXClientData client_data); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4428 | namespace { |
| 4429 | class AnnotateTokensWorker { |
| 4430 | AnnotateTokensData &Annotated; |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4431 | CXToken *Tokens; |
| 4432 | CXCursor *Cursors; |
| 4433 | unsigned NumTokens; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4434 | unsigned TokIdx; |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4435 | unsigned PreprocessingTokIdx; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4436 | CursorVisitor AnnotateVis; |
| 4437 | SourceManager &SrcMgr; |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4438 | bool HasContextSensitiveKeywords; |
| 4439 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4440 | bool MoreTokens() const { return TokIdx < NumTokens; } |
| 4441 | unsigned NextToken() const { return TokIdx; } |
| 4442 | void AdvanceToken() { ++TokIdx; } |
| 4443 | SourceLocation GetTokenLoc(unsigned tokI) { |
| 4444 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); |
| 4445 | } |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4446 | bool isFunctionMacroToken(unsigned tokI) const { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4447 | return Tokens[tokI].int_data[3] != 0; |
| 4448 | } |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4449 | SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4450 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[3]); |
| 4451 | } |
| 4452 | |
| 4453 | void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4454 | void annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult, |
| 4455 | SourceRange); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4456 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4457 | public: |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4458 | AnnotateTokensWorker(AnnotateTokensData &annotated, |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4459 | CXToken *tokens, CXCursor *cursors, unsigned numTokens, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4460 | CXTranslationUnit tu, SourceRange RegionOfInterest) |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4461 | : Annotated(annotated), Tokens(tokens), Cursors(cursors), |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4462 | NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4463 | AnnotateVis(tu, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 4464 | AnnotateTokensVisitor, this, |
| 4465 | /*VisitPreprocessorLast=*/true, |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 4466 | /*VisitIncludedEntities=*/false, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 4467 | RegionOfInterest), |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4468 | SrcMgr(static_cast<ASTUnit*>(tu->TUData)->getSourceManager()), |
| 4469 | HasContextSensitiveKeywords(false) { } |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4470 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4471 | void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4472 | enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4473 | void AnnotateTokens(CXCursor parent); |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 4474 | void AnnotateTokens() { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4475 | AnnotateTokens(clang_getTranslationUnitCursor(AnnotateVis.getTU())); |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 4476 | } |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4477 | |
| 4478 | /// \brief Determine whether the annotator saw any cursors that have |
| 4479 | /// context-sensitive keywords. |
| 4480 | bool hasContextSensitiveKeywords() const { |
| 4481 | return HasContextSensitiveKeywords; |
| 4482 | } |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4483 | }; |
| 4484 | } |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4485 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4486 | void AnnotateTokensWorker::AnnotateTokens(CXCursor parent) { |
| 4487 | // Walk the AST within the region of interest, annotating tokens |
| 4488 | // along the way. |
| 4489 | VisitChildren(parent); |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4490 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4491 | for (unsigned I = 0 ; I < TokIdx ; ++I) { |
| 4492 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4493 | if (Pos != Annotated.end() && |
| 4494 | (clang_isInvalid(Cursors[I].kind) || |
| 4495 | Pos->second.kind != CXCursor_PreprocessingDirective)) |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4496 | Cursors[I] = Pos->second; |
| 4497 | } |
| 4498 | |
| 4499 | // Finish up annotating any tokens left. |
| 4500 | if (!MoreTokens()) |
| 4501 | return; |
| 4502 | |
| 4503 | const CXCursor &C = clang_getNullCursor(); |
| 4504 | for (unsigned I = TokIdx ; I < NumTokens ; ++I) { |
| 4505 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); |
| 4506 | Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second; |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4507 | } |
| 4508 | } |
| 4509 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4510 | /// \brief It annotates and advances tokens with a cursor until the comparison |
| 4511 | //// between the cursor location and the source range is the same as |
| 4512 | /// \arg compResult. |
| 4513 | /// |
| 4514 | /// Pass RangeBefore to annotate tokens with a cursor until a range is reached. |
| 4515 | /// Pass RangeOverlap to annotate tokens inside a range. |
| 4516 | void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC, |
| 4517 | RangeComparisonResult compResult, |
| 4518 | SourceRange range) { |
| 4519 | while (MoreTokens()) { |
| 4520 | const unsigned I = NextToken(); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4521 | if (isFunctionMacroToken(I)) |
| 4522 | return annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4523 | |
| 4524 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4525 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 4526 | Cursors[I] = updateC; |
| 4527 | AdvanceToken(); |
| 4528 | continue; |
| 4529 | } |
| 4530 | break; |
| 4531 | } |
| 4532 | } |
| 4533 | |
| 4534 | /// \brief Special annotation handling for macro argument tokens. |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4535 | void AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens( |
| 4536 | CXCursor updateC, |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4537 | RangeComparisonResult compResult, |
| 4538 | SourceRange range) { |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4539 | assert(MoreTokens()); |
| 4540 | assert(isFunctionMacroToken(NextToken()) && |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4541 | "Should be called only for macro arg tokens"); |
| 4542 | |
| 4543 | // This works differently than annotateAndAdvanceTokens; because expanded |
| 4544 | // macro arguments can have arbitrary translation-unit source order, we do not |
| 4545 | // advance the token index one by one until a token fails the range test. |
| 4546 | // We only advance once past all of the macro arg tokens if all of them |
| 4547 | // pass the range test. If one of them fails we keep the token index pointing |
| 4548 | // at the start of the macro arg tokens so that the failing token will be |
| 4549 | // annotated by a subsequent annotation try. |
| 4550 | |
| 4551 | bool atLeastOneCompFail = false; |
| 4552 | |
| 4553 | unsigned I = NextToken(); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4554 | for (; I < NumTokens && isFunctionMacroToken(I); ++I) { |
| 4555 | SourceLocation TokLoc = getFunctionMacroTokenLoc(I); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4556 | if (TokLoc.isFileID()) |
| 4557 | continue; // not macro arg token, it's parens or comma. |
| 4558 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 4559 | if (clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 4560 | Cursors[I] = updateC; |
| 4561 | } else |
| 4562 | atLeastOneCompFail = true; |
| 4563 | } |
| 4564 | |
| 4565 | if (!atLeastOneCompFail) |
| 4566 | TokIdx = I; // All of the tokens were handled, advance beyond all of them. |
| 4567 | } |
| 4568 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4569 | enum CXChildVisitResult |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4570 | AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4571 | CXSourceLocation Loc = clang_getCursorLocation(cursor); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4572 | SourceRange cursorRange = getRawCursorExtent(cursor); |
Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 4573 | if (cursorRange.isInvalid()) |
| 4574 | return CXChildVisit_Recurse; |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4575 | |
| 4576 | if (!HasContextSensitiveKeywords) { |
| 4577 | // Objective-C properties can have context-sensitive keywords. |
| 4578 | if (cursor.kind == CXCursor_ObjCPropertyDecl) { |
| 4579 | if (ObjCPropertyDecl *Property |
| 4580 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor))) |
| 4581 | HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0; |
| 4582 | } |
| 4583 | // Objective-C methods can have context-sensitive keywords. |
| 4584 | else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl || |
| 4585 | cursor.kind == CXCursor_ObjCClassMethodDecl) { |
| 4586 | if (ObjCMethodDecl *Method |
| 4587 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 4588 | if (Method->getObjCDeclQualifier()) |
| 4589 | HasContextSensitiveKeywords = true; |
| 4590 | else { |
| 4591 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 4592 | PEnd = Method->param_end(); |
| 4593 | P != PEnd; ++P) { |
| 4594 | if ((*P)->getObjCDeclQualifier()) { |
| 4595 | HasContextSensitiveKeywords = true; |
| 4596 | break; |
| 4597 | } |
| 4598 | } |
| 4599 | } |
| 4600 | } |
| 4601 | } |
| 4602 | // C++ methods can have context-sensitive keywords. |
| 4603 | else if (cursor.kind == CXCursor_CXXMethod) { |
| 4604 | if (CXXMethodDecl *Method |
| 4605 | = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) { |
| 4606 | if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>()) |
| 4607 | HasContextSensitiveKeywords = true; |
| 4608 | } |
| 4609 | } |
| 4610 | // C++ classes can have context-sensitive keywords. |
| 4611 | else if (cursor.kind == CXCursor_StructDecl || |
| 4612 | cursor.kind == CXCursor_ClassDecl || |
| 4613 | cursor.kind == CXCursor_ClassTemplate || |
| 4614 | cursor.kind == CXCursor_ClassTemplatePartialSpecialization) { |
| 4615 | if (Decl *D = getCursorDecl(cursor)) |
| 4616 | if (D->hasAttr<FinalAttr>()) |
| 4617 | HasContextSensitiveKeywords = true; |
| 4618 | } |
| 4619 | } |
| 4620 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4621 | if (clang_isPreprocessing(cursor.kind)) { |
Chandler Carruth | cea731a | 2011-07-14 16:07:57 +0000 | [diff] [blame] | 4622 | // For macro expansions, just note where the beginning of the macro |
| 4623 | // expansion occurs. |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 4624 | if (cursor.kind == CXCursor_MacroExpansion) { |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4625 | Annotated[Loc.int_data] = cursor; |
| 4626 | return CXChildVisit_Recurse; |
| 4627 | } |
| 4628 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4629 | // Items in the preprocessing record are kept separate from items in |
| 4630 | // declarations, so we keep a separate token index. |
| 4631 | unsigned SavedTokIdx = TokIdx; |
| 4632 | TokIdx = PreprocessingTokIdx; |
| 4633 | |
| 4634 | // Skip tokens up until we catch up to the beginning of the preprocessing |
| 4635 | // entry. |
| 4636 | while (MoreTokens()) { |
| 4637 | const unsigned I = NextToken(); |
| 4638 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4639 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 4640 | case RangeBefore: |
| 4641 | AdvanceToken(); |
| 4642 | continue; |
| 4643 | case RangeAfter: |
| 4644 | case RangeOverlap: |
| 4645 | break; |
| 4646 | } |
| 4647 | break; |
| 4648 | } |
| 4649 | |
| 4650 | // Look at all of the tokens within this range. |
| 4651 | while (MoreTokens()) { |
| 4652 | const unsigned I = NextToken(); |
| 4653 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4654 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 4655 | case RangeBefore: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4656 | llvm_unreachable("Infeasible"); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4657 | case RangeAfter: |
| 4658 | break; |
| 4659 | case RangeOverlap: |
| 4660 | Cursors[I] = cursor; |
| 4661 | AdvanceToken(); |
| 4662 | continue; |
| 4663 | } |
| 4664 | break; |
| 4665 | } |
| 4666 | |
| 4667 | // Save the preprocessing token index; restore the non-preprocessing |
| 4668 | // token index. |
| 4669 | PreprocessingTokIdx = TokIdx; |
| 4670 | TokIdx = SavedTokIdx; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4671 | return CXChildVisit_Recurse; |
| 4672 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4673 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4674 | if (cursorRange.isInvalid()) |
| 4675 | return CXChildVisit_Continue; |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4676 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4677 | SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data); |
| 4678 | |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4679 | // Adjust the annotated range based specific declarations. |
| 4680 | const enum CXCursorKind cursorK = clang_getCursorKind(cursor); |
| 4681 | if (cursorK >= CXCursor_FirstDecl && cursorK <= CXCursor_LastDecl) { |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 4682 | Decl *D = cxcursor::getCursorDecl(cursor); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4683 | |
| 4684 | SourceLocation StartLoc; |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 4685 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4686 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
| 4687 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 4688 | } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
| 4689 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
| 4690 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4691 | } |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4692 | |
| 4693 | if (StartLoc.isValid() && L.isValid() && |
| 4694 | SrcMgr.isBeforeInTranslationUnit(StartLoc, L)) |
| 4695 | cursorRange.setBegin(StartLoc); |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4696 | } |
Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 4697 | |
Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 4698 | // If the location of the cursor occurs within a macro instantiation, record |
| 4699 | // the spelling location of the cursor in our annotation map. We can then |
| 4700 | // paper over the token labelings during a post-processing step to try and |
| 4701 | // get cursor mappings for tokens that are the *arguments* of a macro |
| 4702 | // instantiation. |
| 4703 | if (L.isMacroID()) { |
| 4704 | unsigned rawEncoding = SrcMgr.getSpellingLoc(L).getRawEncoding(); |
| 4705 | // Only invalidate the old annotation if it isn't part of a preprocessing |
| 4706 | // directive. Here we assume that the default construction of CXCursor |
| 4707 | // results in CXCursor.kind being an initialized value (i.e., 0). If |
| 4708 | // this isn't the case, we can fix by doing lookup + insertion. |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4709 | |
Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 4710 | CXCursor &oldC = Annotated[rawEncoding]; |
| 4711 | if (!clang_isPreprocessing(oldC.kind)) |
| 4712 | oldC = cursor; |
| 4713 | } |
| 4714 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4715 | const enum CXCursorKind K = clang_getCursorKind(parent); |
| 4716 | const CXCursor updateC = |
Ted Kremenek | d8b0a84 | 2010-08-25 22:16:02 +0000 | [diff] [blame] | 4717 | (clang_isInvalid(K) || K == CXCursor_TranslationUnit) |
| 4718 | ? clang_getNullCursor() : parent; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4719 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4720 | annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4721 | |
Argyrios Kyrtzidis | 5517b89 | 2011-06-27 19:42:20 +0000 | [diff] [blame] | 4722 | // Avoid having the cursor of an expression "overwrite" the annotation of the |
| 4723 | // variable declaration that it belongs to. |
| 4724 | // This can happen for C++ constructor expressions whose range generally |
| 4725 | // include the variable declaration, e.g.: |
| 4726 | // MyCXXClass foo; // Make sure we don't annotate 'foo' as a CallExpr cursor. |
| 4727 | if (clang_isExpression(cursorK)) { |
| 4728 | Expr *E = getCursorExpr(cursor); |
Argyrios Kyrtzidis | 8ccac3d | 2011-06-29 22:20:07 +0000 | [diff] [blame] | 4729 | if (Decl *D = getCursorParentDecl(cursor)) { |
Argyrios Kyrtzidis | 5517b89 | 2011-06-27 19:42:20 +0000 | [diff] [blame] | 4730 | const unsigned I = NextToken(); |
| 4731 | if (E->getLocStart().isValid() && D->getLocation().isValid() && |
| 4732 | E->getLocStart() == D->getLocation() && |
| 4733 | E->getLocStart() == GetTokenLoc(I)) { |
| 4734 | Cursors[I] = updateC; |
| 4735 | AdvanceToken(); |
| 4736 | } |
| 4737 | } |
| 4738 | } |
| 4739 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4740 | // Visit children to get their cursor information. |
| 4741 | const unsigned BeforeChildren = NextToken(); |
| 4742 | VisitChildren(cursor); |
| 4743 | const unsigned AfterChildren = NextToken(); |
| 4744 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4745 | // Scan the tokens that are at the end of the cursor, but are not captured |
| 4746 | // but the child cursors. |
| 4747 | annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4748 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4749 | // Scan the tokens that are at the beginning of the cursor, but are not |
| 4750 | // capture by the child cursors. |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4751 | for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { |
| 4752 | if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 4753 | break; |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4754 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4755 | Cursors[I] = cursor; |
| 4756 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4757 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4758 | return CXChildVisit_Continue; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4759 | } |
| 4760 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4761 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 4762 | CXCursor parent, |
| 4763 | CXClientData client_data) { |
| 4764 | return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); |
| 4765 | } |
| 4766 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4767 | namespace { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4768 | |
| 4769 | /// \brief Uses the macro expansions in the preprocessing record to find |
| 4770 | /// and mark tokens that are macro arguments. This info is used by the |
| 4771 | /// AnnotateTokensWorker. |
| 4772 | class MarkMacroArgTokensVisitor { |
| 4773 | SourceManager &SM; |
| 4774 | CXToken *Tokens; |
| 4775 | unsigned NumTokens; |
| 4776 | unsigned CurIdx; |
| 4777 | |
| 4778 | public: |
| 4779 | MarkMacroArgTokensVisitor(SourceManager &SM, |
| 4780 | CXToken *tokens, unsigned numTokens) |
| 4781 | : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { } |
| 4782 | |
| 4783 | CXChildVisitResult visit(CXCursor cursor, CXCursor parent) { |
| 4784 | if (cursor.kind != CXCursor_MacroExpansion) |
| 4785 | return CXChildVisit_Continue; |
| 4786 | |
| 4787 | SourceRange macroRange = getCursorMacroExpansion(cursor)->getSourceRange(); |
| 4788 | if (macroRange.getBegin() == macroRange.getEnd()) |
| 4789 | return CXChildVisit_Continue; // it's not a function macro. |
| 4790 | |
| 4791 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 4792 | if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx), |
| 4793 | macroRange.getBegin())) |
| 4794 | break; |
| 4795 | } |
| 4796 | |
| 4797 | if (CurIdx == NumTokens) |
| 4798 | return CXChildVisit_Break; |
| 4799 | |
| 4800 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 4801 | SourceLocation tokLoc = getTokenLoc(CurIdx); |
| 4802 | if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd())) |
| 4803 | break; |
| 4804 | |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4805 | setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc)); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4806 | } |
| 4807 | |
| 4808 | if (CurIdx == NumTokens) |
| 4809 | return CXChildVisit_Break; |
| 4810 | |
| 4811 | return CXChildVisit_Continue; |
| 4812 | } |
| 4813 | |
| 4814 | private: |
| 4815 | SourceLocation getTokenLoc(unsigned tokI) { |
| 4816 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); |
| 4817 | } |
| 4818 | |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4819 | void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4820 | // The third field is reserved and currently not used. Use it here |
| 4821 | // to mark macro arg expanded tokens with their expanded locations. |
| 4822 | Tokens[tokI].int_data[3] = loc.getRawEncoding(); |
| 4823 | } |
| 4824 | }; |
| 4825 | |
| 4826 | } // end anonymous namespace |
| 4827 | |
| 4828 | static CXChildVisitResult |
| 4829 | MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent, |
| 4830 | CXClientData client_data) { |
| 4831 | return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor, |
| 4832 | parent); |
| 4833 | } |
| 4834 | |
| 4835 | namespace { |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4836 | struct clang_annotateTokens_Data { |
| 4837 | CXTranslationUnit TU; |
| 4838 | ASTUnit *CXXUnit; |
| 4839 | CXToken *Tokens; |
| 4840 | unsigned NumTokens; |
| 4841 | CXCursor *Cursors; |
| 4842 | }; |
| 4843 | } |
| 4844 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4845 | static void annotatePreprocessorTokens(CXTranslationUnit TU, |
| 4846 | SourceRange RegionOfInterest, |
| 4847 | AnnotateTokensData &Annotated) { |
| 4848 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 4849 | |
| 4850 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 4851 | std::pair<FileID, unsigned> BeginLocInfo |
| 4852 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin()); |
| 4853 | std::pair<FileID, unsigned> EndLocInfo |
| 4854 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd()); |
| 4855 | |
| 4856 | if (BeginLocInfo.first != EndLocInfo.first) |
| 4857 | return; |
| 4858 | |
| 4859 | StringRef Buffer; |
| 4860 | bool Invalid = false; |
| 4861 | Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 4862 | if (Buffer.empty() || Invalid) |
| 4863 | return; |
| 4864 | |
| 4865 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 4866 | CXXUnit->getASTContext().getLangOptions(), |
| 4867 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, |
| 4868 | Buffer.end()); |
| 4869 | Lex.SetCommentRetentionState(true); |
| 4870 | |
| 4871 | // Lex tokens in raw mode until we hit the end of the range, to avoid |
| 4872 | // entering #includes or expanding macros. |
| 4873 | while (true) { |
| 4874 | Token Tok; |
| 4875 | Lex.LexFromRawLexer(Tok); |
| 4876 | |
| 4877 | reprocess: |
| 4878 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
| 4879 | // We have found a preprocessing directive. Gobble it up so that we |
| 4880 | // don't see it while preprocessing these tokens later, but keep track |
| 4881 | // of all of the token locations inside this preprocessing directive so |
| 4882 | // that we can annotate them appropriately. |
| 4883 | // |
| 4884 | // FIXME: Some simple tests here could identify macro definitions and |
| 4885 | // #undefs, to provide specific cursor kinds for those. |
| 4886 | SmallVector<SourceLocation, 32> Locations; |
| 4887 | do { |
| 4888 | Locations.push_back(Tok.getLocation()); |
| 4889 | Lex.LexFromRawLexer(Tok); |
| 4890 | } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof)); |
| 4891 | |
| 4892 | using namespace cxcursor; |
| 4893 | CXCursor Cursor |
| 4894 | = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(), |
| 4895 | Locations.back()), |
| 4896 | TU); |
| 4897 | for (unsigned I = 0, N = Locations.size(); I != N; ++I) { |
| 4898 | Annotated[Locations[I].getRawEncoding()] = Cursor; |
| 4899 | } |
| 4900 | |
| 4901 | if (Tok.isAtStartOfLine()) |
| 4902 | goto reprocess; |
| 4903 | |
| 4904 | continue; |
| 4905 | } |
| 4906 | |
| 4907 | if (Tok.is(tok::eof)) |
| 4908 | break; |
| 4909 | } |
| 4910 | } |
| 4911 | |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 4912 | // This gets run a separate thread to avoid stack blowout. |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4913 | static void clang_annotateTokensImpl(void *UserData) { |
| 4914 | CXTranslationUnit TU = ((clang_annotateTokens_Data*)UserData)->TU; |
| 4915 | ASTUnit *CXXUnit = ((clang_annotateTokens_Data*)UserData)->CXXUnit; |
| 4916 | CXToken *Tokens = ((clang_annotateTokens_Data*)UserData)->Tokens; |
| 4917 | const unsigned NumTokens = ((clang_annotateTokens_Data*)UserData)->NumTokens; |
| 4918 | CXCursor *Cursors = ((clang_annotateTokens_Data*)UserData)->Cursors; |
| 4919 | |
| 4920 | // Determine the region of interest, which contains all of the tokens. |
| 4921 | SourceRange RegionOfInterest; |
| 4922 | RegionOfInterest.setBegin( |
| 4923 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0]))); |
| 4924 | RegionOfInterest.setEnd( |
| 4925 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, |
| 4926 | Tokens[NumTokens-1]))); |
| 4927 | |
| 4928 | // A mapping from the source locations found when re-lexing or traversing the |
| 4929 | // region of interest to the corresponding cursors. |
| 4930 | AnnotateTokensData Annotated; |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4931 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4932 | // Relex the tokens within the source range to look for preprocessing |
| 4933 | // directives. |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4934 | annotatePreprocessorTokens(TU, RegionOfInterest, Annotated); |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4935 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4936 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) { |
| 4937 | // Search and mark tokens that are macro argument expansions. |
| 4938 | MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(), |
| 4939 | Tokens, NumTokens); |
| 4940 | CursorVisitor MacroArgMarker(TU, |
| 4941 | MarkMacroArgTokensVisitorDelegate, &Visitor, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 4942 | /*VisitPreprocessorLast=*/true, |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 4943 | /*VisitIncludedEntities=*/false, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 4944 | RegionOfInterest); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4945 | MacroArgMarker.visitPreprocessedEntitiesInRegion(); |
| 4946 | } |
| 4947 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4948 | // Annotate all of the source locations in the region of interest that map to |
| 4949 | // a specific cursor. |
| 4950 | AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens, |
| 4951 | TU, RegionOfInterest); |
| 4952 | |
| 4953 | // FIXME: We use a ridiculous stack size here because the data-recursion |
| 4954 | // algorithm uses a large stack frame than the non-data recursive version, |
| 4955 | // and AnnotationTokensWorker currently transforms the data-recursion |
| 4956 | // algorithm back into a traditional recursion by explicitly calling |
| 4957 | // VisitChildren(). We will need to remove this explicit recursive call. |
| 4958 | W.AnnotateTokens(); |
| 4959 | |
| 4960 | // If we ran into any entities that involve context-sensitive keywords, |
| 4961 | // take another pass through the tokens to mark them as such. |
| 4962 | if (W.hasContextSensitiveKeywords()) { |
| 4963 | for (unsigned I = 0; I != NumTokens; ++I) { |
| 4964 | if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier) |
| 4965 | continue; |
| 4966 | |
| 4967 | if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) { |
| 4968 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 4969 | if (ObjCPropertyDecl *Property |
| 4970 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) { |
| 4971 | if (Property->getPropertyAttributesAsWritten() != 0 && |
| 4972 | llvm::StringSwitch<bool>(II->getName()) |
| 4973 | .Case("readonly", true) |
| 4974 | .Case("assign", true) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4975 | .Case("unsafe_unretained", true) |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4976 | .Case("readwrite", true) |
| 4977 | .Case("retain", true) |
| 4978 | .Case("copy", true) |
| 4979 | .Case("nonatomic", true) |
| 4980 | .Case("atomic", true) |
| 4981 | .Case("getter", true) |
| 4982 | .Case("setter", true) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4983 | .Case("strong", true) |
| 4984 | .Case("weak", true) |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4985 | .Default(false)) |
| 4986 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 4987 | } |
| 4988 | continue; |
| 4989 | } |
| 4990 | |
| 4991 | if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl || |
| 4992 | Cursors[I].kind == CXCursor_ObjCClassMethodDecl) { |
| 4993 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 4994 | if (llvm::StringSwitch<bool>(II->getName()) |
| 4995 | .Case("in", true) |
| 4996 | .Case("out", true) |
| 4997 | .Case("inout", true) |
| 4998 | .Case("oneway", true) |
| 4999 | .Case("bycopy", true) |
| 5000 | .Case("byref", true) |
| 5001 | .Default(false)) |
| 5002 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5003 | continue; |
| 5004 | } |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 5005 | |
| 5006 | if (Cursors[I].kind == CXCursor_CXXFinalAttr || |
| 5007 | Cursors[I].kind == CXCursor_CXXOverrideAttr) { |
| 5008 | Tokens[I].int_data[0] = CXToken_Keyword; |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5009 | continue; |
| 5010 | } |
| 5011 | } |
| 5012 | } |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5013 | } |
| 5014 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 5015 | extern "C" { |
| 5016 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5017 | void clang_annotateTokens(CXTranslationUnit TU, |
| 5018 | CXToken *Tokens, unsigned NumTokens, |
| 5019 | CXCursor *Cursors) { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5020 | |
| 5021 | if (NumTokens == 0 || !Tokens || !Cursors) |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5022 | return; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5023 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5024 | // Any token we don't specifically annotate will have a NULL cursor. |
| 5025 | CXCursor C = clang_getNullCursor(); |
| 5026 | for (unsigned I = 0; I != NumTokens; ++I) |
| 5027 | Cursors[I] = C; |
| 5028 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5029 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5030 | if (!CXXUnit) |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5031 | return; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5032 | |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 5033 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5034 | |
| 5035 | clang_annotateTokens_Data data = { TU, CXXUnit, Tokens, NumTokens, Cursors }; |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5036 | llvm::CrashRecoveryContext CRC; |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5037 | if (!RunSafely(CRC, clang_annotateTokensImpl, &data, |
Ted Kremenek | 6c53fdd | 2010-11-14 17:47:35 +0000 | [diff] [blame] | 5038 | GetSafetyThreadStackSize() * 2)) { |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5039 | fprintf(stderr, "libclang: crash detected while annotating tokens\n"); |
| 5040 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5041 | } |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5042 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5043 | } // end: extern "C" |
| 5044 | |
| 5045 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 5046 | // Operations for querying linkage of a cursor. |
| 5047 | //===----------------------------------------------------------------------===// |
| 5048 | |
| 5049 | extern "C" { |
| 5050 | CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 5051 | if (!clang_isDeclaration(cursor.kind)) |
| 5052 | return CXLinkage_Invalid; |
| 5053 | |
Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 5054 | Decl *D = cxcursor::getCursorDecl(cursor); |
| 5055 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
| 5056 | switch (ND->getLinkage()) { |
| 5057 | case NoLinkage: return CXLinkage_NoLinkage; |
| 5058 | case InternalLinkage: return CXLinkage_Internal; |
| 5059 | case UniqueExternalLinkage: return CXLinkage_UniqueExternal; |
| 5060 | case ExternalLinkage: return CXLinkage_External; |
| 5061 | }; |
| 5062 | |
| 5063 | return CXLinkage_Invalid; |
| 5064 | } |
| 5065 | } // end: extern "C" |
| 5066 | |
| 5067 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5068 | // Operations for querying language of a cursor. |
| 5069 | //===----------------------------------------------------------------------===// |
| 5070 | |
| 5071 | static CXLanguageKind getDeclLanguage(const Decl *D) { |
| 5072 | switch (D->getKind()) { |
| 5073 | default: |
| 5074 | break; |
| 5075 | case Decl::ImplicitParam: |
| 5076 | case Decl::ObjCAtDefsField: |
| 5077 | case Decl::ObjCCategory: |
| 5078 | case Decl::ObjCCategoryImpl: |
| 5079 | case Decl::ObjCClass: |
| 5080 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5081 | case Decl::ObjCForwardProtocol: |
| 5082 | case Decl::ObjCImplementation: |
| 5083 | case Decl::ObjCInterface: |
| 5084 | case Decl::ObjCIvar: |
| 5085 | case Decl::ObjCMethod: |
| 5086 | case Decl::ObjCProperty: |
| 5087 | case Decl::ObjCPropertyImpl: |
| 5088 | case Decl::ObjCProtocol: |
| 5089 | return CXLanguage_ObjC; |
| 5090 | case Decl::CXXConstructor: |
| 5091 | case Decl::CXXConversion: |
| 5092 | case Decl::CXXDestructor: |
| 5093 | case Decl::CXXMethod: |
| 5094 | case Decl::CXXRecord: |
| 5095 | case Decl::ClassTemplate: |
| 5096 | case Decl::ClassTemplatePartialSpecialization: |
| 5097 | case Decl::ClassTemplateSpecialization: |
| 5098 | case Decl::Friend: |
| 5099 | case Decl::FriendTemplate: |
| 5100 | case Decl::FunctionTemplate: |
| 5101 | case Decl::LinkageSpec: |
| 5102 | case Decl::Namespace: |
| 5103 | case Decl::NamespaceAlias: |
| 5104 | case Decl::NonTypeTemplateParm: |
| 5105 | case Decl::StaticAssert: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5106 | case Decl::TemplateTemplateParm: |
| 5107 | case Decl::TemplateTypeParm: |
| 5108 | case Decl::UnresolvedUsingTypename: |
| 5109 | case Decl::UnresolvedUsingValue: |
| 5110 | case Decl::Using: |
| 5111 | case Decl::UsingDirective: |
| 5112 | case Decl::UsingShadow: |
| 5113 | return CXLanguage_CPlusPlus; |
| 5114 | } |
| 5115 | |
| 5116 | return CXLanguage_C; |
| 5117 | } |
| 5118 | |
| 5119 | extern "C" { |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5120 | |
| 5121 | enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { |
| 5122 | if (clang_isDeclaration(cursor.kind)) |
| 5123 | if (Decl *D = cxcursor::getCursorDecl(cursor)) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5124 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()) |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5125 | return CXAvailability_Available; |
| 5126 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5127 | switch (D->getAvailability()) { |
| 5128 | case AR_Available: |
| 5129 | case AR_NotYetIntroduced: |
| 5130 | return CXAvailability_Available; |
| 5131 | |
| 5132 | case AR_Deprecated: |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5133 | return CXAvailability_Deprecated; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5134 | |
| 5135 | case AR_Unavailable: |
| 5136 | return CXAvailability_NotAvailable; |
| 5137 | } |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5138 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5139 | |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5140 | return CXAvailability_Available; |
| 5141 | } |
| 5142 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5143 | CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { |
| 5144 | if (clang_isDeclaration(cursor.kind)) |
| 5145 | return getDeclLanguage(cxcursor::getCursorDecl(cursor)); |
| 5146 | |
| 5147 | return CXLanguage_Invalid; |
| 5148 | } |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5149 | |
| 5150 | /// \brief If the given cursor is the "templated" declaration |
| 5151 | /// descibing a class or function template, return the class or |
| 5152 | /// function template. |
| 5153 | static Decl *maybeGetTemplateCursor(Decl *D) { |
| 5154 | if (!D) |
| 5155 | return 0; |
| 5156 | |
| 5157 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5158 | if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate()) |
| 5159 | return FunTmpl; |
| 5160 | |
| 5161 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
| 5162 | if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate()) |
| 5163 | return ClassTmpl; |
| 5164 | |
| 5165 | return D; |
| 5166 | } |
| 5167 | |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5168 | CXCursor clang_getCursorSemanticParent(CXCursor cursor) { |
| 5169 | if (clang_isDeclaration(cursor.kind)) { |
| 5170 | if (Decl *D = getCursorDecl(cursor)) { |
| 5171 | DeclContext *DC = D->getDeclContext(); |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5172 | if (!DC) |
| 5173 | return clang_getNullCursor(); |
| 5174 | |
| 5175 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 5176 | getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5177 | } |
| 5178 | } |
| 5179 | |
| 5180 | if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { |
| 5181 | if (Decl *D = getCursorDecl(cursor)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5182 | return MakeCXCursor(D, getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5183 | } |
| 5184 | |
| 5185 | return clang_getNullCursor(); |
| 5186 | } |
| 5187 | |
| 5188 | CXCursor clang_getCursorLexicalParent(CXCursor cursor) { |
| 5189 | if (clang_isDeclaration(cursor.kind)) { |
| 5190 | if (Decl *D = getCursorDecl(cursor)) { |
| 5191 | DeclContext *DC = D->getLexicalDeclContext(); |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5192 | if (!DC) |
| 5193 | return clang_getNullCursor(); |
| 5194 | |
| 5195 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 5196 | getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5197 | } |
| 5198 | } |
| 5199 | |
| 5200 | // FIXME: Note that we can't easily compute the lexical context of a |
| 5201 | // statement or expression, so we return nothing. |
| 5202 | return clang_getNullCursor(); |
| 5203 | } |
| 5204 | |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5205 | void clang_getOverriddenCursors(CXCursor cursor, |
| 5206 | CXCursor **overridden, |
| 5207 | unsigned *num_overridden) { |
| 5208 | if (overridden) |
| 5209 | *overridden = 0; |
| 5210 | if (num_overridden) |
| 5211 | *num_overridden = 0; |
| 5212 | if (!overridden || !num_overridden) |
| 5213 | return; |
| 5214 | |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 5215 | SmallVector<CXCursor, 8> Overridden; |
| 5216 | cxcursor::getOverriddenCursors(cursor, Overridden); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5217 | |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 5218 | *num_overridden = Overridden.size(); |
| 5219 | *overridden = new CXCursor [Overridden.size()]; |
| 5220 | std::copy(Overridden.begin(), Overridden.end(), *overridden); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5221 | } |
| 5222 | |
| 5223 | void clang_disposeOverriddenCursors(CXCursor *overridden) { |
| 5224 | delete [] overridden; |
| 5225 | } |
| 5226 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 5227 | CXFile clang_getIncludedFile(CXCursor cursor) { |
| 5228 | if (cursor.kind != CXCursor_InclusionDirective) |
| 5229 | return 0; |
| 5230 | |
| 5231 | InclusionDirective *ID = getCursorInclusionDirective(cursor); |
| 5232 | return (void *)ID->getFile(); |
| 5233 | } |
| 5234 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5235 | } // end: extern "C" |
| 5236 | |
Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 5237 | |
| 5238 | //===----------------------------------------------------------------------===// |
| 5239 | // C++ AST instrospection. |
| 5240 | //===----------------------------------------------------------------------===// |
| 5241 | |
| 5242 | extern "C" { |
| 5243 | unsigned clang_CXXMethod_isStatic(CXCursor C) { |
| 5244 | if (!clang_isDeclaration(C.kind)) |
| 5245 | return 0; |
Douglas Gregor | 49f6f54 | 2010-08-31 22:12:17 +0000 | [diff] [blame] | 5246 | |
| 5247 | CXXMethodDecl *Method = 0; |
| 5248 | Decl *D = cxcursor::getCursorDecl(C); |
| 5249 | if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 5250 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 5251 | else |
| 5252 | Method = dyn_cast_or_null<CXXMethodDecl>(D); |
| 5253 | return (Method && Method->isStatic()) ? 1 : 0; |
Ted Kremenek | 40b492a | 2010-05-17 20:12:45 +0000 | [diff] [blame] | 5254 | } |
Ted Kremenek | b12903e | 2010-05-18 22:32:15 +0000 | [diff] [blame] | 5255 | |
Douglas Gregor | 211924b | 2011-05-12 15:17:24 +0000 | [diff] [blame] | 5256 | unsigned clang_CXXMethod_isVirtual(CXCursor C) { |
| 5257 | if (!clang_isDeclaration(C.kind)) |
| 5258 | return 0; |
| 5259 | |
| 5260 | CXXMethodDecl *Method = 0; |
| 5261 | Decl *D = cxcursor::getCursorDecl(C); |
| 5262 | if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 5263 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 5264 | else |
| 5265 | Method = dyn_cast_or_null<CXXMethodDecl>(D); |
| 5266 | return (Method && Method->isVirtual()) ? 1 : 0; |
| 5267 | } |
Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 5268 | } // end: extern "C" |
| 5269 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5270 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5271 | // Attribute introspection. |
| 5272 | //===----------------------------------------------------------------------===// |
| 5273 | |
| 5274 | extern "C" { |
| 5275 | CXType clang_getIBOutletCollectionType(CXCursor C) { |
| 5276 | if (C.kind != CXCursor_IBOutletCollectionAttr) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5277 | return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C)); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5278 | |
| 5279 | IBOutletCollectionAttr *A = |
| 5280 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); |
| 5281 | |
Argyrios Kyrtzidis | 18aa2ff | 2011-09-13 18:49:52 +0000 | [diff] [blame] | 5282 | return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C)); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5283 | } |
| 5284 | } // end: extern "C" |
| 5285 | |
| 5286 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5287 | // Inspecting memory usage. |
| 5288 | //===----------------------------------------------------------------------===// |
| 5289 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5290 | typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5291 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5292 | static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries, |
| 5293 | enum CXTUResourceUsageKind k, |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5294 | unsigned long amount) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5295 | CXTUResourceUsageEntry entry = { k, amount }; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5296 | entries.push_back(entry); |
| 5297 | } |
| 5298 | |
| 5299 | extern "C" { |
| 5300 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5301 | const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5302 | const char *str = ""; |
| 5303 | switch (kind) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5304 | case CXTUResourceUsage_AST: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5305 | str = "ASTContext: expressions, declarations, and types"; |
| 5306 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5307 | case CXTUResourceUsage_Identifiers: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5308 | str = "ASTContext: identifiers"; |
| 5309 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5310 | case CXTUResourceUsage_Selectors: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5311 | str = "ASTContext: selectors"; |
Ted Kremenek | e294ab7 | 2011-04-19 04:36:17 +0000 | [diff] [blame] | 5312 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5313 | case CXTUResourceUsage_GlobalCompletionResults: |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5314 | str = "Code completion: cached global results"; |
Ted Kremenek | e294ab7 | 2011-04-19 04:36:17 +0000 | [diff] [blame] | 5315 | break; |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 5316 | case CXTUResourceUsage_SourceManagerContentCache: |
| 5317 | str = "SourceManager: content cache allocator"; |
| 5318 | break; |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5319 | case CXTUResourceUsage_AST_SideTables: |
| 5320 | str = "ASTContext: side tables"; |
| 5321 | break; |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5322 | case CXTUResourceUsage_SourceManager_Membuffer_Malloc: |
| 5323 | str = "SourceManager: malloc'ed memory buffers"; |
| 5324 | break; |
| 5325 | case CXTUResourceUsage_SourceManager_Membuffer_MMap: |
| 5326 | str = "SourceManager: mmap'ed memory buffers"; |
| 5327 | break; |
Ted Kremenek | e9b5f3d | 2011-04-28 23:46:20 +0000 | [diff] [blame] | 5328 | case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc: |
| 5329 | str = "ExternalASTSource: malloc'ed memory buffers"; |
| 5330 | break; |
| 5331 | case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap: |
| 5332 | str = "ExternalASTSource: mmap'ed memory buffers"; |
| 5333 | break; |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5334 | case CXTUResourceUsage_Preprocessor: |
| 5335 | str = "Preprocessor: malloc'ed memory"; |
| 5336 | break; |
| 5337 | case CXTUResourceUsage_PreprocessingRecord: |
| 5338 | str = "Preprocessor: PreprocessingRecord"; |
| 5339 | break; |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5340 | case CXTUResourceUsage_SourceManager_DataStructures: |
| 5341 | str = "SourceManager: data structures and tables"; |
| 5342 | break; |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 5343 | case CXTUResourceUsage_Preprocessor_HeaderSearch: |
| 5344 | str = "Preprocessor: header search tables"; |
| 5345 | break; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5346 | } |
| 5347 | return str; |
| 5348 | } |
| 5349 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5350 | CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5351 | if (!TU) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5352 | CXTUResourceUsage usage = { (void*) 0, 0, 0 }; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5353 | return usage; |
| 5354 | } |
| 5355 | |
| 5356 | ASTUnit *astUnit = static_cast<ASTUnit*>(TU->TUData); |
| 5357 | llvm::OwningPtr<MemUsageEntries> entries(new MemUsageEntries()); |
| 5358 | ASTContext &astContext = astUnit->getASTContext(); |
| 5359 | |
| 5360 | // How much memory is used by AST nodes and types? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5361 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST, |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5362 | (unsigned long) astContext.getASTAllocatedMemory()); |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5363 | |
| 5364 | // How much memory is used by identifiers? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5365 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5366 | (unsigned long) astContext.Idents.getAllocator().getTotalMemory()); |
| 5367 | |
| 5368 | // How much memory is used for selectors? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5369 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5370 | (unsigned long) astContext.Selectors.getTotalMemory()); |
| 5371 | |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5372 | // How much memory is used by ASTContext's side tables? |
| 5373 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables, |
| 5374 | (unsigned long) astContext.getSideTableAllocatedMemory()); |
| 5375 | |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5376 | // How much memory is used for caching global code completion results? |
| 5377 | unsigned long completionBytes = 0; |
| 5378 | if (GlobalCodeCompletionAllocator *completionAllocator = |
| 5379 | astUnit->getCachedCompletionAllocator().getPtr()) { |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5380 | completionBytes = completionAllocator->getTotalMemory(); |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5381 | } |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 5382 | createCXTUResourceUsageEntry(*entries, |
| 5383 | CXTUResourceUsage_GlobalCompletionResults, |
| 5384 | completionBytes); |
| 5385 | |
| 5386 | // How much memory is being used by SourceManager's content cache? |
| 5387 | createCXTUResourceUsageEntry(*entries, |
| 5388 | CXTUResourceUsage_SourceManagerContentCache, |
| 5389 | (unsigned long) astContext.getSourceManager().getContentCacheSize()); |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5390 | |
| 5391 | // How much memory is being used by the MemoryBuffer's in SourceManager? |
| 5392 | const SourceManager::MemoryBufferSizes &srcBufs = |
| 5393 | astUnit->getSourceManager().getMemoryBufferSizes(); |
| 5394 | |
| 5395 | createCXTUResourceUsageEntry(*entries, |
| 5396 | CXTUResourceUsage_SourceManager_Membuffer_Malloc, |
| 5397 | (unsigned long) srcBufs.malloc_bytes); |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5398 | createCXTUResourceUsageEntry(*entries, |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5399 | CXTUResourceUsage_SourceManager_Membuffer_MMap, |
| 5400 | (unsigned long) srcBufs.mmap_bytes); |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5401 | createCXTUResourceUsageEntry(*entries, |
| 5402 | CXTUResourceUsage_SourceManager_DataStructures, |
| 5403 | (unsigned long) astContext.getSourceManager() |
| 5404 | .getDataStructureSizes()); |
Ted Kremenek | e9b5f3d | 2011-04-28 23:46:20 +0000 | [diff] [blame] | 5405 | |
| 5406 | // How much memory is being used by the ExternalASTSource? |
| 5407 | if (ExternalASTSource *esrc = astContext.getExternalSource()) { |
| 5408 | const ExternalASTSource::MemoryBufferSizes &sizes = |
| 5409 | esrc->getMemoryBufferSizes(); |
| 5410 | |
| 5411 | createCXTUResourceUsageEntry(*entries, |
| 5412 | CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc, |
| 5413 | (unsigned long) sizes.malloc_bytes); |
| 5414 | createCXTUResourceUsageEntry(*entries, |
| 5415 | CXTUResourceUsage_ExternalASTSource_Membuffer_MMap, |
| 5416 | (unsigned long) sizes.mmap_bytes); |
| 5417 | } |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5418 | |
| 5419 | // How much memory is being used by the Preprocessor? |
| 5420 | Preprocessor &pp = astUnit->getPreprocessor(); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5421 | createCXTUResourceUsageEntry(*entries, |
| 5422 | CXTUResourceUsage_Preprocessor, |
Argyrios Kyrtzidis | c5c5e92 | 2011-06-29 22:20:04 +0000 | [diff] [blame] | 5423 | pp.getTotalMemory()); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5424 | |
| 5425 | if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) { |
| 5426 | createCXTUResourceUsageEntry(*entries, |
| 5427 | CXTUResourceUsage_PreprocessingRecord, |
| 5428 | pRec->getTotalMemory()); |
| 5429 | } |
| 5430 | |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 5431 | createCXTUResourceUsageEntry(*entries, |
| 5432 | CXTUResourceUsage_Preprocessor_HeaderSearch, |
| 5433 | pp.getHeaderSearchInfo().getTotalMemory()); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5434 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5435 | CXTUResourceUsage usage = { (void*) entries.get(), |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5436 | (unsigned) entries->size(), |
| 5437 | entries->size() ? &(*entries)[0] : 0 }; |
| 5438 | entries.take(); |
| 5439 | return usage; |
| 5440 | } |
| 5441 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5442 | void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5443 | if (usage.data) |
| 5444 | delete (MemUsageEntries*) usage.data; |
| 5445 | } |
| 5446 | |
| 5447 | } // end extern "C" |
| 5448 | |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 5449 | void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) { |
| 5450 | CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU); |
| 5451 | for (unsigned I = 0; I != Usage.numEntries; ++I) |
| 5452 | fprintf(stderr, " %s: %lu\n", |
| 5453 | clang_getTUResourceUsageName(Usage.entries[I].kind), |
| 5454 | Usage.entries[I].amount); |
| 5455 | |
| 5456 | clang_disposeCXTUResourceUsage(Usage); |
| 5457 | } |
| 5458 | |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5459 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5460 | // Misc. utility functions. |
| 5461 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 5462 | |
Daniel Dunbar | abdce7a | 2010-11-05 17:21:46 +0000 | [diff] [blame] | 5463 | /// Default to using an 8 MB stack size on "safety" threads. |
| 5464 | static unsigned SafetyStackThreadSize = 8 << 20; |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 5465 | |
| 5466 | namespace clang { |
| 5467 | |
| 5468 | bool RunSafely(llvm::CrashRecoveryContext &CRC, |
Ted Kremenek | 6c53fdd | 2010-11-14 17:47:35 +0000 | [diff] [blame] | 5469 | void (*Fn)(void*), void *UserData, |
| 5470 | unsigned Size) { |
| 5471 | if (!Size) |
| 5472 | Size = GetSafetyThreadStackSize(); |
| 5473 | if (Size) |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 5474 | return CRC.RunSafelyOnThread(Fn, UserData, Size); |
| 5475 | return CRC.RunSafely(Fn, UserData); |
| 5476 | } |
| 5477 | |
| 5478 | unsigned GetSafetyThreadStackSize() { |
| 5479 | return SafetyStackThreadSize; |
| 5480 | } |
| 5481 | |
| 5482 | void SetSafetyThreadStackSize(unsigned Value) { |
| 5483 | SafetyStackThreadSize = Value; |
| 5484 | } |
| 5485 | |
| 5486 | } |
| 5487 | |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5488 | extern "C" { |
| 5489 | |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 5490 | CXString clang_getClangVersion() { |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 5491 | return createCXString(getClangFullVersion()); |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5492 | } |
| 5493 | |
| 5494 | } // end: extern "C" |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5495 | |