Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 1 | //===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 7 | // |
Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 10 | // This file implements the main API hooks in the Clang-C Source Indexing |
| 11 | // library. |
Ted Kremenek | d2fa566 | 2009-08-26 22:36:44 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 15 | #include "CIndexer.h" |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 16 | #include "CXCursor.h" |
Ted Kremenek | 0a90d32 | 2010-11-17 23:24:11 +0000 | [diff] [blame] | 17 | #include "CXTranslationUnit.h" |
Ted Kremenek | ed12273 | 2010-11-16 01:56:27 +0000 | [diff] [blame] | 18 | #include "CXString.h" |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 19 | #include "CXType.h" |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 20 | #include "CXSourceLocation.h" |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 21 | #include "CIndexDiagnostic.h" |
Argyrios Kyrtzidis | e397bf1 | 2011-11-03 19:02:34 +0000 | [diff] [blame] | 22 | #include "CursorVisitor.h" |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 23 | |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 24 | #include "clang/Basic/Version.h" |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 25 | |
Steve Naroff | fb57042 | 2009-09-22 19:25:29 +0000 | [diff] [blame] | 26 | #include "clang/AST/StmtVisitor.h" |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 27 | #include "clang/Basic/Diagnostic.h" |
| 28 | #include "clang/Frontend/ASTUnit.h" |
| 29 | #include "clang/Frontend/CompilerInstance.h" |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 30 | #include "clang/Frontend/FrontendDiagnostic.h" |
Ted Kremenek | d821065 | 2010-01-06 23:43:31 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Lexer.h" |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 32 | #include "clang/Lex/HeaderSearch.h" |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 33 | #include "clang/Lex/PreprocessingRecord.h" |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 34 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/STLExtras.h" |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/Optional.h" |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/StringSwitch.h" |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 38 | #include "clang/Analysis/Support/SaveAndRestore.h" |
Daniel Dunbar | c7df4f3 | 2010-08-18 18:43:14 +0000 | [diff] [blame] | 39 | #include "llvm/Support/CrashRecoveryContext.h" |
Daniel Dunbar | 48615ff | 2010-10-08 19:30:33 +0000 | [diff] [blame] | 40 | #include "llvm/Support/PrettyStackTrace.h" |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 41 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 42 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 7a07fcb | 2010-08-09 21:00:09 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Timer.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Mutex.h" |
| 45 | #include "llvm/Support/Program.h" |
| 46 | #include "llvm/Support/Signals.h" |
| 47 | #include "llvm/Support/Threading.h" |
Ted Kremenek | 37f1ea0 | 2010-11-15 23:11:54 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 49 | |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 50 | using namespace clang; |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 51 | using namespace clang::cxcursor; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 52 | using namespace clang::cxstring; |
Argyrios Kyrtzidis | 9049cf6 | 2011-10-12 07:07:33 +0000 | [diff] [blame] | 53 | using namespace clang::cxtu; |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 54 | |
Argyrios Kyrtzidis | 9049cf6 | 2011-10-12 07:07:33 +0000 | [diff] [blame] | 55 | CXTranslationUnit cxtu::MakeCXTranslationUnit(ASTUnit *TU) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 56 | if (!TU) |
| 57 | return 0; |
| 58 | CXTranslationUnit D = new CXTranslationUnitImpl(); |
| 59 | D->TUData = TU; |
| 60 | D->StringPool = createCXStringPool(); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 61 | D->Diagnostics = 0; |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 62 | return D; |
| 63 | } |
| 64 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 65 | cxtu::CXTUOwner::~CXTUOwner() { |
| 66 | if (TU) |
| 67 | clang_disposeTranslationUnit(TU); |
| 68 | } |
| 69 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 70 | /// \brief Compare two source ranges to determine their relative position in |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 71 | /// the translation unit. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 72 | static RangeComparisonResult RangeCompare(SourceManager &SM, |
| 73 | SourceRange R1, |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 74 | SourceRange R2) { |
| 75 | assert(R1.isValid() && "First range is invalid?"); |
| 76 | assert(R2.isValid() && "Second range is invalid?"); |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 77 | if (R1.getEnd() != R2.getBegin() && |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 78 | SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin())) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 79 | return RangeBefore; |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 80 | if (R2.getEnd() != R1.getBegin() && |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 81 | SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin())) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 82 | return RangeAfter; |
| 83 | return RangeOverlap; |
| 84 | } |
| 85 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 86 | /// \brief Determine if a source location falls within, before, or after a |
| 87 | /// a given source range. |
| 88 | static RangeComparisonResult LocationCompare(SourceManager &SM, |
| 89 | SourceLocation L, SourceRange R) { |
| 90 | assert(R.isValid() && "First range is invalid?"); |
| 91 | assert(L.isValid() && "Second range is invalid?"); |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 92 | if (L == R.getBegin() || L == R.getEnd()) |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 93 | return RangeOverlap; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 94 | if (SM.isBeforeInTranslationUnit(L, R.getBegin())) |
| 95 | return RangeBefore; |
| 96 | if (SM.isBeforeInTranslationUnit(R.getEnd(), L)) |
| 97 | return RangeAfter; |
| 98 | return RangeOverlap; |
| 99 | } |
| 100 | |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 101 | /// \brief Translate a Clang source range into a CIndex source range. |
| 102 | /// |
| 103 | /// Clang internally represents ranges where the end location points to the |
| 104 | /// start of the token at the end. However, for external clients it is more |
| 105 | /// useful to have a CXSourceRange be a proper half-open interval. This routine |
| 106 | /// does the appropriate translation. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 107 | CXSourceRange cxloc::translateSourceRange(const SourceManager &SM, |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 108 | const LangOptions &LangOpts, |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 109 | const CharSourceRange &R) { |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 110 | // 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] | 111 | // location accordingly. |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 112 | SourceLocation EndLoc = R.getEnd(); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 113 | if (EndLoc.isValid() && EndLoc.isMacroID()) |
Chandler Carruth | edc3dcc | 2011-07-25 16:56:02 +0000 | [diff] [blame] | 114 | EndLoc = SM.getExpansionRange(EndLoc).second; |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 115 | if (R.isTokenRange() && !EndLoc.isInvalid() && EndLoc.isFileID()) { |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 116 | unsigned Length = Lexer::MeasureTokenLength(EndLoc, SM, LangOpts); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 117 | EndLoc = EndLoc.getLocWithOffset(Length); |
Daniel Dunbar | 76dd3c2 | 2010-02-14 01:47:29 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts }, |
| 121 | R.getBegin().getRawEncoding(), |
| 122 | EndLoc.getRawEncoding() }; |
| 123 | return Result; |
| 124 | } |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 125 | |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 126 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 127 | // Cursor visitor. |
Ted Kremenek | 8a8da7d | 2010-01-06 03:42:32 +0000 | [diff] [blame] | 128 | //===----------------------------------------------------------------------===// |
| 129 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 130 | static SourceRange getRawCursorExtent(CXCursor C); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 131 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr); |
| 132 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 133 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 134 | RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 135 | return RangeCompare(AU->getSourceManager(), R, RegionOfInterest); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 138 | /// \brief Visit the given cursor and, if requested by the visitor, |
| 139 | /// its children. |
| 140 | /// |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 141 | /// \param Cursor the cursor to visit. |
| 142 | /// |
| 143 | /// \param CheckRegionOfInterest if true, then the caller already checked that |
| 144 | /// this cursor is within the region of interest. |
| 145 | /// |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 146 | /// \returns true if the visitation should be aborted, false if it |
| 147 | /// should continue. |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 148 | bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 149 | if (clang_isInvalid(Cursor.kind)) |
| 150 | return false; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 151 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 152 | if (clang_isDeclaration(Cursor.kind)) { |
| 153 | Decl *D = getCursorDecl(Cursor); |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 154 | if (!D) { |
| 155 | assert(0 && "Invalid declaration cursor"); |
| 156 | return true; // abort. |
| 157 | } |
| 158 | |
Argyrios Kyrtzidis | 65ab907 | 2011-09-26 19:05:37 +0000 | [diff] [blame] | 159 | // Ignore implicit declarations, unless it's an objc method because |
| 160 | // currently we should report implicit methods for properties when indexing. |
| 161 | if (D->isImplicit() && !isa<ObjCMethodDecl>(D)) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 165 | // If we have a range of interest, and this cursor doesn't intersect with it, |
| 166 | // we're done. |
| 167 | if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 168 | SourceRange Range = getRawCursorExtent(Cursor); |
Daniel Dunbar | f408f32 | 2010-02-14 08:32:05 +0000 | [diff] [blame] | 169 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 170 | return false; |
| 171 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 172 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 173 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 174 | case CXChildVisit_Break: |
| 175 | return true; |
| 176 | |
| 177 | case CXChildVisit_Continue: |
| 178 | return false; |
| 179 | |
| 180 | case CXChildVisit_Recurse: |
| 181 | return VisitChildren(Cursor); |
| 182 | } |
| 183 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 184 | llvm_unreachable("Invalid CXChildVisitResult!"); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 187 | static bool visitPreprocessedEntitiesInRange(SourceRange R, |
| 188 | PreprocessingRecord &PPRec, |
| 189 | CursorVisitor &Visitor) { |
| 190 | SourceManager &SM = Visitor.getASTUnit()->getSourceManager(); |
| 191 | FileID FID; |
| 192 | |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 193 | if (!Visitor.shouldVisitIncludedEntities()) { |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 194 | // If the begin/end of the range lie in the same FileID, do the optimization |
| 195 | // 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] | 196 | FID = SM.getFileID(SM.getFileLoc(R.getBegin())); |
| 197 | if (FID != SM.getFileID(SM.getFileLoc(R.getEnd()))) |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 198 | FID = FileID(); |
| 199 | } |
| 200 | |
| 201 | std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> |
| 202 | Entities = PPRec.getPreprocessedEntitiesInRange(R); |
| 203 | return Visitor.visitPreprocessedEntities(Entities.first, Entities.second, |
| 204 | PPRec, FID); |
| 205 | } |
| 206 | |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 207 | void CursorVisitor::visitFileRegion() { |
| 208 | if (RegionOfInterest.isInvalid()) |
| 209 | return; |
| 210 | |
| 211 | ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData); |
| 212 | SourceManager &SM = Unit->getSourceManager(); |
| 213 | |
| 214 | std::pair<FileID, unsigned> |
| 215 | Begin = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getBegin())), |
| 216 | End = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getEnd())); |
| 217 | |
| 218 | if (End.first != Begin.first) { |
| 219 | // If the end does not reside in the same file, try to recover by |
| 220 | // picking the end of the file of begin location. |
| 221 | End.first = Begin.first; |
| 222 | End.second = SM.getFileIDSize(Begin.first); |
| 223 | } |
| 224 | |
| 225 | assert(Begin.first == End.first); |
| 226 | if (Begin.second > End.second) |
| 227 | return; |
| 228 | |
| 229 | FileID File = Begin.first; |
| 230 | unsigned Offset = Begin.second; |
| 231 | unsigned Length = End.second - Begin.second; |
| 232 | |
Argyrios Kyrtzidis | b49e728 | 2011-11-29 03:14:11 +0000 | [diff] [blame] | 233 | if (!VisitDeclsOnly && !VisitPreprocessorLast) |
| 234 | if (visitPreprocessedEntitiesInRegion()) |
| 235 | return; // visitation break. |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 236 | |
| 237 | visitDeclsFromFileRegion(File, Offset, Length); |
| 238 | |
Argyrios Kyrtzidis | b49e728 | 2011-11-29 03:14:11 +0000 | [diff] [blame] | 239 | if (!VisitDeclsOnly && VisitPreprocessorLast) |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 240 | visitPreprocessedEntitiesInRegion(); |
| 241 | } |
| 242 | |
Argyrios Kyrtzidis | e2079cf | 2011-11-16 08:58:54 +0000 | [diff] [blame] | 243 | static bool isInLexicalContext(Decl *D, DeclContext *DC) { |
| 244 | if (!DC) |
| 245 | return false; |
| 246 | |
| 247 | for (DeclContext *DeclDC = D->getLexicalDeclContext(); |
| 248 | DeclDC; DeclDC = DeclDC->getLexicalParent()) { |
| 249 | if (DeclDC == DC) |
| 250 | return true; |
| 251 | } |
| 252 | return false; |
| 253 | } |
| 254 | |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 255 | void CursorVisitor::visitDeclsFromFileRegion(FileID File, |
| 256 | unsigned Offset, unsigned Length) { |
| 257 | ASTUnit *Unit = static_cast<ASTUnit *>(TU->TUData); |
| 258 | SourceManager &SM = Unit->getSourceManager(); |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 259 | SourceRange Range = RegionOfInterest; |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 260 | |
| 261 | SmallVector<Decl *, 16> Decls; |
| 262 | Unit->findFileRegionDecls(File, Offset, Length, Decls); |
| 263 | |
| 264 | // If we didn't find any file level decls for the file, try looking at the |
| 265 | // file that it was included from. |
Argyrios Kyrtzidis | c14a03d | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 266 | while (Decls.empty() || Decls.front()->isTopLevelDeclInObjCContainer()) { |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 267 | bool Invalid = false; |
| 268 | const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid); |
| 269 | if (Invalid) |
| 270 | return; |
| 271 | |
| 272 | SourceLocation Outer; |
| 273 | if (SLEntry.isFile()) |
| 274 | Outer = SLEntry.getFile().getIncludeLoc(); |
| 275 | else |
| 276 | Outer = SLEntry.getExpansion().getExpansionLocStart(); |
| 277 | if (Outer.isInvalid()) |
| 278 | return; |
| 279 | |
| 280 | llvm::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer); |
| 281 | Length = 0; |
| 282 | Unit->findFileRegionDecls(File, Offset, Length, Decls); |
| 283 | } |
| 284 | |
| 285 | assert(!Decls.empty()); |
| 286 | |
| 287 | bool VisitedAtLeastOnce = false; |
Argyrios Kyrtzidis | e2079cf | 2011-11-16 08:58:54 +0000 | [diff] [blame] | 288 | DeclContext *CurDC = 0; |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 289 | SmallVector<Decl *, 16>::iterator DIt = Decls.begin(); |
| 290 | for (SmallVector<Decl *, 16>::iterator DE = Decls.end(); DIt != DE; ++DIt) { |
| 291 | Decl *D = *DIt; |
Argyrios Kyrtzidis | ed8bef4 | 2011-11-28 22:38:07 +0000 | [diff] [blame] | 292 | if (D->getSourceRange().isInvalid()) |
| 293 | continue; |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 294 | |
Argyrios Kyrtzidis | e2079cf | 2011-11-16 08:58:54 +0000 | [diff] [blame] | 295 | if (isInLexicalContext(D, CurDC)) |
| 296 | continue; |
| 297 | |
| 298 | CurDC = dyn_cast<DeclContext>(D); |
| 299 | |
Argyrios Kyrtzidis | e2079cf | 2011-11-16 08:58:54 +0000 | [diff] [blame] | 300 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
| 301 | if (!TD->isFreeStanding()) |
| 302 | continue; |
| 303 | |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 304 | RangeComparisonResult CompRes = RangeCompare(SM, D->getSourceRange(),Range); |
| 305 | if (CompRes == RangeBefore) |
| 306 | continue; |
| 307 | if (CompRes == RangeAfter) |
| 308 | break; |
| 309 | |
| 310 | assert(CompRes == RangeOverlap); |
| 311 | VisitedAtLeastOnce = true; |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 312 | |
| 313 | if (isa<ObjCContainerDecl>(D)) { |
| 314 | FileDI_current = &DIt; |
| 315 | FileDE_current = DE; |
| 316 | } else { |
| 317 | FileDI_current = 0; |
| 318 | } |
| 319 | |
Argyrios Kyrtzidis | ba98617 | 2011-11-03 19:02:28 +0000 | [diff] [blame] | 320 | if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true)) |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 321 | break; |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | if (VisitedAtLeastOnce) |
| 325 | return; |
| 326 | |
| 327 | // No Decls overlapped with the range. Move up the lexical context until there |
| 328 | // is a context that contains the range or we reach the translation unit |
| 329 | // level. |
| 330 | DeclContext *DC = DIt == Decls.begin() ? (*DIt)->getLexicalDeclContext() |
| 331 | : (*(DIt-1))->getLexicalDeclContext(); |
| 332 | |
| 333 | while (DC && !DC->isTranslationUnit()) { |
| 334 | Decl *D = cast<Decl>(DC); |
| 335 | SourceRange CurDeclRange = D->getSourceRange(); |
| 336 | if (CurDeclRange.isInvalid()) |
| 337 | break; |
| 338 | |
| 339 | if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) { |
Argyrios Kyrtzidis | ba98617 | 2011-11-03 19:02:28 +0000 | [diff] [blame] | 340 | Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true); |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 341 | break; |
| 342 | } |
| 343 | |
| 344 | DC = D->getLexicalDeclContext(); |
| 345 | } |
| 346 | } |
| 347 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 348 | bool CursorVisitor::visitPreprocessedEntitiesInRegion() { |
Argyrios Kyrtzidis | b49e728 | 2011-11-29 03:14:11 +0000 | [diff] [blame] | 349 | if (!AU->getPreprocessor().getPreprocessingRecord()) |
| 350 | return false; |
| 351 | |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 352 | PreprocessingRecord &PPRec |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 353 | = *AU->getPreprocessor().getPreprocessingRecord(); |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 354 | SourceManager &SM = AU->getSourceManager(); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 355 | |
Argyrios Kyrtzidis | 92ddef1 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 356 | if (RegionOfInterest.isValid()) { |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 357 | SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest); |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 358 | SourceLocation B = MappedRange.getBegin(); |
| 359 | SourceLocation E = MappedRange.getEnd(); |
| 360 | |
| 361 | if (AU->isInPreambleFileID(B)) { |
| 362 | if (SM.isLoadedSourceLocation(E)) |
| 363 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), |
| 364 | PPRec, *this); |
| 365 | |
| 366 | // Beginning of range lies in the preamble but it also extends beyond |
| 367 | // it into the main file. Split the range into 2 parts, one covering |
| 368 | // the preamble and another covering the main file. This allows subsequent |
| 369 | // calls to visitPreprocessedEntitiesInRange to accept a source range that |
| 370 | // lies in the same FileID, allowing it to skip preprocessed entities that |
| 371 | // do not come from the same FileID. |
| 372 | bool breaked = |
| 373 | visitPreprocessedEntitiesInRange( |
| 374 | SourceRange(B, AU->getEndOfPreambleFileID()), |
| 375 | PPRec, *this); |
| 376 | if (breaked) return true; |
| 377 | return visitPreprocessedEntitiesInRange( |
| 378 | SourceRange(AU->getStartOfMainFileID(), E), |
| 379 | PPRec, *this); |
| 380 | } |
| 381 | |
| 382 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this); |
Argyrios Kyrtzidis | 92ddef1 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 385 | bool OnlyLocalDecls |
Douglas Gregor | 32038bb | 2010-12-21 19:07:48 +0000 | [diff] [blame] | 386 | = !AU->isMainFileAST() && AU->getOnlyLocalDecls(); |
| 387 | |
Argyrios Kyrtzidis | 92ddef1 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 388 | if (OnlyLocalDecls) |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 389 | return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(), |
| 390 | PPRec); |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 391 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 392 | return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec); |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | template<typename InputIterator> |
| 396 | bool CursorVisitor::visitPreprocessedEntities(InputIterator First, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 397 | InputIterator Last, |
| 398 | PreprocessingRecord &PPRec, |
| 399 | FileID FID) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 400 | for (; First != Last; ++First) { |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 401 | if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID)) |
| 402 | continue; |
| 403 | |
| 404 | PreprocessedEntity *PPE = *First; |
| 405 | if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 406 | if (Visit(MakeMacroExpansionCursor(ME, TU))) |
| 407 | return true; |
| 408 | |
| 409 | continue; |
| 410 | } |
| 411 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 412 | if (MacroDefinition *MD = dyn_cast<MacroDefinition>(PPE)) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 413 | if (Visit(MakeMacroDefinitionCursor(MD, TU))) |
| 414 | return true; |
| 415 | |
| 416 | continue; |
| 417 | } |
| 418 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 419 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 420 | if (Visit(MakeInclusionDirectiveCursor(ID, TU))) |
| 421 | return true; |
| 422 | |
| 423 | continue; |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 427 | return false; |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 430 | /// \brief Visit the children of the given cursor. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 431 | /// |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 432 | /// \returns true if the visitation should be aborted, false if it |
| 433 | /// should continue. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 434 | bool CursorVisitor::VisitChildren(CXCursor Cursor) { |
Douglas Gregor | c314aa4 | 2011-03-02 19:17:03 +0000 | [diff] [blame] | 435 | if (clang_isReference(Cursor.kind) && |
| 436 | Cursor.kind != CXCursor_CXXBaseSpecifier) { |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 437 | // By definition, references have no children. |
| 438 | return false; |
| 439 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 440 | |
| 441 | // 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] | 442 | // done. |
Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 443 | SetParentRAII SetParent(Parent, StmtParent, Cursor); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 444 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 445 | if (clang_isDeclaration(Cursor.kind)) { |
| 446 | Decl *D = getCursorDecl(Cursor); |
Douglas Gregor | 06d9b1a | 2011-04-14 21:41:34 +0000 | [diff] [blame] | 447 | if (!D) |
| 448 | return false; |
| 449 | |
Ted Kremenek | 539311e | 2010-02-18 18:47:01 +0000 | [diff] [blame] | 450 | return VisitAttributes(D) || Visit(D); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 451 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 452 | |
Douglas Gregor | 06d9b1a | 2011-04-14 21:41:34 +0000 | [diff] [blame] | 453 | if (clang_isStatement(Cursor.kind)) { |
| 454 | if (Stmt *S = getCursorStmt(Cursor)) |
| 455 | return Visit(S); |
| 456 | |
| 457 | return false; |
| 458 | } |
| 459 | |
| 460 | if (clang_isExpression(Cursor.kind)) { |
| 461 | if (Expr *E = getCursorExpr(Cursor)) |
| 462 | return Visit(E); |
| 463 | |
| 464 | return false; |
| 465 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 466 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 467 | if (clang_isTranslationUnit(Cursor.kind)) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 468 | CXTranslationUnit tu = getCursorTU(Cursor); |
| 469 | ASTUnit *CXXUnit = static_cast<ASTUnit*>(tu->TUData); |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 470 | |
| 471 | int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast }; |
| 472 | for (unsigned I = 0; I != 2; ++I) { |
| 473 | if (VisitOrder[I]) { |
| 474 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && |
| 475 | RegionOfInterest.isInvalid()) { |
| 476 | for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(), |
| 477 | TLEnd = CXXUnit->top_level_end(); |
| 478 | TL != TLEnd; ++TL) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 479 | if (Visit(MakeCXCursor(*TL, tu, RegionOfInterest), true)) |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 480 | return true; |
| 481 | } |
| 482 | } else if (VisitDeclContext( |
| 483 | CXXUnit->getASTContext().getTranslationUnitDecl())) |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 484 | return true; |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 485 | continue; |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 486 | } |
Bob Wilson | 3178cb6 | 2010-03-19 03:57:57 +0000 | [diff] [blame] | 487 | |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 488 | // Walk the preprocessing record. |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 489 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) |
| 490 | visitPreprocessedEntitiesInRegion(); |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 491 | } |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 492 | |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 493 | return false; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 494 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 495 | |
Douglas Gregor | c314aa4 | 2011-03-02 19:17:03 +0000 | [diff] [blame] | 496 | if (Cursor.kind == CXCursor_CXXBaseSpecifier) { |
| 497 | if (CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) { |
| 498 | if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) { |
| 499 | return Visit(BaseTSInfo->getTypeLoc()); |
| 500 | } |
| 501 | } |
| 502 | } |
Argyrios Kyrtzidis | 221d5a5 | 2011-09-13 18:49:56 +0000 | [diff] [blame] | 503 | |
| 504 | if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { |
| 505 | IBOutletCollectionAttr *A = |
| 506 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor)); |
| 507 | if (const ObjCInterfaceType *InterT = A->getInterface()->getAs<ObjCInterfaceType>()) |
| 508 | return Visit(cxcursor::MakeCursorObjCClassRef(InterT->getInterface(), |
| 509 | A->getInterfaceLoc(), TU)); |
| 510 | } |
| 511 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 512 | // Nothing to visit at the moment. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 513 | return false; |
| 514 | } |
| 515 | |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 516 | bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { |
Douglas Gregor | 13c8ccb | 2011-04-22 23:49:24 +0000 | [diff] [blame] | 517 | if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten()) |
| 518 | if (Visit(TSInfo->getTypeLoc())) |
| 519 | return true; |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 520 | |
Ted Kremenek | 664cffd | 2010-07-22 11:30:19 +0000 | [diff] [blame] | 521 | if (Stmt *Body = B->getBody()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 522 | return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest)); |
Ted Kremenek | 664cffd | 2010-07-22 11:30:19 +0000 | [diff] [blame] | 523 | |
| 524 | return false; |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 527 | llvm::Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) { |
| 528 | if (RegionOfInterest.isValid()) { |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 529 | SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager()); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 530 | if (Range.isInvalid()) |
| 531 | return llvm::Optional<bool>(); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 532 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 533 | switch (CompareRegionOfInterest(Range)) { |
| 534 | case RangeBefore: |
| 535 | // This declaration comes before the region of interest; skip it. |
| 536 | return llvm::Optional<bool>(); |
| 537 | |
| 538 | case RangeAfter: |
| 539 | // This declaration comes after the region of interest; we're done. |
| 540 | return false; |
| 541 | |
| 542 | case RangeOverlap: |
| 543 | // This declaration overlaps the region of interest; visit it. |
| 544 | break; |
| 545 | } |
| 546 | } |
| 547 | return true; |
| 548 | } |
| 549 | |
| 550 | bool CursorVisitor::VisitDeclContext(DeclContext *DC) { |
| 551 | DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 552 | |
| 553 | // FIXME: Eventually remove. This part of a hack to support proper |
| 554 | // iteration over all Decls contained lexically within an ObjC container. |
| 555 | SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I); |
| 556 | SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E); |
| 557 | |
| 558 | for ( ; I != E; ++I) { |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 559 | Decl *D = *I; |
| 560 | if (D->getLexicalDeclContext() != DC) |
| 561 | continue; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 562 | CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest); |
Argyrios Kyrtzidis | 1836db0 | 2012-02-04 01:04:58 +0000 | [diff] [blame] | 563 | |
| 564 | // FIXME: ObjCClassRef/ObjCProtocolRef for forward class/protocol |
| 565 | // declarations is a mismatch with the compiler semantics. |
| 566 | if (Cursor.kind == CXCursor_ObjCInterfaceDecl) { |
| 567 | ObjCInterfaceDecl *ID = cast<ObjCInterfaceDecl>(D); |
| 568 | if (!ID->isThisDeclarationADefinition()) |
| 569 | Cursor = MakeCursorObjCClassRef(ID, ID->getLocation(), TU); |
| 570 | |
| 571 | } else if (Cursor.kind == CXCursor_ObjCProtocolDecl) { |
| 572 | ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D); |
| 573 | if (!PD->isThisDeclarationADefinition()) |
| 574 | Cursor = MakeCursorObjCProtocolRef(PD, PD->getLocation(), TU); |
| 575 | } |
| 576 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 577 | const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); |
| 578 | if (!V.hasValue()) |
| 579 | continue; |
| 580 | if (!V.getValue()) |
| 581 | return false; |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 582 | if (Visit(Cursor, true)) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 583 | return true; |
| 584 | } |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 585 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 588 | bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 589 | llvm_unreachable("Translation units are visited directly by Visit()"); |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 592 | bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 593 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 594 | return Visit(TSInfo->getTypeLoc()); |
| 595 | |
| 596 | return false; |
| 597 | } |
| 598 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 599 | bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { |
| 600 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 601 | return Visit(TSInfo->getTypeLoc()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 602 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 603 | return false; |
| 604 | } |
| 605 | |
| 606 | bool CursorVisitor::VisitTagDecl(TagDecl *D) { |
| 607 | return VisitDeclContext(D); |
| 608 | } |
| 609 | |
Douglas Gregor | 0ab1e9f | 2010-09-01 17:32:36 +0000 | [diff] [blame] | 610 | bool CursorVisitor::VisitClassTemplateSpecializationDecl( |
| 611 | ClassTemplateSpecializationDecl *D) { |
| 612 | bool ShouldVisitBody = false; |
| 613 | switch (D->getSpecializationKind()) { |
| 614 | case TSK_Undeclared: |
| 615 | case TSK_ImplicitInstantiation: |
| 616 | // Nothing to visit |
| 617 | return false; |
| 618 | |
| 619 | case TSK_ExplicitInstantiationDeclaration: |
| 620 | case TSK_ExplicitInstantiationDefinition: |
| 621 | break; |
| 622 | |
| 623 | case TSK_ExplicitSpecialization: |
| 624 | ShouldVisitBody = true; |
| 625 | break; |
| 626 | } |
| 627 | |
| 628 | // Visit the template arguments used in the specialization. |
| 629 | if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { |
| 630 | TypeLoc TL = SpecType->getTypeLoc(); |
| 631 | if (TemplateSpecializationTypeLoc *TSTLoc |
| 632 | = dyn_cast<TemplateSpecializationTypeLoc>(&TL)) { |
| 633 | for (unsigned I = 0, N = TSTLoc->getNumArgs(); I != N; ++I) |
| 634 | if (VisitTemplateArgumentLoc(TSTLoc->getArgLoc(I))) |
| 635 | return true; |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | if (ShouldVisitBody && VisitCXXRecordDecl(D)) |
| 640 | return true; |
| 641 | |
| 642 | return false; |
| 643 | } |
| 644 | |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 645 | bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( |
| 646 | ClassTemplatePartialSpecializationDecl *D) { |
| 647 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 648 | // before visiting these template parameters. |
| 649 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 650 | return true; |
| 651 | |
| 652 | // Visit the partial specialization arguments. |
| 653 | const TemplateArgumentLoc *TemplateArgs = D->getTemplateArgsAsWritten(); |
| 654 | for (unsigned I = 0, N = D->getNumTemplateArgsAsWritten(); I != N; ++I) |
| 655 | if (VisitTemplateArgumentLoc(TemplateArgs[I])) |
| 656 | return true; |
| 657 | |
| 658 | return VisitCXXRecordDecl(D); |
| 659 | } |
| 660 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 661 | bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 662 | // Visit the default argument. |
| 663 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 664 | if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) |
| 665 | if (Visit(DefArg->getTypeLoc())) |
| 666 | return true; |
| 667 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 668 | return false; |
| 669 | } |
| 670 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 671 | bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 672 | if (Expr *Init = D->getInitExpr()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 673 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 674 | return false; |
| 675 | } |
| 676 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 677 | bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { |
| 678 | if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) |
| 679 | if (Visit(TSInfo->getTypeLoc())) |
| 680 | return true; |
| 681 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 682 | // Visit the nested-name-specifier, if present. |
| 683 | if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc()) |
| 684 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 685 | return true; |
| 686 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 687 | return false; |
| 688 | } |
| 689 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 690 | /// \brief Compare two base or member initializers based on their source order. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 691 | static int CompareCXXCtorInitializers(const void* Xp, const void *Yp) { |
| 692 | CXXCtorInitializer const * const *X |
| 693 | = static_cast<CXXCtorInitializer const * const *>(Xp); |
| 694 | CXXCtorInitializer const * const *Y |
| 695 | = static_cast<CXXCtorInitializer const * const *>(Yp); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 696 | |
| 697 | if ((*X)->getSourceOrder() < (*Y)->getSourceOrder()) |
| 698 | return -1; |
| 699 | else if ((*X)->getSourceOrder() > (*Y)->getSourceOrder()) |
| 700 | return 1; |
| 701 | else |
| 702 | return 0; |
| 703 | } |
| 704 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 705 | bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 706 | if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { |
| 707 | // Visit the function declaration's syntactic components in the order |
| 708 | // written. This requires a bit of work. |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 709 | TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 710 | FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL); |
| 711 | |
| 712 | // If we have a function declared directly (without the use of a typedef), |
| 713 | // visit just the return type. Otherwise, just visit the function's type |
| 714 | // now. |
| 715 | if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL->getResultLoc())) || |
| 716 | (!FTL && Visit(TL))) |
| 717 | return true; |
| 718 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 719 | // Visit the nested-name-specifier, if present. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 720 | if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc()) |
| 721 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 722 | return true; |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 723 | |
| 724 | // Visit the declaration name. |
| 725 | if (VisitDeclarationNameInfo(ND->getNameInfo())) |
| 726 | return true; |
| 727 | |
| 728 | // FIXME: Visit explicitly-specified template arguments! |
| 729 | |
| 730 | // Visit the function parameters, if we have a function type. |
| 731 | if (FTL && VisitFunctionTypeLoc(*FTL, true)) |
| 732 | return true; |
| 733 | |
| 734 | // FIXME: Attributes? |
| 735 | } |
| 736 | |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 737 | if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) { |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 738 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) { |
| 739 | // Find the initializers that were written in the source. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 740 | SmallVector<CXXCtorInitializer *, 4> WrittenInits; |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 741 | for (CXXConstructorDecl::init_iterator I = Constructor->init_begin(), |
| 742 | IEnd = Constructor->init_end(); |
| 743 | I != IEnd; ++I) { |
| 744 | if (!(*I)->isWritten()) |
| 745 | continue; |
| 746 | |
| 747 | WrittenInits.push_back(*I); |
| 748 | } |
| 749 | |
| 750 | // Sort the initializers in source order |
| 751 | llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 752 | &CompareCXXCtorInitializers); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 753 | |
| 754 | // Visit the initializers in source order |
| 755 | for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 756 | CXXCtorInitializer *Init = WrittenInits[I]; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 757 | if (Init->isAnyMemberInitializer()) { |
| 758 | if (Visit(MakeCursorMemberRef(Init->getAnyMember(), |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 759 | Init->getMemberLocation(), TU))) |
| 760 | return true; |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 761 | } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) { |
| 762 | if (Visit(TInfo->getTypeLoc())) |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 763 | return true; |
| 764 | } |
| 765 | |
| 766 | // Visit the initializer value. |
| 767 | if (Expr *Initializer = Init->getInit()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 768 | if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest))) |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 769 | return true; |
| 770 | } |
| 771 | } |
| 772 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 773 | if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 774 | return true; |
| 775 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 776 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 777 | return false; |
| 778 | } |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 779 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 780 | bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { |
| 781 | if (VisitDeclaratorDecl(D)) |
| 782 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 783 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 784 | if (Expr *BitWidth = D->getBitWidth()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 785 | return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 786 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 787 | return false; |
| 788 | } |
| 789 | |
| 790 | bool CursorVisitor::VisitVarDecl(VarDecl *D) { |
| 791 | if (VisitDeclaratorDecl(D)) |
| 792 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 793 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 794 | if (Expr *Init = D->getInit()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 795 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 796 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 797 | return false; |
| 798 | } |
| 799 | |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 800 | bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 801 | if (VisitDeclaratorDecl(D)) |
| 802 | return true; |
| 803 | |
| 804 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 805 | if (Expr *DefArg = D->getDefaultArgument()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 806 | return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 807 | |
| 808 | return false; |
| 809 | } |
| 810 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 811 | bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 812 | // FIXME: Visit the "outer" template parameter lists on the FunctionDecl |
| 813 | // before visiting these template parameters. |
| 814 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 815 | return true; |
| 816 | |
| 817 | return VisitFunctionDecl(D->getTemplatedDecl()); |
| 818 | } |
| 819 | |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 820 | bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 821 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 822 | // before visiting these template parameters. |
| 823 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 824 | return true; |
| 825 | |
| 826 | return VisitCXXRecordDecl(D->getTemplatedDecl()); |
| 827 | } |
| 828 | |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 829 | bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 830 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 831 | return true; |
| 832 | |
| 833 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && |
| 834 | VisitTemplateArgumentLoc(D->getDefaultArgument())) |
| 835 | return true; |
| 836 | |
| 837 | return false; |
| 838 | } |
| 839 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 840 | bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 841 | if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo()) |
| 842 | if (Visit(TSInfo->getTypeLoc())) |
| 843 | return true; |
| 844 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 845 | for (ObjCMethodDecl::param_iterator P = ND->param_begin(), |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 846 | PEnd = ND->param_end(); |
| 847 | P != PEnd; ++P) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 848 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 849 | return true; |
| 850 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 851 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 852 | if (ND->isThisDeclarationADefinition() && |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 853 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 854 | return true; |
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 | return false; |
| 857 | } |
| 858 | |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 859 | template <typename DeclIt> |
| 860 | static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current, |
| 861 | SourceManager &SM, SourceLocation EndLoc, |
| 862 | SmallVectorImpl<Decl *> &Decls) { |
| 863 | DeclIt next = *DI_current; |
| 864 | while (++next != DE_current) { |
| 865 | Decl *D_next = *next; |
| 866 | if (!D_next) |
| 867 | break; |
| 868 | SourceLocation L = D_next->getLocStart(); |
| 869 | if (!L.isValid()) |
| 870 | break; |
| 871 | if (SM.isBeforeInTranslationUnit(L, EndLoc)) { |
| 872 | *DI_current = next; |
| 873 | Decls.push_back(D_next); |
| 874 | continue; |
| 875 | } |
| 876 | break; |
| 877 | } |
| 878 | } |
| 879 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 880 | namespace { |
| 881 | struct ContainerDeclsSort { |
| 882 | SourceManager &SM; |
| 883 | ContainerDeclsSort(SourceManager &sm) : SM(sm) {} |
| 884 | bool operator()(Decl *A, Decl *B) { |
| 885 | SourceLocation L_A = A->getLocStart(); |
| 886 | SourceLocation L_B = B->getLocStart(); |
| 887 | assert(L_A.isValid() && L_B.isValid()); |
| 888 | return SM.isBeforeInTranslationUnit(L_A, L_B); |
| 889 | } |
| 890 | }; |
| 891 | } |
| 892 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 893 | bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 894 | // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially |
| 895 | // an @implementation can lexically contain Decls that are not properly |
| 896 | // nested in the AST. When we identify such cases, we need to retrofit |
| 897 | // this nesting here. |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 898 | if (!DI_current && !FileDI_current) |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 899 | return VisitDeclContext(D); |
| 900 | |
| 901 | // Scan the Decls that immediately come after the container |
| 902 | // in the current DeclContext. If any fall within the |
| 903 | // container's lexical region, stash them into a vector |
| 904 | // for later processing. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 905 | SmallVector<Decl *, 24> DeclsInContainer; |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 906 | SourceLocation EndLoc = D->getSourceRange().getEnd(); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 907 | SourceManager &SM = AU->getSourceManager(); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 908 | if (EndLoc.isValid()) { |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 909 | if (DI_current) { |
| 910 | addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc, |
| 911 | DeclsInContainer); |
| 912 | } else { |
| 913 | addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc, |
| 914 | DeclsInContainer); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 915 | } |
| 916 | } |
| 917 | |
| 918 | // The common case. |
| 919 | if (DeclsInContainer.empty()) |
| 920 | return VisitDeclContext(D); |
| 921 | |
| 922 | // Get all the Decls in the DeclContext, and sort them with the |
| 923 | // additional ones we've collected. Then visit them. |
| 924 | for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end(); |
| 925 | I!=E; ++I) { |
| 926 | Decl *subDecl = *I; |
Ted Kremenek | 0582c89 | 2010-11-02 23:17:51 +0000 | [diff] [blame] | 927 | if (!subDecl || subDecl->getLexicalDeclContext() != D || |
| 928 | subDecl->getLocStart().isInvalid()) |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 929 | continue; |
| 930 | DeclsInContainer.push_back(subDecl); |
| 931 | } |
| 932 | |
| 933 | // Now sort the Decls so that they appear in lexical order. |
| 934 | std::sort(DeclsInContainer.begin(), DeclsInContainer.end(), |
| 935 | ContainerDeclsSort(SM)); |
| 936 | |
| 937 | // Now visit the decls. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 938 | for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(), |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 939 | E = DeclsInContainer.end(); I != E; ++I) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 940 | CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 941 | const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); |
| 942 | if (!V.hasValue()) |
| 943 | continue; |
| 944 | if (!V.getValue()) |
| 945 | return false; |
| 946 | if (Visit(Cursor, true)) |
| 947 | return true; |
| 948 | } |
| 949 | return false; |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 952 | bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 953 | if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), |
| 954 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 955 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 956 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 957 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 958 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 959 | E = ND->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 960 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 961 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 962 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 963 | return VisitObjCContainerDecl(ND); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 966 | bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 967 | if (!PID->isThisDeclarationADefinition()) |
| 968 | return Visit(MakeCursorObjCProtocolRef(PID, PID->getLocation(), TU)); |
| 969 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 970 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
| 971 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 972 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 973 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 974 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 975 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 976 | return VisitObjCContainerDecl(PID); |
| 977 | } |
| 978 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 979 | bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 980 | if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc())) |
John McCall | fc92920 | 2010-06-04 22:33:30 +0000 | [diff] [blame] | 981 | return true; |
| 982 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 983 | // FIXME: This implements a workaround with @property declarations also being |
| 984 | // installed in the DeclContext for the @interface. Eventually this code |
| 985 | // should be removed. |
| 986 | ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); |
| 987 | if (!CDecl || !CDecl->IsClassExtension()) |
| 988 | return false; |
| 989 | |
| 990 | ObjCInterfaceDecl *ID = CDecl->getClassInterface(); |
| 991 | if (!ID) |
| 992 | return false; |
| 993 | |
| 994 | IdentifierInfo *PropertyId = PD->getIdentifier(); |
| 995 | ObjCPropertyDecl *prevDecl = |
| 996 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId); |
| 997 | |
| 998 | if (!prevDecl) |
| 999 | return false; |
| 1000 | |
| 1001 | // Visit synthesized methods since they will be skipped when visiting |
| 1002 | // the @interface. |
| 1003 | if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1004 | if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1005 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1006 | return true; |
| 1007 | |
| 1008 | if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 1009 | if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1010 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1011 | return true; |
| 1012 | |
| 1013 | return false; |
| 1014 | } |
| 1015 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1016 | bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 1017 | if (!D->isThisDeclarationADefinition()) { |
| 1018 | // Forward declaration is treated like a reference. |
| 1019 | return Visit(MakeCursorObjCClassRef(D, D->getLocation(), TU)); |
| 1020 | } |
| 1021 | |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1022 | // Issue callbacks for super class. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1023 | if (D->getSuperClass() && |
| 1024 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1025 | D->getSuperClassLoc(), |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1026 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1027 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1028 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 1029 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1030 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 1031 | E = D->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1032 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1033 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1034 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1035 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1038 | bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 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::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 1043 | // 'ID' could be null when dealing with invalid code. |
| 1044 | if (ObjCInterfaceDecl *ID = D->getClassInterface()) |
| 1045 | if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) |
| 1046 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1047 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1048 | return VisitObjCImplDecl(D); |
| 1049 | } |
| 1050 | |
| 1051 | bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 1052 | #if 0 |
| 1053 | // Issue callbacks for super class. |
| 1054 | // FIXME: No source location information! |
| 1055 | if (D->getSuperClass() && |
| 1056 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1057 | D->getSuperClassLoc(), |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1058 | TU))) |
| 1059 | return true; |
| 1060 | #endif |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1061 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1062 | return VisitObjCImplDecl(D); |
| 1063 | } |
| 1064 | |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1065 | bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) { |
| 1066 | if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl()) |
| 1067 | return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU)); |
| 1068 | |
| 1069 | return false; |
| 1070 | } |
| 1071 | |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 1072 | bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { |
| 1073 | return VisitDeclContext(D); |
| 1074 | } |
| 1075 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 1076 | bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1077 | // Visit nested-name-specifier. |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 1078 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1079 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1080 | return true; |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 1081 | |
| 1082 | return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), |
| 1083 | D->getTargetNameLoc(), TU)); |
| 1084 | } |
| 1085 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1086 | bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1087 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1088 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1089 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1090 | return true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1091 | } |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1092 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1093 | if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU))) |
| 1094 | return true; |
| 1095 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1096 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1097 | } |
| 1098 | |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1099 | bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1100 | // Visit nested-name-specifier. |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 1101 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1102 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1103 | return true; |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1104 | |
| 1105 | return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), |
| 1106 | D->getIdentLocation(), TU)); |
| 1107 | } |
| 1108 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1109 | bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1110 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1111 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1112 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1113 | return true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1114 | } |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1115 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1116 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1117 | } |
| 1118 | |
| 1119 | bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( |
| 1120 | UnresolvedUsingTypenameDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1121 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1122 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1123 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1124 | return true; |
| 1125 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1126 | return false; |
| 1127 | } |
| 1128 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1129 | bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { |
| 1130 | switch (Name.getName().getNameKind()) { |
| 1131 | case clang::DeclarationName::Identifier: |
| 1132 | case clang::DeclarationName::CXXLiteralOperatorName: |
| 1133 | case clang::DeclarationName::CXXOperatorName: |
| 1134 | case clang::DeclarationName::CXXUsingDirective: |
| 1135 | return false; |
| 1136 | |
| 1137 | case clang::DeclarationName::CXXConstructorName: |
| 1138 | case clang::DeclarationName::CXXDestructorName: |
| 1139 | case clang::DeclarationName::CXXConversionFunctionName: |
| 1140 | if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) |
| 1141 | return Visit(TSInfo->getTypeLoc()); |
| 1142 | return false; |
| 1143 | |
| 1144 | case clang::DeclarationName::ObjCZeroArgSelector: |
| 1145 | case clang::DeclarationName::ObjCOneArgSelector: |
| 1146 | case clang::DeclarationName::ObjCMultiArgSelector: |
| 1147 | // FIXME: Per-identifier location info? |
| 1148 | return false; |
| 1149 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1150 | |
| 1151 | llvm_unreachable("Invalid DeclarationName::Kind!"); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1154 | bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, |
| 1155 | SourceRange Range) { |
| 1156 | // FIXME: This whole routine is a hack to work around the lack of proper |
| 1157 | // source information in nested-name-specifiers (PR5791). Since we do have |
| 1158 | // a beginning source location, we can visit the first component of the |
| 1159 | // nested-name-specifier, if it's a single-token component. |
| 1160 | if (!NNS) |
| 1161 | return false; |
| 1162 | |
| 1163 | // Get the first component in the nested-name-specifier. |
| 1164 | while (NestedNameSpecifier *Prefix = NNS->getPrefix()) |
| 1165 | NNS = Prefix; |
| 1166 | |
| 1167 | switch (NNS->getKind()) { |
| 1168 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1169 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), |
| 1170 | TU)); |
| 1171 | |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 1172 | case NestedNameSpecifier::NamespaceAlias: |
| 1173 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1174 | Range.getBegin(), TU)); |
| 1175 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1176 | case NestedNameSpecifier::TypeSpec: { |
| 1177 | // If the type has a form where we know that the beginning of the source |
| 1178 | // range matches up with a reference cursor. Visit the appropriate reference |
| 1179 | // cursor. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1180 | const Type *T = NNS->getAsType(); |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1181 | if (const TypedefType *Typedef = dyn_cast<TypedefType>(T)) |
| 1182 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); |
| 1183 | if (const TagType *Tag = dyn_cast<TagType>(T)) |
| 1184 | return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); |
| 1185 | if (const TemplateSpecializationType *TST |
| 1186 | = dyn_cast<TemplateSpecializationType>(T)) |
| 1187 | return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); |
| 1188 | break; |
| 1189 | } |
| 1190 | |
| 1191 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1192 | case NestedNameSpecifier::Global: |
| 1193 | case NestedNameSpecifier::Identifier: |
| 1194 | break; |
| 1195 | } |
| 1196 | |
| 1197 | return false; |
| 1198 | } |
| 1199 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1200 | bool |
| 1201 | CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1202 | SmallVector<NestedNameSpecifierLoc, 4> Qualifiers; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1203 | for (; Qualifier; Qualifier = Qualifier.getPrefix()) |
| 1204 | Qualifiers.push_back(Qualifier); |
| 1205 | |
| 1206 | while (!Qualifiers.empty()) { |
| 1207 | NestedNameSpecifierLoc Q = Qualifiers.pop_back_val(); |
| 1208 | NestedNameSpecifier *NNS = Q.getNestedNameSpecifier(); |
| 1209 | switch (NNS->getKind()) { |
| 1210 | case NestedNameSpecifier::Namespace: |
| 1211 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1212 | Q.getLocalBeginLoc(), |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1213 | TU))) |
| 1214 | return true; |
| 1215 | |
| 1216 | break; |
| 1217 | |
| 1218 | case NestedNameSpecifier::NamespaceAlias: |
| 1219 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1220 | Q.getLocalBeginLoc(), |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1221 | TU))) |
| 1222 | return true; |
| 1223 | |
| 1224 | break; |
| 1225 | |
| 1226 | case NestedNameSpecifier::TypeSpec: |
| 1227 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1228 | if (Visit(Q.getTypeLoc())) |
| 1229 | return true; |
| 1230 | |
| 1231 | break; |
| 1232 | |
| 1233 | case NestedNameSpecifier::Global: |
| 1234 | case NestedNameSpecifier::Identifier: |
| 1235 | break; |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | return false; |
| 1240 | } |
| 1241 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1242 | bool CursorVisitor::VisitTemplateParameters( |
| 1243 | const TemplateParameterList *Params) { |
| 1244 | if (!Params) |
| 1245 | return false; |
| 1246 | |
| 1247 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 1248 | PEnd = Params->end(); |
| 1249 | P != PEnd; ++P) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1250 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1251 | return true; |
| 1252 | } |
| 1253 | |
| 1254 | return false; |
| 1255 | } |
| 1256 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1257 | bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { |
| 1258 | switch (Name.getKind()) { |
| 1259 | case TemplateName::Template: |
| 1260 | return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); |
| 1261 | |
| 1262 | case TemplateName::OverloadedTemplate: |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1263 | // Visit the overloaded template set. |
| 1264 | if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU))) |
| 1265 | return true; |
| 1266 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1267 | return false; |
| 1268 | |
| 1269 | case TemplateName::DependentTemplate: |
| 1270 | // FIXME: Visit nested-name-specifier. |
| 1271 | return false; |
| 1272 | |
| 1273 | case TemplateName::QualifiedTemplate: |
| 1274 | // FIXME: Visit nested-name-specifier. |
| 1275 | return Visit(MakeCursorTemplateRef( |
| 1276 | Name.getAsQualifiedTemplateName()->getDecl(), |
| 1277 | Loc, TU)); |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1278 | |
| 1279 | case TemplateName::SubstTemplateTemplateParm: |
| 1280 | return Visit(MakeCursorTemplateRef( |
| 1281 | Name.getAsSubstTemplateTemplateParm()->getParameter(), |
| 1282 | Loc, TU)); |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 1283 | |
| 1284 | case TemplateName::SubstTemplateTemplateParmPack: |
| 1285 | return Visit(MakeCursorTemplateRef( |
| 1286 | Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), |
| 1287 | Loc, TU)); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1288 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1289 | |
| 1290 | llvm_unreachable("Invalid TemplateName::Kind!"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1293 | bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { |
| 1294 | switch (TAL.getArgument().getKind()) { |
| 1295 | case TemplateArgument::Null: |
| 1296 | case TemplateArgument::Integral: |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 1297 | case TemplateArgument::Pack: |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1298 | return false; |
| 1299 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1300 | case TemplateArgument::Type: |
| 1301 | if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) |
| 1302 | return Visit(TSInfo->getTypeLoc()); |
| 1303 | return false; |
| 1304 | |
| 1305 | case TemplateArgument::Declaration: |
| 1306 | if (Expr *E = TAL.getSourceDeclExpression()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1307 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1308 | return false; |
| 1309 | |
| 1310 | case TemplateArgument::Expression: |
| 1311 | if (Expr *E = TAL.getSourceExpression()) |
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::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1316 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 1317 | if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc())) |
| 1318 | return true; |
| 1319 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1320 | return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1321 | TAL.getTemplateNameLoc()); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1322 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1323 | |
| 1324 | llvm_unreachable("Invalid TemplateArgument::Kind!"); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 1327 | bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 1328 | return VisitDeclContext(D); |
| 1329 | } |
| 1330 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1331 | bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1332 | return Visit(TL.getUnqualifiedLoc()); |
| 1333 | } |
| 1334 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1335 | bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 1336 | ASTContext &Context = AU->getASTContext(); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1337 | |
| 1338 | // Some builtin types (such as Objective-C's "id", "sel", and |
| 1339 | // "Class") have associated declarations. Create cursors for those. |
| 1340 | QualType VisitType; |
John McCall | e0a22d0 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 1341 | switch (TL.getTypePtr()->getKind()) { |
John McCall | 2dde35b | 2011-10-18 22:28:37 +0000 | [diff] [blame] | 1342 | |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1343 | case BuiltinType::Void: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1344 | case BuiltinType::NullPtr: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1345 | case BuiltinType::Dependent: |
John McCall | 2dde35b | 2011-10-18 22:28:37 +0000 | [diff] [blame] | 1346 | #define BUILTIN_TYPE(Id, SingletonId) |
| 1347 | #define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1348 | #define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1349 | #define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1350 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1351 | #include "clang/AST/BuiltinTypes.def" |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1352 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1353 | |
Ted Kremenek | c4174cc | 2010-02-18 18:52:18 +0000 | [diff] [blame] | 1354 | case BuiltinType::ObjCId: |
| 1355 | VisitType = Context.getObjCIdType(); |
| 1356 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1357 | |
| 1358 | case BuiltinType::ObjCClass: |
| 1359 | VisitType = Context.getObjCClassType(); |
| 1360 | break; |
| 1361 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1362 | case BuiltinType::ObjCSel: |
| 1363 | VisitType = Context.getObjCSelType(); |
| 1364 | break; |
| 1365 | } |
| 1366 | |
| 1367 | if (!VisitType.isNull()) { |
| 1368 | if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1369 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1370 | TU)); |
| 1371 | } |
| 1372 | |
| 1373 | return false; |
| 1374 | } |
| 1375 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1376 | bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1377 | return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU)); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1380 | bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 1381 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1382 | } |
| 1383 | |
| 1384 | bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { |
Argyrios Kyrtzidis | 6f155de | 2011-08-25 22:24:47 +0000 | [diff] [blame] | 1385 | if (TL.isDefinition()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1386 | return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest)); |
Argyrios Kyrtzidis | 6f155de | 2011-08-25 22:24:47 +0000 | [diff] [blame] | 1387 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1388 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1389 | } |
| 1390 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1391 | bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
Chandler Carruth | 960d13d | 2011-05-01 09:53:37 +0000 | [diff] [blame] | 1392 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1395 | bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 1396 | if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU))) |
| 1397 | return true; |
| 1398 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1399 | return false; |
| 1400 | } |
| 1401 | |
| 1402 | bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 1403 | if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) |
| 1404 | return true; |
| 1405 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1406 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1407 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1408 | TU))) |
| 1409 | return true; |
| 1410 | } |
| 1411 | |
| 1412 | return false; |
| 1413 | } |
| 1414 | |
| 1415 | bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1416 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1419 | bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) { |
| 1420 | return Visit(TL.getInnerLoc()); |
| 1421 | } |
| 1422 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1423 | bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1424 | return Visit(TL.getPointeeLoc()); |
| 1425 | } |
| 1426 | |
| 1427 | bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1428 | return Visit(TL.getPointeeLoc()); |
| 1429 | } |
| 1430 | |
| 1431 | bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1432 | return Visit(TL.getPointeeLoc()); |
| 1433 | } |
| 1434 | |
| 1435 | bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1436 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1440 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Argyrios Kyrtzidis | 3422fbc | 2011-08-15 18:44:43 +0000 | [diff] [blame] | 1443 | bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 1444 | return Visit(TL.getModifiedLoc()); |
| 1445 | } |
| 1446 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1447 | bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, |
| 1448 | bool SkipResultType) { |
| 1449 | if (!SkipResultType && Visit(TL.getResultLoc())) |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1450 | return true; |
| 1451 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1452 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
Ted Kremenek | 5dbacb4 | 2010-04-07 00:27:13 +0000 | [diff] [blame] | 1453 | if (Decl *D = TL.getArg(I)) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1454 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest))) |
Ted Kremenek | 5dbacb4 | 2010-04-07 00:27:13 +0000 | [diff] [blame] | 1455 | return true; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1456 | |
| 1457 | return false; |
| 1458 | } |
| 1459 | |
| 1460 | bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1461 | if (Visit(TL.getElementLoc())) |
| 1462 | return true; |
| 1463 | |
| 1464 | if (Expr *Size = TL.getSizeExpr()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1465 | return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1466 | |
| 1467 | return false; |
| 1468 | } |
| 1469 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1470 | bool CursorVisitor::VisitTemplateSpecializationTypeLoc( |
| 1471 | TemplateSpecializationTypeLoc TL) { |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1472 | // Visit the template name. |
| 1473 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1474 | TL.getTemplateNameLoc())) |
| 1475 | return true; |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1476 | |
| 1477 | // Visit the template arguments. |
| 1478 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1479 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1480 | return true; |
| 1481 | |
| 1482 | return false; |
| 1483 | } |
| 1484 | |
Douglas Gregor | 2332c11 | 2010-01-21 20:48:56 +0000 | [diff] [blame] | 1485 | bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 1486 | return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); |
| 1487 | } |
| 1488 | |
| 1489 | bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 1490 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1491 | return Visit(TSInfo->getTypeLoc()); |
| 1492 | |
| 1493 | return false; |
| 1494 | } |
| 1495 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1496 | bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 1497 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1498 | return Visit(TSInfo->getTypeLoc()); |
| 1499 | |
| 1500 | return false; |
| 1501 | } |
| 1502 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 1503 | bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
| 1504 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1505 | return true; |
| 1506 | |
| 1507 | return false; |
| 1508 | } |
| 1509 | |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 1510 | bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc( |
| 1511 | DependentTemplateSpecializationTypeLoc TL) { |
| 1512 | // Visit the nested-name-specifier, if there is one. |
| 1513 | if (TL.getQualifierLoc() && |
| 1514 | VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1515 | return true; |
| 1516 | |
| 1517 | // Visit the template arguments. |
| 1518 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1519 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1520 | return true; |
| 1521 | |
| 1522 | return false; |
| 1523 | } |
| 1524 | |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1525 | bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 1526 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1527 | return true; |
| 1528 | |
| 1529 | return Visit(TL.getNamedTypeLoc()); |
| 1530 | } |
| 1531 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 1532 | bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { |
| 1533 | return Visit(TL.getPatternLoc()); |
| 1534 | } |
| 1535 | |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 1536 | bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 1537 | if (Expr *E = TL.getUnderlyingExpr()) |
| 1538 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1539 | |
| 1540 | return false; |
| 1541 | } |
| 1542 | |
| 1543 | bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 1544 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1545 | } |
| 1546 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1547 | bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) { |
| 1548 | return Visit(TL.getValueLoc()); |
| 1549 | } |
| 1550 | |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 1551 | #define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \ |
| 1552 | bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \ |
| 1553 | return Visit##PARENT##Loc(TL); \ |
| 1554 | } |
| 1555 | |
| 1556 | DEFAULT_TYPELOC_IMPL(Complex, Type) |
| 1557 | DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType) |
| 1558 | DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType) |
| 1559 | DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType) |
| 1560 | DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType) |
| 1561 | DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type) |
| 1562 | DEFAULT_TYPELOC_IMPL(Vector, Type) |
| 1563 | DEFAULT_TYPELOC_IMPL(ExtVector, VectorType) |
| 1564 | DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType) |
| 1565 | DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType) |
| 1566 | DEFAULT_TYPELOC_IMPL(Record, TagType) |
| 1567 | DEFAULT_TYPELOC_IMPL(Enum, TagType) |
| 1568 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type) |
| 1569 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type) |
| 1570 | DEFAULT_TYPELOC_IMPL(Auto, Type) |
| 1571 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 1572 | bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1573 | // Visit the nested-name-specifier, if present. |
| 1574 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1575 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1576 | return true; |
| 1577 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 1578 | if (D->isCompleteDefinition()) { |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 1579 | for (CXXRecordDecl::base_class_iterator I = D->bases_begin(), |
| 1580 | E = D->bases_end(); I != E; ++I) { |
| 1581 | if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU))) |
| 1582 | return true; |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | return VisitTagDecl(D); |
| 1587 | } |
| 1588 | |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1589 | bool CursorVisitor::VisitAttributes(Decl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1590 | for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end(); |
| 1591 | i != e; ++i) |
| 1592 | if (Visit(MakeCXCursor(*i, D, TU))) |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1593 | return true; |
| 1594 | |
| 1595 | return false; |
| 1596 | } |
| 1597 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1598 | //===----------------------------------------------------------------------===// |
| 1599 | // Data-recursive visitor methods. |
| 1600 | //===----------------------------------------------------------------------===// |
| 1601 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1602 | namespace { |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1603 | #define DEF_JOB(NAME, DATA, KIND)\ |
| 1604 | class NAME : public VisitorJob {\ |
| 1605 | public:\ |
| 1606 | NAME(DATA *d, CXCursor parent) : VisitorJob(parent, VisitorJob::KIND, d) {} \ |
| 1607 | static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\ |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1608 | DATA *get() const { return static_cast<DATA*>(data[0]); }\ |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1609 | }; |
| 1610 | |
| 1611 | DEF_JOB(StmtVisit, Stmt, StmtVisitKind) |
| 1612 | DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind) |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1613 | DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1614 | DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind) |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1615 | DEF_JOB(ExplicitTemplateArgsVisit, ASTTemplateArgumentListInfo, |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1616 | ExplicitTemplateArgsVisitKind) |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 1617 | DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind) |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 1618 | DEF_JOB(LambdaExprParts, LambdaExpr, LambdaExprPartsKind) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1619 | #undef DEF_JOB |
| 1620 | |
| 1621 | class DeclVisit : public VisitorJob { |
| 1622 | public: |
| 1623 | DeclVisit(Decl *d, CXCursor parent, bool isFirst) : |
| 1624 | VisitorJob(parent, VisitorJob::DeclVisitKind, |
| 1625 | d, isFirst ? (void*) 1 : (void*) 0) {} |
| 1626 | static bool classof(const VisitorJob *VJ) { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 1627 | return VJ->getKind() == DeclVisitKind; |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1628 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1629 | Decl *get() const { return static_cast<Decl*>(data[0]); } |
| 1630 | bool isFirst() const { return data[1] ? true : false; } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1631 | }; |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1632 | class TypeLocVisit : public VisitorJob { |
| 1633 | public: |
| 1634 | TypeLocVisit(TypeLoc tl, CXCursor parent) : |
| 1635 | VisitorJob(parent, VisitorJob::TypeLocVisitKind, |
| 1636 | tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {} |
| 1637 | |
| 1638 | static bool classof(const VisitorJob *VJ) { |
| 1639 | return VJ->getKind() == TypeLocVisitKind; |
| 1640 | } |
| 1641 | |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 1642 | TypeLoc get() const { |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1643 | QualType T = QualType::getFromOpaquePtr(data[0]); |
| 1644 | return TypeLoc(T, data[1]); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1645 | } |
| 1646 | }; |
| 1647 | |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1648 | class LabelRefVisit : public VisitorJob { |
| 1649 | public: |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1650 | LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent) |
| 1651 | : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD, |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1652 | labelLoc.getPtrEncoding()) {} |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1653 | |
| 1654 | static bool classof(const VisitorJob *VJ) { |
| 1655 | return VJ->getKind() == VisitorJob::LabelRefVisitKind; |
| 1656 | } |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1657 | LabelDecl *get() const { return static_cast<LabelDecl*>(data[0]); } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1658 | SourceLocation getLoc() const { |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1659 | return SourceLocation::getFromPtrEncoding(data[1]); } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1660 | }; |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1661 | |
| 1662 | class NestedNameSpecifierLocVisit : public VisitorJob { |
| 1663 | public: |
| 1664 | NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent) |
| 1665 | : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind, |
| 1666 | Qualifier.getNestedNameSpecifier(), |
| 1667 | Qualifier.getOpaqueData()) { } |
| 1668 | |
| 1669 | static bool classof(const VisitorJob *VJ) { |
| 1670 | return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind; |
| 1671 | } |
| 1672 | |
| 1673 | NestedNameSpecifierLoc get() const { |
| 1674 | return NestedNameSpecifierLoc(static_cast<NestedNameSpecifier*>(data[0]), |
| 1675 | data[1]); |
| 1676 | } |
| 1677 | }; |
| 1678 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1679 | class DeclarationNameInfoVisit : public VisitorJob { |
| 1680 | public: |
| 1681 | DeclarationNameInfoVisit(Stmt *S, CXCursor parent) |
| 1682 | : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {} |
| 1683 | static bool classof(const VisitorJob *VJ) { |
| 1684 | return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind; |
| 1685 | } |
| 1686 | DeclarationNameInfo get() const { |
| 1687 | Stmt *S = static_cast<Stmt*>(data[0]); |
| 1688 | switch (S->getStmtClass()) { |
| 1689 | default: |
| 1690 | llvm_unreachable("Unhandled Stmt"); |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 1691 | case clang::Stmt::MSDependentExistsStmtClass: |
| 1692 | return cast<MSDependentExistsStmt>(S)->getNameInfo(); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1693 | case Stmt::CXXDependentScopeMemberExprClass: |
| 1694 | return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo(); |
| 1695 | case Stmt::DependentScopeDeclRefExprClass: |
| 1696 | return cast<DependentScopeDeclRefExpr>(S)->getNameInfo(); |
| 1697 | } |
| 1698 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1699 | }; |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1700 | class MemberRefVisit : public VisitorJob { |
| 1701 | public: |
| 1702 | MemberRefVisit(FieldDecl *D, SourceLocation L, CXCursor parent) |
| 1703 | : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D, |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1704 | L.getPtrEncoding()) {} |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1705 | static bool classof(const VisitorJob *VJ) { |
| 1706 | return VJ->getKind() == VisitorJob::MemberRefVisitKind; |
| 1707 | } |
| 1708 | FieldDecl *get() const { |
| 1709 | return static_cast<FieldDecl*>(data[0]); |
| 1710 | } |
| 1711 | SourceLocation getLoc() const { |
| 1712 | return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]); |
| 1713 | } |
| 1714 | }; |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1715 | class EnqueueVisitor : public StmtVisitor<EnqueueVisitor, void> { |
| 1716 | VisitorWorkList &WL; |
| 1717 | CXCursor Parent; |
| 1718 | public: |
| 1719 | EnqueueVisitor(VisitorWorkList &wl, CXCursor parent) |
| 1720 | : WL(wl), Parent(parent) {} |
| 1721 | |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1722 | void VisitAddrLabelExpr(AddrLabelExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1723 | void VisitBlockExpr(BlockExpr *B); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1724 | void VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
Ted Kremenek | 083c7e2 | 2010-11-13 05:38:03 +0000 | [diff] [blame] | 1725 | void VisitCompoundStmt(CompoundStmt *S); |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1726 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { /* Do nothing. */ } |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 1727 | void VisitMSDependentExistsStmt(MSDependentExistsStmt *S); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1728 | void VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1729 | void VisitCXXNewExpr(CXXNewExpr *E); |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 1730 | void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1731 | void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1732 | void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1733 | void VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); |
Ted Kremenek | b8dd1ca | 2010-11-17 00:50:41 +0000 | [diff] [blame] | 1734 | void VisitCXXTypeidExpr(CXXTypeidExpr *E); |
Ted Kremenek | 55b933a | 2010-11-17 00:50:36 +0000 | [diff] [blame] | 1735 | void VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); |
Ted Kremenek | 1e7e877 | 2010-11-17 00:50:52 +0000 | [diff] [blame] | 1736 | void VisitCXXUuidofExpr(CXXUuidofExpr *E); |
Argyrios Kyrtzidis | dcbb2fb | 2011-12-03 03:49:44 +0000 | [diff] [blame] | 1737 | void VisitCXXCatchStmt(CXXCatchStmt *S); |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1738 | void VisitDeclRefExpr(DeclRefExpr *D); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1739 | void VisitDeclStmt(DeclStmt *S); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1740 | void VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1741 | void VisitDesignatedInitExpr(DesignatedInitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1742 | void VisitExplicitCastExpr(ExplicitCastExpr *E); |
| 1743 | void VisitForStmt(ForStmt *FS); |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1744 | void VisitGotoStmt(GotoStmt *GS); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1745 | void VisitIfStmt(IfStmt *If); |
| 1746 | void VisitInitListExpr(InitListExpr *IE); |
| 1747 | void VisitMemberExpr(MemberExpr *M); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1748 | void VisitOffsetOfExpr(OffsetOfExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1749 | void VisitObjCEncodeExpr(ObjCEncodeExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1750 | void VisitObjCMessageExpr(ObjCMessageExpr *M); |
| 1751 | void VisitOverloadExpr(OverloadExpr *E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1752 | void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1753 | void VisitStmt(Stmt *S); |
| 1754 | void VisitSwitchStmt(SwitchStmt *S); |
| 1755 | void VisitWhileStmt(WhileStmt *W); |
Ted Kremenek | 2939b6f | 2010-11-17 00:50:50 +0000 | [diff] [blame] | 1756 | void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 1757 | void VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E); |
Douglas Gregor | 4ca8ac2 | 2012-02-24 07:38:34 +0000 | [diff] [blame^] | 1758 | void VisitTypeTraitExpr(TypeTraitExpr *E); |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 1759 | void VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 1760 | void VisitExpressionTraitExpr(ExpressionTraitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1761 | void VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U); |
Ted Kremenek | 9d3bf79 | 2010-11-17 00:50:43 +0000 | [diff] [blame] | 1762 | void VisitVAArgExpr(VAArgExpr *E); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 1763 | void VisitSizeOfPackExpr(SizeOfPackExpr *E); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1764 | void VisitPseudoObjectExpr(PseudoObjectExpr *E); |
| 1765 | void VisitOpaqueValueExpr(OpaqueValueExpr *E); |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 1766 | void VisitLambdaExpr(LambdaExpr *E); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1767 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1768 | private: |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1769 | void AddDeclarationNameInfo(Stmt *S); |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1770 | void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier); |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1771 | void AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1772 | void AddMemberRef(FieldDecl *D, SourceLocation L); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1773 | void AddStmt(Stmt *S); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1774 | void AddDecl(Decl *D, bool isFirst = true); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1775 | void AddTypeLoc(TypeSourceInfo *TI); |
| 1776 | void EnqueueChildren(Stmt *S); |
| 1777 | }; |
| 1778 | } // end anonyous namespace |
| 1779 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1780 | void EnqueueVisitor::AddDeclarationNameInfo(Stmt *S) { |
| 1781 | // 'S' should always be non-null, since it comes from the |
| 1782 | // statement we are visiting. |
| 1783 | WL.push_back(DeclarationNameInfoVisit(S, Parent)); |
| 1784 | } |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1785 | |
| 1786 | void |
| 1787 | EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 1788 | if (Qualifier) |
| 1789 | WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent)); |
| 1790 | } |
| 1791 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1792 | void EnqueueVisitor::AddStmt(Stmt *S) { |
| 1793 | if (S) |
| 1794 | WL.push_back(StmtVisit(S, Parent)); |
| 1795 | } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1796 | void EnqueueVisitor::AddDecl(Decl *D, bool isFirst) { |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1797 | if (D) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1798 | WL.push_back(DeclVisit(D, Parent, isFirst)); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1799 | } |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1800 | void EnqueueVisitor:: |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1801 | AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1802 | if (A) |
| 1803 | WL.push_back(ExplicitTemplateArgsVisit( |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1804 | const_cast<ASTTemplateArgumentListInfo*>(A), Parent)); |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1805 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1806 | void EnqueueVisitor::AddMemberRef(FieldDecl *D, SourceLocation L) { |
| 1807 | if (D) |
| 1808 | WL.push_back(MemberRefVisit(D, L, Parent)); |
| 1809 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1810 | void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) { |
| 1811 | if (TI) |
| 1812 | WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent)); |
| 1813 | } |
| 1814 | void EnqueueVisitor::EnqueueChildren(Stmt *S) { |
Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 1815 | unsigned size = WL.size(); |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1816 | for (Stmt::child_range Child = S->children(); Child; ++Child) { |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1817 | AddStmt(*Child); |
Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 1818 | } |
| 1819 | if (size == WL.size()) |
| 1820 | return; |
| 1821 | // Now reverse the entries we just added. This will match the DFS |
| 1822 | // ordering performed by the worklist. |
| 1823 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 1824 | std::reverse(I, E); |
| 1825 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1826 | void EnqueueVisitor::VisitAddrLabelExpr(AddrLabelExpr *E) { |
| 1827 | WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent)); |
| 1828 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1829 | void EnqueueVisitor::VisitBlockExpr(BlockExpr *B) { |
| 1830 | AddDecl(B->getBlockDecl()); |
| 1831 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1832 | void EnqueueVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 1833 | EnqueueChildren(E); |
| 1834 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1835 | } |
Ted Kremenek | 083c7e2 | 2010-11-13 05:38:03 +0000 | [diff] [blame] | 1836 | void EnqueueVisitor::VisitCompoundStmt(CompoundStmt *S) { |
| 1837 | for (CompoundStmt::reverse_body_iterator I = S->body_rbegin(), |
| 1838 | E = S->body_rend(); I != E; ++I) { |
| 1839 | AddStmt(*I); |
| 1840 | } |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1841 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1842 | void EnqueueVisitor:: |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 1843 | VisitMSDependentExistsStmt(MSDependentExistsStmt *S) { |
| 1844 | AddStmt(S->getSubStmt()); |
| 1845 | AddDeclarationNameInfo(S); |
| 1846 | if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc()) |
| 1847 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 1848 | } |
| 1849 | |
| 1850 | void EnqueueVisitor:: |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1851 | VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) { |
| 1852 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
| 1853 | AddDeclarationNameInfo(E); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1854 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 1855 | AddNestedNameSpecifierLoc(QualifierLoc); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1856 | if (!E->isImplicitAccess()) |
| 1857 | AddStmt(E->getBase()); |
| 1858 | } |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1859 | void EnqueueVisitor::VisitCXXNewExpr(CXXNewExpr *E) { |
Sebastian Redl | 2aed8b8 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 1860 | // Enqueue the initializer , if any. |
| 1861 | AddStmt(E->getInitializer()); |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1862 | // Enqueue the array size, if any. |
| 1863 | AddStmt(E->getArraySize()); |
| 1864 | // Enqueue the allocated type. |
| 1865 | AddTypeLoc(E->getAllocatedTypeSourceInfo()); |
| 1866 | // Enqueue the placement arguments. |
| 1867 | for (unsigned I = E->getNumPlacementArgs(); I > 0; --I) |
| 1868 | AddStmt(E->getPlacementArg(I-1)); |
| 1869 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1870 | void EnqueueVisitor::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *CE) { |
Ted Kremenek | 8b8d8c9 | 2010-11-13 05:55:56 +0000 | [diff] [blame] | 1871 | for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I) |
| 1872 | AddStmt(CE->getArg(I-1)); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1873 | AddStmt(CE->getCallee()); |
| 1874 | AddStmt(CE->getArg(0)); |
| 1875 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1876 | void EnqueueVisitor::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { |
| 1877 | // Visit the name of the type being destroyed. |
| 1878 | AddTypeLoc(E->getDestroyedTypeInfo()); |
| 1879 | // Visit the scope type that looks disturbingly like the nested-name-specifier |
| 1880 | // but isn't. |
| 1881 | AddTypeLoc(E->getScopeTypeInfo()); |
| 1882 | // Visit the nested-name-specifier. |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1883 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 1884 | AddNestedNameSpecifierLoc(QualifierLoc); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1885 | // Visit base expression. |
| 1886 | AddStmt(E->getBase()); |
| 1887 | } |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 1888 | void EnqueueVisitor::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
| 1889 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1890 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1891 | void EnqueueVisitor::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { |
| 1892 | EnqueueChildren(E); |
| 1893 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1894 | } |
Ted Kremenek | b8dd1ca | 2010-11-17 00:50:41 +0000 | [diff] [blame] | 1895 | void EnqueueVisitor::VisitCXXTypeidExpr(CXXTypeidExpr *E) { |
| 1896 | EnqueueChildren(E); |
| 1897 | if (E->isTypeOperand()) |
| 1898 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 1899 | } |
Ted Kremenek | 55b933a | 2010-11-17 00:50:36 +0000 | [diff] [blame] | 1900 | |
| 1901 | void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr |
| 1902 | *E) { |
| 1903 | EnqueueChildren(E); |
| 1904 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1905 | } |
Ted Kremenek | 1e7e877 | 2010-11-17 00:50:52 +0000 | [diff] [blame] | 1906 | void EnqueueVisitor::VisitCXXUuidofExpr(CXXUuidofExpr *E) { |
| 1907 | EnqueueChildren(E); |
| 1908 | if (E->isTypeOperand()) |
| 1909 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 1910 | } |
Argyrios Kyrtzidis | dcbb2fb | 2011-12-03 03:49:44 +0000 | [diff] [blame] | 1911 | |
| 1912 | void EnqueueVisitor::VisitCXXCatchStmt(CXXCatchStmt *S) { |
| 1913 | EnqueueChildren(S); |
| 1914 | AddDecl(S->getExceptionDecl()); |
| 1915 | } |
| 1916 | |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1917 | void EnqueueVisitor::VisitDeclRefExpr(DeclRefExpr *DR) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1918 | if (DR->hasExplicitTemplateArgs()) { |
| 1919 | AddExplicitTemplateArgs(&DR->getExplicitTemplateArgs()); |
| 1920 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1921 | WL.push_back(DeclRefExprParts(DR, Parent)); |
| 1922 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1923 | void EnqueueVisitor::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { |
| 1924 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
| 1925 | AddDeclarationNameInfo(E); |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 1926 | AddNestedNameSpecifierLoc(E->getQualifierLoc()); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1927 | } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1928 | void EnqueueVisitor::VisitDeclStmt(DeclStmt *S) { |
| 1929 | unsigned size = WL.size(); |
| 1930 | bool isFirst = true; |
| 1931 | for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); |
| 1932 | D != DEnd; ++D) { |
| 1933 | AddDecl(*D, isFirst); |
| 1934 | isFirst = false; |
| 1935 | } |
| 1936 | if (size == WL.size()) |
| 1937 | return; |
| 1938 | // Now reverse the entries we just added. This will match the DFS |
| 1939 | // ordering performed by the worklist. |
| 1940 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 1941 | std::reverse(I, E); |
| 1942 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1943 | void EnqueueVisitor::VisitDesignatedInitExpr(DesignatedInitExpr *E) { |
| 1944 | AddStmt(E->getInit()); |
| 1945 | typedef DesignatedInitExpr::Designator Designator; |
| 1946 | for (DesignatedInitExpr::reverse_designators_iterator |
| 1947 | D = E->designators_rbegin(), DEnd = E->designators_rend(); |
| 1948 | D != DEnd; ++D) { |
| 1949 | if (D->isFieldDesignator()) { |
| 1950 | if (FieldDecl *Field = D->getField()) |
| 1951 | AddMemberRef(Field, D->getFieldLoc()); |
| 1952 | continue; |
| 1953 | } |
| 1954 | if (D->isArrayDesignator()) { |
| 1955 | AddStmt(E->getArrayIndex(*D)); |
| 1956 | continue; |
| 1957 | } |
| 1958 | assert(D->isArrayRangeDesignator() && "Unknown designator kind"); |
| 1959 | AddStmt(E->getArrayRangeEnd(*D)); |
| 1960 | AddStmt(E->getArrayRangeStart(*D)); |
| 1961 | } |
| 1962 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1963 | void EnqueueVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) { |
| 1964 | EnqueueChildren(E); |
| 1965 | AddTypeLoc(E->getTypeInfoAsWritten()); |
| 1966 | } |
| 1967 | void EnqueueVisitor::VisitForStmt(ForStmt *FS) { |
| 1968 | AddStmt(FS->getBody()); |
| 1969 | AddStmt(FS->getInc()); |
| 1970 | AddStmt(FS->getCond()); |
| 1971 | AddDecl(FS->getConditionVariable()); |
| 1972 | AddStmt(FS->getInit()); |
| 1973 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1974 | void EnqueueVisitor::VisitGotoStmt(GotoStmt *GS) { |
| 1975 | WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent)); |
| 1976 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1977 | void EnqueueVisitor::VisitIfStmt(IfStmt *If) { |
| 1978 | AddStmt(If->getElse()); |
| 1979 | AddStmt(If->getThen()); |
| 1980 | AddStmt(If->getCond()); |
| 1981 | AddDecl(If->getConditionVariable()); |
| 1982 | } |
| 1983 | void EnqueueVisitor::VisitInitListExpr(InitListExpr *IE) { |
| 1984 | // We care about the syntactic form of the initializer list, only. |
| 1985 | if (InitListExpr *Syntactic = IE->getSyntacticForm()) |
| 1986 | IE = Syntactic; |
| 1987 | EnqueueChildren(IE); |
| 1988 | } |
| 1989 | void EnqueueVisitor::VisitMemberExpr(MemberExpr *M) { |
Douglas Gregor | 89629a7 | 2010-11-17 17:15:08 +0000 | [diff] [blame] | 1990 | WL.push_back(MemberExprParts(M, Parent)); |
| 1991 | |
| 1992 | // If the base of the member access expression is an implicit 'this', don't |
| 1993 | // visit it. |
| 1994 | // FIXME: If we ever want to show these implicit accesses, this will be |
| 1995 | // unfortunate. However, clang_getCursor() relies on this behavior. |
Douglas Gregor | 75e8504 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 1996 | if (!M->isImplicitAccess()) |
| 1997 | AddStmt(M->getBase()); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1998 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1999 | void EnqueueVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) { |
| 2000 | AddTypeLoc(E->getEncodedTypeSourceInfo()); |
| 2001 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2002 | void EnqueueVisitor::VisitObjCMessageExpr(ObjCMessageExpr *M) { |
| 2003 | EnqueueChildren(M); |
| 2004 | AddTypeLoc(M->getClassReceiverTypeInfo()); |
| 2005 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2006 | void EnqueueVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) { |
| 2007 | // Visit the components of the offsetof expression. |
| 2008 | for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) { |
| 2009 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 2010 | const OffsetOfNode &Node = E->getComponent(I-1); |
| 2011 | switch (Node.getKind()) { |
| 2012 | case OffsetOfNode::Array: |
| 2013 | AddStmt(E->getIndexExpr(Node.getArrayExprIndex())); |
| 2014 | break; |
| 2015 | case OffsetOfNode::Field: |
Abramo Bagnara | 06dec89 | 2011-03-12 09:45:03 +0000 | [diff] [blame] | 2016 | AddMemberRef(Node.getField(), Node.getSourceRange().getEnd()); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2017 | break; |
| 2018 | case OffsetOfNode::Identifier: |
| 2019 | case OffsetOfNode::Base: |
| 2020 | continue; |
| 2021 | } |
| 2022 | } |
| 2023 | // Visit the type into which we're computing the offset. |
| 2024 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2025 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2026 | void EnqueueVisitor::VisitOverloadExpr(OverloadExpr *E) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2027 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2028 | WL.push_back(OverloadExprParts(E, Parent)); |
| 2029 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2030 | void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr( |
| 2031 | UnaryExprOrTypeTraitExpr *E) { |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 2032 | EnqueueChildren(E); |
| 2033 | if (E->isArgumentType()) |
| 2034 | AddTypeLoc(E->getArgumentTypeInfo()); |
| 2035 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2036 | void EnqueueVisitor::VisitStmt(Stmt *S) { |
| 2037 | EnqueueChildren(S); |
| 2038 | } |
| 2039 | void EnqueueVisitor::VisitSwitchStmt(SwitchStmt *S) { |
| 2040 | AddStmt(S->getBody()); |
| 2041 | AddStmt(S->getCond()); |
| 2042 | AddDecl(S->getConditionVariable()); |
| 2043 | } |
Ted Kremenek | fafa75a | 2010-11-17 00:50:39 +0000 | [diff] [blame] | 2044 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2045 | void EnqueueVisitor::VisitWhileStmt(WhileStmt *W) { |
| 2046 | AddStmt(W->getBody()); |
| 2047 | AddStmt(W->getCond()); |
| 2048 | AddDecl(W->getConditionVariable()); |
| 2049 | } |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2050 | |
Ted Kremenek | 2939b6f | 2010-11-17 00:50:50 +0000 | [diff] [blame] | 2051 | void EnqueueVisitor::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { |
| 2052 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2053 | } |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2054 | |
| 2055 | void EnqueueVisitor::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) { |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2056 | AddTypeLoc(E->getRhsTypeSourceInfo()); |
Francois Pichet | 0a03a3f | 2010-12-08 09:11:05 +0000 | [diff] [blame] | 2057 | AddTypeLoc(E->getLhsTypeSourceInfo()); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Douglas Gregor | 4ca8ac2 | 2012-02-24 07:38:34 +0000 | [diff] [blame^] | 2060 | void EnqueueVisitor::VisitTypeTraitExpr(TypeTraitExpr *E) { |
| 2061 | for (unsigned I = E->getNumArgs(); I > 0; --I) |
| 2062 | AddTypeLoc(E->getArg(I-1)); |
| 2063 | } |
| 2064 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2065 | void EnqueueVisitor::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { |
| 2066 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2067 | } |
| 2068 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 2069 | void EnqueueVisitor::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { |
| 2070 | EnqueueChildren(E); |
| 2071 | } |
| 2072 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2073 | void EnqueueVisitor::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U) { |
| 2074 | VisitOverloadExpr(U); |
| 2075 | if (!U->isImplicitAccess()) |
| 2076 | AddStmt(U->getBase()); |
| 2077 | } |
Ted Kremenek | 9d3bf79 | 2010-11-17 00:50:43 +0000 | [diff] [blame] | 2078 | void EnqueueVisitor::VisitVAArgExpr(VAArgExpr *E) { |
| 2079 | AddStmt(E->getSubExpr()); |
| 2080 | AddTypeLoc(E->getWrittenTypeInfo()); |
| 2081 | } |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2082 | void EnqueueVisitor::VisitSizeOfPackExpr(SizeOfPackExpr *E) { |
| 2083 | WL.push_back(SizeOfPackExprParts(E, Parent)); |
| 2084 | } |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2085 | void EnqueueVisitor::VisitOpaqueValueExpr(OpaqueValueExpr *E) { |
| 2086 | // If the opaque value has a source expression, just transparently |
| 2087 | // visit that. This is useful for (e.g.) pseudo-object expressions. |
| 2088 | if (Expr *SourceExpr = E->getSourceExpr()) |
| 2089 | return Visit(SourceExpr); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2090 | } |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 2091 | void EnqueueVisitor::VisitLambdaExpr(LambdaExpr *E) { |
| 2092 | AddStmt(E->getBody()); |
| 2093 | WL.push_back(LambdaExprParts(E, Parent)); |
| 2094 | } |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2095 | void EnqueueVisitor::VisitPseudoObjectExpr(PseudoObjectExpr *E) { |
| 2096 | // Treat the expression like its syntactic form. |
| 2097 | Visit(E->getSyntacticForm()); |
| 2098 | } |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2099 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2100 | void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, Stmt *S) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2101 | EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S); |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { |
| 2105 | if (RegionOfInterest.isValid()) { |
| 2106 | SourceRange Range = getRawCursorExtent(C); |
| 2107 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 2108 | return false; |
| 2109 | } |
| 2110 | return true; |
| 2111 | } |
| 2112 | |
| 2113 | bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { |
| 2114 | while (!WL.empty()) { |
| 2115 | // Dequeue the worklist item. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2116 | VisitorJob LI = WL.back(); |
| 2117 | WL.pop_back(); |
| 2118 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2119 | // Set the Parent field, then back to its old value once we're done. |
| 2120 | SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); |
| 2121 | |
| 2122 | switch (LI.getKind()) { |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2123 | case VisitorJob::DeclVisitKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2124 | Decl *D = cast<DeclVisit>(&LI)->get(); |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2125 | if (!D) |
| 2126 | continue; |
| 2127 | |
| 2128 | // For now, perform default visitation for Decls. |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2129 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest, |
| 2130 | cast<DeclVisit>(&LI)->isFirst()))) |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2131 | return true; |
| 2132 | |
| 2133 | continue; |
| 2134 | } |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2135 | case VisitorJob::ExplicitTemplateArgsVisitKind: { |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 2136 | const ASTTemplateArgumentListInfo *ArgList = |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2137 | cast<ExplicitTemplateArgsVisit>(&LI)->get(); |
| 2138 | for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(), |
| 2139 | *ArgEnd = Arg + ArgList->NumTemplateArgs; |
| 2140 | Arg != ArgEnd; ++Arg) { |
| 2141 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2142 | return true; |
| 2143 | } |
| 2144 | continue; |
| 2145 | } |
Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 2146 | case VisitorJob::TypeLocVisitKind: { |
| 2147 | // Perform default visitation for TypeLocs. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2148 | if (Visit(cast<TypeLocVisit>(&LI)->get())) |
Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 2149 | return true; |
| 2150 | continue; |
| 2151 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2152 | case VisitorJob::LabelRefVisitKind: { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2153 | LabelDecl *LS = cast<LabelRefVisit>(&LI)->get(); |
Ted Kremenek | e745501 | 2011-02-23 04:54:51 +0000 | [diff] [blame] | 2154 | if (LabelStmt *stmt = LS->getStmt()) { |
| 2155 | if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(), |
| 2156 | TU))) { |
| 2157 | return true; |
| 2158 | } |
| 2159 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2160 | continue; |
| 2161 | } |
Ted Kremenek | 47695c8 | 2011-08-18 22:25:21 +0000 | [diff] [blame] | 2162 | |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 2163 | case VisitorJob::NestedNameSpecifierLocVisitKind: { |
| 2164 | NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI); |
| 2165 | if (VisitNestedNameSpecifierLoc(V->get())) |
| 2166 | return true; |
| 2167 | continue; |
| 2168 | } |
| 2169 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 2170 | case VisitorJob::DeclarationNameInfoVisitKind: { |
| 2171 | if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI) |
| 2172 | ->get())) |
| 2173 | return true; |
| 2174 | continue; |
| 2175 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2176 | case VisitorJob::MemberRefVisitKind: { |
| 2177 | MemberRefVisit *V = cast<MemberRefVisit>(&LI); |
| 2178 | if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU))) |
| 2179 | return true; |
| 2180 | continue; |
| 2181 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2182 | case VisitorJob::StmtVisitKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2183 | Stmt *S = cast<StmtVisit>(&LI)->get(); |
Ted Kremenek | 8c269ac | 2010-11-11 23:11:43 +0000 | [diff] [blame] | 2184 | if (!S) |
| 2185 | continue; |
| 2186 | |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2187 | // Update the current cursor. |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2188 | CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2189 | if (!IsInRegionOfInterest(Cursor)) |
| 2190 | continue; |
| 2191 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 2192 | case CXChildVisit_Break: return true; |
| 2193 | case CXChildVisit_Continue: break; |
| 2194 | case CXChildVisit_Recurse: |
| 2195 | EnqueueWorkList(WL, S); |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2196 | break; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2197 | } |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2198 | continue; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2199 | } |
| 2200 | case VisitorJob::MemberExprPartsKind: { |
| 2201 | // Handle the other pieces in the MemberExpr besides the base. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2202 | MemberExpr *M = cast<MemberExprParts>(&LI)->get(); |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2203 | |
| 2204 | // Visit the nested-name-specifier |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2205 | if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc()) |
| 2206 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2207 | return true; |
| 2208 | |
| 2209 | // Visit the declaration name. |
| 2210 | if (VisitDeclarationNameInfo(M->getMemberNameInfo())) |
| 2211 | return true; |
| 2212 | |
| 2213 | // Visit the explicitly-specified template arguments, if any. |
| 2214 | if (M->hasExplicitTemplateArgs()) { |
| 2215 | for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), |
| 2216 | *ArgEnd = Arg + M->getNumTemplateArgs(); |
| 2217 | Arg != ArgEnd; ++Arg) { |
| 2218 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2219 | return true; |
| 2220 | } |
| 2221 | } |
| 2222 | continue; |
| 2223 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2224 | case VisitorJob::DeclRefExprPartsKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2225 | DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get(); |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2226 | // Visit nested-name-specifier, if present. |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2227 | if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc()) |
| 2228 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2229 | return true; |
| 2230 | // Visit declaration name. |
| 2231 | if (VisitDeclarationNameInfo(DR->getNameInfo())) |
| 2232 | return true; |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2233 | continue; |
| 2234 | } |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2235 | case VisitorJob::OverloadExprPartsKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2236 | OverloadExpr *O = cast<OverloadExprParts>(&LI)->get(); |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2237 | // Visit the nested-name-specifier. |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2238 | if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc()) |
| 2239 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2240 | return true; |
| 2241 | // Visit the declaration name. |
| 2242 | if (VisitDeclarationNameInfo(O->getNameInfo())) |
| 2243 | return true; |
| 2244 | // Visit the overloaded declaration reference. |
| 2245 | if (Visit(MakeCursorOverloadedDeclRef(O, TU))) |
| 2246 | return true; |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2247 | continue; |
| 2248 | } |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2249 | case VisitorJob::SizeOfPackExprPartsKind: { |
| 2250 | SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get(); |
| 2251 | NamedDecl *Pack = E->getPack(); |
| 2252 | if (isa<TemplateTypeParmDecl>(Pack)) { |
| 2253 | if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack), |
| 2254 | E->getPackLoc(), TU))) |
| 2255 | return true; |
| 2256 | |
| 2257 | continue; |
| 2258 | } |
| 2259 | |
| 2260 | if (isa<TemplateTemplateParmDecl>(Pack)) { |
| 2261 | if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack), |
| 2262 | E->getPackLoc(), TU))) |
| 2263 | return true; |
| 2264 | |
| 2265 | continue; |
| 2266 | } |
| 2267 | |
| 2268 | // Non-type template parameter packs and function parameter packs are |
| 2269 | // treated like DeclRefExpr cursors. |
| 2270 | continue; |
| 2271 | } |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 2272 | |
| 2273 | case VisitorJob::LambdaExprPartsKind: { |
| 2274 | // Visit captures. |
| 2275 | LambdaExpr *E = cast<LambdaExprParts>(&LI)->get(); |
| 2276 | for (LambdaExpr::capture_iterator C = E->explicit_capture_begin(), |
| 2277 | CEnd = E->explicit_capture_end(); |
| 2278 | C != CEnd; ++C) { |
| 2279 | if (C->capturesThis()) |
| 2280 | continue; |
| 2281 | |
| 2282 | if (Visit(MakeCursorVariableRef(C->getCapturedVar(), |
| 2283 | C->getLocation(), |
| 2284 | TU))) |
| 2285 | return true; |
| 2286 | } |
| 2287 | |
| 2288 | // Visit parameters and return type, if present. |
| 2289 | if (E->hasExplicitParameters() || E->hasExplicitResultType()) { |
| 2290 | TypeLoc TL = E->getCallOperator()->getTypeSourceInfo()->getTypeLoc(); |
| 2291 | if (E->hasExplicitParameters() && E->hasExplicitResultType()) { |
| 2292 | // Visit the whole type. |
| 2293 | if (Visit(TL)) |
| 2294 | return true; |
| 2295 | } else if (isa<FunctionProtoTypeLoc>(TL)) { |
| 2296 | FunctionProtoTypeLoc Proto = cast<FunctionProtoTypeLoc>(TL); |
| 2297 | if (E->hasExplicitParameters()) { |
| 2298 | // Visit parameters. |
| 2299 | for (unsigned I = 0, N = Proto.getNumArgs(); I != N; ++I) |
| 2300 | if (Visit(MakeCXCursor(Proto.getArg(I), TU))) |
| 2301 | return true; |
| 2302 | } else { |
| 2303 | // Visit result type. |
| 2304 | if (Visit(Proto.getResultLoc())) |
| 2305 | return true; |
| 2306 | } |
| 2307 | } |
| 2308 | } |
| 2309 | break; |
| 2310 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2311 | } |
| 2312 | } |
| 2313 | return false; |
| 2314 | } |
| 2315 | |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2316 | bool CursorVisitor::Visit(Stmt *S) { |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 2317 | VisitorWorkList *WL = 0; |
| 2318 | if (!WorkListFreeList.empty()) { |
| 2319 | WL = WorkListFreeList.back(); |
| 2320 | WL->clear(); |
| 2321 | WorkListFreeList.pop_back(); |
| 2322 | } |
| 2323 | else { |
| 2324 | WL = new VisitorWorkList(); |
| 2325 | WorkListCache.push_back(WL); |
| 2326 | } |
| 2327 | EnqueueWorkList(*WL, S); |
| 2328 | bool result = RunVisitorWorkList(*WL); |
| 2329 | WorkListFreeList.push_back(WL); |
| 2330 | return result; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Francois Pichet | 48a8d14 | 2011-07-25 22:00:44 +0000 | [diff] [blame] | 2333 | namespace { |
| 2334 | typedef llvm::SmallVector<SourceRange, 4> RefNamePieces; |
| 2335 | RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr, |
| 2336 | const DeclarationNameInfo &NI, |
| 2337 | const SourceRange &QLoc, |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 2338 | const ASTTemplateArgumentListInfo *TemplateArgs = 0){ |
Francois Pichet | 48a8d14 | 2011-07-25 22:00:44 +0000 | [diff] [blame] | 2339 | const bool WantQualifier = NameFlags & CXNameRange_WantQualifier; |
| 2340 | const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs; |
| 2341 | const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece; |
| 2342 | |
| 2343 | const DeclarationName::NameKind Kind = NI.getName().getNameKind(); |
| 2344 | |
| 2345 | RefNamePieces Pieces; |
| 2346 | |
| 2347 | if (WantQualifier && QLoc.isValid()) |
| 2348 | Pieces.push_back(QLoc); |
| 2349 | |
| 2350 | if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr) |
| 2351 | Pieces.push_back(NI.getLoc()); |
| 2352 | |
| 2353 | if (WantTemplateArgs && TemplateArgs) |
| 2354 | Pieces.push_back(SourceRange(TemplateArgs->LAngleLoc, |
| 2355 | TemplateArgs->RAngleLoc)); |
| 2356 | |
| 2357 | if (Kind == DeclarationName::CXXOperatorName) { |
| 2358 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 2359 | NI.getInfo().CXXOperatorName.BeginOpNameLoc)); |
| 2360 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 2361 | NI.getInfo().CXXOperatorName.EndOpNameLoc)); |
| 2362 | } |
| 2363 | |
| 2364 | if (WantSinglePiece) { |
| 2365 | SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd()); |
| 2366 | Pieces.clear(); |
| 2367 | Pieces.push_back(R); |
| 2368 | } |
| 2369 | |
| 2370 | return Pieces; |
| 2371 | } |
| 2372 | } |
| 2373 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2374 | //===----------------------------------------------------------------------===// |
| 2375 | // Misc. API hooks. |
| 2376 | //===----------------------------------------------------------------------===// |
| 2377 | |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2378 | static llvm::sys::Mutex EnableMultithreadingMutex; |
| 2379 | static bool EnabledMultithreading; |
| 2380 | |
Argyrios Kyrtzidis | fa39f5b | 2011-12-15 04:52:41 +0000 | [diff] [blame] | 2381 | static void fatal_error_handler(void *user_data, const std::string& reason) { |
Argyrios Kyrtzidis | fa39f5b | 2011-12-15 04:52:41 +0000 | [diff] [blame] | 2382 | // Write the result out to stderr avoiding errs() because raw_ostreams can |
| 2383 | // call report_fatal_error. |
Argyrios Kyrtzidis | db7a800 | 2011-12-15 06:51:30 +0000 | [diff] [blame] | 2384 | fprintf(stderr, "LIBCLANG FATAL ERROR: %s\n", reason.c_str()); |
Argyrios Kyrtzidis | fa39f5b | 2011-12-15 04:52:41 +0000 | [diff] [blame] | 2385 | ::abort(); |
| 2386 | } |
| 2387 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 2388 | extern "C" { |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2389 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 2390 | int displayDiagnostics) { |
Daniel Dunbar | 48615ff | 2010-10-08 19:30:33 +0000 | [diff] [blame] | 2391 | // Disable pretty stack trace functionality, which will otherwise be a very |
| 2392 | // poor citizen of the world and set up all sorts of signal handlers. |
| 2393 | llvm::DisablePrettyStackTrace = true; |
| 2394 | |
Daniel Dunbar | c7df4f3 | 2010-08-18 18:43:14 +0000 | [diff] [blame] | 2395 | // We use crash recovery to make some of our APIs more reliable, implicitly |
| 2396 | // enable it. |
| 2397 | llvm::CrashRecoveryContext::Enable(); |
| 2398 | |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2399 | // Enable support for multithreading in LLVM. |
| 2400 | { |
| 2401 | llvm::sys::ScopedLock L(EnableMultithreadingMutex); |
| 2402 | if (!EnabledMultithreading) { |
Argyrios Kyrtzidis | fa39f5b | 2011-12-15 04:52:41 +0000 | [diff] [blame] | 2403 | llvm::install_fatal_error_handler(fatal_error_handler, 0); |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2404 | llvm::llvm_start_multithreaded(); |
| 2405 | EnabledMultithreading = true; |
| 2406 | } |
| 2407 | } |
| 2408 | |
Douglas Gregor | a030b7c | 2010-01-22 20:35:53 +0000 | [diff] [blame] | 2409 | CIndexer *CIdxr = new CIndexer(); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2410 | if (excludeDeclarationsFromPCH) |
| 2411 | CIdxr->setOnlyLocalDecls(); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2412 | if (displayDiagnostics) |
| 2413 | CIdxr->setDisplayDiagnostics(); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2414 | return CIdxr; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2417 | void clang_disposeIndex(CXIndex CIdx) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2418 | if (CIdx) |
| 2419 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
Ted Kremenek | d2427dd | 2011-03-18 23:05:39 +0000 | [diff] [blame] | 2422 | void clang_toggleCrashRecovery(unsigned isEnabled) { |
| 2423 | if (isEnabled) |
| 2424 | llvm::CrashRecoveryContext::Enable(); |
| 2425 | else |
| 2426 | llvm::CrashRecoveryContext::Disable(); |
| 2427 | } |
| 2428 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2429 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2430 | const char *ast_filename) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2431 | if (!CIdx) |
| 2432 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2433 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 2434 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 2435 | FileSystemOptions FileSystemOpts; |
| 2436 | FileSystemOpts.WorkingDir = CXXIdx->getWorkingDirectory(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2437 | |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 2438 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags; |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2439 | ASTUnit *TU = ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2440 | CXXIdx->getOnlyLocalDecls(), |
| 2441 | 0, 0, true); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2442 | return MakeCXTranslationUnit(TU); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 2445 | unsigned clang_defaultEditingTranslationUnitOptions() { |
Douglas Gregor | 2a2c50b | 2010-09-27 05:49:58 +0000 | [diff] [blame] | 2446 | return CXTranslationUnit_PrecompiledPreamble | |
Douglas Gregor | b5af843 | 2011-08-25 22:54:01 +0000 | [diff] [blame] | 2447 | CXTranslationUnit_CacheCompletionResults; |
Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2450 | CXTranslationUnit |
| 2451 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 2452 | const char *source_filename, |
| 2453 | int num_command_line_args, |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2454 | const char * const *command_line_args, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2455 | unsigned num_unsaved_files, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2456 | struct CXUnsavedFile *unsaved_files) { |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 2457 | unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord | |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2458 | CXTranslationUnit_NestedMacroExpansions; |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2459 | return clang_parseTranslationUnit(CIdx, source_filename, |
| 2460 | command_line_args, num_command_line_args, |
| 2461 | unsaved_files, num_unsaved_files, |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 2462 | Options); |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2463 | } |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2464 | |
| 2465 | struct ParseTranslationUnitInfo { |
| 2466 | CXIndex CIdx; |
| 2467 | const char *source_filename; |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2468 | const char *const *command_line_args; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2469 | int num_command_line_args; |
| 2470 | struct CXUnsavedFile *unsaved_files; |
| 2471 | unsigned num_unsaved_files; |
| 2472 | unsigned options; |
| 2473 | CXTranslationUnit result; |
| 2474 | }; |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2475 | static void clang_parseTranslationUnit_Impl(void *UserData) { |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2476 | ParseTranslationUnitInfo *PTUI = |
| 2477 | static_cast<ParseTranslationUnitInfo*>(UserData); |
| 2478 | CXIndex CIdx = PTUI->CIdx; |
| 2479 | const char *source_filename = PTUI->source_filename; |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2480 | const char * const *command_line_args = PTUI->command_line_args; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2481 | int num_command_line_args = PTUI->num_command_line_args; |
| 2482 | struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files; |
| 2483 | unsigned num_unsaved_files = PTUI->num_unsaved_files; |
| 2484 | unsigned options = PTUI->options; |
| 2485 | PTUI->result = 0; |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2486 | |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2487 | if (!CIdx) |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2488 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2489 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2490 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 2491 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2492 | bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 2493 | // FIXME: Add a flag for modules. |
| 2494 | TranslationUnitKind TUKind |
| 2495 | = (options & CXTranslationUnit_Incomplete)? TU_Prefix : TU_Complete; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2496 | bool CacheCodeCompetionResults |
| 2497 | = options & CXTranslationUnit_CacheCompletionResults; |
| 2498 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2499 | // Configure the diagnostics. |
| 2500 | DiagnosticOptions DiagOpts; |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 2501 | IntrusiveRefCntPtr<DiagnosticsEngine> |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2502 | Diags(CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args, |
| 2503 | command_line_args)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2504 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2505 | // Recover resources if we crash before exiting this function. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2506 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 2507 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2508 | DiagCleanup(Diags.getPtr()); |
| 2509 | |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 2510 | OwningPtr<std::vector<ASTUnit::RemappedFile> > |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2511 | RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); |
| 2512 | |
| 2513 | // Recover resources if we crash before exiting this function. |
| 2514 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 2515 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 2516 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2517 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2518 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2519 | const llvm::MemoryBuffer *Buffer |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 2520 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2521 | RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, |
| 2522 | Buffer)); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2523 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2524 | |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 2525 | OwningPtr<std::vector<const char *> > |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2526 | Args(new std::vector<const char*>()); |
| 2527 | |
| 2528 | // Recover resources if we crash before exiting this method. |
| 2529 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > |
| 2530 | ArgsCleanup(Args.get()); |
| 2531 | |
Douglas Gregor | 52ddc5d | 2010-07-09 18:39:07 +0000 | [diff] [blame] | 2532 | // Since the Clang C library is primarily used by batch tools dealing with |
| 2533 | // (often very broken) source code, where spell-checking can have a |
| 2534 | // significant negative impact on performance (particularly when |
| 2535 | // precompiled headers are involved), we disable it by default. |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2536 | // Only do this if we haven't found a spell-checking-related argument. |
| 2537 | bool FoundSpellCheckingArgument = false; |
| 2538 | for (int I = 0; I != num_command_line_args; ++I) { |
| 2539 | if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || |
| 2540 | strcmp(command_line_args[I], "-fspell-checking") == 0) { |
| 2541 | FoundSpellCheckingArgument = true; |
| 2542 | break; |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2543 | } |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2544 | } |
| 2545 | if (!FoundSpellCheckingArgument) |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2546 | Args->push_back("-fno-spell-checking"); |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2547 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2548 | Args->insert(Args->end(), command_line_args, |
| 2549 | command_line_args + num_command_line_args); |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 2550 | |
Argyrios Kyrtzidis | c842955 | 2011-03-20 18:17:52 +0000 | [diff] [blame] | 2551 | // The 'source_filename' argument is optional. If the caller does not |
| 2552 | // specify it then it is assumed that the source file is specified |
| 2553 | // in the actual argument list. |
| 2554 | // Put the source file after command_line_args otherwise if '-x' flag is |
| 2555 | // present it will be unused. |
| 2556 | if (source_filename) |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2557 | Args->push_back(source_filename); |
Argyrios Kyrtzidis | c842955 | 2011-03-20 18:17:52 +0000 | [diff] [blame] | 2558 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2559 | // Do we need the detailed preprocessing record? |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2560 | bool NestedMacroExpansions = false; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2561 | if (options & CXTranslationUnit_DetailedPreprocessingRecord) { |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2562 | Args->push_back("-Xclang"); |
| 2563 | Args->push_back("-detailed-preprocessing-record"); |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2564 | NestedMacroExpansions |
| 2565 | = (options & CXTranslationUnit_NestedMacroExpansions); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
Argyrios Kyrtzidis | 026f691 | 2010-11-18 21:47:04 +0000 | [diff] [blame] | 2568 | unsigned NumErrors = Diags->getClient()->getNumErrors(); |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 2569 | OwningPtr<ASTUnit> Unit( |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2570 | ASTUnit::LoadFromCommandLine(Args->size() ? &(*Args)[0] : 0 |
| 2571 | /* vector::data() not portable */, |
| 2572 | Args->size() ? (&(*Args)[0] + Args->size()) :0, |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2573 | Diags, |
| 2574 | CXXIdx->getClangResourcesPath(), |
| 2575 | CXXIdx->getOnlyLocalDecls(), |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 2576 | /*CaptureDiagnostics=*/true, |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2577 | RemappedFiles->size() ? &(*RemappedFiles)[0]:0, |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2578 | RemappedFiles->size(), |
Argyrios Kyrtzidis | 299a4a9 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 2579 | /*RemappedFilesKeepOriginalName=*/true, |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2580 | PrecompilePreamble, |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 2581 | TUKind, |
Douglas Gregor | 99ba202 | 2010-10-27 17:24:53 +0000 | [diff] [blame] | 2582 | CacheCodeCompetionResults, |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2583 | NestedMacroExpansions)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2584 | |
Argyrios Kyrtzidis | 026f691 | 2010-11-18 21:47:04 +0000 | [diff] [blame] | 2585 | if (NumErrors != Diags->getClient()->getNumErrors()) { |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2586 | // Make sure to check that 'Unit' is non-NULL. |
| 2587 | if (CXXIdx->getDisplayDiagnostics() && Unit.get()) { |
| 2588 | for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), |
| 2589 | DEnd = Unit->stored_diag_end(); |
| 2590 | D != DEnd; ++D) { |
| 2591 | CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions()); |
| 2592 | CXString Msg = clang_formatDiagnostic(&Diag, |
| 2593 | clang_defaultDiagnosticDisplayOptions()); |
| 2594 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 2595 | clang_disposeString(Msg); |
| 2596 | } |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 2597 | #ifdef LLVM_ON_WIN32 |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2598 | // On Windows, force a flush, since there may be multiple copies of |
| 2599 | // stderr and stdout in the file system, all with different buffers |
| 2600 | // but writing to the same device. |
| 2601 | fflush(stderr); |
| 2602 | #endif |
| 2603 | } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2604 | } |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 2605 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2606 | PTUI->result = MakeCXTranslationUnit(Unit.take()); |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2607 | } |
| 2608 | CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, |
| 2609 | const char *source_filename, |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2610 | const char * const *command_line_args, |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2611 | int num_command_line_args, |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2612 | struct CXUnsavedFile *unsaved_files, |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2613 | unsigned num_unsaved_files, |
| 2614 | unsigned options) { |
| 2615 | ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args, |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2616 | num_command_line_args, unsaved_files, |
| 2617 | num_unsaved_files, options, 0 }; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2618 | llvm::CrashRecoveryContext CRC; |
| 2619 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 2620 | if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) { |
Daniel Dunbar | 60a4543 | 2010-08-23 22:35:34 +0000 | [diff] [blame] | 2621 | fprintf(stderr, "libclang: crash detected during parsing: {\n"); |
| 2622 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 2623 | fprintf(stderr, " 'command_line_args' : ["); |
| 2624 | for (int i = 0; i != num_command_line_args; ++i) { |
| 2625 | if (i) |
| 2626 | fprintf(stderr, ", "); |
| 2627 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 2628 | } |
| 2629 | fprintf(stderr, "],\n"); |
| 2630 | fprintf(stderr, " 'unsaved_files' : ["); |
| 2631 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 2632 | if (i) |
| 2633 | fprintf(stderr, ", "); |
| 2634 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 2635 | unsaved_files[i].Length); |
| 2636 | } |
| 2637 | fprintf(stderr, "],\n"); |
| 2638 | fprintf(stderr, " 'options' : %d,\n", options); |
| 2639 | fprintf(stderr, "}\n"); |
| 2640 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2641 | return 0; |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2642 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
| 2643 | PrintLibclangResourceUsage(PTUI.result); |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2644 | } |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2645 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2646 | return PTUI.result; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
Douglas Gregor | 1999844 | 2010-08-13 15:35:05 +0000 | [diff] [blame] | 2649 | unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { |
| 2650 | return CXSaveTranslationUnit_None; |
| 2651 | } |
| 2652 | |
| 2653 | int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, |
| 2654 | unsigned options) { |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2655 | if (!TU) |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 2656 | return CXSaveError_InvalidTU; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2657 | |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 2658 | CXSaveError result = static_cast<ASTUnit *>(TU->TUData)->Save(FileName); |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2659 | if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 2660 | PrintLibclangResourceUsage(TU); |
| 2661 | return result; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2662 | } |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2663 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2664 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2665 | if (CTUnit) { |
| 2666 | // If the translation unit has been marked as unsafe to free, just discard |
| 2667 | // it. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2668 | if (static_cast<ASTUnit *>(CTUnit->TUData)->isUnsafeToFree()) |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2669 | return; |
| 2670 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2671 | delete static_cast<ASTUnit *>(CTUnit->TUData); |
| 2672 | disposeCXStringPool(CTUnit->StringPool); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 2673 | delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2674 | delete CTUnit; |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2675 | } |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 2676 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2677 | |
Douglas Gregor | e1e13bf | 2010-08-11 15:58:42 +0000 | [diff] [blame] | 2678 | unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { |
| 2679 | return CXReparse_None; |
| 2680 | } |
| 2681 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2682 | struct ReparseTranslationUnitInfo { |
| 2683 | CXTranslationUnit TU; |
| 2684 | unsigned num_unsaved_files; |
| 2685 | struct CXUnsavedFile *unsaved_files; |
| 2686 | unsigned options; |
| 2687 | int result; |
| 2688 | }; |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2689 | |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2690 | static void clang_reparseTranslationUnit_Impl(void *UserData) { |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2691 | ReparseTranslationUnitInfo *RTUI = |
| 2692 | static_cast<ReparseTranslationUnitInfo*>(UserData); |
| 2693 | CXTranslationUnit TU = RTUI->TU; |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 2694 | |
| 2695 | // Reset the associated diagnostics. |
| 2696 | delete static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics); |
| 2697 | TU->Diagnostics = 0; |
| 2698 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2699 | unsigned num_unsaved_files = RTUI->num_unsaved_files; |
| 2700 | struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files; |
| 2701 | unsigned options = RTUI->options; |
| 2702 | (void) options; |
| 2703 | RTUI->result = 1; |
| 2704 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2705 | if (!TU) |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2706 | return; |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2707 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2708 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2709 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2710 | |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 2711 | OwningPtr<std::vector<ASTUnit::RemappedFile> > |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2712 | RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); |
| 2713 | |
| 2714 | // Recover resources if we crash before exiting this function. |
| 2715 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 2716 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 2717 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2718 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2719 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2720 | const llvm::MemoryBuffer *Buffer |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2721 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2722 | RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, |
| 2723 | Buffer)); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2726 | if (!CXXUnit->Reparse(RemappedFiles->size() ? &(*RemappedFiles)[0] : 0, |
| 2727 | RemappedFiles->size())) |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2728 | RTUI->result = 0; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2729 | } |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2730 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2731 | int clang_reparseTranslationUnit(CXTranslationUnit TU, |
| 2732 | unsigned num_unsaved_files, |
| 2733 | struct CXUnsavedFile *unsaved_files, |
| 2734 | unsigned options) { |
| 2735 | ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files, |
| 2736 | options, 0 }; |
Argyrios Kyrtzidis | 8c4b47e | 2011-10-28 22:54:33 +0000 | [diff] [blame] | 2737 | |
Argyrios Kyrtzidis | e7de9b4 | 2011-10-29 19:32:39 +0000 | [diff] [blame] | 2738 | if (getenv("LIBCLANG_NOTHREADS")) { |
Argyrios Kyrtzidis | 8c4b47e | 2011-10-28 22:54:33 +0000 | [diff] [blame] | 2739 | clang_reparseTranslationUnit_Impl(&RTUI); |
| 2740 | return RTUI.result; |
| 2741 | } |
| 2742 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2743 | llvm::CrashRecoveryContext CRC; |
| 2744 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 2745 | if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) { |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2746 | fprintf(stderr, "libclang: crash detected during reparsing\n"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2747 | static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true); |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2748 | return 1; |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2749 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 2750 | PrintLibclangResourceUsage(TU); |
Ted Kremenek | 1dfb26a | 2010-10-29 01:06:50 +0000 | [diff] [blame] | 2751 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2752 | return RTUI.result; |
| 2753 | } |
| 2754 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2755 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2756 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2757 | if (!CTUnit) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2758 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2759 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2760 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit->TUData); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2761 | return createCXString(CXXUnit->getOriginalSourceFileName(), true); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 2762 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 2763 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2764 | CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2765 | CXCursor Result = { CXCursor_TranslationUnit, 0, { 0, 0, TU } }; |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2766 | return Result; |
| 2767 | } |
| 2768 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2769 | } // end: extern "C" |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2770 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2771 | //===----------------------------------------------------------------------===// |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2772 | // CXFile Operations. |
| 2773 | //===----------------------------------------------------------------------===// |
| 2774 | |
| 2775 | extern "C" { |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2776 | CXString clang_getFileName(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2777 | if (!SFile) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2778 | return createCXString((const char*)NULL); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2779 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2780 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2781 | return createCXString(FEnt->getName()); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | time_t clang_getFileTime(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2785 | if (!SFile) |
| 2786 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2787 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2788 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 2789 | return FEnt->getModificationTime(); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2790 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2791 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2792 | CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) { |
| 2793 | if (!tu) |
| 2794 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2795 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2796 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2797 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2798 | FileManager &FMgr = CXXUnit->getFileManager(); |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 2799 | return const_cast<FileEntry *>(FMgr.getFile(file_name)); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2800 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2801 | |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 2802 | unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit tu, CXFile file) { |
| 2803 | if (!tu || !file) |
| 2804 | return 0; |
| 2805 | |
| 2806 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
| 2807 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
| 2808 | return CXXUnit->getPreprocessor().getHeaderSearchInfo() |
| 2809 | .isFileMultipleIncludeGuarded(FEnt); |
| 2810 | } |
| 2811 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2812 | } // end: extern "C" |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2813 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2814 | //===----------------------------------------------------------------------===// |
| 2815 | // CXCursor Operations. |
| 2816 | //===----------------------------------------------------------------------===// |
| 2817 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2818 | static Decl *getDeclFromExpr(Stmt *E) { |
Argyrios Kyrtzidis | c295461 | 2011-09-12 22:17:26 +0000 | [diff] [blame] | 2819 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2820 | return getDeclFromExpr(CE->getSubExpr()); |
| 2821 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2822 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 2823 | return RefExpr->getDecl(); |
Douglas Gregor | 38f28c1 | 2010-10-22 22:24:08 +0000 | [diff] [blame] | 2824 | if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) |
| 2825 | return RefExpr->getDecl(); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2826 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 2827 | return ME->getMemberDecl(); |
| 2828 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 2829 | return RE->getDecl(); |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2830 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 2831 | return PRE->isExplicitProperty() ? PRE->getExplicitProperty() : 0; |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2832 | if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) |
| 2833 | return getDeclFromExpr(POE->getSyntacticForm()); |
| 2834 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) |
| 2835 | if (Expr *Src = OVE->getSourceExpr()) |
| 2836 | return getDeclFromExpr(Src); |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2837 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2838 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 2839 | return getDeclFromExpr(CE->getCallee()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2840 | if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 2841 | if (!CE->isElidable()) |
| 2842 | return CE->getConstructor(); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2843 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 2844 | return OME->getMethodDecl(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2845 | |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2846 | if (ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) |
| 2847 | return PE->getProtocol(); |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 2848 | if (SubstNonTypeTemplateParmPackExpr *NTTP |
| 2849 | = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E)) |
| 2850 | return NTTP->getParameterPack(); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2851 | if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
| 2852 | if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || |
| 2853 | isa<ParmVarDecl>(SizeOfPack->getPack())) |
| 2854 | return SizeOfPack->getPack(); |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2855 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2856 | return 0; |
| 2857 | } |
| 2858 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2859 | static SourceLocation getLocationFromExpr(Expr *E) { |
Argyrios Kyrtzidis | c295461 | 2011-09-12 22:17:26 +0000 | [diff] [blame] | 2860 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
| 2861 | return getLocationFromExpr(CE->getSubExpr()); |
| 2862 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2863 | if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
| 2864 | return /*FIXME:*/Msg->getLeftLoc(); |
| 2865 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 2866 | return DRE->getLocation(); |
Douglas Gregor | 38f28c1 | 2010-10-22 22:24:08 +0000 | [diff] [blame] | 2867 | if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) |
| 2868 | return RefExpr->getLocation(); |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2869 | if (MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
| 2870 | return Member->getMemberLoc(); |
| 2871 | if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
| 2872 | return Ivar->getLocation(); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2873 | if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
| 2874 | return SizeOfPack->getPackLoc(); |
| 2875 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2876 | return E->getLocStart(); |
| 2877 | } |
| 2878 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2879 | extern "C" { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2880 | |
| 2881 | unsigned clang_visitChildren(CXCursor parent, |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 2882 | CXCursorVisitor visitor, |
| 2883 | CXClientData client_data) { |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 2884 | CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, |
| 2885 | /*VisitPreprocessorLast=*/false); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 2886 | return CursorVis.VisitChildren(parent); |
| 2887 | } |
| 2888 | |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 2889 | #ifndef __has_feature |
| 2890 | #define __has_feature(x) 0 |
| 2891 | #endif |
| 2892 | #if __has_feature(blocks) |
| 2893 | typedef enum CXChildVisitResult |
| 2894 | (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent); |
| 2895 | |
| 2896 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 2897 | CXClientData client_data) { |
| 2898 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 2899 | return block(cursor, parent); |
| 2900 | } |
| 2901 | #else |
| 2902 | // If we are compiled with a compiler that doesn't have native blocks support, |
| 2903 | // define and call the block manually, so the |
| 2904 | typedef struct _CXChildVisitResult |
| 2905 | { |
| 2906 | void *isa; |
| 2907 | int flags; |
| 2908 | int reserved; |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2909 | enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, |
| 2910 | CXCursor); |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 2911 | } *CXCursorVisitorBlock; |
| 2912 | |
| 2913 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 2914 | CXClientData client_data) { |
| 2915 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 2916 | return block->invoke(block, cursor, parent); |
| 2917 | } |
| 2918 | #endif |
| 2919 | |
| 2920 | |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2921 | unsigned clang_visitChildrenWithBlock(CXCursor parent, |
| 2922 | CXCursorVisitorBlock block) { |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 2923 | return clang_visitChildren(parent, visitWithBlock, block); |
| 2924 | } |
| 2925 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2926 | static CXString getDeclSpelling(Decl *D) { |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 2927 | if (!D) |
| 2928 | return createCXString(""); |
| 2929 | |
| 2930 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 2931 | if (!ND) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2932 | if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 2933 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 2934 | return createCXString(Property->getIdentifier()->getName()); |
| 2935 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2936 | return createCXString(""); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2939 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2940 | return createCXString(OMD->getSelector().getAsString()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2941 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2942 | if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
| 2943 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 2944 | // and returns different names. NamedDecl returns the class name and |
| 2945 | // ObjCCategoryImplDecl returns the category name. |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2946 | return createCXString(CIMP->getIdentifier()->getNameStart()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2947 | |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 2948 | if (isa<UsingDirectiveDecl>(D)) |
| 2949 | return createCXString(""); |
| 2950 | |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 2951 | SmallString<1024> S; |
Ted Kremenek | 50aa6ac | 2010-05-19 21:51:10 +0000 | [diff] [blame] | 2952 | llvm::raw_svector_ostream os(S); |
| 2953 | ND->printName(os); |
| 2954 | |
| 2955 | return createCXString(os.str()); |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2956 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2957 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2958 | CXString clang_getCursorSpelling(CXCursor C) { |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2959 | if (clang_isTranslationUnit(C.kind)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2960 | return clang_getTranslationUnitSpelling( |
| 2961 | static_cast<CXTranslationUnit>(C.data[2])); |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2962 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 2963 | if (clang_isReference(C.kind)) { |
| 2964 | switch (C.kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2965 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 2966 | ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2967 | return createCXString(Super->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2968 | } |
| 2969 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 2970 | ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2971 | return createCXString(Class->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2972 | } |
| 2973 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 2974 | ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 2975 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2976 | return createCXString(OID->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2977 | } |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 2978 | case CXCursor_CXXBaseSpecifier: { |
| 2979 | CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); |
| 2980 | return createCXString(B->getType().getAsString()); |
| 2981 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2982 | case CXCursor_TypeRef: { |
| 2983 | TypeDecl *Type = getCursorTypeRef(C).first; |
| 2984 | assert(Type && "Missing type decl"); |
| 2985 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2986 | return createCXString(getCursorContext(C).getTypeDeclType(Type). |
| 2987 | getAsString()); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2988 | } |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 2989 | case CXCursor_TemplateRef: { |
| 2990 | TemplateDecl *Template = getCursorTemplateRef(C).first; |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 2991 | assert(Template && "Missing template decl"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 2992 | |
| 2993 | return createCXString(Template->getNameAsString()); |
| 2994 | } |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 2995 | |
| 2996 | case CXCursor_NamespaceRef: { |
| 2997 | NamedDecl *NS = getCursorNamespaceRef(C).first; |
| 2998 | assert(NS && "Missing namespace decl"); |
| 2999 | |
| 3000 | return createCXString(NS->getNameAsString()); |
| 3001 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3002 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3003 | case CXCursor_MemberRef: { |
| 3004 | FieldDecl *Field = getCursorMemberRef(C).first; |
| 3005 | assert(Field && "Missing member decl"); |
| 3006 | |
| 3007 | return createCXString(Field->getNameAsString()); |
| 3008 | } |
| 3009 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3010 | case CXCursor_LabelRef: { |
| 3011 | LabelStmt *Label = getCursorLabelRef(C).first; |
| 3012 | assert(Label && "Missing label"); |
| 3013 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3014 | return createCXString(Label->getName()); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3017 | case CXCursor_OverloadedDeclRef: { |
| 3018 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
| 3019 | if (Decl *D = Storage.dyn_cast<Decl *>()) { |
| 3020 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 3021 | return createCXString(ND->getNameAsString()); |
| 3022 | return createCXString(""); |
| 3023 | } |
| 3024 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
| 3025 | return createCXString(E->getName().getAsString()); |
| 3026 | OverloadedTemplateStorage *Ovl |
| 3027 | = Storage.get<OverloadedTemplateStorage*>(); |
| 3028 | if (Ovl->size() == 0) |
| 3029 | return createCXString(""); |
| 3030 | return createCXString((*Ovl->begin())->getNameAsString()); |
| 3031 | } |
| 3032 | |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 3033 | case CXCursor_VariableRef: { |
| 3034 | VarDecl *Var = getCursorVariableRef(C).first; |
| 3035 | assert(Var && "Missing variable decl"); |
| 3036 | |
| 3037 | return createCXString(Var->getNameAsString()); |
| 3038 | } |
| 3039 | |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 3040 | default: |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3041 | return createCXString("<not implemented>"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3042 | } |
| 3043 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3044 | |
| 3045 | if (clang_isExpression(C.kind)) { |
| 3046 | Decl *D = getDeclFromExpr(getCursorExpr(C)); |
| 3047 | if (D) |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3048 | return getDeclSpelling(D); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3049 | return createCXString(""); |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3050 | } |
| 3051 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3052 | if (clang_isStatement(C.kind)) { |
| 3053 | Stmt *S = getCursorStmt(C); |
| 3054 | if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3055 | return createCXString(Label->getName()); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3056 | |
| 3057 | return createCXString(""); |
| 3058 | } |
| 3059 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3060 | if (C.kind == CXCursor_MacroExpansion) |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3061 | return createCXString(getCursorMacroExpansion(C)->getName() |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 3062 | ->getNameStart()); |
| 3063 | |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3064 | if (C.kind == CXCursor_MacroDefinition) |
| 3065 | return createCXString(getCursorMacroDefinition(C)->getName() |
| 3066 | ->getNameStart()); |
| 3067 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3068 | if (C.kind == CXCursor_InclusionDirective) |
| 3069 | return createCXString(getCursorInclusionDirective(C)->getFileName()); |
| 3070 | |
Douglas Gregor | 60cbfac | 2010-01-25 16:56:17 +0000 | [diff] [blame] | 3071 | if (clang_isDeclaration(C.kind)) |
| 3072 | return getDeclSpelling(getCursorDecl(C)); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3073 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 3074 | if (C.kind == CXCursor_AnnotateAttr) { |
| 3075 | AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C)); |
| 3076 | return createCXString(AA->getAnnotation()); |
| 3077 | } |
| 3078 | |
Argyrios Kyrtzidis | 84b7964 | 2011-12-06 22:05:01 +0000 | [diff] [blame] | 3079 | if (C.kind == CXCursor_AsmLabelAttr) { |
| 3080 | AsmLabelAttr *AA = cast<AsmLabelAttr>(cxcursor::getCursorAttr(C)); |
| 3081 | return createCXString(AA->getLabel()); |
| 3082 | } |
| 3083 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3084 | return createCXString(""); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3087 | CXString clang_getCursorDisplayName(CXCursor C) { |
| 3088 | if (!clang_isDeclaration(C.kind)) |
| 3089 | return clang_getCursorSpelling(C); |
| 3090 | |
| 3091 | Decl *D = getCursorDecl(C); |
| 3092 | if (!D) |
| 3093 | return createCXString(""); |
| 3094 | |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3095 | PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy(); |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3096 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
| 3097 | D = FunTmpl->getTemplatedDecl(); |
| 3098 | |
| 3099 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 3100 | SmallString<64> Str; |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3101 | llvm::raw_svector_ostream OS(Str); |
Benjamin Kramer | a59d20b | 2012-02-07 11:57:57 +0000 | [diff] [blame] | 3102 | OS << *Function; |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3103 | if (Function->getPrimaryTemplate()) |
| 3104 | OS << "<>"; |
| 3105 | OS << "("; |
| 3106 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { |
| 3107 | if (I) |
| 3108 | OS << ", "; |
| 3109 | OS << Function->getParamDecl(I)->getType().getAsString(Policy); |
| 3110 | } |
| 3111 | |
| 3112 | if (Function->isVariadic()) { |
| 3113 | if (Function->getNumParams()) |
| 3114 | OS << ", "; |
| 3115 | OS << "..."; |
| 3116 | } |
| 3117 | OS << ")"; |
| 3118 | return createCXString(OS.str()); |
| 3119 | } |
| 3120 | |
| 3121 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 3122 | SmallString<64> Str; |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3123 | llvm::raw_svector_ostream OS(Str); |
Benjamin Kramer | a59d20b | 2012-02-07 11:57:57 +0000 | [diff] [blame] | 3124 | OS << *ClassTemplate; |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3125 | OS << "<"; |
| 3126 | TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); |
| 3127 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 3128 | if (I) |
| 3129 | OS << ", "; |
| 3130 | |
| 3131 | NamedDecl *Param = Params->getParam(I); |
| 3132 | if (Param->getIdentifier()) { |
| 3133 | OS << Param->getIdentifier()->getName(); |
| 3134 | continue; |
| 3135 | } |
| 3136 | |
| 3137 | // There is no parameter name, which makes this tricky. Try to come up |
| 3138 | // with something useful that isn't too long. |
| 3139 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 3140 | OS << (TTP->wasDeclaredWithTypename()? "typename" : "class"); |
| 3141 | else if (NonTypeTemplateParmDecl *NTTP |
| 3142 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 3143 | OS << NTTP->getType().getAsString(Policy); |
| 3144 | else |
| 3145 | OS << "template<...> class"; |
| 3146 | } |
| 3147 | |
| 3148 | OS << ">"; |
| 3149 | return createCXString(OS.str()); |
| 3150 | } |
| 3151 | |
| 3152 | if (ClassTemplateSpecializationDecl *ClassSpec |
| 3153 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 3154 | // If the type was explicitly written, use that. |
| 3155 | if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) |
| 3156 | return createCXString(TSInfo->getType().getAsString(Policy)); |
| 3157 | |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 3158 | SmallString<64> Str; |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3159 | llvm::raw_svector_ostream OS(Str); |
Benjamin Kramer | a59d20b | 2012-02-07 11:57:57 +0000 | [diff] [blame] | 3160 | OS << *ClassSpec; |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3161 | OS << TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3162 | ClassSpec->getTemplateArgs().data(), |
| 3163 | ClassSpec->getTemplateArgs().size(), |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3164 | Policy); |
| 3165 | return createCXString(OS.str()); |
| 3166 | } |
| 3167 | |
| 3168 | return clang_getCursorSpelling(C); |
| 3169 | } |
| 3170 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3171 | CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3172 | switch (Kind) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3173 | case CXCursor_FunctionDecl: |
| 3174 | return createCXString("FunctionDecl"); |
| 3175 | case CXCursor_TypedefDecl: |
| 3176 | return createCXString("TypedefDecl"); |
| 3177 | case CXCursor_EnumDecl: |
| 3178 | return createCXString("EnumDecl"); |
| 3179 | case CXCursor_EnumConstantDecl: |
| 3180 | return createCXString("EnumConstantDecl"); |
| 3181 | case CXCursor_StructDecl: |
| 3182 | return createCXString("StructDecl"); |
| 3183 | case CXCursor_UnionDecl: |
| 3184 | return createCXString("UnionDecl"); |
| 3185 | case CXCursor_ClassDecl: |
| 3186 | return createCXString("ClassDecl"); |
| 3187 | case CXCursor_FieldDecl: |
| 3188 | return createCXString("FieldDecl"); |
| 3189 | case CXCursor_VarDecl: |
| 3190 | return createCXString("VarDecl"); |
| 3191 | case CXCursor_ParmDecl: |
| 3192 | return createCXString("ParmDecl"); |
| 3193 | case CXCursor_ObjCInterfaceDecl: |
| 3194 | return createCXString("ObjCInterfaceDecl"); |
| 3195 | case CXCursor_ObjCCategoryDecl: |
| 3196 | return createCXString("ObjCCategoryDecl"); |
| 3197 | case CXCursor_ObjCProtocolDecl: |
| 3198 | return createCXString("ObjCProtocolDecl"); |
| 3199 | case CXCursor_ObjCPropertyDecl: |
| 3200 | return createCXString("ObjCPropertyDecl"); |
| 3201 | case CXCursor_ObjCIvarDecl: |
| 3202 | return createCXString("ObjCIvarDecl"); |
| 3203 | case CXCursor_ObjCInstanceMethodDecl: |
| 3204 | return createCXString("ObjCInstanceMethodDecl"); |
| 3205 | case CXCursor_ObjCClassMethodDecl: |
| 3206 | return createCXString("ObjCClassMethodDecl"); |
| 3207 | case CXCursor_ObjCImplementationDecl: |
| 3208 | return createCXString("ObjCImplementationDecl"); |
| 3209 | case CXCursor_ObjCCategoryImplDecl: |
| 3210 | return createCXString("ObjCCategoryImplDecl"); |
Ted Kremenek | 8bd5a69 | 2010-04-13 23:39:06 +0000 | [diff] [blame] | 3211 | case CXCursor_CXXMethod: |
| 3212 | return createCXString("CXXMethod"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3213 | case CXCursor_UnexposedDecl: |
| 3214 | return createCXString("UnexposedDecl"); |
| 3215 | case CXCursor_ObjCSuperClassRef: |
| 3216 | return createCXString("ObjCSuperClassRef"); |
| 3217 | case CXCursor_ObjCProtocolRef: |
| 3218 | return createCXString("ObjCProtocolRef"); |
| 3219 | case CXCursor_ObjCClassRef: |
| 3220 | return createCXString("ObjCClassRef"); |
| 3221 | case CXCursor_TypeRef: |
| 3222 | return createCXString("TypeRef"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3223 | case CXCursor_TemplateRef: |
| 3224 | return createCXString("TemplateRef"); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3225 | case CXCursor_NamespaceRef: |
| 3226 | return createCXString("NamespaceRef"); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3227 | case CXCursor_MemberRef: |
| 3228 | return createCXString("MemberRef"); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3229 | case CXCursor_LabelRef: |
| 3230 | return createCXString("LabelRef"); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3231 | case CXCursor_OverloadedDeclRef: |
| 3232 | return createCXString("OverloadedDeclRef"); |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 3233 | case CXCursor_VariableRef: |
| 3234 | return createCXString("VariableRef"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3235 | case CXCursor_IntegerLiteral: |
| 3236 | return createCXString("IntegerLiteral"); |
| 3237 | case CXCursor_FloatingLiteral: |
| 3238 | return createCXString("FloatingLiteral"); |
| 3239 | case CXCursor_ImaginaryLiteral: |
| 3240 | return createCXString("ImaginaryLiteral"); |
| 3241 | case CXCursor_StringLiteral: |
| 3242 | return createCXString("StringLiteral"); |
| 3243 | case CXCursor_CharacterLiteral: |
| 3244 | return createCXString("CharacterLiteral"); |
| 3245 | case CXCursor_ParenExpr: |
| 3246 | return createCXString("ParenExpr"); |
| 3247 | case CXCursor_UnaryOperator: |
| 3248 | return createCXString("UnaryOperator"); |
| 3249 | case CXCursor_ArraySubscriptExpr: |
| 3250 | return createCXString("ArraySubscriptExpr"); |
| 3251 | case CXCursor_BinaryOperator: |
| 3252 | return createCXString("BinaryOperator"); |
| 3253 | case CXCursor_CompoundAssignOperator: |
| 3254 | return createCXString("CompoundAssignOperator"); |
| 3255 | case CXCursor_ConditionalOperator: |
| 3256 | return createCXString("ConditionalOperator"); |
| 3257 | case CXCursor_CStyleCastExpr: |
| 3258 | return createCXString("CStyleCastExpr"); |
| 3259 | case CXCursor_CompoundLiteralExpr: |
| 3260 | return createCXString("CompoundLiteralExpr"); |
| 3261 | case CXCursor_InitListExpr: |
| 3262 | return createCXString("InitListExpr"); |
| 3263 | case CXCursor_AddrLabelExpr: |
| 3264 | return createCXString("AddrLabelExpr"); |
| 3265 | case CXCursor_StmtExpr: |
| 3266 | return createCXString("StmtExpr"); |
| 3267 | case CXCursor_GenericSelectionExpr: |
| 3268 | return createCXString("GenericSelectionExpr"); |
| 3269 | case CXCursor_GNUNullExpr: |
| 3270 | return createCXString("GNUNullExpr"); |
| 3271 | case CXCursor_CXXStaticCastExpr: |
| 3272 | return createCXString("CXXStaticCastExpr"); |
| 3273 | case CXCursor_CXXDynamicCastExpr: |
| 3274 | return createCXString("CXXDynamicCastExpr"); |
| 3275 | case CXCursor_CXXReinterpretCastExpr: |
| 3276 | return createCXString("CXXReinterpretCastExpr"); |
| 3277 | case CXCursor_CXXConstCastExpr: |
| 3278 | return createCXString("CXXConstCastExpr"); |
| 3279 | case CXCursor_CXXFunctionalCastExpr: |
| 3280 | return createCXString("CXXFunctionalCastExpr"); |
| 3281 | case CXCursor_CXXTypeidExpr: |
| 3282 | return createCXString("CXXTypeidExpr"); |
| 3283 | case CXCursor_CXXBoolLiteralExpr: |
| 3284 | return createCXString("CXXBoolLiteralExpr"); |
| 3285 | case CXCursor_CXXNullPtrLiteralExpr: |
| 3286 | return createCXString("CXXNullPtrLiteralExpr"); |
| 3287 | case CXCursor_CXXThisExpr: |
| 3288 | return createCXString("CXXThisExpr"); |
| 3289 | case CXCursor_CXXThrowExpr: |
| 3290 | return createCXString("CXXThrowExpr"); |
| 3291 | case CXCursor_CXXNewExpr: |
| 3292 | return createCXString("CXXNewExpr"); |
| 3293 | case CXCursor_CXXDeleteExpr: |
| 3294 | return createCXString("CXXDeleteExpr"); |
| 3295 | case CXCursor_UnaryExpr: |
| 3296 | return createCXString("UnaryExpr"); |
| 3297 | case CXCursor_ObjCStringLiteral: |
| 3298 | return createCXString("ObjCStringLiteral"); |
| 3299 | case CXCursor_ObjCEncodeExpr: |
| 3300 | return createCXString("ObjCEncodeExpr"); |
| 3301 | case CXCursor_ObjCSelectorExpr: |
| 3302 | return createCXString("ObjCSelectorExpr"); |
| 3303 | case CXCursor_ObjCProtocolExpr: |
| 3304 | return createCXString("ObjCProtocolExpr"); |
| 3305 | case CXCursor_ObjCBridgedCastExpr: |
| 3306 | return createCXString("ObjCBridgedCastExpr"); |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 3307 | case CXCursor_BlockExpr: |
| 3308 | return createCXString("BlockExpr"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3309 | case CXCursor_PackExpansionExpr: |
| 3310 | return createCXString("PackExpansionExpr"); |
| 3311 | case CXCursor_SizeOfPackExpr: |
| 3312 | return createCXString("SizeOfPackExpr"); |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 3313 | case CXCursor_LambdaExpr: |
| 3314 | return createCXString("LambdaExpr"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3315 | case CXCursor_UnexposedExpr: |
| 3316 | return createCXString("UnexposedExpr"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3317 | case CXCursor_DeclRefExpr: |
| 3318 | return createCXString("DeclRefExpr"); |
| 3319 | case CXCursor_MemberRefExpr: |
| 3320 | return createCXString("MemberRefExpr"); |
| 3321 | case CXCursor_CallExpr: |
| 3322 | return createCXString("CallExpr"); |
| 3323 | case CXCursor_ObjCMessageExpr: |
| 3324 | return createCXString("ObjCMessageExpr"); |
| 3325 | case CXCursor_UnexposedStmt: |
| 3326 | return createCXString("UnexposedStmt"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3327 | case CXCursor_DeclStmt: |
| 3328 | return createCXString("DeclStmt"); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3329 | case CXCursor_LabelStmt: |
| 3330 | return createCXString("LabelStmt"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3331 | case CXCursor_CompoundStmt: |
| 3332 | return createCXString("CompoundStmt"); |
| 3333 | case CXCursor_CaseStmt: |
| 3334 | return createCXString("CaseStmt"); |
| 3335 | case CXCursor_DefaultStmt: |
| 3336 | return createCXString("DefaultStmt"); |
| 3337 | case CXCursor_IfStmt: |
| 3338 | return createCXString("IfStmt"); |
| 3339 | case CXCursor_SwitchStmt: |
| 3340 | return createCXString("SwitchStmt"); |
| 3341 | case CXCursor_WhileStmt: |
| 3342 | return createCXString("WhileStmt"); |
| 3343 | case CXCursor_DoStmt: |
| 3344 | return createCXString("DoStmt"); |
| 3345 | case CXCursor_ForStmt: |
| 3346 | return createCXString("ForStmt"); |
| 3347 | case CXCursor_GotoStmt: |
| 3348 | return createCXString("GotoStmt"); |
| 3349 | case CXCursor_IndirectGotoStmt: |
| 3350 | return createCXString("IndirectGotoStmt"); |
| 3351 | case CXCursor_ContinueStmt: |
| 3352 | return createCXString("ContinueStmt"); |
| 3353 | case CXCursor_BreakStmt: |
| 3354 | return createCXString("BreakStmt"); |
| 3355 | case CXCursor_ReturnStmt: |
| 3356 | return createCXString("ReturnStmt"); |
| 3357 | case CXCursor_AsmStmt: |
| 3358 | return createCXString("AsmStmt"); |
| 3359 | case CXCursor_ObjCAtTryStmt: |
| 3360 | return createCXString("ObjCAtTryStmt"); |
| 3361 | case CXCursor_ObjCAtCatchStmt: |
| 3362 | return createCXString("ObjCAtCatchStmt"); |
| 3363 | case CXCursor_ObjCAtFinallyStmt: |
| 3364 | return createCXString("ObjCAtFinallyStmt"); |
| 3365 | case CXCursor_ObjCAtThrowStmt: |
| 3366 | return createCXString("ObjCAtThrowStmt"); |
| 3367 | case CXCursor_ObjCAtSynchronizedStmt: |
| 3368 | return createCXString("ObjCAtSynchronizedStmt"); |
| 3369 | case CXCursor_ObjCAutoreleasePoolStmt: |
| 3370 | return createCXString("ObjCAutoreleasePoolStmt"); |
| 3371 | case CXCursor_ObjCForCollectionStmt: |
| 3372 | return createCXString("ObjCForCollectionStmt"); |
| 3373 | case CXCursor_CXXCatchStmt: |
| 3374 | return createCXString("CXXCatchStmt"); |
| 3375 | case CXCursor_CXXTryStmt: |
| 3376 | return createCXString("CXXTryStmt"); |
| 3377 | case CXCursor_CXXForRangeStmt: |
| 3378 | return createCXString("CXXForRangeStmt"); |
| 3379 | case CXCursor_SEHTryStmt: |
| 3380 | return createCXString("SEHTryStmt"); |
| 3381 | case CXCursor_SEHExceptStmt: |
| 3382 | return createCXString("SEHExceptStmt"); |
| 3383 | case CXCursor_SEHFinallyStmt: |
| 3384 | return createCXString("SEHFinallyStmt"); |
| 3385 | case CXCursor_NullStmt: |
| 3386 | return createCXString("NullStmt"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3387 | case CXCursor_InvalidFile: |
| 3388 | return createCXString("InvalidFile"); |
Ted Kremenek | 292db64 | 2010-03-19 20:39:05 +0000 | [diff] [blame] | 3389 | case CXCursor_InvalidCode: |
| 3390 | return createCXString("InvalidCode"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3391 | case CXCursor_NoDeclFound: |
| 3392 | return createCXString("NoDeclFound"); |
| 3393 | case CXCursor_NotImplemented: |
| 3394 | return createCXString("NotImplemented"); |
| 3395 | case CXCursor_TranslationUnit: |
| 3396 | return createCXString("TranslationUnit"); |
Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 3397 | case CXCursor_UnexposedAttr: |
| 3398 | return createCXString("UnexposedAttr"); |
| 3399 | case CXCursor_IBActionAttr: |
| 3400 | return createCXString("attribute(ibaction)"); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3401 | case CXCursor_IBOutletAttr: |
| 3402 | return createCXString("attribute(iboutlet)"); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 3403 | case CXCursor_IBOutletCollectionAttr: |
| 3404 | return createCXString("attribute(iboutletcollection)"); |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 3405 | case CXCursor_CXXFinalAttr: |
| 3406 | return createCXString("attribute(final)"); |
| 3407 | case CXCursor_CXXOverrideAttr: |
| 3408 | return createCXString("attribute(override)"); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 3409 | case CXCursor_AnnotateAttr: |
| 3410 | return createCXString("attribute(annotate)"); |
Argyrios Kyrtzidis | 84b7964 | 2011-12-06 22:05:01 +0000 | [diff] [blame] | 3411 | case CXCursor_AsmLabelAttr: |
| 3412 | return createCXString("asm label"); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3413 | case CXCursor_PreprocessingDirective: |
| 3414 | return createCXString("preprocessing directive"); |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3415 | case CXCursor_MacroDefinition: |
| 3416 | return createCXString("macro definition"); |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3417 | case CXCursor_MacroExpansion: |
| 3418 | return createCXString("macro expansion"); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3419 | case CXCursor_InclusionDirective: |
| 3420 | return createCXString("inclusion directive"); |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 3421 | case CXCursor_Namespace: |
| 3422 | return createCXString("Namespace"); |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 3423 | case CXCursor_LinkageSpec: |
| 3424 | return createCXString("LinkageSpec"); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3425 | case CXCursor_CXXBaseSpecifier: |
| 3426 | return createCXString("C++ base class specifier"); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 3427 | case CXCursor_Constructor: |
| 3428 | return createCXString("CXXConstructor"); |
| 3429 | case CXCursor_Destructor: |
| 3430 | return createCXString("CXXDestructor"); |
| 3431 | case CXCursor_ConversionFunction: |
| 3432 | return createCXString("CXXConversion"); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 3433 | case CXCursor_TemplateTypeParameter: |
| 3434 | return createCXString("TemplateTypeParameter"); |
| 3435 | case CXCursor_NonTypeTemplateParameter: |
| 3436 | return createCXString("NonTypeTemplateParameter"); |
| 3437 | case CXCursor_TemplateTemplateParameter: |
| 3438 | return createCXString("TemplateTemplateParameter"); |
| 3439 | case CXCursor_FunctionTemplate: |
| 3440 | return createCXString("FunctionTemplate"); |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 3441 | case CXCursor_ClassTemplate: |
| 3442 | return createCXString("ClassTemplate"); |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 3443 | case CXCursor_ClassTemplatePartialSpecialization: |
| 3444 | return createCXString("ClassTemplatePartialSpecialization"); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3445 | case CXCursor_NamespaceAlias: |
| 3446 | return createCXString("NamespaceAlias"); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 3447 | case CXCursor_UsingDirective: |
| 3448 | return createCXString("UsingDirective"); |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 3449 | case CXCursor_UsingDeclaration: |
| 3450 | return createCXString("UsingDeclaration"); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3451 | case CXCursor_TypeAliasDecl: |
Douglas Gregor | 352697a | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3452 | return createCXString("TypeAliasDecl"); |
| 3453 | case CXCursor_ObjCSynthesizeDecl: |
| 3454 | return createCXString("ObjCSynthesizeDecl"); |
| 3455 | case CXCursor_ObjCDynamicDecl: |
| 3456 | return createCXString("ObjCDynamicDecl"); |
Argyrios Kyrtzidis | 2dfdb94 | 2011-09-30 17:58:23 +0000 | [diff] [blame] | 3457 | case CXCursor_CXXAccessSpecifier: |
| 3458 | return createCXString("CXXAccessSpecifier"); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3459 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3460 | |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 3461 | llvm_unreachable("Unhandled CXCursorKind"); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 3462 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3463 | |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3464 | struct GetCursorData { |
| 3465 | SourceLocation TokenBeginLoc; |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3466 | bool PointsAtMacroArgExpansion; |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3467 | CXCursor &BestCursor; |
| 3468 | |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3469 | GetCursorData(SourceManager &SM, |
| 3470 | SourceLocation tokenBegin, CXCursor &outputCursor) |
| 3471 | : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) { |
| 3472 | PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin); |
| 3473 | } |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3474 | }; |
| 3475 | |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3476 | static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, |
| 3477 | CXCursor parent, |
| 3478 | CXClientData client_data) { |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3479 | GetCursorData *Data = static_cast<GetCursorData *>(client_data); |
| 3480 | CXCursor *BestCursor = &Data->BestCursor; |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3481 | |
| 3482 | // If we point inside a macro argument we should provide info of what the |
| 3483 | // token is so use the actual cursor, don't replace it with a macro expansion |
| 3484 | // cursor. |
| 3485 | if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion) |
| 3486 | return CXChildVisit_Recurse; |
Argyrios Kyrtzidis | 65ab907 | 2011-09-26 19:05:37 +0000 | [diff] [blame] | 3487 | |
| 3488 | if (clang_isDeclaration(cursor.kind)) { |
| 3489 | // Avoid having the implicit methods override the property decls. |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 3490 | if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) |
Argyrios Kyrtzidis | 65ab907 | 2011-09-26 19:05:37 +0000 | [diff] [blame] | 3491 | if (MD->isImplicit()) |
| 3492 | return CXChildVisit_Break; |
| 3493 | } |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3494 | |
| 3495 | if (clang_isExpression(cursor.kind) && |
| 3496 | clang_isDeclaration(BestCursor->kind)) { |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 3497 | if (Decl *D = getCursorDecl(*BestCursor)) { |
| 3498 | // Avoid having the cursor of an expression replace the declaration cursor |
| 3499 | // when the expression source range overlaps the declaration range. |
| 3500 | // This can happen for C++ constructor expressions whose range generally |
| 3501 | // include the variable declaration, e.g.: |
| 3502 | // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor. |
| 3503 | if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() && |
| 3504 | D->getLocation() == Data->TokenBeginLoc) |
| 3505 | return CXChildVisit_Break; |
| 3506 | } |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3509 | // If our current best cursor is the construction of a temporary object, |
| 3510 | // don't replace that cursor with a type reference, because we want |
| 3511 | // clang_getCursor() to point at the constructor. |
| 3512 | if (clang_isExpression(BestCursor->kind) && |
| 3513 | isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 3514 | cursor.kind == CXCursor_TypeRef) { |
| 3515 | // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it |
| 3516 | // as having the actual point on the type reference. |
| 3517 | *BestCursor = getTypeRefedCallExprCursor(*BestCursor); |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3518 | return CXChildVisit_Recurse; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 3519 | } |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3520 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3521 | *BestCursor = cursor; |
| 3522 | return CXChildVisit_Recurse; |
| 3523 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3524 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 3525 | CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { |
| 3526 | if (!TU) |
Ted Kremenek | f462989 | 2010-01-14 01:51:23 +0000 | [diff] [blame] | 3527 | return clang_getNullCursor(); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3528 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3529 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 3530 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 3531 | |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3532 | SourceLocation SLoc = cxloc::translateSourceLocation(Loc); |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3533 | CXCursor Result = cxcursor::getCursor(TU, SLoc); |
Ted Kremenek | a629ea4 | 2010-07-29 00:52:07 +0000 | [diff] [blame] | 3534 | |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3535 | bool Logging = getenv("LIBCLANG_LOGGING"); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3536 | if (Logging) { |
| 3537 | CXFile SearchFile; |
| 3538 | unsigned SearchLine, SearchColumn; |
| 3539 | CXFile ResultFile; |
| 3540 | unsigned ResultLine, ResultColumn; |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3541 | CXString SearchFileName, ResultFileName, KindSpelling, USR; |
| 3542 | const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : ""; |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3543 | CXSourceLocation ResultLoc = clang_getCursorLocation(Result); |
| 3544 | |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 3545 | clang_getExpansionLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, 0); |
| 3546 | clang_getExpansionLocation(ResultLoc, &ResultFile, &ResultLine, |
| 3547 | &ResultColumn, 0); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3548 | SearchFileName = clang_getFileName(SearchFile); |
| 3549 | ResultFileName = clang_getFileName(ResultFile); |
| 3550 | KindSpelling = clang_getCursorKindSpelling(Result.kind); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3551 | USR = clang_getCursorUSR(Result); |
| 3552 | 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] | 3553 | clang_getCString(SearchFileName), SearchLine, SearchColumn, |
| 3554 | clang_getCString(KindSpelling), |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3555 | clang_getCString(ResultFileName), ResultLine, ResultColumn, |
| 3556 | clang_getCString(USR), IsDef); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3557 | clang_disposeString(SearchFileName); |
| 3558 | clang_disposeString(ResultFileName); |
| 3559 | clang_disposeString(KindSpelling); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3560 | clang_disposeString(USR); |
Douglas Gregor | 0aefbd8 | 2010-12-10 01:45:00 +0000 | [diff] [blame] | 3561 | |
| 3562 | CXCursor Definition = clang_getCursorDefinition(Result); |
| 3563 | if (!clang_equalCursors(Definition, clang_getNullCursor())) { |
| 3564 | CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition); |
| 3565 | CXString DefinitionKindSpelling |
| 3566 | = clang_getCursorKindSpelling(Definition.kind); |
| 3567 | CXFile DefinitionFile; |
| 3568 | unsigned DefinitionLine, DefinitionColumn; |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 3569 | clang_getExpansionLocation(DefinitionLoc, &DefinitionFile, |
| 3570 | &DefinitionLine, &DefinitionColumn, 0); |
Douglas Gregor | 0aefbd8 | 2010-12-10 01:45:00 +0000 | [diff] [blame] | 3571 | CXString DefinitionFileName = clang_getFileName(DefinitionFile); |
| 3572 | fprintf(stderr, " -> %s(%s:%d:%d)\n", |
| 3573 | clang_getCString(DefinitionKindSpelling), |
| 3574 | clang_getCString(DefinitionFileName), |
| 3575 | DefinitionLine, DefinitionColumn); |
| 3576 | clang_disposeString(DefinitionFileName); |
| 3577 | clang_disposeString(DefinitionKindSpelling); |
| 3578 | } |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3579 | } |
| 3580 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3581 | return Result; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 3582 | } |
| 3583 | |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3584 | CXCursor clang_getNullCursor(void) { |
Douglas Gregor | 5bfb8c1 | 2010-01-20 23:34:41 +0000 | [diff] [blame] | 3585 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3586 | } |
| 3587 | |
| 3588 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 3589 | return X == Y; |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3590 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 3591 | |
Douglas Gregor | 9ce5584 | 2010-11-20 00:09:34 +0000 | [diff] [blame] | 3592 | unsigned clang_hashCursor(CXCursor C) { |
| 3593 | unsigned Index = 0; |
| 3594 | if (clang_isExpression(C.kind) || clang_isStatement(C.kind)) |
| 3595 | Index = 1; |
| 3596 | |
| 3597 | return llvm::DenseMapInfo<std::pair<unsigned, void*> >::getHashValue( |
| 3598 | std::make_pair(C.kind, C.data[Index])); |
| 3599 | } |
| 3600 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3601 | unsigned clang_isInvalid(enum CXCursorKind K) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 3602 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 3603 | } |
| 3604 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3605 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3606 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 3607 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 3608 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3609 | unsigned clang_isReference(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3610 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 3611 | } |
| 3612 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3613 | unsigned clang_isExpression(enum CXCursorKind K) { |
| 3614 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; |
| 3615 | } |
| 3616 | |
| 3617 | unsigned clang_isStatement(enum CXCursorKind K) { |
| 3618 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; |
| 3619 | } |
| 3620 | |
Douglas Gregor | 8be80e1 | 2011-07-06 03:00:34 +0000 | [diff] [blame] | 3621 | unsigned clang_isAttribute(enum CXCursorKind K) { |
| 3622 | return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr; |
| 3623 | } |
| 3624 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3625 | unsigned clang_isTranslationUnit(enum CXCursorKind K) { |
| 3626 | return K == CXCursor_TranslationUnit; |
| 3627 | } |
| 3628 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3629 | unsigned clang_isPreprocessing(enum CXCursorKind K) { |
| 3630 | return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; |
| 3631 | } |
| 3632 | |
Ted Kremenek | ad6eff6 | 2010-03-08 21:17:29 +0000 | [diff] [blame] | 3633 | unsigned clang_isUnexposed(enum CXCursorKind K) { |
| 3634 | switch (K) { |
| 3635 | case CXCursor_UnexposedDecl: |
| 3636 | case CXCursor_UnexposedExpr: |
| 3637 | case CXCursor_UnexposedStmt: |
| 3638 | case CXCursor_UnexposedAttr: |
| 3639 | return true; |
| 3640 | default: |
| 3641 | return false; |
| 3642 | } |
| 3643 | } |
| 3644 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3645 | CXCursorKind clang_getCursorKind(CXCursor C) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 3646 | return C.kind; |
| 3647 | } |
| 3648 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3649 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 3650 | if (clang_isReference(C.kind)) { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3651 | switch (C.kind) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3652 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3653 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 3654 | = getCursorObjCSuperClassRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3655 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3656 | } |
| 3657 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3658 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3659 | std::pair<ObjCProtocolDecl *, SourceLocation> P |
| 3660 | = getCursorObjCProtocolRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3661 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3662 | } |
| 3663 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3664 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3665 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 3666 | = getCursorObjCClassRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3667 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3668 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3669 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3670 | case CXCursor_TypeRef: { |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3671 | std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3672 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3673 | } |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3674 | |
| 3675 | case CXCursor_TemplateRef: { |
| 3676 | std::pair<TemplateDecl *, SourceLocation> P = getCursorTemplateRef(C); |
| 3677 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3678 | } |
| 3679 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3680 | case CXCursor_NamespaceRef: { |
| 3681 | std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); |
| 3682 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3683 | } |
| 3684 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3685 | case CXCursor_MemberRef: { |
| 3686 | std::pair<FieldDecl *, SourceLocation> P = getCursorMemberRef(C); |
| 3687 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3688 | } |
| 3689 | |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 3690 | case CXCursor_VariableRef: { |
| 3691 | std::pair<VarDecl *, SourceLocation> P = getCursorVariableRef(C); |
| 3692 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3693 | } |
| 3694 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3695 | case CXCursor_CXXBaseSpecifier: { |
Douglas Gregor | 1b0f7af | 2010-10-02 19:51:13 +0000 | [diff] [blame] | 3696 | CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); |
| 3697 | if (!BaseSpec) |
| 3698 | return clang_getNullLocation(); |
| 3699 | |
| 3700 | if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) |
| 3701 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3702 | TSInfo->getTypeLoc().getBeginLoc()); |
| 3703 | |
| 3704 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3705 | BaseSpec->getSourceRange().getBegin()); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3706 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3707 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3708 | case CXCursor_LabelRef: { |
| 3709 | std::pair<LabelStmt *, SourceLocation> P = getCursorLabelRef(C); |
| 3710 | return cxloc::translateSourceLocation(getCursorContext(C), P.second); |
| 3711 | } |
| 3712 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3713 | case CXCursor_OverloadedDeclRef: |
| 3714 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3715 | getCursorOverloadedDeclRef(C).second); |
| 3716 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3717 | default: |
| 3718 | // FIXME: Need a way to enumerate all non-reference cases. |
| 3719 | llvm_unreachable("Missed a reference kind"); |
| 3720 | } |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3721 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3722 | |
| 3723 | if (clang_isExpression(C.kind)) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3724 | return cxloc::translateSourceLocation(getCursorContext(C), |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3725 | getLocationFromExpr(getCursorExpr(C))); |
| 3726 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3727 | if (clang_isStatement(C.kind)) |
| 3728 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3729 | getCursorStmt(C)->getLocStart()); |
| 3730 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3731 | if (C.kind == CXCursor_PreprocessingDirective) { |
| 3732 | SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); |
| 3733 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3734 | } |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3735 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3736 | if (C.kind == CXCursor_MacroExpansion) { |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 3737 | SourceLocation L |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3738 | = cxcursor::getCursorMacroExpansion(C)->getSourceRange().getBegin(); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3739 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3740 | } |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3741 | |
| 3742 | if (C.kind == CXCursor_MacroDefinition) { |
| 3743 | SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); |
| 3744 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3745 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3746 | |
| 3747 | if (C.kind == CXCursor_InclusionDirective) { |
| 3748 | SourceLocation L |
| 3749 | = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); |
| 3750 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3751 | } |
| 3752 | |
Ted Kremenek | 9a700d2 | 2010-05-12 06:16:13 +0000 | [diff] [blame] | 3753 | if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 3754 | return clang_getNullLocation(); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3755 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3756 | Decl *D = getCursorDecl(C); |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 3757 | if (!D) |
| 3758 | return clang_getNullLocation(); |
| 3759 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3760 | SourceLocation Loc = D->getLocation(); |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 3761 | // FIXME: Multiple variables declared in a single declaration |
| 3762 | // currently lack the information needed to correctly determine their |
| 3763 | // ranges when accounting for the type-specifier. We use context |
| 3764 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3765 | // and if so, whether it is the first decl. |
| 3766 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3767 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3768 | Loc = VD->getLocation(); |
| 3769 | } |
| 3770 | |
Douglas Gregor | 2ca54fe | 2010-03-22 15:53:50 +0000 | [diff] [blame] | 3771 | return cxloc::translateSourceLocation(getCursorContext(C), Loc); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3772 | } |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3773 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3774 | } // end extern "C" |
| 3775 | |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3776 | CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) { |
| 3777 | assert(TU); |
| 3778 | |
| 3779 | // Guard against an invalid SourceLocation, or we may assert in one |
| 3780 | // of the following calls. |
| 3781 | if (SLoc.isInvalid()) |
| 3782 | return clang_getNullCursor(); |
| 3783 | |
| 3784 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 3785 | |
| 3786 | // Translate the given source location to make it point at the beginning of |
| 3787 | // the token under the cursor. |
| 3788 | SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), |
| 3789 | CXXUnit->getASTContext().getLangOptions()); |
| 3790 | |
| 3791 | CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 3792 | if (SLoc.isValid()) { |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3793 | GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result); |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3794 | CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData, |
| 3795 | /*VisitPreprocessorLast=*/true, |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 3796 | /*VisitIncludedEntities=*/false, |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3797 | SourceLocation(SLoc)); |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 3798 | CursorVis.visitFileRegion(); |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3799 | } |
| 3800 | |
| 3801 | return Result; |
| 3802 | } |
| 3803 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3804 | static SourceRange getRawCursorExtent(CXCursor C) { |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3805 | if (clang_isReference(C.kind)) { |
| 3806 | switch (C.kind) { |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3807 | case CXCursor_ObjCSuperClassRef: |
| 3808 | return getCursorObjCSuperClassRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3809 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3810 | case CXCursor_ObjCProtocolRef: |
| 3811 | return getCursorObjCProtocolRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3812 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3813 | case CXCursor_ObjCClassRef: |
| 3814 | return getCursorObjCClassRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3815 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3816 | case CXCursor_TypeRef: |
| 3817 | return getCursorTypeRef(C).second; |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3818 | |
| 3819 | case CXCursor_TemplateRef: |
| 3820 | return getCursorTemplateRef(C).second; |
| 3821 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3822 | case CXCursor_NamespaceRef: |
| 3823 | return getCursorNamespaceRef(C).second; |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3824 | |
| 3825 | case CXCursor_MemberRef: |
| 3826 | return getCursorMemberRef(C).second; |
| 3827 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3828 | case CXCursor_CXXBaseSpecifier: |
Douglas Gregor | 1b0f7af | 2010-10-02 19:51:13 +0000 | [diff] [blame] | 3829 | return getCursorCXXBaseSpecifier(C)->getSourceRange(); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3830 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3831 | case CXCursor_LabelRef: |
| 3832 | return getCursorLabelRef(C).second; |
| 3833 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3834 | case CXCursor_OverloadedDeclRef: |
| 3835 | return getCursorOverloadedDeclRef(C).second; |
| 3836 | |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 3837 | case CXCursor_VariableRef: |
| 3838 | return getCursorVariableRef(C).second; |
| 3839 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3840 | default: |
| 3841 | // FIXME: Need a way to enumerate all non-reference cases. |
| 3842 | llvm_unreachable("Missed a reference kind"); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3843 | } |
| 3844 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3845 | |
| 3846 | if (clang_isExpression(C.kind)) |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3847 | return getCursorExpr(C)->getSourceRange(); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3848 | |
| 3849 | if (clang_isStatement(C.kind)) |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3850 | return getCursorStmt(C)->getSourceRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3851 | |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 3852 | if (clang_isAttribute(C.kind)) |
| 3853 | return getCursorAttr(C)->getRange(); |
| 3854 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3855 | if (C.kind == CXCursor_PreprocessingDirective) |
| 3856 | return cxcursor::getCursorPreprocessingDirective(C); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3857 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 3858 | if (C.kind == CXCursor_MacroExpansion) { |
| 3859 | ASTUnit *TU = getCursorASTUnit(C); |
| 3860 | SourceRange Range = cxcursor::getCursorMacroExpansion(C)->getSourceRange(); |
| 3861 | return TU->mapRangeFromPreamble(Range); |
| 3862 | } |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3863 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 3864 | if (C.kind == CXCursor_MacroDefinition) { |
| 3865 | ASTUnit *TU = getCursorASTUnit(C); |
| 3866 | SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange(); |
| 3867 | return TU->mapRangeFromPreamble(Range); |
| 3868 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3869 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 3870 | if (C.kind == CXCursor_InclusionDirective) { |
| 3871 | ASTUnit *TU = getCursorASTUnit(C); |
| 3872 | SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange(); |
| 3873 | return TU->mapRangeFromPreamble(Range); |
| 3874 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3875 | |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 3876 | if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { |
| 3877 | Decl *D = cxcursor::getCursorDecl(C); |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 3878 | if (!D) |
| 3879 | return SourceRange(); |
| 3880 | |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 3881 | SourceRange R = D->getSourceRange(); |
| 3882 | // FIXME: Multiple variables declared in a single declaration |
| 3883 | // currently lack the information needed to correctly determine their |
| 3884 | // ranges when accounting for the type-specifier. We use context |
| 3885 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3886 | // and if so, whether it is the first decl. |
| 3887 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3888 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3889 | R.setBegin(VD->getLocation()); |
| 3890 | } |
| 3891 | return R; |
| 3892 | } |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3893 | return SourceRange(); |
| 3894 | } |
| 3895 | |
| 3896 | /// \brief Retrieves the "raw" cursor extent, which is then extended to include |
| 3897 | /// the decl-specifier-seq for declarations. |
| 3898 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) { |
| 3899 | if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { |
| 3900 | Decl *D = cxcursor::getCursorDecl(C); |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 3901 | if (!D) |
| 3902 | return SourceRange(); |
| 3903 | |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3904 | SourceRange R = D->getSourceRange(); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3905 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 3906 | // Adjust the start of the location for declarations preceded by |
| 3907 | // declaration specifiers. |
| 3908 | SourceLocation StartLoc; |
| 3909 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
| 3910 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
| 3911 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 3912 | } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
| 3913 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
| 3914 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 3915 | } |
| 3916 | |
| 3917 | if (StartLoc.isValid() && R.getBegin().isValid() && |
| 3918 | SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin())) |
| 3919 | R.setBegin(StartLoc); |
| 3920 | |
| 3921 | // FIXME: Multiple variables declared in a single declaration |
| 3922 | // currently lack the information needed to correctly determine their |
| 3923 | // ranges when accounting for the type-specifier. We use context |
| 3924 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3925 | // and if so, whether it is the first decl. |
| 3926 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3927 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3928 | R.setBegin(VD->getLocation()); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
| 3931 | return R; |
| 3932 | } |
| 3933 | |
| 3934 | return getRawCursorExtent(C); |
| 3935 | } |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3936 | |
| 3937 | extern "C" { |
| 3938 | |
| 3939 | CXSourceRange clang_getCursorExtent(CXCursor C) { |
| 3940 | SourceRange R = getRawCursorExtent(C); |
| 3941 | if (R.isInvalid()) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 3942 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3943 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3944 | return cxloc::translateSourceRange(getCursorContext(C), R); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3945 | } |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3946 | |
| 3947 | CXCursor clang_getCursorReferenced(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3948 | if (clang_isInvalid(C.kind)) |
| 3949 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3950 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3951 | CXTranslationUnit tu = getCursorTU(C); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3952 | if (clang_isDeclaration(C.kind)) { |
| 3953 | Decl *D = getCursorDecl(C); |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 3954 | if (!D) |
| 3955 | return clang_getNullCursor(); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3956 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3957 | return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3958 | if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3959 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 3960 | return MakeCXCursor(Property, tu); |
| 3961 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3962 | return C; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3965 | if (clang_isExpression(C.kind)) { |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3966 | Expr *E = getCursorExpr(C); |
| 3967 | Decl *D = getDeclFromExpr(E); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 3968 | if (D) { |
| 3969 | CXCursor declCursor = MakeCXCursor(D, tu); |
| 3970 | declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C), |
| 3971 | declCursor); |
| 3972 | return declCursor; |
| 3973 | } |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3974 | |
| 3975 | if (OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3976 | return MakeCursorOverloadedDeclRef(Ovl, tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3977 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3978 | return clang_getNullCursor(); |
| 3979 | } |
| 3980 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3981 | if (clang_isStatement(C.kind)) { |
| 3982 | Stmt *S = getCursorStmt(C); |
| 3983 | if (GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) |
Ted Kremenek | 37c2e96 | 2011-03-15 23:47:49 +0000 | [diff] [blame] | 3984 | if (LabelDecl *label = Goto->getLabel()) |
| 3985 | if (LabelStmt *labelS = label->getStmt()) |
| 3986 | return MakeCXCursor(labelS, getCursorDecl(C), tu); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3987 | |
| 3988 | return clang_getNullCursor(); |
| 3989 | } |
| 3990 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3991 | if (C.kind == CXCursor_MacroExpansion) { |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3992 | if (MacroDefinition *Def = getCursorMacroExpansion(C)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3993 | return MakeMacroDefinitionCursor(Def, tu); |
Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 3994 | } |
| 3995 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3996 | if (!clang_isReference(C.kind)) |
| 3997 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3998 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3999 | switch (C.kind) { |
| 4000 | case CXCursor_ObjCSuperClassRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4001 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4002 | |
| 4003 | case CXCursor_ObjCProtocolRef: { |
Argyrios Kyrtzidis | 98c16b8 | 2012-01-24 19:40:15 +0000 | [diff] [blame] | 4004 | ObjCProtocolDecl *Prot = getCursorObjCProtocolRef(C).first; |
| 4005 | if (ObjCProtocolDecl *Def = Prot->getDefinition()) |
| 4006 | return MakeCXCursor(Def, tu); |
| 4007 | |
Argyrios Kyrtzidis | c15707d | 2012-01-24 21:39:26 +0000 | [diff] [blame] | 4008 | return MakeCXCursor(Prot, tu); |
Argyrios Kyrtzidis | 98c16b8 | 2012-01-24 19:40:15 +0000 | [diff] [blame] | 4009 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4010 | |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 4011 | case CXCursor_ObjCClassRef: { |
| 4012 | ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
| 4013 | if (ObjCInterfaceDecl *Def = Class->getDefinition()) |
| 4014 | return MakeCXCursor(Def, tu); |
| 4015 | |
Argyrios Kyrtzidis | c15707d | 2012-01-24 21:39:26 +0000 | [diff] [blame] | 4016 | return MakeCXCursor(Class, tu); |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 4017 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 4018 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4019 | case CXCursor_TypeRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4020 | return MakeCXCursor(getCursorTypeRef(C).first, tu ); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 4021 | |
| 4022 | case CXCursor_TemplateRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4023 | return MakeCXCursor(getCursorTemplateRef(C).first, tu ); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 4024 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 4025 | case CXCursor_NamespaceRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4026 | return MakeCXCursor(getCursorNamespaceRef(C).first, tu ); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 4027 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 4028 | case CXCursor_MemberRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4029 | return MakeCXCursor(getCursorMemberRef(C).first, tu ); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 4030 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 4031 | case CXCursor_CXXBaseSpecifier: { |
| 4032 | CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); |
| 4033 | return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4034 | tu )); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 4035 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4036 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 4037 | case CXCursor_LabelRef: |
| 4038 | // FIXME: We end up faking the "parent" declaration here because we |
| 4039 | // don't want to make CXCursor larger. |
| 4040 | return MakeCXCursor(getCursorLabelRef(C).first, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4041 | static_cast<ASTUnit*>(tu->TUData)->getASTContext() |
| 4042 | .getTranslationUnitDecl(), |
| 4043 | tu); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 4044 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4045 | case CXCursor_OverloadedDeclRef: |
| 4046 | return C; |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 4047 | |
| 4048 | case CXCursor_VariableRef: |
| 4049 | return MakeCXCursor(getCursorVariableRef(C).first, tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4050 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4051 | default: |
| 4052 | // We would prefer to enumerate all non-reference cursor kinds here. |
| 4053 | llvm_unreachable("Unhandled reference cursor kind"); |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4054 | } |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 4055 | } |
| 4056 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4057 | CXCursor clang_getCursorDefinition(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 4058 | if (clang_isInvalid(C.kind)) |
| 4059 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4060 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4061 | CXTranslationUnit TU = getCursorTU(C); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4062 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4063 | bool WasReference = false; |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 4064 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4065 | C = clang_getCursorReferenced(C); |
| 4066 | WasReference = true; |
| 4067 | } |
| 4068 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 4069 | if (C.kind == CXCursor_MacroExpansion) |
Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 4070 | return clang_getCursorReferenced(C); |
| 4071 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4072 | if (!clang_isDeclaration(C.kind)) |
| 4073 | return clang_getNullCursor(); |
| 4074 | |
| 4075 | Decl *D = getCursorDecl(C); |
| 4076 | if (!D) |
| 4077 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4078 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4079 | switch (D->getKind()) { |
| 4080 | // Declaration kinds that don't really separate the notions of |
| 4081 | // declaration and definition. |
| 4082 | case Decl::Namespace: |
| 4083 | case Decl::Typedef: |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4084 | case Decl::TypeAlias: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4085 | case Decl::TypeAliasTemplate: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4086 | case Decl::TemplateTypeParm: |
| 4087 | case Decl::EnumConstant: |
| 4088 | case Decl::Field: |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 4089 | case Decl::IndirectField: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4090 | case Decl::ObjCIvar: |
| 4091 | case Decl::ObjCAtDefsField: |
| 4092 | case Decl::ImplicitParam: |
| 4093 | case Decl::ParmVar: |
| 4094 | case Decl::NonTypeTemplateParm: |
| 4095 | case Decl::TemplateTemplateParm: |
| 4096 | case Decl::ObjCCategoryImpl: |
| 4097 | case Decl::ObjCImplementation: |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 4098 | case Decl::AccessSpec: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4099 | case Decl::LinkageSpec: |
| 4100 | case Decl::ObjCPropertyImpl: |
| 4101 | case Decl::FileScopeAsm: |
| 4102 | case Decl::StaticAssert: |
| 4103 | case Decl::Block: |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 4104 | case Decl::Label: // FIXME: Is this right?? |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4105 | case Decl::ClassScopeFunctionSpecialization: |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4106 | case Decl::Import: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4107 | return C; |
| 4108 | |
| 4109 | // Declaration kinds that don't make any sense here, but are |
| 4110 | // nonetheless harmless. |
| 4111 | case Decl::TranslationUnit: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4112 | break; |
| 4113 | |
| 4114 | // Declaration kinds for which the definition is not resolvable. |
| 4115 | case Decl::UnresolvedUsingTypename: |
| 4116 | case Decl::UnresolvedUsingValue: |
| 4117 | break; |
| 4118 | |
| 4119 | case Decl::UsingDirective: |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 4120 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4121 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4122 | |
| 4123 | case Decl::NamespaceAlias: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4124 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4125 | |
| 4126 | case Decl::Enum: |
| 4127 | case Decl::Record: |
| 4128 | case Decl::CXXRecord: |
| 4129 | case Decl::ClassTemplateSpecialization: |
| 4130 | case Decl::ClassTemplatePartialSpecialization: |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4131 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4132 | return MakeCXCursor(Def, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4133 | return clang_getNullCursor(); |
| 4134 | |
| 4135 | case Decl::Function: |
| 4136 | case Decl::CXXMethod: |
| 4137 | case Decl::CXXConstructor: |
| 4138 | case Decl::CXXDestructor: |
| 4139 | case Decl::CXXConversion: { |
| 4140 | const FunctionDecl *Def = 0; |
| 4141 | if (cast<FunctionDecl>(D)->getBody(Def)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4142 | return MakeCXCursor(const_cast<FunctionDecl *>(Def), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4143 | return clang_getNullCursor(); |
| 4144 | } |
| 4145 | |
| 4146 | case Decl::Var: { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 4147 | // Ask the variable if it has a definition. |
| 4148 | if (VarDecl *Def = cast<VarDecl>(D)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4149 | return MakeCXCursor(Def, TU); |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 4150 | return clang_getNullCursor(); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4151 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4152 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4153 | case Decl::FunctionTemplate: { |
| 4154 | const FunctionDecl *Def = 0; |
| 4155 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4156 | return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4157 | return clang_getNullCursor(); |
| 4158 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4159 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4160 | case Decl::ClassTemplate: { |
| 4161 | if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4162 | ->getDefinition()) |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 4163 | return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4164 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4165 | return clang_getNullCursor(); |
| 4166 | } |
| 4167 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4168 | case Decl::Using: |
| 4169 | return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4170 | D->getLocation(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4171 | |
| 4172 | case Decl::UsingShadow: |
| 4173 | return clang_getCursorDefinition( |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4174 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4175 | TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4176 | |
| 4177 | case Decl::ObjCMethod: { |
| 4178 | ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); |
| 4179 | if (Method->isThisDeclarationADefinition()) |
| 4180 | return C; |
| 4181 | |
| 4182 | // Dig out the method definition in the associated |
| 4183 | // @implementation, if we have it. |
| 4184 | // FIXME: The ASTs should make finding the definition easier. |
| 4185 | if (ObjCInterfaceDecl *Class |
| 4186 | = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) |
| 4187 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) |
| 4188 | if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), |
| 4189 | Method->isInstanceMethod())) |
| 4190 | if (Def->isThisDeclarationADefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4191 | return MakeCXCursor(Def, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4192 | |
| 4193 | return clang_getNullCursor(); |
| 4194 | } |
| 4195 | |
| 4196 | case Decl::ObjCCategory: |
| 4197 | if (ObjCCategoryImplDecl *Impl |
| 4198 | = cast<ObjCCategoryDecl>(D)->getImplementation()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4199 | return MakeCXCursor(Impl, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4200 | return clang_getNullCursor(); |
| 4201 | |
| 4202 | case Decl::ObjCProtocol: |
Douglas Gregor | 5e2a1ff | 2012-01-01 19:29:29 +0000 | [diff] [blame] | 4203 | if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(D)->getDefinition()) |
| 4204 | return MakeCXCursor(Def, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4205 | return clang_getNullCursor(); |
| 4206 | |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 4207 | case Decl::ObjCInterface: { |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4208 | // There are two notions of a "definition" for an Objective-C |
| 4209 | // class: the interface and its implementation. When we resolved a |
| 4210 | // reference to an Objective-C class, produce the @interface as |
| 4211 | // the definition; when we were provided with the interface, |
| 4212 | // produce the @implementation as the definition. |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 4213 | ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4214 | if (WasReference) { |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 4215 | if (ObjCInterfaceDecl *Def = IFace->getDefinition()) |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 4216 | return MakeCXCursor(Def, TU); |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 4217 | } else if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4218 | return MakeCXCursor(Impl, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4219 | return clang_getNullCursor(); |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 4220 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4221 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4222 | case Decl::ObjCProperty: |
| 4223 | // FIXME: We don't really know where to find the |
| 4224 | // ObjCPropertyImplDecls that implement this property. |
| 4225 | return clang_getNullCursor(); |
| 4226 | |
| 4227 | case Decl::ObjCCompatibleAlias: |
| 4228 | if (ObjCInterfaceDecl *Class |
| 4229 | = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 4230 | if (ObjCInterfaceDecl *Def = Class->getDefinition()) |
| 4231 | return MakeCXCursor(Def, TU); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4232 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4233 | return clang_getNullCursor(); |
| 4234 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4235 | case Decl::Friend: |
| 4236 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4237 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4238 | return clang_getNullCursor(); |
| 4239 | |
| 4240 | case Decl::FriendTemplate: |
| 4241 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4242 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4243 | return clang_getNullCursor(); |
| 4244 | } |
| 4245 | |
| 4246 | return clang_getNullCursor(); |
| 4247 | } |
| 4248 | |
| 4249 | unsigned clang_isCursorDefinition(CXCursor C) { |
| 4250 | if (!clang_isDeclaration(C.kind)) |
| 4251 | return 0; |
| 4252 | |
| 4253 | return clang_getCursorDefinition(C) == C; |
| 4254 | } |
| 4255 | |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4256 | CXCursor clang_getCanonicalCursor(CXCursor C) { |
| 4257 | if (!clang_isDeclaration(C.kind)) |
| 4258 | return C; |
| 4259 | |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4260 | if (Decl *D = getCursorDecl(C)) { |
Argyrios Kyrtzidis | debb00f | 2011-07-15 22:37:58 +0000 | [diff] [blame] | 4261 | if (ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 4262 | if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl()) |
| 4263 | return MakeCXCursor(CatD, getCursorTU(C)); |
| 4264 | |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4265 | if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) |
| 4266 | if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
| 4267 | return MakeCXCursor(IFD, getCursorTU(C)); |
| 4268 | |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4269 | return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C)); |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4270 | } |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4271 | |
| 4272 | return C; |
| 4273 | } |
| 4274 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4275 | unsigned clang_getNumOverloadedDecls(CXCursor C) { |
Douglas Gregor | 7c432dd | 2010-09-16 13:54:00 +0000 | [diff] [blame] | 4276 | if (C.kind != CXCursor_OverloadedDeclRef) |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4277 | return 0; |
| 4278 | |
| 4279 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
| 4280 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
| 4281 | return E->getNumDecls(); |
| 4282 | |
| 4283 | if (OverloadedTemplateStorage *S |
| 4284 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 4285 | return S->size(); |
| 4286 | |
| 4287 | Decl *D = Storage.get<Decl*>(); |
| 4288 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Argyrios Kyrtzidis | 826faa2 | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 4289 | return Using->shadow_size(); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4290 | |
| 4291 | return 0; |
| 4292 | } |
| 4293 | |
| 4294 | CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { |
Douglas Gregor | 7c432dd | 2010-09-16 13:54:00 +0000 | [diff] [blame] | 4295 | if (cursor.kind != CXCursor_OverloadedDeclRef) |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4296 | return clang_getNullCursor(); |
| 4297 | |
| 4298 | if (index >= clang_getNumOverloadedDecls(cursor)) |
| 4299 | return clang_getNullCursor(); |
| 4300 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4301 | CXTranslationUnit TU = getCursorTU(cursor); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4302 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first; |
| 4303 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4304 | return MakeCXCursor(E->decls_begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4305 | |
| 4306 | if (OverloadedTemplateStorage *S |
| 4307 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4308 | return MakeCXCursor(S->begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4309 | |
| 4310 | Decl *D = Storage.get<Decl*>(); |
| 4311 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) { |
| 4312 | // FIXME: This is, unfortunately, linear time. |
| 4313 | UsingDecl::shadow_iterator Pos = Using->shadow_begin(); |
| 4314 | std::advance(Pos, index); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4315 | return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4316 | } |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4317 | |
| 4318 | return clang_getNullCursor(); |
| 4319 | } |
| 4320 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 4321 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4322 | const char **startBuf, |
| 4323 | const char **endBuf, |
| 4324 | unsigned *startLine, |
| 4325 | unsigned *startColumn, |
| 4326 | unsigned *endLine, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 4327 | unsigned *endColumn) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 4328 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
| 4329 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4330 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 4331 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4332 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4333 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 4334 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 4335 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 4336 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 4337 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 4338 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 4339 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 4340 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4341 | |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 4342 | |
| 4343 | CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags, |
| 4344 | unsigned PieceIndex) { |
| 4345 | RefNamePieces Pieces; |
| 4346 | |
| 4347 | switch (C.kind) { |
| 4348 | case CXCursor_MemberRefExpr: |
| 4349 | if (MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C))) |
| 4350 | Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(), |
| 4351 | E->getQualifierLoc().getSourceRange()); |
| 4352 | break; |
| 4353 | |
| 4354 | case CXCursor_DeclRefExpr: |
| 4355 | if (DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C))) |
| 4356 | Pieces = buildPieces(NameFlags, false, E->getNameInfo(), |
| 4357 | E->getQualifierLoc().getSourceRange(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4358 | E->getOptionalExplicitTemplateArgs()); |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 4359 | break; |
| 4360 | |
| 4361 | case CXCursor_CallExpr: |
| 4362 | if (CXXOperatorCallExpr *OCE = |
| 4363 | dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) { |
| 4364 | Expr *Callee = OCE->getCallee(); |
| 4365 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) |
| 4366 | Callee = ICE->getSubExpr(); |
| 4367 | |
| 4368 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) |
| 4369 | Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(), |
| 4370 | DRE->getQualifierLoc().getSourceRange()); |
| 4371 | } |
| 4372 | break; |
| 4373 | |
| 4374 | default: |
| 4375 | break; |
| 4376 | } |
| 4377 | |
| 4378 | if (Pieces.empty()) { |
| 4379 | if (PieceIndex == 0) |
| 4380 | return clang_getCursorExtent(C); |
| 4381 | } else if (PieceIndex < Pieces.size()) { |
| 4382 | SourceRange R = Pieces[PieceIndex]; |
| 4383 | if (R.isValid()) |
| 4384 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 4385 | } |
| 4386 | |
| 4387 | return clang_getNullRange(); |
| 4388 | } |
| 4389 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 4390 | void clang_enableStackTraces(void) { |
| 4391 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 4392 | } |
| 4393 | |
Daniel Dunbar | 995aaf9 | 2010-11-04 01:26:29 +0000 | [diff] [blame] | 4394 | void clang_executeOnThread(void (*fn)(void*), void *user_data, |
| 4395 | unsigned stack_size) { |
| 4396 | llvm::llvm_execute_on_thread(fn, user_data, stack_size); |
| 4397 | } |
| 4398 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 4399 | } // end: extern "C" |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4400 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 4401 | //===----------------------------------------------------------------------===// |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4402 | // Token-based Operations. |
| 4403 | //===----------------------------------------------------------------------===// |
| 4404 | |
| 4405 | /* CXToken layout: |
| 4406 | * int_data[0]: a CXTokenKind |
| 4407 | * int_data[1]: starting token location |
| 4408 | * int_data[2]: token length |
| 4409 | * int_data[3]: reserved |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4410 | * ptr_data: for identifiers and keywords, an IdentifierInfo*. |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4411 | * otherwise unused. |
| 4412 | */ |
| 4413 | extern "C" { |
| 4414 | |
| 4415 | CXTokenKind clang_getTokenKind(CXToken CXTok) { |
| 4416 | return static_cast<CXTokenKind>(CXTok.int_data[0]); |
| 4417 | } |
| 4418 | |
| 4419 | CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { |
| 4420 | switch (clang_getTokenKind(CXTok)) { |
| 4421 | case CXToken_Identifier: |
| 4422 | case CXToken_Keyword: |
| 4423 | // We know we have an IdentifierInfo*, so use that. |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 4424 | return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data) |
| 4425 | ->getNameStart()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4426 | |
| 4427 | case CXToken_Literal: { |
| 4428 | // We have stashed the starting pointer in the ptr_data field. Use it. |
| 4429 | const char *Text = static_cast<const char *>(CXTok.ptr_data); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4430 | return createCXString(StringRef(Text, CXTok.int_data[2])); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4431 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4432 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4433 | case CXToken_Punctuation: |
| 4434 | case CXToken_Comment: |
| 4435 | break; |
| 4436 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4437 | |
| 4438 | // We have to find the starting buffer pointer the hard way, by |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4439 | // deconstructing the source location. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4440 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4441 | if (!CXXUnit) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 4442 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4443 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4444 | SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); |
| 4445 | std::pair<FileID, unsigned> LocInfo |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4446 | = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4447 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4448 | StringRef Buffer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4449 | = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); |
| 4450 | if (Invalid) |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4451 | return createCXString(""); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4452 | |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4453 | return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2])); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4454 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4455 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4456 | CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4457 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4458 | if (!CXXUnit) |
| 4459 | return clang_getNullLocation(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4460 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4461 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), |
| 4462 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 4463 | } |
| 4464 | |
| 4465 | CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4466 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 4467 | if (!CXXUnit) |
| 4468 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4469 | |
| 4470 | return cxloc::translateSourceRange(CXXUnit->getASTContext(), |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4471 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 4472 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4473 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4474 | static void getTokens(ASTUnit *CXXUnit, SourceRange Range, |
| 4475 | SmallVectorImpl<CXToken> &CXTokens) { |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4476 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 4477 | std::pair<FileID, unsigned> BeginLocInfo |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4478 | = SourceMgr.getDecomposedLoc(Range.getBegin()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4479 | std::pair<FileID, unsigned> EndLocInfo |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4480 | = SourceMgr.getDecomposedLoc(Range.getEnd()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4481 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4482 | // Cannot tokenize across files. |
| 4483 | if (BeginLocInfo.first != EndLocInfo.first) |
| 4484 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4485 | |
| 4486 | // Create a lexer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4487 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4488 | StringRef Buffer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4489 | = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
Douglas Gregor | 47a3fcd | 2010-03-16 20:26:15 +0000 | [diff] [blame] | 4490 | if (Invalid) |
| 4491 | return; |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4492 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4493 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 4494 | CXXUnit->getASTContext().getLangOptions(), |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4495 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4496 | Lex.SetCommentRetentionState(true); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4497 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4498 | // Lex tokens until we hit the end of the range. |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4499 | const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4500 | Token Tok; |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4501 | bool previousWasAt = false; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4502 | do { |
| 4503 | // Lex the next token |
| 4504 | Lex.LexFromRawLexer(Tok); |
| 4505 | if (Tok.is(tok::eof)) |
| 4506 | break; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4507 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4508 | // Initialize the CXToken. |
| 4509 | CXToken CXTok; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4510 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4511 | // - Common fields |
| 4512 | CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); |
| 4513 | CXTok.int_data[2] = Tok.getLength(); |
| 4514 | CXTok.int_data[3] = 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4515 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4516 | // - Kind-specific fields |
| 4517 | if (Tok.isLiteral()) { |
| 4518 | CXTok.int_data[0] = CXToken_Literal; |
| 4519 | CXTok.ptr_data = (void *)Tok.getLiteralData(); |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4520 | } else if (Tok.is(tok::raw_identifier)) { |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4521 | // Lookup the identifier to determine whether we have a keyword. |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4522 | IdentifierInfo *II |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4523 | = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok); |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4524 | |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4525 | if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4526 | CXTok.int_data[0] = CXToken_Keyword; |
| 4527 | } |
| 4528 | else { |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4529 | CXTok.int_data[0] = Tok.is(tok::identifier) |
| 4530 | ? CXToken_Identifier |
| 4531 | : CXToken_Keyword; |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4532 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4533 | CXTok.ptr_data = II; |
| 4534 | } else if (Tok.is(tok::comment)) { |
| 4535 | CXTok.int_data[0] = CXToken_Comment; |
| 4536 | CXTok.ptr_data = 0; |
| 4537 | } else { |
| 4538 | CXTok.int_data[0] = CXToken_Punctuation; |
| 4539 | CXTok.ptr_data = 0; |
| 4540 | } |
| 4541 | CXTokens.push_back(CXTok); |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4542 | previousWasAt = Tok.is(tok::at); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4543 | } while (Lex.getBufferLocation() <= EffectiveBufferEnd); |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4544 | } |
| 4545 | |
| 4546 | void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, |
| 4547 | CXToken **Tokens, unsigned *NumTokens) { |
| 4548 | if (Tokens) |
| 4549 | *Tokens = 0; |
| 4550 | if (NumTokens) |
| 4551 | *NumTokens = 0; |
| 4552 | |
| 4553 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 4554 | if (!CXXUnit || !Tokens || !NumTokens) |
| 4555 | return; |
| 4556 | |
| 4557 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 4558 | |
| 4559 | SourceRange R = cxloc::translateCXSourceRange(Range); |
| 4560 | if (R.isInvalid()) |
| 4561 | return; |
| 4562 | |
| 4563 | SmallVector<CXToken, 32> CXTokens; |
| 4564 | getTokens(CXXUnit, R, CXTokens); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4565 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4566 | if (CXTokens.empty()) |
| 4567 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4568 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4569 | *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size()); |
| 4570 | memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); |
| 4571 | *NumTokens = CXTokens.size(); |
| 4572 | } |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4573 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4574 | void clang_disposeTokens(CXTranslationUnit TU, |
| 4575 | CXToken *Tokens, unsigned NumTokens) { |
| 4576 | free(Tokens); |
| 4577 | } |
| 4578 | |
| 4579 | } // end: extern "C" |
| 4580 | |
| 4581 | //===----------------------------------------------------------------------===// |
| 4582 | // Token annotation APIs. |
| 4583 | //===----------------------------------------------------------------------===// |
| 4584 | |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4585 | typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4586 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 4587 | CXCursor parent, |
| 4588 | CXClientData client_data); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4589 | namespace { |
| 4590 | class AnnotateTokensWorker { |
| 4591 | AnnotateTokensData &Annotated; |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4592 | CXToken *Tokens; |
| 4593 | CXCursor *Cursors; |
| 4594 | unsigned NumTokens; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4595 | unsigned TokIdx; |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4596 | unsigned PreprocessingTokIdx; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4597 | CursorVisitor AnnotateVis; |
| 4598 | SourceManager &SrcMgr; |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4599 | bool HasContextSensitiveKeywords; |
| 4600 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4601 | bool MoreTokens() const { return TokIdx < NumTokens; } |
| 4602 | unsigned NextToken() const { return TokIdx; } |
| 4603 | void AdvanceToken() { ++TokIdx; } |
| 4604 | SourceLocation GetTokenLoc(unsigned tokI) { |
| 4605 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); |
| 4606 | } |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4607 | bool isFunctionMacroToken(unsigned tokI) const { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4608 | return Tokens[tokI].int_data[3] != 0; |
| 4609 | } |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4610 | SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4611 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[3]); |
| 4612 | } |
| 4613 | |
| 4614 | void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4615 | void annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult, |
| 4616 | SourceRange); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4617 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4618 | public: |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4619 | AnnotateTokensWorker(AnnotateTokensData &annotated, |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4620 | CXToken *tokens, CXCursor *cursors, unsigned numTokens, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4621 | CXTranslationUnit tu, SourceRange RegionOfInterest) |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4622 | : Annotated(annotated), Tokens(tokens), Cursors(cursors), |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4623 | NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4624 | AnnotateVis(tu, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 4625 | AnnotateTokensVisitor, this, |
| 4626 | /*VisitPreprocessorLast=*/true, |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 4627 | /*VisitIncludedEntities=*/false, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 4628 | RegionOfInterest), |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4629 | SrcMgr(static_cast<ASTUnit*>(tu->TUData)->getSourceManager()), |
| 4630 | HasContextSensitiveKeywords(false) { } |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4631 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4632 | void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4633 | enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 4634 | void AnnotateTokens(); |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4635 | |
| 4636 | /// \brief Determine whether the annotator saw any cursors that have |
| 4637 | /// context-sensitive keywords. |
| 4638 | bool hasContextSensitiveKeywords() const { |
| 4639 | return HasContextSensitiveKeywords; |
| 4640 | } |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4641 | }; |
| 4642 | } |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4643 | |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 4644 | void AnnotateTokensWorker::AnnotateTokens() { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4645 | // Walk the AST within the region of interest, annotating tokens |
| 4646 | // along the way. |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 4647 | AnnotateVis.visitFileRegion(); |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4648 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4649 | for (unsigned I = 0 ; I < TokIdx ; ++I) { |
| 4650 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4651 | if (Pos != Annotated.end() && |
| 4652 | (clang_isInvalid(Cursors[I].kind) || |
| 4653 | Pos->second.kind != CXCursor_PreprocessingDirective)) |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4654 | Cursors[I] = Pos->second; |
| 4655 | } |
| 4656 | |
| 4657 | // Finish up annotating any tokens left. |
| 4658 | if (!MoreTokens()) |
| 4659 | return; |
| 4660 | |
| 4661 | const CXCursor &C = clang_getNullCursor(); |
| 4662 | for (unsigned I = TokIdx ; I < NumTokens ; ++I) { |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 4663 | if (I < PreprocessingTokIdx && clang_isPreprocessing(Cursors[I].kind)) |
| 4664 | continue; |
| 4665 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4666 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); |
| 4667 | Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second; |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4668 | } |
| 4669 | } |
| 4670 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4671 | /// \brief It annotates and advances tokens with a cursor until the comparison |
| 4672 | //// between the cursor location and the source range is the same as |
| 4673 | /// \arg compResult. |
| 4674 | /// |
| 4675 | /// Pass RangeBefore to annotate tokens with a cursor until a range is reached. |
| 4676 | /// Pass RangeOverlap to annotate tokens inside a range. |
| 4677 | void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC, |
| 4678 | RangeComparisonResult compResult, |
| 4679 | SourceRange range) { |
| 4680 | while (MoreTokens()) { |
| 4681 | const unsigned I = NextToken(); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4682 | if (isFunctionMacroToken(I)) |
| 4683 | return annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4684 | |
| 4685 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4686 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 4687 | Cursors[I] = updateC; |
| 4688 | AdvanceToken(); |
| 4689 | continue; |
| 4690 | } |
| 4691 | break; |
| 4692 | } |
| 4693 | } |
| 4694 | |
| 4695 | /// \brief Special annotation handling for macro argument tokens. |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4696 | void AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens( |
| 4697 | CXCursor updateC, |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4698 | RangeComparisonResult compResult, |
| 4699 | SourceRange range) { |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4700 | assert(MoreTokens()); |
| 4701 | assert(isFunctionMacroToken(NextToken()) && |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4702 | "Should be called only for macro arg tokens"); |
| 4703 | |
| 4704 | // This works differently than annotateAndAdvanceTokens; because expanded |
| 4705 | // macro arguments can have arbitrary translation-unit source order, we do not |
| 4706 | // advance the token index one by one until a token fails the range test. |
| 4707 | // We only advance once past all of the macro arg tokens if all of them |
| 4708 | // pass the range test. If one of them fails we keep the token index pointing |
| 4709 | // at the start of the macro arg tokens so that the failing token will be |
| 4710 | // annotated by a subsequent annotation try. |
| 4711 | |
| 4712 | bool atLeastOneCompFail = false; |
| 4713 | |
| 4714 | unsigned I = NextToken(); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4715 | for (; I < NumTokens && isFunctionMacroToken(I); ++I) { |
| 4716 | SourceLocation TokLoc = getFunctionMacroTokenLoc(I); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4717 | if (TokLoc.isFileID()) |
| 4718 | continue; // not macro arg token, it's parens or comma. |
| 4719 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 4720 | if (clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 4721 | Cursors[I] = updateC; |
| 4722 | } else |
| 4723 | atLeastOneCompFail = true; |
| 4724 | } |
| 4725 | |
| 4726 | if (!atLeastOneCompFail) |
| 4727 | TokIdx = I; // All of the tokens were handled, advance beyond all of them. |
| 4728 | } |
| 4729 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4730 | enum CXChildVisitResult |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4731 | AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4732 | CXSourceLocation Loc = clang_getCursorLocation(cursor); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4733 | SourceRange cursorRange = getRawCursorExtent(cursor); |
Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 4734 | if (cursorRange.isInvalid()) |
| 4735 | return CXChildVisit_Recurse; |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4736 | |
| 4737 | if (!HasContextSensitiveKeywords) { |
| 4738 | // Objective-C properties can have context-sensitive keywords. |
| 4739 | if (cursor.kind == CXCursor_ObjCPropertyDecl) { |
| 4740 | if (ObjCPropertyDecl *Property |
| 4741 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor))) |
| 4742 | HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0; |
| 4743 | } |
| 4744 | // Objective-C methods can have context-sensitive keywords. |
| 4745 | else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl || |
| 4746 | cursor.kind == CXCursor_ObjCClassMethodDecl) { |
| 4747 | if (ObjCMethodDecl *Method |
| 4748 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 4749 | if (Method->getObjCDeclQualifier()) |
| 4750 | HasContextSensitiveKeywords = true; |
| 4751 | else { |
| 4752 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 4753 | PEnd = Method->param_end(); |
| 4754 | P != PEnd; ++P) { |
| 4755 | if ((*P)->getObjCDeclQualifier()) { |
| 4756 | HasContextSensitiveKeywords = true; |
| 4757 | break; |
| 4758 | } |
| 4759 | } |
| 4760 | } |
| 4761 | } |
| 4762 | } |
| 4763 | // C++ methods can have context-sensitive keywords. |
| 4764 | else if (cursor.kind == CXCursor_CXXMethod) { |
| 4765 | if (CXXMethodDecl *Method |
| 4766 | = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) { |
| 4767 | if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>()) |
| 4768 | HasContextSensitiveKeywords = true; |
| 4769 | } |
| 4770 | } |
| 4771 | // C++ classes can have context-sensitive keywords. |
| 4772 | else if (cursor.kind == CXCursor_StructDecl || |
| 4773 | cursor.kind == CXCursor_ClassDecl || |
| 4774 | cursor.kind == CXCursor_ClassTemplate || |
| 4775 | cursor.kind == CXCursor_ClassTemplatePartialSpecialization) { |
| 4776 | if (Decl *D = getCursorDecl(cursor)) |
| 4777 | if (D->hasAttr<FinalAttr>()) |
| 4778 | HasContextSensitiveKeywords = true; |
| 4779 | } |
| 4780 | } |
| 4781 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4782 | if (clang_isPreprocessing(cursor.kind)) { |
Chandler Carruth | cea731a | 2011-07-14 16:07:57 +0000 | [diff] [blame] | 4783 | // For macro expansions, just note where the beginning of the macro |
| 4784 | // expansion occurs. |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 4785 | if (cursor.kind == CXCursor_MacroExpansion) { |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4786 | Annotated[Loc.int_data] = cursor; |
| 4787 | return CXChildVisit_Recurse; |
| 4788 | } |
| 4789 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4790 | // Items in the preprocessing record are kept separate from items in |
| 4791 | // declarations, so we keep a separate token index. |
| 4792 | unsigned SavedTokIdx = TokIdx; |
| 4793 | TokIdx = PreprocessingTokIdx; |
| 4794 | |
| 4795 | // Skip tokens up until we catch up to the beginning of the preprocessing |
| 4796 | // entry. |
| 4797 | while (MoreTokens()) { |
| 4798 | const unsigned I = NextToken(); |
| 4799 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4800 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 4801 | case RangeBefore: |
| 4802 | AdvanceToken(); |
| 4803 | continue; |
| 4804 | case RangeAfter: |
| 4805 | case RangeOverlap: |
| 4806 | break; |
| 4807 | } |
| 4808 | break; |
| 4809 | } |
| 4810 | |
| 4811 | // Look at all of the tokens within this range. |
| 4812 | while (MoreTokens()) { |
| 4813 | const unsigned I = NextToken(); |
| 4814 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4815 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 4816 | case RangeBefore: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4817 | llvm_unreachable("Infeasible"); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4818 | case RangeAfter: |
| 4819 | break; |
| 4820 | case RangeOverlap: |
| 4821 | Cursors[I] = cursor; |
| 4822 | AdvanceToken(); |
| 4823 | continue; |
| 4824 | } |
| 4825 | break; |
| 4826 | } |
| 4827 | |
| 4828 | // Save the preprocessing token index; restore the non-preprocessing |
| 4829 | // token index. |
| 4830 | PreprocessingTokIdx = TokIdx; |
| 4831 | TokIdx = SavedTokIdx; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4832 | return CXChildVisit_Recurse; |
| 4833 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4834 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4835 | if (cursorRange.isInvalid()) |
| 4836 | return CXChildVisit_Continue; |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4837 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4838 | SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data); |
| 4839 | |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4840 | // Adjust the annotated range based specific declarations. |
| 4841 | const enum CXCursorKind cursorK = clang_getCursorKind(cursor); |
| 4842 | if (cursorK >= CXCursor_FirstDecl && cursorK <= CXCursor_LastDecl) { |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 4843 | Decl *D = cxcursor::getCursorDecl(cursor); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4844 | |
| 4845 | SourceLocation StartLoc; |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 4846 | if (const DeclaratorDecl *DD = dyn_cast_or_null<DeclaratorDecl>(D)) { |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4847 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
| 4848 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 4849 | } else if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(D)) { |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4850 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
| 4851 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4852 | } |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4853 | |
| 4854 | if (StartLoc.isValid() && L.isValid() && |
| 4855 | SrcMgr.isBeforeInTranslationUnit(StartLoc, L)) |
| 4856 | cursorRange.setBegin(StartLoc); |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4857 | } |
Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 4858 | |
Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 4859 | // If the location of the cursor occurs within a macro instantiation, record |
| 4860 | // the spelling location of the cursor in our annotation map. We can then |
| 4861 | // paper over the token labelings during a post-processing step to try and |
| 4862 | // get cursor mappings for tokens that are the *arguments* of a macro |
| 4863 | // instantiation. |
| 4864 | if (L.isMacroID()) { |
| 4865 | unsigned rawEncoding = SrcMgr.getSpellingLoc(L).getRawEncoding(); |
| 4866 | // Only invalidate the old annotation if it isn't part of a preprocessing |
| 4867 | // directive. Here we assume that the default construction of CXCursor |
| 4868 | // results in CXCursor.kind being an initialized value (i.e., 0). If |
| 4869 | // this isn't the case, we can fix by doing lookup + insertion. |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4870 | |
Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 4871 | CXCursor &oldC = Annotated[rawEncoding]; |
| 4872 | if (!clang_isPreprocessing(oldC.kind)) |
| 4873 | oldC = cursor; |
| 4874 | } |
| 4875 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4876 | const enum CXCursorKind K = clang_getCursorKind(parent); |
| 4877 | const CXCursor updateC = |
Ted Kremenek | d8b0a84 | 2010-08-25 22:16:02 +0000 | [diff] [blame] | 4878 | (clang_isInvalid(K) || K == CXCursor_TranslationUnit) |
| 4879 | ? clang_getNullCursor() : parent; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4880 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4881 | annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4882 | |
Argyrios Kyrtzidis | 5517b89 | 2011-06-27 19:42:20 +0000 | [diff] [blame] | 4883 | // Avoid having the cursor of an expression "overwrite" the annotation of the |
| 4884 | // variable declaration that it belongs to. |
| 4885 | // This can happen for C++ constructor expressions whose range generally |
| 4886 | // include the variable declaration, e.g.: |
| 4887 | // MyCXXClass foo; // Make sure we don't annotate 'foo' as a CallExpr cursor. |
| 4888 | if (clang_isExpression(cursorK)) { |
| 4889 | Expr *E = getCursorExpr(cursor); |
Argyrios Kyrtzidis | 8ccac3d | 2011-06-29 22:20:07 +0000 | [diff] [blame] | 4890 | if (Decl *D = getCursorParentDecl(cursor)) { |
Argyrios Kyrtzidis | 5517b89 | 2011-06-27 19:42:20 +0000 | [diff] [blame] | 4891 | const unsigned I = NextToken(); |
| 4892 | if (E->getLocStart().isValid() && D->getLocation().isValid() && |
| 4893 | E->getLocStart() == D->getLocation() && |
| 4894 | E->getLocStart() == GetTokenLoc(I)) { |
| 4895 | Cursors[I] = updateC; |
| 4896 | AdvanceToken(); |
| 4897 | } |
| 4898 | } |
| 4899 | } |
| 4900 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4901 | // Visit children to get their cursor information. |
| 4902 | const unsigned BeforeChildren = NextToken(); |
| 4903 | VisitChildren(cursor); |
| 4904 | const unsigned AfterChildren = NextToken(); |
| 4905 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4906 | // Scan the tokens that are at the end of the cursor, but are not captured |
| 4907 | // but the child cursors. |
| 4908 | annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4909 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4910 | // Scan the tokens that are at the beginning of the cursor, but are not |
| 4911 | // capture by the child cursors. |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4912 | for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { |
| 4913 | if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 4914 | break; |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4915 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4916 | Cursors[I] = cursor; |
| 4917 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4918 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4919 | return CXChildVisit_Continue; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4920 | } |
| 4921 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4922 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 4923 | CXCursor parent, |
| 4924 | CXClientData client_data) { |
| 4925 | return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); |
| 4926 | } |
| 4927 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4928 | namespace { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4929 | |
| 4930 | /// \brief Uses the macro expansions in the preprocessing record to find |
| 4931 | /// and mark tokens that are macro arguments. This info is used by the |
| 4932 | /// AnnotateTokensWorker. |
| 4933 | class MarkMacroArgTokensVisitor { |
| 4934 | SourceManager &SM; |
| 4935 | CXToken *Tokens; |
| 4936 | unsigned NumTokens; |
| 4937 | unsigned CurIdx; |
| 4938 | |
| 4939 | public: |
| 4940 | MarkMacroArgTokensVisitor(SourceManager &SM, |
| 4941 | CXToken *tokens, unsigned numTokens) |
| 4942 | : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { } |
| 4943 | |
| 4944 | CXChildVisitResult visit(CXCursor cursor, CXCursor parent) { |
| 4945 | if (cursor.kind != CXCursor_MacroExpansion) |
| 4946 | return CXChildVisit_Continue; |
| 4947 | |
| 4948 | SourceRange macroRange = getCursorMacroExpansion(cursor)->getSourceRange(); |
| 4949 | if (macroRange.getBegin() == macroRange.getEnd()) |
| 4950 | return CXChildVisit_Continue; // it's not a function macro. |
| 4951 | |
| 4952 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 4953 | if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx), |
| 4954 | macroRange.getBegin())) |
| 4955 | break; |
| 4956 | } |
| 4957 | |
| 4958 | if (CurIdx == NumTokens) |
| 4959 | return CXChildVisit_Break; |
| 4960 | |
| 4961 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 4962 | SourceLocation tokLoc = getTokenLoc(CurIdx); |
| 4963 | if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd())) |
| 4964 | break; |
| 4965 | |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4966 | setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc)); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4967 | } |
| 4968 | |
| 4969 | if (CurIdx == NumTokens) |
| 4970 | return CXChildVisit_Break; |
| 4971 | |
| 4972 | return CXChildVisit_Continue; |
| 4973 | } |
| 4974 | |
| 4975 | private: |
| 4976 | SourceLocation getTokenLoc(unsigned tokI) { |
| 4977 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); |
| 4978 | } |
| 4979 | |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4980 | void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4981 | // The third field is reserved and currently not used. Use it here |
| 4982 | // to mark macro arg expanded tokens with their expanded locations. |
| 4983 | Tokens[tokI].int_data[3] = loc.getRawEncoding(); |
| 4984 | } |
| 4985 | }; |
| 4986 | |
| 4987 | } // end anonymous namespace |
| 4988 | |
| 4989 | static CXChildVisitResult |
| 4990 | MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent, |
| 4991 | CXClientData client_data) { |
| 4992 | return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor, |
| 4993 | parent); |
| 4994 | } |
| 4995 | |
| 4996 | namespace { |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4997 | struct clang_annotateTokens_Data { |
| 4998 | CXTranslationUnit TU; |
| 4999 | ASTUnit *CXXUnit; |
| 5000 | CXToken *Tokens; |
| 5001 | unsigned NumTokens; |
| 5002 | CXCursor *Cursors; |
| 5003 | }; |
| 5004 | } |
| 5005 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 5006 | static void annotatePreprocessorTokens(CXTranslationUnit TU, |
| 5007 | SourceRange RegionOfInterest, |
| 5008 | AnnotateTokensData &Annotated) { |
| 5009 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 5010 | |
| 5011 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 5012 | std::pair<FileID, unsigned> BeginLocInfo |
| 5013 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin()); |
| 5014 | std::pair<FileID, unsigned> EndLocInfo |
| 5015 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd()); |
| 5016 | |
| 5017 | if (BeginLocInfo.first != EndLocInfo.first) |
| 5018 | return; |
| 5019 | |
| 5020 | StringRef Buffer; |
| 5021 | bool Invalid = false; |
| 5022 | Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 5023 | if (Buffer.empty() || Invalid) |
| 5024 | return; |
| 5025 | |
| 5026 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 5027 | CXXUnit->getASTContext().getLangOptions(), |
| 5028 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, |
| 5029 | Buffer.end()); |
| 5030 | Lex.SetCommentRetentionState(true); |
| 5031 | |
| 5032 | // Lex tokens in raw mode until we hit the end of the range, to avoid |
| 5033 | // entering #includes or expanding macros. |
| 5034 | while (true) { |
| 5035 | Token Tok; |
| 5036 | Lex.LexFromRawLexer(Tok); |
| 5037 | |
| 5038 | reprocess: |
| 5039 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
| 5040 | // We have found a preprocessing directive. Gobble it up so that we |
| 5041 | // don't see it while preprocessing these tokens later, but keep track |
| 5042 | // of all of the token locations inside this preprocessing directive so |
| 5043 | // that we can annotate them appropriately. |
| 5044 | // |
| 5045 | // FIXME: Some simple tests here could identify macro definitions and |
| 5046 | // #undefs, to provide specific cursor kinds for those. |
| 5047 | SmallVector<SourceLocation, 32> Locations; |
| 5048 | do { |
| 5049 | Locations.push_back(Tok.getLocation()); |
| 5050 | Lex.LexFromRawLexer(Tok); |
| 5051 | } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof)); |
| 5052 | |
| 5053 | using namespace cxcursor; |
| 5054 | CXCursor Cursor |
| 5055 | = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(), |
| 5056 | Locations.back()), |
| 5057 | TU); |
| 5058 | for (unsigned I = 0, N = Locations.size(); I != N; ++I) { |
| 5059 | Annotated[Locations[I].getRawEncoding()] = Cursor; |
| 5060 | } |
| 5061 | |
| 5062 | if (Tok.isAtStartOfLine()) |
| 5063 | goto reprocess; |
| 5064 | |
| 5065 | continue; |
| 5066 | } |
| 5067 | |
| 5068 | if (Tok.is(tok::eof)) |
| 5069 | break; |
| 5070 | } |
| 5071 | } |
| 5072 | |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5073 | // This gets run a separate thread to avoid stack blowout. |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5074 | static void clang_annotateTokensImpl(void *UserData) { |
| 5075 | CXTranslationUnit TU = ((clang_annotateTokens_Data*)UserData)->TU; |
| 5076 | ASTUnit *CXXUnit = ((clang_annotateTokens_Data*)UserData)->CXXUnit; |
| 5077 | CXToken *Tokens = ((clang_annotateTokens_Data*)UserData)->Tokens; |
| 5078 | const unsigned NumTokens = ((clang_annotateTokens_Data*)UserData)->NumTokens; |
| 5079 | CXCursor *Cursors = ((clang_annotateTokens_Data*)UserData)->Cursors; |
| 5080 | |
| 5081 | // Determine the region of interest, which contains all of the tokens. |
| 5082 | SourceRange RegionOfInterest; |
| 5083 | RegionOfInterest.setBegin( |
| 5084 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0]))); |
| 5085 | RegionOfInterest.setEnd( |
| 5086 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, |
| 5087 | Tokens[NumTokens-1]))); |
| 5088 | |
| 5089 | // A mapping from the source locations found when re-lexing or traversing the |
| 5090 | // region of interest to the corresponding cursors. |
| 5091 | AnnotateTokensData Annotated; |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 5092 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5093 | // Relex the tokens within the source range to look for preprocessing |
| 5094 | // directives. |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 5095 | annotatePreprocessorTokens(TU, RegionOfInterest, Annotated); |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5096 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5097 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) { |
| 5098 | // Search and mark tokens that are macro argument expansions. |
| 5099 | MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(), |
| 5100 | Tokens, NumTokens); |
| 5101 | CursorVisitor MacroArgMarker(TU, |
| 5102 | MarkMacroArgTokensVisitorDelegate, &Visitor, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 5103 | /*VisitPreprocessorLast=*/true, |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 5104 | /*VisitIncludedEntities=*/false, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 5105 | RegionOfInterest); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5106 | MacroArgMarker.visitPreprocessedEntitiesInRegion(); |
| 5107 | } |
| 5108 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5109 | // Annotate all of the source locations in the region of interest that map to |
| 5110 | // a specific cursor. |
| 5111 | AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens, |
| 5112 | TU, RegionOfInterest); |
| 5113 | |
| 5114 | // FIXME: We use a ridiculous stack size here because the data-recursion |
| 5115 | // algorithm uses a large stack frame than the non-data recursive version, |
| 5116 | // and AnnotationTokensWorker currently transforms the data-recursion |
| 5117 | // algorithm back into a traditional recursion by explicitly calling |
| 5118 | // VisitChildren(). We will need to remove this explicit recursive call. |
| 5119 | W.AnnotateTokens(); |
| 5120 | |
| 5121 | // If we ran into any entities that involve context-sensitive keywords, |
| 5122 | // take another pass through the tokens to mark them as such. |
| 5123 | if (W.hasContextSensitiveKeywords()) { |
| 5124 | for (unsigned I = 0; I != NumTokens; ++I) { |
| 5125 | if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier) |
| 5126 | continue; |
| 5127 | |
| 5128 | if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) { |
| 5129 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 5130 | if (ObjCPropertyDecl *Property |
| 5131 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) { |
| 5132 | if (Property->getPropertyAttributesAsWritten() != 0 && |
| 5133 | llvm::StringSwitch<bool>(II->getName()) |
| 5134 | .Case("readonly", true) |
| 5135 | .Case("assign", true) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5136 | .Case("unsafe_unretained", true) |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5137 | .Case("readwrite", true) |
| 5138 | .Case("retain", true) |
| 5139 | .Case("copy", true) |
| 5140 | .Case("nonatomic", true) |
| 5141 | .Case("atomic", true) |
| 5142 | .Case("getter", true) |
| 5143 | .Case("setter", true) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5144 | .Case("strong", true) |
| 5145 | .Case("weak", true) |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5146 | .Default(false)) |
| 5147 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5148 | } |
| 5149 | continue; |
| 5150 | } |
| 5151 | |
| 5152 | if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl || |
| 5153 | Cursors[I].kind == CXCursor_ObjCClassMethodDecl) { |
| 5154 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 5155 | if (llvm::StringSwitch<bool>(II->getName()) |
| 5156 | .Case("in", true) |
| 5157 | .Case("out", true) |
| 5158 | .Case("inout", true) |
| 5159 | .Case("oneway", true) |
| 5160 | .Case("bycopy", true) |
| 5161 | .Case("byref", true) |
| 5162 | .Default(false)) |
| 5163 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5164 | continue; |
| 5165 | } |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 5166 | |
| 5167 | if (Cursors[I].kind == CXCursor_CXXFinalAttr || |
| 5168 | Cursors[I].kind == CXCursor_CXXOverrideAttr) { |
| 5169 | Tokens[I].int_data[0] = CXToken_Keyword; |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5170 | continue; |
| 5171 | } |
| 5172 | } |
| 5173 | } |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5174 | } |
| 5175 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 5176 | extern "C" { |
| 5177 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5178 | void clang_annotateTokens(CXTranslationUnit TU, |
| 5179 | CXToken *Tokens, unsigned NumTokens, |
| 5180 | CXCursor *Cursors) { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5181 | |
| 5182 | if (NumTokens == 0 || !Tokens || !Cursors) |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5183 | return; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5184 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5185 | // Any token we don't specifically annotate will have a NULL cursor. |
| 5186 | CXCursor C = clang_getNullCursor(); |
| 5187 | for (unsigned I = 0; I != NumTokens; ++I) |
| 5188 | Cursors[I] = C; |
| 5189 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5190 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5191 | if (!CXXUnit) |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5192 | return; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5193 | |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 5194 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5195 | |
| 5196 | clang_annotateTokens_Data data = { TU, CXXUnit, Tokens, NumTokens, Cursors }; |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5197 | llvm::CrashRecoveryContext CRC; |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5198 | if (!RunSafely(CRC, clang_annotateTokensImpl, &data, |
Ted Kremenek | 6c53fdd | 2010-11-14 17:47:35 +0000 | [diff] [blame] | 5199 | GetSafetyThreadStackSize() * 2)) { |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5200 | fprintf(stderr, "libclang: crash detected while annotating tokens\n"); |
| 5201 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5202 | } |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5203 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5204 | } // end: extern "C" |
| 5205 | |
| 5206 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 5207 | // Operations for querying linkage of a cursor. |
| 5208 | //===----------------------------------------------------------------------===// |
| 5209 | |
| 5210 | extern "C" { |
| 5211 | CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 5212 | if (!clang_isDeclaration(cursor.kind)) |
| 5213 | return CXLinkage_Invalid; |
| 5214 | |
Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 5215 | Decl *D = cxcursor::getCursorDecl(cursor); |
| 5216 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
| 5217 | switch (ND->getLinkage()) { |
| 5218 | case NoLinkage: return CXLinkage_NoLinkage; |
| 5219 | case InternalLinkage: return CXLinkage_Internal; |
| 5220 | case UniqueExternalLinkage: return CXLinkage_UniqueExternal; |
| 5221 | case ExternalLinkage: return CXLinkage_External; |
| 5222 | }; |
| 5223 | |
| 5224 | return CXLinkage_Invalid; |
| 5225 | } |
| 5226 | } // end: extern "C" |
| 5227 | |
| 5228 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5229 | // Operations for querying language of a cursor. |
| 5230 | //===----------------------------------------------------------------------===// |
| 5231 | |
| 5232 | static CXLanguageKind getDeclLanguage(const Decl *D) { |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 5233 | if (!D) |
| 5234 | return CXLanguage_C; |
| 5235 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5236 | switch (D->getKind()) { |
| 5237 | default: |
| 5238 | break; |
| 5239 | case Decl::ImplicitParam: |
| 5240 | case Decl::ObjCAtDefsField: |
| 5241 | case Decl::ObjCCategory: |
| 5242 | case Decl::ObjCCategoryImpl: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5243 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5244 | case Decl::ObjCImplementation: |
| 5245 | case Decl::ObjCInterface: |
| 5246 | case Decl::ObjCIvar: |
| 5247 | case Decl::ObjCMethod: |
| 5248 | case Decl::ObjCProperty: |
| 5249 | case Decl::ObjCPropertyImpl: |
| 5250 | case Decl::ObjCProtocol: |
| 5251 | return CXLanguage_ObjC; |
| 5252 | case Decl::CXXConstructor: |
| 5253 | case Decl::CXXConversion: |
| 5254 | case Decl::CXXDestructor: |
| 5255 | case Decl::CXXMethod: |
| 5256 | case Decl::CXXRecord: |
| 5257 | case Decl::ClassTemplate: |
| 5258 | case Decl::ClassTemplatePartialSpecialization: |
| 5259 | case Decl::ClassTemplateSpecialization: |
| 5260 | case Decl::Friend: |
| 5261 | case Decl::FriendTemplate: |
| 5262 | case Decl::FunctionTemplate: |
| 5263 | case Decl::LinkageSpec: |
| 5264 | case Decl::Namespace: |
| 5265 | case Decl::NamespaceAlias: |
| 5266 | case Decl::NonTypeTemplateParm: |
| 5267 | case Decl::StaticAssert: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5268 | case Decl::TemplateTemplateParm: |
| 5269 | case Decl::TemplateTypeParm: |
| 5270 | case Decl::UnresolvedUsingTypename: |
| 5271 | case Decl::UnresolvedUsingValue: |
| 5272 | case Decl::Using: |
| 5273 | case Decl::UsingDirective: |
| 5274 | case Decl::UsingShadow: |
| 5275 | return CXLanguage_CPlusPlus; |
| 5276 | } |
| 5277 | |
| 5278 | return CXLanguage_C; |
| 5279 | } |
| 5280 | |
| 5281 | extern "C" { |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5282 | |
| 5283 | enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { |
| 5284 | if (clang_isDeclaration(cursor.kind)) |
| 5285 | if (Decl *D = cxcursor::getCursorDecl(cursor)) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5286 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()) |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5287 | return CXAvailability_Available; |
| 5288 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5289 | switch (D->getAvailability()) { |
| 5290 | case AR_Available: |
| 5291 | case AR_NotYetIntroduced: |
| 5292 | return CXAvailability_Available; |
| 5293 | |
| 5294 | case AR_Deprecated: |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5295 | return CXAvailability_Deprecated; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5296 | |
| 5297 | case AR_Unavailable: |
| 5298 | return CXAvailability_NotAvailable; |
| 5299 | } |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5300 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5301 | |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5302 | return CXAvailability_Available; |
| 5303 | } |
| 5304 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5305 | CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { |
| 5306 | if (clang_isDeclaration(cursor.kind)) |
| 5307 | return getDeclLanguage(cxcursor::getCursorDecl(cursor)); |
| 5308 | |
| 5309 | return CXLanguage_Invalid; |
| 5310 | } |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5311 | |
| 5312 | /// \brief If the given cursor is the "templated" declaration |
| 5313 | /// descibing a class or function template, return the class or |
| 5314 | /// function template. |
| 5315 | static Decl *maybeGetTemplateCursor(Decl *D) { |
| 5316 | if (!D) |
| 5317 | return 0; |
| 5318 | |
| 5319 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5320 | if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate()) |
| 5321 | return FunTmpl; |
| 5322 | |
| 5323 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
| 5324 | if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate()) |
| 5325 | return ClassTmpl; |
| 5326 | |
| 5327 | return D; |
| 5328 | } |
| 5329 | |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5330 | CXCursor clang_getCursorSemanticParent(CXCursor cursor) { |
| 5331 | if (clang_isDeclaration(cursor.kind)) { |
| 5332 | if (Decl *D = getCursorDecl(cursor)) { |
| 5333 | DeclContext *DC = D->getDeclContext(); |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5334 | if (!DC) |
| 5335 | return clang_getNullCursor(); |
| 5336 | |
| 5337 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 5338 | getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5339 | } |
| 5340 | } |
| 5341 | |
| 5342 | if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { |
| 5343 | if (Decl *D = getCursorDecl(cursor)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5344 | return MakeCXCursor(D, getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5345 | } |
| 5346 | |
| 5347 | return clang_getNullCursor(); |
| 5348 | } |
| 5349 | |
| 5350 | CXCursor clang_getCursorLexicalParent(CXCursor cursor) { |
| 5351 | if (clang_isDeclaration(cursor.kind)) { |
| 5352 | if (Decl *D = getCursorDecl(cursor)) { |
| 5353 | DeclContext *DC = D->getLexicalDeclContext(); |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5354 | if (!DC) |
| 5355 | return clang_getNullCursor(); |
| 5356 | |
| 5357 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 5358 | getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5359 | } |
| 5360 | } |
| 5361 | |
| 5362 | // FIXME: Note that we can't easily compute the lexical context of a |
| 5363 | // statement or expression, so we return nothing. |
| 5364 | return clang_getNullCursor(); |
| 5365 | } |
| 5366 | |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5367 | void clang_getOverriddenCursors(CXCursor cursor, |
| 5368 | CXCursor **overridden, |
| 5369 | unsigned *num_overridden) { |
| 5370 | if (overridden) |
| 5371 | *overridden = 0; |
| 5372 | if (num_overridden) |
| 5373 | *num_overridden = 0; |
| 5374 | if (!overridden || !num_overridden) |
| 5375 | return; |
| 5376 | |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 5377 | SmallVector<CXCursor, 8> Overridden; |
| 5378 | cxcursor::getOverriddenCursors(cursor, Overridden); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5379 | |
Ted Kremenek | 2407712 | 2011-11-14 23:51:37 +0000 | [diff] [blame] | 5380 | // Don't allocate memory if we have no overriden cursors. |
| 5381 | if (Overridden.size() == 0) |
| 5382 | return; |
| 5383 | |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 5384 | *num_overridden = Overridden.size(); |
| 5385 | *overridden = new CXCursor [Overridden.size()]; |
| 5386 | std::copy(Overridden.begin(), Overridden.end(), *overridden); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5387 | } |
| 5388 | |
| 5389 | void clang_disposeOverriddenCursors(CXCursor *overridden) { |
| 5390 | delete [] overridden; |
| 5391 | } |
| 5392 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 5393 | CXFile clang_getIncludedFile(CXCursor cursor) { |
| 5394 | if (cursor.kind != CXCursor_InclusionDirective) |
| 5395 | return 0; |
| 5396 | |
| 5397 | InclusionDirective *ID = getCursorInclusionDirective(cursor); |
| 5398 | return (void *)ID->getFile(); |
| 5399 | } |
| 5400 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5401 | } // end: extern "C" |
| 5402 | |
Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 5403 | |
| 5404 | //===----------------------------------------------------------------------===// |
| 5405 | // C++ AST instrospection. |
| 5406 | //===----------------------------------------------------------------------===// |
| 5407 | |
| 5408 | extern "C" { |
| 5409 | unsigned clang_CXXMethod_isStatic(CXCursor C) { |
| 5410 | if (!clang_isDeclaration(C.kind)) |
| 5411 | return 0; |
Douglas Gregor | 49f6f54 | 2010-08-31 22:12:17 +0000 | [diff] [blame] | 5412 | |
| 5413 | CXXMethodDecl *Method = 0; |
| 5414 | Decl *D = cxcursor::getCursorDecl(C); |
| 5415 | if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 5416 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 5417 | else |
| 5418 | Method = dyn_cast_or_null<CXXMethodDecl>(D); |
| 5419 | return (Method && Method->isStatic()) ? 1 : 0; |
Ted Kremenek | 40b492a | 2010-05-17 20:12:45 +0000 | [diff] [blame] | 5420 | } |
Ted Kremenek | b12903e | 2010-05-18 22:32:15 +0000 | [diff] [blame] | 5421 | |
Douglas Gregor | 211924b | 2011-05-12 15:17:24 +0000 | [diff] [blame] | 5422 | unsigned clang_CXXMethod_isVirtual(CXCursor C) { |
| 5423 | if (!clang_isDeclaration(C.kind)) |
| 5424 | return 0; |
| 5425 | |
| 5426 | CXXMethodDecl *Method = 0; |
| 5427 | Decl *D = cxcursor::getCursorDecl(C); |
| 5428 | if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 5429 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 5430 | else |
| 5431 | Method = dyn_cast_or_null<CXXMethodDecl>(D); |
| 5432 | return (Method && Method->isVirtual()) ? 1 : 0; |
| 5433 | } |
Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 5434 | } // end: extern "C" |
| 5435 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5436 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5437 | // Attribute introspection. |
| 5438 | //===----------------------------------------------------------------------===// |
| 5439 | |
| 5440 | extern "C" { |
| 5441 | CXType clang_getIBOutletCollectionType(CXCursor C) { |
| 5442 | if (C.kind != CXCursor_IBOutletCollectionAttr) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5443 | return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C)); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5444 | |
| 5445 | IBOutletCollectionAttr *A = |
| 5446 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); |
| 5447 | |
Argyrios Kyrtzidis | 18aa2ff | 2011-09-13 18:49:52 +0000 | [diff] [blame] | 5448 | return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C)); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5449 | } |
| 5450 | } // end: extern "C" |
| 5451 | |
| 5452 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5453 | // Inspecting memory usage. |
| 5454 | //===----------------------------------------------------------------------===// |
| 5455 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5456 | typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5457 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5458 | static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries, |
| 5459 | enum CXTUResourceUsageKind k, |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5460 | unsigned long amount) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5461 | CXTUResourceUsageEntry entry = { k, amount }; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5462 | entries.push_back(entry); |
| 5463 | } |
| 5464 | |
| 5465 | extern "C" { |
| 5466 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5467 | const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5468 | const char *str = ""; |
| 5469 | switch (kind) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5470 | case CXTUResourceUsage_AST: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5471 | str = "ASTContext: expressions, declarations, and types"; |
| 5472 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5473 | case CXTUResourceUsage_Identifiers: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5474 | str = "ASTContext: identifiers"; |
| 5475 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5476 | case CXTUResourceUsage_Selectors: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5477 | str = "ASTContext: selectors"; |
Ted Kremenek | e294ab7 | 2011-04-19 04:36:17 +0000 | [diff] [blame] | 5478 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5479 | case CXTUResourceUsage_GlobalCompletionResults: |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5480 | str = "Code completion: cached global results"; |
Ted Kremenek | e294ab7 | 2011-04-19 04:36:17 +0000 | [diff] [blame] | 5481 | break; |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 5482 | case CXTUResourceUsage_SourceManagerContentCache: |
| 5483 | str = "SourceManager: content cache allocator"; |
| 5484 | break; |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5485 | case CXTUResourceUsage_AST_SideTables: |
| 5486 | str = "ASTContext: side tables"; |
| 5487 | break; |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5488 | case CXTUResourceUsage_SourceManager_Membuffer_Malloc: |
| 5489 | str = "SourceManager: malloc'ed memory buffers"; |
| 5490 | break; |
| 5491 | case CXTUResourceUsage_SourceManager_Membuffer_MMap: |
| 5492 | str = "SourceManager: mmap'ed memory buffers"; |
| 5493 | break; |
Ted Kremenek | e9b5f3d | 2011-04-28 23:46:20 +0000 | [diff] [blame] | 5494 | case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc: |
| 5495 | str = "ExternalASTSource: malloc'ed memory buffers"; |
| 5496 | break; |
| 5497 | case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap: |
| 5498 | str = "ExternalASTSource: mmap'ed memory buffers"; |
| 5499 | break; |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5500 | case CXTUResourceUsage_Preprocessor: |
| 5501 | str = "Preprocessor: malloc'ed memory"; |
| 5502 | break; |
| 5503 | case CXTUResourceUsage_PreprocessingRecord: |
| 5504 | str = "Preprocessor: PreprocessingRecord"; |
| 5505 | break; |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5506 | case CXTUResourceUsage_SourceManager_DataStructures: |
| 5507 | str = "SourceManager: data structures and tables"; |
| 5508 | break; |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 5509 | case CXTUResourceUsage_Preprocessor_HeaderSearch: |
| 5510 | str = "Preprocessor: header search tables"; |
| 5511 | break; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5512 | } |
| 5513 | return str; |
| 5514 | } |
| 5515 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5516 | CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5517 | if (!TU) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5518 | CXTUResourceUsage usage = { (void*) 0, 0, 0 }; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5519 | return usage; |
| 5520 | } |
| 5521 | |
| 5522 | ASTUnit *astUnit = static_cast<ASTUnit*>(TU->TUData); |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 5523 | OwningPtr<MemUsageEntries> entries(new MemUsageEntries()); |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5524 | ASTContext &astContext = astUnit->getASTContext(); |
| 5525 | |
| 5526 | // How much memory is used by AST nodes and types? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5527 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST, |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5528 | (unsigned long) astContext.getASTAllocatedMemory()); |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5529 | |
| 5530 | // How much memory is used by identifiers? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5531 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5532 | (unsigned long) astContext.Idents.getAllocator().getTotalMemory()); |
| 5533 | |
| 5534 | // How much memory is used for selectors? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5535 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5536 | (unsigned long) astContext.Selectors.getTotalMemory()); |
| 5537 | |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5538 | // How much memory is used by ASTContext's side tables? |
| 5539 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables, |
| 5540 | (unsigned long) astContext.getSideTableAllocatedMemory()); |
| 5541 | |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5542 | // How much memory is used for caching global code completion results? |
| 5543 | unsigned long completionBytes = 0; |
| 5544 | if (GlobalCodeCompletionAllocator *completionAllocator = |
| 5545 | astUnit->getCachedCompletionAllocator().getPtr()) { |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5546 | completionBytes = completionAllocator->getTotalMemory(); |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5547 | } |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 5548 | createCXTUResourceUsageEntry(*entries, |
| 5549 | CXTUResourceUsage_GlobalCompletionResults, |
| 5550 | completionBytes); |
| 5551 | |
| 5552 | // How much memory is being used by SourceManager's content cache? |
| 5553 | createCXTUResourceUsageEntry(*entries, |
| 5554 | CXTUResourceUsage_SourceManagerContentCache, |
| 5555 | (unsigned long) astContext.getSourceManager().getContentCacheSize()); |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5556 | |
| 5557 | // How much memory is being used by the MemoryBuffer's in SourceManager? |
| 5558 | const SourceManager::MemoryBufferSizes &srcBufs = |
| 5559 | astUnit->getSourceManager().getMemoryBufferSizes(); |
| 5560 | |
| 5561 | createCXTUResourceUsageEntry(*entries, |
| 5562 | CXTUResourceUsage_SourceManager_Membuffer_Malloc, |
| 5563 | (unsigned long) srcBufs.malloc_bytes); |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5564 | createCXTUResourceUsageEntry(*entries, |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5565 | CXTUResourceUsage_SourceManager_Membuffer_MMap, |
| 5566 | (unsigned long) srcBufs.mmap_bytes); |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5567 | createCXTUResourceUsageEntry(*entries, |
| 5568 | CXTUResourceUsage_SourceManager_DataStructures, |
| 5569 | (unsigned long) astContext.getSourceManager() |
| 5570 | .getDataStructureSizes()); |
Ted Kremenek | e9b5f3d | 2011-04-28 23:46:20 +0000 | [diff] [blame] | 5571 | |
| 5572 | // How much memory is being used by the ExternalASTSource? |
| 5573 | if (ExternalASTSource *esrc = astContext.getExternalSource()) { |
| 5574 | const ExternalASTSource::MemoryBufferSizes &sizes = |
| 5575 | esrc->getMemoryBufferSizes(); |
| 5576 | |
| 5577 | createCXTUResourceUsageEntry(*entries, |
| 5578 | CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc, |
| 5579 | (unsigned long) sizes.malloc_bytes); |
| 5580 | createCXTUResourceUsageEntry(*entries, |
| 5581 | CXTUResourceUsage_ExternalASTSource_Membuffer_MMap, |
| 5582 | (unsigned long) sizes.mmap_bytes); |
| 5583 | } |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5584 | |
| 5585 | // How much memory is being used by the Preprocessor? |
| 5586 | Preprocessor &pp = astUnit->getPreprocessor(); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5587 | createCXTUResourceUsageEntry(*entries, |
| 5588 | CXTUResourceUsage_Preprocessor, |
Argyrios Kyrtzidis | c5c5e92 | 2011-06-29 22:20:04 +0000 | [diff] [blame] | 5589 | pp.getTotalMemory()); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5590 | |
| 5591 | if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) { |
| 5592 | createCXTUResourceUsageEntry(*entries, |
| 5593 | CXTUResourceUsage_PreprocessingRecord, |
| 5594 | pRec->getTotalMemory()); |
| 5595 | } |
| 5596 | |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 5597 | createCXTUResourceUsageEntry(*entries, |
| 5598 | CXTUResourceUsage_Preprocessor_HeaderSearch, |
| 5599 | pp.getHeaderSearchInfo().getTotalMemory()); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5600 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5601 | CXTUResourceUsage usage = { (void*) entries.get(), |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5602 | (unsigned) entries->size(), |
| 5603 | entries->size() ? &(*entries)[0] : 0 }; |
| 5604 | entries.take(); |
| 5605 | return usage; |
| 5606 | } |
| 5607 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5608 | void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5609 | if (usage.data) |
| 5610 | delete (MemUsageEntries*) usage.data; |
| 5611 | } |
| 5612 | |
| 5613 | } // end extern "C" |
| 5614 | |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 5615 | void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) { |
| 5616 | CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU); |
| 5617 | for (unsigned I = 0; I != Usage.numEntries; ++I) |
| 5618 | fprintf(stderr, " %s: %lu\n", |
| 5619 | clang_getTUResourceUsageName(Usage.entries[I].kind), |
| 5620 | Usage.entries[I].amount); |
| 5621 | |
| 5622 | clang_disposeCXTUResourceUsage(Usage); |
| 5623 | } |
| 5624 | |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5625 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5626 | // Misc. utility functions. |
| 5627 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 5628 | |
Daniel Dunbar | abdce7a | 2010-11-05 17:21:46 +0000 | [diff] [blame] | 5629 | /// Default to using an 8 MB stack size on "safety" threads. |
| 5630 | static unsigned SafetyStackThreadSize = 8 << 20; |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 5631 | |
| 5632 | namespace clang { |
| 5633 | |
| 5634 | bool RunSafely(llvm::CrashRecoveryContext &CRC, |
Ted Kremenek | 6c53fdd | 2010-11-14 17:47:35 +0000 | [diff] [blame] | 5635 | void (*Fn)(void*), void *UserData, |
| 5636 | unsigned Size) { |
| 5637 | if (!Size) |
| 5638 | Size = GetSafetyThreadStackSize(); |
| 5639 | if (Size) |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 5640 | return CRC.RunSafelyOnThread(Fn, UserData, Size); |
| 5641 | return CRC.RunSafely(Fn, UserData); |
| 5642 | } |
| 5643 | |
| 5644 | unsigned GetSafetyThreadStackSize() { |
| 5645 | return SafetyStackThreadSize; |
| 5646 | } |
| 5647 | |
| 5648 | void SetSafetyThreadStackSize(unsigned Value) { |
| 5649 | SafetyStackThreadSize = Value; |
| 5650 | } |
| 5651 | |
| 5652 | } |
| 5653 | |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5654 | extern "C" { |
| 5655 | |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 5656 | CXString clang_getClangVersion() { |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 5657 | return createCXString(getClangFullVersion()); |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5658 | } |
| 5659 | |
| 5660 | } // end: extern "C" |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5661 | |