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 | |
Douglas Gregor | fd64377 | 2010-01-25 16:45:46 +0000 | [diff] [blame] | 184 | return false; |
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. |
| 196 | FID = SM.getFileID(R.getBegin()); |
| 197 | if (FID != SM.getFileID(R.getEnd())) |
| 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 | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 300 | // We handle forward decls via ObjCClassDecl. |
| 301 | if (ObjCInterfaceDecl *InterD = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 302 | if (InterD->isForwardDecl()) |
| 303 | continue; |
| 304 | // An interface that started as a forward decl may have changed location |
| 305 | // because its @interface was parsed. |
| 306 | if (InterD->isInitiallyForwardDecl() && |
| 307 | !SM.isInFileID(SM.getFileLoc(InterD->getLocation()), File)) |
| 308 | continue; |
| 309 | } |
| 310 | |
Argyrios Kyrtzidis | e2079cf | 2011-11-16 08:58:54 +0000 | [diff] [blame] | 311 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
| 312 | if (!TD->isFreeStanding()) |
| 313 | continue; |
| 314 | |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 315 | RangeComparisonResult CompRes = RangeCompare(SM, D->getSourceRange(),Range); |
| 316 | if (CompRes == RangeBefore) |
| 317 | continue; |
| 318 | if (CompRes == RangeAfter) |
| 319 | break; |
| 320 | |
| 321 | assert(CompRes == RangeOverlap); |
| 322 | VisitedAtLeastOnce = true; |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 323 | |
| 324 | if (isa<ObjCContainerDecl>(D)) { |
| 325 | FileDI_current = &DIt; |
| 326 | FileDE_current = DE; |
| 327 | } else { |
| 328 | FileDI_current = 0; |
| 329 | } |
| 330 | |
Argyrios Kyrtzidis | ba98617 | 2011-11-03 19:02:28 +0000 | [diff] [blame] | 331 | if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true)) |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 332 | break; |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | if (VisitedAtLeastOnce) |
| 336 | return; |
| 337 | |
| 338 | // No Decls overlapped with the range. Move up the lexical context until there |
| 339 | // is a context that contains the range or we reach the translation unit |
| 340 | // level. |
| 341 | DeclContext *DC = DIt == Decls.begin() ? (*DIt)->getLexicalDeclContext() |
| 342 | : (*(DIt-1))->getLexicalDeclContext(); |
| 343 | |
| 344 | while (DC && !DC->isTranslationUnit()) { |
| 345 | Decl *D = cast<Decl>(DC); |
| 346 | SourceRange CurDeclRange = D->getSourceRange(); |
| 347 | if (CurDeclRange.isInvalid()) |
| 348 | break; |
| 349 | |
| 350 | if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) { |
Argyrios Kyrtzidis | ba98617 | 2011-11-03 19:02:28 +0000 | [diff] [blame] | 351 | Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true); |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 352 | break; |
| 353 | } |
| 354 | |
| 355 | DC = D->getLexicalDeclContext(); |
| 356 | } |
| 357 | } |
| 358 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 359 | bool CursorVisitor::visitPreprocessedEntitiesInRegion() { |
Argyrios Kyrtzidis | b49e728 | 2011-11-29 03:14:11 +0000 | [diff] [blame] | 360 | if (!AU->getPreprocessor().getPreprocessingRecord()) |
| 361 | return false; |
| 362 | |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 363 | PreprocessingRecord &PPRec |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 364 | = *AU->getPreprocessor().getPreprocessingRecord(); |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 365 | SourceManager &SM = AU->getSourceManager(); |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 366 | |
Argyrios Kyrtzidis | 92ddef1 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 367 | if (RegionOfInterest.isValid()) { |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 368 | SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest); |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 369 | SourceLocation B = MappedRange.getBegin(); |
| 370 | SourceLocation E = MappedRange.getEnd(); |
| 371 | |
| 372 | if (AU->isInPreambleFileID(B)) { |
| 373 | if (SM.isLoadedSourceLocation(E)) |
| 374 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), |
| 375 | PPRec, *this); |
| 376 | |
| 377 | // Beginning of range lies in the preamble but it also extends beyond |
| 378 | // it into the main file. Split the range into 2 parts, one covering |
| 379 | // the preamble and another covering the main file. This allows subsequent |
| 380 | // calls to visitPreprocessedEntitiesInRange to accept a source range that |
| 381 | // lies in the same FileID, allowing it to skip preprocessed entities that |
| 382 | // do not come from the same FileID. |
| 383 | bool breaked = |
| 384 | visitPreprocessedEntitiesInRange( |
| 385 | SourceRange(B, AU->getEndOfPreambleFileID()), |
| 386 | PPRec, *this); |
| 387 | if (breaked) return true; |
| 388 | return visitPreprocessedEntitiesInRange( |
| 389 | SourceRange(AU->getStartOfMainFileID(), E), |
| 390 | PPRec, *this); |
| 391 | } |
| 392 | |
| 393 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this); |
Argyrios Kyrtzidis | 92ddef1 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 396 | bool OnlyLocalDecls |
Douglas Gregor | 32038bb | 2010-12-21 19:07:48 +0000 | [diff] [blame] | 397 | = !AU->isMainFileAST() && AU->getOnlyLocalDecls(); |
| 398 | |
Argyrios Kyrtzidis | 92ddef1 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 399 | if (OnlyLocalDecls) |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 400 | return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(), |
| 401 | PPRec); |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 402 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 403 | return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec); |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | template<typename InputIterator> |
| 407 | bool CursorVisitor::visitPreprocessedEntities(InputIterator First, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 408 | InputIterator Last, |
| 409 | PreprocessingRecord &PPRec, |
| 410 | FileID FID) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 411 | for (; First != Last; ++First) { |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 412 | if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID)) |
| 413 | continue; |
| 414 | |
| 415 | PreprocessedEntity *PPE = *First; |
| 416 | if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 417 | if (Visit(MakeMacroExpansionCursor(ME, TU))) |
| 418 | return true; |
| 419 | |
| 420 | continue; |
| 421 | } |
| 422 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 423 | if (MacroDefinition *MD = dyn_cast<MacroDefinition>(PPE)) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 424 | if (Visit(MakeMacroDefinitionCursor(MD, TU))) |
| 425 | return true; |
| 426 | |
| 427 | continue; |
| 428 | } |
| 429 | |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 430 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) { |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 431 | if (Visit(MakeInclusionDirectiveCursor(ID, TU))) |
| 432 | return true; |
| 433 | |
| 434 | continue; |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 438 | return false; |
Douglas Gregor | 788f5a1 | 2010-03-20 00:41:21 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 441 | /// \brief Visit the children of the given cursor. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 442 | /// |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 443 | /// \returns true if the visitation should be aborted, false if it |
| 444 | /// should continue. |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 445 | bool CursorVisitor::VisitChildren(CXCursor Cursor) { |
Douglas Gregor | c314aa4 | 2011-03-02 19:17:03 +0000 | [diff] [blame] | 446 | if (clang_isReference(Cursor.kind) && |
| 447 | Cursor.kind != CXCursor_CXXBaseSpecifier) { |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 448 | // By definition, references have no children. |
| 449 | return false; |
| 450 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 451 | |
| 452 | // 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] | 453 | // done. |
Ted Kremenek | 0f91f6a | 2010-05-13 00:25:00 +0000 | [diff] [blame] | 454 | SetParentRAII SetParent(Parent, StmtParent, Cursor); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 455 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 456 | if (clang_isDeclaration(Cursor.kind)) { |
| 457 | Decl *D = getCursorDecl(Cursor); |
Douglas Gregor | 06d9b1a | 2011-04-14 21:41:34 +0000 | [diff] [blame] | 458 | if (!D) |
| 459 | return false; |
| 460 | |
Ted Kremenek | 539311e | 2010-02-18 18:47:01 +0000 | [diff] [blame] | 461 | return VisitAttributes(D) || Visit(D); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 462 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 463 | |
Douglas Gregor | 06d9b1a | 2011-04-14 21:41:34 +0000 | [diff] [blame] | 464 | if (clang_isStatement(Cursor.kind)) { |
| 465 | if (Stmt *S = getCursorStmt(Cursor)) |
| 466 | return Visit(S); |
| 467 | |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | if (clang_isExpression(Cursor.kind)) { |
| 472 | if (Expr *E = getCursorExpr(Cursor)) |
| 473 | return Visit(E); |
| 474 | |
| 475 | return false; |
| 476 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 477 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 478 | if (clang_isTranslationUnit(Cursor.kind)) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 479 | CXTranslationUnit tu = getCursorTU(Cursor); |
| 480 | ASTUnit *CXXUnit = static_cast<ASTUnit*>(tu->TUData); |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 481 | |
| 482 | int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast }; |
| 483 | for (unsigned I = 0; I != 2; ++I) { |
| 484 | if (VisitOrder[I]) { |
| 485 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && |
| 486 | RegionOfInterest.isInvalid()) { |
| 487 | for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(), |
| 488 | TLEnd = CXXUnit->top_level_end(); |
| 489 | TL != TLEnd; ++TL) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 490 | if (Visit(MakeCXCursor(*TL, tu, RegionOfInterest), true)) |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 491 | return true; |
| 492 | } |
| 493 | } else if (VisitDeclContext( |
| 494 | CXXUnit->getASTContext().getTranslationUnitDecl())) |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 495 | return true; |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 496 | continue; |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 497 | } |
Bob Wilson | 3178cb6 | 2010-03-19 03:57:57 +0000 | [diff] [blame] | 498 | |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 499 | // Walk the preprocessing record. |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 500 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) |
| 501 | visitPreprocessedEntitiesInRegion(); |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 502 | } |
Douglas Gregor | 04a9eb3 | 2011-03-16 23:23:30 +0000 | [diff] [blame] | 503 | |
Douglas Gregor | 7b691f33 | 2010-01-20 21:13:59 +0000 | [diff] [blame] | 504 | return false; |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 505 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 506 | |
Douglas Gregor | c314aa4 | 2011-03-02 19:17:03 +0000 | [diff] [blame] | 507 | if (Cursor.kind == CXCursor_CXXBaseSpecifier) { |
| 508 | if (CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) { |
| 509 | if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) { |
| 510 | return Visit(BaseTSInfo->getTypeLoc()); |
| 511 | } |
| 512 | } |
| 513 | } |
Argyrios Kyrtzidis | 221d5a5 | 2011-09-13 18:49:56 +0000 | [diff] [blame] | 514 | |
| 515 | if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { |
| 516 | IBOutletCollectionAttr *A = |
| 517 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor)); |
| 518 | if (const ObjCInterfaceType *InterT = A->getInterface()->getAs<ObjCInterfaceType>()) |
| 519 | return Visit(cxcursor::MakeCursorObjCClassRef(InterT->getInterface(), |
| 520 | A->getInterfaceLoc(), TU)); |
| 521 | } |
| 522 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 523 | // Nothing to visit at the moment. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 524 | return false; |
| 525 | } |
| 526 | |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 527 | bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { |
Douglas Gregor | 13c8ccb | 2011-04-22 23:49:24 +0000 | [diff] [blame] | 528 | if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten()) |
| 529 | if (Visit(TSInfo->getTypeLoc())) |
| 530 | return true; |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 531 | |
Ted Kremenek | 664cffd | 2010-07-22 11:30:19 +0000 | [diff] [blame] | 532 | if (Stmt *Body = B->getBody()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 533 | return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest)); |
Ted Kremenek | 664cffd | 2010-07-22 11:30:19 +0000 | [diff] [blame] | 534 | |
| 535 | return false; |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 538 | llvm::Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) { |
| 539 | if (RegionOfInterest.isValid()) { |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 540 | SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager()); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 541 | if (Range.isInvalid()) |
| 542 | return llvm::Optional<bool>(); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 543 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 544 | switch (CompareRegionOfInterest(Range)) { |
| 545 | case RangeBefore: |
| 546 | // This declaration comes before the region of interest; skip it. |
| 547 | return llvm::Optional<bool>(); |
| 548 | |
| 549 | case RangeAfter: |
| 550 | // This declaration comes after the region of interest; we're done. |
| 551 | return false; |
| 552 | |
| 553 | case RangeOverlap: |
| 554 | // This declaration overlaps the region of interest; visit it. |
| 555 | break; |
| 556 | } |
| 557 | } |
| 558 | return true; |
| 559 | } |
| 560 | |
| 561 | bool CursorVisitor::VisitDeclContext(DeclContext *DC) { |
| 562 | DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 563 | |
| 564 | // FIXME: Eventually remove. This part of a hack to support proper |
| 565 | // iteration over all Decls contained lexically within an ObjC container. |
| 566 | SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I); |
| 567 | SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E); |
| 568 | |
| 569 | for ( ; I != E; ++I) { |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 570 | Decl *D = *I; |
| 571 | if (D->getLexicalDeclContext() != DC) |
| 572 | continue; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 573 | CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 574 | const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); |
| 575 | if (!V.hasValue()) |
| 576 | continue; |
| 577 | if (!V.getValue()) |
| 578 | return false; |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 579 | if (Visit(Cursor, true)) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 580 | return true; |
| 581 | } |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 582 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 585 | bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 586 | llvm_unreachable("Translation units are visited directly by Visit()"); |
| 587 | return false; |
| 588 | } |
| 589 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 590 | bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 591 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 592 | return Visit(TSInfo->getTypeLoc()); |
| 593 | |
| 594 | return false; |
| 595 | } |
| 596 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 597 | bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { |
| 598 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 599 | return Visit(TSInfo->getTypeLoc()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 600 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 601 | return false; |
| 602 | } |
| 603 | |
| 604 | bool CursorVisitor::VisitTagDecl(TagDecl *D) { |
| 605 | return VisitDeclContext(D); |
| 606 | } |
| 607 | |
Douglas Gregor | 0ab1e9f | 2010-09-01 17:32:36 +0000 | [diff] [blame] | 608 | bool CursorVisitor::VisitClassTemplateSpecializationDecl( |
| 609 | ClassTemplateSpecializationDecl *D) { |
| 610 | bool ShouldVisitBody = false; |
| 611 | switch (D->getSpecializationKind()) { |
| 612 | case TSK_Undeclared: |
| 613 | case TSK_ImplicitInstantiation: |
| 614 | // Nothing to visit |
| 615 | return false; |
| 616 | |
| 617 | case TSK_ExplicitInstantiationDeclaration: |
| 618 | case TSK_ExplicitInstantiationDefinition: |
| 619 | break; |
| 620 | |
| 621 | case TSK_ExplicitSpecialization: |
| 622 | ShouldVisitBody = true; |
| 623 | break; |
| 624 | } |
| 625 | |
| 626 | // Visit the template arguments used in the specialization. |
| 627 | if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { |
| 628 | TypeLoc TL = SpecType->getTypeLoc(); |
| 629 | if (TemplateSpecializationTypeLoc *TSTLoc |
| 630 | = dyn_cast<TemplateSpecializationTypeLoc>(&TL)) { |
| 631 | for (unsigned I = 0, N = TSTLoc->getNumArgs(); I != N; ++I) |
| 632 | if (VisitTemplateArgumentLoc(TSTLoc->getArgLoc(I))) |
| 633 | return true; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | if (ShouldVisitBody && VisitCXXRecordDecl(D)) |
| 638 | return true; |
| 639 | |
| 640 | return false; |
| 641 | } |
| 642 | |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 643 | bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( |
| 644 | ClassTemplatePartialSpecializationDecl *D) { |
| 645 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 646 | // before visiting these template parameters. |
| 647 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 648 | return true; |
| 649 | |
| 650 | // Visit the partial specialization arguments. |
| 651 | const TemplateArgumentLoc *TemplateArgs = D->getTemplateArgsAsWritten(); |
| 652 | for (unsigned I = 0, N = D->getNumTemplateArgsAsWritten(); I != N; ++I) |
| 653 | if (VisitTemplateArgumentLoc(TemplateArgs[I])) |
| 654 | return true; |
| 655 | |
| 656 | return VisitCXXRecordDecl(D); |
| 657 | } |
| 658 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 659 | bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 660 | // Visit the default argument. |
| 661 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 662 | if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) |
| 663 | if (Visit(DefArg->getTypeLoc())) |
| 664 | return true; |
| 665 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 666 | return false; |
| 667 | } |
| 668 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 669 | bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 670 | if (Expr *Init = D->getInitExpr()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 671 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 672 | return false; |
| 673 | } |
| 674 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 675 | bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { |
| 676 | if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) |
| 677 | if (Visit(TSInfo->getTypeLoc())) |
| 678 | return true; |
| 679 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 680 | // Visit the nested-name-specifier, if present. |
| 681 | if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc()) |
| 682 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 683 | return true; |
| 684 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 685 | return false; |
| 686 | } |
| 687 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 688 | /// \brief Compare two base or member initializers based on their source order. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 689 | static int CompareCXXCtorInitializers(const void* Xp, const void *Yp) { |
| 690 | CXXCtorInitializer const * const *X |
| 691 | = static_cast<CXXCtorInitializer const * const *>(Xp); |
| 692 | CXXCtorInitializer const * const *Y |
| 693 | = static_cast<CXXCtorInitializer const * const *>(Yp); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 694 | |
| 695 | if ((*X)->getSourceOrder() < (*Y)->getSourceOrder()) |
| 696 | return -1; |
| 697 | else if ((*X)->getSourceOrder() > (*Y)->getSourceOrder()) |
| 698 | return 1; |
| 699 | else |
| 700 | return 0; |
| 701 | } |
| 702 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 703 | bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 704 | if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { |
| 705 | // Visit the function declaration's syntactic components in the order |
| 706 | // written. This requires a bit of work. |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 707 | TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 708 | FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL); |
| 709 | |
| 710 | // If we have a function declared directly (without the use of a typedef), |
| 711 | // visit just the return type. Otherwise, just visit the function's type |
| 712 | // now. |
| 713 | if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL->getResultLoc())) || |
| 714 | (!FTL && Visit(TL))) |
| 715 | return true; |
| 716 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 717 | // Visit the nested-name-specifier, if present. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 718 | if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc()) |
| 719 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 720 | return true; |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 721 | |
| 722 | // Visit the declaration name. |
| 723 | if (VisitDeclarationNameInfo(ND->getNameInfo())) |
| 724 | return true; |
| 725 | |
| 726 | // FIXME: Visit explicitly-specified template arguments! |
| 727 | |
| 728 | // Visit the function parameters, if we have a function type. |
| 729 | if (FTL && VisitFunctionTypeLoc(*FTL, true)) |
| 730 | return true; |
| 731 | |
| 732 | // FIXME: Attributes? |
| 733 | } |
| 734 | |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 735 | if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) { |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 736 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) { |
| 737 | // Find the initializers that were written in the source. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 738 | SmallVector<CXXCtorInitializer *, 4> WrittenInits; |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 739 | for (CXXConstructorDecl::init_iterator I = Constructor->init_begin(), |
| 740 | IEnd = Constructor->init_end(); |
| 741 | I != IEnd; ++I) { |
| 742 | if (!(*I)->isWritten()) |
| 743 | continue; |
| 744 | |
| 745 | WrittenInits.push_back(*I); |
| 746 | } |
| 747 | |
| 748 | // Sort the initializers in source order |
| 749 | llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 750 | &CompareCXXCtorInitializers); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 751 | |
| 752 | // Visit the initializers in source order |
| 753 | for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 754 | CXXCtorInitializer *Init = WrittenInits[I]; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 755 | if (Init->isAnyMemberInitializer()) { |
| 756 | if (Visit(MakeCursorMemberRef(Init->getAnyMember(), |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 757 | Init->getMemberLocation(), TU))) |
| 758 | return true; |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 759 | } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) { |
| 760 | if (Visit(TInfo->getTypeLoc())) |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 761 | return true; |
| 762 | } |
| 763 | |
| 764 | // Visit the initializer value. |
| 765 | if (Expr *Initializer = Init->getInit()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 766 | if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest))) |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 767 | return true; |
| 768 | } |
| 769 | } |
| 770 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 771 | if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 772 | return true; |
| 773 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 774 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 775 | return false; |
| 776 | } |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 777 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 778 | bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { |
| 779 | if (VisitDeclaratorDecl(D)) |
| 780 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 781 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 782 | if (Expr *BitWidth = D->getBitWidth()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 783 | return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 784 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 785 | return false; |
| 786 | } |
| 787 | |
| 788 | bool CursorVisitor::VisitVarDecl(VarDecl *D) { |
| 789 | if (VisitDeclaratorDecl(D)) |
| 790 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 791 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 792 | if (Expr *Init = D->getInit()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 793 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 794 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 795 | return false; |
| 796 | } |
| 797 | |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 798 | bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 799 | if (VisitDeclaratorDecl(D)) |
| 800 | return true; |
| 801 | |
| 802 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 803 | if (Expr *DefArg = D->getDefaultArgument()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 804 | return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 805 | |
| 806 | return false; |
| 807 | } |
| 808 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 809 | bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 810 | // FIXME: Visit the "outer" template parameter lists on the FunctionDecl |
| 811 | // before visiting these template parameters. |
| 812 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 813 | return true; |
| 814 | |
| 815 | return VisitFunctionDecl(D->getTemplatedDecl()); |
| 816 | } |
| 817 | |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 818 | bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 819 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 820 | // before visiting these template parameters. |
| 821 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 822 | return true; |
| 823 | |
| 824 | return VisitCXXRecordDecl(D->getTemplatedDecl()); |
| 825 | } |
| 826 | |
Douglas Gregor | 84b51d7 | 2010-09-01 20:16:53 +0000 | [diff] [blame] | 827 | bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 828 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 829 | return true; |
| 830 | |
| 831 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && |
| 832 | VisitTemplateArgumentLoc(D->getDefaultArgument())) |
| 833 | return true; |
| 834 | |
| 835 | return false; |
| 836 | } |
| 837 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 838 | bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
Douglas Gregor | 4bc1cb6 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 839 | if (TypeSourceInfo *TSInfo = ND->getResultTypeSourceInfo()) |
| 840 | if (Visit(TSInfo->getTypeLoc())) |
| 841 | return true; |
| 842 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 843 | for (ObjCMethodDecl::param_iterator P = ND->param_begin(), |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 844 | PEnd = ND->param_end(); |
| 845 | P != PEnd; ++P) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 846 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 847 | return true; |
| 848 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 849 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 850 | if (ND->isThisDeclarationADefinition() && |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 851 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 852 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 853 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 854 | return false; |
| 855 | } |
| 856 | |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 857 | template <typename DeclIt> |
| 858 | static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current, |
| 859 | SourceManager &SM, SourceLocation EndLoc, |
| 860 | SmallVectorImpl<Decl *> &Decls) { |
| 861 | DeclIt next = *DI_current; |
| 862 | while (++next != DE_current) { |
| 863 | Decl *D_next = *next; |
| 864 | if (!D_next) |
| 865 | break; |
| 866 | SourceLocation L = D_next->getLocStart(); |
| 867 | if (!L.isValid()) |
| 868 | break; |
| 869 | if (SM.isBeforeInTranslationUnit(L, EndLoc)) { |
| 870 | *DI_current = next; |
| 871 | Decls.push_back(D_next); |
| 872 | continue; |
| 873 | } |
| 874 | break; |
| 875 | } |
| 876 | } |
| 877 | |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 878 | namespace { |
| 879 | struct ContainerDeclsSort { |
| 880 | SourceManager &SM; |
| 881 | ContainerDeclsSort(SourceManager &sm) : SM(sm) {} |
| 882 | bool operator()(Decl *A, Decl *B) { |
| 883 | SourceLocation L_A = A->getLocStart(); |
| 884 | SourceLocation L_B = B->getLocStart(); |
| 885 | assert(L_A.isValid() && L_B.isValid()); |
| 886 | return SM.isBeforeInTranslationUnit(L_A, L_B); |
| 887 | } |
| 888 | }; |
| 889 | } |
| 890 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 891 | bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 892 | // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially |
| 893 | // an @implementation can lexically contain Decls that are not properly |
| 894 | // nested in the AST. When we identify such cases, we need to retrofit |
| 895 | // this nesting here. |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 896 | if (!DI_current && !FileDI_current) |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 897 | return VisitDeclContext(D); |
| 898 | |
| 899 | // Scan the Decls that immediately come after the container |
| 900 | // in the current DeclContext. If any fall within the |
| 901 | // container's lexical region, stash them into a vector |
| 902 | // for later processing. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 903 | SmallVector<Decl *, 24> DeclsInContainer; |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 904 | SourceLocation EndLoc = D->getSourceRange().getEnd(); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 905 | SourceManager &SM = AU->getSourceManager(); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 906 | if (EndLoc.isValid()) { |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 907 | if (DI_current) { |
| 908 | addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc, |
| 909 | DeclsInContainer); |
| 910 | } else { |
| 911 | addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc, |
| 912 | DeclsInContainer); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
| 916 | // The common case. |
| 917 | if (DeclsInContainer.empty()) |
| 918 | return VisitDeclContext(D); |
| 919 | |
| 920 | // Get all the Decls in the DeclContext, and sort them with the |
| 921 | // additional ones we've collected. Then visit them. |
| 922 | for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end(); |
| 923 | I!=E; ++I) { |
| 924 | Decl *subDecl = *I; |
Ted Kremenek | 0582c89 | 2010-11-02 23:17:51 +0000 | [diff] [blame] | 925 | if (!subDecl || subDecl->getLexicalDeclContext() != D || |
| 926 | subDecl->getLocStart().isInvalid()) |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 927 | continue; |
| 928 | DeclsInContainer.push_back(subDecl); |
| 929 | } |
| 930 | |
| 931 | // Now sort the Decls so that they appear in lexical order. |
| 932 | std::sort(DeclsInContainer.begin(), DeclsInContainer.end(), |
| 933 | ContainerDeclsSort(SM)); |
| 934 | |
| 935 | // Now visit the decls. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 936 | for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(), |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 937 | E = DeclsInContainer.end(); I != E; ++I) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 938 | CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest); |
Ted Kremenek | d8c370c | 2010-11-02 23:10:24 +0000 | [diff] [blame] | 939 | const llvm::Optional<bool> &V = shouldVisitCursor(Cursor); |
| 940 | if (!V.hasValue()) |
| 941 | continue; |
| 942 | if (!V.getValue()) |
| 943 | return false; |
| 944 | if (Visit(Cursor, true)) |
| 945 | return true; |
| 946 | } |
| 947 | return false; |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 950 | bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 951 | if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), |
| 952 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 953 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 954 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 955 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 956 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 957 | E = ND->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 958 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 959 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 960 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 961 | return VisitObjCContainerDecl(ND); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 964 | bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 965 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
| 966 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 967 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 968 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 969 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 970 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 971 | return VisitObjCContainerDecl(PID); |
| 972 | } |
| 973 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 974 | bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 975 | if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc())) |
John McCall | fc92920 | 2010-06-04 22:33:30 +0000 | [diff] [blame] | 976 | return true; |
| 977 | |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 978 | // FIXME: This implements a workaround with @property declarations also being |
| 979 | // installed in the DeclContext for the @interface. Eventually this code |
| 980 | // should be removed. |
| 981 | ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); |
| 982 | if (!CDecl || !CDecl->IsClassExtension()) |
| 983 | return false; |
| 984 | |
| 985 | ObjCInterfaceDecl *ID = CDecl->getClassInterface(); |
| 986 | if (!ID) |
| 987 | return false; |
| 988 | |
| 989 | IdentifierInfo *PropertyId = PD->getIdentifier(); |
| 990 | ObjCPropertyDecl *prevDecl = |
| 991 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId); |
| 992 | |
| 993 | if (!prevDecl) |
| 994 | return false; |
| 995 | |
| 996 | // Visit synthesized methods since they will be skipped when visiting |
| 997 | // the @interface. |
| 998 | if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) |
Ted Kremenek | a054fb4 | 2010-09-21 20:52:59 +0000 | [diff] [blame] | 999 | if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1000 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 1001 | return true; |
| 1002 | |
| 1003 | if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) |
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 | return false; |
| 1009 | } |
| 1010 | |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1011 | bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1012 | // Issue callbacks for super class. |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1013 | if (D->getSuperClass() && |
| 1014 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1015 | D->getSuperClassLoc(), |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1016 | TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1017 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1018 | |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 1019 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1020 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 1021 | E = D->protocol_end(); I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1022 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1023 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1024 | |
Douglas Gregor | a59e390 | 2010-01-21 23:27:09 +0000 | [diff] [blame] | 1025 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1028 | bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 1029 | return VisitObjCContainerDecl(D); |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1032 | bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 1033 | // 'ID' could be null when dealing with invalid code. |
| 1034 | if (ObjCInterfaceDecl *ID = D->getClassInterface()) |
| 1035 | if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) |
| 1036 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1037 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1038 | return VisitObjCImplDecl(D); |
| 1039 | } |
| 1040 | |
| 1041 | bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 1042 | #if 0 |
| 1043 | // Issue callbacks for super class. |
| 1044 | // FIXME: No source location information! |
| 1045 | if (D->getSuperClass() && |
| 1046 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1047 | D->getSuperClassLoc(), |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1048 | TU))) |
| 1049 | return true; |
| 1050 | #endif |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1051 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1052 | return VisitObjCImplDecl(D); |
| 1053 | } |
| 1054 | |
| 1055 | bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 1056 | ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1057 | for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(), |
| 1058 | E = D->protocol_end(); |
| 1059 | I != E; ++I, ++PL) |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 1060 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 1061 | return true; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1062 | |
| 1063 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1066 | bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) { |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 1067 | if (Visit(MakeCursorObjCClassRef(D->getForwardInterfaceDecl(), |
Douglas Gregor | af76472 | 2011-12-14 17:12:03 +0000 | [diff] [blame] | 1068 | D->getNameLoc(), TU))) |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1069 | return true; |
Douglas Gregor | 1ef2fc1 | 2010-01-22 00:50:27 +0000 | [diff] [blame] | 1070 | return false; |
Ted Kremenek | dd6bcc5 | 2010-01-13 00:22:49 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Douglas Gregor | a4ffd85 | 2010-11-17 01:03:52 +0000 | [diff] [blame] | 1073 | bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) { |
| 1074 | if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl()) |
| 1075 | return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU)); |
| 1076 | |
| 1077 | return false; |
| 1078 | } |
| 1079 | |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 1080 | bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { |
| 1081 | return VisitDeclContext(D); |
| 1082 | } |
| 1083 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 1084 | bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1085 | // Visit nested-name-specifier. |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 1086 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1087 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1088 | return true; |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 1089 | |
| 1090 | return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), |
| 1091 | D->getTargetNameLoc(), TU)); |
| 1092 | } |
| 1093 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1094 | bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1095 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1096 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1097 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1098 | return true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1099 | } |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1100 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1101 | if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU))) |
| 1102 | return true; |
| 1103 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1104 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1105 | } |
| 1106 | |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1107 | bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1108 | // Visit nested-name-specifier. |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 1109 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1110 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1111 | return true; |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1112 | |
| 1113 | return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), |
| 1114 | D->getIdentLocation(), TU)); |
| 1115 | } |
| 1116 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1117 | bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1118 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1119 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1120 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1121 | return true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1122 | } |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1123 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1124 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1125 | } |
| 1126 | |
| 1127 | bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( |
| 1128 | UnresolvedUsingTypenameDecl *D) { |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1129 | // Visit nested-name-specifier. |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1130 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1131 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1132 | return true; |
| 1133 | |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 1134 | return false; |
| 1135 | } |
| 1136 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1137 | bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { |
| 1138 | switch (Name.getName().getNameKind()) { |
| 1139 | case clang::DeclarationName::Identifier: |
| 1140 | case clang::DeclarationName::CXXLiteralOperatorName: |
| 1141 | case clang::DeclarationName::CXXOperatorName: |
| 1142 | case clang::DeclarationName::CXXUsingDirective: |
| 1143 | return false; |
| 1144 | |
| 1145 | case clang::DeclarationName::CXXConstructorName: |
| 1146 | case clang::DeclarationName::CXXDestructorName: |
| 1147 | case clang::DeclarationName::CXXConversionFunctionName: |
| 1148 | if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) |
| 1149 | return Visit(TSInfo->getTypeLoc()); |
| 1150 | return false; |
| 1151 | |
| 1152 | case clang::DeclarationName::ObjCZeroArgSelector: |
| 1153 | case clang::DeclarationName::ObjCOneArgSelector: |
| 1154 | case clang::DeclarationName::ObjCMultiArgSelector: |
| 1155 | // FIXME: Per-identifier location info? |
| 1156 | return false; |
| 1157 | } |
| 1158 | |
| 1159 | return false; |
| 1160 | } |
| 1161 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1162 | bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, |
| 1163 | SourceRange Range) { |
| 1164 | // FIXME: This whole routine is a hack to work around the lack of proper |
| 1165 | // source information in nested-name-specifiers (PR5791). Since we do have |
| 1166 | // a beginning source location, we can visit the first component of the |
| 1167 | // nested-name-specifier, if it's a single-token component. |
| 1168 | if (!NNS) |
| 1169 | return false; |
| 1170 | |
| 1171 | // Get the first component in the nested-name-specifier. |
| 1172 | while (NestedNameSpecifier *Prefix = NNS->getPrefix()) |
| 1173 | NNS = Prefix; |
| 1174 | |
| 1175 | switch (NNS->getKind()) { |
| 1176 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1177 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), |
| 1178 | TU)); |
| 1179 | |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 1180 | case NestedNameSpecifier::NamespaceAlias: |
| 1181 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1182 | Range.getBegin(), TU)); |
| 1183 | |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1184 | case NestedNameSpecifier::TypeSpec: { |
| 1185 | // If the type has a form where we know that the beginning of the source |
| 1186 | // range matches up with a reference cursor. Visit the appropriate reference |
| 1187 | // cursor. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1188 | const Type *T = NNS->getAsType(); |
Douglas Gregor | c5ade2e | 2010-09-02 17:35:32 +0000 | [diff] [blame] | 1189 | if (const TypedefType *Typedef = dyn_cast<TypedefType>(T)) |
| 1190 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); |
| 1191 | if (const TagType *Tag = dyn_cast<TagType>(T)) |
| 1192 | return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); |
| 1193 | if (const TemplateSpecializationType *TST |
| 1194 | = dyn_cast<TemplateSpecializationType>(T)) |
| 1195 | return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); |
| 1196 | break; |
| 1197 | } |
| 1198 | |
| 1199 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1200 | case NestedNameSpecifier::Global: |
| 1201 | case NestedNameSpecifier::Identifier: |
| 1202 | break; |
| 1203 | } |
| 1204 | |
| 1205 | return false; |
| 1206 | } |
| 1207 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1208 | bool |
| 1209 | CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1210 | SmallVector<NestedNameSpecifierLoc, 4> Qualifiers; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1211 | for (; Qualifier; Qualifier = Qualifier.getPrefix()) |
| 1212 | Qualifiers.push_back(Qualifier); |
| 1213 | |
| 1214 | while (!Qualifiers.empty()) { |
| 1215 | NestedNameSpecifierLoc Q = Qualifiers.pop_back_val(); |
| 1216 | NestedNameSpecifier *NNS = Q.getNestedNameSpecifier(); |
| 1217 | switch (NNS->getKind()) { |
| 1218 | case NestedNameSpecifier::Namespace: |
| 1219 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), |
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::NamespaceAlias: |
| 1227 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1228 | Q.getLocalBeginLoc(), |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1229 | TU))) |
| 1230 | return true; |
| 1231 | |
| 1232 | break; |
| 1233 | |
| 1234 | case NestedNameSpecifier::TypeSpec: |
| 1235 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1236 | if (Visit(Q.getTypeLoc())) |
| 1237 | return true; |
| 1238 | |
| 1239 | break; |
| 1240 | |
| 1241 | case NestedNameSpecifier::Global: |
| 1242 | case NestedNameSpecifier::Identifier: |
| 1243 | break; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | return false; |
| 1248 | } |
| 1249 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1250 | bool CursorVisitor::VisitTemplateParameters( |
| 1251 | const TemplateParameterList *Params) { |
| 1252 | if (!Params) |
| 1253 | return false; |
| 1254 | |
| 1255 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 1256 | PEnd = Params->end(); |
| 1257 | P != PEnd; ++P) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1258 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1259 | return true; |
| 1260 | } |
| 1261 | |
| 1262 | return false; |
| 1263 | } |
| 1264 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1265 | bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { |
| 1266 | switch (Name.getKind()) { |
| 1267 | case TemplateName::Template: |
| 1268 | return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); |
| 1269 | |
| 1270 | case TemplateName::OverloadedTemplate: |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 1271 | // Visit the overloaded template set. |
| 1272 | if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU))) |
| 1273 | return true; |
| 1274 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1275 | return false; |
| 1276 | |
| 1277 | case TemplateName::DependentTemplate: |
| 1278 | // FIXME: Visit nested-name-specifier. |
| 1279 | return false; |
| 1280 | |
| 1281 | case TemplateName::QualifiedTemplate: |
| 1282 | // FIXME: Visit nested-name-specifier. |
| 1283 | return Visit(MakeCursorTemplateRef( |
| 1284 | Name.getAsQualifiedTemplateName()->getDecl(), |
| 1285 | Loc, TU)); |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1286 | |
| 1287 | case TemplateName::SubstTemplateTemplateParm: |
| 1288 | return Visit(MakeCursorTemplateRef( |
| 1289 | Name.getAsSubstTemplateTemplateParm()->getParameter(), |
| 1290 | Loc, TU)); |
Douglas Gregor | 1aee05d | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 1291 | |
| 1292 | case TemplateName::SubstTemplateTemplateParmPack: |
| 1293 | return Visit(MakeCursorTemplateRef( |
| 1294 | Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), |
| 1295 | Loc, TU)); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | return false; |
| 1299 | } |
| 1300 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1301 | bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { |
| 1302 | switch (TAL.getArgument().getKind()) { |
| 1303 | case TemplateArgument::Null: |
| 1304 | case TemplateArgument::Integral: |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 1305 | case TemplateArgument::Pack: |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1306 | return false; |
| 1307 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1308 | case TemplateArgument::Type: |
| 1309 | if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) |
| 1310 | return Visit(TSInfo->getTypeLoc()); |
| 1311 | return false; |
| 1312 | |
| 1313 | case TemplateArgument::Declaration: |
| 1314 | if (Expr *E = TAL.getSourceDeclExpression()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1315 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1316 | return false; |
| 1317 | |
| 1318 | case TemplateArgument::Expression: |
| 1319 | if (Expr *E = TAL.getSourceExpression()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1320 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1321 | return false; |
| 1322 | |
| 1323 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1324 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 1325 | if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc())) |
| 1326 | return true; |
| 1327 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1328 | return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1329 | TAL.getTemplateNameLoc()); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | return false; |
| 1333 | } |
| 1334 | |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 1335 | bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 1336 | return VisitDeclContext(D); |
| 1337 | } |
| 1338 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1339 | bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1340 | return Visit(TL.getUnqualifiedLoc()); |
| 1341 | } |
| 1342 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1343 | bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 1344 | ASTContext &Context = AU->getASTContext(); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1345 | |
| 1346 | // Some builtin types (such as Objective-C's "id", "sel", and |
| 1347 | // "Class") have associated declarations. Create cursors for those. |
| 1348 | QualType VisitType; |
John McCall | e0a22d0 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 1349 | switch (TL.getTypePtr()->getKind()) { |
John McCall | 2dde35b | 2011-10-18 22:28:37 +0000 | [diff] [blame] | 1350 | |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1351 | case BuiltinType::Void: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1352 | case BuiltinType::NullPtr: |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1353 | case BuiltinType::Dependent: |
John McCall | 2dde35b | 2011-10-18 22:28:37 +0000 | [diff] [blame] | 1354 | #define BUILTIN_TYPE(Id, SingletonId) |
| 1355 | #define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1356 | #define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1357 | #define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1358 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1359 | #include "clang/AST/BuiltinTypes.def" |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1360 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1361 | |
Ted Kremenek | c4174cc | 2010-02-18 18:52:18 +0000 | [diff] [blame] | 1362 | case BuiltinType::ObjCId: |
| 1363 | VisitType = Context.getObjCIdType(); |
| 1364 | break; |
Ted Kremenek | 6b3b514 | 2010-02-18 22:32:43 +0000 | [diff] [blame] | 1365 | |
| 1366 | case BuiltinType::ObjCClass: |
| 1367 | VisitType = Context.getObjCClassType(); |
| 1368 | break; |
| 1369 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1370 | case BuiltinType::ObjCSel: |
| 1371 | VisitType = Context.getObjCSelType(); |
| 1372 | break; |
| 1373 | } |
| 1374 | |
| 1375 | if (!VisitType.isNull()) { |
| 1376 | if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1377 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1378 | TU)); |
| 1379 | } |
| 1380 | |
| 1381 | return false; |
| 1382 | } |
| 1383 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1384 | bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1385 | return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU)); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1388 | bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 1389 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1390 | } |
| 1391 | |
| 1392 | bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { |
Argyrios Kyrtzidis | 6f155de | 2011-08-25 22:24:47 +0000 | [diff] [blame] | 1393 | if (TL.isDefinition()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1394 | return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest)); |
Argyrios Kyrtzidis | 6f155de | 2011-08-25 22:24:47 +0000 | [diff] [blame] | 1395 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1396 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1397 | } |
| 1398 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1399 | bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
Chandler Carruth | 960d13d | 2011-05-01 09:53:37 +0000 | [diff] [blame] | 1400 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1403 | bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 1404 | if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU))) |
| 1405 | return true; |
| 1406 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1407 | return false; |
| 1408 | } |
| 1409 | |
| 1410 | bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 1411 | if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) |
| 1412 | return true; |
| 1413 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1414 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1415 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1416 | TU))) |
| 1417 | return true; |
| 1418 | } |
| 1419 | |
| 1420 | return false; |
| 1421 | } |
| 1422 | |
| 1423 | bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1424 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1427 | bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) { |
| 1428 | return Visit(TL.getInnerLoc()); |
| 1429 | } |
| 1430 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1431 | bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1432 | return Visit(TL.getPointeeLoc()); |
| 1433 | } |
| 1434 | |
| 1435 | bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1436 | return Visit(TL.getPointeeLoc()); |
| 1437 | } |
| 1438 | |
| 1439 | bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1440 | return Visit(TL.getPointeeLoc()); |
| 1441 | } |
| 1442 | |
| 1443 | bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1444 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 1448 | return Visit(TL.getPointeeLoc()); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Argyrios Kyrtzidis | 3422fbc | 2011-08-15 18:44:43 +0000 | [diff] [blame] | 1451 | bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 1452 | return Visit(TL.getModifiedLoc()); |
| 1453 | } |
| 1454 | |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 1455 | bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, |
| 1456 | bool SkipResultType) { |
| 1457 | if (!SkipResultType && Visit(TL.getResultLoc())) |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1458 | return true; |
| 1459 | |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1460 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
Ted Kremenek | 5dbacb4 | 2010-04-07 00:27:13 +0000 | [diff] [blame] | 1461 | if (Decl *D = TL.getArg(I)) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1462 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest))) |
Ted Kremenek | 5dbacb4 | 2010-04-07 00:27:13 +0000 | [diff] [blame] | 1463 | return true; |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1464 | |
| 1465 | return false; |
| 1466 | } |
| 1467 | |
| 1468 | bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1469 | if (Visit(TL.getElementLoc())) |
| 1470 | return true; |
| 1471 | |
| 1472 | if (Expr *Size = TL.getSizeExpr()) |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1473 | return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest)); |
Douglas Gregor | f20dfbc | 2010-01-21 17:29:07 +0000 | [diff] [blame] | 1474 | |
| 1475 | return false; |
| 1476 | } |
| 1477 | |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1478 | bool CursorVisitor::VisitTemplateSpecializationTypeLoc( |
| 1479 | TemplateSpecializationTypeLoc TL) { |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 1480 | // Visit the template name. |
| 1481 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1482 | TL.getTemplateNameLoc())) |
| 1483 | return true; |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 1484 | |
| 1485 | // Visit the template arguments. |
| 1486 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1487 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1488 | return true; |
| 1489 | |
| 1490 | return false; |
| 1491 | } |
| 1492 | |
Douglas Gregor | 2332c11 | 2010-01-21 20:48:56 +0000 | [diff] [blame] | 1493 | bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 1494 | return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); |
| 1495 | } |
| 1496 | |
| 1497 | bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 1498 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1499 | return Visit(TSInfo->getTypeLoc()); |
| 1500 | |
| 1501 | return false; |
| 1502 | } |
| 1503 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1504 | bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 1505 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1506 | return Visit(TSInfo->getTypeLoc()); |
| 1507 | |
| 1508 | return false; |
| 1509 | } |
| 1510 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 1511 | bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
| 1512 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1513 | return true; |
| 1514 | |
| 1515 | return false; |
| 1516 | } |
| 1517 | |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 1518 | bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc( |
| 1519 | DependentTemplateSpecializationTypeLoc TL) { |
| 1520 | // Visit the nested-name-specifier, if there is one. |
| 1521 | if (TL.getQualifierLoc() && |
| 1522 | VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1523 | return true; |
| 1524 | |
| 1525 | // Visit the template arguments. |
| 1526 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1527 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1528 | return true; |
| 1529 | |
| 1530 | return false; |
| 1531 | } |
| 1532 | |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1533 | bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 1534 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1535 | return true; |
| 1536 | |
| 1537 | return Visit(TL.getNamedTypeLoc()); |
| 1538 | } |
| 1539 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 1540 | bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { |
| 1541 | return Visit(TL.getPatternLoc()); |
| 1542 | } |
| 1543 | |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 1544 | bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 1545 | if (Expr *E = TL.getUnderlyingExpr()) |
| 1546 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1547 | |
| 1548 | return false; |
| 1549 | } |
| 1550 | |
| 1551 | bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 1552 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1553 | } |
| 1554 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1555 | bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) { |
| 1556 | return Visit(TL.getValueLoc()); |
| 1557 | } |
| 1558 | |
Argyrios Kyrtzidis | 427964e | 2011-08-15 22:40:24 +0000 | [diff] [blame] | 1559 | #define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \ |
| 1560 | bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \ |
| 1561 | return Visit##PARENT##Loc(TL); \ |
| 1562 | } |
| 1563 | |
| 1564 | DEFAULT_TYPELOC_IMPL(Complex, Type) |
| 1565 | DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType) |
| 1566 | DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType) |
| 1567 | DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType) |
| 1568 | DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType) |
| 1569 | DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type) |
| 1570 | DEFAULT_TYPELOC_IMPL(Vector, Type) |
| 1571 | DEFAULT_TYPELOC_IMPL(ExtVector, VectorType) |
| 1572 | DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType) |
| 1573 | DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType) |
| 1574 | DEFAULT_TYPELOC_IMPL(Record, TagType) |
| 1575 | DEFAULT_TYPELOC_IMPL(Enum, TagType) |
| 1576 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type) |
| 1577 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type) |
| 1578 | DEFAULT_TYPELOC_IMPL(Auto, Type) |
| 1579 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 1580 | bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1581 | // Visit the nested-name-specifier, if present. |
| 1582 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1583 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1584 | return true; |
| 1585 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 1586 | if (D->isCompleteDefinition()) { |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 1587 | for (CXXRecordDecl::base_class_iterator I = D->bases_begin(), |
| 1588 | E = D->bases_end(); I != E; ++I) { |
| 1589 | if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU))) |
| 1590 | return true; |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | return VisitTagDecl(D); |
| 1595 | } |
| 1596 | |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1597 | bool CursorVisitor::VisitAttributes(Decl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1598 | for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end(); |
| 1599 | i != e; ++i) |
| 1600 | if (Visit(MakeCXCursor(*i, D, TU))) |
Ted Kremenek | 09dfa37 | 2010-02-18 05:46:33 +0000 | [diff] [blame] | 1601 | return true; |
| 1602 | |
| 1603 | return false; |
| 1604 | } |
| 1605 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 1606 | //===----------------------------------------------------------------------===// |
| 1607 | // Data-recursive visitor methods. |
| 1608 | //===----------------------------------------------------------------------===// |
| 1609 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1610 | namespace { |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1611 | #define DEF_JOB(NAME, DATA, KIND)\ |
| 1612 | class NAME : public VisitorJob {\ |
| 1613 | public:\ |
| 1614 | NAME(DATA *d, CXCursor parent) : VisitorJob(parent, VisitorJob::KIND, d) {} \ |
| 1615 | static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\ |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1616 | DATA *get() const { return static_cast<DATA*>(data[0]); }\ |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1617 | }; |
| 1618 | |
| 1619 | DEF_JOB(StmtVisit, Stmt, StmtVisitKind) |
| 1620 | DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind) |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1621 | DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1622 | DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind) |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1623 | DEF_JOB(ExplicitTemplateArgsVisit, ASTTemplateArgumentListInfo, |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1624 | ExplicitTemplateArgsVisitKind) |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 1625 | DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1626 | #undef DEF_JOB |
| 1627 | |
| 1628 | class DeclVisit : public VisitorJob { |
| 1629 | public: |
| 1630 | DeclVisit(Decl *d, CXCursor parent, bool isFirst) : |
| 1631 | VisitorJob(parent, VisitorJob::DeclVisitKind, |
| 1632 | d, isFirst ? (void*) 1 : (void*) 0) {} |
| 1633 | static bool classof(const VisitorJob *VJ) { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 1634 | return VJ->getKind() == DeclVisitKind; |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1635 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1636 | Decl *get() const { return static_cast<Decl*>(data[0]); } |
| 1637 | bool isFirst() const { return data[1] ? true : false; } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1638 | }; |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1639 | class TypeLocVisit : public VisitorJob { |
| 1640 | public: |
| 1641 | TypeLocVisit(TypeLoc tl, CXCursor parent) : |
| 1642 | VisitorJob(parent, VisitorJob::TypeLocVisitKind, |
| 1643 | tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {} |
| 1644 | |
| 1645 | static bool classof(const VisitorJob *VJ) { |
| 1646 | return VJ->getKind() == TypeLocVisitKind; |
| 1647 | } |
| 1648 | |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 1649 | TypeLoc get() const { |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1650 | QualType T = QualType::getFromOpaquePtr(data[0]); |
| 1651 | return TypeLoc(T, data[1]); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1652 | } |
| 1653 | }; |
| 1654 | |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1655 | class LabelRefVisit : public VisitorJob { |
| 1656 | public: |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1657 | LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent) |
| 1658 | : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD, |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1659 | labelLoc.getPtrEncoding()) {} |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1660 | |
| 1661 | static bool classof(const VisitorJob *VJ) { |
| 1662 | return VJ->getKind() == VisitorJob::LabelRefVisitKind; |
| 1663 | } |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 1664 | LabelDecl *get() const { return static_cast<LabelDecl*>(data[0]); } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1665 | SourceLocation getLoc() const { |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1666 | return SourceLocation::getFromPtrEncoding(data[1]); } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1667 | }; |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1668 | |
| 1669 | class NestedNameSpecifierLocVisit : public VisitorJob { |
| 1670 | public: |
| 1671 | NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent) |
| 1672 | : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind, |
| 1673 | Qualifier.getNestedNameSpecifier(), |
| 1674 | Qualifier.getOpaqueData()) { } |
| 1675 | |
| 1676 | static bool classof(const VisitorJob *VJ) { |
| 1677 | return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind; |
| 1678 | } |
| 1679 | |
| 1680 | NestedNameSpecifierLoc get() const { |
| 1681 | return NestedNameSpecifierLoc(static_cast<NestedNameSpecifier*>(data[0]), |
| 1682 | data[1]); |
| 1683 | } |
| 1684 | }; |
| 1685 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1686 | class DeclarationNameInfoVisit : public VisitorJob { |
| 1687 | public: |
| 1688 | DeclarationNameInfoVisit(Stmt *S, CXCursor parent) |
| 1689 | : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {} |
| 1690 | static bool classof(const VisitorJob *VJ) { |
| 1691 | return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind; |
| 1692 | } |
| 1693 | DeclarationNameInfo get() const { |
| 1694 | Stmt *S = static_cast<Stmt*>(data[0]); |
| 1695 | switch (S->getStmtClass()) { |
| 1696 | default: |
| 1697 | llvm_unreachable("Unhandled Stmt"); |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 1698 | case clang::Stmt::MSDependentExistsStmtClass: |
| 1699 | return cast<MSDependentExistsStmt>(S)->getNameInfo(); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1700 | case Stmt::CXXDependentScopeMemberExprClass: |
| 1701 | return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo(); |
| 1702 | case Stmt::DependentScopeDeclRefExprClass: |
| 1703 | return cast<DependentScopeDeclRefExpr>(S)->getNameInfo(); |
| 1704 | } |
| 1705 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1706 | }; |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1707 | class MemberRefVisit : public VisitorJob { |
| 1708 | public: |
| 1709 | MemberRefVisit(FieldDecl *D, SourceLocation L, CXCursor parent) |
| 1710 | : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D, |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1711 | L.getPtrEncoding()) {} |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1712 | static bool classof(const VisitorJob *VJ) { |
| 1713 | return VJ->getKind() == VisitorJob::MemberRefVisitKind; |
| 1714 | } |
| 1715 | FieldDecl *get() const { |
| 1716 | return static_cast<FieldDecl*>(data[0]); |
| 1717 | } |
| 1718 | SourceLocation getLoc() const { |
| 1719 | return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]); |
| 1720 | } |
| 1721 | }; |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1722 | class EnqueueVisitor : public StmtVisitor<EnqueueVisitor, void> { |
| 1723 | VisitorWorkList &WL; |
| 1724 | CXCursor Parent; |
| 1725 | public: |
| 1726 | EnqueueVisitor(VisitorWorkList &wl, CXCursor parent) |
| 1727 | : WL(wl), Parent(parent) {} |
| 1728 | |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1729 | void VisitAddrLabelExpr(AddrLabelExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1730 | void VisitBlockExpr(BlockExpr *B); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1731 | void VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
Ted Kremenek | 083c7e2 | 2010-11-13 05:38:03 +0000 | [diff] [blame] | 1732 | void VisitCompoundStmt(CompoundStmt *S); |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1733 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { /* Do nothing. */ } |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 1734 | void VisitMSDependentExistsStmt(MSDependentExistsStmt *S); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1735 | void VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1736 | void VisitCXXNewExpr(CXXNewExpr *E); |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 1737 | void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1738 | void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1739 | void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1740 | void VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); |
Ted Kremenek | b8dd1ca | 2010-11-17 00:50:41 +0000 | [diff] [blame] | 1741 | void VisitCXXTypeidExpr(CXXTypeidExpr *E); |
Ted Kremenek | 55b933a | 2010-11-17 00:50:36 +0000 | [diff] [blame] | 1742 | void VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); |
Ted Kremenek | 1e7e877 | 2010-11-17 00:50:52 +0000 | [diff] [blame] | 1743 | void VisitCXXUuidofExpr(CXXUuidofExpr *E); |
Argyrios Kyrtzidis | dcbb2fb | 2011-12-03 03:49:44 +0000 | [diff] [blame] | 1744 | void VisitCXXCatchStmt(CXXCatchStmt *S); |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1745 | void VisitDeclRefExpr(DeclRefExpr *D); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1746 | void VisitDeclStmt(DeclStmt *S); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1747 | void VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1748 | void VisitDesignatedInitExpr(DesignatedInitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1749 | void VisitExplicitCastExpr(ExplicitCastExpr *E); |
| 1750 | void VisitForStmt(ForStmt *FS); |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1751 | void VisitGotoStmt(GotoStmt *GS); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1752 | void VisitIfStmt(IfStmt *If); |
| 1753 | void VisitInitListExpr(InitListExpr *IE); |
| 1754 | void VisitMemberExpr(MemberExpr *M); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1755 | void VisitOffsetOfExpr(OffsetOfExpr *E); |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1756 | void VisitObjCEncodeExpr(ObjCEncodeExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1757 | void VisitObjCMessageExpr(ObjCMessageExpr *M); |
| 1758 | void VisitOverloadExpr(OverloadExpr *E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1759 | void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1760 | void VisitStmt(Stmt *S); |
| 1761 | void VisitSwitchStmt(SwitchStmt *S); |
| 1762 | void VisitWhileStmt(WhileStmt *W); |
Ted Kremenek | 2939b6f | 2010-11-17 00:50:50 +0000 | [diff] [blame] | 1763 | void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 1764 | void VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E); |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 1765 | void VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E); |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 1766 | void VisitExpressionTraitExpr(ExpressionTraitExpr *E); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1767 | void VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U); |
Ted Kremenek | 9d3bf79 | 2010-11-17 00:50:43 +0000 | [diff] [blame] | 1768 | void VisitVAArgExpr(VAArgExpr *E); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 1769 | void VisitSizeOfPackExpr(SizeOfPackExpr *E); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1770 | void VisitPseudoObjectExpr(PseudoObjectExpr *E); |
| 1771 | void VisitOpaqueValueExpr(OpaqueValueExpr *E); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1772 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1773 | private: |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1774 | void AddDeclarationNameInfo(Stmt *S); |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1775 | void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier); |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1776 | void AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1777 | void AddMemberRef(FieldDecl *D, SourceLocation L); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1778 | void AddStmt(Stmt *S); |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1779 | void AddDecl(Decl *D, bool isFirst = true); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1780 | void AddTypeLoc(TypeSourceInfo *TI); |
| 1781 | void EnqueueChildren(Stmt *S); |
| 1782 | }; |
| 1783 | } // end anonyous namespace |
| 1784 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1785 | void EnqueueVisitor::AddDeclarationNameInfo(Stmt *S) { |
| 1786 | // 'S' should always be non-null, since it comes from the |
| 1787 | // statement we are visiting. |
| 1788 | WL.push_back(DeclarationNameInfoVisit(S, Parent)); |
| 1789 | } |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1790 | |
| 1791 | void |
| 1792 | EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 1793 | if (Qualifier) |
| 1794 | WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent)); |
| 1795 | } |
| 1796 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1797 | void EnqueueVisitor::AddStmt(Stmt *S) { |
| 1798 | if (S) |
| 1799 | WL.push_back(StmtVisit(S, Parent)); |
| 1800 | } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1801 | void EnqueueVisitor::AddDecl(Decl *D, bool isFirst) { |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1802 | if (D) |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1803 | WL.push_back(DeclVisit(D, Parent, isFirst)); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1804 | } |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1805 | void EnqueueVisitor:: |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1806 | AddExplicitTemplateArgs(const ASTTemplateArgumentListInfo *A) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1807 | if (A) |
| 1808 | WL.push_back(ExplicitTemplateArgsVisit( |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 1809 | const_cast<ASTTemplateArgumentListInfo*>(A), Parent)); |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1810 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1811 | void EnqueueVisitor::AddMemberRef(FieldDecl *D, SourceLocation L) { |
| 1812 | if (D) |
| 1813 | WL.push_back(MemberRefVisit(D, L, Parent)); |
| 1814 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1815 | void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) { |
| 1816 | if (TI) |
| 1817 | WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent)); |
| 1818 | } |
| 1819 | void EnqueueVisitor::EnqueueChildren(Stmt *S) { |
Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 1820 | unsigned size = WL.size(); |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1821 | for (Stmt::child_range Child = S->children(); Child; ++Child) { |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1822 | AddStmt(*Child); |
Ted Kremenek | a6b7043 | 2010-11-12 21:34:09 +0000 | [diff] [blame] | 1823 | } |
| 1824 | if (size == WL.size()) |
| 1825 | return; |
| 1826 | // Now reverse the entries we just added. This will match the DFS |
| 1827 | // ordering performed by the worklist. |
| 1828 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 1829 | std::reverse(I, E); |
| 1830 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1831 | void EnqueueVisitor::VisitAddrLabelExpr(AddrLabelExpr *E) { |
| 1832 | WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent)); |
| 1833 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1834 | void EnqueueVisitor::VisitBlockExpr(BlockExpr *B) { |
| 1835 | AddDecl(B->getBlockDecl()); |
| 1836 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1837 | void EnqueueVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 1838 | EnqueueChildren(E); |
| 1839 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1840 | } |
Ted Kremenek | 083c7e2 | 2010-11-13 05:38:03 +0000 | [diff] [blame] | 1841 | void EnqueueVisitor::VisitCompoundStmt(CompoundStmt *S) { |
| 1842 | for (CompoundStmt::reverse_body_iterator I = S->body_rbegin(), |
| 1843 | E = S->body_rend(); I != E; ++I) { |
| 1844 | AddStmt(*I); |
| 1845 | } |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1846 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1847 | void EnqueueVisitor:: |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 1848 | VisitMSDependentExistsStmt(MSDependentExistsStmt *S) { |
| 1849 | AddStmt(S->getSubStmt()); |
| 1850 | AddDeclarationNameInfo(S); |
| 1851 | if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc()) |
| 1852 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 1853 | } |
| 1854 | |
| 1855 | void EnqueueVisitor:: |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1856 | VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) { |
| 1857 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
| 1858 | AddDeclarationNameInfo(E); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1859 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 1860 | AddNestedNameSpecifierLoc(QualifierLoc); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1861 | if (!E->isImplicitAccess()) |
| 1862 | AddStmt(E->getBase()); |
| 1863 | } |
Ted Kremenek | 11b8e3e | 2010-11-13 05:55:53 +0000 | [diff] [blame] | 1864 | void EnqueueVisitor::VisitCXXNewExpr(CXXNewExpr *E) { |
| 1865 | // Enqueue the initializer or constructor arguments. |
| 1866 | for (unsigned I = E->getNumConstructorArgs(); I > 0; --I) |
| 1867 | AddStmt(E->getConstructorArg(I-1)); |
| 1868 | // Enqueue the array size, if any. |
| 1869 | AddStmt(E->getArraySize()); |
| 1870 | // Enqueue the allocated type. |
| 1871 | AddTypeLoc(E->getAllocatedTypeSourceInfo()); |
| 1872 | // Enqueue the placement arguments. |
| 1873 | for (unsigned I = E->getNumPlacementArgs(); I > 0; --I) |
| 1874 | AddStmt(E->getPlacementArg(I-1)); |
| 1875 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1876 | void EnqueueVisitor::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *CE) { |
Ted Kremenek | 8b8d8c9 | 2010-11-13 05:55:56 +0000 | [diff] [blame] | 1877 | for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I) |
| 1878 | AddStmt(CE->getArg(I-1)); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1879 | AddStmt(CE->getCallee()); |
| 1880 | AddStmt(CE->getArg(0)); |
| 1881 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1882 | void EnqueueVisitor::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { |
| 1883 | // Visit the name of the type being destroyed. |
| 1884 | AddTypeLoc(E->getDestroyedTypeInfo()); |
| 1885 | // Visit the scope type that looks disturbingly like the nested-name-specifier |
| 1886 | // but isn't. |
| 1887 | AddTypeLoc(E->getScopeTypeInfo()); |
| 1888 | // Visit the nested-name-specifier. |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 1889 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 1890 | AddNestedNameSpecifierLoc(QualifierLoc); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1891 | // Visit base expression. |
| 1892 | AddStmt(E->getBase()); |
| 1893 | } |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 1894 | void EnqueueVisitor::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
| 1895 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1896 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 1897 | void EnqueueVisitor::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { |
| 1898 | EnqueueChildren(E); |
| 1899 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1900 | } |
Ted Kremenek | b8dd1ca | 2010-11-17 00:50:41 +0000 | [diff] [blame] | 1901 | void EnqueueVisitor::VisitCXXTypeidExpr(CXXTypeidExpr *E) { |
| 1902 | EnqueueChildren(E); |
| 1903 | if (E->isTypeOperand()) |
| 1904 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 1905 | } |
Ted Kremenek | 55b933a | 2010-11-17 00:50:36 +0000 | [diff] [blame] | 1906 | |
| 1907 | void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr |
| 1908 | *E) { |
| 1909 | EnqueueChildren(E); |
| 1910 | AddTypeLoc(E->getTypeSourceInfo()); |
| 1911 | } |
Ted Kremenek | 1e7e877 | 2010-11-17 00:50:52 +0000 | [diff] [blame] | 1912 | void EnqueueVisitor::VisitCXXUuidofExpr(CXXUuidofExpr *E) { |
| 1913 | EnqueueChildren(E); |
| 1914 | if (E->isTypeOperand()) |
| 1915 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 1916 | } |
Argyrios Kyrtzidis | dcbb2fb | 2011-12-03 03:49:44 +0000 | [diff] [blame] | 1917 | |
| 1918 | void EnqueueVisitor::VisitCXXCatchStmt(CXXCatchStmt *S) { |
| 1919 | EnqueueChildren(S); |
| 1920 | AddDecl(S->getExceptionDecl()); |
| 1921 | } |
| 1922 | |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1923 | void EnqueueVisitor::VisitDeclRefExpr(DeclRefExpr *DR) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 1924 | if (DR->hasExplicitTemplateArgs()) { |
| 1925 | AddExplicitTemplateArgs(&DR->getExplicitTemplateArgs()); |
| 1926 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 1927 | WL.push_back(DeclRefExprParts(DR, Parent)); |
| 1928 | } |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1929 | void EnqueueVisitor::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { |
| 1930 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
| 1931 | AddDeclarationNameInfo(E); |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 1932 | AddNestedNameSpecifierLoc(E->getQualifierLoc()); |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 1933 | } |
Ted Kremenek | 035dc41 | 2010-11-13 00:36:50 +0000 | [diff] [blame] | 1934 | void EnqueueVisitor::VisitDeclStmt(DeclStmt *S) { |
| 1935 | unsigned size = WL.size(); |
| 1936 | bool isFirst = true; |
| 1937 | for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); |
| 1938 | D != DEnd; ++D) { |
| 1939 | AddDecl(*D, isFirst); |
| 1940 | isFirst = false; |
| 1941 | } |
| 1942 | if (size == WL.size()) |
| 1943 | return; |
| 1944 | // Now reverse the entries we just added. This will match the DFS |
| 1945 | // ordering performed by the worklist. |
| 1946 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 1947 | std::reverse(I, E); |
| 1948 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 1949 | void EnqueueVisitor::VisitDesignatedInitExpr(DesignatedInitExpr *E) { |
| 1950 | AddStmt(E->getInit()); |
| 1951 | typedef DesignatedInitExpr::Designator Designator; |
| 1952 | for (DesignatedInitExpr::reverse_designators_iterator |
| 1953 | D = E->designators_rbegin(), DEnd = E->designators_rend(); |
| 1954 | D != DEnd; ++D) { |
| 1955 | if (D->isFieldDesignator()) { |
| 1956 | if (FieldDecl *Field = D->getField()) |
| 1957 | AddMemberRef(Field, D->getFieldLoc()); |
| 1958 | continue; |
| 1959 | } |
| 1960 | if (D->isArrayDesignator()) { |
| 1961 | AddStmt(E->getArrayIndex(*D)); |
| 1962 | continue; |
| 1963 | } |
| 1964 | assert(D->isArrayRangeDesignator() && "Unknown designator kind"); |
| 1965 | AddStmt(E->getArrayRangeEnd(*D)); |
| 1966 | AddStmt(E->getArrayRangeStart(*D)); |
| 1967 | } |
| 1968 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1969 | void EnqueueVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) { |
| 1970 | EnqueueChildren(E); |
| 1971 | AddTypeLoc(E->getTypeInfoAsWritten()); |
| 1972 | } |
| 1973 | void EnqueueVisitor::VisitForStmt(ForStmt *FS) { |
| 1974 | AddStmt(FS->getBody()); |
| 1975 | AddStmt(FS->getInc()); |
| 1976 | AddStmt(FS->getCond()); |
| 1977 | AddDecl(FS->getConditionVariable()); |
| 1978 | AddStmt(FS->getInit()); |
| 1979 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 1980 | void EnqueueVisitor::VisitGotoStmt(GotoStmt *GS) { |
| 1981 | WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent)); |
| 1982 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 1983 | void EnqueueVisitor::VisitIfStmt(IfStmt *If) { |
| 1984 | AddStmt(If->getElse()); |
| 1985 | AddStmt(If->getThen()); |
| 1986 | AddStmt(If->getCond()); |
| 1987 | AddDecl(If->getConditionVariable()); |
| 1988 | } |
| 1989 | void EnqueueVisitor::VisitInitListExpr(InitListExpr *IE) { |
| 1990 | // We care about the syntactic form of the initializer list, only. |
| 1991 | if (InitListExpr *Syntactic = IE->getSyntacticForm()) |
| 1992 | IE = Syntactic; |
| 1993 | EnqueueChildren(IE); |
| 1994 | } |
| 1995 | void EnqueueVisitor::VisitMemberExpr(MemberExpr *M) { |
Douglas Gregor | 89629a7 | 2010-11-17 17:15:08 +0000 | [diff] [blame] | 1996 | WL.push_back(MemberExprParts(M, Parent)); |
| 1997 | |
| 1998 | // If the base of the member access expression is an implicit 'this', don't |
| 1999 | // visit it. |
| 2000 | // FIXME: If we ever want to show these implicit accesses, this will be |
| 2001 | // unfortunate. However, clang_getCursor() relies on this behavior. |
Douglas Gregor | 75e8504 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 2002 | if (!M->isImplicitAccess()) |
| 2003 | AddStmt(M->getBase()); |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2004 | } |
Ted Kremenek | 73d15c4 | 2010-11-13 01:09:29 +0000 | [diff] [blame] | 2005 | void EnqueueVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) { |
| 2006 | AddTypeLoc(E->getEncodedTypeSourceInfo()); |
| 2007 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2008 | void EnqueueVisitor::VisitObjCMessageExpr(ObjCMessageExpr *M) { |
| 2009 | EnqueueChildren(M); |
| 2010 | AddTypeLoc(M->getClassReceiverTypeInfo()); |
| 2011 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2012 | void EnqueueVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) { |
| 2013 | // Visit the components of the offsetof expression. |
| 2014 | for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) { |
| 2015 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 2016 | const OffsetOfNode &Node = E->getComponent(I-1); |
| 2017 | switch (Node.getKind()) { |
| 2018 | case OffsetOfNode::Array: |
| 2019 | AddStmt(E->getIndexExpr(Node.getArrayExprIndex())); |
| 2020 | break; |
| 2021 | case OffsetOfNode::Field: |
Abramo Bagnara | 06dec89 | 2011-03-12 09:45:03 +0000 | [diff] [blame] | 2022 | AddMemberRef(Node.getField(), Node.getSourceRange().getEnd()); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2023 | break; |
| 2024 | case OffsetOfNode::Identifier: |
| 2025 | case OffsetOfNode::Base: |
| 2026 | continue; |
| 2027 | } |
| 2028 | } |
| 2029 | // Visit the type into which we're computing the offset. |
| 2030 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2031 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2032 | void EnqueueVisitor::VisitOverloadExpr(OverloadExpr *E) { |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2033 | AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs()); |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2034 | WL.push_back(OverloadExprParts(E, Parent)); |
| 2035 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2036 | void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr( |
| 2037 | UnaryExprOrTypeTraitExpr *E) { |
Ted Kremenek | 6d0a00d | 2010-11-17 02:18:35 +0000 | [diff] [blame] | 2038 | EnqueueChildren(E); |
| 2039 | if (E->isArgumentType()) |
| 2040 | AddTypeLoc(E->getArgumentTypeInfo()); |
| 2041 | } |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2042 | void EnqueueVisitor::VisitStmt(Stmt *S) { |
| 2043 | EnqueueChildren(S); |
| 2044 | } |
| 2045 | void EnqueueVisitor::VisitSwitchStmt(SwitchStmt *S) { |
| 2046 | AddStmt(S->getBody()); |
| 2047 | AddStmt(S->getCond()); |
| 2048 | AddDecl(S->getConditionVariable()); |
| 2049 | } |
Ted Kremenek | fafa75a | 2010-11-17 00:50:39 +0000 | [diff] [blame] | 2050 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2051 | void EnqueueVisitor::VisitWhileStmt(WhileStmt *W) { |
| 2052 | AddStmt(W->getBody()); |
| 2053 | AddStmt(W->getCond()); |
| 2054 | AddDecl(W->getConditionVariable()); |
| 2055 | } |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2056 | |
Ted Kremenek | 2939b6f | 2010-11-17 00:50:50 +0000 | [diff] [blame] | 2057 | void EnqueueVisitor::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { |
| 2058 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2059 | } |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2060 | |
| 2061 | void EnqueueVisitor::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) { |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2062 | AddTypeLoc(E->getRhsTypeSourceInfo()); |
Francois Pichet | 0a03a3f | 2010-12-08 09:11:05 +0000 | [diff] [blame] | 2063 | AddTypeLoc(E->getLhsTypeSourceInfo()); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2066 | void EnqueueVisitor::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) { |
| 2067 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2068 | } |
| 2069 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 2070 | void EnqueueVisitor::VisitExpressionTraitExpr(ExpressionTraitExpr *E) { |
| 2071 | EnqueueChildren(E); |
| 2072 | } |
| 2073 | |
Ted Kremenek | 28a7194 | 2010-11-13 00:36:47 +0000 | [diff] [blame] | 2074 | void EnqueueVisitor::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U) { |
| 2075 | VisitOverloadExpr(U); |
| 2076 | if (!U->isImplicitAccess()) |
| 2077 | AddStmt(U->getBase()); |
| 2078 | } |
Ted Kremenek | 9d3bf79 | 2010-11-17 00:50:43 +0000 | [diff] [blame] | 2079 | void EnqueueVisitor::VisitVAArgExpr(VAArgExpr *E) { |
| 2080 | AddStmt(E->getSubExpr()); |
| 2081 | AddTypeLoc(E->getWrittenTypeInfo()); |
| 2082 | } |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2083 | void EnqueueVisitor::VisitSizeOfPackExpr(SizeOfPackExpr *E) { |
| 2084 | WL.push_back(SizeOfPackExprParts(E, Parent)); |
| 2085 | } |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2086 | void EnqueueVisitor::VisitOpaqueValueExpr(OpaqueValueExpr *E) { |
| 2087 | // If the opaque value has a source expression, just transparently |
| 2088 | // visit that. This is useful for (e.g.) pseudo-object expressions. |
| 2089 | if (Expr *SourceExpr = E->getSourceExpr()) |
| 2090 | return Visit(SourceExpr); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2091 | } |
| 2092 | void EnqueueVisitor::VisitPseudoObjectExpr(PseudoObjectExpr *E) { |
| 2093 | // Treat the expression like its syntactic form. |
| 2094 | Visit(E->getSyntacticForm()); |
| 2095 | } |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2096 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2097 | void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, Stmt *S) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2098 | EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S); |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { |
| 2102 | if (RegionOfInterest.isValid()) { |
| 2103 | SourceRange Range = getRawCursorExtent(C); |
| 2104 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 2105 | return false; |
| 2106 | } |
| 2107 | return true; |
| 2108 | } |
| 2109 | |
| 2110 | bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { |
| 2111 | while (!WL.empty()) { |
| 2112 | // Dequeue the worklist item. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2113 | VisitorJob LI = WL.back(); |
| 2114 | WL.pop_back(); |
| 2115 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2116 | // Set the Parent field, then back to its old value once we're done. |
| 2117 | SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); |
| 2118 | |
| 2119 | switch (LI.getKind()) { |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2120 | case VisitorJob::DeclVisitKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2121 | Decl *D = cast<DeclVisit>(&LI)->get(); |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2122 | if (!D) |
| 2123 | continue; |
| 2124 | |
| 2125 | // For now, perform default visitation for Decls. |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2126 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest, |
| 2127 | cast<DeclVisit>(&LI)->isFirst()))) |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2128 | return true; |
| 2129 | |
| 2130 | continue; |
| 2131 | } |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2132 | case VisitorJob::ExplicitTemplateArgsVisitKind: { |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 2133 | const ASTTemplateArgumentListInfo *ArgList = |
Ted Kremenek | 60608ec | 2010-11-17 00:50:47 +0000 | [diff] [blame] | 2134 | cast<ExplicitTemplateArgsVisit>(&LI)->get(); |
| 2135 | for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(), |
| 2136 | *ArgEnd = Arg + ArgList->NumTemplateArgs; |
| 2137 | Arg != ArgEnd; ++Arg) { |
| 2138 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2139 | return true; |
| 2140 | } |
| 2141 | continue; |
| 2142 | } |
Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 2143 | case VisitorJob::TypeLocVisitKind: { |
| 2144 | // Perform default visitation for TypeLocs. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2145 | if (Visit(cast<TypeLocVisit>(&LI)->get())) |
Ted Kremenek | cdb4caf | 2010-11-12 21:34:12 +0000 | [diff] [blame] | 2146 | return true; |
| 2147 | continue; |
| 2148 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2149 | case VisitorJob::LabelRefVisitKind: { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2150 | LabelDecl *LS = cast<LabelRefVisit>(&LI)->get(); |
Ted Kremenek | e745501 | 2011-02-23 04:54:51 +0000 | [diff] [blame] | 2151 | if (LabelStmt *stmt = LS->getStmt()) { |
| 2152 | if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(), |
| 2153 | TU))) { |
| 2154 | return true; |
| 2155 | } |
| 2156 | } |
Ted Kremenek | ae1fd6f | 2010-11-17 00:50:45 +0000 | [diff] [blame] | 2157 | continue; |
| 2158 | } |
Ted Kremenek | 47695c8 | 2011-08-18 22:25:21 +0000 | [diff] [blame] | 2159 | |
Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 2160 | case VisitorJob::NestedNameSpecifierLocVisitKind: { |
| 2161 | NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI); |
| 2162 | if (VisitNestedNameSpecifierLoc(V->get())) |
| 2163 | return true; |
| 2164 | continue; |
| 2165 | } |
| 2166 | |
Ted Kremenek | f64d803 | 2010-11-18 00:02:32 +0000 | [diff] [blame] | 2167 | case VisitorJob::DeclarationNameInfoVisitKind: { |
| 2168 | if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI) |
| 2169 | ->get())) |
| 2170 | return true; |
| 2171 | continue; |
| 2172 | } |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2173 | case VisitorJob::MemberRefVisitKind: { |
| 2174 | MemberRefVisit *V = cast<MemberRefVisit>(&LI); |
| 2175 | if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU))) |
| 2176 | return true; |
| 2177 | continue; |
| 2178 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2179 | case VisitorJob::StmtVisitKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2180 | Stmt *S = cast<StmtVisit>(&LI)->get(); |
Ted Kremenek | 8c269ac | 2010-11-11 23:11:43 +0000 | [diff] [blame] | 2181 | if (!S) |
| 2182 | continue; |
| 2183 | |
Ted Kremenek | f110745 | 2010-11-12 18:26:56 +0000 | [diff] [blame] | 2184 | // Update the current cursor. |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2185 | CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest); |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2186 | if (!IsInRegionOfInterest(Cursor)) |
| 2187 | continue; |
| 2188 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 2189 | case CXChildVisit_Break: return true; |
| 2190 | case CXChildVisit_Continue: break; |
| 2191 | case CXChildVisit_Recurse: |
| 2192 | EnqueueWorkList(WL, S); |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2193 | break; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2194 | } |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2195 | continue; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2196 | } |
| 2197 | case VisitorJob::MemberExprPartsKind: { |
| 2198 | // Handle the other pieces in the MemberExpr besides the base. |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2199 | MemberExpr *M = cast<MemberExprParts>(&LI)->get(); |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2200 | |
| 2201 | // Visit the nested-name-specifier |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2202 | if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc()) |
| 2203 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2204 | return true; |
| 2205 | |
| 2206 | // Visit the declaration name. |
| 2207 | if (VisitDeclarationNameInfo(M->getMemberNameInfo())) |
| 2208 | return true; |
| 2209 | |
| 2210 | // Visit the explicitly-specified template arguments, if any. |
| 2211 | if (M->hasExplicitTemplateArgs()) { |
| 2212 | for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), |
| 2213 | *ArgEnd = Arg + M->getNumTemplateArgs(); |
| 2214 | Arg != ArgEnd; ++Arg) { |
| 2215 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2216 | return true; |
| 2217 | } |
| 2218 | } |
| 2219 | continue; |
| 2220 | } |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2221 | case VisitorJob::DeclRefExprPartsKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2222 | DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get(); |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2223 | // Visit nested-name-specifier, if present. |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2224 | if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc()) |
| 2225 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2226 | return true; |
| 2227 | // Visit declaration name. |
| 2228 | if (VisitDeclarationNameInfo(DR->getNameInfo())) |
| 2229 | return true; |
Ted Kremenek | e4979cc | 2010-11-13 00:58:18 +0000 | [diff] [blame] | 2230 | continue; |
| 2231 | } |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2232 | case VisitorJob::OverloadExprPartsKind: { |
Ted Kremenek | 82f3c50 | 2010-11-15 22:23:26 +0000 | [diff] [blame] | 2233 | OverloadExpr *O = cast<OverloadExprParts>(&LI)->get(); |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2234 | // Visit the nested-name-specifier. |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2235 | if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc()) |
| 2236 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2237 | return true; |
| 2238 | // Visit the declaration name. |
| 2239 | if (VisitDeclarationNameInfo(O->getNameInfo())) |
| 2240 | return true; |
| 2241 | // Visit the overloaded declaration reference. |
| 2242 | if (Visit(MakeCursorOverloadedDeclRef(O, TU))) |
| 2243 | return true; |
Ted Kremenek | 6045878 | 2010-11-12 21:34:16 +0000 | [diff] [blame] | 2244 | continue; |
| 2245 | } |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2246 | case VisitorJob::SizeOfPackExprPartsKind: { |
| 2247 | SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get(); |
| 2248 | NamedDecl *Pack = E->getPack(); |
| 2249 | if (isa<TemplateTypeParmDecl>(Pack)) { |
| 2250 | if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack), |
| 2251 | E->getPackLoc(), TU))) |
| 2252 | return true; |
| 2253 | |
| 2254 | continue; |
| 2255 | } |
| 2256 | |
| 2257 | if (isa<TemplateTemplateParmDecl>(Pack)) { |
| 2258 | if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack), |
| 2259 | E->getPackLoc(), TU))) |
| 2260 | return true; |
| 2261 | |
| 2262 | continue; |
| 2263 | } |
| 2264 | |
| 2265 | // Non-type template parameter packs and function parameter packs are |
| 2266 | // treated like DeclRefExpr cursors. |
| 2267 | continue; |
| 2268 | } |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2269 | } |
| 2270 | } |
| 2271 | return false; |
| 2272 | } |
| 2273 | |
Ted Kremenek | cdba659 | 2010-11-18 00:42:18 +0000 | [diff] [blame] | 2274 | bool CursorVisitor::Visit(Stmt *S) { |
Ted Kremenek | d1ded66 | 2010-11-15 23:31:32 +0000 | [diff] [blame] | 2275 | VisitorWorkList *WL = 0; |
| 2276 | if (!WorkListFreeList.empty()) { |
| 2277 | WL = WorkListFreeList.back(); |
| 2278 | WL->clear(); |
| 2279 | WorkListFreeList.pop_back(); |
| 2280 | } |
| 2281 | else { |
| 2282 | WL = new VisitorWorkList(); |
| 2283 | WorkListCache.push_back(WL); |
| 2284 | } |
| 2285 | EnqueueWorkList(*WL, S); |
| 2286 | bool result = RunVisitorWorkList(*WL); |
| 2287 | WorkListFreeList.push_back(WL); |
| 2288 | return result; |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
Francois Pichet | 48a8d14 | 2011-07-25 22:00:44 +0000 | [diff] [blame] | 2291 | namespace { |
| 2292 | typedef llvm::SmallVector<SourceRange, 4> RefNamePieces; |
| 2293 | RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr, |
| 2294 | const DeclarationNameInfo &NI, |
| 2295 | const SourceRange &QLoc, |
Argyrios Kyrtzidis | b0c3e09 | 2011-09-22 20:07:03 +0000 | [diff] [blame] | 2296 | const ASTTemplateArgumentListInfo *TemplateArgs = 0){ |
Francois Pichet | 48a8d14 | 2011-07-25 22:00:44 +0000 | [diff] [blame] | 2297 | const bool WantQualifier = NameFlags & CXNameRange_WantQualifier; |
| 2298 | const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs; |
| 2299 | const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece; |
| 2300 | |
| 2301 | const DeclarationName::NameKind Kind = NI.getName().getNameKind(); |
| 2302 | |
| 2303 | RefNamePieces Pieces; |
| 2304 | |
| 2305 | if (WantQualifier && QLoc.isValid()) |
| 2306 | Pieces.push_back(QLoc); |
| 2307 | |
| 2308 | if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr) |
| 2309 | Pieces.push_back(NI.getLoc()); |
| 2310 | |
| 2311 | if (WantTemplateArgs && TemplateArgs) |
| 2312 | Pieces.push_back(SourceRange(TemplateArgs->LAngleLoc, |
| 2313 | TemplateArgs->RAngleLoc)); |
| 2314 | |
| 2315 | if (Kind == DeclarationName::CXXOperatorName) { |
| 2316 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 2317 | NI.getInfo().CXXOperatorName.BeginOpNameLoc)); |
| 2318 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 2319 | NI.getInfo().CXXOperatorName.EndOpNameLoc)); |
| 2320 | } |
| 2321 | |
| 2322 | if (WantSinglePiece) { |
| 2323 | SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd()); |
| 2324 | Pieces.clear(); |
| 2325 | Pieces.push_back(R); |
| 2326 | } |
| 2327 | |
| 2328 | return Pieces; |
| 2329 | } |
| 2330 | } |
| 2331 | |
Ted Kremenek | c0e1d92 | 2010-11-11 08:05:18 +0000 | [diff] [blame] | 2332 | //===----------------------------------------------------------------------===// |
| 2333 | // Misc. API hooks. |
| 2334 | //===----------------------------------------------------------------------===// |
| 2335 | |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2336 | static llvm::sys::Mutex EnableMultithreadingMutex; |
| 2337 | static bool EnabledMultithreading; |
| 2338 | |
Argyrios Kyrtzidis | fa39f5b | 2011-12-15 04:52:41 +0000 | [diff] [blame^] | 2339 | static void fatal_error_handler(void *user_data, const std::string& reason) { |
| 2340 | llvm::SmallString<64> Buffer; |
| 2341 | llvm::raw_svector_ostream OS(Buffer); |
| 2342 | OS << "LIBCLANG FATAL ERROR: " << reason << "\n"; |
| 2343 | StringRef MessageStr = OS.str(); |
| 2344 | // Write the result out to stderr avoiding errs() because raw_ostreams can |
| 2345 | // call report_fatal_error. |
| 2346 | ::write(2, MessageStr.data(), MessageStr.size()); |
| 2347 | ::abort(); |
| 2348 | } |
| 2349 | |
Benjamin Kramer | 5e4bc59 | 2009-10-18 16:11:04 +0000 | [diff] [blame] | 2350 | extern "C" { |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2351 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 2352 | int displayDiagnostics) { |
Daniel Dunbar | 48615ff | 2010-10-08 19:30:33 +0000 | [diff] [blame] | 2353 | // Disable pretty stack trace functionality, which will otherwise be a very |
| 2354 | // poor citizen of the world and set up all sorts of signal handlers. |
| 2355 | llvm::DisablePrettyStackTrace = true; |
| 2356 | |
Daniel Dunbar | c7df4f3 | 2010-08-18 18:43:14 +0000 | [diff] [blame] | 2357 | // We use crash recovery to make some of our APIs more reliable, implicitly |
| 2358 | // enable it. |
| 2359 | llvm::CrashRecoveryContext::Enable(); |
| 2360 | |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2361 | // Enable support for multithreading in LLVM. |
| 2362 | { |
| 2363 | llvm::sys::ScopedLock L(EnableMultithreadingMutex); |
| 2364 | if (!EnabledMultithreading) { |
Argyrios Kyrtzidis | fa39f5b | 2011-12-15 04:52:41 +0000 | [diff] [blame^] | 2365 | llvm::install_fatal_error_handler(fatal_error_handler, 0); |
Douglas Gregor | 8c8d541 | 2010-09-24 21:18:36 +0000 | [diff] [blame] | 2366 | llvm::llvm_start_multithreaded(); |
| 2367 | EnabledMultithreading = true; |
| 2368 | } |
| 2369 | } |
| 2370 | |
Douglas Gregor | a030b7c | 2010-01-22 20:35:53 +0000 | [diff] [blame] | 2371 | CIndexer *CIdxr = new CIndexer(); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2372 | if (excludeDeclarationsFromPCH) |
| 2373 | CIdxr->setOnlyLocalDecls(); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2374 | if (displayDiagnostics) |
| 2375 | CIdxr->setDisplayDiagnostics(); |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2376 | return CIdxr; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2379 | void clang_disposeIndex(CXIndex CIdx) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2380 | if (CIdx) |
| 2381 | delete static_cast<CIndexer *>(CIdx); |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
Ted Kremenek | d2427dd | 2011-03-18 23:05:39 +0000 | [diff] [blame] | 2384 | void clang_toggleCrashRecovery(unsigned isEnabled) { |
| 2385 | if (isEnabled) |
| 2386 | llvm::CrashRecoveryContext::Enable(); |
| 2387 | else |
| 2388 | llvm::CrashRecoveryContext::Disable(); |
| 2389 | } |
| 2390 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2391 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2392 | const char *ast_filename) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2393 | if (!CIdx) |
| 2394 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2395 | |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 2396 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 2397 | FileSystemOptions FileSystemOpts; |
| 2398 | FileSystemOpts.WorkingDir = CXXIdx->getWorkingDirectory(); |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2399 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2400 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags; |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2401 | ASTUnit *TU = ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2402 | CXXIdx->getOnlyLocalDecls(), |
| 2403 | 0, 0, true); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2404 | return MakeCXTranslationUnit(TU); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 2407 | unsigned clang_defaultEditingTranslationUnitOptions() { |
Douglas Gregor | 2a2c50b | 2010-09-27 05:49:58 +0000 | [diff] [blame] | 2408 | return CXTranslationUnit_PrecompiledPreamble | |
Douglas Gregor | b5af843 | 2011-08-25 22:54:01 +0000 | [diff] [blame] | 2409 | CXTranslationUnit_CacheCompletionResults; |
Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2412 | CXTranslationUnit |
| 2413 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 2414 | const char *source_filename, |
| 2415 | int num_command_line_args, |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2416 | const char * const *command_line_args, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2417 | unsigned num_unsaved_files, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2418 | struct CXUnsavedFile *unsaved_files) { |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 2419 | unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord | |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2420 | CXTranslationUnit_NestedMacroExpansions; |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2421 | return clang_parseTranslationUnit(CIdx, source_filename, |
| 2422 | command_line_args, num_command_line_args, |
| 2423 | unsaved_files, num_unsaved_files, |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 2424 | Options); |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2425 | } |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2426 | |
| 2427 | struct ParseTranslationUnitInfo { |
| 2428 | CXIndex CIdx; |
| 2429 | const char *source_filename; |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2430 | const char *const *command_line_args; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2431 | int num_command_line_args; |
| 2432 | struct CXUnsavedFile *unsaved_files; |
| 2433 | unsigned num_unsaved_files; |
| 2434 | unsigned options; |
| 2435 | CXTranslationUnit result; |
| 2436 | }; |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2437 | static void clang_parseTranslationUnit_Impl(void *UserData) { |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2438 | ParseTranslationUnitInfo *PTUI = |
| 2439 | static_cast<ParseTranslationUnitInfo*>(UserData); |
| 2440 | CXIndex CIdx = PTUI->CIdx; |
| 2441 | const char *source_filename = PTUI->source_filename; |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2442 | const char * const *command_line_args = PTUI->command_line_args; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2443 | int num_command_line_args = PTUI->num_command_line_args; |
| 2444 | struct CXUnsavedFile *unsaved_files = PTUI->unsaved_files; |
| 2445 | unsigned num_unsaved_files = PTUI->num_unsaved_files; |
| 2446 | unsigned options = PTUI->options; |
| 2447 | PTUI->result = 0; |
Douglas Gregor | 5a43021 | 2010-07-21 18:52:53 +0000 | [diff] [blame] | 2448 | |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2449 | if (!CIdx) |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2450 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2451 | |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2452 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 2453 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2454 | bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 2455 | // FIXME: Add a flag for modules. |
| 2456 | TranslationUnitKind TUKind |
| 2457 | = (options & CXTranslationUnit_Incomplete)? TU_Prefix : TU_Complete; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2458 | bool CacheCodeCompetionResults |
| 2459 | = options & CXTranslationUnit_CacheCompletionResults; |
| 2460 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2461 | // Configure the diagnostics. |
| 2462 | DiagnosticOptions DiagOpts; |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2463 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2464 | Diags(CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args, |
| 2465 | command_line_args)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2466 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2467 | // Recover resources if we crash before exiting this function. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2468 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 2469 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2470 | DiagCleanup(Diags.getPtr()); |
| 2471 | |
| 2472 | llvm::OwningPtr<std::vector<ASTUnit::RemappedFile> > |
| 2473 | RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); |
| 2474 | |
| 2475 | // Recover resources if we crash before exiting this function. |
| 2476 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 2477 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 2478 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2479 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2480 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2481 | const llvm::MemoryBuffer *Buffer |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 2482 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2483 | RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, |
| 2484 | Buffer)); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 2485 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2486 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2487 | llvm::OwningPtr<std::vector<const char *> > |
| 2488 | Args(new std::vector<const char*>()); |
| 2489 | |
| 2490 | // Recover resources if we crash before exiting this method. |
| 2491 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > |
| 2492 | ArgsCleanup(Args.get()); |
| 2493 | |
Douglas Gregor | 52ddc5d | 2010-07-09 18:39:07 +0000 | [diff] [blame] | 2494 | // Since the Clang C library is primarily used by batch tools dealing with |
| 2495 | // (often very broken) source code, where spell-checking can have a |
| 2496 | // significant negative impact on performance (particularly when |
| 2497 | // precompiled headers are involved), we disable it by default. |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2498 | // Only do this if we haven't found a spell-checking-related argument. |
| 2499 | bool FoundSpellCheckingArgument = false; |
| 2500 | for (int I = 0; I != num_command_line_args; ++I) { |
| 2501 | if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || |
| 2502 | strcmp(command_line_args[I], "-fspell-checking") == 0) { |
| 2503 | FoundSpellCheckingArgument = true; |
| 2504 | break; |
Steve Naroff | e56b4ba | 2009-10-20 14:46:24 +0000 | [diff] [blame] | 2505 | } |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2506 | } |
| 2507 | if (!FoundSpellCheckingArgument) |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2508 | Args->push_back("-fno-spell-checking"); |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2509 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2510 | Args->insert(Args->end(), command_line_args, |
| 2511 | command_line_args + num_command_line_args); |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 2512 | |
Argyrios Kyrtzidis | c842955 | 2011-03-20 18:17:52 +0000 | [diff] [blame] | 2513 | // The 'source_filename' argument is optional. If the caller does not |
| 2514 | // specify it then it is assumed that the source file is specified |
| 2515 | // in the actual argument list. |
| 2516 | // Put the source file after command_line_args otherwise if '-x' flag is |
| 2517 | // present it will be unused. |
| 2518 | if (source_filename) |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2519 | Args->push_back(source_filename); |
Argyrios Kyrtzidis | c842955 | 2011-03-20 18:17:52 +0000 | [diff] [blame] | 2520 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2521 | // Do we need the detailed preprocessing record? |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2522 | bool NestedMacroExpansions = false; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2523 | if (options & CXTranslationUnit_DetailedPreprocessingRecord) { |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2524 | Args->push_back("-Xclang"); |
| 2525 | Args->push_back("-detailed-preprocessing-record"); |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2526 | NestedMacroExpansions |
| 2527 | = (options & CXTranslationUnit_NestedMacroExpansions); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
Argyrios Kyrtzidis | 026f691 | 2010-11-18 21:47:04 +0000 | [diff] [blame] | 2530 | unsigned NumErrors = Diags->getClient()->getNumErrors(); |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2531 | llvm::OwningPtr<ASTUnit> Unit( |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2532 | ASTUnit::LoadFromCommandLine(Args->size() ? &(*Args)[0] : 0 |
| 2533 | /* vector::data() not portable */, |
| 2534 | Args->size() ? (&(*Args)[0] + Args->size()) :0, |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2535 | Diags, |
| 2536 | CXXIdx->getClangResourcesPath(), |
| 2537 | CXXIdx->getOnlyLocalDecls(), |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 2538 | /*CaptureDiagnostics=*/true, |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2539 | RemappedFiles->size() ? &(*RemappedFiles)[0]:0, |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2540 | RemappedFiles->size(), |
Argyrios Kyrtzidis | 299a4a9 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 2541 | /*RemappedFilesKeepOriginalName=*/true, |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2542 | PrecompilePreamble, |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 2543 | TUKind, |
Douglas Gregor | 99ba202 | 2010-10-27 17:24:53 +0000 | [diff] [blame] | 2544 | CacheCodeCompetionResults, |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 2545 | NestedMacroExpansions)); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2546 | |
Argyrios Kyrtzidis | 026f691 | 2010-11-18 21:47:04 +0000 | [diff] [blame] | 2547 | if (NumErrors != Diags->getClient()->getNumErrors()) { |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2548 | // Make sure to check that 'Unit' is non-NULL. |
| 2549 | if (CXXIdx->getDisplayDiagnostics() && Unit.get()) { |
| 2550 | for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), |
| 2551 | DEnd = Unit->stored_diag_end(); |
| 2552 | D != DEnd; ++D) { |
| 2553 | CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions()); |
| 2554 | CXString Msg = clang_formatDiagnostic(&Diag, |
| 2555 | clang_defaultDiagnosticDisplayOptions()); |
| 2556 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 2557 | clang_disposeString(Msg); |
| 2558 | } |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 2559 | #ifdef LLVM_ON_WIN32 |
Douglas Gregor | b10daed | 2010-10-11 16:52:23 +0000 | [diff] [blame] | 2560 | // On Windows, force a flush, since there may be multiple copies of |
| 2561 | // stderr and stdout in the file system, all with different buffers |
| 2562 | // but writing to the same device. |
| 2563 | fflush(stderr); |
| 2564 | #endif |
| 2565 | } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2566 | } |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 2567 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2568 | PTUI->result = MakeCXTranslationUnit(Unit.take()); |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2569 | } |
| 2570 | CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx, |
| 2571 | const char *source_filename, |
Douglas Gregor | 2ef6944 | 2010-09-01 16:43:19 +0000 | [diff] [blame] | 2572 | const char * const *command_line_args, |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2573 | int num_command_line_args, |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2574 | struct CXUnsavedFile *unsaved_files, |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2575 | unsigned num_unsaved_files, |
| 2576 | unsigned options) { |
| 2577 | ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args, |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2578 | num_command_line_args, unsaved_files, |
| 2579 | num_unsaved_files, options, 0 }; |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2580 | llvm::CrashRecoveryContext CRC; |
| 2581 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 2582 | if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) { |
Daniel Dunbar | 60a4543 | 2010-08-23 22:35:34 +0000 | [diff] [blame] | 2583 | fprintf(stderr, "libclang: crash detected during parsing: {\n"); |
| 2584 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 2585 | fprintf(stderr, " 'command_line_args' : ["); |
| 2586 | for (int i = 0; i != num_command_line_args; ++i) { |
| 2587 | if (i) |
| 2588 | fprintf(stderr, ", "); |
| 2589 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 2590 | } |
| 2591 | fprintf(stderr, "],\n"); |
| 2592 | fprintf(stderr, " 'unsaved_files' : ["); |
| 2593 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 2594 | if (i) |
| 2595 | fprintf(stderr, ", "); |
| 2596 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 2597 | unsaved_files[i].Length); |
| 2598 | } |
| 2599 | fprintf(stderr, "],\n"); |
| 2600 | fprintf(stderr, " 'options' : %d,\n", options); |
| 2601 | fprintf(stderr, "}\n"); |
| 2602 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2603 | return 0; |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2604 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
| 2605 | PrintLibclangResourceUsage(PTUI.result); |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2606 | } |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2607 | |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2608 | return PTUI.result; |
Steve Naroff | 5b7d8e2 | 2009-10-15 20:04:39 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
Douglas Gregor | 1999844 | 2010-08-13 15:35:05 +0000 | [diff] [blame] | 2611 | unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { |
| 2612 | return CXSaveTranslationUnit_None; |
| 2613 | } |
| 2614 | |
| 2615 | int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, |
| 2616 | unsigned options) { |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2617 | if (!TU) |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 2618 | return CXSaveError_InvalidTU; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2619 | |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 2620 | CXSaveError result = static_cast<ASTUnit *>(TU->TUData)->Save(FileName); |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2621 | if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 2622 | PrintLibclangResourceUsage(TU); |
| 2623 | return result; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2624 | } |
Daniel Dunbar | 19ffd49 | 2010-08-18 18:43:17 +0000 | [diff] [blame] | 2625 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2626 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2627 | if (CTUnit) { |
| 2628 | // If the translation unit has been marked as unsafe to free, just discard |
| 2629 | // it. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2630 | if (static_cast<ASTUnit *>(CTUnit->TUData)->isUnsafeToFree()) |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2631 | return; |
| 2632 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2633 | delete static_cast<ASTUnit *>(CTUnit->TUData); |
| 2634 | disposeCXStringPool(CTUnit->StringPool); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 2635 | delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2636 | delete CTUnit; |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2637 | } |
Steve Naroff | 2bd6b9f | 2009-09-17 18:33:27 +0000 | [diff] [blame] | 2638 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 2639 | |
Douglas Gregor | e1e13bf | 2010-08-11 15:58:42 +0000 | [diff] [blame] | 2640 | unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { |
| 2641 | return CXReparse_None; |
| 2642 | } |
| 2643 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2644 | struct ReparseTranslationUnitInfo { |
| 2645 | CXTranslationUnit TU; |
| 2646 | unsigned num_unsaved_files; |
| 2647 | struct CXUnsavedFile *unsaved_files; |
| 2648 | unsigned options; |
| 2649 | int result; |
| 2650 | }; |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2651 | |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2652 | static void clang_reparseTranslationUnit_Impl(void *UserData) { |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2653 | ReparseTranslationUnitInfo *RTUI = |
| 2654 | static_cast<ReparseTranslationUnitInfo*>(UserData); |
| 2655 | CXTranslationUnit TU = RTUI->TU; |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 2656 | |
| 2657 | // Reset the associated diagnostics. |
| 2658 | delete static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics); |
| 2659 | TU->Diagnostics = 0; |
| 2660 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2661 | unsigned num_unsaved_files = RTUI->num_unsaved_files; |
| 2662 | struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files; |
| 2663 | unsigned options = RTUI->options; |
| 2664 | (void) options; |
| 2665 | RTUI->result = 1; |
| 2666 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2667 | if (!TU) |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2668 | return; |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2669 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2670 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2671 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2672 | |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2673 | llvm::OwningPtr<std::vector<ASTUnit::RemappedFile> > |
| 2674 | RemappedFiles(new std::vector<ASTUnit::RemappedFile>()); |
| 2675 | |
| 2676 | // Recover resources if we crash before exiting this function. |
| 2677 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 2678 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 2679 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2680 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2681 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2682 | const llvm::MemoryBuffer *Buffer |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2683 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
Ted Kremenek | 25a11e1 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2684 | RemappedFiles->push_back(std::make_pair(unsaved_files[I].Filename, |
| 2685 | Buffer)); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
Ted Kremenek | 4ee9926 | 2011-03-22 20:16:19 +0000 | [diff] [blame] | 2688 | if (!CXXUnit->Reparse(RemappedFiles->size() ? &(*RemappedFiles)[0] : 0, |
| 2689 | RemappedFiles->size())) |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2690 | RTUI->result = 0; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2691 | } |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 2692 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2693 | int clang_reparseTranslationUnit(CXTranslationUnit TU, |
| 2694 | unsigned num_unsaved_files, |
| 2695 | struct CXUnsavedFile *unsaved_files, |
| 2696 | unsigned options) { |
| 2697 | ReparseTranslationUnitInfo RTUI = { TU, num_unsaved_files, unsaved_files, |
| 2698 | options, 0 }; |
Argyrios Kyrtzidis | 8c4b47e | 2011-10-28 22:54:33 +0000 | [diff] [blame] | 2699 | |
Argyrios Kyrtzidis | e7de9b4 | 2011-10-29 19:32:39 +0000 | [diff] [blame] | 2700 | if (getenv("LIBCLANG_NOTHREADS")) { |
Argyrios Kyrtzidis | 8c4b47e | 2011-10-28 22:54:33 +0000 | [diff] [blame] | 2701 | clang_reparseTranslationUnit_Impl(&RTUI); |
| 2702 | return RTUI.result; |
| 2703 | } |
| 2704 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2705 | llvm::CrashRecoveryContext CRC; |
| 2706 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 2707 | if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) { |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 2708 | fprintf(stderr, "libclang: crash detected during reparsing\n"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2709 | static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true); |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2710 | return 1; |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 2711 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 2712 | PrintLibclangResourceUsage(TU); |
Ted Kremenek | 1dfb26a | 2010-10-29 01:06:50 +0000 | [diff] [blame] | 2713 | |
Daniel Dunbar | ea94bbc | 2010-08-18 23:09:31 +0000 | [diff] [blame] | 2714 | return RTUI.result; |
| 2715 | } |
| 2716 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2717 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2718 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Douglas Gregor | 2b37c9e | 2010-01-29 00:47:48 +0000 | [diff] [blame] | 2719 | if (!CTUnit) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2720 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2721 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2722 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit->TUData); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2723 | return createCXString(CXXUnit->getOriginalSourceFileName(), true); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 2724 | } |
Daniel Dunbar | 1eb79b5 | 2009-08-28 16:30:07 +0000 | [diff] [blame] | 2725 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2726 | CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2727 | CXCursor Result = { CXCursor_TranslationUnit, 0, { 0, 0, TU } }; |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2728 | return Result; |
| 2729 | } |
| 2730 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2731 | } // end: extern "C" |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 2732 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2733 | //===----------------------------------------------------------------------===// |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2734 | // CXFile Operations. |
| 2735 | //===----------------------------------------------------------------------===// |
| 2736 | |
| 2737 | extern "C" { |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2738 | CXString clang_getFileName(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2739 | if (!SFile) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2740 | return createCXString((const char*)NULL); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2741 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2742 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 2743 | return createCXString(FEnt->getName()); |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
| 2746 | time_t clang_getFileTime(CXFile SFile) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 2747 | if (!SFile) |
| 2748 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2749 | |
Steve Naroff | 8814503 | 2009-10-27 14:35:18 +0000 | [diff] [blame] | 2750 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 2751 | return FEnt->getModificationTime(); |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2752 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2753 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2754 | CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) { |
| 2755 | if (!tu) |
| 2756 | return 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2757 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2758 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2759 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2760 | FileManager &FMgr = CXXUnit->getFileManager(); |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 2761 | return const_cast<FileEntry *>(FMgr.getFile(file_name)); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 2762 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2763 | |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 2764 | unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit tu, CXFile file) { |
| 2765 | if (!tu || !file) |
| 2766 | return 0; |
| 2767 | |
| 2768 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData); |
| 2769 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
| 2770 | return CXXUnit->getPreprocessor().getHeaderSearchInfo() |
| 2771 | .isFileMultipleIncludeGuarded(FEnt); |
| 2772 | } |
| 2773 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2774 | } // end: extern "C" |
Steve Naroff | ee9405e | 2009-09-25 21:45:39 +0000 | [diff] [blame] | 2775 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2776 | //===----------------------------------------------------------------------===// |
| 2777 | // CXCursor Operations. |
| 2778 | //===----------------------------------------------------------------------===// |
| 2779 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2780 | static Decl *getDeclFromExpr(Stmt *E) { |
Argyrios Kyrtzidis | c295461 | 2011-09-12 22:17:26 +0000 | [diff] [blame] | 2781 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2782 | return getDeclFromExpr(CE->getSubExpr()); |
| 2783 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2784 | if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
| 2785 | return RefExpr->getDecl(); |
Douglas Gregor | 38f28c1 | 2010-10-22 22:24:08 +0000 | [diff] [blame] | 2786 | if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) |
| 2787 | return RefExpr->getDecl(); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2788 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 2789 | return ME->getMemberDecl(); |
| 2790 | if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
| 2791 | return RE->getDecl(); |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2792 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 2793 | return PRE->isExplicitProperty() ? PRE->getExplicitProperty() : 0; |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2794 | if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) |
| 2795 | return getDeclFromExpr(POE->getSyntacticForm()); |
| 2796 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) |
| 2797 | if (Expr *Src = OVE->getSourceExpr()) |
| 2798 | return getDeclFromExpr(Src); |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2799 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2800 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 2801 | return getDeclFromExpr(CE->getCallee()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2802 | if (CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 2803 | if (!CE->isElidable()) |
| 2804 | return CE->getConstructor(); |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2805 | if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
| 2806 | return OME->getMethodDecl(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2807 | |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2808 | if (ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) |
| 2809 | return PE->getProtocol(); |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 2810 | if (SubstNonTypeTemplateParmPackExpr *NTTP |
| 2811 | = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E)) |
| 2812 | return NTTP->getParameterPack(); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2813 | if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
| 2814 | if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || |
| 2815 | isa<ParmVarDecl>(SizeOfPack->getPack())) |
| 2816 | return SizeOfPack->getPack(); |
Douglas Gregor | db1314e | 2010-10-01 21:11:22 +0000 | [diff] [blame] | 2817 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2818 | return 0; |
| 2819 | } |
| 2820 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2821 | static SourceLocation getLocationFromExpr(Expr *E) { |
Argyrios Kyrtzidis | c295461 | 2011-09-12 22:17:26 +0000 | [diff] [blame] | 2822 | if (ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
| 2823 | return getLocationFromExpr(CE->getSubExpr()); |
| 2824 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2825 | if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
| 2826 | return /*FIXME:*/Msg->getLeftLoc(); |
| 2827 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 2828 | return DRE->getLocation(); |
Douglas Gregor | 38f28c1 | 2010-10-22 22:24:08 +0000 | [diff] [blame] | 2829 | if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E)) |
| 2830 | return RefExpr->getLocation(); |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2831 | if (MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
| 2832 | return Member->getMemberLoc(); |
| 2833 | if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
| 2834 | return Ivar->getLocation(); |
Douglas Gregor | 94d9629 | 2011-01-19 20:34:17 +0000 | [diff] [blame] | 2835 | if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
| 2836 | return SizeOfPack->getPackLoc(); |
| 2837 | |
Daniel Dunbar | c29f4c3 | 2010-02-02 05:00:22 +0000 | [diff] [blame] | 2838 | return E->getLocStart(); |
| 2839 | } |
| 2840 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 2841 | extern "C" { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2842 | |
| 2843 | unsigned clang_visitChildren(CXCursor parent, |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 2844 | CXCursorVisitor visitor, |
| 2845 | CXClientData client_data) { |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 2846 | CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, |
| 2847 | /*VisitPreprocessorLast=*/false); |
Douglas Gregor | b1373d0 | 2010-01-20 20:59:29 +0000 | [diff] [blame] | 2848 | return CursorVis.VisitChildren(parent); |
| 2849 | } |
| 2850 | |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 2851 | #ifndef __has_feature |
| 2852 | #define __has_feature(x) 0 |
| 2853 | #endif |
| 2854 | #if __has_feature(blocks) |
| 2855 | typedef enum CXChildVisitResult |
| 2856 | (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent); |
| 2857 | |
| 2858 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 2859 | CXClientData client_data) { |
| 2860 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 2861 | return block(cursor, parent); |
| 2862 | } |
| 2863 | #else |
| 2864 | // If we are compiled with a compiler that doesn't have native blocks support, |
| 2865 | // define and call the block manually, so the |
| 2866 | typedef struct _CXChildVisitResult |
| 2867 | { |
| 2868 | void *isa; |
| 2869 | int flags; |
| 2870 | int reserved; |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2871 | enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, |
| 2872 | CXCursor); |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 2873 | } *CXCursorVisitorBlock; |
| 2874 | |
| 2875 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 2876 | CXClientData client_data) { |
| 2877 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 2878 | return block->invoke(block, cursor, parent); |
| 2879 | } |
| 2880 | #endif |
| 2881 | |
| 2882 | |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 2883 | unsigned clang_visitChildrenWithBlock(CXCursor parent, |
| 2884 | CXCursorVisitorBlock block) { |
David Chisnall | 3387c65 | 2010-11-03 14:12:26 +0000 | [diff] [blame] | 2885 | return clang_visitChildren(parent, visitWithBlock, block); |
| 2886 | } |
| 2887 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2888 | static CXString getDeclSpelling(Decl *D) { |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 2889 | if (!D) |
| 2890 | return createCXString(""); |
| 2891 | |
| 2892 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 2893 | if (!ND) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2894 | if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 2895 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 2896 | return createCXString(Property->getIdentifier()->getName()); |
| 2897 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2898 | return createCXString(""); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2901 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2902 | return createCXString(OMD->getSelector().getAsString()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2903 | |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2904 | if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
| 2905 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 2906 | // and returns different names. NamedDecl returns the class name and |
| 2907 | // ObjCCategoryImplDecl returns the category name. |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2908 | return createCXString(CIMP->getIdentifier()->getNameStart()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2909 | |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 2910 | if (isa<UsingDirectiveDecl>(D)) |
| 2911 | return createCXString(""); |
| 2912 | |
Ted Kremenek | 50aa6ac | 2010-05-19 21:51:10 +0000 | [diff] [blame] | 2913 | llvm::SmallString<1024> S; |
| 2914 | llvm::raw_svector_ostream os(S); |
| 2915 | ND->printName(os); |
| 2916 | |
| 2917 | return createCXString(os.str()); |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 2918 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 2919 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 2920 | CXString clang_getCursorSpelling(CXCursor C) { |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2921 | if (clang_isTranslationUnit(C.kind)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2922 | return clang_getTranslationUnitSpelling( |
| 2923 | static_cast<CXTranslationUnit>(C.data[2])); |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 2924 | |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 2925 | if (clang_isReference(C.kind)) { |
| 2926 | switch (C.kind) { |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2927 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 2928 | ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2929 | return createCXString(Super->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2930 | } |
| 2931 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 2932 | ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2933 | return createCXString(Class->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2934 | } |
| 2935 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 2936 | ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 2937 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2938 | return createCXString(OID->getIdentifier()->getNameStart()); |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2939 | } |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 2940 | case CXCursor_CXXBaseSpecifier: { |
| 2941 | CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); |
| 2942 | return createCXString(B->getType().getAsString()); |
| 2943 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2944 | case CXCursor_TypeRef: { |
| 2945 | TypeDecl *Type = getCursorTypeRef(C).first; |
| 2946 | assert(Type && "Missing type decl"); |
| 2947 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2948 | return createCXString(getCursorContext(C).getTypeDeclType(Type). |
| 2949 | getAsString()); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2950 | } |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 2951 | case CXCursor_TemplateRef: { |
| 2952 | TemplateDecl *Template = getCursorTemplateRef(C).first; |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 2953 | assert(Template && "Missing template decl"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 2954 | |
| 2955 | return createCXString(Template->getNameAsString()); |
| 2956 | } |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 2957 | |
| 2958 | case CXCursor_NamespaceRef: { |
| 2959 | NamedDecl *NS = getCursorNamespaceRef(C).first; |
| 2960 | assert(NS && "Missing namespace decl"); |
| 2961 | |
| 2962 | return createCXString(NS->getNameAsString()); |
| 2963 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 2964 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 2965 | case CXCursor_MemberRef: { |
| 2966 | FieldDecl *Field = getCursorMemberRef(C).first; |
| 2967 | assert(Field && "Missing member decl"); |
| 2968 | |
| 2969 | return createCXString(Field->getNameAsString()); |
| 2970 | } |
| 2971 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 2972 | case CXCursor_LabelRef: { |
| 2973 | LabelStmt *Label = getCursorLabelRef(C).first; |
| 2974 | assert(Label && "Missing label"); |
| 2975 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2976 | return createCXString(Label->getName()); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 2979 | case CXCursor_OverloadedDeclRef: { |
| 2980 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
| 2981 | if (Decl *D = Storage.dyn_cast<Decl *>()) { |
| 2982 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 2983 | return createCXString(ND->getNameAsString()); |
| 2984 | return createCXString(""); |
| 2985 | } |
| 2986 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
| 2987 | return createCXString(E->getName().getAsString()); |
| 2988 | OverloadedTemplateStorage *Ovl |
| 2989 | = Storage.get<OverloadedTemplateStorage*>(); |
| 2990 | if (Ovl->size() == 0) |
| 2991 | return createCXString(""); |
| 2992 | return createCXString((*Ovl->begin())->getNameAsString()); |
| 2993 | } |
| 2994 | |
Daniel Dunbar | acca725 | 2009-11-30 20:42:49 +0000 | [diff] [blame] | 2995 | default: |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 2996 | return createCXString("<not implemented>"); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 2997 | } |
| 2998 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 2999 | |
| 3000 | if (clang_isExpression(C.kind)) { |
| 3001 | Decl *D = getDeclFromExpr(getCursorExpr(C)); |
| 3002 | if (D) |
Douglas Gregor | 78205d4 | 2010-01-20 21:45:58 +0000 | [diff] [blame] | 3003 | return getDeclSpelling(D); |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3004 | return createCXString(""); |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3007 | if (clang_isStatement(C.kind)) { |
| 3008 | Stmt *S = getCursorStmt(C); |
| 3009 | if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3010 | return createCXString(Label->getName()); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3011 | |
| 3012 | return createCXString(""); |
| 3013 | } |
| 3014 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3015 | if (C.kind == CXCursor_MacroExpansion) |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3016 | return createCXString(getCursorMacroExpansion(C)->getName() |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 3017 | ->getNameStart()); |
| 3018 | |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3019 | if (C.kind == CXCursor_MacroDefinition) |
| 3020 | return createCXString(getCursorMacroDefinition(C)->getName() |
| 3021 | ->getNameStart()); |
| 3022 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3023 | if (C.kind == CXCursor_InclusionDirective) |
| 3024 | return createCXString(getCursorInclusionDirective(C)->getFileName()); |
| 3025 | |
Douglas Gregor | 60cbfac | 2010-01-25 16:56:17 +0000 | [diff] [blame] | 3026 | if (clang_isDeclaration(C.kind)) |
| 3027 | return getDeclSpelling(getCursorDecl(C)); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3028 | |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 3029 | if (C.kind == CXCursor_AnnotateAttr) { |
| 3030 | AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C)); |
| 3031 | return createCXString(AA->getAnnotation()); |
| 3032 | } |
| 3033 | |
Argyrios Kyrtzidis | 84b7964 | 2011-12-06 22:05:01 +0000 | [diff] [blame] | 3034 | if (C.kind == CXCursor_AsmLabelAttr) { |
| 3035 | AsmLabelAttr *AA = cast<AsmLabelAttr>(cxcursor::getCursorAttr(C)); |
| 3036 | return createCXString(AA->getLabel()); |
| 3037 | } |
| 3038 | |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 3039 | return createCXString(""); |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3040 | } |
| 3041 | |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3042 | CXString clang_getCursorDisplayName(CXCursor C) { |
| 3043 | if (!clang_isDeclaration(C.kind)) |
| 3044 | return clang_getCursorSpelling(C); |
| 3045 | |
| 3046 | Decl *D = getCursorDecl(C); |
| 3047 | if (!D) |
| 3048 | return createCXString(""); |
| 3049 | |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 3050 | PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy(); |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3051 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
| 3052 | D = FunTmpl->getTemplatedDecl(); |
| 3053 | |
| 3054 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
| 3055 | llvm::SmallString<64> Str; |
| 3056 | llvm::raw_svector_ostream OS(Str); |
| 3057 | OS << Function->getNameAsString(); |
| 3058 | if (Function->getPrimaryTemplate()) |
| 3059 | OS << "<>"; |
| 3060 | OS << "("; |
| 3061 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { |
| 3062 | if (I) |
| 3063 | OS << ", "; |
| 3064 | OS << Function->getParamDecl(I)->getType().getAsString(Policy); |
| 3065 | } |
| 3066 | |
| 3067 | if (Function->isVariadic()) { |
| 3068 | if (Function->getNumParams()) |
| 3069 | OS << ", "; |
| 3070 | OS << "..."; |
| 3071 | } |
| 3072 | OS << ")"; |
| 3073 | return createCXString(OS.str()); |
| 3074 | } |
| 3075 | |
| 3076 | if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { |
| 3077 | llvm::SmallString<64> Str; |
| 3078 | llvm::raw_svector_ostream OS(Str); |
| 3079 | OS << ClassTemplate->getNameAsString(); |
| 3080 | OS << "<"; |
| 3081 | TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); |
| 3082 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 3083 | if (I) |
| 3084 | OS << ", "; |
| 3085 | |
| 3086 | NamedDecl *Param = Params->getParam(I); |
| 3087 | if (Param->getIdentifier()) { |
| 3088 | OS << Param->getIdentifier()->getName(); |
| 3089 | continue; |
| 3090 | } |
| 3091 | |
| 3092 | // There is no parameter name, which makes this tricky. Try to come up |
| 3093 | // with something useful that isn't too long. |
| 3094 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 3095 | OS << (TTP->wasDeclaredWithTypename()? "typename" : "class"); |
| 3096 | else if (NonTypeTemplateParmDecl *NTTP |
| 3097 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 3098 | OS << NTTP->getType().getAsString(Policy); |
| 3099 | else |
| 3100 | OS << "template<...> class"; |
| 3101 | } |
| 3102 | |
| 3103 | OS << ">"; |
| 3104 | return createCXString(OS.str()); |
| 3105 | } |
| 3106 | |
| 3107 | if (ClassTemplateSpecializationDecl *ClassSpec |
| 3108 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 3109 | // If the type was explicitly written, use that. |
| 3110 | if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) |
| 3111 | return createCXString(TSInfo->getType().getAsString(Policy)); |
| 3112 | |
| 3113 | llvm::SmallString<64> Str; |
| 3114 | llvm::raw_svector_ostream OS(Str); |
| 3115 | OS << ClassSpec->getNameAsString(); |
| 3116 | OS << TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3117 | ClassSpec->getTemplateArgs().data(), |
| 3118 | ClassSpec->getTemplateArgs().size(), |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 3119 | Policy); |
| 3120 | return createCXString(OS.str()); |
| 3121 | } |
| 3122 | |
| 3123 | return clang_getCursorSpelling(C); |
| 3124 | } |
| 3125 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3126 | CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3127 | switch (Kind) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3128 | case CXCursor_FunctionDecl: |
| 3129 | return createCXString("FunctionDecl"); |
| 3130 | case CXCursor_TypedefDecl: |
| 3131 | return createCXString("TypedefDecl"); |
| 3132 | case CXCursor_EnumDecl: |
| 3133 | return createCXString("EnumDecl"); |
| 3134 | case CXCursor_EnumConstantDecl: |
| 3135 | return createCXString("EnumConstantDecl"); |
| 3136 | case CXCursor_StructDecl: |
| 3137 | return createCXString("StructDecl"); |
| 3138 | case CXCursor_UnionDecl: |
| 3139 | return createCXString("UnionDecl"); |
| 3140 | case CXCursor_ClassDecl: |
| 3141 | return createCXString("ClassDecl"); |
| 3142 | case CXCursor_FieldDecl: |
| 3143 | return createCXString("FieldDecl"); |
| 3144 | case CXCursor_VarDecl: |
| 3145 | return createCXString("VarDecl"); |
| 3146 | case CXCursor_ParmDecl: |
| 3147 | return createCXString("ParmDecl"); |
| 3148 | case CXCursor_ObjCInterfaceDecl: |
| 3149 | return createCXString("ObjCInterfaceDecl"); |
| 3150 | case CXCursor_ObjCCategoryDecl: |
| 3151 | return createCXString("ObjCCategoryDecl"); |
| 3152 | case CXCursor_ObjCProtocolDecl: |
| 3153 | return createCXString("ObjCProtocolDecl"); |
| 3154 | case CXCursor_ObjCPropertyDecl: |
| 3155 | return createCXString("ObjCPropertyDecl"); |
| 3156 | case CXCursor_ObjCIvarDecl: |
| 3157 | return createCXString("ObjCIvarDecl"); |
| 3158 | case CXCursor_ObjCInstanceMethodDecl: |
| 3159 | return createCXString("ObjCInstanceMethodDecl"); |
| 3160 | case CXCursor_ObjCClassMethodDecl: |
| 3161 | return createCXString("ObjCClassMethodDecl"); |
| 3162 | case CXCursor_ObjCImplementationDecl: |
| 3163 | return createCXString("ObjCImplementationDecl"); |
| 3164 | case CXCursor_ObjCCategoryImplDecl: |
| 3165 | return createCXString("ObjCCategoryImplDecl"); |
Ted Kremenek | 8bd5a69 | 2010-04-13 23:39:06 +0000 | [diff] [blame] | 3166 | case CXCursor_CXXMethod: |
| 3167 | return createCXString("CXXMethod"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3168 | case CXCursor_UnexposedDecl: |
| 3169 | return createCXString("UnexposedDecl"); |
| 3170 | case CXCursor_ObjCSuperClassRef: |
| 3171 | return createCXString("ObjCSuperClassRef"); |
| 3172 | case CXCursor_ObjCProtocolRef: |
| 3173 | return createCXString("ObjCProtocolRef"); |
| 3174 | case CXCursor_ObjCClassRef: |
| 3175 | return createCXString("ObjCClassRef"); |
| 3176 | case CXCursor_TypeRef: |
| 3177 | return createCXString("TypeRef"); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3178 | case CXCursor_TemplateRef: |
| 3179 | return createCXString("TemplateRef"); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3180 | case CXCursor_NamespaceRef: |
| 3181 | return createCXString("NamespaceRef"); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3182 | case CXCursor_MemberRef: |
| 3183 | return createCXString("MemberRef"); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3184 | case CXCursor_LabelRef: |
| 3185 | return createCXString("LabelRef"); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3186 | case CXCursor_OverloadedDeclRef: |
| 3187 | return createCXString("OverloadedDeclRef"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3188 | case CXCursor_IntegerLiteral: |
| 3189 | return createCXString("IntegerLiteral"); |
| 3190 | case CXCursor_FloatingLiteral: |
| 3191 | return createCXString("FloatingLiteral"); |
| 3192 | case CXCursor_ImaginaryLiteral: |
| 3193 | return createCXString("ImaginaryLiteral"); |
| 3194 | case CXCursor_StringLiteral: |
| 3195 | return createCXString("StringLiteral"); |
| 3196 | case CXCursor_CharacterLiteral: |
| 3197 | return createCXString("CharacterLiteral"); |
| 3198 | case CXCursor_ParenExpr: |
| 3199 | return createCXString("ParenExpr"); |
| 3200 | case CXCursor_UnaryOperator: |
| 3201 | return createCXString("UnaryOperator"); |
| 3202 | case CXCursor_ArraySubscriptExpr: |
| 3203 | return createCXString("ArraySubscriptExpr"); |
| 3204 | case CXCursor_BinaryOperator: |
| 3205 | return createCXString("BinaryOperator"); |
| 3206 | case CXCursor_CompoundAssignOperator: |
| 3207 | return createCXString("CompoundAssignOperator"); |
| 3208 | case CXCursor_ConditionalOperator: |
| 3209 | return createCXString("ConditionalOperator"); |
| 3210 | case CXCursor_CStyleCastExpr: |
| 3211 | return createCXString("CStyleCastExpr"); |
| 3212 | case CXCursor_CompoundLiteralExpr: |
| 3213 | return createCXString("CompoundLiteralExpr"); |
| 3214 | case CXCursor_InitListExpr: |
| 3215 | return createCXString("InitListExpr"); |
| 3216 | case CXCursor_AddrLabelExpr: |
| 3217 | return createCXString("AddrLabelExpr"); |
| 3218 | case CXCursor_StmtExpr: |
| 3219 | return createCXString("StmtExpr"); |
| 3220 | case CXCursor_GenericSelectionExpr: |
| 3221 | return createCXString("GenericSelectionExpr"); |
| 3222 | case CXCursor_GNUNullExpr: |
| 3223 | return createCXString("GNUNullExpr"); |
| 3224 | case CXCursor_CXXStaticCastExpr: |
| 3225 | return createCXString("CXXStaticCastExpr"); |
| 3226 | case CXCursor_CXXDynamicCastExpr: |
| 3227 | return createCXString("CXXDynamicCastExpr"); |
| 3228 | case CXCursor_CXXReinterpretCastExpr: |
| 3229 | return createCXString("CXXReinterpretCastExpr"); |
| 3230 | case CXCursor_CXXConstCastExpr: |
| 3231 | return createCXString("CXXConstCastExpr"); |
| 3232 | case CXCursor_CXXFunctionalCastExpr: |
| 3233 | return createCXString("CXXFunctionalCastExpr"); |
| 3234 | case CXCursor_CXXTypeidExpr: |
| 3235 | return createCXString("CXXTypeidExpr"); |
| 3236 | case CXCursor_CXXBoolLiteralExpr: |
| 3237 | return createCXString("CXXBoolLiteralExpr"); |
| 3238 | case CXCursor_CXXNullPtrLiteralExpr: |
| 3239 | return createCXString("CXXNullPtrLiteralExpr"); |
| 3240 | case CXCursor_CXXThisExpr: |
| 3241 | return createCXString("CXXThisExpr"); |
| 3242 | case CXCursor_CXXThrowExpr: |
| 3243 | return createCXString("CXXThrowExpr"); |
| 3244 | case CXCursor_CXXNewExpr: |
| 3245 | return createCXString("CXXNewExpr"); |
| 3246 | case CXCursor_CXXDeleteExpr: |
| 3247 | return createCXString("CXXDeleteExpr"); |
| 3248 | case CXCursor_UnaryExpr: |
| 3249 | return createCXString("UnaryExpr"); |
| 3250 | case CXCursor_ObjCStringLiteral: |
| 3251 | return createCXString("ObjCStringLiteral"); |
| 3252 | case CXCursor_ObjCEncodeExpr: |
| 3253 | return createCXString("ObjCEncodeExpr"); |
| 3254 | case CXCursor_ObjCSelectorExpr: |
| 3255 | return createCXString("ObjCSelectorExpr"); |
| 3256 | case CXCursor_ObjCProtocolExpr: |
| 3257 | return createCXString("ObjCProtocolExpr"); |
| 3258 | case CXCursor_ObjCBridgedCastExpr: |
| 3259 | return createCXString("ObjCBridgedCastExpr"); |
Ted Kremenek | 1ee6cad | 2010-04-11 21:47:37 +0000 | [diff] [blame] | 3260 | case CXCursor_BlockExpr: |
| 3261 | return createCXString("BlockExpr"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3262 | case CXCursor_PackExpansionExpr: |
| 3263 | return createCXString("PackExpansionExpr"); |
| 3264 | case CXCursor_SizeOfPackExpr: |
| 3265 | return createCXString("SizeOfPackExpr"); |
| 3266 | case CXCursor_UnexposedExpr: |
| 3267 | return createCXString("UnexposedExpr"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3268 | case CXCursor_DeclRefExpr: |
| 3269 | return createCXString("DeclRefExpr"); |
| 3270 | case CXCursor_MemberRefExpr: |
| 3271 | return createCXString("MemberRefExpr"); |
| 3272 | case CXCursor_CallExpr: |
| 3273 | return createCXString("CallExpr"); |
| 3274 | case CXCursor_ObjCMessageExpr: |
| 3275 | return createCXString("ObjCMessageExpr"); |
| 3276 | case CXCursor_UnexposedStmt: |
| 3277 | return createCXString("UnexposedStmt"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3278 | case CXCursor_DeclStmt: |
| 3279 | return createCXString("DeclStmt"); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3280 | case CXCursor_LabelStmt: |
| 3281 | return createCXString("LabelStmt"); |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 3282 | case CXCursor_CompoundStmt: |
| 3283 | return createCXString("CompoundStmt"); |
| 3284 | case CXCursor_CaseStmt: |
| 3285 | return createCXString("CaseStmt"); |
| 3286 | case CXCursor_DefaultStmt: |
| 3287 | return createCXString("DefaultStmt"); |
| 3288 | case CXCursor_IfStmt: |
| 3289 | return createCXString("IfStmt"); |
| 3290 | case CXCursor_SwitchStmt: |
| 3291 | return createCXString("SwitchStmt"); |
| 3292 | case CXCursor_WhileStmt: |
| 3293 | return createCXString("WhileStmt"); |
| 3294 | case CXCursor_DoStmt: |
| 3295 | return createCXString("DoStmt"); |
| 3296 | case CXCursor_ForStmt: |
| 3297 | return createCXString("ForStmt"); |
| 3298 | case CXCursor_GotoStmt: |
| 3299 | return createCXString("GotoStmt"); |
| 3300 | case CXCursor_IndirectGotoStmt: |
| 3301 | return createCXString("IndirectGotoStmt"); |
| 3302 | case CXCursor_ContinueStmt: |
| 3303 | return createCXString("ContinueStmt"); |
| 3304 | case CXCursor_BreakStmt: |
| 3305 | return createCXString("BreakStmt"); |
| 3306 | case CXCursor_ReturnStmt: |
| 3307 | return createCXString("ReturnStmt"); |
| 3308 | case CXCursor_AsmStmt: |
| 3309 | return createCXString("AsmStmt"); |
| 3310 | case CXCursor_ObjCAtTryStmt: |
| 3311 | return createCXString("ObjCAtTryStmt"); |
| 3312 | case CXCursor_ObjCAtCatchStmt: |
| 3313 | return createCXString("ObjCAtCatchStmt"); |
| 3314 | case CXCursor_ObjCAtFinallyStmt: |
| 3315 | return createCXString("ObjCAtFinallyStmt"); |
| 3316 | case CXCursor_ObjCAtThrowStmt: |
| 3317 | return createCXString("ObjCAtThrowStmt"); |
| 3318 | case CXCursor_ObjCAtSynchronizedStmt: |
| 3319 | return createCXString("ObjCAtSynchronizedStmt"); |
| 3320 | case CXCursor_ObjCAutoreleasePoolStmt: |
| 3321 | return createCXString("ObjCAutoreleasePoolStmt"); |
| 3322 | case CXCursor_ObjCForCollectionStmt: |
| 3323 | return createCXString("ObjCForCollectionStmt"); |
| 3324 | case CXCursor_CXXCatchStmt: |
| 3325 | return createCXString("CXXCatchStmt"); |
| 3326 | case CXCursor_CXXTryStmt: |
| 3327 | return createCXString("CXXTryStmt"); |
| 3328 | case CXCursor_CXXForRangeStmt: |
| 3329 | return createCXString("CXXForRangeStmt"); |
| 3330 | case CXCursor_SEHTryStmt: |
| 3331 | return createCXString("SEHTryStmt"); |
| 3332 | case CXCursor_SEHExceptStmt: |
| 3333 | return createCXString("SEHExceptStmt"); |
| 3334 | case CXCursor_SEHFinallyStmt: |
| 3335 | return createCXString("SEHFinallyStmt"); |
| 3336 | case CXCursor_NullStmt: |
| 3337 | return createCXString("NullStmt"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3338 | case CXCursor_InvalidFile: |
| 3339 | return createCXString("InvalidFile"); |
Ted Kremenek | 292db64 | 2010-03-19 20:39:05 +0000 | [diff] [blame] | 3340 | case CXCursor_InvalidCode: |
| 3341 | return createCXString("InvalidCode"); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3342 | case CXCursor_NoDeclFound: |
| 3343 | return createCXString("NoDeclFound"); |
| 3344 | case CXCursor_NotImplemented: |
| 3345 | return createCXString("NotImplemented"); |
| 3346 | case CXCursor_TranslationUnit: |
| 3347 | return createCXString("TranslationUnit"); |
Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 3348 | case CXCursor_UnexposedAttr: |
| 3349 | return createCXString("UnexposedAttr"); |
| 3350 | case CXCursor_IBActionAttr: |
| 3351 | return createCXString("attribute(ibaction)"); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3352 | case CXCursor_IBOutletAttr: |
| 3353 | return createCXString("attribute(iboutlet)"); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 3354 | case CXCursor_IBOutletCollectionAttr: |
| 3355 | return createCXString("attribute(iboutletcollection)"); |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 3356 | case CXCursor_CXXFinalAttr: |
| 3357 | return createCXString("attribute(final)"); |
| 3358 | case CXCursor_CXXOverrideAttr: |
| 3359 | return createCXString("attribute(override)"); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 3360 | case CXCursor_AnnotateAttr: |
| 3361 | return createCXString("attribute(annotate)"); |
Argyrios Kyrtzidis | 84b7964 | 2011-12-06 22:05:01 +0000 | [diff] [blame] | 3362 | case CXCursor_AsmLabelAttr: |
| 3363 | return createCXString("asm label"); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3364 | case CXCursor_PreprocessingDirective: |
| 3365 | return createCXString("preprocessing directive"); |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3366 | case CXCursor_MacroDefinition: |
| 3367 | return createCXString("macro definition"); |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3368 | case CXCursor_MacroExpansion: |
| 3369 | return createCXString("macro expansion"); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3370 | case CXCursor_InclusionDirective: |
| 3371 | return createCXString("inclusion directive"); |
Ted Kremenek | 8f06e0e | 2010-05-06 23:38:21 +0000 | [diff] [blame] | 3372 | case CXCursor_Namespace: |
| 3373 | return createCXString("Namespace"); |
Ted Kremenek | a0536d8 | 2010-05-07 01:04:29 +0000 | [diff] [blame] | 3374 | case CXCursor_LinkageSpec: |
| 3375 | return createCXString("LinkageSpec"); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3376 | case CXCursor_CXXBaseSpecifier: |
| 3377 | return createCXString("C++ base class specifier"); |
Douglas Gregor | 01829d3 | 2010-08-31 14:41:23 +0000 | [diff] [blame] | 3378 | case CXCursor_Constructor: |
| 3379 | return createCXString("CXXConstructor"); |
| 3380 | case CXCursor_Destructor: |
| 3381 | return createCXString("CXXDestructor"); |
| 3382 | case CXCursor_ConversionFunction: |
| 3383 | return createCXString("CXXConversion"); |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 3384 | case CXCursor_TemplateTypeParameter: |
| 3385 | return createCXString("TemplateTypeParameter"); |
| 3386 | case CXCursor_NonTypeTemplateParameter: |
| 3387 | return createCXString("NonTypeTemplateParameter"); |
| 3388 | case CXCursor_TemplateTemplateParameter: |
| 3389 | return createCXString("TemplateTemplateParameter"); |
| 3390 | case CXCursor_FunctionTemplate: |
| 3391 | return createCXString("FunctionTemplate"); |
Douglas Gregor | 39d6f07 | 2010-08-31 19:02:00 +0000 | [diff] [blame] | 3392 | case CXCursor_ClassTemplate: |
| 3393 | return createCXString("ClassTemplate"); |
Douglas Gregor | 74dbe64 | 2010-08-31 19:31:58 +0000 | [diff] [blame] | 3394 | case CXCursor_ClassTemplatePartialSpecialization: |
| 3395 | return createCXString("ClassTemplatePartialSpecialization"); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3396 | case CXCursor_NamespaceAlias: |
| 3397 | return createCXString("NamespaceAlias"); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 3398 | case CXCursor_UsingDirective: |
| 3399 | return createCXString("UsingDirective"); |
Douglas Gregor | 7e24256 | 2010-09-01 19:52:22 +0000 | [diff] [blame] | 3400 | case CXCursor_UsingDeclaration: |
| 3401 | return createCXString("UsingDeclaration"); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3402 | case CXCursor_TypeAliasDecl: |
Douglas Gregor | 352697a | 2011-06-03 23:08:58 +0000 | [diff] [blame] | 3403 | return createCXString("TypeAliasDecl"); |
| 3404 | case CXCursor_ObjCSynthesizeDecl: |
| 3405 | return createCXString("ObjCSynthesizeDecl"); |
| 3406 | case CXCursor_ObjCDynamicDecl: |
| 3407 | return createCXString("ObjCDynamicDecl"); |
Argyrios Kyrtzidis | 2dfdb94 | 2011-09-30 17:58:23 +0000 | [diff] [blame] | 3408 | case CXCursor_CXXAccessSpecifier: |
| 3409 | return createCXString("CXXAccessSpecifier"); |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3410 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3411 | |
Ted Kremenek | deb06bd | 2010-01-16 02:02:09 +0000 | [diff] [blame] | 3412 | llvm_unreachable("Unhandled CXCursorKind"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3413 | return createCXString((const char*) 0); |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 3414 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3415 | |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3416 | struct GetCursorData { |
| 3417 | SourceLocation TokenBeginLoc; |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3418 | bool PointsAtMacroArgExpansion; |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3419 | CXCursor &BestCursor; |
| 3420 | |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3421 | GetCursorData(SourceManager &SM, |
| 3422 | SourceLocation tokenBegin, CXCursor &outputCursor) |
| 3423 | : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) { |
| 3424 | PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin); |
| 3425 | } |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3426 | }; |
| 3427 | |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3428 | static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, |
| 3429 | CXCursor parent, |
| 3430 | CXClientData client_data) { |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3431 | GetCursorData *Data = static_cast<GetCursorData *>(client_data); |
| 3432 | CXCursor *BestCursor = &Data->BestCursor; |
Argyrios Kyrtzidis | 4b43b30 | 2011-08-17 00:31:25 +0000 | [diff] [blame] | 3433 | |
| 3434 | // If we point inside a macro argument we should provide info of what the |
| 3435 | // token is so use the actual cursor, don't replace it with a macro expansion |
| 3436 | // cursor. |
| 3437 | if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion) |
| 3438 | return CXChildVisit_Recurse; |
Argyrios Kyrtzidis | 65ab907 | 2011-09-26 19:05:37 +0000 | [diff] [blame] | 3439 | |
| 3440 | if (clang_isDeclaration(cursor.kind)) { |
| 3441 | // Avoid having the implicit methods override the property decls. |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 3442 | if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) |
Argyrios Kyrtzidis | 65ab907 | 2011-09-26 19:05:37 +0000 | [diff] [blame] | 3443 | if (MD->isImplicit()) |
| 3444 | return CXChildVisit_Break; |
| 3445 | } |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3446 | |
| 3447 | if (clang_isExpression(cursor.kind) && |
| 3448 | clang_isDeclaration(BestCursor->kind)) { |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 3449 | if (Decl *D = getCursorDecl(*BestCursor)) { |
| 3450 | // Avoid having the cursor of an expression replace the declaration cursor |
| 3451 | // when the expression source range overlaps the declaration range. |
| 3452 | // This can happen for C++ constructor expressions whose range generally |
| 3453 | // include the variable declaration, e.g.: |
| 3454 | // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor. |
| 3455 | if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() && |
| 3456 | D->getLocation() == Data->TokenBeginLoc) |
| 3457 | return CXChildVisit_Break; |
| 3458 | } |
Argyrios Kyrtzidis | 064c44b | 2011-06-27 19:42:23 +0000 | [diff] [blame] | 3459 | } |
| 3460 | |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3461 | // If our current best cursor is the construction of a temporary object, |
| 3462 | // don't replace that cursor with a type reference, because we want |
| 3463 | // clang_getCursor() to point at the constructor. |
| 3464 | if (clang_isExpression(BestCursor->kind) && |
| 3465 | isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 3466 | cursor.kind == CXCursor_TypeRef) { |
| 3467 | // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it |
| 3468 | // as having the actual point on the type reference. |
| 3469 | *BestCursor = getTypeRefedCallExprCursor(*BestCursor); |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3470 | return CXChildVisit_Recurse; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 3471 | } |
Douglas Gregor | 93798e2 | 2010-11-05 21:11:19 +0000 | [diff] [blame] | 3472 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3473 | *BestCursor = cursor; |
| 3474 | return CXChildVisit_Recurse; |
| 3475 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3476 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 3477 | CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { |
| 3478 | if (!TU) |
Ted Kremenek | f462989 | 2010-01-14 01:51:23 +0000 | [diff] [blame] | 3479 | return clang_getNullCursor(); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3480 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3481 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 3482 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 3483 | |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3484 | SourceLocation SLoc = cxloc::translateSourceLocation(Loc); |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3485 | CXCursor Result = cxcursor::getCursor(TU, SLoc); |
Ted Kremenek | a629ea4 | 2010-07-29 00:52:07 +0000 | [diff] [blame] | 3486 | |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3487 | bool Logging = getenv("LIBCLANG_LOGGING"); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3488 | if (Logging) { |
| 3489 | CXFile SearchFile; |
| 3490 | unsigned SearchLine, SearchColumn; |
| 3491 | CXFile ResultFile; |
| 3492 | unsigned ResultLine, ResultColumn; |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3493 | CXString SearchFileName, ResultFileName, KindSpelling, USR; |
| 3494 | const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : ""; |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3495 | CXSourceLocation ResultLoc = clang_getCursorLocation(Result); |
| 3496 | |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 3497 | clang_getExpansionLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, 0); |
| 3498 | clang_getExpansionLocation(ResultLoc, &ResultFile, &ResultLine, |
| 3499 | &ResultColumn, 0); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3500 | SearchFileName = clang_getFileName(SearchFile); |
| 3501 | ResultFileName = clang_getFileName(ResultFile); |
| 3502 | KindSpelling = clang_getCursorKindSpelling(Result.kind); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3503 | USR = clang_getCursorUSR(Result); |
| 3504 | 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] | 3505 | clang_getCString(SearchFileName), SearchLine, SearchColumn, |
| 3506 | clang_getCString(KindSpelling), |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3507 | clang_getCString(ResultFileName), ResultLine, ResultColumn, |
| 3508 | clang_getCString(USR), IsDef); |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3509 | clang_disposeString(SearchFileName); |
| 3510 | clang_disposeString(ResultFileName); |
| 3511 | clang_disposeString(KindSpelling); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3512 | clang_disposeString(USR); |
Douglas Gregor | 0aefbd8 | 2010-12-10 01:45:00 +0000 | [diff] [blame] | 3513 | |
| 3514 | CXCursor Definition = clang_getCursorDefinition(Result); |
| 3515 | if (!clang_equalCursors(Definition, clang_getNullCursor())) { |
| 3516 | CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition); |
| 3517 | CXString DefinitionKindSpelling |
| 3518 | = clang_getCursorKindSpelling(Definition.kind); |
| 3519 | CXFile DefinitionFile; |
| 3520 | unsigned DefinitionLine, DefinitionColumn; |
Chandler Carruth | 2017422 | 2011-08-31 16:53:37 +0000 | [diff] [blame] | 3521 | clang_getExpansionLocation(DefinitionLoc, &DefinitionFile, |
| 3522 | &DefinitionLine, &DefinitionColumn, 0); |
Douglas Gregor | 0aefbd8 | 2010-12-10 01:45:00 +0000 | [diff] [blame] | 3523 | CXString DefinitionFileName = clang_getFileName(DefinitionFile); |
| 3524 | fprintf(stderr, " -> %s(%s:%d:%d)\n", |
| 3525 | clang_getCString(DefinitionKindSpelling), |
| 3526 | clang_getCString(DefinitionFileName), |
| 3527 | DefinitionLine, DefinitionColumn); |
| 3528 | clang_disposeString(DefinitionFileName); |
| 3529 | clang_disposeString(DefinitionKindSpelling); |
| 3530 | } |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3531 | } |
| 3532 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3533 | return Result; |
Steve Naroff | 600866c | 2009-08-27 19:51:58 +0000 | [diff] [blame] | 3534 | } |
| 3535 | |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3536 | CXCursor clang_getNullCursor(void) { |
Douglas Gregor | 5bfb8c1 | 2010-01-20 23:34:41 +0000 | [diff] [blame] | 3537 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 3541 | return X == Y; |
Ted Kremenek | 7388555 | 2009-11-17 19:28:59 +0000 | [diff] [blame] | 3542 | } |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 3543 | |
Douglas Gregor | 9ce5584 | 2010-11-20 00:09:34 +0000 | [diff] [blame] | 3544 | unsigned clang_hashCursor(CXCursor C) { |
| 3545 | unsigned Index = 0; |
| 3546 | if (clang_isExpression(C.kind) || clang_isStatement(C.kind)) |
| 3547 | Index = 1; |
| 3548 | |
| 3549 | return llvm::DenseMapInfo<std::pair<unsigned, void*> >::getHashValue( |
| 3550 | std::make_pair(C.kind, C.data[Index])); |
| 3551 | } |
| 3552 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3553 | unsigned clang_isInvalid(enum CXCursorKind K) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 3554 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 3555 | } |
| 3556 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3557 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 3558 | return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; |
| 3559 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 3560 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3561 | unsigned clang_isReference(enum CXCursorKind K) { |
Steve Naroff | f334b4e | 2009-09-02 18:26:48 +0000 | [diff] [blame] | 3562 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 3563 | } |
| 3564 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3565 | unsigned clang_isExpression(enum CXCursorKind K) { |
| 3566 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; |
| 3567 | } |
| 3568 | |
| 3569 | unsigned clang_isStatement(enum CXCursorKind K) { |
| 3570 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; |
| 3571 | } |
| 3572 | |
Douglas Gregor | 8be80e1 | 2011-07-06 03:00:34 +0000 | [diff] [blame] | 3573 | unsigned clang_isAttribute(enum CXCursorKind K) { |
| 3574 | return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr; |
| 3575 | } |
| 3576 | |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 3577 | unsigned clang_isTranslationUnit(enum CXCursorKind K) { |
| 3578 | return K == CXCursor_TranslationUnit; |
| 3579 | } |
| 3580 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3581 | unsigned clang_isPreprocessing(enum CXCursorKind K) { |
| 3582 | return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; |
| 3583 | } |
| 3584 | |
Ted Kremenek | ad6eff6 | 2010-03-08 21:17:29 +0000 | [diff] [blame] | 3585 | unsigned clang_isUnexposed(enum CXCursorKind K) { |
| 3586 | switch (K) { |
| 3587 | case CXCursor_UnexposedDecl: |
| 3588 | case CXCursor_UnexposedExpr: |
| 3589 | case CXCursor_UnexposedStmt: |
| 3590 | case CXCursor_UnexposedAttr: |
| 3591 | return true; |
| 3592 | default: |
| 3593 | return false; |
| 3594 | } |
| 3595 | } |
| 3596 | |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 3597 | CXCursorKind clang_getCursorKind(CXCursor C) { |
Steve Naroff | 9efa767 | 2009-09-04 15:44:05 +0000 | [diff] [blame] | 3598 | return C.kind; |
| 3599 | } |
| 3600 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3601 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 3602 | if (clang_isReference(C.kind)) { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3603 | switch (C.kind) { |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3604 | case CXCursor_ObjCSuperClassRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3605 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 3606 | = getCursorObjCSuperClassRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3607 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3610 | case CXCursor_ObjCProtocolRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3611 | std::pair<ObjCProtocolDecl *, SourceLocation> P |
| 3612 | = getCursorObjCProtocolRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3613 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3614 | } |
| 3615 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3616 | case CXCursor_ObjCClassRef: { |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3617 | std::pair<ObjCInterfaceDecl *, SourceLocation> P |
| 3618 | = getCursorObjCClassRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3619 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3620 | } |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3621 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3622 | case CXCursor_TypeRef: { |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3623 | std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C); |
Ted Kremenek | a297de2 | 2010-01-25 22:34:44 +0000 | [diff] [blame] | 3624 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3625 | } |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3626 | |
| 3627 | case CXCursor_TemplateRef: { |
| 3628 | std::pair<TemplateDecl *, SourceLocation> P = getCursorTemplateRef(C); |
| 3629 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3630 | } |
| 3631 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3632 | case CXCursor_NamespaceRef: { |
| 3633 | std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); |
| 3634 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3635 | } |
| 3636 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3637 | case CXCursor_MemberRef: { |
| 3638 | std::pair<FieldDecl *, SourceLocation> P = getCursorMemberRef(C); |
| 3639 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 3640 | } |
| 3641 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3642 | case CXCursor_CXXBaseSpecifier: { |
Douglas Gregor | 1b0f7af | 2010-10-02 19:51:13 +0000 | [diff] [blame] | 3643 | CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); |
| 3644 | if (!BaseSpec) |
| 3645 | return clang_getNullLocation(); |
| 3646 | |
| 3647 | if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) |
| 3648 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3649 | TSInfo->getTypeLoc().getBeginLoc()); |
| 3650 | |
| 3651 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3652 | BaseSpec->getSourceRange().getBegin()); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3653 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3654 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3655 | case CXCursor_LabelRef: { |
| 3656 | std::pair<LabelStmt *, SourceLocation> P = getCursorLabelRef(C); |
| 3657 | return cxloc::translateSourceLocation(getCursorContext(C), P.second); |
| 3658 | } |
| 3659 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3660 | case CXCursor_OverloadedDeclRef: |
| 3661 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3662 | getCursorOverloadedDeclRef(C).second); |
| 3663 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3664 | default: |
| 3665 | // FIXME: Need a way to enumerate all non-reference cases. |
| 3666 | llvm_unreachable("Missed a reference kind"); |
| 3667 | } |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3668 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3669 | |
| 3670 | if (clang_isExpression(C.kind)) |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3671 | return cxloc::translateSourceLocation(getCursorContext(C), |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3672 | getLocationFromExpr(getCursorExpr(C))); |
| 3673 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3674 | if (clang_isStatement(C.kind)) |
| 3675 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 3676 | getCursorStmt(C)->getLocStart()); |
| 3677 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 3678 | if (C.kind == CXCursor_PreprocessingDirective) { |
| 3679 | SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); |
| 3680 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3681 | } |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3682 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3683 | if (C.kind == CXCursor_MacroExpansion) { |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 3684 | SourceLocation L |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3685 | = cxcursor::getCursorMacroExpansion(C)->getSourceRange().getBegin(); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3686 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3687 | } |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3688 | |
| 3689 | if (C.kind == CXCursor_MacroDefinition) { |
| 3690 | SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); |
| 3691 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3692 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3693 | |
| 3694 | if (C.kind == CXCursor_InclusionDirective) { |
| 3695 | SourceLocation L |
| 3696 | = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); |
| 3697 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 3698 | } |
| 3699 | |
Ted Kremenek | 9a700d2 | 2010-05-12 06:16:13 +0000 | [diff] [blame] | 3700 | if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 3701 | return clang_getNullLocation(); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3702 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3703 | Decl *D = getCursorDecl(C); |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 3704 | if (!D) |
| 3705 | return clang_getNullLocation(); |
| 3706 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 3707 | SourceLocation Loc = D->getLocation(); |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 3708 | // FIXME: Multiple variables declared in a single declaration |
| 3709 | // currently lack the information needed to correctly determine their |
| 3710 | // ranges when accounting for the type-specifier. We use context |
| 3711 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3712 | // and if so, whether it is the first decl. |
| 3713 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3714 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3715 | Loc = VD->getLocation(); |
| 3716 | } |
| 3717 | |
Douglas Gregor | 2ca54fe | 2010-03-22 15:53:50 +0000 | [diff] [blame] | 3718 | return cxloc::translateSourceLocation(getCursorContext(C), Loc); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 3719 | } |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3720 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3721 | } // end extern "C" |
| 3722 | |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3723 | CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) { |
| 3724 | assert(TU); |
| 3725 | |
| 3726 | // Guard against an invalid SourceLocation, or we may assert in one |
| 3727 | // of the following calls. |
| 3728 | if (SLoc.isInvalid()) |
| 3729 | return clang_getNullCursor(); |
| 3730 | |
| 3731 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 3732 | |
| 3733 | // Translate the given source location to make it point at the beginning of |
| 3734 | // the token under the cursor. |
| 3735 | SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), |
| 3736 | CXXUnit->getASTContext().getLangOptions()); |
| 3737 | |
| 3738 | CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 3739 | if (SLoc.isValid()) { |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3740 | GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result); |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3741 | CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData, |
| 3742 | /*VisitPreprocessorLast=*/true, |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 3743 | /*VisitIncludedEntities=*/false, |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3744 | SourceLocation(SLoc)); |
Argyrios Kyrtzidis | dfb332d | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 3745 | CursorVis.visitFileRegion(); |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 3746 | } |
| 3747 | |
| 3748 | return Result; |
| 3749 | } |
| 3750 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3751 | static SourceRange getRawCursorExtent(CXCursor C) { |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3752 | if (clang_isReference(C.kind)) { |
| 3753 | switch (C.kind) { |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3754 | case CXCursor_ObjCSuperClassRef: |
| 3755 | return getCursorObjCSuperClassRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3756 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3757 | case CXCursor_ObjCProtocolRef: |
| 3758 | return getCursorObjCProtocolRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3759 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3760 | case CXCursor_ObjCClassRef: |
| 3761 | return getCursorObjCClassRef(C).second; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3762 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3763 | case CXCursor_TypeRef: |
| 3764 | return getCursorTypeRef(C).second; |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3765 | |
| 3766 | case CXCursor_TemplateRef: |
| 3767 | return getCursorTemplateRef(C).second; |
| 3768 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3769 | case CXCursor_NamespaceRef: |
| 3770 | return getCursorNamespaceRef(C).second; |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3771 | |
| 3772 | case CXCursor_MemberRef: |
| 3773 | return getCursorMemberRef(C).second; |
| 3774 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3775 | case CXCursor_CXXBaseSpecifier: |
Douglas Gregor | 1b0f7af | 2010-10-02 19:51:13 +0000 | [diff] [blame] | 3776 | return getCursorCXXBaseSpecifier(C)->getSourceRange(); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3777 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3778 | case CXCursor_LabelRef: |
| 3779 | return getCursorLabelRef(C).second; |
| 3780 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3781 | case CXCursor_OverloadedDeclRef: |
| 3782 | return getCursorOverloadedDeclRef(C).second; |
| 3783 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3784 | default: |
| 3785 | // FIXME: Need a way to enumerate all non-reference cases. |
| 3786 | llvm_unreachable("Missed a reference kind"); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3787 | } |
| 3788 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3789 | |
| 3790 | if (clang_isExpression(C.kind)) |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3791 | return getCursorExpr(C)->getSourceRange(); |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 3792 | |
| 3793 | if (clang_isStatement(C.kind)) |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3794 | return getCursorStmt(C)->getSourceRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3795 | |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 3796 | if (clang_isAttribute(C.kind)) |
| 3797 | return getCursorAttr(C)->getRange(); |
| 3798 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3799 | if (C.kind == CXCursor_PreprocessingDirective) |
| 3800 | return cxcursor::getCursorPreprocessingDirective(C); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 3801 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 3802 | if (C.kind == CXCursor_MacroExpansion) { |
| 3803 | ASTUnit *TU = getCursorASTUnit(C); |
| 3804 | SourceRange Range = cxcursor::getCursorMacroExpansion(C)->getSourceRange(); |
| 3805 | return TU->mapRangeFromPreamble(Range); |
| 3806 | } |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 3807 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 3808 | if (C.kind == CXCursor_MacroDefinition) { |
| 3809 | ASTUnit *TU = getCursorASTUnit(C); |
| 3810 | SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange(); |
| 3811 | return TU->mapRangeFromPreamble(Range); |
| 3812 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3813 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 3814 | if (C.kind == CXCursor_InclusionDirective) { |
| 3815 | ASTUnit *TU = getCursorASTUnit(C); |
| 3816 | SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange(); |
| 3817 | return TU->mapRangeFromPreamble(Range); |
| 3818 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 3819 | |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 3820 | if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { |
| 3821 | Decl *D = cxcursor::getCursorDecl(C); |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 3822 | if (!D) |
| 3823 | return SourceRange(); |
| 3824 | |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 3825 | SourceRange R = D->getSourceRange(); |
| 3826 | // FIXME: Multiple variables declared in a single declaration |
| 3827 | // currently lack the information needed to correctly determine their |
| 3828 | // ranges when accounting for the type-specifier. We use context |
| 3829 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3830 | // and if so, whether it is the first decl. |
| 3831 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3832 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3833 | R.setBegin(VD->getLocation()); |
| 3834 | } |
| 3835 | return R; |
| 3836 | } |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3837 | return SourceRange(); |
| 3838 | } |
| 3839 | |
| 3840 | /// \brief Retrieves the "raw" cursor extent, which is then extended to include |
| 3841 | /// the decl-specifier-seq for declarations. |
| 3842 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) { |
| 3843 | if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) { |
| 3844 | Decl *D = cxcursor::getCursorDecl(C); |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 3845 | if (!D) |
| 3846 | return SourceRange(); |
| 3847 | |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3848 | SourceRange R = D->getSourceRange(); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3849 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 3850 | // Adjust the start of the location for declarations preceded by |
| 3851 | // declaration specifiers. |
| 3852 | SourceLocation StartLoc; |
| 3853 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
| 3854 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
| 3855 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 3856 | } else if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
| 3857 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
| 3858 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
| 3859 | } |
| 3860 | |
| 3861 | if (StartLoc.isValid() && R.getBegin().isValid() && |
| 3862 | SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin())) |
| 3863 | R.setBegin(StartLoc); |
| 3864 | |
| 3865 | // FIXME: Multiple variables declared in a single declaration |
| 3866 | // currently lack the information needed to correctly determine their |
| 3867 | // ranges when accounting for the type-specifier. We use context |
| 3868 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 3869 | // and if so, whether it is the first decl. |
| 3870 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3871 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 3872 | R.setBegin(VD->getLocation()); |
Douglas Gregor | 6653798 | 2010-11-17 17:14:07 +0000 | [diff] [blame] | 3873 | } |
| 3874 | |
| 3875 | return R; |
| 3876 | } |
| 3877 | |
| 3878 | return getRawCursorExtent(C); |
| 3879 | } |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3880 | |
| 3881 | extern "C" { |
| 3882 | |
| 3883 | CXSourceRange clang_getCursorExtent(CXCursor C) { |
| 3884 | SourceRange R = getRawCursorExtent(C); |
| 3885 | if (R.isInvalid()) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 3886 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3887 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 3888 | return cxloc::translateSourceRange(getCursorContext(C), R); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 3889 | } |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3890 | |
| 3891 | CXCursor clang_getCursorReferenced(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3892 | if (clang_isInvalid(C.kind)) |
| 3893 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3894 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3895 | CXTranslationUnit tu = getCursorTU(C); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3896 | if (clang_isDeclaration(C.kind)) { |
| 3897 | Decl *D = getCursorDecl(C); |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 3898 | if (!D) |
| 3899 | return clang_getNullCursor(); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3900 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3901 | return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3902 | if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3903 | return MakeCursorOverloadedDeclRef(Classes, D->getLocation(), tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3904 | if (ObjCForwardProtocolDecl *Protocols |
| 3905 | = dyn_cast<ObjCForwardProtocolDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3906 | return MakeCursorOverloadedDeclRef(Protocols, D->getLocation(), tu); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3907 | if (ObjCPropertyImplDecl *PropImpl =dyn_cast<ObjCPropertyImplDecl>(D)) |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 3908 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 3909 | return MakeCXCursor(Property, tu); |
| 3910 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3911 | return C; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3912 | } |
| 3913 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3914 | if (clang_isExpression(C.kind)) { |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3915 | Expr *E = getCursorExpr(C); |
| 3916 | Decl *D = getDeclFromExpr(E); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 3917 | if (D) { |
| 3918 | CXCursor declCursor = MakeCXCursor(D, tu); |
| 3919 | declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C), |
| 3920 | declCursor); |
| 3921 | return declCursor; |
| 3922 | } |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3923 | |
| 3924 | if (OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3925 | return MakeCursorOverloadedDeclRef(Ovl, tu); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3926 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 3927 | return clang_getNullCursor(); |
| 3928 | } |
| 3929 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3930 | if (clang_isStatement(C.kind)) { |
| 3931 | Stmt *S = getCursorStmt(C); |
| 3932 | if (GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) |
Ted Kremenek | 37c2e96 | 2011-03-15 23:47:49 +0000 | [diff] [blame] | 3933 | if (LabelDecl *label = Goto->getLabel()) |
| 3934 | if (LabelStmt *labelS = label->getStmt()) |
| 3935 | return MakeCXCursor(labelS, getCursorDecl(C), tu); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3936 | |
| 3937 | return clang_getNullCursor(); |
| 3938 | } |
| 3939 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 3940 | if (C.kind == CXCursor_MacroExpansion) { |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 3941 | if (MacroDefinition *Def = getCursorMacroExpansion(C)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3942 | return MakeMacroDefinitionCursor(Def, tu); |
Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 3943 | } |
| 3944 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3945 | if (!clang_isReference(C.kind)) |
| 3946 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3947 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3948 | switch (C.kind) { |
| 3949 | case CXCursor_ObjCSuperClassRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3950 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3951 | |
| 3952 | case CXCursor_ObjCProtocolRef: { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3953 | return MakeCXCursor(getCursorObjCProtocolRef(C).first, tu); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3954 | |
| 3955 | case CXCursor_ObjCClassRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3956 | return MakeCXCursor(getCursorObjCClassRef(C).first, tu ); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 3957 | |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3958 | case CXCursor_TypeRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3959 | return MakeCXCursor(getCursorTypeRef(C).first, tu ); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3960 | |
| 3961 | case CXCursor_TemplateRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3962 | return MakeCXCursor(getCursorTemplateRef(C).first, tu ); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 3963 | |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3964 | case CXCursor_NamespaceRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3965 | return MakeCXCursor(getCursorNamespaceRef(C).first, tu ); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 3966 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3967 | case CXCursor_MemberRef: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3968 | return MakeCXCursor(getCursorMemberRef(C).first, tu ); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 3969 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3970 | case CXCursor_CXXBaseSpecifier: { |
| 3971 | CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); |
| 3972 | return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3973 | tu )); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 3974 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3975 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3976 | case CXCursor_LabelRef: |
| 3977 | // FIXME: We end up faking the "parent" declaration here because we |
| 3978 | // don't want to make CXCursor larger. |
| 3979 | return MakeCXCursor(getCursorLabelRef(C).first, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3980 | static_cast<ASTUnit*>(tu->TUData)->getASTContext() |
| 3981 | .getTranslationUnitDecl(), |
| 3982 | tu); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 3983 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 3984 | case CXCursor_OverloadedDeclRef: |
| 3985 | return C; |
| 3986 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3987 | default: |
| 3988 | // We would prefer to enumerate all non-reference cursor kinds here. |
| 3989 | llvm_unreachable("Unhandled reference cursor kind"); |
| 3990 | break; |
| 3991 | } |
| 3992 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 3993 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 3994 | return clang_getNullCursor(); |
| 3995 | } |
| 3996 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 3997 | CXCursor clang_getCursorDefinition(CXCursor C) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 3998 | if (clang_isInvalid(C.kind)) |
| 3999 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4000 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4001 | CXTranslationUnit TU = getCursorTU(C); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4002 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4003 | bool WasReference = false; |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 4004 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4005 | C = clang_getCursorReferenced(C); |
| 4006 | WasReference = true; |
| 4007 | } |
| 4008 | |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 4009 | if (C.kind == CXCursor_MacroExpansion) |
Douglas Gregor | bf7efa2 | 2010-03-18 18:23:03 +0000 | [diff] [blame] | 4010 | return clang_getCursorReferenced(C); |
| 4011 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4012 | if (!clang_isDeclaration(C.kind)) |
| 4013 | return clang_getNullCursor(); |
| 4014 | |
| 4015 | Decl *D = getCursorDecl(C); |
| 4016 | if (!D) |
| 4017 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4018 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4019 | switch (D->getKind()) { |
| 4020 | // Declaration kinds that don't really separate the notions of |
| 4021 | // declaration and definition. |
| 4022 | case Decl::Namespace: |
| 4023 | case Decl::Typedef: |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4024 | case Decl::TypeAlias: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4025 | case Decl::TypeAliasTemplate: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4026 | case Decl::TemplateTypeParm: |
| 4027 | case Decl::EnumConstant: |
| 4028 | case Decl::Field: |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 4029 | case Decl::IndirectField: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4030 | case Decl::ObjCIvar: |
| 4031 | case Decl::ObjCAtDefsField: |
| 4032 | case Decl::ImplicitParam: |
| 4033 | case Decl::ParmVar: |
| 4034 | case Decl::NonTypeTemplateParm: |
| 4035 | case Decl::TemplateTemplateParm: |
| 4036 | case Decl::ObjCCategoryImpl: |
| 4037 | case Decl::ObjCImplementation: |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 4038 | case Decl::AccessSpec: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4039 | case Decl::LinkageSpec: |
| 4040 | case Decl::ObjCPropertyImpl: |
| 4041 | case Decl::FileScopeAsm: |
| 4042 | case Decl::StaticAssert: |
| 4043 | case Decl::Block: |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 4044 | case Decl::Label: // FIXME: Is this right?? |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4045 | case Decl::ClassScopeFunctionSpecialization: |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4046 | case Decl::Import: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4047 | return C; |
| 4048 | |
| 4049 | // Declaration kinds that don't make any sense here, but are |
| 4050 | // nonetheless harmless. |
| 4051 | case Decl::TranslationUnit: |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4052 | break; |
| 4053 | |
| 4054 | // Declaration kinds for which the definition is not resolvable. |
| 4055 | case Decl::UnresolvedUsingTypename: |
| 4056 | case Decl::UnresolvedUsingValue: |
| 4057 | break; |
| 4058 | |
| 4059 | case Decl::UsingDirective: |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 4060 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4061 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4062 | |
| 4063 | case Decl::NamespaceAlias: |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4064 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4065 | |
| 4066 | case Decl::Enum: |
| 4067 | case Decl::Record: |
| 4068 | case Decl::CXXRecord: |
| 4069 | case Decl::ClassTemplateSpecialization: |
| 4070 | case Decl::ClassTemplatePartialSpecialization: |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4071 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4072 | return MakeCXCursor(Def, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4073 | return clang_getNullCursor(); |
| 4074 | |
| 4075 | case Decl::Function: |
| 4076 | case Decl::CXXMethod: |
| 4077 | case Decl::CXXConstructor: |
| 4078 | case Decl::CXXDestructor: |
| 4079 | case Decl::CXXConversion: { |
| 4080 | const FunctionDecl *Def = 0; |
| 4081 | if (cast<FunctionDecl>(D)->getBody(Def)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4082 | return MakeCXCursor(const_cast<FunctionDecl *>(Def), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4083 | return clang_getNullCursor(); |
| 4084 | } |
| 4085 | |
| 4086 | case Decl::Var: { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 4087 | // Ask the variable if it has a definition. |
| 4088 | if (VarDecl *Def = cast<VarDecl>(D)->getDefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4089 | return MakeCXCursor(Def, TU); |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 4090 | return clang_getNullCursor(); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4091 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4092 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4093 | case Decl::FunctionTemplate: { |
| 4094 | const FunctionDecl *Def = 0; |
| 4095 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4096 | return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4097 | return clang_getNullCursor(); |
| 4098 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4099 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4100 | case Decl::ClassTemplate: { |
| 4101 | if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4102 | ->getDefinition()) |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 4103 | return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4104 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4105 | return clang_getNullCursor(); |
| 4106 | } |
| 4107 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4108 | case Decl::Using: |
| 4109 | return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4110 | D->getLocation(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4111 | |
| 4112 | case Decl::UsingShadow: |
| 4113 | return clang_getCursorDefinition( |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4114 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4115 | TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4116 | |
| 4117 | case Decl::ObjCMethod: { |
| 4118 | ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); |
| 4119 | if (Method->isThisDeclarationADefinition()) |
| 4120 | return C; |
| 4121 | |
| 4122 | // Dig out the method definition in the associated |
| 4123 | // @implementation, if we have it. |
| 4124 | // FIXME: The ASTs should make finding the definition easier. |
| 4125 | if (ObjCInterfaceDecl *Class |
| 4126 | = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) |
| 4127 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) |
| 4128 | if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), |
| 4129 | Method->isInstanceMethod())) |
| 4130 | if (Def->isThisDeclarationADefinition()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4131 | return MakeCXCursor(Def, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4132 | |
| 4133 | return clang_getNullCursor(); |
| 4134 | } |
| 4135 | |
| 4136 | case Decl::ObjCCategory: |
| 4137 | if (ObjCCategoryImplDecl *Impl |
| 4138 | = cast<ObjCCategoryDecl>(D)->getImplementation()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4139 | return MakeCXCursor(Impl, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4140 | return clang_getNullCursor(); |
| 4141 | |
| 4142 | case Decl::ObjCProtocol: |
| 4143 | if (!cast<ObjCProtocolDecl>(D)->isForwardDecl()) |
| 4144 | return C; |
| 4145 | return clang_getNullCursor(); |
| 4146 | |
| 4147 | case Decl::ObjCInterface: |
| 4148 | // There are two notions of a "definition" for an Objective-C |
| 4149 | // class: the interface and its implementation. When we resolved a |
| 4150 | // reference to an Objective-C class, produce the @interface as |
| 4151 | // the definition; when we were provided with the interface, |
| 4152 | // produce the @implementation as the definition. |
| 4153 | if (WasReference) { |
| 4154 | if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl()) |
| 4155 | return C; |
| 4156 | } else if (ObjCImplementationDecl *Impl |
| 4157 | = cast<ObjCInterfaceDecl>(D)->getImplementation()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4158 | return MakeCXCursor(Impl, TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4159 | return clang_getNullCursor(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4160 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4161 | case Decl::ObjCProperty: |
| 4162 | // FIXME: We don't really know where to find the |
| 4163 | // ObjCPropertyImplDecls that implement this property. |
| 4164 | return clang_getNullCursor(); |
| 4165 | |
| 4166 | case Decl::ObjCCompatibleAlias: |
| 4167 | if (ObjCInterfaceDecl *Class |
| 4168 | = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) |
| 4169 | if (!Class->isForwardDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4170 | return MakeCXCursor(Class, TU); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4171 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4172 | return clang_getNullCursor(); |
| 4173 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4174 | case Decl::ObjCForwardProtocol: |
| 4175 | return MakeCursorOverloadedDeclRef(cast<ObjCForwardProtocolDecl>(D), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4176 | D->getLocation(), TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4177 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4178 | case Decl::ObjCClass: |
Daniel Dunbar | 9e1ebdd | 2010-11-05 07:19:21 +0000 | [diff] [blame] | 4179 | return MakeCursorOverloadedDeclRef(cast<ObjCClassDecl>(D), D->getLocation(), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4180 | TU); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4181 | |
| 4182 | case Decl::Friend: |
| 4183 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4184 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4185 | return clang_getNullCursor(); |
| 4186 | |
| 4187 | case Decl::FriendTemplate: |
| 4188 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4189 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 4190 | return clang_getNullCursor(); |
| 4191 | } |
| 4192 | |
| 4193 | return clang_getNullCursor(); |
| 4194 | } |
| 4195 | |
| 4196 | unsigned clang_isCursorDefinition(CXCursor C) { |
| 4197 | if (!clang_isDeclaration(C.kind)) |
| 4198 | return 0; |
| 4199 | |
| 4200 | return clang_getCursorDefinition(C) == C; |
| 4201 | } |
| 4202 | |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4203 | CXCursor clang_getCanonicalCursor(CXCursor C) { |
| 4204 | if (!clang_isDeclaration(C.kind)) |
| 4205 | return C; |
| 4206 | |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4207 | if (Decl *D = getCursorDecl(C)) { |
Argyrios Kyrtzidis | debb00f | 2011-07-15 22:37:58 +0000 | [diff] [blame] | 4208 | if (ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 4209 | if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl()) |
| 4210 | return MakeCXCursor(CatD, getCursorTU(C)); |
| 4211 | |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4212 | if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) |
| 4213 | if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
| 4214 | return MakeCXCursor(IFD, getCursorTU(C)); |
| 4215 | |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4216 | return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C)); |
Argyrios Kyrtzidis | e2f854d | 2011-07-15 22:27:18 +0000 | [diff] [blame] | 4217 | } |
Douglas Gregor | 1a9d050 | 2010-11-19 23:44:15 +0000 | [diff] [blame] | 4218 | |
| 4219 | return C; |
| 4220 | } |
| 4221 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4222 | unsigned clang_getNumOverloadedDecls(CXCursor C) { |
Douglas Gregor | 7c432dd | 2010-09-16 13:54:00 +0000 | [diff] [blame] | 4223 | if (C.kind != CXCursor_OverloadedDeclRef) |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4224 | return 0; |
| 4225 | |
| 4226 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
| 4227 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
| 4228 | return E->getNumDecls(); |
| 4229 | |
| 4230 | if (OverloadedTemplateStorage *S |
| 4231 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 4232 | return S->size(); |
| 4233 | |
| 4234 | Decl *D = Storage.get<Decl*>(); |
| 4235 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Argyrios Kyrtzidis | 826faa2 | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 4236 | return Using->shadow_size(); |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 4237 | if (isa<ObjCClassDecl>(D)) |
| 4238 | return 1; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4239 | if (ObjCForwardProtocolDecl *Protocols =dyn_cast<ObjCForwardProtocolDecl>(D)) |
| 4240 | return Protocols->protocol_size(); |
| 4241 | |
| 4242 | return 0; |
| 4243 | } |
| 4244 | |
| 4245 | CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { |
Douglas Gregor | 7c432dd | 2010-09-16 13:54:00 +0000 | [diff] [blame] | 4246 | if (cursor.kind != CXCursor_OverloadedDeclRef) |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4247 | return clang_getNullCursor(); |
| 4248 | |
| 4249 | if (index >= clang_getNumOverloadedDecls(cursor)) |
| 4250 | return clang_getNullCursor(); |
| 4251 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4252 | CXTranslationUnit TU = getCursorTU(cursor); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4253 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first; |
| 4254 | if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4255 | return MakeCXCursor(E->decls_begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4256 | |
| 4257 | if (OverloadedTemplateStorage *S |
| 4258 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4259 | return MakeCXCursor(S->begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4260 | |
| 4261 | Decl *D = Storage.get<Decl*>(); |
| 4262 | if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) { |
| 4263 | // FIXME: This is, unfortunately, linear time. |
| 4264 | UsingDecl::shadow_iterator Pos = Using->shadow_begin(); |
| 4265 | std::advance(Pos, index); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4266 | return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4267 | } |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4268 | if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D)) |
Fariborz Jahanian | 95ed778 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 4269 | return MakeCXCursor(Classes->getForwardInterfaceDecl(), TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4270 | if (ObjCForwardProtocolDecl *Protocols = dyn_cast<ObjCForwardProtocolDecl>(D)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4271 | return MakeCXCursor(Protocols->protocol_begin()[index], TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 4272 | |
| 4273 | return clang_getNullCursor(); |
| 4274 | } |
| 4275 | |
Daniel Dunbar | 0d7dd22 | 2009-11-30 20:42:43 +0000 | [diff] [blame] | 4276 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4277 | const char **startBuf, |
| 4278 | const char **endBuf, |
| 4279 | unsigned *startLine, |
| 4280 | unsigned *startColumn, |
| 4281 | unsigned *endLine, |
Daniel Dunbar | 9ebfa31 | 2009-12-01 03:14:51 +0000 | [diff] [blame] | 4282 | unsigned *endColumn) { |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 4283 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
| 4284 | NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C)); |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4285 | FunctionDecl *FD = dyn_cast<FunctionDecl>(ND); |
| 4286 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4287 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4288 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 4289 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 4290 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 4291 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 4292 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 4293 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 4294 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 4295 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4296 | |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 4297 | |
| 4298 | CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags, |
| 4299 | unsigned PieceIndex) { |
| 4300 | RefNamePieces Pieces; |
| 4301 | |
| 4302 | switch (C.kind) { |
| 4303 | case CXCursor_MemberRefExpr: |
| 4304 | if (MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C))) |
| 4305 | Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(), |
| 4306 | E->getQualifierLoc().getSourceRange()); |
| 4307 | break; |
| 4308 | |
| 4309 | case CXCursor_DeclRefExpr: |
| 4310 | if (DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C))) |
| 4311 | Pieces = buildPieces(NameFlags, false, E->getNameInfo(), |
| 4312 | E->getQualifierLoc().getSourceRange(), |
| 4313 | E->getExplicitTemplateArgsOpt()); |
| 4314 | break; |
| 4315 | |
| 4316 | case CXCursor_CallExpr: |
| 4317 | if (CXXOperatorCallExpr *OCE = |
| 4318 | dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) { |
| 4319 | Expr *Callee = OCE->getCallee(); |
| 4320 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) |
| 4321 | Callee = ICE->getSubExpr(); |
| 4322 | |
| 4323 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) |
| 4324 | Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(), |
| 4325 | DRE->getQualifierLoc().getSourceRange()); |
| 4326 | } |
| 4327 | break; |
| 4328 | |
| 4329 | default: |
| 4330 | break; |
| 4331 | } |
| 4332 | |
| 4333 | if (Pieces.empty()) { |
| 4334 | if (PieceIndex == 0) |
| 4335 | return clang_getCursorExtent(C); |
| 4336 | } else if (PieceIndex < Pieces.size()) { |
| 4337 | SourceRange R = Pieces[PieceIndex]; |
| 4338 | if (R.isValid()) |
| 4339 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 4340 | } |
| 4341 | |
| 4342 | return clang_getNullRange(); |
| 4343 | } |
| 4344 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 4345 | void clang_enableStackTraces(void) { |
| 4346 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 4347 | } |
| 4348 | |
Daniel Dunbar | 995aaf9 | 2010-11-04 01:26:29 +0000 | [diff] [blame] | 4349 | void clang_executeOnThread(void (*fn)(void*), void *user_data, |
| 4350 | unsigned stack_size) { |
| 4351 | llvm::llvm_execute_on_thread(fn, user_data, stack_size); |
| 4352 | } |
| 4353 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 4354 | } // end: extern "C" |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 4355 | |
Ted Kremenek | fb48049 | 2010-01-13 21:46:36 +0000 | [diff] [blame] | 4356 | //===----------------------------------------------------------------------===// |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4357 | // Token-based Operations. |
| 4358 | //===----------------------------------------------------------------------===// |
| 4359 | |
| 4360 | /* CXToken layout: |
| 4361 | * int_data[0]: a CXTokenKind |
| 4362 | * int_data[1]: starting token location |
| 4363 | * int_data[2]: token length |
| 4364 | * int_data[3]: reserved |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4365 | * ptr_data: for identifiers and keywords, an IdentifierInfo*. |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4366 | * otherwise unused. |
| 4367 | */ |
| 4368 | extern "C" { |
| 4369 | |
| 4370 | CXTokenKind clang_getTokenKind(CXToken CXTok) { |
| 4371 | return static_cast<CXTokenKind>(CXTok.int_data[0]); |
| 4372 | } |
| 4373 | |
| 4374 | CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { |
| 4375 | switch (clang_getTokenKind(CXTok)) { |
| 4376 | case CXToken_Identifier: |
| 4377 | case CXToken_Keyword: |
| 4378 | // We know we have an IdentifierInfo*, so use that. |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 4379 | return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data) |
| 4380 | ->getNameStart()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4381 | |
| 4382 | case CXToken_Literal: { |
| 4383 | // We have stashed the starting pointer in the ptr_data field. Use it. |
| 4384 | const char *Text = static_cast<const char *>(CXTok.ptr_data); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4385 | return createCXString(StringRef(Text, CXTok.int_data[2])); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4386 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4387 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4388 | case CXToken_Punctuation: |
| 4389 | case CXToken_Comment: |
| 4390 | break; |
| 4391 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4392 | |
| 4393 | // We have to find the starting buffer pointer the hard way, by |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4394 | // deconstructing the source location. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4395 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4396 | if (!CXXUnit) |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 4397 | return createCXString(""); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4398 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4399 | SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); |
| 4400 | std::pair<FileID, unsigned> LocInfo |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4401 | = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4402 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4403 | StringRef Buffer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4404 | = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); |
| 4405 | if (Invalid) |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4406 | return createCXString(""); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4407 | |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4408 | return createCXString(Buffer.substr(LocInfo.second, CXTok.int_data[2])); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4409 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4410 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4411 | CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4412 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4413 | if (!CXXUnit) |
| 4414 | return clang_getNullLocation(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4415 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4416 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), |
| 4417 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 4418 | } |
| 4419 | |
| 4420 | CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4421 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 4422 | if (!CXXUnit) |
| 4423 | return clang_getNullRange(); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4424 | |
| 4425 | return cxloc::translateSourceRange(CXXUnit->getASTContext(), |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4426 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 4427 | } |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4428 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4429 | static void getTokens(ASTUnit *CXXUnit, SourceRange Range, |
| 4430 | SmallVectorImpl<CXToken> &CXTokens) { |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4431 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 4432 | std::pair<FileID, unsigned> BeginLocInfo |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4433 | = SourceMgr.getDecomposedLoc(Range.getBegin()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4434 | std::pair<FileID, unsigned> EndLocInfo |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4435 | = SourceMgr.getDecomposedLoc(Range.getEnd()); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4436 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4437 | // Cannot tokenize across files. |
| 4438 | if (BeginLocInfo.first != EndLocInfo.first) |
| 4439 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4440 | |
| 4441 | // Create a lexer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4442 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4443 | StringRef Buffer |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 4444 | = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
Douglas Gregor | 47a3fcd | 2010-03-16 20:26:15 +0000 | [diff] [blame] | 4445 | if (Invalid) |
| 4446 | return; |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4447 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4448 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 4449 | CXXUnit->getASTContext().getLangOptions(), |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4450 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4451 | Lex.SetCommentRetentionState(true); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4452 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4453 | // Lex tokens until we hit the end of the range. |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 4454 | const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4455 | Token Tok; |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4456 | bool previousWasAt = false; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4457 | do { |
| 4458 | // Lex the next token |
| 4459 | Lex.LexFromRawLexer(Tok); |
| 4460 | if (Tok.is(tok::eof)) |
| 4461 | break; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4462 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4463 | // Initialize the CXToken. |
| 4464 | CXToken CXTok; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4465 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4466 | // - Common fields |
| 4467 | CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); |
| 4468 | CXTok.int_data[2] = Tok.getLength(); |
| 4469 | CXTok.int_data[3] = 0; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4470 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4471 | // - Kind-specific fields |
| 4472 | if (Tok.isLiteral()) { |
| 4473 | CXTok.int_data[0] = CXToken_Literal; |
| 4474 | CXTok.ptr_data = (void *)Tok.getLiteralData(); |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4475 | } else if (Tok.is(tok::raw_identifier)) { |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 4476 | // Lookup the identifier to determine whether we have a keyword. |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4477 | IdentifierInfo *II |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4478 | = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok); |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4479 | |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4480 | if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4481 | CXTok.int_data[0] = CXToken_Keyword; |
| 4482 | } |
| 4483 | else { |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 4484 | CXTok.int_data[0] = Tok.is(tok::identifier) |
| 4485 | ? CXToken_Identifier |
| 4486 | : CXToken_Keyword; |
Ted Kremenek | aa8a66d | 2010-05-05 00:55:20 +0000 | [diff] [blame] | 4487 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4488 | CXTok.ptr_data = II; |
| 4489 | } else if (Tok.is(tok::comment)) { |
| 4490 | CXTok.int_data[0] = CXToken_Comment; |
| 4491 | CXTok.ptr_data = 0; |
| 4492 | } else { |
| 4493 | CXTok.int_data[0] = CXToken_Punctuation; |
| 4494 | CXTok.ptr_data = 0; |
| 4495 | } |
| 4496 | CXTokens.push_back(CXTok); |
David Chisnall | 096428b | 2010-10-13 21:44:48 +0000 | [diff] [blame] | 4497 | previousWasAt = Tok.is(tok::at); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4498 | } while (Lex.getBufferLocation() <= EffectiveBufferEnd); |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4499 | } |
| 4500 | |
| 4501 | void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, |
| 4502 | CXToken **Tokens, unsigned *NumTokens) { |
| 4503 | if (Tokens) |
| 4504 | *Tokens = 0; |
| 4505 | if (NumTokens) |
| 4506 | *NumTokens = 0; |
| 4507 | |
| 4508 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 4509 | if (!CXXUnit || !Tokens || !NumTokens) |
| 4510 | return; |
| 4511 | |
| 4512 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 4513 | |
| 4514 | SourceRange R = cxloc::translateCXSourceRange(Range); |
| 4515 | if (R.isInvalid()) |
| 4516 | return; |
| 4517 | |
| 4518 | SmallVector<CXToken, 32> CXTokens; |
| 4519 | getTokens(CXXUnit, R, CXTokens); |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4520 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4521 | if (CXTokens.empty()) |
| 4522 | return; |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 4523 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4524 | *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size()); |
| 4525 | memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); |
| 4526 | *NumTokens = CXTokens.size(); |
| 4527 | } |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4528 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4529 | void clang_disposeTokens(CXTranslationUnit TU, |
| 4530 | CXToken *Tokens, unsigned NumTokens) { |
| 4531 | free(Tokens); |
| 4532 | } |
| 4533 | |
| 4534 | } // end: extern "C" |
| 4535 | |
| 4536 | //===----------------------------------------------------------------------===// |
| 4537 | // Token annotation APIs. |
| 4538 | //===----------------------------------------------------------------------===// |
| 4539 | |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4540 | typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4541 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 4542 | CXCursor parent, |
| 4543 | CXClientData client_data); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4544 | namespace { |
| 4545 | class AnnotateTokensWorker { |
| 4546 | AnnotateTokensData &Annotated; |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4547 | CXToken *Tokens; |
| 4548 | CXCursor *Cursors; |
| 4549 | unsigned NumTokens; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4550 | unsigned TokIdx; |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4551 | unsigned PreprocessingTokIdx; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4552 | CursorVisitor AnnotateVis; |
| 4553 | SourceManager &SrcMgr; |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4554 | bool HasContextSensitiveKeywords; |
| 4555 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4556 | bool MoreTokens() const { return TokIdx < NumTokens; } |
| 4557 | unsigned NextToken() const { return TokIdx; } |
| 4558 | void AdvanceToken() { ++TokIdx; } |
| 4559 | SourceLocation GetTokenLoc(unsigned tokI) { |
| 4560 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); |
| 4561 | } |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4562 | bool isFunctionMacroToken(unsigned tokI) const { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4563 | return Tokens[tokI].int_data[3] != 0; |
| 4564 | } |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4565 | SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4566 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[3]); |
| 4567 | } |
| 4568 | |
| 4569 | void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4570 | void annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult, |
| 4571 | SourceRange); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4572 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4573 | public: |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4574 | AnnotateTokensWorker(AnnotateTokensData &annotated, |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4575 | CXToken *tokens, CXCursor *cursors, unsigned numTokens, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4576 | CXTranslationUnit tu, SourceRange RegionOfInterest) |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4577 | : Annotated(annotated), Tokens(tokens), Cursors(cursors), |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4578 | NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0), |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 4579 | AnnotateVis(tu, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 4580 | AnnotateTokensVisitor, this, |
| 4581 | /*VisitPreprocessorLast=*/true, |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 4582 | /*VisitIncludedEntities=*/false, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 4583 | RegionOfInterest), |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4584 | SrcMgr(static_cast<ASTUnit*>(tu->TUData)->getSourceManager()), |
| 4585 | HasContextSensitiveKeywords(false) { } |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4586 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4587 | void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4588 | enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 4589 | void AnnotateTokens(); |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4590 | |
| 4591 | /// \brief Determine whether the annotator saw any cursors that have |
| 4592 | /// context-sensitive keywords. |
| 4593 | bool hasContextSensitiveKeywords() const { |
| 4594 | return HasContextSensitiveKeywords; |
| 4595 | } |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4596 | }; |
| 4597 | } |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4598 | |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 4599 | void AnnotateTokensWorker::AnnotateTokens() { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4600 | // Walk the AST within the region of interest, annotating tokens |
| 4601 | // along the way. |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 4602 | AnnotateVis.visitFileRegion(); |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4603 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4604 | for (unsigned I = 0 ; I < TokIdx ; ++I) { |
| 4605 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4606 | if (Pos != Annotated.end() && |
| 4607 | (clang_isInvalid(Cursors[I].kind) || |
| 4608 | Pos->second.kind != CXCursor_PreprocessingDirective)) |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4609 | Cursors[I] = Pos->second; |
| 4610 | } |
| 4611 | |
| 4612 | // Finish up annotating any tokens left. |
| 4613 | if (!MoreTokens()) |
| 4614 | return; |
| 4615 | |
| 4616 | const CXCursor &C = clang_getNullCursor(); |
| 4617 | for (unsigned I = TokIdx ; I < NumTokens ; ++I) { |
Argyrios Kyrtzidis | 03ee2dd | 2011-11-16 08:58:57 +0000 | [diff] [blame] | 4618 | if (I < PreprocessingTokIdx && clang_isPreprocessing(Cursors[I].kind)) |
| 4619 | continue; |
| 4620 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4621 | AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]); |
| 4622 | Cursors[I] = (Pos == Annotated.end()) ? C : Pos->second; |
Ted Kremenek | 11949cb | 2010-05-05 00:55:17 +0000 | [diff] [blame] | 4623 | } |
| 4624 | } |
| 4625 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4626 | /// \brief It annotates and advances tokens with a cursor until the comparison |
| 4627 | //// between the cursor location and the source range is the same as |
| 4628 | /// \arg compResult. |
| 4629 | /// |
| 4630 | /// Pass RangeBefore to annotate tokens with a cursor until a range is reached. |
| 4631 | /// Pass RangeOverlap to annotate tokens inside a range. |
| 4632 | void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC, |
| 4633 | RangeComparisonResult compResult, |
| 4634 | SourceRange range) { |
| 4635 | while (MoreTokens()) { |
| 4636 | const unsigned I = NextToken(); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4637 | if (isFunctionMacroToken(I)) |
| 4638 | return annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4639 | |
| 4640 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4641 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 4642 | Cursors[I] = updateC; |
| 4643 | AdvanceToken(); |
| 4644 | continue; |
| 4645 | } |
| 4646 | break; |
| 4647 | } |
| 4648 | } |
| 4649 | |
| 4650 | /// \brief Special annotation handling for macro argument tokens. |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4651 | void AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens( |
| 4652 | CXCursor updateC, |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4653 | RangeComparisonResult compResult, |
| 4654 | SourceRange range) { |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4655 | assert(MoreTokens()); |
| 4656 | assert(isFunctionMacroToken(NextToken()) && |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4657 | "Should be called only for macro arg tokens"); |
| 4658 | |
| 4659 | // This works differently than annotateAndAdvanceTokens; because expanded |
| 4660 | // macro arguments can have arbitrary translation-unit source order, we do not |
| 4661 | // advance the token index one by one until a token fails the range test. |
| 4662 | // We only advance once past all of the macro arg tokens if all of them |
| 4663 | // pass the range test. If one of them fails we keep the token index pointing |
| 4664 | // at the start of the macro arg tokens so that the failing token will be |
| 4665 | // annotated by a subsequent annotation try. |
| 4666 | |
| 4667 | bool atLeastOneCompFail = false; |
| 4668 | |
| 4669 | unsigned I = NextToken(); |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4670 | for (; I < NumTokens && isFunctionMacroToken(I); ++I) { |
| 4671 | SourceLocation TokLoc = getFunctionMacroTokenLoc(I); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4672 | if (TokLoc.isFileID()) |
| 4673 | continue; // not macro arg token, it's parens or comma. |
| 4674 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 4675 | if (clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 4676 | Cursors[I] = updateC; |
| 4677 | } else |
| 4678 | atLeastOneCompFail = true; |
| 4679 | } |
| 4680 | |
| 4681 | if (!atLeastOneCompFail) |
| 4682 | TokIdx = I; // All of the tokens were handled, advance beyond all of them. |
| 4683 | } |
| 4684 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4685 | enum CXChildVisitResult |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4686 | AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4687 | CXSourceLocation Loc = clang_getCursorLocation(cursor); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4688 | SourceRange cursorRange = getRawCursorExtent(cursor); |
Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 4689 | if (cursorRange.isInvalid()) |
| 4690 | return CXChildVisit_Recurse; |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 4691 | |
| 4692 | if (!HasContextSensitiveKeywords) { |
| 4693 | // Objective-C properties can have context-sensitive keywords. |
| 4694 | if (cursor.kind == CXCursor_ObjCPropertyDecl) { |
| 4695 | if (ObjCPropertyDecl *Property |
| 4696 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor))) |
| 4697 | HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0; |
| 4698 | } |
| 4699 | // Objective-C methods can have context-sensitive keywords. |
| 4700 | else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl || |
| 4701 | cursor.kind == CXCursor_ObjCClassMethodDecl) { |
| 4702 | if (ObjCMethodDecl *Method |
| 4703 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 4704 | if (Method->getObjCDeclQualifier()) |
| 4705 | HasContextSensitiveKeywords = true; |
| 4706 | else { |
| 4707 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), |
| 4708 | PEnd = Method->param_end(); |
| 4709 | P != PEnd; ++P) { |
| 4710 | if ((*P)->getObjCDeclQualifier()) { |
| 4711 | HasContextSensitiveKeywords = true; |
| 4712 | break; |
| 4713 | } |
| 4714 | } |
| 4715 | } |
| 4716 | } |
| 4717 | } |
| 4718 | // C++ methods can have context-sensitive keywords. |
| 4719 | else if (cursor.kind == CXCursor_CXXMethod) { |
| 4720 | if (CXXMethodDecl *Method |
| 4721 | = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) { |
| 4722 | if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>()) |
| 4723 | HasContextSensitiveKeywords = true; |
| 4724 | } |
| 4725 | } |
| 4726 | // C++ classes can have context-sensitive keywords. |
| 4727 | else if (cursor.kind == CXCursor_StructDecl || |
| 4728 | cursor.kind == CXCursor_ClassDecl || |
| 4729 | cursor.kind == CXCursor_ClassTemplate || |
| 4730 | cursor.kind == CXCursor_ClassTemplatePartialSpecialization) { |
| 4731 | if (Decl *D = getCursorDecl(cursor)) |
| 4732 | if (D->hasAttr<FinalAttr>()) |
| 4733 | HasContextSensitiveKeywords = true; |
| 4734 | } |
| 4735 | } |
| 4736 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4737 | if (clang_isPreprocessing(cursor.kind)) { |
Chandler Carruth | cea731a | 2011-07-14 16:07:57 +0000 | [diff] [blame] | 4738 | // For macro expansions, just note where the beginning of the macro |
| 4739 | // expansion occurs. |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 4740 | if (cursor.kind == CXCursor_MacroExpansion) { |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4741 | Annotated[Loc.int_data] = cursor; |
| 4742 | return CXChildVisit_Recurse; |
| 4743 | } |
| 4744 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4745 | // Items in the preprocessing record are kept separate from items in |
| 4746 | // declarations, so we keep a separate token index. |
| 4747 | unsigned SavedTokIdx = TokIdx; |
| 4748 | TokIdx = PreprocessingTokIdx; |
| 4749 | |
| 4750 | // Skip tokens up until we catch up to the beginning of the preprocessing |
| 4751 | // entry. |
| 4752 | while (MoreTokens()) { |
| 4753 | const unsigned I = NextToken(); |
| 4754 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4755 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 4756 | case RangeBefore: |
| 4757 | AdvanceToken(); |
| 4758 | continue; |
| 4759 | case RangeAfter: |
| 4760 | case RangeOverlap: |
| 4761 | break; |
| 4762 | } |
| 4763 | break; |
| 4764 | } |
| 4765 | |
| 4766 | // Look at all of the tokens within this range. |
| 4767 | while (MoreTokens()) { |
| 4768 | const unsigned I = NextToken(); |
| 4769 | SourceLocation TokLoc = GetTokenLoc(I); |
| 4770 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 4771 | case RangeBefore: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4772 | llvm_unreachable("Infeasible"); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4773 | case RangeAfter: |
| 4774 | break; |
| 4775 | case RangeOverlap: |
| 4776 | Cursors[I] = cursor; |
| 4777 | AdvanceToken(); |
| 4778 | continue; |
| 4779 | } |
| 4780 | break; |
| 4781 | } |
| 4782 | |
| 4783 | // Save the preprocessing token index; restore the non-preprocessing |
| 4784 | // token index. |
| 4785 | PreprocessingTokIdx = TokIdx; |
| 4786 | TokIdx = SavedTokIdx; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4787 | return CXChildVisit_Recurse; |
| 4788 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4789 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4790 | if (cursorRange.isInvalid()) |
| 4791 | return CXChildVisit_Continue; |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4792 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4793 | SourceLocation L = SourceLocation::getFromRawEncoding(Loc.int_data); |
| 4794 | |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4795 | // Adjust the annotated range based specific declarations. |
| 4796 | const enum CXCursorKind cursorK = clang_getCursorKind(cursor); |
| 4797 | if (cursorK >= CXCursor_FirstDecl && cursorK <= CXCursor_LastDecl) { |
Ted Kremenek | 23173d7 | 2010-05-18 21:09:07 +0000 | [diff] [blame] | 4798 | Decl *D = cxcursor::getCursorDecl(cursor); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4799 | |
| 4800 | SourceLocation StartLoc; |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 4801 | if (const DeclaratorDecl *DD = dyn_cast_or_null<DeclaratorDecl>(D)) { |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4802 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
| 4803 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 4804 | } else if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(D)) { |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4805 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
| 4806 | StartLoc = TI->getTypeLoc().getSourceRange().getBegin(); |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4807 | } |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 4808 | |
| 4809 | if (StartLoc.isValid() && L.isValid() && |
| 4810 | SrcMgr.isBeforeInTranslationUnit(StartLoc, L)) |
| 4811 | cursorRange.setBegin(StartLoc); |
Ted Kremenek | a333c66 | 2010-05-12 05:29:33 +0000 | [diff] [blame] | 4812 | } |
Douglas Gregor | 81d3c04 | 2010-11-01 20:13:04 +0000 | [diff] [blame] | 4813 | |
Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 4814 | // If the location of the cursor occurs within a macro instantiation, record |
| 4815 | // the spelling location of the cursor in our annotation map. We can then |
| 4816 | // paper over the token labelings during a post-processing step to try and |
| 4817 | // get cursor mappings for tokens that are the *arguments* of a macro |
| 4818 | // instantiation. |
| 4819 | if (L.isMacroID()) { |
| 4820 | unsigned rawEncoding = SrcMgr.getSpellingLoc(L).getRawEncoding(); |
| 4821 | // Only invalidate the old annotation if it isn't part of a preprocessing |
| 4822 | // directive. Here we assume that the default construction of CXCursor |
| 4823 | // results in CXCursor.kind being an initialized value (i.e., 0). If |
| 4824 | // this isn't the case, we can fix by doing lookup + insertion. |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4825 | |
Ted Kremenek | 3f40460 | 2010-08-14 01:14:06 +0000 | [diff] [blame] | 4826 | CXCursor &oldC = Annotated[rawEncoding]; |
| 4827 | if (!clang_isPreprocessing(oldC.kind)) |
| 4828 | oldC = cursor; |
| 4829 | } |
| 4830 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4831 | const enum CXCursorKind K = clang_getCursorKind(parent); |
| 4832 | const CXCursor updateC = |
Ted Kremenek | d8b0a84 | 2010-08-25 22:16:02 +0000 | [diff] [blame] | 4833 | (clang_isInvalid(K) || K == CXCursor_TranslationUnit) |
| 4834 | ? clang_getNullCursor() : parent; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4835 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4836 | annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange); |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4837 | |
Argyrios Kyrtzidis | 5517b89 | 2011-06-27 19:42:20 +0000 | [diff] [blame] | 4838 | // Avoid having the cursor of an expression "overwrite" the annotation of the |
| 4839 | // variable declaration that it belongs to. |
| 4840 | // This can happen for C++ constructor expressions whose range generally |
| 4841 | // include the variable declaration, e.g.: |
| 4842 | // MyCXXClass foo; // Make sure we don't annotate 'foo' as a CallExpr cursor. |
| 4843 | if (clang_isExpression(cursorK)) { |
| 4844 | Expr *E = getCursorExpr(cursor); |
Argyrios Kyrtzidis | 8ccac3d | 2011-06-29 22:20:07 +0000 | [diff] [blame] | 4845 | if (Decl *D = getCursorParentDecl(cursor)) { |
Argyrios Kyrtzidis | 5517b89 | 2011-06-27 19:42:20 +0000 | [diff] [blame] | 4846 | const unsigned I = NextToken(); |
| 4847 | if (E->getLocStart().isValid() && D->getLocation().isValid() && |
| 4848 | E->getLocStart() == D->getLocation() && |
| 4849 | E->getLocStart() == GetTokenLoc(I)) { |
| 4850 | Cursors[I] = updateC; |
| 4851 | AdvanceToken(); |
| 4852 | } |
| 4853 | } |
| 4854 | } |
| 4855 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4856 | // Visit children to get their cursor information. |
| 4857 | const unsigned BeforeChildren = NextToken(); |
| 4858 | VisitChildren(cursor); |
| 4859 | const unsigned AfterChildren = NextToken(); |
| 4860 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4861 | // Scan the tokens that are at the end of the cursor, but are not captured |
| 4862 | // but the child cursors. |
| 4863 | annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange); |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4864 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4865 | // Scan the tokens that are at the beginning of the cursor, but are not |
| 4866 | // capture by the child cursors. |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4867 | for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { |
| 4868 | if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 4869 | break; |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 4870 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4871 | Cursors[I] = cursor; |
| 4872 | } |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4873 | |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 4874 | return CXChildVisit_Continue; |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 4875 | } |
| 4876 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 4877 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 4878 | CXCursor parent, |
| 4879 | CXClientData client_data) { |
| 4880 | return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); |
| 4881 | } |
| 4882 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4883 | namespace { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4884 | |
| 4885 | /// \brief Uses the macro expansions in the preprocessing record to find |
| 4886 | /// and mark tokens that are macro arguments. This info is used by the |
| 4887 | /// AnnotateTokensWorker. |
| 4888 | class MarkMacroArgTokensVisitor { |
| 4889 | SourceManager &SM; |
| 4890 | CXToken *Tokens; |
| 4891 | unsigned NumTokens; |
| 4892 | unsigned CurIdx; |
| 4893 | |
| 4894 | public: |
| 4895 | MarkMacroArgTokensVisitor(SourceManager &SM, |
| 4896 | CXToken *tokens, unsigned numTokens) |
| 4897 | : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { } |
| 4898 | |
| 4899 | CXChildVisitResult visit(CXCursor cursor, CXCursor parent) { |
| 4900 | if (cursor.kind != CXCursor_MacroExpansion) |
| 4901 | return CXChildVisit_Continue; |
| 4902 | |
| 4903 | SourceRange macroRange = getCursorMacroExpansion(cursor)->getSourceRange(); |
| 4904 | if (macroRange.getBegin() == macroRange.getEnd()) |
| 4905 | return CXChildVisit_Continue; // it's not a function macro. |
| 4906 | |
| 4907 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 4908 | if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx), |
| 4909 | macroRange.getBegin())) |
| 4910 | break; |
| 4911 | } |
| 4912 | |
| 4913 | if (CurIdx == NumTokens) |
| 4914 | return CXChildVisit_Break; |
| 4915 | |
| 4916 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 4917 | SourceLocation tokLoc = getTokenLoc(CurIdx); |
| 4918 | if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd())) |
| 4919 | break; |
| 4920 | |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4921 | setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc)); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4922 | } |
| 4923 | |
| 4924 | if (CurIdx == NumTokens) |
| 4925 | return CXChildVisit_Break; |
| 4926 | |
| 4927 | return CXChildVisit_Continue; |
| 4928 | } |
| 4929 | |
| 4930 | private: |
| 4931 | SourceLocation getTokenLoc(unsigned tokI) { |
| 4932 | return SourceLocation::getFromRawEncoding(Tokens[tokI].int_data[1]); |
| 4933 | } |
| 4934 | |
Argyrios Kyrtzidis | 5f616b7 | 2011-08-30 19:43:19 +0000 | [diff] [blame] | 4935 | void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) { |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 4936 | // The third field is reserved and currently not used. Use it here |
| 4937 | // to mark macro arg expanded tokens with their expanded locations. |
| 4938 | Tokens[tokI].int_data[3] = loc.getRawEncoding(); |
| 4939 | } |
| 4940 | }; |
| 4941 | |
| 4942 | } // end anonymous namespace |
| 4943 | |
| 4944 | static CXChildVisitResult |
| 4945 | MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent, |
| 4946 | CXClientData client_data) { |
| 4947 | return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor, |
| 4948 | parent); |
| 4949 | } |
| 4950 | |
| 4951 | namespace { |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 4952 | struct clang_annotateTokens_Data { |
| 4953 | CXTranslationUnit TU; |
| 4954 | ASTUnit *CXXUnit; |
| 4955 | CXToken *Tokens; |
| 4956 | unsigned NumTokens; |
| 4957 | CXCursor *Cursors; |
| 4958 | }; |
| 4959 | } |
| 4960 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 4961 | static void annotatePreprocessorTokens(CXTranslationUnit TU, |
| 4962 | SourceRange RegionOfInterest, |
| 4963 | AnnotateTokensData &Annotated) { |
| 4964 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
| 4965 | |
| 4966 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 4967 | std::pair<FileID, unsigned> BeginLocInfo |
| 4968 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin()); |
| 4969 | std::pair<FileID, unsigned> EndLocInfo |
| 4970 | = SourceMgr.getDecomposedLoc(RegionOfInterest.getEnd()); |
| 4971 | |
| 4972 | if (BeginLocInfo.first != EndLocInfo.first) |
| 4973 | return; |
| 4974 | |
| 4975 | StringRef Buffer; |
| 4976 | bool Invalid = false; |
| 4977 | Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 4978 | if (Buffer.empty() || Invalid) |
| 4979 | return; |
| 4980 | |
| 4981 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 4982 | CXXUnit->getASTContext().getLangOptions(), |
| 4983 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, |
| 4984 | Buffer.end()); |
| 4985 | Lex.SetCommentRetentionState(true); |
| 4986 | |
| 4987 | // Lex tokens in raw mode until we hit the end of the range, to avoid |
| 4988 | // entering #includes or expanding macros. |
| 4989 | while (true) { |
| 4990 | Token Tok; |
| 4991 | Lex.LexFromRawLexer(Tok); |
| 4992 | |
| 4993 | reprocess: |
| 4994 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
| 4995 | // We have found a preprocessing directive. Gobble it up so that we |
| 4996 | // don't see it while preprocessing these tokens later, but keep track |
| 4997 | // of all of the token locations inside this preprocessing directive so |
| 4998 | // that we can annotate them appropriately. |
| 4999 | // |
| 5000 | // FIXME: Some simple tests here could identify macro definitions and |
| 5001 | // #undefs, to provide specific cursor kinds for those. |
| 5002 | SmallVector<SourceLocation, 32> Locations; |
| 5003 | do { |
| 5004 | Locations.push_back(Tok.getLocation()); |
| 5005 | Lex.LexFromRawLexer(Tok); |
| 5006 | } while (!Tok.isAtStartOfLine() && !Tok.is(tok::eof)); |
| 5007 | |
| 5008 | using namespace cxcursor; |
| 5009 | CXCursor Cursor |
| 5010 | = MakePreprocessingDirectiveCursor(SourceRange(Locations.front(), |
| 5011 | Locations.back()), |
| 5012 | TU); |
| 5013 | for (unsigned I = 0, N = Locations.size(); I != N; ++I) { |
| 5014 | Annotated[Locations[I].getRawEncoding()] = Cursor; |
| 5015 | } |
| 5016 | |
| 5017 | if (Tok.isAtStartOfLine()) |
| 5018 | goto reprocess; |
| 5019 | |
| 5020 | continue; |
| 5021 | } |
| 5022 | |
| 5023 | if (Tok.is(tok::eof)) |
| 5024 | break; |
| 5025 | } |
| 5026 | } |
| 5027 | |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5028 | // This gets run a separate thread to avoid stack blowout. |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5029 | static void clang_annotateTokensImpl(void *UserData) { |
| 5030 | CXTranslationUnit TU = ((clang_annotateTokens_Data*)UserData)->TU; |
| 5031 | ASTUnit *CXXUnit = ((clang_annotateTokens_Data*)UserData)->CXXUnit; |
| 5032 | CXToken *Tokens = ((clang_annotateTokens_Data*)UserData)->Tokens; |
| 5033 | const unsigned NumTokens = ((clang_annotateTokens_Data*)UserData)->NumTokens; |
| 5034 | CXCursor *Cursors = ((clang_annotateTokens_Data*)UserData)->Cursors; |
| 5035 | |
| 5036 | // Determine the region of interest, which contains all of the tokens. |
| 5037 | SourceRange RegionOfInterest; |
| 5038 | RegionOfInterest.setBegin( |
| 5039 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0]))); |
| 5040 | RegionOfInterest.setEnd( |
| 5041 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, |
| 5042 | Tokens[NumTokens-1]))); |
| 5043 | |
| 5044 | // A mapping from the source locations found when re-lexing or traversing the |
| 5045 | // region of interest to the corresponding cursors. |
| 5046 | AnnotateTokensData Annotated; |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 5047 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5048 | // Relex the tokens within the source range to look for preprocessing |
| 5049 | // directives. |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 5050 | annotatePreprocessorTokens(TU, RegionOfInterest, Annotated); |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5051 | |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5052 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) { |
| 5053 | // Search and mark tokens that are macro argument expansions. |
| 5054 | MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(), |
| 5055 | Tokens, NumTokens); |
| 5056 | CursorVisitor MacroArgMarker(TU, |
| 5057 | MarkMacroArgTokensVisitorDelegate, &Visitor, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 5058 | /*VisitPreprocessorLast=*/true, |
Argyrios Kyrtzidis | e709846 | 2011-10-31 07:19:54 +0000 | [diff] [blame] | 5059 | /*VisitIncludedEntities=*/false, |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 5060 | RegionOfInterest); |
Argyrios Kyrtzidis | a676379 | 2011-08-18 18:03:34 +0000 | [diff] [blame] | 5061 | MacroArgMarker.visitPreprocessedEntitiesInRegion(); |
| 5062 | } |
| 5063 | |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5064 | // Annotate all of the source locations in the region of interest that map to |
| 5065 | // a specific cursor. |
| 5066 | AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens, |
| 5067 | TU, RegionOfInterest); |
| 5068 | |
| 5069 | // FIXME: We use a ridiculous stack size here because the data-recursion |
| 5070 | // algorithm uses a large stack frame than the non-data recursive version, |
| 5071 | // and AnnotationTokensWorker currently transforms the data-recursion |
| 5072 | // algorithm back into a traditional recursion by explicitly calling |
| 5073 | // VisitChildren(). We will need to remove this explicit recursive call. |
| 5074 | W.AnnotateTokens(); |
| 5075 | |
| 5076 | // If we ran into any entities that involve context-sensitive keywords, |
| 5077 | // take another pass through the tokens to mark them as such. |
| 5078 | if (W.hasContextSensitiveKeywords()) { |
| 5079 | for (unsigned I = 0; I != NumTokens; ++I) { |
| 5080 | if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier) |
| 5081 | continue; |
| 5082 | |
| 5083 | if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) { |
| 5084 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 5085 | if (ObjCPropertyDecl *Property |
| 5086 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) { |
| 5087 | if (Property->getPropertyAttributesAsWritten() != 0 && |
| 5088 | llvm::StringSwitch<bool>(II->getName()) |
| 5089 | .Case("readonly", true) |
| 5090 | .Case("assign", true) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5091 | .Case("unsafe_unretained", true) |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5092 | .Case("readwrite", true) |
| 5093 | .Case("retain", true) |
| 5094 | .Case("copy", true) |
| 5095 | .Case("nonatomic", true) |
| 5096 | .Case("atomic", true) |
| 5097 | .Case("getter", true) |
| 5098 | .Case("setter", true) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5099 | .Case("strong", true) |
| 5100 | .Case("weak", true) |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5101 | .Default(false)) |
| 5102 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5103 | } |
| 5104 | continue; |
| 5105 | } |
| 5106 | |
| 5107 | if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl || |
| 5108 | Cursors[I].kind == CXCursor_ObjCClassMethodDecl) { |
| 5109 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 5110 | if (llvm::StringSwitch<bool>(II->getName()) |
| 5111 | .Case("in", true) |
| 5112 | .Case("out", true) |
| 5113 | .Case("inout", true) |
| 5114 | .Case("oneway", true) |
| 5115 | .Case("bycopy", true) |
| 5116 | .Case("byref", true) |
| 5117 | .Default(false)) |
| 5118 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 5119 | continue; |
| 5120 | } |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 5121 | |
| 5122 | if (Cursors[I].kind == CXCursor_CXXFinalAttr || |
| 5123 | Cursors[I].kind == CXCursor_CXXOverrideAttr) { |
| 5124 | Tokens[I].int_data[0] = CXToken_Keyword; |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5125 | continue; |
| 5126 | } |
| 5127 | } |
| 5128 | } |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5129 | } |
| 5130 | |
Ted Kremenek | 6db6109 | 2010-05-05 00:55:15 +0000 | [diff] [blame] | 5131 | extern "C" { |
| 5132 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5133 | void clang_annotateTokens(CXTranslationUnit TU, |
| 5134 | CXToken *Tokens, unsigned NumTokens, |
| 5135 | CXCursor *Cursors) { |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5136 | |
| 5137 | if (NumTokens == 0 || !Tokens || !Cursors) |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5138 | return; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5139 | |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5140 | // Any token we don't specifically annotate will have a NULL cursor. |
| 5141 | CXCursor C = clang_getNullCursor(); |
| 5142 | for (unsigned I = 0; I != NumTokens; ++I) |
| 5143 | Cursors[I] = C; |
| 5144 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5145 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 4419b67 | 2010-10-21 06:10:04 +0000 | [diff] [blame] | 5146 | if (!CXXUnit) |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 5147 | return; |
Ted Kremenek | fbd84ca | 2010-05-05 00:55:23 +0000 | [diff] [blame] | 5148 | |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 5149 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5150 | |
| 5151 | clang_annotateTokens_Data data = { TU, CXXUnit, Tokens, NumTokens, Cursors }; |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5152 | llvm::CrashRecoveryContext CRC; |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5153 | if (!RunSafely(CRC, clang_annotateTokensImpl, &data, |
Ted Kremenek | 6c53fdd | 2010-11-14 17:47:35 +0000 | [diff] [blame] | 5154 | GetSafetyThreadStackSize() * 2)) { |
Ted Kremenek | ab97961 | 2010-11-11 08:05:23 +0000 | [diff] [blame] | 5155 | fprintf(stderr, "libclang: crash detected while annotating tokens\n"); |
| 5156 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5157 | } |
Ted Kremenek | 6628a61 | 2011-03-18 22:51:30 +0000 | [diff] [blame] | 5158 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 5159 | } // end: extern "C" |
| 5160 | |
| 5161 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 5162 | // Operations for querying linkage of a cursor. |
| 5163 | //===----------------------------------------------------------------------===// |
| 5164 | |
| 5165 | extern "C" { |
| 5166 | CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { |
Douglas Gregor | 0396f46 | 2010-03-19 05:22:59 +0000 | [diff] [blame] | 5167 | if (!clang_isDeclaration(cursor.kind)) |
| 5168 | return CXLinkage_Invalid; |
| 5169 | |
Ted Kremenek | 16b4259 | 2010-03-03 06:36:57 +0000 | [diff] [blame] | 5170 | Decl *D = cxcursor::getCursorDecl(cursor); |
| 5171 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
| 5172 | switch (ND->getLinkage()) { |
| 5173 | case NoLinkage: return CXLinkage_NoLinkage; |
| 5174 | case InternalLinkage: return CXLinkage_Internal; |
| 5175 | case UniqueExternalLinkage: return CXLinkage_UniqueExternal; |
| 5176 | case ExternalLinkage: return CXLinkage_External; |
| 5177 | }; |
| 5178 | |
| 5179 | return CXLinkage_Invalid; |
| 5180 | } |
| 5181 | } // end: extern "C" |
| 5182 | |
| 5183 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5184 | // Operations for querying language of a cursor. |
| 5185 | //===----------------------------------------------------------------------===// |
| 5186 | |
| 5187 | static CXLanguageKind getDeclLanguage(const Decl *D) { |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 5188 | if (!D) |
| 5189 | return CXLanguage_C; |
| 5190 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5191 | switch (D->getKind()) { |
| 5192 | default: |
| 5193 | break; |
| 5194 | case Decl::ImplicitParam: |
| 5195 | case Decl::ObjCAtDefsField: |
| 5196 | case Decl::ObjCCategory: |
| 5197 | case Decl::ObjCCategoryImpl: |
| 5198 | case Decl::ObjCClass: |
| 5199 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5200 | case Decl::ObjCForwardProtocol: |
| 5201 | case Decl::ObjCImplementation: |
| 5202 | case Decl::ObjCInterface: |
| 5203 | case Decl::ObjCIvar: |
| 5204 | case Decl::ObjCMethod: |
| 5205 | case Decl::ObjCProperty: |
| 5206 | case Decl::ObjCPropertyImpl: |
| 5207 | case Decl::ObjCProtocol: |
| 5208 | return CXLanguage_ObjC; |
| 5209 | case Decl::CXXConstructor: |
| 5210 | case Decl::CXXConversion: |
| 5211 | case Decl::CXXDestructor: |
| 5212 | case Decl::CXXMethod: |
| 5213 | case Decl::CXXRecord: |
| 5214 | case Decl::ClassTemplate: |
| 5215 | case Decl::ClassTemplatePartialSpecialization: |
| 5216 | case Decl::ClassTemplateSpecialization: |
| 5217 | case Decl::Friend: |
| 5218 | case Decl::FriendTemplate: |
| 5219 | case Decl::FunctionTemplate: |
| 5220 | case Decl::LinkageSpec: |
| 5221 | case Decl::Namespace: |
| 5222 | case Decl::NamespaceAlias: |
| 5223 | case Decl::NonTypeTemplateParm: |
| 5224 | case Decl::StaticAssert: |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5225 | case Decl::TemplateTemplateParm: |
| 5226 | case Decl::TemplateTypeParm: |
| 5227 | case Decl::UnresolvedUsingTypename: |
| 5228 | case Decl::UnresolvedUsingValue: |
| 5229 | case Decl::Using: |
| 5230 | case Decl::UsingDirective: |
| 5231 | case Decl::UsingShadow: |
| 5232 | return CXLanguage_CPlusPlus; |
| 5233 | } |
| 5234 | |
| 5235 | return CXLanguage_C; |
| 5236 | } |
| 5237 | |
| 5238 | extern "C" { |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5239 | |
| 5240 | enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { |
| 5241 | if (clang_isDeclaration(cursor.kind)) |
| 5242 | if (Decl *D = cxcursor::getCursorDecl(cursor)) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5243 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()) |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5244 | return CXAvailability_Available; |
| 5245 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5246 | switch (D->getAvailability()) { |
| 5247 | case AR_Available: |
| 5248 | case AR_NotYetIntroduced: |
| 5249 | return CXAvailability_Available; |
| 5250 | |
| 5251 | case AR_Deprecated: |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5252 | return CXAvailability_Deprecated; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5253 | |
| 5254 | case AR_Unavailable: |
| 5255 | return CXAvailability_NotAvailable; |
| 5256 | } |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5257 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5258 | |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 5259 | return CXAvailability_Available; |
| 5260 | } |
| 5261 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5262 | CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { |
| 5263 | if (clang_isDeclaration(cursor.kind)) |
| 5264 | return getDeclLanguage(cxcursor::getCursorDecl(cursor)); |
| 5265 | |
| 5266 | return CXLanguage_Invalid; |
| 5267 | } |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5268 | |
| 5269 | /// \brief If the given cursor is the "templated" declaration |
| 5270 | /// descibing a class or function template, return the class or |
| 5271 | /// function template. |
| 5272 | static Decl *maybeGetTemplateCursor(Decl *D) { |
| 5273 | if (!D) |
| 5274 | return 0; |
| 5275 | |
| 5276 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5277 | if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate()) |
| 5278 | return FunTmpl; |
| 5279 | |
| 5280 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
| 5281 | if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate()) |
| 5282 | return ClassTmpl; |
| 5283 | |
| 5284 | return D; |
| 5285 | } |
| 5286 | |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5287 | CXCursor clang_getCursorSemanticParent(CXCursor cursor) { |
| 5288 | if (clang_isDeclaration(cursor.kind)) { |
| 5289 | if (Decl *D = getCursorDecl(cursor)) { |
| 5290 | DeclContext *DC = D->getDeclContext(); |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5291 | if (!DC) |
| 5292 | return clang_getNullCursor(); |
| 5293 | |
| 5294 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 5295 | getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5296 | } |
| 5297 | } |
| 5298 | |
| 5299 | if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { |
| 5300 | if (Decl *D = getCursorDecl(cursor)) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5301 | return MakeCXCursor(D, getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5302 | } |
| 5303 | |
| 5304 | return clang_getNullCursor(); |
| 5305 | } |
| 5306 | |
| 5307 | CXCursor clang_getCursorLexicalParent(CXCursor cursor) { |
| 5308 | if (clang_isDeclaration(cursor.kind)) { |
| 5309 | if (Decl *D = getCursorDecl(cursor)) { |
| 5310 | DeclContext *DC = D->getLexicalDeclContext(); |
Douglas Gregor | 3910cfd | 2010-12-21 07:55:45 +0000 | [diff] [blame] | 5311 | if (!DC) |
| 5312 | return clang_getNullCursor(); |
| 5313 | |
| 5314 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 5315 | getCursorTU(cursor)); |
Douglas Gregor | 2be5bc9 | 2010-09-22 21:22:29 +0000 | [diff] [blame] | 5316 | } |
| 5317 | } |
| 5318 | |
| 5319 | // FIXME: Note that we can't easily compute the lexical context of a |
| 5320 | // statement or expression, so we return nothing. |
| 5321 | return clang_getNullCursor(); |
| 5322 | } |
| 5323 | |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5324 | void clang_getOverriddenCursors(CXCursor cursor, |
| 5325 | CXCursor **overridden, |
| 5326 | unsigned *num_overridden) { |
| 5327 | if (overridden) |
| 5328 | *overridden = 0; |
| 5329 | if (num_overridden) |
| 5330 | *num_overridden = 0; |
| 5331 | if (!overridden || !num_overridden) |
| 5332 | return; |
| 5333 | |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 5334 | SmallVector<CXCursor, 8> Overridden; |
| 5335 | cxcursor::getOverriddenCursors(cursor, Overridden); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5336 | |
Ted Kremenek | 2407712 | 2011-11-14 23:51:37 +0000 | [diff] [blame] | 5337 | // Don't allocate memory if we have no overriden cursors. |
| 5338 | if (Overridden.size() == 0) |
| 5339 | return; |
| 5340 | |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 5341 | *num_overridden = Overridden.size(); |
| 5342 | *overridden = new CXCursor [Overridden.size()]; |
| 5343 | std::copy(Overridden.begin(), Overridden.end(), *overridden); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 5344 | } |
| 5345 | |
| 5346 | void clang_disposeOverriddenCursors(CXCursor *overridden) { |
| 5347 | delete [] overridden; |
| 5348 | } |
| 5349 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 5350 | CXFile clang_getIncludedFile(CXCursor cursor) { |
| 5351 | if (cursor.kind != CXCursor_InclusionDirective) |
| 5352 | return 0; |
| 5353 | |
| 5354 | InclusionDirective *ID = getCursorInclusionDirective(cursor); |
| 5355 | return (void *)ID->getFile(); |
| 5356 | } |
| 5357 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5358 | } // end: extern "C" |
| 5359 | |
Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 5360 | |
| 5361 | //===----------------------------------------------------------------------===// |
| 5362 | // C++ AST instrospection. |
| 5363 | //===----------------------------------------------------------------------===// |
| 5364 | |
| 5365 | extern "C" { |
| 5366 | unsigned clang_CXXMethod_isStatic(CXCursor C) { |
| 5367 | if (!clang_isDeclaration(C.kind)) |
| 5368 | return 0; |
Douglas Gregor | 49f6f54 | 2010-08-31 22:12:17 +0000 | [diff] [blame] | 5369 | |
| 5370 | CXXMethodDecl *Method = 0; |
| 5371 | Decl *D = cxcursor::getCursorDecl(C); |
| 5372 | if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 5373 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 5374 | else |
| 5375 | Method = dyn_cast_or_null<CXXMethodDecl>(D); |
| 5376 | return (Method && Method->isStatic()) ? 1 : 0; |
Ted Kremenek | 40b492a | 2010-05-17 20:12:45 +0000 | [diff] [blame] | 5377 | } |
Ted Kremenek | b12903e | 2010-05-18 22:32:15 +0000 | [diff] [blame] | 5378 | |
Douglas Gregor | 211924b | 2011-05-12 15:17:24 +0000 | [diff] [blame] | 5379 | unsigned clang_CXXMethod_isVirtual(CXCursor C) { |
| 5380 | if (!clang_isDeclaration(C.kind)) |
| 5381 | return 0; |
| 5382 | |
| 5383 | CXXMethodDecl *Method = 0; |
| 5384 | Decl *D = cxcursor::getCursorDecl(C); |
| 5385 | if (FunctionTemplateDecl *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 5386 | Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 5387 | else |
| 5388 | Method = dyn_cast_or_null<CXXMethodDecl>(D); |
| 5389 | return (Method && Method->isVirtual()) ? 1 : 0; |
| 5390 | } |
Ted Kremenek | 9ada39a | 2010-05-17 20:06:56 +0000 | [diff] [blame] | 5391 | } // end: extern "C" |
| 5392 | |
Ted Kremenek | 45e1dae | 2010-04-12 21:22:16 +0000 | [diff] [blame] | 5393 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5394 | // Attribute introspection. |
| 5395 | //===----------------------------------------------------------------------===// |
| 5396 | |
| 5397 | extern "C" { |
| 5398 | CXType clang_getIBOutletCollectionType(CXCursor C) { |
| 5399 | if (C.kind != CXCursor_IBOutletCollectionAttr) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 5400 | return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C)); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5401 | |
| 5402 | IBOutletCollectionAttr *A = |
| 5403 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); |
| 5404 | |
Argyrios Kyrtzidis | 18aa2ff | 2011-09-13 18:49:52 +0000 | [diff] [blame] | 5405 | return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C)); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 5406 | } |
| 5407 | } // end: extern "C" |
| 5408 | |
| 5409 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5410 | // Inspecting memory usage. |
| 5411 | //===----------------------------------------------------------------------===// |
| 5412 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5413 | typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5414 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5415 | static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries, |
| 5416 | enum CXTUResourceUsageKind k, |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5417 | unsigned long amount) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5418 | CXTUResourceUsageEntry entry = { k, amount }; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5419 | entries.push_back(entry); |
| 5420 | } |
| 5421 | |
| 5422 | extern "C" { |
| 5423 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5424 | const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5425 | const char *str = ""; |
| 5426 | switch (kind) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5427 | case CXTUResourceUsage_AST: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5428 | str = "ASTContext: expressions, declarations, and types"; |
| 5429 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5430 | case CXTUResourceUsage_Identifiers: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5431 | str = "ASTContext: identifiers"; |
| 5432 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5433 | case CXTUResourceUsage_Selectors: |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5434 | str = "ASTContext: selectors"; |
Ted Kremenek | e294ab7 | 2011-04-19 04:36:17 +0000 | [diff] [blame] | 5435 | break; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5436 | case CXTUResourceUsage_GlobalCompletionResults: |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5437 | str = "Code completion: cached global results"; |
Ted Kremenek | e294ab7 | 2011-04-19 04:36:17 +0000 | [diff] [blame] | 5438 | break; |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 5439 | case CXTUResourceUsage_SourceManagerContentCache: |
| 5440 | str = "SourceManager: content cache allocator"; |
| 5441 | break; |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5442 | case CXTUResourceUsage_AST_SideTables: |
| 5443 | str = "ASTContext: side tables"; |
| 5444 | break; |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5445 | case CXTUResourceUsage_SourceManager_Membuffer_Malloc: |
| 5446 | str = "SourceManager: malloc'ed memory buffers"; |
| 5447 | break; |
| 5448 | case CXTUResourceUsage_SourceManager_Membuffer_MMap: |
| 5449 | str = "SourceManager: mmap'ed memory buffers"; |
| 5450 | break; |
Ted Kremenek | e9b5f3d | 2011-04-28 23:46:20 +0000 | [diff] [blame] | 5451 | case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc: |
| 5452 | str = "ExternalASTSource: malloc'ed memory buffers"; |
| 5453 | break; |
| 5454 | case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap: |
| 5455 | str = "ExternalASTSource: mmap'ed memory buffers"; |
| 5456 | break; |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5457 | case CXTUResourceUsage_Preprocessor: |
| 5458 | str = "Preprocessor: malloc'ed memory"; |
| 5459 | break; |
| 5460 | case CXTUResourceUsage_PreprocessingRecord: |
| 5461 | str = "Preprocessor: PreprocessingRecord"; |
| 5462 | break; |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5463 | case CXTUResourceUsage_SourceManager_DataStructures: |
| 5464 | str = "SourceManager: data structures and tables"; |
| 5465 | break; |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 5466 | case CXTUResourceUsage_Preprocessor_HeaderSearch: |
| 5467 | str = "Preprocessor: header search tables"; |
| 5468 | break; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5469 | } |
| 5470 | return str; |
| 5471 | } |
| 5472 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5473 | CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5474 | if (!TU) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5475 | CXTUResourceUsage usage = { (void*) 0, 0, 0 }; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5476 | return usage; |
| 5477 | } |
| 5478 | |
| 5479 | ASTUnit *astUnit = static_cast<ASTUnit*>(TU->TUData); |
| 5480 | llvm::OwningPtr<MemUsageEntries> entries(new MemUsageEntries()); |
| 5481 | ASTContext &astContext = astUnit->getASTContext(); |
| 5482 | |
| 5483 | // How much memory is used by AST nodes and types? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5484 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST, |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5485 | (unsigned long) astContext.getASTAllocatedMemory()); |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5486 | |
| 5487 | // How much memory is used by identifiers? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5488 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5489 | (unsigned long) astContext.Idents.getAllocator().getTotalMemory()); |
| 5490 | |
| 5491 | // How much memory is used for selectors? |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5492 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5493 | (unsigned long) astContext.Selectors.getTotalMemory()); |
| 5494 | |
Ted Kremenek | ba29bd2 | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 5495 | // How much memory is used by ASTContext's side tables? |
| 5496 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables, |
| 5497 | (unsigned long) astContext.getSideTableAllocatedMemory()); |
| 5498 | |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5499 | // How much memory is used for caching global code completion results? |
| 5500 | unsigned long completionBytes = 0; |
| 5501 | if (GlobalCodeCompletionAllocator *completionAllocator = |
| 5502 | astUnit->getCachedCompletionAllocator().getPtr()) { |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5503 | completionBytes = completionAllocator->getTotalMemory(); |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 5504 | } |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 5505 | createCXTUResourceUsageEntry(*entries, |
| 5506 | CXTUResourceUsage_GlobalCompletionResults, |
| 5507 | completionBytes); |
| 5508 | |
| 5509 | // How much memory is being used by SourceManager's content cache? |
| 5510 | createCXTUResourceUsageEntry(*entries, |
| 5511 | CXTUResourceUsage_SourceManagerContentCache, |
| 5512 | (unsigned long) astContext.getSourceManager().getContentCacheSize()); |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5513 | |
| 5514 | // How much memory is being used by the MemoryBuffer's in SourceManager? |
| 5515 | const SourceManager::MemoryBufferSizes &srcBufs = |
| 5516 | astUnit->getSourceManager().getMemoryBufferSizes(); |
| 5517 | |
| 5518 | createCXTUResourceUsageEntry(*entries, |
| 5519 | CXTUResourceUsage_SourceManager_Membuffer_Malloc, |
| 5520 | (unsigned long) srcBufs.malloc_bytes); |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5521 | createCXTUResourceUsageEntry(*entries, |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 5522 | CXTUResourceUsage_SourceManager_Membuffer_MMap, |
| 5523 | (unsigned long) srcBufs.mmap_bytes); |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 5524 | createCXTUResourceUsageEntry(*entries, |
| 5525 | CXTUResourceUsage_SourceManager_DataStructures, |
| 5526 | (unsigned long) astContext.getSourceManager() |
| 5527 | .getDataStructureSizes()); |
Ted Kremenek | e9b5f3d | 2011-04-28 23:46:20 +0000 | [diff] [blame] | 5528 | |
| 5529 | // How much memory is being used by the ExternalASTSource? |
| 5530 | if (ExternalASTSource *esrc = astContext.getExternalSource()) { |
| 5531 | const ExternalASTSource::MemoryBufferSizes &sizes = |
| 5532 | esrc->getMemoryBufferSizes(); |
| 5533 | |
| 5534 | createCXTUResourceUsageEntry(*entries, |
| 5535 | CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc, |
| 5536 | (unsigned long) sizes.malloc_bytes); |
| 5537 | createCXTUResourceUsageEntry(*entries, |
| 5538 | CXTUResourceUsage_ExternalASTSource_Membuffer_MMap, |
| 5539 | (unsigned long) sizes.mmap_bytes); |
| 5540 | } |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5541 | |
| 5542 | // How much memory is being used by the Preprocessor? |
| 5543 | Preprocessor &pp = astUnit->getPreprocessor(); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5544 | createCXTUResourceUsageEntry(*entries, |
| 5545 | CXTUResourceUsage_Preprocessor, |
Argyrios Kyrtzidis | c5c5e92 | 2011-06-29 22:20:04 +0000 | [diff] [blame] | 5546 | pp.getTotalMemory()); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5547 | |
| 5548 | if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) { |
| 5549 | createCXTUResourceUsageEntry(*entries, |
| 5550 | CXTUResourceUsage_PreprocessingRecord, |
| 5551 | pRec->getTotalMemory()); |
| 5552 | } |
| 5553 | |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 5554 | createCXTUResourceUsageEntry(*entries, |
| 5555 | CXTUResourceUsage_Preprocessor_HeaderSearch, |
| 5556 | pp.getHeaderSearchInfo().getTotalMemory()); |
Ted Kremenek | 5e1db6a | 2011-05-04 01:38:46 +0000 | [diff] [blame] | 5557 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5558 | CXTUResourceUsage usage = { (void*) entries.get(), |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5559 | (unsigned) entries->size(), |
| 5560 | entries->size() ? &(*entries)[0] : 0 }; |
| 5561 | entries.take(); |
| 5562 | return usage; |
| 5563 | } |
| 5564 | |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 5565 | void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) { |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5566 | if (usage.data) |
| 5567 | delete (MemUsageEntries*) usage.data; |
| 5568 | } |
| 5569 | |
| 5570 | } // end extern "C" |
| 5571 | |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 5572 | void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) { |
| 5573 | CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU); |
| 5574 | for (unsigned I = 0; I != Usage.numEntries; ++I) |
| 5575 | fprintf(stderr, " %s: %lu\n", |
| 5576 | clang_getTUResourceUsageName(Usage.entries[I].kind), |
| 5577 | Usage.entries[I].amount); |
| 5578 | |
| 5579 | clang_disposeCXTUResourceUsage(Usage); |
| 5580 | } |
| 5581 | |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5582 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5583 | // Misc. utility functions. |
| 5584 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f0e23e8 | 2010-02-17 00:41:40 +0000 | [diff] [blame] | 5585 | |
Daniel Dunbar | abdce7a | 2010-11-05 17:21:46 +0000 | [diff] [blame] | 5586 | /// Default to using an 8 MB stack size on "safety" threads. |
| 5587 | static unsigned SafetyStackThreadSize = 8 << 20; |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 5588 | |
| 5589 | namespace clang { |
| 5590 | |
| 5591 | bool RunSafely(llvm::CrashRecoveryContext &CRC, |
Ted Kremenek | 6c53fdd | 2010-11-14 17:47:35 +0000 | [diff] [blame] | 5592 | void (*Fn)(void*), void *UserData, |
| 5593 | unsigned Size) { |
| 5594 | if (!Size) |
| 5595 | Size = GetSafetyThreadStackSize(); |
| 5596 | if (Size) |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 5597 | return CRC.RunSafelyOnThread(Fn, UserData, Size); |
| 5598 | return CRC.RunSafely(Fn, UserData); |
| 5599 | } |
| 5600 | |
| 5601 | unsigned GetSafetyThreadStackSize() { |
| 5602 | return SafetyStackThreadSize; |
| 5603 | } |
| 5604 | |
| 5605 | void SetSafetyThreadStackSize(unsigned Value) { |
| 5606 | SafetyStackThreadSize = Value; |
| 5607 | } |
| 5608 | |
| 5609 | } |
| 5610 | |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5611 | extern "C" { |
| 5612 | |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 5613 | CXString clang_getClangVersion() { |
Ted Kremenek | ee4db4f | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 5614 | return createCXString(getClangFullVersion()); |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 5615 | } |
| 5616 | |
| 5617 | } // end: extern "C" |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 5618 | |