Guy Benyei | 11169dd | 2012-12-18 14:30:41 +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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the main API hooks in the Clang-C Source Indexing |
| 11 | // library. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 15 | #include "CIndexDiagnostic.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 16 | #include "CIndexer.h" |
Chandler Carruth | 4b41745 | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 17 | #include "CLog.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 18 | #include "CXCursor.h" |
| 19 | #include "CXSourceLocation.h" |
| 20 | #include "CXString.h" |
| 21 | #include "CXTranslationUnit.h" |
| 22 | #include "CXType.h" |
| 23 | #include "CursorVisitor.h" |
David Blaikie | 0a4e61f | 2013-09-13 18:32:52 +0000 | [diff] [blame] | 24 | #include "clang/AST/Attr.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 25 | #include "clang/AST/StmtVisitor.h" |
| 26 | #include "clang/Basic/Diagnostic.h" |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 27 | #include "clang/Basic/DiagnosticCategories.h" |
| 28 | #include "clang/Basic/DiagnosticIDs.h" |
Emilio Cobos Alvarez | 485ad42 | 2017-04-28 15:56:39 +0000 | [diff] [blame] | 29 | #include "clang/Basic/TargetInfo.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 30 | #include "clang/Basic/Version.h" |
| 31 | #include "clang/Frontend/ASTUnit.h" |
| 32 | #include "clang/Frontend/CompilerInstance.h" |
| 33 | #include "clang/Frontend/FrontendDiagnostic.h" |
Argyrios Kyrtzidis | ca741ce | 2016-02-14 22:30:14 +0000 | [diff] [blame] | 34 | #include "clang/Index/CodegenNameGenerator.h" |
Dmitri Gribenko | 9e60511 | 2013-11-13 22:16:51 +0000 | [diff] [blame] | 35 | #include "clang/Index/CommentToXML.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 36 | #include "clang/Lex/HeaderSearch.h" |
| 37 | #include "clang/Lex/Lexer.h" |
| 38 | #include "clang/Lex/PreprocessingRecord.h" |
| 39 | #include "clang/Lex/Preprocessor.h" |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 40 | #include "clang/Serialization/SerializationDiagnostic.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/Optional.h" |
| 42 | #include "llvm/ADT/STLExtras.h" |
| 43 | #include "llvm/ADT/StringSwitch.h" |
Alp Toker | 1d257e1 | 2014-06-04 03:28:55 +0000 | [diff] [blame] | 44 | #include "llvm/Config/llvm-config.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Compiler.h" |
| 46 | #include "llvm/Support/CrashRecoveryContext.h" |
Chandler Carruth | 4b41745 | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Format.h" |
Chandler Carruth | 37ad258 | 2014-06-27 15:14:39 +0000 | [diff] [blame] | 48 | #include "llvm/Support/ManagedStatic.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 49 | #include "llvm/Support/MemoryBuffer.h" |
| 50 | #include "llvm/Support/Mutex.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Program.h" |
| 52 | #include "llvm/Support/SaveAndRestore.h" |
| 53 | #include "llvm/Support/Signals.h" |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 54 | #include "llvm/Support/TargetSelect.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 55 | #include "llvm/Support/Threading.h" |
| 56 | #include "llvm/Support/Timer.h" |
| 57 | #include "llvm/Support/raw_ostream.h" |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 58 | |
Alp Toker | 1a86ad2 | 2014-07-06 06:24:00 +0000 | [diff] [blame] | 59 | #if LLVM_ENABLE_THREADS != 0 && defined(__APPLE__) |
| 60 | #define USE_DARWIN_THREADS |
| 61 | #endif |
| 62 | |
| 63 | #ifdef USE_DARWIN_THREADS |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 64 | #include <pthread.h> |
| 65 | #endif |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 66 | |
| 67 | using namespace clang; |
| 68 | using namespace clang::cxcursor; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 69 | using namespace clang::cxtu; |
| 70 | using namespace clang::cxindex; |
| 71 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 72 | CXTranslationUnit cxtu::MakeCXTranslationUnit(CIndexer *CIdx, |
| 73 | std::unique_ptr<ASTUnit> AU) { |
Dmitri Gribenko | d36209e | 2013-01-26 21:32:42 +0000 | [diff] [blame] | 74 | if (!AU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 75 | return nullptr; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 76 | assert(CIdx); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 77 | CXTranslationUnit D = new CXTranslationUnitImpl(); |
| 78 | D->CIdx = CIdx; |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 79 | D->TheASTUnit = AU.release(); |
Dmitri Gribenko | 7489521 | 2013-02-03 13:52:47 +0000 | [diff] [blame] | 80 | D->StringPool = new cxstring::CXStringPool(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 81 | D->Diagnostics = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 82 | D->OverridenCursorsPool = createOverridenCXCursorsPool(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 83 | D->CommentToXML = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 84 | return D; |
| 85 | } |
| 86 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 87 | bool cxtu::isASTReadError(ASTUnit *AU) { |
| 88 | for (ASTUnit::stored_diag_iterator D = AU->stored_diag_begin(), |
| 89 | DEnd = AU->stored_diag_end(); |
| 90 | D != DEnd; ++D) { |
| 91 | if (D->getLevel() >= DiagnosticsEngine::Error && |
| 92 | DiagnosticIDs::getCategoryNumberForDiag(D->getID()) == |
| 93 | diag::DiagCat_AST_Deserialization_Issue) |
| 94 | return true; |
| 95 | } |
| 96 | return false; |
| 97 | } |
| 98 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 99 | cxtu::CXTUOwner::~CXTUOwner() { |
| 100 | if (TU) |
| 101 | clang_disposeTranslationUnit(TU); |
| 102 | } |
| 103 | |
| 104 | /// \brief Compare two source ranges to determine their relative position in |
| 105 | /// the translation unit. |
| 106 | static RangeComparisonResult RangeCompare(SourceManager &SM, |
| 107 | SourceRange R1, |
| 108 | SourceRange R2) { |
| 109 | assert(R1.isValid() && "First range is invalid?"); |
| 110 | assert(R2.isValid() && "Second range is invalid?"); |
| 111 | if (R1.getEnd() != R2.getBegin() && |
| 112 | SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin())) |
| 113 | return RangeBefore; |
| 114 | if (R2.getEnd() != R1.getBegin() && |
| 115 | SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin())) |
| 116 | return RangeAfter; |
| 117 | return RangeOverlap; |
| 118 | } |
| 119 | |
| 120 | /// \brief Determine if a source location falls within, before, or after a |
| 121 | /// a given source range. |
| 122 | static RangeComparisonResult LocationCompare(SourceManager &SM, |
| 123 | SourceLocation L, SourceRange R) { |
| 124 | assert(R.isValid() && "First range is invalid?"); |
| 125 | assert(L.isValid() && "Second range is invalid?"); |
| 126 | if (L == R.getBegin() || L == R.getEnd()) |
| 127 | return RangeOverlap; |
| 128 | if (SM.isBeforeInTranslationUnit(L, R.getBegin())) |
| 129 | return RangeBefore; |
| 130 | if (SM.isBeforeInTranslationUnit(R.getEnd(), L)) |
| 131 | return RangeAfter; |
| 132 | return RangeOverlap; |
| 133 | } |
| 134 | |
| 135 | /// \brief Translate a Clang source range into a CIndex source range. |
| 136 | /// |
| 137 | /// Clang internally represents ranges where the end location points to the |
| 138 | /// start of the token at the end. However, for external clients it is more |
| 139 | /// useful to have a CXSourceRange be a proper half-open interval. This routine |
| 140 | /// does the appropriate translation. |
| 141 | CXSourceRange cxloc::translateSourceRange(const SourceManager &SM, |
| 142 | const LangOptions &LangOpts, |
| 143 | const CharSourceRange &R) { |
| 144 | // We want the last character in this location, so we will adjust the |
| 145 | // location accordingly. |
| 146 | SourceLocation EndLoc = R.getEnd(); |
| 147 | if (EndLoc.isValid() && EndLoc.isMacroID() && !SM.isMacroArgExpansion(EndLoc)) |
| 148 | EndLoc = SM.getExpansionRange(EndLoc).second; |
Yaron Keren | 8b56366 | 2015-10-03 10:46:20 +0000 | [diff] [blame] | 149 | if (R.isTokenRange() && EndLoc.isValid()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 150 | unsigned Length = Lexer::MeasureTokenLength(SM.getSpellingLoc(EndLoc), |
| 151 | SM, LangOpts); |
| 152 | EndLoc = EndLoc.getLocWithOffset(Length); |
| 153 | } |
| 154 | |
Bill Wendling | eade362 | 2013-01-23 08:25:41 +0000 | [diff] [blame] | 155 | CXSourceRange Result = { |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 156 | { &SM, &LangOpts }, |
Bill Wendling | eade362 | 2013-01-23 08:25:41 +0000 | [diff] [blame] | 157 | R.getBegin().getRawEncoding(), |
| 158 | EndLoc.getRawEncoding() |
| 159 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 160 | return Result; |
| 161 | } |
| 162 | |
| 163 | //===----------------------------------------------------------------------===// |
| 164 | // Cursor visitor. |
| 165 | //===----------------------------------------------------------------------===// |
| 166 | |
| 167 | static SourceRange getRawCursorExtent(CXCursor C); |
| 168 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr); |
| 169 | |
| 170 | |
| 171 | RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { |
| 172 | return RangeCompare(AU->getSourceManager(), R, RegionOfInterest); |
| 173 | } |
| 174 | |
| 175 | /// \brief Visit the given cursor and, if requested by the visitor, |
| 176 | /// its children. |
| 177 | /// |
| 178 | /// \param Cursor the cursor to visit. |
| 179 | /// |
| 180 | /// \param CheckedRegionOfInterest if true, then the caller already checked |
| 181 | /// that this cursor is within the region of interest. |
| 182 | /// |
| 183 | /// \returns true if the visitation should be aborted, false if it |
| 184 | /// should continue. |
| 185 | bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { |
| 186 | if (clang_isInvalid(Cursor.kind)) |
| 187 | return false; |
| 188 | |
| 189 | if (clang_isDeclaration(Cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 190 | const Decl *D = getCursorDecl(Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 191 | if (!D) { |
| 192 | assert(0 && "Invalid declaration cursor"); |
| 193 | return true; // abort. |
| 194 | } |
| 195 | |
| 196 | // Ignore implicit declarations, unless it's an objc method because |
| 197 | // currently we should report implicit methods for properties when indexing. |
| 198 | if (D->isImplicit() && !isa<ObjCMethodDecl>(D)) |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | // If we have a range of interest, and this cursor doesn't intersect with it, |
| 203 | // we're done. |
| 204 | if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { |
| 205 | SourceRange Range = getRawCursorExtent(Cursor); |
| 206 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 211 | case CXChildVisit_Break: |
| 212 | return true; |
| 213 | |
| 214 | case CXChildVisit_Continue: |
| 215 | return false; |
| 216 | |
| 217 | case CXChildVisit_Recurse: { |
| 218 | bool ret = VisitChildren(Cursor); |
| 219 | if (PostChildrenVisitor) |
| 220 | if (PostChildrenVisitor(Cursor, ClientData)) |
| 221 | return true; |
| 222 | return ret; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | llvm_unreachable("Invalid CXChildVisitResult!"); |
| 227 | } |
| 228 | |
| 229 | static bool visitPreprocessedEntitiesInRange(SourceRange R, |
| 230 | PreprocessingRecord &PPRec, |
| 231 | CursorVisitor &Visitor) { |
| 232 | SourceManager &SM = Visitor.getASTUnit()->getSourceManager(); |
| 233 | FileID FID; |
| 234 | |
| 235 | if (!Visitor.shouldVisitIncludedEntities()) { |
| 236 | // If the begin/end of the range lie in the same FileID, do the optimization |
| 237 | // where we skip preprocessed entities that do not come from the same FileID. |
| 238 | FID = SM.getFileID(SM.getFileLoc(R.getBegin())); |
| 239 | if (FID != SM.getFileID(SM.getFileLoc(R.getEnd()))) |
| 240 | FID = FileID(); |
| 241 | } |
| 242 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 243 | const auto &Entities = PPRec.getPreprocessedEntitiesInRange(R); |
| 244 | return Visitor.visitPreprocessedEntities(Entities.begin(), Entities.end(), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 245 | PPRec, FID); |
| 246 | } |
| 247 | |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 248 | bool CursorVisitor::visitFileRegion() { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 249 | if (RegionOfInterest.isInvalid()) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 250 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 251 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 252 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 253 | SourceManager &SM = Unit->getSourceManager(); |
| 254 | |
| 255 | std::pair<FileID, unsigned> |
| 256 | Begin = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getBegin())), |
| 257 | End = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getEnd())); |
| 258 | |
| 259 | if (End.first != Begin.first) { |
| 260 | // If the end does not reside in the same file, try to recover by |
| 261 | // picking the end of the file of begin location. |
| 262 | End.first = Begin.first; |
| 263 | End.second = SM.getFileIDSize(Begin.first); |
| 264 | } |
| 265 | |
| 266 | assert(Begin.first == End.first); |
| 267 | if (Begin.second > End.second) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 268 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 269 | |
| 270 | FileID File = Begin.first; |
| 271 | unsigned Offset = Begin.second; |
| 272 | unsigned Length = End.second - Begin.second; |
| 273 | |
| 274 | if (!VisitDeclsOnly && !VisitPreprocessorLast) |
| 275 | if (visitPreprocessedEntitiesInRegion()) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 276 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 277 | |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 278 | if (visitDeclsFromFileRegion(File, Offset, Length)) |
| 279 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 280 | |
| 281 | if (!VisitDeclsOnly && VisitPreprocessorLast) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 282 | return visitPreprocessedEntitiesInRegion(); |
| 283 | |
| 284 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static bool isInLexicalContext(Decl *D, DeclContext *DC) { |
| 288 | if (!DC) |
| 289 | return false; |
| 290 | |
| 291 | for (DeclContext *DeclDC = D->getLexicalDeclContext(); |
| 292 | DeclDC; DeclDC = DeclDC->getLexicalParent()) { |
| 293 | if (DeclDC == DC) |
| 294 | return true; |
| 295 | } |
| 296 | return false; |
| 297 | } |
| 298 | |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 299 | bool CursorVisitor::visitDeclsFromFileRegion(FileID File, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 300 | unsigned Offset, unsigned Length) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 301 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 302 | SourceManager &SM = Unit->getSourceManager(); |
| 303 | SourceRange Range = RegionOfInterest; |
| 304 | |
| 305 | SmallVector<Decl *, 16> Decls; |
| 306 | Unit->findFileRegionDecls(File, Offset, Length, Decls); |
| 307 | |
| 308 | // If we didn't find any file level decls for the file, try looking at the |
| 309 | // file that it was included from. |
| 310 | while (Decls.empty() || Decls.front()->isTopLevelDeclInObjCContainer()) { |
| 311 | bool Invalid = false; |
| 312 | const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid); |
| 313 | if (Invalid) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 314 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 315 | |
| 316 | SourceLocation Outer; |
| 317 | if (SLEntry.isFile()) |
| 318 | Outer = SLEntry.getFile().getIncludeLoc(); |
| 319 | else |
| 320 | Outer = SLEntry.getExpansion().getExpansionLocStart(); |
| 321 | if (Outer.isInvalid()) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 322 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 323 | |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 324 | std::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 325 | Length = 0; |
| 326 | Unit->findFileRegionDecls(File, Offset, Length, Decls); |
| 327 | } |
| 328 | |
| 329 | assert(!Decls.empty()); |
| 330 | |
| 331 | bool VisitedAtLeastOnce = false; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 332 | DeclContext *CurDC = nullptr; |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 333 | SmallVectorImpl<Decl *>::iterator DIt = Decls.begin(); |
| 334 | for (SmallVectorImpl<Decl *>::iterator DE = Decls.end(); DIt != DE; ++DIt) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 335 | Decl *D = *DIt; |
| 336 | if (D->getSourceRange().isInvalid()) |
| 337 | continue; |
| 338 | |
| 339 | if (isInLexicalContext(D, CurDC)) |
| 340 | continue; |
| 341 | |
| 342 | CurDC = dyn_cast<DeclContext>(D); |
| 343 | |
| 344 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
| 345 | if (!TD->isFreeStanding()) |
| 346 | continue; |
| 347 | |
| 348 | RangeComparisonResult CompRes = RangeCompare(SM, D->getSourceRange(),Range); |
| 349 | if (CompRes == RangeBefore) |
| 350 | continue; |
| 351 | if (CompRes == RangeAfter) |
| 352 | break; |
| 353 | |
| 354 | assert(CompRes == RangeOverlap); |
| 355 | VisitedAtLeastOnce = true; |
| 356 | |
| 357 | if (isa<ObjCContainerDecl>(D)) { |
| 358 | FileDI_current = &DIt; |
| 359 | FileDE_current = DE; |
| 360 | } else { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 361 | FileDI_current = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true)) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 365 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | if (VisitedAtLeastOnce) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 369 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 370 | |
| 371 | // No Decls overlapped with the range. Move up the lexical context until there |
| 372 | // is a context that contains the range or we reach the translation unit |
| 373 | // level. |
| 374 | DeclContext *DC = DIt == Decls.begin() ? (*DIt)->getLexicalDeclContext() |
| 375 | : (*(DIt-1))->getLexicalDeclContext(); |
| 376 | |
| 377 | while (DC && !DC->isTranslationUnit()) { |
| 378 | Decl *D = cast<Decl>(DC); |
| 379 | SourceRange CurDeclRange = D->getSourceRange(); |
| 380 | if (CurDeclRange.isInvalid()) |
| 381 | break; |
| 382 | |
| 383 | if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) { |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 384 | if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true)) |
| 385 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | DC = D->getLexicalDeclContext(); |
| 389 | } |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 390 | |
| 391 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | bool CursorVisitor::visitPreprocessedEntitiesInRegion() { |
| 395 | if (!AU->getPreprocessor().getPreprocessingRecord()) |
| 396 | return false; |
| 397 | |
| 398 | PreprocessingRecord &PPRec |
| 399 | = *AU->getPreprocessor().getPreprocessingRecord(); |
| 400 | SourceManager &SM = AU->getSourceManager(); |
| 401 | |
| 402 | if (RegionOfInterest.isValid()) { |
| 403 | SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest); |
| 404 | SourceLocation B = MappedRange.getBegin(); |
| 405 | SourceLocation E = MappedRange.getEnd(); |
| 406 | |
| 407 | if (AU->isInPreambleFileID(B)) { |
| 408 | if (SM.isLoadedSourceLocation(E)) |
| 409 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), |
| 410 | PPRec, *this); |
| 411 | |
| 412 | // Beginning of range lies in the preamble but it also extends beyond |
| 413 | // it into the main file. Split the range into 2 parts, one covering |
| 414 | // the preamble and another covering the main file. This allows subsequent |
| 415 | // calls to visitPreprocessedEntitiesInRange to accept a source range that |
| 416 | // lies in the same FileID, allowing it to skip preprocessed entities that |
| 417 | // do not come from the same FileID. |
| 418 | bool breaked = |
| 419 | visitPreprocessedEntitiesInRange( |
| 420 | SourceRange(B, AU->getEndOfPreambleFileID()), |
| 421 | PPRec, *this); |
| 422 | if (breaked) return true; |
| 423 | return visitPreprocessedEntitiesInRange( |
| 424 | SourceRange(AU->getStartOfMainFileID(), E), |
| 425 | PPRec, *this); |
| 426 | } |
| 427 | |
| 428 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this); |
| 429 | } |
| 430 | |
| 431 | bool OnlyLocalDecls |
| 432 | = !AU->isMainFileAST() && AU->getOnlyLocalDecls(); |
| 433 | |
| 434 | if (OnlyLocalDecls) |
| 435 | return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(), |
| 436 | PPRec); |
| 437 | |
| 438 | return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec); |
| 439 | } |
| 440 | |
| 441 | template<typename InputIterator> |
| 442 | bool CursorVisitor::visitPreprocessedEntities(InputIterator First, |
| 443 | InputIterator Last, |
| 444 | PreprocessingRecord &PPRec, |
| 445 | FileID FID) { |
| 446 | for (; First != Last; ++First) { |
| 447 | if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID)) |
| 448 | continue; |
| 449 | |
| 450 | PreprocessedEntity *PPE = *First; |
Argyrios Kyrtzidis | 1030f26 | 2013-05-07 20:37:17 +0000 | [diff] [blame] | 451 | if (!PPE) |
| 452 | continue; |
| 453 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 454 | if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) { |
| 455 | if (Visit(MakeMacroExpansionCursor(ME, TU))) |
| 456 | return true; |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 457 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 458 | continue; |
| 459 | } |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 460 | |
| 461 | if (MacroDefinitionRecord *MD = dyn_cast<MacroDefinitionRecord>(PPE)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 462 | if (Visit(MakeMacroDefinitionCursor(MD, TU))) |
| 463 | return true; |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 464 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 465 | continue; |
| 466 | } |
| 467 | |
| 468 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) { |
| 469 | if (Visit(MakeInclusionDirectiveCursor(ID, TU))) |
| 470 | return true; |
| 471 | |
| 472 | continue; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | /// \brief Visit the children of the given cursor. |
| 480 | /// |
| 481 | /// \returns true if the visitation should be aborted, false if it |
| 482 | /// should continue. |
| 483 | bool CursorVisitor::VisitChildren(CXCursor Cursor) { |
| 484 | if (clang_isReference(Cursor.kind) && |
| 485 | Cursor.kind != CXCursor_CXXBaseSpecifier) { |
| 486 | // By definition, references have no children. |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | // Set the Parent field to Cursor, then back to its old value once we're |
| 491 | // done. |
| 492 | SetParentRAII SetParent(Parent, StmtParent, Cursor); |
| 493 | |
| 494 | if (clang_isDeclaration(Cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 495 | Decl *D = const_cast<Decl *>(getCursorDecl(Cursor)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 496 | if (!D) |
| 497 | return false; |
| 498 | |
| 499 | return VisitAttributes(D) || Visit(D); |
| 500 | } |
| 501 | |
| 502 | if (clang_isStatement(Cursor.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 503 | if (const Stmt *S = getCursorStmt(Cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 504 | return Visit(S); |
| 505 | |
| 506 | return false; |
| 507 | } |
| 508 | |
| 509 | if (clang_isExpression(Cursor.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 510 | if (const Expr *E = getCursorExpr(Cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 511 | return Visit(E); |
| 512 | |
| 513 | return false; |
| 514 | } |
| 515 | |
| 516 | if (clang_isTranslationUnit(Cursor.kind)) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 517 | CXTranslationUnit TU = getCursorTU(Cursor); |
| 518 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 519 | |
| 520 | int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast }; |
| 521 | for (unsigned I = 0; I != 2; ++I) { |
| 522 | if (VisitOrder[I]) { |
| 523 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && |
| 524 | RegionOfInterest.isInvalid()) { |
| 525 | for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(), |
| 526 | TLEnd = CXXUnit->top_level_end(); |
| 527 | TL != TLEnd; ++TL) { |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 528 | const Optional<bool> V = handleDeclForVisitation(*TL); |
| 529 | if (!V.hasValue()) |
| 530 | continue; |
| 531 | return V.getValue(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 532 | } |
| 533 | } else if (VisitDeclContext( |
| 534 | CXXUnit->getASTContext().getTranslationUnitDecl())) |
| 535 | return true; |
| 536 | continue; |
| 537 | } |
| 538 | |
| 539 | // Walk the preprocessing record. |
| 540 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) |
| 541 | visitPreprocessedEntitiesInRegion(); |
| 542 | } |
| 543 | |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | if (Cursor.kind == CXCursor_CXXBaseSpecifier) { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 548 | if (const CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 549 | if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) { |
| 550 | return Visit(BaseTSInfo->getTypeLoc()); |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 556 | const IBOutletCollectionAttr *A = |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 557 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor)); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 558 | if (const ObjCObjectType *ObjT = A->getInterface()->getAs<ObjCObjectType>()) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 559 | return Visit(cxcursor::MakeCursorObjCClassRef( |
| 560 | ObjT->getInterface(), |
| 561 | A->getInterfaceLoc()->getTypeLoc().getLocStart(), TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 564 | // If pointing inside a macro definition, check if the token is an identifier |
| 565 | // that was ever defined as a macro. In such a case, create a "pseudo" macro |
| 566 | // expansion cursor for that token. |
| 567 | SourceLocation BeginLoc = RegionOfInterest.getBegin(); |
| 568 | if (Cursor.kind == CXCursor_MacroDefinition && |
| 569 | BeginLoc == RegionOfInterest.getEnd()) { |
| 570 | SourceLocation Loc = AU->mapLocationToPreamble(BeginLoc); |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 571 | const MacroInfo *MI = |
| 572 | getMacroInfo(cxcursor::getCursorMacroDefinition(Cursor), TU); |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 573 | if (MacroDefinitionRecord *MacroDef = |
| 574 | checkForMacroInMacroDefinition(MI, Loc, TU)) |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 575 | return Visit(cxcursor::MakeMacroExpansionCursor(MacroDef, BeginLoc, TU)); |
| 576 | } |
| 577 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 578 | // Nothing to visit at the moment. |
| 579 | return false; |
| 580 | } |
| 581 | |
| 582 | bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { |
| 583 | if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten()) |
| 584 | if (Visit(TSInfo->getTypeLoc())) |
| 585 | return true; |
| 586 | |
| 587 | if (Stmt *Body = B->getBody()) |
| 588 | return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest)); |
| 589 | |
| 590 | return false; |
| 591 | } |
| 592 | |
Ted Kremenek | 0332558 | 2013-02-21 01:29:01 +0000 | [diff] [blame] | 593 | Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 594 | if (RegionOfInterest.isValid()) { |
| 595 | SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager()); |
| 596 | if (Range.isInvalid()) |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 597 | return None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 598 | |
| 599 | switch (CompareRegionOfInterest(Range)) { |
| 600 | case RangeBefore: |
| 601 | // This declaration comes before the region of interest; skip it. |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 602 | return None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 603 | |
| 604 | case RangeAfter: |
| 605 | // This declaration comes after the region of interest; we're done. |
| 606 | return false; |
| 607 | |
| 608 | case RangeOverlap: |
| 609 | // This declaration overlaps the region of interest; visit it. |
| 610 | break; |
| 611 | } |
| 612 | } |
| 613 | return true; |
| 614 | } |
| 615 | |
| 616 | bool CursorVisitor::VisitDeclContext(DeclContext *DC) { |
| 617 | DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 618 | |
| 619 | // FIXME: Eventually remove. This part of a hack to support proper |
| 620 | // iteration over all Decls contained lexically within an ObjC container. |
| 621 | SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I); |
| 622 | SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E); |
| 623 | |
| 624 | for ( ; I != E; ++I) { |
| 625 | Decl *D = *I; |
| 626 | if (D->getLexicalDeclContext() != DC) |
| 627 | continue; |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 628 | const Optional<bool> V = handleDeclForVisitation(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 629 | if (!V.hasValue()) |
| 630 | continue; |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 631 | return V.getValue(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 632 | } |
| 633 | return false; |
| 634 | } |
| 635 | |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 636 | Optional<bool> CursorVisitor::handleDeclForVisitation(const Decl *D) { |
| 637 | CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest); |
| 638 | |
| 639 | // Ignore synthesized ivars here, otherwise if we have something like: |
| 640 | // @synthesize prop = _prop; |
| 641 | // and '_prop' is not declared, we will encounter a '_prop' ivar before |
| 642 | // encountering the 'prop' synthesize declaration and we will think that |
| 643 | // we passed the region-of-interest. |
| 644 | if (auto *ivarD = dyn_cast<ObjCIvarDecl>(D)) { |
| 645 | if (ivarD->getSynthesize()) |
| 646 | return None; |
| 647 | } |
| 648 | |
| 649 | // FIXME: ObjCClassRef/ObjCProtocolRef for forward class/protocol |
| 650 | // declarations is a mismatch with the compiler semantics. |
| 651 | if (Cursor.kind == CXCursor_ObjCInterfaceDecl) { |
| 652 | auto *ID = cast<ObjCInterfaceDecl>(D); |
| 653 | if (!ID->isThisDeclarationADefinition()) |
| 654 | Cursor = MakeCursorObjCClassRef(ID, ID->getLocation(), TU); |
| 655 | |
| 656 | } else if (Cursor.kind == CXCursor_ObjCProtocolDecl) { |
| 657 | auto *PD = cast<ObjCProtocolDecl>(D); |
| 658 | if (!PD->isThisDeclarationADefinition()) |
| 659 | Cursor = MakeCursorObjCProtocolRef(PD, PD->getLocation(), TU); |
| 660 | } |
| 661 | |
| 662 | const Optional<bool> V = shouldVisitCursor(Cursor); |
| 663 | if (!V.hasValue()) |
| 664 | return None; |
| 665 | if (!V.getValue()) |
| 666 | return false; |
| 667 | if (Visit(Cursor, true)) |
| 668 | return true; |
| 669 | return None; |
| 670 | } |
| 671 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 672 | bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 673 | llvm_unreachable("Translation units are visited directly by Visit()"); |
| 674 | } |
| 675 | |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 676 | bool CursorVisitor::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| 677 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 678 | return true; |
| 679 | |
| 680 | return Visit(MakeCXCursor(D->getTemplatedDecl(), TU, RegionOfInterest)); |
| 681 | } |
| 682 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 683 | bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 684 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 685 | return Visit(TSInfo->getTypeLoc()); |
| 686 | |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { |
| 691 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 692 | return Visit(TSInfo->getTypeLoc()); |
| 693 | |
| 694 | return false; |
| 695 | } |
| 696 | |
| 697 | bool CursorVisitor::VisitTagDecl(TagDecl *D) { |
| 698 | return VisitDeclContext(D); |
| 699 | } |
| 700 | |
| 701 | bool CursorVisitor::VisitClassTemplateSpecializationDecl( |
| 702 | ClassTemplateSpecializationDecl *D) { |
| 703 | bool ShouldVisitBody = false; |
| 704 | switch (D->getSpecializationKind()) { |
| 705 | case TSK_Undeclared: |
| 706 | case TSK_ImplicitInstantiation: |
| 707 | // Nothing to visit |
| 708 | return false; |
| 709 | |
| 710 | case TSK_ExplicitInstantiationDeclaration: |
| 711 | case TSK_ExplicitInstantiationDefinition: |
| 712 | break; |
| 713 | |
| 714 | case TSK_ExplicitSpecialization: |
| 715 | ShouldVisitBody = true; |
| 716 | break; |
| 717 | } |
| 718 | |
| 719 | // Visit the template arguments used in the specialization. |
| 720 | if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { |
| 721 | TypeLoc TL = SpecType->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 722 | if (TemplateSpecializationTypeLoc TSTLoc = |
| 723 | TL.getAs<TemplateSpecializationTypeLoc>()) { |
| 724 | for (unsigned I = 0, N = TSTLoc.getNumArgs(); I != N; ++I) |
| 725 | if (VisitTemplateArgumentLoc(TSTLoc.getArgLoc(I))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 726 | return true; |
| 727 | } |
| 728 | } |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 729 | |
| 730 | return ShouldVisitBody && VisitCXXRecordDecl(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( |
| 734 | ClassTemplatePartialSpecializationDecl *D) { |
| 735 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 736 | // before visiting these template parameters. |
| 737 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 738 | return true; |
| 739 | |
| 740 | // Visit the partial specialization arguments. |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 741 | const ASTTemplateArgumentListInfo *Info = D->getTemplateArgsAsWritten(); |
| 742 | const TemplateArgumentLoc *TemplateArgs = Info->getTemplateArgs(); |
| 743 | for (unsigned I = 0, N = Info->NumTemplateArgs; I != N; ++I) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 744 | if (VisitTemplateArgumentLoc(TemplateArgs[I])) |
| 745 | return true; |
| 746 | |
| 747 | return VisitCXXRecordDecl(D); |
| 748 | } |
| 749 | |
| 750 | bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
| 751 | // Visit the default argument. |
| 752 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 753 | if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) |
| 754 | if (Visit(DefArg->getTypeLoc())) |
| 755 | return true; |
| 756 | |
| 757 | return false; |
| 758 | } |
| 759 | |
| 760 | bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 761 | if (Expr *Init = D->getInitExpr()) |
| 762 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
| 763 | return false; |
| 764 | } |
| 765 | |
| 766 | bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { |
Argyrios Kyrtzidis | 2ec7674 | 2013-04-05 21:04:10 +0000 | [diff] [blame] | 767 | unsigned NumParamList = DD->getNumTemplateParameterLists(); |
| 768 | for (unsigned i = 0; i < NumParamList; i++) { |
| 769 | TemplateParameterList* Params = DD->getTemplateParameterList(i); |
| 770 | if (VisitTemplateParameters(Params)) |
| 771 | return true; |
| 772 | } |
| 773 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 774 | if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) |
| 775 | if (Visit(TSInfo->getTypeLoc())) |
| 776 | return true; |
| 777 | |
| 778 | // Visit the nested-name-specifier, if present. |
| 779 | if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc()) |
| 780 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 781 | return true; |
| 782 | |
| 783 | return false; |
| 784 | } |
| 785 | |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 786 | /// \brief Compare two base or member initializers based on their source order. |
| 787 | static int CompareCXXCtorInitializers(CXXCtorInitializer *const *X, |
| 788 | CXXCtorInitializer *const *Y) { |
| 789 | return (*X)->getSourceOrder() - (*Y)->getSourceOrder(); |
| 790 | } |
| 791 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 792 | bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
Argyrios Kyrtzidis | 2ec7674 | 2013-04-05 21:04:10 +0000 | [diff] [blame] | 793 | unsigned NumParamList = ND->getNumTemplateParameterLists(); |
| 794 | for (unsigned i = 0; i < NumParamList; i++) { |
| 795 | TemplateParameterList* Params = ND->getTemplateParameterList(i); |
| 796 | if (VisitTemplateParameters(Params)) |
| 797 | return true; |
| 798 | } |
| 799 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 800 | if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { |
| 801 | // Visit the function declaration's syntactic components in the order |
| 802 | // written. This requires a bit of work. |
| 803 | TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 804 | FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 805 | |
| 806 | // If we have a function declared directly (without the use of a typedef), |
| 807 | // visit just the return type. Otherwise, just visit the function's type |
| 808 | // now. |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 809 | if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL.getReturnLoc())) || |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 810 | (!FTL && Visit(TL))) |
| 811 | return true; |
| 812 | |
| 813 | // Visit the nested-name-specifier, if present. |
| 814 | if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc()) |
| 815 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 816 | return true; |
| 817 | |
| 818 | // Visit the declaration name. |
Argyrios Kyrtzidis | 4a4d2b4 | 2014-02-09 08:13:47 +0000 | [diff] [blame] | 819 | if (!isa<CXXDestructorDecl>(ND)) |
| 820 | if (VisitDeclarationNameInfo(ND->getNameInfo())) |
| 821 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 822 | |
| 823 | // FIXME: Visit explicitly-specified template arguments! |
| 824 | |
| 825 | // Visit the function parameters, if we have a function type. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 826 | if (FTL && VisitFunctionTypeLoc(FTL, true)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 827 | return true; |
| 828 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 829 | // FIXME: Attributes? |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) { |
| 833 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) { |
| 834 | // Find the initializers that were written in the source. |
| 835 | SmallVector<CXXCtorInitializer *, 4> WrittenInits; |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 836 | for (auto *I : Constructor->inits()) { |
| 837 | if (!I->isWritten()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 838 | continue; |
| 839 | |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 840 | WrittenInits.push_back(I); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | // Sort the initializers in source order |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 844 | llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(), |
| 845 | &CompareCXXCtorInitializers); |
| 846 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 847 | // Visit the initializers in source order |
| 848 | for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) { |
| 849 | CXXCtorInitializer *Init = WrittenInits[I]; |
| 850 | if (Init->isAnyMemberInitializer()) { |
| 851 | if (Visit(MakeCursorMemberRef(Init->getAnyMember(), |
| 852 | Init->getMemberLocation(), TU))) |
| 853 | return true; |
| 854 | } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) { |
| 855 | if (Visit(TInfo->getTypeLoc())) |
| 856 | return true; |
| 857 | } |
| 858 | |
| 859 | // Visit the initializer value. |
| 860 | if (Expr *Initializer = Init->getInit()) |
| 861 | if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest))) |
| 862 | return true; |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
| 867 | return true; |
| 868 | } |
| 869 | |
| 870 | return false; |
| 871 | } |
| 872 | |
| 873 | bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { |
| 874 | if (VisitDeclaratorDecl(D)) |
| 875 | return true; |
| 876 | |
| 877 | if (Expr *BitWidth = D->getBitWidth()) |
| 878 | return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest)); |
| 879 | |
| 880 | return false; |
| 881 | } |
| 882 | |
| 883 | bool CursorVisitor::VisitVarDecl(VarDecl *D) { |
| 884 | if (VisitDeclaratorDecl(D)) |
| 885 | return true; |
| 886 | |
| 887 | if (Expr *Init = D->getInit()) |
| 888 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
| 889 | |
| 890 | return false; |
| 891 | } |
| 892 | |
| 893 | bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 894 | if (VisitDeclaratorDecl(D)) |
| 895 | return true; |
| 896 | |
| 897 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 898 | if (Expr *DefArg = D->getDefaultArgument()) |
| 899 | return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest)); |
| 900 | |
| 901 | return false; |
| 902 | } |
| 903 | |
| 904 | bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 905 | // FIXME: Visit the "outer" template parameter lists on the FunctionDecl |
| 906 | // before visiting these template parameters. |
| 907 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 908 | return true; |
| 909 | |
| 910 | return VisitFunctionDecl(D->getTemplatedDecl()); |
| 911 | } |
| 912 | |
| 913 | bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 914 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 915 | // before visiting these template parameters. |
| 916 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 917 | return true; |
| 918 | |
| 919 | return VisitCXXRecordDecl(D->getTemplatedDecl()); |
| 920 | } |
| 921 | |
| 922 | bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 923 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 924 | return true; |
| 925 | |
| 926 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && |
| 927 | VisitTemplateArgumentLoc(D->getDefaultArgument())) |
| 928 | return true; |
| 929 | |
| 930 | return false; |
| 931 | } |
| 932 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 933 | bool CursorVisitor::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { |
| 934 | // Visit the bound, if it's explicit. |
| 935 | if (D->hasExplicitBound()) { |
| 936 | if (auto TInfo = D->getTypeSourceInfo()) { |
| 937 | if (Visit(TInfo->getTypeLoc())) |
| 938 | return true; |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | return false; |
| 943 | } |
| 944 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 945 | bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 946 | if (TypeSourceInfo *TSInfo = ND->getReturnTypeSourceInfo()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 947 | if (Visit(TSInfo->getTypeLoc())) |
| 948 | return true; |
| 949 | |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 950 | for (const auto *P : ND->parameters()) { |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 951 | if (Visit(MakeCXCursor(P, TU, RegionOfInterest))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 952 | return true; |
| 953 | } |
| 954 | |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 955 | return ND->isThisDeclarationADefinition() && |
| 956 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | template <typename DeclIt> |
| 960 | static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current, |
| 961 | SourceManager &SM, SourceLocation EndLoc, |
| 962 | SmallVectorImpl<Decl *> &Decls) { |
| 963 | DeclIt next = *DI_current; |
| 964 | while (++next != DE_current) { |
| 965 | Decl *D_next = *next; |
| 966 | if (!D_next) |
| 967 | break; |
| 968 | SourceLocation L = D_next->getLocStart(); |
| 969 | if (!L.isValid()) |
| 970 | break; |
| 971 | if (SM.isBeforeInTranslationUnit(L, EndLoc)) { |
| 972 | *DI_current = next; |
| 973 | Decls.push_back(D_next); |
| 974 | continue; |
| 975 | } |
| 976 | break; |
| 977 | } |
| 978 | } |
| 979 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 980 | bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
| 981 | // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially |
| 982 | // an @implementation can lexically contain Decls that are not properly |
| 983 | // nested in the AST. When we identify such cases, we need to retrofit |
| 984 | // this nesting here. |
| 985 | if (!DI_current && !FileDI_current) |
| 986 | return VisitDeclContext(D); |
| 987 | |
| 988 | // Scan the Decls that immediately come after the container |
| 989 | // in the current DeclContext. If any fall within the |
| 990 | // container's lexical region, stash them into a vector |
| 991 | // for later processing. |
| 992 | SmallVector<Decl *, 24> DeclsInContainer; |
| 993 | SourceLocation EndLoc = D->getSourceRange().getEnd(); |
| 994 | SourceManager &SM = AU->getSourceManager(); |
| 995 | if (EndLoc.isValid()) { |
| 996 | if (DI_current) { |
| 997 | addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc, |
| 998 | DeclsInContainer); |
| 999 | } else { |
| 1000 | addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc, |
| 1001 | DeclsInContainer); |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | // The common case. |
| 1006 | if (DeclsInContainer.empty()) |
| 1007 | return VisitDeclContext(D); |
| 1008 | |
| 1009 | // Get all the Decls in the DeclContext, and sort them with the |
| 1010 | // additional ones we've collected. Then visit them. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1011 | for (auto *SubDecl : D->decls()) { |
| 1012 | if (!SubDecl || SubDecl->getLexicalDeclContext() != D || |
| 1013 | SubDecl->getLocStart().isInvalid()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1014 | continue; |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1015 | DeclsInContainer.push_back(SubDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | // Now sort the Decls so that they appear in lexical order. |
| 1019 | std::sort(DeclsInContainer.begin(), DeclsInContainer.end(), |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 1020 | [&SM](Decl *A, Decl *B) { |
| 1021 | SourceLocation L_A = A->getLocStart(); |
| 1022 | SourceLocation L_B = B->getLocStart(); |
| 1023 | assert(L_A.isValid() && L_B.isValid()); |
| 1024 | return SM.isBeforeInTranslationUnit(L_A, L_B); |
| 1025 | }); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1026 | |
| 1027 | // Now visit the decls. |
| 1028 | for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(), |
| 1029 | E = DeclsInContainer.end(); I != E; ++I) { |
| 1030 | CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest); |
Ted Kremenek | 0332558 | 2013-02-21 01:29:01 +0000 | [diff] [blame] | 1031 | const Optional<bool> &V = shouldVisitCursor(Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1032 | if (!V.hasValue()) |
| 1033 | continue; |
| 1034 | if (!V.getValue()) |
| 1035 | return false; |
| 1036 | if (Visit(Cursor, true)) |
| 1037 | return true; |
| 1038 | } |
| 1039 | return false; |
| 1040 | } |
| 1041 | |
| 1042 | bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 1043 | if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), |
| 1044 | TU))) |
| 1045 | return true; |
| 1046 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1047 | if (VisitObjCTypeParamList(ND->getTypeParamList())) |
| 1048 | return true; |
| 1049 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1050 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 1051 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 1052 | E = ND->protocol_end(); I != E; ++I, ++PL) |
| 1053 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1054 | return true; |
| 1055 | |
| 1056 | return VisitObjCContainerDecl(ND); |
| 1057 | } |
| 1058 | |
| 1059 | bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 1060 | if (!PID->isThisDeclarationADefinition()) |
| 1061 | return Visit(MakeCursorObjCProtocolRef(PID, PID->getLocation(), TU)); |
| 1062 | |
| 1063 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
| 1064 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 1065 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 1066 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1067 | return true; |
| 1068 | |
| 1069 | return VisitObjCContainerDecl(PID); |
| 1070 | } |
| 1071 | |
| 1072 | bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { |
| 1073 | if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc())) |
| 1074 | return true; |
| 1075 | |
| 1076 | // FIXME: This implements a workaround with @property declarations also being |
| 1077 | // installed in the DeclContext for the @interface. Eventually this code |
| 1078 | // should be removed. |
| 1079 | ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); |
| 1080 | if (!CDecl || !CDecl->IsClassExtension()) |
| 1081 | return false; |
| 1082 | |
| 1083 | ObjCInterfaceDecl *ID = CDecl->getClassInterface(); |
| 1084 | if (!ID) |
| 1085 | return false; |
| 1086 | |
| 1087 | IdentifierInfo *PropertyId = PD->getIdentifier(); |
| 1088 | ObjCPropertyDecl *prevDecl = |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 1089 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId, |
| 1090 | PD->getQueryKind()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1091 | |
| 1092 | if (!prevDecl) |
| 1093 | return false; |
| 1094 | |
| 1095 | // Visit synthesized methods since they will be skipped when visiting |
| 1096 | // the @interface. |
| 1097 | if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) |
| 1098 | if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl) |
| 1099 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
| 1100 | return true; |
| 1101 | |
| 1102 | if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) |
| 1103 | if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl) |
| 1104 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
| 1105 | return true; |
| 1106 | |
| 1107 | return false; |
| 1108 | } |
| 1109 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1110 | bool CursorVisitor::VisitObjCTypeParamList(ObjCTypeParamList *typeParamList) { |
| 1111 | if (!typeParamList) |
| 1112 | return false; |
| 1113 | |
| 1114 | for (auto *typeParam : *typeParamList) { |
| 1115 | // Visit the type parameter. |
| 1116 | if (Visit(MakeCXCursor(typeParam, TU, RegionOfInterest))) |
| 1117 | return true; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | return false; |
| 1121 | } |
| 1122 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1123 | bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 1124 | if (!D->isThisDeclarationADefinition()) { |
| 1125 | // Forward declaration is treated like a reference. |
| 1126 | return Visit(MakeCursorObjCClassRef(D, D->getLocation(), TU)); |
| 1127 | } |
| 1128 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1129 | // Objective-C type parameters. |
| 1130 | if (VisitObjCTypeParamList(D->getTypeParamListAsWritten())) |
| 1131 | return true; |
| 1132 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1133 | // Issue callbacks for super class. |
| 1134 | if (D->getSuperClass() && |
| 1135 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
| 1136 | D->getSuperClassLoc(), |
| 1137 | TU))) |
| 1138 | return true; |
| 1139 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1140 | if (TypeSourceInfo *SuperClassTInfo = D->getSuperClassTInfo()) |
| 1141 | if (Visit(SuperClassTInfo->getTypeLoc())) |
| 1142 | return true; |
| 1143 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1144 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1145 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 1146 | E = D->protocol_end(); I != E; ++I, ++PL) |
| 1147 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1148 | return true; |
| 1149 | |
| 1150 | return VisitObjCContainerDecl(D); |
| 1151 | } |
| 1152 | |
| 1153 | bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 1154 | return VisitObjCContainerDecl(D); |
| 1155 | } |
| 1156 | |
| 1157 | bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 1158 | // 'ID' could be null when dealing with invalid code. |
| 1159 | if (ObjCInterfaceDecl *ID = D->getClassInterface()) |
| 1160 | if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) |
| 1161 | return true; |
| 1162 | |
| 1163 | return VisitObjCImplDecl(D); |
| 1164 | } |
| 1165 | |
| 1166 | bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 1167 | #if 0 |
| 1168 | // Issue callbacks for super class. |
| 1169 | // FIXME: No source location information! |
| 1170 | if (D->getSuperClass() && |
| 1171 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
| 1172 | D->getSuperClassLoc(), |
| 1173 | TU))) |
| 1174 | return true; |
| 1175 | #endif |
| 1176 | |
| 1177 | return VisitObjCImplDecl(D); |
| 1178 | } |
| 1179 | |
| 1180 | bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) { |
| 1181 | if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl()) |
| 1182 | if (PD->isIvarNameSpecified()) |
| 1183 | return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU)); |
| 1184 | |
| 1185 | return false; |
| 1186 | } |
| 1187 | |
| 1188 | bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { |
| 1189 | return VisitDeclContext(D); |
| 1190 | } |
| 1191 | |
| 1192 | bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 1193 | // Visit nested-name-specifier. |
| 1194 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1195 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1196 | return true; |
| 1197 | |
| 1198 | return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), |
| 1199 | D->getTargetNameLoc(), TU)); |
| 1200 | } |
| 1201 | |
| 1202 | bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { |
| 1203 | // Visit nested-name-specifier. |
| 1204 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1205 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1206 | return true; |
| 1207 | } |
| 1208 | |
| 1209 | if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU))) |
| 1210 | return true; |
| 1211 | |
| 1212 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1213 | } |
| 1214 | |
| 1215 | bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 1216 | // Visit nested-name-specifier. |
| 1217 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1218 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1219 | return true; |
| 1220 | |
| 1221 | return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), |
| 1222 | D->getIdentLocation(), TU)); |
| 1223 | } |
| 1224 | |
| 1225 | bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
| 1226 | // Visit nested-name-specifier. |
| 1227 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1228 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1229 | return true; |
| 1230 | } |
| 1231 | |
| 1232 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1233 | } |
| 1234 | |
| 1235 | bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( |
| 1236 | UnresolvedUsingTypenameDecl *D) { |
| 1237 | // Visit nested-name-specifier. |
| 1238 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1239 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1240 | return true; |
| 1241 | |
| 1242 | return false; |
| 1243 | } |
| 1244 | |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 1245 | bool CursorVisitor::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 1246 | if (Visit(MakeCXCursor(D->getAssertExpr(), StmtParent, TU, RegionOfInterest))) |
| 1247 | return true; |
Richard Trieu | f3b7766 | 2016-09-13 01:37:01 +0000 | [diff] [blame] | 1248 | if (StringLiteral *Message = D->getMessage()) |
| 1249 | if (Visit(MakeCXCursor(Message, StmtParent, TU, RegionOfInterest))) |
| 1250 | return true; |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 1251 | return false; |
| 1252 | } |
| 1253 | |
Olivier Goffart | d211c64 | 2016-11-04 06:29:27 +0000 | [diff] [blame] | 1254 | bool CursorVisitor::VisitFriendDecl(FriendDecl *D) { |
| 1255 | if (NamedDecl *FriendD = D->getFriendDecl()) { |
| 1256 | if (Visit(MakeCXCursor(FriendD, TU, RegionOfInterest))) |
| 1257 | return true; |
| 1258 | } else if (TypeSourceInfo *TI = D->getFriendType()) { |
| 1259 | if (Visit(TI->getTypeLoc())) |
| 1260 | return true; |
| 1261 | } |
| 1262 | return false; |
| 1263 | } |
| 1264 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1265 | bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { |
| 1266 | switch (Name.getName().getNameKind()) { |
| 1267 | case clang::DeclarationName::Identifier: |
| 1268 | case clang::DeclarationName::CXXLiteralOperatorName: |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 1269 | case clang::DeclarationName::CXXDeductionGuideName: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1270 | case clang::DeclarationName::CXXOperatorName: |
| 1271 | case clang::DeclarationName::CXXUsingDirective: |
| 1272 | return false; |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 1273 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1274 | case clang::DeclarationName::CXXConstructorName: |
| 1275 | case clang::DeclarationName::CXXDestructorName: |
| 1276 | case clang::DeclarationName::CXXConversionFunctionName: |
| 1277 | if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) |
| 1278 | return Visit(TSInfo->getTypeLoc()); |
| 1279 | return false; |
| 1280 | |
| 1281 | case clang::DeclarationName::ObjCZeroArgSelector: |
| 1282 | case clang::DeclarationName::ObjCOneArgSelector: |
| 1283 | case clang::DeclarationName::ObjCMultiArgSelector: |
| 1284 | // FIXME: Per-identifier location info? |
| 1285 | return false; |
| 1286 | } |
| 1287 | |
| 1288 | llvm_unreachable("Invalid DeclarationName::Kind!"); |
| 1289 | } |
| 1290 | |
| 1291 | bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, |
| 1292 | SourceRange Range) { |
| 1293 | // FIXME: This whole routine is a hack to work around the lack of proper |
| 1294 | // source information in nested-name-specifiers (PR5791). Since we do have |
| 1295 | // a beginning source location, we can visit the first component of the |
| 1296 | // nested-name-specifier, if it's a single-token component. |
| 1297 | if (!NNS) |
| 1298 | return false; |
| 1299 | |
| 1300 | // Get the first component in the nested-name-specifier. |
| 1301 | while (NestedNameSpecifier *Prefix = NNS->getPrefix()) |
| 1302 | NNS = Prefix; |
| 1303 | |
| 1304 | switch (NNS->getKind()) { |
| 1305 | case NestedNameSpecifier::Namespace: |
| 1306 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), |
| 1307 | TU)); |
| 1308 | |
| 1309 | case NestedNameSpecifier::NamespaceAlias: |
| 1310 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1311 | Range.getBegin(), TU)); |
| 1312 | |
| 1313 | case NestedNameSpecifier::TypeSpec: { |
| 1314 | // If the type has a form where we know that the beginning of the source |
| 1315 | // range matches up with a reference cursor. Visit the appropriate reference |
| 1316 | // cursor. |
| 1317 | const Type *T = NNS->getAsType(); |
| 1318 | if (const TypedefType *Typedef = dyn_cast<TypedefType>(T)) |
| 1319 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); |
| 1320 | if (const TagType *Tag = dyn_cast<TagType>(T)) |
| 1321 | return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); |
| 1322 | if (const TemplateSpecializationType *TST |
| 1323 | = dyn_cast<TemplateSpecializationType>(T)) |
| 1324 | return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); |
| 1325 | break; |
| 1326 | } |
| 1327 | |
| 1328 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1329 | case NestedNameSpecifier::Global: |
| 1330 | case NestedNameSpecifier::Identifier: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 1331 | case NestedNameSpecifier::Super: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1332 | break; |
| 1333 | } |
| 1334 | |
| 1335 | return false; |
| 1336 | } |
| 1337 | |
| 1338 | bool |
| 1339 | CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 1340 | SmallVector<NestedNameSpecifierLoc, 4> Qualifiers; |
| 1341 | for (; Qualifier; Qualifier = Qualifier.getPrefix()) |
| 1342 | Qualifiers.push_back(Qualifier); |
| 1343 | |
| 1344 | while (!Qualifiers.empty()) { |
| 1345 | NestedNameSpecifierLoc Q = Qualifiers.pop_back_val(); |
| 1346 | NestedNameSpecifier *NNS = Q.getNestedNameSpecifier(); |
| 1347 | switch (NNS->getKind()) { |
| 1348 | case NestedNameSpecifier::Namespace: |
| 1349 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), |
| 1350 | Q.getLocalBeginLoc(), |
| 1351 | TU))) |
| 1352 | return true; |
| 1353 | |
| 1354 | break; |
| 1355 | |
| 1356 | case NestedNameSpecifier::NamespaceAlias: |
| 1357 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1358 | Q.getLocalBeginLoc(), |
| 1359 | TU))) |
| 1360 | return true; |
| 1361 | |
| 1362 | break; |
| 1363 | |
| 1364 | case NestedNameSpecifier::TypeSpec: |
| 1365 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1366 | if (Visit(Q.getTypeLoc())) |
| 1367 | return true; |
| 1368 | |
| 1369 | break; |
| 1370 | |
| 1371 | case NestedNameSpecifier::Global: |
| 1372 | case NestedNameSpecifier::Identifier: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 1373 | case NestedNameSpecifier::Super: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1374 | break; |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | return false; |
| 1379 | } |
| 1380 | |
| 1381 | bool CursorVisitor::VisitTemplateParameters( |
| 1382 | const TemplateParameterList *Params) { |
| 1383 | if (!Params) |
| 1384 | return false; |
| 1385 | |
| 1386 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 1387 | PEnd = Params->end(); |
| 1388 | P != PEnd; ++P) { |
| 1389 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
| 1390 | return true; |
| 1391 | } |
| 1392 | |
| 1393 | return false; |
| 1394 | } |
| 1395 | |
| 1396 | bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { |
| 1397 | switch (Name.getKind()) { |
| 1398 | case TemplateName::Template: |
| 1399 | return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); |
| 1400 | |
| 1401 | case TemplateName::OverloadedTemplate: |
| 1402 | // Visit the overloaded template set. |
| 1403 | if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU))) |
| 1404 | return true; |
| 1405 | |
| 1406 | return false; |
| 1407 | |
| 1408 | case TemplateName::DependentTemplate: |
| 1409 | // FIXME: Visit nested-name-specifier. |
| 1410 | return false; |
| 1411 | |
| 1412 | case TemplateName::QualifiedTemplate: |
| 1413 | // FIXME: Visit nested-name-specifier. |
| 1414 | return Visit(MakeCursorTemplateRef( |
| 1415 | Name.getAsQualifiedTemplateName()->getDecl(), |
| 1416 | Loc, TU)); |
| 1417 | |
| 1418 | case TemplateName::SubstTemplateTemplateParm: |
| 1419 | return Visit(MakeCursorTemplateRef( |
| 1420 | Name.getAsSubstTemplateTemplateParm()->getParameter(), |
| 1421 | Loc, TU)); |
| 1422 | |
| 1423 | case TemplateName::SubstTemplateTemplateParmPack: |
| 1424 | return Visit(MakeCursorTemplateRef( |
| 1425 | Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), |
| 1426 | Loc, TU)); |
| 1427 | } |
| 1428 | |
| 1429 | llvm_unreachable("Invalid TemplateName::Kind!"); |
| 1430 | } |
| 1431 | |
| 1432 | bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { |
| 1433 | switch (TAL.getArgument().getKind()) { |
| 1434 | case TemplateArgument::Null: |
| 1435 | case TemplateArgument::Integral: |
| 1436 | case TemplateArgument::Pack: |
| 1437 | return false; |
| 1438 | |
| 1439 | case TemplateArgument::Type: |
| 1440 | if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) |
| 1441 | return Visit(TSInfo->getTypeLoc()); |
| 1442 | return false; |
| 1443 | |
| 1444 | case TemplateArgument::Declaration: |
| 1445 | if (Expr *E = TAL.getSourceDeclExpression()) |
| 1446 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
| 1447 | return false; |
| 1448 | |
| 1449 | case TemplateArgument::NullPtr: |
| 1450 | if (Expr *E = TAL.getSourceNullPtrExpression()) |
| 1451 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
| 1452 | return false; |
| 1453 | |
| 1454 | case TemplateArgument::Expression: |
| 1455 | if (Expr *E = TAL.getSourceExpression()) |
| 1456 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
| 1457 | return false; |
| 1458 | |
| 1459 | case TemplateArgument::Template: |
| 1460 | case TemplateArgument::TemplateExpansion: |
| 1461 | if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc())) |
| 1462 | return true; |
| 1463 | |
| 1464 | return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(), |
| 1465 | TAL.getTemplateNameLoc()); |
| 1466 | } |
| 1467 | |
| 1468 | llvm_unreachable("Invalid TemplateArgument::Kind!"); |
| 1469 | } |
| 1470 | |
| 1471 | bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 1472 | return VisitDeclContext(D); |
| 1473 | } |
| 1474 | |
| 1475 | bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1476 | return Visit(TL.getUnqualifiedLoc()); |
| 1477 | } |
| 1478 | |
| 1479 | bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
| 1480 | ASTContext &Context = AU->getASTContext(); |
| 1481 | |
| 1482 | // Some builtin types (such as Objective-C's "id", "sel", and |
| 1483 | // "Class") have associated declarations. Create cursors for those. |
| 1484 | QualType VisitType; |
| 1485 | switch (TL.getTypePtr()->getKind()) { |
| 1486 | |
| 1487 | case BuiltinType::Void: |
| 1488 | case BuiltinType::NullPtr: |
| 1489 | case BuiltinType::Dependent: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 1490 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 1491 | case BuiltinType::Id: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 1492 | #include "clang/Basic/OpenCLImageTypes.def" |
NAKAMURA Takumi | 288c42e | 2013-02-07 12:47:42 +0000 | [diff] [blame] | 1493 | case BuiltinType::OCLSampler: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1494 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1495 | case BuiltinType::OCLClkEvent: |
| 1496 | case BuiltinType::OCLQueue: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1497 | case BuiltinType::OCLReserveID: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1498 | #define BUILTIN_TYPE(Id, SingletonId) |
| 1499 | #define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1500 | #define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1501 | #define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1502 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1503 | #include "clang/AST/BuiltinTypes.def" |
| 1504 | break; |
| 1505 | |
| 1506 | case BuiltinType::ObjCId: |
| 1507 | VisitType = Context.getObjCIdType(); |
| 1508 | break; |
| 1509 | |
| 1510 | case BuiltinType::ObjCClass: |
| 1511 | VisitType = Context.getObjCClassType(); |
| 1512 | break; |
| 1513 | |
| 1514 | case BuiltinType::ObjCSel: |
| 1515 | VisitType = Context.getObjCSelType(); |
| 1516 | break; |
| 1517 | } |
| 1518 | |
| 1519 | if (!VisitType.isNull()) { |
| 1520 | if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) |
| 1521 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), |
| 1522 | TU)); |
| 1523 | } |
| 1524 | |
| 1525 | return false; |
| 1526 | } |
| 1527 | |
| 1528 | bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 1529 | return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU)); |
| 1530 | } |
| 1531 | |
| 1532 | bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 1533 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1534 | } |
| 1535 | |
| 1536 | bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { |
| 1537 | if (TL.isDefinition()) |
| 1538 | return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest)); |
| 1539 | |
| 1540 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1541 | } |
| 1542 | |
| 1543 | bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1544 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1545 | } |
| 1546 | |
| 1547 | bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 1548 | return Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 1551 | bool CursorVisitor::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { |
| 1552 | if (Visit(MakeCursorTypeRef(TL.getDecl(), TL.getLocStart(), TU))) |
| 1553 | return true; |
| 1554 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1555 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1556 | TU))) |
| 1557 | return true; |
| 1558 | } |
| 1559 | |
| 1560 | return false; |
| 1561 | } |
| 1562 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1563 | bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 1564 | if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) |
| 1565 | return true; |
| 1566 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1567 | for (unsigned I = 0, N = TL.getNumTypeArgs(); I != N; ++I) { |
| 1568 | if (Visit(TL.getTypeArgTInfo(I)->getTypeLoc())) |
| 1569 | return true; |
| 1570 | } |
| 1571 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1572 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1573 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1574 | TU))) |
| 1575 | return true; |
| 1576 | } |
| 1577 | |
| 1578 | return false; |
| 1579 | } |
| 1580 | |
| 1581 | bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 1582 | return Visit(TL.getPointeeLoc()); |
| 1583 | } |
| 1584 | |
| 1585 | bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) { |
| 1586 | return Visit(TL.getInnerLoc()); |
| 1587 | } |
| 1588 | |
| 1589 | bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1590 | return Visit(TL.getPointeeLoc()); |
| 1591 | } |
| 1592 | |
| 1593 | bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1594 | return Visit(TL.getPointeeLoc()); |
| 1595 | } |
| 1596 | |
| 1597 | bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1598 | return Visit(TL.getPointeeLoc()); |
| 1599 | } |
| 1600 | |
| 1601 | bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 1602 | return Visit(TL.getPointeeLoc()); |
| 1603 | } |
| 1604 | |
| 1605 | bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 1606 | return Visit(TL.getPointeeLoc()); |
| 1607 | } |
| 1608 | |
| 1609 | bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 1610 | return Visit(TL.getModifiedLoc()); |
| 1611 | } |
| 1612 | |
| 1613 | bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, |
| 1614 | bool SkipResultType) { |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 1615 | if (!SkipResultType && Visit(TL.getReturnLoc())) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1616 | return true; |
| 1617 | |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 1618 | for (unsigned I = 0, N = TL.getNumParams(); I != N; ++I) |
| 1619 | if (Decl *D = TL.getParam(I)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1620 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest))) |
| 1621 | return true; |
| 1622 | |
| 1623 | return false; |
| 1624 | } |
| 1625 | |
| 1626 | bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1627 | if (Visit(TL.getElementLoc())) |
| 1628 | return true; |
| 1629 | |
| 1630 | if (Expr *Size = TL.getSizeExpr()) |
| 1631 | return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest)); |
| 1632 | |
| 1633 | return false; |
| 1634 | } |
| 1635 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 1636 | bool CursorVisitor::VisitDecayedTypeLoc(DecayedTypeLoc TL) { |
| 1637 | return Visit(TL.getOriginalLoc()); |
| 1638 | } |
| 1639 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 1640 | bool CursorVisitor::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { |
| 1641 | return Visit(TL.getOriginalLoc()); |
| 1642 | } |
| 1643 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1644 | bool CursorVisitor::VisitDeducedTemplateSpecializationTypeLoc( |
| 1645 | DeducedTemplateSpecializationTypeLoc TL) { |
| 1646 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1647 | TL.getTemplateNameLoc())) |
| 1648 | return true; |
| 1649 | |
| 1650 | return false; |
| 1651 | } |
| 1652 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1653 | bool CursorVisitor::VisitTemplateSpecializationTypeLoc( |
| 1654 | TemplateSpecializationTypeLoc TL) { |
| 1655 | // Visit the template name. |
| 1656 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1657 | TL.getTemplateNameLoc())) |
| 1658 | return true; |
| 1659 | |
| 1660 | // Visit the template arguments. |
| 1661 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1662 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1663 | return true; |
| 1664 | |
| 1665 | return false; |
| 1666 | } |
| 1667 | |
| 1668 | bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 1669 | return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); |
| 1670 | } |
| 1671 | |
| 1672 | bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 1673 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1674 | return Visit(TSInfo->getTypeLoc()); |
| 1675 | |
| 1676 | return false; |
| 1677 | } |
| 1678 | |
| 1679 | bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 1680 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1681 | return Visit(TSInfo->getTypeLoc()); |
| 1682 | |
| 1683 | return false; |
| 1684 | } |
| 1685 | |
| 1686 | bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 1687 | return VisitNestedNameSpecifierLoc(TL.getQualifierLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc( |
| 1691 | DependentTemplateSpecializationTypeLoc TL) { |
| 1692 | // Visit the nested-name-specifier, if there is one. |
| 1693 | if (TL.getQualifierLoc() && |
| 1694 | VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1695 | return true; |
| 1696 | |
| 1697 | // Visit the template arguments. |
| 1698 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1699 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1700 | return true; |
| 1701 | |
| 1702 | return false; |
| 1703 | } |
| 1704 | |
| 1705 | bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 1706 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1707 | return true; |
| 1708 | |
| 1709 | return Visit(TL.getNamedTypeLoc()); |
| 1710 | } |
| 1711 | |
| 1712 | bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { |
| 1713 | return Visit(TL.getPatternLoc()); |
| 1714 | } |
| 1715 | |
| 1716 | bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 1717 | if (Expr *E = TL.getUnderlyingExpr()) |
| 1718 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1719 | |
| 1720 | return false; |
| 1721 | } |
| 1722 | |
| 1723 | bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 1724 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1725 | } |
| 1726 | |
| 1727 | bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) { |
| 1728 | return Visit(TL.getValueLoc()); |
| 1729 | } |
| 1730 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 1731 | bool CursorVisitor::VisitPipeTypeLoc(PipeTypeLoc TL) { |
| 1732 | return Visit(TL.getValueLoc()); |
| 1733 | } |
| 1734 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1735 | #define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \ |
| 1736 | bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \ |
| 1737 | return Visit##PARENT##Loc(TL); \ |
| 1738 | } |
| 1739 | |
| 1740 | DEFAULT_TYPELOC_IMPL(Complex, Type) |
| 1741 | DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType) |
| 1742 | DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType) |
| 1743 | DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType) |
| 1744 | DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType) |
| 1745 | DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type) |
| 1746 | DEFAULT_TYPELOC_IMPL(Vector, Type) |
| 1747 | DEFAULT_TYPELOC_IMPL(ExtVector, VectorType) |
| 1748 | DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType) |
| 1749 | DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType) |
| 1750 | DEFAULT_TYPELOC_IMPL(Record, TagType) |
| 1751 | DEFAULT_TYPELOC_IMPL(Enum, TagType) |
| 1752 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type) |
| 1753 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type) |
| 1754 | DEFAULT_TYPELOC_IMPL(Auto, Type) |
| 1755 | |
| 1756 | bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 1757 | // Visit the nested-name-specifier, if present. |
| 1758 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1759 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1760 | return true; |
| 1761 | |
| 1762 | if (D->isCompleteDefinition()) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1763 | for (const auto &I : D->bases()) { |
| 1764 | if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(&I, TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1765 | return true; |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | return VisitTagDecl(D); |
| 1770 | } |
| 1771 | |
| 1772 | bool CursorVisitor::VisitAttributes(Decl *D) { |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 1773 | for (const auto *I : D->attrs()) |
| 1774 | if (Visit(MakeCXCursor(I, D, TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1775 | return true; |
| 1776 | |
| 1777 | return false; |
| 1778 | } |
| 1779 | |
| 1780 | //===----------------------------------------------------------------------===// |
| 1781 | // Data-recursive visitor methods. |
| 1782 | //===----------------------------------------------------------------------===// |
| 1783 | |
| 1784 | namespace { |
| 1785 | #define DEF_JOB(NAME, DATA, KIND)\ |
| 1786 | class NAME : public VisitorJob {\ |
| 1787 | public:\ |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1788 | NAME(const DATA *d, CXCursor parent) : \ |
| 1789 | VisitorJob(parent, VisitorJob::KIND, d) {} \ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1790 | static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\ |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1791 | const DATA *get() const { return static_cast<const DATA*>(data[0]); }\ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1792 | }; |
| 1793 | |
| 1794 | DEF_JOB(StmtVisit, Stmt, StmtVisitKind) |
| 1795 | DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind) |
| 1796 | DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind) |
| 1797 | DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1798 | DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind) |
| 1799 | DEF_JOB(LambdaExprParts, LambdaExpr, LambdaExprPartsKind) |
| 1800 | DEF_JOB(PostChildrenVisit, void, PostChildrenVisitKind) |
| 1801 | #undef DEF_JOB |
| 1802 | |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 1803 | class ExplicitTemplateArgsVisit : public VisitorJob { |
| 1804 | public: |
| 1805 | ExplicitTemplateArgsVisit(const TemplateArgumentLoc *Begin, |
| 1806 | const TemplateArgumentLoc *End, CXCursor parent) |
| 1807 | : VisitorJob(parent, VisitorJob::ExplicitTemplateArgsVisitKind, Begin, |
| 1808 | End) {} |
| 1809 | static bool classof(const VisitorJob *VJ) { |
| 1810 | return VJ->getKind() == ExplicitTemplateArgsVisitKind; |
| 1811 | } |
| 1812 | const TemplateArgumentLoc *begin() const { |
| 1813 | return static_cast<const TemplateArgumentLoc *>(data[0]); |
| 1814 | } |
| 1815 | const TemplateArgumentLoc *end() { |
| 1816 | return static_cast<const TemplateArgumentLoc *>(data[1]); |
| 1817 | } |
| 1818 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1819 | class DeclVisit : public VisitorJob { |
| 1820 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1821 | DeclVisit(const Decl *D, CXCursor parent, bool isFirst) : |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1822 | VisitorJob(parent, VisitorJob::DeclVisitKind, |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 1823 | D, isFirst ? (void*) 1 : (void*) nullptr) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1824 | static bool classof(const VisitorJob *VJ) { |
| 1825 | return VJ->getKind() == DeclVisitKind; |
| 1826 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1827 | const Decl *get() const { return static_cast<const Decl *>(data[0]); } |
Dmitri Gribenko | e5423a7 | 2015-03-23 19:23:50 +0000 | [diff] [blame] | 1828 | bool isFirst() const { return data[1] != nullptr; } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1829 | }; |
| 1830 | class TypeLocVisit : public VisitorJob { |
| 1831 | public: |
| 1832 | TypeLocVisit(TypeLoc tl, CXCursor parent) : |
| 1833 | VisitorJob(parent, VisitorJob::TypeLocVisitKind, |
| 1834 | tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {} |
| 1835 | |
| 1836 | static bool classof(const VisitorJob *VJ) { |
| 1837 | return VJ->getKind() == TypeLocVisitKind; |
| 1838 | } |
| 1839 | |
| 1840 | TypeLoc get() const { |
| 1841 | QualType T = QualType::getFromOpaquePtr(data[0]); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1842 | return TypeLoc(T, const_cast<void *>(data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1843 | } |
| 1844 | }; |
| 1845 | |
| 1846 | class LabelRefVisit : public VisitorJob { |
| 1847 | public: |
| 1848 | LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent) |
| 1849 | : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD, |
| 1850 | labelLoc.getPtrEncoding()) {} |
| 1851 | |
| 1852 | static bool classof(const VisitorJob *VJ) { |
| 1853 | return VJ->getKind() == VisitorJob::LabelRefVisitKind; |
| 1854 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1855 | const LabelDecl *get() const { |
| 1856 | return static_cast<const LabelDecl *>(data[0]); |
| 1857 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1858 | SourceLocation getLoc() const { |
| 1859 | return SourceLocation::getFromPtrEncoding(data[1]); } |
| 1860 | }; |
| 1861 | |
| 1862 | class NestedNameSpecifierLocVisit : public VisitorJob { |
| 1863 | public: |
| 1864 | NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent) |
| 1865 | : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind, |
| 1866 | Qualifier.getNestedNameSpecifier(), |
| 1867 | Qualifier.getOpaqueData()) { } |
| 1868 | |
| 1869 | static bool classof(const VisitorJob *VJ) { |
| 1870 | return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind; |
| 1871 | } |
| 1872 | |
| 1873 | NestedNameSpecifierLoc get() const { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1874 | return NestedNameSpecifierLoc( |
| 1875 | const_cast<NestedNameSpecifier *>( |
| 1876 | static_cast<const NestedNameSpecifier *>(data[0])), |
| 1877 | const_cast<void *>(data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1878 | } |
| 1879 | }; |
| 1880 | |
| 1881 | class DeclarationNameInfoVisit : public VisitorJob { |
| 1882 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1883 | DeclarationNameInfoVisit(const Stmt *S, CXCursor parent) |
Dmitri Gribenko | dd7dacf | 2013-02-03 13:19:54 +0000 | [diff] [blame] | 1884 | : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1885 | static bool classof(const VisitorJob *VJ) { |
| 1886 | return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind; |
| 1887 | } |
| 1888 | DeclarationNameInfo get() const { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1889 | const Stmt *S = static_cast<const Stmt *>(data[0]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1890 | switch (S->getStmtClass()) { |
| 1891 | default: |
| 1892 | llvm_unreachable("Unhandled Stmt"); |
| 1893 | case clang::Stmt::MSDependentExistsStmtClass: |
| 1894 | return cast<MSDependentExistsStmt>(S)->getNameInfo(); |
| 1895 | case Stmt::CXXDependentScopeMemberExprClass: |
| 1896 | return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo(); |
| 1897 | case Stmt::DependentScopeDeclRefExprClass: |
| 1898 | return cast<DependentScopeDeclRefExpr>(S)->getNameInfo(); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1899 | case Stmt::OMPCriticalDirectiveClass: |
| 1900 | return cast<OMPCriticalDirective>(S)->getDirectiveName(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1901 | } |
| 1902 | } |
| 1903 | }; |
| 1904 | class MemberRefVisit : public VisitorJob { |
| 1905 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1906 | MemberRefVisit(const FieldDecl *D, SourceLocation L, CXCursor parent) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1907 | : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D, |
| 1908 | L.getPtrEncoding()) {} |
| 1909 | static bool classof(const VisitorJob *VJ) { |
| 1910 | return VJ->getKind() == VisitorJob::MemberRefVisitKind; |
| 1911 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1912 | const FieldDecl *get() const { |
| 1913 | return static_cast<const FieldDecl *>(data[0]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1914 | } |
| 1915 | SourceLocation getLoc() const { |
| 1916 | return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]); |
| 1917 | } |
| 1918 | }; |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1919 | class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void> { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1920 | friend class OMPClauseEnqueue; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1921 | VisitorWorkList &WL; |
| 1922 | CXCursor Parent; |
| 1923 | public: |
| 1924 | EnqueueVisitor(VisitorWorkList &wl, CXCursor parent) |
| 1925 | : WL(wl), Parent(parent) {} |
| 1926 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1927 | void VisitAddrLabelExpr(const AddrLabelExpr *E); |
| 1928 | void VisitBlockExpr(const BlockExpr *B); |
| 1929 | void VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 1930 | void VisitCompoundStmt(const CompoundStmt *S); |
| 1931 | void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { /* Do nothing. */ } |
| 1932 | void VisitMSDependentExistsStmt(const MSDependentExistsStmt *S); |
| 1933 | void VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E); |
| 1934 | void VisitCXXNewExpr(const CXXNewExpr *E); |
| 1935 | void VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E); |
| 1936 | void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *E); |
| 1937 | void VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E); |
| 1938 | void VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *E); |
| 1939 | void VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
| 1940 | void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *E); |
| 1941 | void VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
| 1942 | void VisitCXXCatchStmt(const CXXCatchStmt *S); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 1943 | void VisitCXXForRangeStmt(const CXXForRangeStmt *S); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1944 | void VisitDeclRefExpr(const DeclRefExpr *D); |
| 1945 | void VisitDeclStmt(const DeclStmt *S); |
| 1946 | void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E); |
| 1947 | void VisitDesignatedInitExpr(const DesignatedInitExpr *E); |
| 1948 | void VisitExplicitCastExpr(const ExplicitCastExpr *E); |
| 1949 | void VisitForStmt(const ForStmt *FS); |
| 1950 | void VisitGotoStmt(const GotoStmt *GS); |
| 1951 | void VisitIfStmt(const IfStmt *If); |
| 1952 | void VisitInitListExpr(const InitListExpr *IE); |
| 1953 | void VisitMemberExpr(const MemberExpr *M); |
| 1954 | void VisitOffsetOfExpr(const OffsetOfExpr *E); |
| 1955 | void VisitObjCEncodeExpr(const ObjCEncodeExpr *E); |
| 1956 | void VisitObjCMessageExpr(const ObjCMessageExpr *M); |
| 1957 | void VisitOverloadExpr(const OverloadExpr *E); |
| 1958 | void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
| 1959 | void VisitStmt(const Stmt *S); |
| 1960 | void VisitSwitchStmt(const SwitchStmt *S); |
| 1961 | void VisitWhileStmt(const WhileStmt *W); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1962 | void VisitTypeTraitExpr(const TypeTraitExpr *E); |
| 1963 | void VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E); |
| 1964 | void VisitExpressionTraitExpr(const ExpressionTraitExpr *E); |
| 1965 | void VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U); |
| 1966 | void VisitVAArgExpr(const VAArgExpr *E); |
| 1967 | void VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
| 1968 | void VisitPseudoObjectExpr(const PseudoObjectExpr *E); |
| 1969 | void VisitOpaqueValueExpr(const OpaqueValueExpr *E); |
| 1970 | void VisitLambdaExpr(const LambdaExpr *E); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1971 | void VisitOMPExecutableDirective(const OMPExecutableDirective *D); |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 1972 | void VisitOMPLoopDirective(const OMPLoopDirective *D); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1973 | void VisitOMPParallelDirective(const OMPParallelDirective *D); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 1974 | void VisitOMPSimdDirective(const OMPSimdDirective *D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1975 | void VisitOMPForDirective(const OMPForDirective *D); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1976 | void VisitOMPForSimdDirective(const OMPForSimdDirective *D); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 1977 | void VisitOMPSectionsDirective(const OMPSectionsDirective *D); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1978 | void VisitOMPSectionDirective(const OMPSectionDirective *D); |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 1979 | void VisitOMPSingleDirective(const OMPSingleDirective *D); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1980 | void VisitOMPMasterDirective(const OMPMasterDirective *D); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1981 | void VisitOMPCriticalDirective(const OMPCriticalDirective *D); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1982 | void VisitOMPParallelForDirective(const OMPParallelForDirective *D); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1983 | void VisitOMPParallelForSimdDirective(const OMPParallelForSimdDirective *D); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1984 | void VisitOMPParallelSectionsDirective(const OMPParallelSectionsDirective *D); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1985 | void VisitOMPTaskDirective(const OMPTaskDirective *D); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1986 | void VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 1987 | void VisitOMPBarrierDirective(const OMPBarrierDirective *D); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 1988 | void VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1989 | void VisitOMPTaskgroupDirective(const OMPTaskgroupDirective *D); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 1990 | void |
| 1991 | VisitOMPCancellationPointDirective(const OMPCancellationPointDirective *D); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 1992 | void VisitOMPCancelDirective(const OMPCancelDirective *D); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 1993 | void VisitOMPFlushDirective(const OMPFlushDirective *D); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1994 | void VisitOMPOrderedDirective(const OMPOrderedDirective *D); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1995 | void VisitOMPAtomicDirective(const OMPAtomicDirective *D); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1996 | void VisitOMPTargetDirective(const OMPTargetDirective *D); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 1997 | void VisitOMPTargetDataDirective(const OMPTargetDataDirective *D); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 1998 | void VisitOMPTargetEnterDataDirective(const OMPTargetEnterDataDirective *D); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 1999 | void VisitOMPTargetExitDataDirective(const OMPTargetExitDataDirective *D); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 2000 | void VisitOMPTargetParallelDirective(const OMPTargetParallelDirective *D); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 2001 | void |
| 2002 | VisitOMPTargetParallelForDirective(const OMPTargetParallelForDirective *D); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2003 | void VisitOMPTeamsDirective(const OMPTeamsDirective *D); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 2004 | void VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 2005 | void VisitOMPTaskLoopSimdDirective(const OMPTaskLoopSimdDirective *D); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2006 | void VisitOMPDistributeDirective(const OMPDistributeDirective *D); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2007 | void VisitOMPDistributeParallelForDirective( |
| 2008 | const OMPDistributeParallelForDirective *D); |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2009 | void VisitOMPDistributeParallelForSimdDirective( |
| 2010 | const OMPDistributeParallelForSimdDirective *D); |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2011 | void VisitOMPDistributeSimdDirective(const OMPDistributeSimdDirective *D); |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 2012 | void VisitOMPTargetParallelForSimdDirective( |
| 2013 | const OMPTargetParallelForSimdDirective *D); |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2014 | void VisitOMPTargetSimdDirective(const OMPTargetSimdDirective *D); |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 2015 | void VisitOMPTeamsDistributeDirective(const OMPTeamsDistributeDirective *D); |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 2016 | void VisitOMPTeamsDistributeSimdDirective( |
| 2017 | const OMPTeamsDistributeSimdDirective *D); |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 2018 | void VisitOMPTeamsDistributeParallelForSimdDirective( |
| 2019 | const OMPTeamsDistributeParallelForSimdDirective *D); |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 2020 | void VisitOMPTeamsDistributeParallelForDirective( |
| 2021 | const OMPTeamsDistributeParallelForDirective *D); |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 2022 | void VisitOMPTargetTeamsDirective(const OMPTargetTeamsDirective *D); |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 2023 | void VisitOMPTargetTeamsDistributeDirective( |
| 2024 | const OMPTargetTeamsDistributeDirective *D); |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 2025 | void VisitOMPTargetTeamsDistributeParallelForDirective( |
| 2026 | const OMPTargetTeamsDistributeParallelForDirective *D); |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 2027 | void VisitOMPTargetTeamsDistributeParallelForSimdDirective( |
| 2028 | const OMPTargetTeamsDistributeParallelForSimdDirective *D); |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 2029 | void VisitOMPTargetTeamsDistributeSimdDirective( |
| 2030 | const OMPTargetTeamsDistributeSimdDirective *D); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2031 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2032 | private: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2033 | void AddDeclarationNameInfo(const Stmt *S); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2034 | void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier); |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2035 | void AddExplicitTemplateArgs(const TemplateArgumentLoc *A, |
| 2036 | unsigned NumTemplateArgs); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2037 | void AddMemberRef(const FieldDecl *D, SourceLocation L); |
| 2038 | void AddStmt(const Stmt *S); |
| 2039 | void AddDecl(const Decl *D, bool isFirst = true); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2040 | void AddTypeLoc(TypeSourceInfo *TI); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2041 | void EnqueueChildren(const Stmt *S); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2042 | void EnqueueChildren(const OMPClause *S); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2043 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2044 | } // end anonyous namespace |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2045 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2046 | void EnqueueVisitor::AddDeclarationNameInfo(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2047 | // 'S' should always be non-null, since it comes from the |
| 2048 | // statement we are visiting. |
| 2049 | WL.push_back(DeclarationNameInfoVisit(S, Parent)); |
| 2050 | } |
| 2051 | |
| 2052 | void |
| 2053 | EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 2054 | if (Qualifier) |
| 2055 | WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent)); |
| 2056 | } |
| 2057 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2058 | void EnqueueVisitor::AddStmt(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2059 | if (S) |
| 2060 | WL.push_back(StmtVisit(S, Parent)); |
| 2061 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2062 | void EnqueueVisitor::AddDecl(const Decl *D, bool isFirst) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2063 | if (D) |
| 2064 | WL.push_back(DeclVisit(D, Parent, isFirst)); |
| 2065 | } |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2066 | void EnqueueVisitor::AddExplicitTemplateArgs(const TemplateArgumentLoc *A, |
| 2067 | unsigned NumTemplateArgs) { |
| 2068 | WL.push_back(ExplicitTemplateArgsVisit(A, A + NumTemplateArgs, Parent)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2069 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2070 | void EnqueueVisitor::AddMemberRef(const FieldDecl *D, SourceLocation L) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2071 | if (D) |
| 2072 | WL.push_back(MemberRefVisit(D, L, Parent)); |
| 2073 | } |
| 2074 | void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) { |
| 2075 | if (TI) |
| 2076 | WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent)); |
| 2077 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2078 | void EnqueueVisitor::EnqueueChildren(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2079 | unsigned size = WL.size(); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2080 | for (const Stmt *SubStmt : S->children()) { |
| 2081 | AddStmt(SubStmt); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2082 | } |
| 2083 | if (size == WL.size()) |
| 2084 | return; |
| 2085 | // Now reverse the entries we just added. This will match the DFS |
| 2086 | // ordering performed by the worklist. |
| 2087 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2088 | std::reverse(I, E); |
| 2089 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2090 | namespace { |
| 2091 | class OMPClauseEnqueue : public ConstOMPClauseVisitor<OMPClauseEnqueue> { |
| 2092 | EnqueueVisitor *Visitor; |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2093 | /// \brief Process clauses with list of variables. |
| 2094 | template <typename T> |
| 2095 | void VisitOMPClauseList(T *Node); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2096 | public: |
| 2097 | OMPClauseEnqueue(EnqueueVisitor *Visitor) : Visitor(Visitor) { } |
| 2098 | #define OPENMP_CLAUSE(Name, Class) \ |
| 2099 | void Visit##Class(const Class *C); |
| 2100 | #include "clang/Basic/OpenMPKinds.def" |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2101 | void VisitOMPClauseWithPreInit(const OMPClauseWithPreInit *C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2102 | void VisitOMPClauseWithPostUpdate(const OMPClauseWithPostUpdate *C); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2103 | }; |
| 2104 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2105 | void OMPClauseEnqueue::VisitOMPClauseWithPreInit( |
| 2106 | const OMPClauseWithPreInit *C) { |
| 2107 | Visitor->AddStmt(C->getPreInitStmt()); |
| 2108 | } |
| 2109 | |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2110 | void OMPClauseEnqueue::VisitOMPClauseWithPostUpdate( |
| 2111 | const OMPClauseWithPostUpdate *C) { |
Alexey Bataev | 37e594c | 2016-03-04 07:21:16 +0000 | [diff] [blame] | 2112 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2113 | Visitor->AddStmt(C->getPostUpdateExpr()); |
| 2114 | } |
| 2115 | |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2116 | void OMPClauseEnqueue::VisitOMPIfClause(const OMPIfClause *C) { |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 2117 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2118 | Visitor->AddStmt(C->getCondition()); |
| 2119 | } |
| 2120 | |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 2121 | void OMPClauseEnqueue::VisitOMPFinalClause(const OMPFinalClause *C) { |
| 2122 | Visitor->AddStmt(C->getCondition()); |
| 2123 | } |
| 2124 | |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2125 | void OMPClauseEnqueue::VisitOMPNumThreadsClause(const OMPNumThreadsClause *C) { |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 2126 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2127 | Visitor->AddStmt(C->getNumThreads()); |
| 2128 | } |
| 2129 | |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 2130 | void OMPClauseEnqueue::VisitOMPSafelenClause(const OMPSafelenClause *C) { |
| 2131 | Visitor->AddStmt(C->getSafelen()); |
| 2132 | } |
| 2133 | |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 2134 | void OMPClauseEnqueue::VisitOMPSimdlenClause(const OMPSimdlenClause *C) { |
| 2135 | Visitor->AddStmt(C->getSimdlen()); |
| 2136 | } |
| 2137 | |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 2138 | void OMPClauseEnqueue::VisitOMPCollapseClause(const OMPCollapseClause *C) { |
| 2139 | Visitor->AddStmt(C->getNumForLoops()); |
| 2140 | } |
| 2141 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2142 | void OMPClauseEnqueue::VisitOMPDefaultClause(const OMPDefaultClause *C) { } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2143 | |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 2144 | void OMPClauseEnqueue::VisitOMPProcBindClause(const OMPProcBindClause *C) { } |
| 2145 | |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2146 | void OMPClauseEnqueue::VisitOMPScheduleClause(const OMPScheduleClause *C) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2147 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2148 | Visitor->AddStmt(C->getChunkSize()); |
| 2149 | } |
| 2150 | |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 2151 | void OMPClauseEnqueue::VisitOMPOrderedClause(const OMPOrderedClause *C) { |
| 2152 | Visitor->AddStmt(C->getNumForLoops()); |
| 2153 | } |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 2154 | |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 2155 | void OMPClauseEnqueue::VisitOMPNowaitClause(const OMPNowaitClause *) {} |
| 2156 | |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 2157 | void OMPClauseEnqueue::VisitOMPUntiedClause(const OMPUntiedClause *) {} |
| 2158 | |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 2159 | void OMPClauseEnqueue::VisitOMPMergeableClause(const OMPMergeableClause *) {} |
| 2160 | |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 2161 | void OMPClauseEnqueue::VisitOMPReadClause(const OMPReadClause *) {} |
| 2162 | |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 2163 | void OMPClauseEnqueue::VisitOMPWriteClause(const OMPWriteClause *) {} |
| 2164 | |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 2165 | void OMPClauseEnqueue::VisitOMPUpdateClause(const OMPUpdateClause *) {} |
| 2166 | |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 2167 | void OMPClauseEnqueue::VisitOMPCaptureClause(const OMPCaptureClause *) {} |
| 2168 | |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 2169 | void OMPClauseEnqueue::VisitOMPSeqCstClause(const OMPSeqCstClause *) {} |
| 2170 | |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 2171 | void OMPClauseEnqueue::VisitOMPThreadsClause(const OMPThreadsClause *) {} |
| 2172 | |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 2173 | void OMPClauseEnqueue::VisitOMPSIMDClause(const OMPSIMDClause *) {} |
| 2174 | |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 2175 | void OMPClauseEnqueue::VisitOMPNogroupClause(const OMPNogroupClause *) {} |
| 2176 | |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 2177 | void OMPClauseEnqueue::VisitOMPDeviceClause(const OMPDeviceClause *C) { |
| 2178 | Visitor->AddStmt(C->getDevice()); |
| 2179 | } |
| 2180 | |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 2181 | void OMPClauseEnqueue::VisitOMPNumTeamsClause(const OMPNumTeamsClause *C) { |
Arpith Chacko Jacob | bc12634 | 2017-01-25 11:28:18 +0000 | [diff] [blame] | 2182 | VisitOMPClauseWithPreInit(C); |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 2183 | Visitor->AddStmt(C->getNumTeams()); |
| 2184 | } |
| 2185 | |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 2186 | void OMPClauseEnqueue::VisitOMPThreadLimitClause(const OMPThreadLimitClause *C) { |
Arpith Chacko Jacob | 7ecc0b7 | 2017-01-25 11:44:35 +0000 | [diff] [blame] | 2187 | VisitOMPClauseWithPreInit(C); |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 2188 | Visitor->AddStmt(C->getThreadLimit()); |
| 2189 | } |
| 2190 | |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 2191 | void OMPClauseEnqueue::VisitOMPPriorityClause(const OMPPriorityClause *C) { |
| 2192 | Visitor->AddStmt(C->getPriority()); |
| 2193 | } |
| 2194 | |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 2195 | void OMPClauseEnqueue::VisitOMPGrainsizeClause(const OMPGrainsizeClause *C) { |
| 2196 | Visitor->AddStmt(C->getGrainsize()); |
| 2197 | } |
| 2198 | |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 2199 | void OMPClauseEnqueue::VisitOMPNumTasksClause(const OMPNumTasksClause *C) { |
| 2200 | Visitor->AddStmt(C->getNumTasks()); |
| 2201 | } |
| 2202 | |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 2203 | void OMPClauseEnqueue::VisitOMPHintClause(const OMPHintClause *C) { |
| 2204 | Visitor->AddStmt(C->getHint()); |
| 2205 | } |
| 2206 | |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2207 | template<typename T> |
| 2208 | void OMPClauseEnqueue::VisitOMPClauseList(T *Node) { |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2209 | for (const auto *I : Node->varlists()) { |
Aaron Ballman | 2205d2a | 2014-03-14 15:55:35 +0000 | [diff] [blame] | 2210 | Visitor->AddStmt(I); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2211 | } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2212 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2213 | |
| 2214 | void OMPClauseEnqueue::VisitOMPPrivateClause(const OMPPrivateClause *C) { |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2215 | VisitOMPClauseList(C); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2216 | for (const auto *E : C->private_copies()) { |
| 2217 | Visitor->AddStmt(E); |
| 2218 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2219 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2220 | void OMPClauseEnqueue::VisitOMPFirstprivateClause( |
| 2221 | const OMPFirstprivateClause *C) { |
| 2222 | VisitOMPClauseList(C); |
Alexey Bataev | 417089f | 2016-02-17 13:19:37 +0000 | [diff] [blame] | 2223 | VisitOMPClauseWithPreInit(C); |
| 2224 | for (const auto *E : C->private_copies()) { |
| 2225 | Visitor->AddStmt(E); |
| 2226 | } |
| 2227 | for (const auto *E : C->inits()) { |
| 2228 | Visitor->AddStmt(E); |
| 2229 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2230 | } |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2231 | void OMPClauseEnqueue::VisitOMPLastprivateClause( |
| 2232 | const OMPLastprivateClause *C) { |
| 2233 | VisitOMPClauseList(C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2234 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2235 | for (auto *E : C->private_copies()) { |
| 2236 | Visitor->AddStmt(E); |
| 2237 | } |
| 2238 | for (auto *E : C->source_exprs()) { |
| 2239 | Visitor->AddStmt(E); |
| 2240 | } |
| 2241 | for (auto *E : C->destination_exprs()) { |
| 2242 | Visitor->AddStmt(E); |
| 2243 | } |
| 2244 | for (auto *E : C->assignment_ops()) { |
| 2245 | Visitor->AddStmt(E); |
| 2246 | } |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2247 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2248 | void OMPClauseEnqueue::VisitOMPSharedClause(const OMPSharedClause *C) { |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2249 | VisitOMPClauseList(C); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2250 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2251 | void OMPClauseEnqueue::VisitOMPReductionClause(const OMPReductionClause *C) { |
| 2252 | VisitOMPClauseList(C); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2253 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 2254 | for (auto *E : C->privates()) { |
| 2255 | Visitor->AddStmt(E); |
| 2256 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2257 | for (auto *E : C->lhs_exprs()) { |
| 2258 | Visitor->AddStmt(E); |
| 2259 | } |
| 2260 | for (auto *E : C->rhs_exprs()) { |
| 2261 | Visitor->AddStmt(E); |
| 2262 | } |
| 2263 | for (auto *E : C->reduction_ops()) { |
| 2264 | Visitor->AddStmt(E); |
| 2265 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2266 | } |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2267 | void OMPClauseEnqueue::VisitOMPLinearClause(const OMPLinearClause *C) { |
| 2268 | VisitOMPClauseList(C); |
Alexey Bataev | 78849fb | 2016-03-09 09:49:00 +0000 | [diff] [blame] | 2269 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 2270 | for (const auto *E : C->privates()) { |
| 2271 | Visitor->AddStmt(E); |
| 2272 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 2273 | for (const auto *E : C->inits()) { |
| 2274 | Visitor->AddStmt(E); |
| 2275 | } |
| 2276 | for (const auto *E : C->updates()) { |
| 2277 | Visitor->AddStmt(E); |
| 2278 | } |
| 2279 | for (const auto *E : C->finals()) { |
| 2280 | Visitor->AddStmt(E); |
| 2281 | } |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2282 | Visitor->AddStmt(C->getStep()); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 2283 | Visitor->AddStmt(C->getCalcStep()); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2284 | } |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 2285 | void OMPClauseEnqueue::VisitOMPAlignedClause(const OMPAlignedClause *C) { |
| 2286 | VisitOMPClauseList(C); |
| 2287 | Visitor->AddStmt(C->getAlignment()); |
| 2288 | } |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 2289 | void OMPClauseEnqueue::VisitOMPCopyinClause(const OMPCopyinClause *C) { |
| 2290 | VisitOMPClauseList(C); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 2291 | for (auto *E : C->source_exprs()) { |
| 2292 | Visitor->AddStmt(E); |
| 2293 | } |
| 2294 | for (auto *E : C->destination_exprs()) { |
| 2295 | Visitor->AddStmt(E); |
| 2296 | } |
| 2297 | for (auto *E : C->assignment_ops()) { |
| 2298 | Visitor->AddStmt(E); |
| 2299 | } |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 2300 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2301 | void |
| 2302 | OMPClauseEnqueue::VisitOMPCopyprivateClause(const OMPCopyprivateClause *C) { |
| 2303 | VisitOMPClauseList(C); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2304 | for (auto *E : C->source_exprs()) { |
| 2305 | Visitor->AddStmt(E); |
| 2306 | } |
| 2307 | for (auto *E : C->destination_exprs()) { |
| 2308 | Visitor->AddStmt(E); |
| 2309 | } |
| 2310 | for (auto *E : C->assignment_ops()) { |
| 2311 | Visitor->AddStmt(E); |
| 2312 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2313 | } |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2314 | void OMPClauseEnqueue::VisitOMPFlushClause(const OMPFlushClause *C) { |
| 2315 | VisitOMPClauseList(C); |
| 2316 | } |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 2317 | void OMPClauseEnqueue::VisitOMPDependClause(const OMPDependClause *C) { |
| 2318 | VisitOMPClauseList(C); |
| 2319 | } |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 2320 | void OMPClauseEnqueue::VisitOMPMapClause(const OMPMapClause *C) { |
| 2321 | VisitOMPClauseList(C); |
| 2322 | } |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2323 | void OMPClauseEnqueue::VisitOMPDistScheduleClause( |
| 2324 | const OMPDistScheduleClause *C) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2325 | VisitOMPClauseWithPreInit(C); |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2326 | Visitor->AddStmt(C->getChunkSize()); |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2327 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2328 | void OMPClauseEnqueue::VisitOMPDefaultmapClause( |
| 2329 | const OMPDefaultmapClause * /*C*/) {} |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 2330 | void OMPClauseEnqueue::VisitOMPToClause(const OMPToClause *C) { |
| 2331 | VisitOMPClauseList(C); |
| 2332 | } |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 2333 | void OMPClauseEnqueue::VisitOMPFromClause(const OMPFromClause *C) { |
| 2334 | VisitOMPClauseList(C); |
| 2335 | } |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 2336 | void OMPClauseEnqueue::VisitOMPUseDevicePtrClause(const OMPUseDevicePtrClause *C) { |
| 2337 | VisitOMPClauseList(C); |
| 2338 | } |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 2339 | void OMPClauseEnqueue::VisitOMPIsDevicePtrClause(const OMPIsDevicePtrClause *C) { |
| 2340 | VisitOMPClauseList(C); |
| 2341 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2342 | } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2343 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2344 | void EnqueueVisitor::EnqueueChildren(const OMPClause *S) { |
| 2345 | unsigned size = WL.size(); |
| 2346 | OMPClauseEnqueue Visitor(this); |
| 2347 | Visitor.Visit(S); |
| 2348 | if (size == WL.size()) |
| 2349 | return; |
| 2350 | // Now reverse the entries we just added. This will match the DFS |
| 2351 | // ordering performed by the worklist. |
| 2352 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2353 | std::reverse(I, E); |
| 2354 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2355 | void EnqueueVisitor::VisitAddrLabelExpr(const AddrLabelExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2356 | WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent)); |
| 2357 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2358 | void EnqueueVisitor::VisitBlockExpr(const BlockExpr *B) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2359 | AddDecl(B->getBlockDecl()); |
| 2360 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2361 | void EnqueueVisitor::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2362 | EnqueueChildren(E); |
| 2363 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2364 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2365 | void EnqueueVisitor::VisitCompoundStmt(const CompoundStmt *S) { |
Pete Cooper | 57d3f14 | 2015-07-30 17:22:52 +0000 | [diff] [blame] | 2366 | for (auto &I : llvm::reverse(S->body())) |
| 2367 | AddStmt(I); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2368 | } |
| 2369 | void EnqueueVisitor:: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2370 | VisitMSDependentExistsStmt(const MSDependentExistsStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2371 | AddStmt(S->getSubStmt()); |
| 2372 | AddDeclarationNameInfo(S); |
| 2373 | if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc()) |
| 2374 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2375 | } |
| 2376 | |
| 2377 | void EnqueueVisitor:: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2378 | VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2379 | if (E->hasExplicitTemplateArgs()) |
| 2380 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2381 | AddDeclarationNameInfo(E); |
| 2382 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 2383 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2384 | if (!E->isImplicitAccess()) |
| 2385 | AddStmt(E->getBase()); |
| 2386 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2387 | void EnqueueVisitor::VisitCXXNewExpr(const CXXNewExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2388 | // Enqueue the initializer , if any. |
| 2389 | AddStmt(E->getInitializer()); |
| 2390 | // Enqueue the array size, if any. |
| 2391 | AddStmt(E->getArraySize()); |
| 2392 | // Enqueue the allocated type. |
| 2393 | AddTypeLoc(E->getAllocatedTypeSourceInfo()); |
| 2394 | // Enqueue the placement arguments. |
| 2395 | for (unsigned I = E->getNumPlacementArgs(); I > 0; --I) |
| 2396 | AddStmt(E->getPlacementArg(I-1)); |
| 2397 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2398 | void EnqueueVisitor::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CE) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2399 | for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I) |
| 2400 | AddStmt(CE->getArg(I-1)); |
| 2401 | AddStmt(CE->getCallee()); |
| 2402 | AddStmt(CE->getArg(0)); |
| 2403 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2404 | void EnqueueVisitor::VisitCXXPseudoDestructorExpr( |
| 2405 | const CXXPseudoDestructorExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2406 | // Visit the name of the type being destroyed. |
| 2407 | AddTypeLoc(E->getDestroyedTypeInfo()); |
| 2408 | // Visit the scope type that looks disturbingly like the nested-name-specifier |
| 2409 | // but isn't. |
| 2410 | AddTypeLoc(E->getScopeTypeInfo()); |
| 2411 | // Visit the nested-name-specifier. |
| 2412 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 2413 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2414 | // Visit base expression. |
| 2415 | AddStmt(E->getBase()); |
| 2416 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2417 | void EnqueueVisitor::VisitCXXScalarValueInitExpr( |
| 2418 | const CXXScalarValueInitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2419 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2420 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2421 | void EnqueueVisitor::VisitCXXTemporaryObjectExpr( |
| 2422 | const CXXTemporaryObjectExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2423 | EnqueueChildren(E); |
| 2424 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2425 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2426 | void EnqueueVisitor::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2427 | EnqueueChildren(E); |
| 2428 | if (E->isTypeOperand()) |
| 2429 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 2430 | } |
| 2431 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2432 | void EnqueueVisitor::VisitCXXUnresolvedConstructExpr( |
| 2433 | const CXXUnresolvedConstructExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2434 | EnqueueChildren(E); |
| 2435 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2436 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2437 | void EnqueueVisitor::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2438 | EnqueueChildren(E); |
| 2439 | if (E->isTypeOperand()) |
| 2440 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 2441 | } |
| 2442 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2443 | void EnqueueVisitor::VisitCXXCatchStmt(const CXXCatchStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2444 | EnqueueChildren(S); |
| 2445 | AddDecl(S->getExceptionDecl()); |
| 2446 | } |
| 2447 | |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2448 | void EnqueueVisitor::VisitCXXForRangeStmt(const CXXForRangeStmt *S) { |
Argyrios Kyrtzidis | cde7069 | 2014-11-13 09:50:19 +0000 | [diff] [blame] | 2449 | AddStmt(S->getBody()); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2450 | AddStmt(S->getRangeInit()); |
Argyrios Kyrtzidis | cde7069 | 2014-11-13 09:50:19 +0000 | [diff] [blame] | 2451 | AddDecl(S->getLoopVariable()); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2454 | void EnqueueVisitor::VisitDeclRefExpr(const DeclRefExpr *DR) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2455 | if (DR->hasExplicitTemplateArgs()) |
| 2456 | AddExplicitTemplateArgs(DR->getTemplateArgs(), DR->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2457 | WL.push_back(DeclRefExprParts(DR, Parent)); |
| 2458 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2459 | void EnqueueVisitor::VisitDependentScopeDeclRefExpr( |
| 2460 | const DependentScopeDeclRefExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2461 | if (E->hasExplicitTemplateArgs()) |
| 2462 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2463 | AddDeclarationNameInfo(E); |
| 2464 | AddNestedNameSpecifierLoc(E->getQualifierLoc()); |
| 2465 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2466 | void EnqueueVisitor::VisitDeclStmt(const DeclStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2467 | unsigned size = WL.size(); |
| 2468 | bool isFirst = true; |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 2469 | for (const auto *D : S->decls()) { |
| 2470 | AddDecl(D, isFirst); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2471 | isFirst = false; |
| 2472 | } |
| 2473 | if (size == WL.size()) |
| 2474 | return; |
| 2475 | // Now reverse the entries we just added. This will match the DFS |
| 2476 | // ordering performed by the worklist. |
| 2477 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2478 | std::reverse(I, E); |
| 2479 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2480 | void EnqueueVisitor::VisitDesignatedInitExpr(const DesignatedInitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2481 | AddStmt(E->getInit()); |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2482 | for (const DesignatedInitExpr::Designator &D : |
| 2483 | llvm::reverse(E->designators())) { |
| 2484 | if (D.isFieldDesignator()) { |
| 2485 | if (FieldDecl *Field = D.getField()) |
| 2486 | AddMemberRef(Field, D.getFieldLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2487 | continue; |
| 2488 | } |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2489 | if (D.isArrayDesignator()) { |
| 2490 | AddStmt(E->getArrayIndex(D)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2491 | continue; |
| 2492 | } |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2493 | assert(D.isArrayRangeDesignator() && "Unknown designator kind"); |
| 2494 | AddStmt(E->getArrayRangeEnd(D)); |
| 2495 | AddStmt(E->getArrayRangeStart(D)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2496 | } |
| 2497 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2498 | void EnqueueVisitor::VisitExplicitCastExpr(const ExplicitCastExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2499 | EnqueueChildren(E); |
| 2500 | AddTypeLoc(E->getTypeInfoAsWritten()); |
| 2501 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2502 | void EnqueueVisitor::VisitForStmt(const ForStmt *FS) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2503 | AddStmt(FS->getBody()); |
| 2504 | AddStmt(FS->getInc()); |
| 2505 | AddStmt(FS->getCond()); |
| 2506 | AddDecl(FS->getConditionVariable()); |
| 2507 | AddStmt(FS->getInit()); |
| 2508 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2509 | void EnqueueVisitor::VisitGotoStmt(const GotoStmt *GS) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2510 | WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent)); |
| 2511 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2512 | void EnqueueVisitor::VisitIfStmt(const IfStmt *If) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2513 | AddStmt(If->getElse()); |
| 2514 | AddStmt(If->getThen()); |
| 2515 | AddStmt(If->getCond()); |
| 2516 | AddDecl(If->getConditionVariable()); |
| 2517 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2518 | void EnqueueVisitor::VisitInitListExpr(const InitListExpr *IE) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2519 | // We care about the syntactic form of the initializer list, only. |
| 2520 | if (InitListExpr *Syntactic = IE->getSyntacticForm()) |
| 2521 | IE = Syntactic; |
| 2522 | EnqueueChildren(IE); |
| 2523 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2524 | void EnqueueVisitor::VisitMemberExpr(const MemberExpr *M) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2525 | WL.push_back(MemberExprParts(M, Parent)); |
| 2526 | |
| 2527 | // If the base of the member access expression is an implicit 'this', don't |
| 2528 | // visit it. |
| 2529 | // FIXME: If we ever want to show these implicit accesses, this will be |
| 2530 | // unfortunate. However, clang_getCursor() relies on this behavior. |
Argyrios Kyrtzidis | 58d0e7a | 2015-03-13 04:40:07 +0000 | [diff] [blame] | 2531 | if (M->isImplicitAccess()) |
| 2532 | return; |
| 2533 | |
| 2534 | // Ignore base anonymous struct/union fields, otherwise they will shadow the |
| 2535 | // real field that that we are interested in. |
| 2536 | if (auto *SubME = dyn_cast<MemberExpr>(M->getBase())) { |
| 2537 | if (auto *FD = dyn_cast_or_null<FieldDecl>(SubME->getMemberDecl())) { |
| 2538 | if (FD->isAnonymousStructOrUnion()) { |
| 2539 | AddStmt(SubME->getBase()); |
| 2540 | return; |
| 2541 | } |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | AddStmt(M->getBase()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2546 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2547 | void EnqueueVisitor::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2548 | AddTypeLoc(E->getEncodedTypeSourceInfo()); |
| 2549 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2550 | void EnqueueVisitor::VisitObjCMessageExpr(const ObjCMessageExpr *M) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2551 | EnqueueChildren(M); |
| 2552 | AddTypeLoc(M->getClassReceiverTypeInfo()); |
| 2553 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2554 | void EnqueueVisitor::VisitOffsetOfExpr(const OffsetOfExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2555 | // Visit the components of the offsetof expression. |
| 2556 | for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2557 | const OffsetOfNode &Node = E->getComponent(I-1); |
| 2558 | switch (Node.getKind()) { |
| 2559 | case OffsetOfNode::Array: |
| 2560 | AddStmt(E->getIndexExpr(Node.getArrayExprIndex())); |
| 2561 | break; |
| 2562 | case OffsetOfNode::Field: |
| 2563 | AddMemberRef(Node.getField(), Node.getSourceRange().getEnd()); |
| 2564 | break; |
| 2565 | case OffsetOfNode::Identifier: |
| 2566 | case OffsetOfNode::Base: |
| 2567 | continue; |
| 2568 | } |
| 2569 | } |
| 2570 | // Visit the type into which we're computing the offset. |
| 2571 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2572 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2573 | void EnqueueVisitor::VisitOverloadExpr(const OverloadExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2574 | if (E->hasExplicitTemplateArgs()) |
| 2575 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2576 | WL.push_back(OverloadExprParts(E, Parent)); |
| 2577 | } |
| 2578 | void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr( |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2579 | const UnaryExprOrTypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2580 | EnqueueChildren(E); |
| 2581 | if (E->isArgumentType()) |
| 2582 | AddTypeLoc(E->getArgumentTypeInfo()); |
| 2583 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2584 | void EnqueueVisitor::VisitStmt(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2585 | EnqueueChildren(S); |
| 2586 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2587 | void EnqueueVisitor::VisitSwitchStmt(const SwitchStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2588 | AddStmt(S->getBody()); |
| 2589 | AddStmt(S->getCond()); |
| 2590 | AddDecl(S->getConditionVariable()); |
| 2591 | } |
| 2592 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2593 | void EnqueueVisitor::VisitWhileStmt(const WhileStmt *W) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2594 | AddStmt(W->getBody()); |
| 2595 | AddStmt(W->getCond()); |
| 2596 | AddDecl(W->getConditionVariable()); |
| 2597 | } |
| 2598 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2599 | void EnqueueVisitor::VisitTypeTraitExpr(const TypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2600 | for (unsigned I = E->getNumArgs(); I > 0; --I) |
| 2601 | AddTypeLoc(E->getArg(I-1)); |
| 2602 | } |
| 2603 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2604 | void EnqueueVisitor::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2605 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2606 | } |
| 2607 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2608 | void EnqueueVisitor::VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2609 | EnqueueChildren(E); |
| 2610 | } |
| 2611 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2612 | void EnqueueVisitor::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2613 | VisitOverloadExpr(U); |
| 2614 | if (!U->isImplicitAccess()) |
| 2615 | AddStmt(U->getBase()); |
| 2616 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2617 | void EnqueueVisitor::VisitVAArgExpr(const VAArgExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2618 | AddStmt(E->getSubExpr()); |
| 2619 | AddTypeLoc(E->getWrittenTypeInfo()); |
| 2620 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2621 | void EnqueueVisitor::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2622 | WL.push_back(SizeOfPackExprParts(E, Parent)); |
| 2623 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2624 | void EnqueueVisitor::VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2625 | // If the opaque value has a source expression, just transparently |
| 2626 | // visit that. This is useful for (e.g.) pseudo-object expressions. |
| 2627 | if (Expr *SourceExpr = E->getSourceExpr()) |
| 2628 | return Visit(SourceExpr); |
| 2629 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2630 | void EnqueueVisitor::VisitLambdaExpr(const LambdaExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2631 | AddStmt(E->getBody()); |
| 2632 | WL.push_back(LambdaExprParts(E, Parent)); |
| 2633 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2634 | void EnqueueVisitor::VisitPseudoObjectExpr(const PseudoObjectExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2635 | // Treat the expression like its syntactic form. |
| 2636 | Visit(E->getSyntacticForm()); |
| 2637 | } |
| 2638 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2639 | void EnqueueVisitor::VisitOMPExecutableDirective( |
| 2640 | const OMPExecutableDirective *D) { |
| 2641 | EnqueueChildren(D); |
| 2642 | for (ArrayRef<OMPClause *>::iterator I = D->clauses().begin(), |
| 2643 | E = D->clauses().end(); |
| 2644 | I != E; ++I) |
| 2645 | EnqueueChildren(*I); |
| 2646 | } |
| 2647 | |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2648 | void EnqueueVisitor::VisitOMPLoopDirective(const OMPLoopDirective *D) { |
| 2649 | VisitOMPExecutableDirective(D); |
| 2650 | } |
| 2651 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2652 | void EnqueueVisitor::VisitOMPParallelDirective(const OMPParallelDirective *D) { |
| 2653 | VisitOMPExecutableDirective(D); |
| 2654 | } |
| 2655 | |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2656 | void EnqueueVisitor::VisitOMPSimdDirective(const OMPSimdDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2657 | VisitOMPLoopDirective(D); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2660 | void EnqueueVisitor::VisitOMPForDirective(const OMPForDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2661 | VisitOMPLoopDirective(D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2664 | void EnqueueVisitor::VisitOMPForSimdDirective(const OMPForSimdDirective *D) { |
| 2665 | VisitOMPLoopDirective(D); |
| 2666 | } |
| 2667 | |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2668 | void EnqueueVisitor::VisitOMPSectionsDirective(const OMPSectionsDirective *D) { |
| 2669 | VisitOMPExecutableDirective(D); |
| 2670 | } |
| 2671 | |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2672 | void EnqueueVisitor::VisitOMPSectionDirective(const OMPSectionDirective *D) { |
| 2673 | VisitOMPExecutableDirective(D); |
| 2674 | } |
| 2675 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 2676 | void EnqueueVisitor::VisitOMPSingleDirective(const OMPSingleDirective *D) { |
| 2677 | VisitOMPExecutableDirective(D); |
| 2678 | } |
| 2679 | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2680 | void EnqueueVisitor::VisitOMPMasterDirective(const OMPMasterDirective *D) { |
| 2681 | VisitOMPExecutableDirective(D); |
| 2682 | } |
| 2683 | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2684 | void EnqueueVisitor::VisitOMPCriticalDirective(const OMPCriticalDirective *D) { |
| 2685 | VisitOMPExecutableDirective(D); |
| 2686 | AddDeclarationNameInfo(D); |
| 2687 | } |
| 2688 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2689 | void |
| 2690 | EnqueueVisitor::VisitOMPParallelForDirective(const OMPParallelForDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2691 | VisitOMPLoopDirective(D); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2692 | } |
| 2693 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2694 | void EnqueueVisitor::VisitOMPParallelForSimdDirective( |
| 2695 | const OMPParallelForSimdDirective *D) { |
| 2696 | VisitOMPLoopDirective(D); |
| 2697 | } |
| 2698 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2699 | void EnqueueVisitor::VisitOMPParallelSectionsDirective( |
| 2700 | const OMPParallelSectionsDirective *D) { |
| 2701 | VisitOMPExecutableDirective(D); |
| 2702 | } |
| 2703 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2704 | void EnqueueVisitor::VisitOMPTaskDirective(const OMPTaskDirective *D) { |
| 2705 | VisitOMPExecutableDirective(D); |
| 2706 | } |
| 2707 | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2708 | void |
| 2709 | EnqueueVisitor::VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D) { |
| 2710 | VisitOMPExecutableDirective(D); |
| 2711 | } |
| 2712 | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 2713 | void EnqueueVisitor::VisitOMPBarrierDirective(const OMPBarrierDirective *D) { |
| 2714 | VisitOMPExecutableDirective(D); |
| 2715 | } |
| 2716 | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 2717 | void EnqueueVisitor::VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D) { |
| 2718 | VisitOMPExecutableDirective(D); |
| 2719 | } |
| 2720 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2721 | void EnqueueVisitor::VisitOMPTaskgroupDirective( |
| 2722 | const OMPTaskgroupDirective *D) { |
| 2723 | VisitOMPExecutableDirective(D); |
| 2724 | } |
| 2725 | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2726 | void EnqueueVisitor::VisitOMPFlushDirective(const OMPFlushDirective *D) { |
| 2727 | VisitOMPExecutableDirective(D); |
| 2728 | } |
| 2729 | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2730 | void EnqueueVisitor::VisitOMPOrderedDirective(const OMPOrderedDirective *D) { |
| 2731 | VisitOMPExecutableDirective(D); |
| 2732 | } |
| 2733 | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2734 | void EnqueueVisitor::VisitOMPAtomicDirective(const OMPAtomicDirective *D) { |
| 2735 | VisitOMPExecutableDirective(D); |
| 2736 | } |
| 2737 | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 2738 | void EnqueueVisitor::VisitOMPTargetDirective(const OMPTargetDirective *D) { |
| 2739 | VisitOMPExecutableDirective(D); |
| 2740 | } |
| 2741 | |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2742 | void EnqueueVisitor::VisitOMPTargetDataDirective(const |
| 2743 | OMPTargetDataDirective *D) { |
| 2744 | VisitOMPExecutableDirective(D); |
| 2745 | } |
| 2746 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 2747 | void EnqueueVisitor::VisitOMPTargetEnterDataDirective( |
| 2748 | const OMPTargetEnterDataDirective *D) { |
| 2749 | VisitOMPExecutableDirective(D); |
| 2750 | } |
| 2751 | |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 2752 | void EnqueueVisitor::VisitOMPTargetExitDataDirective( |
| 2753 | const OMPTargetExitDataDirective *D) { |
| 2754 | VisitOMPExecutableDirective(D); |
| 2755 | } |
| 2756 | |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 2757 | void EnqueueVisitor::VisitOMPTargetParallelDirective( |
| 2758 | const OMPTargetParallelDirective *D) { |
| 2759 | VisitOMPExecutableDirective(D); |
| 2760 | } |
| 2761 | |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 2762 | void EnqueueVisitor::VisitOMPTargetParallelForDirective( |
| 2763 | const OMPTargetParallelForDirective *D) { |
| 2764 | VisitOMPLoopDirective(D); |
| 2765 | } |
| 2766 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2767 | void EnqueueVisitor::VisitOMPTeamsDirective(const OMPTeamsDirective *D) { |
| 2768 | VisitOMPExecutableDirective(D); |
| 2769 | } |
| 2770 | |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2771 | void EnqueueVisitor::VisitOMPCancellationPointDirective( |
| 2772 | const OMPCancellationPointDirective *D) { |
| 2773 | VisitOMPExecutableDirective(D); |
| 2774 | } |
| 2775 | |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2776 | void EnqueueVisitor::VisitOMPCancelDirective(const OMPCancelDirective *D) { |
| 2777 | VisitOMPExecutableDirective(D); |
| 2778 | } |
| 2779 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 2780 | void EnqueueVisitor::VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D) { |
| 2781 | VisitOMPLoopDirective(D); |
| 2782 | } |
| 2783 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 2784 | void EnqueueVisitor::VisitOMPTaskLoopSimdDirective( |
| 2785 | const OMPTaskLoopSimdDirective *D) { |
| 2786 | VisitOMPLoopDirective(D); |
| 2787 | } |
| 2788 | |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2789 | void EnqueueVisitor::VisitOMPDistributeDirective( |
| 2790 | const OMPDistributeDirective *D) { |
| 2791 | VisitOMPLoopDirective(D); |
| 2792 | } |
| 2793 | |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2794 | void EnqueueVisitor::VisitOMPDistributeParallelForDirective( |
| 2795 | const OMPDistributeParallelForDirective *D) { |
| 2796 | VisitOMPLoopDirective(D); |
| 2797 | } |
| 2798 | |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2799 | void EnqueueVisitor::VisitOMPDistributeParallelForSimdDirective( |
| 2800 | const OMPDistributeParallelForSimdDirective *D) { |
| 2801 | VisitOMPLoopDirective(D); |
| 2802 | } |
| 2803 | |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2804 | void EnqueueVisitor::VisitOMPDistributeSimdDirective( |
| 2805 | const OMPDistributeSimdDirective *D) { |
| 2806 | VisitOMPLoopDirective(D); |
| 2807 | } |
| 2808 | |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 2809 | void EnqueueVisitor::VisitOMPTargetParallelForSimdDirective( |
| 2810 | const OMPTargetParallelForSimdDirective *D) { |
| 2811 | VisitOMPLoopDirective(D); |
| 2812 | } |
| 2813 | |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2814 | void EnqueueVisitor::VisitOMPTargetSimdDirective( |
| 2815 | const OMPTargetSimdDirective *D) { |
| 2816 | VisitOMPLoopDirective(D); |
| 2817 | } |
| 2818 | |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 2819 | void EnqueueVisitor::VisitOMPTeamsDistributeDirective( |
| 2820 | const OMPTeamsDistributeDirective *D) { |
| 2821 | VisitOMPLoopDirective(D); |
| 2822 | } |
| 2823 | |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 2824 | void EnqueueVisitor::VisitOMPTeamsDistributeSimdDirective( |
| 2825 | const OMPTeamsDistributeSimdDirective *D) { |
| 2826 | VisitOMPLoopDirective(D); |
| 2827 | } |
| 2828 | |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 2829 | void EnqueueVisitor::VisitOMPTeamsDistributeParallelForSimdDirective( |
| 2830 | const OMPTeamsDistributeParallelForSimdDirective *D) { |
| 2831 | VisitOMPLoopDirective(D); |
| 2832 | } |
| 2833 | |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 2834 | void EnqueueVisitor::VisitOMPTeamsDistributeParallelForDirective( |
| 2835 | const OMPTeamsDistributeParallelForDirective *D) { |
| 2836 | VisitOMPLoopDirective(D); |
| 2837 | } |
| 2838 | |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 2839 | void EnqueueVisitor::VisitOMPTargetTeamsDirective( |
| 2840 | const OMPTargetTeamsDirective *D) { |
| 2841 | VisitOMPExecutableDirective(D); |
| 2842 | } |
| 2843 | |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 2844 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeDirective( |
| 2845 | const OMPTargetTeamsDistributeDirective *D) { |
| 2846 | VisitOMPLoopDirective(D); |
| 2847 | } |
| 2848 | |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 2849 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeParallelForDirective( |
| 2850 | const OMPTargetTeamsDistributeParallelForDirective *D) { |
| 2851 | VisitOMPLoopDirective(D); |
| 2852 | } |
| 2853 | |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 2854 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeParallelForSimdDirective( |
| 2855 | const OMPTargetTeamsDistributeParallelForSimdDirective *D) { |
| 2856 | VisitOMPLoopDirective(D); |
| 2857 | } |
| 2858 | |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 2859 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeSimdDirective( |
| 2860 | const OMPTargetTeamsDistributeSimdDirective *D) { |
| 2861 | VisitOMPLoopDirective(D); |
| 2862 | } |
| 2863 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2864 | void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2865 | EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S); |
| 2866 | } |
| 2867 | |
| 2868 | bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { |
| 2869 | if (RegionOfInterest.isValid()) { |
| 2870 | SourceRange Range = getRawCursorExtent(C); |
| 2871 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 2872 | return false; |
| 2873 | } |
| 2874 | return true; |
| 2875 | } |
| 2876 | |
| 2877 | bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { |
| 2878 | while (!WL.empty()) { |
| 2879 | // Dequeue the worklist item. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2880 | VisitorJob LI = WL.pop_back_val(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2881 | |
| 2882 | // Set the Parent field, then back to its old value once we're done. |
| 2883 | SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); |
| 2884 | |
| 2885 | switch (LI.getKind()) { |
| 2886 | case VisitorJob::DeclVisitKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2887 | const Decl *D = cast<DeclVisit>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2888 | if (!D) |
| 2889 | continue; |
| 2890 | |
| 2891 | // For now, perform default visitation for Decls. |
| 2892 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest, |
| 2893 | cast<DeclVisit>(&LI)->isFirst()))) |
| 2894 | return true; |
| 2895 | |
| 2896 | continue; |
| 2897 | } |
| 2898 | case VisitorJob::ExplicitTemplateArgsVisitKind: { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2899 | for (const TemplateArgumentLoc &Arg : |
| 2900 | *cast<ExplicitTemplateArgsVisit>(&LI)) { |
| 2901 | if (VisitTemplateArgumentLoc(Arg)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2902 | return true; |
| 2903 | } |
| 2904 | continue; |
| 2905 | } |
| 2906 | case VisitorJob::TypeLocVisitKind: { |
| 2907 | // Perform default visitation for TypeLocs. |
| 2908 | if (Visit(cast<TypeLocVisit>(&LI)->get())) |
| 2909 | return true; |
| 2910 | continue; |
| 2911 | } |
| 2912 | case VisitorJob::LabelRefVisitKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2913 | const LabelDecl *LS = cast<LabelRefVisit>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2914 | if (LabelStmt *stmt = LS->getStmt()) { |
| 2915 | if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(), |
| 2916 | TU))) { |
| 2917 | return true; |
| 2918 | } |
| 2919 | } |
| 2920 | continue; |
| 2921 | } |
| 2922 | |
| 2923 | case VisitorJob::NestedNameSpecifierLocVisitKind: { |
| 2924 | NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI); |
| 2925 | if (VisitNestedNameSpecifierLoc(V->get())) |
| 2926 | return true; |
| 2927 | continue; |
| 2928 | } |
| 2929 | |
| 2930 | case VisitorJob::DeclarationNameInfoVisitKind: { |
| 2931 | if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI) |
| 2932 | ->get())) |
| 2933 | return true; |
| 2934 | continue; |
| 2935 | } |
| 2936 | case VisitorJob::MemberRefVisitKind: { |
| 2937 | MemberRefVisit *V = cast<MemberRefVisit>(&LI); |
| 2938 | if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU))) |
| 2939 | return true; |
| 2940 | continue; |
| 2941 | } |
| 2942 | case VisitorJob::StmtVisitKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2943 | const Stmt *S = cast<StmtVisit>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2944 | if (!S) |
| 2945 | continue; |
| 2946 | |
| 2947 | // Update the current cursor. |
| 2948 | CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest); |
| 2949 | if (!IsInRegionOfInterest(Cursor)) |
| 2950 | continue; |
| 2951 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 2952 | case CXChildVisit_Break: return true; |
| 2953 | case CXChildVisit_Continue: break; |
| 2954 | case CXChildVisit_Recurse: |
| 2955 | if (PostChildrenVisitor) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 2956 | WL.push_back(PostChildrenVisit(nullptr, Cursor)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2957 | EnqueueWorkList(WL, S); |
| 2958 | break; |
| 2959 | } |
| 2960 | continue; |
| 2961 | } |
| 2962 | case VisitorJob::MemberExprPartsKind: { |
| 2963 | // Handle the other pieces in the MemberExpr besides the base. |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2964 | const MemberExpr *M = cast<MemberExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2965 | |
| 2966 | // Visit the nested-name-specifier |
| 2967 | if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc()) |
| 2968 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 2969 | return true; |
| 2970 | |
| 2971 | // Visit the declaration name. |
| 2972 | if (VisitDeclarationNameInfo(M->getMemberNameInfo())) |
| 2973 | return true; |
| 2974 | |
| 2975 | // Visit the explicitly-specified template arguments, if any. |
| 2976 | if (M->hasExplicitTemplateArgs()) { |
| 2977 | for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), |
| 2978 | *ArgEnd = Arg + M->getNumTemplateArgs(); |
| 2979 | Arg != ArgEnd; ++Arg) { |
| 2980 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2981 | return true; |
| 2982 | } |
| 2983 | } |
| 2984 | continue; |
| 2985 | } |
| 2986 | case VisitorJob::DeclRefExprPartsKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2987 | const DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2988 | // Visit nested-name-specifier, if present. |
| 2989 | if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc()) |
| 2990 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 2991 | return true; |
| 2992 | // Visit declaration name. |
| 2993 | if (VisitDeclarationNameInfo(DR->getNameInfo())) |
| 2994 | return true; |
| 2995 | continue; |
| 2996 | } |
| 2997 | case VisitorJob::OverloadExprPartsKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2998 | const OverloadExpr *O = cast<OverloadExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2999 | // Visit the nested-name-specifier. |
| 3000 | if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc()) |
| 3001 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 3002 | return true; |
| 3003 | // Visit the declaration name. |
| 3004 | if (VisitDeclarationNameInfo(O->getNameInfo())) |
| 3005 | return true; |
| 3006 | // Visit the overloaded declaration reference. |
| 3007 | if (Visit(MakeCursorOverloadedDeclRef(O, TU))) |
| 3008 | return true; |
| 3009 | continue; |
| 3010 | } |
| 3011 | case VisitorJob::SizeOfPackExprPartsKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3012 | const SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3013 | NamedDecl *Pack = E->getPack(); |
| 3014 | if (isa<TemplateTypeParmDecl>(Pack)) { |
| 3015 | if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack), |
| 3016 | E->getPackLoc(), TU))) |
| 3017 | return true; |
| 3018 | |
| 3019 | continue; |
| 3020 | } |
| 3021 | |
| 3022 | if (isa<TemplateTemplateParmDecl>(Pack)) { |
| 3023 | if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack), |
| 3024 | E->getPackLoc(), TU))) |
| 3025 | return true; |
| 3026 | |
| 3027 | continue; |
| 3028 | } |
| 3029 | |
| 3030 | // Non-type template parameter packs and function parameter packs are |
| 3031 | // treated like DeclRefExpr cursors. |
| 3032 | continue; |
| 3033 | } |
| 3034 | |
| 3035 | case VisitorJob::LambdaExprPartsKind: { |
| 3036 | // Visit captures. |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3037 | const LambdaExpr *E = cast<LambdaExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3038 | for (LambdaExpr::capture_iterator C = E->explicit_capture_begin(), |
| 3039 | CEnd = E->explicit_capture_end(); |
| 3040 | C != CEnd; ++C) { |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 3041 | // FIXME: Lambda init-captures. |
| 3042 | if (!C->capturesVariable()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3043 | continue; |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 3044 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3045 | if (Visit(MakeCursorVariableRef(C->getCapturedVar(), |
| 3046 | C->getLocation(), |
| 3047 | TU))) |
| 3048 | return true; |
| 3049 | } |
| 3050 | |
| 3051 | // Visit parameters and return type, if present. |
| 3052 | if (E->hasExplicitParameters() || E->hasExplicitResultType()) { |
| 3053 | TypeLoc TL = E->getCallOperator()->getTypeSourceInfo()->getTypeLoc(); |
| 3054 | if (E->hasExplicitParameters() && E->hasExplicitResultType()) { |
| 3055 | // Visit the whole type. |
| 3056 | if (Visit(TL)) |
| 3057 | return true; |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3058 | } else if (FunctionProtoTypeLoc Proto = |
| 3059 | TL.getAs<FunctionProtoTypeLoc>()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3060 | if (E->hasExplicitParameters()) { |
| 3061 | // Visit parameters. |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3062 | for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) |
| 3063 | if (Visit(MakeCXCursor(Proto.getParam(I), TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3064 | return true; |
| 3065 | } else { |
| 3066 | // Visit result type. |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 3067 | if (Visit(Proto.getReturnLoc())) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3068 | return true; |
| 3069 | } |
| 3070 | } |
| 3071 | } |
| 3072 | break; |
| 3073 | } |
| 3074 | |
| 3075 | case VisitorJob::PostChildrenVisitKind: |
| 3076 | if (PostChildrenVisitor(Parent, ClientData)) |
| 3077 | return true; |
| 3078 | break; |
| 3079 | } |
| 3080 | } |
| 3081 | return false; |
| 3082 | } |
| 3083 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3084 | bool CursorVisitor::Visit(const Stmt *S) { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3085 | VisitorWorkList *WL = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3086 | if (!WorkListFreeList.empty()) { |
| 3087 | WL = WorkListFreeList.back(); |
| 3088 | WL->clear(); |
| 3089 | WorkListFreeList.pop_back(); |
| 3090 | } |
| 3091 | else { |
| 3092 | WL = new VisitorWorkList(); |
| 3093 | WorkListCache.push_back(WL); |
| 3094 | } |
| 3095 | EnqueueWorkList(*WL, S); |
| 3096 | bool result = RunVisitorWorkList(*WL); |
| 3097 | WorkListFreeList.push_back(WL); |
| 3098 | return result; |
| 3099 | } |
| 3100 | |
| 3101 | namespace { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3102 | typedef SmallVector<SourceRange, 4> RefNamePieces; |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3103 | RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr, |
| 3104 | const DeclarationNameInfo &NI, SourceRange QLoc, |
| 3105 | const SourceRange *TemplateArgsLoc = nullptr) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3106 | const bool WantQualifier = NameFlags & CXNameRange_WantQualifier; |
| 3107 | const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs; |
| 3108 | const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece; |
| 3109 | |
| 3110 | const DeclarationName::NameKind Kind = NI.getName().getNameKind(); |
| 3111 | |
| 3112 | RefNamePieces Pieces; |
| 3113 | |
| 3114 | if (WantQualifier && QLoc.isValid()) |
| 3115 | Pieces.push_back(QLoc); |
| 3116 | |
| 3117 | if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr) |
| 3118 | Pieces.push_back(NI.getLoc()); |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3119 | |
| 3120 | if (WantTemplateArgs && TemplateArgsLoc && TemplateArgsLoc->isValid()) |
| 3121 | Pieces.push_back(*TemplateArgsLoc); |
| 3122 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3123 | if (Kind == DeclarationName::CXXOperatorName) { |
| 3124 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 3125 | NI.getInfo().CXXOperatorName.BeginOpNameLoc)); |
| 3126 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 3127 | NI.getInfo().CXXOperatorName.EndOpNameLoc)); |
| 3128 | } |
| 3129 | |
| 3130 | if (WantSinglePiece) { |
| 3131 | SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd()); |
| 3132 | Pieces.clear(); |
| 3133 | Pieces.push_back(R); |
| 3134 | } |
| 3135 | |
| 3136 | return Pieces; |
| 3137 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3138 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3139 | |
| 3140 | //===----------------------------------------------------------------------===// |
| 3141 | // Misc. API hooks. |
| 3142 | //===----------------------------------------------------------------------===// |
| 3143 | |
Chad Rosier | 05c71aa | 2013-03-27 18:28:23 +0000 | [diff] [blame] | 3144 | static void fatal_error_handler(void *user_data, const std::string& reason, |
| 3145 | bool gen_crash_diag) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3146 | // Write the result out to stderr avoiding errs() because raw_ostreams can |
| 3147 | // call report_fatal_error. |
| 3148 | fprintf(stderr, "LIBCLANG FATAL ERROR: %s\n", reason.c_str()); |
| 3149 | ::abort(); |
| 3150 | } |
| 3151 | |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 3152 | namespace { |
| 3153 | struct RegisterFatalErrorHandler { |
| 3154 | RegisterFatalErrorHandler() { |
| 3155 | llvm::install_fatal_error_handler(fatal_error_handler, nullptr); |
| 3156 | } |
| 3157 | }; |
| 3158 | } |
| 3159 | |
| 3160 | static llvm::ManagedStatic<RegisterFatalErrorHandler> RegisterFatalErrorHandlerOnce; |
| 3161 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3162 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 3163 | int displayDiagnostics) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3164 | // We use crash recovery to make some of our APIs more reliable, implicitly |
| 3165 | // enable it. |
Argyrios Kyrtzidis | 3701f54 | 2013-11-27 08:58:09 +0000 | [diff] [blame] | 3166 | if (!getenv("LIBCLANG_DISABLE_CRASH_RECOVERY")) |
| 3167 | llvm::CrashRecoveryContext::Enable(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3168 | |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 3169 | // Look through the managed static to trigger construction of the managed |
| 3170 | // static which registers our fatal error handler. This ensures it is only |
| 3171 | // registered once. |
| 3172 | (void)*RegisterFatalErrorHandlerOnce; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3173 | |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 3174 | // Initialize targets for clang module support. |
| 3175 | llvm::InitializeAllTargets(); |
| 3176 | llvm::InitializeAllTargetMCs(); |
| 3177 | llvm::InitializeAllAsmPrinters(); |
| 3178 | llvm::InitializeAllAsmParsers(); |
| 3179 | |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 3180 | CIndexer *CIdxr = new CIndexer(); |
| 3181 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3182 | if (excludeDeclarationsFromPCH) |
| 3183 | CIdxr->setOnlyLocalDecls(); |
| 3184 | if (displayDiagnostics) |
| 3185 | CIdxr->setDisplayDiagnostics(); |
| 3186 | |
| 3187 | if (getenv("LIBCLANG_BGPRIO_INDEX")) |
| 3188 | CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() | |
| 3189 | CXGlobalOpt_ThreadBackgroundPriorityForIndexing); |
| 3190 | if (getenv("LIBCLANG_BGPRIO_EDIT")) |
| 3191 | CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() | |
| 3192 | CXGlobalOpt_ThreadBackgroundPriorityForEditing); |
| 3193 | |
| 3194 | return CIdxr; |
| 3195 | } |
| 3196 | |
| 3197 | void clang_disposeIndex(CXIndex CIdx) { |
| 3198 | if (CIdx) |
| 3199 | delete static_cast<CIndexer *>(CIdx); |
| 3200 | } |
| 3201 | |
| 3202 | void clang_CXIndex_setGlobalOptions(CXIndex CIdx, unsigned options) { |
| 3203 | if (CIdx) |
| 3204 | static_cast<CIndexer *>(CIdx)->setCXGlobalOptFlags(options); |
| 3205 | } |
| 3206 | |
| 3207 | unsigned clang_CXIndex_getGlobalOptions(CXIndex CIdx) { |
| 3208 | if (CIdx) |
| 3209 | return static_cast<CIndexer *>(CIdx)->getCXGlobalOptFlags(); |
| 3210 | return 0; |
| 3211 | } |
| 3212 | |
| 3213 | void clang_toggleCrashRecovery(unsigned isEnabled) { |
| 3214 | if (isEnabled) |
| 3215 | llvm::CrashRecoveryContext::Enable(); |
| 3216 | else |
| 3217 | llvm::CrashRecoveryContext::Disable(); |
| 3218 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3219 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3220 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
| 3221 | const char *ast_filename) { |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3222 | CXTranslationUnit TU; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3223 | enum CXErrorCode Result = |
| 3224 | clang_createTranslationUnit2(CIdx, ast_filename, &TU); |
Reid Kleckner | fd48fc6 | 2014-02-12 23:56:20 +0000 | [diff] [blame] | 3225 | (void)Result; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3226 | assert((TU && Result == CXError_Success) || |
| 3227 | (!TU && Result != CXError_Success)); |
| 3228 | return TU; |
| 3229 | } |
| 3230 | |
| 3231 | enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx, |
| 3232 | const char *ast_filename, |
| 3233 | CXTranslationUnit *out_TU) { |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3234 | if (out_TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3235 | *out_TU = nullptr; |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3236 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3237 | if (!CIdx || !ast_filename || !out_TU) |
| 3238 | return CXError_InvalidArguments; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3239 | |
Argyrios Kyrtzidis | 2702101 | 2013-05-24 22:24:07 +0000 | [diff] [blame] | 3240 | LOG_FUNC_SECTION { |
| 3241 | *Log << ast_filename; |
| 3242 | } |
| 3243 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3244 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 3245 | FileSystemOptions FileSystemOpts; |
| 3246 | |
Justin Bogner | d512c1e | 2014-10-15 00:33:06 +0000 | [diff] [blame] | 3247 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags = |
| 3248 | CompilerInstance::createDiagnostics(new DiagnosticOptions()); |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 3249 | std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile( |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 3250 | ast_filename, CXXIdx->getPCHContainerOperations()->getRawReader(), |
| 3251 | ASTUnit::LoadEverything, Diags, |
Adrian Prantl | 6b21ab2 | 2015-08-27 19:46:20 +0000 | [diff] [blame] | 3252 | FileSystemOpts, /*UseDebugInfo=*/false, |
| 3253 | CXXIdx->getOnlyLocalDecls(), None, |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 3254 | /*CaptureDiagnostics=*/true, |
| 3255 | /*AllowPCHWithCompilerErrors=*/true, |
| 3256 | /*UserFilesAreVolatile=*/true); |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 3257 | *out_TU = MakeCXTranslationUnit(CXXIdx, std::move(AU)); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3258 | return *out_TU ? CXError_Success : CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
| 3261 | unsigned clang_defaultEditingTranslationUnitOptions() { |
| 3262 | return CXTranslationUnit_PrecompiledPreamble | |
| 3263 | CXTranslationUnit_CacheCompletionResults; |
| 3264 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3265 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3266 | CXTranslationUnit |
| 3267 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 3268 | const char *source_filename, |
| 3269 | int num_command_line_args, |
| 3270 | const char * const *command_line_args, |
| 3271 | unsigned num_unsaved_files, |
| 3272 | struct CXUnsavedFile *unsaved_files) { |
| 3273 | unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord; |
| 3274 | return clang_parseTranslationUnit(CIdx, source_filename, |
| 3275 | command_line_args, num_command_line_args, |
| 3276 | unsaved_files, num_unsaved_files, |
| 3277 | Options); |
| 3278 | } |
| 3279 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3280 | static CXErrorCode |
| 3281 | clang_parseTranslationUnit_Impl(CXIndex CIdx, const char *source_filename, |
| 3282 | const char *const *command_line_args, |
| 3283 | int num_command_line_args, |
| 3284 | ArrayRef<CXUnsavedFile> unsaved_files, |
| 3285 | unsigned options, CXTranslationUnit *out_TU) { |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3286 | // Set up the initial return values. |
| 3287 | if (out_TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3288 | *out_TU = nullptr; |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3289 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3290 | // Check arguments. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3291 | if (!CIdx || !out_TU) |
| 3292 | return CXError_InvalidArguments; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3293 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3294 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 3295 | |
| 3296 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
| 3297 | setThreadBackgroundPriority(); |
| 3298 | |
| 3299 | bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3300 | bool CreatePreambleOnFirstParse = |
| 3301 | options & CXTranslationUnit_CreatePreambleOnFirstParse; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3302 | // FIXME: Add a flag for modules. |
| 3303 | TranslationUnitKind TUKind |
Argyrios Kyrtzidis | 735e92c | 2017-06-09 01:20:48 +0000 | [diff] [blame] | 3304 | = (options & (CXTranslationUnit_Incomplete | |
| 3305 | CXTranslationUnit_SingleFileParse))? TU_Prefix : TU_Complete; |
Alp Toker | 8c8a875 | 2013-12-03 06:53:35 +0000 | [diff] [blame] | 3306 | bool CacheCodeCompletionResults |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3307 | = options & CXTranslationUnit_CacheCompletionResults; |
| 3308 | bool IncludeBriefCommentsInCodeCompletion |
| 3309 | = options & CXTranslationUnit_IncludeBriefCommentsInCodeCompletion; |
| 3310 | bool SkipFunctionBodies = options & CXTranslationUnit_SkipFunctionBodies; |
Argyrios Kyrtzidis | 735e92c | 2017-06-09 01:20:48 +0000 | [diff] [blame] | 3311 | bool SingleFileParse = options & CXTranslationUnit_SingleFileParse; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3312 | bool ForSerialization = options & CXTranslationUnit_ForSerialization; |
| 3313 | |
| 3314 | // Configure the diagnostics. |
| 3315 | IntrusiveRefCntPtr<DiagnosticsEngine> |
Sean Silva | f1b49e2 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 3316 | Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3317 | |
Manuel Klimek | 016c024 | 2016-03-01 10:56:19 +0000 | [diff] [blame] | 3318 | if (options & CXTranslationUnit_KeepGoing) |
Richard Smith | e37391c | 2017-05-03 00:28:49 +0000 | [diff] [blame] | 3319 | Diags->setSuppressAfterFatalError(false); |
Manuel Klimek | 016c024 | 2016-03-01 10:56:19 +0000 | [diff] [blame] | 3320 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3321 | // Recover resources if we crash before exiting this function. |
| 3322 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 3323 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 3324 | DiagCleanup(Diags.get()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3325 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3326 | std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles( |
| 3327 | new std::vector<ASTUnit::RemappedFile>()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3328 | |
| 3329 | // Recover resources if we crash before exiting this function. |
| 3330 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 3331 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 3332 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3333 | for (auto &UF : unsaved_files) { |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3334 | std::unique_ptr<llvm::MemoryBuffer> MB = |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3335 | llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3336 | RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3337 | } |
| 3338 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3339 | std::unique_ptr<std::vector<const char *>> Args( |
| 3340 | new std::vector<const char *>()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3341 | |
| 3342 | // Recover resources if we crash before exiting this method. |
| 3343 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > |
| 3344 | ArgsCleanup(Args.get()); |
| 3345 | |
| 3346 | // Since the Clang C library is primarily used by batch tools dealing with |
| 3347 | // (often very broken) source code, where spell-checking can have a |
| 3348 | // significant negative impact on performance (particularly when |
| 3349 | // precompiled headers are involved), we disable it by default. |
| 3350 | // Only do this if we haven't found a spell-checking-related argument. |
| 3351 | bool FoundSpellCheckingArgument = false; |
| 3352 | for (int I = 0; I != num_command_line_args; ++I) { |
| 3353 | if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || |
| 3354 | strcmp(command_line_args[I], "-fspell-checking") == 0) { |
| 3355 | FoundSpellCheckingArgument = true; |
| 3356 | break; |
| 3357 | } |
| 3358 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3359 | Args->insert(Args->end(), command_line_args, |
| 3360 | command_line_args + num_command_line_args); |
| 3361 | |
Benjamin Kramer | c02670e | 2015-11-18 16:14:27 +0000 | [diff] [blame] | 3362 | if (!FoundSpellCheckingArgument) |
| 3363 | Args->insert(Args->begin() + 1, "-fno-spell-checking"); |
| 3364 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3365 | // The 'source_filename' argument is optional. If the caller does not |
| 3366 | // specify it then it is assumed that the source file is specified |
| 3367 | // in the actual argument list. |
| 3368 | // Put the source file after command_line_args otherwise if '-x' flag is |
| 3369 | // present it will be unused. |
| 3370 | if (source_filename) |
| 3371 | Args->push_back(source_filename); |
| 3372 | |
| 3373 | // Do we need the detailed preprocessing record? |
| 3374 | if (options & CXTranslationUnit_DetailedPreprocessingRecord) { |
| 3375 | Args->push_back("-Xclang"); |
| 3376 | Args->push_back("-detailed-preprocessing-record"); |
| 3377 | } |
Alex Lorenz | cb00640 | 2017-04-27 13:47:03 +0000 | [diff] [blame] | 3378 | |
| 3379 | // Suppress any editor placeholder diagnostics. |
| 3380 | Args->push_back("-fallow-editor-placeholders"); |
| 3381 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3382 | unsigned NumErrors = Diags->getClient()->getNumErrors(); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3383 | std::unique_ptr<ASTUnit> ErrUnit; |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3384 | // Unless the user specified that they want the preamble on the first parse |
| 3385 | // set it up to be created on the first reparse. This makes the first parse |
| 3386 | // faster, trading for a slower (first) reparse. |
| 3387 | unsigned PrecompilePreambleAfterNParses = |
| 3388 | !PrecompilePreamble ? 0 : 2 - CreatePreambleOnFirstParse; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3389 | std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCommandLine( |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 3390 | Args->data(), Args->data() + Args->size(), |
| 3391 | CXXIdx->getPCHContainerOperations(), Diags, |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3392 | CXXIdx->getClangResourcesPath(), CXXIdx->getOnlyLocalDecls(), |
| 3393 | /*CaptureDiagnostics=*/true, *RemappedFiles.get(), |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3394 | /*RemappedFilesKeepOriginalName=*/true, PrecompilePreambleAfterNParses, |
| 3395 | TUKind, CacheCodeCompletionResults, IncludeBriefCommentsInCodeCompletion, |
Argyrios Kyrtzidis | 735e92c | 2017-06-09 01:20:48 +0000 | [diff] [blame] | 3396 | /*AllowPCHWithCompilerErrors=*/true, SkipFunctionBodies, SingleFileParse, |
Argyrios Kyrtzidis | a3e2ff1 | 2015-11-20 03:36:21 +0000 | [diff] [blame] | 3397 | /*UserFilesAreVolatile=*/true, ForSerialization, |
| 3398 | CXXIdx->getPCHContainerOperations()->getRawReader().getFormat(), |
| 3399 | &ErrUnit)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3400 | |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3401 | // Early failures in LoadFromCommandLine may return with ErrUnit unset. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3402 | if (!Unit && !ErrUnit) |
| 3403 | return CXError_ASTReadError; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3404 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3405 | if (NumErrors != Diags->getClient()->getNumErrors()) { |
| 3406 | // Make sure to check that 'Unit' is non-NULL. |
| 3407 | if (CXXIdx->getDisplayDiagnostics()) |
| 3408 | printDiagsToStderr(Unit ? Unit.get() : ErrUnit.get()); |
| 3409 | } |
| 3410 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3411 | if (isASTReadError(Unit ? Unit.get() : ErrUnit.get())) |
| 3412 | return CXError_ASTReadError; |
| 3413 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 3414 | *out_TU = MakeCXTranslationUnit(CXXIdx, std::move(Unit)); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3415 | return *out_TU ? CXError_Success : CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3416 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3417 | |
| 3418 | CXTranslationUnit |
| 3419 | clang_parseTranslationUnit(CXIndex CIdx, |
| 3420 | const char *source_filename, |
| 3421 | const char *const *command_line_args, |
| 3422 | int num_command_line_args, |
| 3423 | struct CXUnsavedFile *unsaved_files, |
| 3424 | unsigned num_unsaved_files, |
| 3425 | unsigned options) { |
| 3426 | CXTranslationUnit TU; |
| 3427 | enum CXErrorCode Result = clang_parseTranslationUnit2( |
| 3428 | CIdx, source_filename, command_line_args, num_command_line_args, |
| 3429 | unsaved_files, num_unsaved_files, options, &TU); |
Reid Kleckner | 6eaf05a | 2014-02-13 01:19:59 +0000 | [diff] [blame] | 3430 | (void)Result; |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3431 | assert((TU && Result == CXError_Success) || |
| 3432 | (!TU && Result != CXError_Success)); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3433 | return TU; |
| 3434 | } |
| 3435 | |
| 3436 | enum CXErrorCode clang_parseTranslationUnit2( |
Benjamin Kramer | c02670e | 2015-11-18 16:14:27 +0000 | [diff] [blame] | 3437 | CXIndex CIdx, const char *source_filename, |
| 3438 | const char *const *command_line_args, int num_command_line_args, |
| 3439 | struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files, |
| 3440 | unsigned options, CXTranslationUnit *out_TU) { |
| 3441 | SmallVector<const char *, 4> Args; |
| 3442 | Args.push_back("clang"); |
| 3443 | Args.append(command_line_args, command_line_args + num_command_line_args); |
| 3444 | return clang_parseTranslationUnit2FullArgv( |
| 3445 | CIdx, source_filename, Args.data(), Args.size(), unsaved_files, |
| 3446 | num_unsaved_files, options, out_TU); |
| 3447 | } |
| 3448 | |
| 3449 | enum CXErrorCode clang_parseTranslationUnit2FullArgv( |
| 3450 | CXIndex CIdx, const char *source_filename, |
| 3451 | const char *const *command_line_args, int num_command_line_args, |
| 3452 | struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files, |
| 3453 | unsigned options, CXTranslationUnit *out_TU) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 3454 | LOG_FUNC_SECTION { |
| 3455 | *Log << source_filename << ": "; |
| 3456 | for (int i = 0; i != num_command_line_args; ++i) |
| 3457 | *Log << command_line_args[i] << " "; |
| 3458 | } |
| 3459 | |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3460 | if (num_unsaved_files && !unsaved_files) |
| 3461 | return CXError_InvalidArguments; |
| 3462 | |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 3463 | CXErrorCode result = CXError_Failure; |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3464 | auto ParseTranslationUnitImpl = [=, &result] { |
| 3465 | result = clang_parseTranslationUnit_Impl( |
| 3466 | CIdx, source_filename, command_line_args, num_command_line_args, |
| 3467 | llvm::makeArrayRef(unsaved_files, num_unsaved_files), options, out_TU); |
| 3468 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3469 | llvm::CrashRecoveryContext CRC; |
| 3470 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3471 | if (!RunSafely(CRC, ParseTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3472 | fprintf(stderr, "libclang: crash detected during parsing: {\n"); |
| 3473 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 3474 | fprintf(stderr, " 'command_line_args' : ["); |
| 3475 | for (int i = 0; i != num_command_line_args; ++i) { |
| 3476 | if (i) |
| 3477 | fprintf(stderr, ", "); |
| 3478 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 3479 | } |
| 3480 | fprintf(stderr, "],\n"); |
| 3481 | fprintf(stderr, " 'unsaved_files' : ["); |
| 3482 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 3483 | if (i) |
| 3484 | fprintf(stderr, ", "); |
| 3485 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 3486 | unsaved_files[i].Length); |
| 3487 | } |
| 3488 | fprintf(stderr, "],\n"); |
| 3489 | fprintf(stderr, " 'options' : %d,\n", options); |
| 3490 | fprintf(stderr, "}\n"); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3491 | |
| 3492 | return CXError_Crashed; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3493 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3494 | if (CXTranslationUnit *TU = out_TU) |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3495 | PrintLibclangResourceUsage(*TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3496 | } |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 3497 | |
| 3498 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3499 | } |
| 3500 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3501 | CXString clang_Type_getObjCEncoding(CXType CT) { |
| 3502 | CXTranslationUnit tu = static_cast<CXTranslationUnit>(CT.data[1]); |
| 3503 | ASTContext &Ctx = getASTUnit(tu)->getASTContext(); |
| 3504 | std::string encoding; |
| 3505 | Ctx.getObjCEncodingForType(QualType::getFromOpaquePtr(CT.data[0]), |
| 3506 | encoding); |
| 3507 | |
| 3508 | return cxstring::createDup(encoding); |
| 3509 | } |
| 3510 | |
| 3511 | static const IdentifierInfo *getMacroIdentifier(CXCursor C) { |
| 3512 | if (C.kind == CXCursor_MacroDefinition) { |
| 3513 | if (const MacroDefinitionRecord *MDR = getCursorMacroDefinition(C)) |
| 3514 | return MDR->getName(); |
| 3515 | } else if (C.kind == CXCursor_MacroExpansion) { |
| 3516 | MacroExpansionCursor ME = getCursorMacroExpansion(C); |
| 3517 | return ME.getName(); |
| 3518 | } |
| 3519 | return nullptr; |
| 3520 | } |
| 3521 | |
| 3522 | unsigned clang_Cursor_isMacroFunctionLike(CXCursor C) { |
| 3523 | const IdentifierInfo *II = getMacroIdentifier(C); |
| 3524 | if (!II) { |
| 3525 | return false; |
| 3526 | } |
| 3527 | ASTUnit *ASTU = getCursorASTUnit(C); |
| 3528 | Preprocessor &PP = ASTU->getPreprocessor(); |
| 3529 | if (const MacroInfo *MI = PP.getMacroInfo(II)) |
| 3530 | return MI->isFunctionLike(); |
| 3531 | return false; |
| 3532 | } |
| 3533 | |
| 3534 | unsigned clang_Cursor_isMacroBuiltin(CXCursor C) { |
| 3535 | const IdentifierInfo *II = getMacroIdentifier(C); |
| 3536 | if (!II) { |
| 3537 | return false; |
| 3538 | } |
| 3539 | ASTUnit *ASTU = getCursorASTUnit(C); |
| 3540 | Preprocessor &PP = ASTU->getPreprocessor(); |
| 3541 | if (const MacroInfo *MI = PP.getMacroInfo(II)) |
| 3542 | return MI->isBuiltinMacro(); |
| 3543 | return false; |
| 3544 | } |
| 3545 | |
| 3546 | unsigned clang_Cursor_isFunctionInlined(CXCursor C) { |
| 3547 | const Decl *D = getCursorDecl(C); |
| 3548 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 3549 | if (!FD) { |
| 3550 | return false; |
| 3551 | } |
| 3552 | return FD->isInlined(); |
| 3553 | } |
| 3554 | |
| 3555 | static StringLiteral* getCFSTR_value(CallExpr *callExpr) { |
| 3556 | if (callExpr->getNumArgs() != 1) { |
| 3557 | return nullptr; |
| 3558 | } |
| 3559 | |
| 3560 | StringLiteral *S = nullptr; |
| 3561 | auto *arg = callExpr->getArg(0); |
| 3562 | if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) { |
| 3563 | ImplicitCastExpr *I = static_cast<ImplicitCastExpr *>(arg); |
| 3564 | auto *subExpr = I->getSubExprAsWritten(); |
| 3565 | |
| 3566 | if(subExpr->getStmtClass() != Stmt::StringLiteralClass){ |
| 3567 | return nullptr; |
| 3568 | } |
| 3569 | |
| 3570 | S = static_cast<StringLiteral *>(I->getSubExprAsWritten()); |
| 3571 | } else if (arg->getStmtClass() == Stmt::StringLiteralClass) { |
| 3572 | S = static_cast<StringLiteral *>(callExpr->getArg(0)); |
| 3573 | } else { |
| 3574 | return nullptr; |
| 3575 | } |
| 3576 | return S; |
| 3577 | } |
| 3578 | |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3579 | struct ExprEvalResult { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3580 | CXEvalResultKind EvalType; |
| 3581 | union { |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3582 | unsigned long long unsignedVal; |
| 3583 | long long intVal; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3584 | double floatVal; |
| 3585 | char *stringVal; |
| 3586 | } EvalData; |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3587 | bool IsUnsignedInt; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3588 | ~ExprEvalResult() { |
| 3589 | if (EvalType != CXEval_UnExposed && EvalType != CXEval_Float && |
| 3590 | EvalType != CXEval_Int) { |
| 3591 | delete EvalData.stringVal; |
| 3592 | } |
| 3593 | } |
| 3594 | }; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3595 | |
| 3596 | void clang_EvalResult_dispose(CXEvalResult E) { |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3597 | delete static_cast<ExprEvalResult *>(E); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3598 | } |
| 3599 | |
| 3600 | CXEvalResultKind clang_EvalResult_getKind(CXEvalResult E) { |
| 3601 | if (!E) { |
| 3602 | return CXEval_UnExposed; |
| 3603 | } |
| 3604 | return ((ExprEvalResult *)E)->EvalType; |
| 3605 | } |
| 3606 | |
| 3607 | int clang_EvalResult_getAsInt(CXEvalResult E) { |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3608 | return clang_EvalResult_getAsLongLong(E); |
| 3609 | } |
| 3610 | |
| 3611 | long long clang_EvalResult_getAsLongLong(CXEvalResult E) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3612 | if (!E) { |
| 3613 | return 0; |
| 3614 | } |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3615 | ExprEvalResult *Result = (ExprEvalResult*)E; |
| 3616 | if (Result->IsUnsignedInt) |
| 3617 | return Result->EvalData.unsignedVal; |
| 3618 | return Result->EvalData.intVal; |
| 3619 | } |
| 3620 | |
| 3621 | unsigned clang_EvalResult_isUnsignedInt(CXEvalResult E) { |
| 3622 | return ((ExprEvalResult *)E)->IsUnsignedInt; |
| 3623 | } |
| 3624 | |
| 3625 | unsigned long long clang_EvalResult_getAsUnsigned(CXEvalResult E) { |
| 3626 | if (!E) { |
| 3627 | return 0; |
| 3628 | } |
| 3629 | |
| 3630 | ExprEvalResult *Result = (ExprEvalResult*)E; |
| 3631 | if (Result->IsUnsignedInt) |
| 3632 | return Result->EvalData.unsignedVal; |
| 3633 | return Result->EvalData.intVal; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3634 | } |
| 3635 | |
| 3636 | double clang_EvalResult_getAsDouble(CXEvalResult E) { |
| 3637 | if (!E) { |
| 3638 | return 0; |
| 3639 | } |
| 3640 | return ((ExprEvalResult *)E)->EvalData.floatVal; |
| 3641 | } |
| 3642 | |
| 3643 | const char* clang_EvalResult_getAsStr(CXEvalResult E) { |
| 3644 | if (!E) { |
| 3645 | return nullptr; |
| 3646 | } |
| 3647 | return ((ExprEvalResult *)E)->EvalData.stringVal; |
| 3648 | } |
| 3649 | |
| 3650 | static const ExprEvalResult* evaluateExpr(Expr *expr, CXCursor C) { |
| 3651 | Expr::EvalResult ER; |
| 3652 | ASTContext &ctx = getCursorContext(C); |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3653 | if (!expr) |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3654 | return nullptr; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3655 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3656 | expr = expr->IgnoreParens(); |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3657 | if (!expr->EvaluateAsRValue(ER, ctx)) |
| 3658 | return nullptr; |
| 3659 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3660 | QualType rettype; |
| 3661 | CallExpr *callExpr; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3662 | auto result = llvm::make_unique<ExprEvalResult>(); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3663 | result->EvalType = CXEval_UnExposed; |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3664 | result->IsUnsignedInt = false; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3665 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3666 | if (ER.Val.isInt()) { |
| 3667 | result->EvalType = CXEval_Int; |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3668 | |
| 3669 | auto& val = ER.Val.getInt(); |
| 3670 | if (val.isUnsigned()) { |
| 3671 | result->IsUnsignedInt = true; |
| 3672 | result->EvalData.unsignedVal = val.getZExtValue(); |
| 3673 | } else { |
| 3674 | result->EvalData.intVal = val.getExtValue(); |
| 3675 | } |
| 3676 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3677 | return result.release(); |
| 3678 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3679 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3680 | if (ER.Val.isFloat()) { |
| 3681 | llvm::SmallVector<char, 100> Buffer; |
| 3682 | ER.Val.getFloat().toString(Buffer); |
| 3683 | std::string floatStr(Buffer.data(), Buffer.size()); |
| 3684 | result->EvalType = CXEval_Float; |
| 3685 | bool ignored; |
| 3686 | llvm::APFloat apFloat = ER.Val.getFloat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3687 | apFloat.convert(llvm::APFloat::IEEEdouble(), |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3688 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 3689 | result->EvalData.floatVal = apFloat.convertToDouble(); |
| 3690 | return result.release(); |
| 3691 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3692 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3693 | if (expr->getStmtClass() == Stmt::ImplicitCastExprClass) { |
| 3694 | const ImplicitCastExpr *I = dyn_cast<ImplicitCastExpr>(expr); |
| 3695 | auto *subExpr = I->getSubExprAsWritten(); |
| 3696 | if (subExpr->getStmtClass() == Stmt::StringLiteralClass || |
| 3697 | subExpr->getStmtClass() == Stmt::ObjCStringLiteralClass) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3698 | const StringLiteral *StrE = nullptr; |
| 3699 | const ObjCStringLiteral *ObjCExpr; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3700 | ObjCExpr = dyn_cast<ObjCStringLiteral>(subExpr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3701 | |
| 3702 | if (ObjCExpr) { |
| 3703 | StrE = ObjCExpr->getString(); |
| 3704 | result->EvalType = CXEval_ObjCStrLiteral; |
| 3705 | } else { |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3706 | StrE = cast<StringLiteral>(I->getSubExprAsWritten()); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3707 | result->EvalType = CXEval_StrLiteral; |
| 3708 | } |
| 3709 | |
| 3710 | std::string strRef(StrE->getString().str()); |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3711 | result->EvalData.stringVal = new char[strRef.size() + 1]; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3712 | strncpy((char *)result->EvalData.stringVal, strRef.c_str(), |
| 3713 | strRef.size()); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3714 | result->EvalData.stringVal[strRef.size()] = '\0'; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3715 | return result.release(); |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3716 | } |
| 3717 | } else if (expr->getStmtClass() == Stmt::ObjCStringLiteralClass || |
| 3718 | expr->getStmtClass() == Stmt::StringLiteralClass) { |
| 3719 | const StringLiteral *StrE = nullptr; |
| 3720 | const ObjCStringLiteral *ObjCExpr; |
| 3721 | ObjCExpr = dyn_cast<ObjCStringLiteral>(expr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3722 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3723 | if (ObjCExpr) { |
| 3724 | StrE = ObjCExpr->getString(); |
| 3725 | result->EvalType = CXEval_ObjCStrLiteral; |
| 3726 | } else { |
| 3727 | StrE = cast<StringLiteral>(expr); |
| 3728 | result->EvalType = CXEval_StrLiteral; |
| 3729 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3730 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3731 | std::string strRef(StrE->getString().str()); |
| 3732 | result->EvalData.stringVal = new char[strRef.size() + 1]; |
| 3733 | strncpy((char *)result->EvalData.stringVal, strRef.c_str(), strRef.size()); |
| 3734 | result->EvalData.stringVal[strRef.size()] = '\0'; |
| 3735 | return result.release(); |
| 3736 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3737 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3738 | if (expr->getStmtClass() == Stmt::CStyleCastExprClass) { |
| 3739 | CStyleCastExpr *CC = static_cast<CStyleCastExpr *>(expr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3740 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3741 | rettype = CC->getType(); |
| 3742 | if (rettype.getAsString() == "CFStringRef" && |
| 3743 | CC->getSubExpr()->getStmtClass() == Stmt::CallExprClass) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3744 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3745 | callExpr = static_cast<CallExpr *>(CC->getSubExpr()); |
| 3746 | StringLiteral *S = getCFSTR_value(callExpr); |
| 3747 | if (S) { |
| 3748 | std::string strLiteral(S->getString().str()); |
| 3749 | result->EvalType = CXEval_CFStr; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3750 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3751 | result->EvalData.stringVal = new char[strLiteral.size() + 1]; |
| 3752 | strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(), |
| 3753 | strLiteral.size()); |
| 3754 | result->EvalData.stringVal[strLiteral.size()] = '\0'; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3755 | return result.release(); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3756 | } |
| 3757 | } |
| 3758 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3759 | } else if (expr->getStmtClass() == Stmt::CallExprClass) { |
| 3760 | callExpr = static_cast<CallExpr *>(expr); |
| 3761 | rettype = callExpr->getCallReturnType(ctx); |
| 3762 | |
| 3763 | if (rettype->isVectorType() || callExpr->getNumArgs() > 1) |
| 3764 | return nullptr; |
| 3765 | |
| 3766 | if (rettype->isIntegralType(ctx) || rettype->isRealFloatingType()) { |
| 3767 | if (callExpr->getNumArgs() == 1 && |
| 3768 | !callExpr->getArg(0)->getType()->isIntegralType(ctx)) |
| 3769 | return nullptr; |
| 3770 | } else if (rettype.getAsString() == "CFStringRef") { |
| 3771 | |
| 3772 | StringLiteral *S = getCFSTR_value(callExpr); |
| 3773 | if (S) { |
| 3774 | std::string strLiteral(S->getString().str()); |
| 3775 | result->EvalType = CXEval_CFStr; |
| 3776 | result->EvalData.stringVal = new char[strLiteral.size() + 1]; |
| 3777 | strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(), |
| 3778 | strLiteral.size()); |
| 3779 | result->EvalData.stringVal[strLiteral.size()] = '\0'; |
| 3780 | return result.release(); |
| 3781 | } |
| 3782 | } |
| 3783 | } else if (expr->getStmtClass() == Stmt::DeclRefExprClass) { |
| 3784 | DeclRefExpr *D = static_cast<DeclRefExpr *>(expr); |
| 3785 | ValueDecl *V = D->getDecl(); |
| 3786 | if (V->getKind() == Decl::Function) { |
| 3787 | std::string strName = V->getNameAsString(); |
| 3788 | result->EvalType = CXEval_Other; |
| 3789 | result->EvalData.stringVal = new char[strName.size() + 1]; |
| 3790 | strncpy(result->EvalData.stringVal, strName.c_str(), strName.size()); |
| 3791 | result->EvalData.stringVal[strName.size()] = '\0'; |
| 3792 | return result.release(); |
| 3793 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3794 | } |
| 3795 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3796 | return nullptr; |
| 3797 | } |
| 3798 | |
| 3799 | CXEvalResult clang_Cursor_Evaluate(CXCursor C) { |
| 3800 | const Decl *D = getCursorDecl(C); |
| 3801 | if (D) { |
| 3802 | const Expr *expr = nullptr; |
| 3803 | if (auto *Var = dyn_cast<VarDecl>(D)) { |
| 3804 | expr = Var->getInit(); |
| 3805 | } else if (auto *Field = dyn_cast<FieldDecl>(D)) { |
| 3806 | expr = Field->getInClassInitializer(); |
| 3807 | } |
| 3808 | if (expr) |
Aaron Ballman | 01dc157 | 2016-01-20 15:25:30 +0000 | [diff] [blame] | 3809 | return const_cast<CXEvalResult>(reinterpret_cast<const void *>( |
| 3810 | evaluateExpr(const_cast<Expr *>(expr), C))); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3811 | return nullptr; |
| 3812 | } |
| 3813 | |
| 3814 | const CompoundStmt *compoundStmt = dyn_cast_or_null<CompoundStmt>(getCursorStmt(C)); |
| 3815 | if (compoundStmt) { |
| 3816 | Expr *expr = nullptr; |
| 3817 | for (auto *bodyIterator : compoundStmt->body()) { |
| 3818 | if ((expr = dyn_cast<Expr>(bodyIterator))) { |
| 3819 | break; |
| 3820 | } |
| 3821 | } |
| 3822 | if (expr) |
Aaron Ballman | 01dc157 | 2016-01-20 15:25:30 +0000 | [diff] [blame] | 3823 | return const_cast<CXEvalResult>( |
| 3824 | reinterpret_cast<const void *>(evaluateExpr(expr, C))); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3825 | } |
| 3826 | return nullptr; |
| 3827 | } |
| 3828 | |
| 3829 | unsigned clang_Cursor_hasAttrs(CXCursor C) { |
| 3830 | const Decl *D = getCursorDecl(C); |
| 3831 | if (!D) { |
| 3832 | return 0; |
| 3833 | } |
| 3834 | |
| 3835 | if (D->hasAttrs()) { |
| 3836 | return 1; |
| 3837 | } |
| 3838 | |
| 3839 | return 0; |
| 3840 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3841 | unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { |
| 3842 | return CXSaveTranslationUnit_None; |
| 3843 | } |
| 3844 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3845 | static CXSaveError clang_saveTranslationUnit_Impl(CXTranslationUnit TU, |
| 3846 | const char *FileName, |
| 3847 | unsigned options) { |
| 3848 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3849 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
| 3850 | setThreadBackgroundPriority(); |
| 3851 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3852 | bool hadError = cxtu::getASTUnit(TU)->Save(FileName); |
| 3853 | return hadError ? CXSaveError_Unknown : CXSaveError_None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
| 3856 | int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, |
| 3857 | unsigned options) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 3858 | LOG_FUNC_SECTION { |
| 3859 | *Log << TU << ' ' << FileName; |
| 3860 | } |
| 3861 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 3862 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3863 | LOG_BAD_TU(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3864 | return CXSaveError_InvalidTU; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3865 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3866 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3867 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3868 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 3869 | if (!CXXUnit->hasSema()) |
| 3870 | return CXSaveError_InvalidTU; |
| 3871 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3872 | CXSaveError result; |
| 3873 | auto SaveTranslationUnitImpl = [=, &result]() { |
| 3874 | result = clang_saveTranslationUnit_Impl(TU, FileName, options); |
| 3875 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3876 | |
| 3877 | if (!CXXUnit->getDiagnostics().hasUnrecoverableErrorOccurred() || |
| 3878 | getenv("LIBCLANG_NOTHREADS")) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3879 | SaveTranslationUnitImpl(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3880 | |
| 3881 | if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 3882 | PrintLibclangResourceUsage(TU); |
| 3883 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3884 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | // We have an AST that has invalid nodes due to compiler errors. |
| 3888 | // Use a crash recovery thread for protection. |
| 3889 | |
| 3890 | llvm::CrashRecoveryContext CRC; |
| 3891 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3892 | if (!RunSafely(CRC, SaveTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3893 | fprintf(stderr, "libclang: crash detected during AST saving: {\n"); |
| 3894 | fprintf(stderr, " 'filename' : '%s'\n", FileName); |
| 3895 | fprintf(stderr, " 'options' : %d,\n", options); |
| 3896 | fprintf(stderr, "}\n"); |
| 3897 | |
| 3898 | return CXSaveError_Unknown; |
| 3899 | |
| 3900 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
| 3901 | PrintLibclangResourceUsage(TU); |
| 3902 | } |
| 3903 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3904 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3905 | } |
| 3906 | |
| 3907 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
| 3908 | if (CTUnit) { |
| 3909 | // If the translation unit has been marked as unsafe to free, just discard |
| 3910 | // it. |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3911 | ASTUnit *Unit = cxtu::getASTUnit(CTUnit); |
| 3912 | if (Unit && Unit->isUnsafeToFree()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3913 | return; |
| 3914 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3915 | delete cxtu::getASTUnit(CTUnit); |
Dmitri Gribenko | b95b3f1 | 2013-01-26 22:44:19 +0000 | [diff] [blame] | 3916 | delete CTUnit->StringPool; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3917 | delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics); |
| 3918 | disposeOverridenCXCursorsPool(CTUnit->OverridenCursorsPool); |
Dmitri Gribenko | 9e60511 | 2013-11-13 22:16:51 +0000 | [diff] [blame] | 3919 | delete CTUnit->CommentToXML; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3920 | delete CTUnit; |
| 3921 | } |
| 3922 | } |
| 3923 | |
Erik Verbruggen | 346066b | 2017-05-30 14:25:54 +0000 | [diff] [blame] | 3924 | unsigned clang_suspendTranslationUnit(CXTranslationUnit CTUnit) { |
| 3925 | if (CTUnit) { |
| 3926 | ASTUnit *Unit = cxtu::getASTUnit(CTUnit); |
| 3927 | |
| 3928 | if (Unit && Unit->isUnsafeToFree()) |
| 3929 | return false; |
| 3930 | |
| 3931 | Unit->ResetForParse(); |
| 3932 | return true; |
| 3933 | } |
| 3934 | |
| 3935 | return false; |
| 3936 | } |
| 3937 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3938 | unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { |
| 3939 | return CXReparse_None; |
| 3940 | } |
| 3941 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3942 | static CXErrorCode |
| 3943 | clang_reparseTranslationUnit_Impl(CXTranslationUnit TU, |
| 3944 | ArrayRef<CXUnsavedFile> unsaved_files, |
| 3945 | unsigned options) { |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3946 | // Check arguments. |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 3947 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3948 | LOG_BAD_TU(TU); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3949 | return CXError_InvalidArguments; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3950 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3951 | |
| 3952 | // Reset the associated diagnostics. |
| 3953 | delete static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3954 | TU->Diagnostics = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3955 | |
Dmitri Gribenko | 183436e | 2013-01-26 21:49:50 +0000 | [diff] [blame] | 3956 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3957 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) |
| 3958 | setThreadBackgroundPriority(); |
| 3959 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3960 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3961 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3962 | |
| 3963 | std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles( |
| 3964 | new std::vector<ASTUnit::RemappedFile>()); |
| 3965 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3966 | // Recover resources if we crash before exiting this function. |
| 3967 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 3968 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3969 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3970 | for (auto &UF : unsaved_files) { |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3971 | std::unique_ptr<llvm::MemoryBuffer> MB = |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3972 | llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3973 | RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3974 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3975 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 3976 | if (!CXXUnit->Reparse(CXXIdx->getPCHContainerOperations(), |
| 3977 | *RemappedFiles.get())) |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3978 | return CXError_Success; |
| 3979 | if (isASTReadError(CXXUnit)) |
| 3980 | return CXError_ASTReadError; |
| 3981 | return CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | int clang_reparseTranslationUnit(CXTranslationUnit TU, |
| 3985 | unsigned num_unsaved_files, |
| 3986 | struct CXUnsavedFile *unsaved_files, |
| 3987 | unsigned options) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 3988 | LOG_FUNC_SECTION { |
| 3989 | *Log << TU; |
| 3990 | } |
| 3991 | |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3992 | if (num_unsaved_files && !unsaved_files) |
| 3993 | return CXError_InvalidArguments; |
| 3994 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3995 | CXErrorCode result; |
| 3996 | auto ReparseTranslationUnitImpl = [=, &result]() { |
| 3997 | result = clang_reparseTranslationUnit_Impl( |
| 3998 | TU, llvm::makeArrayRef(unsaved_files, num_unsaved_files), options); |
| 3999 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4000 | |
| 4001 | if (getenv("LIBCLANG_NOTHREADS")) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4002 | ReparseTranslationUnitImpl(); |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 4003 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4004 | } |
| 4005 | |
| 4006 | llvm::CrashRecoveryContext CRC; |
| 4007 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4008 | if (!RunSafely(CRC, ReparseTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4009 | fprintf(stderr, "libclang: crash detected during reparsing\n"); |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4010 | cxtu::getASTUnit(TU)->setUnsafeToFree(true); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 4011 | return CXError_Crashed; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4012 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 4013 | PrintLibclangResourceUsage(TU); |
| 4014 | |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 4015 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4016 | } |
| 4017 | |
| 4018 | |
| 4019 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4020 | if (isNotUsableTU(CTUnit)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4021 | LOG_BAD_TU(CTUnit); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4022 | return cxstring::createEmpty(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4023 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4024 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4025 | ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4026 | return cxstring::createDup(CXXUnit->getOriginalSourceFileName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4027 | } |
| 4028 | |
| 4029 | CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4030 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4031 | LOG_BAD_TU(TU); |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 4032 | return clang_getNullCursor(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4033 | } |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 4034 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4035 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4036 | return MakeCXCursor(CXXUnit->getASTContext().getTranslationUnitDecl(), TU); |
| 4037 | } |
| 4038 | |
Emilio Cobos Alvarez | 485ad42 | 2017-04-28 15:56:39 +0000 | [diff] [blame] | 4039 | CXTargetInfo clang_getTranslationUnitTargetInfo(CXTranslationUnit CTUnit) { |
| 4040 | if (isNotUsableTU(CTUnit)) { |
| 4041 | LOG_BAD_TU(CTUnit); |
| 4042 | return nullptr; |
| 4043 | } |
| 4044 | |
| 4045 | CXTargetInfoImpl* impl = new CXTargetInfoImpl(); |
| 4046 | impl->TranslationUnit = CTUnit; |
| 4047 | return impl; |
| 4048 | } |
| 4049 | |
| 4050 | CXString clang_TargetInfo_getTriple(CXTargetInfo TargetInfo) { |
| 4051 | if (!TargetInfo) |
| 4052 | return cxstring::createEmpty(); |
| 4053 | |
| 4054 | CXTranslationUnit CTUnit = TargetInfo->TranslationUnit; |
| 4055 | assert(!isNotUsableTU(CTUnit) && |
| 4056 | "Unexpected unusable translation unit in TargetInfo"); |
| 4057 | |
| 4058 | ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); |
| 4059 | std::string Triple = |
| 4060 | CXXUnit->getASTContext().getTargetInfo().getTriple().normalize(); |
| 4061 | return cxstring::createDup(Triple); |
| 4062 | } |
| 4063 | |
| 4064 | int clang_TargetInfo_getPointerWidth(CXTargetInfo TargetInfo) { |
| 4065 | if (!TargetInfo) |
| 4066 | return -1; |
| 4067 | |
| 4068 | CXTranslationUnit CTUnit = TargetInfo->TranslationUnit; |
| 4069 | assert(!isNotUsableTU(CTUnit) && |
| 4070 | "Unexpected unusable translation unit in TargetInfo"); |
| 4071 | |
| 4072 | ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); |
| 4073 | return CXXUnit->getASTContext().getTargetInfo().getMaxPointerWidth(); |
| 4074 | } |
| 4075 | |
| 4076 | void clang_TargetInfo_dispose(CXTargetInfo TargetInfo) { |
| 4077 | if (!TargetInfo) |
| 4078 | return; |
| 4079 | |
| 4080 | delete TargetInfo; |
| 4081 | } |
| 4082 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4083 | //===----------------------------------------------------------------------===// |
| 4084 | // CXFile Operations. |
| 4085 | //===----------------------------------------------------------------------===// |
| 4086 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4087 | CXString clang_getFileName(CXFile SFile) { |
| 4088 | if (!SFile) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 4089 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4090 | |
| 4091 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4092 | return cxstring::createRef(FEnt->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4093 | } |
| 4094 | |
| 4095 | time_t clang_getFileTime(CXFile SFile) { |
| 4096 | if (!SFile) |
| 4097 | return 0; |
| 4098 | |
| 4099 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 4100 | return FEnt->getModificationTime(); |
| 4101 | } |
| 4102 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4103 | CXFile clang_getFile(CXTranslationUnit TU, const char *file_name) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4104 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4105 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 4106 | return nullptr; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4107 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4108 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4109 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4110 | |
| 4111 | FileManager &FMgr = CXXUnit->getFileManager(); |
| 4112 | return const_cast<FileEntry *>(FMgr.getFile(file_name)); |
| 4113 | } |
| 4114 | |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4115 | unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit TU, |
| 4116 | CXFile file) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4117 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4118 | LOG_BAD_TU(TU); |
| 4119 | return 0; |
| 4120 | } |
| 4121 | |
| 4122 | if (!file) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4123 | return 0; |
| 4124 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4125 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4126 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
| 4127 | return CXXUnit->getPreprocessor().getHeaderSearchInfo() |
| 4128 | .isFileMultipleIncludeGuarded(FEnt); |
| 4129 | } |
| 4130 | |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4131 | int clang_getFileUniqueID(CXFile file, CXFileUniqueID *outID) { |
| 4132 | if (!file || !outID) |
| 4133 | return 1; |
| 4134 | |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4135 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 4136 | const llvm::sys::fs::UniqueID &ID = FEnt->getUniqueID(); |
| 4137 | outID->data[0] = ID.getDevice(); |
| 4138 | outID->data[1] = ID.getFile(); |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4139 | outID->data[2] = FEnt->getModificationTime(); |
| 4140 | return 0; |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4141 | } |
| 4142 | |
Argyrios Kyrtzidis | ac3997e | 2014-08-16 00:26:19 +0000 | [diff] [blame] | 4143 | int clang_File_isEqual(CXFile file1, CXFile file2) { |
| 4144 | if (file1 == file2) |
| 4145 | return true; |
| 4146 | |
| 4147 | if (!file1 || !file2) |
| 4148 | return false; |
| 4149 | |
| 4150 | FileEntry *FEnt1 = static_cast<FileEntry *>(file1); |
| 4151 | FileEntry *FEnt2 = static_cast<FileEntry *>(file2); |
| 4152 | return FEnt1->getUniqueID() == FEnt2->getUniqueID(); |
| 4153 | } |
| 4154 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4155 | //===----------------------------------------------------------------------===// |
| 4156 | // CXCursor Operations. |
| 4157 | //===----------------------------------------------------------------------===// |
| 4158 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4159 | static const Decl *getDeclFromExpr(const Stmt *E) { |
| 4160 | if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4161 | return getDeclFromExpr(CE->getSubExpr()); |
| 4162 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4163 | if (const DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4164 | return RefExpr->getDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4165 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4166 | return ME->getMemberDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4167 | if (const ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4168 | return RE->getDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4169 | if (const ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4170 | if (PRE->isExplicitProperty()) |
| 4171 | return PRE->getExplicitProperty(); |
| 4172 | // It could be messaging both getter and setter as in: |
| 4173 | // ++myobj.myprop; |
| 4174 | // in which case prefer to associate the setter since it is less obvious |
| 4175 | // from inspecting the source that the setter is going to get called. |
| 4176 | if (PRE->isMessagingSetter()) |
| 4177 | return PRE->getImplicitPropertySetter(); |
| 4178 | return PRE->getImplicitPropertyGetter(); |
| 4179 | } |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4180 | if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4181 | return getDeclFromExpr(POE->getSyntacticForm()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4182 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4183 | if (Expr *Src = OVE->getSourceExpr()) |
| 4184 | return getDeclFromExpr(Src); |
| 4185 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4186 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4187 | return getDeclFromExpr(CE->getCallee()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4188 | if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4189 | if (!CE->isElidable()) |
| 4190 | return CE->getConstructor(); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4191 | if (const CXXInheritedCtorInitExpr *CE = |
| 4192 | dyn_cast<CXXInheritedCtorInitExpr>(E)) |
| 4193 | return CE->getConstructor(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4194 | if (const ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4195 | return OME->getMethodDecl(); |
| 4196 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4197 | if (const ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4198 | return PE->getProtocol(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4199 | if (const SubstNonTypeTemplateParmPackExpr *NTTP |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4200 | = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E)) |
| 4201 | return NTTP->getParameterPack(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4202 | if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4203 | if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || |
| 4204 | isa<ParmVarDecl>(SizeOfPack->getPack())) |
| 4205 | return SizeOfPack->getPack(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 4206 | |
| 4207 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4208 | } |
| 4209 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4210 | static SourceLocation getLocationFromExpr(const Expr *E) { |
| 4211 | if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4212 | return getLocationFromExpr(CE->getSubExpr()); |
| 4213 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4214 | if (const ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4215 | return /*FIXME:*/Msg->getLeftLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4216 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4217 | return DRE->getLocation(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4218 | if (const MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4219 | return Member->getMemberLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4220 | if (const ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4221 | return Ivar->getLocation(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4222 | if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4223 | return SizeOfPack->getPackLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4224 | if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4225 | return PropRef->getLocation(); |
| 4226 | |
| 4227 | return E->getLocStart(); |
| 4228 | } |
| 4229 | |
NAKAMURA Takumi | a01f4c3 | 2016-12-19 16:50:43 +0000 | [diff] [blame] | 4230 | extern "C" { |
| 4231 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4232 | unsigned clang_visitChildren(CXCursor parent, |
| 4233 | CXCursorVisitor visitor, |
| 4234 | CXClientData client_data) { |
| 4235 | CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, |
| 4236 | /*VisitPreprocessorLast=*/false); |
| 4237 | return CursorVis.VisitChildren(parent); |
| 4238 | } |
| 4239 | |
| 4240 | #ifndef __has_feature |
| 4241 | #define __has_feature(x) 0 |
| 4242 | #endif |
| 4243 | #if __has_feature(blocks) |
| 4244 | typedef enum CXChildVisitResult |
| 4245 | (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent); |
| 4246 | |
| 4247 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 4248 | CXClientData client_data) { |
| 4249 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 4250 | return block(cursor, parent); |
| 4251 | } |
| 4252 | #else |
| 4253 | // If we are compiled with a compiler that doesn't have native blocks support, |
| 4254 | // define and call the block manually, so the |
| 4255 | typedef struct _CXChildVisitResult |
| 4256 | { |
| 4257 | void *isa; |
| 4258 | int flags; |
| 4259 | int reserved; |
| 4260 | enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, |
| 4261 | CXCursor); |
| 4262 | } *CXCursorVisitorBlock; |
| 4263 | |
| 4264 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 4265 | CXClientData client_data) { |
| 4266 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 4267 | return block->invoke(block, cursor, parent); |
| 4268 | } |
| 4269 | #endif |
| 4270 | |
| 4271 | |
| 4272 | unsigned clang_visitChildrenWithBlock(CXCursor parent, |
| 4273 | CXCursorVisitorBlock block) { |
| 4274 | return clang_visitChildren(parent, visitWithBlock, block); |
| 4275 | } |
| 4276 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4277 | static CXString getDeclSpelling(const Decl *D) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4278 | if (!D) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4279 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4280 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4281 | const NamedDecl *ND = dyn_cast<NamedDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4282 | if (!ND) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4283 | if (const ObjCPropertyImplDecl *PropImpl = |
| 4284 | dyn_cast<ObjCPropertyImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4285 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4286 | return cxstring::createDup(Property->getIdentifier()->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4287 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4288 | if (const ImportDecl *ImportD = dyn_cast<ImportDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4289 | if (Module *Mod = ImportD->getImportedModule()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4290 | return cxstring::createDup(Mod->getFullModuleName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4291 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4292 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4293 | } |
| 4294 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4295 | if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4296 | return cxstring::createDup(OMD->getSelector().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4297 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4298 | if (const ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4299 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 4300 | // and returns different names. NamedDecl returns the class name and |
| 4301 | // ObjCCategoryImplDecl returns the category name. |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4302 | return cxstring::createRef(CIMP->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4303 | |
| 4304 | if (isa<UsingDirectiveDecl>(D)) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4305 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4306 | |
| 4307 | SmallString<1024> S; |
| 4308 | llvm::raw_svector_ostream os(S); |
| 4309 | ND->printName(os); |
| 4310 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4311 | return cxstring::createDup(os.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4312 | } |
| 4313 | |
| 4314 | CXString clang_getCursorSpelling(CXCursor C) { |
| 4315 | if (clang_isTranslationUnit(C.kind)) |
Dmitri Gribenko | 2c173b4 | 2013-01-11 19:28:44 +0000 | [diff] [blame] | 4316 | return clang_getTranslationUnitSpelling(getCursorTU(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4317 | |
| 4318 | if (clang_isReference(C.kind)) { |
| 4319 | switch (C.kind) { |
| 4320 | case CXCursor_ObjCSuperClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4321 | const ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4322 | return cxstring::createRef(Super->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4323 | } |
| 4324 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4325 | const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4326 | return cxstring::createRef(Class->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4327 | } |
| 4328 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4329 | const ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4330 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4331 | return cxstring::createRef(OID->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4332 | } |
| 4333 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4334 | const CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4335 | return cxstring::createDup(B->getType().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4336 | } |
| 4337 | case CXCursor_TypeRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4338 | const TypeDecl *Type = getCursorTypeRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4339 | assert(Type && "Missing type decl"); |
| 4340 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4341 | return cxstring::createDup(getCursorContext(C).getTypeDeclType(Type). |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4342 | getAsString()); |
| 4343 | } |
| 4344 | case CXCursor_TemplateRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4345 | const TemplateDecl *Template = getCursorTemplateRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4346 | assert(Template && "Missing template decl"); |
| 4347 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4348 | return cxstring::createDup(Template->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4349 | } |
| 4350 | |
| 4351 | case CXCursor_NamespaceRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4352 | const NamedDecl *NS = getCursorNamespaceRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4353 | assert(NS && "Missing namespace decl"); |
| 4354 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4355 | return cxstring::createDup(NS->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | case CXCursor_MemberRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4359 | const FieldDecl *Field = getCursorMemberRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4360 | assert(Field && "Missing member decl"); |
| 4361 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4362 | return cxstring::createDup(Field->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4363 | } |
| 4364 | |
| 4365 | case CXCursor_LabelRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4366 | const LabelStmt *Label = getCursorLabelRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4367 | assert(Label && "Missing label"); |
| 4368 | |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4369 | return cxstring::createRef(Label->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | case CXCursor_OverloadedDeclRef: { |
| 4373 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4374 | if (const Decl *D = Storage.dyn_cast<const Decl *>()) { |
| 4375 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4376 | return cxstring::createDup(ND->getNameAsString()); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4377 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4378 | } |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4379 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4380 | return cxstring::createDup(E->getName().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4381 | OverloadedTemplateStorage *Ovl |
| 4382 | = Storage.get<OverloadedTemplateStorage*>(); |
| 4383 | if (Ovl->size() == 0) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4384 | return cxstring::createEmpty(); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4385 | return cxstring::createDup((*Ovl->begin())->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4386 | } |
| 4387 | |
| 4388 | case CXCursor_VariableRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4389 | const VarDecl *Var = getCursorVariableRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4390 | assert(Var && "Missing variable decl"); |
| 4391 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4392 | return cxstring::createDup(Var->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4393 | } |
| 4394 | |
| 4395 | default: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4396 | return cxstring::createRef("<not implemented>"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4397 | } |
| 4398 | } |
| 4399 | |
| 4400 | if (clang_isExpression(C.kind)) { |
Argyrios Kyrtzidis | 3227d86 | 2014-03-03 19:40:52 +0000 | [diff] [blame] | 4401 | const Expr *E = getCursorExpr(C); |
| 4402 | |
| 4403 | if (C.kind == CXCursor_ObjCStringLiteral || |
| 4404 | C.kind == CXCursor_StringLiteral) { |
| 4405 | const StringLiteral *SLit; |
| 4406 | if (const ObjCStringLiteral *OSL = dyn_cast<ObjCStringLiteral>(E)) { |
| 4407 | SLit = OSL->getString(); |
| 4408 | } else { |
| 4409 | SLit = cast<StringLiteral>(E); |
| 4410 | } |
| 4411 | SmallString<256> Buf; |
| 4412 | llvm::raw_svector_ostream OS(Buf); |
| 4413 | SLit->outputString(OS); |
| 4414 | return cxstring::createDup(OS.str()); |
| 4415 | } |
| 4416 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4417 | const Decl *D = getDeclFromExpr(getCursorExpr(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4418 | if (D) |
| 4419 | return getDeclSpelling(D); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4420 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4421 | } |
| 4422 | |
| 4423 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4424 | const Stmt *S = getCursorStmt(C); |
| 4425 | if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4426 | return cxstring::createRef(Label->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4427 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4428 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4429 | } |
| 4430 | |
| 4431 | if (C.kind == CXCursor_MacroExpansion) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4432 | return cxstring::createRef(getCursorMacroExpansion(C).getName() |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4433 | ->getNameStart()); |
| 4434 | |
| 4435 | if (C.kind == CXCursor_MacroDefinition) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4436 | return cxstring::createRef(getCursorMacroDefinition(C)->getName() |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4437 | ->getNameStart()); |
| 4438 | |
| 4439 | if (C.kind == CXCursor_InclusionDirective) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4440 | return cxstring::createDup(getCursorInclusionDirective(C)->getFileName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4441 | |
| 4442 | if (clang_isDeclaration(C.kind)) |
| 4443 | return getDeclSpelling(getCursorDecl(C)); |
| 4444 | |
| 4445 | if (C.kind == CXCursor_AnnotateAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 4446 | const AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C)); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4447 | return cxstring::createDup(AA->getAnnotation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
| 4450 | if (C.kind == CXCursor_AsmLabelAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 4451 | const AsmLabelAttr *AA = cast<AsmLabelAttr>(cxcursor::getCursorAttr(C)); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4452 | return cxstring::createDup(AA->getLabel()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4453 | } |
| 4454 | |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 4455 | if (C.kind == CXCursor_PackedAttr) { |
| 4456 | return cxstring::createRef("packed"); |
| 4457 | } |
| 4458 | |
Saleem Abdulrasool | 79c6971 | 2015-09-05 18:53:43 +0000 | [diff] [blame] | 4459 | if (C.kind == CXCursor_VisibilityAttr) { |
| 4460 | const VisibilityAttr *AA = cast<VisibilityAttr>(cxcursor::getCursorAttr(C)); |
| 4461 | switch (AA->getVisibility()) { |
| 4462 | case VisibilityAttr::VisibilityType::Default: |
| 4463 | return cxstring::createRef("default"); |
| 4464 | case VisibilityAttr::VisibilityType::Hidden: |
| 4465 | return cxstring::createRef("hidden"); |
| 4466 | case VisibilityAttr::VisibilityType::Protected: |
| 4467 | return cxstring::createRef("protected"); |
| 4468 | } |
| 4469 | llvm_unreachable("unknown visibility type"); |
| 4470 | } |
| 4471 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4472 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4473 | } |
| 4474 | |
| 4475 | CXSourceRange clang_Cursor_getSpellingNameRange(CXCursor C, |
| 4476 | unsigned pieceIndex, |
| 4477 | unsigned options) { |
| 4478 | if (clang_Cursor_isNull(C)) |
| 4479 | return clang_getNullRange(); |
| 4480 | |
| 4481 | ASTContext &Ctx = getCursorContext(C); |
| 4482 | |
| 4483 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4484 | const Stmt *S = getCursorStmt(C); |
| 4485 | if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4486 | if (pieceIndex > 0) |
| 4487 | return clang_getNullRange(); |
| 4488 | return cxloc::translateSourceRange(Ctx, Label->getIdentLoc()); |
| 4489 | } |
| 4490 | |
| 4491 | return clang_getNullRange(); |
| 4492 | } |
| 4493 | |
| 4494 | if (C.kind == CXCursor_ObjCMessageExpr) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4495 | if (const ObjCMessageExpr * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4496 | ME = dyn_cast_or_null<ObjCMessageExpr>(getCursorExpr(C))) { |
| 4497 | if (pieceIndex >= ME->getNumSelectorLocs()) |
| 4498 | return clang_getNullRange(); |
| 4499 | return cxloc::translateSourceRange(Ctx, ME->getSelectorLoc(pieceIndex)); |
| 4500 | } |
| 4501 | } |
| 4502 | |
| 4503 | if (C.kind == CXCursor_ObjCInstanceMethodDecl || |
| 4504 | C.kind == CXCursor_ObjCClassMethodDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4505 | if (const ObjCMethodDecl * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4506 | MD = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(C))) { |
| 4507 | if (pieceIndex >= MD->getNumSelectorLocs()) |
| 4508 | return clang_getNullRange(); |
| 4509 | return cxloc::translateSourceRange(Ctx, MD->getSelectorLoc(pieceIndex)); |
| 4510 | } |
| 4511 | } |
| 4512 | |
| 4513 | if (C.kind == CXCursor_ObjCCategoryDecl || |
| 4514 | C.kind == CXCursor_ObjCCategoryImplDecl) { |
| 4515 | if (pieceIndex > 0) |
| 4516 | return clang_getNullRange(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4517 | if (const ObjCCategoryDecl * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4518 | CD = dyn_cast_or_null<ObjCCategoryDecl>(getCursorDecl(C))) |
| 4519 | return cxloc::translateSourceRange(Ctx, CD->getCategoryNameLoc()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4520 | if (const ObjCCategoryImplDecl * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4521 | CID = dyn_cast_or_null<ObjCCategoryImplDecl>(getCursorDecl(C))) |
| 4522 | return cxloc::translateSourceRange(Ctx, CID->getCategoryNameLoc()); |
| 4523 | } |
| 4524 | |
| 4525 | if (C.kind == CXCursor_ModuleImportDecl) { |
| 4526 | if (pieceIndex > 0) |
| 4527 | return clang_getNullRange(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4528 | if (const ImportDecl *ImportD = |
| 4529 | dyn_cast_or_null<ImportDecl>(getCursorDecl(C))) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4530 | ArrayRef<SourceLocation> Locs = ImportD->getIdentifierLocs(); |
| 4531 | if (!Locs.empty()) |
| 4532 | return cxloc::translateSourceRange(Ctx, |
| 4533 | SourceRange(Locs.front(), Locs.back())); |
| 4534 | } |
| 4535 | return clang_getNullRange(); |
| 4536 | } |
| 4537 | |
Argyrios Kyrtzidis | a2a1e53 | 2014-08-26 20:23:26 +0000 | [diff] [blame] | 4538 | if (C.kind == CXCursor_CXXMethod || C.kind == CXCursor_Destructor || |
Kevin Funk | 4be5d67 | 2016-12-20 09:56:56 +0000 | [diff] [blame] | 4539 | C.kind == CXCursor_ConversionFunction || |
| 4540 | C.kind == CXCursor_FunctionDecl) { |
Argyrios Kyrtzidis | a2a1e53 | 2014-08-26 20:23:26 +0000 | [diff] [blame] | 4541 | if (pieceIndex > 0) |
| 4542 | return clang_getNullRange(); |
| 4543 | if (const FunctionDecl *FD = |
| 4544 | dyn_cast_or_null<FunctionDecl>(getCursorDecl(C))) { |
| 4545 | DeclarationNameInfo FunctionName = FD->getNameInfo(); |
| 4546 | return cxloc::translateSourceRange(Ctx, FunctionName.getSourceRange()); |
| 4547 | } |
| 4548 | return clang_getNullRange(); |
| 4549 | } |
| 4550 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4551 | // FIXME: A CXCursor_InclusionDirective should give the location of the |
| 4552 | // filename, but we don't keep track of this. |
| 4553 | |
| 4554 | // FIXME: A CXCursor_AnnotateAttr should give the location of the annotation |
| 4555 | // but we don't keep track of this. |
| 4556 | |
| 4557 | // FIXME: A CXCursor_AsmLabelAttr should give the location of the label |
| 4558 | // but we don't keep track of this. |
| 4559 | |
| 4560 | // Default handling, give the location of the cursor. |
| 4561 | |
| 4562 | if (pieceIndex > 0) |
| 4563 | return clang_getNullRange(); |
| 4564 | |
| 4565 | CXSourceLocation CXLoc = clang_getCursorLocation(C); |
| 4566 | SourceLocation Loc = cxloc::translateSourceLocation(CXLoc); |
| 4567 | return cxloc::translateSourceRange(Ctx, Loc); |
| 4568 | } |
| 4569 | |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4570 | CXString clang_Cursor_getMangling(CXCursor C) { |
| 4571 | if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind)) |
| 4572 | return cxstring::createEmpty(); |
| 4573 | |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4574 | // Mangling only works for functions and variables. |
Eli Bendersky | 7975959 | 2014-08-01 15:01:10 +0000 | [diff] [blame] | 4575 | const Decl *D = getCursorDecl(C); |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4576 | if (!D || !(isa<FunctionDecl>(D) || isa<VarDecl>(D))) |
| 4577 | return cxstring::createEmpty(); |
| 4578 | |
Argyrios Kyrtzidis | ca741ce | 2016-02-14 22:30:14 +0000 | [diff] [blame] | 4579 | ASTContext &Ctx = D->getASTContext(); |
| 4580 | index::CodegenNameGenerator CGNameGen(Ctx); |
| 4581 | return cxstring::createDup(CGNameGen.getName(D)); |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4582 | } |
| 4583 | |
Saleem Abdulrasool | 6003443 | 2015-11-12 03:57:22 +0000 | [diff] [blame] | 4584 | CXStringSet *clang_Cursor_getCXXManglings(CXCursor C) { |
| 4585 | if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind)) |
| 4586 | return nullptr; |
| 4587 | |
| 4588 | const Decl *D = getCursorDecl(C); |
| 4589 | if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D))) |
| 4590 | return nullptr; |
| 4591 | |
Argyrios Kyrtzidis | ca741ce | 2016-02-14 22:30:14 +0000 | [diff] [blame] | 4592 | ASTContext &Ctx = D->getASTContext(); |
| 4593 | index::CodegenNameGenerator CGNameGen(Ctx); |
| 4594 | std::vector<std::string> Manglings = CGNameGen.getAllManglings(D); |
Saleem Abdulrasool | 6003443 | 2015-11-12 03:57:22 +0000 | [diff] [blame] | 4595 | return cxstring::createSet(Manglings); |
| 4596 | } |
| 4597 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4598 | CXString clang_getCursorDisplayName(CXCursor C) { |
| 4599 | if (!clang_isDeclaration(C.kind)) |
| 4600 | return clang_getCursorSpelling(C); |
| 4601 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4602 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4603 | if (!D) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4604 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4605 | |
| 4606 | PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4607 | if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4608 | D = FunTmpl->getTemplatedDecl(); |
| 4609 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4610 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4611 | SmallString<64> Str; |
| 4612 | llvm::raw_svector_ostream OS(Str); |
| 4613 | OS << *Function; |
| 4614 | if (Function->getPrimaryTemplate()) |
| 4615 | OS << "<>"; |
| 4616 | OS << "("; |
| 4617 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { |
| 4618 | if (I) |
| 4619 | OS << ", "; |
| 4620 | OS << Function->getParamDecl(I)->getType().getAsString(Policy); |
| 4621 | } |
| 4622 | |
| 4623 | if (Function->isVariadic()) { |
| 4624 | if (Function->getNumParams()) |
| 4625 | OS << ", "; |
| 4626 | OS << "..."; |
| 4627 | } |
| 4628 | OS << ")"; |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4629 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4632 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4633 | SmallString<64> Str; |
| 4634 | llvm::raw_svector_ostream OS(Str); |
| 4635 | OS << *ClassTemplate; |
| 4636 | OS << "<"; |
| 4637 | TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); |
| 4638 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 4639 | if (I) |
| 4640 | OS << ", "; |
| 4641 | |
| 4642 | NamedDecl *Param = Params->getParam(I); |
| 4643 | if (Param->getIdentifier()) { |
| 4644 | OS << Param->getIdentifier()->getName(); |
| 4645 | continue; |
| 4646 | } |
| 4647 | |
| 4648 | // There is no parameter name, which makes this tricky. Try to come up |
| 4649 | // with something useful that isn't too long. |
| 4650 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 4651 | OS << (TTP->wasDeclaredWithTypename()? "typename" : "class"); |
| 4652 | else if (NonTypeTemplateParmDecl *NTTP |
| 4653 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 4654 | OS << NTTP->getType().getAsString(Policy); |
| 4655 | else |
| 4656 | OS << "template<...> class"; |
| 4657 | } |
| 4658 | |
| 4659 | OS << ">"; |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4660 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4661 | } |
| 4662 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4663 | if (const ClassTemplateSpecializationDecl *ClassSpec |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4664 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 4665 | // If the type was explicitly written, use that. |
| 4666 | if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4667 | return cxstring::createDup(TSInfo->getType().getAsString(Policy)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4668 | |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 4669 | SmallString<128> Str; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4670 | llvm::raw_svector_ostream OS(Str); |
| 4671 | OS << *ClassSpec; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4672 | TemplateSpecializationType::PrintTemplateArgumentList( |
| 4673 | OS, ClassSpec->getTemplateArgs().asArray(), Policy); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4674 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4675 | } |
| 4676 | |
| 4677 | return clang_getCursorSpelling(C); |
| 4678 | } |
| 4679 | |
| 4680 | CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
| 4681 | switch (Kind) { |
| 4682 | case CXCursor_FunctionDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4683 | return cxstring::createRef("FunctionDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4684 | case CXCursor_TypedefDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4685 | return cxstring::createRef("TypedefDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4686 | case CXCursor_EnumDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4687 | return cxstring::createRef("EnumDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4688 | case CXCursor_EnumConstantDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4689 | return cxstring::createRef("EnumConstantDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4690 | case CXCursor_StructDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4691 | return cxstring::createRef("StructDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4692 | case CXCursor_UnionDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4693 | return cxstring::createRef("UnionDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4694 | case CXCursor_ClassDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4695 | return cxstring::createRef("ClassDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4696 | case CXCursor_FieldDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4697 | return cxstring::createRef("FieldDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4698 | case CXCursor_VarDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4699 | return cxstring::createRef("VarDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4700 | case CXCursor_ParmDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4701 | return cxstring::createRef("ParmDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4702 | case CXCursor_ObjCInterfaceDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4703 | return cxstring::createRef("ObjCInterfaceDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4704 | case CXCursor_ObjCCategoryDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4705 | return cxstring::createRef("ObjCCategoryDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4706 | case CXCursor_ObjCProtocolDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4707 | return cxstring::createRef("ObjCProtocolDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4708 | case CXCursor_ObjCPropertyDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4709 | return cxstring::createRef("ObjCPropertyDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4710 | case CXCursor_ObjCIvarDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4711 | return cxstring::createRef("ObjCIvarDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4712 | case CXCursor_ObjCInstanceMethodDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4713 | return cxstring::createRef("ObjCInstanceMethodDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4714 | case CXCursor_ObjCClassMethodDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4715 | return cxstring::createRef("ObjCClassMethodDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4716 | case CXCursor_ObjCImplementationDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4717 | return cxstring::createRef("ObjCImplementationDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4718 | case CXCursor_ObjCCategoryImplDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4719 | return cxstring::createRef("ObjCCategoryImplDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4720 | case CXCursor_CXXMethod: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4721 | return cxstring::createRef("CXXMethod"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4722 | case CXCursor_UnexposedDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4723 | return cxstring::createRef("UnexposedDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4724 | case CXCursor_ObjCSuperClassRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4725 | return cxstring::createRef("ObjCSuperClassRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4726 | case CXCursor_ObjCProtocolRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4727 | return cxstring::createRef("ObjCProtocolRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4728 | case CXCursor_ObjCClassRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4729 | return cxstring::createRef("ObjCClassRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4730 | case CXCursor_TypeRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4731 | return cxstring::createRef("TypeRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4732 | case CXCursor_TemplateRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4733 | return cxstring::createRef("TemplateRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4734 | case CXCursor_NamespaceRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4735 | return cxstring::createRef("NamespaceRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4736 | case CXCursor_MemberRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4737 | return cxstring::createRef("MemberRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4738 | case CXCursor_LabelRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4739 | return cxstring::createRef("LabelRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4740 | case CXCursor_OverloadedDeclRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4741 | return cxstring::createRef("OverloadedDeclRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4742 | case CXCursor_VariableRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4743 | return cxstring::createRef("VariableRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4744 | case CXCursor_IntegerLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4745 | return cxstring::createRef("IntegerLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4746 | case CXCursor_FloatingLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4747 | return cxstring::createRef("FloatingLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4748 | case CXCursor_ImaginaryLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4749 | return cxstring::createRef("ImaginaryLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4750 | case CXCursor_StringLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4751 | return cxstring::createRef("StringLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4752 | case CXCursor_CharacterLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4753 | return cxstring::createRef("CharacterLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4754 | case CXCursor_ParenExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4755 | return cxstring::createRef("ParenExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4756 | case CXCursor_UnaryOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4757 | return cxstring::createRef("UnaryOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4758 | case CXCursor_ArraySubscriptExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4759 | return cxstring::createRef("ArraySubscriptExpr"); |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 4760 | case CXCursor_OMPArraySectionExpr: |
| 4761 | return cxstring::createRef("OMPArraySectionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4762 | case CXCursor_BinaryOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4763 | return cxstring::createRef("BinaryOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4764 | case CXCursor_CompoundAssignOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4765 | return cxstring::createRef("CompoundAssignOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4766 | case CXCursor_ConditionalOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4767 | return cxstring::createRef("ConditionalOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4768 | case CXCursor_CStyleCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4769 | return cxstring::createRef("CStyleCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4770 | case CXCursor_CompoundLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4771 | return cxstring::createRef("CompoundLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4772 | case CXCursor_InitListExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4773 | return cxstring::createRef("InitListExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4774 | case CXCursor_AddrLabelExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4775 | return cxstring::createRef("AddrLabelExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4776 | case CXCursor_StmtExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4777 | return cxstring::createRef("StmtExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4778 | case CXCursor_GenericSelectionExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4779 | return cxstring::createRef("GenericSelectionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4780 | case CXCursor_GNUNullExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4781 | return cxstring::createRef("GNUNullExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4782 | case CXCursor_CXXStaticCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4783 | return cxstring::createRef("CXXStaticCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4784 | case CXCursor_CXXDynamicCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4785 | return cxstring::createRef("CXXDynamicCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4786 | case CXCursor_CXXReinterpretCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4787 | return cxstring::createRef("CXXReinterpretCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4788 | case CXCursor_CXXConstCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4789 | return cxstring::createRef("CXXConstCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4790 | case CXCursor_CXXFunctionalCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4791 | return cxstring::createRef("CXXFunctionalCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4792 | case CXCursor_CXXTypeidExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4793 | return cxstring::createRef("CXXTypeidExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4794 | case CXCursor_CXXBoolLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4795 | return cxstring::createRef("CXXBoolLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4796 | case CXCursor_CXXNullPtrLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4797 | return cxstring::createRef("CXXNullPtrLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4798 | case CXCursor_CXXThisExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4799 | return cxstring::createRef("CXXThisExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4800 | case CXCursor_CXXThrowExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4801 | return cxstring::createRef("CXXThrowExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4802 | case CXCursor_CXXNewExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4803 | return cxstring::createRef("CXXNewExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4804 | case CXCursor_CXXDeleteExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4805 | return cxstring::createRef("CXXDeleteExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4806 | case CXCursor_UnaryExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4807 | return cxstring::createRef("UnaryExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4808 | case CXCursor_ObjCStringLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4809 | return cxstring::createRef("ObjCStringLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4810 | case CXCursor_ObjCBoolLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4811 | return cxstring::createRef("ObjCBoolLiteralExpr"); |
Erik Pilkington | 29099de | 2016-07-16 00:35:23 +0000 | [diff] [blame] | 4812 | case CXCursor_ObjCAvailabilityCheckExpr: |
| 4813 | return cxstring::createRef("ObjCAvailabilityCheckExpr"); |
Argyrios Kyrtzidis | c2233be | 2013-04-23 17:57:17 +0000 | [diff] [blame] | 4814 | case CXCursor_ObjCSelfExpr: |
| 4815 | return cxstring::createRef("ObjCSelfExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4816 | case CXCursor_ObjCEncodeExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4817 | return cxstring::createRef("ObjCEncodeExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4818 | case CXCursor_ObjCSelectorExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4819 | return cxstring::createRef("ObjCSelectorExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4820 | case CXCursor_ObjCProtocolExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4821 | return cxstring::createRef("ObjCProtocolExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4822 | case CXCursor_ObjCBridgedCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4823 | return cxstring::createRef("ObjCBridgedCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4824 | case CXCursor_BlockExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4825 | return cxstring::createRef("BlockExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4826 | case CXCursor_PackExpansionExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4827 | return cxstring::createRef("PackExpansionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4828 | case CXCursor_SizeOfPackExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4829 | return cxstring::createRef("SizeOfPackExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4830 | case CXCursor_LambdaExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4831 | return cxstring::createRef("LambdaExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4832 | case CXCursor_UnexposedExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4833 | return cxstring::createRef("UnexposedExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4834 | case CXCursor_DeclRefExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4835 | return cxstring::createRef("DeclRefExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4836 | case CXCursor_MemberRefExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4837 | return cxstring::createRef("MemberRefExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4838 | case CXCursor_CallExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4839 | return cxstring::createRef("CallExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4840 | case CXCursor_ObjCMessageExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4841 | return cxstring::createRef("ObjCMessageExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4842 | case CXCursor_UnexposedStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4843 | return cxstring::createRef("UnexposedStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4844 | case CXCursor_DeclStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4845 | return cxstring::createRef("DeclStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4846 | case CXCursor_LabelStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4847 | return cxstring::createRef("LabelStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4848 | case CXCursor_CompoundStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4849 | return cxstring::createRef("CompoundStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4850 | case CXCursor_CaseStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4851 | return cxstring::createRef("CaseStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4852 | case CXCursor_DefaultStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4853 | return cxstring::createRef("DefaultStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4854 | case CXCursor_IfStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4855 | return cxstring::createRef("IfStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4856 | case CXCursor_SwitchStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4857 | return cxstring::createRef("SwitchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4858 | case CXCursor_WhileStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4859 | return cxstring::createRef("WhileStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4860 | case CXCursor_DoStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4861 | return cxstring::createRef("DoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4862 | case CXCursor_ForStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4863 | return cxstring::createRef("ForStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4864 | case CXCursor_GotoStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4865 | return cxstring::createRef("GotoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4866 | case CXCursor_IndirectGotoStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4867 | return cxstring::createRef("IndirectGotoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4868 | case CXCursor_ContinueStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4869 | return cxstring::createRef("ContinueStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4870 | case CXCursor_BreakStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4871 | return cxstring::createRef("BreakStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4872 | case CXCursor_ReturnStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4873 | return cxstring::createRef("ReturnStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4874 | case CXCursor_GCCAsmStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4875 | return cxstring::createRef("GCCAsmStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4876 | case CXCursor_MSAsmStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4877 | return cxstring::createRef("MSAsmStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4878 | case CXCursor_ObjCAtTryStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4879 | return cxstring::createRef("ObjCAtTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4880 | case CXCursor_ObjCAtCatchStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4881 | return cxstring::createRef("ObjCAtCatchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4882 | case CXCursor_ObjCAtFinallyStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4883 | return cxstring::createRef("ObjCAtFinallyStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4884 | case CXCursor_ObjCAtThrowStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4885 | return cxstring::createRef("ObjCAtThrowStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4886 | case CXCursor_ObjCAtSynchronizedStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4887 | return cxstring::createRef("ObjCAtSynchronizedStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4888 | case CXCursor_ObjCAutoreleasePoolStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4889 | return cxstring::createRef("ObjCAutoreleasePoolStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4890 | case CXCursor_ObjCForCollectionStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4891 | return cxstring::createRef("ObjCForCollectionStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4892 | case CXCursor_CXXCatchStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4893 | return cxstring::createRef("CXXCatchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4894 | case CXCursor_CXXTryStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4895 | return cxstring::createRef("CXXTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4896 | case CXCursor_CXXForRangeStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4897 | return cxstring::createRef("CXXForRangeStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4898 | case CXCursor_SEHTryStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4899 | return cxstring::createRef("SEHTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4900 | case CXCursor_SEHExceptStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4901 | return cxstring::createRef("SEHExceptStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4902 | case CXCursor_SEHFinallyStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4903 | return cxstring::createRef("SEHFinallyStmt"); |
Nico Weber | 9b98207 | 2014-07-07 00:12:30 +0000 | [diff] [blame] | 4904 | case CXCursor_SEHLeaveStmt: |
| 4905 | return cxstring::createRef("SEHLeaveStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4906 | case CXCursor_NullStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4907 | return cxstring::createRef("NullStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4908 | case CXCursor_InvalidFile: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4909 | return cxstring::createRef("InvalidFile"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4910 | case CXCursor_InvalidCode: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4911 | return cxstring::createRef("InvalidCode"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4912 | case CXCursor_NoDeclFound: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4913 | return cxstring::createRef("NoDeclFound"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4914 | case CXCursor_NotImplemented: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4915 | return cxstring::createRef("NotImplemented"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4916 | case CXCursor_TranslationUnit: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4917 | return cxstring::createRef("TranslationUnit"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4918 | case CXCursor_UnexposedAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4919 | return cxstring::createRef("UnexposedAttr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4920 | case CXCursor_IBActionAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4921 | return cxstring::createRef("attribute(ibaction)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4922 | case CXCursor_IBOutletAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4923 | return cxstring::createRef("attribute(iboutlet)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4924 | case CXCursor_IBOutletCollectionAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4925 | return cxstring::createRef("attribute(iboutletcollection)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4926 | case CXCursor_CXXFinalAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4927 | return cxstring::createRef("attribute(final)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4928 | case CXCursor_CXXOverrideAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4929 | return cxstring::createRef("attribute(override)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4930 | case CXCursor_AnnotateAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4931 | return cxstring::createRef("attribute(annotate)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4932 | case CXCursor_AsmLabelAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4933 | return cxstring::createRef("asm label"); |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 4934 | case CXCursor_PackedAttr: |
| 4935 | return cxstring::createRef("attribute(packed)"); |
Joey Gouly | 8122838 | 2014-05-01 15:41:58 +0000 | [diff] [blame] | 4936 | case CXCursor_PureAttr: |
| 4937 | return cxstring::createRef("attribute(pure)"); |
| 4938 | case CXCursor_ConstAttr: |
| 4939 | return cxstring::createRef("attribute(const)"); |
| 4940 | case CXCursor_NoDuplicateAttr: |
| 4941 | return cxstring::createRef("attribute(noduplicate)"); |
Eli Bendersky | 2581e66 | 2014-05-28 19:29:58 +0000 | [diff] [blame] | 4942 | case CXCursor_CUDAConstantAttr: |
| 4943 | return cxstring::createRef("attribute(constant)"); |
| 4944 | case CXCursor_CUDADeviceAttr: |
| 4945 | return cxstring::createRef("attribute(device)"); |
| 4946 | case CXCursor_CUDAGlobalAttr: |
| 4947 | return cxstring::createRef("attribute(global)"); |
| 4948 | case CXCursor_CUDAHostAttr: |
| 4949 | return cxstring::createRef("attribute(host)"); |
Eli Bendersky | 9b07147 | 2014-08-08 14:59:00 +0000 | [diff] [blame] | 4950 | case CXCursor_CUDASharedAttr: |
| 4951 | return cxstring::createRef("attribute(shared)"); |
Saleem Abdulrasool | 79c6971 | 2015-09-05 18:53:43 +0000 | [diff] [blame] | 4952 | case CXCursor_VisibilityAttr: |
| 4953 | return cxstring::createRef("attribute(visibility)"); |
Saleem Abdulrasool | 8aa0b80 | 2015-12-10 18:45:18 +0000 | [diff] [blame] | 4954 | case CXCursor_DLLExport: |
| 4955 | return cxstring::createRef("attribute(dllexport)"); |
| 4956 | case CXCursor_DLLImport: |
| 4957 | return cxstring::createRef("attribute(dllimport)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4958 | case CXCursor_PreprocessingDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4959 | return cxstring::createRef("preprocessing directive"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4960 | case CXCursor_MacroDefinition: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4961 | return cxstring::createRef("macro definition"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4962 | case CXCursor_MacroExpansion: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4963 | return cxstring::createRef("macro expansion"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4964 | case CXCursor_InclusionDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4965 | return cxstring::createRef("inclusion directive"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4966 | case CXCursor_Namespace: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4967 | return cxstring::createRef("Namespace"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4968 | case CXCursor_LinkageSpec: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4969 | return cxstring::createRef("LinkageSpec"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4970 | case CXCursor_CXXBaseSpecifier: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4971 | return cxstring::createRef("C++ base class specifier"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4972 | case CXCursor_Constructor: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4973 | return cxstring::createRef("CXXConstructor"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4974 | case CXCursor_Destructor: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4975 | return cxstring::createRef("CXXDestructor"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4976 | case CXCursor_ConversionFunction: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4977 | return cxstring::createRef("CXXConversion"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4978 | case CXCursor_TemplateTypeParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4979 | return cxstring::createRef("TemplateTypeParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4980 | case CXCursor_NonTypeTemplateParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4981 | return cxstring::createRef("NonTypeTemplateParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4982 | case CXCursor_TemplateTemplateParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4983 | return cxstring::createRef("TemplateTemplateParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4984 | case CXCursor_FunctionTemplate: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4985 | return cxstring::createRef("FunctionTemplate"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4986 | case CXCursor_ClassTemplate: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4987 | return cxstring::createRef("ClassTemplate"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4988 | case CXCursor_ClassTemplatePartialSpecialization: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4989 | return cxstring::createRef("ClassTemplatePartialSpecialization"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4990 | case CXCursor_NamespaceAlias: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4991 | return cxstring::createRef("NamespaceAlias"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4992 | case CXCursor_UsingDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4993 | return cxstring::createRef("UsingDirective"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4994 | case CXCursor_UsingDeclaration: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4995 | return cxstring::createRef("UsingDeclaration"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4996 | case CXCursor_TypeAliasDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4997 | return cxstring::createRef("TypeAliasDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4998 | case CXCursor_ObjCSynthesizeDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4999 | return cxstring::createRef("ObjCSynthesizeDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5000 | case CXCursor_ObjCDynamicDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5001 | return cxstring::createRef("ObjCDynamicDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5002 | case CXCursor_CXXAccessSpecifier: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5003 | return cxstring::createRef("CXXAccessSpecifier"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5004 | case CXCursor_ModuleImportDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5005 | return cxstring::createRef("ModuleImport"); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 5006 | case CXCursor_OMPParallelDirective: |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 5007 | return cxstring::createRef("OMPParallelDirective"); |
| 5008 | case CXCursor_OMPSimdDirective: |
| 5009 | return cxstring::createRef("OMPSimdDirective"); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5010 | case CXCursor_OMPForDirective: |
| 5011 | return cxstring::createRef("OMPForDirective"); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 5012 | case CXCursor_OMPForSimdDirective: |
| 5013 | return cxstring::createRef("OMPForSimdDirective"); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 5014 | case CXCursor_OMPSectionsDirective: |
| 5015 | return cxstring::createRef("OMPSectionsDirective"); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 5016 | case CXCursor_OMPSectionDirective: |
| 5017 | return cxstring::createRef("OMPSectionDirective"); |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 5018 | case CXCursor_OMPSingleDirective: |
| 5019 | return cxstring::createRef("OMPSingleDirective"); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 5020 | case CXCursor_OMPMasterDirective: |
| 5021 | return cxstring::createRef("OMPMasterDirective"); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 5022 | case CXCursor_OMPCriticalDirective: |
| 5023 | return cxstring::createRef("OMPCriticalDirective"); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 5024 | case CXCursor_OMPParallelForDirective: |
| 5025 | return cxstring::createRef("OMPParallelForDirective"); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 5026 | case CXCursor_OMPParallelForSimdDirective: |
| 5027 | return cxstring::createRef("OMPParallelForSimdDirective"); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 5028 | case CXCursor_OMPParallelSectionsDirective: |
| 5029 | return cxstring::createRef("OMPParallelSectionsDirective"); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5030 | case CXCursor_OMPTaskDirective: |
| 5031 | return cxstring::createRef("OMPTaskDirective"); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 5032 | case CXCursor_OMPTaskyieldDirective: |
| 5033 | return cxstring::createRef("OMPTaskyieldDirective"); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 5034 | case CXCursor_OMPBarrierDirective: |
| 5035 | return cxstring::createRef("OMPBarrierDirective"); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 5036 | case CXCursor_OMPTaskwaitDirective: |
| 5037 | return cxstring::createRef("OMPTaskwaitDirective"); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 5038 | case CXCursor_OMPTaskgroupDirective: |
| 5039 | return cxstring::createRef("OMPTaskgroupDirective"); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 5040 | case CXCursor_OMPFlushDirective: |
| 5041 | return cxstring::createRef("OMPFlushDirective"); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 5042 | case CXCursor_OMPOrderedDirective: |
| 5043 | return cxstring::createRef("OMPOrderedDirective"); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 5044 | case CXCursor_OMPAtomicDirective: |
| 5045 | return cxstring::createRef("OMPAtomicDirective"); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 5046 | case CXCursor_OMPTargetDirective: |
| 5047 | return cxstring::createRef("OMPTargetDirective"); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 5048 | case CXCursor_OMPTargetDataDirective: |
| 5049 | return cxstring::createRef("OMPTargetDataDirective"); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 5050 | case CXCursor_OMPTargetEnterDataDirective: |
| 5051 | return cxstring::createRef("OMPTargetEnterDataDirective"); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 5052 | case CXCursor_OMPTargetExitDataDirective: |
| 5053 | return cxstring::createRef("OMPTargetExitDataDirective"); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 5054 | case CXCursor_OMPTargetParallelDirective: |
| 5055 | return cxstring::createRef("OMPTargetParallelDirective"); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 5056 | case CXCursor_OMPTargetParallelForDirective: |
| 5057 | return cxstring::createRef("OMPTargetParallelForDirective"); |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 5058 | case CXCursor_OMPTargetUpdateDirective: |
| 5059 | return cxstring::createRef("OMPTargetUpdateDirective"); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 5060 | case CXCursor_OMPTeamsDirective: |
| 5061 | return cxstring::createRef("OMPTeamsDirective"); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 5062 | case CXCursor_OMPCancellationPointDirective: |
| 5063 | return cxstring::createRef("OMPCancellationPointDirective"); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 5064 | case CXCursor_OMPCancelDirective: |
| 5065 | return cxstring::createRef("OMPCancelDirective"); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 5066 | case CXCursor_OMPTaskLoopDirective: |
| 5067 | return cxstring::createRef("OMPTaskLoopDirective"); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 5068 | case CXCursor_OMPTaskLoopSimdDirective: |
| 5069 | return cxstring::createRef("OMPTaskLoopSimdDirective"); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 5070 | case CXCursor_OMPDistributeDirective: |
| 5071 | return cxstring::createRef("OMPDistributeDirective"); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 5072 | case CXCursor_OMPDistributeParallelForDirective: |
| 5073 | return cxstring::createRef("OMPDistributeParallelForDirective"); |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 5074 | case CXCursor_OMPDistributeParallelForSimdDirective: |
| 5075 | return cxstring::createRef("OMPDistributeParallelForSimdDirective"); |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 5076 | case CXCursor_OMPDistributeSimdDirective: |
| 5077 | return cxstring::createRef("OMPDistributeSimdDirective"); |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 5078 | case CXCursor_OMPTargetParallelForSimdDirective: |
| 5079 | return cxstring::createRef("OMPTargetParallelForSimdDirective"); |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 5080 | case CXCursor_OMPTargetSimdDirective: |
| 5081 | return cxstring::createRef("OMPTargetSimdDirective"); |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 5082 | case CXCursor_OMPTeamsDistributeDirective: |
| 5083 | return cxstring::createRef("OMPTeamsDistributeDirective"); |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 5084 | case CXCursor_OMPTeamsDistributeSimdDirective: |
| 5085 | return cxstring::createRef("OMPTeamsDistributeSimdDirective"); |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 5086 | case CXCursor_OMPTeamsDistributeParallelForSimdDirective: |
| 5087 | return cxstring::createRef("OMPTeamsDistributeParallelForSimdDirective"); |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 5088 | case CXCursor_OMPTeamsDistributeParallelForDirective: |
| 5089 | return cxstring::createRef("OMPTeamsDistributeParallelForDirective"); |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 5090 | case CXCursor_OMPTargetTeamsDirective: |
| 5091 | return cxstring::createRef("OMPTargetTeamsDirective"); |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 5092 | case CXCursor_OMPTargetTeamsDistributeDirective: |
| 5093 | return cxstring::createRef("OMPTargetTeamsDistributeDirective"); |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 5094 | case CXCursor_OMPTargetTeamsDistributeParallelForDirective: |
| 5095 | return cxstring::createRef("OMPTargetTeamsDistributeParallelForDirective"); |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 5096 | case CXCursor_OMPTargetTeamsDistributeParallelForSimdDirective: |
| 5097 | return cxstring::createRef( |
| 5098 | "OMPTargetTeamsDistributeParallelForSimdDirective"); |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 5099 | case CXCursor_OMPTargetTeamsDistributeSimdDirective: |
| 5100 | return cxstring::createRef("OMPTargetTeamsDistributeSimdDirective"); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5101 | case CXCursor_OverloadCandidate: |
| 5102 | return cxstring::createRef("OverloadCandidate"); |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 5103 | case CXCursor_TypeAliasTemplateDecl: |
| 5104 | return cxstring::createRef("TypeAliasTemplateDecl"); |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 5105 | case CXCursor_StaticAssert: |
| 5106 | return cxstring::createRef("StaticAssert"); |
Olivier Goffart | d211c64 | 2016-11-04 06:29:27 +0000 | [diff] [blame] | 5107 | case CXCursor_FriendDecl: |
| 5108 | return cxstring::createRef("FriendDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5109 | } |
| 5110 | |
| 5111 | llvm_unreachable("Unhandled CXCursorKind"); |
| 5112 | } |
| 5113 | |
| 5114 | struct GetCursorData { |
| 5115 | SourceLocation TokenBeginLoc; |
| 5116 | bool PointsAtMacroArgExpansion; |
| 5117 | bool VisitedObjCPropertyImplDecl; |
| 5118 | SourceLocation VisitedDeclaratorDeclStartLoc; |
| 5119 | CXCursor &BestCursor; |
| 5120 | |
| 5121 | GetCursorData(SourceManager &SM, |
| 5122 | SourceLocation tokenBegin, CXCursor &outputCursor) |
| 5123 | : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) { |
| 5124 | PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin); |
| 5125 | VisitedObjCPropertyImplDecl = false; |
| 5126 | } |
| 5127 | }; |
| 5128 | |
| 5129 | static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, |
| 5130 | CXCursor parent, |
| 5131 | CXClientData client_data) { |
| 5132 | GetCursorData *Data = static_cast<GetCursorData *>(client_data); |
| 5133 | CXCursor *BestCursor = &Data->BestCursor; |
| 5134 | |
| 5135 | // If we point inside a macro argument we should provide info of what the |
| 5136 | // token is so use the actual cursor, don't replace it with a macro expansion |
| 5137 | // cursor. |
| 5138 | if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion) |
| 5139 | return CXChildVisit_Recurse; |
| 5140 | |
| 5141 | if (clang_isDeclaration(cursor.kind)) { |
| 5142 | // Avoid having the implicit methods override the property decls. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5143 | if (const ObjCMethodDecl *MD |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5144 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 5145 | if (MD->isImplicit()) |
| 5146 | return CXChildVisit_Break; |
| 5147 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5148 | } else if (const ObjCInterfaceDecl *ID |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5149 | = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(cursor))) { |
| 5150 | // Check that when we have multiple @class references in the same line, |
| 5151 | // that later ones do not override the previous ones. |
| 5152 | // If we have: |
| 5153 | // @class Foo, Bar; |
| 5154 | // source ranges for both start at '@', so 'Bar' will end up overriding |
| 5155 | // 'Foo' even though the cursor location was at 'Foo'. |
| 5156 | if (BestCursor->kind == CXCursor_ObjCInterfaceDecl || |
| 5157 | BestCursor->kind == CXCursor_ObjCClassRef) |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5158 | if (const ObjCInterfaceDecl *PrevID |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5159 | = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(*BestCursor))){ |
| 5160 | if (PrevID != ID && |
| 5161 | !PrevID->isThisDeclarationADefinition() && |
| 5162 | !ID->isThisDeclarationADefinition()) |
| 5163 | return CXChildVisit_Break; |
| 5164 | } |
| 5165 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5166 | } else if (const DeclaratorDecl *DD |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5167 | = dyn_cast_or_null<DeclaratorDecl>(getCursorDecl(cursor))) { |
| 5168 | SourceLocation StartLoc = DD->getSourceRange().getBegin(); |
| 5169 | // Check that when we have multiple declarators in the same line, |
| 5170 | // that later ones do not override the previous ones. |
| 5171 | // If we have: |
| 5172 | // int Foo, Bar; |
| 5173 | // source ranges for both start at 'int', so 'Bar' will end up overriding |
| 5174 | // 'Foo' even though the cursor location was at 'Foo'. |
| 5175 | if (Data->VisitedDeclaratorDeclStartLoc == StartLoc) |
| 5176 | return CXChildVisit_Break; |
| 5177 | Data->VisitedDeclaratorDeclStartLoc = StartLoc; |
| 5178 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5179 | } else if (const ObjCPropertyImplDecl *PropImp |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5180 | = dyn_cast_or_null<ObjCPropertyImplDecl>(getCursorDecl(cursor))) { |
| 5181 | (void)PropImp; |
| 5182 | // Check that when we have multiple @synthesize in the same line, |
| 5183 | // that later ones do not override the previous ones. |
| 5184 | // If we have: |
| 5185 | // @synthesize Foo, Bar; |
| 5186 | // source ranges for both start at '@', so 'Bar' will end up overriding |
| 5187 | // 'Foo' even though the cursor location was at 'Foo'. |
| 5188 | if (Data->VisitedObjCPropertyImplDecl) |
| 5189 | return CXChildVisit_Break; |
| 5190 | Data->VisitedObjCPropertyImplDecl = true; |
| 5191 | } |
| 5192 | } |
| 5193 | |
| 5194 | if (clang_isExpression(cursor.kind) && |
| 5195 | clang_isDeclaration(BestCursor->kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5196 | if (const Decl *D = getCursorDecl(*BestCursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5197 | // Avoid having the cursor of an expression replace the declaration cursor |
| 5198 | // when the expression source range overlaps the declaration range. |
| 5199 | // This can happen for C++ constructor expressions whose range generally |
| 5200 | // include the variable declaration, e.g.: |
| 5201 | // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor. |
| 5202 | if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() && |
| 5203 | D->getLocation() == Data->TokenBeginLoc) |
| 5204 | return CXChildVisit_Break; |
| 5205 | } |
| 5206 | } |
| 5207 | |
| 5208 | // If our current best cursor is the construction of a temporary object, |
| 5209 | // don't replace that cursor with a type reference, because we want |
| 5210 | // clang_getCursor() to point at the constructor. |
| 5211 | if (clang_isExpression(BestCursor->kind) && |
| 5212 | isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && |
| 5213 | cursor.kind == CXCursor_TypeRef) { |
| 5214 | // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it |
| 5215 | // as having the actual point on the type reference. |
| 5216 | *BestCursor = getTypeRefedCallExprCursor(*BestCursor); |
| 5217 | return CXChildVisit_Recurse; |
| 5218 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 5219 | |
| 5220 | // If we already have an Objective-C superclass reference, don't |
| 5221 | // update it further. |
| 5222 | if (BestCursor->kind == CXCursor_ObjCSuperClassRef) |
| 5223 | return CXChildVisit_Break; |
| 5224 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5225 | *BestCursor = cursor; |
| 5226 | return CXChildVisit_Recurse; |
| 5227 | } |
| 5228 | |
| 5229 | CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 5230 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 5231 | LOG_BAD_TU(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5232 | return clang_getNullCursor(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 5233 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5234 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 5235 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5236 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 5237 | |
| 5238 | SourceLocation SLoc = cxloc::translateSourceLocation(Loc); |
| 5239 | CXCursor Result = cxcursor::getCursor(TU, SLoc); |
| 5240 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5241 | LOG_FUNC_SECTION { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5242 | CXFile SearchFile; |
| 5243 | unsigned SearchLine, SearchColumn; |
| 5244 | CXFile ResultFile; |
| 5245 | unsigned ResultLine, ResultColumn; |
| 5246 | CXString SearchFileName, ResultFileName, KindSpelling, USR; |
| 5247 | const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : ""; |
| 5248 | CXSourceLocation ResultLoc = clang_getCursorLocation(Result); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5249 | |
| 5250 | clang_getFileLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, |
| 5251 | nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5252 | clang_getFileLocation(ResultLoc, &ResultFile, &ResultLine, |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5253 | &ResultColumn, nullptr); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5254 | SearchFileName = clang_getFileName(SearchFile); |
| 5255 | ResultFileName = clang_getFileName(ResultFile); |
| 5256 | KindSpelling = clang_getCursorKindSpelling(Result.kind); |
| 5257 | USR = clang_getCursorUSR(Result); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5258 | *Log << llvm::format("(%s:%d:%d) = %s", |
| 5259 | clang_getCString(SearchFileName), SearchLine, SearchColumn, |
| 5260 | clang_getCString(KindSpelling)) |
| 5261 | << llvm::format("(%s:%d:%d):%s%s", |
| 5262 | clang_getCString(ResultFileName), ResultLine, ResultColumn, |
| 5263 | clang_getCString(USR), IsDef); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5264 | clang_disposeString(SearchFileName); |
| 5265 | clang_disposeString(ResultFileName); |
| 5266 | clang_disposeString(KindSpelling); |
| 5267 | clang_disposeString(USR); |
| 5268 | |
| 5269 | CXCursor Definition = clang_getCursorDefinition(Result); |
| 5270 | if (!clang_equalCursors(Definition, clang_getNullCursor())) { |
| 5271 | CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition); |
| 5272 | CXString DefinitionKindSpelling |
| 5273 | = clang_getCursorKindSpelling(Definition.kind); |
| 5274 | CXFile DefinitionFile; |
| 5275 | unsigned DefinitionLine, DefinitionColumn; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5276 | clang_getFileLocation(DefinitionLoc, &DefinitionFile, |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5277 | &DefinitionLine, &DefinitionColumn, nullptr); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5278 | CXString DefinitionFileName = clang_getFileName(DefinitionFile); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5279 | *Log << llvm::format(" -> %s(%s:%d:%d)", |
| 5280 | clang_getCString(DefinitionKindSpelling), |
| 5281 | clang_getCString(DefinitionFileName), |
| 5282 | DefinitionLine, DefinitionColumn); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5283 | clang_disposeString(DefinitionFileName); |
| 5284 | clang_disposeString(DefinitionKindSpelling); |
| 5285 | } |
| 5286 | } |
| 5287 | |
| 5288 | return Result; |
| 5289 | } |
| 5290 | |
| 5291 | CXCursor clang_getNullCursor(void) { |
| 5292 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
| 5293 | } |
| 5294 | |
| 5295 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5296 | // Clear out the "FirstInDeclGroup" part in a declaration cursor, since we |
| 5297 | // can't set consistently. For example, when visiting a DeclStmt we will set |
| 5298 | // it but we don't set it on the result of clang_getCursorDefinition for |
| 5299 | // a reference of the same declaration. |
| 5300 | // FIXME: Setting "FirstInDeclGroup" in CXCursors is a hack that only works |
| 5301 | // when visiting a DeclStmt currently, the AST should be enhanced to be able |
| 5302 | // to provide that kind of info. |
| 5303 | if (clang_isDeclaration(X.kind)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5304 | X.data[1] = nullptr; |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5305 | if (clang_isDeclaration(Y.kind)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5306 | Y.data[1] = nullptr; |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5307 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5308 | return X == Y; |
| 5309 | } |
| 5310 | |
| 5311 | unsigned clang_hashCursor(CXCursor C) { |
| 5312 | unsigned Index = 0; |
| 5313 | if (clang_isExpression(C.kind) || clang_isStatement(C.kind)) |
| 5314 | Index = 1; |
| 5315 | |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5316 | return llvm::DenseMapInfo<std::pair<unsigned, const void*> >::getHashValue( |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5317 | std::make_pair(C.kind, C.data[Index])); |
| 5318 | } |
| 5319 | |
| 5320 | unsigned clang_isInvalid(enum CXCursorKind K) { |
| 5321 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 5322 | } |
| 5323 | |
| 5324 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
| 5325 | return (K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl) || |
| 5326 | (K >= CXCursor_FirstExtraDecl && K <= CXCursor_LastExtraDecl); |
| 5327 | } |
| 5328 | |
| 5329 | unsigned clang_isReference(enum CXCursorKind K) { |
| 5330 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 5331 | } |
| 5332 | |
| 5333 | unsigned clang_isExpression(enum CXCursorKind K) { |
| 5334 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; |
| 5335 | } |
| 5336 | |
| 5337 | unsigned clang_isStatement(enum CXCursorKind K) { |
| 5338 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; |
| 5339 | } |
| 5340 | |
| 5341 | unsigned clang_isAttribute(enum CXCursorKind K) { |
| 5342 | return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr; |
| 5343 | } |
| 5344 | |
| 5345 | unsigned clang_isTranslationUnit(enum CXCursorKind K) { |
| 5346 | return K == CXCursor_TranslationUnit; |
| 5347 | } |
| 5348 | |
| 5349 | unsigned clang_isPreprocessing(enum CXCursorKind K) { |
| 5350 | return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; |
| 5351 | } |
| 5352 | |
| 5353 | unsigned clang_isUnexposed(enum CXCursorKind K) { |
| 5354 | switch (K) { |
| 5355 | case CXCursor_UnexposedDecl: |
| 5356 | case CXCursor_UnexposedExpr: |
| 5357 | case CXCursor_UnexposedStmt: |
| 5358 | case CXCursor_UnexposedAttr: |
| 5359 | return true; |
| 5360 | default: |
| 5361 | return false; |
| 5362 | } |
| 5363 | } |
| 5364 | |
| 5365 | CXCursorKind clang_getCursorKind(CXCursor C) { |
| 5366 | return C.kind; |
| 5367 | } |
| 5368 | |
| 5369 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 5370 | if (clang_isReference(C.kind)) { |
| 5371 | switch (C.kind) { |
| 5372 | case CXCursor_ObjCSuperClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5373 | std::pair<const ObjCInterfaceDecl *, SourceLocation> P |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5374 | = getCursorObjCSuperClassRef(C); |
| 5375 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5376 | } |
| 5377 | |
| 5378 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5379 | std::pair<const ObjCProtocolDecl *, SourceLocation> P |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5380 | = getCursorObjCProtocolRef(C); |
| 5381 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5382 | } |
| 5383 | |
| 5384 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5385 | std::pair<const ObjCInterfaceDecl *, SourceLocation> P |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5386 | = getCursorObjCClassRef(C); |
| 5387 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5388 | } |
| 5389 | |
| 5390 | case CXCursor_TypeRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5391 | std::pair<const TypeDecl *, SourceLocation> P = getCursorTypeRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5392 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5393 | } |
| 5394 | |
| 5395 | case CXCursor_TemplateRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5396 | std::pair<const TemplateDecl *, SourceLocation> P = |
| 5397 | getCursorTemplateRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5398 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5399 | } |
| 5400 | |
| 5401 | case CXCursor_NamespaceRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5402 | std::pair<const NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5403 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5404 | } |
| 5405 | |
| 5406 | case CXCursor_MemberRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5407 | std::pair<const FieldDecl *, SourceLocation> P = getCursorMemberRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5408 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5409 | } |
| 5410 | |
| 5411 | case CXCursor_VariableRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5412 | std::pair<const VarDecl *, SourceLocation> P = getCursorVariableRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5413 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5414 | } |
| 5415 | |
| 5416 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5417 | const CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5418 | if (!BaseSpec) |
| 5419 | return clang_getNullLocation(); |
| 5420 | |
| 5421 | if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) |
| 5422 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5423 | TSInfo->getTypeLoc().getBeginLoc()); |
| 5424 | |
| 5425 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5426 | BaseSpec->getLocStart()); |
| 5427 | } |
| 5428 | |
| 5429 | case CXCursor_LabelRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5430 | std::pair<const LabelStmt *, SourceLocation> P = getCursorLabelRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5431 | return cxloc::translateSourceLocation(getCursorContext(C), P.second); |
| 5432 | } |
| 5433 | |
| 5434 | case CXCursor_OverloadedDeclRef: |
| 5435 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5436 | getCursorOverloadedDeclRef(C).second); |
| 5437 | |
| 5438 | default: |
| 5439 | // FIXME: Need a way to enumerate all non-reference cases. |
| 5440 | llvm_unreachable("Missed a reference kind"); |
| 5441 | } |
| 5442 | } |
| 5443 | |
| 5444 | if (clang_isExpression(C.kind)) |
| 5445 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5446 | getLocationFromExpr(getCursorExpr(C))); |
| 5447 | |
| 5448 | if (clang_isStatement(C.kind)) |
| 5449 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5450 | getCursorStmt(C)->getLocStart()); |
| 5451 | |
| 5452 | if (C.kind == CXCursor_PreprocessingDirective) { |
| 5453 | SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); |
| 5454 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5455 | } |
| 5456 | |
| 5457 | if (C.kind == CXCursor_MacroExpansion) { |
| 5458 | SourceLocation L |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 5459 | = cxcursor::getCursorMacroExpansion(C).getSourceRange().getBegin(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5460 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5461 | } |
| 5462 | |
| 5463 | if (C.kind == CXCursor_MacroDefinition) { |
| 5464 | SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); |
| 5465 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5466 | } |
| 5467 | |
| 5468 | if (C.kind == CXCursor_InclusionDirective) { |
| 5469 | SourceLocation L |
| 5470 | = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); |
| 5471 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5472 | } |
| 5473 | |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 5474 | if (clang_isAttribute(C.kind)) { |
| 5475 | SourceLocation L |
| 5476 | = cxcursor::getCursorAttr(C)->getLocation(); |
| 5477 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5478 | } |
| 5479 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5480 | if (!clang_isDeclaration(C.kind)) |
| 5481 | return clang_getNullLocation(); |
| 5482 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5483 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5484 | if (!D) |
| 5485 | return clang_getNullLocation(); |
| 5486 | |
| 5487 | SourceLocation Loc = D->getLocation(); |
| 5488 | // FIXME: Multiple variables declared in a single declaration |
| 5489 | // currently lack the information needed to correctly determine their |
| 5490 | // ranges when accounting for the type-specifier. We use context |
| 5491 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 5492 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5493 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5494 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 5495 | Loc = VD->getLocation(); |
| 5496 | } |
| 5497 | |
| 5498 | // For ObjC methods, give the start location of the method name. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5499 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5500 | Loc = MD->getSelectorStartLoc(); |
| 5501 | |
| 5502 | return cxloc::translateSourceLocation(getCursorContext(C), Loc); |
| 5503 | } |
| 5504 | |
NAKAMURA Takumi | a01f4c3 | 2016-12-19 16:50:43 +0000 | [diff] [blame] | 5505 | } // end extern "C" |
| 5506 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5507 | CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) { |
| 5508 | assert(TU); |
| 5509 | |
| 5510 | // Guard against an invalid SourceLocation, or we may assert in one |
| 5511 | // of the following calls. |
| 5512 | if (SLoc.isInvalid()) |
| 5513 | return clang_getNullCursor(); |
| 5514 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 5515 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5516 | |
| 5517 | // Translate the given source location to make it point at the beginning of |
| 5518 | // the token under the cursor. |
| 5519 | SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), |
| 5520 | CXXUnit->getASTContext().getLangOpts()); |
| 5521 | |
| 5522 | CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 5523 | if (SLoc.isValid()) { |
| 5524 | GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result); |
| 5525 | CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData, |
| 5526 | /*VisitPreprocessorLast=*/true, |
| 5527 | /*VisitIncludedEntities=*/false, |
| 5528 | SourceLocation(SLoc)); |
| 5529 | CursorVis.visitFileRegion(); |
| 5530 | } |
| 5531 | |
| 5532 | return Result; |
| 5533 | } |
| 5534 | |
| 5535 | static SourceRange getRawCursorExtent(CXCursor C) { |
| 5536 | if (clang_isReference(C.kind)) { |
| 5537 | switch (C.kind) { |
| 5538 | case CXCursor_ObjCSuperClassRef: |
| 5539 | return getCursorObjCSuperClassRef(C).second; |
| 5540 | |
| 5541 | case CXCursor_ObjCProtocolRef: |
| 5542 | return getCursorObjCProtocolRef(C).second; |
| 5543 | |
| 5544 | case CXCursor_ObjCClassRef: |
| 5545 | return getCursorObjCClassRef(C).second; |
| 5546 | |
| 5547 | case CXCursor_TypeRef: |
| 5548 | return getCursorTypeRef(C).second; |
| 5549 | |
| 5550 | case CXCursor_TemplateRef: |
| 5551 | return getCursorTemplateRef(C).second; |
| 5552 | |
| 5553 | case CXCursor_NamespaceRef: |
| 5554 | return getCursorNamespaceRef(C).second; |
| 5555 | |
| 5556 | case CXCursor_MemberRef: |
| 5557 | return getCursorMemberRef(C).second; |
| 5558 | |
| 5559 | case CXCursor_CXXBaseSpecifier: |
| 5560 | return getCursorCXXBaseSpecifier(C)->getSourceRange(); |
| 5561 | |
| 5562 | case CXCursor_LabelRef: |
| 5563 | return getCursorLabelRef(C).second; |
| 5564 | |
| 5565 | case CXCursor_OverloadedDeclRef: |
| 5566 | return getCursorOverloadedDeclRef(C).second; |
| 5567 | |
| 5568 | case CXCursor_VariableRef: |
| 5569 | return getCursorVariableRef(C).second; |
| 5570 | |
| 5571 | default: |
| 5572 | // FIXME: Need a way to enumerate all non-reference cases. |
| 5573 | llvm_unreachable("Missed a reference kind"); |
| 5574 | } |
| 5575 | } |
| 5576 | |
| 5577 | if (clang_isExpression(C.kind)) |
| 5578 | return getCursorExpr(C)->getSourceRange(); |
| 5579 | |
| 5580 | if (clang_isStatement(C.kind)) |
| 5581 | return getCursorStmt(C)->getSourceRange(); |
| 5582 | |
| 5583 | if (clang_isAttribute(C.kind)) |
| 5584 | return getCursorAttr(C)->getRange(); |
| 5585 | |
| 5586 | if (C.kind == CXCursor_PreprocessingDirective) |
| 5587 | return cxcursor::getCursorPreprocessingDirective(C); |
| 5588 | |
| 5589 | if (C.kind == CXCursor_MacroExpansion) { |
| 5590 | ASTUnit *TU = getCursorASTUnit(C); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 5591 | SourceRange Range = cxcursor::getCursorMacroExpansion(C).getSourceRange(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5592 | return TU->mapRangeFromPreamble(Range); |
| 5593 | } |
| 5594 | |
| 5595 | if (C.kind == CXCursor_MacroDefinition) { |
| 5596 | ASTUnit *TU = getCursorASTUnit(C); |
| 5597 | SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange(); |
| 5598 | return TU->mapRangeFromPreamble(Range); |
| 5599 | } |
| 5600 | |
| 5601 | if (C.kind == CXCursor_InclusionDirective) { |
| 5602 | ASTUnit *TU = getCursorASTUnit(C); |
| 5603 | SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange(); |
| 5604 | return TU->mapRangeFromPreamble(Range); |
| 5605 | } |
| 5606 | |
| 5607 | if (C.kind == CXCursor_TranslationUnit) { |
| 5608 | ASTUnit *TU = getCursorASTUnit(C); |
| 5609 | FileID MainID = TU->getSourceManager().getMainFileID(); |
| 5610 | SourceLocation Start = TU->getSourceManager().getLocForStartOfFile(MainID); |
| 5611 | SourceLocation End = TU->getSourceManager().getLocForEndOfFile(MainID); |
| 5612 | return SourceRange(Start, End); |
| 5613 | } |
| 5614 | |
| 5615 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5616 | const Decl *D = cxcursor::getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5617 | if (!D) |
| 5618 | return SourceRange(); |
| 5619 | |
| 5620 | SourceRange R = D->getSourceRange(); |
| 5621 | // FIXME: Multiple variables declared in a single declaration |
| 5622 | // currently lack the information needed to correctly determine their |
| 5623 | // ranges when accounting for the type-specifier. We use context |
| 5624 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 5625 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5626 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5627 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 5628 | R.setBegin(VD->getLocation()); |
| 5629 | } |
| 5630 | return R; |
| 5631 | } |
| 5632 | return SourceRange(); |
| 5633 | } |
| 5634 | |
| 5635 | /// \brief Retrieves the "raw" cursor extent, which is then extended to include |
| 5636 | /// the decl-specifier-seq for declarations. |
| 5637 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) { |
| 5638 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5639 | const Decl *D = cxcursor::getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5640 | if (!D) |
| 5641 | return SourceRange(); |
| 5642 | |
| 5643 | SourceRange R = D->getSourceRange(); |
| 5644 | |
| 5645 | // Adjust the start of the location for declarations preceded by |
| 5646 | // declaration specifiers. |
| 5647 | SourceLocation StartLoc; |
| 5648 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
| 5649 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
| 5650 | StartLoc = TI->getTypeLoc().getLocStart(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5651 | } else if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5652 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
| 5653 | StartLoc = TI->getTypeLoc().getLocStart(); |
| 5654 | } |
| 5655 | |
| 5656 | if (StartLoc.isValid() && R.getBegin().isValid() && |
| 5657 | SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin())) |
| 5658 | R.setBegin(StartLoc); |
| 5659 | |
| 5660 | // FIXME: Multiple variables declared in a single declaration |
| 5661 | // currently lack the information needed to correctly determine their |
| 5662 | // ranges when accounting for the type-specifier. We use context |
| 5663 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 5664 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5665 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5666 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 5667 | R.setBegin(VD->getLocation()); |
| 5668 | } |
| 5669 | |
| 5670 | return R; |
| 5671 | } |
| 5672 | |
| 5673 | return getRawCursorExtent(C); |
| 5674 | } |
| 5675 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5676 | CXSourceRange clang_getCursorExtent(CXCursor C) { |
| 5677 | SourceRange R = getRawCursorExtent(C); |
| 5678 | if (R.isInvalid()) |
| 5679 | return clang_getNullRange(); |
| 5680 | |
| 5681 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 5682 | } |
| 5683 | |
| 5684 | CXCursor clang_getCursorReferenced(CXCursor C) { |
| 5685 | if (clang_isInvalid(C.kind)) |
| 5686 | return clang_getNullCursor(); |
| 5687 | |
| 5688 | CXTranslationUnit tu = getCursorTU(C); |
| 5689 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5690 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5691 | if (!D) |
| 5692 | return clang_getNullCursor(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5693 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5694 | return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5695 | if (const ObjCPropertyImplDecl *PropImpl = |
| 5696 | dyn_cast<ObjCPropertyImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5697 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 5698 | return MakeCXCursor(Property, tu); |
| 5699 | |
| 5700 | return C; |
| 5701 | } |
| 5702 | |
| 5703 | if (clang_isExpression(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5704 | const Expr *E = getCursorExpr(C); |
| 5705 | const Decl *D = getDeclFromExpr(E); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5706 | if (D) { |
| 5707 | CXCursor declCursor = MakeCXCursor(D, tu); |
| 5708 | declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C), |
| 5709 | declCursor); |
| 5710 | return declCursor; |
| 5711 | } |
| 5712 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5713 | if (const OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5714 | return MakeCursorOverloadedDeclRef(Ovl, tu); |
| 5715 | |
| 5716 | return clang_getNullCursor(); |
| 5717 | } |
| 5718 | |
| 5719 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 5720 | const Stmt *S = getCursorStmt(C); |
| 5721 | if (const GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5722 | if (LabelDecl *label = Goto->getLabel()) |
| 5723 | if (LabelStmt *labelS = label->getStmt()) |
| 5724 | return MakeCXCursor(labelS, getCursorDecl(C), tu); |
| 5725 | |
| 5726 | return clang_getNullCursor(); |
| 5727 | } |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 5728 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5729 | if (C.kind == CXCursor_MacroExpansion) { |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 5730 | if (const MacroDefinitionRecord *Def = |
| 5731 | getCursorMacroExpansion(C).getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5732 | return MakeMacroDefinitionCursor(Def, tu); |
| 5733 | } |
| 5734 | |
| 5735 | if (!clang_isReference(C.kind)) |
| 5736 | return clang_getNullCursor(); |
| 5737 | |
| 5738 | switch (C.kind) { |
| 5739 | case CXCursor_ObjCSuperClassRef: |
| 5740 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu); |
| 5741 | |
| 5742 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5743 | const ObjCProtocolDecl *Prot = getCursorObjCProtocolRef(C).first; |
| 5744 | if (const ObjCProtocolDecl *Def = Prot->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5745 | return MakeCXCursor(Def, tu); |
| 5746 | |
| 5747 | return MakeCXCursor(Prot, tu); |
| 5748 | } |
| 5749 | |
| 5750 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5751 | const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
| 5752 | if (const ObjCInterfaceDecl *Def = Class->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5753 | return MakeCXCursor(Def, tu); |
| 5754 | |
| 5755 | return MakeCXCursor(Class, tu); |
| 5756 | } |
| 5757 | |
| 5758 | case CXCursor_TypeRef: |
| 5759 | return MakeCXCursor(getCursorTypeRef(C).first, tu ); |
| 5760 | |
| 5761 | case CXCursor_TemplateRef: |
| 5762 | return MakeCXCursor(getCursorTemplateRef(C).first, tu ); |
| 5763 | |
| 5764 | case CXCursor_NamespaceRef: |
| 5765 | return MakeCXCursor(getCursorNamespaceRef(C).first, tu ); |
| 5766 | |
| 5767 | case CXCursor_MemberRef: |
| 5768 | return MakeCXCursor(getCursorMemberRef(C).first, tu ); |
| 5769 | |
| 5770 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5771 | const CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5772 | return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), |
| 5773 | tu )); |
| 5774 | } |
| 5775 | |
| 5776 | case CXCursor_LabelRef: |
| 5777 | // FIXME: We end up faking the "parent" declaration here because we |
| 5778 | // don't want to make CXCursor larger. |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 5779 | return MakeCXCursor(getCursorLabelRef(C).first, |
| 5780 | cxtu::getASTUnit(tu)->getASTContext() |
| 5781 | .getTranslationUnitDecl(), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5782 | tu); |
| 5783 | |
| 5784 | case CXCursor_OverloadedDeclRef: |
| 5785 | return C; |
| 5786 | |
| 5787 | case CXCursor_VariableRef: |
| 5788 | return MakeCXCursor(getCursorVariableRef(C).first, tu); |
| 5789 | |
| 5790 | default: |
| 5791 | // We would prefer to enumerate all non-reference cursor kinds here. |
| 5792 | llvm_unreachable("Unhandled reference cursor kind"); |
| 5793 | } |
| 5794 | } |
| 5795 | |
| 5796 | CXCursor clang_getCursorDefinition(CXCursor C) { |
| 5797 | if (clang_isInvalid(C.kind)) |
| 5798 | return clang_getNullCursor(); |
| 5799 | |
| 5800 | CXTranslationUnit TU = getCursorTU(C); |
| 5801 | |
| 5802 | bool WasReference = false; |
| 5803 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { |
| 5804 | C = clang_getCursorReferenced(C); |
| 5805 | WasReference = true; |
| 5806 | } |
| 5807 | |
| 5808 | if (C.kind == CXCursor_MacroExpansion) |
| 5809 | return clang_getCursorReferenced(C); |
| 5810 | |
| 5811 | if (!clang_isDeclaration(C.kind)) |
| 5812 | return clang_getNullCursor(); |
| 5813 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5814 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5815 | if (!D) |
| 5816 | return clang_getNullCursor(); |
| 5817 | |
| 5818 | switch (D->getKind()) { |
| 5819 | // Declaration kinds that don't really separate the notions of |
| 5820 | // declaration and definition. |
| 5821 | case Decl::Namespace: |
| 5822 | case Decl::Typedef: |
| 5823 | case Decl::TypeAlias: |
| 5824 | case Decl::TypeAliasTemplate: |
| 5825 | case Decl::TemplateTypeParm: |
| 5826 | case Decl::EnumConstant: |
| 5827 | case Decl::Field: |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 5828 | case Decl::Binding: |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5829 | case Decl::MSProperty: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5830 | case Decl::IndirectField: |
| 5831 | case Decl::ObjCIvar: |
| 5832 | case Decl::ObjCAtDefsField: |
| 5833 | case Decl::ImplicitParam: |
| 5834 | case Decl::ParmVar: |
| 5835 | case Decl::NonTypeTemplateParm: |
| 5836 | case Decl::TemplateTemplateParm: |
| 5837 | case Decl::ObjCCategoryImpl: |
| 5838 | case Decl::ObjCImplementation: |
| 5839 | case Decl::AccessSpec: |
| 5840 | case Decl::LinkageSpec: |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 5841 | case Decl::Export: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5842 | case Decl::ObjCPropertyImpl: |
| 5843 | case Decl::FileScopeAsm: |
| 5844 | case Decl::StaticAssert: |
| 5845 | case Decl::Block: |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 5846 | case Decl::Captured: |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 5847 | case Decl::OMPCapturedExpr: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5848 | case Decl::Label: // FIXME: Is this right?? |
| 5849 | case Decl::ClassScopeFunctionSpecialization: |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 5850 | case Decl::CXXDeductionGuide: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5851 | case Decl::Import: |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 5852 | case Decl::OMPThreadPrivate: |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 5853 | case Decl::OMPDeclareReduction: |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 5854 | case Decl::ObjCTypeParam: |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 5855 | case Decl::BuiltinTemplate: |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 5856 | case Decl::PragmaComment: |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 5857 | case Decl::PragmaDetectMismatch: |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 5858 | case Decl::UsingPack: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5859 | return C; |
| 5860 | |
| 5861 | // Declaration kinds that don't make any sense here, but are |
| 5862 | // nonetheless harmless. |
David Blaikie | f005d3c | 2013-02-22 17:44:58 +0000 | [diff] [blame] | 5863 | case Decl::Empty: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5864 | case Decl::TranslationUnit: |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 5865 | case Decl::ExternCContext: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5866 | break; |
| 5867 | |
| 5868 | // Declaration kinds for which the definition is not resolvable. |
| 5869 | case Decl::UnresolvedUsingTypename: |
| 5870 | case Decl::UnresolvedUsingValue: |
| 5871 | break; |
| 5872 | |
| 5873 | case Decl::UsingDirective: |
| 5874 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), |
| 5875 | TU); |
| 5876 | |
| 5877 | case Decl::NamespaceAlias: |
| 5878 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU); |
| 5879 | |
| 5880 | case Decl::Enum: |
| 5881 | case Decl::Record: |
| 5882 | case Decl::CXXRecord: |
| 5883 | case Decl::ClassTemplateSpecialization: |
| 5884 | case Decl::ClassTemplatePartialSpecialization: |
| 5885 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) |
| 5886 | return MakeCXCursor(Def, TU); |
| 5887 | return clang_getNullCursor(); |
| 5888 | |
| 5889 | case Decl::Function: |
| 5890 | case Decl::CXXMethod: |
| 5891 | case Decl::CXXConstructor: |
| 5892 | case Decl::CXXDestructor: |
| 5893 | case Decl::CXXConversion: { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5894 | const FunctionDecl *Def = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5895 | if (cast<FunctionDecl>(D)->getBody(Def)) |
Dmitri Gribenko | 9c256e3 | 2013-01-14 00:46:27 +0000 | [diff] [blame] | 5896 | return MakeCXCursor(Def, TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5897 | return clang_getNullCursor(); |
| 5898 | } |
| 5899 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5900 | case Decl::Var: |
| 5901 | case Decl::VarTemplateSpecialization: |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 5902 | case Decl::VarTemplatePartialSpecialization: |
| 5903 | case Decl::Decomposition: { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5904 | // Ask the variable if it has a definition. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5905 | if (const VarDecl *Def = cast<VarDecl>(D)->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5906 | return MakeCXCursor(Def, TU); |
| 5907 | return clang_getNullCursor(); |
| 5908 | } |
| 5909 | |
| 5910 | case Decl::FunctionTemplate: { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5911 | const FunctionDecl *Def = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5912 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) |
| 5913 | return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU); |
| 5914 | return clang_getNullCursor(); |
| 5915 | } |
| 5916 | |
| 5917 | case Decl::ClassTemplate: { |
| 5918 | if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() |
| 5919 | ->getDefinition()) |
| 5920 | return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), |
| 5921 | TU); |
| 5922 | return clang_getNullCursor(); |
| 5923 | } |
| 5924 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5925 | case Decl::VarTemplate: { |
| 5926 | if (VarDecl *Def = |
| 5927 | cast<VarTemplateDecl>(D)->getTemplatedDecl()->getDefinition()) |
| 5928 | return MakeCXCursor(cast<VarDecl>(Def)->getDescribedVarTemplate(), TU); |
| 5929 | return clang_getNullCursor(); |
| 5930 | } |
| 5931 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5932 | case Decl::Using: |
| 5933 | return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), |
| 5934 | D->getLocation(), TU); |
| 5935 | |
| 5936 | case Decl::UsingShadow: |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 5937 | case Decl::ConstructorUsingShadow: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5938 | return clang_getCursorDefinition( |
| 5939 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), |
| 5940 | TU)); |
| 5941 | |
| 5942 | case Decl::ObjCMethod: { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5943 | const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5944 | if (Method->isThisDeclarationADefinition()) |
| 5945 | return C; |
| 5946 | |
| 5947 | // Dig out the method definition in the associated |
| 5948 | // @implementation, if we have it. |
| 5949 | // FIXME: The ASTs should make finding the definition easier. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5950 | if (const ObjCInterfaceDecl *Class |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5951 | = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) |
| 5952 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) |
| 5953 | if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), |
| 5954 | Method->isInstanceMethod())) |
| 5955 | if (Def->isThisDeclarationADefinition()) |
| 5956 | return MakeCXCursor(Def, TU); |
| 5957 | |
| 5958 | return clang_getNullCursor(); |
| 5959 | } |
| 5960 | |
| 5961 | case Decl::ObjCCategory: |
| 5962 | if (ObjCCategoryImplDecl *Impl |
| 5963 | = cast<ObjCCategoryDecl>(D)->getImplementation()) |
| 5964 | return MakeCXCursor(Impl, TU); |
| 5965 | return clang_getNullCursor(); |
| 5966 | |
| 5967 | case Decl::ObjCProtocol: |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5968 | if (const ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(D)->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5969 | return MakeCXCursor(Def, TU); |
| 5970 | return clang_getNullCursor(); |
| 5971 | |
| 5972 | case Decl::ObjCInterface: { |
| 5973 | // There are two notions of a "definition" for an Objective-C |
| 5974 | // class: the interface and its implementation. When we resolved a |
| 5975 | // reference to an Objective-C class, produce the @interface as |
| 5976 | // the definition; when we were provided with the interface, |
| 5977 | // produce the @implementation as the definition. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5978 | const ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5979 | if (WasReference) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5980 | if (const ObjCInterfaceDecl *Def = IFace->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5981 | return MakeCXCursor(Def, TU); |
| 5982 | } else if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
| 5983 | return MakeCXCursor(Impl, TU); |
| 5984 | return clang_getNullCursor(); |
| 5985 | } |
| 5986 | |
| 5987 | case Decl::ObjCProperty: |
| 5988 | // FIXME: We don't really know where to find the |
| 5989 | // ObjCPropertyImplDecls that implement this property. |
| 5990 | return clang_getNullCursor(); |
| 5991 | |
| 5992 | case Decl::ObjCCompatibleAlias: |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5993 | if (const ObjCInterfaceDecl *Class |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5994 | = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5995 | if (const ObjCInterfaceDecl *Def = Class->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5996 | return MakeCXCursor(Def, TU); |
| 5997 | |
| 5998 | return clang_getNullCursor(); |
| 5999 | |
| 6000 | case Decl::Friend: |
| 6001 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) |
| 6002 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
| 6003 | return clang_getNullCursor(); |
| 6004 | |
| 6005 | case Decl::FriendTemplate: |
| 6006 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) |
| 6007 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
| 6008 | return clang_getNullCursor(); |
| 6009 | } |
| 6010 | |
| 6011 | return clang_getNullCursor(); |
| 6012 | } |
| 6013 | |
| 6014 | unsigned clang_isCursorDefinition(CXCursor C) { |
| 6015 | if (!clang_isDeclaration(C.kind)) |
| 6016 | return 0; |
| 6017 | |
| 6018 | return clang_getCursorDefinition(C) == C; |
| 6019 | } |
| 6020 | |
| 6021 | CXCursor clang_getCanonicalCursor(CXCursor C) { |
| 6022 | if (!clang_isDeclaration(C.kind)) |
| 6023 | return C; |
| 6024 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6025 | if (const Decl *D = getCursorDecl(C)) { |
| 6026 | if (const ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6027 | if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl()) |
| 6028 | return MakeCXCursor(CatD, getCursorTU(C)); |
| 6029 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6030 | if (const ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) |
| 6031 | if (const ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6032 | return MakeCXCursor(IFD, getCursorTU(C)); |
| 6033 | |
| 6034 | return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C)); |
| 6035 | } |
| 6036 | |
| 6037 | return C; |
| 6038 | } |
| 6039 | |
| 6040 | int clang_Cursor_getObjCSelectorIndex(CXCursor cursor) { |
| 6041 | return cxcursor::getSelectorIdentifierIndexAndLoc(cursor).first; |
| 6042 | } |
| 6043 | |
| 6044 | unsigned clang_getNumOverloadedDecls(CXCursor C) { |
| 6045 | if (C.kind != CXCursor_OverloadedDeclRef) |
| 6046 | return 0; |
| 6047 | |
| 6048 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6049 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6050 | return E->getNumDecls(); |
| 6051 | |
| 6052 | if (OverloadedTemplateStorage *S |
| 6053 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 6054 | return S->size(); |
| 6055 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6056 | const Decl *D = Storage.get<const Decl *>(); |
| 6057 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6058 | return Using->shadow_size(); |
| 6059 | |
| 6060 | return 0; |
| 6061 | } |
| 6062 | |
| 6063 | CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { |
| 6064 | if (cursor.kind != CXCursor_OverloadedDeclRef) |
| 6065 | return clang_getNullCursor(); |
| 6066 | |
| 6067 | if (index >= clang_getNumOverloadedDecls(cursor)) |
| 6068 | return clang_getNullCursor(); |
| 6069 | |
| 6070 | CXTranslationUnit TU = getCursorTU(cursor); |
| 6071 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6072 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6073 | return MakeCXCursor(E->decls_begin()[index], TU); |
| 6074 | |
| 6075 | if (OverloadedTemplateStorage *S |
| 6076 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 6077 | return MakeCXCursor(S->begin()[index], TU); |
| 6078 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6079 | const Decl *D = Storage.get<const Decl *>(); |
| 6080 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6081 | // FIXME: This is, unfortunately, linear time. |
| 6082 | UsingDecl::shadow_iterator Pos = Using->shadow_begin(); |
| 6083 | std::advance(Pos, index); |
| 6084 | return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU); |
| 6085 | } |
| 6086 | |
| 6087 | return clang_getNullCursor(); |
| 6088 | } |
| 6089 | |
| 6090 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
| 6091 | const char **startBuf, |
| 6092 | const char **endBuf, |
| 6093 | unsigned *startLine, |
| 6094 | unsigned *startColumn, |
| 6095 | unsigned *endLine, |
| 6096 | unsigned *endColumn) { |
| 6097 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6098 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(getCursorDecl(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6099 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
| 6100 | |
| 6101 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 6102 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 6103 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 6104 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 6105 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 6106 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 6107 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 6108 | } |
| 6109 | |
| 6110 | |
| 6111 | CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags, |
| 6112 | unsigned PieceIndex) { |
| 6113 | RefNamePieces Pieces; |
| 6114 | |
| 6115 | switch (C.kind) { |
| 6116 | case CXCursor_MemberRefExpr: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6117 | if (const MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6118 | Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(), |
| 6119 | E->getQualifierLoc().getSourceRange()); |
| 6120 | break; |
| 6121 | |
| 6122 | case CXCursor_DeclRefExpr: |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 6123 | if (const DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C))) { |
| 6124 | SourceRange TemplateArgLoc(E->getLAngleLoc(), E->getRAngleLoc()); |
| 6125 | Pieces = |
| 6126 | buildPieces(NameFlags, false, E->getNameInfo(), |
| 6127 | E->getQualifierLoc().getSourceRange(), &TemplateArgLoc); |
| 6128 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6129 | break; |
| 6130 | |
| 6131 | case CXCursor_CallExpr: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6132 | if (const CXXOperatorCallExpr *OCE = |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6133 | dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6134 | const Expr *Callee = OCE->getCallee(); |
| 6135 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6136 | Callee = ICE->getSubExpr(); |
| 6137 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6138 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6139 | Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(), |
| 6140 | DRE->getQualifierLoc().getSourceRange()); |
| 6141 | } |
| 6142 | break; |
| 6143 | |
| 6144 | default: |
| 6145 | break; |
| 6146 | } |
| 6147 | |
| 6148 | if (Pieces.empty()) { |
| 6149 | if (PieceIndex == 0) |
| 6150 | return clang_getCursorExtent(C); |
| 6151 | } else if (PieceIndex < Pieces.size()) { |
| 6152 | SourceRange R = Pieces[PieceIndex]; |
| 6153 | if (R.isValid()) |
| 6154 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 6155 | } |
| 6156 | |
| 6157 | return clang_getNullRange(); |
| 6158 | } |
| 6159 | |
| 6160 | void clang_enableStackTraces(void) { |
Richard Smith | dfed58a | 2016-06-09 00:53:41 +0000 | [diff] [blame] | 6161 | // FIXME: Provide an argv0 here so we can find llvm-symbolizer. |
| 6162 | llvm::sys::PrintStackTraceOnErrorSignal(StringRef()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6163 | } |
| 6164 | |
| 6165 | void clang_executeOnThread(void (*fn)(void*), void *user_data, |
| 6166 | unsigned stack_size) { |
| 6167 | llvm::llvm_execute_on_thread(fn, user_data, stack_size); |
| 6168 | } |
| 6169 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6170 | //===----------------------------------------------------------------------===// |
| 6171 | // Token-based Operations. |
| 6172 | //===----------------------------------------------------------------------===// |
| 6173 | |
| 6174 | /* CXToken layout: |
| 6175 | * int_data[0]: a CXTokenKind |
| 6176 | * int_data[1]: starting token location |
| 6177 | * int_data[2]: token length |
| 6178 | * int_data[3]: reserved |
| 6179 | * ptr_data: for identifiers and keywords, an IdentifierInfo*. |
| 6180 | * otherwise unused. |
| 6181 | */ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6182 | CXTokenKind clang_getTokenKind(CXToken CXTok) { |
| 6183 | return static_cast<CXTokenKind>(CXTok.int_data[0]); |
| 6184 | } |
| 6185 | |
| 6186 | CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { |
| 6187 | switch (clang_getTokenKind(CXTok)) { |
| 6188 | case CXToken_Identifier: |
| 6189 | case CXToken_Keyword: |
| 6190 | // We know we have an IdentifierInfo*, so use that. |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 6191 | return cxstring::createRef(static_cast<IdentifierInfo *>(CXTok.ptr_data) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6192 | ->getNameStart()); |
| 6193 | |
| 6194 | case CXToken_Literal: { |
| 6195 | // We have stashed the starting pointer in the ptr_data field. Use it. |
| 6196 | const char *Text = static_cast<const char *>(CXTok.ptr_data); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 6197 | return cxstring::createDup(StringRef(Text, CXTok.int_data[2])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6198 | } |
| 6199 | |
| 6200 | case CXToken_Punctuation: |
| 6201 | case CXToken_Comment: |
| 6202 | break; |
| 6203 | } |
| 6204 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6205 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6206 | LOG_BAD_TU(TU); |
| 6207 | return cxstring::createEmpty(); |
| 6208 | } |
| 6209 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6210 | // We have to find the starting buffer pointer the hard way, by |
| 6211 | // deconstructing the source location. |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6212 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6213 | if (!CXXUnit) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 6214 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6215 | |
| 6216 | SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); |
| 6217 | std::pair<FileID, unsigned> LocInfo |
| 6218 | = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc); |
| 6219 | bool Invalid = false; |
| 6220 | StringRef Buffer |
| 6221 | = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); |
| 6222 | if (Invalid) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 6223 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6224 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 6225 | return cxstring::createDup(Buffer.substr(LocInfo.second, CXTok.int_data[2])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6226 | } |
| 6227 | |
| 6228 | CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6229 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6230 | LOG_BAD_TU(TU); |
| 6231 | return clang_getNullLocation(); |
| 6232 | } |
| 6233 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6234 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6235 | if (!CXXUnit) |
| 6236 | return clang_getNullLocation(); |
| 6237 | |
| 6238 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), |
| 6239 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 6240 | } |
| 6241 | |
| 6242 | CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6243 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6244 | LOG_BAD_TU(TU); |
| 6245 | return clang_getNullRange(); |
| 6246 | } |
| 6247 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6248 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6249 | if (!CXXUnit) |
| 6250 | return clang_getNullRange(); |
| 6251 | |
| 6252 | return cxloc::translateSourceRange(CXXUnit->getASTContext(), |
| 6253 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 6254 | } |
| 6255 | |
| 6256 | static void getTokens(ASTUnit *CXXUnit, SourceRange Range, |
| 6257 | SmallVectorImpl<CXToken> &CXTokens) { |
| 6258 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 6259 | std::pair<FileID, unsigned> BeginLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6260 | = SourceMgr.getDecomposedSpellingLoc(Range.getBegin()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6261 | std::pair<FileID, unsigned> EndLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6262 | = SourceMgr.getDecomposedSpellingLoc(Range.getEnd()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6263 | |
| 6264 | // Cannot tokenize across files. |
| 6265 | if (BeginLocInfo.first != EndLocInfo.first) |
| 6266 | return; |
| 6267 | |
| 6268 | // Create a lexer |
| 6269 | bool Invalid = false; |
| 6270 | StringRef Buffer |
| 6271 | = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 6272 | if (Invalid) |
| 6273 | return; |
| 6274 | |
| 6275 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 6276 | CXXUnit->getASTContext().getLangOpts(), |
| 6277 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); |
| 6278 | Lex.SetCommentRetentionState(true); |
| 6279 | |
| 6280 | // Lex tokens until we hit the end of the range. |
| 6281 | const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; |
| 6282 | Token Tok; |
| 6283 | bool previousWasAt = false; |
| 6284 | do { |
| 6285 | // Lex the next token |
| 6286 | Lex.LexFromRawLexer(Tok); |
| 6287 | if (Tok.is(tok::eof)) |
| 6288 | break; |
| 6289 | |
| 6290 | // Initialize the CXToken. |
| 6291 | CXToken CXTok; |
| 6292 | |
| 6293 | // - Common fields |
| 6294 | CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); |
| 6295 | CXTok.int_data[2] = Tok.getLength(); |
| 6296 | CXTok.int_data[3] = 0; |
| 6297 | |
| 6298 | // - Kind-specific fields |
| 6299 | if (Tok.isLiteral()) { |
| 6300 | CXTok.int_data[0] = CXToken_Literal; |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 6301 | CXTok.ptr_data = const_cast<char *>(Tok.getLiteralData()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6302 | } else if (Tok.is(tok::raw_identifier)) { |
| 6303 | // Lookup the identifier to determine whether we have a keyword. |
| 6304 | IdentifierInfo *II |
| 6305 | = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok); |
| 6306 | |
| 6307 | if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { |
| 6308 | CXTok.int_data[0] = CXToken_Keyword; |
| 6309 | } |
| 6310 | else { |
| 6311 | CXTok.int_data[0] = Tok.is(tok::identifier) |
| 6312 | ? CXToken_Identifier |
| 6313 | : CXToken_Keyword; |
| 6314 | } |
| 6315 | CXTok.ptr_data = II; |
| 6316 | } else if (Tok.is(tok::comment)) { |
| 6317 | CXTok.int_data[0] = CXToken_Comment; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6318 | CXTok.ptr_data = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6319 | } else { |
| 6320 | CXTok.int_data[0] = CXToken_Punctuation; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6321 | CXTok.ptr_data = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6322 | } |
| 6323 | CXTokens.push_back(CXTok); |
| 6324 | previousWasAt = Tok.is(tok::at); |
Argyrios Kyrtzidis | c7c6a07 | 2016-11-09 23:58:39 +0000 | [diff] [blame] | 6325 | } while (Lex.getBufferLocation() < EffectiveBufferEnd); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6326 | } |
| 6327 | |
| 6328 | void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, |
| 6329 | CXToken **Tokens, unsigned *NumTokens) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 6330 | LOG_FUNC_SECTION { |
| 6331 | *Log << TU << ' ' << Range; |
| 6332 | } |
| 6333 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6334 | if (Tokens) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6335 | *Tokens = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6336 | if (NumTokens) |
| 6337 | *NumTokens = 0; |
| 6338 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6339 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6340 | LOG_BAD_TU(TU); |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 6341 | return; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6342 | } |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 6343 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6344 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6345 | if (!CXXUnit || !Tokens || !NumTokens) |
| 6346 | return; |
| 6347 | |
| 6348 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 6349 | |
| 6350 | SourceRange R = cxloc::translateCXSourceRange(Range); |
| 6351 | if (R.isInvalid()) |
| 6352 | return; |
| 6353 | |
| 6354 | SmallVector<CXToken, 32> CXTokens; |
| 6355 | getTokens(CXXUnit, R, CXTokens); |
| 6356 | |
| 6357 | if (CXTokens.empty()) |
| 6358 | return; |
| 6359 | |
| 6360 | *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size()); |
| 6361 | memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); |
| 6362 | *NumTokens = CXTokens.size(); |
| 6363 | } |
| 6364 | |
| 6365 | void clang_disposeTokens(CXTranslationUnit TU, |
| 6366 | CXToken *Tokens, unsigned NumTokens) { |
| 6367 | free(Tokens); |
| 6368 | } |
| 6369 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6370 | //===----------------------------------------------------------------------===// |
| 6371 | // Token annotation APIs. |
| 6372 | //===----------------------------------------------------------------------===// |
| 6373 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6374 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 6375 | CXCursor parent, |
| 6376 | CXClientData client_data); |
| 6377 | static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor, |
| 6378 | CXClientData client_data); |
| 6379 | |
| 6380 | namespace { |
| 6381 | class AnnotateTokensWorker { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6382 | CXToken *Tokens; |
| 6383 | CXCursor *Cursors; |
| 6384 | unsigned NumTokens; |
| 6385 | unsigned TokIdx; |
| 6386 | unsigned PreprocessingTokIdx; |
| 6387 | CursorVisitor AnnotateVis; |
| 6388 | SourceManager &SrcMgr; |
| 6389 | bool HasContextSensitiveKeywords; |
| 6390 | |
| 6391 | struct PostChildrenInfo { |
| 6392 | CXCursor Cursor; |
| 6393 | SourceRange CursorRange; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6394 | unsigned BeforeReachingCursorIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6395 | unsigned BeforeChildrenTokenIdx; |
| 6396 | }; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6397 | SmallVector<PostChildrenInfo, 8> PostChildrenInfos; |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6398 | |
| 6399 | CXToken &getTok(unsigned Idx) { |
| 6400 | assert(Idx < NumTokens); |
| 6401 | return Tokens[Idx]; |
| 6402 | } |
| 6403 | const CXToken &getTok(unsigned Idx) const { |
| 6404 | assert(Idx < NumTokens); |
| 6405 | return Tokens[Idx]; |
| 6406 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6407 | bool MoreTokens() const { return TokIdx < NumTokens; } |
| 6408 | unsigned NextToken() const { return TokIdx; } |
| 6409 | void AdvanceToken() { ++TokIdx; } |
| 6410 | SourceLocation GetTokenLoc(unsigned tokI) { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6411 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6412 | } |
| 6413 | bool isFunctionMacroToken(unsigned tokI) const { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6414 | return getTok(tokI).int_data[3] != 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6415 | } |
| 6416 | SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6417 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[3]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6418 | } |
| 6419 | |
| 6420 | void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange); |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6421 | bool annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6422 | SourceRange); |
| 6423 | |
| 6424 | public: |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6425 | AnnotateTokensWorker(CXToken *tokens, CXCursor *cursors, unsigned numTokens, |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6426 | CXTranslationUnit TU, SourceRange RegionOfInterest) |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6427 | : Tokens(tokens), Cursors(cursors), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6428 | NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0), |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6429 | AnnotateVis(TU, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6430 | AnnotateTokensVisitor, this, |
| 6431 | /*VisitPreprocessorLast=*/true, |
| 6432 | /*VisitIncludedEntities=*/false, |
| 6433 | RegionOfInterest, |
| 6434 | /*VisitDeclsOnly=*/false, |
| 6435 | AnnotateTokensPostChildrenVisitor), |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6436 | SrcMgr(cxtu::getASTUnit(TU)->getSourceManager()), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6437 | HasContextSensitiveKeywords(false) { } |
| 6438 | |
| 6439 | void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } |
| 6440 | enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); |
| 6441 | bool postVisitChildren(CXCursor cursor); |
| 6442 | void AnnotateTokens(); |
| 6443 | |
| 6444 | /// \brief Determine whether the annotator saw any cursors that have |
| 6445 | /// context-sensitive keywords. |
| 6446 | bool hasContextSensitiveKeywords() const { |
| 6447 | return HasContextSensitiveKeywords; |
| 6448 | } |
| 6449 | |
| 6450 | ~AnnotateTokensWorker() { |
| 6451 | assert(PostChildrenInfos.empty()); |
| 6452 | } |
| 6453 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6454 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6455 | |
| 6456 | void AnnotateTokensWorker::AnnotateTokens() { |
| 6457 | // Walk the AST within the region of interest, annotating tokens |
| 6458 | // along the way. |
| 6459 | AnnotateVis.visitFileRegion(); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6460 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6461 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6462 | static inline void updateCursorAnnotation(CXCursor &Cursor, |
| 6463 | const CXCursor &updateC) { |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6464 | if (clang_isInvalid(updateC.kind) || !clang_isInvalid(Cursor.kind)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6465 | return; |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6466 | Cursor = updateC; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6467 | } |
| 6468 | |
| 6469 | /// \brief It annotates and advances tokens with a cursor until the comparison |
| 6470 | //// between the cursor location and the source range is the same as |
| 6471 | /// \arg compResult. |
| 6472 | /// |
| 6473 | /// Pass RangeBefore to annotate tokens with a cursor until a range is reached. |
| 6474 | /// Pass RangeOverlap to annotate tokens inside a range. |
| 6475 | void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC, |
| 6476 | RangeComparisonResult compResult, |
| 6477 | SourceRange range) { |
| 6478 | while (MoreTokens()) { |
| 6479 | const unsigned I = NextToken(); |
| 6480 | if (isFunctionMacroToken(I)) |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6481 | if (!annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range)) |
| 6482 | return; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6483 | |
| 6484 | SourceLocation TokLoc = GetTokenLoc(I); |
| 6485 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6486 | updateCursorAnnotation(Cursors[I], updateC); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6487 | AdvanceToken(); |
| 6488 | continue; |
| 6489 | } |
| 6490 | break; |
| 6491 | } |
| 6492 | } |
| 6493 | |
| 6494 | /// \brief Special annotation handling for macro argument tokens. |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6495 | /// \returns true if it advanced beyond all macro tokens, false otherwise. |
| 6496 | bool AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens( |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6497 | CXCursor updateC, |
| 6498 | RangeComparisonResult compResult, |
| 6499 | SourceRange range) { |
| 6500 | assert(MoreTokens()); |
| 6501 | assert(isFunctionMacroToken(NextToken()) && |
| 6502 | "Should be called only for macro arg tokens"); |
| 6503 | |
| 6504 | // This works differently than annotateAndAdvanceTokens; because expanded |
| 6505 | // macro arguments can have arbitrary translation-unit source order, we do not |
| 6506 | // advance the token index one by one until a token fails the range test. |
| 6507 | // We only advance once past all of the macro arg tokens if all of them |
| 6508 | // pass the range test. If one of them fails we keep the token index pointing |
| 6509 | // at the start of the macro arg tokens so that the failing token will be |
| 6510 | // annotated by a subsequent annotation try. |
| 6511 | |
| 6512 | bool atLeastOneCompFail = false; |
| 6513 | |
| 6514 | unsigned I = NextToken(); |
| 6515 | for (; I < NumTokens && isFunctionMacroToken(I); ++I) { |
| 6516 | SourceLocation TokLoc = getFunctionMacroTokenLoc(I); |
| 6517 | if (TokLoc.isFileID()) |
| 6518 | continue; // not macro arg token, it's parens or comma. |
| 6519 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 6520 | if (clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 6521 | Cursors[I] = updateC; |
| 6522 | } else |
| 6523 | atLeastOneCompFail = true; |
| 6524 | } |
| 6525 | |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6526 | if (atLeastOneCompFail) |
| 6527 | return false; |
| 6528 | |
| 6529 | TokIdx = I; // All of the tokens were handled, advance beyond all of them. |
| 6530 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6531 | } |
| 6532 | |
| 6533 | enum CXChildVisitResult |
| 6534 | AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6535 | SourceRange cursorRange = getRawCursorExtent(cursor); |
| 6536 | if (cursorRange.isInvalid()) |
| 6537 | return CXChildVisit_Recurse; |
| 6538 | |
| 6539 | if (!HasContextSensitiveKeywords) { |
| 6540 | // Objective-C properties can have context-sensitive keywords. |
| 6541 | if (cursor.kind == CXCursor_ObjCPropertyDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6542 | if (const ObjCPropertyDecl *Property |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6543 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor))) |
| 6544 | HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0; |
| 6545 | } |
| 6546 | // Objective-C methods can have context-sensitive keywords. |
| 6547 | else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl || |
| 6548 | cursor.kind == CXCursor_ObjCClassMethodDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6549 | if (const ObjCMethodDecl *Method |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6550 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 6551 | if (Method->getObjCDeclQualifier()) |
| 6552 | HasContextSensitiveKeywords = true; |
| 6553 | else { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 6554 | for (const auto *P : Method->parameters()) { |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 6555 | if (P->getObjCDeclQualifier()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6556 | HasContextSensitiveKeywords = true; |
| 6557 | break; |
| 6558 | } |
| 6559 | } |
| 6560 | } |
| 6561 | } |
| 6562 | } |
| 6563 | // C++ methods can have context-sensitive keywords. |
| 6564 | else if (cursor.kind == CXCursor_CXXMethod) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6565 | if (const CXXMethodDecl *Method |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6566 | = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) { |
| 6567 | if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>()) |
| 6568 | HasContextSensitiveKeywords = true; |
| 6569 | } |
| 6570 | } |
| 6571 | // C++ classes can have context-sensitive keywords. |
| 6572 | else if (cursor.kind == CXCursor_StructDecl || |
| 6573 | cursor.kind == CXCursor_ClassDecl || |
| 6574 | cursor.kind == CXCursor_ClassTemplate || |
| 6575 | cursor.kind == CXCursor_ClassTemplatePartialSpecialization) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6576 | if (const Decl *D = getCursorDecl(cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6577 | if (D->hasAttr<FinalAttr>()) |
| 6578 | HasContextSensitiveKeywords = true; |
| 6579 | } |
| 6580 | } |
Argyrios Kyrtzidis | 990b386 | 2013-06-04 18:24:30 +0000 | [diff] [blame] | 6581 | |
| 6582 | // Don't override a property annotation with its getter/setter method. |
| 6583 | if (cursor.kind == CXCursor_ObjCInstanceMethodDecl && |
| 6584 | parent.kind == CXCursor_ObjCPropertyDecl) |
| 6585 | return CXChildVisit_Continue; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6586 | |
| 6587 | if (clang_isPreprocessing(cursor.kind)) { |
| 6588 | // Items in the preprocessing record are kept separate from items in |
| 6589 | // declarations, so we keep a separate token index. |
| 6590 | unsigned SavedTokIdx = TokIdx; |
| 6591 | TokIdx = PreprocessingTokIdx; |
| 6592 | |
| 6593 | // Skip tokens up until we catch up to the beginning of the preprocessing |
| 6594 | // entry. |
| 6595 | while (MoreTokens()) { |
| 6596 | const unsigned I = NextToken(); |
| 6597 | SourceLocation TokLoc = GetTokenLoc(I); |
| 6598 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 6599 | case RangeBefore: |
| 6600 | AdvanceToken(); |
| 6601 | continue; |
| 6602 | case RangeAfter: |
| 6603 | case RangeOverlap: |
| 6604 | break; |
| 6605 | } |
| 6606 | break; |
| 6607 | } |
| 6608 | |
| 6609 | // Look at all of the tokens within this range. |
| 6610 | while (MoreTokens()) { |
| 6611 | const unsigned I = NextToken(); |
| 6612 | SourceLocation TokLoc = GetTokenLoc(I); |
| 6613 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 6614 | case RangeBefore: |
| 6615 | llvm_unreachable("Infeasible"); |
| 6616 | case RangeAfter: |
| 6617 | break; |
| 6618 | case RangeOverlap: |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6619 | // For macro expansions, just note where the beginning of the macro |
| 6620 | // expansion occurs. |
| 6621 | if (cursor.kind == CXCursor_MacroExpansion) { |
| 6622 | if (TokLoc == cursorRange.getBegin()) |
| 6623 | Cursors[I] = cursor; |
| 6624 | AdvanceToken(); |
| 6625 | break; |
| 6626 | } |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6627 | // We may have already annotated macro names inside macro definitions. |
| 6628 | if (Cursors[I].kind != CXCursor_MacroExpansion) |
| 6629 | Cursors[I] = cursor; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6630 | AdvanceToken(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6631 | continue; |
| 6632 | } |
| 6633 | break; |
| 6634 | } |
| 6635 | |
| 6636 | // Save the preprocessing token index; restore the non-preprocessing |
| 6637 | // token index. |
| 6638 | PreprocessingTokIdx = TokIdx; |
| 6639 | TokIdx = SavedTokIdx; |
| 6640 | return CXChildVisit_Recurse; |
| 6641 | } |
| 6642 | |
| 6643 | if (cursorRange.isInvalid()) |
| 6644 | return CXChildVisit_Continue; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6645 | |
| 6646 | unsigned BeforeReachingCursorIdx = NextToken(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6647 | const enum CXCursorKind cursorK = clang_getCursorKind(cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6648 | const enum CXCursorKind K = clang_getCursorKind(parent); |
| 6649 | const CXCursor updateC = |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6650 | (clang_isInvalid(K) || K == CXCursor_TranslationUnit || |
| 6651 | // Attributes are annotated out-of-order, skip tokens until we reach it. |
| 6652 | clang_isAttribute(cursor.kind)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6653 | ? clang_getNullCursor() : parent; |
| 6654 | |
| 6655 | annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange); |
| 6656 | |
| 6657 | // Avoid having the cursor of an expression "overwrite" the annotation of the |
| 6658 | // variable declaration that it belongs to. |
| 6659 | // This can happen for C++ constructor expressions whose range generally |
| 6660 | // include the variable declaration, e.g.: |
| 6661 | // MyCXXClass foo; // Make sure we don't annotate 'foo' as a CallExpr cursor. |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6662 | if (clang_isExpression(cursorK) && MoreTokens()) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6663 | const Expr *E = getCursorExpr(cursor); |
Dmitri Gribenko | a169118 | 2013-01-26 18:12:08 +0000 | [diff] [blame] | 6664 | if (const Decl *D = getCursorParentDecl(cursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6665 | const unsigned I = NextToken(); |
| 6666 | if (E->getLocStart().isValid() && D->getLocation().isValid() && |
| 6667 | E->getLocStart() == D->getLocation() && |
| 6668 | E->getLocStart() == GetTokenLoc(I)) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6669 | updateCursorAnnotation(Cursors[I], updateC); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6670 | AdvanceToken(); |
| 6671 | } |
| 6672 | } |
| 6673 | } |
| 6674 | |
| 6675 | // Before recursing into the children keep some state that we are going |
| 6676 | // to use in the AnnotateTokensWorker::postVisitChildren callback to do some |
| 6677 | // extra work after the child nodes are visited. |
| 6678 | // Note that we don't call VisitChildren here to avoid traversing statements |
| 6679 | // code-recursively which can blow the stack. |
| 6680 | |
| 6681 | PostChildrenInfo Info; |
| 6682 | Info.Cursor = cursor; |
| 6683 | Info.CursorRange = cursorRange; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6684 | Info.BeforeReachingCursorIdx = BeforeReachingCursorIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6685 | Info.BeforeChildrenTokenIdx = NextToken(); |
| 6686 | PostChildrenInfos.push_back(Info); |
| 6687 | |
| 6688 | return CXChildVisit_Recurse; |
| 6689 | } |
| 6690 | |
| 6691 | bool AnnotateTokensWorker::postVisitChildren(CXCursor cursor) { |
| 6692 | if (PostChildrenInfos.empty()) |
| 6693 | return false; |
| 6694 | const PostChildrenInfo &Info = PostChildrenInfos.back(); |
| 6695 | if (!clang_equalCursors(Info.Cursor, cursor)) |
| 6696 | return false; |
| 6697 | |
| 6698 | const unsigned BeforeChildren = Info.BeforeChildrenTokenIdx; |
| 6699 | const unsigned AfterChildren = NextToken(); |
| 6700 | SourceRange cursorRange = Info.CursorRange; |
| 6701 | |
| 6702 | // Scan the tokens that are at the end of the cursor, but are not captured |
| 6703 | // but the child cursors. |
| 6704 | annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange); |
| 6705 | |
| 6706 | // Scan the tokens that are at the beginning of the cursor, but are not |
| 6707 | // capture by the child cursors. |
| 6708 | for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { |
| 6709 | if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 6710 | break; |
| 6711 | |
| 6712 | Cursors[I] = cursor; |
| 6713 | } |
| 6714 | |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6715 | // Attributes are annotated out-of-order, rewind TokIdx to when we first |
| 6716 | // encountered the attribute cursor. |
| 6717 | if (clang_isAttribute(cursor.kind)) |
| 6718 | TokIdx = Info.BeforeReachingCursorIdx; |
| 6719 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6720 | PostChildrenInfos.pop_back(); |
| 6721 | return false; |
| 6722 | } |
| 6723 | |
| 6724 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 6725 | CXCursor parent, |
| 6726 | CXClientData client_data) { |
| 6727 | return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); |
| 6728 | } |
| 6729 | |
| 6730 | static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor, |
| 6731 | CXClientData client_data) { |
| 6732 | return static_cast<AnnotateTokensWorker*>(client_data)-> |
| 6733 | postVisitChildren(cursor); |
| 6734 | } |
| 6735 | |
| 6736 | namespace { |
| 6737 | |
| 6738 | /// \brief Uses the macro expansions in the preprocessing record to find |
| 6739 | /// and mark tokens that are macro arguments. This info is used by the |
| 6740 | /// AnnotateTokensWorker. |
| 6741 | class MarkMacroArgTokensVisitor { |
| 6742 | SourceManager &SM; |
| 6743 | CXToken *Tokens; |
| 6744 | unsigned NumTokens; |
| 6745 | unsigned CurIdx; |
| 6746 | |
| 6747 | public: |
| 6748 | MarkMacroArgTokensVisitor(SourceManager &SM, |
| 6749 | CXToken *tokens, unsigned numTokens) |
| 6750 | : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { } |
| 6751 | |
| 6752 | CXChildVisitResult visit(CXCursor cursor, CXCursor parent) { |
| 6753 | if (cursor.kind != CXCursor_MacroExpansion) |
| 6754 | return CXChildVisit_Continue; |
| 6755 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 6756 | SourceRange macroRange = getCursorMacroExpansion(cursor).getSourceRange(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6757 | if (macroRange.getBegin() == macroRange.getEnd()) |
| 6758 | return CXChildVisit_Continue; // it's not a function macro. |
| 6759 | |
| 6760 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 6761 | if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx), |
| 6762 | macroRange.getBegin())) |
| 6763 | break; |
| 6764 | } |
| 6765 | |
| 6766 | if (CurIdx == NumTokens) |
| 6767 | return CXChildVisit_Break; |
| 6768 | |
| 6769 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 6770 | SourceLocation tokLoc = getTokenLoc(CurIdx); |
| 6771 | if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd())) |
| 6772 | break; |
| 6773 | |
| 6774 | setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc)); |
| 6775 | } |
| 6776 | |
| 6777 | if (CurIdx == NumTokens) |
| 6778 | return CXChildVisit_Break; |
| 6779 | |
| 6780 | return CXChildVisit_Continue; |
| 6781 | } |
| 6782 | |
| 6783 | private: |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6784 | CXToken &getTok(unsigned Idx) { |
| 6785 | assert(Idx < NumTokens); |
| 6786 | return Tokens[Idx]; |
| 6787 | } |
| 6788 | const CXToken &getTok(unsigned Idx) const { |
| 6789 | assert(Idx < NumTokens); |
| 6790 | return Tokens[Idx]; |
| 6791 | } |
| 6792 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6793 | SourceLocation getTokenLoc(unsigned tokI) { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6794 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6795 | } |
| 6796 | |
| 6797 | void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) { |
| 6798 | // The third field is reserved and currently not used. Use it here |
| 6799 | // to mark macro arg expanded tokens with their expanded locations. |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6800 | getTok(tokI).int_data[3] = loc.getRawEncoding(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6801 | } |
| 6802 | }; |
| 6803 | |
| 6804 | } // end anonymous namespace |
| 6805 | |
| 6806 | static CXChildVisitResult |
| 6807 | MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent, |
| 6808 | CXClientData client_data) { |
| 6809 | return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor, |
| 6810 | parent); |
| 6811 | } |
| 6812 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6813 | /// \brief Used by \c annotatePreprocessorTokens. |
| 6814 | /// \returns true if lexing was finished, false otherwise. |
| 6815 | static bool lexNext(Lexer &Lex, Token &Tok, |
| 6816 | unsigned &NextIdx, unsigned NumTokens) { |
| 6817 | if (NextIdx >= NumTokens) |
| 6818 | return true; |
| 6819 | |
| 6820 | ++NextIdx; |
| 6821 | Lex.LexFromRawLexer(Tok); |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 6822 | return Tok.is(tok::eof); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6823 | } |
| 6824 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6825 | static void annotatePreprocessorTokens(CXTranslationUnit TU, |
| 6826 | SourceRange RegionOfInterest, |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6827 | CXCursor *Cursors, |
| 6828 | CXToken *Tokens, |
| 6829 | unsigned NumTokens) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6830 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6831 | |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6832 | Preprocessor &PP = CXXUnit->getPreprocessor(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6833 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 6834 | std::pair<FileID, unsigned> BeginLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6835 | = SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getBegin()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6836 | std::pair<FileID, unsigned> EndLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6837 | = SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getEnd()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6838 | |
| 6839 | if (BeginLocInfo.first != EndLocInfo.first) |
| 6840 | return; |
| 6841 | |
| 6842 | StringRef Buffer; |
| 6843 | bool Invalid = false; |
| 6844 | Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 6845 | if (Buffer.empty() || Invalid) |
| 6846 | return; |
| 6847 | |
| 6848 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 6849 | CXXUnit->getASTContext().getLangOpts(), |
| 6850 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, |
| 6851 | Buffer.end()); |
| 6852 | Lex.SetCommentRetentionState(true); |
| 6853 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6854 | unsigned NextIdx = 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6855 | // Lex tokens in raw mode until we hit the end of the range, to avoid |
| 6856 | // entering #includes or expanding macros. |
| 6857 | while (true) { |
| 6858 | Token Tok; |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6859 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 6860 | break; |
| 6861 | unsigned TokIdx = NextIdx-1; |
| 6862 | assert(Tok.getLocation() == |
| 6863 | SourceLocation::getFromRawEncoding(Tokens[TokIdx].int_data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6864 | |
| 6865 | reprocess: |
| 6866 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6867 | // We have found a preprocessing directive. Annotate the tokens |
| 6868 | // appropriately. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6869 | // |
| 6870 | // FIXME: Some simple tests here could identify macro definitions and |
| 6871 | // #undefs, to provide specific cursor kinds for those. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6872 | |
| 6873 | SourceLocation BeginLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6874 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 6875 | break; |
| 6876 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6877 | MacroInfo *MI = nullptr; |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 6878 | if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "define") { |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6879 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 6880 | break; |
| 6881 | |
| 6882 | if (Tok.is(tok::raw_identifier)) { |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 6883 | IdentifierInfo &II = |
| 6884 | PP.getIdentifierTable().get(Tok.getRawIdentifier()); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6885 | SourceLocation MappedTokLoc = |
| 6886 | CXXUnit->mapLocationToPreamble(Tok.getLocation()); |
| 6887 | MI = getMacroInfo(II, MappedTokLoc, TU); |
| 6888 | } |
| 6889 | } |
| 6890 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6891 | bool finished = false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6892 | do { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6893 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) { |
| 6894 | finished = true; |
| 6895 | break; |
| 6896 | } |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6897 | // If we are in a macro definition, check if the token was ever a |
| 6898 | // macro name and annotate it if that's the case. |
| 6899 | if (MI) { |
| 6900 | SourceLocation SaveLoc = Tok.getLocation(); |
| 6901 | Tok.setLocation(CXXUnit->mapLocationToPreamble(SaveLoc)); |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 6902 | MacroDefinitionRecord *MacroDef = |
| 6903 | checkForMacroInMacroDefinition(MI, Tok, TU); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6904 | Tok.setLocation(SaveLoc); |
| 6905 | if (MacroDef) |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 6906 | Cursors[NextIdx - 1] = |
| 6907 | MakeMacroExpansionCursor(MacroDef, Tok.getLocation(), TU); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6908 | } |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6909 | } while (!Tok.isAtStartOfLine()); |
| 6910 | |
| 6911 | unsigned LastIdx = finished ? NextIdx-1 : NextIdx-2; |
| 6912 | assert(TokIdx <= LastIdx); |
| 6913 | SourceLocation EndLoc = |
| 6914 | SourceLocation::getFromRawEncoding(Tokens[LastIdx].int_data[1]); |
| 6915 | CXCursor Cursor = |
| 6916 | MakePreprocessingDirectiveCursor(SourceRange(BeginLoc, EndLoc), TU); |
| 6917 | |
| 6918 | for (; TokIdx <= LastIdx; ++TokIdx) |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6919 | updateCursorAnnotation(Cursors[TokIdx], Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6920 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6921 | if (finished) |
| 6922 | break; |
| 6923 | goto reprocess; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6924 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6925 | } |
| 6926 | } |
| 6927 | |
| 6928 | // This gets run a separate thread to avoid stack blowout. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 6929 | static void clang_annotateTokensImpl(CXTranslationUnit TU, ASTUnit *CXXUnit, |
| 6930 | CXToken *Tokens, unsigned NumTokens, |
| 6931 | CXCursor *Cursors) { |
Dmitri Gribenko | 183436e | 2013-01-26 21:49:50 +0000 | [diff] [blame] | 6932 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6933 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) |
| 6934 | setThreadBackgroundPriority(); |
| 6935 | |
| 6936 | // Determine the region of interest, which contains all of the tokens. |
| 6937 | SourceRange RegionOfInterest; |
| 6938 | RegionOfInterest.setBegin( |
| 6939 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0]))); |
| 6940 | RegionOfInterest.setEnd( |
| 6941 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, |
| 6942 | Tokens[NumTokens-1]))); |
| 6943 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6944 | // Relex the tokens within the source range to look for preprocessing |
| 6945 | // directives. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6946 | annotatePreprocessorTokens(TU, RegionOfInterest, Cursors, Tokens, NumTokens); |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6947 | |
| 6948 | // If begin location points inside a macro argument, set it to the expansion |
| 6949 | // location so we can have the full context when annotating semantically. |
| 6950 | { |
| 6951 | SourceManager &SM = CXXUnit->getSourceManager(); |
| 6952 | SourceLocation Loc = |
| 6953 | SM.getMacroArgExpandedLocation(RegionOfInterest.getBegin()); |
| 6954 | if (Loc.isMacroID()) |
| 6955 | RegionOfInterest.setBegin(SM.getExpansionLoc(Loc)); |
| 6956 | } |
| 6957 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6958 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) { |
| 6959 | // Search and mark tokens that are macro argument expansions. |
| 6960 | MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(), |
| 6961 | Tokens, NumTokens); |
| 6962 | CursorVisitor MacroArgMarker(TU, |
| 6963 | MarkMacroArgTokensVisitorDelegate, &Visitor, |
| 6964 | /*VisitPreprocessorLast=*/true, |
| 6965 | /*VisitIncludedEntities=*/false, |
| 6966 | RegionOfInterest); |
| 6967 | MacroArgMarker.visitPreprocessedEntitiesInRegion(); |
| 6968 | } |
| 6969 | |
| 6970 | // Annotate all of the source locations in the region of interest that map to |
| 6971 | // a specific cursor. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6972 | AnnotateTokensWorker W(Tokens, Cursors, NumTokens, TU, RegionOfInterest); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6973 | |
| 6974 | // FIXME: We use a ridiculous stack size here because the data-recursion |
| 6975 | // algorithm uses a large stack frame than the non-data recursive version, |
| 6976 | // and AnnotationTokensWorker currently transforms the data-recursion |
| 6977 | // algorithm back into a traditional recursion by explicitly calling |
| 6978 | // VisitChildren(). We will need to remove this explicit recursive call. |
| 6979 | W.AnnotateTokens(); |
| 6980 | |
| 6981 | // If we ran into any entities that involve context-sensitive keywords, |
| 6982 | // take another pass through the tokens to mark them as such. |
| 6983 | if (W.hasContextSensitiveKeywords()) { |
| 6984 | for (unsigned I = 0; I != NumTokens; ++I) { |
| 6985 | if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier) |
| 6986 | continue; |
| 6987 | |
| 6988 | if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) { |
| 6989 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6990 | if (const ObjCPropertyDecl *Property |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6991 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) { |
| 6992 | if (Property->getPropertyAttributesAsWritten() != 0 && |
| 6993 | llvm::StringSwitch<bool>(II->getName()) |
| 6994 | .Case("readonly", true) |
| 6995 | .Case("assign", true) |
| 6996 | .Case("unsafe_unretained", true) |
| 6997 | .Case("readwrite", true) |
| 6998 | .Case("retain", true) |
| 6999 | .Case("copy", true) |
| 7000 | .Case("nonatomic", true) |
| 7001 | .Case("atomic", true) |
| 7002 | .Case("getter", true) |
| 7003 | .Case("setter", true) |
| 7004 | .Case("strong", true) |
| 7005 | .Case("weak", true) |
Manman Ren | 04fd4d8 | 2016-05-31 23:22:04 +0000 | [diff] [blame] | 7006 | .Case("class", true) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7007 | .Default(false)) |
| 7008 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 7009 | } |
| 7010 | continue; |
| 7011 | } |
| 7012 | |
| 7013 | if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl || |
| 7014 | Cursors[I].kind == CXCursor_ObjCClassMethodDecl) { |
| 7015 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 7016 | if (llvm::StringSwitch<bool>(II->getName()) |
| 7017 | .Case("in", true) |
| 7018 | .Case("out", true) |
| 7019 | .Case("inout", true) |
| 7020 | .Case("oneway", true) |
| 7021 | .Case("bycopy", true) |
| 7022 | .Case("byref", true) |
| 7023 | .Default(false)) |
| 7024 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 7025 | continue; |
| 7026 | } |
| 7027 | |
| 7028 | if (Cursors[I].kind == CXCursor_CXXFinalAttr || |
| 7029 | Cursors[I].kind == CXCursor_CXXOverrideAttr) { |
| 7030 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 7031 | continue; |
| 7032 | } |
| 7033 | } |
| 7034 | } |
| 7035 | } |
| 7036 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7037 | void clang_annotateTokens(CXTranslationUnit TU, |
| 7038 | CXToken *Tokens, unsigned NumTokens, |
| 7039 | CXCursor *Cursors) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7040 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7041 | LOG_BAD_TU(TU); |
| 7042 | return; |
| 7043 | } |
| 7044 | if (NumTokens == 0 || !Tokens || !Cursors) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7045 | LOG_FUNC_SECTION { *Log << "<null input>"; } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7046 | return; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7047 | } |
| 7048 | |
| 7049 | LOG_FUNC_SECTION { |
| 7050 | *Log << TU << ' '; |
| 7051 | CXSourceLocation bloc = clang_getTokenLocation(TU, Tokens[0]); |
| 7052 | CXSourceLocation eloc = clang_getTokenLocation(TU, Tokens[NumTokens-1]); |
| 7053 | *Log << clang_getRange(bloc, eloc); |
| 7054 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7055 | |
| 7056 | // Any token we don't specifically annotate will have a NULL cursor. |
| 7057 | CXCursor C = clang_getNullCursor(); |
| 7058 | for (unsigned I = 0; I != NumTokens; ++I) |
| 7059 | Cursors[I] = C; |
| 7060 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7061 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7062 | if (!CXXUnit) |
| 7063 | return; |
| 7064 | |
| 7065 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 7066 | |
| 7067 | auto AnnotateTokensImpl = [=]() { |
| 7068 | clang_annotateTokensImpl(TU, CXXUnit, Tokens, NumTokens, Cursors); |
| 7069 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7070 | llvm::CrashRecoveryContext CRC; |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 7071 | if (!RunSafely(CRC, AnnotateTokensImpl, GetSafetyThreadStackSize() * 2)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7072 | fprintf(stderr, "libclang: crash detected while annotating tokens\n"); |
| 7073 | } |
| 7074 | } |
| 7075 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7076 | //===----------------------------------------------------------------------===// |
| 7077 | // Operations for querying linkage of a cursor. |
| 7078 | //===----------------------------------------------------------------------===// |
| 7079 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7080 | CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { |
| 7081 | if (!clang_isDeclaration(cursor.kind)) |
| 7082 | return CXLinkage_Invalid; |
| 7083 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7084 | const Decl *D = cxcursor::getCursorDecl(cursor); |
| 7085 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7086 | switch (ND->getLinkageInternal()) { |
Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 7087 | case NoLinkage: |
| 7088 | case VisibleNoLinkage: return CXLinkage_NoLinkage; |
Richard Smith | af10ea2 | 2017-07-08 00:37:59 +0000 | [diff] [blame] | 7089 | case ModuleInternalLinkage: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7090 | case InternalLinkage: return CXLinkage_Internal; |
| 7091 | case UniqueExternalLinkage: return CXLinkage_UniqueExternal; |
Richard Smith | af10ea2 | 2017-07-08 00:37:59 +0000 | [diff] [blame] | 7092 | case ModuleLinkage: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7093 | case ExternalLinkage: return CXLinkage_External; |
| 7094 | }; |
| 7095 | |
| 7096 | return CXLinkage_Invalid; |
| 7097 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7098 | |
| 7099 | //===----------------------------------------------------------------------===// |
Ehsan Akhgari | b743de7 | 2016-05-31 15:55:51 +0000 | [diff] [blame] | 7100 | // Operations for querying visibility of a cursor. |
| 7101 | //===----------------------------------------------------------------------===// |
| 7102 | |
Ehsan Akhgari | b743de7 | 2016-05-31 15:55:51 +0000 | [diff] [blame] | 7103 | CXVisibilityKind clang_getCursorVisibility(CXCursor cursor) { |
| 7104 | if (!clang_isDeclaration(cursor.kind)) |
| 7105 | return CXVisibility_Invalid; |
| 7106 | |
| 7107 | const Decl *D = cxcursor::getCursorDecl(cursor); |
| 7108 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
| 7109 | switch (ND->getVisibility()) { |
| 7110 | case HiddenVisibility: return CXVisibility_Hidden; |
| 7111 | case ProtectedVisibility: return CXVisibility_Protected; |
| 7112 | case DefaultVisibility: return CXVisibility_Default; |
| 7113 | }; |
| 7114 | |
| 7115 | return CXVisibility_Invalid; |
| 7116 | } |
Ehsan Akhgari | b743de7 | 2016-05-31 15:55:51 +0000 | [diff] [blame] | 7117 | |
| 7118 | //===----------------------------------------------------------------------===// |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7119 | // Operations for querying language of a cursor. |
| 7120 | //===----------------------------------------------------------------------===// |
| 7121 | |
| 7122 | static CXLanguageKind getDeclLanguage(const Decl *D) { |
| 7123 | if (!D) |
| 7124 | return CXLanguage_C; |
| 7125 | |
| 7126 | switch (D->getKind()) { |
| 7127 | default: |
| 7128 | break; |
| 7129 | case Decl::ImplicitParam: |
| 7130 | case Decl::ObjCAtDefsField: |
| 7131 | case Decl::ObjCCategory: |
| 7132 | case Decl::ObjCCategoryImpl: |
| 7133 | case Decl::ObjCCompatibleAlias: |
| 7134 | case Decl::ObjCImplementation: |
| 7135 | case Decl::ObjCInterface: |
| 7136 | case Decl::ObjCIvar: |
| 7137 | case Decl::ObjCMethod: |
| 7138 | case Decl::ObjCProperty: |
| 7139 | case Decl::ObjCPropertyImpl: |
| 7140 | case Decl::ObjCProtocol: |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 7141 | case Decl::ObjCTypeParam: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7142 | return CXLanguage_ObjC; |
| 7143 | case Decl::CXXConstructor: |
| 7144 | case Decl::CXXConversion: |
| 7145 | case Decl::CXXDestructor: |
| 7146 | case Decl::CXXMethod: |
| 7147 | case Decl::CXXRecord: |
| 7148 | case Decl::ClassTemplate: |
| 7149 | case Decl::ClassTemplatePartialSpecialization: |
| 7150 | case Decl::ClassTemplateSpecialization: |
| 7151 | case Decl::Friend: |
| 7152 | case Decl::FriendTemplate: |
| 7153 | case Decl::FunctionTemplate: |
| 7154 | case Decl::LinkageSpec: |
| 7155 | case Decl::Namespace: |
| 7156 | case Decl::NamespaceAlias: |
| 7157 | case Decl::NonTypeTemplateParm: |
| 7158 | case Decl::StaticAssert: |
| 7159 | case Decl::TemplateTemplateParm: |
| 7160 | case Decl::TemplateTypeParm: |
| 7161 | case Decl::UnresolvedUsingTypename: |
| 7162 | case Decl::UnresolvedUsingValue: |
| 7163 | case Decl::Using: |
| 7164 | case Decl::UsingDirective: |
| 7165 | case Decl::UsingShadow: |
| 7166 | return CXLanguage_CPlusPlus; |
| 7167 | } |
| 7168 | |
| 7169 | return CXLanguage_C; |
| 7170 | } |
| 7171 | |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7172 | static CXAvailabilityKind getCursorAvailabilityForDecl(const Decl *D) { |
| 7173 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()) |
Manuel Klimek | 8e3a7ed | 2015-09-25 17:53:16 +0000 | [diff] [blame] | 7174 | return CXAvailability_NotAvailable; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7175 | |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7176 | switch (D->getAvailability()) { |
| 7177 | case AR_Available: |
| 7178 | case AR_NotYetIntroduced: |
| 7179 | if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(D)) |
Benjamin Kramer | 656363d | 2013-10-15 18:53:18 +0000 | [diff] [blame] | 7180 | return getCursorAvailabilityForDecl( |
| 7181 | cast<Decl>(EnumConst->getDeclContext())); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7182 | return CXAvailability_Available; |
| 7183 | |
| 7184 | case AR_Deprecated: |
| 7185 | return CXAvailability_Deprecated; |
| 7186 | |
| 7187 | case AR_Unavailable: |
| 7188 | return CXAvailability_NotAvailable; |
| 7189 | } |
Benjamin Kramer | 656363d | 2013-10-15 18:53:18 +0000 | [diff] [blame] | 7190 | |
| 7191 | llvm_unreachable("Unknown availability kind!"); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7192 | } |
| 7193 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7194 | enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { |
| 7195 | if (clang_isDeclaration(cursor.kind)) |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7196 | if (const Decl *D = cxcursor::getCursorDecl(cursor)) |
| 7197 | return getCursorAvailabilityForDecl(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7198 | |
| 7199 | return CXAvailability_Available; |
| 7200 | } |
| 7201 | |
| 7202 | static CXVersion convertVersion(VersionTuple In) { |
| 7203 | CXVersion Out = { -1, -1, -1 }; |
| 7204 | if (In.empty()) |
| 7205 | return Out; |
| 7206 | |
| 7207 | Out.Major = In.getMajor(); |
| 7208 | |
NAKAMURA Takumi | c2b5d1f | 2013-02-21 02:32:34 +0000 | [diff] [blame] | 7209 | Optional<unsigned> Minor = In.getMinor(); |
| 7210 | if (Minor.hasValue()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7211 | Out.Minor = *Minor; |
| 7212 | else |
| 7213 | return Out; |
| 7214 | |
NAKAMURA Takumi | c2b5d1f | 2013-02-21 02:32:34 +0000 | [diff] [blame] | 7215 | Optional<unsigned> Subminor = In.getSubminor(); |
| 7216 | if (Subminor.hasValue()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7217 | Out.Subminor = *Subminor; |
| 7218 | |
| 7219 | return Out; |
| 7220 | } |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7221 | |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7222 | static void getCursorPlatformAvailabilityForDecl( |
| 7223 | const Decl *D, int *always_deprecated, CXString *deprecated_message, |
| 7224 | int *always_unavailable, CXString *unavailable_message, |
| 7225 | SmallVectorImpl<AvailabilityAttr *> &AvailabilityAttrs) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7226 | bool HadAvailAttr = false; |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 7227 | for (auto A : D->attrs()) { |
| 7228 | if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(A)) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7229 | HadAvailAttr = true; |
| 7230 | if (always_deprecated) |
| 7231 | *always_deprecated = 1; |
Nico Weber | aacf031 | 2014-04-24 05:16:45 +0000 | [diff] [blame] | 7232 | if (deprecated_message) { |
Argyrios Kyrtzidis | edfe07f | 2014-04-24 06:05:40 +0000 | [diff] [blame] | 7233 | clang_disposeString(*deprecated_message); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7234 | *deprecated_message = cxstring::createDup(Deprecated->getMessage()); |
Nico Weber | aacf031 | 2014-04-24 05:16:45 +0000 | [diff] [blame] | 7235 | } |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7236 | continue; |
| 7237 | } |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7238 | |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 7239 | if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(A)) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7240 | HadAvailAttr = true; |
| 7241 | if (always_unavailable) |
| 7242 | *always_unavailable = 1; |
| 7243 | if (unavailable_message) { |
Argyrios Kyrtzidis | edfe07f | 2014-04-24 06:05:40 +0000 | [diff] [blame] | 7244 | clang_disposeString(*unavailable_message); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7245 | *unavailable_message = cxstring::createDup(Unavailable->getMessage()); |
| 7246 | } |
| 7247 | continue; |
| 7248 | } |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7249 | |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 7250 | if (AvailabilityAttr *Avail = dyn_cast<AvailabilityAttr>(A)) { |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7251 | AvailabilityAttrs.push_back(Avail); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7252 | HadAvailAttr = true; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7253 | } |
| 7254 | } |
| 7255 | |
| 7256 | if (!HadAvailAttr) |
| 7257 | if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(D)) |
| 7258 | return getCursorPlatformAvailabilityForDecl( |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7259 | cast<Decl>(EnumConst->getDeclContext()), always_deprecated, |
| 7260 | deprecated_message, always_unavailable, unavailable_message, |
| 7261 | AvailabilityAttrs); |
| 7262 | |
| 7263 | if (AvailabilityAttrs.empty()) |
| 7264 | return; |
| 7265 | |
| 7266 | std::sort(AvailabilityAttrs.begin(), AvailabilityAttrs.end(), |
| 7267 | [](AvailabilityAttr *LHS, AvailabilityAttr *RHS) { |
| 7268 | return LHS->getPlatform() > RHS->getPlatform(); |
| 7269 | }); |
| 7270 | ASTContext &Ctx = D->getASTContext(); |
| 7271 | auto It = std::unique( |
| 7272 | AvailabilityAttrs.begin(), AvailabilityAttrs.end(), |
| 7273 | [&Ctx](AvailabilityAttr *LHS, AvailabilityAttr *RHS) { |
| 7274 | if (LHS->getPlatform() != RHS->getPlatform()) |
| 7275 | return false; |
| 7276 | |
| 7277 | if (LHS->getIntroduced() == RHS->getIntroduced() && |
| 7278 | LHS->getDeprecated() == RHS->getDeprecated() && |
| 7279 | LHS->getObsoleted() == RHS->getObsoleted() && |
| 7280 | LHS->getMessage() == RHS->getMessage() && |
| 7281 | LHS->getReplacement() == RHS->getReplacement()) |
| 7282 | return true; |
| 7283 | |
| 7284 | if ((!LHS->getIntroduced().empty() && !RHS->getIntroduced().empty()) || |
| 7285 | (!LHS->getDeprecated().empty() && !RHS->getDeprecated().empty()) || |
| 7286 | (!LHS->getObsoleted().empty() && !RHS->getObsoleted().empty())) |
| 7287 | return false; |
| 7288 | |
| 7289 | if (LHS->getIntroduced().empty() && !RHS->getIntroduced().empty()) |
| 7290 | LHS->setIntroduced(Ctx, RHS->getIntroduced()); |
| 7291 | |
| 7292 | if (LHS->getDeprecated().empty() && !RHS->getDeprecated().empty()) { |
| 7293 | LHS->setDeprecated(Ctx, RHS->getDeprecated()); |
| 7294 | if (LHS->getMessage().empty()) |
| 7295 | LHS->setMessage(Ctx, RHS->getMessage()); |
| 7296 | if (LHS->getReplacement().empty()) |
| 7297 | LHS->setReplacement(Ctx, RHS->getReplacement()); |
| 7298 | } |
| 7299 | |
| 7300 | if (LHS->getObsoleted().empty() && !RHS->getObsoleted().empty()) { |
| 7301 | LHS->setObsoleted(Ctx, RHS->getObsoleted()); |
| 7302 | if (LHS->getMessage().empty()) |
| 7303 | LHS->setMessage(Ctx, RHS->getMessage()); |
| 7304 | if (LHS->getReplacement().empty()) |
| 7305 | LHS->setReplacement(Ctx, RHS->getReplacement()); |
| 7306 | } |
| 7307 | |
| 7308 | return true; |
| 7309 | }); |
| 7310 | AvailabilityAttrs.erase(It, AvailabilityAttrs.end()); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7311 | } |
| 7312 | |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7313 | int clang_getCursorPlatformAvailability(CXCursor cursor, int *always_deprecated, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7314 | CXString *deprecated_message, |
| 7315 | int *always_unavailable, |
| 7316 | CXString *unavailable_message, |
| 7317 | CXPlatformAvailability *availability, |
| 7318 | int availability_size) { |
| 7319 | if (always_deprecated) |
| 7320 | *always_deprecated = 0; |
| 7321 | if (deprecated_message) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 7322 | *deprecated_message = cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7323 | if (always_unavailable) |
| 7324 | *always_unavailable = 0; |
| 7325 | if (unavailable_message) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 7326 | *unavailable_message = cxstring::createEmpty(); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7327 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7328 | if (!clang_isDeclaration(cursor.kind)) |
| 7329 | return 0; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7330 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7331 | const Decl *D = cxcursor::getCursorDecl(cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7332 | if (!D) |
| 7333 | return 0; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7334 | |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7335 | SmallVector<AvailabilityAttr *, 8> AvailabilityAttrs; |
| 7336 | getCursorPlatformAvailabilityForDecl(D, always_deprecated, deprecated_message, |
| 7337 | always_unavailable, unavailable_message, |
| 7338 | AvailabilityAttrs); |
| 7339 | for (const auto &Avail : |
| 7340 | llvm::enumerate(llvm::makeArrayRef(AvailabilityAttrs) |
| 7341 | .take_front(availability_size))) { |
| 7342 | availability[Avail.index()].Platform = |
| 7343 | cxstring::createDup(Avail.value()->getPlatform()->getName()); |
| 7344 | availability[Avail.index()].Introduced = |
| 7345 | convertVersion(Avail.value()->getIntroduced()); |
| 7346 | availability[Avail.index()].Deprecated = |
| 7347 | convertVersion(Avail.value()->getDeprecated()); |
| 7348 | availability[Avail.index()].Obsoleted = |
| 7349 | convertVersion(Avail.value()->getObsoleted()); |
| 7350 | availability[Avail.index()].Unavailable = Avail.value()->getUnavailable(); |
| 7351 | availability[Avail.index()].Message = |
| 7352 | cxstring::createDup(Avail.value()->getMessage()); |
| 7353 | } |
| 7354 | |
| 7355 | return AvailabilityAttrs.size(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7356 | } |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7357 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7358 | void clang_disposeCXPlatformAvailability(CXPlatformAvailability *availability) { |
| 7359 | clang_disposeString(availability->Platform); |
| 7360 | clang_disposeString(availability->Message); |
| 7361 | } |
| 7362 | |
| 7363 | CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { |
| 7364 | if (clang_isDeclaration(cursor.kind)) |
| 7365 | return getDeclLanguage(cxcursor::getCursorDecl(cursor)); |
| 7366 | |
| 7367 | return CXLanguage_Invalid; |
| 7368 | } |
| 7369 | |
| 7370 | /// \brief If the given cursor is the "templated" declaration |
| 7371 | /// descibing a class or function template, return the class or |
| 7372 | /// function template. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7373 | static const Decl *maybeGetTemplateCursor(const Decl *D) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7374 | if (!D) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7375 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7376 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7377 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7378 | if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate()) |
| 7379 | return FunTmpl; |
| 7380 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7381 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7382 | if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate()) |
| 7383 | return ClassTmpl; |
| 7384 | |
| 7385 | return D; |
| 7386 | } |
| 7387 | |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 7388 | |
| 7389 | enum CX_StorageClass clang_Cursor_getStorageClass(CXCursor C) { |
| 7390 | StorageClass sc = SC_None; |
| 7391 | const Decl *D = getCursorDecl(C); |
| 7392 | if (D) { |
| 7393 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7394 | sc = FD->getStorageClass(); |
| 7395 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7396 | sc = VD->getStorageClass(); |
| 7397 | } else { |
| 7398 | return CX_SC_Invalid; |
| 7399 | } |
| 7400 | } else { |
| 7401 | return CX_SC_Invalid; |
| 7402 | } |
| 7403 | switch (sc) { |
| 7404 | case SC_None: |
| 7405 | return CX_SC_None; |
| 7406 | case SC_Extern: |
| 7407 | return CX_SC_Extern; |
| 7408 | case SC_Static: |
| 7409 | return CX_SC_Static; |
| 7410 | case SC_PrivateExtern: |
| 7411 | return CX_SC_PrivateExtern; |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 7412 | case SC_Auto: |
| 7413 | return CX_SC_Auto; |
| 7414 | case SC_Register: |
| 7415 | return CX_SC_Register; |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 7416 | } |
Kaelyn Takata | ab61e70 | 2014-10-15 18:03:26 +0000 | [diff] [blame] | 7417 | llvm_unreachable("Unhandled storage class!"); |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 7418 | } |
| 7419 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7420 | CXCursor clang_getCursorSemanticParent(CXCursor cursor) { |
| 7421 | if (clang_isDeclaration(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7422 | if (const Decl *D = getCursorDecl(cursor)) { |
| 7423 | const DeclContext *DC = D->getDeclContext(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7424 | if (!DC) |
| 7425 | return clang_getNullCursor(); |
| 7426 | |
| 7427 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 7428 | getCursorTU(cursor)); |
| 7429 | } |
| 7430 | } |
| 7431 | |
| 7432 | if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7433 | if (const Decl *D = getCursorDecl(cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7434 | return MakeCXCursor(D, getCursorTU(cursor)); |
| 7435 | } |
| 7436 | |
| 7437 | return clang_getNullCursor(); |
| 7438 | } |
| 7439 | |
| 7440 | CXCursor clang_getCursorLexicalParent(CXCursor cursor) { |
| 7441 | if (clang_isDeclaration(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7442 | if (const Decl *D = getCursorDecl(cursor)) { |
| 7443 | const DeclContext *DC = D->getLexicalDeclContext(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7444 | if (!DC) |
| 7445 | return clang_getNullCursor(); |
| 7446 | |
| 7447 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 7448 | getCursorTU(cursor)); |
| 7449 | } |
| 7450 | } |
| 7451 | |
| 7452 | // FIXME: Note that we can't easily compute the lexical context of a |
| 7453 | // statement or expression, so we return nothing. |
| 7454 | return clang_getNullCursor(); |
| 7455 | } |
| 7456 | |
| 7457 | CXFile clang_getIncludedFile(CXCursor cursor) { |
| 7458 | if (cursor.kind != CXCursor_InclusionDirective) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7459 | return nullptr; |
| 7460 | |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 7461 | const InclusionDirective *ID = getCursorInclusionDirective(cursor); |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 7462 | return const_cast<FileEntry *>(ID->getFile()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7463 | } |
| 7464 | |
Argyrios Kyrtzidis | 9adfd8a | 2013-04-18 22:15:49 +0000 | [diff] [blame] | 7465 | unsigned clang_Cursor_getObjCPropertyAttributes(CXCursor C, unsigned reserved) { |
| 7466 | if (C.kind != CXCursor_ObjCPropertyDecl) |
| 7467 | return CXObjCPropertyAttr_noattr; |
| 7468 | |
| 7469 | unsigned Result = CXObjCPropertyAttr_noattr; |
| 7470 | const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(getCursorDecl(C)); |
| 7471 | ObjCPropertyDecl::PropertyAttributeKind Attr = |
| 7472 | PD->getPropertyAttributesAsWritten(); |
| 7473 | |
| 7474 | #define SET_CXOBJCPROP_ATTR(A) \ |
| 7475 | if (Attr & ObjCPropertyDecl::OBJC_PR_##A) \ |
| 7476 | Result |= CXObjCPropertyAttr_##A |
| 7477 | SET_CXOBJCPROP_ATTR(readonly); |
| 7478 | SET_CXOBJCPROP_ATTR(getter); |
| 7479 | SET_CXOBJCPROP_ATTR(assign); |
| 7480 | SET_CXOBJCPROP_ATTR(readwrite); |
| 7481 | SET_CXOBJCPROP_ATTR(retain); |
| 7482 | SET_CXOBJCPROP_ATTR(copy); |
| 7483 | SET_CXOBJCPROP_ATTR(nonatomic); |
| 7484 | SET_CXOBJCPROP_ATTR(setter); |
| 7485 | SET_CXOBJCPROP_ATTR(atomic); |
| 7486 | SET_CXOBJCPROP_ATTR(weak); |
| 7487 | SET_CXOBJCPROP_ATTR(strong); |
| 7488 | SET_CXOBJCPROP_ATTR(unsafe_unretained); |
Manman Ren | 04fd4d8 | 2016-05-31 23:22:04 +0000 | [diff] [blame] | 7489 | SET_CXOBJCPROP_ATTR(class); |
Argyrios Kyrtzidis | 9adfd8a | 2013-04-18 22:15:49 +0000 | [diff] [blame] | 7490 | #undef SET_CXOBJCPROP_ATTR |
| 7491 | |
| 7492 | return Result; |
| 7493 | } |
| 7494 | |
Argyrios Kyrtzidis | 9d9bc01 | 2013-04-18 23:29:12 +0000 | [diff] [blame] | 7495 | unsigned clang_Cursor_getObjCDeclQualifiers(CXCursor C) { |
| 7496 | if (!clang_isDeclaration(C.kind)) |
| 7497 | return CXObjCDeclQualifier_None; |
| 7498 | |
| 7499 | Decl::ObjCDeclQualifier QT = Decl::OBJC_TQ_None; |
| 7500 | const Decl *D = getCursorDecl(C); |
| 7501 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 7502 | QT = MD->getObjCDeclQualifier(); |
| 7503 | else if (const ParmVarDecl *PD = dyn_cast<ParmVarDecl>(D)) |
| 7504 | QT = PD->getObjCDeclQualifier(); |
| 7505 | if (QT == Decl::OBJC_TQ_None) |
| 7506 | return CXObjCDeclQualifier_None; |
| 7507 | |
| 7508 | unsigned Result = CXObjCDeclQualifier_None; |
| 7509 | if (QT & Decl::OBJC_TQ_In) Result |= CXObjCDeclQualifier_In; |
| 7510 | if (QT & Decl::OBJC_TQ_Inout) Result |= CXObjCDeclQualifier_Inout; |
| 7511 | if (QT & Decl::OBJC_TQ_Out) Result |= CXObjCDeclQualifier_Out; |
| 7512 | if (QT & Decl::OBJC_TQ_Bycopy) Result |= CXObjCDeclQualifier_Bycopy; |
| 7513 | if (QT & Decl::OBJC_TQ_Byref) Result |= CXObjCDeclQualifier_Byref; |
| 7514 | if (QT & Decl::OBJC_TQ_Oneway) Result |= CXObjCDeclQualifier_Oneway; |
| 7515 | |
| 7516 | return Result; |
| 7517 | } |
| 7518 | |
Argyrios Kyrtzidis | 7b50fc5 | 2013-07-05 20:44:37 +0000 | [diff] [blame] | 7519 | unsigned clang_Cursor_isObjCOptional(CXCursor C) { |
| 7520 | if (!clang_isDeclaration(C.kind)) |
| 7521 | return 0; |
| 7522 | |
| 7523 | const Decl *D = getCursorDecl(C); |
| 7524 | if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 7525 | return PD->getPropertyImplementation() == ObjCPropertyDecl::Optional; |
| 7526 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 7527 | return MD->getImplementationControl() == ObjCMethodDecl::Optional; |
| 7528 | |
| 7529 | return 0; |
| 7530 | } |
| 7531 | |
Argyrios Kyrtzidis | 23814e4 | 2013-04-18 23:53:05 +0000 | [diff] [blame] | 7532 | unsigned clang_Cursor_isVariadic(CXCursor C) { |
| 7533 | if (!clang_isDeclaration(C.kind)) |
| 7534 | return 0; |
| 7535 | |
| 7536 | const Decl *D = getCursorDecl(C); |
| 7537 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 7538 | return FD->isVariadic(); |
| 7539 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 7540 | return MD->isVariadic(); |
| 7541 | |
| 7542 | return 0; |
| 7543 | } |
| 7544 | |
Argyrios Kyrtzidis | 0381cc7 | 2017-05-10 15:10:36 +0000 | [diff] [blame] | 7545 | unsigned clang_Cursor_isExternalSymbol(CXCursor C, |
| 7546 | CXString *language, CXString *definedIn, |
| 7547 | unsigned *isGenerated) { |
| 7548 | if (!clang_isDeclaration(C.kind)) |
| 7549 | return 0; |
| 7550 | |
| 7551 | const Decl *D = getCursorDecl(C); |
| 7552 | |
Argyrios Kyrtzidis | 11d7048 | 2017-05-20 04:11:33 +0000 | [diff] [blame] | 7553 | if (auto *attr = D->getExternalSourceSymbolAttr()) { |
Argyrios Kyrtzidis | 0381cc7 | 2017-05-10 15:10:36 +0000 | [diff] [blame] | 7554 | if (language) |
| 7555 | *language = cxstring::createDup(attr->getLanguage()); |
| 7556 | if (definedIn) |
| 7557 | *definedIn = cxstring::createDup(attr->getDefinedIn()); |
| 7558 | if (isGenerated) |
| 7559 | *isGenerated = attr->getGeneratedDeclaration(); |
| 7560 | return 1; |
| 7561 | } |
| 7562 | return 0; |
| 7563 | } |
| 7564 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7565 | CXSourceRange clang_Cursor_getCommentRange(CXCursor C) { |
| 7566 | if (!clang_isDeclaration(C.kind)) |
| 7567 | return clang_getNullRange(); |
| 7568 | |
| 7569 | const Decl *D = getCursorDecl(C); |
| 7570 | ASTContext &Context = getCursorContext(C); |
| 7571 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 7572 | if (!RC) |
| 7573 | return clang_getNullRange(); |
| 7574 | |
| 7575 | return cxloc::translateSourceRange(Context, RC->getSourceRange()); |
| 7576 | } |
| 7577 | |
| 7578 | CXString clang_Cursor_getRawCommentText(CXCursor C) { |
| 7579 | if (!clang_isDeclaration(C.kind)) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 7580 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7581 | |
| 7582 | const Decl *D = getCursorDecl(C); |
| 7583 | ASTContext &Context = getCursorContext(C); |
| 7584 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 7585 | StringRef RawText = RC ? RC->getRawText(Context.getSourceManager()) : |
| 7586 | StringRef(); |
| 7587 | |
| 7588 | // Don't duplicate the string because RawText points directly into source |
| 7589 | // code. |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 7590 | return cxstring::createRef(RawText); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7591 | } |
| 7592 | |
| 7593 | CXString clang_Cursor_getBriefCommentText(CXCursor C) { |
| 7594 | if (!clang_isDeclaration(C.kind)) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 7595 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7596 | |
| 7597 | const Decl *D = getCursorDecl(C); |
| 7598 | const ASTContext &Context = getCursorContext(C); |
| 7599 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 7600 | |
| 7601 | if (RC) { |
| 7602 | StringRef BriefText = RC->getBriefText(Context); |
| 7603 | |
| 7604 | // Don't duplicate the string because RawComment ensures that this memory |
| 7605 | // will not go away. |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 7606 | return cxstring::createRef(BriefText); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7607 | } |
| 7608 | |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 7609 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7610 | } |
| 7611 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7612 | CXModule clang_Cursor_getModule(CXCursor C) { |
| 7613 | if (C.kind == CXCursor_ModuleImportDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7614 | if (const ImportDecl *ImportD = |
| 7615 | dyn_cast_or_null<ImportDecl>(getCursorDecl(C))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7616 | return ImportD->getImportedModule(); |
| 7617 | } |
| 7618 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7619 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7620 | } |
| 7621 | |
Argyrios Kyrtzidis | f6d49c3 | 2014-05-14 23:14:37 +0000 | [diff] [blame] | 7622 | CXModule clang_getModuleForFile(CXTranslationUnit TU, CXFile File) { |
| 7623 | if (isNotUsableTU(TU)) { |
| 7624 | LOG_BAD_TU(TU); |
| 7625 | return nullptr; |
| 7626 | } |
| 7627 | if (!File) |
| 7628 | return nullptr; |
| 7629 | FileEntry *FE = static_cast<FileEntry *>(File); |
| 7630 | |
| 7631 | ASTUnit &Unit = *cxtu::getASTUnit(TU); |
| 7632 | HeaderSearch &HS = Unit.getPreprocessor().getHeaderSearchInfo(); |
| 7633 | ModuleMap::KnownHeader Header = HS.findModuleForHeader(FE); |
| 7634 | |
Richard Smith | feb54b6 | 2014-10-23 02:01:19 +0000 | [diff] [blame] | 7635 | return Header.getModule(); |
Argyrios Kyrtzidis | f6d49c3 | 2014-05-14 23:14:37 +0000 | [diff] [blame] | 7636 | } |
| 7637 | |
Argyrios Kyrtzidis | 12fdb9e | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 7638 | CXFile clang_Module_getASTFile(CXModule CXMod) { |
| 7639 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7640 | return nullptr; |
Argyrios Kyrtzidis | 12fdb9e | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 7641 | Module *Mod = static_cast<Module*>(CXMod); |
| 7642 | return const_cast<FileEntry *>(Mod->getASTFile()); |
| 7643 | } |
| 7644 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7645 | CXModule clang_Module_getParent(CXModule CXMod) { |
| 7646 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7647 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7648 | Module *Mod = static_cast<Module*>(CXMod); |
| 7649 | return Mod->Parent; |
| 7650 | } |
| 7651 | |
| 7652 | CXString clang_Module_getName(CXModule CXMod) { |
| 7653 | if (!CXMod) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 7654 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7655 | Module *Mod = static_cast<Module*>(CXMod); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 7656 | return cxstring::createDup(Mod->Name); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7657 | } |
| 7658 | |
| 7659 | CXString clang_Module_getFullName(CXModule CXMod) { |
| 7660 | if (!CXMod) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 7661 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7662 | Module *Mod = static_cast<Module*>(CXMod); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 7663 | return cxstring::createDup(Mod->getFullModuleName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7664 | } |
| 7665 | |
Argyrios Kyrtzidis | 884337f | 2014-05-15 04:44:25 +0000 | [diff] [blame] | 7666 | int clang_Module_isSystem(CXModule CXMod) { |
| 7667 | if (!CXMod) |
| 7668 | return 0; |
| 7669 | Module *Mod = static_cast<Module*>(CXMod); |
| 7670 | return Mod->IsSystem; |
| 7671 | } |
| 7672 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7673 | unsigned clang_Module_getNumTopLevelHeaders(CXTranslationUnit TU, |
| 7674 | CXModule CXMod) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7675 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7676 | LOG_BAD_TU(TU); |
| 7677 | return 0; |
| 7678 | } |
| 7679 | if (!CXMod) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7680 | return 0; |
| 7681 | Module *Mod = static_cast<Module*>(CXMod); |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7682 | FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager(); |
| 7683 | ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr); |
| 7684 | return TopHeaders.size(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7685 | } |
| 7686 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7687 | CXFile clang_Module_getTopLevelHeader(CXTranslationUnit TU, |
| 7688 | CXModule CXMod, unsigned Index) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7689 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7690 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7691 | return nullptr; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7692 | } |
| 7693 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7694 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7695 | Module *Mod = static_cast<Module*>(CXMod); |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7696 | FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7697 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7698 | ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr); |
| 7699 | if (Index < TopHeaders.size()) |
| 7700 | return const_cast<FileEntry *>(TopHeaders[Index]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7701 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7702 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7703 | } |
| 7704 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7705 | //===----------------------------------------------------------------------===// |
| 7706 | // C++ AST instrospection. |
| 7707 | //===----------------------------------------------------------------------===// |
| 7708 | |
Jonathan Coe | 2956535 | 2016-04-27 12:48:25 +0000 | [diff] [blame] | 7709 | unsigned clang_CXXConstructor_isDefaultConstructor(CXCursor C) { |
| 7710 | if (!clang_isDeclaration(C.kind)) |
| 7711 | return 0; |
| 7712 | |
| 7713 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7714 | const CXXConstructorDecl *Constructor = |
| 7715 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 7716 | return (Constructor && Constructor->isDefaultConstructor()) ? 1 : 0; |
| 7717 | } |
| 7718 | |
| 7719 | unsigned clang_CXXConstructor_isCopyConstructor(CXCursor C) { |
| 7720 | if (!clang_isDeclaration(C.kind)) |
| 7721 | return 0; |
| 7722 | |
| 7723 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7724 | const CXXConstructorDecl *Constructor = |
| 7725 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 7726 | return (Constructor && Constructor->isCopyConstructor()) ? 1 : 0; |
| 7727 | } |
| 7728 | |
| 7729 | unsigned clang_CXXConstructor_isMoveConstructor(CXCursor C) { |
| 7730 | if (!clang_isDeclaration(C.kind)) |
| 7731 | return 0; |
| 7732 | |
| 7733 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7734 | const CXXConstructorDecl *Constructor = |
| 7735 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 7736 | return (Constructor && Constructor->isMoveConstructor()) ? 1 : 0; |
| 7737 | } |
| 7738 | |
| 7739 | unsigned clang_CXXConstructor_isConvertingConstructor(CXCursor C) { |
| 7740 | if (!clang_isDeclaration(C.kind)) |
| 7741 | return 0; |
| 7742 | |
| 7743 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7744 | const CXXConstructorDecl *Constructor = |
| 7745 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 7746 | // Passing 'false' excludes constructors marked 'explicit'. |
| 7747 | return (Constructor && Constructor->isConvertingConstructor(false)) ? 1 : 0; |
| 7748 | } |
| 7749 | |
Saleem Abdulrasool | 6ea75db | 2015-10-27 15:50:22 +0000 | [diff] [blame] | 7750 | unsigned clang_CXXField_isMutable(CXCursor C) { |
| 7751 | if (!clang_isDeclaration(C.kind)) |
| 7752 | return 0; |
| 7753 | |
| 7754 | if (const auto D = cxcursor::getCursorDecl(C)) |
| 7755 | if (const auto FD = dyn_cast_or_null<FieldDecl>(D)) |
| 7756 | return FD->isMutable() ? 1 : 0; |
| 7757 | return 0; |
| 7758 | } |
| 7759 | |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 7760 | unsigned clang_CXXMethod_isPureVirtual(CXCursor C) { |
| 7761 | if (!clang_isDeclaration(C.kind)) |
| 7762 | return 0; |
| 7763 | |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 7764 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 7765 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7766 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 7767 | return (Method && Method->isVirtual() && Method->isPure()) ? 1 : 0; |
| 7768 | } |
| 7769 | |
Dmitri Gribenko | e570ede | 2014-04-07 14:59:13 +0000 | [diff] [blame] | 7770 | unsigned clang_CXXMethod_isConst(CXCursor C) { |
| 7771 | if (!clang_isDeclaration(C.kind)) |
| 7772 | return 0; |
| 7773 | |
| 7774 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7775 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7776 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Dmitri Gribenko | e570ede | 2014-04-07 14:59:13 +0000 | [diff] [blame] | 7777 | return (Method && (Method->getTypeQualifiers() & Qualifiers::Const)) ? 1 : 0; |
| 7778 | } |
| 7779 | |
Jonathan Coe | 2956535 | 2016-04-27 12:48:25 +0000 | [diff] [blame] | 7780 | unsigned clang_CXXMethod_isDefaulted(CXCursor C) { |
| 7781 | if (!clang_isDeclaration(C.kind)) |
| 7782 | return 0; |
| 7783 | |
| 7784 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7785 | const CXXMethodDecl *Method = |
| 7786 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
| 7787 | return (Method && Method->isDefaulted()) ? 1 : 0; |
| 7788 | } |
| 7789 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7790 | unsigned clang_CXXMethod_isStatic(CXCursor C) { |
| 7791 | if (!clang_isDeclaration(C.kind)) |
| 7792 | return 0; |
| 7793 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7794 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 7795 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7796 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7797 | return (Method && Method->isStatic()) ? 1 : 0; |
| 7798 | } |
| 7799 | |
| 7800 | unsigned clang_CXXMethod_isVirtual(CXCursor C) { |
| 7801 | if (!clang_isDeclaration(C.kind)) |
| 7802 | return 0; |
| 7803 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7804 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 7805 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7806 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7807 | return (Method && Method->isVirtual()) ? 1 : 0; |
| 7808 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7809 | |
Alex Lorenz | ff7f42e | 2017-07-12 11:35:11 +0000 | [diff] [blame^] | 7810 | unsigned clang_EnumDecl_isScoped(CXCursor C) { |
| 7811 | if (!clang_isDeclaration(C.kind)) |
| 7812 | return 0; |
| 7813 | |
| 7814 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7815 | auto *Enum = dyn_cast_or_null<EnumDecl>(D); |
| 7816 | return (Enum && Enum->isScoped()) ? 1 : 0; |
| 7817 | } |
| 7818 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7819 | //===----------------------------------------------------------------------===// |
| 7820 | // Attribute introspection. |
| 7821 | //===----------------------------------------------------------------------===// |
| 7822 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7823 | CXType clang_getIBOutletCollectionType(CXCursor C) { |
| 7824 | if (C.kind != CXCursor_IBOutletCollectionAttr) |
| 7825 | return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C)); |
| 7826 | |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 7827 | const IBOutletCollectionAttr *A = |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7828 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); |
| 7829 | |
| 7830 | return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C)); |
| 7831 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7832 | |
| 7833 | //===----------------------------------------------------------------------===// |
| 7834 | // Inspecting memory usage. |
| 7835 | //===----------------------------------------------------------------------===// |
| 7836 | |
| 7837 | typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries; |
| 7838 | |
| 7839 | static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries, |
| 7840 | enum CXTUResourceUsageKind k, |
| 7841 | unsigned long amount) { |
| 7842 | CXTUResourceUsageEntry entry = { k, amount }; |
| 7843 | entries.push_back(entry); |
| 7844 | } |
| 7845 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7846 | const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) { |
| 7847 | const char *str = ""; |
| 7848 | switch (kind) { |
| 7849 | case CXTUResourceUsage_AST: |
| 7850 | str = "ASTContext: expressions, declarations, and types"; |
| 7851 | break; |
| 7852 | case CXTUResourceUsage_Identifiers: |
| 7853 | str = "ASTContext: identifiers"; |
| 7854 | break; |
| 7855 | case CXTUResourceUsage_Selectors: |
| 7856 | str = "ASTContext: selectors"; |
| 7857 | break; |
| 7858 | case CXTUResourceUsage_GlobalCompletionResults: |
| 7859 | str = "Code completion: cached global results"; |
| 7860 | break; |
| 7861 | case CXTUResourceUsage_SourceManagerContentCache: |
| 7862 | str = "SourceManager: content cache allocator"; |
| 7863 | break; |
| 7864 | case CXTUResourceUsage_AST_SideTables: |
| 7865 | str = "ASTContext: side tables"; |
| 7866 | break; |
| 7867 | case CXTUResourceUsage_SourceManager_Membuffer_Malloc: |
| 7868 | str = "SourceManager: malloc'ed memory buffers"; |
| 7869 | break; |
| 7870 | case CXTUResourceUsage_SourceManager_Membuffer_MMap: |
| 7871 | str = "SourceManager: mmap'ed memory buffers"; |
| 7872 | break; |
| 7873 | case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc: |
| 7874 | str = "ExternalASTSource: malloc'ed memory buffers"; |
| 7875 | break; |
| 7876 | case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap: |
| 7877 | str = "ExternalASTSource: mmap'ed memory buffers"; |
| 7878 | break; |
| 7879 | case CXTUResourceUsage_Preprocessor: |
| 7880 | str = "Preprocessor: malloc'ed memory"; |
| 7881 | break; |
| 7882 | case CXTUResourceUsage_PreprocessingRecord: |
| 7883 | str = "Preprocessor: PreprocessingRecord"; |
| 7884 | break; |
| 7885 | case CXTUResourceUsage_SourceManager_DataStructures: |
| 7886 | str = "SourceManager: data structures and tables"; |
| 7887 | break; |
| 7888 | case CXTUResourceUsage_Preprocessor_HeaderSearch: |
| 7889 | str = "Preprocessor: header search tables"; |
| 7890 | break; |
| 7891 | } |
| 7892 | return str; |
| 7893 | } |
| 7894 | |
| 7895 | CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7896 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7897 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7898 | CXTUResourceUsage usage = { (void*) nullptr, 0, nullptr }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7899 | return usage; |
| 7900 | } |
| 7901 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7902 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 7903 | std::unique_ptr<MemUsageEntries> entries(new MemUsageEntries()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7904 | ASTContext &astContext = astUnit->getASTContext(); |
| 7905 | |
| 7906 | // How much memory is used by AST nodes and types? |
| 7907 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST, |
| 7908 | (unsigned long) astContext.getASTAllocatedMemory()); |
| 7909 | |
| 7910 | // How much memory is used by identifiers? |
| 7911 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers, |
| 7912 | (unsigned long) astContext.Idents.getAllocator().getTotalMemory()); |
| 7913 | |
| 7914 | // How much memory is used for selectors? |
| 7915 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors, |
| 7916 | (unsigned long) astContext.Selectors.getTotalMemory()); |
| 7917 | |
| 7918 | // How much memory is used by ASTContext's side tables? |
| 7919 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables, |
| 7920 | (unsigned long) astContext.getSideTableAllocatedMemory()); |
| 7921 | |
| 7922 | // How much memory is used for caching global code completion results? |
| 7923 | unsigned long completionBytes = 0; |
| 7924 | if (GlobalCodeCompletionAllocator *completionAllocator = |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 7925 | astUnit->getCachedCompletionAllocator().get()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7926 | completionBytes = completionAllocator->getTotalMemory(); |
| 7927 | } |
| 7928 | createCXTUResourceUsageEntry(*entries, |
| 7929 | CXTUResourceUsage_GlobalCompletionResults, |
| 7930 | completionBytes); |
| 7931 | |
| 7932 | // How much memory is being used by SourceManager's content cache? |
| 7933 | createCXTUResourceUsageEntry(*entries, |
| 7934 | CXTUResourceUsage_SourceManagerContentCache, |
| 7935 | (unsigned long) astContext.getSourceManager().getContentCacheSize()); |
| 7936 | |
| 7937 | // How much memory is being used by the MemoryBuffer's in SourceManager? |
| 7938 | const SourceManager::MemoryBufferSizes &srcBufs = |
| 7939 | astUnit->getSourceManager().getMemoryBufferSizes(); |
| 7940 | |
| 7941 | createCXTUResourceUsageEntry(*entries, |
| 7942 | CXTUResourceUsage_SourceManager_Membuffer_Malloc, |
| 7943 | (unsigned long) srcBufs.malloc_bytes); |
| 7944 | createCXTUResourceUsageEntry(*entries, |
| 7945 | CXTUResourceUsage_SourceManager_Membuffer_MMap, |
| 7946 | (unsigned long) srcBufs.mmap_bytes); |
| 7947 | createCXTUResourceUsageEntry(*entries, |
| 7948 | CXTUResourceUsage_SourceManager_DataStructures, |
| 7949 | (unsigned long) astContext.getSourceManager() |
| 7950 | .getDataStructureSizes()); |
| 7951 | |
| 7952 | // How much memory is being used by the ExternalASTSource? |
| 7953 | if (ExternalASTSource *esrc = astContext.getExternalSource()) { |
| 7954 | const ExternalASTSource::MemoryBufferSizes &sizes = |
| 7955 | esrc->getMemoryBufferSizes(); |
| 7956 | |
| 7957 | createCXTUResourceUsageEntry(*entries, |
| 7958 | CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc, |
| 7959 | (unsigned long) sizes.malloc_bytes); |
| 7960 | createCXTUResourceUsageEntry(*entries, |
| 7961 | CXTUResourceUsage_ExternalASTSource_Membuffer_MMap, |
| 7962 | (unsigned long) sizes.mmap_bytes); |
| 7963 | } |
| 7964 | |
| 7965 | // How much memory is being used by the Preprocessor? |
| 7966 | Preprocessor &pp = astUnit->getPreprocessor(); |
| 7967 | createCXTUResourceUsageEntry(*entries, |
| 7968 | CXTUResourceUsage_Preprocessor, |
| 7969 | pp.getTotalMemory()); |
| 7970 | |
| 7971 | if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) { |
| 7972 | createCXTUResourceUsageEntry(*entries, |
| 7973 | CXTUResourceUsage_PreprocessingRecord, |
| 7974 | pRec->getTotalMemory()); |
| 7975 | } |
| 7976 | |
| 7977 | createCXTUResourceUsageEntry(*entries, |
| 7978 | CXTUResourceUsage_Preprocessor_HeaderSearch, |
| 7979 | pp.getHeaderSearchInfo().getTotalMemory()); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7980 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7981 | CXTUResourceUsage usage = { (void*) entries.get(), |
| 7982 | (unsigned) entries->size(), |
Alexander Kornienko | 6ee521c | 2015-01-23 15:36:10 +0000 | [diff] [blame] | 7983 | !entries->empty() ? &(*entries)[0] : nullptr }; |
Eric Fiselier | e95fc44 | 2016-11-14 07:03:50 +0000 | [diff] [blame] | 7984 | (void)entries.release(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7985 | return usage; |
| 7986 | } |
| 7987 | |
| 7988 | void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) { |
| 7989 | if (usage.data) |
| 7990 | delete (MemUsageEntries*) usage.data; |
| 7991 | } |
| 7992 | |
Argyrios Kyrtzidis | 0e282ef | 2013-12-06 18:55:45 +0000 | [diff] [blame] | 7993 | CXSourceRangeList *clang_getSkippedRanges(CXTranslationUnit TU, CXFile file) { |
| 7994 | CXSourceRangeList *skipped = new CXSourceRangeList; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 7995 | skipped->count = 0; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7996 | skipped->ranges = nullptr; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 7997 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7998 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7999 | LOG_BAD_TU(TU); |
| 8000 | return skipped; |
| 8001 | } |
| 8002 | |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8003 | if (!file) |
| 8004 | return skipped; |
| 8005 | |
| 8006 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
| 8007 | PreprocessingRecord *ppRec = astUnit->getPreprocessor().getPreprocessingRecord(); |
| 8008 | if (!ppRec) |
| 8009 | return skipped; |
| 8010 | |
| 8011 | ASTContext &Ctx = astUnit->getASTContext(); |
| 8012 | SourceManager &sm = Ctx.getSourceManager(); |
| 8013 | FileEntry *fileEntry = static_cast<FileEntry *>(file); |
| 8014 | FileID wantedFileID = sm.translateFile(fileEntry); |
| 8015 | |
| 8016 | const std::vector<SourceRange> &SkippedRanges = ppRec->getSkippedRanges(); |
| 8017 | std::vector<SourceRange> wantedRanges; |
| 8018 | for (std::vector<SourceRange>::const_iterator i = SkippedRanges.begin(), ei = SkippedRanges.end(); |
| 8019 | i != ei; ++i) { |
| 8020 | if (sm.getFileID(i->getBegin()) == wantedFileID || sm.getFileID(i->getEnd()) == wantedFileID) |
| 8021 | wantedRanges.push_back(*i); |
| 8022 | } |
| 8023 | |
| 8024 | skipped->count = wantedRanges.size(); |
| 8025 | skipped->ranges = new CXSourceRange[skipped->count]; |
| 8026 | for (unsigned i = 0, ei = skipped->count; i != ei; ++i) |
| 8027 | skipped->ranges[i] = cxloc::translateSourceRange(Ctx, wantedRanges[i]); |
| 8028 | |
| 8029 | return skipped; |
| 8030 | } |
| 8031 | |
Cameron Desrochers | d809128 | 2016-08-18 15:43:55 +0000 | [diff] [blame] | 8032 | CXSourceRangeList *clang_getAllSkippedRanges(CXTranslationUnit TU) { |
| 8033 | CXSourceRangeList *skipped = new CXSourceRangeList; |
| 8034 | skipped->count = 0; |
| 8035 | skipped->ranges = nullptr; |
| 8036 | |
| 8037 | if (isNotUsableTU(TU)) { |
| 8038 | LOG_BAD_TU(TU); |
| 8039 | return skipped; |
| 8040 | } |
| 8041 | |
| 8042 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
| 8043 | PreprocessingRecord *ppRec = astUnit->getPreprocessor().getPreprocessingRecord(); |
| 8044 | if (!ppRec) |
| 8045 | return skipped; |
| 8046 | |
| 8047 | ASTContext &Ctx = astUnit->getASTContext(); |
| 8048 | |
| 8049 | const std::vector<SourceRange> &SkippedRanges = ppRec->getSkippedRanges(); |
| 8050 | |
| 8051 | skipped->count = SkippedRanges.size(); |
| 8052 | skipped->ranges = new CXSourceRange[skipped->count]; |
| 8053 | for (unsigned i = 0, ei = skipped->count; i != ei; ++i) |
| 8054 | skipped->ranges[i] = cxloc::translateSourceRange(Ctx, SkippedRanges[i]); |
| 8055 | |
| 8056 | return skipped; |
| 8057 | } |
| 8058 | |
Argyrios Kyrtzidis | 0e282ef | 2013-12-06 18:55:45 +0000 | [diff] [blame] | 8059 | void clang_disposeSourceRangeList(CXSourceRangeList *ranges) { |
| 8060 | if (ranges) { |
| 8061 | delete[] ranges->ranges; |
| 8062 | delete ranges; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8063 | } |
| 8064 | } |
| 8065 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8066 | void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) { |
| 8067 | CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU); |
| 8068 | for (unsigned I = 0; I != Usage.numEntries; ++I) |
| 8069 | fprintf(stderr, " %s: %lu\n", |
| 8070 | clang_getTUResourceUsageName(Usage.entries[I].kind), |
| 8071 | Usage.entries[I].amount); |
| 8072 | |
| 8073 | clang_disposeCXTUResourceUsage(Usage); |
| 8074 | } |
| 8075 | |
| 8076 | //===----------------------------------------------------------------------===// |
| 8077 | // Misc. utility functions. |
| 8078 | //===----------------------------------------------------------------------===// |
| 8079 | |
| 8080 | /// Default to using an 8 MB stack size on "safety" threads. |
| 8081 | static unsigned SafetyStackThreadSize = 8 << 20; |
| 8082 | |
| 8083 | namespace clang { |
| 8084 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 8085 | bool RunSafely(llvm::CrashRecoveryContext &CRC, llvm::function_ref<void()> Fn, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8086 | unsigned Size) { |
| 8087 | if (!Size) |
| 8088 | Size = GetSafetyThreadStackSize(); |
| 8089 | if (Size) |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 8090 | return CRC.RunSafelyOnThread(Fn, Size); |
| 8091 | return CRC.RunSafely(Fn); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8092 | } |
| 8093 | |
| 8094 | unsigned GetSafetyThreadStackSize() { |
| 8095 | return SafetyStackThreadSize; |
| 8096 | } |
| 8097 | |
| 8098 | void SetSafetyThreadStackSize(unsigned Value) { |
| 8099 | SafetyStackThreadSize = Value; |
| 8100 | } |
| 8101 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 8102 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8103 | |
| 8104 | void clang::setThreadBackgroundPriority() { |
| 8105 | if (getenv("LIBCLANG_BGPRIO_DISABLE")) |
| 8106 | return; |
| 8107 | |
Alp Toker | 1a86ad2 | 2014-07-06 06:24:00 +0000 | [diff] [blame] | 8108 | #ifdef USE_DARWIN_THREADS |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8109 | setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG); |
| 8110 | #endif |
| 8111 | } |
| 8112 | |
| 8113 | void cxindex::printDiagsToStderr(ASTUnit *Unit) { |
| 8114 | if (!Unit) |
| 8115 | return; |
| 8116 | |
| 8117 | for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), |
| 8118 | DEnd = Unit->stored_diag_end(); |
| 8119 | D != DEnd; ++D) { |
Ben Langmuir | 749323f | 2014-04-22 17:40:12 +0000 | [diff] [blame] | 8120 | CXStoredDiagnostic Diag(*D, Unit->getLangOpts()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8121 | CXString Msg = clang_formatDiagnostic(&Diag, |
| 8122 | clang_defaultDiagnosticDisplayOptions()); |
| 8123 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 8124 | clang_disposeString(Msg); |
| 8125 | } |
| 8126 | #ifdef LLVM_ON_WIN32 |
| 8127 | // On Windows, force a flush, since there may be multiple copies of |
| 8128 | // stderr and stdout in the file system, all with different buffers |
| 8129 | // but writing to the same device. |
| 8130 | fflush(stderr); |
| 8131 | #endif |
| 8132 | } |
| 8133 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8134 | MacroInfo *cxindex::getMacroInfo(const IdentifierInfo &II, |
| 8135 | SourceLocation MacroDefLoc, |
| 8136 | CXTranslationUnit TU){ |
| 8137 | if (MacroDefLoc.isInvalid() || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8138 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8139 | if (!II.hadMacroDefinition()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8140 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8141 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8142 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 8143 | Preprocessor &PP = Unit->getPreprocessor(); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 8144 | MacroDirective *MD = PP.getLocalMacroDirectiveHistory(&II); |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 8145 | if (MD) { |
| 8146 | for (MacroDirective::DefInfo |
| 8147 | Def = MD->getDefinition(); Def; Def = Def.getPreviousDefinition()) { |
| 8148 | if (MacroDefLoc == Def.getMacroInfo()->getDefinitionLoc()) |
| 8149 | return Def.getMacroInfo(); |
| 8150 | } |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8151 | } |
| 8152 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8153 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8154 | } |
| 8155 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 8156 | const MacroInfo *cxindex::getMacroInfo(const MacroDefinitionRecord *MacroDef, |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 8157 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8158 | if (!MacroDef || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8159 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8160 | const IdentifierInfo *II = MacroDef->getName(); |
| 8161 | if (!II) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8162 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8163 | |
| 8164 | return getMacroInfo(*II, MacroDef->getLocation(), TU); |
| 8165 | } |
| 8166 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 8167 | MacroDefinitionRecord * |
| 8168 | cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, const Token &Tok, |
| 8169 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8170 | if (!MI || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8171 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8172 | if (Tok.isNot(tok::raw_identifier)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8173 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8174 | |
| 8175 | if (MI->getNumTokens() == 0) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8176 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8177 | SourceRange DefRange(MI->getReplacementToken(0).getLocation(), |
| 8178 | MI->getDefinitionEndLoc()); |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8179 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8180 | |
| 8181 | // Check that the token is inside the definition and not its argument list. |
| 8182 | SourceManager &SM = Unit->getSourceManager(); |
| 8183 | if (SM.isBeforeInTranslationUnit(Tok.getLocation(), DefRange.getBegin())) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8184 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8185 | if (SM.isBeforeInTranslationUnit(DefRange.getEnd(), Tok.getLocation())) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8186 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8187 | |
| 8188 | Preprocessor &PP = Unit->getPreprocessor(); |
| 8189 | PreprocessingRecord *PPRec = PP.getPreprocessingRecord(); |
| 8190 | if (!PPRec) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8191 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8192 | |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 8193 | IdentifierInfo &II = PP.getIdentifierTable().get(Tok.getRawIdentifier()); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8194 | if (!II.hadMacroDefinition()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8195 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8196 | |
| 8197 | // Check that the identifier is not one of the macro arguments. |
| 8198 | if (std::find(MI->arg_begin(), MI->arg_end(), &II) != MI->arg_end()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8199 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8200 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 8201 | MacroDirective *InnerMD = PP.getLocalMacroDirectiveHistory(&II); |
Argyrios Kyrtzidis | 09c9e81 | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 8202 | if (!InnerMD) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8203 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8204 | |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 8205 | return PPRec->findMacroDefinition(InnerMD->getMacroInfo()); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8206 | } |
| 8207 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 8208 | MacroDefinitionRecord * |
| 8209 | cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, SourceLocation Loc, |
| 8210 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8211 | if (Loc.isInvalid() || !MI || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8212 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8213 | |
| 8214 | if (MI->getNumTokens() == 0) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8215 | return nullptr; |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8216 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8217 | Preprocessor &PP = Unit->getPreprocessor(); |
| 8218 | if (!PP.getPreprocessingRecord()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8219 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8220 | Loc = Unit->getSourceManager().getSpellingLoc(Loc); |
| 8221 | Token Tok; |
| 8222 | if (PP.getRawToken(Loc, Tok)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8223 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8224 | |
| 8225 | return checkForMacroInMacroDefinition(MI, Tok, TU); |
| 8226 | } |
| 8227 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8228 | CXString clang_getClangVersion() { |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 8229 | return cxstring::createDup(getClangFullVersion()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8230 | } |
| 8231 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8232 | Logger &cxindex::Logger::operator<<(CXTranslationUnit TU) { |
| 8233 | if (TU) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8234 | if (ASTUnit *Unit = cxtu::getASTUnit(TU)) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8235 | LogOS << '<' << Unit->getMainFileName() << '>'; |
Argyrios Kyrtzidis | 37f2ab4 | 2013-03-05 20:21:14 +0000 | [diff] [blame] | 8236 | if (Unit->isMainFileAST()) |
| 8237 | LogOS << " (" << Unit->getASTFileName() << ')'; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8238 | return *this; |
| 8239 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 8240 | } else { |
| 8241 | LogOS << "<NULL TU>"; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8242 | } |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8243 | return *this; |
| 8244 | } |
| 8245 | |
Argyrios Kyrtzidis | ba4b5f8 | 2013-03-08 02:32:26 +0000 | [diff] [blame] | 8246 | Logger &cxindex::Logger::operator<<(const FileEntry *FE) { |
| 8247 | *this << FE->getName(); |
| 8248 | return *this; |
| 8249 | } |
| 8250 | |
| 8251 | Logger &cxindex::Logger::operator<<(CXCursor cursor) { |
| 8252 | CXString cursorName = clang_getCursorDisplayName(cursor); |
| 8253 | *this << cursorName << "@" << clang_getCursorLocation(cursor); |
| 8254 | clang_disposeString(cursorName); |
| 8255 | return *this; |
| 8256 | } |
| 8257 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8258 | Logger &cxindex::Logger::operator<<(CXSourceLocation Loc) { |
| 8259 | CXFile File; |
| 8260 | unsigned Line, Column; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8261 | clang_getFileLocation(Loc, &File, &Line, &Column, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8262 | CXString FileName = clang_getFileName(File); |
| 8263 | *this << llvm::format("(%s:%d:%d)", clang_getCString(FileName), Line, Column); |
| 8264 | clang_disposeString(FileName); |
| 8265 | return *this; |
| 8266 | } |
| 8267 | |
| 8268 | Logger &cxindex::Logger::operator<<(CXSourceRange range) { |
| 8269 | CXSourceLocation BLoc = clang_getRangeStart(range); |
| 8270 | CXSourceLocation ELoc = clang_getRangeEnd(range); |
| 8271 | |
| 8272 | CXFile BFile; |
| 8273 | unsigned BLine, BColumn; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8274 | clang_getFileLocation(BLoc, &BFile, &BLine, &BColumn, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8275 | |
| 8276 | CXFile EFile; |
| 8277 | unsigned ELine, EColumn; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8278 | clang_getFileLocation(ELoc, &EFile, &ELine, &EColumn, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8279 | |
| 8280 | CXString BFileName = clang_getFileName(BFile); |
| 8281 | if (BFile == EFile) { |
| 8282 | *this << llvm::format("[%s %d:%d-%d:%d]", clang_getCString(BFileName), |
| 8283 | BLine, BColumn, ELine, EColumn); |
| 8284 | } else { |
| 8285 | CXString EFileName = clang_getFileName(EFile); |
| 8286 | *this << llvm::format("[%s:%d:%d - ", clang_getCString(BFileName), |
| 8287 | BLine, BColumn) |
| 8288 | << llvm::format("%s:%d:%d]", clang_getCString(EFileName), |
| 8289 | ELine, EColumn); |
| 8290 | clang_disposeString(EFileName); |
| 8291 | } |
| 8292 | clang_disposeString(BFileName); |
| 8293 | return *this; |
| 8294 | } |
| 8295 | |
| 8296 | Logger &cxindex::Logger::operator<<(CXString Str) { |
| 8297 | *this << clang_getCString(Str); |
| 8298 | return *this; |
| 8299 | } |
| 8300 | |
| 8301 | Logger &cxindex::Logger::operator<<(const llvm::format_object_base &Fmt) { |
| 8302 | LogOS << Fmt; |
| 8303 | return *this; |
| 8304 | } |
| 8305 | |
Chandler Carruth | 37ad258 | 2014-06-27 15:14:39 +0000 | [diff] [blame] | 8306 | static llvm::ManagedStatic<llvm::sys::Mutex> LoggingMutex; |
| 8307 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8308 | cxindex::Logger::~Logger() { |
Chandler Carruth | 37ad258 | 2014-06-27 15:14:39 +0000 | [diff] [blame] | 8309 | llvm::sys::ScopedLock L(*LoggingMutex); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8310 | |
| 8311 | static llvm::TimeRecord sBeginTR = llvm::TimeRecord::getCurrentTime(); |
| 8312 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8313 | raw_ostream &OS = llvm::errs(); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8314 | OS << "[libclang:" << Name << ':'; |
| 8315 | |
Alp Toker | 1a86ad2 | 2014-07-06 06:24:00 +0000 | [diff] [blame] | 8316 | #ifdef USE_DARWIN_THREADS |
| 8317 | // TODO: Portability. |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8318 | mach_port_t tid = pthread_mach_thread_np(pthread_self()); |
| 8319 | OS << tid << ':'; |
| 8320 | #endif |
| 8321 | |
| 8322 | llvm::TimeRecord TR = llvm::TimeRecord::getCurrentTime(); |
| 8323 | OS << llvm::format("%7.4f] ", TR.getWallTime() - sBeginTR.getWallTime()); |
Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 8324 | OS << Msg << '\n'; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8325 | |
| 8326 | if (Trace) { |
Zachary Turner | 1fe2a8d | 2015-03-05 19:15:09 +0000 | [diff] [blame] | 8327 | llvm::sys::PrintStackTrace(OS); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8328 | OS << "--------------------------------------------------\n"; |
| 8329 | } |
| 8330 | } |
Benjamin Kramer | c1ffdab | 2016-03-03 08:58:18 +0000 | [diff] [blame] | 8331 | |
| 8332 | #ifdef CLANG_TOOL_EXTRA_BUILD |
| 8333 | // This anchor is used to force the linker to link the clang-tidy plugin. |
| 8334 | extern volatile int ClangTidyPluginAnchorSource; |
| 8335 | static int LLVM_ATTRIBUTE_UNUSED ClangTidyPluginAnchorDestination = |
| 8336 | ClangTidyPluginAnchorSource; |
Benjamin Kramer | 9eba735 | 2016-11-17 15:22:36 +0000 | [diff] [blame] | 8337 | |
| 8338 | // This anchor is used to force the linker to link the clang-include-fixer |
| 8339 | // plugin. |
| 8340 | extern volatile int ClangIncludeFixerPluginAnchorSource; |
| 8341 | static int LLVM_ATTRIBUTE_UNUSED ClangIncludeFixerPluginAnchorDestination = |
| 8342 | ClangIncludeFixerPluginAnchorSource; |
Benjamin Kramer | c1ffdab | 2016-03-03 08:58:18 +0000 | [diff] [blame] | 8343 | #endif |