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" |
Argyrios Kyrtzidis | b2c60b0 | 2012-03-01 19:45:56 +0000 | [diff] [blame] | 38 | #include "llvm/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; |
Argyrios Kyrtzidis | e722ed6 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 54 | using namespace clang::cxindex; |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 55 | |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 56 | CXTranslationUnit cxtu::MakeCXTranslationUnit(CIndexer *CIdx, ASTUnit *TU) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 57 | if (!TU) |
| 58 | return 0; |
| 59 | CXTranslationUnit D = new CXTranslationUnitImpl(); |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 60 | D->CIdx = CIdx; |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 61 | D->TUData = TU; |
| 62 | D->StringPool = createCXStringPool(); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 63 | D->Diagnostics = 0; |
Ted Kremenek | bbf66ca | 2012-04-30 19:06:49 +0000 | [diff] [blame] | 64 | D->OverridenCursorsPool = createOverridenCXCursorsPool(); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 65 | return D; |
| 66 | } |
| 67 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 68 | cxtu::CXTUOwner::~CXTUOwner() { |
| 69 | if (TU) |
| 70 | clang_disposeTranslationUnit(TU); |
| 71 | } |
| 72 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 73 | /// \brief Compare two source ranges to determine their relative position in |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 74 | /// the translation unit. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 75 | static RangeComparisonResult RangeCompare(SourceManager &SM, |
| 76 | SourceRange R1, |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 77 | SourceRange R2) { |
| 78 | assert(R1.isValid() && "First range is invalid?"); |
| 79 | assert(R2.isValid() && "Second range is invalid?"); |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 80 | if (R1.getEnd() != R2.getBegin() && |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 81 | SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin())) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 82 | return RangeBefore; |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 83 | if (R2.getEnd() != R1.getBegin() && |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 84 | SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin())) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 85 | return RangeAfter; |
| 86 | return RangeOverlap; |
| 87 | } |
| 88 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 89 | /// \brief Determine if a source location falls within, before, or after a |
| 90 | /// a given source range. |
| 91 | static RangeComparisonResult LocationCompare(SourceManager &SM, |
| 92 | SourceLocation L, SourceRange R) { |
| 93 | assert(R.isValid() && "First range is invalid?"); |
| 94 | assert(L.isValid() && "Second range is invalid?"); |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 95 | if (L == R.getBegin() || L == R.getEnd()) |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 96 | return RangeOverlap; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 97 | if (SM.isBeforeInTranslationUnit(L, R.getBegin())) |
| 98 | return RangeBefore; |
| 99 | if (SM.isBeforeInTranslationUnit(R.getEnd(), L)) |
| 100 | return RangeAfter; |
| 101 | return RangeOverlap; |
| 102 | } |
| 103 | |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 104 | /// \brief Translate a Clang source range into a CIndex source range. |
| 105 | /// |
| 106 | /// Clang internally represents ranges where the end location points to the |
| 107 | /// start of the token at the end. However, for external clients it is more |
| 108 | /// useful to have a CXSourceRange be a proper half-open interval. This routine |
| 109 | /// does the appropriate translation. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 110 | CXSourceRange cxloc::translateSourceRange(const SourceManager &SM, |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 111 | const LangOptions &LangOpts, |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 112 | const CharSourceRange &R) { |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 113 | // 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] | 114 | // location accordingly. |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 115 | SourceLocation EndLoc = R.getEnd(); |
Argyrios Kyrtzidis | eba8cd5 | 2012-04-11 18:15:01 +0000 | [diff] [blame] | 116 | if (EndLoc.isValid() && EndLoc.isMacroID() && !SM.isMacroArgExpansion(EndLoc)) |
Chandler Carruth | edc3dcc | 2011-07-25 16:56:02 +0000 | [diff] [blame] | 117 | EndLoc = SM.getExpansionRange(EndLoc).second; |
Argyrios Kyrtzidis | eba8cd5 | 2012-04-11 18:15:01 +0000 | [diff] [blame] | 118 | if (R.isTokenRange() && !EndLoc.isInvalid()) { |
| 119 | unsigned Length = Lexer::MeasureTokenLength(SM.getSpellingLoc(EndLoc), |
| 120 | SM, LangOpts); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 121 | EndLoc = EndLoc.getLocWithOffset(Length); |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts }, |
| 125 | R.getBegin().getRawEncoding(), |
| 126 | EndLoc.getRawEncoding() }; |
| 127 | return Result; |
| 128 | } |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 130 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 131 | // Cursor visitor. |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 132 | //===----------------------------------------------------------------------===// |
| 133 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 134 | static SourceRange getRawCursorExtent(CXCursor C); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 135 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr); |
| 136 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 137 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 138 | RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 139 | return RangeCompare(AU->getSourceManager(), R, RegionOfInterest); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 142 | /// \brief Visit the given cursor and, if requested by the visitor, |
| 143 | /// its children. |
| 144 | /// |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 145 | /// \param Cursor the cursor to visit. |
| 146 | /// |
| 147 | /// \param CheckRegionOfInterest if true, then the caller already checked that |
| 148 | /// this cursor is within the region of interest. |
| 149 | /// |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 150 | /// \returns true if the visitation should be aborted, false if it |
| 151 | /// should continue. |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 152 | bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 153 | if (clang_isInvalid(Cursor.kind)) |
| 154 | return false; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 155 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 156 | if (clang_isDeclaration(Cursor.kind)) { |
| 157 | Decl *D = getCursorDecl(Cursor); |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 158 | if (!D) { |
| 159 | assert(0 && "Invalid declaration cursor"); |
| 160 | return true; // abort. |
| 161 | } |
| 162 | |
Argyrios Kyrtzidis | 65ab907 | 2011-09-26 19:05:37 +0000 | [diff] [blame] | 163 | // Ignore implicit declarations, unless it's an objc method because |
| 164 | // currently we should report implicit methods for properties when indexing. |
| 165 | if (D->isImplicit() && !isa<ObjCMethodDecl>(D)) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 166 | return false; |
| 167 | } |
| 168 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 169 | // If we have a range of interest, and this cursor doesn't intersect with it, |
| 170 | // we're done. |
| 171 | if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 172 | SourceRange Range = getRawCursorExtent(Cursor); |
Daniel Dunbar | f408f32 | 2010-02-14 08:32:05 +0000 | [diff] [blame] | 173 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 174 | return false; |
| 175 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 176 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 177 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 178 | case CXChildVisit_Break: |
| 179 | return true; |
| 180 | |
| 181 | case CXChildVisit_Continue: |
| 182 | return false; |
| 183 | |
| 184 | case CXChildVisit_Recurse: |
| 185 | return VisitChildren(Cursor); |
| 186 | } |
| 187 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 188 | llvm_unreachable("Invalid CXChildVisitResult!"); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 191 | static bool visitPreprocessedEntitiesInRange(SourceRange R, |
| 192 | PreprocessingRecord &PPRec, |
| 193 | CursorVisitor &Visitor) { |
| 194 | SourceManager &SM = Visitor.getASTUnit()->getSourceManager(); |
| 195 | FileID FID; |
| 196 | |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 197 | if (!Visitor.shouldVisitIncludedEntities()) { |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 198 | // If the begin/end of the range lie in the same FileID, do the optimization |
| 199 | // where we skip preprocessed entities that do not come from the same FileID. |
Argyrios Kyrtzidis | acca411 | 2011-12-21 16:56:38 +0000 | [diff] [blame] | 200 | FID = SM.getFileID(SM.getFileLoc(R.getBegin())); |
| 201 | if (FID != SM.getFileID(SM.getFileLoc(R.getEnd()))) |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 202 | FID = FileID(); |
| 203 | } |
| 204 | |
| 205 | std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> |
| 206 | Entities = PPRec.getPreprocessedEntitiesInRange(R); |
| 207 | return Visitor.visitPreprocessedEntities(Entities.first, Entities.second, |
| 208 | PPRec, FID); |
| 209 | } |
| 210 | |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 211 | void CursorVisitor::visitFileRegion() { |
| 212 | if (RegionOfInterest.isInvalid()) |
| 213 | return; |
| 214 | |
| 215 | ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData); |
| 216 | SourceManager &SM = Unit->getSourceManager(); |
| 217 | |
| 218 | std::pair<FileID, unsigned> |
| 219 | Begin = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getBegin())), |
| 220 | End = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getEnd())); |
| 221 | |
| 222 | if (End.first != Begin.first) { |
| 223 | // If the end does not reside in the same file, try to recover by |
| 224 | // picking the end of the file of begin location. |
| 225 | End.first = Begin.first; |
| 226 | End.second = SM.getFileIDSize(Begin.first); |
| 227 | } |
| 228 | |
| 229 | assert(Begin.first == End.first); |
| 230 | if (Begin.second > End.second) |
| 231 | return; |
| 232 | |
| 233 | FileID File = Begin.first; |
| 234 | unsigned Offset = Begin.second; |
| 235 | unsigned Length = End.second - Begin.second; |
| 236 | |
Argyrios Kyrtzidis | b49e728 | 2011-11-29 03:14:11 +0000 | [diff] [blame] | 237 | if (!VisitDeclsOnly && !VisitPreprocessorLast) |
| 238 | if (visitPreprocessedEntitiesInRegion()) |
| 239 | return; // visitation break. |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 240 | |
| 241 | visitDeclsFromFileRegion(File, Offset, Length); |
| 242 | |
Argyrios Kyrtzidis | b49e728 | 2011-11-29 03:14:11 +0000 | [diff] [blame] | 243 | if (!VisitDeclsOnly && VisitPreprocessorLast) |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 244 | visitPreprocessedEntitiesInRegion(); |
| 245 | } |
| 246 | |
Argyrios Kyrtzidis | e2079cf | 2011-11-16 08:58:54 +0000 | [diff] [blame] | 247 | static bool isInLexicalContext(Decl *D, DeclContext *DC) { |
| 248 | if (!DC) |
| 249 | return false; |
| 250 | |
| 251 | for (DeclContext *DeclDC = D->getLexicalDeclContext(); |
| 252 | DeclDC; DeclDC = DeclDC->getLexicalParent()) { |
| 253 | if (DeclDC == DC) |
| 254 | return true; |
| 255 | } |
| 256 | return false; |
| 257 | } |
| 258 | |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 259 | void CursorVisitor::visitDeclsFromFileRegion(FileID File, |
| 260 | unsigned Offset, unsigned Length) { |
| 261 | ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData); |
| 262 | SourceManager &SM = Unit->getSourceManager(); |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 263 | SourceRange Range = RegionOfInterest; |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 264 | |
| 265 | SmallVector<Decl *, 16> Decls; |
| 266 | Unit->findFileRegionDecls(File, Offset, Length, Decls); |
| 267 | |
| 268 | // If we didn't find any file level decls for the file, try looking at the |
| 269 | // file that it was included from. |
Argyrios Kyrtzidis | c14a03d | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 270 | while (Decls.empty() || Decls.front()->isTopLevelDeclInObjCContainer()) { |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 271 | bool Invalid = false; |
| 272 | const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid); |
| 273 | if (Invalid) |
| 274 | return; |
| 275 | |
| 276 | SourceLocation Outer; |
| 277 | if (SLEntry.isFile()) |
| 278 | Outer = SLEntry.getFile().getIncludeLoc(); |
| 279 | else |
| 280 | Outer = SLEntry.getExpansion().getExpansionLocStart(); |
| 281 | if (Outer.isInvalid()) |
| 282 | return; |
| 283 | |
| 284 | llvm::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer); |
| 285 | Length = 0; |
| 286 | Unit->findFileRegionDecls(File, Offset, Length, Decls); |
| 287 | } |
| 288 | |
| 289 | assert(!Decls.empty()); |
| 290 | |
| 291 | bool VisitedAtLeastOnce = false; |
Argyrios Kyrtzidis | e2079cf | 2011-11-16 08:58:54 +0000 | [diff] [blame] | 292 | DeclContext *CurDC = 0; |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 293 | SmallVector<Decl *, 16>::iterator DIt = Decls.begin(); |
| 294 | for (SmallVector<Decl *, 16>::iterator DE = Decls.end(); DIt != DE; ++DIt) { |
| 295 | Decl *D = *DIt; |
Argyrios Kyrtzidis | ed8bef4 | 2011-11-28 22:38:07 +0000 | [diff] [blame] | 296 | if (D->getSourceRange().isInvalid()) |
| 297 | continue; |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 298 | |
Argyrios Kyrtzidis | e2079cf | 2011-11-16 08:58:54 +0000 | [diff] [blame] | 299 | if (isInLexicalContext(D, CurDC)) |
| 300 | continue; |
| 301 | |
| 302 | CurDC = dyn_cast<DeclContext>(D); |
| 303 | |
Argyrios Kyrtzidis | e2079cf | 2011-11-16 08:58:54 +0000 | [diff] [blame] | 304 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
| 305 | if (!TD->isFreeStanding()) |
| 306 | continue; |
| 307 | |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 308 | RangeComparisonResult CompRes = RangeCompare(SM, D->getSourceRange(),Range); |
| 309 | if (CompRes == RangeBefore) |
| 310 | continue; |
| 311 | if (CompRes == RangeAfter) |
| 312 | break; |
| 313 | |
| 314 | assert(CompRes == RangeOverlap); |
| 315 | VisitedAtLeastOnce = true; |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 316 | |
| 317 | if (isa<ObjCContainerDecl>(D)) { |
| 318 | FileDI_current = &DIt; |
| 319 | FileDE_current = DE; |
| 320 | } else { |
| 321 | FileDI_current = 0; |
| 322 | } |
| 323 | |
Argyrios Kyrtzidis | ba98617 | 2011-11-03 19:02:28 +0000 | [diff] [blame] | 324 | if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true)) |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 325 | break; |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | if (VisitedAtLeastOnce) |
| 329 | return; |
| 330 | |
| 331 | // No Decls overlapped with the range. Move up the lexical context until there |
| 332 | // is a context that contains the range or we reach the translation unit |
| 333 | // level. |
| 334 | DeclContext *DC = DIt == Decls.begin() ? (*DIt)->getLexicalDeclContext() |
| 335 | : (*(DIt-1))->getLexicalDeclContext(); |
| 336 | |
| 337 | while (DC && !DC->isTranslationUnit()) { |
| 338 | Decl *D = cast<Decl>(DC); |
| 339 | SourceRange CurDeclRange = D->getSourceRange(); |
| 340 | if (CurDeclRange.isInvalid()) |
| 341 | break; |
| 342 | |
| 343 | if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) { |
Argyrios Kyrtzidis | ba98617 | 2011-11-03 19:02:28 +0000 | [diff] [blame] | 344 | Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true); |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 345 | break; |
| 346 | } |
| 347 | |
| 348 | DC = D->getLexicalDeclContext(); |
| 349 | } |
| 350 | } |
| 351 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 352 | bool CursorVisitor::visitPreprocessedEntitiesInRegion() { |
Argyrios Kyrtzidis | b49e728 | 2011-11-29 03:14:11 +0000 | [diff] [blame] | 353 | if (!AU->getPreprocessor().getPreprocessingRecord()) |
| 354 | return false; |
| 355 | |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 356 | PreprocessingRecord &PPRec |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 357 | = *AU->getPreprocessor().getPreprocessingRecord(); |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 358 | SourceManager &SM = AU->getSourceManager(); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 359 | |
Argyrios Kyrtzidis | 92ddef1 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 360 | if (RegionOfInterest.isValid()) { |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 361 | SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest); |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 362 | SourceLocation B = MappedRange.getBegin(); |
| 363 | SourceLocation E = MappedRange.getEnd(); |
| 364 | |
| 365 | if (AU->isInPreambleFileID(B)) { |
| 366 | if (SM.isLoadedSourceLocation(E)) |
| 367 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), |
| 368 | PPRec, *this); |
| 369 | |
| 370 | // Beginning of range lies in the preamble but it also extends beyond |
| 371 | // it into the main file. Split the range into 2 parts, one covering |
| 372 | // the preamble and another covering the main file. This allows subsequent |
| 373 | // calls to visitPreprocessedEntitiesInRange to accept a source range that |
| 374 | // lies in the same FileID, allowing it to skip preprocessed entities that |
| 375 | // do not come from the same FileID. |
| 376 | bool breaked = |
| 377 | visitPreprocessedEntitiesInRange( |
| 378 | SourceRange(B, AU->getEndOfPreambleFileID()), |
| 379 | PPRec, *this); |
| 380 | if (breaked) return true; |
| 381 | return visitPreprocessedEntitiesInRange( |
| 382 | SourceRange(AU->getStartOfMainFileID(), E), |
| 383 | PPRec, *this); |
| 384 | } |
| 385 | |
| 386 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this); |
Argyrios Kyrtzidis | 92ddef1 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 389 | bool OnlyLocalDecls |
Douglas Gregor | 32038bb | 2010-12-21 19:07:48 +0000 | [diff] [blame] | 390 | = !AU->isMainFileAST() && AU->getOnlyLocalDecls(); |
| 391 | |
Argyrios Kyrtzidis | 92ddef1 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 392 | if (OnlyLocalDecls) |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 393 | return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(), |
| 394 | PPRec); |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 395 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 396 | return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec); |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | template<typename InputIterator> |
| 400 | bool CursorVisitor::visitPreprocessedEntities(InputIterator First, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 401 | InputIterator Last, |
| 402 | PreprocessingRecord &PPRec, |
| 403 | FileID FID) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 404 | for (; First != Last; ++First) { |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 405 | if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID)) |
| 406 | continue; |
| 407 | |
| 408 | PreprocessedEntity *PPE = *First; |
| 409 | if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 410 | if (Visit(MakeMacroExpansionCursor(ME, TU))) |
| 411 | return true; |
| 412 | |
| 413 | continue; |
| 414 | } |
| 415 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 416 | if (MacroDefinition *MD = dyn_cast<MacroDefinition>(PPE)) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 417 | if (Visit(MakeMacroDefinitionCursor(MD, TU))) |
| 418 | return true; |
| 419 | |
| 420 | continue; |
| 421 | } |
| 422 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 423 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 424 | if (Visit(MakeInclusionDirectiveCursor(ID, TU))) |
| 425 | return true; |
| 426 | |
| 427 | continue; |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 431 | return false; |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 434 | /// \brief Visit the children of the given cursor. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 435 | /// |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 436 | /// \returns true if the visitation should be aborted, false if it |
| 437 | /// should continue. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 438 | bool CursorVisitor::VisitChildren(CXCursor Cursor) { |
Douglas Gregor | c314aa4 | 2011-03-02 19:17:03 +0000 | [diff] [blame] | 439 | if (clang_isReference(Cursor.kind) && |
| 440 | Cursor.kind != CXCursor_CXXBaseSpecifier) { |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 441 | // By definition, references have no children. |
| 442 | return false; |
| 443 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 444 | |
| 445 | // 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] | 446 | // done. |
Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 447 | SetParentRAII SetParent(Parent, StmtParent, Cursor); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 448 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 449 | if (clang_isDeclaration(Cursor.kind)) { |
| 450 | Decl *D = getCursorDecl(Cursor); |
Douglas Gregor | 06d9b1a | 2011-04-14 21:41:34 +0000 | [diff] [blame] | 451 | if (!D) |
| 452 | return false; |
| 453 | |
Ted Kremenek | 539311e | 2010-02-18 18:47:01 +0000 | [diff] [blame] | 454 | return VisitAttributes(D) || Visit(D); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 455 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 456 | |
Douglas Gregor | 06d9b1a | 2011-04-14 21:41:34 +0000 | [diff] [blame] | 457 | if (clang_isStatement(Cursor.kind)) { |
| 458 | if (Stmt *S = getCursorStmt(Cursor)) |
| 459 | return Visit(S); |
| 460 | |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | if (clang_isExpression(Cursor.kind)) { |
| 465 | if (Expr *E = getCursorExpr(Cursor)) |
| 466 | return Visit(E); |
| 467 | |
| 468 | return false; |
| 469 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 470 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 471 | if (clang_isTranslationUnit(Cursor.kind)) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 472 | CXTranslationUnit tu = getCursorTU(Cursor); |
| 473 | ASTUnit *CXXUnit = static_cast<ASTUnit*>(tu->TUData); |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 474 | |
| 475 | int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast }; |
| 476 | for (unsigned I = 0; I != 2; ++I) { |
| 477 | if (VisitOrder[I]) { |
| 478 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && |
| 479 | RegionOfInterest.isInvalid()) { |
| 480 | for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(), |
| 481 | TLEnd = CXXUnit->top_level_end(); |
| 482 | TL != TLEnd; ++TL) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 483 | if (Visit(MakeCXCursor(*TL, tu, RegionOfInterest), true)) |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 484 | return true; |
| 485 | } |
| 486 | } else if (VisitDeclContext( |
| 487 | CXXUnit->getASTContext().getTranslationUnitDecl())) |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 488 | return true; |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 489 | continue; |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 490 | } |
Bob Wilson | 3178cb6 | 2010-03-19 03:57:57 +0000 | [diff] [blame] | 491 | |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 492 | // Walk the preprocessing record. |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 493 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) |
| 494 | visitPreprocessedEntitiesInRegion(); |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 495 | } |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 496 | |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 497 | return false; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 498 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 499 | |
Douglas Gregor | c314aa4 | 2011-03-02 19:17:03 +0000 | [diff] [blame] | 500 | if (Cursor.kind == CXCursor_CXXBaseSpecifier) { |
| 501 | if (CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) { |
| 502 | if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) { |
| 503 | return Visit(BaseTSInfo->getTypeLoc()); |
| 504 | } |
| 505 | } |
| 506 | } |
Argyrios Kyrtzidis | 221d5a5 | 2011-09-13 18:49:56 +0000 | [diff] [blame] | 507 | |
| 508 | if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { |
| 509 | IBOutletCollectionAttr *A = |
| 510 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor)); |
| 511 | if (const ObjCInterfaceType *InterT = A->getInterface()->getAs<ObjCInterfaceType>()) |
| 512 | return Visit(cxcursor::MakeCursorObjCClassRef(InterT->getInterface(), |
| 513 | A->getInterfaceLoc(), TU)); |
| 514 | } |
| 515 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 516 | // Nothing to visit at the moment. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 517 | return false; |
| 518 | } |
| 519 | |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 520 | bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { |
Douglas Gregor | 13c8ccb | 2011-04-22 23:49:24 +0000 | [diff] [blame] | 521 | if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten()) |
| 522 | if (Visit(TSInfo->getTypeLoc())) |
| 523 | return true; |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 524 | |
Ted Kremenek | 664cffd | 2010-07-22 11:30:19 +0000 | [diff] [blame] | 525 | if (Stmt *Body = B->getBody()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 526 | return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest)); |
Ted Kremenek | 664cffd | 2010-07-22 11:30:19 +0000 | [diff] [blame] | 527 | |
| 528 | return false; |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 531 | llvm::Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) { |
| 532 | if (RegionOfInterest.isValid()) { |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 533 | SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager()); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 534 | if (Range.isInvalid()) |
| 535 | return llvm::Optional<bool>(); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 536 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 537 | switch (CompareRegionOfInterest(Range)) { |
| 538 | case RangeBefore: |
| 539 | // This declaration comes before the region of interest; skip it. |
| 540 | return llvm::Optional<bool>(); |
| 541 | |
| 542 | case RangeAfter: |
| 543 | // This declaration comes after the region of interest; we're done. |
| 544 | return false; |
| 545 | |
| 546 | case RangeOverlap: |
| 547 | // This declaration overlaps the region of interest; visit it. |
| 548 | break; |
| 549 | } |
| 550 | } |
| 551 | return true; |
| 552 | } |
| 553 | |
| 554 | bool CursorVisitor::VisitDeclContext(DeclContext *DC) { |
| 555 | DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 556 | |
| 557 | // FIXME: Eventually remove. This part of a hack to support proper |
| 558 | // iteration over all Decls contained lexically within an ObjC container. |
| 559 | SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I); |
| 560 | SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E); |
| 561 | |
| 562 | for ( ; I != E; ++I) { |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 563 | Decl *D = *I; |
| 564 | if (D->getLexicalDeclContext() != DC) |
| 565 | continue; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 566 | CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest); |
Argyrios Kyrtzidis | 1836db0 | 2012-02-04 01:04:58 +0000 | [diff] [blame] | 567 | |
| 568 | // FIXME: ObjCClassRef/ObjCProtocolRef for forward class/protocol |
| 569 | // declarations is a mismatch with the compiler semantics. |
| 570 | if (Cursor.kind == CXCursor_ObjCInterfaceDecl) { |
| 571 | ObjCInterfaceDecl *ID = cast<ObjCInterfaceDecl>(D); |
| 572 | if (!ID->isThisDeclarationADefinition()) |
| 573 | Cursor = MakeCursorObjCClassRef(ID, ID->getLocation(), TU); |
| 574 | |
| 575 | } else if (Cursor.kind == CXCursor_ObjCProtocolDecl) { |
| 576 | ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D); |
| 577 | if (!PD->isThisDeclarationADefinition()) |
| 578 | Cursor = MakeCursorObjCProtocolRef(PD, PD->getLocation(), TU); |
| 579 | } |
| 580 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 581 | const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); |
| 582 | if (!V.hasValue()) |
| 583 | continue; |
| 584 | if (!V.getValue()) |
| 585 | return false; |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 586 | if (Visit(Cursor, true)) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 587 | return true; |
| 588 | } |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 589 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 592 | bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 593 | llvm_unreachable("Translation units are visited directly by Visit()"); |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 596 | bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 597 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 598 | return Visit(TSInfo->getTypeLoc()); |
| 599 | |
| 600 | return false; |
| 601 | } |
| 602 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 603 | bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { |
| 604 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 605 | return Visit(TSInfo->getTypeLoc()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 606 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 607 | return false; |
| 608 | } |
| 609 | |
| 610 | bool CursorVisitor::VisitTagDecl(TagDecl *D) { |
| 611 | return VisitDeclContext(D); |
| 612 | } |
| 613 | |
Douglas Gregor | 0ab1e9f | 2010-09-01 17:32:36 +0000 | [diff] [blame] | 614 | bool CursorVisitor::VisitClassTemplateSpecializationDecl( |
| 615 | ClassTemplateSpecializationDecl *D) { |
| 616 | bool ShouldVisitBody = false; |
| 617 | switch (D->getSpecializationKind()) { |
| 618 | case TSK_Undeclared: |
| 619 | case TSK_ImplicitInstantiation: |
| 620 | // Nothing to visit |
| 621 | return false; |
| 622 | |
| 623 | case TSK_ExplicitInstantiationDeclaration: |
| 624 | case TSK_ExplicitInstantiationDefinition: |
| 625 | break; |
| 626 | |
| 627 | case TSK_ExplicitSpecialization: |
| 628 | ShouldVisitBody = true; |
| 629 | break; |
| 630 | } |
| 631 | |
| 632 | // Visit the template arguments used in the specialization. |
| 633 | if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { |
| 634 | TypeLoc TL = SpecType->getTypeLoc(); |
| 635 | if (TemplateSpecializationTypeLoc *TSTLoc |
| 636 | = dyn_cast<TemplateSpecializationTypeLoc>(&TL)) { |
| 637 | for (unsigned I = 0, N = TSTLoc->getNumArgs(); I != N; ++I) |
| 638 | if (VisitTemplateArgumentLoc(TSTLoc->getArgLoc(I))) |
| 639 | return true; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | if (ShouldVisitBody && VisitCXXRecordDecl(D)) |
| 644 | return true; |
| 645 | |
| 646 | return false; |
| 647 | } |
| 648 | |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 649 | bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( |
| 650 | ClassTemplatePartialSpecializationDecl *D) { |
| 651 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 652 | // before visiting these template parameters. |
| 653 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 654 | return true; |
| 655 | |
| 656 | // Visit the partial specialization arguments. |
| 657 | const TemplateArgumentLoc *TemplateArgs = D->getTemplateArgsAsWritten(); |
| 658 | for (unsigned I = 0, N = D->getNumTemplateArgsAsWritten(); I != N; ++I) |
| 659 | if (VisitTemplateArgumentLoc(TemplateArgs[I])) |
| 660 | return true; |
| 661 | |
| 662 | return VisitCXXRecordDecl(D); |
| 663 | } |
| 664 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 665 | bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 666 | // Visit the default argument. |
| 667 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 668 | if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) |
| 669 | if (Visit(DefArg->getTypeLoc())) |
| 670 | return true; |
| 671 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 672 | return false; |
| 673 | } |
| 674 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 675 | bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 676 | if (Expr *Init = D->getInitExpr()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 677 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 678 | return false; |
| 679 | } |
| 680 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 681 | bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { |
| 682 | if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) |
| 683 | if (Visit(TSInfo->getTypeLoc())) |
| 684 | return true; |
| 685 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 686 | // Visit the nested-name-specifier, if present. |
| 687 | if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc()) |
| 688 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 689 | return true; |
| 690 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 691 | return false; |
| 692 | } |
| 693 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 694 | /// \brief Compare two base or member initializers based on their source order. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 695 | static int CompareCXXCtorInitializers(const void* Xp, const void *Yp) { |
| 696 | CXXCtorInitializer const * const *X |
| 697 | = static_cast<CXXCtorInitializer const * const *>(Xp); |
| 698 | CXXCtorInitializer const * const *Y |
| 699 | = static_cast<CXXCtorInitializer const * const *>(Yp); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 700 | |
| 701 | if ((*X)->getSourceOrder() < (*Y)->getSourceOrder()) |
| 702 | return -1; |
| 703 | else if ((*X)->getSourceOrder() > (*Y)->getSourceOrder()) |
| 704 | return 1; |
| 705 | else |
| 706 | return 0; |
| 707 | } |
| 708 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 709 | bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 710 | if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { |
| 711 | // Visit the function declaration's syntactic components in the order |
| 712 | // written. This requires a bit of work. |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 713 | TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 714 | FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL); |
| 715 | |
| 716 | // If we have a function declared directly (without the use of a typedef), |
| 717 | // visit just the return type. Otherwise, just visit the function's type |
| 718 | // now. |
| 719 | if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL->getResultLoc())) || |
| 720 | (!FTL && Visit(TL))) |
| 721 | return true; |
| 722 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 723 | // Visit the nested-name-specifier, if present. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 724 | if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc()) |
| 725 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 726 | return true; |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 727 | |
| 728 | // Visit the declaration name. |
| 729 | if (VisitDeclarationNameInfo(ND->getNameInfo())) |
| 730 | return true; |
| 731 | |
| 732 | // FIXME: Visit explicitly-specified template arguments! |
| 733 | |
| 734 | // Visit the function parameters, if we have a function type. |
| 735 | if (FTL && VisitFunctionTypeLoc(*FTL, true)) |
| 736 | return true; |
| 737 | |
| 738 | // FIXME: Attributes? |
| 739 | } |
| 740 | |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 741 | if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) { |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 742 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) { |
| 743 | // Find the initializers that were written in the source. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 744 | SmallVector<CXXCtorInitializer *, 4> WrittenInits; |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 745 | for (CXXConstructorDecl::init_iterator I = Constructor->init_begin(), |
| 746 | IEnd = Constructor->init_end(); |
| 747 | I != IEnd; ++I) { |
| 748 | if (!(*I)->isWritten()) |
| 749 | continue; |
| 750 | |
| 751 | WrittenInits.push_back(*I); |
| 752 | } |
| 753 | |
| 754 | // Sort the initializers in source order |
| 755 | llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 756 | &CompareCXXCtorInitializers); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 757 | |
| 758 | // Visit the initializers in source order |
| 759 | for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 760 | CXXCtorInitializer *Init = WrittenInits[I]; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 761 | if (Init->isAnyMemberInitializer()) { |
| 762 | if (Visit(MakeCursorMemberRef(Init->getAnyMember(), |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 763 | Init->getMemberLocation(), TU))) |
| 764 | return true; |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 765 | } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) { |
| 766 | if (Visit(TInfo->getTypeLoc())) |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 767 | return true; |
| 768 | } |
| 769 | |
| 770 | // Visit the initializer value. |
| 771 | if (Expr *Initializer = Init->getInit()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 772 | if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest))) |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 773 | return true; |
| 774 | } |
| 775 | } |
| 776 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 777 | if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 778 | return true; |
| 779 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 780 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 781 | return false; |
| 782 | } |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 783 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 784 | bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { |
| 785 | if (VisitDeclaratorDecl(D)) |
| 786 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 787 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 788 | if (Expr *BitWidth = D->getBitWidth()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 789 | return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 790 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 791 | return false; |
| 792 | } |
| 793 | |
| 794 | bool CursorVisitor::VisitVarDecl(VarDecl *D) { |
| 795 | if (VisitDeclaratorDecl(D)) |
| 796 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 797 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 798 | if (Expr *Init = D->getInit()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 799 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 800 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 801 | return false; |
| 802 | } |
| 803 | |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 804 | bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 805 | if (VisitDeclaratorDecl(D)) |
| 806 | return true; |
| 807 | |
| 808 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 809 | if (Expr *DefArg = D->getDefaultArgument()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 810 | return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 811 | |
| 812 | return false; |
| 813 | } |
| 814 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 815 | bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 816 | // FIXME: Visit the "outer" template parameter lists on the FunctionDecl |
| 817 | // before visiting these template parameters. |
| 818 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 819 | return true; |
| 820 | |
| 821 | return VisitFunctionDecl(D->getTemplatedDecl()); |
| 822 | } |
| 823 | |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 824 | bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 825 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 826 | // before visiting these template parameters. |
| 827 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 828 | return true; |
| 829 | |
| 830 | return VisitCXXRecordDecl(D->getTemplatedDecl()); |
| 831 | } |
| 832 | |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 833 | bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 834 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 835 | return true; |
| 836 | |
| 837 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && |
| 838 | VisitTemplateArgumentLoc(D->getDefaultArgument())) |
| 839 | return true; |
| 840 | |
| 841 | return false; |
| 842 | } |
| 843 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 844 | bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 845 | if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo()) |
| 846 | if (Visit(TSInfo->getTypeLoc())) |
| 847 | return true; |
| 848 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 849 | for (ObjCMethodDecl::param_iterator P = ND->param_begin(), |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 850 | PEnd = ND->param_end(); |
| 851 | P != PEnd; ++P) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 852 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 853 | return true; |
| 854 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 855 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 856 | if (ND->isThisDeclarationADefinition() && |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 857 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 858 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 859 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 860 | return false; |
| 861 | } |
| 862 | |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 863 | template <typename DeclIt> |
| 864 | static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current, |
| 865 | SourceManager &SM, SourceLocation EndLoc, |
| 866 | SmallVectorImpl<Decl *> &Decls) { |
| 867 | DeclIt next = *DI_current; |
| 868 | while (++next != DE_current) { |
| 869 | Decl *D_next = *next; |
| 870 | if (!D_next) |
| 871 | break; |
| 872 | SourceLocation L = D_next->getLocStart(); |
| 873 | if (!L.isValid()) |
| 874 | break; |
| 875 | if (SM.isBeforeInTranslationUnit(L, EndLoc)) { |
| 876 | *DI_current = next; |
| 877 | Decls.push_back(D_next); |
| 878 | continue; |
| 879 | } |
| 880 | break; |
| 881 | } |
| 882 | } |
| 883 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 884 | namespace { |
| 885 | struct ContainerDeclsSort { |
| 886 | SourceManager &SM; |
| 887 | ContainerDeclsSort(SourceManager &sm) : SM(sm) {} |
| 888 | bool operator()(Decl *A, Decl *B) { |
| 889 | SourceLocation L_A = A->getLocStart(); |
| 890 | SourceLocation L_B = B->getLocStart(); |
| 891 | assert(L_A.isValid() && L_B.isValid()); |
| 892 | return SM.isBeforeInTranslationUnit(L_A, L_B); |
| 893 | } |
| 894 | }; |
| 895 | } |
| 896 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 897 | bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 898 | // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially |
| 899 | // an @implementation can lexically contain Decls that are not properly |
| 900 | // nested in the AST. When we identify such cases, we need to retrofit |
| 901 | // this nesting here. |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 902 | if (!DI_current && !FileDI_current) |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 903 | return VisitDeclContext(D); |
| 904 | |
| 905 | // Scan the Decls that immediately come after the container |
| 906 | // in the current DeclContext. If any fall within the |
| 907 | // container's lexical region, stash them into a vector |
| 908 | // for later processing. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 909 | SmallVector<Decl *, 24> DeclsInContainer; |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 910 | SourceLocation EndLoc = D->getSourceRange().getEnd(); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 911 | SourceManager &SM = AU->getSourceManager(); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 912 | if (EndLoc.isValid()) { |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 913 | if (DI_current) { |
| 914 | addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc, |
| 915 | DeclsInContainer); |
| 916 | } else { |
| 917 | addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc, |
| 918 | DeclsInContainer); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 919 | } |
| 920 | } |
| 921 | |
| 922 | // The common case. |
| 923 | if (DeclsInContainer.empty()) |
| 924 | return VisitDeclContext(D); |
| 925 | |
| 926 | // Get all the Decls in the DeclContext, and sort them with the |
| 927 | // additional ones we've collected. Then visit them. |
| 928 | for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end(); |
| 929 | I!=E; ++I) { |
| 930 | Decl *subDecl = *I; |
Ted Kremenek | 0582c89 | 2010-11-02 23:17:51 +0000 | [diff] [blame] | 931 | if (!subDecl || subDecl->getLexicalDeclContext() != D || |
| 932 | subDecl->getLocStart().isInvalid()) |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 933 | continue; |
| 934 | DeclsInContainer.push_back(subDecl); |
| 935 | } |
| 936 | |
| 937 | // Now sort the Decls so that they appear in lexical order. |
| 938 | std::sort(DeclsInContainer.begin(), DeclsInContainer.end(), |
| 939 | ContainerDeclsSort(SM)); |
| 940 | |
| 941 | // Now visit the decls. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 942 | for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(), |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 943 | E = DeclsInContainer.end(); I != E; ++I) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 944 | CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 945 | const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); |
| 946 | if (!V.hasValue()) |
| 947 | continue; |
| 948 | if (!V.getValue()) |
| 949 | return false; |
| 950 | if (Visit(Cursor, true)) |
| 951 | return true; |
| 952 | } |
| 953 | return false; |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 956 | bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 957 | if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), |
| 958 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 959 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 960 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 961 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 962 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 963 | E = ND->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 964 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, 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 | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 967 | return VisitObjCContainerDecl(ND); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 970 | bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 971 | if (!PID->isThisDeclarationADefinition()) |
| 972 | return Visit(MakeCursorObjCProtocolRef(PID, PID->getLocation(), TU)); |
| 973 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 974 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
| 975 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 976 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 977 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 978 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 979 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 980 | return VisitObjCContainerDecl(PID); |
| 981 | } |
| 982 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 983 | bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 984 | if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc())) |
John McCall | fc92920 | 2010-06-04 22:33:30 +0000 | [diff] [blame] | 985 | return true; |
| 986 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 987 | // FIXME: This implements a workaround with @property declarations also being |
| 988 | // installed in the DeclContext for the @interface. Eventually this code |
| 989 | // should be removed. |
| 990 | ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); |
| 991 | if (!CDecl || !CDecl->IsClassExtension()) |
| 992 | return false; |
| 993 | |
| 994 | ObjCInterfaceDecl *ID = CDecl->getClassInterface(); |
| 995 | if (!ID) |
| 996 | return false; |
| 997 | |
| 998 | IdentifierInfo *PropertyId = PD->getIdentifier(); |
| 999 | ObjCPropertyDecl *prevDecl = |
| 1000 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId); |
| 1001 | |
| 1002 | if (!prevDecl) |
| 1003 | return false; |
| 1004 | |
| 1005 | // Visit synthesized methods since they will be skipped when visiting |
| 1006 | // the @interface. |
| 1007 | if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1008 | if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1009 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1010 | return true; |
| 1011 | |
| 1012 | if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1013 | if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1014 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1015 | return true; |
| 1016 | |
| 1017 | return false; |
| 1018 | } |
| 1019 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1020 | bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 1021 | if (!D->isThisDeclarationADefinition()) { |
| 1022 | // Forward declaration is treated like a reference. |
| 1023 | return Visit(MakeCursorObjCClassRef(D, D->getLocation(), TU)); |
| 1024 | } |
| 1025 | |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1026 | // Issue callbacks for super class. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1027 | if (D->getSuperClass() && |
| 1028 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1029 | D->getSuperClassLoc(), |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1030 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1031 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1032 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 1033 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1034 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 1035 | E = D->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1036 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1037 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1038 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1039 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1042 | bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 1043 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1046 | bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 1047 | // 'ID' could be null when dealing with invalid code. |
| 1048 | if (ObjCInterfaceDecl *ID = D->getClassInterface()) |
| 1049 | if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) |
| 1050 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1051 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1052 | return VisitObjCImplDecl(D); |
| 1053 | } |
| 1054 | |
| 1055 | bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 1056 | #if 0 |
| 1057 | // Issue callbacks for super class. |
| 1058 | // FIXME: No source location information! |
| 1059 | if (D->getSuperClass() && |
| 1060 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1061 | D->getSuperClassLoc(), |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1062 | TU))) |
| 1063 | return true; |
| 1064 | #endif |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1065 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1066 | return VisitObjCImplDecl(D); |
| 1067 | } |
| 1068 | |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1069 | bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) { |
| 1070 | if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl()) |
Argyrios Kyrtzidis | 135bf8e | 2012-06-09 03:03:02 +0000 | [diff] [blame^] | 1071 | if (PD->isIvarNameSpecified()) |
| 1072 | return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU)); |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1073 | |
| 1074 | return false; |
| 1075 | } |
| 1076 | |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 1077 | bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { |
| 1078 | return VisitDeclContext(D); |
| 1079 | } |
| 1080 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 1081 | bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1082 | // Visit nested-name-specifier. |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 1083 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1084 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1085 | return true; |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 1086 | |
| 1087 | return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), |
| 1088 | D->getTargetNameLoc(), TU)); |
| 1089 | } |
| 1090 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1091 | bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1092 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1093 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1094 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1095 | return true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1096 | } |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1097 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1098 | if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU))) |
| 1099 | return true; |
| 1100 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1101 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1102 | } |
| 1103 | |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1104 | bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1105 | // Visit nested-name-specifier. |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 1106 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1107 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1108 | return true; |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1109 | |
| 1110 | return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), |
| 1111 | D->getIdentLocation(), TU)); |
| 1112 | } |
| 1113 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1114 | bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1115 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1116 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1117 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1118 | return true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1119 | } |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1120 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1121 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1122 | } |
| 1123 | |
| 1124 | bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( |
| 1125 | UnresolvedUsingTypenameDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1126 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1127 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1128 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1129 | return true; |
| 1130 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1131 | return false; |
| 1132 | } |
| 1133 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1134 | bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { |
| 1135 | switch (Name.getName().getNameKind()) { |
| 1136 | case clang::DeclarationName::Identifier: |
| 1137 | case clang::DeclarationName::CXXLiteralOperatorName: |
| 1138 | case clang::DeclarationName::CXXOperatorName: |
| 1139 | case clang::DeclarationName::CXXUsingDirective: |
| 1140 | return false; |
| 1141 | |
| 1142 | case clang::DeclarationName::CXXConstructorName: |
| 1143 | case clang::DeclarationName::CXXDestructorName: |
| 1144 | case clang::DeclarationName::CXXConversionFunctionName: |
| 1145 | if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) |
| 1146 | return Visit(TSInfo->getTypeLoc()); |
| 1147 | return false; |
| 1148 | |
| 1149 | case clang::DeclarationName::ObjCZeroArgSelector: |
| 1150 | case clang::DeclarationName::ObjCOneArgSelector: |
| 1151 | case clang::DeclarationName::ObjCMultiArgSelector: |
| 1152 | // FIXME: Per-identifier location info? |
| 1153 | return false; |
| 1154 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1155 | |
| 1156 | llvm_unreachable("Invalid DeclarationName::Kind!"); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1159 | bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, |
| 1160 | SourceRange Range) { |
| 1161 | // FIXME: This whole routine is a hack to work around the lack of proper |
| 1162 | // source information in nested-name-specifiers (PR5791). Since we do have |
| 1163 | // a beginning source location, we can visit the first component of the |
| 1164 | // nested-name-specifier, if it's a single-token component. |
| 1165 | if (!NNS) |
| 1166 | return false; |
| 1167 | |
| 1168 | // Get the first component in the nested-name-specifier. |
| 1169 | while (NestedNameSpecifier *Prefix = NNS->getPrefix()) |
| 1170 | NNS = Prefix; |
| 1171 | |
| 1172 | switch (NNS->getKind()) { |
| 1173 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1174 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), |
| 1175 | TU)); |
| 1176 | |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 1177 | case NestedNameSpecifier::NamespaceAlias: |
| 1178 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1179 | Range.getBegin(), TU)); |
| 1180 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1181 | case NestedNameSpecifier::TypeSpec: { |
| 1182 | // If the type has a form where we know that the beginning of the source |
| 1183 | // range matches up with a reference cursor. Visit the appropriate reference |
| 1184 | // cursor. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1185 | const Type *T = NNS->getAsType(); |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1186 | if (const TypedefType *Typedef = dyn_cast<TypedefType>(T)) |
| 1187 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); |
| 1188 | if (const TagType *Tag = dyn_cast<TagType>(T)) |
| 1189 | return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); |
| 1190 | if (const TemplateSpecializationType *TST |
| 1191 | = dyn_cast<TemplateSpecializationType>(T)) |
| 1192 | return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); |
| 1193 | break; |
| 1194 | } |
| 1195 | |
| 1196 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1197 | case NestedNameSpecifier::Global: |
| 1198 | case NestedNameSpecifier::Identifier: |
| 1199 | break; |
| 1200 | } |
| 1201 | |
| 1202 | return false; |
| 1203 | } |
| 1204 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1205 | bool |
| 1206 | CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1207 | SmallVector<NestedNameSpecifierLoc, 4> Qualifiers; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1208 | for (; Qualifier; Qualifier = Qualifier.getPrefix()) |
| 1209 | Qualifiers.push_back(Qualifier); |
| 1210 | |
| 1211 | while (!Qualifiers.empty()) { |
| 1212 | NestedNameSpecifierLoc Q = Qualifiers.pop_back_val(); |
| 1213 | NestedNameSpecifier *NNS = Q.getNestedNameSpecifier(); |
| 1214 | switch (NNS->getKind()) { |
| 1215 | case NestedNameSpecifier::Namespace: |
| 1216 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1217 | Q.getLocalBeginLoc(), |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1218 | TU))) |
| 1219 | return true; |
| 1220 | |
| 1221 | break; |
| 1222 | |
| 1223 | case NestedNameSpecifier::NamespaceAlias: |
| 1224 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1225 | Q.getLocalBeginLoc(), |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1226 | TU))) |
| 1227 | return true; |
| 1228 | |
| 1229 | break; |
| 1230 | |
| 1231 | case NestedNameSpecifier::TypeSpec: |
| 1232 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1233 | if (Visit(Q.getTypeLoc())) |
| 1234 | return true; |
| 1235 | |
| 1236 | break; |
| 1237 | |
| 1238 | case NestedNameSpecifier::Global: |
| 1239 | case NestedNameSpecifier::Identifier: |
| 1240 | break; |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | return false; |
| 1245 | } |
| 1246 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1247 | bool CursorVisitor::VisitTemplateParameters( |
| 1248 | const TemplateParameterList *Params) { |
| 1249 | if (!Params) |
| 1250 | return false; |
| 1251 | |
| 1252 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 1253 | PEnd = Params->end(); |
| 1254 | P != PEnd; ++P) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1255 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1256 | return true; |
| 1257 | } |
| 1258 | |
| 1259 | return false; |
| 1260 | } |
| 1261 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1262 | bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { |
| 1263 | switch (Name.getKind()) { |
| 1264 | case TemplateName::Template: |
| 1265 | return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); |
| 1266 | |
| 1267 | case TemplateName::OverloadedTemplate: |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1268 | // Visit the overloaded template set. |
| 1269 | if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU))) |
| 1270 | return true; |
| 1271 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1272 | return false; |
| 1273 | |
| 1274 | case TemplateName::DependentTemplate: |
| 1275 | // FIXME: Visit nested-name-specifier. |
| 1276 | return false; |
| 1277 | |
| 1278 | case TemplateName::QualifiedTemplate: |
| 1279 | // FIXME: Visit nested-name-specifier. |
| 1280 | return Visit(MakeCursorTemplateRef( |
| 1281 | Name.getAsQualifiedTemplateName()->getDecl(), |
| 1282 | Loc, TU)); |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1283 | |
| 1284 | case TemplateName::SubstTemplateTemplateParm: |
| 1285 | return Visit(MakeCursorTemplateRef( |
| 1286 | Name.getAsSubstTemplateTemplateParm()->getParameter(), |
| 1287 | Loc, TU)); |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 1288 | |
| 1289 | case TemplateName::SubstTemplateTemplateParmPack: |
| 1290 | return Visit(MakeCursorTemplateRef( |
| 1291 | Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), |
| 1292 | Loc, TU)); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1293 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1294 | |
| 1295 | llvm_unreachable("Invalid TemplateName::Kind!"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1298 | bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { |
| 1299 | switch (TAL.getArgument().getKind()) { |
| 1300 | case TemplateArgument::Null: |
| 1301 | case TemplateArgument::Integral: |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 1302 | case TemplateArgument::Pack: |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1303 | return false; |
| 1304 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1305 | case TemplateArgument::Type: |
| 1306 | if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) |
| 1307 | return Visit(TSInfo->getTypeLoc()); |
| 1308 | return false; |
| 1309 | |
| 1310 | case TemplateArgument::Declaration: |
| 1311 | if (Expr *E = TAL.getSourceDeclExpression()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1312 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1313 | return false; |
| 1314 | |
| 1315 | case TemplateArgument::Expression: |
| 1316 | if (Expr *E = TAL.getSourceExpression()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1317 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1318 | return false; |
| 1319 | |
| 1320 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1321 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 1322 | if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc())) |
| 1323 | return true; |
| 1324 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1325 | return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1326 | TAL.getTemplateNameLoc()); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1327 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1328 | |
| 1329 | llvm_unreachable("Invalid TemplateArgument::Kind!"); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 1332 | bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 1333 | return VisitDeclContext(D); |
| 1334 | } |
| 1335 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1336 | bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1337 | return Visit(TL.getUnqualifiedLoc()); |
| 1338 | } |
| 1339 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1340 | bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 1341 | ASTContext &Context = AU->getASTContext(); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1342 | |
| 1343 | // Some builtin types (such as Objective-C's "id", "sel", and |
| 1344 | // "Class") have associated declarations. Create cursors for those. |
| 1345 | QualType VisitType; |
John McCall | e0a22d0 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 1346 | switch (TL.getTypePtr()->getKind()) { |
John McCall | 2dde35b | 2011-10-18 22:28:37 +0000 | [diff] [blame] | 1347 | |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1348 | case BuiltinType::Void: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1349 | case BuiltinType::NullPtr: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1350 | case BuiltinType::Dependent: |
John McCall | 2dde35b | 2011-10-18 22:28:37 +0000 | [diff] [blame] | 1351 | #define BUILTIN_TYPE(Id, SingletonId) |
| 1352 | #define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1353 | #define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1354 | #define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1355 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1356 | #include "clang/AST/BuiltinTypes.def" |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1357 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1358 | |
Ted Kremenek | c4174cc | 2010-02-18 18:52:18 +0000 | [diff] [blame] | 1359 | case BuiltinType::ObjCId: |
| 1360 | VisitType = Context.getObjCIdType(); |
| 1361 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1362 | |
| 1363 | case BuiltinType::ObjCClass: |
| 1364 | VisitType = Context.getObjCClassType(); |
| 1365 | break; |
| 1366 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1367 | case BuiltinType::ObjCSel: |
| 1368 | VisitType = Context.getObjCSelType(); |
| 1369 | break; |
| 1370 | } |
| 1371 | |
| 1372 | if (!VisitType.isNull()) { |
| 1373 | if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1374 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1375 | TU)); |
| 1376 | } |
| 1377 | |
| 1378 | return false; |
| 1379 | } |
| 1380 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1381 | bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1382 | return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU)); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1385 | bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 1386 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1387 | } |
| 1388 | |
| 1389 | bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { |
Argyrios Kyrtzidis | 6f155de | 2011-08-25 22:24:47 +0000 | [diff] [blame] | 1390 | if (TL.isDefinition()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1391 | return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest)); |
Argyrios Kyrtzidis | 6f155de | 2011-08-25 22:24:47 +0000 | [diff] [blame] | 1392 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1393 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1394 | } |
| 1395 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1396 | bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
Chandler Carruth | 960d13d | 2011-05-01 09:53:37 +0000 | [diff] [blame] | 1397 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1400 | bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 1401 | if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU))) |
| 1402 | return true; |
| 1403 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1404 | return false; |
| 1405 | } |
| 1406 | |
| 1407 | bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 1408 | if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) |
| 1409 | return true; |
| 1410 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1411 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1412 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1413 | TU))) |
| 1414 | return true; |
| 1415 | } |
| 1416 | |
| 1417 | return false; |
| 1418 | } |
| 1419 | |
| 1420 | bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1421 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1424 | bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) { |
| 1425 | return Visit(TL.getInnerLoc()); |
| 1426 | } |
| 1427 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1428 | bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1429 | return Visit(TL.getPointeeLoc()); |
| 1430 | } |
| 1431 | |
| 1432 | bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1433 | return Visit(TL.getPointeeLoc()); |
| 1434 | } |
| 1435 | |
| 1436 | bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1437 | return Visit(TL.getPointeeLoc()); |
| 1438 | } |
| 1439 | |
| 1440 | bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1441 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1445 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Argyrios Kyrtzidis | 3422fbc | 2011-08-15 18:44:43 +0000 | [diff] [blame] | 1448 | bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 1449 | return Visit(TL.getModifiedLoc()); |
| 1450 | } |
| 1451 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1452 | bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, |
| 1453 | bool SkipResultType) { |
| 1454 | if (!SkipResultType && Visit(TL.getResultLoc())) |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1455 | return true; |
| 1456 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1457 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
Ted Kremenek | 5dbacb4 | 2010-04-07 00:27:13 +0000 | [diff] [blame] | 1458 | if (Decl *D = TL.getArg(I)) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1459 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest))) |
Ted Kremenek | 5dbacb4 | 2010-04-07 00:27:13 +0000 | [diff] [blame] | 1460 | return true; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1461 | |
| 1462 | return false; |
| 1463 | } |
| 1464 | |
| 1465 | bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1466 | if (Visit(TL.getElementLoc())) |
| 1467 | return true; |
| 1468 | |
| 1469 | if (Expr *Size = TL.getSizeExpr()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1470 | return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1471 | |
| 1472 | return false; |
| 1473 | } |
| 1474 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1475 | bool CursorVisitor::VisitTemplateSpecializationTypeLoc( |
| 1476 | TemplateSpecializationTypeLoc TL) { |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1477 | // Visit the template name. |
| 1478 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1479 | TL.getTemplateNameLoc())) |
| 1480 | return true; |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1481 | |
| 1482 | // Visit the template arguments. |
| 1483 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1484 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1485 | return true; |
| 1486 | |
| 1487 | return false; |
| 1488 | } |
| 1489 | |
Douglas Gregor | 2332c11 | 2010-01-21 20:48:56 +0000 | [diff] [blame] | 1490 | bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 1491 | return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); |
| 1492 | } |
| 1493 | |
| 1494 | bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 1495 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1496 | return Visit(TSInfo->getTypeLoc()); |
| 1497 | |
| 1498 | return false; |
| 1499 | } |
| 1500 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1501 | bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 1502 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1503 | return Visit(TSInfo->getTypeLoc()); |
| 1504 | |
| 1505 | return false; |
| 1506 | } |
| 1507 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 1508 | bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
| 1509 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1510 | return true; |
| 1511 | |
| 1512 | return false; |
| 1513 | } |
| 1514 | |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 1515 | bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc( |
| 1516 | DependentTemplateSpecializationTypeLoc TL) { |
| 1517 | // Visit the nested-name-specifier, if there is one. |
| 1518 | if (TL.getQualifierLoc() && |
| 1519 | VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1520 | return true; |
| 1521 | |
| 1522 | // Visit the template arguments. |
| 1523 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1524 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1525 | return true; |
| 1526 | |
| 1527 | return false; |
| 1528 | } |
| 1529 | |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1530 | bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 1531 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1532 | return true; |
| 1533 | |
| 1534 | return Visit(TL.getNamedTypeLoc()); |
| 1535 | } |
| 1536 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 1537 | bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { |
| 1538 | return Visit(TL.getPatternLoc()); |
| 1539 | } |
| 1540 | |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 1541 | bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 1542 | if (Expr *E = TL.getUnderlyingExpr()) |
| 1543 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1544 | |
| 1545 | return false; |
| 1546 | } |
| 1547 | |
| 1548 | bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 1549 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1550 | } |
| 1551 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1552 | bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) { |
| 1553 | return Visit(TL.getValueLoc()); |
| 1554 | } |
| 1555 | |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 1556 | #define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \ |
| 1557 | bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \ |
| 1558 | return Visit##PARENT##Loc(TL); \ |
| 1559 | } |
| 1560 | |
| 1561 | DEFAULT_TYPELOC_IMPL(Complex, Type) |
| 1562 | DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType) |
| 1563 | DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType) |
| 1564 | DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType) |
| 1565 | DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType) |
| 1566 | DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type) |
| 1567 | DEFAULT_TYPELOC_IMPL(Vector, Type) |
| 1568 | DEFAULT_TYPELOC_IMPL(ExtVector, VectorType) |
| 1569 | DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType) |
| 1570 | DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType) |
| 1571 | DEFAULT_TYPELOC_IMPL(Record, TagType) |
| 1572 | DEFAULT_TYPELOC_IMPL(Enum, TagType) |
| 1573 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type) |
| 1574 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type) |
| 1575 | DEFAULT_TYPELOC_IMPL(Auto, Type) |
| 1576 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 1577 | bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1578 | // Visit the nested-name-specifier, if present. |
| 1579 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1580 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1581 | return true; |
| 1582 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 1583 | if (D->isCompleteDefinition()) { |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 1584 | for (CXXRecordDecl::base_class_iterator I = D->bases_begin(), |
| 1585 | E = D->bases_end(); I != E; ++I) { |
| 1586 | if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU))) |
| 1587 | return true; |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | return VisitTagDecl(D); |
| 1592 | } |
| 1593 | |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1594 | bool CursorVisitor::VisitAttributes(Decl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1595 | for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end(); |
| 1596 | i != e; ++i) |
| 1597 | if (Visit(MakeCXCursor(*i, D, TU))) |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1598 | return true; |
| 1599 | |
| 1600 | return false; |
| 1601 | } |
| 1602 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1603 | //===----------------------------------------------------------------------===// |
| 1604 | // Data-recursive visitor methods. |
| 1605 | //===----------------------------------------------------------------------===// |
| 1606 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1607 | namespace { |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1608 | #define DEF_JOB(NAME, DATA, KIND)\ |
| 1609 | class NAME : public VisitorJob {\ |
| 1610 | public:\ |
| 1611 | NAME(DATA *d, CXCursor parent) : VisitorJob(parent, VisitorJob::KIND, d) {} \ |
| 1612 | static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\ |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1613 | DATA *get() const { return static_cast<DATA*>(data[0]); }\ |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1614 | }; |
| 1615 | |
| 1616 | DEF_JOB(StmtVisit, Stmt, StmtVisitKind) |
| 1617 | DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind) |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1618 | DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1619 | DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind) |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1620 | DEF_JOB(ExplicitTemplateArgsVisit, ASTTemplateArgumentListInfo, |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1621 | ExplicitTemplateArgsVisitKind) |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 1622 | DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind) |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 1623 | DEF_JOB(LambdaExprParts, LambdaExpr, LambdaExprPartsKind) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1624 | #undef DEF_JOB |
| 1625 | |
| 1626 | class DeclVisit : public VisitorJob { |
| 1627 | public: |
| 1628 | DeclVisit(Decl *d, CXCursor parent, bool isFirst) : |
| 1629 | VisitorJob(parent, VisitorJob::DeclVisitKind, |
| 1630 | d, isFirst ? (void*) 1 : (void*) 0) {} |
| 1631 | static bool classof(const VisitorJob *VJ) { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 1632 | return VJ->getKind() == DeclVisitKind; |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1633 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1634 | Decl *get() const { return static_cast<Decl*>(data[0]); } |
| 1635 | bool isFirst() const { return data[1] ? true : false; } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1636 | }; |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1637 | class TypeLocVisit : public VisitorJob { |
| 1638 | public: |
| 1639 | TypeLocVisit(TypeLoc tl, CXCursor parent) : |
| 1640 | VisitorJob(parent, VisitorJob::TypeLocVisitKind, |
| 1641 | tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {} |
| 1642 | |
| 1643 | static bool classof(const VisitorJob *VJ) { |
| 1644 | return VJ->getKind() == TypeLocVisitKind; |
| 1645 | } |
| 1646 | |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 1647 | TypeLoc get() const { |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1648 | QualType T = QualType::getFromOpaquePtr(data[0]); |
| 1649 | return TypeLoc(T, data[1]); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1650 | } |
| 1651 | }; |
| 1652 | |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1653 | class LabelRefVisit : public VisitorJob { |
| 1654 | public: |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1655 | LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent) |
| 1656 | : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD, |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1657 | labelLoc.getPtrEncoding()) {} |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1658 | |
| 1659 | static bool classof(const VisitorJob *VJ) { |
| 1660 | return VJ->getKind() == VisitorJob::LabelRefVisitKind; |
| 1661 | } |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1662 | LabelDecl *get() const { return static_cast<LabelDecl*>(data[0]); } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1663 | SourceLocation getLoc() const { |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1664 | return SourceLocation::getFromPtrEncoding(data[1]); } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1665 | }; |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1666 | |
| 1667 | class NestedNameSpecifierLocVisit : public VisitorJob { |
| 1668 | public: |
| 1669 | NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent) |
| 1670 | : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind, |
| 1671 | Qualifier.getNestedNameSpecifier(), |
| 1672 | Qualifier.getOpaqueData()) { } |
| 1673 | |
| 1674 | static bool classof(const VisitorJob *VJ) { |
| 1675 | return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind; |
| 1676 | } |
| 1677 | |
| 1678 | NestedNameSpecifierLoc get() const { |
| 1679 | return NestedNameSpecifierLoc(static_cast<NestedNameSpecifier*>(data[0]), |
| 1680 | data[1]); |
| 1681 | } |
| 1682 | }; |
| 1683 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1684 | class DeclarationNameInfoVisit : public VisitorJob { |
| 1685 | public: |
| 1686 | DeclarationNameInfoVisit(Stmt *S, CXCursor parent) |
| 1687 | : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {} |
| 1688 | static bool classof(const VisitorJob *VJ) { |
| 1689 | return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind; |
| 1690 | } |
| 1691 | DeclarationNameInfo get() const { |
| 1692 | Stmt *S = static_cast<Stmt*>(data[0]); |
| 1693 | switch (S->getStmtClass()) { |
| 1694 | default: |
| 1695 | llvm_unreachable("Unhandled Stmt"); |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 1696 | case clang::Stmt::MSDependentExistsStmtClass: |
| 1697 | return cast<MSDependentExistsStmt>(S)->getNameInfo(); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1698 | case Stmt::CXXDependentScopeMemberExprClass: |
| 1699 | return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo(); |
| 1700 | case Stmt::DependentScopeDeclRefExprClass: |
| 1701 | return cast<DependentScopeDeclRefExpr>(S)->getNameInfo(); |
| 1702 | } |
| 1703 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1704 | }; |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1705 | class MemberRefVisit : public VisitorJob { |
| 1706 | public: |
| 1707 | MemberRefVisit(FieldDecl *D, SourceLocation L, CXCursor parent) |
| 1708 | : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D, |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1709 | L.getPtrEncoding()) {} |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1710 | static bool classof(const VisitorJob *VJ) { |
| 1711 | return VJ->getKind() == VisitorJob::MemberRefVisitKind; |
| 1712 | } |
| 1713 | FieldDecl *get() const { |
| 1714 | return static_cast<FieldDecl*>(data[0]); |
| 1715 | } |
| 1716 | SourceLocation getLoc() const { |
| 1717 | return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]); |
| 1718 | } |
| 1719 | }; |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1720 | class EnqueueVisitor : public StmtVisitor<EnqueueVisitor, void> { |
| 1721 | VisitorWorkList &WL; |
| 1722 | CXCursor Parent; |
| 1723 | public: |
| 1724 | EnqueueVisitor(VisitorWorkList &wl, CXCursor parent) |
| 1725 | : WL(wl), Parent(parent) {} |
| 1726 | |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1727 | void VisitAddrLabelExpr(AddrLabelExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1728 | void VisitBlockExpr(BlockExpr *B); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1729 | void VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
Ted Kremenek | 083c7e2 | 2010-11-13 05:38:03 +0000 | [diff] [blame] | 1730 | void VisitCompoundStmt(CompoundStmt *S); |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1731 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { /* Do nothing. */ } |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 1732 | void VisitMSDependentExistsStmt(MSDependentExistsStmt *S); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1733 | void VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1734 | void VisitCXXNewExpr(CXXNewExpr *E); |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 1735 | void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1736 | void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1737 | void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1738 | void VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); |
Ted Kremenek | b8dd1ca | 2010-11-17 00:50:41 +0000 | [diff] [blame] | 1739 | void VisitCXXTypeidExpr(CXXTypeidExpr *E); |
Ted Kremenek | 55b933a | 2010-11-17 00:50:36 +0000 | [diff] [blame] | 1740 | void VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); |
Ted Kremenek | 1e7e877 | 2010-11-17 00:50:52 +0000 | [diff] [blame] | 1741 | void VisitCXXUuidofExpr(CXXUuidofExpr *E); |
Argyrios Kyrtzidis | dcbb2fb | 2011-12-03 03:49:44 +0000 | [diff] [blame] | 1742 | void VisitCXXCatchStmt(CXXCatchStmt *S); |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1743 | void VisitDeclRefExpr(DeclRefExpr *D); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1744 | void VisitDeclStmt(DeclStmt *S); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1745 | void VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1746 | void VisitDesignatedInitExpr(DesignatedInitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1747 | void VisitExplicitCastExpr(ExplicitCastExpr *E); |
| 1748 | void VisitForStmt(ForStmt *FS); |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1749 | void VisitGotoStmt(GotoStmt *GS); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1750 | void VisitIfStmt(IfStmt *If); |
| 1751 | void VisitInitListExpr(InitListExpr *IE); |
| 1752 | void VisitMemberExpr(MemberExpr *M); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1753 | void VisitOffsetOfExpr(OffsetOfExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1754 | void VisitObjCEncodeExpr(ObjCEncodeExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1755 | void VisitObjCMessageExpr(ObjCMessageExpr *M); |
| 1756 | void VisitOverloadExpr(OverloadExpr *E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1757 | void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1758 | void VisitStmt(Stmt *S); |
| 1759 | void VisitSwitchStmt(SwitchStmt *S); |
| 1760 | void VisitWhileStmt(WhileStmt *W); |
Ted Kremenek | 2939b6f | 2010-11-17 00:50:50 +0000 | [diff] [blame] | 1761 | void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 1762 | void VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E); |
Douglas Gregor | 4ca8ac2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 1763 | void VisitTypeTraitExpr(TypeTraitExpr *E); |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 1764 | void VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 1765 | void VisitExpressionTraitExpr(ExpressionTraitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1766 | void VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U); |
Ted Kremenek | 9d3bf79 | 2010-11-17 00:50:43 +0000 | [diff] [blame] | 1767 | void VisitVAArgExpr(VAArgExpr *E); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 1768 | void VisitSizeOfPackExpr(SizeOfPackExpr *E); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1769 | void VisitPseudoObjectExpr(PseudoObjectExpr *E); |
| 1770 | void VisitOpaqueValueExpr(OpaqueValueExpr *E); |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 1771 | void VisitLambdaExpr(LambdaExpr *E); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1772 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1773 | private: |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1774 | void AddDeclarationNameInfo(Stmt *S); |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1775 | void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier); |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1776 | void AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1777 | void AddMemberRef(FieldDecl *D, SourceLocation L); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1778 | void AddStmt(Stmt *S); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1779 | void AddDecl(Decl *D, bool isFirst = true); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1780 | void AddTypeLoc(TypeSourceInfo *TI); |
| 1781 | void EnqueueChildren(Stmt *S); |
| 1782 | }; |
| 1783 | } // end anonyous namespace |
| 1784 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1785 | void EnqueueVisitor::AddDeclarationNameInfo(Stmt *S) { |
| 1786 | // 'S' should always be non-null, since it comes from the |
| 1787 | // statement we are visiting. |
| 1788 | WL.push_back(DeclarationNameInfoVisit(S, Parent)); |
| 1789 | } |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1790 | |
| 1791 | void |
| 1792 | EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 1793 | if (Qualifier) |
| 1794 | WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent)); |
| 1795 | } |
| 1796 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1797 | void EnqueueVisitor::AddStmt(Stmt *S) { |
| 1798 | if (S) |
| 1799 | WL.push_back(StmtVisit(S, Parent)); |
| 1800 | } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1801 | void EnqueueVisitor::AddDecl(Decl *D, bool isFirst) { |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1802 | if (D) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1803 | WL.push_back(DeclVisit(D, Parent, isFirst)); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1804 | } |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1805 | void EnqueueVisitor:: |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1806 | AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1807 | if (A) |
| 1808 | WL.push_back(ExplicitTemplateArgsVisit( |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1809 | const_cast<ASTTemplateArgumentListInfo*>(A), Parent)); |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1810 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1811 | void EnqueueVisitor::AddMemberRef(FieldDecl *D, SourceLocation L) { |
| 1812 | if (D) |
| 1813 | WL.push_back(MemberRefVisit(D, L, Parent)); |
| 1814 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1815 | void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) { |
| 1816 | if (TI) |
| 1817 | WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent)); |
| 1818 | } |
| 1819 | void EnqueueVisitor::EnqueueChildren(Stmt *S) { |
Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 1820 | unsigned size = WL.size(); |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1821 | for (Stmt::child_range Child = S->children(); Child; ++Child) { |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1822 | AddStmt(*Child); |
Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 1823 | } |
| 1824 | if (size == WL.size()) |
| 1825 | return; |
| 1826 | // Now reverse the entries we just added. This will match the DFS |
| 1827 | // ordering performed by the worklist. |
| 1828 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 1829 | std::reverse(I, E); |
| 1830 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1831 | void EnqueueVisitor::VisitAddrLabelExpr(AddrLabelExpr *E) { |
| 1832 | WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent)); |
| 1833 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1834 | void EnqueueVisitor::VisitBlockExpr(BlockExpr *B) { |
| 1835 | AddDecl(B->getBlockDecl()); |
| 1836 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1837 | void EnqueueVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 1838 | EnqueueChildren(E); |
| 1839 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1840 | } |
Ted Kremenek | 083c7e2 | 2010-11-13 05:38:03 +0000 | [diff] [blame] | 1841 | void EnqueueVisitor::VisitCompoundStmt(CompoundStmt *S) { |
| 1842 | for (CompoundStmt::reverse_body_iterator I = S->body_rbegin(), |
| 1843 | E = S->body_rend(); I != E; ++I) { |
| 1844 | AddStmt(*I); |
| 1845 | } |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1846 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1847 | void EnqueueVisitor:: |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 1848 | VisitMSDependentExistsStmt(MSDependentExistsStmt *S) { |
| 1849 | AddStmt(S->getSubStmt()); |
| 1850 | AddDeclarationNameInfo(S); |
| 1851 | if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc()) |
| 1852 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 1853 | } |
| 1854 | |
| 1855 | void EnqueueVisitor:: |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1856 | VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) { |
| 1857 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
| 1858 | AddDeclarationNameInfo(E); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1859 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 1860 | AddNestedNameSpecifierLoc(QualifierLoc); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1861 | if (!E->isImplicitAccess()) |
| 1862 | AddStmt(E->getBase()); |
| 1863 | } |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1864 | void EnqueueVisitor::VisitCXXNewExpr(CXXNewExpr *E) { |
Sebastian Redl | 2aed8b8 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1865 | // Enqueue the initializer , if any. |
| 1866 | AddStmt(E->getInitializer()); |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1867 | // Enqueue the array size, if any. |
| 1868 | AddStmt(E->getArraySize()); |
| 1869 | // Enqueue the allocated type. |
| 1870 | AddTypeLoc(E->getAllocatedTypeSourceInfo()); |
| 1871 | // Enqueue the placement arguments. |
| 1872 | for (unsigned I = E->getNumPlacementArgs(); I > 0; --I) |
| 1873 | AddStmt(E->getPlacementArg(I-1)); |
| 1874 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1875 | void EnqueueVisitor::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *CE) { |
Ted Kremenek | 8b8d8c9 | 2010-11-13 05:55:56 +0000 | [diff] [blame] | 1876 | for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I) |
| 1877 | AddStmt(CE->getArg(I-1)); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1878 | AddStmt(CE->getCallee()); |
| 1879 | AddStmt(CE->getArg(0)); |
| 1880 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1881 | void EnqueueVisitor::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { |
| 1882 | // Visit the name of the type being destroyed. |
| 1883 | AddTypeLoc(E->getDestroyedTypeInfo()); |
| 1884 | // Visit the scope type that looks disturbingly like the nested-name-specifier |
| 1885 | // but isn't. |
| 1886 | AddTypeLoc(E->getScopeTypeInfo()); |
| 1887 | // Visit the nested-name-specifier. |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1888 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 1889 | AddNestedNameSpecifierLoc(QualifierLoc); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1890 | // Visit base expression. |
| 1891 | AddStmt(E->getBase()); |
| 1892 | } |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 1893 | void EnqueueVisitor::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
| 1894 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1895 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1896 | void EnqueueVisitor::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { |
| 1897 | EnqueueChildren(E); |
| 1898 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1899 | } |
Ted Kremenek | b8dd1ca | 2010-11-17 00:50:41 +0000 | [diff] [blame] | 1900 | void EnqueueVisitor::VisitCXXTypeidExpr(CXXTypeidExpr *E) { |
| 1901 | EnqueueChildren(E); |
| 1902 | if (E->isTypeOperand()) |
| 1903 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 1904 | } |
Ted Kremenek | 55b933a | 2010-11-17 00:50:36 +0000 | [diff] [blame] | 1905 | |
| 1906 | void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr |
| 1907 | *E) { |
| 1908 | EnqueueChildren(E); |
| 1909 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1910 | } |
Ted Kremenek | 1e7e877 | 2010-11-17 00:50:52 +0000 | [diff] [blame] | 1911 | void EnqueueVisitor::VisitCXXUuidofExpr(CXXUuidofExpr *E) { |
| 1912 | EnqueueChildren(E); |
| 1913 | if (E->isTypeOperand()) |
| 1914 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 1915 | } |
Argyrios Kyrtzidis | dcbb2fb | 2011-12-03 03:49:44 +0000 | [diff] [blame] | 1916 | |
| 1917 | void EnqueueVisitor::VisitCXXCatchStmt(CXXCatchStmt *S) { |
| 1918 | EnqueueChildren(S); |
| 1919 | AddDecl(S->getExceptionDecl()); |
| 1920 | } |
| 1921 | |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1922 | void EnqueueVisitor::VisitDeclRefExpr(DeclRefExpr *DR) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1923 | if (DR->hasExplicitTemplateArgs()) { |
| 1924 | AddExplicitTemplateArgs(&DR->getExplicitTemplateArgs()); |
| 1925 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1926 | WL.push_back(DeclRefExprParts(DR, Parent)); |
| 1927 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1928 | void EnqueueVisitor::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { |
| 1929 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
| 1930 | AddDeclarationNameInfo(E); |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 1931 | AddNestedNameSpecifierLoc(E->getQualifierLoc()); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1932 | } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1933 | void EnqueueVisitor::VisitDeclStmt(DeclStmt *S) { |
| 1934 | unsigned size = WL.size(); |
| 1935 | bool isFirst = true; |
| 1936 | for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); |
| 1937 | D != DEnd; ++D) { |
| 1938 | AddDecl(*D, isFirst); |
| 1939 | isFirst = false; |
| 1940 | } |
| 1941 | if (size == WL.size()) |
| 1942 | return; |
| 1943 | // Now reverse the entries we just added. This will match the DFS |
| 1944 | // ordering performed by the worklist. |
| 1945 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 1946 | std::reverse(I, E); |
| 1947 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1948 | void EnqueueVisitor::VisitDesignatedInitExpr(DesignatedInitExpr *E) { |
| 1949 | AddStmt(E->getInit()); |
| 1950 | typedef DesignatedInitExpr::Designator Designator; |
| 1951 | for (DesignatedInitExpr::reverse_designators_iterator |
| 1952 | D = E->designators_rbegin(), DEnd = E->designators_rend(); |
| 1953 | D != DEnd; ++D) { |
| 1954 | if (D->isFieldDesignator()) { |
| 1955 | if (FieldDecl *Field = D->getField()) |
| 1956 | AddMemberRef(Field, D->getFieldLoc()); |
| 1957 | continue; |
| 1958 | } |
| 1959 | if (D->isArrayDesignator()) { |
| 1960 | AddStmt(E->getArrayIndex(*D)); |
| 1961 | continue; |
| 1962 | } |
| 1963 | assert(D->isArrayRangeDesignator() && "Unknown designator kind"); |
| 1964 | AddStmt(E->getArrayRangeEnd(*D)); |
| 1965 | AddStmt(E->getArrayRangeStart(*D)); |
| 1966 | } |
| 1967 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1968 | void EnqueueVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) { |
| 1969 | EnqueueChildren(E); |
| 1970 | AddTypeLoc(E->getTypeInfoAsWritten()); |
| 1971 | } |
| 1972 | void EnqueueVisitor::VisitForStmt(ForStmt *FS) { |
| 1973 | AddStmt(FS->getBody()); |
| 1974 | AddStmt(FS->getInc()); |
| 1975 | AddStmt(FS->getCond()); |
| 1976 | AddDecl(FS->getConditionVariable()); |
| 1977 | AddStmt(FS->getInit()); |
| 1978 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1979 | void EnqueueVisitor::VisitGotoStmt(GotoStmt *GS) { |
| 1980 | WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent)); |
| 1981 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1982 | void EnqueueVisitor::VisitIfStmt(IfStmt *If) { |
| 1983 | AddStmt(If->getElse()); |
| 1984 | AddStmt(If->getThen()); |
| 1985 | AddStmt(If->getCond()); |
| 1986 | AddDecl(If->getConditionVariable()); |
| 1987 | } |
| 1988 | void EnqueueVisitor::VisitInitListExpr(InitListExpr *IE) { |
| 1989 | // We care about the syntactic form of the initializer list, only. |
| 1990 | if (InitListExpr *Syntactic = IE->getSyntacticForm()) |
| 1991 | IE = Syntactic; |
| 1992 | EnqueueChildren(IE); |
| 1993 | } |
| 1994 | void EnqueueVisitor::VisitMemberExpr(MemberExpr *M) { |
Douglas Gregor | 89629a7 | 2010-11-17 17:15:08 +0000 | [diff] [blame] | 1995 | WL.push_back(MemberExprParts(M, Parent)); |
| 1996 | |
| 1997 | // If the base of the member access expression is an implicit 'this', don't |
| 1998 | // visit it. |
| 1999 | // FIXME: If we ever want to show these implicit accesses, this will be |
| 2000 | // unfortunate. However, clang_getCursor() relies on this behavior. |
Douglas Gregor | 75e8504 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 2001 | if (!M->isImplicitAccess()) |
| 2002 | AddStmt(M->getBase()); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2003 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 2004 | void EnqueueVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) { |
| 2005 | AddTypeLoc(E->getEncodedTypeSourceInfo()); |
| 2006 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2007 | void EnqueueVisitor::VisitObjCMessageExpr(ObjCMessageExpr *M) { |
| 2008 | EnqueueChildren(M); |
| 2009 | AddTypeLoc(M->getClassReceiverTypeInfo()); |
| 2010 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2011 | void EnqueueVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) { |
| 2012 | // Visit the components of the offsetof expression. |
| 2013 | for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) { |
| 2014 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 2015 | const OffsetOfNode &Node = E->getComponent(I-1); |
| 2016 | switch (Node.getKind()) { |
| 2017 | case OffsetOfNode::Array: |
| 2018 | AddStmt(E->getIndexExpr(Node.getArrayExprIndex())); |
| 2019 | break; |
| 2020 | case OffsetOfNode::Field: |
Abramo Bagnara | 06dec89 | 2011-03-12 09:45:03 +0000 | [diff] [blame] | 2021 | AddMemberRef(Node.getField(), Node.getSourceRange().getEnd()); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2022 | break; |
| 2023 | case OffsetOfNode::Identifier: |
| 2024 | case OffsetOfNode::Base: |
| 2025 | continue; |
| 2026 | } |
| 2027 | } |
| 2028 | // Visit the type into which we're computing the offset. |
| 2029 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2030 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2031 | void EnqueueVisitor::VisitOverloadExpr(OverloadExpr *E) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2032 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2033 | WL.push_back(OverloadExprParts(E, Parent)); |
| 2034 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2035 | void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr( |
| 2036 | UnaryExprOrTypeTraitExpr *E) { |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 2037 | EnqueueChildren(E); |
| 2038 | if (E->isArgumentType()) |
| 2039 | AddTypeLoc(E->getArgumentTypeInfo()); |
| 2040 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2041 | void EnqueueVisitor::VisitStmt(Stmt *S) { |
| 2042 | EnqueueChildren(S); |
| 2043 | } |
| 2044 | void EnqueueVisitor::VisitSwitchStmt(SwitchStmt *S) { |
| 2045 | AddStmt(S->getBody()); |
| 2046 | AddStmt(S->getCond()); |
| 2047 | AddDecl(S->getConditionVariable()); |
| 2048 | } |
Ted Kremenek | fafa75a | 2010-11-17 00:50:39 +0000 | [diff] [blame] | 2049 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2050 | void EnqueueVisitor::VisitWhileStmt(WhileStmt *W) { |
| 2051 | AddStmt(W->getBody()); |
| 2052 | AddStmt(W->getCond()); |
| 2053 | AddDecl(W->getConditionVariable()); |
| 2054 | } |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2055 | |
Ted Kremenek | 2939b6f | 2010-11-17 00:50:50 +0000 | [diff] [blame] | 2056 | void EnqueueVisitor::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { |
| 2057 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2058 | } |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2059 | |
| 2060 | void EnqueueVisitor::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) { |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2061 | AddTypeLoc(E->getRhsTypeSourceInfo()); |
Francois Pichet | 0a03a3f | 2010-12-08 09:11:05 +0000 | [diff] [blame] | 2062 | AddTypeLoc(E->getLhsTypeSourceInfo()); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Douglas Gregor | 4ca8ac2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 2065 | void EnqueueVisitor::VisitTypeTraitExpr(TypeTraitExpr *E) { |
| 2066 | for (unsigned I = E->getNumArgs(); I > 0; --I) |
| 2067 | AddTypeLoc(E->getArg(I-1)); |
| 2068 | } |
| 2069 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2070 | void EnqueueVisitor::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { |
| 2071 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2072 | } |
| 2073 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 2074 | void EnqueueVisitor::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { |
| 2075 | EnqueueChildren(E); |
| 2076 | } |
| 2077 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2078 | void EnqueueVisitor::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U) { |
| 2079 | VisitOverloadExpr(U); |
| 2080 | if (!U->isImplicitAccess()) |
| 2081 | AddStmt(U->getBase()); |
| 2082 | } |
Ted Kremenek | 9d3bf79 | 2010-11-17 00:50:43 +0000 | [diff] [blame] | 2083 | void EnqueueVisitor::VisitVAArgExpr(VAArgExpr *E) { |
| 2084 | AddStmt(E->getSubExpr()); |
| 2085 | AddTypeLoc(E->getWrittenTypeInfo()); |
| 2086 | } |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2087 | void EnqueueVisitor::VisitSizeOfPackExpr(SizeOfPackExpr *E) { |
| 2088 | WL.push_back(SizeOfPackExprParts(E, Parent)); |
| 2089 | } |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2090 | void EnqueueVisitor::VisitOpaqueValueExpr(OpaqueValueExpr *E) { |
| 2091 | // If the opaque value has a source expression, just transparently |
| 2092 | // visit that. This is useful for (e.g.) pseudo-object expressions. |
| 2093 | if (Expr *SourceExpr = E->getSourceExpr()) |
| 2094 | return Visit(SourceExpr); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2095 | } |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 2096 | void EnqueueVisitor::VisitLambdaExpr(LambdaExpr *E) { |
| 2097 | AddStmt(E->getBody()); |
| 2098 | WL.push_back(LambdaExprParts(E, Parent)); |
| 2099 | } |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2100 | void EnqueueVisitor::VisitPseudoObjectExpr(PseudoObjectExpr *E) { |
| 2101 | // Treat the expression like its syntactic form. |
| 2102 | Visit(E->getSyntacticForm()); |
| 2103 | } |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2104 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2105 | void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, Stmt *S) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2106 | EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S); |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { |
| 2110 | if (RegionOfInterest.isValid()) { |
| 2111 | SourceRange Range = getRawCursorExtent(C); |
| 2112 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 2113 | return false; |
| 2114 | } |
| 2115 | return true; |
| 2116 | } |
| 2117 | |
| 2118 | bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { |
| 2119 | while (!WL.empty()) { |
| 2120 | // Dequeue the worklist item. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2121 | VisitorJob LI = WL.back(); |
| 2122 | WL.pop_back(); |
| 2123 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2124 | // Set the Parent field, then back to its old value once we're done. |
| 2125 | SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); |
| 2126 | |
| 2127 | switch (LI.getKind()) { |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2128 | case VisitorJob::DeclVisitKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2129 | Decl *D = cast<DeclVisit>(&LI)->get(); |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2130 | if (!D) |
| 2131 | continue; |
| 2132 | |
| 2133 | // For now, perform default visitation for Decls. |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2134 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest, |
| 2135 | cast<DeclVisit>(&LI)->isFirst()))) |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2136 | return true; |
| 2137 | |
| 2138 | continue; |
| 2139 | } |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2140 | case VisitorJob::ExplicitTemplateArgsVisitKind: { |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 2141 | const ASTTemplateArgumentListInfo *ArgList = |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2142 | cast<ExplicitTemplateArgsVisit>(&LI)->get(); |
| 2143 | for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(), |
| 2144 | *ArgEnd = Arg + ArgList->NumTemplateArgs; |
| 2145 | Arg != ArgEnd; ++Arg) { |
| 2146 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2147 | return true; |
| 2148 | } |
| 2149 | continue; |
| 2150 | } |
Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 2151 | case VisitorJob::TypeLocVisitKind: { |
| 2152 | // Perform default visitation for TypeLocs. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2153 | if (Visit(cast<TypeLocVisit>(&LI)->get())) |
Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 2154 | return true; |
| 2155 | continue; |
| 2156 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2157 | case VisitorJob::LabelRefVisitKind: { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2158 | LabelDecl *LS = cast<LabelRefVisit>(&LI)->get(); |
Ted Kremenek | e745501 | 2011-02-23 04:54:51 +0000 | [diff] [blame] | 2159 | if (LabelStmt *stmt = LS->getStmt()) { |
| 2160 | if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(), |
| 2161 | TU))) { |
| 2162 | return true; |
| 2163 | } |
| 2164 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2165 | continue; |
| 2166 | } |
Ted Kremenek | 47695c8 | 2011-08-18 22:25:21 +0000 | [diff] [blame] | 2167 | |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 2168 | case VisitorJob::NestedNameSpecifierLocVisitKind: { |
| 2169 | NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI); |
| 2170 | if (VisitNestedNameSpecifierLoc(V->get())) |
| 2171 | return true; |
| 2172 | continue; |
| 2173 | } |
| 2174 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 2175 | case VisitorJob::DeclarationNameInfoVisitKind: { |
| 2176 | if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI) |
| 2177 | ->get())) |
| 2178 | return true; |
| 2179 | continue; |
| 2180 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2181 | case VisitorJob::MemberRefVisitKind: { |
| 2182 | MemberRefVisit *V = cast<MemberRefVisit>(&LI); |
| 2183 | if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU))) |
| 2184 | return true; |
| 2185 | continue; |
| 2186 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2187 | case VisitorJob::StmtVisitKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2188 | Stmt *S = cast<StmtVisit>(&LI)->get(); |
Ted Kremenek | 8c269ac | 2010-11-11 23:11:43 +0000 | [diff] [blame] | 2189 | if (!S) |
| 2190 | continue; |
| 2191 | |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2192 | // Update the current cursor. |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2193 | CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2194 | if (!IsInRegionOfInterest(Cursor)) |
| 2195 | continue; |
| 2196 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 2197 | case CXChildVisit_Break: return true; |
| 2198 | case CXChildVisit_Continue: break; |
| 2199 | case CXChildVisit_Recurse: |
| 2200 | EnqueueWorkList(WL, S); |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2201 | break; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2202 | } |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2203 | continue; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2204 | } |
| 2205 | case VisitorJob::MemberExprPartsKind: { |
| 2206 | // Handle the other pieces in the MemberExpr besides the base. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2207 | MemberExpr *M = cast<MemberExprParts>(&LI)->get(); |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2208 | |
| 2209 | // Visit the nested-name-specifier |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2210 | if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc()) |
| 2211 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2212 | return true; |
| 2213 | |
| 2214 | // Visit the declaration name. |
| 2215 | if (VisitDeclarationNameInfo(M->getMemberNameInfo())) |
| 2216 | return true; |
| 2217 | |
| 2218 | // Visit the explicitly-specified template arguments, if any. |
| 2219 | if (M->hasExplicitTemplateArgs()) { |
| 2220 | for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), |
| 2221 | *ArgEnd = Arg + M->getNumTemplateArgs(); |
| 2222 | Arg != ArgEnd; ++Arg) { |
| 2223 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2224 | return true; |
| 2225 | } |
| 2226 | } |
| 2227 | continue; |
| 2228 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2229 | case VisitorJob::DeclRefExprPartsKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2230 | DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get(); |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2231 | // Visit nested-name-specifier, if present. |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2232 | if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc()) |
| 2233 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2234 | return true; |
| 2235 | // Visit declaration name. |
| 2236 | if (VisitDeclarationNameInfo(DR->getNameInfo())) |
| 2237 | return true; |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2238 | continue; |
| 2239 | } |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2240 | case VisitorJob::OverloadExprPartsKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2241 | OverloadExpr *O = cast<OverloadExprParts>(&LI)->get(); |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2242 | // Visit the nested-name-specifier. |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2243 | if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc()) |
| 2244 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2245 | return true; |
| 2246 | // Visit the declaration name. |
| 2247 | if (VisitDeclarationNameInfo(O->getNameInfo())) |
| 2248 | return true; |
| 2249 | // Visit the overloaded declaration reference. |
| 2250 | if (Visit(MakeCursorOverloadedDeclRef(O, TU))) |
| 2251 | return true; |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2252 | continue; |
| 2253 | } |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2254 | case VisitorJob::SizeOfPackExprPartsKind: { |
| 2255 | SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get(); |
| 2256 | NamedDecl *Pack = E->getPack(); |
| 2257 | if (isa<TemplateTypeParmDecl>(Pack)) { |
| 2258 | if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack), |
| 2259 | E->getPackLoc(), TU))) |
| 2260 | return true; |
| 2261 | |
| 2262 | continue; |
| 2263 | } |
| 2264 | |
| 2265 | if (isa<TemplateTemplateParmDecl>(Pack)) { |
| 2266 | if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack), |
| 2267 | E->getPackLoc(), TU))) |
| 2268 | return true; |
| 2269 | |
| 2270 | continue; |
| 2271 | } |
| 2272 | |
| 2273 | // Non-type template parameter packs and function parameter packs are |
| 2274 | // treated like DeclRefExpr cursors. |
| 2275 | continue; |
| 2276 | } |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 2277 | |
| 2278 | case VisitorJob::LambdaExprPartsKind: { |
| 2279 | // Visit captures. |
| 2280 | LambdaExpr *E = cast<LambdaExprParts>(&LI)->get(); |
| 2281 | for (LambdaExpr::capture_iterator C = E->explicit_capture_begin(), |
| 2282 | CEnd = E->explicit_capture_end(); |
| 2283 | C != CEnd; ++C) { |
| 2284 | if (C->capturesThis()) |
| 2285 | continue; |
| 2286 | |
| 2287 | if (Visit(MakeCursorVariableRef(C->getCapturedVar(), |
| 2288 | C->getLocation(), |
| 2289 | TU))) |
| 2290 | return true; |
| 2291 | } |
| 2292 | |
| 2293 | // Visit parameters and return type, if present. |
| 2294 | if (E->hasExplicitParameters() || E->hasExplicitResultType()) { |
| 2295 | TypeLoc TL = E->getCallOperator()->getTypeSourceInfo()->getTypeLoc(); |
| 2296 | if (E->hasExplicitParameters() && E->hasExplicitResultType()) { |
| 2297 | // Visit the whole type. |
| 2298 | if (Visit(TL)) |
| 2299 | return true; |
| 2300 | } else if (isa<FunctionProtoTypeLoc>(TL)) { |
| 2301 | FunctionProtoTypeLoc Proto = cast<FunctionProtoTypeLoc>(TL); |
| 2302 | if (E->hasExplicitParameters()) { |
| 2303 | // Visit parameters. |
| 2304 | for (unsigned I = 0, N = Proto.getNumArgs(); I != N; ++I) |
| 2305 | if (Visit(MakeCXCursor(Proto.getArg(I), TU))) |
| 2306 | return true; |
| 2307 | } else { |
| 2308 | // Visit result type. |
| 2309 | if (Visit(Proto.getResultLoc())) |
| 2310 | return true; |
| 2311 | } |
| 2312 | } |
| 2313 | } |
| 2314 | break; |
| 2315 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2316 | } |
| 2317 | } |
| 2318 | return false; |
| 2319 | } |
| 2320 | |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2321 | bool CursorVisitor::Visit(Stmt *S) { |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 2322 | VisitorWorkList *WL = 0; |
| 2323 | if (!WorkListFreeList.empty()) { |
| 2324 | WL = WorkListFreeList.back(); |
| 2325 | WL->clear(); |
| 2326 | WorkListFreeList.pop_back(); |
| 2327 | } |
| 2328 | else { |
| 2329 | WL = new VisitorWorkList(); |
| 2330 | WorkListCache.push_back(WL); |
| 2331 | } |
| 2332 | EnqueueWorkList(*WL, S); |
| 2333 | bool result = RunVisitorWorkList(*WL); |
| 2334 | WorkListFreeList.push_back(WL); |
| 2335 | return result; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2336 | } |
| 2337 | |
Francois Pichet | 48a8d14 | 2011-07-25 22:00:44 +0000 | [diff] [blame] | 2338 | namespace { |
| 2339 | typedef llvm::SmallVector<SourceRange, 4> RefNamePieces; |
| 2340 | RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr, |
| 2341 | const DeclarationNameInfo &NI, |
| 2342 | const SourceRange &QLoc, |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 2343 | const ASTTemplateArgumentListInfo *TemplateArgs = 0){ |
Francois Pichet | 48a8d14 | 2011-07-25 22:00:44 +0000 | [diff] [blame] | 2344 | const bool WantQualifier = NameFlags & CXNameRange_WantQualifier; |
| 2345 | const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs; |
| 2346 | const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece; |
| 2347 | |
| 2348 | const DeclarationName::NameKind Kind = NI.getName().getNameKind(); |
| 2349 | |
| 2350 | RefNamePieces Pieces; |
| 2351 | |
| 2352 | if (WantQualifier && QLoc.isValid()) |
| 2353 | Pieces.push_back(QLoc); |
| 2354 | |
| 2355 | if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr) |
| 2356 | Pieces.push_back(NI.getLoc()); |
| 2357 | |
| 2358 | if (WantTemplateArgs && TemplateArgs) |
| 2359 | Pieces.push_back(SourceRange(TemplateArgs->LAngleLoc, |
| 2360 | TemplateArgs->RAngleLoc)); |
| 2361 | |
| 2362 | if (Kind == DeclarationName::CXXOperatorName) { |
| 2363 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 2364 | NI.getInfo().CXXOperatorName.BeginOpNameLoc)); |
| 2365 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 2366 | NI.getInfo().CXXOperatorName.EndOpNameLoc)); |
| 2367 | } |
| 2368 | |
| 2369 | if (WantSinglePiece) { |
| 2370 | SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd()); |
| 2371 | Pieces.clear(); |
| 2372 | Pieces.push_back(R); |
| 2373 | } |
| 2374 | |
| 2375 | return Pieces; |
| 2376 | } |
| 2377 | } |
| 2378 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2379 | //===----------------------------------------------------------------------===// |
| 2380 | // Misc. API hooks. |
| 2381 | //===----------------------------------------------------------------------===// |
| 2382 | |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2383 | static llvm::sys::Mutex EnableMultithreadingMutex; |
| 2384 | static bool EnabledMultithreading; |
| 2385 | |
Argyrios Kyrtzidis | fa39f5b | 2011-12-15 04:52:41 +0000 | [diff] [blame] | 2386 | static void fatal_error_handler(void *user_data, const std::string& reason) { |
Argyrios Kyrtzidis | fa39f5b | 2011-12-15 04:52:41 +0000 | [diff] [blame] | 2387 | // Write the result out to stderr avoiding errs() because raw_ostreams can |
| 2388 | // call report_fatal_error. |
Argyrios Kyrtzidis | db7a800 | 2011-12-15 06:51:30 +0000 | [diff] [blame] | 2389 | fprintf(stderr, "LIBCLANG FATAL ERROR: %s\n", reason.c_str()); |
Argyrios Kyrtzidis | fa39f5b | 2011-12-15 04:52:41 +0000 | [diff] [blame] | 2390 | ::abort(); |
| 2391 | } |
| 2392 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 2393 | extern "C" { |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2394 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 2395 | int displayDiagnostics) { |
Daniel Dunbar | 48615ff | 2010-10-08 19:30:33 +0000 | [diff] [blame] | 2396 | // Disable pretty stack trace functionality, which will otherwise be a very |
| 2397 | // poor citizen of the world and set up all sorts of signal handlers. |
| 2398 | llvm::DisablePrettyStackTrace = true; |
| 2399 | |
Daniel Dunbar | c7df4f3 | 2010-08-18 18:43:14 +0000 | [diff] [blame] | 2400 | // We use crash recovery to make some of our APIs more reliable, implicitly |
| 2401 | // enable it. |
| 2402 | llvm::CrashRecoveryContext::Enable(); |
| 2403 | |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2404 | // Enable support for multithreading in LLVM. |
| 2405 | { |
| 2406 | llvm::sys::ScopedLock L(EnableMultithreadingMutex); |
| 2407 | if (!EnabledMultithreading) { |
Argyrios Kyrtzidis | fa39f5b | 2011-12-15 04:52:41 +0000 | [diff] [blame] | 2408 | llvm::install_fatal_error_handler(fatal_error_handler, 0); |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2409 | llvm::llvm_start_multithreaded(); |
| 2410 | EnabledMultithreading = true; |
| 2411 | } |
| 2412 | } |
| 2413 | |
Douglas Gregor | a030b7c | 2010-01-22 20:35:53 +0000 | [diff] [blame] | 2414 | CIndexer *CIdxr = new CIndexer(); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2415 | if (excludeDeclarationsFromPCH) |
| 2416 | CIdxr->setOnlyLocalDecls(); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2417 | if (displayDiagnostics) |
| 2418 | CIdxr->setDisplayDiagnostics(); |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 2419 | |
| 2420 | if (getenv("LIBCLANG_BGPRIO_INDEX")) |
| 2421 | CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() | |
| 2422 | CXGlobalOpt_ThreadBackgroundPriorityForIndexing); |
| 2423 | if (getenv("LIBCLANG_BGPRIO_EDIT")) |
| 2424 | CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() | |
| 2425 | CXGlobalOpt_ThreadBackgroundPriorityForEditing); |
| 2426 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2427 | return CIdxr; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2430 | void clang_disposeIndex(CXIndex CIdx) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2431 | if (CIdx) |
| 2432 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 2435 | void clang_CXIndex_setGlobalOptions(CXIndex CIdx, unsigned options) { |
| 2436 | if (CIdx) |
| 2437 | static_cast<CIndexer *>(CIdx)->setCXGlobalOptFlags(options); |
| 2438 | } |
| 2439 | |
| 2440 | unsigned clang_CXIndex_getGlobalOptions(CXIndex CIdx) { |
| 2441 | if (CIdx) |
| 2442 | return static_cast<CIndexer *>(CIdx)->getCXGlobalOptFlags(); |
| 2443 | return 0; |
| 2444 | } |
| 2445 | |
Ted Kremenek | d2427dd | 2011-03-18 23:05:39 +0000 | [diff] [blame] | 2446 | void clang_toggleCrashRecovery(unsigned isEnabled) { |
| 2447 | if (isEnabled) |
| 2448 | llvm::CrashRecoveryContext::Enable(); |
| 2449 | else |
| 2450 | llvm::CrashRecoveryContext::Disable(); |
| 2451 | } |
| 2452 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2453 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2454 | const char *ast_filename) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2455 | if (!CIdx) |
| 2456 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2457 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 2458 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 2459 | FileSystemOptions FileSystemOpts; |
| 2460 | FileSystemOpts.WorkingDir = CXXIdx->getWorkingDirectory(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2461 | |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 2462 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags; |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2463 | ASTUnit *TU = ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2464 | CXXIdx->getOnlyLocalDecls(), |
Argyrios Kyrtzidis | bef35c9 | 2012-03-07 01:51:17 +0000 | [diff] [blame] | 2465 | 0, 0, |
| 2466 | /*CaptureDiagnostics=*/true, |
| 2467 | /*AllowPCHWithCompilerErrors=*/true); |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 2468 | return MakeCXTranslationUnit(CXXIdx, TU); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2469 | } |
| 2470 | |
Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 2471 | unsigned clang_defaultEditingTranslationUnitOptions() { |
Douglas Gregor | 2a2c50b | 2010-09-27 05:49:58 +0000 | [diff] [blame] | 2472 | return CXTranslationUnit_PrecompiledPreamble | |
Douglas Gregor | b5af843 | 2011-08-25 22:54:01 +0000 | [diff] [blame] | 2473 | CXTranslationUnit_CacheCompletionResults; |
Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2476 | CXTranslationUnit |
| 2477 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 2478 | const char *source_filename, |
| 2479 | int num_command_line_args, |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2480 | const char * const *command_line_args, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2481 | unsigned num_unsaved_files, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2482 | struct CXUnsavedFile *unsaved_files) { |
Argyrios Kyrtzidis | e1d4330 | 2012-02-25 02:41:16 +0000 | [diff] [blame] | 2483 | unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord; |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2484 | return clang_parseTranslationUnit(CIdx, source_filename, |
| 2485 | command_line_args, num_command_line_args, |
| 2486 | unsaved_files, num_unsaved_files, |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 2487 | Options); |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2488 | } |
Argyrios Kyrtzidis | e722ed6 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 2489 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2490 | struct ParseTranslationUnitInfo { |
| 2491 | CXIndex CIdx; |
| 2492 | const char *source_filename; |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2493 | const char *const *command_line_args; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2494 | int num_command_line_args; |
| 2495 | struct CXUnsavedFile *unsaved_files; |
| 2496 | unsigned num_unsaved_files; |
| 2497 | unsigned options; |
| 2498 | CXTranslationUnit result; |
| 2499 | }; |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2500 | static void clang_parseTranslationUnit_Impl(void *UserData) { |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2501 | ParseTranslationUnitInfo *PTUI = |
| 2502 | static_cast<ParseTranslationUnitInfo*>(UserData); |
| 2503 | CXIndex CIdx = PTUI->CIdx; |
| 2504 | const char *source_filename = PTUI->source_filename; |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2505 | const char * const *command_line_args = PTUI->command_line_args; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2506 | int num_command_line_args = PTUI->num_command_line_args; |
| 2507 | struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files; |
| 2508 | unsigned num_unsaved_files = PTUI->num_unsaved_files; |
| 2509 | unsigned options = PTUI->options; |
| 2510 | PTUI->result = 0; |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2511 | |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2512 | if (!CIdx) |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2513 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2514 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2515 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 2516 | |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 2517 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
Argyrios Kyrtzidis | 81b5ac3 | 2012-03-28 02:49:54 +0000 | [diff] [blame] | 2518 | setThreadBackgroundPriority(); |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 2519 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2520 | bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 2521 | // FIXME: Add a flag for modules. |
| 2522 | TranslationUnitKind TUKind |
| 2523 | = (options & CXTranslationUnit_Incomplete)? TU_Prefix : TU_Complete; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2524 | bool CacheCodeCompetionResults |
| 2525 | = options & CXTranslationUnit_CacheCompletionResults; |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 2526 | bool SkipFunctionBodies = options & CXTranslationUnit_SkipFunctionBodies; |
| 2527 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2528 | // Configure the diagnostics. |
| 2529 | DiagnosticOptions DiagOpts; |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 2530 | IntrusiveRefCntPtr<DiagnosticsEngine> |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2531 | Diags(CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args, |
| 2532 | command_line_args)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2533 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2534 | // Recover resources if we crash before exiting this function. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2535 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 2536 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2537 | DiagCleanup(Diags.getPtr()); |
| 2538 | |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 2539 | OwningPtr<std::vector<ASTUnit::RemappedFile> > |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2540 | RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); |
| 2541 | |
| 2542 | // Recover resources if we crash before exiting this function. |
| 2543 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 2544 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 2545 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2546 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2547 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2548 | const llvm::MemoryBuffer *Buffer |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 2549 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2550 | RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, |
| 2551 | Buffer)); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2552 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2553 | |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 2554 | OwningPtr<std::vector<const char *> > |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2555 | Args(new std::vector<const char*>()); |
| 2556 | |
| 2557 | // Recover resources if we crash before exiting this method. |
| 2558 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > |
| 2559 | ArgsCleanup(Args.get()); |
| 2560 | |
Douglas Gregor | 52ddc5d | 2010-07-09 18:39:07 +0000 | [diff] [blame] | 2561 | // Since the Clang C library is primarily used by batch tools dealing with |
| 2562 | // (often very broken) source code, where spell-checking can have a |
| 2563 | // significant negative impact on performance (particularly when |
| 2564 | // precompiled headers are involved), we disable it by default. |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2565 | // Only do this if we haven't found a spell-checking-related argument. |
| 2566 | bool FoundSpellCheckingArgument = false; |
| 2567 | for (int I = 0; I != num_command_line_args; ++I) { |
| 2568 | if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || |
| 2569 | strcmp(command_line_args[I], "-fspell-checking") == 0) { |
| 2570 | FoundSpellCheckingArgument = true; |
| 2571 | break; |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2572 | } |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2573 | } |
| 2574 | if (!FoundSpellCheckingArgument) |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2575 | Args->push_back("-fno-spell-checking"); |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2576 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2577 | Args->insert(Args->end(), command_line_args, |
| 2578 | command_line_args + num_command_line_args); |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 2579 | |
Argyrios Kyrtzidis | c842955 | 2011-03-20 18:17:52 +0000 | [diff] [blame] | 2580 | // The 'source_filename' argument is optional. If the caller does not |
| 2581 | // specify it then it is assumed that the source file is specified |
| 2582 | // in the actual argument list. |
| 2583 | // Put the source file after command_line_args otherwise if '-x' flag is |
| 2584 | // present it will be unused. |
| 2585 | if (source_filename) |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2586 | Args->push_back(source_filename); |
Argyrios Kyrtzidis | c842955 | 2011-03-20 18:17:52 +0000 | [diff] [blame] | 2587 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2588 | // Do we need the detailed preprocessing record? |
| 2589 | if (options & CXTranslationUnit_DetailedPreprocessingRecord) { |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2590 | Args->push_back("-Xclang"); |
| 2591 | Args->push_back("-detailed-preprocessing-record"); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2592 | } |
| 2593 | |
Argyrios Kyrtzidis | 026f691 | 2010-11-18 21:47:04 +0000 | [diff] [blame] | 2594 | unsigned NumErrors = Diags->getClient()->getNumErrors(); |
Argyrios Kyrtzidis | e722ed6 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 2595 | OwningPtr<ASTUnit> ErrUnit; |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 2596 | OwningPtr<ASTUnit> Unit( |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2597 | ASTUnit::LoadFromCommandLine(Args->size() ? &(*Args)[0] : 0 |
| 2598 | /* vector::data() not portable */, |
| 2599 | Args->size() ? (&(*Args)[0] + Args->size()) :0, |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2600 | Diags, |
| 2601 | CXXIdx->getClangResourcesPath(), |
| 2602 | CXXIdx->getOnlyLocalDecls(), |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 2603 | /*CaptureDiagnostics=*/true, |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2604 | RemappedFiles->size() ? &(*RemappedFiles)[0]:0, |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2605 | RemappedFiles->size(), |
Argyrios Kyrtzidis | 299a4a9 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 2606 | /*RemappedFilesKeepOriginalName=*/true, |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2607 | PrecompilePreamble, |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 2608 | TUKind, |
Argyrios Kyrtzidis | bef35c9 | 2012-03-07 01:51:17 +0000 | [diff] [blame] | 2609 | CacheCodeCompetionResults, |
Argyrios Kyrtzidis | e722ed6 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 2610 | /*AllowPCHWithCompilerErrors=*/true, |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 2611 | SkipFunctionBodies, |
Argyrios Kyrtzidis | e722ed6 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 2612 | &ErrUnit)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2613 | |
Argyrios Kyrtzidis | 026f691 | 2010-11-18 21:47:04 +0000 | [diff] [blame] | 2614 | if (NumErrors != Diags->getClient()->getNumErrors()) { |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2615 | // Make sure to check that 'Unit' is non-NULL. |
Argyrios Kyrtzidis | e722ed6 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 2616 | if (CXXIdx->getDisplayDiagnostics()) |
| 2617 | printDiagsToStderr(Unit ? Unit.get() : ErrUnit.get()); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2618 | } |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 2619 | |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 2620 | PTUI->result = MakeCXTranslationUnit(CXXIdx, Unit.take()); |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2621 | } |
| 2622 | CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, |
| 2623 | const char *source_filename, |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2624 | const char * const *command_line_args, |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2625 | int num_command_line_args, |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2626 | struct CXUnsavedFile *unsaved_files, |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2627 | unsigned num_unsaved_files, |
| 2628 | unsigned options) { |
| 2629 | ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args, |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2630 | num_command_line_args, unsaved_files, |
| 2631 | num_unsaved_files, options, 0 }; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2632 | llvm::CrashRecoveryContext CRC; |
| 2633 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 2634 | if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) { |
Daniel Dunbar | 60a4543 | 2010-08-23 22:35:34 +0000 | [diff] [blame] | 2635 | fprintf(stderr, "libclang: crash detected during parsing: {\n"); |
| 2636 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 2637 | fprintf(stderr, " 'command_line_args' : ["); |
| 2638 | for (int i = 0; i != num_command_line_args; ++i) { |
| 2639 | if (i) |
| 2640 | fprintf(stderr, ", "); |
| 2641 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 2642 | } |
| 2643 | fprintf(stderr, "],\n"); |
| 2644 | fprintf(stderr, " 'unsaved_files' : ["); |
| 2645 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 2646 | if (i) |
| 2647 | fprintf(stderr, ", "); |
| 2648 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 2649 | unsaved_files[i].Length); |
| 2650 | } |
| 2651 | fprintf(stderr, "],\n"); |
| 2652 | fprintf(stderr, " 'options' : %d,\n", options); |
| 2653 | fprintf(stderr, "}\n"); |
| 2654 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2655 | return 0; |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2656 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
| 2657 | PrintLibclangResourceUsage(PTUI.result); |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2658 | } |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2659 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2660 | return PTUI.result; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
Douglas Gregor | 1999844 | 2010-08-13 15:35:05 +0000 | [diff] [blame] | 2663 | unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { |
| 2664 | return CXSaveTranslationUnit_None; |
| 2665 | } |
Argyrios Kyrtzidis | 142bcb5 | 2012-03-28 02:17:59 +0000 | [diff] [blame] | 2666 | |
| 2667 | namespace { |
| 2668 | |
| 2669 | struct SaveTranslationUnitInfo { |
| 2670 | CXTranslationUnit TU; |
| 2671 | const char *FileName; |
| 2672 | unsigned options; |
| 2673 | CXSaveError result; |
| 2674 | }; |
| 2675 | |
| 2676 | } |
| 2677 | |
| 2678 | static void clang_saveTranslationUnit_Impl(void *UserData) { |
| 2679 | SaveTranslationUnitInfo *STUI = |
| 2680 | static_cast<SaveTranslationUnitInfo*>(UserData); |
| 2681 | |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 2682 | CIndexer *CXXIdx = (CIndexer*)STUI->TU->CIdx; |
| 2683 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
Argyrios Kyrtzidis | 81b5ac3 | 2012-03-28 02:49:54 +0000 | [diff] [blame] | 2684 | setThreadBackgroundPriority(); |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 2685 | |
Argyrios Kyrtzidis | 142bcb5 | 2012-03-28 02:17:59 +0000 | [diff] [blame] | 2686 | STUI->result = static_cast<ASTUnit *>(STUI->TU->TUData)->Save(STUI->FileName); |
| 2687 | } |
| 2688 | |
Douglas Gregor | 1999844 | 2010-08-13 15:35:05 +0000 | [diff] [blame] | 2689 | int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, |
| 2690 | unsigned options) { |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2691 | if (!TU) |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 2692 | return CXSaveError_InvalidTU; |
Argyrios Kyrtzidis | 142bcb5 | 2012-03-28 02:17:59 +0000 | [diff] [blame] | 2693 | |
| 2694 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 2695 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Argyrios Kyrtzidis | 374a00b | 2012-06-08 05:48:06 +0000 | [diff] [blame] | 2696 | if (!CXXUnit->hasSema()) |
| 2697 | return CXSaveError_InvalidTU; |
Argyrios Kyrtzidis | 142bcb5 | 2012-03-28 02:17:59 +0000 | [diff] [blame] | 2698 | |
| 2699 | SaveTranslationUnitInfo STUI = { TU, FileName, options, CXSaveError_None }; |
| 2700 | |
| 2701 | if (!CXXUnit->getDiagnostics().hasUnrecoverableErrorOccurred() || |
| 2702 | getenv("LIBCLANG_NOTHREADS")) { |
| 2703 | clang_saveTranslationUnit_Impl(&STUI); |
| 2704 | |
| 2705 | if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 2706 | PrintLibclangResourceUsage(TU); |
| 2707 | |
| 2708 | return STUI.result; |
| 2709 | } |
| 2710 | |
| 2711 | // We have an AST that has invalid nodes due to compiler errors. |
| 2712 | // Use a crash recovery thread for protection. |
| 2713 | |
| 2714 | llvm::CrashRecoveryContext CRC; |
| 2715 | |
| 2716 | if (!RunSafely(CRC, clang_saveTranslationUnit_Impl, &STUI)) { |
| 2717 | fprintf(stderr, "libclang: crash detected during AST saving: {\n"); |
| 2718 | fprintf(stderr, " 'filename' : '%s'\n", FileName); |
| 2719 | fprintf(stderr, " 'options' : %d,\n", options); |
| 2720 | fprintf(stderr, "}\n"); |
| 2721 | |
| 2722 | return CXSaveError_Unknown; |
| 2723 | |
| 2724 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2725 | PrintLibclangResourceUsage(TU); |
Argyrios Kyrtzidis | 142bcb5 | 2012-03-28 02:17:59 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
| 2728 | return STUI.result; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2729 | } |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2730 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2731 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2732 | if (CTUnit) { |
| 2733 | // If the translation unit has been marked as unsafe to free, just discard |
| 2734 | // it. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2735 | if (static_cast<ASTUnit *>(CTUnit->TUData)->isUnsafeToFree()) |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2736 | return; |
| 2737 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2738 | delete static_cast<ASTUnit *>(CTUnit->TUData); |
| 2739 | disposeCXStringPool(CTUnit->StringPool); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 2740 | delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics); |
Ted Kremenek | bbf66ca | 2012-04-30 19:06:49 +0000 | [diff] [blame] | 2741 | disposeOverridenCXCursorsPool(CTUnit->OverridenCursorsPool); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2742 | delete CTUnit; |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2743 | } |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 2744 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2745 | |
Douglas Gregor | e1e13bf | 2010-08-11 15:58:42 +0000 | [diff] [blame] | 2746 | unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { |
| 2747 | return CXReparse_None; |
| 2748 | } |
| 2749 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2750 | struct ReparseTranslationUnitInfo { |
| 2751 | CXTranslationUnit TU; |
| 2752 | unsigned num_unsaved_files; |
| 2753 | struct CXUnsavedFile *unsaved_files; |
| 2754 | unsigned options; |
| 2755 | int result; |
| 2756 | }; |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2757 | |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2758 | static void clang_reparseTranslationUnit_Impl(void *UserData) { |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2759 | ReparseTranslationUnitInfo *RTUI = |
| 2760 | static_cast<ReparseTranslationUnitInfo*>(UserData); |
| 2761 | CXTranslationUnit TU = RTUI->TU; |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 2762 | |
| 2763 | // Reset the associated diagnostics. |
| 2764 | delete static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics); |
| 2765 | TU->Diagnostics = 0; |
| 2766 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2767 | unsigned num_unsaved_files = RTUI->num_unsaved_files; |
| 2768 | struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files; |
| 2769 | unsigned options = RTUI->options; |
| 2770 | (void) options; |
| 2771 | RTUI->result = 1; |
| 2772 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2773 | if (!TU) |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2774 | return; |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2775 | |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 2776 | CIndexer *CXXIdx = (CIndexer*)TU->CIdx; |
| 2777 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) |
Argyrios Kyrtzidis | 81b5ac3 | 2012-03-28 02:49:54 +0000 | [diff] [blame] | 2778 | setThreadBackgroundPriority(); |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 2779 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2780 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2781 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2782 | |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 2783 | OwningPtr<std::vector<ASTUnit::RemappedFile> > |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2784 | RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); |
| 2785 | |
| 2786 | // Recover resources if we crash before exiting this function. |
| 2787 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 2788 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 2789 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2790 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2791 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2792 | const llvm::MemoryBuffer *Buffer |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2793 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2794 | RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, |
| 2795 | Buffer)); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2798 | if (!CXXUnit->Reparse(RemappedFiles->size() ? &(*RemappedFiles)[0] : 0, |
| 2799 | RemappedFiles->size())) |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2800 | RTUI->result = 0; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2801 | } |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2802 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2803 | int clang_reparseTranslationUnit(CXTranslationUnit TU, |
| 2804 | unsigned num_unsaved_files, |
| 2805 | struct CXUnsavedFile *unsaved_files, |
| 2806 | unsigned options) { |
| 2807 | ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files, |
| 2808 | options, 0 }; |
Argyrios Kyrtzidis | 8c4b47e | 2011-10-28 22:54:33 +0000 | [diff] [blame] | 2809 | |
Argyrios Kyrtzidis | e7de9b4 | 2011-10-29 19:32:39 +0000 | [diff] [blame] | 2810 | if (getenv("LIBCLANG_NOTHREADS")) { |
Argyrios Kyrtzidis | 8c4b47e | 2011-10-28 22:54:33 +0000 | [diff] [blame] | 2811 | clang_reparseTranslationUnit_Impl(&RTUI); |
| 2812 | return RTUI.result; |
| 2813 | } |
| 2814 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2815 | llvm::CrashRecoveryContext CRC; |
| 2816 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 2817 | if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) { |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2818 | fprintf(stderr, "libclang: crash detected during reparsing\n"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2819 | static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true); |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2820 | return 1; |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2821 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 2822 | PrintLibclangResourceUsage(TU); |
Ted Kremenek | 1dfb26a | 2010-10-29 01:06:50 +0000 | [diff] [blame] | 2823 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2824 | return RTUI.result; |
| 2825 | } |
| 2826 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2827 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2828 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2829 | if (!CTUnit) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2830 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2831 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2832 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit->TUData); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2833 | return createCXString(CXXUnit->getOriginalSourceFileName(), true); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 2834 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 2835 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2836 | CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { |
Douglas Gregor | 8e5900c | 2012-04-30 23:41:16 +0000 | [diff] [blame] | 2837 | ASTUnit *CXXUnit = static_cast<ASTUnit*>(TU->TUData); |
| 2838 | return MakeCXCursor(CXXUnit->getASTContext().getTranslationUnitDecl(), TU); |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2841 | } // end: extern "C" |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2842 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2843 | //===----------------------------------------------------------------------===// |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2844 | // CXFile Operations. |
| 2845 | //===----------------------------------------------------------------------===// |
| 2846 | |
| 2847 | extern "C" { |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2848 | CXString clang_getFileName(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2849 | if (!SFile) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2850 | return createCXString((const char*)NULL); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2851 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2852 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2853 | return createCXString(FEnt->getName()); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
| 2856 | time_t clang_getFileTime(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2857 | if (!SFile) |
| 2858 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2859 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2860 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 2861 | return FEnt->getModificationTime(); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2862 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2863 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2864 | CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) { |
| 2865 | if (!tu) |
| 2866 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2867 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2868 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2869 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2870 | FileManager &FMgr = CXXUnit->getFileManager(); |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 2871 | return const_cast<FileEntry *>(FMgr.getFile(file_name)); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2872 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2873 | |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 2874 | unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit tu, CXFile file) { |
| 2875 | if (!tu || !file) |
| 2876 | return 0; |
| 2877 | |
| 2878 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
| 2879 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
| 2880 | return CXXUnit->getPreprocessor().getHeaderSearchInfo() |
| 2881 | .isFileMultipleIncludeGuarded(FEnt); |
| 2882 | } |
| 2883 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2884 | } // end: extern "C" |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2885 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2886 | //===----------------------------------------------------------------------===// |
| 2887 | // CXCursor Operations. |
| 2888 | //===----------------------------------------------------------------------===// |
| 2889 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2890 | static Decl *getDeclFromExpr(Stmt *E) { |
Argyrios Kyrtzidis | c295461 | 2011-09-12 22:17:26 +0000 | [diff] [blame] | 2891 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2892 | return getDeclFromExpr(CE->getSubExpr()); |
| 2893 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2894 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 2895 | return RefExpr->getDecl(); |
| 2896 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 2897 | return ME->getMemberDecl(); |
| 2898 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 2899 | return RE->getDecl(); |
Argyrios Kyrtzidis | b085d89 | 2012-03-30 00:19:18 +0000 | [diff] [blame] | 2900 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) { |
| 2901 | if (PRE->isExplicitProperty()) |
| 2902 | return PRE->getExplicitProperty(); |
| 2903 | // It could be messaging both getter and setter as in: |
| 2904 | // ++myobj.myprop; |
| 2905 | // in which case prefer to associate the setter since it is less obvious |
| 2906 | // from inspecting the source that the setter is going to get called. |
| 2907 | if (PRE->isMessagingSetter()) |
| 2908 | return PRE->getImplicitPropertySetter(); |
| 2909 | return PRE->getImplicitPropertyGetter(); |
| 2910 | } |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2911 | if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) |
| 2912 | return getDeclFromExpr(POE->getSyntacticForm()); |
| 2913 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) |
| 2914 | if (Expr *Src = OVE->getSourceExpr()) |
| 2915 | return getDeclFromExpr(Src); |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2916 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2917 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 2918 | return getDeclFromExpr(CE->getCallee()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2919 | if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 2920 | if (!CE->isElidable()) |
| 2921 | return CE->getConstructor(); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2922 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 2923 | return OME->getMethodDecl(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2924 | |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2925 | if (ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) |
| 2926 | return PE->getProtocol(); |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 2927 | if (SubstNonTypeTemplateParmPackExpr *NTTP |
| 2928 | = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E)) |
| 2929 | return NTTP->getParameterPack(); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2930 | if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
| 2931 | if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || |
| 2932 | isa<ParmVarDecl>(SizeOfPack->getPack())) |
| 2933 | return SizeOfPack->getPack(); |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2934 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2935 | return 0; |
| 2936 | } |
| 2937 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2938 | static SourceLocation getLocationFromExpr(Expr *E) { |
Argyrios Kyrtzidis | c295461 | 2011-09-12 22:17:26 +0000 | [diff] [blame] | 2939 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
| 2940 | return getLocationFromExpr(CE->getSubExpr()); |
| 2941 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2942 | if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
| 2943 | return /*FIXME:*/Msg->getLeftLoc(); |
| 2944 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 2945 | return DRE->getLocation(); |
| 2946 | if (MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
| 2947 | return Member->getMemberLoc(); |
| 2948 | if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
| 2949 | return Ivar->getLocation(); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2950 | if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
| 2951 | return SizeOfPack->getPackLoc(); |
Argyrios Kyrtzidis | d046952 | 2012-03-30 00:19:13 +0000 | [diff] [blame] | 2952 | if (ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) |
| 2953 | return PropRef->getLocation(); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2954 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2955 | return E->getLocStart(); |
| 2956 | } |
| 2957 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2958 | extern "C" { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2959 | |
| 2960 | unsigned clang_visitChildren(CXCursor parent, |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 2961 | CXCursorVisitor visitor, |
| 2962 | CXClientData client_data) { |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 2963 | CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, |
| 2964 | /*VisitPreprocessorLast=*/false); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 2965 | return CursorVis.VisitChildren(parent); |
| 2966 | } |
| 2967 | |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 2968 | #ifndef __has_feature |
| 2969 | #define __has_feature(x) 0 |
| 2970 | #endif |
| 2971 | #if __has_feature(blocks) |
| 2972 | typedef enum CXChildVisitResult |
| 2973 | (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent); |
| 2974 | |
| 2975 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 2976 | CXClientData client_data) { |
| 2977 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 2978 | return block(cursor, parent); |
| 2979 | } |
| 2980 | #else |
| 2981 | // If we are compiled with a compiler that doesn't have native blocks support, |
| 2982 | // define and call the block manually, so the |
| 2983 | typedef struct _CXChildVisitResult |
| 2984 | { |
| 2985 | void *isa; |
| 2986 | int flags; |
| 2987 | int reserved; |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2988 | enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, |
| 2989 | CXCursor); |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 2990 | } *CXCursorVisitorBlock; |
| 2991 | |
| 2992 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 2993 | CXClientData client_data) { |
| 2994 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 2995 | return block->invoke(block, cursor, parent); |
| 2996 | } |
| 2997 | #endif |
| 2998 | |
| 2999 | |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 3000 | unsigned clang_visitChildrenWithBlock(CXCursor parent, |
| 3001 | CXCursorVisitorBlock block) { |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 3002 | return clang_visitChildren(parent, visitWithBlock, block); |
| 3003 | } |
| 3004 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3005 | static CXString getDeclSpelling(Decl *D) { |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 3006 | if (!D) |
| 3007 | return createCXString(""); |
| 3008 | |
| 3009 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3010 | if (!ND) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3011 | if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3012 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 3013 | return createCXString(Property->getIdentifier()->getName()); |
| 3014 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3015 | return createCXString(""); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3018 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3019 | return createCXString(OMD->getSelector().getAsString()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3020 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3021 | if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
| 3022 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 3023 | // and returns different names. NamedDecl returns the class name and |
| 3024 | // ObjCCategoryImplDecl returns the category name. |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3025 | return createCXString(CIMP->getIdentifier()->getNameStart()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3026 | |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 3027 | if (isa<UsingDirectiveDecl>(D)) |
| 3028 | return createCXString(""); |
| 3029 | |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 3030 | SmallString<1024> S; |
Ted Kremenek | 50aa6ac | 2010-05-19 21:51:10 +0000 | [diff] [blame] | 3031 | llvm::raw_svector_ostream os(S); |
| 3032 | ND->printName(os); |
| 3033 | |
| 3034 | return createCXString(os.str()); |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3035 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3036 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3037 | CXString clang_getCursorSpelling(CXCursor C) { |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3038 | if (clang_isTranslationUnit(C.kind)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3039 | return clang_getTranslationUnitSpelling( |
| 3040 | static_cast<CXTranslationUnit>(C.data[2])); |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3041 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3042 | if (clang_isReference(C.kind)) { |
| 3043 | switch (C.kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3044 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 3045 | ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3046 | return createCXString(Super->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3047 | } |
| 3048 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 3049 | ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3050 | return createCXString(Class->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3051 | } |
| 3052 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 3053 | ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3054 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3055 | return createCXString(OID->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3056 | } |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3057 | case CXCursor_CXXBaseSpecifier: { |
| 3058 | CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); |
| 3059 | return createCXString(B->getType().getAsString()); |
| 3060 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3061 | case CXCursor_TypeRef: { |
| 3062 | TypeDecl *Type = getCursorTypeRef(C).first; |
| 3063 | assert(Type && "Missing type decl"); |
| 3064 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3065 | return createCXString(getCursorContext(C).getTypeDeclType(Type). |
| 3066 | getAsString()); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3067 | } |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3068 | case CXCursor_TemplateRef: { |
| 3069 | TemplateDecl *Template = getCursorTemplateRef(C).first; |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3070 | assert(Template && "Missing template decl"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3071 | |
| 3072 | return createCXString(Template->getNameAsString()); |
| 3073 | } |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3074 | |
| 3075 | case CXCursor_NamespaceRef: { |
| 3076 | NamedDecl *NS = getCursorNamespaceRef(C).first; |
| 3077 | assert(NS && "Missing namespace decl"); |
| 3078 | |
| 3079 | return createCXString(NS->getNameAsString()); |
| 3080 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3081 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3082 | case CXCursor_MemberRef: { |
| 3083 | FieldDecl *Field = getCursorMemberRef(C).first; |
| 3084 | assert(Field && "Missing member decl"); |
| 3085 | |
| 3086 | return createCXString(Field->getNameAsString()); |
| 3087 | } |
| 3088 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3089 | case CXCursor_LabelRef: { |
| 3090 | LabelStmt *Label = getCursorLabelRef(C).first; |
| 3091 | assert(Label && "Missing label"); |
| 3092 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3093 | return createCXString(Label->getName()); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3094 | } |
| 3095 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3096 | case CXCursor_OverloadedDeclRef: { |
| 3097 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
| 3098 | if (Decl *D = Storage.dyn_cast<Decl *>()) { |
| 3099 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 3100 | return createCXString(ND->getNameAsString()); |
| 3101 | return createCXString(""); |
| 3102 | } |
| 3103 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
| 3104 | return createCXString(E->getName().getAsString()); |
| 3105 | OverloadedTemplateStorage *Ovl |
| 3106 | = Storage.get<OverloadedTemplateStorage*>(); |
| 3107 | if (Ovl->size() == 0) |
| 3108 | return createCXString(""); |
| 3109 | return createCXString((*Ovl->begin())->getNameAsString()); |
| 3110 | } |
| 3111 | |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 3112 | case CXCursor_VariableRef: { |
| 3113 | VarDecl *Var = getCursorVariableRef(C).first; |
| 3114 | assert(Var && "Missing variable decl"); |
| 3115 | |
| 3116 | return createCXString(Var->getNameAsString()); |
| 3117 | } |
| 3118 | |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3119 | default: |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3120 | return createCXString("<not implemented>"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3121 | } |
| 3122 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3123 | |
| 3124 | if (clang_isExpression(C.kind)) { |
| 3125 | Decl *D = getDeclFromExpr(getCursorExpr(C)); |
| 3126 | if (D) |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3127 | return getDeclSpelling(D); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3128 | return createCXString(""); |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3131 | if (clang_isStatement(C.kind)) { |
| 3132 | Stmt *S = getCursorStmt(C); |
| 3133 | if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3134 | return createCXString(Label->getName()); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3135 | |
| 3136 | return createCXString(""); |
| 3137 | } |
| 3138 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3139 | if (C.kind == CXCursor_MacroExpansion) |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3140 | return createCXString(getCursorMacroExpansion(C)->getName() |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 3141 | ->getNameStart()); |
| 3142 | |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3143 | if (C.kind == CXCursor_MacroDefinition) |
| 3144 | return createCXString(getCursorMacroDefinition(C)->getName() |
| 3145 | ->getNameStart()); |
| 3146 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3147 | if (C.kind == CXCursor_InclusionDirective) |
| 3148 | return createCXString(getCursorInclusionDirective(C)->getFileName()); |
| 3149 | |
Douglas Gregor | 60cbfac | 2010-01-25 16:56:17 +0000 | [diff] [blame] | 3150 | if (clang_isDeclaration(C.kind)) |
| 3151 | return getDeclSpelling(getCursorDecl(C)); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3152 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 3153 | if (C.kind == CXCursor_AnnotateAttr) { |
| 3154 | AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C)); |
| 3155 | return createCXString(AA->getAnnotation()); |
| 3156 | } |
| 3157 | |
Argyrios Kyrtzidis | 84b7964 | 2011-12-06 22:05:01 +0000 | [diff] [blame] | 3158 | if (C.kind == CXCursor_AsmLabelAttr) { |
| 3159 | AsmLabelAttr *AA = cast<AsmLabelAttr>(cxcursor::getCursorAttr(C)); |
| 3160 | return createCXString(AA->getLabel()); |
| 3161 | } |
| 3162 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3163 | return createCXString(""); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
Argyrios Kyrtzidis | ba1da14 | 2012-03-30 20:58:35 +0000 | [diff] [blame] | 3166 | CXSourceRange clang_Cursor_getSpellingNameRange(CXCursor C, |
| 3167 | unsigned pieceIndex, |
| 3168 | unsigned options) { |
| 3169 | if (clang_Cursor_isNull(C)) |
| 3170 | return clang_getNullRange(); |
| 3171 | |
| 3172 | ASTContext &Ctx = getCursorContext(C); |
| 3173 | |
| 3174 | if (clang_isStatement(C.kind)) { |
| 3175 | Stmt *S = getCursorStmt(C); |
| 3176 | if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) { |
| 3177 | if (pieceIndex > 0) |
| 3178 | return clang_getNullRange(); |
| 3179 | return cxloc::translateSourceRange(Ctx, Label->getIdentLoc()); |
| 3180 | } |
| 3181 | |
| 3182 | return clang_getNullRange(); |
| 3183 | } |
| 3184 | |
| 3185 | if (C.kind == CXCursor_ObjCMessageExpr) { |
| 3186 | if (ObjCMessageExpr * |
| 3187 | ME = dyn_cast_or_null<ObjCMessageExpr>(getCursorExpr(C))) { |
| 3188 | if (pieceIndex >= ME->getNumSelectorLocs()) |
| 3189 | return clang_getNullRange(); |
| 3190 | return cxloc::translateSourceRange(Ctx, ME->getSelectorLoc(pieceIndex)); |
| 3191 | } |
| 3192 | } |
| 3193 | |
| 3194 | if (C.kind == CXCursor_ObjCInstanceMethodDecl || |
| 3195 | C.kind == CXCursor_ObjCClassMethodDecl) { |
| 3196 | if (ObjCMethodDecl * |
| 3197 | MD = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(C))) { |
| 3198 | if (pieceIndex >= MD->getNumSelectorLocs()) |
| 3199 | return clang_getNullRange(); |
| 3200 | return cxloc::translateSourceRange(Ctx, MD->getSelectorLoc(pieceIndex)); |
| 3201 | } |
| 3202 | } |
| 3203 | |
Argyrios Kyrtzidis | 9482a18 | 2012-04-16 20:01:28 +0000 | [diff] [blame] | 3204 | if (C.kind == CXCursor_ObjCCategoryDecl || |
| 3205 | C.kind == CXCursor_ObjCCategoryImplDecl) { |
| 3206 | if (pieceIndex > 0) |
| 3207 | return clang_getNullRange(); |
| 3208 | if (ObjCCategoryDecl * |
| 3209 | CD = dyn_cast_or_null<ObjCCategoryDecl>(getCursorDecl(C))) |
| 3210 | return cxloc::translateSourceRange(Ctx, CD->getCategoryNameLoc()); |
| 3211 | if (ObjCCategoryImplDecl * |
| 3212 | CID = dyn_cast_or_null<ObjCCategoryImplDecl>(getCursorDecl(C))) |
| 3213 | return cxloc::translateSourceRange(Ctx, CID->getCategoryNameLoc()); |
| 3214 | } |
| 3215 | |
Argyrios Kyrtzidis | ba1da14 | 2012-03-30 20:58:35 +0000 | [diff] [blame] | 3216 | // FIXME: A CXCursor_InclusionDirective should give the location of the |
| 3217 | // filename, but we don't keep track of this. |
| 3218 | |
| 3219 | // FIXME: A CXCursor_AnnotateAttr should give the location of the annotation |
| 3220 | // but we don't keep track of this. |
| 3221 | |
| 3222 | // FIXME: A CXCursor_AsmLabelAttr should give the location of the label |
| 3223 | // but we don't keep track of this. |
| 3224 | |
| 3225 | // Default handling, give the location of the cursor. |
| 3226 | |
| 3227 | if (pieceIndex > 0) |
| 3228 | return clang_getNullRange(); |
| 3229 | |
| 3230 | CXSourceLocation CXLoc = clang_getCursorLocation(C); |
| 3231 | SourceLocation Loc = cxloc::translateSourceLocation(CXLoc); |
| 3232 | return cxloc::translateSourceRange(Ctx, Loc); |
| 3233 | } |
| 3234 | |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3235 | CXString clang_getCursorDisplayName(CXCursor C) { |
| 3236 | if (!clang_isDeclaration(C.kind)) |
| 3237 | return clang_getCursorSpelling(C); |
| 3238 | |
| 3239 | Decl *D = getCursorDecl(C); |
| 3240 | if (!D) |
| 3241 | return createCXString(""); |
| 3242 | |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3243 | PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy(); |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3244 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
| 3245 | D = FunTmpl->getTemplatedDecl(); |
| 3246 | |
| 3247 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 3248 | SmallString<64> Str; |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3249 | llvm::raw_svector_ostream OS(Str); |
Benjamin Kramer | a59d20b | 2012-02-07 11:57:57 +0000 | [diff] [blame] | 3250 | OS << *Function; |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3251 | if (Function->getPrimaryTemplate()) |
| 3252 | OS << "<>"; |
| 3253 | OS << "("; |
| 3254 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { |
| 3255 | if (I) |
| 3256 | OS << ", "; |
| 3257 | OS << Function->getParamDecl(I)->getType().getAsString(Policy); |
| 3258 | } |
| 3259 | |
| 3260 | if (Function->isVariadic()) { |
| 3261 | if (Function->getNumParams()) |
| 3262 | OS << ", "; |
| 3263 | OS << "..."; |
| 3264 | } |
| 3265 | OS << ")"; |
| 3266 | return createCXString(OS.str()); |
| 3267 | } |
| 3268 | |
| 3269 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 3270 | SmallString<64> Str; |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3271 | llvm::raw_svector_ostream OS(Str); |
Benjamin Kramer | a59d20b | 2012-02-07 11:57:57 +0000 | [diff] [blame] | 3272 | OS << *ClassTemplate; |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3273 | OS << "<"; |
| 3274 | TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); |
| 3275 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 3276 | if (I) |
| 3277 | OS << ", "; |
| 3278 | |
| 3279 | NamedDecl *Param = Params->getParam(I); |
| 3280 | if (Param->getIdentifier()) { |
| 3281 | OS << Param->getIdentifier()->getName(); |
| 3282 | continue; |
| 3283 | } |
| 3284 | |
| 3285 | // There is no parameter name, which makes this tricky. Try to come up |
| 3286 | // with something useful that isn't too long. |
| 3287 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 3288 | OS << (TTP->wasDeclaredWithTypename()? "typename" : "class"); |
| 3289 | else if (NonTypeTemplateParmDecl *NTTP |
| 3290 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 3291 | OS << NTTP->getType().getAsString(Policy); |
| 3292 | else |
| 3293 | OS << "template<...> class"; |
| 3294 | } |
| 3295 | |
| 3296 | OS << ">"; |
| 3297 | return createCXString(OS.str()); |
| 3298 | } |
| 3299 | |
| 3300 | if (ClassTemplateSpecializationDecl *ClassSpec |
| 3301 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 3302 | // If the type was explicitly written, use that. |
| 3303 | if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) |
| 3304 | return createCXString(TSInfo->getType().getAsString(Policy)); |
| 3305 | |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 3306 | SmallString<64> Str; |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3307 | llvm::raw_svector_ostream OS(Str); |
Benjamin Kramer | a59d20b | 2012-02-07 11:57:57 +0000 | [diff] [blame] | 3308 | OS << *ClassSpec; |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3309 | OS << TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3310 | ClassSpec->getTemplateArgs().data(), |
| 3311 | ClassSpec->getTemplateArgs().size(), |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3312 | Policy); |
| 3313 | return createCXString(OS.str()); |
| 3314 | } |
| 3315 | |
| 3316 | return clang_getCursorSpelling(C); |
| 3317 | } |
| 3318 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3319 | CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3320 | switch (Kind) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3321 | case CXCursor_FunctionDecl: |
| 3322 | return createCXString("FunctionDecl"); |
| 3323 | case CXCursor_TypedefDecl: |
| 3324 | return createCXString("TypedefDecl"); |
| 3325 | case CXCursor_EnumDecl: |
| 3326 | return createCXString("EnumDecl"); |
| 3327 | case CXCursor_EnumConstantDecl: |
| 3328 | return createCXString("EnumConstantDecl"); |
| 3329 | case CXCursor_StructDecl: |
| 3330 | return createCXString("StructDecl"); |
| 3331 | case CXCursor_UnionDecl: |
| 3332 | return createCXString("UnionDecl"); |
| 3333 | case CXCursor_ClassDecl: |
| 3334 | return createCXString("ClassDecl"); |
| 3335 | case CXCursor_FieldDecl: |
| 3336 | return createCXString("FieldDecl"); |
| 3337 | case CXCursor_VarDecl: |
| 3338 | return createCXString("VarDecl"); |
| 3339 | case CXCursor_ParmDecl: |
| 3340 | return createCXString("ParmDecl"); |
| 3341 | case CXCursor_ObjCInterfaceDecl: |
| 3342 | return createCXString("ObjCInterfaceDecl"); |
| 3343 | case CXCursor_ObjCCategoryDecl: |
| 3344 | return createCXString("ObjCCategoryDecl"); |
| 3345 | case CXCursor_ObjCProtocolDecl: |
| 3346 | return createCXString("ObjCProtocolDecl"); |
| 3347 | case CXCursor_ObjCPropertyDecl: |
| 3348 | return createCXString("ObjCPropertyDecl"); |
| 3349 | case CXCursor_ObjCIvarDecl: |
| 3350 | return createCXString("ObjCIvarDecl"); |
| 3351 | case CXCursor_ObjCInstanceMethodDecl: |
| 3352 | return createCXString("ObjCInstanceMethodDecl"); |
| 3353 | case CXCursor_ObjCClassMethodDecl: |
| 3354 | return createCXString("ObjCClassMethodDecl"); |
| 3355 | case CXCursor_ObjCImplementationDecl: |
| 3356 | return createCXString("ObjCImplementationDecl"); |
| 3357 | case CXCursor_ObjCCategoryImplDecl: |
| 3358 | return createCXString("ObjCCategoryImplDecl"); |
Ted Kremenek | 8bd5a69 | 2010-04-13 23:39:06 +0000 | [diff] [blame] | 3359 | case CXCursor_CXXMethod: |
| 3360 | return createCXString("CXXMethod"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3361 | case CXCursor_UnexposedDecl: |
| 3362 | return createCXString("UnexposedDecl"); |
| 3363 | case CXCursor_ObjCSuperClassRef: |
| 3364 | return createCXString("ObjCSuperClassRef"); |
| 3365 | case CXCursor_ObjCProtocolRef: |
| 3366 | return createCXString("ObjCProtocolRef"); |
| 3367 | case CXCursor_ObjCClassRef: |
| 3368 | return createCXString("ObjCClassRef"); |
| 3369 | case CXCursor_TypeRef: |
| 3370 | return createCXString("TypeRef"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3371 | case CXCursor_TemplateRef: |
| 3372 | return createCXString("TemplateRef"); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3373 | case CXCursor_NamespaceRef: |
| 3374 | return createCXString("NamespaceRef"); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3375 | case CXCursor_MemberRef: |
| 3376 | return createCXString("MemberRef"); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3377 | case CXCursor_LabelRef: |
| 3378 | return createCXString("LabelRef"); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3379 | case CXCursor_OverloadedDeclRef: |
| 3380 | return createCXString("OverloadedDeclRef"); |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 3381 | case CXCursor_VariableRef: |
| 3382 | return createCXString("VariableRef"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3383 | case CXCursor_IntegerLiteral: |
| 3384 | return createCXString("IntegerLiteral"); |
| 3385 | case CXCursor_FloatingLiteral: |
| 3386 | return createCXString("FloatingLiteral"); |
| 3387 | case CXCursor_ImaginaryLiteral: |
| 3388 | return createCXString("ImaginaryLiteral"); |
| 3389 | case CXCursor_StringLiteral: |
| 3390 | return createCXString("StringLiteral"); |
| 3391 | case CXCursor_CharacterLiteral: |
| 3392 | return createCXString("CharacterLiteral"); |
| 3393 | case CXCursor_ParenExpr: |
| 3394 | return createCXString("ParenExpr"); |
| 3395 | case CXCursor_UnaryOperator: |
| 3396 | return createCXString("UnaryOperator"); |
| 3397 | case CXCursor_ArraySubscriptExpr: |
| 3398 | return createCXString("ArraySubscriptExpr"); |
| 3399 | case CXCursor_BinaryOperator: |
| 3400 | return createCXString("BinaryOperator"); |
| 3401 | case CXCursor_CompoundAssignOperator: |
| 3402 | return createCXString("CompoundAssignOperator"); |
| 3403 | case CXCursor_ConditionalOperator: |
| 3404 | return createCXString("ConditionalOperator"); |
| 3405 | case CXCursor_CStyleCastExpr: |
| 3406 | return createCXString("CStyleCastExpr"); |
| 3407 | case CXCursor_CompoundLiteralExpr: |
| 3408 | return createCXString("CompoundLiteralExpr"); |
| 3409 | case CXCursor_InitListExpr: |
| 3410 | return createCXString("InitListExpr"); |
| 3411 | case CXCursor_AddrLabelExpr: |
| 3412 | return createCXString("AddrLabelExpr"); |
| 3413 | case CXCursor_StmtExpr: |
| 3414 | return createCXString("StmtExpr"); |
| 3415 | case CXCursor_GenericSelectionExpr: |
| 3416 | return createCXString("GenericSelectionExpr"); |
| 3417 | case CXCursor_GNUNullExpr: |
| 3418 | return createCXString("GNUNullExpr"); |
| 3419 | case CXCursor_CXXStaticCastExpr: |
| 3420 | return createCXString("CXXStaticCastExpr"); |
| 3421 | case CXCursor_CXXDynamicCastExpr: |
| 3422 | return createCXString("CXXDynamicCastExpr"); |
| 3423 | case CXCursor_CXXReinterpretCastExpr: |
| 3424 | return createCXString("CXXReinterpretCastExpr"); |
| 3425 | case CXCursor_CXXConstCastExpr: |
| 3426 | return createCXString("CXXConstCastExpr"); |
| 3427 | case CXCursor_CXXFunctionalCastExpr: |
| 3428 | return createCXString("CXXFunctionalCastExpr"); |
| 3429 | case CXCursor_CXXTypeidExpr: |
| 3430 | return createCXString("CXXTypeidExpr"); |
| 3431 | case CXCursor_CXXBoolLiteralExpr: |
| 3432 | return createCXString("CXXBoolLiteralExpr"); |
| 3433 | case CXCursor_CXXNullPtrLiteralExpr: |
| 3434 | return createCXString("CXXNullPtrLiteralExpr"); |
| 3435 | case CXCursor_CXXThisExpr: |
| 3436 | return createCXString("CXXThisExpr"); |
| 3437 | case CXCursor_CXXThrowExpr: |
| 3438 | return createCXString("CXXThrowExpr"); |
| 3439 | case CXCursor_CXXNewExpr: |
| 3440 | return createCXString("CXXNewExpr"); |
| 3441 | case CXCursor_CXXDeleteExpr: |
| 3442 | return createCXString("CXXDeleteExpr"); |
| 3443 | case CXCursor_UnaryExpr: |
| 3444 | return createCXString("UnaryExpr"); |
| 3445 | case CXCursor_ObjCStringLiteral: |
| 3446 | return createCXString("ObjCStringLiteral"); |
Ted Kremenek | b3f7542 | 2012-03-06 20:06:06 +0000 | [diff] [blame] | 3447 | case CXCursor_ObjCBoolLiteralExpr: |
| 3448 | return createCXString("ObjCBoolLiteralExpr"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3449 | case CXCursor_ObjCEncodeExpr: |
| 3450 | return createCXString("ObjCEncodeExpr"); |
| 3451 | case CXCursor_ObjCSelectorExpr: |
| 3452 | return createCXString("ObjCSelectorExpr"); |
| 3453 | case CXCursor_ObjCProtocolExpr: |
| 3454 | return createCXString("ObjCProtocolExpr"); |
| 3455 | case CXCursor_ObjCBridgedCastExpr: |
| 3456 | return createCXString("ObjCBridgedCastExpr"); |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 3457 | case CXCursor_BlockExpr: |
| 3458 | return createCXString("BlockExpr"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3459 | case CXCursor_PackExpansionExpr: |
| 3460 | return createCXString("PackExpansionExpr"); |
| 3461 | case CXCursor_SizeOfPackExpr: |
| 3462 | return createCXString("SizeOfPackExpr"); |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 3463 | case CXCursor_LambdaExpr: |
| 3464 | return createCXString("LambdaExpr"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3465 | case CXCursor_UnexposedExpr: |
| 3466 | return createCXString("UnexposedExpr"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3467 | case CXCursor_DeclRefExpr: |
| 3468 | return createCXString("DeclRefExpr"); |
| 3469 | case CXCursor_MemberRefExpr: |
| 3470 | return createCXString("MemberRefExpr"); |
| 3471 | case CXCursor_CallExpr: |
| 3472 | return createCXString("CallExpr"); |
| 3473 | case CXCursor_ObjCMessageExpr: |
| 3474 | return createCXString("ObjCMessageExpr"); |
| 3475 | case CXCursor_UnexposedStmt: |
| 3476 | return createCXString("UnexposedStmt"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3477 | case CXCursor_DeclStmt: |
| 3478 | return createCXString("DeclStmt"); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3479 | case CXCursor_LabelStmt: |
| 3480 | return createCXString("LabelStmt"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3481 | case CXCursor_CompoundStmt: |
| 3482 | return createCXString("CompoundStmt"); |
| 3483 | case CXCursor_CaseStmt: |
| 3484 | return createCXString("CaseStmt"); |
| 3485 | case CXCursor_DefaultStmt: |
| 3486 | return createCXString("DefaultStmt"); |
| 3487 | case CXCursor_IfStmt: |
| 3488 | return createCXString("IfStmt"); |
| 3489 | case CXCursor_SwitchStmt: |
| 3490 | return createCXString("SwitchStmt"); |
| 3491 | case CXCursor_WhileStmt: |
| 3492 | return createCXString("WhileStmt"); |
| 3493 | case CXCursor_DoStmt: |
| 3494 | return createCXString("DoStmt"); |
| 3495 | case CXCursor_ForStmt: |
| 3496 | return createCXString("ForStmt"); |
| 3497 | case CXCursor_GotoStmt: |
| 3498 | return createCXString("GotoStmt"); |
| 3499 | case CXCursor_IndirectGotoStmt: |
| 3500 | return createCXString("IndirectGotoStmt"); |
| 3501 | case CXCursor_ContinueStmt: |
| 3502 | return createCXString("ContinueStmt"); |
| 3503 | case CXCursor_BreakStmt: |
| 3504 | return createCXString("BreakStmt"); |
| 3505 | case CXCursor_ReturnStmt: |
| 3506 | return createCXString("ReturnStmt"); |
| 3507 | case CXCursor_AsmStmt: |
| 3508 | return createCXString("AsmStmt"); |
| 3509 | case CXCursor_ObjCAtTryStmt: |
| 3510 | return createCXString("ObjCAtTryStmt"); |
| 3511 | case CXCursor_ObjCAtCatchStmt: |
| 3512 | return createCXString("ObjCAtCatchStmt"); |
| 3513 | case CXCursor_ObjCAtFinallyStmt: |
| 3514 | return createCXString("ObjCAtFinallyStmt"); |
| 3515 | case CXCursor_ObjCAtThrowStmt: |
| 3516 | return createCXString("ObjCAtThrowStmt"); |
| 3517 | case CXCursor_ObjCAtSynchronizedStmt: |
| 3518 | return createCXString("ObjCAtSynchronizedStmt"); |
| 3519 | case CXCursor_ObjCAutoreleasePoolStmt: |
| 3520 | return createCXString("ObjCAutoreleasePoolStmt"); |
| 3521 | case CXCursor_ObjCForCollectionStmt: |
| 3522 | return createCXString("ObjCForCollectionStmt"); |
| 3523 | case CXCursor_CXXCatchStmt: |
| 3524 | return createCXString("CXXCatchStmt"); |
| 3525 | case CXCursor_CXXTryStmt: |
| 3526 | return createCXString("CXXTryStmt"); |
| 3527 | case CXCursor_CXXForRangeStmt: |
| 3528 | return createCXString("CXXForRangeStmt"); |
| 3529 | case CXCursor_SEHTryStmt: |
| 3530 | return createCXString("SEHTryStmt"); |
| 3531 | case CXCursor_SEHExceptStmt: |
| 3532 | return createCXString("SEHExceptStmt"); |
| 3533 | case CXCursor_SEHFinallyStmt: |
| 3534 | return createCXString("SEHFinallyStmt"); |
| 3535 | case CXCursor_NullStmt: |
| 3536 | return createCXString("NullStmt"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3537 | case CXCursor_InvalidFile: |
| 3538 | return createCXString("InvalidFile"); |
Ted Kremenek | 292db64 | 2010-03-19 20:39:05 +0000 | [diff] [blame] | 3539 | case CXCursor_InvalidCode: |
| 3540 | return createCXString("InvalidCode"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3541 | case CXCursor_NoDeclFound: |
| 3542 | return createCXString("NoDeclFound"); |
| 3543 | case CXCursor_NotImplemented: |
| 3544 | return createCXString("NotImplemented"); |
| 3545 | case CXCursor_TranslationUnit: |
| 3546 | return createCXString("TranslationUnit"); |
Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 3547 | case CXCursor_UnexposedAttr: |
| 3548 | return createCXString("UnexposedAttr"); |
| 3549 | case CXCursor_IBActionAttr: |
| 3550 | return createCXString("attribute(ibaction)"); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3551 | case CXCursor_IBOutletAttr: |
| 3552 | return createCXString("attribute(iboutlet)"); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 3553 | case CXCursor_IBOutletCollectionAttr: |
| 3554 | return createCXString("attribute(iboutletcollection)"); |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 3555 | case CXCursor_CXXFinalAttr: |
| 3556 | return createCXString("attribute(final)"); |
| 3557 | case CXCursor_CXXOverrideAttr: |
| 3558 | return createCXString("attribute(override)"); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 3559 | case CXCursor_AnnotateAttr: |
| 3560 | return createCXString("attribute(annotate)"); |
Argyrios Kyrtzidis | 84b7964 | 2011-12-06 22:05:01 +0000 | [diff] [blame] | 3561 | case CXCursor_AsmLabelAttr: |
| 3562 | return createCXString("asm label"); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3563 | case CXCursor_PreprocessingDirective: |
| 3564 | return createCXString("preprocessing directive"); |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3565 | case CXCursor_MacroDefinition: |
| 3566 | return createCXString("macro definition"); |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3567 | case CXCursor_MacroExpansion: |
| 3568 | return createCXString("macro expansion"); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3569 | case CXCursor_InclusionDirective: |
| 3570 | return createCXString("inclusion directive"); |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 3571 | case CXCursor_Namespace: |
| 3572 | return createCXString("Namespace"); |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 3573 | case CXCursor_LinkageSpec: |
| 3574 | return createCXString("LinkageSpec"); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3575 | case CXCursor_CXXBaseSpecifier: |
| 3576 | return createCXString("C++ base class specifier"); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 3577 | case CXCursor_Constructor: |
| 3578 | return createCXString("CXXConstructor"); |
| 3579 | case CXCursor_Destructor: |
| 3580 | return createCXString("CXXDestructor"); |
| 3581 | case CXCursor_ConversionFunction: |
| 3582 | return createCXString("CXXConversion"); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 3583 | case CXCursor_TemplateTypeParameter: |
| 3584 | return createCXString("TemplateTypeParameter"); |
| 3585 | case CXCursor_NonTypeTemplateParameter: |
| 3586 | return createCXString("NonTypeTemplateParameter"); |
| 3587 | case CXCursor_TemplateTemplateParameter: |
| 3588 | return createCXString("TemplateTemplateParameter"); |
| 3589 | case CXCursor_FunctionTemplate: |
| 3590 | return createCXString("FunctionTemplate"); |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 3591 | case CXCursor_ClassTemplate: |
| 3592 | return createCXString("ClassTemplate"); |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 3593 | case CXCursor_ClassTemplatePartialSpecialization: |
| 3594 | return createCXString("ClassTemplatePartialSpecialization"); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3595 | case CXCursor_NamespaceAlias: |
| 3596 | return createCXString("NamespaceAlias"); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 3597 | case CXCursor_UsingDirective: |
| 3598 | return createCXString("UsingDirective"); |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 3599 | case CXCursor_UsingDeclaration: |
| 3600 | return createCXString("UsingDeclaration"); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3601 | case CXCursor_TypeAliasDecl: |
Douglas Gregor | 352697a | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3602 | return createCXString("TypeAliasDecl"); |
| 3603 | case CXCursor_ObjCSynthesizeDecl: |
| 3604 | return createCXString("ObjCSynthesizeDecl"); |
| 3605 | case CXCursor_ObjCDynamicDecl: |
| 3606 | return createCXString("ObjCDynamicDecl"); |
Argyrios Kyrtzidis | 2dfdb94 | 2011-09-30 17:58:23 +0000 | [diff] [blame] | 3607 | case CXCursor_CXXAccessSpecifier: |
| 3608 | return createCXString("CXXAccessSpecifier"); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3609 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3610 | |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 3611 | llvm_unreachable("Unhandled CXCursorKind"); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 3612 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3613 | |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3614 | struct GetCursorData { |
| 3615 | SourceLocation TokenBeginLoc; |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3616 | bool PointsAtMacroArgExpansion; |
Argyrios Kyrtzidis | 135bf8e | 2012-06-09 03:03:02 +0000 | [diff] [blame^] | 3617 | bool VisitedObjCPropertyImplDecl; |
| 3618 | SourceLocation VisitedDeclaratorDeclStartLoc; |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3619 | CXCursor &BestCursor; |
| 3620 | |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3621 | GetCursorData(SourceManager &SM, |
| 3622 | SourceLocation tokenBegin, CXCursor &outputCursor) |
| 3623 | : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) { |
| 3624 | PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin); |
Argyrios Kyrtzidis | 135bf8e | 2012-06-09 03:03:02 +0000 | [diff] [blame^] | 3625 | VisitedObjCPropertyImplDecl = false; |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3626 | } |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3627 | }; |
| 3628 | |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3629 | static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, |
| 3630 | CXCursor parent, |
| 3631 | CXClientData client_data) { |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3632 | GetCursorData *Data = static_cast<GetCursorData *>(client_data); |
| 3633 | CXCursor *BestCursor = &Data->BestCursor; |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3634 | |
| 3635 | // If we point inside a macro argument we should provide info of what the |
| 3636 | // token is so use the actual cursor, don't replace it with a macro expansion |
| 3637 | // cursor. |
| 3638 | if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion) |
| 3639 | return CXChildVisit_Recurse; |
Argyrios Kyrtzidis | 65ab907 | 2011-09-26 19:05:37 +0000 | [diff] [blame] | 3640 | |
| 3641 | if (clang_isDeclaration(cursor.kind)) { |
| 3642 | // Avoid having the implicit methods override the property decls. |
Argyrios Kyrtzidis | a9d45a3 | 2012-04-16 22:42:01 +0000 | [diff] [blame] | 3643 | if (ObjCMethodDecl *MD |
| 3644 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
Argyrios Kyrtzidis | 65ab907 | 2011-09-26 19:05:37 +0000 | [diff] [blame] | 3645 | if (MD->isImplicit()) |
| 3646 | return CXChildVisit_Break; |
Argyrios Kyrtzidis | a9d45a3 | 2012-04-16 22:42:01 +0000 | [diff] [blame] | 3647 | |
| 3648 | } else if (ObjCInterfaceDecl *ID |
| 3649 | = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(cursor))) { |
| 3650 | // Check that when we have multiple @class references in the same line, |
| 3651 | // that later ones do not override the previous ones. |
| 3652 | // If we have: |
| 3653 | // @class Foo, Bar; |
| 3654 | // source ranges for both start at '@', so 'Bar' will end up overriding |
| 3655 | // 'Foo' even though the cursor location was at 'Foo'. |
| 3656 | if (BestCursor->kind == CXCursor_ObjCInterfaceDecl || |
| 3657 | BestCursor->kind == CXCursor_ObjCClassRef) |
| 3658 | if (ObjCInterfaceDecl *PrevID |
| 3659 | = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(*BestCursor))){ |
| 3660 | if (PrevID != ID && |
| 3661 | !PrevID->isThisDeclarationADefinition() && |
| 3662 | !ID->isThisDeclarationADefinition()) |
| 3663 | return CXChildVisit_Break; |
| 3664 | } |
Argyrios Kyrtzidis | 135bf8e | 2012-06-09 03:03:02 +0000 | [diff] [blame^] | 3665 | |
| 3666 | } else if (DeclaratorDecl *DD |
| 3667 | = dyn_cast_or_null<DeclaratorDecl>(getCursorDecl(cursor))) { |
| 3668 | SourceLocation StartLoc = DD->getSourceRange().getBegin(); |
| 3669 | // Check that when we have multiple declarators in the same line, |
| 3670 | // that later ones do not override the previous ones. |
| 3671 | // If we have: |
| 3672 | // int Foo, Bar; |
| 3673 | // source ranges for both start at 'int', so 'Bar' will end up overriding |
| 3674 | // 'Foo' even though the cursor location was at 'Foo'. |
| 3675 | if (Data->VisitedDeclaratorDeclStartLoc == StartLoc) |
| 3676 | return CXChildVisit_Break; |
| 3677 | Data->VisitedDeclaratorDeclStartLoc = StartLoc; |
| 3678 | |
| 3679 | } else if (ObjCPropertyImplDecl *PropImp |
| 3680 | = dyn_cast_or_null<ObjCPropertyImplDecl>(getCursorDecl(cursor))) { |
| 3681 | (void)PropImp; |
| 3682 | // Check that when we have multiple @synthesize in the same line, |
| 3683 | // that later ones do not override the previous ones. |
| 3684 | // If we have: |
| 3685 | // @synthesize Foo, Bar; |
| 3686 | // source ranges for both start at '@', so 'Bar' will end up overriding |
| 3687 | // 'Foo' even though the cursor location was at 'Foo'. |
| 3688 | if (Data->VisitedObjCPropertyImplDecl) |
| 3689 | return CXChildVisit_Break; |
| 3690 | Data->VisitedObjCPropertyImplDecl = true; |
Argyrios Kyrtzidis | a9d45a3 | 2012-04-16 22:42:01 +0000 | [diff] [blame] | 3691 | } |
Argyrios Kyrtzidis | 65ab907 | 2011-09-26 19:05:37 +0000 | [diff] [blame] | 3692 | } |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3693 | |
| 3694 | if (clang_isExpression(cursor.kind) && |
| 3695 | clang_isDeclaration(BestCursor->kind)) { |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 3696 | if (Decl *D = getCursorDecl(*BestCursor)) { |
| 3697 | // Avoid having the cursor of an expression replace the declaration cursor |
| 3698 | // when the expression source range overlaps the declaration range. |
| 3699 | // This can happen for C++ constructor expressions whose range generally |
| 3700 | // include the variable declaration, e.g.: |
| 3701 | // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor. |
| 3702 | if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() && |
| 3703 | D->getLocation() == Data->TokenBeginLoc) |
| 3704 | return CXChildVisit_Break; |
| 3705 | } |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3708 | // If our current best cursor is the construction of a temporary object, |
| 3709 | // don't replace that cursor with a type reference, because we want |
| 3710 | // clang_getCursor() to point at the constructor. |
| 3711 | if (clang_isExpression(BestCursor->kind) && |
| 3712 | isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 3713 | cursor.kind == CXCursor_TypeRef) { |
| 3714 | // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it |
| 3715 | // as having the actual point on the type reference. |
| 3716 | *BestCursor = getTypeRefedCallExprCursor(*BestCursor); |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3717 | return CXChildVisit_Recurse; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 3718 | } |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3719 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3720 | *BestCursor = cursor; |
| 3721 | return CXChildVisit_Recurse; |
| 3722 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3723 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 3724 | CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { |
| 3725 | if (!TU) |
Ted Kremenek | f462989 | 2010-01-14 01:51:23 +0000 | [diff] [blame] | 3726 | return clang_getNullCursor(); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3727 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3728 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 3729 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 3730 | |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3731 | SourceLocation SLoc = cxloc::translateSourceLocation(Loc); |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3732 | CXCursor Result = cxcursor::getCursor(TU, SLoc); |
Ted Kremenek | a629ea4 | 2010-07-29 00:52:07 +0000 | [diff] [blame] | 3733 | |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3734 | bool Logging = getenv("LIBCLANG_LOGGING"); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3735 | if (Logging) { |
| 3736 | CXFile SearchFile; |
| 3737 | unsigned SearchLine, SearchColumn; |
| 3738 | CXFile ResultFile; |
| 3739 | unsigned ResultLine, ResultColumn; |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3740 | CXString SearchFileName, ResultFileName, KindSpelling, USR; |
| 3741 | const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : ""; |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3742 | CXSourceLocation ResultLoc = clang_getCursorLocation(Result); |
| 3743 | |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 3744 | clang_getExpansionLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, 0); |
| 3745 | clang_getExpansionLocation(ResultLoc, &ResultFile, &ResultLine, |
| 3746 | &ResultColumn, 0); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3747 | SearchFileName = clang_getFileName(SearchFile); |
| 3748 | ResultFileName = clang_getFileName(ResultFile); |
| 3749 | KindSpelling = clang_getCursorKindSpelling(Result.kind); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3750 | USR = clang_getCursorUSR(Result); |
| 3751 | 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] | 3752 | clang_getCString(SearchFileName), SearchLine, SearchColumn, |
| 3753 | clang_getCString(KindSpelling), |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3754 | clang_getCString(ResultFileName), ResultLine, ResultColumn, |
| 3755 | clang_getCString(USR), IsDef); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3756 | clang_disposeString(SearchFileName); |
| 3757 | clang_disposeString(ResultFileName); |
| 3758 | clang_disposeString(KindSpelling); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3759 | clang_disposeString(USR); |
Douglas Gregor | 0aefbd8 | 2010-12-10 01:45:00 +0000 | [diff] [blame] | 3760 | |
| 3761 | CXCursor Definition = clang_getCursorDefinition(Result); |
| 3762 | if (!clang_equalCursors(Definition, clang_getNullCursor())) { |
| 3763 | CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition); |
| 3764 | CXString DefinitionKindSpelling |
| 3765 | = clang_getCursorKindSpelling(Definition.kind); |
| 3766 | CXFile DefinitionFile; |
| 3767 | unsigned DefinitionLine, DefinitionColumn; |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 3768 | clang_getExpansionLocation(DefinitionLoc, &DefinitionFile, |
| 3769 | &DefinitionLine, &DefinitionColumn, 0); |
Douglas Gregor | 0aefbd8 | 2010-12-10 01:45:00 +0000 | [diff] [blame] | 3770 | CXString DefinitionFileName = clang_getFileName(DefinitionFile); |
| 3771 | fprintf(stderr, " -> %s(%s:%d:%d)\n", |
| 3772 | clang_getCString(DefinitionKindSpelling), |
| 3773 | clang_getCString(DefinitionFileName), |
| 3774 | DefinitionLine, DefinitionColumn); |
| 3775 | clang_disposeString(DefinitionFileName); |
| 3776 | clang_disposeString(DefinitionKindSpelling); |
| 3777 | } |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3780 | return Result; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 3781 | } |
| 3782 | |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3783 | CXCursor clang_getNullCursor(void) { |
Douglas Gregor | 5bfb8c1 | 2010-01-20 23:34:41 +0000 | [diff] [blame] | 3784 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
| 3787 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 3788 | return X == Y; |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3789 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 3790 | |
Douglas Gregor | 9ce5584 | 2010-11-20 00:09:34 +0000 | [diff] [blame] | 3791 | unsigned clang_hashCursor(CXCursor C) { |
| 3792 | unsigned Index = 0; |
| 3793 | if (clang_isExpression(C.kind) || clang_isStatement(C.kind)) |
| 3794 | Index = 1; |
| 3795 | |
| 3796 | return llvm::DenseMapInfo<std::pair<unsigned, void*> >::getHashValue( |
| 3797 | std::make_pair(C.kind, C.data[Index])); |
| 3798 | } |
| 3799 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3800 | unsigned clang_isInvalid(enum CXCursorKind K) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 3801 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 3802 | } |
| 3803 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3804 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3805 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 3806 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 3807 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3808 | unsigned clang_isReference(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3809 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 3810 | } |
| 3811 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3812 | unsigned clang_isExpression(enum CXCursorKind K) { |
| 3813 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; |
| 3814 | } |
| 3815 | |
| 3816 | unsigned clang_isStatement(enum CXCursorKind K) { |
| 3817 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; |
| 3818 | } |
| 3819 | |
Douglas Gregor | 8be80e1 | 2011-07-06 03:00:34 +0000 | [diff] [blame] | 3820 | unsigned clang_isAttribute(enum CXCursorKind K) { |
| 3821 | return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr; |
| 3822 | } |
| 3823 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3824 | unsigned clang_isTranslationUnit(enum CXCursorKind K) { |
| 3825 | return K == CXCursor_TranslationUnit; |
| 3826 | } |
| 3827 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3828 | unsigned clang_isPreprocessing(enum CXCursorKind K) { |
| 3829 | return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; |
| 3830 | } |
| 3831 | |
Ted Kremenek | ad6eff6 | 2010-03-08 21:17:29 +0000 | [diff] [blame] | 3832 | unsigned clang_isUnexposed(enum CXCursorKind K) { |
| 3833 | switch (K) { |
| 3834 | case CXCursor_UnexposedDecl: |
| 3835 | case CXCursor_UnexposedExpr: |
| 3836 | case CXCursor_UnexposedStmt: |
| 3837 | case CXCursor_UnexposedAttr: |
| 3838 | return true; |
| 3839 | default: |
| 3840 | return false; |
| 3841 | } |
| 3842 | } |
| 3843 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3844 | CXCursorKind clang_getCursorKind(CXCursor C) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 3845 | return C.kind; |
| 3846 | } |
| 3847 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3848 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 3849 | if (clang_isReference(C.kind)) { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3850 | switch (C.kind) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3851 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3852 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 3853 | = getCursorObjCSuperClassRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3854 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3855 | } |
| 3856 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3857 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3858 | std::pair<ObjCProtocolDecl *, SourceLocation> P |
| 3859 | = getCursorObjCProtocolRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3860 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3863 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3864 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 3865 | = getCursorObjCClassRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3866 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3867 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3868 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3869 | case CXCursor_TypeRef: { |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3870 | std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3871 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3872 | } |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3873 | |
| 3874 | case CXCursor_TemplateRef: { |
| 3875 | std::pair<TemplateDecl *, SourceLocation> P = getCursorTemplateRef(C); |
| 3876 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3877 | } |
| 3878 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3879 | case CXCursor_NamespaceRef: { |
| 3880 | std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); |
| 3881 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3882 | } |
| 3883 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3884 | case CXCursor_MemberRef: { |
| 3885 | std::pair<FieldDecl *, SourceLocation> P = getCursorMemberRef(C); |
| 3886 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3887 | } |
| 3888 | |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 3889 | case CXCursor_VariableRef: { |
| 3890 | std::pair<VarDecl *, SourceLocation> P = getCursorVariableRef(C); |
| 3891 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3892 | } |
| 3893 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3894 | case CXCursor_CXXBaseSpecifier: { |
Douglas Gregor | 1b0f7af | 2010-10-02 19:51:13 +0000 | [diff] [blame] | 3895 | CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); |
| 3896 | if (!BaseSpec) |
| 3897 | return clang_getNullLocation(); |
| 3898 | |
| 3899 | if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) |
| 3900 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3901 | TSInfo->getTypeLoc().getBeginLoc()); |
| 3902 | |
| 3903 | return cxloc::translateSourceLocation(getCursorContext(C), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3904 | BaseSpec->getLocStart()); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3905 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3906 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3907 | case CXCursor_LabelRef: { |
| 3908 | std::pair<LabelStmt *, SourceLocation> P = getCursorLabelRef(C); |
| 3909 | return cxloc::translateSourceLocation(getCursorContext(C), P.second); |
| 3910 | } |
| 3911 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3912 | case CXCursor_OverloadedDeclRef: |
| 3913 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3914 | getCursorOverloadedDeclRef(C).second); |
| 3915 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3916 | default: |
| 3917 | // FIXME: Need a way to enumerate all non-reference cases. |
| 3918 | llvm_unreachable("Missed a reference kind"); |
| 3919 | } |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3920 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3921 | |
| 3922 | if (clang_isExpression(C.kind)) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3923 | return cxloc::translateSourceLocation(getCursorContext(C), |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3924 | getLocationFromExpr(getCursorExpr(C))); |
| 3925 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3926 | if (clang_isStatement(C.kind)) |
| 3927 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3928 | getCursorStmt(C)->getLocStart()); |
| 3929 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3930 | if (C.kind == CXCursor_PreprocessingDirective) { |
| 3931 | SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); |
| 3932 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3933 | } |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3934 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3935 | if (C.kind == CXCursor_MacroExpansion) { |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 3936 | SourceLocation L |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3937 | = cxcursor::getCursorMacroExpansion(C)->getSourceRange().getBegin(); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3938 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3939 | } |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3940 | |
| 3941 | if (C.kind == CXCursor_MacroDefinition) { |
| 3942 | SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); |
| 3943 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3944 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3945 | |
| 3946 | if (C.kind == CXCursor_InclusionDirective) { |
| 3947 | SourceLocation L |
| 3948 | = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); |
| 3949 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3950 | } |
| 3951 | |
Ted Kremenek | 9a700d2 | 2010-05-12 06:16:13 +0000 | [diff] [blame] | 3952 | if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 3953 | return clang_getNullLocation(); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3954 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3955 | Decl *D = getCursorDecl(C); |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 3956 | if (!D) |
| 3957 | return clang_getNullLocation(); |
| 3958 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3959 | SourceLocation Loc = D->getLocation(); |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 3960 | // FIXME: Multiple variables declared in a single declaration |
| 3961 | // currently lack the information needed to correctly determine their |
| 3962 | // ranges when accounting for the type-specifier. We use context |
| 3963 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3964 | // and if so, whether it is the first decl. |
| 3965 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3966 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3967 | Loc = VD->getLocation(); |
| 3968 | } |
| 3969 | |
Argyrios Kyrtzidis | ccc6f36 | 2012-03-23 03:33:19 +0000 | [diff] [blame] | 3970 | // For ObjC methods, give the start location of the method name. |
| 3971 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 3972 | Loc = MD->getSelectorStartLoc(); |
| 3973 | |
Douglas Gregor | 2ca54fe | 2010-03-22 15:53:50 +0000 | [diff] [blame] | 3974 | return cxloc::translateSourceLocation(getCursorContext(C), Loc); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3975 | } |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3976 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3977 | } // end extern "C" |
| 3978 | |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3979 | CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) { |
| 3980 | assert(TU); |
| 3981 | |
| 3982 | // Guard against an invalid SourceLocation, or we may assert in one |
| 3983 | // of the following calls. |
| 3984 | if (SLoc.isInvalid()) |
| 3985 | return clang_getNullCursor(); |
| 3986 | |
| 3987 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 3988 | |
| 3989 | // Translate the given source location to make it point at the beginning of |
| 3990 | // the token under the cursor. |
| 3991 | SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3992 | CXXUnit->getASTContext().getLangOpts()); |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3993 | |
| 3994 | CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 3995 | if (SLoc.isValid()) { |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3996 | GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result); |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3997 | CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData, |
| 3998 | /*VisitPreprocessorLast=*/true, |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 3999 | /*VisitIncludedEntities=*/false, |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 4000 | SourceLocation(SLoc)); |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 4001 | CursorVis.visitFileRegion(); |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
| 4004 | return Result; |
| 4005 | } |
| 4006 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4007 | static SourceRange getRawCursorExtent(CXCursor C) { |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 4008 | if (clang_isReference(C.kind)) { |
| 4009 | switch (C.kind) { |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4010 | case CXCursor_ObjCSuperClassRef: |
| 4011 | return getCursorObjCSuperClassRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4012 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4013 | case CXCursor_ObjCProtocolRef: |
| 4014 | return getCursorObjCProtocolRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4015 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4016 | case CXCursor_ObjCClassRef: |
| 4017 | return getCursorObjCClassRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4018 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4019 | case CXCursor_TypeRef: |
| 4020 | return getCursorTypeRef(C).second; |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 4021 | |
| 4022 | case CXCursor_TemplateRef: |
| 4023 | return getCursorTemplateRef(C).second; |
| 4024 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 4025 | case CXCursor_NamespaceRef: |
| 4026 | return getCursorNamespaceRef(C).second; |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 4027 | |
| 4028 | case CXCursor_MemberRef: |
| 4029 | return getCursorMemberRef(C).second; |
| 4030 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 4031 | case CXCursor_CXXBaseSpecifier: |
Douglas Gregor | 1b0f7af | 2010-10-02 19:51:13 +0000 | [diff] [blame] | 4032 | return getCursorCXXBaseSpecifier(C)->getSourceRange(); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 4033 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 4034 | case CXCursor_LabelRef: |
| 4035 | return getCursorLabelRef(C).second; |
| 4036 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4037 | case CXCursor_OverloadedDeclRef: |
| 4038 | return getCursorOverloadedDeclRef(C).second; |
| 4039 | |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 4040 | case CXCursor_VariableRef: |
| 4041 | return getCursorVariableRef(C).second; |
| 4042 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4043 | default: |
| 4044 | // FIXME: Need a way to enumerate all non-reference cases. |
| 4045 | llvm_unreachable("Missed a reference kind"); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 4046 | } |
| 4047 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 4048 | |
| 4049 | if (clang_isExpression(C.kind)) |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4050 | return getCursorExpr(C)->getSourceRange(); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 4051 | |
| 4052 | if (clang_isStatement(C.kind)) |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4053 | return getCursorStmt(C)->getSourceRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4054 | |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 4055 | if (clang_isAttribute(C.kind)) |
| 4056 | return getCursorAttr(C)->getRange(); |
| 4057 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4058 | if (C.kind == CXCursor_PreprocessingDirective) |
| 4059 | return cxcursor::getCursorPreprocessingDirective(C); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 4060 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4061 | if (C.kind == CXCursor_MacroExpansion) { |
| 4062 | ASTUnit *TU = getCursorASTUnit(C); |
| 4063 | SourceRange Range = cxcursor::getCursorMacroExpansion(C)->getSourceRange(); |
| 4064 | return TU->mapRangeFromPreamble(Range); |
| 4065 | } |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 4066 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4067 | if (C.kind == CXCursor_MacroDefinition) { |
| 4068 | ASTUnit *TU = getCursorASTUnit(C); |
| 4069 | SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange(); |
| 4070 | return TU->mapRangeFromPreamble(Range); |
| 4071 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 4072 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4073 | if (C.kind == CXCursor_InclusionDirective) { |
| 4074 | ASTUnit *TU = getCursorASTUnit(C); |
| 4075 | SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange(); |
| 4076 | return TU->mapRangeFromPreamble(Range); |
| 4077 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 4078 | |
Argyrios Kyrtzidis | 0822c5f | 2012-03-19 23:17:58 +0000 | [diff] [blame] | 4079 | if (C.kind == CXCursor_TranslationUnit) { |
| 4080 | ASTUnit *TU = getCursorASTUnit(C); |
| 4081 | FileID MainID = TU->getSourceManager().getMainFileID(); |
| 4082 | SourceLocation Start = TU->getSourceManager().getLocForStartOfFile(MainID); |
| 4083 | SourceLocation End = TU->getSourceManager().getLocForEndOfFile(MainID); |
| 4084 | return SourceRange(Start, End); |
| 4085 | } |
| 4086 | |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 4087 | if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { |
| 4088 | Decl *D = cxcursor::getCursorDecl(C); |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 4089 | if (!D) |
| 4090 | return SourceRange(); |
| 4091 | |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 4092 | SourceRange R = D->getSourceRange(); |
| 4093 | // FIXME: Multiple variables declared in a single declaration |
| 4094 | // currently lack the information needed to correctly determine their |
| 4095 | // ranges when accounting for the type-specifier. We use context |
| 4096 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 4097 | // and if so, whether it is the first decl. |
| 4098 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 4099 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 4100 | R.setBegin(VD->getLocation()); |
| 4101 | } |
| 4102 | return R; |
| 4103 | } |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 4104 | return SourceRange(); |
| 4105 | } |
| 4106 | |
| 4107 | /// \brief Retrieves the "raw" cursor extent, which is then extended to include |
| 4108 | /// the decl-specifier-seq for declarations. |
| 4109 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) { |
| 4110 | if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { |
| 4111 | Decl *D = cxcursor::getCursorDecl(C); |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 4112 | if (!D) |
| 4113 | return SourceRange(); |
| 4114 | |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 4115 | SourceRange R = D->getSourceRange(); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 4116 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4117 | // Adjust the start of the location for declarations preceded by |
| 4118 | // declaration specifiers. |
| 4119 | SourceLocation StartLoc; |
| 4120 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
| 4121 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4122 | StartLoc = TI->getTypeLoc().getLocStart(); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4123 | } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
| 4124 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4125 | StartLoc = TI->getTypeLoc().getLocStart(); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
| 4128 | if (StartLoc.isValid() && R.getBegin().isValid() && |
| 4129 | SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin())) |
| 4130 | R.setBegin(StartLoc); |
| 4131 | |
| 4132 | // FIXME: Multiple variables declared in a single declaration |
| 4133 | // currently lack the information needed to correctly determine their |
| 4134 | // ranges when accounting for the type-specifier. We use context |
| 4135 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 4136 | // and if so, whether it is the first decl. |
| 4137 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 4138 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 4139 | R.setBegin(VD->getLocation()); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
| 4142 | return R; |
| 4143 | } |
| 4144 | |
| 4145 | return getRawCursorExtent(C); |
| 4146 | } |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4147 | |
| 4148 | extern "C" { |
| 4149 | |
| 4150 | CXSourceRange clang_getCursorExtent(CXCursor C) { |
| 4151 | SourceRange R = getRawCursorExtent(C); |
| 4152 | if (R.isInvalid()) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 4153 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4154 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 4155 | return cxloc::translateSourceRange(getCursorContext(C), R); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 4156 | } |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4157 | |
| 4158 | CXCursor clang_getCursorReferenced(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 4159 | if (clang_isInvalid(C.kind)) |
| 4160 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4161 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4162 | CXTranslationUnit tu = getCursorTU(C); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4163 | if (clang_isDeclaration(C.kind)) { |
| 4164 | Decl *D = getCursorDecl(C); |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 4165 | if (!D) |
| 4166 | return clang_getNullCursor(); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4167 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4168 | return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4169 | if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 4170 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 4171 | return MakeCXCursor(Property, tu); |
| 4172 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4173 | return C; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4174 | } |
| 4175 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 4176 | if (clang_isExpression(C.kind)) { |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4177 | Expr *E = getCursorExpr(C); |
| 4178 | Decl *D = getDeclFromExpr(E); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 4179 | if (D) { |
| 4180 | CXCursor declCursor = MakeCXCursor(D, tu); |
| 4181 | declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C), |
| 4182 | declCursor); |
| 4183 | return declCursor; |
| 4184 | } |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4185 | |
| 4186 | if (OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4187 | return MakeCursorOverloadedDeclRef(Ovl, tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4188 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 4189 | return clang_getNullCursor(); |
| 4190 | } |
| 4191 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 4192 | if (clang_isStatement(C.kind)) { |
| 4193 | Stmt *S = getCursorStmt(C); |
| 4194 | if (GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) |
Ted Kremenek | 37c2e96 | 2011-03-15 23:47:49 +0000 | [diff] [blame] | 4195 | if (LabelDecl *label = Goto->getLabel()) |
| 4196 | if (LabelStmt *labelS = label->getStmt()) |
| 4197 | return MakeCXCursor(labelS, getCursorDecl(C), tu); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 4198 | |
| 4199 | return clang_getNullCursor(); |
| 4200 | } |
| 4201 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 4202 | if (C.kind == CXCursor_MacroExpansion) { |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 4203 | if (MacroDefinition *Def = getCursorMacroExpansion(C)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4204 | return MakeMacroDefinitionCursor(Def, tu); |
Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4207 | if (!clang_isReference(C.kind)) |
| 4208 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4209 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4210 | switch (C.kind) { |
| 4211 | case CXCursor_ObjCSuperClassRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4212 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4213 | |
| 4214 | case CXCursor_ObjCProtocolRef: { |
Argyrios Kyrtzidis | 98c16b8 | 2012-01-24 19:40:15 +0000 | [diff] [blame] | 4215 | ObjCProtocolDecl *Prot = getCursorObjCProtocolRef(C).first; |
| 4216 | if (ObjCProtocolDecl *Def = Prot->getDefinition()) |
| 4217 | return MakeCXCursor(Def, tu); |
| 4218 | |
Argyrios Kyrtzidis | c15707d | 2012-01-24 21:39:26 +0000 | [diff] [blame] | 4219 | return MakeCXCursor(Prot, tu); |
Argyrios Kyrtzidis | 98c16b8 | 2012-01-24 19:40:15 +0000 | [diff] [blame] | 4220 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4221 | |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 4222 | case CXCursor_ObjCClassRef: { |
| 4223 | ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
| 4224 | if (ObjCInterfaceDecl *Def = Class->getDefinition()) |
| 4225 | return MakeCXCursor(Def, tu); |
| 4226 | |
Argyrios Kyrtzidis | c15707d | 2012-01-24 21:39:26 +0000 | [diff] [blame] | 4227 | return MakeCXCursor(Class, tu); |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 4228 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 4229 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4230 | case CXCursor_TypeRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4231 | return MakeCXCursor(getCursorTypeRef(C).first, tu ); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 4232 | |
| 4233 | case CXCursor_TemplateRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4234 | return MakeCXCursor(getCursorTemplateRef(C).first, tu ); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 4235 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 4236 | case CXCursor_NamespaceRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4237 | return MakeCXCursor(getCursorNamespaceRef(C).first, tu ); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 4238 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 4239 | case CXCursor_MemberRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4240 | return MakeCXCursor(getCursorMemberRef(C).first, tu ); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 4241 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 4242 | case CXCursor_CXXBaseSpecifier: { |
| 4243 | CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); |
| 4244 | return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4245 | tu )); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 4246 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4247 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 4248 | case CXCursor_LabelRef: |
| 4249 | // FIXME: We end up faking the "parent" declaration here because we |
| 4250 | // don't want to make CXCursor larger. |
| 4251 | return MakeCXCursor(getCursorLabelRef(C).first, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4252 | static_cast<ASTUnit*>(tu->TUData)->getASTContext() |
| 4253 | .getTranslationUnitDecl(), |
| 4254 | tu); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 4255 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4256 | case CXCursor_OverloadedDeclRef: |
| 4257 | return C; |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 4258 | |
| 4259 | case CXCursor_VariableRef: |
| 4260 | return MakeCXCursor(getCursorVariableRef(C).first, tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4261 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4262 | default: |
| 4263 | // We would prefer to enumerate all non-reference cursor kinds here. |
| 4264 | llvm_unreachable("Unhandled reference cursor kind"); |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4265 | } |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4266 | } |
| 4267 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4268 | CXCursor clang_getCursorDefinition(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 4269 | if (clang_isInvalid(C.kind)) |
| 4270 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4271 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4272 | CXTranslationUnit TU = getCursorTU(C); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4273 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4274 | bool WasReference = false; |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 4275 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4276 | C = clang_getCursorReferenced(C); |
| 4277 | WasReference = true; |
| 4278 | } |
| 4279 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 4280 | if (C.kind == CXCursor_MacroExpansion) |
Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 4281 | return clang_getCursorReferenced(C); |
| 4282 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4283 | if (!clang_isDeclaration(C.kind)) |
| 4284 | return clang_getNullCursor(); |
| 4285 | |
| 4286 | Decl *D = getCursorDecl(C); |
| 4287 | if (!D) |
| 4288 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4289 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4290 | switch (D->getKind()) { |
| 4291 | // Declaration kinds that don't really separate the notions of |
| 4292 | // declaration and definition. |
| 4293 | case Decl::Namespace: |
| 4294 | case Decl::Typedef: |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4295 | case Decl::TypeAlias: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4296 | case Decl::TypeAliasTemplate: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4297 | case Decl::TemplateTypeParm: |
| 4298 | case Decl::EnumConstant: |
| 4299 | case Decl::Field: |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 4300 | case Decl::IndirectField: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4301 | case Decl::ObjCIvar: |
| 4302 | case Decl::ObjCAtDefsField: |
| 4303 | case Decl::ImplicitParam: |
| 4304 | case Decl::ParmVar: |
| 4305 | case Decl::NonTypeTemplateParm: |
| 4306 | case Decl::TemplateTemplateParm: |
| 4307 | case Decl::ObjCCategoryImpl: |
| 4308 | case Decl::ObjCImplementation: |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 4309 | case Decl::AccessSpec: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4310 | case Decl::LinkageSpec: |
| 4311 | case Decl::ObjCPropertyImpl: |
| 4312 | case Decl::FileScopeAsm: |
| 4313 | case Decl::StaticAssert: |
| 4314 | case Decl::Block: |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 4315 | case Decl::Label: // FIXME: Is this right?? |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4316 | case Decl::ClassScopeFunctionSpecialization: |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4317 | case Decl::Import: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4318 | return C; |
| 4319 | |
| 4320 | // Declaration kinds that don't make any sense here, but are |
| 4321 | // nonetheless harmless. |
| 4322 | case Decl::TranslationUnit: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4323 | break; |
| 4324 | |
| 4325 | // Declaration kinds for which the definition is not resolvable. |
| 4326 | case Decl::UnresolvedUsingTypename: |
| 4327 | case Decl::UnresolvedUsingValue: |
| 4328 | break; |
| 4329 | |
| 4330 | case Decl::UsingDirective: |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 4331 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4332 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4333 | |
| 4334 | case Decl::NamespaceAlias: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4335 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4336 | |
| 4337 | case Decl::Enum: |
| 4338 | case Decl::Record: |
| 4339 | case Decl::CXXRecord: |
| 4340 | case Decl::ClassTemplateSpecialization: |
| 4341 | case Decl::ClassTemplatePartialSpecialization: |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4342 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4343 | return MakeCXCursor(Def, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4344 | return clang_getNullCursor(); |
| 4345 | |
| 4346 | case Decl::Function: |
| 4347 | case Decl::CXXMethod: |
| 4348 | case Decl::CXXConstructor: |
| 4349 | case Decl::CXXDestructor: |
| 4350 | case Decl::CXXConversion: { |
| 4351 | const FunctionDecl *Def = 0; |
| 4352 | if (cast<FunctionDecl>(D)->getBody(Def)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4353 | return MakeCXCursor(const_cast<FunctionDecl *>(Def), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4354 | return clang_getNullCursor(); |
| 4355 | } |
| 4356 | |
| 4357 | case Decl::Var: { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 4358 | // Ask the variable if it has a definition. |
| 4359 | if (VarDecl *Def = cast<VarDecl>(D)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4360 | return MakeCXCursor(Def, TU); |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 4361 | return clang_getNullCursor(); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4362 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4363 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4364 | case Decl::FunctionTemplate: { |
| 4365 | const FunctionDecl *Def = 0; |
| 4366 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4367 | return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4368 | return clang_getNullCursor(); |
| 4369 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4370 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4371 | case Decl::ClassTemplate: { |
| 4372 | if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4373 | ->getDefinition()) |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 4374 | return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4375 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4376 | return clang_getNullCursor(); |
| 4377 | } |
| 4378 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4379 | case Decl::Using: |
| 4380 | return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4381 | D->getLocation(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4382 | |
| 4383 | case Decl::UsingShadow: |
| 4384 | return clang_getCursorDefinition( |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4385 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4386 | TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4387 | |
| 4388 | case Decl::ObjCMethod: { |
| 4389 | ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); |
| 4390 | if (Method->isThisDeclarationADefinition()) |
| 4391 | return C; |
| 4392 | |
| 4393 | // Dig out the method definition in the associated |
| 4394 | // @implementation, if we have it. |
| 4395 | // FIXME: The ASTs should make finding the definition easier. |
| 4396 | if (ObjCInterfaceDecl *Class |
| 4397 | = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) |
| 4398 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) |
| 4399 | if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), |
| 4400 | Method->isInstanceMethod())) |
| 4401 | if (Def->isThisDeclarationADefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4402 | return MakeCXCursor(Def, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4403 | |
| 4404 | return clang_getNullCursor(); |
| 4405 | } |
| 4406 | |
| 4407 | case Decl::ObjCCategory: |
| 4408 | if (ObjCCategoryImplDecl *Impl |
| 4409 | = cast<ObjCCategoryDecl>(D)->getImplementation()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4410 | return MakeCXCursor(Impl, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4411 | return clang_getNullCursor(); |
| 4412 | |
| 4413 | case Decl::ObjCProtocol: |
Douglas Gregor | 5e2a1ff | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 4414 | if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(D)->getDefinition()) |
| 4415 | return MakeCXCursor(Def, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4416 | return clang_getNullCursor(); |
| 4417 | |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 4418 | case Decl::ObjCInterface: { |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4419 | // There are two notions of a "definition" for an Objective-C |
| 4420 | // class: the interface and its implementation. When we resolved a |
| 4421 | // reference to an Objective-C class, produce the @interface as |
| 4422 | // the definition; when we were provided with the interface, |
| 4423 | // produce the @implementation as the definition. |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 4424 | ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4425 | if (WasReference) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 4426 | if (ObjCInterfaceDecl *Def = IFace->getDefinition()) |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 4427 | return MakeCXCursor(Def, TU); |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 4428 | } else if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4429 | return MakeCXCursor(Impl, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4430 | return clang_getNullCursor(); |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 4431 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4432 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4433 | case Decl::ObjCProperty: |
| 4434 | // FIXME: We don't really know where to find the |
| 4435 | // ObjCPropertyImplDecls that implement this property. |
| 4436 | return clang_getNullCursor(); |
| 4437 | |
| 4438 | case Decl::ObjCCompatibleAlias: |
| 4439 | if (ObjCInterfaceDecl *Class |
| 4440 | = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 4441 | if (ObjCInterfaceDecl *Def = Class->getDefinition()) |
| 4442 | return MakeCXCursor(Def, TU); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4443 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4444 | return clang_getNullCursor(); |
| 4445 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4446 | case Decl::Friend: |
| 4447 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4448 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4449 | return clang_getNullCursor(); |
| 4450 | |
| 4451 | case Decl::FriendTemplate: |
| 4452 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4453 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4454 | return clang_getNullCursor(); |
| 4455 | } |
| 4456 | |
| 4457 | return clang_getNullCursor(); |
| 4458 | } |
| 4459 | |
| 4460 | unsigned clang_isCursorDefinition(CXCursor C) { |
| 4461 | if (!clang_isDeclaration(C.kind)) |
| 4462 | return 0; |
| 4463 | |
| 4464 | return clang_getCursorDefinition(C) == C; |
| 4465 | } |
| 4466 | |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4467 | CXCursor clang_getCanonicalCursor(CXCursor C) { |
| 4468 | if (!clang_isDeclaration(C.kind)) |
| 4469 | return C; |
| 4470 | |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4471 | if (Decl *D = getCursorDecl(C)) { |
Argyrios Kyrtzidis | debb00f | 2011-07-15 22:37:58 +0000 | [diff] [blame] | 4472 | if (ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 4473 | if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl()) |
| 4474 | return MakeCXCursor(CatD, getCursorTU(C)); |
| 4475 | |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4476 | if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) |
| 4477 | if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
| 4478 | return MakeCXCursor(IFD, getCursorTU(C)); |
| 4479 | |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4480 | return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C)); |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4481 | } |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4482 | |
| 4483 | return C; |
| 4484 | } |
Argyrios Kyrtzidis | 34ebe1e | 2012-03-30 22:15:48 +0000 | [diff] [blame] | 4485 | |
| 4486 | int clang_Cursor_getObjCSelectorIndex(CXCursor cursor) { |
| 4487 | return cxcursor::getSelectorIdentifierIndexAndLoc(cursor).first; |
| 4488 | } |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4489 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4490 | unsigned clang_getNumOverloadedDecls(CXCursor C) { |
Douglas Gregor | 7c432dd | 2010-09-16 13:54:00 +0000 | [diff] [blame] | 4491 | if (C.kind != CXCursor_OverloadedDeclRef) |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4492 | return 0; |
| 4493 | |
| 4494 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
| 4495 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
| 4496 | return E->getNumDecls(); |
| 4497 | |
| 4498 | if (OverloadedTemplateStorage *S |
| 4499 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 4500 | return S->size(); |
| 4501 | |
| 4502 | Decl *D = Storage.get<Decl*>(); |
| 4503 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Argyrios Kyrtzidis | 826faa2 | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 4504 | return Using->shadow_size(); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4505 | |
| 4506 | return 0; |
| 4507 | } |
| 4508 | |
| 4509 | CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { |
Douglas Gregor | 7c432dd | 2010-09-16 13:54:00 +0000 | [diff] [blame] | 4510 | if (cursor.kind != CXCursor_OverloadedDeclRef) |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4511 | return clang_getNullCursor(); |
| 4512 | |
| 4513 | if (index >= clang_getNumOverloadedDecls(cursor)) |
| 4514 | return clang_getNullCursor(); |
| 4515 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4516 | CXTranslationUnit TU = getCursorTU(cursor); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4517 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first; |
| 4518 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4519 | return MakeCXCursor(E->decls_begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4520 | |
| 4521 | if (OverloadedTemplateStorage *S |
| 4522 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4523 | return MakeCXCursor(S->begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4524 | |
| 4525 | Decl *D = Storage.get<Decl*>(); |
| 4526 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) { |
| 4527 | // FIXME: This is, unfortunately, linear time. |
| 4528 | UsingDecl::shadow_iterator Pos = Using->shadow_begin(); |
| 4529 | std::advance(Pos, index); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4530 | return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4531 | } |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4532 | |
| 4533 | return clang_getNullCursor(); |
| 4534 | } |
| 4535 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 4536 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4537 | const char **startBuf, |
| 4538 | const char **endBuf, |
| 4539 | unsigned *startLine, |
| 4540 | unsigned *startColumn, |
| 4541 | unsigned *endLine, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 4542 | unsigned *endColumn) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 4543 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
| 4544 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4545 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 4546 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4547 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4548 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 4549 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 4550 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 4551 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 4552 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 4553 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 4554 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 4555 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4556 | |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 4557 | |
| 4558 | CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags, |
| 4559 | unsigned PieceIndex) { |
| 4560 | RefNamePieces Pieces; |
| 4561 | |
| 4562 | switch (C.kind) { |
| 4563 | case CXCursor_MemberRefExpr: |
| 4564 | if (MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C))) |
| 4565 | Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(), |
| 4566 | E->getQualifierLoc().getSourceRange()); |
| 4567 | break; |
| 4568 | |
| 4569 | case CXCursor_DeclRefExpr: |
| 4570 | if (DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C))) |
| 4571 | Pieces = buildPieces(NameFlags, false, E->getNameInfo(), |
| 4572 | E->getQualifierLoc().getSourceRange(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4573 | E->getOptionalExplicitTemplateArgs()); |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 4574 | break; |
| 4575 | |
| 4576 | case CXCursor_CallExpr: |
| 4577 | if (CXXOperatorCallExpr *OCE = |
| 4578 | dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) { |
| 4579 | Expr *Callee = OCE->getCallee(); |
| 4580 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) |
| 4581 | Callee = ICE->getSubExpr(); |
| 4582 | |
| 4583 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) |
| 4584 | Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(), |
| 4585 | DRE->getQualifierLoc().getSourceRange()); |
| 4586 | } |
| 4587 | break; |
| 4588 | |
| 4589 | default: |
| 4590 | break; |
| 4591 | } |
| 4592 | |
| 4593 | if (Pieces.empty()) { |
| 4594 | if (PieceIndex == 0) |
| 4595 | return clang_getCursorExtent(C); |
| 4596 | } else if (PieceIndex < Pieces.size()) { |
| 4597 | SourceRange R = Pieces[PieceIndex]; |
| 4598 | if (R.isValid()) |
| 4599 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 4600 | } |
| 4601 | |
| 4602 | return clang_getNullRange(); |
| 4603 | } |
| 4604 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 4605 | void clang_enableStackTraces(void) { |
| 4606 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 4607 | } |
| 4608 | |
Daniel Dunbar | 995aaf9 | 2010-11-04 01:26:29 +0000 | [diff] [blame] | 4609 | void clang_executeOnThread(void (*fn)(void*), void *user_data, |
| 4610 | unsigned stack_size) { |
| 4611 | llvm::llvm_execute_on_thread(fn, user_data, stack_size); |
| 4612 | } |
| 4613 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 4614 | } // end: extern "C" |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4615 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 4616 | //===----------------------------------------------------------------------===// |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4617 | // Token-based Operations. |
| 4618 | //===----------------------------------------------------------------------===// |
| 4619 | |
| 4620 | /* CXToken layout: |
| 4621 | * int_data[0]: a CXTokenKind |
| 4622 | * int_data[1]: starting token location |
| 4623 | * int_data[2]: token length |
| 4624 | * int_data[3]: reserved |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4625 | * ptr_data: for identifiers and keywords, an IdentifierInfo*. |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4626 | * otherwise unused. |
| 4627 | */ |
| 4628 | extern "C" { |
| 4629 | |
| 4630 | CXTokenKind clang_getTokenKind(CXToken CXTok) { |
| 4631 | return static_cast<CXTokenKind>(CXTok.int_data[0]); |
| 4632 | } |
| 4633 | |
| 4634 | CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { |
| 4635 | switch (clang_getTokenKind(CXTok)) { |
| 4636 | case CXToken_Identifier: |
| 4637 | case CXToken_Keyword: |
| 4638 | // We know we have an IdentifierInfo*, so use that. |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 4639 | return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data) |
| 4640 | ->getNameStart()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4641 | |
| 4642 | case CXToken_Literal: { |
| 4643 | // We have stashed the starting pointer in the ptr_data field. Use it. |
| 4644 | const char *Text = static_cast<const char *>(CXTok.ptr_data); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4645 | return createCXString(StringRef(Text, CXTok.int_data[2])); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4646 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4647 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4648 | case CXToken_Punctuation: |
| 4649 | case CXToken_Comment: |
| 4650 | break; |
| 4651 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4652 | |
| 4653 | // We have to find the starting buffer pointer the hard way, by |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4654 | // deconstructing the source location. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4655 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4656 | if (!CXXUnit) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 4657 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4658 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4659 | SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); |
| 4660 | std::pair<FileID, unsigned> LocInfo |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4661 | = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4662 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4663 | StringRef Buffer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4664 | = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); |
| 4665 | if (Invalid) |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4666 | return createCXString(""); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4667 | |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4668 | return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2])); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4669 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4670 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4671 | CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4672 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4673 | if (!CXXUnit) |
| 4674 | return clang_getNullLocation(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4675 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4676 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), |
| 4677 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 4678 | } |
| 4679 | |
| 4680 | CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4681 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 4682 | if (!CXXUnit) |
| 4683 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4684 | |
| 4685 | return cxloc::translateSourceRange(CXXUnit->getASTContext(), |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4686 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 4687 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4688 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4689 | static void getTokens(ASTUnit *CXXUnit, SourceRange Range, |
| 4690 | SmallVectorImpl<CXToken> &CXTokens) { |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4691 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 4692 | std::pair<FileID, unsigned> BeginLocInfo |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4693 | = SourceMgr.getDecomposedLoc(Range.getBegin()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4694 | std::pair<FileID, unsigned> EndLocInfo |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4695 | = SourceMgr.getDecomposedLoc(Range.getEnd()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4696 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4697 | // Cannot tokenize across files. |
| 4698 | if (BeginLocInfo.first != EndLocInfo.first) |
| 4699 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4700 | |
| 4701 | // Create a lexer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4702 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4703 | StringRef Buffer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4704 | = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
Douglas Gregor | 47a3fcd | 2010-03-16 20:26:15 +0000 | [diff] [blame] | 4705 | if (Invalid) |
| 4706 | return; |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4707 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4708 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4709 | CXXUnit->getASTContext().getLangOpts(), |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4710 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4711 | Lex.SetCommentRetentionState(true); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4712 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4713 | // Lex tokens until we hit the end of the range. |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4714 | const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4715 | Token Tok; |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4716 | bool previousWasAt = false; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4717 | do { |
| 4718 | // Lex the next token |
| 4719 | Lex.LexFromRawLexer(Tok); |
| 4720 | if (Tok.is(tok::eof)) |
| 4721 | break; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4722 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4723 | // Initialize the CXToken. |
| 4724 | CXToken CXTok; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4725 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4726 | // - Common fields |
| 4727 | CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); |
| 4728 | CXTok.int_data[2] = Tok.getLength(); |
| 4729 | CXTok.int_data[3] = 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4730 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4731 | // - Kind-specific fields |
| 4732 | if (Tok.isLiteral()) { |
| 4733 | CXTok.int_data[0] = CXToken_Literal; |
| 4734 | CXTok.ptr_data = (void *)Tok.getLiteralData(); |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4735 | } else if (Tok.is(tok::raw_identifier)) { |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4736 | // Lookup the identifier to determine whether we have a keyword. |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4737 | IdentifierInfo *II |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4738 | = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok); |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4739 | |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4740 | if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4741 | CXTok.int_data[0] = CXToken_Keyword; |
| 4742 | } |
| 4743 | else { |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4744 | CXTok.int_data[0] = Tok.is(tok::identifier) |
| 4745 | ? CXToken_Identifier |
| 4746 | : CXToken_Keyword; |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4747 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4748 | CXTok.ptr_data = II; |
| 4749 | } else if (Tok.is(tok::comment)) { |
| 4750 | CXTok.int_data[0] = CXToken_Comment; |
| 4751 | CXTok.ptr_data = 0; |
| 4752 | } else { |
| 4753 | CXTok.int_data[0] = CXToken_Punctuation; |
| 4754 | CXTok.ptr_data = 0; |
| 4755 | } |
| 4756 | CXTokens.push_back(CXTok); |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4757 | previousWasAt = Tok.is(tok::at); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4758 | } while (Lex.getBufferLocation() <= EffectiveBufferEnd); |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4759 | } |
| 4760 | |
| 4761 | void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, |
| 4762 | CXToken **Tokens, unsigned *NumTokens) { |
| 4763 | if (Tokens) |
| 4764 | *Tokens = 0; |
| 4765 | if (NumTokens) |
| 4766 | *NumTokens = 0; |
| 4767 | |
| 4768 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 4769 | if (!CXXUnit || !Tokens || !NumTokens) |
| 4770 | return; |
| 4771 | |
| 4772 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 4773 | |
| 4774 | SourceRange R = cxloc::translateCXSourceRange(Range); |
| 4775 | if (R.isInvalid()) |
| 4776 | return; |
| 4777 | |
| 4778 | SmallVector<CXToken, 32> CXTokens; |
| 4779 | getTokens(CXXUnit, R, CXTokens); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4780 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4781 | if (CXTokens.empty()) |
| 4782 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4783 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4784 | *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size()); |
| 4785 | memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); |
| 4786 | *NumTokens = CXTokens.size(); |
| 4787 | } |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4788 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4789 | void clang_disposeTokens(CXTranslationUnit TU, |
| 4790 | CXToken *Tokens, unsigned NumTokens) { |
| 4791 | free(Tokens); |
| 4792 | } |
| 4793 | |
| 4794 | } // end: extern "C" |
| 4795 | |
| 4796 | //===----------------------------------------------------------------------===// |
| 4797 | // Token annotation APIs. |
| 4798 | //===----------------------------------------------------------------------===// |
| 4799 | |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4800 | typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4801 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 4802 | CXCursor parent, |
| 4803 | CXClientData client_data); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4804 | namespace { |
| 4805 | class AnnotateTokensWorker { |
| 4806 | AnnotateTokensData &Annotated; |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4807 | CXToken *Tokens; |
| 4808 | CXCursor *Cursors; |
| 4809 | unsigned NumTokens; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4810 | unsigned TokIdx; |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4811 | unsigned PreprocessingTokIdx; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4812 | CursorVisitor AnnotateVis; |
| 4813 | SourceManager &SrcMgr; |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4814 | bool HasContextSensitiveKeywords; |
| 4815 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4816 | bool MoreTokens() const { return TokIdx < NumTokens; } |
| 4817 | unsigned NextToken() const { return TokIdx; } |
| 4818 | void AdvanceToken() { ++TokIdx; } |
| 4819 | SourceLocation GetTokenLoc(unsigned tokI) { |
| 4820 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); |
| 4821 | } |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4822 | bool isFunctionMacroToken(unsigned tokI) const { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4823 | return Tokens[tokI].int_data[3] != 0; |
| 4824 | } |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4825 | SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4826 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[3]); |
| 4827 | } |
| 4828 | |
| 4829 | void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4830 | void annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult, |
| 4831 | SourceRange); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4832 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4833 | public: |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4834 | AnnotateTokensWorker(AnnotateTokensData &annotated, |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4835 | CXToken *tokens, CXCursor *cursors, unsigned numTokens, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4836 | CXTranslationUnit tu, SourceRange RegionOfInterest) |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4837 | : Annotated(annotated), Tokens(tokens), Cursors(cursors), |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4838 | NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4839 | AnnotateVis(tu, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 4840 | AnnotateTokensVisitor, this, |
| 4841 | /*VisitPreprocessorLast=*/true, |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 4842 | /*VisitIncludedEntities=*/false, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 4843 | RegionOfInterest), |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4844 | SrcMgr(static_cast<ASTUnit*>(tu->TUData)->getSourceManager()), |
| 4845 | HasContextSensitiveKeywords(false) { } |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4846 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4847 | void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4848 | enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 4849 | void AnnotateTokens(); |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4850 | |
| 4851 | /// \brief Determine whether the annotator saw any cursors that have |
| 4852 | /// context-sensitive keywords. |
| 4853 | bool hasContextSensitiveKeywords() const { |
| 4854 | return HasContextSensitiveKeywords; |
| 4855 | } |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4856 | }; |
| 4857 | } |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4858 | |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 4859 | void AnnotateTokensWorker::AnnotateTokens() { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4860 | // Walk the AST within the region of interest, annotating tokens |
| 4861 | // along the way. |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 4862 | AnnotateVis.visitFileRegion(); |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4863 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4864 | for (unsigned I = 0 ; I < TokIdx ; ++I) { |
| 4865 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4866 | if (Pos != Annotated.end() && |
| 4867 | (clang_isInvalid(Cursors[I].kind) || |
| 4868 | Pos->second.kind != CXCursor_PreprocessingDirective)) |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4869 | Cursors[I] = Pos->second; |
| 4870 | } |
| 4871 | |
| 4872 | // Finish up annotating any tokens left. |
| 4873 | if (!MoreTokens()) |
| 4874 | return; |
| 4875 | |
| 4876 | const CXCursor &C = clang_getNullCursor(); |
| 4877 | for (unsigned I = TokIdx ; I < NumTokens ; ++I) { |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 4878 | if (I < PreprocessingTokIdx && clang_isPreprocessing(Cursors[I].kind)) |
| 4879 | continue; |
| 4880 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4881 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); |
| 4882 | Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second; |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4883 | } |
| 4884 | } |
| 4885 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4886 | /// \brief It annotates and advances tokens with a cursor until the comparison |
| 4887 | //// between the cursor location and the source range is the same as |
| 4888 | /// \arg compResult. |
| 4889 | /// |
| 4890 | /// Pass RangeBefore to annotate tokens with a cursor until a range is reached. |
| 4891 | /// Pass RangeOverlap to annotate tokens inside a range. |
| 4892 | void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC, |
| 4893 | RangeComparisonResult compResult, |
| 4894 | SourceRange range) { |
| 4895 | while (MoreTokens()) { |
| 4896 | const unsigned I = NextToken(); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4897 | if (isFunctionMacroToken(I)) |
| 4898 | return annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4899 | |
| 4900 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4901 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 4902 | Cursors[I] = updateC; |
| 4903 | AdvanceToken(); |
| 4904 | continue; |
| 4905 | } |
| 4906 | break; |
| 4907 | } |
| 4908 | } |
| 4909 | |
| 4910 | /// \brief Special annotation handling for macro argument tokens. |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4911 | void AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens( |
| 4912 | CXCursor updateC, |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4913 | RangeComparisonResult compResult, |
| 4914 | SourceRange range) { |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4915 | assert(MoreTokens()); |
| 4916 | assert(isFunctionMacroToken(NextToken()) && |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4917 | "Should be called only for macro arg tokens"); |
| 4918 | |
| 4919 | // This works differently than annotateAndAdvanceTokens; because expanded |
| 4920 | // macro arguments can have arbitrary translation-unit source order, we do not |
| 4921 | // advance the token index one by one until a token fails the range test. |
| 4922 | // We only advance once past all of the macro arg tokens if all of them |
| 4923 | // pass the range test. If one of them fails we keep the token index pointing |
| 4924 | // at the start of the macro arg tokens so that the failing token will be |
| 4925 | // annotated by a subsequent annotation try. |
| 4926 | |
| 4927 | bool atLeastOneCompFail = false; |
| 4928 | |
| 4929 | unsigned I = NextToken(); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4930 | for (; I < NumTokens && isFunctionMacroToken(I); ++I) { |
| 4931 | SourceLocation TokLoc = getFunctionMacroTokenLoc(I); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4932 | if (TokLoc.isFileID()) |
| 4933 | continue; // not macro arg token, it's parens or comma. |
| 4934 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 4935 | if (clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 4936 | Cursors[I] = updateC; |
| 4937 | } else |
| 4938 | atLeastOneCompFail = true; |
| 4939 | } |
| 4940 | |
| 4941 | if (!atLeastOneCompFail) |
| 4942 | TokIdx = I; // All of the tokens were handled, advance beyond all of them. |
| 4943 | } |
| 4944 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4945 | enum CXChildVisitResult |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4946 | AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4947 | CXSourceLocation Loc = clang_getCursorLocation(cursor); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4948 | SourceRange cursorRange = getRawCursorExtent(cursor); |
Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 4949 | if (cursorRange.isInvalid()) |
| 4950 | return CXChildVisit_Recurse; |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4951 | |
| 4952 | if (!HasContextSensitiveKeywords) { |
| 4953 | // Objective-C properties can have context-sensitive keywords. |
| 4954 | if (cursor.kind == CXCursor_ObjCPropertyDecl) { |
| 4955 | if (ObjCPropertyDecl *Property |
| 4956 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor))) |
| 4957 | HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0; |
| 4958 | } |
| 4959 | // Objective-C methods can have context-sensitive keywords. |
| 4960 | else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl || |
| 4961 | cursor.kind == CXCursor_ObjCClassMethodDecl) { |
| 4962 | if (ObjCMethodDecl *Method |
| 4963 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 4964 | if (Method->getObjCDeclQualifier()) |
| 4965 | HasContextSensitiveKeywords = true; |
| 4966 | else { |
| 4967 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 4968 | PEnd = Method->param_end(); |
| 4969 | P != PEnd; ++P) { |
| 4970 | if ((*P)->getObjCDeclQualifier()) { |
| 4971 | HasContextSensitiveKeywords = true; |
| 4972 | break; |
| 4973 | } |
| 4974 | } |
| 4975 | } |
| 4976 | } |
| 4977 | } |
| 4978 | // C++ methods can have context-sensitive keywords. |
| 4979 | else if (cursor.kind == CXCursor_CXXMethod) { |
| 4980 | if (CXXMethodDecl *Method |
| 4981 | = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) { |
| 4982 | if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>()) |
| 4983 | HasContextSensitiveKeywords = true; |
| 4984 | } |
| 4985 | } |
| 4986 | // C++ classes can have context-sensitive keywords. |
| 4987 | else if (cursor.kind == CXCursor_StructDecl || |
| 4988 | cursor.kind == CXCursor_ClassDecl || |
| 4989 | cursor.kind == CXCursor_ClassTemplate || |
| 4990 | cursor.kind == CXCursor_ClassTemplatePartialSpecialization) { |
| 4991 | if (Decl *D = getCursorDecl(cursor)) |
| 4992 | if (D->hasAttr<FinalAttr>()) |
| 4993 | HasContextSensitiveKeywords = true; |
| 4994 | } |
| 4995 | } |
| 4996 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4997 | if (clang_isPreprocessing(cursor.kind)) { |
Chandler Carruth | cea731a | 2011-07-14 16:07:57 +0000 | [diff] [blame] | 4998 | // For macro expansions, just note where the beginning of the macro |
| 4999 | // expansion occurs. |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 5000 | if (cursor.kind == CXCursor_MacroExpansion) { |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5001 | Annotated[Loc.int_data] = cursor; |
| 5002 | return CXChildVisit_Recurse; |
| 5003 | } |
| 5004 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5005 | // Items in the preprocessing record are kept separate from items in |
| 5006 | // declarations, so we keep a separate token index. |
| 5007 | unsigned SavedTokIdx = TokIdx; |
| 5008 | TokIdx = PreprocessingTokIdx; |
| 5009 | |
| 5010 | // Skip tokens up until we catch up to the beginning of the preprocessing |
| 5011 | // entry. |
| 5012 | while (MoreTokens()) { |
| 5013 | const unsigned I = NextToken(); |
| 5014 | SourceLocation TokLoc = GetTokenLoc(I); |
| 5015 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 5016 | case RangeBefore: |
| 5017 | AdvanceToken(); |
| 5018 | continue; |
| 5019 | case RangeAfter: |
| 5020 | case RangeOverlap: |
| 5021 | break; |
| 5022 | } |
| 5023 | break; |
| 5024 | } |
| 5025 | |
| 5026 | // Look at all of the tokens within this range. |
| 5027 | while (MoreTokens()) { |
| 5028 | const unsigned I = NextToken(); |
| 5029 | SourceLocation TokLoc = GetTokenLoc(I); |
| 5030 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 5031 | case RangeBefore: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5032 | llvm_unreachable("Infeasible"); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5033 | case RangeAfter: |
| 5034 | break; |
| 5035 | case RangeOverlap: |
| 5036 | Cursors[I] = cursor; |
| 5037 | AdvanceToken(); |
| 5038 | continue; |
| 5039 | } |
| 5040 | break; |
| 5041 | } |
| 5042 | |
| 5043 | // Save the preprocessing token index; restore the non-preprocessing |
| 5044 | // token index. |
| 5045 | PreprocessingTokIdx = TokIdx; |
| 5046 | TokIdx = SavedTokIdx; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5047 | return CXChildVisit_Recurse; |
| 5048 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5049 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5050 | if (cursorRange.isInvalid()) |
| 5051 | return CXChildVisit_Continue; |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 5052 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5053 | SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data); |
| 5054 | |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 5055 | // Adjust the annotated range based specific declarations. |
| 5056 | const enum CXCursorKind cursorK = clang_getCursorKind(cursor); |
| 5057 | if (cursorK >= CXCursor_FirstDecl && cursorK <= CXCursor_LastDecl) { |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 5058 | Decl *D = cxcursor::getCursorDecl(cursor); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 5059 | |
| 5060 | SourceLocation StartLoc; |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 5061 | if (const DeclaratorDecl *DD = dyn_cast_or_null<DeclaratorDecl>(D)) { |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 5062 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5063 | StartLoc = TI->getTypeLoc().getLocStart(); |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 5064 | } else if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(D)) { |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 5065 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 5066 | StartLoc = TI->getTypeLoc().getLocStart(); |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 5067 | } |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 5068 | |
| 5069 | if (StartLoc.isValid() && L.isValid() && |
| 5070 | SrcMgr.isBeforeInTranslationUnit(StartLoc, L)) |
| 5071 | cursorRange.setBegin(StartLoc); |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 5072 | } |
Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 5073 | |
Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 5074 | // If the location of the cursor occurs within a macro instantiation, record |
| 5075 | // the spelling location of the cursor in our annotation map. We can then |
| 5076 | // paper over the token labelings during a post-processing step to try and |
| 5077 | // get cursor mappings for tokens that are the *arguments* of a macro |
| 5078 | // instantiation. |
| 5079 | if (L.isMacroID()) { |
| 5080 | unsigned rawEncoding = SrcMgr.getSpellingLoc(L).getRawEncoding(); |
| 5081 | // Only invalidate the old annotation if it isn't part of a preprocessing |
| 5082 | // directive. Here we assume that the default construction of CXCursor |
| 5083 | // results in CXCursor.kind being an initialized value (i.e., 0). If |
| 5084 | // this isn't the case, we can fix by doing lookup + insertion. |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5085 | |
Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 5086 | CXCursor &oldC = Annotated[rawEncoding]; |
| 5087 | if (!clang_isPreprocessing(oldC.kind)) |
| 5088 | oldC = cursor; |
| 5089 | } |
| 5090 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5091 | const enum CXCursorKind K = clang_getCursorKind(parent); |
| 5092 | const CXCursor updateC = |
Ted Kremenek | d8b0a84 | 2010-08-25 22:16:02 +0000 | [diff] [blame] | 5093 | (clang_isInvalid(K) || K == CXCursor_TranslationUnit) |
| 5094 | ? clang_getNullCursor() : parent; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5095 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5096 | annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5097 | |
Argyrios Kyrtzidis | 5517b89 | 2011-06-27 19:42:20 +0000 | [diff] [blame] | 5098 | // Avoid having the cursor of an expression "overwrite" the annotation of the |
| 5099 | // variable declaration that it belongs to. |
| 5100 | // This can happen for C++ constructor expressions whose range generally |
| 5101 | // include the variable declaration, e.g.: |
| 5102 | // MyCXXClass foo; // Make sure we don't annotate 'foo' as a CallExpr cursor. |
| 5103 | if (clang_isExpression(cursorK)) { |
| 5104 | Expr *E = getCursorExpr(cursor); |
Argyrios Kyrtzidis | 8ccac3d | 2011-06-29 22:20:07 +0000 | [diff] [blame] | 5105 | if (Decl *D = getCursorParentDecl(cursor)) { |
Argyrios Kyrtzidis | 5517b89 | 2011-06-27 19:42:20 +0000 | [diff] [blame] | 5106 | const unsigned I = NextToken(); |
| 5107 | if (E->getLocStart().isValid() && D->getLocation().isValid() && |
| 5108 | E->getLocStart() == D->getLocation() && |
| 5109 | E->getLocStart() == GetTokenLoc(I)) { |
| 5110 | Cursors[I] = updateC; |
| 5111 | AdvanceToken(); |
| 5112 | } |
| 5113 | } |
| 5114 | } |
| 5115 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5116 | // Visit children to get their cursor information. |
| 5117 | const unsigned BeforeChildren = NextToken(); |
| 5118 | VisitChildren(cursor); |
| 5119 | const unsigned AfterChildren = NextToken(); |
| 5120 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5121 | // Scan the tokens that are at the end of the cursor, but are not captured |
| 5122 | // but the child cursors. |
| 5123 | annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 5124 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5125 | // Scan the tokens that are at the beginning of the cursor, but are not |
| 5126 | // capture by the child cursors. |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5127 | for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { |
| 5128 | if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 5129 | break; |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5130 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5131 | Cursors[I] = cursor; |
| 5132 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5133 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5134 | return CXChildVisit_Continue; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5135 | } |
| 5136 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 5137 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 5138 | CXCursor parent, |
| 5139 | CXClientData client_data) { |
| 5140 | return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); |
| 5141 | } |
| 5142 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5143 | namespace { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5144 | |
| 5145 | /// \brief Uses the macro expansions in the preprocessing record to find |
| 5146 | /// and mark tokens that are macro arguments. This info is used by the |
| 5147 | /// AnnotateTokensWorker. |
| 5148 | class MarkMacroArgTokensVisitor { |
| 5149 | SourceManager &SM; |
| 5150 | CXToken *Tokens; |
| 5151 | unsigned NumTokens; |
| 5152 | unsigned CurIdx; |
| 5153 | |
| 5154 | public: |
| 5155 | MarkMacroArgTokensVisitor(SourceManager &SM, |
| 5156 | CXToken *tokens, unsigned numTokens) |
| 5157 | : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { } |
| 5158 | |
| 5159 | CXChildVisitResult visit(CXCursor cursor, CXCursor parent) { |
| 5160 | if (cursor.kind != CXCursor_MacroExpansion) |
| 5161 | return CXChildVisit_Continue; |
| 5162 | |
| 5163 | SourceRange macroRange = getCursorMacroExpansion(cursor)->getSourceRange(); |
| 5164 | if (macroRange.getBegin() == macroRange.getEnd()) |
| 5165 | return CXChildVisit_Continue; // it's not a function macro. |
| 5166 | |
| 5167 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 5168 | if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx), |
| 5169 | macroRange.getBegin())) |
| 5170 | break; |
| 5171 | } |
| 5172 | |
| 5173 | if (CurIdx == NumTokens) |
| 5174 | return CXChildVisit_Break; |
| 5175 | |
| 5176 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 5177 | SourceLocation tokLoc = getTokenLoc(CurIdx); |
| 5178 | if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd())) |
| 5179 | break; |
| 5180 | |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 5181 | setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc)); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5182 | } |
| 5183 | |
| 5184 | if (CurIdx == NumTokens) |
| 5185 | return CXChildVisit_Break; |
| 5186 | |
| 5187 | return CXChildVisit_Continue; |
| 5188 | } |
| 5189 | |
| 5190 | private: |
| 5191 | SourceLocation getTokenLoc(unsigned tokI) { |
| 5192 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); |
| 5193 | } |
| 5194 | |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 5195 | void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5196 | // The third field is reserved and currently not used. Use it here |
| 5197 | // to mark macro arg expanded tokens with their expanded locations. |
| 5198 | Tokens[tokI].int_data[3] = loc.getRawEncoding(); |
| 5199 | } |
| 5200 | }; |
| 5201 | |
| 5202 | } // end anonymous namespace |
| 5203 | |
| 5204 | static CXChildVisitResult |
| 5205 | MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent, |
| 5206 | CXClientData client_data) { |
| 5207 | return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor, |
| 5208 | parent); |
| 5209 | } |
| 5210 | |
| 5211 | namespace { |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5212 | struct clang_annotateTokens_Data { |
| 5213 | CXTranslationUnit TU; |
| 5214 | ASTUnit *CXXUnit; |
| 5215 | CXToken *Tokens; |
| 5216 | unsigned NumTokens; |
| 5217 | CXCursor *Cursors; |
| 5218 | }; |
| 5219 | } |
| 5220 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 5221 | static void annotatePreprocessorTokens(CXTranslationUnit TU, |
| 5222 | SourceRange RegionOfInterest, |
| 5223 | AnnotateTokensData &Annotated) { |
| 5224 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 5225 | |
| 5226 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 5227 | std::pair<FileID, unsigned> BeginLocInfo |
| 5228 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin()); |
| 5229 | std::pair<FileID, unsigned> EndLocInfo |
| 5230 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd()); |
| 5231 | |
| 5232 | if (BeginLocInfo.first != EndLocInfo.first) |
| 5233 | return; |
| 5234 | |
| 5235 | StringRef Buffer; |
| 5236 | bool Invalid = false; |
| 5237 | Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 5238 | if (Buffer.empty() || Invalid) |
| 5239 | return; |
| 5240 | |
| 5241 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5242 | CXXUnit->getASTContext().getLangOpts(), |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 5243 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, |
| 5244 | Buffer.end()); |
| 5245 | Lex.SetCommentRetentionState(true); |
| 5246 | |
| 5247 | // Lex tokens in raw mode until we hit the end of the range, to avoid |
| 5248 | // entering #includes or expanding macros. |
| 5249 | while (true) { |
| 5250 | Token Tok; |
| 5251 | Lex.LexFromRawLexer(Tok); |
| 5252 | |
| 5253 | reprocess: |
| 5254 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
| 5255 | // We have found a preprocessing directive. Gobble it up so that we |
| 5256 | // don't see it while preprocessing these tokens later, but keep track |
| 5257 | // of all of the token locations inside this preprocessing directive so |
| 5258 | // that we can annotate them appropriately. |
| 5259 | // |
| 5260 | // FIXME: Some simple tests here could identify macro definitions and |
| 5261 | // #undefs, to provide specific cursor kinds for those. |
| 5262 | SmallVector<SourceLocation, 32> Locations; |
| 5263 | do { |
| 5264 | Locations.push_back(Tok.getLocation()); |
| 5265 | Lex.LexFromRawLexer(Tok); |
| 5266 | } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof)); |
| 5267 | |
| 5268 | using namespace cxcursor; |
| 5269 | CXCursor Cursor |
| 5270 | = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(), |
| 5271 | Locations.back()), |
| 5272 | TU); |
| 5273 | for (unsigned I = 0, N = Locations.size(); I != N; ++I) { |
| 5274 | Annotated[Locations[I].getRawEncoding()] = Cursor; |
| 5275 | } |
| 5276 | |
| 5277 | if (Tok.isAtStartOfLine()) |
| 5278 | goto reprocess; |
| 5279 | |
| 5280 | continue; |
| 5281 | } |
| 5282 | |
| 5283 | if (Tok.is(tok::eof)) |
| 5284 | break; |
| 5285 | } |
| 5286 | } |
| 5287 | |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5288 | // This gets run a separate thread to avoid stack blowout. |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5289 | static void clang_annotateTokensImpl(void *UserData) { |
| 5290 | CXTranslationUnit TU = ((clang_annotateTokens_Data*)UserData)->TU; |
| 5291 | ASTUnit *CXXUnit = ((clang_annotateTokens_Data*)UserData)->CXXUnit; |
| 5292 | CXToken *Tokens = ((clang_annotateTokens_Data*)UserData)->Tokens; |
| 5293 | const unsigned NumTokens = ((clang_annotateTokens_Data*)UserData)->NumTokens; |
| 5294 | CXCursor *Cursors = ((clang_annotateTokens_Data*)UserData)->Cursors; |
| 5295 | |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 5296 | CIndexer *CXXIdx = (CIndexer*)TU->CIdx; |
| 5297 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) |
Argyrios Kyrtzidis | 81b5ac3 | 2012-03-28 02:49:54 +0000 | [diff] [blame] | 5298 | setThreadBackgroundPriority(); |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 5299 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5300 | // Determine the region of interest, which contains all of the tokens. |
| 5301 | SourceRange RegionOfInterest; |
| 5302 | RegionOfInterest.setBegin( |
| 5303 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0]))); |
| 5304 | RegionOfInterest.setEnd( |
| 5305 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, |
| 5306 | Tokens[NumTokens-1]))); |
| 5307 | |
| 5308 | // A mapping from the source locations found when re-lexing or traversing the |
| 5309 | // region of interest to the corresponding cursors. |
| 5310 | AnnotateTokensData Annotated; |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 5311 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5312 | // Relex the tokens within the source range to look for preprocessing |
| 5313 | // directives. |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 5314 | annotatePreprocessorTokens(TU, RegionOfInterest, Annotated); |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5315 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5316 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) { |
| 5317 | // Search and mark tokens that are macro argument expansions. |
| 5318 | MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(), |
| 5319 | Tokens, NumTokens); |
| 5320 | CursorVisitor MacroArgMarker(TU, |
| 5321 | MarkMacroArgTokensVisitorDelegate, &Visitor, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 5322 | /*VisitPreprocessorLast=*/true, |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 5323 | /*VisitIncludedEntities=*/false, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 5324 | RegionOfInterest); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5325 | MacroArgMarker.visitPreprocessedEntitiesInRegion(); |
| 5326 | } |
| 5327 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5328 | // Annotate all of the source locations in the region of interest that map to |
| 5329 | // a specific cursor. |
| 5330 | AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens, |
| 5331 | TU, RegionOfInterest); |
| 5332 | |
| 5333 | // FIXME: We use a ridiculous stack size here because the data-recursion |
| 5334 | // algorithm uses a large stack frame than the non-data recursive version, |
| 5335 | // and AnnotationTokensWorker currently transforms the data-recursion |
| 5336 | // algorithm back into a traditional recursion by explicitly calling |
| 5337 | // VisitChildren(). We will need to remove this explicit recursive call. |
| 5338 | W.AnnotateTokens(); |
| 5339 | |
| 5340 | // If we ran into any entities that involve context-sensitive keywords, |
| 5341 | // take another pass through the tokens to mark them as such. |
| 5342 | if (W.hasContextSensitiveKeywords()) { |
| 5343 | for (unsigned I = 0; I != NumTokens; ++I) { |
| 5344 | if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier) |
| 5345 | continue; |
| 5346 | |
| 5347 | if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) { |
| 5348 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 5349 | if (ObjCPropertyDecl *Property |
| 5350 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) { |
| 5351 | if (Property->getPropertyAttributesAsWritten() != 0 && |
| 5352 | llvm::StringSwitch<bool>(II->getName()) |
| 5353 | .Case("readonly", true) |
| 5354 | .Case("assign", true) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5355 | .Case("unsafe_unretained", true) |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5356 | .Case("readwrite", true) |
| 5357 | .Case("retain", true) |
| 5358 | .Case("copy", true) |
| 5359 | .Case("nonatomic", true) |
| 5360 | .Case("atomic", true) |
| 5361 | .Case("getter", true) |
| 5362 | .Case("setter", true) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5363 | .Case("strong", true) |
| 5364 | .Case("weak", true) |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5365 | .Default(false)) |
| 5366 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5367 | } |
| 5368 | continue; |
| 5369 | } |
| 5370 | |
| 5371 | if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl || |
| 5372 | Cursors[I].kind == CXCursor_ObjCClassMethodDecl) { |
| 5373 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 5374 | if (llvm::StringSwitch<bool>(II->getName()) |
| 5375 | .Case("in", true) |
| 5376 | .Case("out", true) |
| 5377 | .Case("inout", true) |
| 5378 | .Case("oneway", true) |
| 5379 | .Case("bycopy", true) |
| 5380 | .Case("byref", true) |
| 5381 | .Default(false)) |
| 5382 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5383 | continue; |
| 5384 | } |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 5385 | |
| 5386 | if (Cursors[I].kind == CXCursor_CXXFinalAttr || |
| 5387 | Cursors[I].kind == CXCursor_CXXOverrideAttr) { |
| 5388 | Tokens[I].int_data[0] = CXToken_Keyword; |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5389 | continue; |
| 5390 | } |
| 5391 | } |
| 5392 | } |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5393 | } |
| 5394 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 5395 | extern "C" { |
| 5396 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5397 | void clang_annotateTokens(CXTranslationUnit TU, |
| 5398 | CXToken *Tokens, unsigned NumTokens, |
| 5399 | CXCursor *Cursors) { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5400 | |
| 5401 | if (NumTokens == 0 || !Tokens || !Cursors) |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5402 | return; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5403 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5404 | // Any token we don't specifically annotate will have a NULL cursor. |
| 5405 | CXCursor C = clang_getNullCursor(); |
| 5406 | for (unsigned I = 0; I != NumTokens; ++I) |
| 5407 | Cursors[I] = C; |
| 5408 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5409 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5410 | if (!CXXUnit) |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5411 | return; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5412 | |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 5413 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5414 | |
| 5415 | clang_annotateTokens_Data data = { TU, CXXUnit, Tokens, NumTokens, Cursors }; |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5416 | llvm::CrashRecoveryContext CRC; |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5417 | if (!RunSafely(CRC, clang_annotateTokensImpl, &data, |
Ted Kremenek | 6c53fdd | 2010-11-14 17:47:35 +0000 | [diff] [blame] | 5418 | GetSafetyThreadStackSize() * 2)) { |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5419 | fprintf(stderr, "libclang: crash detected while annotating tokens\n"); |
| 5420 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5421 | } |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5422 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5423 | } // end: extern "C" |
| 5424 | |
| 5425 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 5426 | // Operations for querying linkage of a cursor. |
| 5427 | //===----------------------------------------------------------------------===// |
| 5428 | |
| 5429 | extern "C" { |
| 5430 | CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 5431 | if (!clang_isDeclaration(cursor.kind)) |
| 5432 | return CXLinkage_Invalid; |
| 5433 | |
Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 5434 | Decl *D = cxcursor::getCursorDecl(cursor); |
| 5435 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
| 5436 | switch (ND->getLinkage()) { |
| 5437 | case NoLinkage: return CXLinkage_NoLinkage; |
| 5438 | case InternalLinkage: return CXLinkage_Internal; |
| 5439 | case UniqueExternalLinkage: return CXLinkage_UniqueExternal; |
| 5440 | case ExternalLinkage: return CXLinkage_External; |
| 5441 | }; |
| 5442 | |
| 5443 | return CXLinkage_Invalid; |
| 5444 | } |
| 5445 | } // end: extern "C" |
| 5446 | |
| 5447 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5448 | // Operations for querying language of a cursor. |
| 5449 | //===----------------------------------------------------------------------===// |
| 5450 | |
| 5451 | static CXLanguageKind getDeclLanguage(const Decl *D) { |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 5452 | if (!D) |
| 5453 | return CXLanguage_C; |
| 5454 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5455 | switch (D->getKind()) { |
| 5456 | default: |
| 5457 | break; |
| 5458 | case Decl::ImplicitParam: |
| 5459 | case Decl::ObjCAtDefsField: |
| 5460 | case Decl::ObjCCategory: |
| 5461 | case Decl::ObjCCategoryImpl: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5462 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5463 | case Decl::ObjCImplementation: |
| 5464 | case Decl::ObjCInterface: |
| 5465 | case Decl::ObjCIvar: |
| 5466 | case Decl::ObjCMethod: |
| 5467 | case Decl::ObjCProperty: |
| 5468 | case Decl::ObjCPropertyImpl: |
| 5469 | case Decl::ObjCProtocol: |
| 5470 | return CXLanguage_ObjC; |
| 5471 | case Decl::CXXConstructor: |
| 5472 | case Decl::CXXConversion: |
| 5473 | case Decl::CXXDestructor: |
| 5474 | case Decl::CXXMethod: |
| 5475 | case Decl::CXXRecord: |
| 5476 | case Decl::ClassTemplate: |
| 5477 | case Decl::ClassTemplatePartialSpecialization: |
| 5478 | case Decl::ClassTemplateSpecialization: |
| 5479 | case Decl::Friend: |
| 5480 | case Decl::FriendTemplate: |
| 5481 | case Decl::FunctionTemplate: |
| 5482 | case Decl::LinkageSpec: |
| 5483 | case Decl::Namespace: |
| 5484 | case Decl::NamespaceAlias: |
| 5485 | case Decl::NonTypeTemplateParm: |
| 5486 | case Decl::StaticAssert: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5487 | case Decl::TemplateTemplateParm: |
| 5488 | case Decl::TemplateTypeParm: |
| 5489 | case Decl::UnresolvedUsingTypename: |
| 5490 | case Decl::UnresolvedUsingValue: |
| 5491 | case Decl::Using: |
| 5492 | case Decl::UsingDirective: |
| 5493 | case Decl::UsingShadow: |
| 5494 | return CXLanguage_CPlusPlus; |
| 5495 | } |
| 5496 | |
| 5497 | return CXLanguage_C; |
| 5498 | } |
| 5499 | |
| 5500 | extern "C" { |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5501 | |
| 5502 | enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { |
| 5503 | if (clang_isDeclaration(cursor.kind)) |
| 5504 | if (Decl *D = cxcursor::getCursorDecl(cursor)) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5505 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()) |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5506 | return CXAvailability_Available; |
| 5507 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5508 | switch (D->getAvailability()) { |
| 5509 | case AR_Available: |
| 5510 | case AR_NotYetIntroduced: |
| 5511 | return CXAvailability_Available; |
| 5512 | |
| 5513 | case AR_Deprecated: |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5514 | return CXAvailability_Deprecated; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5515 | |
| 5516 | case AR_Unavailable: |
| 5517 | return CXAvailability_NotAvailable; |
| 5518 | } |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5519 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5520 | |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5521 | return CXAvailability_Available; |
| 5522 | } |
| 5523 | |
Douglas Gregor | cc88966 | 2012-05-08 00:14:45 +0000 | [diff] [blame] | 5524 | static CXVersion convertVersion(VersionTuple In) { |
| 5525 | CXVersion Out = { -1, -1, -1 }; |
| 5526 | if (In.empty()) |
| 5527 | return Out; |
| 5528 | |
| 5529 | Out.Major = In.getMajor(); |
| 5530 | |
| 5531 | if (llvm::Optional<unsigned> Minor = In.getMinor()) |
| 5532 | Out.Minor = *Minor; |
| 5533 | else |
| 5534 | return Out; |
| 5535 | |
| 5536 | if (llvm::Optional<unsigned> Subminor = In.getSubminor()) |
| 5537 | Out.Subminor = *Subminor; |
| 5538 | |
| 5539 | return Out; |
| 5540 | } |
| 5541 | |
| 5542 | int clang_getCursorPlatformAvailability(CXCursor cursor, |
| 5543 | int *always_deprecated, |
| 5544 | CXString *deprecated_message, |
| 5545 | int *always_unavailable, |
| 5546 | CXString *unavailable_message, |
| 5547 | CXPlatformAvailability *availability, |
| 5548 | int availability_size) { |
| 5549 | if (always_deprecated) |
| 5550 | *always_deprecated = 0; |
| 5551 | if (deprecated_message) |
| 5552 | *deprecated_message = cxstring::createCXString("", /*DupString=*/false); |
| 5553 | if (always_unavailable) |
| 5554 | *always_unavailable = 0; |
| 5555 | if (unavailable_message) |
| 5556 | *unavailable_message = cxstring::createCXString("", /*DupString=*/false); |
| 5557 | |
| 5558 | if (!clang_isDeclaration(cursor.kind)) |
| 5559 | return 0; |
| 5560 | |
| 5561 | Decl *D = cxcursor::getCursorDecl(cursor); |
| 5562 | if (!D) |
| 5563 | return 0; |
| 5564 | |
| 5565 | int N = 0; |
| 5566 | for (Decl::attr_iterator A = D->attr_begin(), AEnd = D->attr_end(); A != AEnd; |
| 5567 | ++A) { |
| 5568 | if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) { |
| 5569 | if (always_deprecated) |
| 5570 | *always_deprecated = 1; |
| 5571 | if (deprecated_message) |
| 5572 | *deprecated_message = cxstring::createCXString(Deprecated->getMessage()); |
| 5573 | continue; |
| 5574 | } |
| 5575 | |
| 5576 | if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) { |
| 5577 | if (always_unavailable) |
| 5578 | *always_unavailable = 1; |
| 5579 | if (unavailable_message) { |
| 5580 | *unavailable_message |
| 5581 | = cxstring::createCXString(Unavailable->getMessage()); |
| 5582 | } |
| 5583 | continue; |
| 5584 | } |
| 5585 | |
| 5586 | if (AvailabilityAttr *Avail = dyn_cast<AvailabilityAttr>(*A)) { |
| 5587 | if (N < availability_size) { |
| 5588 | availability[N].Platform |
| 5589 | = cxstring::createCXString(Avail->getPlatform()->getName()); |
| 5590 | availability[N].Introduced = convertVersion(Avail->getIntroduced()); |
| 5591 | availability[N].Deprecated = convertVersion(Avail->getDeprecated()); |
| 5592 | availability[N].Obsoleted = convertVersion(Avail->getObsoleted()); |
| 5593 | availability[N].Unavailable = Avail->getUnavailable(); |
| 5594 | availability[N].Message = cxstring::createCXString(Avail->getMessage()); |
| 5595 | } |
| 5596 | ++N; |
| 5597 | } |
| 5598 | } |
| 5599 | |
| 5600 | return N; |
| 5601 | } |
| 5602 | |
| 5603 | void clang_disposeCXPlatformAvailability(CXPlatformAvailability *availability) { |
| 5604 | clang_disposeString(availability->Platform); |
| 5605 | clang_disposeString(availability->Message); |
| 5606 | } |
| 5607 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5608 | CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { |
| 5609 | if (clang_isDeclaration(cursor.kind)) |
| 5610 | return getDeclLanguage(cxcursor::getCursorDecl(cursor)); |
| 5611 | |
| 5612 | return CXLanguage_Invalid; |
| 5613 | } |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5614 | |
| 5615 | /// \brief If the given cursor is the "templated" declaration |
| 5616 | /// descibing a class or function template, return the class or |
| 5617 | /// function template. |
| 5618 | static Decl *maybeGetTemplateCursor(Decl *D) { |
| 5619 | if (!D) |
| 5620 | return 0; |
| 5621 | |
| 5622 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5623 | if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate()) |
| 5624 | return FunTmpl; |
| 5625 | |
| 5626 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
| 5627 | if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate()) |
| 5628 | return ClassTmpl; |
| 5629 | |
| 5630 | return D; |
| 5631 | } |
| 5632 | |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5633 | CXCursor clang_getCursorSemanticParent(CXCursor cursor) { |
| 5634 | if (clang_isDeclaration(cursor.kind)) { |
| 5635 | if (Decl *D = getCursorDecl(cursor)) { |
| 5636 | DeclContext *DC = D->getDeclContext(); |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5637 | if (!DC) |
| 5638 | return clang_getNullCursor(); |
| 5639 | |
| 5640 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 5641 | getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5642 | } |
| 5643 | } |
| 5644 | |
| 5645 | if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { |
| 5646 | if (Decl *D = getCursorDecl(cursor)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5647 | return MakeCXCursor(D, getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5648 | } |
| 5649 | |
| 5650 | return clang_getNullCursor(); |
| 5651 | } |
| 5652 | |
| 5653 | CXCursor clang_getCursorLexicalParent(CXCursor cursor) { |
| 5654 | if (clang_isDeclaration(cursor.kind)) { |
| 5655 | if (Decl *D = getCursorDecl(cursor)) { |
| 5656 | DeclContext *DC = D->getLexicalDeclContext(); |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5657 | if (!DC) |
| 5658 | return clang_getNullCursor(); |
| 5659 | |
| 5660 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 5661 | getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5662 | } |
| 5663 | } |
| 5664 | |
| 5665 | // FIXME: Note that we can't easily compute the lexical context of a |
| 5666 | // statement or expression, so we return nothing. |
| 5667 | return clang_getNullCursor(); |
| 5668 | } |
| 5669 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 5670 | CXFile clang_getIncludedFile(CXCursor cursor) { |
| 5671 | if (cursor.kind != CXCursor_InclusionDirective) |
| 5672 | return 0; |
| 5673 | |
| 5674 | InclusionDirective *ID = getCursorInclusionDirective(cursor); |
| 5675 | return (void *)ID->getFile(); |
| 5676 | } |
| 5677 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5678 | } // end: extern "C" |
| 5679 | |
Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 5680 | |
| 5681 | //===----------------------------------------------------------------------===// |
| 5682 | // C++ AST instrospection. |
| 5683 | //===----------------------------------------------------------------------===// |
| 5684 | |
| 5685 | extern "C" { |
| 5686 | unsigned clang_CXXMethod_isStatic(CXCursor C) { |
| 5687 | if (!clang_isDeclaration(C.kind)) |
| 5688 | return 0; |
Douglas Gregor | 49f6f54 | 2010-08-31 22:12:17 +0000 | [diff] [blame] | 5689 | |
| 5690 | CXXMethodDecl *Method = 0; |
| 5691 | Decl *D = cxcursor::getCursorDecl(C); |
| 5692 | if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 5693 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 5694 | else |
| 5695 | Method = dyn_cast_or_null<CXXMethodDecl>(D); |
| 5696 | return (Method && Method->isStatic()) ? 1 : 0; |
Ted Kremenek | 40b492a | 2010-05-17 20:12:45 +0000 | [diff] [blame] | 5697 | } |
Ted Kremenek | b12903e | 2010-05-18 22:32:15 +0000 | [diff] [blame] | 5698 | |
Douglas Gregor | 211924b | 2011-05-12 15:17:24 +0000 | [diff] [blame] | 5699 | unsigned clang_CXXMethod_isVirtual(CXCursor C) { |
| 5700 | if (!clang_isDeclaration(C.kind)) |
| 5701 | return 0; |
| 5702 | |
| 5703 | CXXMethodDecl *Method = 0; |
| 5704 | Decl *D = cxcursor::getCursorDecl(C); |
| 5705 | if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 5706 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 5707 | else |
| 5708 | Method = dyn_cast_or_null<CXXMethodDecl>(D); |
| 5709 | return (Method && Method->isVirtual()) ? 1 : 0; |
| 5710 | } |
Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 5711 | } // end: extern "C" |
| 5712 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5713 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5714 | // Attribute introspection. |
| 5715 | //===----------------------------------------------------------------------===// |
| 5716 | |
| 5717 | extern "C" { |
| 5718 | CXType clang_getIBOutletCollectionType(CXCursor C) { |
| 5719 | if (C.kind != CXCursor_IBOutletCollectionAttr) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5720 | return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C)); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5721 | |
| 5722 | IBOutletCollectionAttr *A = |
| 5723 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); |
| 5724 | |
Argyrios Kyrtzidis | 18aa2ff | 2011-09-13 18:49:52 +0000 | [diff] [blame] | 5725 | return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C)); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5726 | } |
| 5727 | } // end: extern "C" |
| 5728 | |
| 5729 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5730 | // Inspecting memory usage. |
| 5731 | //===----------------------------------------------------------------------===// |
| 5732 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5733 | typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5734 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5735 | static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries, |
| 5736 | enum CXTUResourceUsageKind k, |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5737 | unsigned long amount) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5738 | CXTUResourceUsageEntry entry = { k, amount }; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5739 | entries.push_back(entry); |
| 5740 | } |
| 5741 | |
| 5742 | extern "C" { |
| 5743 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5744 | const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5745 | const char *str = ""; |
| 5746 | switch (kind) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5747 | case CXTUResourceUsage_AST: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5748 | str = "ASTContext: expressions, declarations, and types"; |
| 5749 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5750 | case CXTUResourceUsage_Identifiers: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5751 | str = "ASTContext: identifiers"; |
| 5752 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5753 | case CXTUResourceUsage_Selectors: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5754 | str = "ASTContext: selectors"; |
Ted Kremenek | e294ab7 | 2011-04-19 04:36:17 +0000 | [diff] [blame] | 5755 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5756 | case CXTUResourceUsage_GlobalCompletionResults: |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5757 | str = "Code completion: cached global results"; |
Ted Kremenek | e294ab7 | 2011-04-19 04:36:17 +0000 | [diff] [blame] | 5758 | break; |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 5759 | case CXTUResourceUsage_SourceManagerContentCache: |
| 5760 | str = "SourceManager: content cache allocator"; |
| 5761 | break; |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5762 | case CXTUResourceUsage_AST_SideTables: |
| 5763 | str = "ASTContext: side tables"; |
| 5764 | break; |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5765 | case CXTUResourceUsage_SourceManager_Membuffer_Malloc: |
| 5766 | str = "SourceManager: malloc'ed memory buffers"; |
| 5767 | break; |
| 5768 | case CXTUResourceUsage_SourceManager_Membuffer_MMap: |
| 5769 | str = "SourceManager: mmap'ed memory buffers"; |
| 5770 | break; |
Ted Kremenek | e9b5f3d | 2011-04-28 23:46:20 +0000 | [diff] [blame] | 5771 | case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc: |
| 5772 | str = "ExternalASTSource: malloc'ed memory buffers"; |
| 5773 | break; |
| 5774 | case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap: |
| 5775 | str = "ExternalASTSource: mmap'ed memory buffers"; |
| 5776 | break; |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5777 | case CXTUResourceUsage_Preprocessor: |
| 5778 | str = "Preprocessor: malloc'ed memory"; |
| 5779 | break; |
| 5780 | case CXTUResourceUsage_PreprocessingRecord: |
| 5781 | str = "Preprocessor: PreprocessingRecord"; |
| 5782 | break; |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5783 | case CXTUResourceUsage_SourceManager_DataStructures: |
| 5784 | str = "SourceManager: data structures and tables"; |
| 5785 | break; |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 5786 | case CXTUResourceUsage_Preprocessor_HeaderSearch: |
| 5787 | str = "Preprocessor: header search tables"; |
| 5788 | break; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5789 | } |
| 5790 | return str; |
| 5791 | } |
| 5792 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5793 | CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5794 | if (!TU) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5795 | CXTUResourceUsage usage = { (void*) 0, 0, 0 }; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5796 | return usage; |
| 5797 | } |
| 5798 | |
| 5799 | ASTUnit *astUnit = static_cast<ASTUnit*>(TU->TUData); |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 5800 | OwningPtr<MemUsageEntries> entries(new MemUsageEntries()); |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5801 | ASTContext &astContext = astUnit->getASTContext(); |
| 5802 | |
| 5803 | // How much memory is used by AST nodes and types? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5804 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST, |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5805 | (unsigned long) astContext.getASTAllocatedMemory()); |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5806 | |
| 5807 | // How much memory is used by identifiers? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5808 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5809 | (unsigned long) astContext.Idents.getAllocator().getTotalMemory()); |
| 5810 | |
| 5811 | // How much memory is used for selectors? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5812 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5813 | (unsigned long) astContext.Selectors.getTotalMemory()); |
| 5814 | |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5815 | // How much memory is used by ASTContext's side tables? |
| 5816 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables, |
| 5817 | (unsigned long) astContext.getSideTableAllocatedMemory()); |
| 5818 | |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5819 | // How much memory is used for caching global code completion results? |
| 5820 | unsigned long completionBytes = 0; |
| 5821 | if (GlobalCodeCompletionAllocator *completionAllocator = |
| 5822 | astUnit->getCachedCompletionAllocator().getPtr()) { |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5823 | completionBytes = completionAllocator->getTotalMemory(); |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5824 | } |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 5825 | createCXTUResourceUsageEntry(*entries, |
| 5826 | CXTUResourceUsage_GlobalCompletionResults, |
| 5827 | completionBytes); |
| 5828 | |
| 5829 | // How much memory is being used by SourceManager's content cache? |
| 5830 | createCXTUResourceUsageEntry(*entries, |
| 5831 | CXTUResourceUsage_SourceManagerContentCache, |
| 5832 | (unsigned long) astContext.getSourceManager().getContentCacheSize()); |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5833 | |
| 5834 | // How much memory is being used by the MemoryBuffer's in SourceManager? |
| 5835 | const SourceManager::MemoryBufferSizes &srcBufs = |
| 5836 | astUnit->getSourceManager().getMemoryBufferSizes(); |
| 5837 | |
| 5838 | createCXTUResourceUsageEntry(*entries, |
| 5839 | CXTUResourceUsage_SourceManager_Membuffer_Malloc, |
| 5840 | (unsigned long) srcBufs.malloc_bytes); |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5841 | createCXTUResourceUsageEntry(*entries, |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5842 | CXTUResourceUsage_SourceManager_Membuffer_MMap, |
| 5843 | (unsigned long) srcBufs.mmap_bytes); |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5844 | createCXTUResourceUsageEntry(*entries, |
| 5845 | CXTUResourceUsage_SourceManager_DataStructures, |
| 5846 | (unsigned long) astContext.getSourceManager() |
| 5847 | .getDataStructureSizes()); |
Ted Kremenek | e9b5f3d | 2011-04-28 23:46:20 +0000 | [diff] [blame] | 5848 | |
| 5849 | // How much memory is being used by the ExternalASTSource? |
| 5850 | if (ExternalASTSource *esrc = astContext.getExternalSource()) { |
| 5851 | const ExternalASTSource::MemoryBufferSizes &sizes = |
| 5852 | esrc->getMemoryBufferSizes(); |
| 5853 | |
| 5854 | createCXTUResourceUsageEntry(*entries, |
| 5855 | CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc, |
| 5856 | (unsigned long) sizes.malloc_bytes); |
| 5857 | createCXTUResourceUsageEntry(*entries, |
| 5858 | CXTUResourceUsage_ExternalASTSource_Membuffer_MMap, |
| 5859 | (unsigned long) sizes.mmap_bytes); |
| 5860 | } |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5861 | |
| 5862 | // How much memory is being used by the Preprocessor? |
| 5863 | Preprocessor &pp = astUnit->getPreprocessor(); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5864 | createCXTUResourceUsageEntry(*entries, |
| 5865 | CXTUResourceUsage_Preprocessor, |
Argyrios Kyrtzidis | c5c5e92 | 2011-06-29 22:20:04 +0000 | [diff] [blame] | 5866 | pp.getTotalMemory()); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5867 | |
| 5868 | if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) { |
| 5869 | createCXTUResourceUsageEntry(*entries, |
| 5870 | CXTUResourceUsage_PreprocessingRecord, |
| 5871 | pRec->getTotalMemory()); |
| 5872 | } |
| 5873 | |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 5874 | createCXTUResourceUsageEntry(*entries, |
| 5875 | CXTUResourceUsage_Preprocessor_HeaderSearch, |
| 5876 | pp.getHeaderSearchInfo().getTotalMemory()); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5877 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5878 | CXTUResourceUsage usage = { (void*) entries.get(), |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5879 | (unsigned) entries->size(), |
| 5880 | entries->size() ? &(*entries)[0] : 0 }; |
| 5881 | entries.take(); |
| 5882 | return usage; |
| 5883 | } |
| 5884 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5885 | void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5886 | if (usage.data) |
| 5887 | delete (MemUsageEntries*) usage.data; |
| 5888 | } |
| 5889 | |
| 5890 | } // end extern "C" |
| 5891 | |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 5892 | void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) { |
| 5893 | CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU); |
| 5894 | for (unsigned I = 0; I != Usage.numEntries; ++I) |
| 5895 | fprintf(stderr, " %s: %lu\n", |
| 5896 | clang_getTUResourceUsageName(Usage.entries[I].kind), |
| 5897 | Usage.entries[I].amount); |
| 5898 | |
| 5899 | clang_disposeCXTUResourceUsage(Usage); |
| 5900 | } |
| 5901 | |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5902 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5903 | // Misc. utility functions. |
| 5904 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 5905 | |
Daniel Dunbar | abdce7a | 2010-11-05 17:21:46 +0000 | [diff] [blame] | 5906 | /// Default to using an 8 MB stack size on "safety" threads. |
| 5907 | static unsigned SafetyStackThreadSize = 8 << 20; |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 5908 | |
| 5909 | namespace clang { |
| 5910 | |
| 5911 | bool RunSafely(llvm::CrashRecoveryContext &CRC, |
Ted Kremenek | 6c53fdd | 2010-11-14 17:47:35 +0000 | [diff] [blame] | 5912 | void (*Fn)(void*), void *UserData, |
| 5913 | unsigned Size) { |
| 5914 | if (!Size) |
| 5915 | Size = GetSafetyThreadStackSize(); |
| 5916 | if (Size) |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 5917 | return CRC.RunSafelyOnThread(Fn, UserData, Size); |
| 5918 | return CRC.RunSafely(Fn, UserData); |
| 5919 | } |
| 5920 | |
| 5921 | unsigned GetSafetyThreadStackSize() { |
| 5922 | return SafetyStackThreadSize; |
| 5923 | } |
| 5924 | |
| 5925 | void SetSafetyThreadStackSize(unsigned Value) { |
| 5926 | SafetyStackThreadSize = Value; |
| 5927 | } |
| 5928 | |
Argyrios Kyrtzidis | 8e7c48a | 2012-03-28 02:49:50 +0000 | [diff] [blame] | 5929 | } |
| 5930 | |
Argyrios Kyrtzidis | 81b5ac3 | 2012-03-28 02:49:54 +0000 | [diff] [blame] | 5931 | void clang::setThreadBackgroundPriority() { |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 5932 | // FIXME: Move to llvm/Support and make it cross-platform. |
| 5933 | #ifdef __APPLE__ |
| 5934 | setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG); |
| 5935 | #endif |
| 5936 | } |
| 5937 | |
Argyrios Kyrtzidis | 9793428 | 2012-04-11 03:52:18 +0000 | [diff] [blame] | 5938 | void cxindex::printDiagsToStderr(ASTUnit *Unit) { |
| 5939 | if (!Unit) |
| 5940 | return; |
| 5941 | |
| 5942 | for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), |
| 5943 | DEnd = Unit->stored_diag_end(); |
| 5944 | D != DEnd; ++D) { |
| 5945 | CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOpts()); |
| 5946 | CXString Msg = clang_formatDiagnostic(&Diag, |
| 5947 | clang_defaultDiagnosticDisplayOptions()); |
| 5948 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 5949 | clang_disposeString(Msg); |
| 5950 | } |
| 5951 | #ifdef LLVM_ON_WIN32 |
| 5952 | // On Windows, force a flush, since there may be multiple copies of |
| 5953 | // stderr and stdout in the file system, all with different buffers |
| 5954 | // but writing to the same device. |
| 5955 | fflush(stderr); |
| 5956 | #endif |
| 5957 | } |
| 5958 | |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5959 | extern "C" { |
| 5960 | |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 5961 | CXString clang_getClangVersion() { |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 5962 | return createCXString(getClangFullVersion()); |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5963 | } |
| 5964 | |
| 5965 | } // end: extern "C" |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5966 | |