Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1 | //===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the main API hooks in the Clang-C Source Indexing |
| 10 | // library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 14 | #include "CIndexDiagnostic.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 15 | #include "CIndexer.h" |
Chandler Carruth | 4b41745 | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 16 | #include "CLog.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 17 | #include "CXCursor.h" |
| 18 | #include "CXSourceLocation.h" |
| 19 | #include "CXString.h" |
| 20 | #include "CXTranslationUnit.h" |
| 21 | #include "CXType.h" |
| 22 | #include "CursorVisitor.h" |
Jan Korous | f7d2376 | 2019-09-12 22:55:55 +0000 | [diff] [blame] | 23 | #include "clang-c/FatalErrorHandler.h" |
David Blaikie | 0a4e61f | 2013-09-13 18:32:52 +0000 | [diff] [blame] | 24 | #include "clang/AST/Attr.h" |
Puyan Lotfi | 9721fbf | 2020-04-23 02:20:56 -0400 | [diff] [blame] | 25 | #include "clang/AST/DeclObjCCommon.h" |
Jan Korous | 7e36ecd | 2019-09-05 20:33:52 +0000 | [diff] [blame] | 26 | #include "clang/AST/Mangle.h" |
Alexey Bataev | 9559834 | 2020-02-10 15:49:05 -0500 | [diff] [blame] | 27 | #include "clang/AST/OpenMPClause.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 28 | #include "clang/AST/StmtVisitor.h" |
| 29 | #include "clang/Basic/Diagnostic.h" |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 30 | #include "clang/Basic/DiagnosticCategories.h" |
| 31 | #include "clang/Basic/DiagnosticIDs.h" |
Richard Smith | 0a7b297 | 2018-07-03 21:34:13 +0000 | [diff] [blame] | 32 | #include "clang/Basic/Stack.h" |
Emilio Cobos Alvarez | 485ad42 | 2017-04-28 15:56:39 +0000 | [diff] [blame] | 33 | #include "clang/Basic/TargetInfo.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 34 | #include "clang/Basic/Version.h" |
| 35 | #include "clang/Frontend/ASTUnit.h" |
| 36 | #include "clang/Frontend/CompilerInstance.h" |
Dmitri Gribenko | 9e60511 | 2013-11-13 22:16:51 +0000 | [diff] [blame] | 37 | #include "clang/Index/CommentToXML.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 38 | #include "clang/Lex/HeaderSearch.h" |
| 39 | #include "clang/Lex/Lexer.h" |
| 40 | #include "clang/Lex/PreprocessingRecord.h" |
| 41 | #include "clang/Lex/Preprocessor.h" |
| 42 | #include "llvm/ADT/Optional.h" |
| 43 | #include "llvm/ADT/STLExtras.h" |
| 44 | #include "llvm/ADT/StringSwitch.h" |
Alp Toker | 1d257e1 | 2014-06-04 03:28:55 +0000 | [diff] [blame] | 45 | #include "llvm/Config/llvm-config.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Compiler.h" |
| 47 | #include "llvm/Support/CrashRecoveryContext.h" |
Chandler Carruth | 4b41745 | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Format.h" |
Chandler Carruth | 37ad258 | 2014-06-27 15:14:39 +0000 | [diff] [blame] | 49 | #include "llvm/Support/ManagedStatic.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 50 | #include "llvm/Support/MemoryBuffer.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" |
Benjamin Kramer | 762bc33 | 2019-08-07 14:44:40 +0000 | [diff] [blame] | 58 | #include <mutex> |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 59 | |
Alp Toker | 1a86ad2 | 2014-07-06 06:24:00 +0000 | [diff] [blame] | 60 | #if LLVM_ENABLE_THREADS != 0 && defined(__APPLE__) |
| 61 | #define USE_DARWIN_THREADS |
| 62 | #endif |
| 63 | |
| 64 | #ifdef USE_DARWIN_THREADS |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 65 | #include <pthread.h> |
| 66 | #endif |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 67 | |
| 68 | using namespace clang; |
| 69 | using namespace clang::cxcursor; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 70 | using namespace clang::cxtu; |
| 71 | using namespace clang::cxindex; |
| 72 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 73 | CXTranslationUnit cxtu::MakeCXTranslationUnit(CIndexer *CIdx, |
| 74 | std::unique_ptr<ASTUnit> AU) { |
Dmitri Gribenko | d36209e | 2013-01-26 21:32:42 +0000 | [diff] [blame] | 75 | if (!AU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 76 | return nullptr; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 77 | assert(CIdx); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 78 | CXTranslationUnit D = new CXTranslationUnitImpl(); |
| 79 | D->CIdx = CIdx; |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 80 | D->TheASTUnit = AU.release(); |
Dmitri Gribenko | 7489521 | 2013-02-03 13:52:47 +0000 | [diff] [blame] | 81 | D->StringPool = new cxstring::CXStringPool(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 82 | D->Diagnostics = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 83 | D->OverridenCursorsPool = createOverridenCXCursorsPool(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 84 | D->CommentToXML = nullptr; |
Alex Lorenz | 690f0e2 | 2017-12-07 20:37:50 +0000 | [diff] [blame] | 85 | D->ParsingOptions = 0; |
| 86 | D->Arguments = {}; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 87 | return D; |
| 88 | } |
| 89 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 90 | bool cxtu::isASTReadError(ASTUnit *AU) { |
| 91 | for (ASTUnit::stored_diag_iterator D = AU->stored_diag_begin(), |
| 92 | DEnd = AU->stored_diag_end(); |
| 93 | D != DEnd; ++D) { |
| 94 | if (D->getLevel() >= DiagnosticsEngine::Error && |
| 95 | DiagnosticIDs::getCategoryNumberForDiag(D->getID()) == |
| 96 | diag::DiagCat_AST_Deserialization_Issue) |
| 97 | return true; |
| 98 | } |
| 99 | return false; |
| 100 | } |
| 101 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 102 | cxtu::CXTUOwner::~CXTUOwner() { |
| 103 | if (TU) |
| 104 | clang_disposeTranslationUnit(TU); |
| 105 | } |
| 106 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 107 | /// Compare two source ranges to determine their relative position in |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 108 | /// the translation unit. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 109 | static RangeComparisonResult RangeCompare(SourceManager &SM, SourceRange R1, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 110 | SourceRange R2) { |
| 111 | assert(R1.isValid() && "First range is invalid?"); |
| 112 | assert(R2.isValid() && "Second range is invalid?"); |
| 113 | if (R1.getEnd() != R2.getBegin() && |
| 114 | SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin())) |
| 115 | return RangeBefore; |
| 116 | if (R2.getEnd() != R1.getBegin() && |
| 117 | SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin())) |
| 118 | return RangeAfter; |
| 119 | return RangeOverlap; |
| 120 | } |
| 121 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 122 | /// Determine if a source location falls within, before, or after a |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 123 | /// a given source range. |
| 124 | static RangeComparisonResult LocationCompare(SourceManager &SM, |
| 125 | SourceLocation L, SourceRange R) { |
| 126 | assert(R.isValid() && "First range is invalid?"); |
| 127 | assert(L.isValid() && "Second range is invalid?"); |
| 128 | if (L == R.getBegin() || L == R.getEnd()) |
| 129 | return RangeOverlap; |
| 130 | if (SM.isBeforeInTranslationUnit(L, R.getBegin())) |
| 131 | return RangeBefore; |
| 132 | if (SM.isBeforeInTranslationUnit(R.getEnd(), L)) |
| 133 | return RangeAfter; |
| 134 | return RangeOverlap; |
| 135 | } |
| 136 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 137 | /// Translate a Clang source range into a CIndex source range. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 138 | /// |
| 139 | /// Clang internally represents ranges where the end location points to the |
| 140 | /// start of the token at the end. However, for external clients it is more |
| 141 | /// useful to have a CXSourceRange be a proper half-open interval. This routine |
| 142 | /// does the appropriate translation. |
| 143 | CXSourceRange cxloc::translateSourceRange(const SourceManager &SM, |
| 144 | const LangOptions &LangOpts, |
| 145 | const CharSourceRange &R) { |
| 146 | // We want the last character in this location, so we will adjust the |
| 147 | // location accordingly. |
| 148 | SourceLocation EndLoc = R.getEnd(); |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 149 | bool IsTokenRange = R.isTokenRange(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 150 | if (EndLoc.isValid() && EndLoc.isMacroID() && |
| 151 | !SM.isMacroArgExpansion(EndLoc)) { |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 152 | CharSourceRange Expansion = SM.getExpansionRange(EndLoc); |
| 153 | EndLoc = Expansion.getEnd(); |
| 154 | IsTokenRange = Expansion.isTokenRange(); |
| 155 | } |
| 156 | if (IsTokenRange && EndLoc.isValid()) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 157 | unsigned Length = |
| 158 | Lexer::MeasureTokenLength(SM.getSpellingLoc(EndLoc), SM, LangOpts); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 159 | EndLoc = EndLoc.getLocWithOffset(Length); |
| 160 | } |
| 161 | |
Bill Wendling | eade362 | 2013-01-23 08:25:41 +0000 | [diff] [blame] | 162 | CXSourceRange Result = { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 163 | {&SM, &LangOpts}, R.getBegin().getRawEncoding(), EndLoc.getRawEncoding()}; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 164 | return Result; |
| 165 | } |
| 166 | |
| 167 | //===----------------------------------------------------------------------===// |
| 168 | // Cursor visitor. |
| 169 | //===----------------------------------------------------------------------===// |
| 170 | |
| 171 | static SourceRange getRawCursorExtent(CXCursor C); |
| 172 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr); |
| 173 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 174 | RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { |
| 175 | return RangeCompare(AU->getSourceManager(), R, RegionOfInterest); |
| 176 | } |
| 177 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 178 | /// Visit the given cursor and, if requested by the visitor, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 179 | /// its children. |
| 180 | /// |
| 181 | /// \param Cursor the cursor to visit. |
| 182 | /// |
| 183 | /// \param CheckedRegionOfInterest if true, then the caller already checked |
| 184 | /// that this cursor is within the region of interest. |
| 185 | /// |
| 186 | /// \returns true if the visitation should be aborted, false if it |
| 187 | /// should continue. |
| 188 | bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { |
| 189 | if (clang_isInvalid(Cursor.kind)) |
| 190 | return false; |
| 191 | |
| 192 | if (clang_isDeclaration(Cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 193 | const Decl *D = getCursorDecl(Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 194 | if (!D) { |
| 195 | assert(0 && "Invalid declaration cursor"); |
| 196 | return true; // abort. |
| 197 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 198 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 199 | // Ignore implicit declarations, unless it's an objc method because |
| 200 | // currently we should report implicit methods for properties when indexing. |
| 201 | if (D->isImplicit() && !isa<ObjCMethodDecl>(D)) |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | // If we have a range of interest, and this cursor doesn't intersect with it, |
| 206 | // we're done. |
| 207 | if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { |
| 208 | SourceRange Range = getRawCursorExtent(Cursor); |
| 209 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 214 | case CXChildVisit_Break: |
| 215 | return true; |
| 216 | |
| 217 | case CXChildVisit_Continue: |
| 218 | return false; |
| 219 | |
| 220 | case CXChildVisit_Recurse: { |
| 221 | bool ret = VisitChildren(Cursor); |
| 222 | if (PostChildrenVisitor) |
| 223 | if (PostChildrenVisitor(Cursor, ClientData)) |
| 224 | return true; |
| 225 | return ret; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | llvm_unreachable("Invalid CXChildVisitResult!"); |
| 230 | } |
| 231 | |
| 232 | static bool visitPreprocessedEntitiesInRange(SourceRange R, |
| 233 | PreprocessingRecord &PPRec, |
| 234 | CursorVisitor &Visitor) { |
| 235 | SourceManager &SM = Visitor.getASTUnit()->getSourceManager(); |
| 236 | FileID FID; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 237 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 238 | if (!Visitor.shouldVisitIncludedEntities()) { |
| 239 | // If the begin/end of the range lie in the same FileID, do the optimization |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 240 | // where we skip preprocessed entities that do not come from the same |
| 241 | // FileID. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 242 | FID = SM.getFileID(SM.getFileLoc(R.getBegin())); |
| 243 | if (FID != SM.getFileID(SM.getFileLoc(R.getEnd()))) |
| 244 | FID = FileID(); |
| 245 | } |
| 246 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 247 | const auto &Entities = PPRec.getPreprocessedEntitiesInRange(R); |
| 248 | return Visitor.visitPreprocessedEntities(Entities.begin(), Entities.end(), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 249 | PPRec, FID); |
| 250 | } |
| 251 | |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 252 | bool CursorVisitor::visitFileRegion() { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 253 | if (RegionOfInterest.isInvalid()) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 254 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 255 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 256 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 257 | SourceManager &SM = Unit->getSourceManager(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 258 | |
| 259 | std::pair<FileID, unsigned> Begin = SM.getDecomposedLoc( |
| 260 | SM.getFileLoc(RegionOfInterest.getBegin())), |
| 261 | End = SM.getDecomposedLoc( |
| 262 | SM.getFileLoc(RegionOfInterest.getEnd())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 263 | |
| 264 | if (End.first != Begin.first) { |
| 265 | // If the end does not reside in the same file, try to recover by |
| 266 | // picking the end of the file of begin location. |
| 267 | End.first = Begin.first; |
| 268 | End.second = SM.getFileIDSize(Begin.first); |
| 269 | } |
| 270 | |
| 271 | assert(Begin.first == End.first); |
| 272 | if (Begin.second > End.second) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 273 | return false; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 274 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 275 | FileID File = Begin.first; |
| 276 | unsigned Offset = Begin.second; |
| 277 | unsigned Length = End.second - Begin.second; |
| 278 | |
| 279 | if (!VisitDeclsOnly && !VisitPreprocessorLast) |
| 280 | if (visitPreprocessedEntitiesInRegion()) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 281 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 282 | |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 283 | if (visitDeclsFromFileRegion(File, Offset, Length)) |
| 284 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 285 | |
| 286 | if (!VisitDeclsOnly && VisitPreprocessorLast) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 287 | return visitPreprocessedEntitiesInRegion(); |
| 288 | |
| 289 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static bool isInLexicalContext(Decl *D, DeclContext *DC) { |
| 293 | if (!DC) |
| 294 | return false; |
| 295 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 296 | for (DeclContext *DeclDC = D->getLexicalDeclContext(); DeclDC; |
| 297 | DeclDC = DeclDC->getLexicalParent()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 298 | if (DeclDC == DC) |
| 299 | return true; |
| 300 | } |
| 301 | return false; |
| 302 | } |
| 303 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 304 | bool CursorVisitor::visitDeclsFromFileRegion(FileID File, unsigned Offset, |
| 305 | unsigned Length) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 306 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 307 | SourceManager &SM = Unit->getSourceManager(); |
| 308 | SourceRange Range = RegionOfInterest; |
| 309 | |
| 310 | SmallVector<Decl *, 16> Decls; |
| 311 | Unit->findFileRegionDecls(File, Offset, Length, Decls); |
| 312 | |
| 313 | // If we didn't find any file level decls for the file, try looking at the |
| 314 | // file that it was included from. |
| 315 | while (Decls.empty() || Decls.front()->isTopLevelDeclInObjCContainer()) { |
| 316 | bool Invalid = false; |
| 317 | const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid); |
| 318 | if (Invalid) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 319 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 320 | |
| 321 | SourceLocation Outer; |
| 322 | if (SLEntry.isFile()) |
| 323 | Outer = SLEntry.getFile().getIncludeLoc(); |
| 324 | else |
| 325 | Outer = SLEntry.getExpansion().getExpansionLocStart(); |
| 326 | if (Outer.isInvalid()) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 327 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 328 | |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 329 | std::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 330 | Length = 0; |
| 331 | Unit->findFileRegionDecls(File, Offset, Length, Decls); |
| 332 | } |
| 333 | |
| 334 | assert(!Decls.empty()); |
| 335 | |
| 336 | bool VisitedAtLeastOnce = false; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 337 | DeclContext *CurDC = nullptr; |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 338 | SmallVectorImpl<Decl *>::iterator DIt = Decls.begin(); |
| 339 | for (SmallVectorImpl<Decl *>::iterator DE = Decls.end(); DIt != DE; ++DIt) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 340 | Decl *D = *DIt; |
| 341 | if (D->getSourceRange().isInvalid()) |
| 342 | continue; |
| 343 | |
| 344 | if (isInLexicalContext(D, CurDC)) |
| 345 | continue; |
| 346 | |
| 347 | CurDC = dyn_cast<DeclContext>(D); |
| 348 | |
| 349 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
| 350 | if (!TD->isFreeStanding()) |
| 351 | continue; |
| 352 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 353 | RangeComparisonResult CompRes = |
| 354 | RangeCompare(SM, D->getSourceRange(), Range); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 355 | if (CompRes == RangeBefore) |
| 356 | continue; |
| 357 | if (CompRes == RangeAfter) |
| 358 | break; |
| 359 | |
| 360 | assert(CompRes == RangeOverlap); |
| 361 | VisitedAtLeastOnce = true; |
| 362 | |
| 363 | if (isa<ObjCContainerDecl>(D)) { |
| 364 | FileDI_current = &DIt; |
| 365 | FileDE_current = DE; |
| 366 | } else { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 367 | FileDI_current = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true)) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 371 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | if (VisitedAtLeastOnce) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 375 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 376 | |
| 377 | // No Decls overlapped with the range. Move up the lexical context until there |
| 378 | // is a context that contains the range or we reach the translation unit |
| 379 | // level. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 380 | DeclContext *DC = DIt == Decls.begin() |
| 381 | ? (*DIt)->getLexicalDeclContext() |
| 382 | : (*(DIt - 1))->getLexicalDeclContext(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 383 | |
| 384 | while (DC && !DC->isTranslationUnit()) { |
| 385 | Decl *D = cast<Decl>(DC); |
| 386 | SourceRange CurDeclRange = D->getSourceRange(); |
| 387 | if (CurDeclRange.isInvalid()) |
| 388 | break; |
| 389 | |
| 390 | if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) { |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 391 | if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true)) |
| 392 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | DC = D->getLexicalDeclContext(); |
| 396 | } |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 397 | |
| 398 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | bool CursorVisitor::visitPreprocessedEntitiesInRegion() { |
| 402 | if (!AU->getPreprocessor().getPreprocessingRecord()) |
| 403 | return false; |
| 404 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 405 | PreprocessingRecord &PPRec = *AU->getPreprocessor().getPreprocessingRecord(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 406 | SourceManager &SM = AU->getSourceManager(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 407 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 408 | if (RegionOfInterest.isValid()) { |
| 409 | SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest); |
| 410 | SourceLocation B = MappedRange.getBegin(); |
| 411 | SourceLocation E = MappedRange.getEnd(); |
| 412 | |
| 413 | if (AU->isInPreambleFileID(B)) { |
| 414 | if (SM.isLoadedSourceLocation(E)) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 415 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, |
| 416 | *this); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 417 | |
| 418 | // Beginning of range lies in the preamble but it also extends beyond |
| 419 | // it into the main file. Split the range into 2 parts, one covering |
| 420 | // the preamble and another covering the main file. This allows subsequent |
| 421 | // calls to visitPreprocessedEntitiesInRange to accept a source range that |
| 422 | // lies in the same FileID, allowing it to skip preprocessed entities that |
| 423 | // do not come from the same FileID. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 424 | bool breaked = visitPreprocessedEntitiesInRange( |
| 425 | SourceRange(B, AU->getEndOfPreambleFileID()), PPRec, *this); |
| 426 | if (breaked) |
| 427 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 428 | return visitPreprocessedEntitiesInRange( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 429 | SourceRange(AU->getStartOfMainFileID(), E), PPRec, *this); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this); |
| 433 | } |
| 434 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 435 | bool OnlyLocalDecls = !AU->isMainFileAST() && AU->getOnlyLocalDecls(); |
| 436 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 437 | if (OnlyLocalDecls) |
| 438 | return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(), |
| 439 | PPRec); |
| 440 | |
| 441 | return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec); |
| 442 | } |
| 443 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 444 | template <typename InputIterator> |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 445 | bool CursorVisitor::visitPreprocessedEntities(InputIterator First, |
| 446 | InputIterator Last, |
| 447 | PreprocessingRecord &PPRec, |
| 448 | FileID FID) { |
| 449 | for (; First != Last; ++First) { |
| 450 | if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID)) |
| 451 | continue; |
| 452 | |
| 453 | PreprocessedEntity *PPE = *First; |
Argyrios Kyrtzidis | 1030f26 | 2013-05-07 20:37:17 +0000 | [diff] [blame] | 454 | if (!PPE) |
| 455 | continue; |
| 456 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 457 | if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) { |
| 458 | if (Visit(MakeMacroExpansionCursor(ME, TU))) |
| 459 | return true; |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 460 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 461 | continue; |
| 462 | } |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 463 | |
| 464 | if (MacroDefinitionRecord *MD = dyn_cast<MacroDefinitionRecord>(PPE)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 465 | if (Visit(MakeMacroDefinitionCursor(MD, TU))) |
| 466 | return true; |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 467 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 468 | continue; |
| 469 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 470 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 471 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) { |
| 472 | if (Visit(MakeInclusionDirectiveCursor(ID, TU))) |
| 473 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 474 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 475 | continue; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | return false; |
| 480 | } |
| 481 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 482 | /// Visit the children of the given cursor. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 483 | /// |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 484 | /// \returns true if the visitation should be aborted, false if it |
| 485 | /// should continue. |
| 486 | bool CursorVisitor::VisitChildren(CXCursor Cursor) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 487 | if (clang_isReference(Cursor.kind) && |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 488 | Cursor.kind != CXCursor_CXXBaseSpecifier) { |
| 489 | // By definition, references have no children. |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | // Set the Parent field to Cursor, then back to its old value once we're |
| 494 | // done. |
| 495 | SetParentRAII SetParent(Parent, StmtParent, Cursor); |
| 496 | |
| 497 | if (clang_isDeclaration(Cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 498 | Decl *D = const_cast<Decl *>(getCursorDecl(Cursor)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 499 | if (!D) |
| 500 | return false; |
| 501 | |
| 502 | return VisitAttributes(D) || Visit(D); |
| 503 | } |
| 504 | |
| 505 | if (clang_isStatement(Cursor.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 506 | if (const Stmt *S = getCursorStmt(Cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 507 | return Visit(S); |
| 508 | |
| 509 | return false; |
| 510 | } |
| 511 | |
| 512 | if (clang_isExpression(Cursor.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 513 | if (const Expr *E = getCursorExpr(Cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 514 | return Visit(E); |
| 515 | |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | if (clang_isTranslationUnit(Cursor.kind)) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 520 | CXTranslationUnit TU = getCursorTU(Cursor); |
| 521 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 522 | |
| 523 | int VisitOrder[2] = {VisitPreprocessorLast, !VisitPreprocessorLast}; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 524 | for (unsigned I = 0; I != 2; ++I) { |
| 525 | if (VisitOrder[I]) { |
| 526 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && |
| 527 | RegionOfInterest.isInvalid()) { |
| 528 | for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(), |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 529 | TLEnd = CXXUnit->top_level_end(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 530 | TL != TLEnd; ++TL) { |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 531 | const Optional<bool> V = handleDeclForVisitation(*TL); |
| 532 | if (!V.hasValue()) |
| 533 | continue; |
| 534 | return V.getValue(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 535 | } |
| 536 | } else if (VisitDeclContext( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 537 | CXXUnit->getASTContext().getTranslationUnitDecl())) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 538 | return true; |
| 539 | continue; |
| 540 | } |
| 541 | |
| 542 | // Walk the preprocessing record. |
| 543 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) |
| 544 | visitPreprocessedEntitiesInRegion(); |
| 545 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 546 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 547 | return false; |
| 548 | } |
| 549 | |
| 550 | if (Cursor.kind == CXCursor_CXXBaseSpecifier) { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 551 | if (const CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 552 | if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) { |
| 553 | return Visit(BaseTSInfo->getTypeLoc()); |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 559 | const IBOutletCollectionAttr *A = |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 560 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor)); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 561 | if (const ObjCObjectType *ObjT = A->getInterface()->getAs<ObjCObjectType>()) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 562 | return Visit(cxcursor::MakeCursorObjCClassRef( |
| 563 | ObjT->getInterface(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 564 | A->getInterfaceLoc()->getTypeLoc().getBeginLoc(), TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 567 | // If pointing inside a macro definition, check if the token is an identifier |
| 568 | // that was ever defined as a macro. In such a case, create a "pseudo" macro |
| 569 | // expansion cursor for that token. |
| 570 | SourceLocation BeginLoc = RegionOfInterest.getBegin(); |
| 571 | if (Cursor.kind == CXCursor_MacroDefinition && |
| 572 | BeginLoc == RegionOfInterest.getEnd()) { |
| 573 | SourceLocation Loc = AU->mapLocationToPreamble(BeginLoc); |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 574 | const MacroInfo *MI = |
| 575 | getMacroInfo(cxcursor::getCursorMacroDefinition(Cursor), TU); |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 576 | if (MacroDefinitionRecord *MacroDef = |
| 577 | checkForMacroInMacroDefinition(MI, Loc, TU)) |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 578 | return Visit(cxcursor::MakeMacroExpansionCursor(MacroDef, BeginLoc, TU)); |
| 579 | } |
| 580 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 581 | // Nothing to visit at the moment. |
| 582 | return false; |
| 583 | } |
| 584 | |
| 585 | bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { |
| 586 | if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten()) |
| 587 | if (Visit(TSInfo->getTypeLoc())) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 588 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 589 | |
| 590 | if (Stmt *Body = B->getBody()) |
| 591 | return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest)); |
| 592 | |
| 593 | return false; |
| 594 | } |
| 595 | |
Ted Kremenek | 0332558 | 2013-02-21 01:29:01 +0000 | [diff] [blame] | 596 | Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 597 | if (RegionOfInterest.isValid()) { |
| 598 | SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager()); |
| 599 | if (Range.isInvalid()) |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 600 | return None; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 601 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 602 | switch (CompareRegionOfInterest(Range)) { |
| 603 | case RangeBefore: |
| 604 | // This declaration comes before the region of interest; skip it. |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 605 | return None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 606 | |
| 607 | case RangeAfter: |
| 608 | // This declaration comes after the region of interest; we're done. |
| 609 | return false; |
| 610 | |
| 611 | case RangeOverlap: |
| 612 | // This declaration overlaps the region of interest; visit it. |
| 613 | break; |
| 614 | } |
| 615 | } |
| 616 | return true; |
| 617 | } |
| 618 | |
| 619 | bool CursorVisitor::VisitDeclContext(DeclContext *DC) { |
| 620 | DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 621 | |
| 622 | // FIXME: Eventually remove. This part of a hack to support proper |
| 623 | // iteration over all Decls contained lexically within an ObjC container. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 624 | SaveAndRestore<DeclContext::decl_iterator *> DI_saved(DI_current, &I); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 625 | SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E); |
| 626 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 627 | for (; I != E; ++I) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 628 | Decl *D = *I; |
| 629 | if (D->getLexicalDeclContext() != DC) |
| 630 | continue; |
Adrian Prantl | 2073dd2 | 2019-11-04 14:28:14 -0800 | [diff] [blame] | 631 | // Filter out synthesized property accessor redeclarations. |
| 632 | if (isa<ObjCImplDecl>(DC)) |
| 633 | if (auto *OMD = dyn_cast<ObjCMethodDecl>(D)) |
| 634 | if (OMD->isSynthesizedAccessorStub()) |
| 635 | continue; |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 636 | const Optional<bool> V = handleDeclForVisitation(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 637 | if (!V.hasValue()) |
| 638 | continue; |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 639 | return V.getValue(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 640 | } |
| 641 | return false; |
| 642 | } |
| 643 | |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 644 | Optional<bool> CursorVisitor::handleDeclForVisitation(const Decl *D) { |
| 645 | CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest); |
| 646 | |
| 647 | // Ignore synthesized ivars here, otherwise if we have something like: |
| 648 | // @synthesize prop = _prop; |
| 649 | // and '_prop' is not declared, we will encounter a '_prop' ivar before |
| 650 | // encountering the 'prop' synthesize declaration and we will think that |
| 651 | // we passed the region-of-interest. |
| 652 | if (auto *ivarD = dyn_cast<ObjCIvarDecl>(D)) { |
| 653 | if (ivarD->getSynthesize()) |
| 654 | return None; |
| 655 | } |
| 656 | |
| 657 | // FIXME: ObjCClassRef/ObjCProtocolRef for forward class/protocol |
| 658 | // declarations is a mismatch with the compiler semantics. |
| 659 | if (Cursor.kind == CXCursor_ObjCInterfaceDecl) { |
| 660 | auto *ID = cast<ObjCInterfaceDecl>(D); |
| 661 | if (!ID->isThisDeclarationADefinition()) |
| 662 | Cursor = MakeCursorObjCClassRef(ID, ID->getLocation(), TU); |
| 663 | |
| 664 | } else if (Cursor.kind == CXCursor_ObjCProtocolDecl) { |
| 665 | auto *PD = cast<ObjCProtocolDecl>(D); |
| 666 | if (!PD->isThisDeclarationADefinition()) |
| 667 | Cursor = MakeCursorObjCProtocolRef(PD, PD->getLocation(), TU); |
| 668 | } |
| 669 | |
| 670 | const Optional<bool> V = shouldVisitCursor(Cursor); |
| 671 | if (!V.hasValue()) |
| 672 | return None; |
| 673 | if (!V.getValue()) |
| 674 | return false; |
| 675 | if (Visit(Cursor, true)) |
| 676 | return true; |
| 677 | return None; |
| 678 | } |
| 679 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 680 | bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 681 | llvm_unreachable("Translation units are visited directly by Visit()"); |
| 682 | } |
| 683 | |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 684 | bool CursorVisitor::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 685 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 686 | return true; |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 687 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 688 | return Visit(MakeCXCursor(D->getTemplatedDecl(), TU, RegionOfInterest)); |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 691 | bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 692 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 693 | return Visit(TSInfo->getTypeLoc()); |
| 694 | |
| 695 | return false; |
| 696 | } |
| 697 | |
| 698 | bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { |
| 699 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 700 | return Visit(TSInfo->getTypeLoc()); |
| 701 | |
| 702 | return false; |
| 703 | } |
| 704 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 705 | bool CursorVisitor::VisitTagDecl(TagDecl *D) { return VisitDeclContext(D); } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 706 | |
| 707 | bool CursorVisitor::VisitClassTemplateSpecializationDecl( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 708 | ClassTemplateSpecializationDecl *D) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 709 | bool ShouldVisitBody = false; |
| 710 | switch (D->getSpecializationKind()) { |
| 711 | case TSK_Undeclared: |
| 712 | case TSK_ImplicitInstantiation: |
| 713 | // Nothing to visit |
| 714 | return false; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 715 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 716 | case TSK_ExplicitInstantiationDeclaration: |
| 717 | case TSK_ExplicitInstantiationDefinition: |
| 718 | break; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 719 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 720 | case TSK_ExplicitSpecialization: |
| 721 | ShouldVisitBody = true; |
| 722 | break; |
| 723 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 724 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 725 | // Visit the template arguments used in the specialization. |
| 726 | if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { |
| 727 | TypeLoc TL = SpecType->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 728 | if (TemplateSpecializationTypeLoc TSTLoc = |
| 729 | TL.getAs<TemplateSpecializationTypeLoc>()) { |
| 730 | for (unsigned I = 0, N = TSTLoc.getNumArgs(); I != N; ++I) |
| 731 | if (VisitTemplateArgumentLoc(TSTLoc.getArgLoc(I))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 732 | return true; |
| 733 | } |
| 734 | } |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 735 | |
| 736 | return ShouldVisitBody && VisitCXXRecordDecl(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 740 | ClassTemplatePartialSpecializationDecl *D) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 741 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 742 | // before visiting these template parameters. |
| 743 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 744 | return true; |
| 745 | |
| 746 | // Visit the partial specialization arguments. |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 747 | const ASTTemplateArgumentListInfo *Info = D->getTemplateArgsAsWritten(); |
| 748 | const TemplateArgumentLoc *TemplateArgs = Info->getTemplateArgs(); |
| 749 | for (unsigned I = 0, N = Info->NumTemplateArgs; I != N; ++I) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 750 | if (VisitTemplateArgumentLoc(TemplateArgs[I])) |
| 751 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 752 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 753 | return VisitCXXRecordDecl(D); |
| 754 | } |
| 755 | |
| 756 | bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
Saar Raz | ff1e0fc | 2020-01-15 02:48:42 +0200 | [diff] [blame] | 757 | if (const auto *TC = D->getTypeConstraint()) |
| 758 | if (Visit(MakeCXCursor(TC->getImmediatelyDeclaredConstraint(), StmtParent, |
| 759 | TU, RegionOfInterest))) |
| 760 | return true; |
| 761 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 762 | // Visit the default argument. |
| 763 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 764 | if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) |
| 765 | if (Visit(DefArg->getTypeLoc())) |
| 766 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 767 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 768 | return false; |
| 769 | } |
| 770 | |
| 771 | bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 772 | if (Expr *Init = D->getInitExpr()) |
| 773 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
| 774 | return false; |
| 775 | } |
| 776 | |
| 777 | bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { |
Argyrios Kyrtzidis | 2ec7674 | 2013-04-05 21:04:10 +0000 | [diff] [blame] | 778 | unsigned NumParamList = DD->getNumTemplateParameterLists(); |
| 779 | for (unsigned i = 0; i < NumParamList; i++) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 780 | TemplateParameterList *Params = DD->getTemplateParameterList(i); |
Argyrios Kyrtzidis | 2ec7674 | 2013-04-05 21:04:10 +0000 | [diff] [blame] | 781 | if (VisitTemplateParameters(Params)) |
| 782 | return true; |
| 783 | } |
| 784 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 785 | if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) |
| 786 | if (Visit(TSInfo->getTypeLoc())) |
| 787 | return true; |
| 788 | |
| 789 | // Visit the nested-name-specifier, if present. |
| 790 | if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc()) |
| 791 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 792 | return true; |
| 793 | |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 794 | return false; |
| 795 | } |
| 796 | |
Ivan Donchevskii | 1d18713 | 2018-01-03 14:35:48 +0000 | [diff] [blame] | 797 | static bool HasTrailingReturnType(FunctionDecl *ND) { |
| 798 | const QualType Ty = ND->getType(); |
| 799 | if (const FunctionType *AFT = Ty->getAs<FunctionType>()) { |
| 800 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(AFT)) |
| 801 | return FT->hasTrailingReturn(); |
| 802 | } |
| 803 | |
| 804 | return false; |
| 805 | } |
| 806 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 807 | /// Compare two base or member initializers based on their source order. |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 808 | static int CompareCXXCtorInitializers(CXXCtorInitializer *const *X, |
| 809 | CXXCtorInitializer *const *Y) { |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 810 | return (*X)->getSourceOrder() - (*Y)->getSourceOrder(); |
| 811 | } |
| 812 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 813 | bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
Argyrios Kyrtzidis | 2ec7674 | 2013-04-05 21:04:10 +0000 | [diff] [blame] | 814 | unsigned NumParamList = ND->getNumTemplateParameterLists(); |
| 815 | for (unsigned i = 0; i < NumParamList; i++) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 816 | TemplateParameterList *Params = ND->getTemplateParameterList(i); |
Argyrios Kyrtzidis | 2ec7674 | 2013-04-05 21:04:10 +0000 | [diff] [blame] | 817 | if (VisitTemplateParameters(Params)) |
| 818 | return true; |
| 819 | } |
| 820 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 821 | if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { |
| 822 | // Visit the function declaration's syntactic components in the order |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 823 | // written. This requires a bit of work. |
| 824 | TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); |
| 825 | FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>(); |
Ivan Donchevskii | 1d18713 | 2018-01-03 14:35:48 +0000 | [diff] [blame] | 826 | const bool HasTrailingRT = HasTrailingReturnType(ND); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 827 | |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 828 | // If we have a function declared directly (without the use of a typedef), |
| 829 | // visit just the return type. Otherwise, just visit the function's type |
| 830 | // now. |
Ivan Donchevskii | 1d18713 | 2018-01-03 14:35:48 +0000 | [diff] [blame] | 831 | if ((FTL && !isa<CXXConversionDecl>(ND) && !HasTrailingRT && |
| 832 | Visit(FTL.getReturnLoc())) || |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 833 | (!FTL && Visit(TL))) |
| 834 | return true; |
Ivan Donchevskii | 1d18713 | 2018-01-03 14:35:48 +0000 | [diff] [blame] | 835 | |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 836 | // Visit the nested-name-specifier, if present. |
| 837 | if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc()) |
| 838 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 839 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 840 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 841 | // Visit the declaration name. |
Argyrios Kyrtzidis | 4a4d2b4 | 2014-02-09 08:13:47 +0000 | [diff] [blame] | 842 | if (!isa<CXXDestructorDecl>(ND)) |
| 843 | if (VisitDeclarationNameInfo(ND->getNameInfo())) |
| 844 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 845 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 846 | // FIXME: Visit explicitly-specified template arguments! |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 847 | |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 848 | // Visit the function parameters, if we have a function type. |
| 849 | if (FTL && VisitFunctionTypeLoc(FTL, true)) |
| 850 | return true; |
Ivan Donchevskii | 1d18713 | 2018-01-03 14:35:48 +0000 | [diff] [blame] | 851 | |
| 852 | // Visit the function's trailing return type. |
| 853 | if (FTL && HasTrailingRT && Visit(FTL.getReturnLoc())) |
| 854 | return true; |
| 855 | |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 856 | // FIXME: Attributes? |
| 857 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 858 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 859 | if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) { |
| 860 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) { |
| 861 | // Find the initializers that were written in the source. |
| 862 | SmallVector<CXXCtorInitializer *, 4> WrittenInits; |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 863 | for (auto *I : Constructor->inits()) { |
| 864 | if (!I->isWritten()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 865 | continue; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 866 | |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 867 | WrittenInits.push_back(I); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 868 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 869 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 870 | // Sort the initializers in source order |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 871 | llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(), |
| 872 | &CompareCXXCtorInitializers); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 873 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 874 | // Visit the initializers in source order |
| 875 | for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) { |
| 876 | CXXCtorInitializer *Init = WrittenInits[I]; |
| 877 | if (Init->isAnyMemberInitializer()) { |
| 878 | if (Visit(MakeCursorMemberRef(Init->getAnyMember(), |
| 879 | Init->getMemberLocation(), TU))) |
| 880 | return true; |
| 881 | } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) { |
| 882 | if (Visit(TInfo->getTypeLoc())) |
| 883 | return true; |
| 884 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 885 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 886 | // Visit the initializer value. |
| 887 | if (Expr *Initializer = Init->getInit()) |
| 888 | if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest))) |
| 889 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 890 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 891 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 892 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 893 | if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
| 894 | return true; |
| 895 | } |
| 896 | |
| 897 | return false; |
| 898 | } |
| 899 | |
| 900 | bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { |
| 901 | if (VisitDeclaratorDecl(D)) |
| 902 | return true; |
| 903 | |
| 904 | if (Expr *BitWidth = D->getBitWidth()) |
| 905 | return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest)); |
| 906 | |
Benjamin Kramer | 99f9759 | 2017-11-15 12:20:41 +0000 | [diff] [blame] | 907 | if (Expr *Init = D->getInClassInitializer()) |
| 908 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
| 909 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 910 | return false; |
| 911 | } |
| 912 | |
| 913 | bool CursorVisitor::VisitVarDecl(VarDecl *D) { |
| 914 | if (VisitDeclaratorDecl(D)) |
| 915 | return true; |
| 916 | |
| 917 | if (Expr *Init = D->getInit()) |
| 918 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
| 919 | |
| 920 | return false; |
| 921 | } |
| 922 | |
| 923 | bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 924 | if (VisitDeclaratorDecl(D)) |
| 925 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 926 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 927 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 928 | if (Expr *DefArg = D->getDefaultArgument()) |
| 929 | return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest)); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 930 | |
| 931 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 935 | // FIXME: Visit the "outer" template parameter lists on the FunctionDecl |
| 936 | // before visiting these template parameters. |
| 937 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 938 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 939 | |
| 940 | auto *FD = D->getTemplatedDecl(); |
Jonathan Coe | 578ac7a | 2017-10-16 23:43:02 +0000 | [diff] [blame] | 941 | return VisitAttributes(FD) || VisitFunctionDecl(FD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 945 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 946 | // before visiting these template parameters. |
| 947 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 948 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 949 | |
| 950 | auto *CD = D->getTemplatedDecl(); |
Jonathan Coe | 578ac7a | 2017-10-16 23:43:02 +0000 | [diff] [blame] | 951 | return VisitAttributes(CD) || VisitCXXRecordDecl(CD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 955 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 956 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 957 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 958 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && |
| 959 | VisitTemplateArgumentLoc(D->getDefaultArgument())) |
| 960 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 961 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 962 | return false; |
| 963 | } |
| 964 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 965 | bool CursorVisitor::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { |
| 966 | // Visit the bound, if it's explicit. |
| 967 | if (D->hasExplicitBound()) { |
| 968 | if (auto TInfo = D->getTypeSourceInfo()) { |
| 969 | if (Visit(TInfo->getTypeLoc())) |
| 970 | return true; |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | return false; |
| 975 | } |
| 976 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 977 | bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 978 | if (TypeSourceInfo *TSInfo = ND->getReturnTypeSourceInfo()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 979 | if (Visit(TSInfo->getTypeLoc())) |
| 980 | return true; |
| 981 | |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 982 | for (const auto *P : ND->parameters()) { |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 983 | if (Visit(MakeCXCursor(P, TU, RegionOfInterest))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 984 | return true; |
| 985 | } |
| 986 | |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 987 | return ND->isThisDeclarationADefinition() && |
| 988 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | template <typename DeclIt> |
| 992 | static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current, |
| 993 | SourceManager &SM, SourceLocation EndLoc, |
| 994 | SmallVectorImpl<Decl *> &Decls) { |
| 995 | DeclIt next = *DI_current; |
| 996 | while (++next != DE_current) { |
| 997 | Decl *D_next = *next; |
| 998 | if (!D_next) |
| 999 | break; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1000 | SourceLocation L = D_next->getBeginLoc(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1001 | if (!L.isValid()) |
| 1002 | break; |
| 1003 | if (SM.isBeforeInTranslationUnit(L, EndLoc)) { |
| 1004 | *DI_current = next; |
| 1005 | Decls.push_back(D_next); |
| 1006 | continue; |
| 1007 | } |
| 1008 | break; |
| 1009 | } |
| 1010 | } |
| 1011 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1012 | bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
| 1013 | // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially |
| 1014 | // an @implementation can lexically contain Decls that are not properly |
| 1015 | // nested in the AST. When we identify such cases, we need to retrofit |
| 1016 | // this nesting here. |
| 1017 | if (!DI_current && !FileDI_current) |
| 1018 | return VisitDeclContext(D); |
| 1019 | |
| 1020 | // Scan the Decls that immediately come after the container |
| 1021 | // in the current DeclContext. If any fall within the |
| 1022 | // container's lexical region, stash them into a vector |
| 1023 | // for later processing. |
| 1024 | SmallVector<Decl *, 24> DeclsInContainer; |
| 1025 | SourceLocation EndLoc = D->getSourceRange().getEnd(); |
| 1026 | SourceManager &SM = AU->getSourceManager(); |
| 1027 | if (EndLoc.isValid()) { |
| 1028 | if (DI_current) { |
| 1029 | addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc, |
| 1030 | DeclsInContainer); |
| 1031 | } else { |
| 1032 | addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc, |
| 1033 | DeclsInContainer); |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | // The common case. |
| 1038 | if (DeclsInContainer.empty()) |
| 1039 | return VisitDeclContext(D); |
| 1040 | |
| 1041 | // Get all the Decls in the DeclContext, and sort them with the |
| 1042 | // additional ones we've collected. Then visit them. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1043 | for (auto *SubDecl : D->decls()) { |
| 1044 | if (!SubDecl || SubDecl->getLexicalDeclContext() != D || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1045 | SubDecl->getBeginLoc().isInvalid()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1046 | continue; |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1047 | DeclsInContainer.push_back(SubDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | // Now sort the Decls so that they appear in lexical order. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1051 | llvm::sort(DeclsInContainer, [&SM](Decl *A, Decl *B) { |
| 1052 | SourceLocation L_A = A->getBeginLoc(); |
| 1053 | SourceLocation L_B = B->getBeginLoc(); |
| 1054 | return L_A != L_B |
| 1055 | ? SM.isBeforeInTranslationUnit(L_A, L_B) |
| 1056 | : SM.isBeforeInTranslationUnit(A->getEndLoc(), B->getEndLoc()); |
| 1057 | }); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1058 | |
| 1059 | // Now visit the decls. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1060 | for (SmallVectorImpl<Decl *>::iterator I = DeclsInContainer.begin(), |
| 1061 | E = DeclsInContainer.end(); |
| 1062 | I != E; ++I) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1063 | CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest); |
Ted Kremenek | 0332558 | 2013-02-21 01:29:01 +0000 | [diff] [blame] | 1064 | const Optional<bool> &V = shouldVisitCursor(Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1065 | if (!V.hasValue()) |
| 1066 | continue; |
| 1067 | if (!V.getValue()) |
| 1068 | return false; |
| 1069 | if (Visit(Cursor, true)) |
| 1070 | return true; |
| 1071 | } |
| 1072 | return false; |
| 1073 | } |
| 1074 | |
| 1075 | bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 1076 | if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), |
| 1077 | TU))) |
| 1078 | return true; |
| 1079 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1080 | if (VisitObjCTypeParamList(ND->getTypeParamList())) |
| 1081 | return true; |
| 1082 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1083 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 1084 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1085 | E = ND->protocol_end(); |
| 1086 | I != E; ++I, ++PL) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1087 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1088 | return true; |
| 1089 | |
| 1090 | return VisitObjCContainerDecl(ND); |
| 1091 | } |
| 1092 | |
| 1093 | bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 1094 | if (!PID->isThisDeclarationADefinition()) |
| 1095 | return Visit(MakeCursorObjCProtocolRef(PID, PID->getLocation(), TU)); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1096 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1097 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
| 1098 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1099 | E = PID->protocol_end(); |
| 1100 | I != E; ++I, ++PL) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1101 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1102 | return true; |
| 1103 | |
| 1104 | return VisitObjCContainerDecl(PID); |
| 1105 | } |
| 1106 | |
| 1107 | bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { |
| 1108 | if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc())) |
| 1109 | return true; |
| 1110 | |
| 1111 | // FIXME: This implements a workaround with @property declarations also being |
| 1112 | // installed in the DeclContext for the @interface. Eventually this code |
| 1113 | // should be removed. |
| 1114 | ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); |
| 1115 | if (!CDecl || !CDecl->IsClassExtension()) |
| 1116 | return false; |
| 1117 | |
| 1118 | ObjCInterfaceDecl *ID = CDecl->getClassInterface(); |
| 1119 | if (!ID) |
| 1120 | return false; |
| 1121 | |
| 1122 | IdentifierInfo *PropertyId = PD->getIdentifier(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1123 | ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl( |
| 1124 | cast<DeclContext>(ID), PropertyId, PD->getQueryKind()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1125 | |
| 1126 | if (!prevDecl) |
| 1127 | return false; |
| 1128 | |
| 1129 | // Visit synthesized methods since they will be skipped when visiting |
| 1130 | // the @interface. |
| 1131 | if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) |
| 1132 | if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl) |
| 1133 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
| 1134 | return true; |
| 1135 | |
| 1136 | if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) |
| 1137 | if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl) |
| 1138 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
| 1139 | return true; |
| 1140 | |
| 1141 | return false; |
| 1142 | } |
| 1143 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1144 | bool CursorVisitor::VisitObjCTypeParamList(ObjCTypeParamList *typeParamList) { |
| 1145 | if (!typeParamList) |
| 1146 | return false; |
| 1147 | |
| 1148 | for (auto *typeParam : *typeParamList) { |
| 1149 | // Visit the type parameter. |
| 1150 | if (Visit(MakeCXCursor(typeParam, TU, RegionOfInterest))) |
| 1151 | return true; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | return false; |
| 1155 | } |
| 1156 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1157 | bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 1158 | if (!D->isThisDeclarationADefinition()) { |
| 1159 | // Forward declaration is treated like a reference. |
| 1160 | return Visit(MakeCursorObjCClassRef(D, D->getLocation(), TU)); |
| 1161 | } |
| 1162 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1163 | // Objective-C type parameters. |
| 1164 | if (VisitObjCTypeParamList(D->getTypeParamListAsWritten())) |
| 1165 | return true; |
| 1166 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1167 | // Issue callbacks for super class. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1168 | if (D->getSuperClass() && Visit(MakeCursorObjCSuperClassRef( |
| 1169 | D->getSuperClass(), D->getSuperClassLoc(), TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1170 | return true; |
| 1171 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1172 | if (TypeSourceInfo *SuperClassTInfo = D->getSuperClassTInfo()) |
| 1173 | if (Visit(SuperClassTInfo->getTypeLoc())) |
| 1174 | return true; |
| 1175 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1176 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1177 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1178 | E = D->protocol_end(); |
| 1179 | I != E; ++I, ++PL) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1180 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1181 | return true; |
| 1182 | |
| 1183 | return VisitObjCContainerDecl(D); |
| 1184 | } |
| 1185 | |
| 1186 | bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 1187 | return VisitObjCContainerDecl(D); |
| 1188 | } |
| 1189 | |
| 1190 | bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 1191 | // 'ID' could be null when dealing with invalid code. |
| 1192 | if (ObjCInterfaceDecl *ID = D->getClassInterface()) |
| 1193 | if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) |
| 1194 | return true; |
| 1195 | |
| 1196 | return VisitObjCImplDecl(D); |
| 1197 | } |
| 1198 | |
| 1199 | bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 1200 | #if 0 |
| 1201 | // Issue callbacks for super class. |
| 1202 | // FIXME: No source location information! |
| 1203 | if (D->getSuperClass() && |
| 1204 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
| 1205 | D->getSuperClassLoc(), |
| 1206 | TU))) |
| 1207 | return true; |
| 1208 | #endif |
| 1209 | |
| 1210 | return VisitObjCImplDecl(D); |
| 1211 | } |
| 1212 | |
| 1213 | bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) { |
| 1214 | if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl()) |
| 1215 | if (PD->isIvarNameSpecified()) |
| 1216 | return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU)); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1217 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1218 | return false; |
| 1219 | } |
| 1220 | |
| 1221 | bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { |
| 1222 | return VisitDeclContext(D); |
| 1223 | } |
| 1224 | |
| 1225 | bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 1226 | // Visit nested-name-specifier. |
| 1227 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1228 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1229 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1230 | |
| 1231 | return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1232 | D->getTargetNameLoc(), TU)); |
| 1233 | } |
| 1234 | |
| 1235 | bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { |
| 1236 | // Visit nested-name-specifier. |
| 1237 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1238 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1239 | return true; |
| 1240 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1241 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1242 | if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU))) |
| 1243 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1244 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1245 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1246 | } |
| 1247 | |
| 1248 | bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 1249 | // Visit nested-name-specifier. |
| 1250 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1251 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1252 | return true; |
| 1253 | |
| 1254 | return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), |
| 1255 | D->getIdentLocation(), TU)); |
| 1256 | } |
| 1257 | |
| 1258 | bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
| 1259 | // Visit nested-name-specifier. |
| 1260 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1261 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1262 | return true; |
| 1263 | } |
| 1264 | |
| 1265 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1266 | } |
| 1267 | |
| 1268 | bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1269 | UnresolvedUsingTypenameDecl *D) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1270 | // Visit nested-name-specifier. |
| 1271 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1272 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1273 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1274 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1275 | return false; |
| 1276 | } |
| 1277 | |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 1278 | bool CursorVisitor::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 1279 | if (Visit(MakeCXCursor(D->getAssertExpr(), StmtParent, TU, RegionOfInterest))) |
| 1280 | return true; |
Richard Trieu | f3b7766 | 2016-09-13 01:37:01 +0000 | [diff] [blame] | 1281 | if (StringLiteral *Message = D->getMessage()) |
| 1282 | if (Visit(MakeCXCursor(Message, StmtParent, TU, RegionOfInterest))) |
| 1283 | return true; |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 1284 | return false; |
| 1285 | } |
| 1286 | |
Olivier Goffart | d211c64 | 2016-11-04 06:29:27 +0000 | [diff] [blame] | 1287 | bool CursorVisitor::VisitFriendDecl(FriendDecl *D) { |
| 1288 | if (NamedDecl *FriendD = D->getFriendDecl()) { |
| 1289 | if (Visit(MakeCXCursor(FriendD, TU, RegionOfInterest))) |
| 1290 | return true; |
| 1291 | } else if (TypeSourceInfo *TI = D->getFriendType()) { |
| 1292 | if (Visit(TI->getTypeLoc())) |
| 1293 | return true; |
| 1294 | } |
| 1295 | return false; |
| 1296 | } |
| 1297 | |
Milian Wolff | 4597e3b | 2020-05-02 22:17:59 +0200 | [diff] [blame] | 1298 | bool CursorVisitor::VisitDecompositionDecl(DecompositionDecl *D) { |
| 1299 | for (auto *B : D->bindings()) { |
| 1300 | if (Visit(MakeCXCursor(B, TU, RegionOfInterest))) |
| 1301 | return true; |
| 1302 | } |
| 1303 | return VisitVarDecl(D); |
| 1304 | } |
| 1305 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1306 | bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { |
| 1307 | switch (Name.getName().getNameKind()) { |
| 1308 | case clang::DeclarationName::Identifier: |
| 1309 | case clang::DeclarationName::CXXLiteralOperatorName: |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 1310 | case clang::DeclarationName::CXXDeductionGuideName: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1311 | case clang::DeclarationName::CXXOperatorName: |
| 1312 | case clang::DeclarationName::CXXUsingDirective: |
| 1313 | return false; |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 1314 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1315 | case clang::DeclarationName::CXXConstructorName: |
| 1316 | case clang::DeclarationName::CXXDestructorName: |
| 1317 | case clang::DeclarationName::CXXConversionFunctionName: |
| 1318 | if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) |
| 1319 | return Visit(TSInfo->getTypeLoc()); |
| 1320 | return false; |
| 1321 | |
| 1322 | case clang::DeclarationName::ObjCZeroArgSelector: |
| 1323 | case clang::DeclarationName::ObjCOneArgSelector: |
| 1324 | case clang::DeclarationName::ObjCMultiArgSelector: |
| 1325 | // FIXME: Per-identifier location info? |
| 1326 | return false; |
| 1327 | } |
| 1328 | |
| 1329 | llvm_unreachable("Invalid DeclarationName::Kind!"); |
| 1330 | } |
| 1331 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1332 | bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1333 | SourceRange Range) { |
| 1334 | // FIXME: This whole routine is a hack to work around the lack of proper |
| 1335 | // source information in nested-name-specifiers (PR5791). Since we do have |
| 1336 | // a beginning source location, we can visit the first component of the |
| 1337 | // nested-name-specifier, if it's a single-token component. |
| 1338 | if (!NNS) |
| 1339 | return false; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1340 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1341 | // Get the first component in the nested-name-specifier. |
| 1342 | while (NestedNameSpecifier *Prefix = NNS->getPrefix()) |
| 1343 | NNS = Prefix; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1344 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1345 | switch (NNS->getKind()) { |
| 1346 | case NestedNameSpecifier::Namespace: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1347 | return Visit( |
| 1348 | MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1349 | |
| 1350 | case NestedNameSpecifier::NamespaceAlias: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1351 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1352 | Range.getBegin(), TU)); |
| 1353 | |
| 1354 | case NestedNameSpecifier::TypeSpec: { |
| 1355 | // If the type has a form where we know that the beginning of the source |
| 1356 | // range matches up with a reference cursor. Visit the appropriate reference |
| 1357 | // cursor. |
| 1358 | const Type *T = NNS->getAsType(); |
| 1359 | if (const TypedefType *Typedef = dyn_cast<TypedefType>(T)) |
| 1360 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); |
| 1361 | if (const TagType *Tag = dyn_cast<TagType>(T)) |
| 1362 | return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1363 | if (const TemplateSpecializationType *TST = |
| 1364 | dyn_cast<TemplateSpecializationType>(T)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1365 | return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); |
| 1366 | break; |
| 1367 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1368 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1369 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1370 | case NestedNameSpecifier::Global: |
| 1371 | case NestedNameSpecifier::Identifier: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 1372 | case NestedNameSpecifier::Super: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1373 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1374 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1375 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1376 | return false; |
| 1377 | } |
| 1378 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1379 | bool CursorVisitor::VisitNestedNameSpecifierLoc( |
| 1380 | NestedNameSpecifierLoc Qualifier) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1381 | SmallVector<NestedNameSpecifierLoc, 4> Qualifiers; |
| 1382 | for (; Qualifier; Qualifier = Qualifier.getPrefix()) |
| 1383 | Qualifiers.push_back(Qualifier); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1384 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1385 | while (!Qualifiers.empty()) { |
| 1386 | NestedNameSpecifierLoc Q = Qualifiers.pop_back_val(); |
| 1387 | NestedNameSpecifier *NNS = Q.getNestedNameSpecifier(); |
| 1388 | switch (NNS->getKind()) { |
| 1389 | case NestedNameSpecifier::Namespace: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1390 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), |
| 1391 | Q.getLocalBeginLoc(), TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1392 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1393 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1394 | break; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1395 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1396 | case NestedNameSpecifier::NamespaceAlias: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1397 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1398 | Q.getLocalBeginLoc(), TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1399 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1400 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1401 | break; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1402 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1403 | case NestedNameSpecifier::TypeSpec: |
| 1404 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1405 | if (Visit(Q.getTypeLoc())) |
| 1406 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1407 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1408 | break; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1409 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1410 | case NestedNameSpecifier::Global: |
| 1411 | case NestedNameSpecifier::Identifier: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 1412 | case NestedNameSpecifier::Super: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1413 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1414 | } |
| 1415 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1416 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1417 | return false; |
| 1418 | } |
| 1419 | |
| 1420 | bool CursorVisitor::VisitTemplateParameters( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1421 | const TemplateParameterList *Params) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1422 | if (!Params) |
| 1423 | return false; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1424 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1425 | for (TemplateParameterList::const_iterator P = Params->begin(), |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1426 | PEnd = Params->end(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1427 | P != PEnd; ++P) { |
| 1428 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
| 1429 | return true; |
| 1430 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1431 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1432 | return false; |
| 1433 | } |
| 1434 | |
| 1435 | bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { |
| 1436 | switch (Name.getKind()) { |
| 1437 | case TemplateName::Template: |
| 1438 | return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); |
| 1439 | |
| 1440 | case TemplateName::OverloadedTemplate: |
| 1441 | // Visit the overloaded template set. |
| 1442 | if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU))) |
| 1443 | return true; |
| 1444 | |
| 1445 | return false; |
| 1446 | |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 1447 | case TemplateName::AssumedTemplate: |
| 1448 | // FIXME: Visit DeclarationName? |
| 1449 | return false; |
| 1450 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1451 | case TemplateName::DependentTemplate: |
| 1452 | // FIXME: Visit nested-name-specifier. |
| 1453 | return false; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1454 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1455 | case TemplateName::QualifiedTemplate: |
| 1456 | // FIXME: Visit nested-name-specifier. |
| 1457 | return Visit(MakeCursorTemplateRef( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1458 | Name.getAsQualifiedTemplateName()->getDecl(), Loc, TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1459 | |
| 1460 | case TemplateName::SubstTemplateTemplateParm: |
| 1461 | return Visit(MakeCursorTemplateRef( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1462 | Name.getAsSubstTemplateTemplateParm()->getParameter(), Loc, TU)); |
| 1463 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1464 | case TemplateName::SubstTemplateTemplateParmPack: |
| 1465 | return Visit(MakeCursorTemplateRef( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1466 | Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), Loc, |
| 1467 | TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | llvm_unreachable("Invalid TemplateName::Kind!"); |
| 1471 | } |
| 1472 | |
| 1473 | bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { |
| 1474 | switch (TAL.getArgument().getKind()) { |
| 1475 | case TemplateArgument::Null: |
| 1476 | case TemplateArgument::Integral: |
| 1477 | case TemplateArgument::Pack: |
| 1478 | return false; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1479 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1480 | case TemplateArgument::Type: |
| 1481 | if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) |
| 1482 | return Visit(TSInfo->getTypeLoc()); |
| 1483 | return false; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1484 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1485 | case TemplateArgument::Declaration: |
| 1486 | if (Expr *E = TAL.getSourceDeclExpression()) |
| 1487 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
| 1488 | return false; |
| 1489 | |
| 1490 | case TemplateArgument::NullPtr: |
| 1491 | if (Expr *E = TAL.getSourceNullPtrExpression()) |
| 1492 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
| 1493 | return false; |
| 1494 | |
| 1495 | case TemplateArgument::Expression: |
| 1496 | if (Expr *E = TAL.getSourceExpression()) |
| 1497 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
| 1498 | return false; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1499 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1500 | case TemplateArgument::Template: |
| 1501 | case TemplateArgument::TemplateExpansion: |
| 1502 | if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc())) |
| 1503 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1504 | |
| 1505 | return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1506 | TAL.getTemplateNameLoc()); |
| 1507 | } |
| 1508 | |
| 1509 | llvm_unreachable("Invalid TemplateArgument::Kind!"); |
| 1510 | } |
| 1511 | |
| 1512 | bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 1513 | return VisitDeclContext(D); |
| 1514 | } |
| 1515 | |
| 1516 | bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1517 | return Visit(TL.getUnqualifiedLoc()); |
| 1518 | } |
| 1519 | |
| 1520 | bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
| 1521 | ASTContext &Context = AU->getASTContext(); |
| 1522 | |
| 1523 | // Some builtin types (such as Objective-C's "id", "sel", and |
| 1524 | // "Class") have associated declarations. Create cursors for those. |
| 1525 | QualType VisitType; |
| 1526 | switch (TL.getTypePtr()->getKind()) { |
| 1527 | |
| 1528 | case BuiltinType::Void: |
| 1529 | case BuiltinType::NullPtr: |
| 1530 | case BuiltinType::Dependent: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1531 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 1532 | case BuiltinType::Id: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 1533 | #include "clang/Basic/OpenCLImageTypes.def" |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1534 | #define EXT_OPAQUE_TYPE(ExtTYpe, Id, Ext) case BuiltinType::Id: |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 1535 | #include "clang/Basic/OpenCLExtensionTypes.def" |
NAKAMURA Takumi | 288c42e | 2013-02-07 12:47:42 +0000 | [diff] [blame] | 1536 | case BuiltinType::OCLSampler: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1537 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1538 | case BuiltinType::OCLClkEvent: |
| 1539 | case BuiltinType::OCLQueue: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1540 | case BuiltinType::OCLReserveID: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1541 | #define SVE_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
Richard Sandiford | eb485fb | 2019-08-09 08:52:54 +0000 | [diff] [blame] | 1542 | #include "clang/Basic/AArch64SVEACLETypes.def" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1543 | #define BUILTIN_TYPE(Id, SingletonId) |
| 1544 | #define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1545 | #define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1546 | #define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1547 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1548 | #include "clang/AST/BuiltinTypes.def" |
| 1549 | break; |
| 1550 | |
| 1551 | case BuiltinType::ObjCId: |
| 1552 | VisitType = Context.getObjCIdType(); |
| 1553 | break; |
| 1554 | |
| 1555 | case BuiltinType::ObjCClass: |
| 1556 | VisitType = Context.getObjCClassType(); |
| 1557 | break; |
| 1558 | |
| 1559 | case BuiltinType::ObjCSel: |
| 1560 | VisitType = Context.getObjCSelType(); |
| 1561 | break; |
| 1562 | } |
| 1563 | |
| 1564 | if (!VisitType.isNull()) { |
| 1565 | if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1566 | return Visit( |
| 1567 | MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | return false; |
| 1571 | } |
| 1572 | |
| 1573 | bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 1574 | return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU)); |
| 1575 | } |
| 1576 | |
| 1577 | bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 1578 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1579 | } |
| 1580 | |
| 1581 | bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { |
| 1582 | if (TL.isDefinition()) |
| 1583 | return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest)); |
| 1584 | |
| 1585 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1586 | } |
| 1587 | |
| 1588 | bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1589 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1590 | } |
| 1591 | |
| 1592 | bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 1593 | return Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 1596 | bool CursorVisitor::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1597 | if (Visit(MakeCursorTypeRef(TL.getDecl(), TL.getBeginLoc(), TU))) |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 1598 | return true; |
| 1599 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1600 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1601 | TU))) |
| 1602 | return true; |
| 1603 | } |
| 1604 | |
| 1605 | return false; |
| 1606 | } |
| 1607 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1608 | bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 1609 | if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) |
| 1610 | return true; |
| 1611 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1612 | for (unsigned I = 0, N = TL.getNumTypeArgs(); I != N; ++I) { |
| 1613 | if (Visit(TL.getTypeArgTInfo(I)->getTypeLoc())) |
| 1614 | return true; |
| 1615 | } |
| 1616 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1617 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1618 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1619 | TU))) |
| 1620 | return true; |
| 1621 | } |
| 1622 | |
| 1623 | return false; |
| 1624 | } |
| 1625 | |
| 1626 | bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 1627 | return Visit(TL.getPointeeLoc()); |
| 1628 | } |
| 1629 | |
| 1630 | bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) { |
| 1631 | return Visit(TL.getInnerLoc()); |
| 1632 | } |
| 1633 | |
Leonard Chan | c72aaf6 | 2019-05-07 03:20:17 +0000 | [diff] [blame] | 1634 | bool CursorVisitor::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { |
| 1635 | return Visit(TL.getInnerLoc()); |
| 1636 | } |
| 1637 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1638 | bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1639 | return Visit(TL.getPointeeLoc()); |
| 1640 | } |
| 1641 | |
| 1642 | bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1643 | return Visit(TL.getPointeeLoc()); |
| 1644 | } |
| 1645 | |
| 1646 | bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1647 | return Visit(TL.getPointeeLoc()); |
| 1648 | } |
| 1649 | |
| 1650 | bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 1651 | return Visit(TL.getPointeeLoc()); |
| 1652 | } |
| 1653 | |
| 1654 | bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 1655 | return Visit(TL.getPointeeLoc()); |
| 1656 | } |
| 1657 | |
| 1658 | bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 1659 | return Visit(TL.getModifiedLoc()); |
| 1660 | } |
| 1661 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1662 | bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1663 | bool SkipResultType) { |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 1664 | if (!SkipResultType && Visit(TL.getReturnLoc())) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1665 | return true; |
| 1666 | |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 1667 | for (unsigned I = 0, N = TL.getNumParams(); I != N; ++I) |
| 1668 | if (Decl *D = TL.getParam(I)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1669 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest))) |
| 1670 | return true; |
| 1671 | |
| 1672 | return false; |
| 1673 | } |
| 1674 | |
| 1675 | bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1676 | if (Visit(TL.getElementLoc())) |
| 1677 | return true; |
| 1678 | |
| 1679 | if (Expr *Size = TL.getSizeExpr()) |
| 1680 | return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest)); |
| 1681 | |
| 1682 | return false; |
| 1683 | } |
| 1684 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 1685 | bool CursorVisitor::VisitDecayedTypeLoc(DecayedTypeLoc TL) { |
| 1686 | return Visit(TL.getOriginalLoc()); |
| 1687 | } |
| 1688 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 1689 | bool CursorVisitor::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { |
| 1690 | return Visit(TL.getOriginalLoc()); |
| 1691 | } |
| 1692 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1693 | bool CursorVisitor::VisitDeducedTemplateSpecializationTypeLoc( |
| 1694 | DeducedTemplateSpecializationTypeLoc TL) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1695 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1696 | TL.getTemplateNameLoc())) |
| 1697 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1698 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1699 | return false; |
| 1700 | } |
| 1701 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1702 | bool CursorVisitor::VisitTemplateSpecializationTypeLoc( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1703 | TemplateSpecializationTypeLoc TL) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1704 | // Visit the template name. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1705 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1706 | TL.getTemplateNameLoc())) |
| 1707 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1708 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1709 | // Visit the template arguments. |
| 1710 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1711 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1712 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1713 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1714 | return false; |
| 1715 | } |
| 1716 | |
| 1717 | bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 1718 | return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); |
| 1719 | } |
| 1720 | |
| 1721 | bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 1722 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1723 | return Visit(TSInfo->getTypeLoc()); |
| 1724 | |
| 1725 | return false; |
| 1726 | } |
| 1727 | |
| 1728 | bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 1729 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1730 | return Visit(TSInfo->getTypeLoc()); |
| 1731 | |
| 1732 | return false; |
| 1733 | } |
| 1734 | |
| 1735 | bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 1736 | return VisitNestedNameSpecifierLoc(TL.getQualifierLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1740 | DependentTemplateSpecializationTypeLoc TL) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1741 | // Visit the nested-name-specifier, if there is one. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1742 | if (TL.getQualifierLoc() && VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1743 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1744 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1745 | // Visit the template arguments. |
| 1746 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1747 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1748 | return true; |
| 1749 | |
| 1750 | return false; |
| 1751 | } |
| 1752 | |
| 1753 | bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 1754 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1755 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1756 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1757 | return Visit(TL.getNamedTypeLoc()); |
| 1758 | } |
| 1759 | |
| 1760 | bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { |
| 1761 | return Visit(TL.getPatternLoc()); |
| 1762 | } |
| 1763 | |
| 1764 | bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 1765 | if (Expr *E = TL.getUnderlyingExpr()) |
| 1766 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1767 | |
| 1768 | return false; |
| 1769 | } |
| 1770 | |
| 1771 | bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 1772 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1773 | } |
| 1774 | |
| 1775 | bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) { |
| 1776 | return Visit(TL.getValueLoc()); |
| 1777 | } |
| 1778 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 1779 | bool CursorVisitor::VisitPipeTypeLoc(PipeTypeLoc TL) { |
| 1780 | return Visit(TL.getValueLoc()); |
| 1781 | } |
| 1782 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1783 | #define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \ |
| 1784 | bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \ |
| 1785 | return Visit##PARENT##Loc(TL); \ |
| 1786 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1787 | |
| 1788 | DEFAULT_TYPELOC_IMPL(Complex, Type) |
| 1789 | DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType) |
| 1790 | DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType) |
| 1791 | DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType) |
| 1792 | DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType) |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 1793 | DEFAULT_TYPELOC_IMPL(DependentAddressSpace, Type) |
Erich Keane | f702b02 | 2018-07-13 19:46:04 +0000 | [diff] [blame] | 1794 | DEFAULT_TYPELOC_IMPL(DependentVector, Type) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1795 | DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type) |
| 1796 | DEFAULT_TYPELOC_IMPL(Vector, Type) |
| 1797 | DEFAULT_TYPELOC_IMPL(ExtVector, VectorType) |
Florian Hahn | 1065869 | 2020-05-11 17:45:51 +0100 | [diff] [blame] | 1798 | DEFAULT_TYPELOC_IMPL(ConstantMatrix, MatrixType) |
| 1799 | DEFAULT_TYPELOC_IMPL(DependentSizedMatrix, MatrixType) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1800 | DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType) |
| 1801 | DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType) |
| 1802 | DEFAULT_TYPELOC_IMPL(Record, TagType) |
| 1803 | DEFAULT_TYPELOC_IMPL(Enum, TagType) |
| 1804 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type) |
| 1805 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type) |
| 1806 | DEFAULT_TYPELOC_IMPL(Auto, Type) |
Erich Keane | 5f0903e | 2020-04-17 10:44:19 -0700 | [diff] [blame] | 1807 | DEFAULT_TYPELOC_IMPL(ExtInt, Type) |
| 1808 | DEFAULT_TYPELOC_IMPL(DependentExtInt, Type) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1809 | |
| 1810 | bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 1811 | // Visit the nested-name-specifier, if present. |
| 1812 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1813 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1814 | return true; |
| 1815 | |
| 1816 | if (D->isCompleteDefinition()) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1817 | for (const auto &I : D->bases()) { |
| 1818 | if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(&I, TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1819 | return true; |
| 1820 | } |
| 1821 | } |
| 1822 | |
| 1823 | return VisitTagDecl(D); |
| 1824 | } |
| 1825 | |
| 1826 | bool CursorVisitor::VisitAttributes(Decl *D) { |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 1827 | for (const auto *I : D->attrs()) |
Michael Wu | 40ff105 | 2018-08-03 05:20:23 +0000 | [diff] [blame] | 1828 | if ((TU->ParsingOptions & CXTranslationUnit_VisitImplicitAttributes || |
| 1829 | !I->isImplicit()) && |
| 1830 | Visit(MakeCXCursor(I, D, TU))) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1831 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1832 | |
| 1833 | return false; |
| 1834 | } |
| 1835 | |
| 1836 | //===----------------------------------------------------------------------===// |
| 1837 | // Data-recursive visitor methods. |
| 1838 | //===----------------------------------------------------------------------===// |
| 1839 | |
| 1840 | namespace { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1841 | #define DEF_JOB(NAME, DATA, KIND) \ |
| 1842 | class NAME : public VisitorJob { \ |
| 1843 | public: \ |
| 1844 | NAME(const DATA *d, CXCursor parent) \ |
| 1845 | : VisitorJob(parent, VisitorJob::KIND, d) {} \ |
| 1846 | static bool classof(const VisitorJob *VJ) { \ |
| 1847 | return VJ->getKind() == KIND; \ |
| 1848 | } \ |
| 1849 | const DATA *get() const { return static_cast<const DATA *>(data[0]); } \ |
| 1850 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1851 | |
| 1852 | DEF_JOB(StmtVisit, Stmt, StmtVisitKind) |
| 1853 | DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind) |
| 1854 | DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind) |
| 1855 | DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1856 | DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind) |
| 1857 | DEF_JOB(LambdaExprParts, LambdaExpr, LambdaExprPartsKind) |
| 1858 | DEF_JOB(PostChildrenVisit, void, PostChildrenVisitKind) |
| 1859 | #undef DEF_JOB |
| 1860 | |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 1861 | class ExplicitTemplateArgsVisit : public VisitorJob { |
| 1862 | public: |
| 1863 | ExplicitTemplateArgsVisit(const TemplateArgumentLoc *Begin, |
| 1864 | const TemplateArgumentLoc *End, CXCursor parent) |
| 1865 | : VisitorJob(parent, VisitorJob::ExplicitTemplateArgsVisitKind, Begin, |
| 1866 | End) {} |
| 1867 | static bool classof(const VisitorJob *VJ) { |
| 1868 | return VJ->getKind() == ExplicitTemplateArgsVisitKind; |
| 1869 | } |
| 1870 | const TemplateArgumentLoc *begin() const { |
| 1871 | return static_cast<const TemplateArgumentLoc *>(data[0]); |
| 1872 | } |
| 1873 | const TemplateArgumentLoc *end() { |
| 1874 | return static_cast<const TemplateArgumentLoc *>(data[1]); |
| 1875 | } |
| 1876 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1877 | class DeclVisit : public VisitorJob { |
| 1878 | public: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1879 | DeclVisit(const Decl *D, CXCursor parent, bool isFirst) |
| 1880 | : VisitorJob(parent, VisitorJob::DeclVisitKind, D, |
| 1881 | isFirst ? (void *)1 : (void *)nullptr) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1882 | static bool classof(const VisitorJob *VJ) { |
| 1883 | return VJ->getKind() == DeclVisitKind; |
| 1884 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1885 | const Decl *get() const { return static_cast<const Decl *>(data[0]); } |
Dmitri Gribenko | e5423a7 | 2015-03-23 19:23:50 +0000 | [diff] [blame] | 1886 | bool isFirst() const { return data[1] != nullptr; } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1887 | }; |
| 1888 | class TypeLocVisit : public VisitorJob { |
| 1889 | public: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1890 | TypeLocVisit(TypeLoc tl, CXCursor parent) |
| 1891 | : VisitorJob(parent, VisitorJob::TypeLocVisitKind, |
| 1892 | tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1893 | |
| 1894 | static bool classof(const VisitorJob *VJ) { |
| 1895 | return VJ->getKind() == TypeLocVisitKind; |
| 1896 | } |
| 1897 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1898 | TypeLoc get() const { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1899 | QualType T = QualType::getFromOpaquePtr(data[0]); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1900 | return TypeLoc(T, const_cast<void *>(data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1901 | } |
| 1902 | }; |
| 1903 | |
| 1904 | class LabelRefVisit : public VisitorJob { |
| 1905 | public: |
| 1906 | LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1907 | : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD, |
| 1908 | labelLoc.getPtrEncoding()) {} |
| 1909 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1910 | static bool classof(const VisitorJob *VJ) { |
| 1911 | return VJ->getKind() == VisitorJob::LabelRefVisitKind; |
| 1912 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1913 | const LabelDecl *get() const { |
| 1914 | return static_cast<const LabelDecl *>(data[0]); |
| 1915 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1916 | SourceLocation getLoc() const { |
| 1917 | return SourceLocation::getFromPtrEncoding(data[1]); |
| 1918 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1919 | }; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1920 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1921 | class NestedNameSpecifierLocVisit : public VisitorJob { |
| 1922 | public: |
| 1923 | NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1924 | : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind, |
| 1925 | Qualifier.getNestedNameSpecifier(), |
| 1926 | Qualifier.getOpaqueData()) {} |
| 1927 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1928 | static bool classof(const VisitorJob *VJ) { |
| 1929 | return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind; |
| 1930 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1931 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1932 | NestedNameSpecifierLoc get() const { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1933 | return NestedNameSpecifierLoc( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1934 | const_cast<NestedNameSpecifier *>( |
| 1935 | static_cast<const NestedNameSpecifier *>(data[0])), |
| 1936 | const_cast<void *>(data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1937 | } |
| 1938 | }; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1939 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1940 | class DeclarationNameInfoVisit : public VisitorJob { |
| 1941 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1942 | DeclarationNameInfoVisit(const Stmt *S, CXCursor parent) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1943 | : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1944 | static bool classof(const VisitorJob *VJ) { |
| 1945 | return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind; |
| 1946 | } |
| 1947 | DeclarationNameInfo get() const { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1948 | const Stmt *S = static_cast<const Stmt *>(data[0]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1949 | switch (S->getStmtClass()) { |
| 1950 | default: |
| 1951 | llvm_unreachable("Unhandled Stmt"); |
| 1952 | case clang::Stmt::MSDependentExistsStmtClass: |
| 1953 | return cast<MSDependentExistsStmt>(S)->getNameInfo(); |
| 1954 | case Stmt::CXXDependentScopeMemberExprClass: |
| 1955 | return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo(); |
| 1956 | case Stmt::DependentScopeDeclRefExprClass: |
| 1957 | return cast<DependentScopeDeclRefExpr>(S)->getNameInfo(); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1958 | case Stmt::OMPCriticalDirectiveClass: |
| 1959 | return cast<OMPCriticalDirective>(S)->getDirectiveName(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1960 | } |
| 1961 | } |
| 1962 | }; |
| 1963 | class MemberRefVisit : public VisitorJob { |
| 1964 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1965 | MemberRefVisit(const FieldDecl *D, SourceLocation L, CXCursor parent) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1966 | : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D, |
| 1967 | L.getPtrEncoding()) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1968 | static bool classof(const VisitorJob *VJ) { |
| 1969 | return VJ->getKind() == VisitorJob::MemberRefVisitKind; |
| 1970 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1971 | const FieldDecl *get() const { |
| 1972 | return static_cast<const FieldDecl *>(data[0]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1973 | } |
| 1974 | SourceLocation getLoc() const { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1975 | return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)data[1]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1976 | } |
| 1977 | }; |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1978 | class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void> { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1979 | friend class OMPClauseEnqueue; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1980 | VisitorWorkList &WL; |
| 1981 | CXCursor Parent; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1982 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1983 | public: |
| 1984 | EnqueueVisitor(VisitorWorkList &wl, CXCursor parent) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1985 | : WL(wl), Parent(parent) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1986 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1987 | void VisitAddrLabelExpr(const AddrLabelExpr *E); |
| 1988 | void VisitBlockExpr(const BlockExpr *B); |
| 1989 | void VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 1990 | void VisitCompoundStmt(const CompoundStmt *S); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 1991 | void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { /* Do nothing. */ |
| 1992 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1993 | void VisitMSDependentExistsStmt(const MSDependentExistsStmt *S); |
| 1994 | void VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E); |
| 1995 | void VisitCXXNewExpr(const CXXNewExpr *E); |
| 1996 | void VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E); |
| 1997 | void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *E); |
| 1998 | void VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E); |
| 1999 | void VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *E); |
| 2000 | void VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
| 2001 | void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *E); |
| 2002 | void VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
| 2003 | void VisitCXXCatchStmt(const CXXCatchStmt *S); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2004 | void VisitCXXForRangeStmt(const CXXForRangeStmt *S); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2005 | void VisitDeclRefExpr(const DeclRefExpr *D); |
| 2006 | void VisitDeclStmt(const DeclStmt *S); |
| 2007 | void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E); |
| 2008 | void VisitDesignatedInitExpr(const DesignatedInitExpr *E); |
| 2009 | void VisitExplicitCastExpr(const ExplicitCastExpr *E); |
| 2010 | void VisitForStmt(const ForStmt *FS); |
| 2011 | void VisitGotoStmt(const GotoStmt *GS); |
| 2012 | void VisitIfStmt(const IfStmt *If); |
| 2013 | void VisitInitListExpr(const InitListExpr *IE); |
| 2014 | void VisitMemberExpr(const MemberExpr *M); |
| 2015 | void VisitOffsetOfExpr(const OffsetOfExpr *E); |
| 2016 | void VisitObjCEncodeExpr(const ObjCEncodeExpr *E); |
| 2017 | void VisitObjCMessageExpr(const ObjCMessageExpr *M); |
| 2018 | void VisitOverloadExpr(const OverloadExpr *E); |
| 2019 | void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
| 2020 | void VisitStmt(const Stmt *S); |
| 2021 | void VisitSwitchStmt(const SwitchStmt *S); |
| 2022 | void VisitWhileStmt(const WhileStmt *W); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2023 | void VisitTypeTraitExpr(const TypeTraitExpr *E); |
| 2024 | void VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E); |
| 2025 | void VisitExpressionTraitExpr(const ExpressionTraitExpr *E); |
| 2026 | void VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U); |
| 2027 | void VisitVAArgExpr(const VAArgExpr *E); |
| 2028 | void VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
| 2029 | void VisitPseudoObjectExpr(const PseudoObjectExpr *E); |
| 2030 | void VisitOpaqueValueExpr(const OpaqueValueExpr *E); |
| 2031 | void VisitLambdaExpr(const LambdaExpr *E); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2032 | void VisitOMPExecutableDirective(const OMPExecutableDirective *D); |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2033 | void VisitOMPLoopDirective(const OMPLoopDirective *D); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2034 | void VisitOMPParallelDirective(const OMPParallelDirective *D); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2035 | void VisitOMPSimdDirective(const OMPSimdDirective *D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2036 | void VisitOMPForDirective(const OMPForDirective *D); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2037 | void VisitOMPForSimdDirective(const OMPForSimdDirective *D); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2038 | void VisitOMPSectionsDirective(const OMPSectionsDirective *D); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2039 | void VisitOMPSectionDirective(const OMPSectionDirective *D); |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 2040 | void VisitOMPSingleDirective(const OMPSingleDirective *D); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2041 | void VisitOMPMasterDirective(const OMPMasterDirective *D); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2042 | void VisitOMPCriticalDirective(const OMPCriticalDirective *D); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2043 | void VisitOMPParallelForDirective(const OMPParallelForDirective *D); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2044 | void VisitOMPParallelForSimdDirective(const OMPParallelForSimdDirective *D); |
cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 2045 | void VisitOMPParallelMasterDirective(const OMPParallelMasterDirective *D); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2046 | void VisitOMPParallelSectionsDirective(const OMPParallelSectionsDirective *D); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2047 | void VisitOMPTaskDirective(const OMPTaskDirective *D); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2048 | void VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 2049 | void VisitOMPBarrierDirective(const OMPBarrierDirective *D); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 2050 | void VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2051 | void VisitOMPTaskgroupDirective(const OMPTaskgroupDirective *D); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2052 | void |
| 2053 | VisitOMPCancellationPointDirective(const OMPCancellationPointDirective *D); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2054 | void VisitOMPCancelDirective(const OMPCancelDirective *D); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2055 | void VisitOMPFlushDirective(const OMPFlushDirective *D); |
Alexey Bataev | c112e94 | 2020-02-28 09:52:15 -0500 | [diff] [blame] | 2056 | void VisitOMPDepobjDirective(const OMPDepobjDirective *D); |
Alexey Bataev | fcba7c3 | 2020-03-20 07:03:01 -0400 | [diff] [blame] | 2057 | void VisitOMPScanDirective(const OMPScanDirective *D); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2058 | void VisitOMPOrderedDirective(const OMPOrderedDirective *D); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2059 | void VisitOMPAtomicDirective(const OMPAtomicDirective *D); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 2060 | void VisitOMPTargetDirective(const OMPTargetDirective *D); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2061 | void VisitOMPTargetDataDirective(const OMPTargetDataDirective *D); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 2062 | void VisitOMPTargetEnterDataDirective(const OMPTargetEnterDataDirective *D); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 2063 | void VisitOMPTargetExitDataDirective(const OMPTargetExitDataDirective *D); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 2064 | void VisitOMPTargetParallelDirective(const OMPTargetParallelDirective *D); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 2065 | void |
| 2066 | VisitOMPTargetParallelForDirective(const OMPTargetParallelForDirective *D); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2067 | void VisitOMPTeamsDirective(const OMPTeamsDirective *D); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 2068 | void VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 2069 | void VisitOMPTaskLoopSimdDirective(const OMPTaskLoopSimdDirective *D); |
Alexey Bataev | 60e51c4 | 2019-10-10 20:13:02 +0000 | [diff] [blame] | 2070 | void VisitOMPMasterTaskLoopDirective(const OMPMasterTaskLoopDirective *D); |
Alexey Bataev | b8552ab | 2019-10-18 16:47:35 +0000 | [diff] [blame] | 2071 | void |
| 2072 | VisitOMPMasterTaskLoopSimdDirective(const OMPMasterTaskLoopSimdDirective *D); |
Alexey Bataev | 5bbcead | 2019-10-14 17:17:41 +0000 | [diff] [blame] | 2073 | void VisitOMPParallelMasterTaskLoopDirective( |
| 2074 | const OMPParallelMasterTaskLoopDirective *D); |
Alexey Bataev | 14a388f | 2019-10-25 10:27:13 -0400 | [diff] [blame] | 2075 | void VisitOMPParallelMasterTaskLoopSimdDirective( |
| 2076 | const OMPParallelMasterTaskLoopSimdDirective *D); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2077 | void VisitOMPDistributeDirective(const OMPDistributeDirective *D); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2078 | void VisitOMPDistributeParallelForDirective( |
| 2079 | const OMPDistributeParallelForDirective *D); |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2080 | void VisitOMPDistributeParallelForSimdDirective( |
| 2081 | const OMPDistributeParallelForSimdDirective *D); |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2082 | void VisitOMPDistributeSimdDirective(const OMPDistributeSimdDirective *D); |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 2083 | void VisitOMPTargetParallelForSimdDirective( |
| 2084 | const OMPTargetParallelForSimdDirective *D); |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2085 | void VisitOMPTargetSimdDirective(const OMPTargetSimdDirective *D); |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 2086 | void VisitOMPTeamsDistributeDirective(const OMPTeamsDistributeDirective *D); |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 2087 | void VisitOMPTeamsDistributeSimdDirective( |
| 2088 | const OMPTeamsDistributeSimdDirective *D); |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 2089 | void VisitOMPTeamsDistributeParallelForSimdDirective( |
| 2090 | const OMPTeamsDistributeParallelForSimdDirective *D); |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 2091 | void VisitOMPTeamsDistributeParallelForDirective( |
| 2092 | const OMPTeamsDistributeParallelForDirective *D); |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 2093 | void VisitOMPTargetTeamsDirective(const OMPTargetTeamsDirective *D); |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 2094 | void VisitOMPTargetTeamsDistributeDirective( |
| 2095 | const OMPTargetTeamsDistributeDirective *D); |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 2096 | void VisitOMPTargetTeamsDistributeParallelForDirective( |
| 2097 | const OMPTargetTeamsDistributeParallelForDirective *D); |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 2098 | void VisitOMPTargetTeamsDistributeParallelForSimdDirective( |
| 2099 | const OMPTargetTeamsDistributeParallelForSimdDirective *D); |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 2100 | void VisitOMPTargetTeamsDistributeSimdDirective( |
| 2101 | const OMPTargetTeamsDistributeSimdDirective *D); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2102 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2103 | private: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2104 | void AddDeclarationNameInfo(const Stmt *S); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2105 | void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier); |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2106 | void AddExplicitTemplateArgs(const TemplateArgumentLoc *A, |
| 2107 | unsigned NumTemplateArgs); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2108 | void AddMemberRef(const FieldDecl *D, SourceLocation L); |
| 2109 | void AddStmt(const Stmt *S); |
| 2110 | void AddDecl(const Decl *D, bool isFirst = true); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2111 | void AddTypeLoc(TypeSourceInfo *TI); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2112 | void EnqueueChildren(const Stmt *S); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2113 | void EnqueueChildren(const OMPClause *S); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2114 | }; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2115 | } // namespace |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2116 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2117 | void EnqueueVisitor::AddDeclarationNameInfo(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2118 | // 'S' should always be non-null, since it comes from the |
| 2119 | // statement we are visiting. |
| 2120 | WL.push_back(DeclarationNameInfoVisit(S, Parent)); |
| 2121 | } |
| 2122 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2123 | void EnqueueVisitor::AddNestedNameSpecifierLoc( |
| 2124 | NestedNameSpecifierLoc Qualifier) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2125 | if (Qualifier) |
| 2126 | WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent)); |
| 2127 | } |
| 2128 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2129 | void EnqueueVisitor::AddStmt(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2130 | if (S) |
| 2131 | WL.push_back(StmtVisit(S, Parent)); |
| 2132 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2133 | void EnqueueVisitor::AddDecl(const Decl *D, bool isFirst) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2134 | if (D) |
| 2135 | WL.push_back(DeclVisit(D, Parent, isFirst)); |
| 2136 | } |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2137 | void EnqueueVisitor::AddExplicitTemplateArgs(const TemplateArgumentLoc *A, |
| 2138 | unsigned NumTemplateArgs) { |
| 2139 | WL.push_back(ExplicitTemplateArgsVisit(A, A + NumTemplateArgs, Parent)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2140 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2141 | void EnqueueVisitor::AddMemberRef(const FieldDecl *D, SourceLocation L) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2142 | if (D) |
| 2143 | WL.push_back(MemberRefVisit(D, L, Parent)); |
| 2144 | } |
| 2145 | void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) { |
| 2146 | if (TI) |
| 2147 | WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent)); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2148 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2149 | void EnqueueVisitor::EnqueueChildren(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2150 | unsigned size = WL.size(); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2151 | for (const Stmt *SubStmt : S->children()) { |
| 2152 | AddStmt(SubStmt); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2153 | } |
| 2154 | if (size == WL.size()) |
| 2155 | return; |
| 2156 | // Now reverse the entries we just added. This will match the DFS |
| 2157 | // ordering performed by the worklist. |
| 2158 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2159 | std::reverse(I, E); |
| 2160 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2161 | namespace { |
| 2162 | class OMPClauseEnqueue : public ConstOMPClauseVisitor<OMPClauseEnqueue> { |
| 2163 | EnqueueVisitor *Visitor; |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2164 | /// Process clauses with list of variables. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2165 | template <typename T> void VisitOMPClauseList(T *Node); |
| 2166 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2167 | public: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2168 | OMPClauseEnqueue(EnqueueVisitor *Visitor) : Visitor(Visitor) {} |
Johannes Doerfert | 419a559 | 2020-03-30 19:58:40 -0500 | [diff] [blame] | 2169 | #define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(const Class *C); |
| 2170 | #include "llvm/Frontend/OpenMP/OMPKinds.def" |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2171 | void VisitOMPClauseWithPreInit(const OMPClauseWithPreInit *C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2172 | void VisitOMPClauseWithPostUpdate(const OMPClauseWithPostUpdate *C); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2173 | }; |
| 2174 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2175 | void OMPClauseEnqueue::VisitOMPClauseWithPreInit( |
| 2176 | const OMPClauseWithPreInit *C) { |
| 2177 | Visitor->AddStmt(C->getPreInitStmt()); |
| 2178 | } |
| 2179 | |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2180 | void OMPClauseEnqueue::VisitOMPClauseWithPostUpdate( |
| 2181 | const OMPClauseWithPostUpdate *C) { |
Alexey Bataev | 37e594c | 2016-03-04 07:21:16 +0000 | [diff] [blame] | 2182 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2183 | Visitor->AddStmt(C->getPostUpdateExpr()); |
| 2184 | } |
| 2185 | |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2186 | void OMPClauseEnqueue::VisitOMPIfClause(const OMPIfClause *C) { |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 2187 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2188 | Visitor->AddStmt(C->getCondition()); |
| 2189 | } |
| 2190 | |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 2191 | void OMPClauseEnqueue::VisitOMPFinalClause(const OMPFinalClause *C) { |
| 2192 | Visitor->AddStmt(C->getCondition()); |
| 2193 | } |
| 2194 | |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2195 | void OMPClauseEnqueue::VisitOMPNumThreadsClause(const OMPNumThreadsClause *C) { |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 2196 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2197 | Visitor->AddStmt(C->getNumThreads()); |
| 2198 | } |
| 2199 | |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 2200 | void OMPClauseEnqueue::VisitOMPSafelenClause(const OMPSafelenClause *C) { |
| 2201 | Visitor->AddStmt(C->getSafelen()); |
| 2202 | } |
| 2203 | |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 2204 | void OMPClauseEnqueue::VisitOMPSimdlenClause(const OMPSimdlenClause *C) { |
| 2205 | Visitor->AddStmt(C->getSimdlen()); |
| 2206 | } |
| 2207 | |
Alexey Bataev | 9cc10fc | 2019-03-12 18:52:33 +0000 | [diff] [blame] | 2208 | void OMPClauseEnqueue::VisitOMPAllocatorClause(const OMPAllocatorClause *C) { |
| 2209 | Visitor->AddStmt(C->getAllocator()); |
| 2210 | } |
| 2211 | |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 2212 | void OMPClauseEnqueue::VisitOMPCollapseClause(const OMPCollapseClause *C) { |
| 2213 | Visitor->AddStmt(C->getNumForLoops()); |
| 2214 | } |
| 2215 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2216 | void OMPClauseEnqueue::VisitOMPDefaultClause(const OMPDefaultClause *C) {} |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2217 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2218 | void OMPClauseEnqueue::VisitOMPProcBindClause(const OMPProcBindClause *C) {} |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 2219 | |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2220 | void OMPClauseEnqueue::VisitOMPScheduleClause(const OMPScheduleClause *C) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2221 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2222 | Visitor->AddStmt(C->getChunkSize()); |
| 2223 | } |
| 2224 | |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 2225 | void OMPClauseEnqueue::VisitOMPOrderedClause(const OMPOrderedClause *C) { |
| 2226 | Visitor->AddStmt(C->getNumForLoops()); |
| 2227 | } |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 2228 | |
Alexey Bataev | 0f0564b | 2020-03-17 09:17:42 -0400 | [diff] [blame] | 2229 | void OMPClauseEnqueue::VisitOMPDetachClause(const OMPDetachClause *C) { |
| 2230 | Visitor->AddStmt(C->getEventHandler()); |
| 2231 | } |
| 2232 | |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 2233 | void OMPClauseEnqueue::VisitOMPNowaitClause(const OMPNowaitClause *) {} |
| 2234 | |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 2235 | void OMPClauseEnqueue::VisitOMPUntiedClause(const OMPUntiedClause *) {} |
| 2236 | |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 2237 | void OMPClauseEnqueue::VisitOMPMergeableClause(const OMPMergeableClause *) {} |
| 2238 | |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 2239 | void OMPClauseEnqueue::VisitOMPReadClause(const OMPReadClause *) {} |
| 2240 | |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 2241 | void OMPClauseEnqueue::VisitOMPWriteClause(const OMPWriteClause *) {} |
| 2242 | |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 2243 | void OMPClauseEnqueue::VisitOMPUpdateClause(const OMPUpdateClause *) {} |
| 2244 | |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 2245 | void OMPClauseEnqueue::VisitOMPCaptureClause(const OMPCaptureClause *) {} |
| 2246 | |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 2247 | void OMPClauseEnqueue::VisitOMPSeqCstClause(const OMPSeqCstClause *) {} |
| 2248 | |
Alexey Bataev | ea9166b | 2020-02-06 16:30:23 -0500 | [diff] [blame] | 2249 | void OMPClauseEnqueue::VisitOMPAcqRelClause(const OMPAcqRelClause *) {} |
| 2250 | |
Alexey Bataev | 04a830f | 2020-02-10 14:30:39 -0500 | [diff] [blame] | 2251 | void OMPClauseEnqueue::VisitOMPAcquireClause(const OMPAcquireClause *) {} |
| 2252 | |
Alexey Bataev | 9559834 | 2020-02-10 15:49:05 -0500 | [diff] [blame] | 2253 | void OMPClauseEnqueue::VisitOMPReleaseClause(const OMPReleaseClause *) {} |
| 2254 | |
Alexey Bataev | 9a8defc | 2020-02-11 11:10:43 -0500 | [diff] [blame] | 2255 | void OMPClauseEnqueue::VisitOMPRelaxedClause(const OMPRelaxedClause *) {} |
| 2256 | |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 2257 | void OMPClauseEnqueue::VisitOMPThreadsClause(const OMPThreadsClause *) {} |
| 2258 | |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 2259 | void OMPClauseEnqueue::VisitOMPSIMDClause(const OMPSIMDClause *) {} |
| 2260 | |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 2261 | void OMPClauseEnqueue::VisitOMPNogroupClause(const OMPNogroupClause *) {} |
| 2262 | |
Alexey Bataev | 375437a | 2020-03-02 14:21:20 -0500 | [diff] [blame] | 2263 | void OMPClauseEnqueue::VisitOMPDestroyClause(const OMPDestroyClause *) {} |
| 2264 | |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 2265 | void OMPClauseEnqueue::VisitOMPUnifiedAddressClause( |
| 2266 | const OMPUnifiedAddressClause *) {} |
| 2267 | |
Patrick Lyster | 4a370b9 | 2018-10-01 13:47:43 +0000 | [diff] [blame] | 2268 | void OMPClauseEnqueue::VisitOMPUnifiedSharedMemoryClause( |
| 2269 | const OMPUnifiedSharedMemoryClause *) {} |
| 2270 | |
Patrick Lyster | 6bdf63b | 2018-10-03 20:07:58 +0000 | [diff] [blame] | 2271 | void OMPClauseEnqueue::VisitOMPReverseOffloadClause( |
| 2272 | const OMPReverseOffloadClause *) {} |
| 2273 | |
Patrick Lyster | 3fe9e39 | 2018-10-11 14:41:10 +0000 | [diff] [blame] | 2274 | void OMPClauseEnqueue::VisitOMPDynamicAllocatorsClause( |
| 2275 | const OMPDynamicAllocatorsClause *) {} |
| 2276 | |
Patrick Lyster | 7a2a27c | 2018-11-02 12:18:11 +0000 | [diff] [blame] | 2277 | void OMPClauseEnqueue::VisitOMPAtomicDefaultMemOrderClause( |
| 2278 | const OMPAtomicDefaultMemOrderClause *) {} |
| 2279 | |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 2280 | void OMPClauseEnqueue::VisitOMPDeviceClause(const OMPDeviceClause *C) { |
| 2281 | Visitor->AddStmt(C->getDevice()); |
| 2282 | } |
| 2283 | |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 2284 | void OMPClauseEnqueue::VisitOMPNumTeamsClause(const OMPNumTeamsClause *C) { |
Arpith Chacko Jacob | bc12634 | 2017-01-25 11:28:18 +0000 | [diff] [blame] | 2285 | VisitOMPClauseWithPreInit(C); |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 2286 | Visitor->AddStmt(C->getNumTeams()); |
| 2287 | } |
| 2288 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2289 | void OMPClauseEnqueue::VisitOMPThreadLimitClause( |
| 2290 | const OMPThreadLimitClause *C) { |
Arpith Chacko Jacob | 7ecc0b7 | 2017-01-25 11:44:35 +0000 | [diff] [blame] | 2291 | VisitOMPClauseWithPreInit(C); |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 2292 | Visitor->AddStmt(C->getThreadLimit()); |
| 2293 | } |
| 2294 | |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 2295 | void OMPClauseEnqueue::VisitOMPPriorityClause(const OMPPriorityClause *C) { |
| 2296 | Visitor->AddStmt(C->getPriority()); |
| 2297 | } |
| 2298 | |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 2299 | void OMPClauseEnqueue::VisitOMPGrainsizeClause(const OMPGrainsizeClause *C) { |
| 2300 | Visitor->AddStmt(C->getGrainsize()); |
| 2301 | } |
| 2302 | |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 2303 | void OMPClauseEnqueue::VisitOMPNumTasksClause(const OMPNumTasksClause *C) { |
| 2304 | Visitor->AddStmt(C->getNumTasks()); |
| 2305 | } |
| 2306 | |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 2307 | void OMPClauseEnqueue::VisitOMPHintClause(const OMPHintClause *C) { |
| 2308 | Visitor->AddStmt(C->getHint()); |
| 2309 | } |
| 2310 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2311 | template <typename T> void OMPClauseEnqueue::VisitOMPClauseList(T *Node) { |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2312 | for (const auto *I : Node->varlists()) { |
Aaron Ballman | 2205d2a | 2014-03-14 15:55:35 +0000 | [diff] [blame] | 2313 | Visitor->AddStmt(I); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2314 | } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2315 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2316 | |
Alexey Bataev | 06dea73 | 2020-03-20 09:41:22 -0400 | [diff] [blame] | 2317 | void OMPClauseEnqueue::VisitOMPInclusiveClause(const OMPInclusiveClause *C) { |
| 2318 | VisitOMPClauseList(C); |
| 2319 | } |
Alexey Bataev | 63828a3 | 2020-03-23 10:41:08 -0400 | [diff] [blame] | 2320 | void OMPClauseEnqueue::VisitOMPExclusiveClause(const OMPExclusiveClause *C) { |
| 2321 | VisitOMPClauseList(C); |
| 2322 | } |
Alexey Bataev | e04483e | 2019-03-27 14:14:31 +0000 | [diff] [blame] | 2323 | void OMPClauseEnqueue::VisitOMPAllocateClause(const OMPAllocateClause *C) { |
| 2324 | VisitOMPClauseList(C); |
| 2325 | Visitor->AddStmt(C->getAllocator()); |
| 2326 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2327 | void OMPClauseEnqueue::VisitOMPPrivateClause(const OMPPrivateClause *C) { |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2328 | VisitOMPClauseList(C); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2329 | for (const auto *E : C->private_copies()) { |
| 2330 | Visitor->AddStmt(E); |
| 2331 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2332 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2333 | void OMPClauseEnqueue::VisitOMPFirstprivateClause( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2334 | const OMPFirstprivateClause *C) { |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2335 | VisitOMPClauseList(C); |
Alexey Bataev | 417089f | 2016-02-17 13:19:37 +0000 | [diff] [blame] | 2336 | VisitOMPClauseWithPreInit(C); |
| 2337 | for (const auto *E : C->private_copies()) { |
| 2338 | Visitor->AddStmt(E); |
| 2339 | } |
| 2340 | for (const auto *E : C->inits()) { |
| 2341 | Visitor->AddStmt(E); |
| 2342 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2343 | } |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2344 | void OMPClauseEnqueue::VisitOMPLastprivateClause( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2345 | const OMPLastprivateClause *C) { |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2346 | VisitOMPClauseList(C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2347 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2348 | for (auto *E : C->private_copies()) { |
| 2349 | Visitor->AddStmt(E); |
| 2350 | } |
| 2351 | for (auto *E : C->source_exprs()) { |
| 2352 | Visitor->AddStmt(E); |
| 2353 | } |
| 2354 | for (auto *E : C->destination_exprs()) { |
| 2355 | Visitor->AddStmt(E); |
| 2356 | } |
| 2357 | for (auto *E : C->assignment_ops()) { |
| 2358 | Visitor->AddStmt(E); |
| 2359 | } |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2360 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2361 | void OMPClauseEnqueue::VisitOMPSharedClause(const OMPSharedClause *C) { |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2362 | VisitOMPClauseList(C); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2363 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2364 | void OMPClauseEnqueue::VisitOMPReductionClause(const OMPReductionClause *C) { |
| 2365 | VisitOMPClauseList(C); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2366 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 2367 | for (auto *E : C->privates()) { |
| 2368 | Visitor->AddStmt(E); |
| 2369 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2370 | for (auto *E : C->lhs_exprs()) { |
| 2371 | Visitor->AddStmt(E); |
| 2372 | } |
| 2373 | for (auto *E : C->rhs_exprs()) { |
| 2374 | Visitor->AddStmt(E); |
| 2375 | } |
| 2376 | for (auto *E : C->reduction_ops()) { |
| 2377 | Visitor->AddStmt(E); |
| 2378 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2379 | } |
Alexey Bataev | 169d96a | 2017-07-18 20:17:46 +0000 | [diff] [blame] | 2380 | void OMPClauseEnqueue::VisitOMPTaskReductionClause( |
| 2381 | const OMPTaskReductionClause *C) { |
| 2382 | VisitOMPClauseList(C); |
| 2383 | VisitOMPClauseWithPostUpdate(C); |
| 2384 | for (auto *E : C->privates()) { |
| 2385 | Visitor->AddStmt(E); |
| 2386 | } |
| 2387 | for (auto *E : C->lhs_exprs()) { |
| 2388 | Visitor->AddStmt(E); |
| 2389 | } |
| 2390 | for (auto *E : C->rhs_exprs()) { |
| 2391 | Visitor->AddStmt(E); |
| 2392 | } |
| 2393 | for (auto *E : C->reduction_ops()) { |
| 2394 | Visitor->AddStmt(E); |
| 2395 | } |
| 2396 | } |
Alexey Bataev | fa312f3 | 2017-07-21 18:48:21 +0000 | [diff] [blame] | 2397 | void OMPClauseEnqueue::VisitOMPInReductionClause( |
| 2398 | const OMPInReductionClause *C) { |
| 2399 | VisitOMPClauseList(C); |
| 2400 | VisitOMPClauseWithPostUpdate(C); |
| 2401 | for (auto *E : C->privates()) { |
| 2402 | Visitor->AddStmt(E); |
| 2403 | } |
| 2404 | for (auto *E : C->lhs_exprs()) { |
| 2405 | Visitor->AddStmt(E); |
| 2406 | } |
| 2407 | for (auto *E : C->rhs_exprs()) { |
| 2408 | Visitor->AddStmt(E); |
| 2409 | } |
| 2410 | for (auto *E : C->reduction_ops()) { |
| 2411 | Visitor->AddStmt(E); |
| 2412 | } |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 2413 | for (auto *E : C->taskgroup_descriptors()) |
| 2414 | Visitor->AddStmt(E); |
Alexey Bataev | fa312f3 | 2017-07-21 18:48:21 +0000 | [diff] [blame] | 2415 | } |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2416 | void OMPClauseEnqueue::VisitOMPLinearClause(const OMPLinearClause *C) { |
| 2417 | VisitOMPClauseList(C); |
Alexey Bataev | 78849fb | 2016-03-09 09:49:00 +0000 | [diff] [blame] | 2418 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 2419 | for (const auto *E : C->privates()) { |
| 2420 | Visitor->AddStmt(E); |
| 2421 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 2422 | for (const auto *E : C->inits()) { |
| 2423 | Visitor->AddStmt(E); |
| 2424 | } |
| 2425 | for (const auto *E : C->updates()) { |
| 2426 | Visitor->AddStmt(E); |
| 2427 | } |
| 2428 | for (const auto *E : C->finals()) { |
| 2429 | Visitor->AddStmt(E); |
| 2430 | } |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2431 | Visitor->AddStmt(C->getStep()); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 2432 | Visitor->AddStmt(C->getCalcStep()); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2433 | } |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 2434 | void OMPClauseEnqueue::VisitOMPAlignedClause(const OMPAlignedClause *C) { |
| 2435 | VisitOMPClauseList(C); |
| 2436 | Visitor->AddStmt(C->getAlignment()); |
| 2437 | } |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 2438 | void OMPClauseEnqueue::VisitOMPCopyinClause(const OMPCopyinClause *C) { |
| 2439 | VisitOMPClauseList(C); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 2440 | for (auto *E : C->source_exprs()) { |
| 2441 | Visitor->AddStmt(E); |
| 2442 | } |
| 2443 | for (auto *E : C->destination_exprs()) { |
| 2444 | Visitor->AddStmt(E); |
| 2445 | } |
| 2446 | for (auto *E : C->assignment_ops()) { |
| 2447 | Visitor->AddStmt(E); |
| 2448 | } |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 2449 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2450 | void OMPClauseEnqueue::VisitOMPCopyprivateClause( |
| 2451 | const OMPCopyprivateClause *C) { |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2452 | VisitOMPClauseList(C); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2453 | for (auto *E : C->source_exprs()) { |
| 2454 | Visitor->AddStmt(E); |
| 2455 | } |
| 2456 | for (auto *E : C->destination_exprs()) { |
| 2457 | Visitor->AddStmt(E); |
| 2458 | } |
| 2459 | for (auto *E : C->assignment_ops()) { |
| 2460 | Visitor->AddStmt(E); |
| 2461 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2462 | } |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2463 | void OMPClauseEnqueue::VisitOMPFlushClause(const OMPFlushClause *C) { |
| 2464 | VisitOMPClauseList(C); |
| 2465 | } |
Alexey Bataev | c112e94 | 2020-02-28 09:52:15 -0500 | [diff] [blame] | 2466 | void OMPClauseEnqueue::VisitOMPDepobjClause(const OMPDepobjClause *C) { |
| 2467 | Visitor->AddStmt(C->getDepobj()); |
| 2468 | } |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 2469 | void OMPClauseEnqueue::VisitOMPDependClause(const OMPDependClause *C) { |
| 2470 | VisitOMPClauseList(C); |
| 2471 | } |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 2472 | void OMPClauseEnqueue::VisitOMPMapClause(const OMPMapClause *C) { |
| 2473 | VisitOMPClauseList(C); |
| 2474 | } |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2475 | void OMPClauseEnqueue::VisitOMPDistScheduleClause( |
| 2476 | const OMPDistScheduleClause *C) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2477 | VisitOMPClauseWithPreInit(C); |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2478 | Visitor->AddStmt(C->getChunkSize()); |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2479 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2480 | void OMPClauseEnqueue::VisitOMPDefaultmapClause( |
| 2481 | const OMPDefaultmapClause * /*C*/) {} |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 2482 | void OMPClauseEnqueue::VisitOMPToClause(const OMPToClause *C) { |
| 2483 | VisitOMPClauseList(C); |
| 2484 | } |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 2485 | void OMPClauseEnqueue::VisitOMPFromClause(const OMPFromClause *C) { |
| 2486 | VisitOMPClauseList(C); |
| 2487 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2488 | void OMPClauseEnqueue::VisitOMPUseDevicePtrClause( |
| 2489 | const OMPUseDevicePtrClause *C) { |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 2490 | VisitOMPClauseList(C); |
| 2491 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2492 | void OMPClauseEnqueue::VisitOMPIsDevicePtrClause( |
| 2493 | const OMPIsDevicePtrClause *C) { |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 2494 | VisitOMPClauseList(C); |
| 2495 | } |
Alexey Bataev | b6e7084 | 2019-12-16 15:54:17 -0500 | [diff] [blame] | 2496 | void OMPClauseEnqueue::VisitOMPNontemporalClause( |
| 2497 | const OMPNontemporalClause *C) { |
| 2498 | VisitOMPClauseList(C); |
Alexey Bataev | 0860db9 | 2019-12-19 10:01:10 -0500 | [diff] [blame] | 2499 | for (const auto *E : C->private_refs()) |
| 2500 | Visitor->AddStmt(E); |
Alexey Bataev | b6e7084 | 2019-12-16 15:54:17 -0500 | [diff] [blame] | 2501 | } |
Alexey Bataev | cb8e691 | 2020-01-31 16:09:26 -0500 | [diff] [blame] | 2502 | void OMPClauseEnqueue::VisitOMPOrderClause(const OMPOrderClause *C) {} |
Alexey Bataev | b5be1c5 | 2020-04-21 13:21:00 -0400 | [diff] [blame] | 2503 | void OMPClauseEnqueue::VisitOMPUsesAllocatorsClause( |
| 2504 | const OMPUsesAllocatorsClause *C) { |
| 2505 | for (unsigned I = 0, E = C->getNumberOfAllocators(); I < E; ++I) { |
| 2506 | const OMPUsesAllocatorsClause::Data &D = C->getAllocatorData(I); |
| 2507 | Visitor->AddStmt(D.Allocator); |
| 2508 | Visitor->AddStmt(D.AllocatorTraits); |
| 2509 | } |
| 2510 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2511 | } // namespace |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2512 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2513 | void EnqueueVisitor::EnqueueChildren(const OMPClause *S) { |
| 2514 | unsigned size = WL.size(); |
| 2515 | OMPClauseEnqueue Visitor(this); |
| 2516 | Visitor.Visit(S); |
| 2517 | if (size == WL.size()) |
| 2518 | return; |
| 2519 | // Now reverse the entries we just added. This will match the DFS |
| 2520 | // ordering performed by the worklist. |
| 2521 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2522 | std::reverse(I, E); |
| 2523 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2524 | void EnqueueVisitor::VisitAddrLabelExpr(const AddrLabelExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2525 | WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent)); |
| 2526 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2527 | void EnqueueVisitor::VisitBlockExpr(const BlockExpr *B) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2528 | AddDecl(B->getBlockDecl()); |
| 2529 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2530 | void EnqueueVisitor::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2531 | EnqueueChildren(E); |
| 2532 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2533 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2534 | void EnqueueVisitor::VisitCompoundStmt(const CompoundStmt *S) { |
Pete Cooper | 57d3f14 | 2015-07-30 17:22:52 +0000 | [diff] [blame] | 2535 | for (auto &I : llvm::reverse(S->body())) |
| 2536 | AddStmt(I); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2537 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2538 | void EnqueueVisitor::VisitMSDependentExistsStmt( |
| 2539 | const MSDependentExistsStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2540 | AddStmt(S->getSubStmt()); |
| 2541 | AddDeclarationNameInfo(S); |
| 2542 | if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc()) |
| 2543 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2544 | } |
| 2545 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2546 | void EnqueueVisitor::VisitCXXDependentScopeMemberExpr( |
| 2547 | const CXXDependentScopeMemberExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2548 | if (E->hasExplicitTemplateArgs()) |
| 2549 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2550 | AddDeclarationNameInfo(E); |
| 2551 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 2552 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2553 | if (!E->isImplicitAccess()) |
| 2554 | AddStmt(E->getBase()); |
| 2555 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2556 | void EnqueueVisitor::VisitCXXNewExpr(const CXXNewExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2557 | // Enqueue the initializer , if any. |
| 2558 | AddStmt(E->getInitializer()); |
| 2559 | // Enqueue the array size, if any. |
Richard Smith | b9fb121 | 2019-05-06 03:47:15 +0000 | [diff] [blame] | 2560 | AddStmt(E->getArraySize().getValueOr(nullptr)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2561 | // Enqueue the allocated type. |
| 2562 | AddTypeLoc(E->getAllocatedTypeSourceInfo()); |
| 2563 | // Enqueue the placement arguments. |
| 2564 | for (unsigned I = E->getNumPlacementArgs(); I > 0; --I) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2565 | AddStmt(E->getPlacementArg(I - 1)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2566 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2567 | void EnqueueVisitor::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CE) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2568 | for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2569 | AddStmt(CE->getArg(I - 1)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2570 | AddStmt(CE->getCallee()); |
| 2571 | AddStmt(CE->getArg(0)); |
| 2572 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2573 | void EnqueueVisitor::VisitCXXPseudoDestructorExpr( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2574 | const CXXPseudoDestructorExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2575 | // Visit the name of the type being destroyed. |
| 2576 | AddTypeLoc(E->getDestroyedTypeInfo()); |
| 2577 | // Visit the scope type that looks disturbingly like the nested-name-specifier |
| 2578 | // but isn't. |
| 2579 | AddTypeLoc(E->getScopeTypeInfo()); |
| 2580 | // Visit the nested-name-specifier. |
| 2581 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 2582 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2583 | // Visit base expression. |
| 2584 | AddStmt(E->getBase()); |
| 2585 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2586 | void EnqueueVisitor::VisitCXXScalarValueInitExpr( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2587 | const CXXScalarValueInitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2588 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2589 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2590 | void EnqueueVisitor::VisitCXXTemporaryObjectExpr( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2591 | const CXXTemporaryObjectExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2592 | EnqueueChildren(E); |
| 2593 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2594 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2595 | void EnqueueVisitor::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2596 | EnqueueChildren(E); |
| 2597 | if (E->isTypeOperand()) |
| 2598 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 2599 | } |
| 2600 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2601 | void EnqueueVisitor::VisitCXXUnresolvedConstructExpr( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2602 | const CXXUnresolvedConstructExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2603 | EnqueueChildren(E); |
| 2604 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2605 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2606 | void EnqueueVisitor::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2607 | EnqueueChildren(E); |
| 2608 | if (E->isTypeOperand()) |
| 2609 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 2610 | } |
| 2611 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2612 | void EnqueueVisitor::VisitCXXCatchStmt(const CXXCatchStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2613 | EnqueueChildren(S); |
| 2614 | AddDecl(S->getExceptionDecl()); |
| 2615 | } |
| 2616 | |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2617 | void EnqueueVisitor::VisitCXXForRangeStmt(const CXXForRangeStmt *S) { |
Argyrios Kyrtzidis | cde7069 | 2014-11-13 09:50:19 +0000 | [diff] [blame] | 2618 | AddStmt(S->getBody()); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2619 | AddStmt(S->getRangeInit()); |
Argyrios Kyrtzidis | cde7069 | 2014-11-13 09:50:19 +0000 | [diff] [blame] | 2620 | AddDecl(S->getLoopVariable()); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2621 | } |
| 2622 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2623 | void EnqueueVisitor::VisitDeclRefExpr(const DeclRefExpr *DR) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2624 | if (DR->hasExplicitTemplateArgs()) |
| 2625 | AddExplicitTemplateArgs(DR->getTemplateArgs(), DR->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2626 | WL.push_back(DeclRefExprParts(DR, Parent)); |
| 2627 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2628 | void EnqueueVisitor::VisitDependentScopeDeclRefExpr( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2629 | const DependentScopeDeclRefExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2630 | if (E->hasExplicitTemplateArgs()) |
| 2631 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2632 | AddDeclarationNameInfo(E); |
| 2633 | AddNestedNameSpecifierLoc(E->getQualifierLoc()); |
| 2634 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2635 | void EnqueueVisitor::VisitDeclStmt(const DeclStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2636 | unsigned size = WL.size(); |
| 2637 | bool isFirst = true; |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 2638 | for (const auto *D : S->decls()) { |
| 2639 | AddDecl(D, isFirst); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2640 | isFirst = false; |
| 2641 | } |
| 2642 | if (size == WL.size()) |
| 2643 | return; |
| 2644 | // Now reverse the entries we just added. This will match the DFS |
| 2645 | // ordering performed by the worklist. |
| 2646 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2647 | std::reverse(I, E); |
| 2648 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2649 | void EnqueueVisitor::VisitDesignatedInitExpr(const DesignatedInitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2650 | AddStmt(E->getInit()); |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2651 | for (const DesignatedInitExpr::Designator &D : |
| 2652 | llvm::reverse(E->designators())) { |
| 2653 | if (D.isFieldDesignator()) { |
| 2654 | if (FieldDecl *Field = D.getField()) |
| 2655 | AddMemberRef(Field, D.getFieldLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2656 | continue; |
| 2657 | } |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2658 | if (D.isArrayDesignator()) { |
| 2659 | AddStmt(E->getArrayIndex(D)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2660 | continue; |
| 2661 | } |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2662 | assert(D.isArrayRangeDesignator() && "Unknown designator kind"); |
| 2663 | AddStmt(E->getArrayRangeEnd(D)); |
| 2664 | AddStmt(E->getArrayRangeStart(D)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2665 | } |
| 2666 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2667 | void EnqueueVisitor::VisitExplicitCastExpr(const ExplicitCastExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2668 | EnqueueChildren(E); |
| 2669 | AddTypeLoc(E->getTypeInfoAsWritten()); |
| 2670 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2671 | void EnqueueVisitor::VisitForStmt(const ForStmt *FS) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2672 | AddStmt(FS->getBody()); |
| 2673 | AddStmt(FS->getInc()); |
| 2674 | AddStmt(FS->getCond()); |
| 2675 | AddDecl(FS->getConditionVariable()); |
| 2676 | AddStmt(FS->getInit()); |
| 2677 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2678 | void EnqueueVisitor::VisitGotoStmt(const GotoStmt *GS) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2679 | WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent)); |
| 2680 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2681 | void EnqueueVisitor::VisitIfStmt(const IfStmt *If) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2682 | AddStmt(If->getElse()); |
| 2683 | AddStmt(If->getThen()); |
| 2684 | AddStmt(If->getCond()); |
Milian Wolff | 08e1812 | 2020-05-02 22:18:09 +0200 | [diff] [blame] | 2685 | AddStmt(If->getInit()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2686 | AddDecl(If->getConditionVariable()); |
| 2687 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2688 | void EnqueueVisitor::VisitInitListExpr(const InitListExpr *IE) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2689 | // We care about the syntactic form of the initializer list, only. |
| 2690 | if (InitListExpr *Syntactic = IE->getSyntacticForm()) |
| 2691 | IE = Syntactic; |
| 2692 | EnqueueChildren(IE); |
| 2693 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2694 | void EnqueueVisitor::VisitMemberExpr(const MemberExpr *M) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2695 | WL.push_back(MemberExprParts(M, Parent)); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2696 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2697 | // If the base of the member access expression is an implicit 'this', don't |
| 2698 | // visit it. |
| 2699 | // FIXME: If we ever want to show these implicit accesses, this will be |
| 2700 | // unfortunate. However, clang_getCursor() relies on this behavior. |
Argyrios Kyrtzidis | 58d0e7a | 2015-03-13 04:40:07 +0000 | [diff] [blame] | 2701 | if (M->isImplicitAccess()) |
| 2702 | return; |
| 2703 | |
| 2704 | // Ignore base anonymous struct/union fields, otherwise they will shadow the |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 2705 | // real field that we are interested in. |
Argyrios Kyrtzidis | 58d0e7a | 2015-03-13 04:40:07 +0000 | [diff] [blame] | 2706 | if (auto *SubME = dyn_cast<MemberExpr>(M->getBase())) { |
| 2707 | if (auto *FD = dyn_cast_or_null<FieldDecl>(SubME->getMemberDecl())) { |
| 2708 | if (FD->isAnonymousStructOrUnion()) { |
| 2709 | AddStmt(SubME->getBase()); |
| 2710 | return; |
| 2711 | } |
| 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | AddStmt(M->getBase()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2716 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2717 | void EnqueueVisitor::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2718 | AddTypeLoc(E->getEncodedTypeSourceInfo()); |
| 2719 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2720 | void EnqueueVisitor::VisitObjCMessageExpr(const ObjCMessageExpr *M) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2721 | EnqueueChildren(M); |
| 2722 | AddTypeLoc(M->getClassReceiverTypeInfo()); |
| 2723 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2724 | void EnqueueVisitor::VisitOffsetOfExpr(const OffsetOfExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2725 | // Visit the components of the offsetof expression. |
| 2726 | for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2727 | const OffsetOfNode &Node = E->getComponent(I - 1); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2728 | switch (Node.getKind()) { |
| 2729 | case OffsetOfNode::Array: |
| 2730 | AddStmt(E->getIndexExpr(Node.getArrayExprIndex())); |
| 2731 | break; |
| 2732 | case OffsetOfNode::Field: |
| 2733 | AddMemberRef(Node.getField(), Node.getSourceRange().getEnd()); |
| 2734 | break; |
| 2735 | case OffsetOfNode::Identifier: |
| 2736 | case OffsetOfNode::Base: |
| 2737 | continue; |
| 2738 | } |
| 2739 | } |
| 2740 | // Visit the type into which we're computing the offset. |
| 2741 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2742 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2743 | void EnqueueVisitor::VisitOverloadExpr(const OverloadExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2744 | if (E->hasExplicitTemplateArgs()) |
| 2745 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2746 | WL.push_back(OverloadExprParts(E, Parent)); |
| 2747 | } |
| 2748 | void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2749 | const UnaryExprOrTypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2750 | EnqueueChildren(E); |
| 2751 | if (E->isArgumentType()) |
| 2752 | AddTypeLoc(E->getArgumentTypeInfo()); |
| 2753 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2754 | void EnqueueVisitor::VisitStmt(const Stmt *S) { EnqueueChildren(S); } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2755 | void EnqueueVisitor::VisitSwitchStmt(const SwitchStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2756 | AddStmt(S->getBody()); |
| 2757 | AddStmt(S->getCond()); |
| 2758 | AddDecl(S->getConditionVariable()); |
| 2759 | } |
| 2760 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2761 | void EnqueueVisitor::VisitWhileStmt(const WhileStmt *W) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2762 | AddStmt(W->getBody()); |
| 2763 | AddStmt(W->getCond()); |
| 2764 | AddDecl(W->getConditionVariable()); |
| 2765 | } |
| 2766 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2767 | void EnqueueVisitor::VisitTypeTraitExpr(const TypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2768 | for (unsigned I = E->getNumArgs(); I > 0; --I) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2769 | AddTypeLoc(E->getArg(I - 1)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2772 | void EnqueueVisitor::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2773 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2774 | } |
| 2775 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2776 | void EnqueueVisitor::VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2777 | EnqueueChildren(E); |
| 2778 | } |
| 2779 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2780 | void EnqueueVisitor::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2781 | VisitOverloadExpr(U); |
| 2782 | if (!U->isImplicitAccess()) |
| 2783 | AddStmt(U->getBase()); |
| 2784 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2785 | void EnqueueVisitor::VisitVAArgExpr(const VAArgExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2786 | AddStmt(E->getSubExpr()); |
| 2787 | AddTypeLoc(E->getWrittenTypeInfo()); |
| 2788 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2789 | void EnqueueVisitor::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2790 | WL.push_back(SizeOfPackExprParts(E, Parent)); |
| 2791 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2792 | void EnqueueVisitor::VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2793 | // If the opaque value has a source expression, just transparently |
| 2794 | // visit that. This is useful for (e.g.) pseudo-object expressions. |
| 2795 | if (Expr *SourceExpr = E->getSourceExpr()) |
| 2796 | return Visit(SourceExpr); |
| 2797 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2798 | void EnqueueVisitor::VisitLambdaExpr(const LambdaExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2799 | AddStmt(E->getBody()); |
| 2800 | WL.push_back(LambdaExprParts(E, Parent)); |
| 2801 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2802 | void EnqueueVisitor::VisitPseudoObjectExpr(const PseudoObjectExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2803 | // Treat the expression like its syntactic form. |
| 2804 | Visit(E->getSyntacticForm()); |
| 2805 | } |
| 2806 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2807 | void EnqueueVisitor::VisitOMPExecutableDirective( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2808 | const OMPExecutableDirective *D) { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2809 | EnqueueChildren(D); |
| 2810 | for (ArrayRef<OMPClause *>::iterator I = D->clauses().begin(), |
| 2811 | E = D->clauses().end(); |
| 2812 | I != E; ++I) |
| 2813 | EnqueueChildren(*I); |
| 2814 | } |
| 2815 | |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2816 | void EnqueueVisitor::VisitOMPLoopDirective(const OMPLoopDirective *D) { |
| 2817 | VisitOMPExecutableDirective(D); |
| 2818 | } |
| 2819 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2820 | void EnqueueVisitor::VisitOMPParallelDirective(const OMPParallelDirective *D) { |
| 2821 | VisitOMPExecutableDirective(D); |
| 2822 | } |
| 2823 | |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2824 | void EnqueueVisitor::VisitOMPSimdDirective(const OMPSimdDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2825 | VisitOMPLoopDirective(D); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2828 | void EnqueueVisitor::VisitOMPForDirective(const OMPForDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2829 | VisitOMPLoopDirective(D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2832 | void EnqueueVisitor::VisitOMPForSimdDirective(const OMPForSimdDirective *D) { |
| 2833 | VisitOMPLoopDirective(D); |
| 2834 | } |
| 2835 | |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2836 | void EnqueueVisitor::VisitOMPSectionsDirective(const OMPSectionsDirective *D) { |
| 2837 | VisitOMPExecutableDirective(D); |
| 2838 | } |
| 2839 | |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2840 | void EnqueueVisitor::VisitOMPSectionDirective(const OMPSectionDirective *D) { |
| 2841 | VisitOMPExecutableDirective(D); |
| 2842 | } |
| 2843 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 2844 | void EnqueueVisitor::VisitOMPSingleDirective(const OMPSingleDirective *D) { |
| 2845 | VisitOMPExecutableDirective(D); |
| 2846 | } |
| 2847 | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2848 | void EnqueueVisitor::VisitOMPMasterDirective(const OMPMasterDirective *D) { |
| 2849 | VisitOMPExecutableDirective(D); |
| 2850 | } |
| 2851 | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2852 | void EnqueueVisitor::VisitOMPCriticalDirective(const OMPCriticalDirective *D) { |
| 2853 | VisitOMPExecutableDirective(D); |
| 2854 | AddDeclarationNameInfo(D); |
| 2855 | } |
| 2856 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2857 | void EnqueueVisitor::VisitOMPParallelForDirective( |
| 2858 | const OMPParallelForDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2859 | VisitOMPLoopDirective(D); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2862 | void EnqueueVisitor::VisitOMPParallelForSimdDirective( |
| 2863 | const OMPParallelForSimdDirective *D) { |
| 2864 | VisitOMPLoopDirective(D); |
| 2865 | } |
| 2866 | |
cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 2867 | void EnqueueVisitor::VisitOMPParallelMasterDirective( |
| 2868 | const OMPParallelMasterDirective *D) { |
| 2869 | VisitOMPExecutableDirective(D); |
| 2870 | } |
| 2871 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2872 | void EnqueueVisitor::VisitOMPParallelSectionsDirective( |
| 2873 | const OMPParallelSectionsDirective *D) { |
| 2874 | VisitOMPExecutableDirective(D); |
| 2875 | } |
| 2876 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2877 | void EnqueueVisitor::VisitOMPTaskDirective(const OMPTaskDirective *D) { |
| 2878 | VisitOMPExecutableDirective(D); |
| 2879 | } |
| 2880 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 2881 | void EnqueueVisitor::VisitOMPTaskyieldDirective( |
| 2882 | const OMPTaskyieldDirective *D) { |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2883 | VisitOMPExecutableDirective(D); |
| 2884 | } |
| 2885 | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 2886 | void EnqueueVisitor::VisitOMPBarrierDirective(const OMPBarrierDirective *D) { |
| 2887 | VisitOMPExecutableDirective(D); |
| 2888 | } |
| 2889 | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 2890 | void EnqueueVisitor::VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D) { |
| 2891 | VisitOMPExecutableDirective(D); |
| 2892 | } |
| 2893 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2894 | void EnqueueVisitor::VisitOMPTaskgroupDirective( |
| 2895 | const OMPTaskgroupDirective *D) { |
| 2896 | VisitOMPExecutableDirective(D); |
Alexey Bataev | 3b1b895 | 2017-07-25 15:53:26 +0000 | [diff] [blame] | 2897 | if (const Expr *E = D->getReductionRef()) |
| 2898 | VisitStmt(E); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2901 | void EnqueueVisitor::VisitOMPFlushDirective(const OMPFlushDirective *D) { |
| 2902 | VisitOMPExecutableDirective(D); |
| 2903 | } |
| 2904 | |
Alexey Bataev | c112e94 | 2020-02-28 09:52:15 -0500 | [diff] [blame] | 2905 | void EnqueueVisitor::VisitOMPDepobjDirective(const OMPDepobjDirective *D) { |
| 2906 | VisitOMPExecutableDirective(D); |
| 2907 | } |
| 2908 | |
Alexey Bataev | fcba7c3 | 2020-03-20 07:03:01 -0400 | [diff] [blame] | 2909 | void EnqueueVisitor::VisitOMPScanDirective(const OMPScanDirective *D) { |
| 2910 | VisitOMPExecutableDirective(D); |
| 2911 | } |
| 2912 | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2913 | void EnqueueVisitor::VisitOMPOrderedDirective(const OMPOrderedDirective *D) { |
| 2914 | VisitOMPExecutableDirective(D); |
| 2915 | } |
| 2916 | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2917 | void EnqueueVisitor::VisitOMPAtomicDirective(const OMPAtomicDirective *D) { |
| 2918 | VisitOMPExecutableDirective(D); |
| 2919 | } |
| 2920 | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 2921 | void EnqueueVisitor::VisitOMPTargetDirective(const OMPTargetDirective *D) { |
| 2922 | VisitOMPExecutableDirective(D); |
| 2923 | } |
| 2924 | |
Alexey Bataev | c112e94 | 2020-02-28 09:52:15 -0500 | [diff] [blame] | 2925 | void EnqueueVisitor::VisitOMPTargetDataDirective( |
| 2926 | const OMPTargetDataDirective *D) { |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2927 | VisitOMPExecutableDirective(D); |
| 2928 | } |
| 2929 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 2930 | void EnqueueVisitor::VisitOMPTargetEnterDataDirective( |
| 2931 | const OMPTargetEnterDataDirective *D) { |
| 2932 | VisitOMPExecutableDirective(D); |
| 2933 | } |
| 2934 | |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 2935 | void EnqueueVisitor::VisitOMPTargetExitDataDirective( |
| 2936 | const OMPTargetExitDataDirective *D) { |
| 2937 | VisitOMPExecutableDirective(D); |
| 2938 | } |
| 2939 | |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 2940 | void EnqueueVisitor::VisitOMPTargetParallelDirective( |
| 2941 | const OMPTargetParallelDirective *D) { |
| 2942 | VisitOMPExecutableDirective(D); |
| 2943 | } |
| 2944 | |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 2945 | void EnqueueVisitor::VisitOMPTargetParallelForDirective( |
| 2946 | const OMPTargetParallelForDirective *D) { |
| 2947 | VisitOMPLoopDirective(D); |
| 2948 | } |
| 2949 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2950 | void EnqueueVisitor::VisitOMPTeamsDirective(const OMPTeamsDirective *D) { |
| 2951 | VisitOMPExecutableDirective(D); |
| 2952 | } |
| 2953 | |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2954 | void EnqueueVisitor::VisitOMPCancellationPointDirective( |
| 2955 | const OMPCancellationPointDirective *D) { |
| 2956 | VisitOMPExecutableDirective(D); |
| 2957 | } |
| 2958 | |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2959 | void EnqueueVisitor::VisitOMPCancelDirective(const OMPCancelDirective *D) { |
| 2960 | VisitOMPExecutableDirective(D); |
| 2961 | } |
| 2962 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 2963 | void EnqueueVisitor::VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D) { |
| 2964 | VisitOMPLoopDirective(D); |
| 2965 | } |
| 2966 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 2967 | void EnqueueVisitor::VisitOMPTaskLoopSimdDirective( |
| 2968 | const OMPTaskLoopSimdDirective *D) { |
| 2969 | VisitOMPLoopDirective(D); |
| 2970 | } |
| 2971 | |
Alexey Bataev | 60e51c4 | 2019-10-10 20:13:02 +0000 | [diff] [blame] | 2972 | void EnqueueVisitor::VisitOMPMasterTaskLoopDirective( |
| 2973 | const OMPMasterTaskLoopDirective *D) { |
| 2974 | VisitOMPLoopDirective(D); |
| 2975 | } |
| 2976 | |
Alexey Bataev | b8552ab | 2019-10-18 16:47:35 +0000 | [diff] [blame] | 2977 | void EnqueueVisitor::VisitOMPMasterTaskLoopSimdDirective( |
| 2978 | const OMPMasterTaskLoopSimdDirective *D) { |
| 2979 | VisitOMPLoopDirective(D); |
| 2980 | } |
| 2981 | |
Alexey Bataev | 5bbcead | 2019-10-14 17:17:41 +0000 | [diff] [blame] | 2982 | void EnqueueVisitor::VisitOMPParallelMasterTaskLoopDirective( |
| 2983 | const OMPParallelMasterTaskLoopDirective *D) { |
| 2984 | VisitOMPLoopDirective(D); |
| 2985 | } |
| 2986 | |
Alexey Bataev | 14a388f | 2019-10-25 10:27:13 -0400 | [diff] [blame] | 2987 | void EnqueueVisitor::VisitOMPParallelMasterTaskLoopSimdDirective( |
| 2988 | const OMPParallelMasterTaskLoopSimdDirective *D) { |
| 2989 | VisitOMPLoopDirective(D); |
| 2990 | } |
| 2991 | |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2992 | void EnqueueVisitor::VisitOMPDistributeDirective( |
| 2993 | const OMPDistributeDirective *D) { |
| 2994 | VisitOMPLoopDirective(D); |
| 2995 | } |
| 2996 | |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2997 | void EnqueueVisitor::VisitOMPDistributeParallelForDirective( |
| 2998 | const OMPDistributeParallelForDirective *D) { |
| 2999 | VisitOMPLoopDirective(D); |
| 3000 | } |
| 3001 | |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 3002 | void EnqueueVisitor::VisitOMPDistributeParallelForSimdDirective( |
| 3003 | const OMPDistributeParallelForSimdDirective *D) { |
| 3004 | VisitOMPLoopDirective(D); |
| 3005 | } |
| 3006 | |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 3007 | void EnqueueVisitor::VisitOMPDistributeSimdDirective( |
| 3008 | const OMPDistributeSimdDirective *D) { |
| 3009 | VisitOMPLoopDirective(D); |
| 3010 | } |
| 3011 | |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 3012 | void EnqueueVisitor::VisitOMPTargetParallelForSimdDirective( |
| 3013 | const OMPTargetParallelForSimdDirective *D) { |
| 3014 | VisitOMPLoopDirective(D); |
| 3015 | } |
| 3016 | |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 3017 | void EnqueueVisitor::VisitOMPTargetSimdDirective( |
| 3018 | const OMPTargetSimdDirective *D) { |
| 3019 | VisitOMPLoopDirective(D); |
| 3020 | } |
| 3021 | |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 3022 | void EnqueueVisitor::VisitOMPTeamsDistributeDirective( |
| 3023 | const OMPTeamsDistributeDirective *D) { |
| 3024 | VisitOMPLoopDirective(D); |
| 3025 | } |
| 3026 | |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 3027 | void EnqueueVisitor::VisitOMPTeamsDistributeSimdDirective( |
| 3028 | const OMPTeamsDistributeSimdDirective *D) { |
| 3029 | VisitOMPLoopDirective(D); |
| 3030 | } |
| 3031 | |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 3032 | void EnqueueVisitor::VisitOMPTeamsDistributeParallelForSimdDirective( |
| 3033 | const OMPTeamsDistributeParallelForSimdDirective *D) { |
| 3034 | VisitOMPLoopDirective(D); |
| 3035 | } |
| 3036 | |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 3037 | void EnqueueVisitor::VisitOMPTeamsDistributeParallelForDirective( |
| 3038 | const OMPTeamsDistributeParallelForDirective *D) { |
| 3039 | VisitOMPLoopDirective(D); |
| 3040 | } |
| 3041 | |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 3042 | void EnqueueVisitor::VisitOMPTargetTeamsDirective( |
| 3043 | const OMPTargetTeamsDirective *D) { |
| 3044 | VisitOMPExecutableDirective(D); |
| 3045 | } |
| 3046 | |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 3047 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeDirective( |
| 3048 | const OMPTargetTeamsDistributeDirective *D) { |
| 3049 | VisitOMPLoopDirective(D); |
| 3050 | } |
| 3051 | |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 3052 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeParallelForDirective( |
| 3053 | const OMPTargetTeamsDistributeParallelForDirective *D) { |
| 3054 | VisitOMPLoopDirective(D); |
| 3055 | } |
| 3056 | |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 3057 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeParallelForSimdDirective( |
| 3058 | const OMPTargetTeamsDistributeParallelForSimdDirective *D) { |
| 3059 | VisitOMPLoopDirective(D); |
| 3060 | } |
| 3061 | |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 3062 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeSimdDirective( |
| 3063 | const OMPTargetTeamsDistributeSimdDirective *D) { |
| 3064 | VisitOMPLoopDirective(D); |
| 3065 | } |
| 3066 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3067 | void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, const Stmt *S) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3068 | EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU, RegionOfInterest)) |
| 3069 | .Visit(S); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3070 | } |
| 3071 | |
| 3072 | bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { |
| 3073 | if (RegionOfInterest.isValid()) { |
| 3074 | SourceRange Range = getRawCursorExtent(C); |
| 3075 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 3076 | return false; |
| 3077 | } |
| 3078 | return true; |
| 3079 | } |
| 3080 | |
| 3081 | bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { |
| 3082 | while (!WL.empty()) { |
| 3083 | // Dequeue the worklist item. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 3084 | VisitorJob LI = WL.pop_back_val(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3085 | |
| 3086 | // Set the Parent field, then back to its old value once we're done. |
| 3087 | SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3088 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3089 | switch (LI.getKind()) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3090 | case VisitorJob::DeclVisitKind: { |
| 3091 | const Decl *D = cast<DeclVisit>(&LI)->get(); |
| 3092 | if (!D) |
| 3093 | continue; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3094 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3095 | // For now, perform default visitation for Decls. |
| 3096 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest, |
| 3097 | cast<DeclVisit>(&LI)->isFirst()))) |
| 3098 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3099 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3100 | continue; |
| 3101 | } |
| 3102 | case VisitorJob::ExplicitTemplateArgsVisitKind: { |
| 3103 | for (const TemplateArgumentLoc &Arg : |
| 3104 | *cast<ExplicitTemplateArgsVisit>(&LI)) { |
| 3105 | if (VisitTemplateArgumentLoc(Arg)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3106 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3107 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3108 | continue; |
| 3109 | } |
| 3110 | case VisitorJob::TypeLocVisitKind: { |
| 3111 | // Perform default visitation for TypeLocs. |
| 3112 | if (Visit(cast<TypeLocVisit>(&LI)->get())) |
| 3113 | return true; |
| 3114 | continue; |
| 3115 | } |
| 3116 | case VisitorJob::LabelRefVisitKind: { |
| 3117 | const LabelDecl *LS = cast<LabelRefVisit>(&LI)->get(); |
| 3118 | if (LabelStmt *stmt = LS->getStmt()) { |
| 3119 | if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(), |
| 3120 | TU))) { |
| 3121 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3122 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3123 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3124 | continue; |
| 3125 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3126 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3127 | case VisitorJob::NestedNameSpecifierLocVisitKind: { |
| 3128 | NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI); |
| 3129 | if (VisitNestedNameSpecifierLoc(V->get())) |
| 3130 | return true; |
| 3131 | continue; |
| 3132 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3133 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3134 | case VisitorJob::DeclarationNameInfoVisitKind: { |
| 3135 | if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI)->get())) |
| 3136 | return true; |
| 3137 | continue; |
| 3138 | } |
| 3139 | case VisitorJob::MemberRefVisitKind: { |
| 3140 | MemberRefVisit *V = cast<MemberRefVisit>(&LI); |
| 3141 | if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU))) |
| 3142 | return true; |
| 3143 | continue; |
| 3144 | } |
| 3145 | case VisitorJob::StmtVisitKind: { |
| 3146 | const Stmt *S = cast<StmtVisit>(&LI)->get(); |
| 3147 | if (!S) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3148 | continue; |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 3149 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3150 | // Update the current cursor. |
| 3151 | CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest); |
| 3152 | if (!IsInRegionOfInterest(Cursor)) |
| 3153 | continue; |
| 3154 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 3155 | case CXChildVisit_Break: |
| 3156 | return true; |
| 3157 | case CXChildVisit_Continue: |
| 3158 | break; |
| 3159 | case CXChildVisit_Recurse: |
| 3160 | if (PostChildrenVisitor) |
| 3161 | WL.push_back(PostChildrenVisit(nullptr, Cursor)); |
| 3162 | EnqueueWorkList(WL, S); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3163 | break; |
| 3164 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3165 | continue; |
| 3166 | } |
| 3167 | case VisitorJob::MemberExprPartsKind: { |
| 3168 | // Handle the other pieces in the MemberExpr besides the base. |
| 3169 | const MemberExpr *M = cast<MemberExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3170 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3171 | // Visit the nested-name-specifier |
| 3172 | if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc()) |
| 3173 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3174 | return true; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3175 | |
| 3176 | // Visit the declaration name. |
| 3177 | if (VisitDeclarationNameInfo(M->getMemberNameInfo())) |
| 3178 | return true; |
| 3179 | |
| 3180 | // Visit the explicitly-specified template arguments, if any. |
| 3181 | if (M->hasExplicitTemplateArgs()) { |
| 3182 | for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), |
| 3183 | *ArgEnd = Arg + M->getNumTemplateArgs(); |
| 3184 | Arg != ArgEnd; ++Arg) { |
| 3185 | if (VisitTemplateArgumentLoc(*Arg)) |
| 3186 | return true; |
| 3187 | } |
| 3188 | } |
| 3189 | continue; |
| 3190 | } |
| 3191 | case VisitorJob::DeclRefExprPartsKind: { |
| 3192 | const DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get(); |
| 3193 | // Visit nested-name-specifier, if present. |
| 3194 | if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc()) |
| 3195 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 3196 | return true; |
| 3197 | // Visit declaration name. |
| 3198 | if (VisitDeclarationNameInfo(DR->getNameInfo())) |
| 3199 | return true; |
| 3200 | continue; |
| 3201 | } |
| 3202 | case VisitorJob::OverloadExprPartsKind: { |
| 3203 | const OverloadExpr *O = cast<OverloadExprParts>(&LI)->get(); |
| 3204 | // Visit the nested-name-specifier. |
| 3205 | if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc()) |
| 3206 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 3207 | return true; |
| 3208 | // Visit the declaration name. |
| 3209 | if (VisitDeclarationNameInfo(O->getNameInfo())) |
| 3210 | return true; |
| 3211 | // Visit the overloaded declaration reference. |
| 3212 | if (Visit(MakeCursorOverloadedDeclRef(O, TU))) |
| 3213 | return true; |
| 3214 | continue; |
| 3215 | } |
| 3216 | case VisitorJob::SizeOfPackExprPartsKind: { |
| 3217 | const SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get(); |
| 3218 | NamedDecl *Pack = E->getPack(); |
| 3219 | if (isa<TemplateTypeParmDecl>(Pack)) { |
| 3220 | if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack), |
| 3221 | E->getPackLoc(), TU))) |
| 3222 | return true; |
| 3223 | |
| 3224 | continue; |
| 3225 | } |
| 3226 | |
| 3227 | if (isa<TemplateTemplateParmDecl>(Pack)) { |
| 3228 | if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack), |
| 3229 | E->getPackLoc(), TU))) |
| 3230 | return true; |
| 3231 | |
| 3232 | continue; |
| 3233 | } |
| 3234 | |
| 3235 | // Non-type template parameter packs and function parameter packs are |
| 3236 | // treated like DeclRefExpr cursors. |
| 3237 | continue; |
| 3238 | } |
| 3239 | |
| 3240 | case VisitorJob::LambdaExprPartsKind: { |
| 3241 | // Visit non-init captures. |
| 3242 | const LambdaExpr *E = cast<LambdaExprParts>(&LI)->get(); |
| 3243 | for (LambdaExpr::capture_iterator C = E->explicit_capture_begin(), |
| 3244 | CEnd = E->explicit_capture_end(); |
| 3245 | C != CEnd; ++C) { |
| 3246 | if (!C->capturesVariable()) |
| 3247 | continue; |
| 3248 | |
| 3249 | if (Visit(MakeCursorVariableRef(C->getCapturedVar(), C->getLocation(), |
| 3250 | TU))) |
| 3251 | return true; |
| 3252 | } |
| 3253 | // Visit init captures |
| 3254 | for (auto InitExpr : E->capture_inits()) { |
| 3255 | if (Visit(InitExpr)) |
| 3256 | return true; |
| 3257 | } |
| 3258 | |
| 3259 | TypeLoc TL = E->getCallOperator()->getTypeSourceInfo()->getTypeLoc(); |
| 3260 | // Visit parameters and return type, if present. |
| 3261 | if (FunctionTypeLoc Proto = TL.getAs<FunctionProtoTypeLoc>()) { |
| 3262 | if (E->hasExplicitParameters()) { |
| 3263 | // Visit parameters. |
| 3264 | for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) |
| 3265 | if (Visit(MakeCXCursor(Proto.getParam(I), TU))) |
| 3266 | return true; |
| 3267 | } |
| 3268 | if (E->hasExplicitResultType()) { |
| 3269 | // Visit result type. |
| 3270 | if (Visit(Proto.getReturnLoc())) |
| 3271 | return true; |
| 3272 | } |
| 3273 | } |
| 3274 | break; |
| 3275 | } |
| 3276 | |
| 3277 | case VisitorJob::PostChildrenVisitKind: |
| 3278 | if (PostChildrenVisitor(Parent, ClientData)) |
| 3279 | return true; |
| 3280 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3281 | } |
| 3282 | } |
| 3283 | return false; |
| 3284 | } |
| 3285 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3286 | bool CursorVisitor::Visit(const Stmt *S) { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3287 | VisitorWorkList *WL = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3288 | if (!WorkListFreeList.empty()) { |
| 3289 | WL = WorkListFreeList.back(); |
| 3290 | WL->clear(); |
| 3291 | WorkListFreeList.pop_back(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3292 | } else { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3293 | WL = new VisitorWorkList(); |
| 3294 | WorkListCache.push_back(WL); |
| 3295 | } |
| 3296 | EnqueueWorkList(*WL, S); |
| 3297 | bool result = RunVisitorWorkList(*WL); |
| 3298 | WorkListFreeList.push_back(WL); |
| 3299 | return result; |
| 3300 | } |
| 3301 | |
| 3302 | namespace { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3303 | typedef SmallVector<SourceRange, 4> RefNamePieces; |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3304 | RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr, |
| 3305 | const DeclarationNameInfo &NI, SourceRange QLoc, |
| 3306 | const SourceRange *TemplateArgsLoc = nullptr) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3307 | const bool WantQualifier = NameFlags & CXNameRange_WantQualifier; |
| 3308 | const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs; |
| 3309 | const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3310 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3311 | const DeclarationName::NameKind Kind = NI.getName().getNameKind(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3312 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3313 | RefNamePieces Pieces; |
| 3314 | |
| 3315 | if (WantQualifier && QLoc.isValid()) |
| 3316 | Pieces.push_back(QLoc); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3317 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3318 | if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr) |
| 3319 | Pieces.push_back(NI.getLoc()); |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3320 | |
| 3321 | if (WantTemplateArgs && TemplateArgsLoc && TemplateArgsLoc->isValid()) |
| 3322 | Pieces.push_back(*TemplateArgsLoc); |
| 3323 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3324 | if (Kind == DeclarationName::CXXOperatorName) { |
| 3325 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3326 | NI.getInfo().CXXOperatorName.BeginOpNameLoc)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3327 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3328 | NI.getInfo().CXXOperatorName.EndOpNameLoc)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3329 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3330 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3331 | if (WantSinglePiece) { |
| 3332 | SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd()); |
| 3333 | Pieces.clear(); |
| 3334 | Pieces.push_back(R); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3335 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3336 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3337 | return Pieces; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3338 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3339 | } // namespace |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3340 | |
| 3341 | //===----------------------------------------------------------------------===// |
| 3342 | // Misc. API hooks. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3343 | //===----------------------------------------------------------------------===// |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3344 | |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 3345 | namespace { |
| 3346 | struct RegisterFatalErrorHandler { |
| 3347 | RegisterFatalErrorHandler() { |
Jan Korous | f7d2376 | 2019-09-12 22:55:55 +0000 | [diff] [blame] | 3348 | clang_install_aborting_llvm_fatal_error_handler(); |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 3349 | } |
| 3350 | }; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3351 | } // namespace |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 3352 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3353 | static llvm::ManagedStatic<RegisterFatalErrorHandler> |
| 3354 | RegisterFatalErrorHandlerOnce; |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 3355 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3356 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 3357 | int displayDiagnostics) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3358 | // We use crash recovery to make some of our APIs more reliable, implicitly |
| 3359 | // enable it. |
Argyrios Kyrtzidis | 3701f54 | 2013-11-27 08:58:09 +0000 | [diff] [blame] | 3360 | if (!getenv("LIBCLANG_DISABLE_CRASH_RECOVERY")) |
| 3361 | llvm::CrashRecoveryContext::Enable(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3362 | |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 3363 | // Look through the managed static to trigger construction of the managed |
| 3364 | // static which registers our fatal error handler. This ensures it is only |
| 3365 | // registered once. |
| 3366 | (void)*RegisterFatalErrorHandlerOnce; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3367 | |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 3368 | // Initialize targets for clang module support. |
| 3369 | llvm::InitializeAllTargets(); |
| 3370 | llvm::InitializeAllTargetMCs(); |
| 3371 | llvm::InitializeAllAsmPrinters(); |
| 3372 | llvm::InitializeAllAsmParsers(); |
| 3373 | |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 3374 | CIndexer *CIdxr = new CIndexer(); |
| 3375 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3376 | if (excludeDeclarationsFromPCH) |
| 3377 | CIdxr->setOnlyLocalDecls(); |
| 3378 | if (displayDiagnostics) |
| 3379 | CIdxr->setDisplayDiagnostics(); |
| 3380 | |
| 3381 | if (getenv("LIBCLANG_BGPRIO_INDEX")) |
| 3382 | CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() | |
| 3383 | CXGlobalOpt_ThreadBackgroundPriorityForIndexing); |
| 3384 | if (getenv("LIBCLANG_BGPRIO_EDIT")) |
| 3385 | CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() | |
| 3386 | CXGlobalOpt_ThreadBackgroundPriorityForEditing); |
| 3387 | |
| 3388 | return CIdxr; |
| 3389 | } |
| 3390 | |
| 3391 | void clang_disposeIndex(CXIndex CIdx) { |
| 3392 | if (CIdx) |
| 3393 | delete static_cast<CIndexer *>(CIdx); |
| 3394 | } |
| 3395 | |
| 3396 | void clang_CXIndex_setGlobalOptions(CXIndex CIdx, unsigned options) { |
| 3397 | if (CIdx) |
| 3398 | static_cast<CIndexer *>(CIdx)->setCXGlobalOptFlags(options); |
| 3399 | } |
| 3400 | |
| 3401 | unsigned clang_CXIndex_getGlobalOptions(CXIndex CIdx) { |
| 3402 | if (CIdx) |
| 3403 | return static_cast<CIndexer *>(CIdx)->getCXGlobalOptFlags(); |
| 3404 | return 0; |
| 3405 | } |
| 3406 | |
Alex Lorenz | 0861579 | 2017-12-04 21:56:36 +0000 | [diff] [blame] | 3407 | void clang_CXIndex_setInvocationEmissionPathOption(CXIndex CIdx, |
| 3408 | const char *Path) { |
| 3409 | if (CIdx) |
| 3410 | static_cast<CIndexer *>(CIdx)->setInvocationEmissionPath(Path ? Path : ""); |
| 3411 | } |
| 3412 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3413 | void clang_toggleCrashRecovery(unsigned isEnabled) { |
| 3414 | if (isEnabled) |
| 3415 | llvm::CrashRecoveryContext::Enable(); |
| 3416 | else |
| 3417 | llvm::CrashRecoveryContext::Disable(); |
| 3418 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3419 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3420 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
| 3421 | const char *ast_filename) { |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3422 | CXTranslationUnit TU; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3423 | enum CXErrorCode Result = |
| 3424 | clang_createTranslationUnit2(CIdx, ast_filename, &TU); |
Reid Kleckner | fd48fc6 | 2014-02-12 23:56:20 +0000 | [diff] [blame] | 3425 | (void)Result; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3426 | assert((TU && Result == CXError_Success) || |
| 3427 | (!TU && Result != CXError_Success)); |
| 3428 | return TU; |
| 3429 | } |
| 3430 | |
| 3431 | enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx, |
| 3432 | const char *ast_filename, |
| 3433 | CXTranslationUnit *out_TU) { |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3434 | if (out_TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3435 | *out_TU = nullptr; |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3436 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3437 | if (!CIdx || !ast_filename || !out_TU) |
| 3438 | return CXError_InvalidArguments; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3439 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3440 | LOG_FUNC_SECTION { *Log << ast_filename; } |
Argyrios Kyrtzidis | 2702101 | 2013-05-24 22:24:07 +0000 | [diff] [blame] | 3441 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3442 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 3443 | FileSystemOptions FileSystemOpts; |
| 3444 | |
Justin Bogner | d512c1e | 2014-10-15 00:33:06 +0000 | [diff] [blame] | 3445 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags = |
| 3446 | CompilerInstance::createDiagnostics(new DiagnosticOptions()); |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 3447 | std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile( |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 3448 | ast_filename, CXXIdx->getPCHContainerOperations()->getRawReader(), |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3449 | ASTUnit::LoadEverything, Diags, FileSystemOpts, /*UseDebugInfo=*/false, |
| 3450 | CXXIdx->getOnlyLocalDecls(), None, CaptureDiagsKind::All, |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 3451 | /*AllowPCHWithCompilerErrors=*/true, |
| 3452 | /*UserFilesAreVolatile=*/true); |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 3453 | *out_TU = MakeCXTranslationUnit(CXXIdx, std::move(AU)); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3454 | return *out_TU ? CXError_Success : CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3455 | } |
| 3456 | |
| 3457 | unsigned clang_defaultEditingTranslationUnitOptions() { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3458 | return CXTranslationUnit_PrecompiledPreamble | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3459 | CXTranslationUnit_CacheCompletionResults; |
| 3460 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3461 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3462 | CXTranslationUnit clang_createTranslationUnitFromSourceFile( |
| 3463 | CXIndex CIdx, const char *source_filename, int num_command_line_args, |
| 3464 | const char *const *command_line_args, unsigned num_unsaved_files, |
| 3465 | struct CXUnsavedFile *unsaved_files) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3466 | unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3467 | return clang_parseTranslationUnit(CIdx, source_filename, command_line_args, |
| 3468 | num_command_line_args, unsaved_files, |
| 3469 | num_unsaved_files, Options); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3470 | } |
| 3471 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3472 | static CXErrorCode |
| 3473 | clang_parseTranslationUnit_Impl(CXIndex CIdx, const char *source_filename, |
| 3474 | const char *const *command_line_args, |
| 3475 | int num_command_line_args, |
| 3476 | ArrayRef<CXUnsavedFile> unsaved_files, |
| 3477 | unsigned options, CXTranslationUnit *out_TU) { |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3478 | // Set up the initial return values. |
| 3479 | if (out_TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3480 | *out_TU = nullptr; |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3481 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3482 | // Check arguments. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3483 | if (!CIdx || !out_TU) |
| 3484 | return CXError_InvalidArguments; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3485 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3486 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 3487 | |
| 3488 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
| 3489 | setThreadBackgroundPriority(); |
| 3490 | |
| 3491 | bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3492 | bool CreatePreambleOnFirstParse = |
| 3493 | options & CXTranslationUnit_CreatePreambleOnFirstParse; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3494 | // FIXME: Add a flag for modules. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3495 | TranslationUnitKind TUKind = (options & (CXTranslationUnit_Incomplete | |
| 3496 | CXTranslationUnit_SingleFileParse)) |
| 3497 | ? TU_Prefix |
| 3498 | : TU_Complete; |
| 3499 | bool CacheCodeCompletionResults = |
| 3500 | options & CXTranslationUnit_CacheCompletionResults; |
| 3501 | bool IncludeBriefCommentsInCodeCompletion = |
| 3502 | options & CXTranslationUnit_IncludeBriefCommentsInCodeCompletion; |
Ivan Donchevskii | f70d28b | 2018-05-17 09:15:22 +0000 | [diff] [blame] | 3503 | bool SingleFileParse = options & CXTranslationUnit_SingleFileParse; |
| 3504 | bool ForSerialization = options & CXTranslationUnit_ForSerialization; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3505 | bool RetainExcludedCB = |
| 3506 | options & CXTranslationUnit_RetainExcludedConditionalBlocks; |
Ivan Donchevskii | 6e89528 | 2018-05-17 09:24:37 +0000 | [diff] [blame] | 3507 | SkipFunctionBodiesScope SkipFunctionBodies = SkipFunctionBodiesScope::None; |
| 3508 | if (options & CXTranslationUnit_SkipFunctionBodies) { |
| 3509 | SkipFunctionBodies = |
| 3510 | (options & CXTranslationUnit_LimitSkipFunctionBodiesToPreamble) |
| 3511 | ? SkipFunctionBodiesScope::Preamble |
| 3512 | : SkipFunctionBodiesScope::PreambleAndMainFile; |
| 3513 | } |
Ivan Donchevskii | f70d28b | 2018-05-17 09:15:22 +0000 | [diff] [blame] | 3514 | |
| 3515 | // Configure the diagnostics. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3516 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
| 3517 | CompilerInstance::createDiagnostics(new DiagnosticOptions)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3518 | |
Manuel Klimek | 016c024 | 2016-03-01 10:56:19 +0000 | [diff] [blame] | 3519 | if (options & CXTranslationUnit_KeepGoing) |
Ivan Donchevskii | 878271b | 2019-03-07 10:13:50 +0000 | [diff] [blame] | 3520 | Diags->setFatalsAsError(true); |
Manuel Klimek | 016c024 | 2016-03-01 10:56:19 +0000 | [diff] [blame] | 3521 | |
Nikolai Kosjar | 8edd8da | 2019-06-11 14:14:24 +0000 | [diff] [blame] | 3522 | CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::All; |
| 3523 | if (options & CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles) |
| 3524 | CaptureDiagnostics = CaptureDiagsKind::AllWithoutNonErrorsFromIncludes; |
| 3525 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3526 | // Recover resources if we crash before exiting this function. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3527 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 3528 | DiagnosticsEngine, |
| 3529 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine>> |
| 3530 | DiagCleanup(Diags.get()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3531 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3532 | std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles( |
| 3533 | new std::vector<ASTUnit::RemappedFile>()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3534 | |
| 3535 | // Recover resources if we crash before exiting this function. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3536 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<ASTUnit::RemappedFile>> |
| 3537 | RemappedCleanup(RemappedFiles.get()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3538 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3539 | for (auto &UF : unsaved_files) { |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3540 | std::unique_ptr<llvm::MemoryBuffer> MB = |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3541 | llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3542 | RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3543 | } |
| 3544 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3545 | std::unique_ptr<std::vector<const char *>> Args( |
| 3546 | new std::vector<const char *>()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3547 | |
| 3548 | // Recover resources if we crash before exiting this method. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3549 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char *>> |
| 3550 | ArgsCleanup(Args.get()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3551 | |
| 3552 | // Since the Clang C library is primarily used by batch tools dealing with |
| 3553 | // (often very broken) source code, where spell-checking can have a |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3554 | // significant negative impact on performance (particularly when |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3555 | // precompiled headers are involved), we disable it by default. |
| 3556 | // Only do this if we haven't found a spell-checking-related argument. |
| 3557 | bool FoundSpellCheckingArgument = false; |
| 3558 | for (int I = 0; I != num_command_line_args; ++I) { |
| 3559 | if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || |
| 3560 | strcmp(command_line_args[I], "-fspell-checking") == 0) { |
| 3561 | FoundSpellCheckingArgument = true; |
| 3562 | break; |
| 3563 | } |
| 3564 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3565 | Args->insert(Args->end(), command_line_args, |
| 3566 | command_line_args + num_command_line_args); |
| 3567 | |
Benjamin Kramer | c02670e | 2015-11-18 16:14:27 +0000 | [diff] [blame] | 3568 | if (!FoundSpellCheckingArgument) |
| 3569 | Args->insert(Args->begin() + 1, "-fno-spell-checking"); |
| 3570 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3571 | // The 'source_filename' argument is optional. If the caller does not |
| 3572 | // specify it then it is assumed that the source file is specified |
| 3573 | // in the actual argument list. |
| 3574 | // Put the source file after command_line_args otherwise if '-x' flag is |
| 3575 | // present it will be unused. |
| 3576 | if (source_filename) |
| 3577 | Args->push_back(source_filename); |
| 3578 | |
| 3579 | // Do we need the detailed preprocessing record? |
| 3580 | if (options & CXTranslationUnit_DetailedPreprocessingRecord) { |
| 3581 | Args->push_back("-Xclang"); |
| 3582 | Args->push_back("-detailed-preprocessing-record"); |
| 3583 | } |
Alex Lorenz | cb00640 | 2017-04-27 13:47:03 +0000 | [diff] [blame] | 3584 | |
| 3585 | // Suppress any editor placeholder diagnostics. |
| 3586 | Args->push_back("-fallow-editor-placeholders"); |
| 3587 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3588 | unsigned NumErrors = Diags->getClient()->getNumErrors(); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3589 | std::unique_ptr<ASTUnit> ErrUnit; |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3590 | // Unless the user specified that they want the preamble on the first parse |
| 3591 | // set it up to be created on the first reparse. This makes the first parse |
| 3592 | // faster, trading for a slower (first) reparse. |
| 3593 | unsigned PrecompilePreambleAfterNParses = |
| 3594 | !PrecompilePreamble ? 0 : 2 - CreatePreambleOnFirstParse; |
Alex Lorenz | 0861579 | 2017-12-04 21:56:36 +0000 | [diff] [blame] | 3595 | |
Alex Lorenz | 0861579 | 2017-12-04 21:56:36 +0000 | [diff] [blame] | 3596 | LibclangInvocationReporter InvocationReporter( |
| 3597 | *CXXIdx, LibclangInvocationReporter::OperationKind::ParseOperation, |
Alex Lorenz | 690f0e2 | 2017-12-07 20:37:50 +0000 | [diff] [blame] | 3598 | options, llvm::makeArrayRef(*Args), /*InvocationArgs=*/None, |
| 3599 | unsaved_files); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3600 | std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCommandLine( |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 3601 | Args->data(), Args->data() + Args->size(), |
| 3602 | CXXIdx->getPCHContainerOperations(), Diags, |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3603 | CXXIdx->getClangResourcesPath(), CXXIdx->getOnlyLocalDecls(), |
Nikolai Kosjar | 8edd8da | 2019-06-11 14:14:24 +0000 | [diff] [blame] | 3604 | CaptureDiagnostics, *RemappedFiles.get(), |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3605 | /*RemappedFilesKeepOriginalName=*/true, PrecompilePreambleAfterNParses, |
| 3606 | TUKind, CacheCodeCompletionResults, IncludeBriefCommentsInCodeCompletion, |
Argyrios Kyrtzidis | 735e92c | 2017-06-09 01:20:48 +0000 | [diff] [blame] | 3607 | /*AllowPCHWithCompilerErrors=*/true, SkipFunctionBodies, SingleFileParse, |
Evgeny Mankov | 2ed2e62 | 2019-08-27 22:15:32 +0000 | [diff] [blame] | 3608 | /*UserFilesAreVolatile=*/true, ForSerialization, RetainExcludedCB, |
Argyrios Kyrtzidis | a3e2ff1 | 2015-11-20 03:36:21 +0000 | [diff] [blame] | 3609 | CXXIdx->getPCHContainerOperations()->getRawReader().getFormat(), |
| 3610 | &ErrUnit)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3611 | |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3612 | // Early failures in LoadFromCommandLine may return with ErrUnit unset. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3613 | if (!Unit && !ErrUnit) |
| 3614 | return CXError_ASTReadError; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3615 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3616 | if (NumErrors != Diags->getClient()->getNumErrors()) { |
| 3617 | // Make sure to check that 'Unit' is non-NULL. |
| 3618 | if (CXXIdx->getDisplayDiagnostics()) |
| 3619 | printDiagsToStderr(Unit ? Unit.get() : ErrUnit.get()); |
| 3620 | } |
| 3621 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3622 | if (isASTReadError(Unit ? Unit.get() : ErrUnit.get())) |
| 3623 | return CXError_ASTReadError; |
| 3624 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 3625 | *out_TU = MakeCXTranslationUnit(CXXIdx, std::move(Unit)); |
Alex Lorenz | 690f0e2 | 2017-12-07 20:37:50 +0000 | [diff] [blame] | 3626 | if (CXTranslationUnitImpl *TU = *out_TU) { |
| 3627 | TU->ParsingOptions = options; |
| 3628 | TU->Arguments.reserve(Args->size()); |
| 3629 | for (const char *Arg : *Args) |
| 3630 | TU->Arguments.push_back(Arg); |
| 3631 | return CXError_Success; |
| 3632 | } |
| 3633 | return CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3634 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3635 | |
| 3636 | CXTranslationUnit |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3637 | clang_parseTranslationUnit(CXIndex CIdx, const char *source_filename, |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3638 | const char *const *command_line_args, |
| 3639 | int num_command_line_args, |
| 3640 | struct CXUnsavedFile *unsaved_files, |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3641 | unsigned num_unsaved_files, unsigned options) { |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3642 | CXTranslationUnit TU; |
| 3643 | enum CXErrorCode Result = clang_parseTranslationUnit2( |
| 3644 | CIdx, source_filename, command_line_args, num_command_line_args, |
| 3645 | unsaved_files, num_unsaved_files, options, &TU); |
Reid Kleckner | 6eaf05a | 2014-02-13 01:19:59 +0000 | [diff] [blame] | 3646 | (void)Result; |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3647 | assert((TU && Result == CXError_Success) || |
| 3648 | (!TU && Result != CXError_Success)); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3649 | return TU; |
| 3650 | } |
| 3651 | |
| 3652 | enum CXErrorCode clang_parseTranslationUnit2( |
Benjamin Kramer | c02670e | 2015-11-18 16:14:27 +0000 | [diff] [blame] | 3653 | CXIndex CIdx, const char *source_filename, |
| 3654 | const char *const *command_line_args, int num_command_line_args, |
| 3655 | struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files, |
| 3656 | unsigned options, CXTranslationUnit *out_TU) { |
Alexandre Ganea | 471d060 | 2019-11-29 10:52:13 -0500 | [diff] [blame] | 3657 | noteBottomOfStack(); |
Benjamin Kramer | c02670e | 2015-11-18 16:14:27 +0000 | [diff] [blame] | 3658 | SmallVector<const char *, 4> Args; |
| 3659 | Args.push_back("clang"); |
| 3660 | Args.append(command_line_args, command_line_args + num_command_line_args); |
| 3661 | return clang_parseTranslationUnit2FullArgv( |
| 3662 | CIdx, source_filename, Args.data(), Args.size(), unsaved_files, |
| 3663 | num_unsaved_files, options, out_TU); |
| 3664 | } |
| 3665 | |
| 3666 | enum CXErrorCode clang_parseTranslationUnit2FullArgv( |
| 3667 | CXIndex CIdx, const char *source_filename, |
| 3668 | const char *const *command_line_args, int num_command_line_args, |
| 3669 | struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files, |
| 3670 | unsigned options, CXTranslationUnit *out_TU) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 3671 | LOG_FUNC_SECTION { |
| 3672 | *Log << source_filename << ": "; |
| 3673 | for (int i = 0; i != num_command_line_args; ++i) |
| 3674 | *Log << command_line_args[i] << " "; |
| 3675 | } |
| 3676 | |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3677 | if (num_unsaved_files && !unsaved_files) |
| 3678 | return CXError_InvalidArguments; |
| 3679 | |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 3680 | CXErrorCode result = CXError_Failure; |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3681 | auto ParseTranslationUnitImpl = [=, &result] { |
Alexandre Ganea | 471d060 | 2019-11-29 10:52:13 -0500 | [diff] [blame] | 3682 | noteBottomOfStack(); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3683 | result = clang_parseTranslationUnit_Impl( |
| 3684 | CIdx, source_filename, command_line_args, num_command_line_args, |
| 3685 | llvm::makeArrayRef(unsaved_files, num_unsaved_files), options, out_TU); |
| 3686 | }; |
Erik Verbruggen | 284848d | 2017-08-29 09:08:02 +0000 | [diff] [blame] | 3687 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3688 | llvm::CrashRecoveryContext CRC; |
| 3689 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3690 | if (!RunSafely(CRC, ParseTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3691 | fprintf(stderr, "libclang: crash detected during parsing: {\n"); |
| 3692 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 3693 | fprintf(stderr, " 'command_line_args' : ["); |
| 3694 | for (int i = 0; i != num_command_line_args; ++i) { |
| 3695 | if (i) |
| 3696 | fprintf(stderr, ", "); |
| 3697 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 3698 | } |
| 3699 | fprintf(stderr, "],\n"); |
| 3700 | fprintf(stderr, " 'unsaved_files' : ["); |
| 3701 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 3702 | if (i) |
| 3703 | fprintf(stderr, ", "); |
| 3704 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 3705 | unsaved_files[i].Length); |
| 3706 | } |
| 3707 | fprintf(stderr, "],\n"); |
| 3708 | fprintf(stderr, " 'options' : %d,\n", options); |
| 3709 | fprintf(stderr, "}\n"); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3710 | |
| 3711 | return CXError_Crashed; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3712 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3713 | if (CXTranslationUnit *TU = out_TU) |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3714 | PrintLibclangResourceUsage(*TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3715 | } |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 3716 | |
| 3717 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3718 | } |
| 3719 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3720 | CXString clang_Type_getObjCEncoding(CXType CT) { |
| 3721 | CXTranslationUnit tu = static_cast<CXTranslationUnit>(CT.data[1]); |
| 3722 | ASTContext &Ctx = getASTUnit(tu)->getASTContext(); |
| 3723 | std::string encoding; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3724 | Ctx.getObjCEncodingForType(QualType::getFromOpaquePtr(CT.data[0]), encoding); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3725 | |
| 3726 | return cxstring::createDup(encoding); |
| 3727 | } |
| 3728 | |
| 3729 | static const IdentifierInfo *getMacroIdentifier(CXCursor C) { |
| 3730 | if (C.kind == CXCursor_MacroDefinition) { |
| 3731 | if (const MacroDefinitionRecord *MDR = getCursorMacroDefinition(C)) |
| 3732 | return MDR->getName(); |
| 3733 | } else if (C.kind == CXCursor_MacroExpansion) { |
| 3734 | MacroExpansionCursor ME = getCursorMacroExpansion(C); |
| 3735 | return ME.getName(); |
| 3736 | } |
| 3737 | return nullptr; |
| 3738 | } |
| 3739 | |
| 3740 | unsigned clang_Cursor_isMacroFunctionLike(CXCursor C) { |
| 3741 | const IdentifierInfo *II = getMacroIdentifier(C); |
| 3742 | if (!II) { |
| 3743 | return false; |
| 3744 | } |
| 3745 | ASTUnit *ASTU = getCursorASTUnit(C); |
| 3746 | Preprocessor &PP = ASTU->getPreprocessor(); |
| 3747 | if (const MacroInfo *MI = PP.getMacroInfo(II)) |
| 3748 | return MI->isFunctionLike(); |
| 3749 | return false; |
| 3750 | } |
| 3751 | |
| 3752 | unsigned clang_Cursor_isMacroBuiltin(CXCursor C) { |
| 3753 | const IdentifierInfo *II = getMacroIdentifier(C); |
| 3754 | if (!II) { |
| 3755 | return false; |
| 3756 | } |
| 3757 | ASTUnit *ASTU = getCursorASTUnit(C); |
| 3758 | Preprocessor &PP = ASTU->getPreprocessor(); |
| 3759 | if (const MacroInfo *MI = PP.getMacroInfo(II)) |
| 3760 | return MI->isBuiltinMacro(); |
| 3761 | return false; |
| 3762 | } |
| 3763 | |
| 3764 | unsigned clang_Cursor_isFunctionInlined(CXCursor C) { |
| 3765 | const Decl *D = getCursorDecl(C); |
| 3766 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 3767 | if (!FD) { |
| 3768 | return false; |
| 3769 | } |
| 3770 | return FD->isInlined(); |
| 3771 | } |
| 3772 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3773 | static StringLiteral *getCFSTR_value(CallExpr *callExpr) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3774 | if (callExpr->getNumArgs() != 1) { |
| 3775 | return nullptr; |
| 3776 | } |
| 3777 | |
| 3778 | StringLiteral *S = nullptr; |
| 3779 | auto *arg = callExpr->getArg(0); |
| 3780 | if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) { |
| 3781 | ImplicitCastExpr *I = static_cast<ImplicitCastExpr *>(arg); |
| 3782 | auto *subExpr = I->getSubExprAsWritten(); |
| 3783 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3784 | if (subExpr->getStmtClass() != Stmt::StringLiteralClass) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3785 | return nullptr; |
| 3786 | } |
| 3787 | |
| 3788 | S = static_cast<StringLiteral *>(I->getSubExprAsWritten()); |
| 3789 | } else if (arg->getStmtClass() == Stmt::StringLiteralClass) { |
| 3790 | S = static_cast<StringLiteral *>(callExpr->getArg(0)); |
| 3791 | } else { |
| 3792 | return nullptr; |
| 3793 | } |
| 3794 | return S; |
| 3795 | } |
| 3796 | |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3797 | struct ExprEvalResult { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3798 | CXEvalResultKind EvalType; |
| 3799 | union { |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3800 | unsigned long long unsignedVal; |
| 3801 | long long intVal; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3802 | double floatVal; |
| 3803 | char *stringVal; |
| 3804 | } EvalData; |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3805 | bool IsUnsignedInt; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3806 | ~ExprEvalResult() { |
| 3807 | if (EvalType != CXEval_UnExposed && EvalType != CXEval_Float && |
| 3808 | EvalType != CXEval_Int) { |
Alex Lorenz | a19cb2e | 2019-01-08 23:28:37 +0000 | [diff] [blame] | 3809 | delete[] EvalData.stringVal; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3810 | } |
| 3811 | } |
| 3812 | }; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3813 | |
| 3814 | void clang_EvalResult_dispose(CXEvalResult E) { |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3815 | delete static_cast<ExprEvalResult *>(E); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
| 3818 | CXEvalResultKind clang_EvalResult_getKind(CXEvalResult E) { |
| 3819 | if (!E) { |
| 3820 | return CXEval_UnExposed; |
| 3821 | } |
| 3822 | return ((ExprEvalResult *)E)->EvalType; |
| 3823 | } |
| 3824 | |
| 3825 | int clang_EvalResult_getAsInt(CXEvalResult E) { |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3826 | return clang_EvalResult_getAsLongLong(E); |
| 3827 | } |
| 3828 | |
| 3829 | long long clang_EvalResult_getAsLongLong(CXEvalResult E) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3830 | if (!E) { |
| 3831 | return 0; |
| 3832 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3833 | ExprEvalResult *Result = (ExprEvalResult *)E; |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3834 | if (Result->IsUnsignedInt) |
| 3835 | return Result->EvalData.unsignedVal; |
| 3836 | return Result->EvalData.intVal; |
| 3837 | } |
| 3838 | |
| 3839 | unsigned clang_EvalResult_isUnsignedInt(CXEvalResult E) { |
| 3840 | return ((ExprEvalResult *)E)->IsUnsignedInt; |
| 3841 | } |
| 3842 | |
| 3843 | unsigned long long clang_EvalResult_getAsUnsigned(CXEvalResult E) { |
| 3844 | if (!E) { |
| 3845 | return 0; |
| 3846 | } |
| 3847 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3848 | ExprEvalResult *Result = (ExprEvalResult *)E; |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3849 | if (Result->IsUnsignedInt) |
| 3850 | return Result->EvalData.unsignedVal; |
| 3851 | return Result->EvalData.intVal; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
| 3854 | double clang_EvalResult_getAsDouble(CXEvalResult E) { |
| 3855 | if (!E) { |
| 3856 | return 0; |
| 3857 | } |
| 3858 | return ((ExprEvalResult *)E)->EvalData.floatVal; |
| 3859 | } |
| 3860 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3861 | const char *clang_EvalResult_getAsStr(CXEvalResult E) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3862 | if (!E) { |
| 3863 | return nullptr; |
| 3864 | } |
| 3865 | return ((ExprEvalResult *)E)->EvalData.stringVal; |
| 3866 | } |
| 3867 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3868 | static const ExprEvalResult *evaluateExpr(Expr *expr, CXCursor C) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3869 | Expr::EvalResult ER; |
| 3870 | ASTContext &ctx = getCursorContext(C); |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3871 | if (!expr) |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3872 | return nullptr; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3873 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3874 | expr = expr->IgnoreParens(); |
Emilio Cobos Alvarez | 7437545 | 2019-07-09 14:27:01 +0000 | [diff] [blame] | 3875 | if (expr->isValueDependent()) |
| 3876 | return nullptr; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3877 | if (!expr->EvaluateAsRValue(ER, ctx)) |
| 3878 | return nullptr; |
| 3879 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3880 | QualType rettype; |
| 3881 | CallExpr *callExpr; |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 3882 | auto result = std::make_unique<ExprEvalResult>(); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3883 | result->EvalType = CXEval_UnExposed; |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3884 | result->IsUnsignedInt = false; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3885 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3886 | if (ER.Val.isInt()) { |
| 3887 | result->EvalType = CXEval_Int; |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3888 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 3889 | auto &val = ER.Val.getInt(); |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3890 | if (val.isUnsigned()) { |
| 3891 | result->IsUnsignedInt = true; |
| 3892 | result->EvalData.unsignedVal = val.getZExtValue(); |
| 3893 | } else { |
| 3894 | result->EvalData.intVal = val.getExtValue(); |
| 3895 | } |
| 3896 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3897 | return result.release(); |
| 3898 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3899 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3900 | if (ER.Val.isFloat()) { |
| 3901 | llvm::SmallVector<char, 100> Buffer; |
| 3902 | ER.Val.getFloat().toString(Buffer); |
| 3903 | std::string floatStr(Buffer.data(), Buffer.size()); |
| 3904 | result->EvalType = CXEval_Float; |
| 3905 | bool ignored; |
| 3906 | llvm::APFloat apFloat = ER.Val.getFloat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3907 | apFloat.convert(llvm::APFloat::IEEEdouble(), |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3908 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 3909 | result->EvalData.floatVal = apFloat.convertToDouble(); |
| 3910 | return result.release(); |
| 3911 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3912 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3913 | if (expr->getStmtClass() == Stmt::ImplicitCastExprClass) { |
| 3914 | const ImplicitCastExpr *I = dyn_cast<ImplicitCastExpr>(expr); |
| 3915 | auto *subExpr = I->getSubExprAsWritten(); |
| 3916 | if (subExpr->getStmtClass() == Stmt::StringLiteralClass || |
| 3917 | subExpr->getStmtClass() == Stmt::ObjCStringLiteralClass) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3918 | const StringLiteral *StrE = nullptr; |
| 3919 | const ObjCStringLiteral *ObjCExpr; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3920 | ObjCExpr = dyn_cast<ObjCStringLiteral>(subExpr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3921 | |
| 3922 | if (ObjCExpr) { |
| 3923 | StrE = ObjCExpr->getString(); |
| 3924 | result->EvalType = CXEval_ObjCStrLiteral; |
| 3925 | } else { |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3926 | StrE = cast<StringLiteral>(I->getSubExprAsWritten()); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3927 | result->EvalType = CXEval_StrLiteral; |
| 3928 | } |
| 3929 | |
| 3930 | std::string strRef(StrE->getString().str()); |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3931 | result->EvalData.stringVal = new char[strRef.size() + 1]; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3932 | strncpy((char *)result->EvalData.stringVal, strRef.c_str(), |
| 3933 | strRef.size()); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3934 | result->EvalData.stringVal[strRef.size()] = '\0'; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3935 | return result.release(); |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3936 | } |
| 3937 | } else if (expr->getStmtClass() == Stmt::ObjCStringLiteralClass || |
| 3938 | expr->getStmtClass() == Stmt::StringLiteralClass) { |
| 3939 | const StringLiteral *StrE = nullptr; |
| 3940 | const ObjCStringLiteral *ObjCExpr; |
| 3941 | ObjCExpr = dyn_cast<ObjCStringLiteral>(expr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3942 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3943 | if (ObjCExpr) { |
| 3944 | StrE = ObjCExpr->getString(); |
| 3945 | result->EvalType = CXEval_ObjCStrLiteral; |
| 3946 | } else { |
| 3947 | StrE = cast<StringLiteral>(expr); |
| 3948 | result->EvalType = CXEval_StrLiteral; |
| 3949 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3950 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3951 | std::string strRef(StrE->getString().str()); |
| 3952 | result->EvalData.stringVal = new char[strRef.size() + 1]; |
| 3953 | strncpy((char *)result->EvalData.stringVal, strRef.c_str(), strRef.size()); |
| 3954 | result->EvalData.stringVal[strRef.size()] = '\0'; |
| 3955 | return result.release(); |
| 3956 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3957 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3958 | if (expr->getStmtClass() == Stmt::CStyleCastExprClass) { |
| 3959 | CStyleCastExpr *CC = static_cast<CStyleCastExpr *>(expr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3960 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3961 | rettype = CC->getType(); |
| 3962 | if (rettype.getAsString() == "CFStringRef" && |
| 3963 | CC->getSubExpr()->getStmtClass() == Stmt::CallExprClass) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3964 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3965 | callExpr = static_cast<CallExpr *>(CC->getSubExpr()); |
| 3966 | StringLiteral *S = getCFSTR_value(callExpr); |
| 3967 | if (S) { |
| 3968 | std::string strLiteral(S->getString().str()); |
| 3969 | result->EvalType = CXEval_CFStr; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3970 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3971 | result->EvalData.stringVal = new char[strLiteral.size() + 1]; |
| 3972 | strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(), |
| 3973 | strLiteral.size()); |
| 3974 | result->EvalData.stringVal[strLiteral.size()] = '\0'; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3975 | return result.release(); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3976 | } |
| 3977 | } |
| 3978 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3979 | } else if (expr->getStmtClass() == Stmt::CallExprClass) { |
| 3980 | callExpr = static_cast<CallExpr *>(expr); |
| 3981 | rettype = callExpr->getCallReturnType(ctx); |
| 3982 | |
| 3983 | if (rettype->isVectorType() || callExpr->getNumArgs() > 1) |
| 3984 | return nullptr; |
| 3985 | |
| 3986 | if (rettype->isIntegralType(ctx) || rettype->isRealFloatingType()) { |
| 3987 | if (callExpr->getNumArgs() == 1 && |
| 3988 | !callExpr->getArg(0)->getType()->isIntegralType(ctx)) |
| 3989 | return nullptr; |
| 3990 | } else if (rettype.getAsString() == "CFStringRef") { |
| 3991 | |
| 3992 | StringLiteral *S = getCFSTR_value(callExpr); |
| 3993 | if (S) { |
| 3994 | std::string strLiteral(S->getString().str()); |
| 3995 | result->EvalType = CXEval_CFStr; |
| 3996 | result->EvalData.stringVal = new char[strLiteral.size() + 1]; |
| 3997 | strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(), |
| 3998 | strLiteral.size()); |
| 3999 | result->EvalData.stringVal[strLiteral.size()] = '\0'; |
| 4000 | return result.release(); |
| 4001 | } |
| 4002 | } |
| 4003 | } else if (expr->getStmtClass() == Stmt::DeclRefExprClass) { |
| 4004 | DeclRefExpr *D = static_cast<DeclRefExpr *>(expr); |
| 4005 | ValueDecl *V = D->getDecl(); |
| 4006 | if (V->getKind() == Decl::Function) { |
| 4007 | std::string strName = V->getNameAsString(); |
| 4008 | result->EvalType = CXEval_Other; |
| 4009 | result->EvalData.stringVal = new char[strName.size() + 1]; |
| 4010 | strncpy(result->EvalData.stringVal, strName.c_str(), strName.size()); |
| 4011 | result->EvalData.stringVal[strName.size()] = '\0'; |
| 4012 | return result.release(); |
| 4013 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 4014 | } |
| 4015 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 4016 | return nullptr; |
| 4017 | } |
| 4018 | |
Alex Lorenz | 65317e1 | 2019-01-08 22:32:51 +0000 | [diff] [blame] | 4019 | static const Expr *evaluateDeclExpr(const Decl *D) { |
| 4020 | if (!D) |
Evgeniy Stepanov | 9b87149 | 2018-07-10 19:48:53 +0000 | [diff] [blame] | 4021 | return nullptr; |
Alex Lorenz | 65317e1 | 2019-01-08 22:32:51 +0000 | [diff] [blame] | 4022 | if (auto *Var = dyn_cast<VarDecl>(D)) |
| 4023 | return Var->getInit(); |
| 4024 | else if (auto *Field = dyn_cast<FieldDecl>(D)) |
| 4025 | return Field->getInClassInitializer(); |
| 4026 | return nullptr; |
| 4027 | } |
Evgeniy Stepanov | 6df47ce | 2018-07-10 19:49:07 +0000 | [diff] [blame] | 4028 | |
Alex Lorenz | 65317e1 | 2019-01-08 22:32:51 +0000 | [diff] [blame] | 4029 | static const Expr *evaluateCompoundStmtExpr(const CompoundStmt *CS) { |
| 4030 | assert(CS && "invalid compound statement"); |
| 4031 | for (auto *bodyIterator : CS->body()) { |
| 4032 | if (const auto *E = dyn_cast<Expr>(bodyIterator)) |
| 4033 | return E; |
Evgeniy Stepanov | 6df47ce | 2018-07-10 19:49:07 +0000 | [diff] [blame] | 4034 | } |
Alex Lorenz | c4cf96e | 2018-07-09 19:56:45 +0000 | [diff] [blame] | 4035 | return nullptr; |
| 4036 | } |
| 4037 | |
Alex Lorenz | 65317e1 | 2019-01-08 22:32:51 +0000 | [diff] [blame] | 4038 | CXEvalResult clang_Cursor_Evaluate(CXCursor C) { |
| 4039 | if (const Expr *E = |
| 4040 | clang_getCursorKind(C) == CXCursor_CompoundStmt |
| 4041 | ? evaluateCompoundStmtExpr(cast<CompoundStmt>(getCursorStmt(C))) |
| 4042 | : evaluateDeclExpr(getCursorDecl(C))) |
| 4043 | return const_cast<CXEvalResult>( |
| 4044 | reinterpret_cast<const void *>(evaluateExpr(const_cast<Expr *>(E), C))); |
| 4045 | return nullptr; |
| 4046 | } |
| 4047 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 4048 | unsigned clang_Cursor_hasAttrs(CXCursor C) { |
| 4049 | const Decl *D = getCursorDecl(C); |
| 4050 | if (!D) { |
| 4051 | return 0; |
| 4052 | } |
| 4053 | |
| 4054 | if (D->hasAttrs()) { |
| 4055 | return 1; |
| 4056 | } |
| 4057 | |
| 4058 | return 0; |
| 4059 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4060 | unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { |
| 4061 | return CXSaveTranslationUnit_None; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4062 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4063 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4064 | static CXSaveError clang_saveTranslationUnit_Impl(CXTranslationUnit TU, |
| 4065 | const char *FileName, |
| 4066 | unsigned options) { |
| 4067 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4068 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
| 4069 | setThreadBackgroundPriority(); |
| 4070 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4071 | bool hadError = cxtu::getASTUnit(TU)->Save(FileName); |
| 4072 | return hadError ? CXSaveError_Unknown : CXSaveError_None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, |
| 4076 | unsigned options) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4077 | LOG_FUNC_SECTION { *Log << TU << ' ' << FileName; } |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 4078 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4079 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4080 | LOG_BAD_TU(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4081 | return CXSaveError_InvalidTU; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4082 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4083 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4084 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4085 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 4086 | if (!CXXUnit->hasSema()) |
| 4087 | return CXSaveError_InvalidTU; |
| 4088 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4089 | CXSaveError result; |
| 4090 | auto SaveTranslationUnitImpl = [=, &result]() { |
| 4091 | result = clang_saveTranslationUnit_Impl(TU, FileName, options); |
| 4092 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4093 | |
Erik Verbruggen | 3cc3911 | 2017-11-14 09:34:39 +0000 | [diff] [blame] | 4094 | if (!CXXUnit->getDiagnostics().hasUnrecoverableErrorOccurred()) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4095 | SaveTranslationUnitImpl(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4096 | |
| 4097 | if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 4098 | PrintLibclangResourceUsage(TU); |
| 4099 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4100 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4101 | } |
| 4102 | |
| 4103 | // We have an AST that has invalid nodes due to compiler errors. |
| 4104 | // Use a crash recovery thread for protection. |
| 4105 | |
| 4106 | llvm::CrashRecoveryContext CRC; |
| 4107 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4108 | if (!RunSafely(CRC, SaveTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4109 | fprintf(stderr, "libclang: crash detected during AST saving: {\n"); |
| 4110 | fprintf(stderr, " 'filename' : '%s'\n", FileName); |
| 4111 | fprintf(stderr, " 'options' : %d,\n", options); |
| 4112 | fprintf(stderr, "}\n"); |
| 4113 | |
| 4114 | return CXSaveError_Unknown; |
| 4115 | |
| 4116 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
| 4117 | PrintLibclangResourceUsage(TU); |
| 4118 | } |
| 4119 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4120 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4121 | } |
| 4122 | |
| 4123 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
| 4124 | if (CTUnit) { |
| 4125 | // If the translation unit has been marked as unsafe to free, just discard |
| 4126 | // it. |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 4127 | ASTUnit *Unit = cxtu::getASTUnit(CTUnit); |
| 4128 | if (Unit && Unit->isUnsafeToFree()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4129 | return; |
| 4130 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4131 | delete cxtu::getASTUnit(CTUnit); |
Dmitri Gribenko | b95b3f1 | 2013-01-26 22:44:19 +0000 | [diff] [blame] | 4132 | delete CTUnit->StringPool; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4133 | delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics); |
| 4134 | disposeOverridenCXCursorsPool(CTUnit->OverridenCursorsPool); |
Dmitri Gribenko | 9e60511 | 2013-11-13 22:16:51 +0000 | [diff] [blame] | 4135 | delete CTUnit->CommentToXML; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4136 | delete CTUnit; |
| 4137 | } |
| 4138 | } |
| 4139 | |
Erik Verbruggen | 346066b | 2017-05-30 14:25:54 +0000 | [diff] [blame] | 4140 | unsigned clang_suspendTranslationUnit(CXTranslationUnit CTUnit) { |
| 4141 | if (CTUnit) { |
| 4142 | ASTUnit *Unit = cxtu::getASTUnit(CTUnit); |
| 4143 | |
| 4144 | if (Unit && Unit->isUnsafeToFree()) |
| 4145 | return false; |
| 4146 | |
| 4147 | Unit->ResetForParse(); |
| 4148 | return true; |
| 4149 | } |
| 4150 | |
| 4151 | return false; |
| 4152 | } |
| 4153 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4154 | unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { |
| 4155 | return CXReparse_None; |
| 4156 | } |
| 4157 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4158 | static CXErrorCode |
| 4159 | clang_reparseTranslationUnit_Impl(CXTranslationUnit TU, |
| 4160 | ArrayRef<CXUnsavedFile> unsaved_files, |
| 4161 | unsigned options) { |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 4162 | // Check arguments. |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4163 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4164 | LOG_BAD_TU(TU); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4165 | return CXError_InvalidArguments; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 4166 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4167 | |
| 4168 | // Reset the associated diagnostics. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4169 | delete static_cast<CXDiagnosticSetImpl *>(TU->Diagnostics); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 4170 | TU->Diagnostics = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4171 | |
Dmitri Gribenko | 183436e | 2013-01-26 21:49:50 +0000 | [diff] [blame] | 4172 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4173 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) |
| 4174 | setThreadBackgroundPriority(); |
| 4175 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4176 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4177 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 4178 | |
| 4179 | std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles( |
| 4180 | new std::vector<ASTUnit::RemappedFile>()); |
| 4181 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4182 | // Recover resources if we crash before exiting this function. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4183 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<ASTUnit::RemappedFile>> |
| 4184 | RemappedCleanup(RemappedFiles.get()); |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 4185 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4186 | for (auto &UF : unsaved_files) { |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 4187 | std::unique_ptr<llvm::MemoryBuffer> MB = |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 4188 | llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 4189 | RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4190 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 4191 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 4192 | if (!CXXUnit->Reparse(CXXIdx->getPCHContainerOperations(), |
| 4193 | *RemappedFiles.get())) |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4194 | return CXError_Success; |
| 4195 | if (isASTReadError(CXXUnit)) |
| 4196 | return CXError_ASTReadError; |
| 4197 | return CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4198 | } |
| 4199 | |
| 4200 | int clang_reparseTranslationUnit(CXTranslationUnit TU, |
| 4201 | unsigned num_unsaved_files, |
| 4202 | struct CXUnsavedFile *unsaved_files, |
| 4203 | unsigned options) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4204 | LOG_FUNC_SECTION { *Log << TU; } |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 4205 | |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 4206 | if (num_unsaved_files && !unsaved_files) |
| 4207 | return CXError_InvalidArguments; |
| 4208 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4209 | CXErrorCode result; |
| 4210 | auto ReparseTranslationUnitImpl = [=, &result]() { |
| 4211 | result = clang_reparseTranslationUnit_Impl( |
| 4212 | TU, llvm::makeArrayRef(unsaved_files, num_unsaved_files), options); |
| 4213 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4214 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4215 | llvm::CrashRecoveryContext CRC; |
| 4216 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4217 | if (!RunSafely(CRC, ReparseTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4218 | fprintf(stderr, "libclang: crash detected during reparsing\n"); |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4219 | cxtu::getASTUnit(TU)->setUnsafeToFree(true); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 4220 | return CXError_Crashed; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4221 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 4222 | PrintLibclangResourceUsage(TU); |
| 4223 | |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 4224 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4225 | } |
| 4226 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4227 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4228 | if (isNotUsableTU(CTUnit)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4229 | LOG_BAD_TU(CTUnit); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4230 | return cxstring::createEmpty(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4231 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4232 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4233 | ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4234 | return cxstring::createDup(CXXUnit->getOriginalSourceFileName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4235 | } |
| 4236 | |
| 4237 | CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4238 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4239 | LOG_BAD_TU(TU); |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 4240 | return clang_getNullCursor(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4241 | } |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 4242 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4243 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4244 | return MakeCXCursor(CXXUnit->getASTContext().getTranslationUnitDecl(), TU); |
| 4245 | } |
| 4246 | |
Emilio Cobos Alvarez | 485ad42 | 2017-04-28 15:56:39 +0000 | [diff] [blame] | 4247 | CXTargetInfo clang_getTranslationUnitTargetInfo(CXTranslationUnit CTUnit) { |
| 4248 | if (isNotUsableTU(CTUnit)) { |
| 4249 | LOG_BAD_TU(CTUnit); |
| 4250 | return nullptr; |
| 4251 | } |
| 4252 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4253 | CXTargetInfoImpl *impl = new CXTargetInfoImpl(); |
Emilio Cobos Alvarez | 485ad42 | 2017-04-28 15:56:39 +0000 | [diff] [blame] | 4254 | impl->TranslationUnit = CTUnit; |
| 4255 | return impl; |
| 4256 | } |
| 4257 | |
| 4258 | CXString clang_TargetInfo_getTriple(CXTargetInfo TargetInfo) { |
| 4259 | if (!TargetInfo) |
| 4260 | return cxstring::createEmpty(); |
| 4261 | |
| 4262 | CXTranslationUnit CTUnit = TargetInfo->TranslationUnit; |
| 4263 | assert(!isNotUsableTU(CTUnit) && |
| 4264 | "Unexpected unusable translation unit in TargetInfo"); |
| 4265 | |
| 4266 | ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); |
| 4267 | std::string Triple = |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4268 | CXXUnit->getASTContext().getTargetInfo().getTriple().normalize(); |
Emilio Cobos Alvarez | 485ad42 | 2017-04-28 15:56:39 +0000 | [diff] [blame] | 4269 | return cxstring::createDup(Triple); |
| 4270 | } |
| 4271 | |
| 4272 | int clang_TargetInfo_getPointerWidth(CXTargetInfo TargetInfo) { |
| 4273 | if (!TargetInfo) |
| 4274 | return -1; |
| 4275 | |
| 4276 | CXTranslationUnit CTUnit = TargetInfo->TranslationUnit; |
| 4277 | assert(!isNotUsableTU(CTUnit) && |
| 4278 | "Unexpected unusable translation unit in TargetInfo"); |
| 4279 | |
| 4280 | ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); |
| 4281 | return CXXUnit->getASTContext().getTargetInfo().getMaxPointerWidth(); |
| 4282 | } |
| 4283 | |
| 4284 | void clang_TargetInfo_dispose(CXTargetInfo TargetInfo) { |
| 4285 | if (!TargetInfo) |
| 4286 | return; |
| 4287 | |
| 4288 | delete TargetInfo; |
| 4289 | } |
| 4290 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4291 | //===----------------------------------------------------------------------===// |
| 4292 | // CXFile Operations. |
| 4293 | //===----------------------------------------------------------------------===// |
| 4294 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4295 | CXString clang_getFileName(CXFile SFile) { |
| 4296 | if (!SFile) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 4297 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4298 | |
| 4299 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4300 | return cxstring::createRef(FEnt->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
| 4303 | time_t clang_getFileTime(CXFile SFile) { |
| 4304 | if (!SFile) |
| 4305 | return 0; |
| 4306 | |
| 4307 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 4308 | return FEnt->getModificationTime(); |
| 4309 | } |
| 4310 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4311 | CXFile clang_getFile(CXTranslationUnit TU, const char *file_name) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4312 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4313 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 4314 | return nullptr; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4315 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4316 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4317 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4318 | |
| 4319 | FileManager &FMgr = CXXUnit->getFileManager(); |
Harlan Haskins | 8d323d1 | 2019-08-01 21:31:56 +0000 | [diff] [blame] | 4320 | auto File = FMgr.getFile(file_name); |
| 4321 | if (!File) |
| 4322 | return nullptr; |
| 4323 | return const_cast<FileEntry *>(*File); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4324 | } |
| 4325 | |
Erik Verbruggen | 3afa3ce | 2017-12-06 09:02:52 +0000 | [diff] [blame] | 4326 | const char *clang_getFileContents(CXTranslationUnit TU, CXFile file, |
| 4327 | size_t *size) { |
| 4328 | if (isNotUsableTU(TU)) { |
| 4329 | LOG_BAD_TU(TU); |
| 4330 | return nullptr; |
| 4331 | } |
| 4332 | |
| 4333 | const SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager(); |
| 4334 | FileID fid = SM.translateFile(static_cast<FileEntry *>(file)); |
| 4335 | bool Invalid = true; |
Nico Weber | 04347d8 | 2019-04-04 21:06:41 +0000 | [diff] [blame] | 4336 | const llvm::MemoryBuffer *buf = SM.getBuffer(fid, &Invalid); |
Erik Verbruggen | 3afa3ce | 2017-12-06 09:02:52 +0000 | [diff] [blame] | 4337 | if (Invalid) { |
| 4338 | if (size) |
| 4339 | *size = 0; |
| 4340 | return nullptr; |
| 4341 | } |
| 4342 | if (size) |
| 4343 | *size = buf->getBufferSize(); |
| 4344 | return buf->getBufferStart(); |
| 4345 | } |
| 4346 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4347 | unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit TU, CXFile file) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4348 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4349 | LOG_BAD_TU(TU); |
| 4350 | return 0; |
| 4351 | } |
| 4352 | |
| 4353 | if (!file) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4354 | return 0; |
| 4355 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4356 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4357 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4358 | return CXXUnit->getPreprocessor() |
| 4359 | .getHeaderSearchInfo() |
| 4360 | .isFileMultipleIncludeGuarded(FEnt); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4363 | int clang_getFileUniqueID(CXFile file, CXFileUniqueID *outID) { |
| 4364 | if (!file || !outID) |
| 4365 | return 1; |
| 4366 | |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4367 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 4368 | const llvm::sys::fs::UniqueID &ID = FEnt->getUniqueID(); |
| 4369 | outID->data[0] = ID.getDevice(); |
| 4370 | outID->data[1] = ID.getFile(); |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4371 | outID->data[2] = FEnt->getModificationTime(); |
| 4372 | return 0; |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
Argyrios Kyrtzidis | ac3997e | 2014-08-16 00:26:19 +0000 | [diff] [blame] | 4375 | int clang_File_isEqual(CXFile file1, CXFile file2) { |
| 4376 | if (file1 == file2) |
| 4377 | return true; |
| 4378 | |
| 4379 | if (!file1 || !file2) |
| 4380 | return false; |
| 4381 | |
| 4382 | FileEntry *FEnt1 = static_cast<FileEntry *>(file1); |
| 4383 | FileEntry *FEnt2 = static_cast<FileEntry *>(file2); |
| 4384 | return FEnt1->getUniqueID() == FEnt2->getUniqueID(); |
| 4385 | } |
| 4386 | |
Fangrui Song | e46ac5f | 2018-04-07 20:50:35 +0000 | [diff] [blame] | 4387 | CXString clang_File_tryGetRealPathName(CXFile SFile) { |
| 4388 | if (!SFile) |
| 4389 | return cxstring::createNull(); |
| 4390 | |
| 4391 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 4392 | return cxstring::createRef(FEnt->tryGetRealPathName()); |
| 4393 | } |
| 4394 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4395 | //===----------------------------------------------------------------------===// |
| 4396 | // CXCursor Operations. |
| 4397 | //===----------------------------------------------------------------------===// |
| 4398 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4399 | static const Decl *getDeclFromExpr(const Stmt *E) { |
| 4400 | if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4401 | return getDeclFromExpr(CE->getSubExpr()); |
| 4402 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4403 | if (const DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4404 | return RefExpr->getDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4405 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4406 | return ME->getMemberDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4407 | if (const ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4408 | return RE->getDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4409 | if (const ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4410 | if (PRE->isExplicitProperty()) |
| 4411 | return PRE->getExplicitProperty(); |
| 4412 | // It could be messaging both getter and setter as in: |
| 4413 | // ++myobj.myprop; |
| 4414 | // in which case prefer to associate the setter since it is less obvious |
| 4415 | // from inspecting the source that the setter is going to get called. |
| 4416 | if (PRE->isMessagingSetter()) |
| 4417 | return PRE->getImplicitPropertySetter(); |
| 4418 | return PRE->getImplicitPropertyGetter(); |
| 4419 | } |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4420 | if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4421 | return getDeclFromExpr(POE->getSyntacticForm()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4422 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4423 | if (Expr *Src = OVE->getSourceExpr()) |
| 4424 | return getDeclFromExpr(Src); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4425 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4426 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4427 | return getDeclFromExpr(CE->getCallee()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4428 | if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4429 | if (!CE->isElidable()) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4430 | return CE->getConstructor(); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4431 | if (const CXXInheritedCtorInitExpr *CE = |
| 4432 | dyn_cast<CXXInheritedCtorInitExpr>(E)) |
| 4433 | return CE->getConstructor(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4434 | if (const ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4435 | return OME->getMethodDecl(); |
| 4436 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4437 | if (const ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4438 | return PE->getProtocol(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4439 | if (const SubstNonTypeTemplateParmPackExpr *NTTP = |
| 4440 | dyn_cast<SubstNonTypeTemplateParmPackExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4441 | return NTTP->getParameterPack(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4442 | if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4443 | if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4444 | isa<ParmVarDecl>(SizeOfPack->getPack())) |
| 4445 | return SizeOfPack->getPack(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 4446 | |
| 4447 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4450 | static SourceLocation getLocationFromExpr(const Expr *E) { |
| 4451 | if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4452 | return getLocationFromExpr(CE->getSubExpr()); |
| 4453 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4454 | if (const ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4455 | return /*FIXME:*/ Msg->getLeftLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4456 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4457 | return DRE->getLocation(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4458 | if (const MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4459 | return Member->getMemberLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4460 | if (const ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4461 | return Ivar->getLocation(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4462 | if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4463 | return SizeOfPack->getPackLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4464 | if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4465 | return PropRef->getLocation(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4466 | |
| 4467 | return E->getBeginLoc(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4468 | } |
| 4469 | |
NAKAMURA Takumi | a01f4c3 | 2016-12-19 16:50:43 +0000 | [diff] [blame] | 4470 | extern "C" { |
| 4471 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4472 | unsigned clang_visitChildren(CXCursor parent, CXCursorVisitor visitor, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4473 | CXClientData client_data) { |
| 4474 | CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, |
| 4475 | /*VisitPreprocessorLast=*/false); |
| 4476 | return CursorVis.VisitChildren(parent); |
| 4477 | } |
| 4478 | |
| 4479 | #ifndef __has_feature |
| 4480 | #define __has_feature(x) 0 |
| 4481 | #endif |
| 4482 | #if __has_feature(blocks) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4483 | typedef enum CXChildVisitResult (^CXCursorVisitorBlock)(CXCursor cursor, |
| 4484 | CXCursor parent); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4485 | |
| 4486 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4487 | CXClientData client_data) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4488 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 4489 | return block(cursor, parent); |
| 4490 | } |
| 4491 | #else |
| 4492 | // If we are compiled with a compiler that doesn't have native blocks support, |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4493 | // define and call the block manually, so the |
| 4494 | typedef struct _CXChildVisitResult { |
| 4495 | void *isa; |
| 4496 | int flags; |
| 4497 | int reserved; |
| 4498 | enum CXChildVisitResult (*invoke)(struct _CXChildVisitResult *, CXCursor, |
| 4499 | CXCursor); |
| 4500 | } * CXCursorVisitorBlock; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4501 | |
| 4502 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4503 | CXClientData client_data) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4504 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 4505 | return block->invoke(block, cursor, parent); |
| 4506 | } |
| 4507 | #endif |
| 4508 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4509 | unsigned clang_visitChildrenWithBlock(CXCursor parent, |
| 4510 | CXCursorVisitorBlock block) { |
| 4511 | return clang_visitChildren(parent, visitWithBlock, block); |
| 4512 | } |
| 4513 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4514 | static CXString getDeclSpelling(const Decl *D) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4515 | if (!D) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4516 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4517 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4518 | const NamedDecl *ND = dyn_cast<NamedDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4519 | if (!ND) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4520 | if (const ObjCPropertyImplDecl *PropImpl = |
| 4521 | dyn_cast<ObjCPropertyImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4522 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4523 | return cxstring::createDup(Property->getIdentifier()->getName()); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4524 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4525 | if (const ImportDecl *ImportD = dyn_cast<ImportDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4526 | if (Module *Mod = ImportD->getImportedModule()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4527 | return cxstring::createDup(Mod->getFullModuleName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4528 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4529 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4530 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4531 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4532 | if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4533 | return cxstring::createDup(OMD->getSelector().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4534 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4535 | if (const ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4536 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 4537 | // and returns different names. NamedDecl returns the class name and |
| 4538 | // ObjCCategoryImplDecl returns the category name. |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4539 | return cxstring::createRef(CIMP->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4540 | |
| 4541 | if (isa<UsingDirectiveDecl>(D)) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4542 | return cxstring::createEmpty(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4543 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4544 | SmallString<1024> S; |
| 4545 | llvm::raw_svector_ostream os(S); |
| 4546 | ND->printName(os); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4547 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4548 | return cxstring::createDup(os.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4549 | } |
| 4550 | |
| 4551 | CXString clang_getCursorSpelling(CXCursor C) { |
| 4552 | if (clang_isTranslationUnit(C.kind)) |
Dmitri Gribenko | 2c173b4 | 2013-01-11 19:28:44 +0000 | [diff] [blame] | 4553 | return clang_getTranslationUnitSpelling(getCursorTU(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4554 | |
| 4555 | if (clang_isReference(C.kind)) { |
| 4556 | switch (C.kind) { |
| 4557 | case CXCursor_ObjCSuperClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4558 | const ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4559 | return cxstring::createRef(Super->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4560 | } |
| 4561 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4562 | const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4563 | return cxstring::createRef(Class->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4564 | } |
| 4565 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4566 | const ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4567 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4568 | return cxstring::createRef(OID->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4569 | } |
| 4570 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4571 | const CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4572 | return cxstring::createDup(B->getType().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4573 | } |
| 4574 | case CXCursor_TypeRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4575 | const TypeDecl *Type = getCursorTypeRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4576 | assert(Type && "Missing type decl"); |
| 4577 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4578 | return cxstring::createDup( |
| 4579 | getCursorContext(C).getTypeDeclType(Type).getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4580 | } |
| 4581 | case CXCursor_TemplateRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4582 | const TemplateDecl *Template = getCursorTemplateRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4583 | assert(Template && "Missing template decl"); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4584 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4585 | return cxstring::createDup(Template->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4586 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4587 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4588 | case CXCursor_NamespaceRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4589 | const NamedDecl *NS = getCursorNamespaceRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4590 | assert(NS && "Missing namespace decl"); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4591 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4592 | return cxstring::createDup(NS->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | case CXCursor_MemberRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4596 | const FieldDecl *Field = getCursorMemberRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4597 | assert(Field && "Missing member decl"); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4598 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4599 | return cxstring::createDup(Field->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4600 | } |
| 4601 | |
| 4602 | case CXCursor_LabelRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4603 | const LabelStmt *Label = getCursorLabelRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4604 | assert(Label && "Missing label"); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4605 | |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4606 | return cxstring::createRef(Label->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4607 | } |
| 4608 | |
| 4609 | case CXCursor_OverloadedDeclRef: { |
| 4610 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4611 | if (const Decl *D = Storage.dyn_cast<const Decl *>()) { |
| 4612 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4613 | return cxstring::createDup(ND->getNameAsString()); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4614 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4615 | } |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4616 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4617 | return cxstring::createDup(E->getName().getAsString()); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4618 | OverloadedTemplateStorage *Ovl = |
| 4619 | Storage.get<OverloadedTemplateStorage *>(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4620 | if (Ovl->size() == 0) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4621 | return cxstring::createEmpty(); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4622 | return cxstring::createDup((*Ovl->begin())->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4623 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4624 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4625 | case CXCursor_VariableRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4626 | const VarDecl *Var = getCursorVariableRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4627 | assert(Var && "Missing variable decl"); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4628 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4629 | return cxstring::createDup(Var->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4630 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4631 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4632 | default: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4633 | return cxstring::createRef("<not implemented>"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4634 | } |
| 4635 | } |
| 4636 | |
| 4637 | if (clang_isExpression(C.kind)) { |
Argyrios Kyrtzidis | 3227d86 | 2014-03-03 19:40:52 +0000 | [diff] [blame] | 4638 | const Expr *E = getCursorExpr(C); |
| 4639 | |
| 4640 | if (C.kind == CXCursor_ObjCStringLiteral || |
| 4641 | C.kind == CXCursor_StringLiteral) { |
| 4642 | const StringLiteral *SLit; |
| 4643 | if (const ObjCStringLiteral *OSL = dyn_cast<ObjCStringLiteral>(E)) { |
| 4644 | SLit = OSL->getString(); |
| 4645 | } else { |
| 4646 | SLit = cast<StringLiteral>(E); |
| 4647 | } |
| 4648 | SmallString<256> Buf; |
| 4649 | llvm::raw_svector_ostream OS(Buf); |
| 4650 | SLit->outputString(OS); |
| 4651 | return cxstring::createDup(OS.str()); |
| 4652 | } |
| 4653 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4654 | const Decl *D = getDeclFromExpr(getCursorExpr(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4655 | if (D) |
| 4656 | return getDeclSpelling(D); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4657 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4661 | const Stmt *S = getCursorStmt(C); |
| 4662 | if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4663 | return cxstring::createRef(Label->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4664 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4665 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4666 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4667 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4668 | if (C.kind == CXCursor_MacroExpansion) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4669 | return cxstring::createRef( |
| 4670 | getCursorMacroExpansion(C).getName()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4671 | |
| 4672 | if (C.kind == CXCursor_MacroDefinition) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4673 | return cxstring::createRef( |
| 4674 | getCursorMacroDefinition(C)->getName()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4675 | |
| 4676 | if (C.kind == CXCursor_InclusionDirective) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4677 | return cxstring::createDup(getCursorInclusionDirective(C)->getFileName()); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4678 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4679 | if (clang_isDeclaration(C.kind)) |
| 4680 | return getDeclSpelling(getCursorDecl(C)); |
| 4681 | |
| 4682 | if (C.kind == CXCursor_AnnotateAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 4683 | const AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C)); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4684 | return cxstring::createDup(AA->getAnnotation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | if (C.kind == CXCursor_AsmLabelAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 4688 | const AsmLabelAttr *AA = cast<AsmLabelAttr>(cxcursor::getCursorAttr(C)); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4689 | return cxstring::createDup(AA->getLabel()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4690 | } |
| 4691 | |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 4692 | if (C.kind == CXCursor_PackedAttr) { |
| 4693 | return cxstring::createRef("packed"); |
| 4694 | } |
| 4695 | |
Saleem Abdulrasool | 79c6971 | 2015-09-05 18:53:43 +0000 | [diff] [blame] | 4696 | if (C.kind == CXCursor_VisibilityAttr) { |
| 4697 | const VisibilityAttr *AA = cast<VisibilityAttr>(cxcursor::getCursorAttr(C)); |
| 4698 | switch (AA->getVisibility()) { |
| 4699 | case VisibilityAttr::VisibilityType::Default: |
| 4700 | return cxstring::createRef("default"); |
| 4701 | case VisibilityAttr::VisibilityType::Hidden: |
| 4702 | return cxstring::createRef("hidden"); |
| 4703 | case VisibilityAttr::VisibilityType::Protected: |
| 4704 | return cxstring::createRef("protected"); |
| 4705 | } |
| 4706 | llvm_unreachable("unknown visibility type"); |
| 4707 | } |
| 4708 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4709 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4710 | } |
| 4711 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4712 | CXSourceRange clang_Cursor_getSpellingNameRange(CXCursor C, unsigned pieceIndex, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4713 | unsigned options) { |
| 4714 | if (clang_Cursor_isNull(C)) |
| 4715 | return clang_getNullRange(); |
| 4716 | |
| 4717 | ASTContext &Ctx = getCursorContext(C); |
| 4718 | |
| 4719 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4720 | const Stmt *S = getCursorStmt(C); |
| 4721 | if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4722 | if (pieceIndex > 0) |
| 4723 | return clang_getNullRange(); |
| 4724 | return cxloc::translateSourceRange(Ctx, Label->getIdentLoc()); |
| 4725 | } |
| 4726 | |
| 4727 | return clang_getNullRange(); |
| 4728 | } |
| 4729 | |
| 4730 | if (C.kind == CXCursor_ObjCMessageExpr) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4731 | if (const ObjCMessageExpr *ME = |
| 4732 | dyn_cast_or_null<ObjCMessageExpr>(getCursorExpr(C))) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4733 | if (pieceIndex >= ME->getNumSelectorLocs()) |
| 4734 | return clang_getNullRange(); |
| 4735 | return cxloc::translateSourceRange(Ctx, ME->getSelectorLoc(pieceIndex)); |
| 4736 | } |
| 4737 | } |
| 4738 | |
| 4739 | if (C.kind == CXCursor_ObjCInstanceMethodDecl || |
| 4740 | C.kind == CXCursor_ObjCClassMethodDecl) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4741 | if (const ObjCMethodDecl *MD = |
| 4742 | dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(C))) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4743 | if (pieceIndex >= MD->getNumSelectorLocs()) |
| 4744 | return clang_getNullRange(); |
| 4745 | return cxloc::translateSourceRange(Ctx, MD->getSelectorLoc(pieceIndex)); |
| 4746 | } |
| 4747 | } |
| 4748 | |
| 4749 | if (C.kind == CXCursor_ObjCCategoryDecl || |
| 4750 | C.kind == CXCursor_ObjCCategoryImplDecl) { |
| 4751 | if (pieceIndex > 0) |
| 4752 | return clang_getNullRange(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4753 | if (const ObjCCategoryDecl *CD = |
| 4754 | dyn_cast_or_null<ObjCCategoryDecl>(getCursorDecl(C))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4755 | return cxloc::translateSourceRange(Ctx, CD->getCategoryNameLoc()); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4756 | if (const ObjCCategoryImplDecl *CID = |
| 4757 | dyn_cast_or_null<ObjCCategoryImplDecl>(getCursorDecl(C))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4758 | return cxloc::translateSourceRange(Ctx, CID->getCategoryNameLoc()); |
| 4759 | } |
| 4760 | |
| 4761 | if (C.kind == CXCursor_ModuleImportDecl) { |
| 4762 | if (pieceIndex > 0) |
| 4763 | return clang_getNullRange(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4764 | if (const ImportDecl *ImportD = |
| 4765 | dyn_cast_or_null<ImportDecl>(getCursorDecl(C))) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4766 | ArrayRef<SourceLocation> Locs = ImportD->getIdentifierLocs(); |
| 4767 | if (!Locs.empty()) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 4768 | return cxloc::translateSourceRange( |
| 4769 | Ctx, SourceRange(Locs.front(), Locs.back())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4770 | } |
| 4771 | return clang_getNullRange(); |
| 4772 | } |
| 4773 | |
Argyrios Kyrtzidis | a2a1e53 | 2014-08-26 20:23:26 +0000 | [diff] [blame] | 4774 | if (C.kind == CXCursor_CXXMethod || C.kind == CXCursor_Destructor || |
Kevin Funk | 4be5d67 | 2016-12-20 09:56:56 +0000 | [diff] [blame] | 4775 | C.kind == CXCursor_ConversionFunction || |
| 4776 | C.kind == CXCursor_FunctionDecl) { |
Argyrios Kyrtzidis | a2a1e53 | 2014-08-26 20:23:26 +0000 | [diff] [blame] | 4777 | if (pieceIndex > 0) |
| 4778 | return clang_getNullRange(); |
| 4779 | if (const FunctionDecl *FD = |
| 4780 | dyn_cast_or_null<FunctionDecl>(getCursorDecl(C))) { |
| 4781 | DeclarationNameInfo FunctionName = FD->getNameInfo(); |
| 4782 | return cxloc::translateSourceRange(Ctx, FunctionName.getSourceRange()); |
| 4783 | } |
| 4784 | return clang_getNullRange(); |
| 4785 | } |
| 4786 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4787 | // FIXME: A CXCursor_InclusionDirective should give the location of the |
| 4788 | // filename, but we don't keep track of this. |
| 4789 | |
| 4790 | // FIXME: A CXCursor_AnnotateAttr should give the location of the annotation |
| 4791 | // but we don't keep track of this. |
| 4792 | |
| 4793 | // FIXME: A CXCursor_AsmLabelAttr should give the location of the label |
| 4794 | // but we don't keep track of this. |
| 4795 | |
| 4796 | // Default handling, give the location of the cursor. |
| 4797 | |
| 4798 | if (pieceIndex > 0) |
| 4799 | return clang_getNullRange(); |
| 4800 | |
| 4801 | CXSourceLocation CXLoc = clang_getCursorLocation(C); |
| 4802 | SourceLocation Loc = cxloc::translateSourceLocation(CXLoc); |
| 4803 | return cxloc::translateSourceRange(Ctx, Loc); |
| 4804 | } |
| 4805 | |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4806 | CXString clang_Cursor_getMangling(CXCursor C) { |
| 4807 | if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind)) |
| 4808 | return cxstring::createEmpty(); |
| 4809 | |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4810 | // Mangling only works for functions and variables. |
Eli Bendersky | 7975959 | 2014-08-01 15:01:10 +0000 | [diff] [blame] | 4811 | const Decl *D = getCursorDecl(C); |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4812 | if (!D || !(isa<FunctionDecl>(D) || isa<VarDecl>(D))) |
| 4813 | return cxstring::createEmpty(); |
| 4814 | |
Argyrios Kyrtzidis | ca741ce | 2016-02-14 22:30:14 +0000 | [diff] [blame] | 4815 | ASTContext &Ctx = D->getASTContext(); |
Jan Korous | 7e36ecd | 2019-09-05 20:33:52 +0000 | [diff] [blame] | 4816 | ASTNameGenerator ASTNameGen(Ctx); |
| 4817 | return cxstring::createDup(ASTNameGen.getName(D)); |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4818 | } |
| 4819 | |
Saleem Abdulrasool | 6003443 | 2015-11-12 03:57:22 +0000 | [diff] [blame] | 4820 | CXStringSet *clang_Cursor_getCXXManglings(CXCursor C) { |
| 4821 | if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind)) |
| 4822 | return nullptr; |
| 4823 | |
| 4824 | const Decl *D = getCursorDecl(C); |
| 4825 | if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D))) |
| 4826 | return nullptr; |
| 4827 | |
Argyrios Kyrtzidis | ca741ce | 2016-02-14 22:30:14 +0000 | [diff] [blame] | 4828 | ASTContext &Ctx = D->getASTContext(); |
Jan Korous | 7e36ecd | 2019-09-05 20:33:52 +0000 | [diff] [blame] | 4829 | ASTNameGenerator ASTNameGen(Ctx); |
| 4830 | std::vector<std::string> Manglings = ASTNameGen.getAllManglings(D); |
Saleem Abdulrasool | 6003443 | 2015-11-12 03:57:22 +0000 | [diff] [blame] | 4831 | return cxstring::createSet(Manglings); |
| 4832 | } |
| 4833 | |
Dave Lee | 1a532c9 | 2017-09-22 16:58:57 +0000 | [diff] [blame] | 4834 | CXStringSet *clang_Cursor_getObjCManglings(CXCursor C) { |
| 4835 | if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind)) |
| 4836 | return nullptr; |
| 4837 | |
| 4838 | const Decl *D = getCursorDecl(C); |
| 4839 | if (!(isa<ObjCInterfaceDecl>(D) || isa<ObjCImplementationDecl>(D))) |
| 4840 | return nullptr; |
| 4841 | |
| 4842 | ASTContext &Ctx = D->getASTContext(); |
Jan Korous | 7e36ecd | 2019-09-05 20:33:52 +0000 | [diff] [blame] | 4843 | ASTNameGenerator ASTNameGen(Ctx); |
| 4844 | std::vector<std::string> Manglings = ASTNameGen.getAllManglings(D); |
Dave Lee | 1a532c9 | 2017-09-22 16:58:57 +0000 | [diff] [blame] | 4845 | return cxstring::createSet(Manglings); |
| 4846 | } |
| 4847 | |
Jonathan Coe | 45ef503 | 2018-01-16 10:19:56 +0000 | [diff] [blame] | 4848 | CXPrintingPolicy clang_getCursorPrintingPolicy(CXCursor C) { |
| 4849 | if (clang_Cursor_isNull(C)) |
| 4850 | return 0; |
| 4851 | return new PrintingPolicy(getCursorContext(C).getPrintingPolicy()); |
| 4852 | } |
| 4853 | |
| 4854 | void clang_PrintingPolicy_dispose(CXPrintingPolicy Policy) { |
| 4855 | if (Policy) |
| 4856 | delete static_cast<PrintingPolicy *>(Policy); |
| 4857 | } |
| 4858 | |
| 4859 | unsigned |
| 4860 | clang_PrintingPolicy_getProperty(CXPrintingPolicy Policy, |
| 4861 | enum CXPrintingPolicyProperty Property) { |
| 4862 | if (!Policy) |
| 4863 | return 0; |
| 4864 | |
| 4865 | PrintingPolicy *P = static_cast<PrintingPolicy *>(Policy); |
| 4866 | switch (Property) { |
| 4867 | case CXPrintingPolicy_Indentation: |
| 4868 | return P->Indentation; |
| 4869 | case CXPrintingPolicy_SuppressSpecifiers: |
| 4870 | return P->SuppressSpecifiers; |
| 4871 | case CXPrintingPolicy_SuppressTagKeyword: |
| 4872 | return P->SuppressTagKeyword; |
| 4873 | case CXPrintingPolicy_IncludeTagDefinition: |
| 4874 | return P->IncludeTagDefinition; |
| 4875 | case CXPrintingPolicy_SuppressScope: |
| 4876 | return P->SuppressScope; |
| 4877 | case CXPrintingPolicy_SuppressUnwrittenScope: |
| 4878 | return P->SuppressUnwrittenScope; |
| 4879 | case CXPrintingPolicy_SuppressInitializers: |
| 4880 | return P->SuppressInitializers; |
| 4881 | case CXPrintingPolicy_ConstantArraySizeAsWritten: |
| 4882 | return P->ConstantArraySizeAsWritten; |
| 4883 | case CXPrintingPolicy_AnonymousTagLocations: |
| 4884 | return P->AnonymousTagLocations; |
| 4885 | case CXPrintingPolicy_SuppressStrongLifetime: |
| 4886 | return P->SuppressStrongLifetime; |
| 4887 | case CXPrintingPolicy_SuppressLifetimeQualifiers: |
| 4888 | return P->SuppressLifetimeQualifiers; |
| 4889 | case CXPrintingPolicy_SuppressTemplateArgsInCXXConstructors: |
| 4890 | return P->SuppressTemplateArgsInCXXConstructors; |
| 4891 | case CXPrintingPolicy_Bool: |
| 4892 | return P->Bool; |
| 4893 | case CXPrintingPolicy_Restrict: |
| 4894 | return P->Restrict; |
| 4895 | case CXPrintingPolicy_Alignof: |
| 4896 | return P->Alignof; |
| 4897 | case CXPrintingPolicy_UnderscoreAlignof: |
| 4898 | return P->UnderscoreAlignof; |
| 4899 | case CXPrintingPolicy_UseVoidForZeroParams: |
| 4900 | return P->UseVoidForZeroParams; |
| 4901 | case CXPrintingPolicy_TerseOutput: |
| 4902 | return P->TerseOutput; |
| 4903 | case CXPrintingPolicy_PolishForDeclaration: |
| 4904 | return P->PolishForDeclaration; |
| 4905 | case CXPrintingPolicy_Half: |
| 4906 | return P->Half; |
| 4907 | case CXPrintingPolicy_MSWChar: |
| 4908 | return P->MSWChar; |
| 4909 | case CXPrintingPolicy_IncludeNewlines: |
| 4910 | return P->IncludeNewlines; |
| 4911 | case CXPrintingPolicy_MSVCFormatting: |
| 4912 | return P->MSVCFormatting; |
| 4913 | case CXPrintingPolicy_ConstantsAsWritten: |
| 4914 | return P->ConstantsAsWritten; |
| 4915 | case CXPrintingPolicy_SuppressImplicitBase: |
| 4916 | return P->SuppressImplicitBase; |
| 4917 | case CXPrintingPolicy_FullyQualifiedName: |
| 4918 | return P->FullyQualifiedName; |
| 4919 | } |
| 4920 | |
| 4921 | assert(false && "Invalid CXPrintingPolicyProperty"); |
| 4922 | return 0; |
| 4923 | } |
| 4924 | |
| 4925 | void clang_PrintingPolicy_setProperty(CXPrintingPolicy Policy, |
| 4926 | enum CXPrintingPolicyProperty Property, |
| 4927 | unsigned Value) { |
| 4928 | if (!Policy) |
| 4929 | return; |
| 4930 | |
| 4931 | PrintingPolicy *P = static_cast<PrintingPolicy *>(Policy); |
| 4932 | switch (Property) { |
| 4933 | case CXPrintingPolicy_Indentation: |
| 4934 | P->Indentation = Value; |
| 4935 | return; |
| 4936 | case CXPrintingPolicy_SuppressSpecifiers: |
| 4937 | P->SuppressSpecifiers = Value; |
| 4938 | return; |
| 4939 | case CXPrintingPolicy_SuppressTagKeyword: |
| 4940 | P->SuppressTagKeyword = Value; |
| 4941 | return; |
| 4942 | case CXPrintingPolicy_IncludeTagDefinition: |
| 4943 | P->IncludeTagDefinition = Value; |
| 4944 | return; |
| 4945 | case CXPrintingPolicy_SuppressScope: |
| 4946 | P->SuppressScope = Value; |
| 4947 | return; |
| 4948 | case CXPrintingPolicy_SuppressUnwrittenScope: |
| 4949 | P->SuppressUnwrittenScope = Value; |
| 4950 | return; |
| 4951 | case CXPrintingPolicy_SuppressInitializers: |
| 4952 | P->SuppressInitializers = Value; |
| 4953 | return; |
| 4954 | case CXPrintingPolicy_ConstantArraySizeAsWritten: |
| 4955 | P->ConstantArraySizeAsWritten = Value; |
| 4956 | return; |
| 4957 | case CXPrintingPolicy_AnonymousTagLocations: |
| 4958 | P->AnonymousTagLocations = Value; |
| 4959 | return; |
| 4960 | case CXPrintingPolicy_SuppressStrongLifetime: |
| 4961 | P->SuppressStrongLifetime = Value; |
| 4962 | return; |
| 4963 | case CXPrintingPolicy_SuppressLifetimeQualifiers: |
| 4964 | P->SuppressLifetimeQualifiers = Value; |
| 4965 | return; |
| 4966 | case CXPrintingPolicy_SuppressTemplateArgsInCXXConstructors: |
| 4967 | P->SuppressTemplateArgsInCXXConstructors = Value; |
| 4968 | return; |
| 4969 | case CXPrintingPolicy_Bool: |
| 4970 | P->Bool = Value; |
| 4971 | return; |
| 4972 | case CXPrintingPolicy_Restrict: |
| 4973 | P->Restrict = Value; |
| 4974 | return; |
| 4975 | case CXPrintingPolicy_Alignof: |
| 4976 | P->Alignof = Value; |
| 4977 | return; |
| 4978 | case CXPrintingPolicy_UnderscoreAlignof: |
| 4979 | P->UnderscoreAlignof = Value; |
| 4980 | return; |
| 4981 | case CXPrintingPolicy_UseVoidForZeroParams: |
| 4982 | P->UseVoidForZeroParams = Value; |
| 4983 | return; |
| 4984 | case CXPrintingPolicy_TerseOutput: |
| 4985 | P->TerseOutput = Value; |
| 4986 | return; |
| 4987 | case CXPrintingPolicy_PolishForDeclaration: |
| 4988 | P->PolishForDeclaration = Value; |
| 4989 | return; |
| 4990 | case CXPrintingPolicy_Half: |
| 4991 | P->Half = Value; |
| 4992 | return; |
| 4993 | case CXPrintingPolicy_MSWChar: |
| 4994 | P->MSWChar = Value; |
| 4995 | return; |
| 4996 | case CXPrintingPolicy_IncludeNewlines: |
| 4997 | P->IncludeNewlines = Value; |
| 4998 | return; |
| 4999 | case CXPrintingPolicy_MSVCFormatting: |
| 5000 | P->MSVCFormatting = Value; |
| 5001 | return; |
| 5002 | case CXPrintingPolicy_ConstantsAsWritten: |
| 5003 | P->ConstantsAsWritten = Value; |
| 5004 | return; |
| 5005 | case CXPrintingPolicy_SuppressImplicitBase: |
| 5006 | P->SuppressImplicitBase = Value; |
| 5007 | return; |
| 5008 | case CXPrintingPolicy_FullyQualifiedName: |
| 5009 | P->FullyQualifiedName = Value; |
| 5010 | return; |
| 5011 | } |
| 5012 | |
| 5013 | assert(false && "Invalid CXPrintingPolicyProperty"); |
| 5014 | } |
| 5015 | |
| 5016 | CXString clang_getCursorPrettyPrinted(CXCursor C, CXPrintingPolicy cxPolicy) { |
| 5017 | if (clang_Cursor_isNull(C)) |
| 5018 | return cxstring::createEmpty(); |
| 5019 | |
| 5020 | if (clang_isDeclaration(C.kind)) { |
| 5021 | const Decl *D = getCursorDecl(C); |
| 5022 | if (!D) |
| 5023 | return cxstring::createEmpty(); |
| 5024 | |
| 5025 | SmallString<128> Str; |
| 5026 | llvm::raw_svector_ostream OS(Str); |
| 5027 | PrintingPolicy *UserPolicy = static_cast<PrintingPolicy *>(cxPolicy); |
| 5028 | D->print(OS, UserPolicy ? *UserPolicy |
| 5029 | : getCursorContext(C).getPrintingPolicy()); |
| 5030 | |
| 5031 | return cxstring::createDup(OS.str()); |
| 5032 | } |
| 5033 | |
| 5034 | return cxstring::createEmpty(); |
| 5035 | } |
| 5036 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5037 | CXString clang_getCursorDisplayName(CXCursor C) { |
| 5038 | if (!clang_isDeclaration(C.kind)) |
| 5039 | return clang_getCursorSpelling(C); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5040 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5041 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5042 | if (!D) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 5043 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5044 | |
| 5045 | PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5046 | if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5047 | D = FunTmpl->getTemplatedDecl(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5048 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5049 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5050 | SmallString<64> Str; |
| 5051 | llvm::raw_svector_ostream OS(Str); |
| 5052 | OS << *Function; |
| 5053 | if (Function->getPrimaryTemplate()) |
| 5054 | OS << "<>"; |
| 5055 | OS << "("; |
| 5056 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { |
| 5057 | if (I) |
| 5058 | OS << ", "; |
| 5059 | OS << Function->getParamDecl(I)->getType().getAsString(Policy); |
| 5060 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5061 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5062 | if (Function->isVariadic()) { |
| 5063 | if (Function->getNumParams()) |
| 5064 | OS << ", "; |
| 5065 | OS << "..."; |
| 5066 | } |
| 5067 | OS << ")"; |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 5068 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5069 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5070 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5071 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5072 | SmallString<64> Str; |
| 5073 | llvm::raw_svector_ostream OS(Str); |
| 5074 | OS << *ClassTemplate; |
| 5075 | OS << "<"; |
| 5076 | TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); |
| 5077 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 5078 | if (I) |
| 5079 | OS << ", "; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5080 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5081 | NamedDecl *Param = Params->getParam(I); |
| 5082 | if (Param->getIdentifier()) { |
| 5083 | OS << Param->getIdentifier()->getName(); |
| 5084 | continue; |
| 5085 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5086 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5087 | // There is no parameter name, which makes this tricky. Try to come up |
| 5088 | // with something useful that isn't too long. |
| 5089 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Saar Raz | ff1e0fc | 2020-01-15 02:48:42 +0200 | [diff] [blame] | 5090 | if (const auto *TC = TTP->getTypeConstraint()) { |
| 5091 | TC->getConceptNameInfo().printName(OS, Policy); |
| 5092 | if (TC->hasExplicitTemplateArgs()) |
| 5093 | OS << "<...>"; |
| 5094 | } else |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5095 | OS << (TTP->wasDeclaredWithTypename() ? "typename" : "class"); |
| 5096 | else if (NonTypeTemplateParmDecl *NTTP = |
| 5097 | dyn_cast<NonTypeTemplateParmDecl>(Param)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5098 | OS << NTTP->getType().getAsString(Policy); |
| 5099 | else |
| 5100 | OS << "template<...> class"; |
| 5101 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5102 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5103 | OS << ">"; |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 5104 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5105 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5106 | |
| 5107 | if (const ClassTemplateSpecializationDecl *ClassSpec = |
| 5108 | dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5109 | // If the type was explicitly written, use that. |
| 5110 | if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 5111 | return cxstring::createDup(TSInfo->getType().getAsString(Policy)); |
Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame] | 5112 | |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 5113 | SmallString<128> Str; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5114 | llvm::raw_svector_ostream OS(Str); |
| 5115 | OS << *ClassSpec; |
Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame] | 5116 | printTemplateArgumentList(OS, ClassSpec->getTemplateArgs().asArray(), |
| 5117 | Policy); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 5118 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5119 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5120 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5121 | return clang_getCursorSpelling(C); |
| 5122 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5123 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5124 | CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
| 5125 | switch (Kind) { |
| 5126 | case CXCursor_FunctionDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5127 | return cxstring::createRef("FunctionDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5128 | case CXCursor_TypedefDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5129 | return cxstring::createRef("TypedefDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5130 | case CXCursor_EnumDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5131 | return cxstring::createRef("EnumDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5132 | case CXCursor_EnumConstantDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5133 | return cxstring::createRef("EnumConstantDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5134 | case CXCursor_StructDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5135 | return cxstring::createRef("StructDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5136 | case CXCursor_UnionDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5137 | return cxstring::createRef("UnionDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5138 | case CXCursor_ClassDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5139 | return cxstring::createRef("ClassDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5140 | case CXCursor_FieldDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5141 | return cxstring::createRef("FieldDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5142 | case CXCursor_VarDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5143 | return cxstring::createRef("VarDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5144 | case CXCursor_ParmDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5145 | return cxstring::createRef("ParmDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5146 | case CXCursor_ObjCInterfaceDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5147 | return cxstring::createRef("ObjCInterfaceDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5148 | case CXCursor_ObjCCategoryDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5149 | return cxstring::createRef("ObjCCategoryDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5150 | case CXCursor_ObjCProtocolDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5151 | return cxstring::createRef("ObjCProtocolDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5152 | case CXCursor_ObjCPropertyDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5153 | return cxstring::createRef("ObjCPropertyDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5154 | case CXCursor_ObjCIvarDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5155 | return cxstring::createRef("ObjCIvarDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5156 | case CXCursor_ObjCInstanceMethodDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5157 | return cxstring::createRef("ObjCInstanceMethodDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5158 | case CXCursor_ObjCClassMethodDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5159 | return cxstring::createRef("ObjCClassMethodDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5160 | case CXCursor_ObjCImplementationDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5161 | return cxstring::createRef("ObjCImplementationDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5162 | case CXCursor_ObjCCategoryImplDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5163 | return cxstring::createRef("ObjCCategoryImplDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5164 | case CXCursor_CXXMethod: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5165 | return cxstring::createRef("CXXMethod"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5166 | case CXCursor_UnexposedDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5167 | return cxstring::createRef("UnexposedDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5168 | case CXCursor_ObjCSuperClassRef: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5169 | return cxstring::createRef("ObjCSuperClassRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5170 | case CXCursor_ObjCProtocolRef: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5171 | return cxstring::createRef("ObjCProtocolRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5172 | case CXCursor_ObjCClassRef: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5173 | return cxstring::createRef("ObjCClassRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5174 | case CXCursor_TypeRef: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5175 | return cxstring::createRef("TypeRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5176 | case CXCursor_TemplateRef: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5177 | return cxstring::createRef("TemplateRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5178 | case CXCursor_NamespaceRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5179 | return cxstring::createRef("NamespaceRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5180 | case CXCursor_MemberRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5181 | return cxstring::createRef("MemberRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5182 | case CXCursor_LabelRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5183 | return cxstring::createRef("LabelRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5184 | case CXCursor_OverloadedDeclRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5185 | return cxstring::createRef("OverloadedDeclRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5186 | case CXCursor_VariableRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5187 | return cxstring::createRef("VariableRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5188 | case CXCursor_IntegerLiteral: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5189 | return cxstring::createRef("IntegerLiteral"); |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 5190 | case CXCursor_FixedPointLiteral: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5191 | return cxstring::createRef("FixedPointLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5192 | case CXCursor_FloatingLiteral: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5193 | return cxstring::createRef("FloatingLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5194 | case CXCursor_ImaginaryLiteral: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5195 | return cxstring::createRef("ImaginaryLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5196 | case CXCursor_StringLiteral: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5197 | return cxstring::createRef("StringLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5198 | case CXCursor_CharacterLiteral: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5199 | return cxstring::createRef("CharacterLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5200 | case CXCursor_ParenExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5201 | return cxstring::createRef("ParenExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5202 | case CXCursor_UnaryOperator: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5203 | return cxstring::createRef("UnaryOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5204 | case CXCursor_ArraySubscriptExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5205 | return cxstring::createRef("ArraySubscriptExpr"); |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 5206 | case CXCursor_OMPArraySectionExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5207 | return cxstring::createRef("OMPArraySectionExpr"); |
Alexey Bataev | 7ac9efb | 2020-02-05 09:33:05 -0500 | [diff] [blame] | 5208 | case CXCursor_OMPArrayShapingExpr: |
| 5209 | return cxstring::createRef("OMPArrayShapingExpr"); |
Alexey Bataev | 13a1504 | 2020-04-01 15:06:38 -0400 | [diff] [blame] | 5210 | case CXCursor_OMPIteratorExpr: |
| 5211 | return cxstring::createRef("OMPIteratorExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5212 | case CXCursor_BinaryOperator: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5213 | return cxstring::createRef("BinaryOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5214 | case CXCursor_CompoundAssignOperator: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5215 | return cxstring::createRef("CompoundAssignOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5216 | case CXCursor_ConditionalOperator: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5217 | return cxstring::createRef("ConditionalOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5218 | case CXCursor_CStyleCastExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5219 | return cxstring::createRef("CStyleCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5220 | case CXCursor_CompoundLiteralExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5221 | return cxstring::createRef("CompoundLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5222 | case CXCursor_InitListExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5223 | return cxstring::createRef("InitListExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5224 | case CXCursor_AddrLabelExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5225 | return cxstring::createRef("AddrLabelExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5226 | case CXCursor_StmtExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5227 | return cxstring::createRef("StmtExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5228 | case CXCursor_GenericSelectionExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5229 | return cxstring::createRef("GenericSelectionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5230 | case CXCursor_GNUNullExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5231 | return cxstring::createRef("GNUNullExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5232 | case CXCursor_CXXStaticCastExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5233 | return cxstring::createRef("CXXStaticCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5234 | case CXCursor_CXXDynamicCastExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5235 | return cxstring::createRef("CXXDynamicCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5236 | case CXCursor_CXXReinterpretCastExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5237 | return cxstring::createRef("CXXReinterpretCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5238 | case CXCursor_CXXConstCastExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5239 | return cxstring::createRef("CXXConstCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5240 | case CXCursor_CXXFunctionalCastExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5241 | return cxstring::createRef("CXXFunctionalCastExpr"); |
Anastasia Stulova | a6a237f | 2020-05-18 11:02:01 +0100 | [diff] [blame^] | 5242 | case CXCursor_CXXAddrspaceCastExpr: |
| 5243 | return cxstring::createRef("CXXAddrspaceCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5244 | case CXCursor_CXXTypeidExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5245 | return cxstring::createRef("CXXTypeidExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5246 | case CXCursor_CXXBoolLiteralExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5247 | return cxstring::createRef("CXXBoolLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5248 | case CXCursor_CXXNullPtrLiteralExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5249 | return cxstring::createRef("CXXNullPtrLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5250 | case CXCursor_CXXThisExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5251 | return cxstring::createRef("CXXThisExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5252 | case CXCursor_CXXThrowExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5253 | return cxstring::createRef("CXXThrowExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5254 | case CXCursor_CXXNewExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5255 | return cxstring::createRef("CXXNewExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5256 | case CXCursor_CXXDeleteExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5257 | return cxstring::createRef("CXXDeleteExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5258 | case CXCursor_UnaryExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5259 | return cxstring::createRef("UnaryExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5260 | case CXCursor_ObjCStringLiteral: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5261 | return cxstring::createRef("ObjCStringLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5262 | case CXCursor_ObjCBoolLiteralExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5263 | return cxstring::createRef("ObjCBoolLiteralExpr"); |
Erik Pilkington | 29099de | 2016-07-16 00:35:23 +0000 | [diff] [blame] | 5264 | case CXCursor_ObjCAvailabilityCheckExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5265 | return cxstring::createRef("ObjCAvailabilityCheckExpr"); |
Argyrios Kyrtzidis | c2233be | 2013-04-23 17:57:17 +0000 | [diff] [blame] | 5266 | case CXCursor_ObjCSelfExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5267 | return cxstring::createRef("ObjCSelfExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5268 | case CXCursor_ObjCEncodeExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5269 | return cxstring::createRef("ObjCEncodeExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5270 | case CXCursor_ObjCSelectorExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5271 | return cxstring::createRef("ObjCSelectorExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5272 | case CXCursor_ObjCProtocolExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5273 | return cxstring::createRef("ObjCProtocolExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5274 | case CXCursor_ObjCBridgedCastExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5275 | return cxstring::createRef("ObjCBridgedCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5276 | case CXCursor_BlockExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5277 | return cxstring::createRef("BlockExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5278 | case CXCursor_PackExpansionExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5279 | return cxstring::createRef("PackExpansionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5280 | case CXCursor_SizeOfPackExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5281 | return cxstring::createRef("SizeOfPackExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5282 | case CXCursor_LambdaExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5283 | return cxstring::createRef("LambdaExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5284 | case CXCursor_UnexposedExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5285 | return cxstring::createRef("UnexposedExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5286 | case CXCursor_DeclRefExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5287 | return cxstring::createRef("DeclRefExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5288 | case CXCursor_MemberRefExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5289 | return cxstring::createRef("MemberRefExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5290 | case CXCursor_CallExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5291 | return cxstring::createRef("CallExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5292 | case CXCursor_ObjCMessageExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5293 | return cxstring::createRef("ObjCMessageExpr"); |
Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 5294 | case CXCursor_BuiltinBitCastExpr: |
| 5295 | return cxstring::createRef("BuiltinBitCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5296 | case CXCursor_UnexposedStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5297 | return cxstring::createRef("UnexposedStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5298 | case CXCursor_DeclStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5299 | return cxstring::createRef("DeclStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5300 | case CXCursor_LabelStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5301 | return cxstring::createRef("LabelStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5302 | case CXCursor_CompoundStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5303 | return cxstring::createRef("CompoundStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5304 | case CXCursor_CaseStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5305 | return cxstring::createRef("CaseStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5306 | case CXCursor_DefaultStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5307 | return cxstring::createRef("DefaultStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5308 | case CXCursor_IfStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5309 | return cxstring::createRef("IfStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5310 | case CXCursor_SwitchStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5311 | return cxstring::createRef("SwitchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5312 | case CXCursor_WhileStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5313 | return cxstring::createRef("WhileStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5314 | case CXCursor_DoStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5315 | return cxstring::createRef("DoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5316 | case CXCursor_ForStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5317 | return cxstring::createRef("ForStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5318 | case CXCursor_GotoStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5319 | return cxstring::createRef("GotoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5320 | case CXCursor_IndirectGotoStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5321 | return cxstring::createRef("IndirectGotoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5322 | case CXCursor_ContinueStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5323 | return cxstring::createRef("ContinueStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5324 | case CXCursor_BreakStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5325 | return cxstring::createRef("BreakStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5326 | case CXCursor_ReturnStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5327 | return cxstring::createRef("ReturnStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5328 | case CXCursor_GCCAsmStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5329 | return cxstring::createRef("GCCAsmStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5330 | case CXCursor_MSAsmStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5331 | return cxstring::createRef("MSAsmStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5332 | case CXCursor_ObjCAtTryStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5333 | return cxstring::createRef("ObjCAtTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5334 | case CXCursor_ObjCAtCatchStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5335 | return cxstring::createRef("ObjCAtCatchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5336 | case CXCursor_ObjCAtFinallyStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5337 | return cxstring::createRef("ObjCAtFinallyStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5338 | case CXCursor_ObjCAtThrowStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5339 | return cxstring::createRef("ObjCAtThrowStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5340 | case CXCursor_ObjCAtSynchronizedStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5341 | return cxstring::createRef("ObjCAtSynchronizedStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5342 | case CXCursor_ObjCAutoreleasePoolStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5343 | return cxstring::createRef("ObjCAutoreleasePoolStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5344 | case CXCursor_ObjCForCollectionStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5345 | return cxstring::createRef("ObjCForCollectionStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5346 | case CXCursor_CXXCatchStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5347 | return cxstring::createRef("CXXCatchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5348 | case CXCursor_CXXTryStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5349 | return cxstring::createRef("CXXTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5350 | case CXCursor_CXXForRangeStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5351 | return cxstring::createRef("CXXForRangeStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5352 | case CXCursor_SEHTryStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5353 | return cxstring::createRef("SEHTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5354 | case CXCursor_SEHExceptStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5355 | return cxstring::createRef("SEHExceptStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5356 | case CXCursor_SEHFinallyStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5357 | return cxstring::createRef("SEHFinallyStmt"); |
Nico Weber | 9b98207 | 2014-07-07 00:12:30 +0000 | [diff] [blame] | 5358 | case CXCursor_SEHLeaveStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5359 | return cxstring::createRef("SEHLeaveStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5360 | case CXCursor_NullStmt: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5361 | return cxstring::createRef("NullStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5362 | case CXCursor_InvalidFile: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5363 | return cxstring::createRef("InvalidFile"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5364 | case CXCursor_InvalidCode: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5365 | return cxstring::createRef("InvalidCode"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5366 | case CXCursor_NoDeclFound: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5367 | return cxstring::createRef("NoDeclFound"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5368 | case CXCursor_NotImplemented: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5369 | return cxstring::createRef("NotImplemented"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5370 | case CXCursor_TranslationUnit: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5371 | return cxstring::createRef("TranslationUnit"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5372 | case CXCursor_UnexposedAttr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5373 | return cxstring::createRef("UnexposedAttr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5374 | case CXCursor_IBActionAttr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5375 | return cxstring::createRef("attribute(ibaction)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5376 | case CXCursor_IBOutletAttr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5377 | return cxstring::createRef("attribute(iboutlet)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5378 | case CXCursor_IBOutletCollectionAttr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5379 | return cxstring::createRef("attribute(iboutletcollection)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5380 | case CXCursor_CXXFinalAttr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5381 | return cxstring::createRef("attribute(final)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5382 | case CXCursor_CXXOverrideAttr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5383 | return cxstring::createRef("attribute(override)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5384 | case CXCursor_AnnotateAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5385 | return cxstring::createRef("attribute(annotate)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5386 | case CXCursor_AsmLabelAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5387 | return cxstring::createRef("asm label"); |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 5388 | case CXCursor_PackedAttr: |
| 5389 | return cxstring::createRef("attribute(packed)"); |
Joey Gouly | 8122838 | 2014-05-01 15:41:58 +0000 | [diff] [blame] | 5390 | case CXCursor_PureAttr: |
| 5391 | return cxstring::createRef("attribute(pure)"); |
| 5392 | case CXCursor_ConstAttr: |
| 5393 | return cxstring::createRef("attribute(const)"); |
| 5394 | case CXCursor_NoDuplicateAttr: |
| 5395 | return cxstring::createRef("attribute(noduplicate)"); |
Eli Bendersky | 2581e66 | 2014-05-28 19:29:58 +0000 | [diff] [blame] | 5396 | case CXCursor_CUDAConstantAttr: |
| 5397 | return cxstring::createRef("attribute(constant)"); |
| 5398 | case CXCursor_CUDADeviceAttr: |
| 5399 | return cxstring::createRef("attribute(device)"); |
| 5400 | case CXCursor_CUDAGlobalAttr: |
| 5401 | return cxstring::createRef("attribute(global)"); |
| 5402 | case CXCursor_CUDAHostAttr: |
| 5403 | return cxstring::createRef("attribute(host)"); |
Eli Bendersky | 9b07147 | 2014-08-08 14:59:00 +0000 | [diff] [blame] | 5404 | case CXCursor_CUDASharedAttr: |
| 5405 | return cxstring::createRef("attribute(shared)"); |
Saleem Abdulrasool | 79c6971 | 2015-09-05 18:53:43 +0000 | [diff] [blame] | 5406 | case CXCursor_VisibilityAttr: |
| 5407 | return cxstring::createRef("attribute(visibility)"); |
Saleem Abdulrasool | 8aa0b80 | 2015-12-10 18:45:18 +0000 | [diff] [blame] | 5408 | case CXCursor_DLLExport: |
| 5409 | return cxstring::createRef("attribute(dllexport)"); |
| 5410 | case CXCursor_DLLImport: |
| 5411 | return cxstring::createRef("attribute(dllimport)"); |
Michael Wu | d092d0b | 2018-08-03 05:03:22 +0000 | [diff] [blame] | 5412 | case CXCursor_NSReturnsRetained: |
| 5413 | return cxstring::createRef("attribute(ns_returns_retained)"); |
| 5414 | case CXCursor_NSReturnsNotRetained: |
| 5415 | return cxstring::createRef("attribute(ns_returns_not_retained)"); |
| 5416 | case CXCursor_NSReturnsAutoreleased: |
| 5417 | return cxstring::createRef("attribute(ns_returns_autoreleased)"); |
| 5418 | case CXCursor_NSConsumesSelf: |
| 5419 | return cxstring::createRef("attribute(ns_consumes_self)"); |
| 5420 | case CXCursor_NSConsumed: |
| 5421 | return cxstring::createRef("attribute(ns_consumed)"); |
| 5422 | case CXCursor_ObjCException: |
| 5423 | return cxstring::createRef("attribute(objc_exception)"); |
| 5424 | case CXCursor_ObjCNSObject: |
| 5425 | return cxstring::createRef("attribute(NSObject)"); |
| 5426 | case CXCursor_ObjCIndependentClass: |
| 5427 | return cxstring::createRef("attribute(objc_independent_class)"); |
| 5428 | case CXCursor_ObjCPreciseLifetime: |
| 5429 | return cxstring::createRef("attribute(objc_precise_lifetime)"); |
| 5430 | case CXCursor_ObjCReturnsInnerPointer: |
| 5431 | return cxstring::createRef("attribute(objc_returns_inner_pointer)"); |
| 5432 | case CXCursor_ObjCRequiresSuper: |
| 5433 | return cxstring::createRef("attribute(objc_requires_super)"); |
| 5434 | case CXCursor_ObjCRootClass: |
| 5435 | return cxstring::createRef("attribute(objc_root_class)"); |
| 5436 | case CXCursor_ObjCSubclassingRestricted: |
| 5437 | return cxstring::createRef("attribute(objc_subclassing_restricted)"); |
| 5438 | case CXCursor_ObjCExplicitProtocolImpl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5439 | return cxstring::createRef( |
| 5440 | "attribute(objc_protocol_requires_explicit_implementation)"); |
Michael Wu | d092d0b | 2018-08-03 05:03:22 +0000 | [diff] [blame] | 5441 | case CXCursor_ObjCDesignatedInitializer: |
| 5442 | return cxstring::createRef("attribute(objc_designated_initializer)"); |
| 5443 | case CXCursor_ObjCRuntimeVisible: |
| 5444 | return cxstring::createRef("attribute(objc_runtime_visible)"); |
| 5445 | case CXCursor_ObjCBoxable: |
| 5446 | return cxstring::createRef("attribute(objc_boxable)"); |
Michael Wu | 58d837d | 2018-08-03 05:55:40 +0000 | [diff] [blame] | 5447 | case CXCursor_FlagEnum: |
| 5448 | return cxstring::createRef("attribute(flag_enum)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5449 | case CXCursor_PreprocessingDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5450 | return cxstring::createRef("preprocessing directive"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5451 | case CXCursor_MacroDefinition: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5452 | return cxstring::createRef("macro definition"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5453 | case CXCursor_MacroExpansion: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5454 | return cxstring::createRef("macro expansion"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5455 | case CXCursor_InclusionDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5456 | return cxstring::createRef("inclusion directive"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5457 | case CXCursor_Namespace: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5458 | return cxstring::createRef("Namespace"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5459 | case CXCursor_LinkageSpec: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5460 | return cxstring::createRef("LinkageSpec"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5461 | case CXCursor_CXXBaseSpecifier: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5462 | return cxstring::createRef("C++ base class specifier"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5463 | case CXCursor_Constructor: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5464 | return cxstring::createRef("CXXConstructor"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5465 | case CXCursor_Destructor: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5466 | return cxstring::createRef("CXXDestructor"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5467 | case CXCursor_ConversionFunction: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5468 | return cxstring::createRef("CXXConversion"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5469 | case CXCursor_TemplateTypeParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5470 | return cxstring::createRef("TemplateTypeParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5471 | case CXCursor_NonTypeTemplateParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5472 | return cxstring::createRef("NonTypeTemplateParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5473 | case CXCursor_TemplateTemplateParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5474 | return cxstring::createRef("TemplateTemplateParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5475 | case CXCursor_FunctionTemplate: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5476 | return cxstring::createRef("FunctionTemplate"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5477 | case CXCursor_ClassTemplate: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5478 | return cxstring::createRef("ClassTemplate"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5479 | case CXCursor_ClassTemplatePartialSpecialization: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5480 | return cxstring::createRef("ClassTemplatePartialSpecialization"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5481 | case CXCursor_NamespaceAlias: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5482 | return cxstring::createRef("NamespaceAlias"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5483 | case CXCursor_UsingDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5484 | return cxstring::createRef("UsingDirective"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5485 | case CXCursor_UsingDeclaration: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5486 | return cxstring::createRef("UsingDeclaration"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5487 | case CXCursor_TypeAliasDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5488 | return cxstring::createRef("TypeAliasDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5489 | case CXCursor_ObjCSynthesizeDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5490 | return cxstring::createRef("ObjCSynthesizeDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5491 | case CXCursor_ObjCDynamicDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5492 | return cxstring::createRef("ObjCDynamicDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5493 | case CXCursor_CXXAccessSpecifier: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5494 | return cxstring::createRef("CXXAccessSpecifier"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5495 | case CXCursor_ModuleImportDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5496 | return cxstring::createRef("ModuleImport"); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 5497 | case CXCursor_OMPParallelDirective: |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 5498 | return cxstring::createRef("OMPParallelDirective"); |
| 5499 | case CXCursor_OMPSimdDirective: |
| 5500 | return cxstring::createRef("OMPSimdDirective"); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5501 | case CXCursor_OMPForDirective: |
| 5502 | return cxstring::createRef("OMPForDirective"); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 5503 | case CXCursor_OMPForSimdDirective: |
| 5504 | return cxstring::createRef("OMPForSimdDirective"); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 5505 | case CXCursor_OMPSectionsDirective: |
| 5506 | return cxstring::createRef("OMPSectionsDirective"); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 5507 | case CXCursor_OMPSectionDirective: |
| 5508 | return cxstring::createRef("OMPSectionDirective"); |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 5509 | case CXCursor_OMPSingleDirective: |
| 5510 | return cxstring::createRef("OMPSingleDirective"); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 5511 | case CXCursor_OMPMasterDirective: |
| 5512 | return cxstring::createRef("OMPMasterDirective"); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 5513 | case CXCursor_OMPCriticalDirective: |
| 5514 | return cxstring::createRef("OMPCriticalDirective"); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 5515 | case CXCursor_OMPParallelForDirective: |
| 5516 | return cxstring::createRef("OMPParallelForDirective"); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 5517 | case CXCursor_OMPParallelForSimdDirective: |
| 5518 | return cxstring::createRef("OMPParallelForSimdDirective"); |
cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 5519 | case CXCursor_OMPParallelMasterDirective: |
| 5520 | return cxstring::createRef("OMPParallelMasterDirective"); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 5521 | case CXCursor_OMPParallelSectionsDirective: |
| 5522 | return cxstring::createRef("OMPParallelSectionsDirective"); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5523 | case CXCursor_OMPTaskDirective: |
| 5524 | return cxstring::createRef("OMPTaskDirective"); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 5525 | case CXCursor_OMPTaskyieldDirective: |
| 5526 | return cxstring::createRef("OMPTaskyieldDirective"); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 5527 | case CXCursor_OMPBarrierDirective: |
| 5528 | return cxstring::createRef("OMPBarrierDirective"); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 5529 | case CXCursor_OMPTaskwaitDirective: |
| 5530 | return cxstring::createRef("OMPTaskwaitDirective"); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 5531 | case CXCursor_OMPTaskgroupDirective: |
| 5532 | return cxstring::createRef("OMPTaskgroupDirective"); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 5533 | case CXCursor_OMPFlushDirective: |
| 5534 | return cxstring::createRef("OMPFlushDirective"); |
Alexey Bataev | c112e94 | 2020-02-28 09:52:15 -0500 | [diff] [blame] | 5535 | case CXCursor_OMPDepobjDirective: |
| 5536 | return cxstring::createRef("OMPDepobjDirective"); |
Alexey Bataev | fcba7c3 | 2020-03-20 07:03:01 -0400 | [diff] [blame] | 5537 | case CXCursor_OMPScanDirective: |
| 5538 | return cxstring::createRef("OMPScanDirective"); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 5539 | case CXCursor_OMPOrderedDirective: |
| 5540 | return cxstring::createRef("OMPOrderedDirective"); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 5541 | case CXCursor_OMPAtomicDirective: |
| 5542 | return cxstring::createRef("OMPAtomicDirective"); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 5543 | case CXCursor_OMPTargetDirective: |
| 5544 | return cxstring::createRef("OMPTargetDirective"); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 5545 | case CXCursor_OMPTargetDataDirective: |
| 5546 | return cxstring::createRef("OMPTargetDataDirective"); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 5547 | case CXCursor_OMPTargetEnterDataDirective: |
| 5548 | return cxstring::createRef("OMPTargetEnterDataDirective"); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 5549 | case CXCursor_OMPTargetExitDataDirective: |
| 5550 | return cxstring::createRef("OMPTargetExitDataDirective"); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 5551 | case CXCursor_OMPTargetParallelDirective: |
| 5552 | return cxstring::createRef("OMPTargetParallelDirective"); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 5553 | case CXCursor_OMPTargetParallelForDirective: |
| 5554 | return cxstring::createRef("OMPTargetParallelForDirective"); |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 5555 | case CXCursor_OMPTargetUpdateDirective: |
| 5556 | return cxstring::createRef("OMPTargetUpdateDirective"); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 5557 | case CXCursor_OMPTeamsDirective: |
| 5558 | return cxstring::createRef("OMPTeamsDirective"); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 5559 | case CXCursor_OMPCancellationPointDirective: |
| 5560 | return cxstring::createRef("OMPCancellationPointDirective"); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 5561 | case CXCursor_OMPCancelDirective: |
| 5562 | return cxstring::createRef("OMPCancelDirective"); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 5563 | case CXCursor_OMPTaskLoopDirective: |
| 5564 | return cxstring::createRef("OMPTaskLoopDirective"); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 5565 | case CXCursor_OMPTaskLoopSimdDirective: |
| 5566 | return cxstring::createRef("OMPTaskLoopSimdDirective"); |
Alexey Bataev | 60e51c4 | 2019-10-10 20:13:02 +0000 | [diff] [blame] | 5567 | case CXCursor_OMPMasterTaskLoopDirective: |
| 5568 | return cxstring::createRef("OMPMasterTaskLoopDirective"); |
Alexey Bataev | b8552ab | 2019-10-18 16:47:35 +0000 | [diff] [blame] | 5569 | case CXCursor_OMPMasterTaskLoopSimdDirective: |
| 5570 | return cxstring::createRef("OMPMasterTaskLoopSimdDirective"); |
Alexey Bataev | 5bbcead | 2019-10-14 17:17:41 +0000 | [diff] [blame] | 5571 | case CXCursor_OMPParallelMasterTaskLoopDirective: |
| 5572 | return cxstring::createRef("OMPParallelMasterTaskLoopDirective"); |
Alexey Bataev | 14a388f | 2019-10-25 10:27:13 -0400 | [diff] [blame] | 5573 | case CXCursor_OMPParallelMasterTaskLoopSimdDirective: |
| 5574 | return cxstring::createRef("OMPParallelMasterTaskLoopSimdDirective"); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 5575 | case CXCursor_OMPDistributeDirective: |
| 5576 | return cxstring::createRef("OMPDistributeDirective"); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 5577 | case CXCursor_OMPDistributeParallelForDirective: |
| 5578 | return cxstring::createRef("OMPDistributeParallelForDirective"); |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 5579 | case CXCursor_OMPDistributeParallelForSimdDirective: |
| 5580 | return cxstring::createRef("OMPDistributeParallelForSimdDirective"); |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 5581 | case CXCursor_OMPDistributeSimdDirective: |
| 5582 | return cxstring::createRef("OMPDistributeSimdDirective"); |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 5583 | case CXCursor_OMPTargetParallelForSimdDirective: |
| 5584 | return cxstring::createRef("OMPTargetParallelForSimdDirective"); |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 5585 | case CXCursor_OMPTargetSimdDirective: |
| 5586 | return cxstring::createRef("OMPTargetSimdDirective"); |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 5587 | case CXCursor_OMPTeamsDistributeDirective: |
| 5588 | return cxstring::createRef("OMPTeamsDistributeDirective"); |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 5589 | case CXCursor_OMPTeamsDistributeSimdDirective: |
| 5590 | return cxstring::createRef("OMPTeamsDistributeSimdDirective"); |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 5591 | case CXCursor_OMPTeamsDistributeParallelForSimdDirective: |
| 5592 | return cxstring::createRef("OMPTeamsDistributeParallelForSimdDirective"); |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 5593 | case CXCursor_OMPTeamsDistributeParallelForDirective: |
| 5594 | return cxstring::createRef("OMPTeamsDistributeParallelForDirective"); |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 5595 | case CXCursor_OMPTargetTeamsDirective: |
| 5596 | return cxstring::createRef("OMPTargetTeamsDirective"); |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 5597 | case CXCursor_OMPTargetTeamsDistributeDirective: |
| 5598 | return cxstring::createRef("OMPTargetTeamsDistributeDirective"); |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 5599 | case CXCursor_OMPTargetTeamsDistributeParallelForDirective: |
| 5600 | return cxstring::createRef("OMPTargetTeamsDistributeParallelForDirective"); |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 5601 | case CXCursor_OMPTargetTeamsDistributeParallelForSimdDirective: |
| 5602 | return cxstring::createRef( |
| 5603 | "OMPTargetTeamsDistributeParallelForSimdDirective"); |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 5604 | case CXCursor_OMPTargetTeamsDistributeSimdDirective: |
| 5605 | return cxstring::createRef("OMPTargetTeamsDistributeSimdDirective"); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5606 | case CXCursor_OverloadCandidate: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5607 | return cxstring::createRef("OverloadCandidate"); |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 5608 | case CXCursor_TypeAliasTemplateDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5609 | return cxstring::createRef("TypeAliasTemplateDecl"); |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 5610 | case CXCursor_StaticAssert: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5611 | return cxstring::createRef("StaticAssert"); |
Olivier Goffart | d211c64 | 2016-11-04 06:29:27 +0000 | [diff] [blame] | 5612 | case CXCursor_FriendDecl: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5613 | return cxstring::createRef("FriendDecl"); |
Sven van Haastregt | dc2c930 | 2019-02-11 11:00:56 +0000 | [diff] [blame] | 5614 | case CXCursor_ConvergentAttr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5615 | return cxstring::createRef("attribute(convergent)"); |
Emilio Cobos Alvarez | 0a3fe50 | 2019-02-25 21:24:52 +0000 | [diff] [blame] | 5616 | case CXCursor_WarnUnusedAttr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5617 | return cxstring::createRef("attribute(warn_unused)"); |
Emilio Cobos Alvarez | 0a3fe50 | 2019-02-25 21:24:52 +0000 | [diff] [blame] | 5618 | case CXCursor_WarnUnusedResultAttr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5619 | return cxstring::createRef("attribute(warn_unused_result)"); |
Emilio Cobos Alvarez | cd74127 | 2019-03-13 16:16:54 +0000 | [diff] [blame] | 5620 | case CXCursor_AlignedAttr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5621 | return cxstring::createRef("attribute(aligned)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5622 | } |
| 5623 | |
| 5624 | llvm_unreachable("Unhandled CXCursorKind"); |
| 5625 | } |
| 5626 | |
| 5627 | struct GetCursorData { |
| 5628 | SourceLocation TokenBeginLoc; |
| 5629 | bool PointsAtMacroArgExpansion; |
| 5630 | bool VisitedObjCPropertyImplDecl; |
| 5631 | SourceLocation VisitedDeclaratorDeclStartLoc; |
| 5632 | CXCursor &BestCursor; |
| 5633 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5634 | GetCursorData(SourceManager &SM, SourceLocation tokenBegin, |
| 5635 | CXCursor &outputCursor) |
| 5636 | : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5637 | PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin); |
| 5638 | VisitedObjCPropertyImplDecl = false; |
| 5639 | } |
| 5640 | }; |
| 5641 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5642 | static enum CXChildVisitResult |
| 5643 | GetCursorVisitor(CXCursor cursor, CXCursor parent, CXClientData client_data) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5644 | GetCursorData *Data = static_cast<GetCursorData *>(client_data); |
| 5645 | CXCursor *BestCursor = &Data->BestCursor; |
| 5646 | |
| 5647 | // If we point inside a macro argument we should provide info of what the |
| 5648 | // token is so use the actual cursor, don't replace it with a macro expansion |
| 5649 | // cursor. |
| 5650 | if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion) |
| 5651 | return CXChildVisit_Recurse; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5652 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5653 | if (clang_isDeclaration(cursor.kind)) { |
| 5654 | // Avoid having the implicit methods override the property decls. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5655 | if (const ObjCMethodDecl *MD = |
| 5656 | dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5657 | if (MD->isImplicit()) |
| 5658 | return CXChildVisit_Break; |
| 5659 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5660 | } else if (const ObjCInterfaceDecl *ID = |
| 5661 | dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(cursor))) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5662 | // Check that when we have multiple @class references in the same line, |
| 5663 | // that later ones do not override the previous ones. |
| 5664 | // If we have: |
| 5665 | // @class Foo, Bar; |
| 5666 | // source ranges for both start at '@', so 'Bar' will end up overriding |
| 5667 | // 'Foo' even though the cursor location was at 'Foo'. |
| 5668 | if (BestCursor->kind == CXCursor_ObjCInterfaceDecl || |
| 5669 | BestCursor->kind == CXCursor_ObjCClassRef) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5670 | if (const ObjCInterfaceDecl *PrevID = |
| 5671 | dyn_cast_or_null<ObjCInterfaceDecl>( |
| 5672 | getCursorDecl(*BestCursor))) { |
| 5673 | if (PrevID != ID && !PrevID->isThisDeclarationADefinition() && |
| 5674 | !ID->isThisDeclarationADefinition()) |
| 5675 | return CXChildVisit_Break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5676 | } |
| 5677 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5678 | } else if (const DeclaratorDecl *DD = |
| 5679 | dyn_cast_or_null<DeclaratorDecl>(getCursorDecl(cursor))) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5680 | SourceLocation StartLoc = DD->getSourceRange().getBegin(); |
| 5681 | // Check that when we have multiple declarators in the same line, |
| 5682 | // that later ones do not override the previous ones. |
| 5683 | // If we have: |
| 5684 | // int Foo, Bar; |
| 5685 | // source ranges for both start at 'int', so 'Bar' will end up overriding |
| 5686 | // 'Foo' even though the cursor location was at 'Foo'. |
| 5687 | if (Data->VisitedDeclaratorDeclStartLoc == StartLoc) |
| 5688 | return CXChildVisit_Break; |
| 5689 | Data->VisitedDeclaratorDeclStartLoc = StartLoc; |
| 5690 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5691 | } else if (const ObjCPropertyImplDecl *PropImp = |
| 5692 | dyn_cast_or_null<ObjCPropertyImplDecl>( |
| 5693 | getCursorDecl(cursor))) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5694 | (void)PropImp; |
| 5695 | // Check that when we have multiple @synthesize in the same line, |
| 5696 | // that later ones do not override the previous ones. |
| 5697 | // If we have: |
| 5698 | // @synthesize Foo, Bar; |
| 5699 | // source ranges for both start at '@', so 'Bar' will end up overriding |
| 5700 | // 'Foo' even though the cursor location was at 'Foo'. |
| 5701 | if (Data->VisitedObjCPropertyImplDecl) |
| 5702 | return CXChildVisit_Break; |
| 5703 | Data->VisitedObjCPropertyImplDecl = true; |
| 5704 | } |
| 5705 | } |
| 5706 | |
| 5707 | if (clang_isExpression(cursor.kind) && |
| 5708 | clang_isDeclaration(BestCursor->kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5709 | if (const Decl *D = getCursorDecl(*BestCursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5710 | // Avoid having the cursor of an expression replace the declaration cursor |
| 5711 | // when the expression source range overlaps the declaration range. |
| 5712 | // This can happen for C++ constructor expressions whose range generally |
| 5713 | // include the variable declaration, e.g.: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5714 | // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl |
| 5715 | // cursor. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5716 | if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() && |
| 5717 | D->getLocation() == Data->TokenBeginLoc) |
| 5718 | return CXChildVisit_Break; |
| 5719 | } |
| 5720 | } |
| 5721 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5722 | // If our current best cursor is the construction of a temporary object, |
| 5723 | // don't replace that cursor with a type reference, because we want |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5724 | // clang_getCursor() to point at the constructor. |
| 5725 | if (clang_isExpression(BestCursor->kind) && |
| 5726 | isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && |
| 5727 | cursor.kind == CXCursor_TypeRef) { |
| 5728 | // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it |
| 5729 | // as having the actual point on the type reference. |
| 5730 | *BestCursor = getTypeRefedCallExprCursor(*BestCursor); |
| 5731 | return CXChildVisit_Recurse; |
| 5732 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 5733 | |
| 5734 | // If we already have an Objective-C superclass reference, don't |
| 5735 | // update it further. |
| 5736 | if (BestCursor->kind == CXCursor_ObjCSuperClassRef) |
| 5737 | return CXChildVisit_Break; |
| 5738 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5739 | *BestCursor = cursor; |
| 5740 | return CXChildVisit_Recurse; |
| 5741 | } |
| 5742 | |
| 5743 | CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 5744 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 5745 | LOG_BAD_TU(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5746 | return clang_getNullCursor(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 5747 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5748 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 5749 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5750 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 5751 | |
| 5752 | SourceLocation SLoc = cxloc::translateSourceLocation(Loc); |
| 5753 | CXCursor Result = cxcursor::getCursor(TU, SLoc); |
| 5754 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5755 | LOG_FUNC_SECTION { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5756 | CXFile SearchFile; |
| 5757 | unsigned SearchLine, SearchColumn; |
| 5758 | CXFile ResultFile; |
| 5759 | unsigned ResultLine, ResultColumn; |
| 5760 | CXString SearchFileName, ResultFileName, KindSpelling, USR; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5761 | const char *IsDef = clang_isCursorDefinition(Result) ? " (Definition)" : ""; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5762 | CXSourceLocation ResultLoc = clang_getCursorLocation(Result); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5763 | |
| 5764 | clang_getFileLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, |
| 5765 | nullptr); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5766 | clang_getFileLocation(ResultLoc, &ResultFile, &ResultLine, &ResultColumn, |
| 5767 | nullptr); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5768 | SearchFileName = clang_getFileName(SearchFile); |
| 5769 | ResultFileName = clang_getFileName(ResultFile); |
| 5770 | KindSpelling = clang_getCursorKindSpelling(Result.kind); |
| 5771 | USR = clang_getCursorUSR(Result); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5772 | *Log << llvm::format("(%s:%d:%d) = %s", clang_getCString(SearchFileName), |
| 5773 | SearchLine, SearchColumn, |
| 5774 | clang_getCString(KindSpelling)) |
| 5775 | << llvm::format("(%s:%d:%d):%s%s", clang_getCString(ResultFileName), |
| 5776 | ResultLine, ResultColumn, clang_getCString(USR), |
| 5777 | IsDef); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5778 | clang_disposeString(SearchFileName); |
| 5779 | clang_disposeString(ResultFileName); |
| 5780 | clang_disposeString(KindSpelling); |
| 5781 | clang_disposeString(USR); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5782 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5783 | CXCursor Definition = clang_getCursorDefinition(Result); |
| 5784 | if (!clang_equalCursors(Definition, clang_getNullCursor())) { |
| 5785 | CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5786 | CXString DefinitionKindSpelling = |
| 5787 | clang_getCursorKindSpelling(Definition.kind); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5788 | CXFile DefinitionFile; |
| 5789 | unsigned DefinitionLine, DefinitionColumn; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5790 | clang_getFileLocation(DefinitionLoc, &DefinitionFile, &DefinitionLine, |
| 5791 | &DefinitionColumn, nullptr); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5792 | CXString DefinitionFileName = clang_getFileName(DefinitionFile); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5793 | *Log << llvm::format(" -> %s(%s:%d:%d)", |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5794 | clang_getCString(DefinitionKindSpelling), |
| 5795 | clang_getCString(DefinitionFileName), DefinitionLine, |
| 5796 | DefinitionColumn); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5797 | clang_disposeString(DefinitionFileName); |
| 5798 | clang_disposeString(DefinitionKindSpelling); |
| 5799 | } |
| 5800 | } |
| 5801 | |
| 5802 | return Result; |
| 5803 | } |
| 5804 | |
| 5805 | CXCursor clang_getNullCursor(void) { |
| 5806 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
| 5807 | } |
| 5808 | |
| 5809 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5810 | // Clear out the "FirstInDeclGroup" part in a declaration cursor, since we |
| 5811 | // can't set consistently. For example, when visiting a DeclStmt we will set |
| 5812 | // it but we don't set it on the result of clang_getCursorDefinition for |
| 5813 | // a reference of the same declaration. |
| 5814 | // FIXME: Setting "FirstInDeclGroup" in CXCursors is a hack that only works |
| 5815 | // when visiting a DeclStmt currently, the AST should be enhanced to be able |
| 5816 | // to provide that kind of info. |
| 5817 | if (clang_isDeclaration(X.kind)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5818 | X.data[1] = nullptr; |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5819 | if (clang_isDeclaration(Y.kind)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5820 | Y.data[1] = nullptr; |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5821 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5822 | return X == Y; |
| 5823 | } |
| 5824 | |
| 5825 | unsigned clang_hashCursor(CXCursor C) { |
| 5826 | unsigned Index = 0; |
| 5827 | if (clang_isExpression(C.kind) || clang_isStatement(C.kind)) |
| 5828 | Index = 1; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5829 | |
| 5830 | return llvm::DenseMapInfo<std::pair<unsigned, const void *>>::getHashValue( |
| 5831 | std::make_pair(C.kind, C.data[Index])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5832 | } |
| 5833 | |
| 5834 | unsigned clang_isInvalid(enum CXCursorKind K) { |
| 5835 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 5836 | } |
| 5837 | |
| 5838 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
| 5839 | return (K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl) || |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 5840 | (K >= CXCursor_FirstExtraDecl && K <= CXCursor_LastExtraDecl); |
| 5841 | } |
| 5842 | |
Ivan Donchevskii | 08ff910 | 2018-01-04 10:59:50 +0000 | [diff] [blame] | 5843 | unsigned clang_isInvalidDeclaration(CXCursor C) { |
| 5844 | if (clang_isDeclaration(C.kind)) { |
| 5845 | if (const Decl *D = getCursorDecl(C)) |
| 5846 | return D->isInvalidDecl(); |
| 5847 | } |
| 5848 | |
| 5849 | return 0; |
| 5850 | } |
| 5851 | |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 5852 | unsigned clang_isReference(enum CXCursorKind K) { |
| 5853 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 5854 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5855 | |
| 5856 | unsigned clang_isExpression(enum CXCursorKind K) { |
| 5857 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; |
| 5858 | } |
| 5859 | |
| 5860 | unsigned clang_isStatement(enum CXCursorKind K) { |
| 5861 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; |
| 5862 | } |
| 5863 | |
| 5864 | unsigned clang_isAttribute(enum CXCursorKind K) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5865 | return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5866 | } |
| 5867 | |
| 5868 | unsigned clang_isTranslationUnit(enum CXCursorKind K) { |
| 5869 | return K == CXCursor_TranslationUnit; |
| 5870 | } |
| 5871 | |
| 5872 | unsigned clang_isPreprocessing(enum CXCursorKind K) { |
| 5873 | return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; |
| 5874 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5875 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5876 | unsigned clang_isUnexposed(enum CXCursorKind K) { |
| 5877 | switch (K) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5878 | case CXCursor_UnexposedDecl: |
| 5879 | case CXCursor_UnexposedExpr: |
| 5880 | case CXCursor_UnexposedStmt: |
| 5881 | case CXCursor_UnexposedAttr: |
| 5882 | return true; |
| 5883 | default: |
| 5884 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5885 | } |
| 5886 | } |
| 5887 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5888 | CXCursorKind clang_getCursorKind(CXCursor C) { return C.kind; } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5889 | |
| 5890 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 5891 | if (clang_isReference(C.kind)) { |
| 5892 | switch (C.kind) { |
| 5893 | case CXCursor_ObjCSuperClassRef: { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5894 | std::pair<const ObjCInterfaceDecl *, SourceLocation> P = |
| 5895 | getCursorObjCSuperClassRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5896 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5897 | } |
| 5898 | |
| 5899 | case CXCursor_ObjCProtocolRef: { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5900 | std::pair<const ObjCProtocolDecl *, SourceLocation> P = |
| 5901 | getCursorObjCProtocolRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5902 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5903 | } |
| 5904 | |
| 5905 | case CXCursor_ObjCClassRef: { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5906 | std::pair<const ObjCInterfaceDecl *, SourceLocation> P = |
| 5907 | getCursorObjCClassRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5908 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5909 | } |
| 5910 | |
| 5911 | case CXCursor_TypeRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5912 | std::pair<const TypeDecl *, SourceLocation> P = getCursorTypeRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5913 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5914 | } |
| 5915 | |
| 5916 | case CXCursor_TemplateRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5917 | std::pair<const TemplateDecl *, SourceLocation> P = |
| 5918 | getCursorTemplateRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5919 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5920 | } |
| 5921 | |
| 5922 | case CXCursor_NamespaceRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5923 | std::pair<const NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5924 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5925 | } |
| 5926 | |
| 5927 | case CXCursor_MemberRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5928 | std::pair<const FieldDecl *, SourceLocation> P = getCursorMemberRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5929 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5930 | } |
| 5931 | |
| 5932 | case CXCursor_VariableRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5933 | std::pair<const VarDecl *, SourceLocation> P = getCursorVariableRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5934 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5935 | } |
| 5936 | |
| 5937 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5938 | const CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5939 | if (!BaseSpec) |
| 5940 | return clang_getNullLocation(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5941 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5942 | if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5943 | return cxloc::translateSourceLocation( |
| 5944 | getCursorContext(C), TSInfo->getTypeLoc().getBeginLoc()); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5945 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5946 | return cxloc::translateSourceLocation(getCursorContext(C), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5947 | BaseSpec->getBeginLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5948 | } |
| 5949 | |
| 5950 | case CXCursor_LabelRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5951 | std::pair<const LabelStmt *, SourceLocation> P = getCursorLabelRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5952 | return cxloc::translateSourceLocation(getCursorContext(C), P.second); |
| 5953 | } |
| 5954 | |
| 5955 | case CXCursor_OverloadedDeclRef: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5956 | return cxloc::translateSourceLocation( |
| 5957 | getCursorContext(C), getCursorOverloadedDeclRef(C).second); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5958 | |
| 5959 | default: |
| 5960 | // FIXME: Need a way to enumerate all non-reference cases. |
| 5961 | llvm_unreachable("Missed a reference kind"); |
| 5962 | } |
| 5963 | } |
| 5964 | |
| 5965 | if (clang_isExpression(C.kind)) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5966 | return cxloc::translateSourceLocation( |
| 5967 | getCursorContext(C), getLocationFromExpr(getCursorExpr(C))); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5968 | |
| 5969 | if (clang_isStatement(C.kind)) |
| 5970 | return cxloc::translateSourceLocation(getCursorContext(C), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5971 | getCursorStmt(C)->getBeginLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5972 | |
| 5973 | if (C.kind == CXCursor_PreprocessingDirective) { |
| 5974 | SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); |
| 5975 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5976 | } |
| 5977 | |
| 5978 | if (C.kind == CXCursor_MacroExpansion) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5979 | SourceLocation L = |
| 5980 | cxcursor::getCursorMacroExpansion(C).getSourceRange().getBegin(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5981 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5982 | } |
| 5983 | |
| 5984 | if (C.kind == CXCursor_MacroDefinition) { |
| 5985 | SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); |
| 5986 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5987 | } |
| 5988 | |
| 5989 | if (C.kind == CXCursor_InclusionDirective) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5990 | SourceLocation L = |
| 5991 | cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5992 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5993 | } |
| 5994 | |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 5995 | if (clang_isAttribute(C.kind)) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 5996 | SourceLocation L = cxcursor::getCursorAttr(C)->getLocation(); |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 5997 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5998 | } |
| 5999 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6000 | if (!clang_isDeclaration(C.kind)) |
| 6001 | return clang_getNullLocation(); |
| 6002 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6003 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6004 | if (!D) |
| 6005 | return clang_getNullLocation(); |
| 6006 | |
| 6007 | SourceLocation Loc = D->getLocation(); |
| 6008 | // FIXME: Multiple variables declared in a single declaration |
| 6009 | // currently lack the information needed to correctly determine their |
| 6010 | // ranges when accounting for the type-specifier. We use context |
| 6011 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 6012 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6013 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6014 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 6015 | Loc = VD->getLocation(); |
| 6016 | } |
| 6017 | |
| 6018 | // For ObjC methods, give the start location of the method name. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6019 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6020 | Loc = MD->getSelectorStartLoc(); |
| 6021 | |
| 6022 | return cxloc::translateSourceLocation(getCursorContext(C), Loc); |
| 6023 | } |
| 6024 | |
NAKAMURA Takumi | a01f4c3 | 2016-12-19 16:50:43 +0000 | [diff] [blame] | 6025 | } // end extern "C" |
| 6026 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6027 | CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) { |
| 6028 | assert(TU); |
| 6029 | |
| 6030 | // Guard against an invalid SourceLocation, or we may assert in one |
| 6031 | // of the following calls. |
| 6032 | if (SLoc.isInvalid()) |
| 6033 | return clang_getNullCursor(); |
| 6034 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6035 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6036 | |
| 6037 | // Translate the given source location to make it point at the beginning of |
| 6038 | // the token under the cursor. |
| 6039 | SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), |
| 6040 | CXXUnit->getASTContext().getLangOpts()); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6041 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6042 | CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 6043 | if (SLoc.isValid()) { |
| 6044 | GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result); |
| 6045 | CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData, |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6046 | /*VisitPreprocessorLast=*/true, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6047 | /*VisitIncludedEntities=*/false, |
| 6048 | SourceLocation(SLoc)); |
| 6049 | CursorVis.visitFileRegion(); |
| 6050 | } |
| 6051 | |
| 6052 | return Result; |
| 6053 | } |
| 6054 | |
| 6055 | static SourceRange getRawCursorExtent(CXCursor C) { |
| 6056 | if (clang_isReference(C.kind)) { |
| 6057 | switch (C.kind) { |
| 6058 | case CXCursor_ObjCSuperClassRef: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6059 | return getCursorObjCSuperClassRef(C).second; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6060 | |
| 6061 | case CXCursor_ObjCProtocolRef: |
| 6062 | return getCursorObjCProtocolRef(C).second; |
| 6063 | |
| 6064 | case CXCursor_ObjCClassRef: |
| 6065 | return getCursorObjCClassRef(C).second; |
| 6066 | |
| 6067 | case CXCursor_TypeRef: |
| 6068 | return getCursorTypeRef(C).second; |
| 6069 | |
| 6070 | case CXCursor_TemplateRef: |
| 6071 | return getCursorTemplateRef(C).second; |
| 6072 | |
| 6073 | case CXCursor_NamespaceRef: |
| 6074 | return getCursorNamespaceRef(C).second; |
| 6075 | |
| 6076 | case CXCursor_MemberRef: |
| 6077 | return getCursorMemberRef(C).second; |
| 6078 | |
| 6079 | case CXCursor_CXXBaseSpecifier: |
| 6080 | return getCursorCXXBaseSpecifier(C)->getSourceRange(); |
| 6081 | |
| 6082 | case CXCursor_LabelRef: |
| 6083 | return getCursorLabelRef(C).second; |
| 6084 | |
| 6085 | case CXCursor_OverloadedDeclRef: |
| 6086 | return getCursorOverloadedDeclRef(C).second; |
| 6087 | |
| 6088 | case CXCursor_VariableRef: |
| 6089 | return getCursorVariableRef(C).second; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6090 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6091 | default: |
| 6092 | // FIXME: Need a way to enumerate all non-reference cases. |
| 6093 | llvm_unreachable("Missed a reference kind"); |
| 6094 | } |
| 6095 | } |
| 6096 | |
| 6097 | if (clang_isExpression(C.kind)) |
| 6098 | return getCursorExpr(C)->getSourceRange(); |
| 6099 | |
| 6100 | if (clang_isStatement(C.kind)) |
| 6101 | return getCursorStmt(C)->getSourceRange(); |
| 6102 | |
| 6103 | if (clang_isAttribute(C.kind)) |
| 6104 | return getCursorAttr(C)->getRange(); |
| 6105 | |
| 6106 | if (C.kind == CXCursor_PreprocessingDirective) |
| 6107 | return cxcursor::getCursorPreprocessingDirective(C); |
| 6108 | |
| 6109 | if (C.kind == CXCursor_MacroExpansion) { |
| 6110 | ASTUnit *TU = getCursorASTUnit(C); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 6111 | SourceRange Range = cxcursor::getCursorMacroExpansion(C).getSourceRange(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6112 | return TU->mapRangeFromPreamble(Range); |
| 6113 | } |
| 6114 | |
| 6115 | if (C.kind == CXCursor_MacroDefinition) { |
| 6116 | ASTUnit *TU = getCursorASTUnit(C); |
| 6117 | SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange(); |
| 6118 | return TU->mapRangeFromPreamble(Range); |
| 6119 | } |
| 6120 | |
| 6121 | if (C.kind == CXCursor_InclusionDirective) { |
| 6122 | ASTUnit *TU = getCursorASTUnit(C); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6123 | SourceRange Range = |
| 6124 | cxcursor::getCursorInclusionDirective(C)->getSourceRange(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6125 | return TU->mapRangeFromPreamble(Range); |
| 6126 | } |
| 6127 | |
| 6128 | if (C.kind == CXCursor_TranslationUnit) { |
| 6129 | ASTUnit *TU = getCursorASTUnit(C); |
| 6130 | FileID MainID = TU->getSourceManager().getMainFileID(); |
| 6131 | SourceLocation Start = TU->getSourceManager().getLocForStartOfFile(MainID); |
| 6132 | SourceLocation End = TU->getSourceManager().getLocForEndOfFile(MainID); |
| 6133 | return SourceRange(Start, End); |
| 6134 | } |
| 6135 | |
| 6136 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6137 | const Decl *D = cxcursor::getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6138 | if (!D) |
| 6139 | return SourceRange(); |
| 6140 | |
| 6141 | SourceRange R = D->getSourceRange(); |
| 6142 | // FIXME: Multiple variables declared in a single declaration |
| 6143 | // currently lack the information needed to correctly determine their |
| 6144 | // ranges when accounting for the type-specifier. We use context |
| 6145 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 6146 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6147 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6148 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 6149 | R.setBegin(VD->getLocation()); |
| 6150 | } |
| 6151 | return R; |
| 6152 | } |
| 6153 | return SourceRange(); |
| 6154 | } |
| 6155 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6156 | /// Retrieves the "raw" cursor extent, which is then extended to include |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6157 | /// the decl-specifier-seq for declarations. |
| 6158 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) { |
| 6159 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6160 | const Decl *D = cxcursor::getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6161 | if (!D) |
| 6162 | return SourceRange(); |
| 6163 | |
| 6164 | SourceRange R = D->getSourceRange(); |
| 6165 | |
| 6166 | // Adjust the start of the location for declarations preceded by |
| 6167 | // declaration specifiers. |
| 6168 | SourceLocation StartLoc; |
| 6169 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
| 6170 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6171 | StartLoc = TI->getTypeLoc().getBeginLoc(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6172 | } else if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6173 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6174 | StartLoc = TI->getTypeLoc().getBeginLoc(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6175 | } |
| 6176 | |
| 6177 | if (StartLoc.isValid() && R.getBegin().isValid() && |
| 6178 | SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin())) |
| 6179 | R.setBegin(StartLoc); |
| 6180 | |
| 6181 | // FIXME: Multiple variables declared in a single declaration |
| 6182 | // currently lack the information needed to correctly determine their |
| 6183 | // ranges when accounting for the type-specifier. We use context |
| 6184 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 6185 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6186 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6187 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 6188 | R.setBegin(VD->getLocation()); |
| 6189 | } |
| 6190 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6191 | return R; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6192 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6193 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6194 | return getRawCursorExtent(C); |
| 6195 | } |
| 6196 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6197 | CXSourceRange clang_getCursorExtent(CXCursor C) { |
| 6198 | SourceRange R = getRawCursorExtent(C); |
| 6199 | if (R.isInvalid()) |
| 6200 | return clang_getNullRange(); |
| 6201 | |
| 6202 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 6203 | } |
| 6204 | |
| 6205 | CXCursor clang_getCursorReferenced(CXCursor C) { |
| 6206 | if (clang_isInvalid(C.kind)) |
| 6207 | return clang_getNullCursor(); |
| 6208 | |
| 6209 | CXTranslationUnit tu = getCursorTU(C); |
| 6210 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6211 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6212 | if (!D) |
| 6213 | return clang_getNullCursor(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6214 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6215 | return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6216 | if (const ObjCPropertyImplDecl *PropImpl = |
| 6217 | dyn_cast<ObjCPropertyImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6218 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 6219 | return MakeCXCursor(Property, tu); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6220 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6221 | return C; |
| 6222 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6223 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6224 | if (clang_isExpression(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6225 | const Expr *E = getCursorExpr(C); |
| 6226 | const Decl *D = getDeclFromExpr(E); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6227 | if (D) { |
| 6228 | CXCursor declCursor = MakeCXCursor(D, tu); |
| 6229 | declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C), |
| 6230 | declCursor); |
| 6231 | return declCursor; |
| 6232 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6233 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6234 | if (const OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6235 | return MakeCursorOverloadedDeclRef(Ovl, tu); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6236 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6237 | return clang_getNullCursor(); |
| 6238 | } |
| 6239 | |
| 6240 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6241 | const Stmt *S = getCursorStmt(C); |
| 6242 | if (const GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6243 | if (LabelDecl *label = Goto->getLabel()) |
| 6244 | if (LabelStmt *labelS = label->getStmt()) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6245 | return MakeCXCursor(labelS, getCursorDecl(C), tu); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6246 | |
| 6247 | return clang_getNullCursor(); |
| 6248 | } |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 6249 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6250 | if (C.kind == CXCursor_MacroExpansion) { |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 6251 | if (const MacroDefinitionRecord *Def = |
| 6252 | getCursorMacroExpansion(C).getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6253 | return MakeMacroDefinitionCursor(Def, tu); |
| 6254 | } |
| 6255 | |
| 6256 | if (!clang_isReference(C.kind)) |
| 6257 | return clang_getNullCursor(); |
| 6258 | |
| 6259 | switch (C.kind) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6260 | case CXCursor_ObjCSuperClassRef: |
| 6261 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6262 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6263 | case CXCursor_ObjCProtocolRef: { |
| 6264 | const ObjCProtocolDecl *Prot = getCursorObjCProtocolRef(C).first; |
| 6265 | if (const ObjCProtocolDecl *Def = Prot->getDefinition()) |
| 6266 | return MakeCXCursor(Def, tu); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6267 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6268 | return MakeCXCursor(Prot, tu); |
| 6269 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6270 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6271 | case CXCursor_ObjCClassRef: { |
| 6272 | const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
| 6273 | if (const ObjCInterfaceDecl *Def = Class->getDefinition()) |
| 6274 | return MakeCXCursor(Def, tu); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6275 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6276 | return MakeCXCursor(Class, tu); |
| 6277 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6278 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6279 | case CXCursor_TypeRef: |
| 6280 | return MakeCXCursor(getCursorTypeRef(C).first, tu); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6281 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6282 | case CXCursor_TemplateRef: |
| 6283 | return MakeCXCursor(getCursorTemplateRef(C).first, tu); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6284 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6285 | case CXCursor_NamespaceRef: |
| 6286 | return MakeCXCursor(getCursorNamespaceRef(C).first, tu); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6287 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6288 | case CXCursor_MemberRef: |
| 6289 | return MakeCXCursor(getCursorMemberRef(C).first, tu); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6290 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6291 | case CXCursor_CXXBaseSpecifier: { |
| 6292 | const CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); |
| 6293 | return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), tu)); |
| 6294 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6295 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6296 | case CXCursor_LabelRef: |
| 6297 | // FIXME: We end up faking the "parent" declaration here because we |
| 6298 | // don't want to make CXCursor larger. |
| 6299 | return MakeCXCursor( |
| 6300 | getCursorLabelRef(C).first, |
| 6301 | cxtu::getASTUnit(tu)->getASTContext().getTranslationUnitDecl(), tu); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6302 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6303 | case CXCursor_OverloadedDeclRef: |
| 6304 | return C; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6305 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6306 | case CXCursor_VariableRef: |
| 6307 | return MakeCXCursor(getCursorVariableRef(C).first, tu); |
| 6308 | |
| 6309 | default: |
| 6310 | // We would prefer to enumerate all non-reference cursor kinds here. |
| 6311 | llvm_unreachable("Unhandled reference cursor kind"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6312 | } |
| 6313 | } |
| 6314 | |
| 6315 | CXCursor clang_getCursorDefinition(CXCursor C) { |
| 6316 | if (clang_isInvalid(C.kind)) |
| 6317 | return clang_getNullCursor(); |
| 6318 | |
| 6319 | CXTranslationUnit TU = getCursorTU(C); |
| 6320 | |
| 6321 | bool WasReference = false; |
| 6322 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { |
| 6323 | C = clang_getCursorReferenced(C); |
| 6324 | WasReference = true; |
| 6325 | } |
| 6326 | |
| 6327 | if (C.kind == CXCursor_MacroExpansion) |
| 6328 | return clang_getCursorReferenced(C); |
| 6329 | |
| 6330 | if (!clang_isDeclaration(C.kind)) |
| 6331 | return clang_getNullCursor(); |
| 6332 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6333 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6334 | if (!D) |
| 6335 | return clang_getNullCursor(); |
| 6336 | |
| 6337 | switch (D->getKind()) { |
| 6338 | // Declaration kinds that don't really separate the notions of |
| 6339 | // declaration and definition. |
| 6340 | case Decl::Namespace: |
| 6341 | case Decl::Typedef: |
| 6342 | case Decl::TypeAlias: |
| 6343 | case Decl::TypeAliasTemplate: |
| 6344 | case Decl::TemplateTypeParm: |
| 6345 | case Decl::EnumConstant: |
| 6346 | case Decl::Field: |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 6347 | case Decl::Binding: |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 6348 | case Decl::MSProperty: |
Richard Smith | bab6df8 | 2020-04-11 22:15:29 -0700 | [diff] [blame] | 6349 | case Decl::MSGuid: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6350 | case Decl::IndirectField: |
| 6351 | case Decl::ObjCIvar: |
| 6352 | case Decl::ObjCAtDefsField: |
| 6353 | case Decl::ImplicitParam: |
| 6354 | case Decl::ParmVar: |
| 6355 | case Decl::NonTypeTemplateParm: |
| 6356 | case Decl::TemplateTemplateParm: |
| 6357 | case Decl::ObjCCategoryImpl: |
| 6358 | case Decl::ObjCImplementation: |
| 6359 | case Decl::AccessSpec: |
| 6360 | case Decl::LinkageSpec: |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 6361 | case Decl::Export: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6362 | case Decl::ObjCPropertyImpl: |
| 6363 | case Decl::FileScopeAsm: |
| 6364 | case Decl::StaticAssert: |
| 6365 | case Decl::Block: |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 6366 | case Decl::Captured: |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 6367 | case Decl::OMPCapturedExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6368 | case Decl::Label: // FIXME: Is this right?? |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6369 | case Decl::ClassScopeFunctionSpecialization: |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 6370 | case Decl::CXXDeductionGuide: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6371 | case Decl::Import: |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 6372 | case Decl::OMPThreadPrivate: |
Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 6373 | case Decl::OMPAllocate: |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 6374 | case Decl::OMPDeclareReduction: |
Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 6375 | case Decl::OMPDeclareMapper: |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 6376 | case Decl::OMPRequires: |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 6377 | case Decl::ObjCTypeParam: |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 6378 | case Decl::BuiltinTemplate: |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 6379 | case Decl::PragmaComment: |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 6380 | case Decl::PragmaDetectMismatch: |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 6381 | case Decl::UsingPack: |
Saar Raz | d7aae33 | 2019-07-10 21:25:49 +0000 | [diff] [blame] | 6382 | case Decl::Concept: |
Tyker | b0561b3 | 2019-11-17 11:41:55 +0100 | [diff] [blame] | 6383 | case Decl::LifetimeExtendedTemporary: |
Saar Raz | a0f50d7 | 2020-01-18 09:11:43 +0200 | [diff] [blame] | 6384 | case Decl::RequiresExprBody: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6385 | return C; |
| 6386 | |
| 6387 | // Declaration kinds that don't make any sense here, but are |
| 6388 | // nonetheless harmless. |
David Blaikie | f005d3c | 2013-02-22 17:44:58 +0000 | [diff] [blame] | 6389 | case Decl::Empty: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6390 | case Decl::TranslationUnit: |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 6391 | case Decl::ExternCContext: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6392 | break; |
| 6393 | |
| 6394 | // Declaration kinds for which the definition is not resolvable. |
| 6395 | case Decl::UnresolvedUsingTypename: |
| 6396 | case Decl::UnresolvedUsingValue: |
| 6397 | break; |
| 6398 | |
| 6399 | case Decl::UsingDirective: |
| 6400 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), |
| 6401 | TU); |
| 6402 | |
| 6403 | case Decl::NamespaceAlias: |
| 6404 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU); |
| 6405 | |
| 6406 | case Decl::Enum: |
| 6407 | case Decl::Record: |
| 6408 | case Decl::CXXRecord: |
| 6409 | case Decl::ClassTemplateSpecialization: |
| 6410 | case Decl::ClassTemplatePartialSpecialization: |
| 6411 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) |
| 6412 | return MakeCXCursor(Def, TU); |
| 6413 | return clang_getNullCursor(); |
| 6414 | |
| 6415 | case Decl::Function: |
| 6416 | case Decl::CXXMethod: |
| 6417 | case Decl::CXXConstructor: |
| 6418 | case Decl::CXXDestructor: |
| 6419 | case Decl::CXXConversion: { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6420 | const FunctionDecl *Def = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6421 | if (cast<FunctionDecl>(D)->getBody(Def)) |
Dmitri Gribenko | 9c256e3 | 2013-01-14 00:46:27 +0000 | [diff] [blame] | 6422 | return MakeCXCursor(Def, TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6423 | return clang_getNullCursor(); |
| 6424 | } |
| 6425 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6426 | case Decl::Var: |
| 6427 | case Decl::VarTemplateSpecialization: |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 6428 | case Decl::VarTemplatePartialSpecialization: |
| 6429 | case Decl::Decomposition: { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6430 | // Ask the variable if it has a definition. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6431 | if (const VarDecl *Def = cast<VarDecl>(D)->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6432 | return MakeCXCursor(Def, TU); |
| 6433 | return clang_getNullCursor(); |
| 6434 | } |
| 6435 | |
| 6436 | case Decl::FunctionTemplate: { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6437 | const FunctionDecl *Def = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6438 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) |
| 6439 | return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU); |
| 6440 | return clang_getNullCursor(); |
| 6441 | } |
| 6442 | |
| 6443 | case Decl::ClassTemplate: { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6444 | if (RecordDecl *Def = |
| 6445 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6446 | return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), |
| 6447 | TU); |
| 6448 | return clang_getNullCursor(); |
| 6449 | } |
| 6450 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6451 | case Decl::VarTemplate: { |
| 6452 | if (VarDecl *Def = |
| 6453 | cast<VarTemplateDecl>(D)->getTemplatedDecl()->getDefinition()) |
| 6454 | return MakeCXCursor(cast<VarDecl>(Def)->getDescribedVarTemplate(), TU); |
| 6455 | return clang_getNullCursor(); |
| 6456 | } |
| 6457 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6458 | case Decl::Using: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6459 | return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), D->getLocation(), |
| 6460 | TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6461 | |
| 6462 | case Decl::UsingShadow: |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6463 | case Decl::ConstructorUsingShadow: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6464 | return clang_getCursorDefinition( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6465 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6466 | |
| 6467 | case Decl::ObjCMethod: { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6468 | const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6469 | if (Method->isThisDeclarationADefinition()) |
| 6470 | return C; |
| 6471 | |
| 6472 | // Dig out the method definition in the associated |
| 6473 | // @implementation, if we have it. |
| 6474 | // FIXME: The ASTs should make finding the definition easier. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6475 | if (const ObjCInterfaceDecl *Class = |
| 6476 | dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6477 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6478 | if (ObjCMethodDecl *Def = ClassImpl->getMethod( |
| 6479 | Method->getSelector(), Method->isInstanceMethod())) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6480 | if (Def->isThisDeclarationADefinition()) |
| 6481 | return MakeCXCursor(Def, TU); |
| 6482 | |
| 6483 | return clang_getNullCursor(); |
| 6484 | } |
| 6485 | |
| 6486 | case Decl::ObjCCategory: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6487 | if (ObjCCategoryImplDecl *Impl = |
| 6488 | cast<ObjCCategoryDecl>(D)->getImplementation()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6489 | return MakeCXCursor(Impl, TU); |
| 6490 | return clang_getNullCursor(); |
| 6491 | |
| 6492 | case Decl::ObjCProtocol: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6493 | if (const ObjCProtocolDecl *Def = |
| 6494 | cast<ObjCProtocolDecl>(D)->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6495 | return MakeCXCursor(Def, TU); |
| 6496 | return clang_getNullCursor(); |
| 6497 | |
| 6498 | case Decl::ObjCInterface: { |
| 6499 | // There are two notions of a "definition" for an Objective-C |
| 6500 | // class: the interface and its implementation. When we resolved a |
| 6501 | // reference to an Objective-C class, produce the @interface as |
| 6502 | // the definition; when we were provided with the interface, |
| 6503 | // produce the @implementation as the definition. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6504 | const ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6505 | if (WasReference) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6506 | if (const ObjCInterfaceDecl *Def = IFace->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6507 | return MakeCXCursor(Def, TU); |
| 6508 | } else if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
| 6509 | return MakeCXCursor(Impl, TU); |
| 6510 | return clang_getNullCursor(); |
| 6511 | } |
| 6512 | |
| 6513 | case Decl::ObjCProperty: |
| 6514 | // FIXME: We don't really know where to find the |
| 6515 | // ObjCPropertyImplDecls that implement this property. |
| 6516 | return clang_getNullCursor(); |
| 6517 | |
| 6518 | case Decl::ObjCCompatibleAlias: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6519 | if (const ObjCInterfaceDecl *Class = |
| 6520 | cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6521 | if (const ObjCInterfaceDecl *Def = Class->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6522 | return MakeCXCursor(Def, TU); |
| 6523 | |
| 6524 | return clang_getNullCursor(); |
| 6525 | |
| 6526 | case Decl::Friend: |
| 6527 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) |
| 6528 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
| 6529 | return clang_getNullCursor(); |
| 6530 | |
| 6531 | case Decl::FriendTemplate: |
| 6532 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) |
| 6533 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
| 6534 | return clang_getNullCursor(); |
| 6535 | } |
| 6536 | |
| 6537 | return clang_getNullCursor(); |
| 6538 | } |
| 6539 | |
| 6540 | unsigned clang_isCursorDefinition(CXCursor C) { |
| 6541 | if (!clang_isDeclaration(C.kind)) |
| 6542 | return 0; |
| 6543 | |
| 6544 | return clang_getCursorDefinition(C) == C; |
| 6545 | } |
| 6546 | |
| 6547 | CXCursor clang_getCanonicalCursor(CXCursor C) { |
| 6548 | if (!clang_isDeclaration(C.kind)) |
| 6549 | return C; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6550 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6551 | if (const Decl *D = getCursorDecl(C)) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6552 | if (const ObjCCategoryImplDecl *CatImplD = |
| 6553 | dyn_cast<ObjCCategoryImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6554 | if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl()) |
| 6555 | return MakeCXCursor(CatD, getCursorTU(C)); |
| 6556 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6557 | if (const ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) |
| 6558 | if (const ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6559 | return MakeCXCursor(IFD, getCursorTU(C)); |
| 6560 | |
| 6561 | return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C)); |
| 6562 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6563 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6564 | return C; |
| 6565 | } |
| 6566 | |
| 6567 | int clang_Cursor_getObjCSelectorIndex(CXCursor cursor) { |
| 6568 | return cxcursor::getSelectorIdentifierIndexAndLoc(cursor).first; |
| 6569 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6570 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6571 | unsigned clang_getNumOverloadedDecls(CXCursor C) { |
| 6572 | if (C.kind != CXCursor_OverloadedDeclRef) |
| 6573 | return 0; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6574 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6575 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6576 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6577 | return E->getNumDecls(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6578 | |
| 6579 | if (OverloadedTemplateStorage *S = |
| 6580 | Storage.dyn_cast<OverloadedTemplateStorage *>()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6581 | return S->size(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6582 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6583 | const Decl *D = Storage.get<const Decl *>(); |
| 6584 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6585 | return Using->shadow_size(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6586 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6587 | return 0; |
| 6588 | } |
| 6589 | |
| 6590 | CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { |
| 6591 | if (cursor.kind != CXCursor_OverloadedDeclRef) |
| 6592 | return clang_getNullCursor(); |
| 6593 | |
| 6594 | if (index >= clang_getNumOverloadedDecls(cursor)) |
| 6595 | return clang_getNullCursor(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6596 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6597 | CXTranslationUnit TU = getCursorTU(cursor); |
| 6598 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6599 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6600 | return MakeCXCursor(E->decls_begin()[index], TU); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6601 | |
| 6602 | if (OverloadedTemplateStorage *S = |
| 6603 | Storage.dyn_cast<OverloadedTemplateStorage *>()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6604 | return MakeCXCursor(S->begin()[index], TU); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6605 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6606 | const Decl *D = Storage.get<const Decl *>(); |
| 6607 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6608 | // FIXME: This is, unfortunately, linear time. |
| 6609 | UsingDecl::shadow_iterator Pos = Using->shadow_begin(); |
| 6610 | std::advance(Pos, index); |
| 6611 | return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU); |
| 6612 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6613 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6614 | return clang_getNullCursor(); |
| 6615 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6616 | |
| 6617 | void clang_getDefinitionSpellingAndExtent( |
| 6618 | CXCursor C, const char **startBuf, const char **endBuf, unsigned *startLine, |
| 6619 | unsigned *startColumn, unsigned *endLine, unsigned *endColumn) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6620 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6621 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(getCursorDecl(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6622 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
| 6623 | |
| 6624 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 6625 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 6626 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 6627 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 6628 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 6629 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 6630 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 6631 | } |
| 6632 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6633 | CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags, |
| 6634 | unsigned PieceIndex) { |
| 6635 | RefNamePieces Pieces; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6636 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6637 | switch (C.kind) { |
| 6638 | case CXCursor_MemberRefExpr: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6639 | if (const MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6640 | Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(), |
| 6641 | E->getQualifierLoc().getSourceRange()); |
| 6642 | break; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6643 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6644 | case CXCursor_DeclRefExpr: |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 6645 | if (const DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C))) { |
| 6646 | SourceRange TemplateArgLoc(E->getLAngleLoc(), E->getRAngleLoc()); |
| 6647 | Pieces = |
| 6648 | buildPieces(NameFlags, false, E->getNameInfo(), |
| 6649 | E->getQualifierLoc().getSourceRange(), &TemplateArgLoc); |
| 6650 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6651 | break; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6652 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6653 | case CXCursor_CallExpr: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6654 | if (const CXXOperatorCallExpr *OCE = |
| 6655 | dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6656 | const Expr *Callee = OCE->getCallee(); |
| 6657 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6658 | Callee = ICE->getSubExpr(); |
| 6659 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6660 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6661 | Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(), |
| 6662 | DRE->getQualifierLoc().getSourceRange()); |
| 6663 | } |
| 6664 | break; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6665 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6666 | default: |
| 6667 | break; |
| 6668 | } |
| 6669 | |
| 6670 | if (Pieces.empty()) { |
| 6671 | if (PieceIndex == 0) |
| 6672 | return clang_getCursorExtent(C); |
| 6673 | } else if (PieceIndex < Pieces.size()) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6674 | SourceRange R = Pieces[PieceIndex]; |
| 6675 | if (R.isValid()) |
| 6676 | return cxloc::translateSourceRange(getCursorContext(C), R); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6677 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6678 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6679 | return clang_getNullRange(); |
| 6680 | } |
| 6681 | |
| 6682 | void clang_enableStackTraces(void) { |
Richard Smith | dfed58a | 2016-06-09 00:53:41 +0000 | [diff] [blame] | 6683 | // FIXME: Provide an argv0 here so we can find llvm-symbolizer. |
| 6684 | llvm::sys::PrintStackTraceOnErrorSignal(StringRef()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6685 | } |
| 6686 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6687 | void clang_executeOnThread(void (*fn)(void *), void *user_data, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6688 | unsigned stack_size) { |
Alexandre Ganea | 471d060 | 2019-11-29 10:52:13 -0500 | [diff] [blame] | 6689 | llvm::llvm_execute_on_thread(fn, user_data, |
| 6690 | stack_size == 0 |
| 6691 | ? clang::DesiredStackSize |
| 6692 | : llvm::Optional<unsigned>(stack_size)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6693 | } |
| 6694 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6695 | //===----------------------------------------------------------------------===// |
| 6696 | // Token-based Operations. |
| 6697 | //===----------------------------------------------------------------------===// |
| 6698 | |
| 6699 | /* CXToken layout: |
| 6700 | * int_data[0]: a CXTokenKind |
| 6701 | * int_data[1]: starting token location |
| 6702 | * int_data[2]: token length |
| 6703 | * int_data[3]: reserved |
| 6704 | * ptr_data: for identifiers and keywords, an IdentifierInfo*. |
| 6705 | * otherwise unused. |
| 6706 | */ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6707 | CXTokenKind clang_getTokenKind(CXToken CXTok) { |
| 6708 | return static_cast<CXTokenKind>(CXTok.int_data[0]); |
| 6709 | } |
| 6710 | |
| 6711 | CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { |
| 6712 | switch (clang_getTokenKind(CXTok)) { |
| 6713 | case CXToken_Identifier: |
| 6714 | case CXToken_Keyword: |
| 6715 | // We know we have an IdentifierInfo*, so use that. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6716 | return cxstring::createRef( |
| 6717 | static_cast<IdentifierInfo *>(CXTok.ptr_data)->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6718 | |
| 6719 | case CXToken_Literal: { |
| 6720 | // We have stashed the starting pointer in the ptr_data field. Use it. |
| 6721 | const char *Text = static_cast<const char *>(CXTok.ptr_data); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 6722 | return cxstring::createDup(StringRef(Text, CXTok.int_data[2])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6723 | } |
| 6724 | |
| 6725 | case CXToken_Punctuation: |
| 6726 | case CXToken_Comment: |
| 6727 | break; |
| 6728 | } |
| 6729 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6730 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6731 | LOG_BAD_TU(TU); |
| 6732 | return cxstring::createEmpty(); |
| 6733 | } |
| 6734 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6735 | // We have to find the starting buffer pointer the hard way, by |
| 6736 | // deconstructing the source location. |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6737 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6738 | if (!CXXUnit) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 6739 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6740 | |
| 6741 | SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6742 | std::pair<FileID, unsigned> LocInfo = |
| 6743 | CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6744 | bool Invalid = false; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6745 | StringRef Buffer = |
| 6746 | CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6747 | if (Invalid) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 6748 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6749 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 6750 | return cxstring::createDup(Buffer.substr(LocInfo.second, CXTok.int_data[2])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6751 | } |
| 6752 | |
| 6753 | CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6754 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6755 | LOG_BAD_TU(TU); |
| 6756 | return clang_getNullLocation(); |
| 6757 | } |
| 6758 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6759 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6760 | if (!CXXUnit) |
| 6761 | return clang_getNullLocation(); |
| 6762 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6763 | return cxloc::translateSourceLocation( |
| 6764 | CXXUnit->getASTContext(), |
| 6765 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6766 | } |
| 6767 | |
| 6768 | CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6769 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6770 | LOG_BAD_TU(TU); |
| 6771 | return clang_getNullRange(); |
| 6772 | } |
| 6773 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6774 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6775 | if (!CXXUnit) |
| 6776 | return clang_getNullRange(); |
| 6777 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6778 | return cxloc::translateSourceRange( |
| 6779 | CXXUnit->getASTContext(), |
| 6780 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6781 | } |
| 6782 | |
| 6783 | static void getTokens(ASTUnit *CXXUnit, SourceRange Range, |
| 6784 | SmallVectorImpl<CXToken> &CXTokens) { |
| 6785 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6786 | std::pair<FileID, unsigned> BeginLocInfo = |
| 6787 | SourceMgr.getDecomposedSpellingLoc(Range.getBegin()); |
| 6788 | std::pair<FileID, unsigned> EndLocInfo = |
| 6789 | SourceMgr.getDecomposedSpellingLoc(Range.getEnd()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6790 | |
| 6791 | // Cannot tokenize across files. |
| 6792 | if (BeginLocInfo.first != EndLocInfo.first) |
| 6793 | return; |
| 6794 | |
| 6795 | // Create a lexer |
| 6796 | bool Invalid = false; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6797 | StringRef Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6798 | if (Invalid) |
| 6799 | return; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6800 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6801 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6802 | CXXUnit->getASTContext().getLangOpts(), Buffer.begin(), |
| 6803 | Buffer.data() + BeginLocInfo.second, Buffer.end()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6804 | Lex.SetCommentRetentionState(true); |
| 6805 | |
| 6806 | // Lex tokens until we hit the end of the range. |
| 6807 | const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; |
| 6808 | Token Tok; |
| 6809 | bool previousWasAt = false; |
| 6810 | do { |
| 6811 | // Lex the next token |
| 6812 | Lex.LexFromRawLexer(Tok); |
| 6813 | if (Tok.is(tok::eof)) |
| 6814 | break; |
| 6815 | |
| 6816 | // Initialize the CXToken. |
| 6817 | CXToken CXTok; |
| 6818 | |
| 6819 | // - Common fields |
| 6820 | CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); |
| 6821 | CXTok.int_data[2] = Tok.getLength(); |
| 6822 | CXTok.int_data[3] = 0; |
| 6823 | |
| 6824 | // - Kind-specific fields |
| 6825 | if (Tok.isLiteral()) { |
| 6826 | CXTok.int_data[0] = CXToken_Literal; |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 6827 | CXTok.ptr_data = const_cast<char *>(Tok.getLiteralData()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6828 | } else if (Tok.is(tok::raw_identifier)) { |
| 6829 | // Lookup the identifier to determine whether we have a keyword. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6830 | IdentifierInfo *II = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6831 | |
| 6832 | if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { |
| 6833 | CXTok.int_data[0] = CXToken_Keyword; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6834 | } else { |
| 6835 | CXTok.int_data[0] = |
| 6836 | Tok.is(tok::identifier) ? CXToken_Identifier : CXToken_Keyword; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6837 | } |
| 6838 | CXTok.ptr_data = II; |
| 6839 | } else if (Tok.is(tok::comment)) { |
| 6840 | CXTok.int_data[0] = CXToken_Comment; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6841 | CXTok.ptr_data = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6842 | } else { |
| 6843 | CXTok.int_data[0] = CXToken_Punctuation; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6844 | CXTok.ptr_data = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6845 | } |
| 6846 | CXTokens.push_back(CXTok); |
| 6847 | previousWasAt = Tok.is(tok::at); |
Argyrios Kyrtzidis | c7c6a07 | 2016-11-09 23:58:39 +0000 | [diff] [blame] | 6848 | } while (Lex.getBufferLocation() < EffectiveBufferEnd); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6849 | } |
| 6850 | |
Ivan Donchevskii | 3957e48 | 2018-06-13 12:37:08 +0000 | [diff] [blame] | 6851 | CXToken *clang_getToken(CXTranslationUnit TU, CXSourceLocation Location) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6852 | LOG_FUNC_SECTION { *Log << TU << ' ' << Location; } |
Ivan Donchevskii | 3957e48 | 2018-06-13 12:37:08 +0000 | [diff] [blame] | 6853 | |
| 6854 | if (isNotUsableTU(TU)) { |
| 6855 | LOG_BAD_TU(TU); |
| 6856 | return NULL; |
| 6857 | } |
| 6858 | |
| 6859 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
| 6860 | if (!CXXUnit) |
| 6861 | return NULL; |
| 6862 | |
| 6863 | SourceLocation Begin = cxloc::translateSourceLocation(Location); |
| 6864 | if (Begin.isInvalid()) |
| 6865 | return NULL; |
| 6866 | SourceManager &SM = CXXUnit->getSourceManager(); |
| 6867 | std::pair<FileID, unsigned> DecomposedEnd = SM.getDecomposedLoc(Begin); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6868 | DecomposedEnd.second += |
| 6869 | Lexer::MeasureTokenLength(Begin, SM, CXXUnit->getLangOpts()); |
Ivan Donchevskii | 3957e48 | 2018-06-13 12:37:08 +0000 | [diff] [blame] | 6870 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6871 | SourceLocation End = |
| 6872 | SM.getComposedLoc(DecomposedEnd.first, DecomposedEnd.second); |
Ivan Donchevskii | 3957e48 | 2018-06-13 12:37:08 +0000 | [diff] [blame] | 6873 | |
| 6874 | SmallVector<CXToken, 32> CXTokens; |
| 6875 | getTokens(CXXUnit, SourceRange(Begin, End), CXTokens); |
| 6876 | |
| 6877 | if (CXTokens.empty()) |
| 6878 | return NULL; |
| 6879 | |
| 6880 | CXTokens.resize(1); |
| 6881 | CXToken *Token = static_cast<CXToken *>(llvm::safe_malloc(sizeof(CXToken))); |
| 6882 | |
| 6883 | memmove(Token, CXTokens.data(), sizeof(CXToken)); |
| 6884 | return Token; |
| 6885 | } |
| 6886 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6887 | void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, CXToken **Tokens, |
| 6888 | unsigned *NumTokens) { |
| 6889 | LOG_FUNC_SECTION { *Log << TU << ' ' << Range; } |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 6890 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6891 | if (Tokens) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6892 | *Tokens = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6893 | if (NumTokens) |
| 6894 | *NumTokens = 0; |
| 6895 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6896 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6897 | LOG_BAD_TU(TU); |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 6898 | return; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6899 | } |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 6900 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6901 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6902 | if (!CXXUnit || !Tokens || !NumTokens) |
| 6903 | return; |
| 6904 | |
| 6905 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6906 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6907 | SourceRange R = cxloc::translateCXSourceRange(Range); |
| 6908 | if (R.isInvalid()) |
| 6909 | return; |
| 6910 | |
| 6911 | SmallVector<CXToken, 32> CXTokens; |
| 6912 | getTokens(CXXUnit, R, CXTokens); |
| 6913 | |
| 6914 | if (CXTokens.empty()) |
| 6915 | return; |
| 6916 | |
Serge Pavlov | 5252573 | 2018-02-21 02:02:39 +0000 | [diff] [blame] | 6917 | *Tokens = static_cast<CXToken *>( |
| 6918 | llvm::safe_malloc(sizeof(CXToken) * CXTokens.size())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6919 | memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); |
| 6920 | *NumTokens = CXTokens.size(); |
| 6921 | } |
| 6922 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6923 | void clang_disposeTokens(CXTranslationUnit TU, CXToken *Tokens, |
| 6924 | unsigned NumTokens) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6925 | free(Tokens); |
| 6926 | } |
| 6927 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6928 | //===----------------------------------------------------------------------===// |
| 6929 | // Token annotation APIs. |
| 6930 | //===----------------------------------------------------------------------===// |
| 6931 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6932 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 6933 | CXCursor parent, |
| 6934 | CXClientData client_data); |
| 6935 | static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor, |
| 6936 | CXClientData client_data); |
| 6937 | |
| 6938 | namespace { |
| 6939 | class AnnotateTokensWorker { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6940 | CXToken *Tokens; |
| 6941 | CXCursor *Cursors; |
| 6942 | unsigned NumTokens; |
| 6943 | unsigned TokIdx; |
| 6944 | unsigned PreprocessingTokIdx; |
| 6945 | CursorVisitor AnnotateVis; |
| 6946 | SourceManager &SrcMgr; |
| 6947 | bool HasContextSensitiveKeywords; |
| 6948 | |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 6949 | struct PostChildrenAction { |
| 6950 | CXCursor cursor; |
| 6951 | enum Action { Invalid, Ignore, Postpone } action; |
| 6952 | }; |
| 6953 | using PostChildrenActions = SmallVector<PostChildrenAction, 0>; |
| 6954 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6955 | struct PostChildrenInfo { |
| 6956 | CXCursor Cursor; |
| 6957 | SourceRange CursorRange; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6958 | unsigned BeforeReachingCursorIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6959 | unsigned BeforeChildrenTokenIdx; |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 6960 | PostChildrenActions ChildActions; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6961 | }; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6962 | SmallVector<PostChildrenInfo, 8> PostChildrenInfos; |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6963 | |
| 6964 | CXToken &getTok(unsigned Idx) { |
| 6965 | assert(Idx < NumTokens); |
| 6966 | return Tokens[Idx]; |
| 6967 | } |
| 6968 | const CXToken &getTok(unsigned Idx) const { |
| 6969 | assert(Idx < NumTokens); |
| 6970 | return Tokens[Idx]; |
| 6971 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6972 | bool MoreTokens() const { return TokIdx < NumTokens; } |
| 6973 | unsigned NextToken() const { return TokIdx; } |
| 6974 | void AdvanceToken() { ++TokIdx; } |
| 6975 | SourceLocation GetTokenLoc(unsigned tokI) { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6976 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6977 | } |
| 6978 | bool isFunctionMacroToken(unsigned tokI) const { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6979 | return getTok(tokI).int_data[3] != 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6980 | } |
| 6981 | SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6982 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[3]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6983 | } |
| 6984 | |
| 6985 | void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange); |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6986 | bool annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6987 | SourceRange); |
| 6988 | |
| 6989 | public: |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6990 | AnnotateTokensWorker(CXToken *tokens, CXCursor *cursors, unsigned numTokens, |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6991 | CXTranslationUnit TU, SourceRange RegionOfInterest) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 6992 | : Tokens(tokens), Cursors(cursors), NumTokens(numTokens), TokIdx(0), |
| 6993 | PreprocessingTokIdx(0), |
| 6994 | AnnotateVis(TU, AnnotateTokensVisitor, this, |
| 6995 | /*VisitPreprocessorLast=*/true, |
| 6996 | /*VisitIncludedEntities=*/false, RegionOfInterest, |
| 6997 | /*VisitDeclsOnly=*/false, |
| 6998 | AnnotateTokensPostChildrenVisitor), |
| 6999 | SrcMgr(cxtu::getASTUnit(TU)->getSourceManager()), |
| 7000 | HasContextSensitiveKeywords(false) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7001 | |
| 7002 | void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } |
| 7003 | enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7004 | bool IsIgnoredChildCursor(CXCursor cursor) const; |
| 7005 | PostChildrenActions DetermineChildActions(CXCursor Cursor) const; |
| 7006 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7007 | bool postVisitChildren(CXCursor cursor); |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7008 | void HandlePostPonedChildCursors(const PostChildrenInfo &Info); |
| 7009 | void HandlePostPonedChildCursor(CXCursor Cursor, unsigned StartTokenIndex); |
| 7010 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7011 | void AnnotateTokens(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7012 | |
| 7013 | /// Determine whether the annotator saw any cursors that have |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7014 | /// context-sensitive keywords. |
| 7015 | bool hasContextSensitiveKeywords() const { |
| 7016 | return HasContextSensitiveKeywords; |
| 7017 | } |
| 7018 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7019 | ~AnnotateTokensWorker() { assert(PostChildrenInfos.empty()); } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7020 | }; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7021 | } // namespace |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7022 | |
| 7023 | void AnnotateTokensWorker::AnnotateTokens() { |
| 7024 | // Walk the AST within the region of interest, annotating tokens |
| 7025 | // along the way. |
| 7026 | AnnotateVis.visitFileRegion(); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7027 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7028 | |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7029 | bool AnnotateTokensWorker::IsIgnoredChildCursor(CXCursor cursor) const { |
| 7030 | if (PostChildrenInfos.empty()) |
| 7031 | return false; |
| 7032 | |
| 7033 | for (const auto &ChildAction : PostChildrenInfos.back().ChildActions) { |
| 7034 | if (ChildAction.cursor == cursor && |
| 7035 | ChildAction.action == PostChildrenAction::Ignore) { |
| 7036 | return true; |
| 7037 | } |
| 7038 | } |
| 7039 | |
| 7040 | return false; |
| 7041 | } |
| 7042 | |
| 7043 | const CXXOperatorCallExpr *GetSubscriptOrCallOperator(CXCursor Cursor) { |
| 7044 | if (!clang_isExpression(Cursor.kind)) |
| 7045 | return nullptr; |
| 7046 | |
| 7047 | const Expr *E = getCursorExpr(Cursor); |
| 7048 | if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 7049 | const OverloadedOperatorKind Kind = OCE->getOperator(); |
| 7050 | if (Kind == OO_Call || Kind == OO_Subscript) |
| 7051 | return OCE; |
| 7052 | } |
| 7053 | |
| 7054 | return nullptr; |
| 7055 | } |
| 7056 | |
| 7057 | AnnotateTokensWorker::PostChildrenActions |
| 7058 | AnnotateTokensWorker::DetermineChildActions(CXCursor Cursor) const { |
| 7059 | PostChildrenActions actions; |
| 7060 | |
| 7061 | // The DeclRefExpr of CXXOperatorCallExpr refering to the custom operator is |
| 7062 | // visited before the arguments to the operator call. For the Call and |
| 7063 | // Subscript operator the range of this DeclRefExpr includes the whole call |
| 7064 | // expression, so that all tokens in that range would be mapped to the |
| 7065 | // operator function, including the tokens of the arguments. To avoid that, |
| 7066 | // ensure to visit this DeclRefExpr as last node. |
| 7067 | if (const auto *OCE = GetSubscriptOrCallOperator(Cursor)) { |
| 7068 | const Expr *Callee = OCE->getCallee(); |
| 7069 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) { |
| 7070 | const Expr *SubExpr = ICE->getSubExpr(); |
| 7071 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(SubExpr)) { |
Fangrui Song | cabb36d | 2018-11-20 08:00:00 +0000 | [diff] [blame] | 7072 | const Decl *parentDecl = getCursorDecl(Cursor); |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7073 | CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor); |
| 7074 | |
| 7075 | // Visit the DeclRefExpr as last. |
| 7076 | CXCursor cxChild = MakeCXCursor(DRE, parentDecl, TU); |
| 7077 | actions.push_back({cxChild, PostChildrenAction::Postpone}); |
| 7078 | |
| 7079 | // The parent of the DeclRefExpr, an ImplicitCastExpr, has an equally |
| 7080 | // wide range as the DeclRefExpr. We can skip visiting this entirely. |
| 7081 | cxChild = MakeCXCursor(ICE, parentDecl, TU); |
| 7082 | actions.push_back({cxChild, PostChildrenAction::Ignore}); |
| 7083 | } |
| 7084 | } |
| 7085 | } |
| 7086 | |
| 7087 | return actions; |
| 7088 | } |
| 7089 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7090 | static inline void updateCursorAnnotation(CXCursor &Cursor, |
| 7091 | const CXCursor &updateC) { |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7092 | if (clang_isInvalid(updateC.kind) || !clang_isInvalid(Cursor.kind)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7093 | return; |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7094 | Cursor = updateC; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7095 | } |
| 7096 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7097 | /// It annotates and advances tokens with a cursor until the comparison |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7098 | //// between the cursor location and the source range is the same as |
| 7099 | /// \arg compResult. |
| 7100 | /// |
| 7101 | /// Pass RangeBefore to annotate tokens with a cursor until a range is reached. |
| 7102 | /// Pass RangeOverlap to annotate tokens inside a range. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7103 | void AnnotateTokensWorker::annotateAndAdvanceTokens( |
| 7104 | CXCursor updateC, RangeComparisonResult compResult, SourceRange range) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7105 | while (MoreTokens()) { |
| 7106 | const unsigned I = NextToken(); |
| 7107 | if (isFunctionMacroToken(I)) |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7108 | if (!annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range)) |
| 7109 | return; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7110 | |
| 7111 | SourceLocation TokLoc = GetTokenLoc(I); |
| 7112 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7113 | updateCursorAnnotation(Cursors[I], updateC); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7114 | AdvanceToken(); |
| 7115 | continue; |
| 7116 | } |
| 7117 | break; |
| 7118 | } |
| 7119 | } |
| 7120 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7121 | /// Special annotation handling for macro argument tokens. |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7122 | /// \returns true if it advanced beyond all macro tokens, false otherwise. |
| 7123 | bool AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7124 | CXCursor updateC, RangeComparisonResult compResult, SourceRange range) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7125 | assert(MoreTokens()); |
| 7126 | assert(isFunctionMacroToken(NextToken()) && |
| 7127 | "Should be called only for macro arg tokens"); |
| 7128 | |
| 7129 | // This works differently than annotateAndAdvanceTokens; because expanded |
| 7130 | // macro arguments can have arbitrary translation-unit source order, we do not |
| 7131 | // advance the token index one by one until a token fails the range test. |
| 7132 | // We only advance once past all of the macro arg tokens if all of them |
| 7133 | // pass the range test. If one of them fails we keep the token index pointing |
| 7134 | // at the start of the macro arg tokens so that the failing token will be |
| 7135 | // annotated by a subsequent annotation try. |
| 7136 | |
| 7137 | bool atLeastOneCompFail = false; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7138 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7139 | unsigned I = NextToken(); |
| 7140 | for (; I < NumTokens && isFunctionMacroToken(I); ++I) { |
| 7141 | SourceLocation TokLoc = getFunctionMacroTokenLoc(I); |
| 7142 | if (TokLoc.isFileID()) |
| 7143 | continue; // not macro arg token, it's parens or comma. |
| 7144 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 7145 | if (clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 7146 | Cursors[I] = updateC; |
| 7147 | } else |
| 7148 | atLeastOneCompFail = true; |
| 7149 | } |
| 7150 | |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7151 | if (atLeastOneCompFail) |
| 7152 | return false; |
| 7153 | |
| 7154 | TokIdx = I; // All of the tokens were handled, advance beyond all of them. |
| 7155 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7156 | } |
| 7157 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7158 | enum CXChildVisitResult AnnotateTokensWorker::Visit(CXCursor cursor, |
| 7159 | CXCursor parent) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7160 | SourceRange cursorRange = getRawCursorExtent(cursor); |
| 7161 | if (cursorRange.isInvalid()) |
| 7162 | return CXChildVisit_Recurse; |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7163 | |
| 7164 | if (IsIgnoredChildCursor(cursor)) |
| 7165 | return CXChildVisit_Continue; |
| 7166 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7167 | if (!HasContextSensitiveKeywords) { |
| 7168 | // Objective-C properties can have context-sensitive keywords. |
| 7169 | if (cursor.kind == CXCursor_ObjCPropertyDecl) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7170 | if (const ObjCPropertyDecl *Property = |
| 7171 | dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor))) |
| 7172 | HasContextSensitiveKeywords = |
| 7173 | Property->getPropertyAttributesAsWritten() != 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7174 | } |
| 7175 | // Objective-C methods can have context-sensitive keywords. |
| 7176 | else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl || |
| 7177 | cursor.kind == CXCursor_ObjCClassMethodDecl) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7178 | if (const ObjCMethodDecl *Method = |
| 7179 | dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7180 | if (Method->getObjCDeclQualifier()) |
| 7181 | HasContextSensitiveKeywords = true; |
| 7182 | else { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 7183 | for (const auto *P : Method->parameters()) { |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 7184 | if (P->getObjCDeclQualifier()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7185 | HasContextSensitiveKeywords = true; |
| 7186 | break; |
| 7187 | } |
| 7188 | } |
| 7189 | } |
| 7190 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7191 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7192 | // C++ methods can have context-sensitive keywords. |
| 7193 | else if (cursor.kind == CXCursor_CXXMethod) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7194 | if (const CXXMethodDecl *Method = |
| 7195 | dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7196 | if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>()) |
| 7197 | HasContextSensitiveKeywords = true; |
| 7198 | } |
| 7199 | } |
| 7200 | // C++ classes can have context-sensitive keywords. |
| 7201 | else if (cursor.kind == CXCursor_StructDecl || |
| 7202 | cursor.kind == CXCursor_ClassDecl || |
| 7203 | cursor.kind == CXCursor_ClassTemplate || |
| 7204 | cursor.kind == CXCursor_ClassTemplatePartialSpecialization) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7205 | if (const Decl *D = getCursorDecl(cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7206 | if (D->hasAttr<FinalAttr>()) |
| 7207 | HasContextSensitiveKeywords = true; |
| 7208 | } |
| 7209 | } |
Argyrios Kyrtzidis | 990b386 | 2013-06-04 18:24:30 +0000 | [diff] [blame] | 7210 | |
| 7211 | // Don't override a property annotation with its getter/setter method. |
| 7212 | if (cursor.kind == CXCursor_ObjCInstanceMethodDecl && |
| 7213 | parent.kind == CXCursor_ObjCPropertyDecl) |
| 7214 | return CXChildVisit_Continue; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7215 | |
| 7216 | if (clang_isPreprocessing(cursor.kind)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7217 | // Items in the preprocessing record are kept separate from items in |
| 7218 | // declarations, so we keep a separate token index. |
| 7219 | unsigned SavedTokIdx = TokIdx; |
| 7220 | TokIdx = PreprocessingTokIdx; |
| 7221 | |
| 7222 | // Skip tokens up until we catch up to the beginning of the preprocessing |
| 7223 | // entry. |
| 7224 | while (MoreTokens()) { |
| 7225 | const unsigned I = NextToken(); |
| 7226 | SourceLocation TokLoc = GetTokenLoc(I); |
| 7227 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 7228 | case RangeBefore: |
| 7229 | AdvanceToken(); |
| 7230 | continue; |
| 7231 | case RangeAfter: |
| 7232 | case RangeOverlap: |
| 7233 | break; |
| 7234 | } |
| 7235 | break; |
| 7236 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7237 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7238 | // Look at all of the tokens within this range. |
| 7239 | while (MoreTokens()) { |
| 7240 | const unsigned I = NextToken(); |
| 7241 | SourceLocation TokLoc = GetTokenLoc(I); |
| 7242 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 7243 | case RangeBefore: |
| 7244 | llvm_unreachable("Infeasible"); |
| 7245 | case RangeAfter: |
| 7246 | break; |
| 7247 | case RangeOverlap: |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 7248 | // For macro expansions, just note where the beginning of the macro |
| 7249 | // expansion occurs. |
| 7250 | if (cursor.kind == CXCursor_MacroExpansion) { |
| 7251 | if (TokLoc == cursorRange.getBegin()) |
| 7252 | Cursors[I] = cursor; |
| 7253 | AdvanceToken(); |
| 7254 | break; |
| 7255 | } |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7256 | // We may have already annotated macro names inside macro definitions. |
| 7257 | if (Cursors[I].kind != CXCursor_MacroExpansion) |
| 7258 | Cursors[I] = cursor; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7259 | AdvanceToken(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7260 | continue; |
| 7261 | } |
| 7262 | break; |
| 7263 | } |
| 7264 | |
| 7265 | // Save the preprocessing token index; restore the non-preprocessing |
| 7266 | // token index. |
| 7267 | PreprocessingTokIdx = TokIdx; |
| 7268 | TokIdx = SavedTokIdx; |
| 7269 | return CXChildVisit_Recurse; |
| 7270 | } |
| 7271 | |
| 7272 | if (cursorRange.isInvalid()) |
| 7273 | return CXChildVisit_Continue; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7274 | |
| 7275 | unsigned BeforeReachingCursorIdx = NextToken(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7276 | const enum CXCursorKind cursorK = clang_getCursorKind(cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7277 | const enum CXCursorKind K = clang_getCursorKind(parent); |
| 7278 | const CXCursor updateC = |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7279 | (clang_isInvalid(K) || K == CXCursor_TranslationUnit || |
| 7280 | // Attributes are annotated out-of-order, skip tokens until we reach it. |
| 7281 | clang_isAttribute(cursor.kind)) |
| 7282 | ? clang_getNullCursor() |
| 7283 | : parent; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7284 | |
| 7285 | annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange); |
| 7286 | |
| 7287 | // Avoid having the cursor of an expression "overwrite" the annotation of the |
| 7288 | // variable declaration that it belongs to. |
| 7289 | // This can happen for C++ constructor expressions whose range generally |
| 7290 | // include the variable declaration, e.g.: |
| 7291 | // 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] | 7292 | if (clang_isExpression(cursorK) && MoreTokens()) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 7293 | const Expr *E = getCursorExpr(cursor); |
Fangrui Song | cabb36d | 2018-11-20 08:00:00 +0000 | [diff] [blame] | 7294 | if (const Decl *D = getCursorDecl(cursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7295 | const unsigned I = NextToken(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7296 | if (E->getBeginLoc().isValid() && D->getLocation().isValid() && |
| 7297 | E->getBeginLoc() == D->getLocation() && |
| 7298 | E->getBeginLoc() == GetTokenLoc(I)) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7299 | updateCursorAnnotation(Cursors[I], updateC); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7300 | AdvanceToken(); |
| 7301 | } |
| 7302 | } |
| 7303 | } |
| 7304 | |
| 7305 | // Before recursing into the children keep some state that we are going |
| 7306 | // to use in the AnnotateTokensWorker::postVisitChildren callback to do some |
| 7307 | // extra work after the child nodes are visited. |
| 7308 | // Note that we don't call VisitChildren here to avoid traversing statements |
| 7309 | // code-recursively which can blow the stack. |
| 7310 | |
| 7311 | PostChildrenInfo Info; |
| 7312 | Info.Cursor = cursor; |
| 7313 | Info.CursorRange = cursorRange; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7314 | Info.BeforeReachingCursorIdx = BeforeReachingCursorIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7315 | Info.BeforeChildrenTokenIdx = NextToken(); |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7316 | Info.ChildActions = DetermineChildActions(cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7317 | PostChildrenInfos.push_back(Info); |
| 7318 | |
| 7319 | return CXChildVisit_Recurse; |
| 7320 | } |
| 7321 | |
| 7322 | bool AnnotateTokensWorker::postVisitChildren(CXCursor cursor) { |
| 7323 | if (PostChildrenInfos.empty()) |
| 7324 | return false; |
| 7325 | const PostChildrenInfo &Info = PostChildrenInfos.back(); |
| 7326 | if (!clang_equalCursors(Info.Cursor, cursor)) |
| 7327 | return false; |
| 7328 | |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7329 | HandlePostPonedChildCursors(Info); |
| 7330 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7331 | const unsigned BeforeChildren = Info.BeforeChildrenTokenIdx; |
| 7332 | const unsigned AfterChildren = NextToken(); |
| 7333 | SourceRange cursorRange = Info.CursorRange; |
| 7334 | |
| 7335 | // Scan the tokens that are at the end of the cursor, but are not captured |
| 7336 | // but the child cursors. |
| 7337 | annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange); |
| 7338 | |
| 7339 | // Scan the tokens that are at the beginning of the cursor, but are not |
| 7340 | // capture by the child cursors. |
| 7341 | for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { |
| 7342 | if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 7343 | break; |
| 7344 | |
| 7345 | Cursors[I] = cursor; |
| 7346 | } |
| 7347 | |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7348 | // Attributes are annotated out-of-order, rewind TokIdx to when we first |
| 7349 | // encountered the attribute cursor. |
| 7350 | if (clang_isAttribute(cursor.kind)) |
| 7351 | TokIdx = Info.BeforeReachingCursorIdx; |
| 7352 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7353 | PostChildrenInfos.pop_back(); |
| 7354 | return false; |
| 7355 | } |
| 7356 | |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7357 | void AnnotateTokensWorker::HandlePostPonedChildCursors( |
| 7358 | const PostChildrenInfo &Info) { |
| 7359 | for (const auto &ChildAction : Info.ChildActions) { |
| 7360 | if (ChildAction.action == PostChildrenAction::Postpone) { |
| 7361 | HandlePostPonedChildCursor(ChildAction.cursor, |
| 7362 | Info.BeforeChildrenTokenIdx); |
| 7363 | } |
| 7364 | } |
| 7365 | } |
| 7366 | |
| 7367 | void AnnotateTokensWorker::HandlePostPonedChildCursor( |
| 7368 | CXCursor Cursor, unsigned StartTokenIndex) { |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7369 | unsigned I = StartTokenIndex; |
| 7370 | |
| 7371 | // The bracket tokens of a Call or Subscript operator are mapped to |
| 7372 | // CallExpr/CXXOperatorCallExpr because we skipped visiting the corresponding |
| 7373 | // DeclRefExpr. Remap these tokens to the DeclRefExpr cursors. |
| 7374 | for (unsigned RefNameRangeNr = 0; I < NumTokens; RefNameRangeNr++) { |
Nikolai Kosjar | 2a647e7 | 2019-05-08 13:19:29 +0000 | [diff] [blame] | 7375 | const CXSourceRange CXRefNameRange = clang_getCursorReferenceNameRange( |
| 7376 | Cursor, CXNameRange_WantQualifier, RefNameRangeNr); |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7377 | if (clang_Range_isNull(CXRefNameRange)) |
| 7378 | break; // All ranges handled. |
| 7379 | |
| 7380 | SourceRange RefNameRange = cxloc::translateCXSourceRange(CXRefNameRange); |
| 7381 | while (I < NumTokens) { |
| 7382 | const SourceLocation TokenLocation = GetTokenLoc(I); |
| 7383 | if (!TokenLocation.isValid()) |
| 7384 | break; |
| 7385 | |
| 7386 | // Adapt the end range, because LocationCompare() reports |
| 7387 | // RangeOverlap even for the not-inclusive end location. |
| 7388 | const SourceLocation fixedEnd = |
| 7389 | RefNameRange.getEnd().getLocWithOffset(-1); |
| 7390 | RefNameRange = SourceRange(RefNameRange.getBegin(), fixedEnd); |
| 7391 | |
| 7392 | const RangeComparisonResult ComparisonResult = |
| 7393 | LocationCompare(SrcMgr, TokenLocation, RefNameRange); |
| 7394 | |
| 7395 | if (ComparisonResult == RangeOverlap) { |
| 7396 | Cursors[I++] = Cursor; |
| 7397 | } else if (ComparisonResult == RangeBefore) { |
| 7398 | ++I; // Not relevant token, check next one. |
| 7399 | } else if (ComparisonResult == RangeAfter) { |
| 7400 | break; // All tokens updated for current range, check next. |
| 7401 | } |
| 7402 | } |
| 7403 | } |
| 7404 | } |
| 7405 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7406 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 7407 | CXCursor parent, |
| 7408 | CXClientData client_data) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7409 | return static_cast<AnnotateTokensWorker *>(client_data) |
| 7410 | ->Visit(cursor, parent); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7411 | } |
| 7412 | |
| 7413 | static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor, |
| 7414 | CXClientData client_data) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7415 | return static_cast<AnnotateTokensWorker *>(client_data) |
| 7416 | ->postVisitChildren(cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7417 | } |
| 7418 | |
| 7419 | namespace { |
| 7420 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7421 | /// Uses the macro expansions in the preprocessing record to find |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7422 | /// and mark tokens that are macro arguments. This info is used by the |
| 7423 | /// AnnotateTokensWorker. |
| 7424 | class MarkMacroArgTokensVisitor { |
| 7425 | SourceManager &SM; |
| 7426 | CXToken *Tokens; |
| 7427 | unsigned NumTokens; |
| 7428 | unsigned CurIdx; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7429 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7430 | public: |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7431 | MarkMacroArgTokensVisitor(SourceManager &SM, CXToken *tokens, |
| 7432 | unsigned numTokens) |
| 7433 | : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7434 | |
| 7435 | CXChildVisitResult visit(CXCursor cursor, CXCursor parent) { |
| 7436 | if (cursor.kind != CXCursor_MacroExpansion) |
| 7437 | return CXChildVisit_Continue; |
| 7438 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7439 | SourceRange macroRange = getCursorMacroExpansion(cursor).getSourceRange(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7440 | if (macroRange.getBegin() == macroRange.getEnd()) |
| 7441 | return CXChildVisit_Continue; // it's not a function macro. |
| 7442 | |
| 7443 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 7444 | if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx), |
| 7445 | macroRange.getBegin())) |
| 7446 | break; |
| 7447 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7448 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7449 | if (CurIdx == NumTokens) |
| 7450 | return CXChildVisit_Break; |
| 7451 | |
| 7452 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 7453 | SourceLocation tokLoc = getTokenLoc(CurIdx); |
| 7454 | if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd())) |
| 7455 | break; |
| 7456 | |
| 7457 | setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc)); |
| 7458 | } |
| 7459 | |
| 7460 | if (CurIdx == NumTokens) |
| 7461 | return CXChildVisit_Break; |
| 7462 | |
| 7463 | return CXChildVisit_Continue; |
| 7464 | } |
| 7465 | |
| 7466 | private: |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 7467 | CXToken &getTok(unsigned Idx) { |
| 7468 | assert(Idx < NumTokens); |
| 7469 | return Tokens[Idx]; |
| 7470 | } |
| 7471 | const CXToken &getTok(unsigned Idx) const { |
| 7472 | assert(Idx < NumTokens); |
| 7473 | return Tokens[Idx]; |
| 7474 | } |
| 7475 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7476 | SourceLocation getTokenLoc(unsigned tokI) { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 7477 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7478 | } |
| 7479 | |
| 7480 | void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) { |
| 7481 | // The third field is reserved and currently not used. Use it here |
| 7482 | // to mark macro arg expanded tokens with their expanded locations. |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 7483 | getTok(tokI).int_data[3] = loc.getRawEncoding(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7484 | } |
| 7485 | }; |
| 7486 | |
| 7487 | } // end anonymous namespace |
| 7488 | |
| 7489 | static CXChildVisitResult |
| 7490 | MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent, |
| 7491 | CXClientData client_data) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7492 | return static_cast<MarkMacroArgTokensVisitor *>(client_data) |
| 7493 | ->visit(cursor, parent); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7494 | } |
| 7495 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7496 | /// Used by \c annotatePreprocessorTokens. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7497 | /// \returns true if lexing was finished, false otherwise. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7498 | static bool lexNext(Lexer &Lex, Token &Tok, unsigned &NextIdx, |
| 7499 | unsigned NumTokens) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7500 | if (NextIdx >= NumTokens) |
| 7501 | return true; |
| 7502 | |
| 7503 | ++NextIdx; |
| 7504 | Lex.LexFromRawLexer(Tok); |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 7505 | return Tok.is(tok::eof); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7506 | } |
| 7507 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7508 | static void annotatePreprocessorTokens(CXTranslationUnit TU, |
| 7509 | SourceRange RegionOfInterest, |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7510 | CXCursor *Cursors, CXToken *Tokens, |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7511 | unsigned NumTokens) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7512 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7513 | |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7514 | Preprocessor &PP = CXXUnit->getPreprocessor(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7515 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7516 | std::pair<FileID, unsigned> BeginLocInfo = |
| 7517 | SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getBegin()); |
| 7518 | std::pair<FileID, unsigned> EndLocInfo = |
| 7519 | SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getEnd()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7520 | |
| 7521 | if (BeginLocInfo.first != EndLocInfo.first) |
| 7522 | return; |
| 7523 | |
| 7524 | StringRef Buffer; |
| 7525 | bool Invalid = false; |
| 7526 | Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 7527 | if (Buffer.empty() || Invalid) |
| 7528 | return; |
| 7529 | |
| 7530 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7531 | CXXUnit->getASTContext().getLangOpts(), Buffer.begin(), |
| 7532 | Buffer.data() + BeginLocInfo.second, Buffer.end()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7533 | Lex.SetCommentRetentionState(true); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7534 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7535 | unsigned NextIdx = 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7536 | // Lex tokens in raw mode until we hit the end of the range, to avoid |
| 7537 | // entering #includes or expanding macros. |
| 7538 | while (true) { |
| 7539 | Token Tok; |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7540 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 7541 | break; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7542 | unsigned TokIdx = NextIdx - 1; |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7543 | assert(Tok.getLocation() == |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7544 | SourceLocation::getFromRawEncoding(Tokens[TokIdx].int_data[1])); |
| 7545 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7546 | reprocess: |
| 7547 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7548 | // We have found a preprocessing directive. Annotate the tokens |
| 7549 | // appropriately. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7550 | // |
| 7551 | // FIXME: Some simple tests here could identify macro definitions and |
| 7552 | // #undefs, to provide specific cursor kinds for those. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7553 | |
| 7554 | SourceLocation BeginLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7555 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 7556 | break; |
| 7557 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7558 | MacroInfo *MI = nullptr; |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 7559 | if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "define") { |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7560 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 7561 | break; |
| 7562 | |
| 7563 | if (Tok.is(tok::raw_identifier)) { |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 7564 | IdentifierInfo &II = |
| 7565 | PP.getIdentifierTable().get(Tok.getRawIdentifier()); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7566 | SourceLocation MappedTokLoc = |
| 7567 | CXXUnit->mapLocationToPreamble(Tok.getLocation()); |
| 7568 | MI = getMacroInfo(II, MappedTokLoc, TU); |
| 7569 | } |
| 7570 | } |
| 7571 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7572 | bool finished = false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7573 | do { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7574 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) { |
| 7575 | finished = true; |
| 7576 | break; |
| 7577 | } |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7578 | // If we are in a macro definition, check if the token was ever a |
| 7579 | // macro name and annotate it if that's the case. |
| 7580 | if (MI) { |
| 7581 | SourceLocation SaveLoc = Tok.getLocation(); |
| 7582 | Tok.setLocation(CXXUnit->mapLocationToPreamble(SaveLoc)); |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 7583 | MacroDefinitionRecord *MacroDef = |
| 7584 | checkForMacroInMacroDefinition(MI, Tok, TU); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7585 | Tok.setLocation(SaveLoc); |
| 7586 | if (MacroDef) |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 7587 | Cursors[NextIdx - 1] = |
| 7588 | MakeMacroExpansionCursor(MacroDef, Tok.getLocation(), TU); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7589 | } |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7590 | } while (!Tok.isAtStartOfLine()); |
| 7591 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7592 | unsigned LastIdx = finished ? NextIdx - 1 : NextIdx - 2; |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7593 | assert(TokIdx <= LastIdx); |
| 7594 | SourceLocation EndLoc = |
| 7595 | SourceLocation::getFromRawEncoding(Tokens[LastIdx].int_data[1]); |
| 7596 | CXCursor Cursor = |
| 7597 | MakePreprocessingDirectiveCursor(SourceRange(BeginLoc, EndLoc), TU); |
| 7598 | |
| 7599 | for (; TokIdx <= LastIdx; ++TokIdx) |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7600 | updateCursorAnnotation(Cursors[TokIdx], Cursor); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7601 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7602 | if (finished) |
| 7603 | break; |
| 7604 | goto reprocess; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7605 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7606 | } |
| 7607 | } |
| 7608 | |
| 7609 | // This gets run a separate thread to avoid stack blowout. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 7610 | static void clang_annotateTokensImpl(CXTranslationUnit TU, ASTUnit *CXXUnit, |
| 7611 | CXToken *Tokens, unsigned NumTokens, |
| 7612 | CXCursor *Cursors) { |
Dmitri Gribenko | 183436e | 2013-01-26 21:49:50 +0000 | [diff] [blame] | 7613 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7614 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) |
| 7615 | setThreadBackgroundPriority(); |
| 7616 | |
| 7617 | // Determine the region of interest, which contains all of the tokens. |
| 7618 | SourceRange RegionOfInterest; |
| 7619 | RegionOfInterest.setBegin( |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7620 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0]))); |
| 7621 | RegionOfInterest.setEnd(cxloc::translateSourceLocation( |
| 7622 | clang_getTokenLocation(TU, Tokens[NumTokens - 1]))); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7623 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7624 | // Relex the tokens within the source range to look for preprocessing |
| 7625 | // directives. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7626 | annotatePreprocessorTokens(TU, RegionOfInterest, Cursors, Tokens, NumTokens); |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 7627 | |
| 7628 | // If begin location points inside a macro argument, set it to the expansion |
| 7629 | // location so we can have the full context when annotating semantically. |
| 7630 | { |
| 7631 | SourceManager &SM = CXXUnit->getSourceManager(); |
| 7632 | SourceLocation Loc = |
| 7633 | SM.getMacroArgExpandedLocation(RegionOfInterest.getBegin()); |
| 7634 | if (Loc.isMacroID()) |
| 7635 | RegionOfInterest.setBegin(SM.getExpansionLoc(Loc)); |
| 7636 | } |
| 7637 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7638 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) { |
| 7639 | // Search and mark tokens that are macro argument expansions. |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7640 | MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(), Tokens, |
| 7641 | NumTokens); |
| 7642 | CursorVisitor MacroArgMarker( |
| 7643 | TU, MarkMacroArgTokensVisitorDelegate, &Visitor, |
| 7644 | /*VisitPreprocessorLast=*/true, |
| 7645 | /*VisitIncludedEntities=*/false, RegionOfInterest); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7646 | MacroArgMarker.visitPreprocessedEntitiesInRegion(); |
| 7647 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7648 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7649 | // Annotate all of the source locations in the region of interest that map to |
| 7650 | // a specific cursor. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7651 | AnnotateTokensWorker W(Tokens, Cursors, NumTokens, TU, RegionOfInterest); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7652 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7653 | // FIXME: We use a ridiculous stack size here because the data-recursion |
| 7654 | // algorithm uses a large stack frame than the non-data recursive version, |
| 7655 | // and AnnotationTokensWorker currently transforms the data-recursion |
| 7656 | // algorithm back into a traditional recursion by explicitly calling |
| 7657 | // VisitChildren(). We will need to remove this explicit recursive call. |
| 7658 | W.AnnotateTokens(); |
| 7659 | |
| 7660 | // If we ran into any entities that involve context-sensitive keywords, |
| 7661 | // take another pass through the tokens to mark them as such. |
| 7662 | if (W.hasContextSensitiveKeywords()) { |
| 7663 | for (unsigned I = 0; I != NumTokens; ++I) { |
| 7664 | if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier) |
| 7665 | continue; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7666 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7667 | if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) { |
| 7668 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7669 | if (const ObjCPropertyDecl *Property = |
| 7670 | dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7671 | if (Property->getPropertyAttributesAsWritten() != 0 && |
| 7672 | llvm::StringSwitch<bool>(II->getName()) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7673 | .Case("readonly", true) |
| 7674 | .Case("assign", true) |
| 7675 | .Case("unsafe_unretained", true) |
| 7676 | .Case("readwrite", true) |
| 7677 | .Case("retain", true) |
| 7678 | .Case("copy", true) |
| 7679 | .Case("nonatomic", true) |
| 7680 | .Case("atomic", true) |
| 7681 | .Case("getter", true) |
| 7682 | .Case("setter", true) |
| 7683 | .Case("strong", true) |
| 7684 | .Case("weak", true) |
| 7685 | .Case("class", true) |
| 7686 | .Default(false)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7687 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 7688 | } |
| 7689 | continue; |
| 7690 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7691 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7692 | if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl || |
| 7693 | Cursors[I].kind == CXCursor_ObjCClassMethodDecl) { |
| 7694 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 7695 | if (llvm::StringSwitch<bool>(II->getName()) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7696 | .Case("in", true) |
| 7697 | .Case("out", true) |
| 7698 | .Case("inout", true) |
| 7699 | .Case("oneway", true) |
| 7700 | .Case("bycopy", true) |
| 7701 | .Case("byref", true) |
| 7702 | .Default(false)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7703 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 7704 | continue; |
| 7705 | } |
| 7706 | |
| 7707 | if (Cursors[I].kind == CXCursor_CXXFinalAttr || |
| 7708 | Cursors[I].kind == CXCursor_CXXOverrideAttr) { |
| 7709 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 7710 | continue; |
| 7711 | } |
| 7712 | } |
| 7713 | } |
| 7714 | } |
| 7715 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7716 | void clang_annotateTokens(CXTranslationUnit TU, CXToken *Tokens, |
| 7717 | unsigned NumTokens, CXCursor *Cursors) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7718 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7719 | LOG_BAD_TU(TU); |
| 7720 | return; |
| 7721 | } |
| 7722 | if (NumTokens == 0 || !Tokens || !Cursors) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7723 | LOG_FUNC_SECTION { *Log << "<null input>"; } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7724 | return; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7725 | } |
| 7726 | |
| 7727 | LOG_FUNC_SECTION { |
| 7728 | *Log << TU << ' '; |
| 7729 | CXSourceLocation bloc = clang_getTokenLocation(TU, Tokens[0]); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7730 | CXSourceLocation eloc = clang_getTokenLocation(TU, Tokens[NumTokens - 1]); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7731 | *Log << clang_getRange(bloc, eloc); |
| 7732 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7733 | |
| 7734 | // Any token we don't specifically annotate will have a NULL cursor. |
| 7735 | CXCursor C = clang_getNullCursor(); |
| 7736 | for (unsigned I = 0; I != NumTokens; ++I) |
| 7737 | Cursors[I] = C; |
| 7738 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7739 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7740 | if (!CXXUnit) |
| 7741 | return; |
| 7742 | |
| 7743 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 7744 | |
| 7745 | auto AnnotateTokensImpl = [=]() { |
| 7746 | clang_annotateTokensImpl(TU, CXXUnit, Tokens, NumTokens, Cursors); |
| 7747 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7748 | llvm::CrashRecoveryContext CRC; |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 7749 | if (!RunSafely(CRC, AnnotateTokensImpl, GetSafetyThreadStackSize() * 2)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7750 | fprintf(stderr, "libclang: crash detected while annotating tokens\n"); |
| 7751 | } |
| 7752 | } |
| 7753 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7754 | //===----------------------------------------------------------------------===// |
| 7755 | // Operations for querying linkage of a cursor. |
| 7756 | //===----------------------------------------------------------------------===// |
| 7757 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7758 | CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { |
| 7759 | if (!clang_isDeclaration(cursor.kind)) |
| 7760 | return CXLinkage_Invalid; |
| 7761 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7762 | const Decl *D = cxcursor::getCursorDecl(cursor); |
| 7763 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7764 | switch (ND->getLinkageInternal()) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7765 | case NoLinkage: |
| 7766 | case VisibleNoLinkage: |
| 7767 | return CXLinkage_NoLinkage; |
| 7768 | case ModuleInternalLinkage: |
| 7769 | case InternalLinkage: |
| 7770 | return CXLinkage_Internal; |
| 7771 | case UniqueExternalLinkage: |
| 7772 | return CXLinkage_UniqueExternal; |
| 7773 | case ModuleLinkage: |
| 7774 | case ExternalLinkage: |
| 7775 | return CXLinkage_External; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7776 | }; |
| 7777 | |
| 7778 | return CXLinkage_Invalid; |
| 7779 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7780 | |
| 7781 | //===----------------------------------------------------------------------===// |
Ehsan Akhgari | b743de7 | 2016-05-31 15:55:51 +0000 | [diff] [blame] | 7782 | // Operations for querying visibility of a cursor. |
| 7783 | //===----------------------------------------------------------------------===// |
| 7784 | |
Ehsan Akhgari | b743de7 | 2016-05-31 15:55:51 +0000 | [diff] [blame] | 7785 | CXVisibilityKind clang_getCursorVisibility(CXCursor cursor) { |
| 7786 | if (!clang_isDeclaration(cursor.kind)) |
| 7787 | return CXVisibility_Invalid; |
| 7788 | |
| 7789 | const Decl *D = cxcursor::getCursorDecl(cursor); |
| 7790 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
| 7791 | switch (ND->getVisibility()) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7792 | case HiddenVisibility: |
| 7793 | return CXVisibility_Hidden; |
| 7794 | case ProtectedVisibility: |
| 7795 | return CXVisibility_Protected; |
| 7796 | case DefaultVisibility: |
| 7797 | return CXVisibility_Default; |
Ehsan Akhgari | b743de7 | 2016-05-31 15:55:51 +0000 | [diff] [blame] | 7798 | }; |
| 7799 | |
| 7800 | return CXVisibility_Invalid; |
| 7801 | } |
Ehsan Akhgari | b743de7 | 2016-05-31 15:55:51 +0000 | [diff] [blame] | 7802 | |
| 7803 | //===----------------------------------------------------------------------===// |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7804 | // Operations for querying language of a cursor. |
| 7805 | //===----------------------------------------------------------------------===// |
| 7806 | |
| 7807 | static CXLanguageKind getDeclLanguage(const Decl *D) { |
| 7808 | if (!D) |
| 7809 | return CXLanguage_C; |
| 7810 | |
| 7811 | switch (D->getKind()) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7812 | default: |
| 7813 | break; |
| 7814 | case Decl::ImplicitParam: |
| 7815 | case Decl::ObjCAtDefsField: |
| 7816 | case Decl::ObjCCategory: |
| 7817 | case Decl::ObjCCategoryImpl: |
| 7818 | case Decl::ObjCCompatibleAlias: |
| 7819 | case Decl::ObjCImplementation: |
| 7820 | case Decl::ObjCInterface: |
| 7821 | case Decl::ObjCIvar: |
| 7822 | case Decl::ObjCMethod: |
| 7823 | case Decl::ObjCProperty: |
| 7824 | case Decl::ObjCPropertyImpl: |
| 7825 | case Decl::ObjCProtocol: |
| 7826 | case Decl::ObjCTypeParam: |
| 7827 | return CXLanguage_ObjC; |
| 7828 | case Decl::CXXConstructor: |
| 7829 | case Decl::CXXConversion: |
| 7830 | case Decl::CXXDestructor: |
| 7831 | case Decl::CXXMethod: |
| 7832 | case Decl::CXXRecord: |
| 7833 | case Decl::ClassTemplate: |
| 7834 | case Decl::ClassTemplatePartialSpecialization: |
| 7835 | case Decl::ClassTemplateSpecialization: |
| 7836 | case Decl::Friend: |
| 7837 | case Decl::FriendTemplate: |
| 7838 | case Decl::FunctionTemplate: |
| 7839 | case Decl::LinkageSpec: |
| 7840 | case Decl::Namespace: |
| 7841 | case Decl::NamespaceAlias: |
| 7842 | case Decl::NonTypeTemplateParm: |
| 7843 | case Decl::StaticAssert: |
| 7844 | case Decl::TemplateTemplateParm: |
| 7845 | case Decl::TemplateTypeParm: |
| 7846 | case Decl::UnresolvedUsingTypename: |
| 7847 | case Decl::UnresolvedUsingValue: |
| 7848 | case Decl::Using: |
| 7849 | case Decl::UsingDirective: |
| 7850 | case Decl::UsingShadow: |
| 7851 | return CXLanguage_CPlusPlus; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7852 | } |
| 7853 | |
| 7854 | return CXLanguage_C; |
| 7855 | } |
| 7856 | |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7857 | static CXAvailabilityKind getCursorAvailabilityForDecl(const Decl *D) { |
| 7858 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()) |
Manuel Klimek | 8e3a7ed | 2015-09-25 17:53:16 +0000 | [diff] [blame] | 7859 | return CXAvailability_NotAvailable; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7860 | |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7861 | switch (D->getAvailability()) { |
| 7862 | case AR_Available: |
| 7863 | case AR_NotYetIntroduced: |
| 7864 | if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(D)) |
Benjamin Kramer | 656363d | 2013-10-15 18:53:18 +0000 | [diff] [blame] | 7865 | return getCursorAvailabilityForDecl( |
| 7866 | cast<Decl>(EnumConst->getDeclContext())); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7867 | return CXAvailability_Available; |
| 7868 | |
| 7869 | case AR_Deprecated: |
| 7870 | return CXAvailability_Deprecated; |
| 7871 | |
| 7872 | case AR_Unavailable: |
| 7873 | return CXAvailability_NotAvailable; |
| 7874 | } |
Benjamin Kramer | 656363d | 2013-10-15 18:53:18 +0000 | [diff] [blame] | 7875 | |
| 7876 | llvm_unreachable("Unknown availability kind!"); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7877 | } |
| 7878 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7879 | enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { |
| 7880 | if (clang_isDeclaration(cursor.kind)) |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7881 | if (const Decl *D = cxcursor::getCursorDecl(cursor)) |
| 7882 | return getCursorAvailabilityForDecl(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7883 | |
| 7884 | return CXAvailability_Available; |
| 7885 | } |
| 7886 | |
| 7887 | static CXVersion convertVersion(VersionTuple In) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7888 | CXVersion Out = {-1, -1, -1}; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7889 | if (In.empty()) |
| 7890 | return Out; |
| 7891 | |
| 7892 | Out.Major = In.getMajor(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7893 | |
NAKAMURA Takumi | c2b5d1f | 2013-02-21 02:32:34 +0000 | [diff] [blame] | 7894 | Optional<unsigned> Minor = In.getMinor(); |
| 7895 | if (Minor.hasValue()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7896 | Out.Minor = *Minor; |
| 7897 | else |
| 7898 | return Out; |
| 7899 | |
NAKAMURA Takumi | c2b5d1f | 2013-02-21 02:32:34 +0000 | [diff] [blame] | 7900 | Optional<unsigned> Subminor = In.getSubminor(); |
| 7901 | if (Subminor.hasValue()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7902 | Out.Subminor = *Subminor; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7903 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7904 | return Out; |
| 7905 | } |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7906 | |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7907 | static void getCursorPlatformAvailabilityForDecl( |
| 7908 | const Decl *D, int *always_deprecated, CXString *deprecated_message, |
| 7909 | int *always_unavailable, CXString *unavailable_message, |
| 7910 | SmallVectorImpl<AvailabilityAttr *> &AvailabilityAttrs) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7911 | bool HadAvailAttr = false; |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 7912 | for (auto A : D->attrs()) { |
| 7913 | if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(A)) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7914 | HadAvailAttr = true; |
| 7915 | if (always_deprecated) |
| 7916 | *always_deprecated = 1; |
Nico Weber | aacf031 | 2014-04-24 05:16:45 +0000 | [diff] [blame] | 7917 | if (deprecated_message) { |
Argyrios Kyrtzidis | edfe07f | 2014-04-24 06:05:40 +0000 | [diff] [blame] | 7918 | clang_disposeString(*deprecated_message); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7919 | *deprecated_message = cxstring::createDup(Deprecated->getMessage()); |
Nico Weber | aacf031 | 2014-04-24 05:16:45 +0000 | [diff] [blame] | 7920 | } |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7921 | continue; |
| 7922 | } |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7923 | |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 7924 | if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(A)) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7925 | HadAvailAttr = true; |
| 7926 | if (always_unavailable) |
| 7927 | *always_unavailable = 1; |
| 7928 | if (unavailable_message) { |
Argyrios Kyrtzidis | edfe07f | 2014-04-24 06:05:40 +0000 | [diff] [blame] | 7929 | clang_disposeString(*unavailable_message); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7930 | *unavailable_message = cxstring::createDup(Unavailable->getMessage()); |
| 7931 | } |
| 7932 | continue; |
| 7933 | } |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7934 | |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 7935 | if (AvailabilityAttr *Avail = dyn_cast<AvailabilityAttr>(A)) { |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7936 | AvailabilityAttrs.push_back(Avail); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7937 | HadAvailAttr = true; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7938 | } |
| 7939 | } |
| 7940 | |
| 7941 | if (!HadAvailAttr) |
| 7942 | if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(D)) |
| 7943 | return getCursorPlatformAvailabilityForDecl( |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7944 | cast<Decl>(EnumConst->getDeclContext()), always_deprecated, |
| 7945 | deprecated_message, always_unavailable, unavailable_message, |
| 7946 | AvailabilityAttrs); |
| 7947 | |
| 7948 | if (AvailabilityAttrs.empty()) |
| 7949 | return; |
| 7950 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 7951 | llvm::sort( |
| 7952 | AvailabilityAttrs, [](AvailabilityAttr *LHS, AvailabilityAttr *RHS) { |
| 7953 | return LHS->getPlatform()->getName() < RHS->getPlatform()->getName(); |
| 7954 | }); |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7955 | ASTContext &Ctx = D->getASTContext(); |
| 7956 | auto It = std::unique( |
| 7957 | AvailabilityAttrs.begin(), AvailabilityAttrs.end(), |
| 7958 | [&Ctx](AvailabilityAttr *LHS, AvailabilityAttr *RHS) { |
| 7959 | if (LHS->getPlatform() != RHS->getPlatform()) |
| 7960 | return false; |
| 7961 | |
| 7962 | if (LHS->getIntroduced() == RHS->getIntroduced() && |
| 7963 | LHS->getDeprecated() == RHS->getDeprecated() && |
| 7964 | LHS->getObsoleted() == RHS->getObsoleted() && |
| 7965 | LHS->getMessage() == RHS->getMessage() && |
| 7966 | LHS->getReplacement() == RHS->getReplacement()) |
| 7967 | return true; |
| 7968 | |
| 7969 | if ((!LHS->getIntroduced().empty() && !RHS->getIntroduced().empty()) || |
| 7970 | (!LHS->getDeprecated().empty() && !RHS->getDeprecated().empty()) || |
| 7971 | (!LHS->getObsoleted().empty() && !RHS->getObsoleted().empty())) |
| 7972 | return false; |
| 7973 | |
| 7974 | if (LHS->getIntroduced().empty() && !RHS->getIntroduced().empty()) |
| 7975 | LHS->setIntroduced(Ctx, RHS->getIntroduced()); |
| 7976 | |
| 7977 | if (LHS->getDeprecated().empty() && !RHS->getDeprecated().empty()) { |
| 7978 | LHS->setDeprecated(Ctx, RHS->getDeprecated()); |
| 7979 | if (LHS->getMessage().empty()) |
| 7980 | LHS->setMessage(Ctx, RHS->getMessage()); |
| 7981 | if (LHS->getReplacement().empty()) |
| 7982 | LHS->setReplacement(Ctx, RHS->getReplacement()); |
| 7983 | } |
| 7984 | |
| 7985 | if (LHS->getObsoleted().empty() && !RHS->getObsoleted().empty()) { |
| 7986 | LHS->setObsoleted(Ctx, RHS->getObsoleted()); |
| 7987 | if (LHS->getMessage().empty()) |
| 7988 | LHS->setMessage(Ctx, RHS->getMessage()); |
| 7989 | if (LHS->getReplacement().empty()) |
| 7990 | LHS->setReplacement(Ctx, RHS->getReplacement()); |
| 7991 | } |
| 7992 | |
| 7993 | return true; |
| 7994 | }); |
| 7995 | AvailabilityAttrs.erase(It, AvailabilityAttrs.end()); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7996 | } |
| 7997 | |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7998 | int clang_getCursorPlatformAvailability(CXCursor cursor, int *always_deprecated, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7999 | CXString *deprecated_message, |
| 8000 | int *always_unavailable, |
| 8001 | CXString *unavailable_message, |
| 8002 | CXPlatformAvailability *availability, |
| 8003 | int availability_size) { |
| 8004 | if (always_deprecated) |
| 8005 | *always_deprecated = 0; |
| 8006 | if (deprecated_message) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 8007 | *deprecated_message = cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8008 | if (always_unavailable) |
| 8009 | *always_unavailable = 0; |
| 8010 | if (unavailable_message) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 8011 | *unavailable_message = cxstring::createEmpty(); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 8012 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8013 | if (!clang_isDeclaration(cursor.kind)) |
| 8014 | return 0; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 8015 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8016 | const Decl *D = cxcursor::getCursorDecl(cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8017 | if (!D) |
| 8018 | return 0; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 8019 | |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 8020 | SmallVector<AvailabilityAttr *, 8> AvailabilityAttrs; |
| 8021 | getCursorPlatformAvailabilityForDecl(D, always_deprecated, deprecated_message, |
| 8022 | always_unavailable, unavailable_message, |
| 8023 | AvailabilityAttrs); |
| 8024 | for (const auto &Avail : |
| 8025 | llvm::enumerate(llvm::makeArrayRef(AvailabilityAttrs) |
| 8026 | .take_front(availability_size))) { |
| 8027 | availability[Avail.index()].Platform = |
| 8028 | cxstring::createDup(Avail.value()->getPlatform()->getName()); |
| 8029 | availability[Avail.index()].Introduced = |
| 8030 | convertVersion(Avail.value()->getIntroduced()); |
| 8031 | availability[Avail.index()].Deprecated = |
| 8032 | convertVersion(Avail.value()->getDeprecated()); |
| 8033 | availability[Avail.index()].Obsoleted = |
| 8034 | convertVersion(Avail.value()->getObsoleted()); |
| 8035 | availability[Avail.index()].Unavailable = Avail.value()->getUnavailable(); |
| 8036 | availability[Avail.index()].Message = |
| 8037 | cxstring::createDup(Avail.value()->getMessage()); |
| 8038 | } |
| 8039 | |
| 8040 | return AvailabilityAttrs.size(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8041 | } |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 8042 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8043 | void clang_disposeCXPlatformAvailability(CXPlatformAvailability *availability) { |
| 8044 | clang_disposeString(availability->Platform); |
| 8045 | clang_disposeString(availability->Message); |
| 8046 | } |
| 8047 | |
| 8048 | CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { |
| 8049 | if (clang_isDeclaration(cursor.kind)) |
| 8050 | return getDeclLanguage(cxcursor::getCursorDecl(cursor)); |
| 8051 | |
| 8052 | return CXLanguage_Invalid; |
| 8053 | } |
| 8054 | |
Saleem Abdulrasool | 50bc565 | 2017-09-13 02:15:09 +0000 | [diff] [blame] | 8055 | CXTLSKind clang_getCursorTLSKind(CXCursor cursor) { |
| 8056 | const Decl *D = cxcursor::getCursorDecl(cursor); |
| 8057 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 8058 | switch (VD->getTLSKind()) { |
| 8059 | case VarDecl::TLS_None: |
| 8060 | return CXTLS_None; |
| 8061 | case VarDecl::TLS_Dynamic: |
| 8062 | return CXTLS_Dynamic; |
| 8063 | case VarDecl::TLS_Static: |
| 8064 | return CXTLS_Static; |
| 8065 | } |
| 8066 | } |
| 8067 | |
| 8068 | return CXTLS_None; |
| 8069 | } |
| 8070 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8071 | /// If the given cursor is the "templated" declaration |
| 8072 | /// describing a class or function template, return the class or |
| 8073 | /// function template. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8074 | static const Decl *maybeGetTemplateCursor(const Decl *D) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8075 | if (!D) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8076 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8077 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8078 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8079 | if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate()) |
| 8080 | return FunTmpl; |
| 8081 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8082 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8083 | if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate()) |
| 8084 | return ClassTmpl; |
| 8085 | |
| 8086 | return D; |
| 8087 | } |
| 8088 | |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 8089 | enum CX_StorageClass clang_Cursor_getStorageClass(CXCursor C) { |
| 8090 | StorageClass sc = SC_None; |
| 8091 | const Decl *D = getCursorDecl(C); |
| 8092 | if (D) { |
| 8093 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 8094 | sc = FD->getStorageClass(); |
| 8095 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 8096 | sc = VD->getStorageClass(); |
| 8097 | } else { |
| 8098 | return CX_SC_Invalid; |
| 8099 | } |
| 8100 | } else { |
| 8101 | return CX_SC_Invalid; |
| 8102 | } |
| 8103 | switch (sc) { |
| 8104 | case SC_None: |
| 8105 | return CX_SC_None; |
| 8106 | case SC_Extern: |
| 8107 | return CX_SC_Extern; |
| 8108 | case SC_Static: |
| 8109 | return CX_SC_Static; |
| 8110 | case SC_PrivateExtern: |
| 8111 | return CX_SC_PrivateExtern; |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 8112 | case SC_Auto: |
| 8113 | return CX_SC_Auto; |
| 8114 | case SC_Register: |
| 8115 | return CX_SC_Register; |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 8116 | } |
Kaelyn Takata | ab61e70 | 2014-10-15 18:03:26 +0000 | [diff] [blame] | 8117 | llvm_unreachable("Unhandled storage class!"); |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 8118 | } |
| 8119 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8120 | CXCursor clang_getCursorSemanticParent(CXCursor cursor) { |
| 8121 | if (clang_isDeclaration(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8122 | if (const Decl *D = getCursorDecl(cursor)) { |
| 8123 | const DeclContext *DC = D->getDeclContext(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8124 | if (!DC) |
| 8125 | return clang_getNullCursor(); |
| 8126 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8127 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8128 | getCursorTU(cursor)); |
| 8129 | } |
| 8130 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8131 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8132 | if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8133 | if (const Decl *D = getCursorDecl(cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8134 | return MakeCXCursor(D, getCursorTU(cursor)); |
| 8135 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8136 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8137 | return clang_getNullCursor(); |
| 8138 | } |
| 8139 | |
| 8140 | CXCursor clang_getCursorLexicalParent(CXCursor cursor) { |
| 8141 | if (clang_isDeclaration(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8142 | if (const Decl *D = getCursorDecl(cursor)) { |
| 8143 | const DeclContext *DC = D->getLexicalDeclContext(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8144 | if (!DC) |
| 8145 | return clang_getNullCursor(); |
| 8146 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8147 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8148 | getCursorTU(cursor)); |
| 8149 | } |
| 8150 | } |
| 8151 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8152 | // FIXME: Note that we can't easily compute the lexical context of a |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8153 | // statement or expression, so we return nothing. |
| 8154 | return clang_getNullCursor(); |
| 8155 | } |
| 8156 | |
| 8157 | CXFile clang_getIncludedFile(CXCursor cursor) { |
| 8158 | if (cursor.kind != CXCursor_InclusionDirective) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8159 | return nullptr; |
| 8160 | |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 8161 | const InclusionDirective *ID = getCursorInclusionDirective(cursor); |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 8162 | return const_cast<FileEntry *>(ID->getFile()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8163 | } |
| 8164 | |
Argyrios Kyrtzidis | 9adfd8a | 2013-04-18 22:15:49 +0000 | [diff] [blame] | 8165 | unsigned clang_Cursor_getObjCPropertyAttributes(CXCursor C, unsigned reserved) { |
| 8166 | if (C.kind != CXCursor_ObjCPropertyDecl) |
| 8167 | return CXObjCPropertyAttr_noattr; |
| 8168 | |
| 8169 | unsigned Result = CXObjCPropertyAttr_noattr; |
| 8170 | const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(getCursorDecl(C)); |
Puyan Lotfi | 9721fbf | 2020-04-23 02:20:56 -0400 | [diff] [blame] | 8171 | ObjCPropertyAttribute::Kind Attr = PD->getPropertyAttributesAsWritten(); |
Argyrios Kyrtzidis | 9adfd8a | 2013-04-18 22:15:49 +0000 | [diff] [blame] | 8172 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8173 | #define SET_CXOBJCPROP_ATTR(A) \ |
Puyan Lotfi | 9721fbf | 2020-04-23 02:20:56 -0400 | [diff] [blame] | 8174 | if (Attr & ObjCPropertyAttribute::kind_##A) \ |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8175 | Result |= CXObjCPropertyAttr_##A |
Argyrios Kyrtzidis | 9adfd8a | 2013-04-18 22:15:49 +0000 | [diff] [blame] | 8176 | SET_CXOBJCPROP_ATTR(readonly); |
| 8177 | SET_CXOBJCPROP_ATTR(getter); |
| 8178 | SET_CXOBJCPROP_ATTR(assign); |
| 8179 | SET_CXOBJCPROP_ATTR(readwrite); |
| 8180 | SET_CXOBJCPROP_ATTR(retain); |
| 8181 | SET_CXOBJCPROP_ATTR(copy); |
| 8182 | SET_CXOBJCPROP_ATTR(nonatomic); |
| 8183 | SET_CXOBJCPROP_ATTR(setter); |
| 8184 | SET_CXOBJCPROP_ATTR(atomic); |
| 8185 | SET_CXOBJCPROP_ATTR(weak); |
| 8186 | SET_CXOBJCPROP_ATTR(strong); |
| 8187 | SET_CXOBJCPROP_ATTR(unsafe_unretained); |
Manman Ren | 04fd4d8 | 2016-05-31 23:22:04 +0000 | [diff] [blame] | 8188 | SET_CXOBJCPROP_ATTR(class); |
Argyrios Kyrtzidis | 9adfd8a | 2013-04-18 22:15:49 +0000 | [diff] [blame] | 8189 | #undef SET_CXOBJCPROP_ATTR |
| 8190 | |
| 8191 | return Result; |
| 8192 | } |
| 8193 | |
Michael Wu | 6e88f53 | 2018-08-03 05:38:29 +0000 | [diff] [blame] | 8194 | CXString clang_Cursor_getObjCPropertyGetterName(CXCursor C) { |
| 8195 | if (C.kind != CXCursor_ObjCPropertyDecl) |
| 8196 | return cxstring::createNull(); |
| 8197 | |
| 8198 | const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(getCursorDecl(C)); |
| 8199 | Selector sel = PD->getGetterName(); |
| 8200 | if (sel.isNull()) |
| 8201 | return cxstring::createNull(); |
| 8202 | |
| 8203 | return cxstring::createDup(sel.getAsString()); |
| 8204 | } |
| 8205 | |
| 8206 | CXString clang_Cursor_getObjCPropertySetterName(CXCursor C) { |
| 8207 | if (C.kind != CXCursor_ObjCPropertyDecl) |
| 8208 | return cxstring::createNull(); |
| 8209 | |
| 8210 | const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(getCursorDecl(C)); |
| 8211 | Selector sel = PD->getSetterName(); |
| 8212 | if (sel.isNull()) |
| 8213 | return cxstring::createNull(); |
| 8214 | |
| 8215 | return cxstring::createDup(sel.getAsString()); |
| 8216 | } |
| 8217 | |
Argyrios Kyrtzidis | 9d9bc01 | 2013-04-18 23:29:12 +0000 | [diff] [blame] | 8218 | unsigned clang_Cursor_getObjCDeclQualifiers(CXCursor C) { |
| 8219 | if (!clang_isDeclaration(C.kind)) |
| 8220 | return CXObjCDeclQualifier_None; |
| 8221 | |
| 8222 | Decl::ObjCDeclQualifier QT = Decl::OBJC_TQ_None; |
| 8223 | const Decl *D = getCursorDecl(C); |
| 8224 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 8225 | QT = MD->getObjCDeclQualifier(); |
| 8226 | else if (const ParmVarDecl *PD = dyn_cast<ParmVarDecl>(D)) |
| 8227 | QT = PD->getObjCDeclQualifier(); |
| 8228 | if (QT == Decl::OBJC_TQ_None) |
| 8229 | return CXObjCDeclQualifier_None; |
| 8230 | |
| 8231 | unsigned Result = CXObjCDeclQualifier_None; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8232 | if (QT & Decl::OBJC_TQ_In) |
| 8233 | Result |= CXObjCDeclQualifier_In; |
| 8234 | if (QT & Decl::OBJC_TQ_Inout) |
| 8235 | Result |= CXObjCDeclQualifier_Inout; |
| 8236 | if (QT & Decl::OBJC_TQ_Out) |
| 8237 | Result |= CXObjCDeclQualifier_Out; |
| 8238 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 8239 | Result |= CXObjCDeclQualifier_Bycopy; |
| 8240 | if (QT & Decl::OBJC_TQ_Byref) |
| 8241 | Result |= CXObjCDeclQualifier_Byref; |
| 8242 | if (QT & Decl::OBJC_TQ_Oneway) |
| 8243 | Result |= CXObjCDeclQualifier_Oneway; |
Argyrios Kyrtzidis | 9d9bc01 | 2013-04-18 23:29:12 +0000 | [diff] [blame] | 8244 | |
| 8245 | return Result; |
| 8246 | } |
| 8247 | |
Argyrios Kyrtzidis | 7b50fc5 | 2013-07-05 20:44:37 +0000 | [diff] [blame] | 8248 | unsigned clang_Cursor_isObjCOptional(CXCursor C) { |
| 8249 | if (!clang_isDeclaration(C.kind)) |
| 8250 | return 0; |
| 8251 | |
| 8252 | const Decl *D = getCursorDecl(C); |
| 8253 | if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 8254 | return PD->getPropertyImplementation() == ObjCPropertyDecl::Optional; |
| 8255 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 8256 | return MD->getImplementationControl() == ObjCMethodDecl::Optional; |
| 8257 | |
| 8258 | return 0; |
| 8259 | } |
| 8260 | |
Argyrios Kyrtzidis | 23814e4 | 2013-04-18 23:53:05 +0000 | [diff] [blame] | 8261 | unsigned clang_Cursor_isVariadic(CXCursor C) { |
| 8262 | if (!clang_isDeclaration(C.kind)) |
| 8263 | return 0; |
| 8264 | |
| 8265 | const Decl *D = getCursorDecl(C); |
| 8266 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 8267 | return FD->isVariadic(); |
| 8268 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 8269 | return MD->isVariadic(); |
| 8270 | |
| 8271 | return 0; |
| 8272 | } |
| 8273 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8274 | unsigned clang_Cursor_isExternalSymbol(CXCursor C, CXString *language, |
| 8275 | CXString *definedIn, |
| 8276 | unsigned *isGenerated) { |
Argyrios Kyrtzidis | 0381cc7 | 2017-05-10 15:10:36 +0000 | [diff] [blame] | 8277 | if (!clang_isDeclaration(C.kind)) |
| 8278 | return 0; |
| 8279 | |
| 8280 | const Decl *D = getCursorDecl(C); |
| 8281 | |
Argyrios Kyrtzidis | 11d7048 | 2017-05-20 04:11:33 +0000 | [diff] [blame] | 8282 | if (auto *attr = D->getExternalSourceSymbolAttr()) { |
Argyrios Kyrtzidis | 0381cc7 | 2017-05-10 15:10:36 +0000 | [diff] [blame] | 8283 | if (language) |
| 8284 | *language = cxstring::createDup(attr->getLanguage()); |
| 8285 | if (definedIn) |
| 8286 | *definedIn = cxstring::createDup(attr->getDefinedIn()); |
| 8287 | if (isGenerated) |
| 8288 | *isGenerated = attr->getGeneratedDeclaration(); |
| 8289 | return 1; |
| 8290 | } |
| 8291 | return 0; |
| 8292 | } |
| 8293 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8294 | CXSourceRange clang_Cursor_getCommentRange(CXCursor C) { |
| 8295 | if (!clang_isDeclaration(C.kind)) |
| 8296 | return clang_getNullRange(); |
| 8297 | |
| 8298 | const Decl *D = getCursorDecl(C); |
| 8299 | ASTContext &Context = getCursorContext(C); |
| 8300 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 8301 | if (!RC) |
| 8302 | return clang_getNullRange(); |
| 8303 | |
| 8304 | return cxloc::translateSourceRange(Context, RC->getSourceRange()); |
| 8305 | } |
| 8306 | |
| 8307 | CXString clang_Cursor_getRawCommentText(CXCursor C) { |
| 8308 | if (!clang_isDeclaration(C.kind)) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 8309 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8310 | |
| 8311 | const Decl *D = getCursorDecl(C); |
| 8312 | ASTContext &Context = getCursorContext(C); |
| 8313 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8314 | StringRef RawText = |
| 8315 | RC ? RC->getRawText(Context.getSourceManager()) : StringRef(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8316 | |
| 8317 | // Don't duplicate the string because RawText points directly into source |
| 8318 | // code. |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 8319 | return cxstring::createRef(RawText); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8320 | } |
| 8321 | |
| 8322 | CXString clang_Cursor_getBriefCommentText(CXCursor C) { |
| 8323 | if (!clang_isDeclaration(C.kind)) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 8324 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8325 | |
| 8326 | const Decl *D = getCursorDecl(C); |
| 8327 | const ASTContext &Context = getCursorContext(C); |
| 8328 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 8329 | |
| 8330 | if (RC) { |
| 8331 | StringRef BriefText = RC->getBriefText(Context); |
| 8332 | |
| 8333 | // Don't duplicate the string because RawComment ensures that this memory |
| 8334 | // will not go away. |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 8335 | return cxstring::createRef(BriefText); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8336 | } |
| 8337 | |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 8338 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8339 | } |
| 8340 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8341 | CXModule clang_Cursor_getModule(CXCursor C) { |
| 8342 | if (C.kind == CXCursor_ModuleImportDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8343 | if (const ImportDecl *ImportD = |
| 8344 | dyn_cast_or_null<ImportDecl>(getCursorDecl(C))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8345 | return ImportD->getImportedModule(); |
| 8346 | } |
| 8347 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8348 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8349 | } |
| 8350 | |
Argyrios Kyrtzidis | f6d49c3 | 2014-05-14 23:14:37 +0000 | [diff] [blame] | 8351 | CXModule clang_getModuleForFile(CXTranslationUnit TU, CXFile File) { |
| 8352 | if (isNotUsableTU(TU)) { |
| 8353 | LOG_BAD_TU(TU); |
| 8354 | return nullptr; |
| 8355 | } |
| 8356 | if (!File) |
| 8357 | return nullptr; |
| 8358 | FileEntry *FE = static_cast<FileEntry *>(File); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8359 | |
Argyrios Kyrtzidis | f6d49c3 | 2014-05-14 23:14:37 +0000 | [diff] [blame] | 8360 | ASTUnit &Unit = *cxtu::getASTUnit(TU); |
| 8361 | HeaderSearch &HS = Unit.getPreprocessor().getHeaderSearchInfo(); |
| 8362 | ModuleMap::KnownHeader Header = HS.findModuleForHeader(FE); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8363 | |
Richard Smith | feb54b6 | 2014-10-23 02:01:19 +0000 | [diff] [blame] | 8364 | return Header.getModule(); |
Argyrios Kyrtzidis | f6d49c3 | 2014-05-14 23:14:37 +0000 | [diff] [blame] | 8365 | } |
| 8366 | |
Argyrios Kyrtzidis | 12fdb9e | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 8367 | CXFile clang_Module_getASTFile(CXModule CXMod) { |
| 8368 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8369 | return nullptr; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8370 | Module *Mod = static_cast<Module *>(CXMod); |
Argyrios Kyrtzidis | 12fdb9e | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 8371 | return const_cast<FileEntry *>(Mod->getASTFile()); |
| 8372 | } |
| 8373 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8374 | CXModule clang_Module_getParent(CXModule CXMod) { |
| 8375 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8376 | return nullptr; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8377 | Module *Mod = static_cast<Module *>(CXMod); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8378 | return Mod->Parent; |
| 8379 | } |
| 8380 | |
| 8381 | CXString clang_Module_getName(CXModule CXMod) { |
| 8382 | if (!CXMod) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 8383 | return cxstring::createEmpty(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8384 | Module *Mod = static_cast<Module *>(CXMod); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 8385 | return cxstring::createDup(Mod->Name); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8386 | } |
| 8387 | |
| 8388 | CXString clang_Module_getFullName(CXModule CXMod) { |
| 8389 | if (!CXMod) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 8390 | return cxstring::createEmpty(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8391 | Module *Mod = static_cast<Module *>(CXMod); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 8392 | return cxstring::createDup(Mod->getFullModuleName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8393 | } |
| 8394 | |
Argyrios Kyrtzidis | 884337f | 2014-05-15 04:44:25 +0000 | [diff] [blame] | 8395 | int clang_Module_isSystem(CXModule CXMod) { |
| 8396 | if (!CXMod) |
| 8397 | return 0; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8398 | Module *Mod = static_cast<Module *>(CXMod); |
Argyrios Kyrtzidis | 884337f | 2014-05-15 04:44:25 +0000 | [diff] [blame] | 8399 | return Mod->IsSystem; |
| 8400 | } |
| 8401 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 8402 | unsigned clang_Module_getNumTopLevelHeaders(CXTranslationUnit TU, |
| 8403 | CXModule CXMod) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 8404 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 8405 | LOG_BAD_TU(TU); |
| 8406 | return 0; |
| 8407 | } |
| 8408 | if (!CXMod) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8409 | return 0; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8410 | Module *Mod = static_cast<Module *>(CXMod); |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 8411 | FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager(); |
| 8412 | ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr); |
| 8413 | return TopHeaders.size(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8414 | } |
| 8415 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8416 | CXFile clang_Module_getTopLevelHeader(CXTranslationUnit TU, CXModule CXMod, |
| 8417 | unsigned Index) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 8418 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 8419 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8420 | return nullptr; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 8421 | } |
| 8422 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8423 | return nullptr; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8424 | Module *Mod = static_cast<Module *>(CXMod); |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 8425 | FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8426 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 8427 | ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr); |
| 8428 | if (Index < TopHeaders.size()) |
| 8429 | return const_cast<FileEntry *>(TopHeaders[Index]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8430 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8431 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8432 | } |
| 8433 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8434 | //===----------------------------------------------------------------------===// |
| 8435 | // C++ AST instrospection. |
| 8436 | //===----------------------------------------------------------------------===// |
| 8437 | |
Jonathan Coe | 2956535 | 2016-04-27 12:48:25 +0000 | [diff] [blame] | 8438 | unsigned clang_CXXConstructor_isDefaultConstructor(CXCursor C) { |
| 8439 | if (!clang_isDeclaration(C.kind)) |
| 8440 | return 0; |
| 8441 | |
| 8442 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8443 | const CXXConstructorDecl *Constructor = |
| 8444 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 8445 | return (Constructor && Constructor->isDefaultConstructor()) ? 1 : 0; |
| 8446 | } |
| 8447 | |
| 8448 | unsigned clang_CXXConstructor_isCopyConstructor(CXCursor C) { |
| 8449 | if (!clang_isDeclaration(C.kind)) |
| 8450 | return 0; |
| 8451 | |
| 8452 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8453 | const CXXConstructorDecl *Constructor = |
| 8454 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 8455 | return (Constructor && Constructor->isCopyConstructor()) ? 1 : 0; |
| 8456 | } |
| 8457 | |
| 8458 | unsigned clang_CXXConstructor_isMoveConstructor(CXCursor C) { |
| 8459 | if (!clang_isDeclaration(C.kind)) |
| 8460 | return 0; |
| 8461 | |
| 8462 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8463 | const CXXConstructorDecl *Constructor = |
| 8464 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 8465 | return (Constructor && Constructor->isMoveConstructor()) ? 1 : 0; |
| 8466 | } |
| 8467 | |
| 8468 | unsigned clang_CXXConstructor_isConvertingConstructor(CXCursor C) { |
| 8469 | if (!clang_isDeclaration(C.kind)) |
| 8470 | return 0; |
| 8471 | |
| 8472 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8473 | const CXXConstructorDecl *Constructor = |
| 8474 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 8475 | // Passing 'false' excludes constructors marked 'explicit'. |
| 8476 | return (Constructor && Constructor->isConvertingConstructor(false)) ? 1 : 0; |
| 8477 | } |
| 8478 | |
Saleem Abdulrasool | 6ea75db | 2015-10-27 15:50:22 +0000 | [diff] [blame] | 8479 | unsigned clang_CXXField_isMutable(CXCursor C) { |
| 8480 | if (!clang_isDeclaration(C.kind)) |
| 8481 | return 0; |
| 8482 | |
| 8483 | if (const auto D = cxcursor::getCursorDecl(C)) |
| 8484 | if (const auto FD = dyn_cast_or_null<FieldDecl>(D)) |
| 8485 | return FD->isMutable() ? 1 : 0; |
| 8486 | return 0; |
| 8487 | } |
| 8488 | |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 8489 | unsigned clang_CXXMethod_isPureVirtual(CXCursor C) { |
| 8490 | if (!clang_isDeclaration(C.kind)) |
| 8491 | return 0; |
| 8492 | |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 8493 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 8494 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8495 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 8496 | return (Method && Method->isVirtual() && Method->isPure()) ? 1 : 0; |
| 8497 | } |
| 8498 | |
Dmitri Gribenko | e570ede | 2014-04-07 14:59:13 +0000 | [diff] [blame] | 8499 | unsigned clang_CXXMethod_isConst(CXCursor C) { |
| 8500 | if (!clang_isDeclaration(C.kind)) |
| 8501 | return 0; |
| 8502 | |
| 8503 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8504 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8505 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 8506 | return (Method && Method->getMethodQualifiers().hasConst()) ? 1 : 0; |
Dmitri Gribenko | e570ede | 2014-04-07 14:59:13 +0000 | [diff] [blame] | 8507 | } |
| 8508 | |
Jonathan Coe | 2956535 | 2016-04-27 12:48:25 +0000 | [diff] [blame] | 8509 | unsigned clang_CXXMethod_isDefaulted(CXCursor C) { |
| 8510 | if (!clang_isDeclaration(C.kind)) |
| 8511 | return 0; |
| 8512 | |
| 8513 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8514 | const CXXMethodDecl *Method = |
| 8515 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
| 8516 | return (Method && Method->isDefaulted()) ? 1 : 0; |
| 8517 | } |
| 8518 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8519 | unsigned clang_CXXMethod_isStatic(CXCursor C) { |
| 8520 | if (!clang_isDeclaration(C.kind)) |
| 8521 | return 0; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8522 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8523 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 8524 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8525 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8526 | return (Method && Method->isStatic()) ? 1 : 0; |
| 8527 | } |
| 8528 | |
| 8529 | unsigned clang_CXXMethod_isVirtual(CXCursor C) { |
| 8530 | if (!clang_isDeclaration(C.kind)) |
| 8531 | return 0; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8532 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8533 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 8534 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8535 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8536 | return (Method && Method->isVirtual()) ? 1 : 0; |
| 8537 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8538 | |
Alex Lorenz | 34ccadc | 2017-12-14 22:01:50 +0000 | [diff] [blame] | 8539 | unsigned clang_CXXRecord_isAbstract(CXCursor C) { |
| 8540 | if (!clang_isDeclaration(C.kind)) |
| 8541 | return 0; |
| 8542 | |
| 8543 | const auto *D = cxcursor::getCursorDecl(C); |
| 8544 | const auto *RD = dyn_cast_or_null<CXXRecordDecl>(D); |
| 8545 | if (RD) |
| 8546 | RD = RD->getDefinition(); |
| 8547 | return (RD && RD->isAbstract()) ? 1 : 0; |
| 8548 | } |
| 8549 | |
Alex Lorenz | ff7f42e | 2017-07-12 11:35:11 +0000 | [diff] [blame] | 8550 | unsigned clang_EnumDecl_isScoped(CXCursor C) { |
| 8551 | if (!clang_isDeclaration(C.kind)) |
| 8552 | return 0; |
| 8553 | |
| 8554 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8555 | auto *Enum = dyn_cast_or_null<EnumDecl>(D); |
| 8556 | return (Enum && Enum->isScoped()) ? 1 : 0; |
| 8557 | } |
| 8558 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8559 | //===----------------------------------------------------------------------===// |
| 8560 | // Attribute introspection. |
| 8561 | //===----------------------------------------------------------------------===// |
| 8562 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8563 | CXType clang_getIBOutletCollectionType(CXCursor C) { |
| 8564 | if (C.kind != CXCursor_IBOutletCollectionAttr) |
| 8565 | return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C)); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8566 | |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 8567 | const IBOutletCollectionAttr *A = |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8568 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); |
| 8569 | |
| 8570 | return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8571 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8572 | |
| 8573 | //===----------------------------------------------------------------------===// |
| 8574 | // Inspecting memory usage. |
| 8575 | //===----------------------------------------------------------------------===// |
| 8576 | |
| 8577 | typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries; |
| 8578 | |
| 8579 | static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries, |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8580 | enum CXTUResourceUsageKind k, |
| 8581 | unsigned long amount) { |
| 8582 | CXTUResourceUsageEntry entry = {k, amount}; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8583 | entries.push_back(entry); |
| 8584 | } |
| 8585 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8586 | const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) { |
| 8587 | const char *str = ""; |
| 8588 | switch (kind) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8589 | case CXTUResourceUsage_AST: |
| 8590 | str = "ASTContext: expressions, declarations, and types"; |
| 8591 | break; |
| 8592 | case CXTUResourceUsage_Identifiers: |
| 8593 | str = "ASTContext: identifiers"; |
| 8594 | break; |
| 8595 | case CXTUResourceUsage_Selectors: |
| 8596 | str = "ASTContext: selectors"; |
| 8597 | break; |
| 8598 | case CXTUResourceUsage_GlobalCompletionResults: |
| 8599 | str = "Code completion: cached global results"; |
| 8600 | break; |
| 8601 | case CXTUResourceUsage_SourceManagerContentCache: |
| 8602 | str = "SourceManager: content cache allocator"; |
| 8603 | break; |
| 8604 | case CXTUResourceUsage_AST_SideTables: |
| 8605 | str = "ASTContext: side tables"; |
| 8606 | break; |
| 8607 | case CXTUResourceUsage_SourceManager_Membuffer_Malloc: |
| 8608 | str = "SourceManager: malloc'ed memory buffers"; |
| 8609 | break; |
| 8610 | case CXTUResourceUsage_SourceManager_Membuffer_MMap: |
| 8611 | str = "SourceManager: mmap'ed memory buffers"; |
| 8612 | break; |
| 8613 | case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc: |
| 8614 | str = "ExternalASTSource: malloc'ed memory buffers"; |
| 8615 | break; |
| 8616 | case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap: |
| 8617 | str = "ExternalASTSource: mmap'ed memory buffers"; |
| 8618 | break; |
| 8619 | case CXTUResourceUsage_Preprocessor: |
| 8620 | str = "Preprocessor: malloc'ed memory"; |
| 8621 | break; |
| 8622 | case CXTUResourceUsage_PreprocessingRecord: |
| 8623 | str = "Preprocessor: PreprocessingRecord"; |
| 8624 | break; |
| 8625 | case CXTUResourceUsage_SourceManager_DataStructures: |
| 8626 | str = "SourceManager: data structures and tables"; |
| 8627 | break; |
| 8628 | case CXTUResourceUsage_Preprocessor_HeaderSearch: |
| 8629 | str = "Preprocessor: header search tables"; |
| 8630 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8631 | } |
| 8632 | return str; |
| 8633 | } |
| 8634 | |
| 8635 | CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 8636 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 8637 | LOG_BAD_TU(TU); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8638 | CXTUResourceUsage usage = {(void *)nullptr, 0, nullptr}; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8639 | return usage; |
| 8640 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8641 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8642 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 8643 | std::unique_ptr<MemUsageEntries> entries(new MemUsageEntries()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8644 | ASTContext &astContext = astUnit->getASTContext(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8645 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8646 | // How much memory is used by AST nodes and types? |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8647 | createCXTUResourceUsageEntry( |
| 8648 | *entries, CXTUResourceUsage_AST, |
| 8649 | (unsigned long)astContext.getASTAllocatedMemory()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8650 | |
| 8651 | // How much memory is used by identifiers? |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8652 | createCXTUResourceUsageEntry( |
| 8653 | *entries, CXTUResourceUsage_Identifiers, |
| 8654 | (unsigned long)astContext.Idents.getAllocator().getTotalMemory()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8655 | |
| 8656 | // How much memory is used for selectors? |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8657 | createCXTUResourceUsageEntry( |
| 8658 | *entries, CXTUResourceUsage_Selectors, |
| 8659 | (unsigned long)astContext.Selectors.getTotalMemory()); |
| 8660 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8661 | // How much memory is used by ASTContext's side tables? |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8662 | createCXTUResourceUsageEntry( |
| 8663 | *entries, CXTUResourceUsage_AST_SideTables, |
| 8664 | (unsigned long)astContext.getSideTableAllocatedMemory()); |
| 8665 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8666 | // How much memory is used for caching global code completion results? |
| 8667 | unsigned long completionBytes = 0; |
| 8668 | if (GlobalCodeCompletionAllocator *completionAllocator = |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8669 | astUnit->getCachedCompletionAllocator().get()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8670 | completionBytes = completionAllocator->getTotalMemory(); |
| 8671 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8672 | createCXTUResourceUsageEntry( |
| 8673 | *entries, CXTUResourceUsage_GlobalCompletionResults, completionBytes); |
| 8674 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8675 | // How much memory is being used by SourceManager's content cache? |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8676 | createCXTUResourceUsageEntry( |
| 8677 | *entries, CXTUResourceUsage_SourceManagerContentCache, |
| 8678 | (unsigned long)astContext.getSourceManager().getContentCacheSize()); |
| 8679 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8680 | // How much memory is being used by the MemoryBuffer's in SourceManager? |
| 8681 | const SourceManager::MemoryBufferSizes &srcBufs = |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8682 | astUnit->getSourceManager().getMemoryBufferSizes(); |
| 8683 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8684 | createCXTUResourceUsageEntry(*entries, |
| 8685 | CXTUResourceUsage_SourceManager_Membuffer_Malloc, |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8686 | (unsigned long)srcBufs.malloc_bytes); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8687 | createCXTUResourceUsageEntry(*entries, |
| 8688 | CXTUResourceUsage_SourceManager_Membuffer_MMap, |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8689 | (unsigned long)srcBufs.mmap_bytes); |
| 8690 | createCXTUResourceUsageEntry( |
| 8691 | *entries, CXTUResourceUsage_SourceManager_DataStructures, |
| 8692 | (unsigned long)astContext.getSourceManager().getDataStructureSizes()); |
| 8693 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8694 | // How much memory is being used by the ExternalASTSource? |
| 8695 | if (ExternalASTSource *esrc = astContext.getExternalSource()) { |
| 8696 | const ExternalASTSource::MemoryBufferSizes &sizes = |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8697 | esrc->getMemoryBufferSizes(); |
| 8698 | |
| 8699 | createCXTUResourceUsageEntry( |
| 8700 | *entries, CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc, |
| 8701 | (unsigned long)sizes.malloc_bytes); |
| 8702 | createCXTUResourceUsageEntry( |
| 8703 | *entries, CXTUResourceUsage_ExternalASTSource_Membuffer_MMap, |
| 8704 | (unsigned long)sizes.mmap_bytes); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8705 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8706 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8707 | // How much memory is being used by the Preprocessor? |
| 8708 | Preprocessor &pp = astUnit->getPreprocessor(); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8709 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Preprocessor, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8710 | pp.getTotalMemory()); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8711 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8712 | if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) { |
| 8713 | createCXTUResourceUsageEntry(*entries, |
| 8714 | CXTUResourceUsage_PreprocessingRecord, |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8715 | pRec->getTotalMemory()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8716 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8717 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8718 | createCXTUResourceUsageEntry(*entries, |
| 8719 | CXTUResourceUsage_Preprocessor_HeaderSearch, |
| 8720 | pp.getHeaderSearchInfo().getTotalMemory()); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8721 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8722 | CXTUResourceUsage usage = {(void *)entries.get(), (unsigned)entries->size(), |
| 8723 | !entries->empty() ? &(*entries)[0] : nullptr}; |
Eric Fiselier | e95fc44 | 2016-11-14 07:03:50 +0000 | [diff] [blame] | 8724 | (void)entries.release(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8725 | return usage; |
| 8726 | } |
| 8727 | |
| 8728 | void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) { |
| 8729 | if (usage.data) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8730 | delete (MemUsageEntries *)usage.data; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8731 | } |
| 8732 | |
Argyrios Kyrtzidis | 0e282ef | 2013-12-06 18:55:45 +0000 | [diff] [blame] | 8733 | CXSourceRangeList *clang_getSkippedRanges(CXTranslationUnit TU, CXFile file) { |
| 8734 | CXSourceRangeList *skipped = new CXSourceRangeList; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8735 | skipped->count = 0; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8736 | skipped->ranges = nullptr; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8737 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 8738 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 8739 | LOG_BAD_TU(TU); |
| 8740 | return skipped; |
| 8741 | } |
| 8742 | |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8743 | if (!file) |
| 8744 | return skipped; |
| 8745 | |
| 8746 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8747 | PreprocessingRecord *ppRec = |
| 8748 | astUnit->getPreprocessor().getPreprocessingRecord(); |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8749 | if (!ppRec) |
| 8750 | return skipped; |
| 8751 | |
| 8752 | ASTContext &Ctx = astUnit->getASTContext(); |
| 8753 | SourceManager &sm = Ctx.getSourceManager(); |
| 8754 | FileEntry *fileEntry = static_cast<FileEntry *>(file); |
| 8755 | FileID wantedFileID = sm.translateFile(fileEntry); |
Cameron Desrochers | b60f1b6 | 2018-01-15 19:14:16 +0000 | [diff] [blame] | 8756 | bool isMainFile = wantedFileID == sm.getMainFileID(); |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8757 | |
| 8758 | const std::vector<SourceRange> &SkippedRanges = ppRec->getSkippedRanges(); |
| 8759 | std::vector<SourceRange> wantedRanges; |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8760 | for (std::vector<SourceRange>::const_iterator i = SkippedRanges.begin(), |
| 8761 | ei = SkippedRanges.end(); |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8762 | i != ei; ++i) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8763 | if (sm.getFileID(i->getBegin()) == wantedFileID || |
| 8764 | sm.getFileID(i->getEnd()) == wantedFileID) |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8765 | wantedRanges.push_back(*i); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8766 | else if (isMainFile && (astUnit->isInPreambleFileID(i->getBegin()) || |
| 8767 | astUnit->isInPreambleFileID(i->getEnd()))) |
Cameron Desrochers | b60f1b6 | 2018-01-15 19:14:16 +0000 | [diff] [blame] | 8768 | wantedRanges.push_back(*i); |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8769 | } |
| 8770 | |
| 8771 | skipped->count = wantedRanges.size(); |
| 8772 | skipped->ranges = new CXSourceRange[skipped->count]; |
| 8773 | for (unsigned i = 0, ei = skipped->count; i != ei; ++i) |
| 8774 | skipped->ranges[i] = cxloc::translateSourceRange(Ctx, wantedRanges[i]); |
| 8775 | |
| 8776 | return skipped; |
| 8777 | } |
| 8778 | |
Cameron Desrochers | d809128 | 2016-08-18 15:43:55 +0000 | [diff] [blame] | 8779 | CXSourceRangeList *clang_getAllSkippedRanges(CXTranslationUnit TU) { |
| 8780 | CXSourceRangeList *skipped = new CXSourceRangeList; |
| 8781 | skipped->count = 0; |
| 8782 | skipped->ranges = nullptr; |
| 8783 | |
| 8784 | if (isNotUsableTU(TU)) { |
| 8785 | LOG_BAD_TU(TU); |
| 8786 | return skipped; |
| 8787 | } |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8788 | |
Cameron Desrochers | d809128 | 2016-08-18 15:43:55 +0000 | [diff] [blame] | 8789 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8790 | PreprocessingRecord *ppRec = |
| 8791 | astUnit->getPreprocessor().getPreprocessingRecord(); |
Cameron Desrochers | d809128 | 2016-08-18 15:43:55 +0000 | [diff] [blame] | 8792 | if (!ppRec) |
| 8793 | return skipped; |
| 8794 | |
| 8795 | ASTContext &Ctx = astUnit->getASTContext(); |
| 8796 | |
| 8797 | const std::vector<SourceRange> &SkippedRanges = ppRec->getSkippedRanges(); |
| 8798 | |
| 8799 | skipped->count = SkippedRanges.size(); |
| 8800 | skipped->ranges = new CXSourceRange[skipped->count]; |
| 8801 | for (unsigned i = 0, ei = skipped->count; i != ei; ++i) |
| 8802 | skipped->ranges[i] = cxloc::translateSourceRange(Ctx, SkippedRanges[i]); |
| 8803 | |
| 8804 | return skipped; |
| 8805 | } |
| 8806 | |
Argyrios Kyrtzidis | 0e282ef | 2013-12-06 18:55:45 +0000 | [diff] [blame] | 8807 | void clang_disposeSourceRangeList(CXSourceRangeList *ranges) { |
| 8808 | if (ranges) { |
| 8809 | delete[] ranges->ranges; |
| 8810 | delete ranges; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8811 | } |
| 8812 | } |
| 8813 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8814 | void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) { |
| 8815 | CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU); |
| 8816 | for (unsigned I = 0; I != Usage.numEntries; ++I) |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8817 | fprintf(stderr, " %s: %lu\n", |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8818 | clang_getTUResourceUsageName(Usage.entries[I].kind), |
| 8819 | Usage.entries[I].amount); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8820 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8821 | clang_disposeCXTUResourceUsage(Usage); |
| 8822 | } |
| 8823 | |
| 8824 | //===----------------------------------------------------------------------===// |
| 8825 | // Misc. utility functions. |
| 8826 | //===----------------------------------------------------------------------===// |
| 8827 | |
Richard Smith | 0a7b297 | 2018-07-03 21:34:13 +0000 | [diff] [blame] | 8828 | /// Default to using our desired 8 MB stack size on "safety" threads. |
| 8829 | static unsigned SafetyStackThreadSize = DesiredStackSize; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8830 | |
| 8831 | namespace clang { |
| 8832 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 8833 | bool RunSafely(llvm::CrashRecoveryContext &CRC, llvm::function_ref<void()> Fn, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8834 | unsigned Size) { |
| 8835 | if (!Size) |
| 8836 | Size = GetSafetyThreadStackSize(); |
Erik Verbruggen | 3cc3911 | 2017-11-14 09:34:39 +0000 | [diff] [blame] | 8837 | if (Size && !getenv("LIBCLANG_NOTHREADS")) |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 8838 | return CRC.RunSafelyOnThread(Fn, Size); |
| 8839 | return CRC.RunSafely(Fn); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8840 | } |
| 8841 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8842 | unsigned GetSafetyThreadStackSize() { return SafetyStackThreadSize; } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8843 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8844 | void SetSafetyThreadStackSize(unsigned Value) { SafetyStackThreadSize = Value; } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8845 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8846 | } // namespace clang |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8847 | |
| 8848 | void clang::setThreadBackgroundPriority() { |
| 8849 | if (getenv("LIBCLANG_BGPRIO_DISABLE")) |
| 8850 | return; |
| 8851 | |
Nico Weber | 18cfd9f | 2019-04-21 19:18:41 +0000 | [diff] [blame] | 8852 | #if LLVM_ENABLE_THREADS |
Kadir Cetinkaya | b8f82ca | 2019-04-18 13:49:20 +0000 | [diff] [blame] | 8853 | llvm::set_thread_priority(llvm::ThreadPriority::Background); |
Nico Weber | 18cfd9f | 2019-04-21 19:18:41 +0000 | [diff] [blame] | 8854 | #endif |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8855 | } |
| 8856 | |
| 8857 | void cxindex::printDiagsToStderr(ASTUnit *Unit) { |
| 8858 | if (!Unit) |
| 8859 | return; |
| 8860 | |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8861 | for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), |
| 8862 | DEnd = Unit->stored_diag_end(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8863 | D != DEnd; ++D) { |
Ben Langmuir | 749323f | 2014-04-22 17:40:12 +0000 | [diff] [blame] | 8864 | CXStoredDiagnostic Diag(*D, Unit->getLangOpts()); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8865 | CXString Msg = |
| 8866 | clang_formatDiagnostic(&Diag, clang_defaultDiagnosticDisplayOptions()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8867 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 8868 | clang_disposeString(Msg); |
| 8869 | } |
Nico Weber | 1865df4 | 2018-04-27 19:11:14 +0000 | [diff] [blame] | 8870 | #ifdef _WIN32 |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8871 | // On Windows, force a flush, since there may be multiple copies of |
| 8872 | // stderr and stdout in the file system, all with different buffers |
| 8873 | // but writing to the same device. |
| 8874 | fflush(stderr); |
| 8875 | #endif |
| 8876 | } |
| 8877 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8878 | MacroInfo *cxindex::getMacroInfo(const IdentifierInfo &II, |
| 8879 | SourceLocation MacroDefLoc, |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8880 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8881 | if (MacroDefLoc.isInvalid() || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8882 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8883 | if (!II.hadMacroDefinition()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8884 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8885 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8886 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 8887 | Preprocessor &PP = Unit->getPreprocessor(); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 8888 | MacroDirective *MD = PP.getLocalMacroDirectiveHistory(&II); |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 8889 | if (MD) { |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 8890 | for (MacroDirective::DefInfo Def = MD->getDefinition(); Def; |
| 8891 | Def = Def.getPreviousDefinition()) { |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 8892 | if (MacroDefLoc == Def.getMacroInfo()->getDefinitionLoc()) |
| 8893 | return Def.getMacroInfo(); |
| 8894 | } |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8895 | } |
| 8896 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8897 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8898 | } |
| 8899 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 8900 | const MacroInfo *cxindex::getMacroInfo(const MacroDefinitionRecord *MacroDef, |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 8901 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8902 | if (!MacroDef || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8903 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8904 | const IdentifierInfo *II = MacroDef->getName(); |
| 8905 | if (!II) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8906 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8907 | |
| 8908 | return getMacroInfo(*II, MacroDef->getLocation(), TU); |
| 8909 | } |
| 8910 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 8911 | MacroDefinitionRecord * |
| 8912 | cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, const Token &Tok, |
| 8913 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8914 | if (!MI || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8915 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8916 | if (Tok.isNot(tok::raw_identifier)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8917 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8918 | |
| 8919 | if (MI->getNumTokens() == 0) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8920 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8921 | SourceRange DefRange(MI->getReplacementToken(0).getLocation(), |
| 8922 | MI->getDefinitionEndLoc()); |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8923 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8924 | |
| 8925 | // Check that the token is inside the definition and not its argument list. |
| 8926 | SourceManager &SM = Unit->getSourceManager(); |
| 8927 | if (SM.isBeforeInTranslationUnit(Tok.getLocation(), DefRange.getBegin())) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8928 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8929 | if (SM.isBeforeInTranslationUnit(DefRange.getEnd(), Tok.getLocation())) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8930 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8931 | |
| 8932 | Preprocessor &PP = Unit->getPreprocessor(); |
| 8933 | PreprocessingRecord *PPRec = PP.getPreprocessingRecord(); |
| 8934 | if (!PPRec) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8935 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8936 | |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 8937 | IdentifierInfo &II = PP.getIdentifierTable().get(Tok.getRawIdentifier()); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8938 | if (!II.hadMacroDefinition()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8939 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8940 | |
| 8941 | // Check that the identifier is not one of the macro arguments. |
Faisal Vali | ac506d7 | 2017-07-17 17:18:43 +0000 | [diff] [blame] | 8942 | if (std::find(MI->param_begin(), MI->param_end(), &II) != MI->param_end()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8943 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8944 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 8945 | MacroDirective *InnerMD = PP.getLocalMacroDirectiveHistory(&II); |
Argyrios Kyrtzidis | 09c9e81 | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 8946 | if (!InnerMD) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8947 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8948 | |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 8949 | return PPRec->findMacroDefinition(InnerMD->getMacroInfo()); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8950 | } |
| 8951 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 8952 | MacroDefinitionRecord * |
| 8953 | cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, SourceLocation Loc, |
| 8954 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8955 | if (Loc.isInvalid() || !MI || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8956 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8957 | |
| 8958 | if (MI->getNumTokens() == 0) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8959 | return nullptr; |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8960 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8961 | Preprocessor &PP = Unit->getPreprocessor(); |
| 8962 | if (!PP.getPreprocessingRecord()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8963 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8964 | Loc = Unit->getSourceManager().getSpellingLoc(Loc); |
| 8965 | Token Tok; |
| 8966 | if (PP.getRawToken(Loc, Tok)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8967 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8968 | |
| 8969 | return checkForMacroInMacroDefinition(MI, Tok, TU); |
| 8970 | } |
| 8971 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8972 | CXString clang_getClangVersion() { |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 8973 | return cxstring::createDup(getClangFullVersion()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8974 | } |
| 8975 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8976 | Logger &cxindex::Logger::operator<<(CXTranslationUnit TU) { |
| 8977 | if (TU) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8978 | if (ASTUnit *Unit = cxtu::getASTUnit(TU)) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8979 | LogOS << '<' << Unit->getMainFileName() << '>'; |
Argyrios Kyrtzidis | 37f2ab4 | 2013-03-05 20:21:14 +0000 | [diff] [blame] | 8980 | if (Unit->isMainFileAST()) |
| 8981 | LogOS << " (" << Unit->getASTFileName() << ')'; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8982 | return *this; |
| 8983 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 8984 | } else { |
| 8985 | LogOS << "<NULL TU>"; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8986 | } |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8987 | return *this; |
| 8988 | } |
| 8989 | |
Argyrios Kyrtzidis | ba4b5f8 | 2013-03-08 02:32:26 +0000 | [diff] [blame] | 8990 | Logger &cxindex::Logger::operator<<(const FileEntry *FE) { |
| 8991 | *this << FE->getName(); |
| 8992 | return *this; |
| 8993 | } |
| 8994 | |
| 8995 | Logger &cxindex::Logger::operator<<(CXCursor cursor) { |
| 8996 | CXString cursorName = clang_getCursorDisplayName(cursor); |
| 8997 | *this << cursorName << "@" << clang_getCursorLocation(cursor); |
| 8998 | clang_disposeString(cursorName); |
| 8999 | return *this; |
| 9000 | } |
| 9001 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9002 | Logger &cxindex::Logger::operator<<(CXSourceLocation Loc) { |
| 9003 | CXFile File; |
| 9004 | unsigned Line, Column; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 9005 | clang_getFileLocation(Loc, &File, &Line, &Column, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9006 | CXString FileName = clang_getFileName(File); |
| 9007 | *this << llvm::format("(%s:%d:%d)", clang_getCString(FileName), Line, Column); |
| 9008 | clang_disposeString(FileName); |
| 9009 | return *this; |
| 9010 | } |
| 9011 | |
| 9012 | Logger &cxindex::Logger::operator<<(CXSourceRange range) { |
| 9013 | CXSourceLocation BLoc = clang_getRangeStart(range); |
| 9014 | CXSourceLocation ELoc = clang_getRangeEnd(range); |
| 9015 | |
| 9016 | CXFile BFile; |
| 9017 | unsigned BLine, BColumn; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 9018 | clang_getFileLocation(BLoc, &BFile, &BLine, &BColumn, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9019 | |
| 9020 | CXFile EFile; |
| 9021 | unsigned ELine, EColumn; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 9022 | clang_getFileLocation(ELoc, &EFile, &ELine, &EColumn, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9023 | |
| 9024 | CXString BFileName = clang_getFileName(BFile); |
| 9025 | if (BFile == EFile) { |
| 9026 | *this << llvm::format("[%s %d:%d-%d:%d]", clang_getCString(BFileName), |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 9027 | BLine, BColumn, ELine, EColumn); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9028 | } else { |
| 9029 | CXString EFileName = clang_getFileName(EFile); |
Michael Kruse | 7520cf0 | 2020-03-25 09:26:14 -0500 | [diff] [blame] | 9030 | *this << llvm::format("[%s:%d:%d - ", clang_getCString(BFileName), BLine, |
| 9031 | BColumn) |
| 9032 | << llvm::format("%s:%d:%d]", clang_getCString(EFileName), ELine, |
| 9033 | EColumn); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9034 | clang_disposeString(EFileName); |
| 9035 | } |
| 9036 | clang_disposeString(BFileName); |
| 9037 | return *this; |
| 9038 | } |
| 9039 | |
| 9040 | Logger &cxindex::Logger::operator<<(CXString Str) { |
| 9041 | *this << clang_getCString(Str); |
| 9042 | return *this; |
| 9043 | } |
| 9044 | |
| 9045 | Logger &cxindex::Logger::operator<<(const llvm::format_object_base &Fmt) { |
| 9046 | LogOS << Fmt; |
| 9047 | return *this; |
| 9048 | } |
| 9049 | |
Benjamin Kramer | 762bc33 | 2019-08-07 14:44:40 +0000 | [diff] [blame] | 9050 | static llvm::ManagedStatic<std::mutex> LoggingMutex; |
Chandler Carruth | 37ad258 | 2014-06-27 15:14:39 +0000 | [diff] [blame] | 9051 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9052 | cxindex::Logger::~Logger() { |
Benjamin Kramer | 762bc33 | 2019-08-07 14:44:40 +0000 | [diff] [blame] | 9053 | std::lock_guard<std::mutex> L(*LoggingMutex); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9054 | |
| 9055 | static llvm::TimeRecord sBeginTR = llvm::TimeRecord::getCurrentTime(); |
| 9056 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9057 | raw_ostream &OS = llvm::errs(); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9058 | OS << "[libclang:" << Name << ':'; |
| 9059 | |
Alp Toker | 1a86ad2 | 2014-07-06 06:24:00 +0000 | [diff] [blame] | 9060 | #ifdef USE_DARWIN_THREADS |
| 9061 | // TODO: Portability. |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9062 | mach_port_t tid = pthread_mach_thread_np(pthread_self()); |
| 9063 | OS << tid << ':'; |
| 9064 | #endif |
| 9065 | |
| 9066 | llvm::TimeRecord TR = llvm::TimeRecord::getCurrentTime(); |
| 9067 | OS << llvm::format("%7.4f] ", TR.getWallTime() - sBeginTR.getWallTime()); |
Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 9068 | OS << Msg << '\n'; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9069 | |
| 9070 | if (Trace) { |
Zachary Turner | 1fe2a8d | 2015-03-05 19:15:09 +0000 | [diff] [blame] | 9071 | llvm::sys::PrintStackTrace(OS); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9072 | OS << "--------------------------------------------------\n"; |
| 9073 | } |
| 9074 | } |
Ivan Donchevskii | c592913 | 2018-12-10 15:58:50 +0000 | [diff] [blame] | 9075 | |
| 9076 | #ifdef CLANG_TOOL_EXTRA_BUILD |
| 9077 | // This anchor is used to force the linker to link the clang-tidy plugin. |
| 9078 | extern volatile int ClangTidyPluginAnchorSource; |
| 9079 | static int LLVM_ATTRIBUTE_UNUSED ClangTidyPluginAnchorDestination = |
| 9080 | ClangTidyPluginAnchorSource; |
| 9081 | |
| 9082 | // This anchor is used to force the linker to link the clang-include-fixer |
| 9083 | // plugin. |
| 9084 | extern volatile int ClangIncludeFixerPluginAnchorSource; |
| 9085 | static int LLVM_ATTRIBUTE_UNUSED ClangIncludeFixerPluginAnchorDestination = |
| 9086 | ClangIncludeFixerPluginAnchorSource; |
| 9087 | #endif |