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" |
Jan Korous | 7e36ecd | 2019-09-05 20:33:52 +0000 | [diff] [blame] | 25 | #include "clang/AST/Mangle.h" |
Alexey Bataev | 9559834 | 2020-02-10 15:49:05 -0500 | [diff] [blame] | 26 | #include "clang/AST/OpenMPClause.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 27 | #include "clang/AST/StmtVisitor.h" |
| 28 | #include "clang/Basic/Diagnostic.h" |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 29 | #include "clang/Basic/DiagnosticCategories.h" |
| 30 | #include "clang/Basic/DiagnosticIDs.h" |
Richard Smith | 0a7b297 | 2018-07-03 21:34:13 +0000 | [diff] [blame] | 31 | #include "clang/Basic/Stack.h" |
Emilio Cobos Alvarez | 485ad42 | 2017-04-28 15:56:39 +0000 | [diff] [blame] | 32 | #include "clang/Basic/TargetInfo.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 33 | #include "clang/Basic/Version.h" |
| 34 | #include "clang/Frontend/ASTUnit.h" |
| 35 | #include "clang/Frontend/CompilerInstance.h" |
Dmitri Gribenko | 9e60511 | 2013-11-13 22:16:51 +0000 | [diff] [blame] | 36 | #include "clang/Index/CommentToXML.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 37 | #include "clang/Lex/HeaderSearch.h" |
| 38 | #include "clang/Lex/Lexer.h" |
| 39 | #include "clang/Lex/PreprocessingRecord.h" |
| 40 | #include "clang/Lex/Preprocessor.h" |
| 41 | #include "llvm/ADT/Optional.h" |
| 42 | #include "llvm/ADT/STLExtras.h" |
| 43 | #include "llvm/ADT/StringSwitch.h" |
Alp Toker | 1d257e1 | 2014-06-04 03:28:55 +0000 | [diff] [blame] | 44 | #include "llvm/Config/llvm-config.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Compiler.h" |
| 46 | #include "llvm/Support/CrashRecoveryContext.h" |
Chandler Carruth | 4b41745 | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Format.h" |
Chandler Carruth | 37ad258 | 2014-06-27 15:14:39 +0000 | [diff] [blame] | 48 | #include "llvm/Support/ManagedStatic.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 49 | #include "llvm/Support/MemoryBuffer.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Program.h" |
| 51 | #include "llvm/Support/SaveAndRestore.h" |
| 52 | #include "llvm/Support/Signals.h" |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 53 | #include "llvm/Support/TargetSelect.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 54 | #include "llvm/Support/Threading.h" |
| 55 | #include "llvm/Support/Timer.h" |
| 56 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 762bc33 | 2019-08-07 14:44:40 +0000 | [diff] [blame] | 57 | #include <mutex> |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 58 | |
Alp Toker | 1a86ad2 | 2014-07-06 06:24:00 +0000 | [diff] [blame] | 59 | #if LLVM_ENABLE_THREADS != 0 && defined(__APPLE__) |
| 60 | #define USE_DARWIN_THREADS |
| 61 | #endif |
| 62 | |
| 63 | #ifdef USE_DARWIN_THREADS |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 64 | #include <pthread.h> |
| 65 | #endif |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 66 | |
| 67 | using namespace clang; |
| 68 | using namespace clang::cxcursor; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 69 | using namespace clang::cxtu; |
| 70 | using namespace clang::cxindex; |
| 71 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 72 | CXTranslationUnit cxtu::MakeCXTranslationUnit(CIndexer *CIdx, |
| 73 | std::unique_ptr<ASTUnit> AU) { |
Dmitri Gribenko | d36209e | 2013-01-26 21:32:42 +0000 | [diff] [blame] | 74 | if (!AU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 75 | return nullptr; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 76 | assert(CIdx); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 77 | CXTranslationUnit D = new CXTranslationUnitImpl(); |
| 78 | D->CIdx = CIdx; |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 79 | D->TheASTUnit = AU.release(); |
Dmitri Gribenko | 7489521 | 2013-02-03 13:52:47 +0000 | [diff] [blame] | 80 | D->StringPool = new cxstring::CXStringPool(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 81 | D->Diagnostics = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 82 | D->OverridenCursorsPool = createOverridenCXCursorsPool(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 83 | D->CommentToXML = nullptr; |
Alex Lorenz | 690f0e2 | 2017-12-07 20:37:50 +0000 | [diff] [blame] | 84 | D->ParsingOptions = 0; |
| 85 | D->Arguments = {}; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 86 | return D; |
| 87 | } |
| 88 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 89 | bool cxtu::isASTReadError(ASTUnit *AU) { |
| 90 | for (ASTUnit::stored_diag_iterator D = AU->stored_diag_begin(), |
| 91 | DEnd = AU->stored_diag_end(); |
| 92 | D != DEnd; ++D) { |
| 93 | if (D->getLevel() >= DiagnosticsEngine::Error && |
| 94 | DiagnosticIDs::getCategoryNumberForDiag(D->getID()) == |
| 95 | diag::DiagCat_AST_Deserialization_Issue) |
| 96 | return true; |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 101 | cxtu::CXTUOwner::~CXTUOwner() { |
| 102 | if (TU) |
| 103 | clang_disposeTranslationUnit(TU); |
| 104 | } |
| 105 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 106 | /// Compare two source ranges to determine their relative position in |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 107 | /// the translation unit. |
| 108 | static RangeComparisonResult RangeCompare(SourceManager &SM, |
| 109 | SourceRange R1, |
| 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(); |
| 150 | if (EndLoc.isValid() && EndLoc.isMacroID() && !SM.isMacroArgExpansion(EndLoc)) { |
| 151 | CharSourceRange Expansion = SM.getExpansionRange(EndLoc); |
| 152 | EndLoc = Expansion.getEnd(); |
| 153 | IsTokenRange = Expansion.isTokenRange(); |
| 154 | } |
| 155 | if (IsTokenRange && EndLoc.isValid()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 156 | unsigned Length = Lexer::MeasureTokenLength(SM.getSpellingLoc(EndLoc), |
| 157 | SM, LangOpts); |
| 158 | EndLoc = EndLoc.getLocWithOffset(Length); |
| 159 | } |
| 160 | |
Bill Wendling | eade362 | 2013-01-23 08:25:41 +0000 | [diff] [blame] | 161 | CXSourceRange Result = { |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 162 | { &SM, &LangOpts }, |
Bill Wendling | eade362 | 2013-01-23 08:25:41 +0000 | [diff] [blame] | 163 | R.getBegin().getRawEncoding(), |
| 164 | EndLoc.getRawEncoding() |
| 165 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 166 | return Result; |
| 167 | } |
| 168 | |
| 169 | //===----------------------------------------------------------------------===// |
| 170 | // Cursor visitor. |
| 171 | //===----------------------------------------------------------------------===// |
| 172 | |
| 173 | static SourceRange getRawCursorExtent(CXCursor C); |
| 174 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr); |
| 175 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 176 | RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { |
| 177 | return RangeCompare(AU->getSourceManager(), R, RegionOfInterest); |
| 178 | } |
| 179 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 180 | /// Visit the given cursor and, if requested by the visitor, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 181 | /// its children. |
| 182 | /// |
| 183 | /// \param Cursor the cursor to visit. |
| 184 | /// |
| 185 | /// \param CheckedRegionOfInterest if true, then the caller already checked |
| 186 | /// that this cursor is within the region of interest. |
| 187 | /// |
| 188 | /// \returns true if the visitation should be aborted, false if it |
| 189 | /// should continue. |
| 190 | bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { |
| 191 | if (clang_isInvalid(Cursor.kind)) |
| 192 | return false; |
| 193 | |
| 194 | if (clang_isDeclaration(Cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 195 | const Decl *D = getCursorDecl(Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 196 | if (!D) { |
| 197 | assert(0 && "Invalid declaration cursor"); |
| 198 | return true; // abort. |
| 199 | } |
| 200 | |
| 201 | // Ignore implicit declarations, unless it's an objc method because |
| 202 | // currently we should report implicit methods for properties when indexing. |
| 203 | if (D->isImplicit() && !isa<ObjCMethodDecl>(D)) |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | // If we have a range of interest, and this cursor doesn't intersect with it, |
| 208 | // we're done. |
| 209 | if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { |
| 210 | SourceRange Range = getRawCursorExtent(Cursor); |
| 211 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 216 | case CXChildVisit_Break: |
| 217 | return true; |
| 218 | |
| 219 | case CXChildVisit_Continue: |
| 220 | return false; |
| 221 | |
| 222 | case CXChildVisit_Recurse: { |
| 223 | bool ret = VisitChildren(Cursor); |
| 224 | if (PostChildrenVisitor) |
| 225 | if (PostChildrenVisitor(Cursor, ClientData)) |
| 226 | return true; |
| 227 | return ret; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | llvm_unreachable("Invalid CXChildVisitResult!"); |
| 232 | } |
| 233 | |
| 234 | static bool visitPreprocessedEntitiesInRange(SourceRange R, |
| 235 | PreprocessingRecord &PPRec, |
| 236 | CursorVisitor &Visitor) { |
| 237 | SourceManager &SM = Visitor.getASTUnit()->getSourceManager(); |
| 238 | FileID FID; |
| 239 | |
| 240 | if (!Visitor.shouldVisitIncludedEntities()) { |
| 241 | // If the begin/end of the range lie in the same FileID, do the optimization |
| 242 | // where we skip preprocessed entities that do not come from the same FileID. |
| 243 | FID = SM.getFileID(SM.getFileLoc(R.getBegin())); |
| 244 | if (FID != SM.getFileID(SM.getFileLoc(R.getEnd()))) |
| 245 | FID = FileID(); |
| 246 | } |
| 247 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 248 | const auto &Entities = PPRec.getPreprocessedEntitiesInRange(R); |
| 249 | return Visitor.visitPreprocessedEntities(Entities.begin(), Entities.end(), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 250 | PPRec, FID); |
| 251 | } |
| 252 | |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 253 | bool CursorVisitor::visitFileRegion() { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 254 | if (RegionOfInterest.isInvalid()) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 255 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 256 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 257 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 258 | SourceManager &SM = Unit->getSourceManager(); |
| 259 | |
| 260 | std::pair<FileID, unsigned> |
| 261 | Begin = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getBegin())), |
| 262 | End = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getEnd())); |
| 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; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 274 | |
| 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 | |
| 296 | for (DeclContext *DeclDC = D->getLexicalDeclContext(); |
| 297 | DeclDC; DeclDC = DeclDC->getLexicalParent()) { |
| 298 | if (DeclDC == DC) |
| 299 | return true; |
| 300 | } |
| 301 | return false; |
| 302 | } |
| 303 | |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 304 | bool CursorVisitor::visitDeclsFromFileRegion(FileID File, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 305 | unsigned Offset, 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 | |
| 353 | RangeComparisonResult CompRes = RangeCompare(SM, D->getSourceRange(),Range); |
| 354 | if (CompRes == RangeBefore) |
| 355 | continue; |
| 356 | if (CompRes == RangeAfter) |
| 357 | break; |
| 358 | |
| 359 | assert(CompRes == RangeOverlap); |
| 360 | VisitedAtLeastOnce = true; |
| 361 | |
| 362 | if (isa<ObjCContainerDecl>(D)) { |
| 363 | FileDI_current = &DIt; |
| 364 | FileDE_current = DE; |
| 365 | } else { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 366 | FileDI_current = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true)) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 370 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | if (VisitedAtLeastOnce) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 374 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 375 | |
| 376 | // No Decls overlapped with the range. Move up the lexical context until there |
| 377 | // is a context that contains the range or we reach the translation unit |
| 378 | // level. |
| 379 | DeclContext *DC = DIt == Decls.begin() ? (*DIt)->getLexicalDeclContext() |
| 380 | : (*(DIt-1))->getLexicalDeclContext(); |
| 381 | |
| 382 | while (DC && !DC->isTranslationUnit()) { |
| 383 | Decl *D = cast<Decl>(DC); |
| 384 | SourceRange CurDeclRange = D->getSourceRange(); |
| 385 | if (CurDeclRange.isInvalid()) |
| 386 | break; |
| 387 | |
| 388 | if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) { |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 389 | if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true)) |
| 390 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | DC = D->getLexicalDeclContext(); |
| 394 | } |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 395 | |
| 396 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | bool CursorVisitor::visitPreprocessedEntitiesInRegion() { |
| 400 | if (!AU->getPreprocessor().getPreprocessingRecord()) |
| 401 | return false; |
| 402 | |
| 403 | PreprocessingRecord &PPRec |
| 404 | = *AU->getPreprocessor().getPreprocessingRecord(); |
| 405 | SourceManager &SM = AU->getSourceManager(); |
| 406 | |
| 407 | if (RegionOfInterest.isValid()) { |
| 408 | SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest); |
| 409 | SourceLocation B = MappedRange.getBegin(); |
| 410 | SourceLocation E = MappedRange.getEnd(); |
| 411 | |
| 412 | if (AU->isInPreambleFileID(B)) { |
| 413 | if (SM.isLoadedSourceLocation(E)) |
| 414 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), |
| 415 | PPRec, *this); |
| 416 | |
| 417 | // Beginning of range lies in the preamble but it also extends beyond |
| 418 | // it into the main file. Split the range into 2 parts, one covering |
| 419 | // the preamble and another covering the main file. This allows subsequent |
| 420 | // calls to visitPreprocessedEntitiesInRange to accept a source range that |
| 421 | // lies in the same FileID, allowing it to skip preprocessed entities that |
| 422 | // do not come from the same FileID. |
| 423 | bool breaked = |
| 424 | visitPreprocessedEntitiesInRange( |
| 425 | SourceRange(B, AU->getEndOfPreambleFileID()), |
| 426 | PPRec, *this); |
| 427 | if (breaked) return true; |
| 428 | return visitPreprocessedEntitiesInRange( |
| 429 | SourceRange(AU->getStartOfMainFileID(), E), |
| 430 | PPRec, *this); |
| 431 | } |
| 432 | |
| 433 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this); |
| 434 | } |
| 435 | |
| 436 | bool OnlyLocalDecls |
| 437 | = !AU->isMainFileAST() && AU->getOnlyLocalDecls(); |
| 438 | |
| 439 | if (OnlyLocalDecls) |
| 440 | return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(), |
| 441 | PPRec); |
| 442 | |
| 443 | return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec); |
| 444 | } |
| 445 | |
| 446 | template<typename InputIterator> |
| 447 | bool CursorVisitor::visitPreprocessedEntities(InputIterator First, |
| 448 | InputIterator Last, |
| 449 | PreprocessingRecord &PPRec, |
| 450 | FileID FID) { |
| 451 | for (; First != Last; ++First) { |
| 452 | if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID)) |
| 453 | continue; |
| 454 | |
| 455 | PreprocessedEntity *PPE = *First; |
Argyrios Kyrtzidis | 1030f26 | 2013-05-07 20:37:17 +0000 | [diff] [blame] | 456 | if (!PPE) |
| 457 | continue; |
| 458 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 459 | if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) { |
| 460 | if (Visit(MakeMacroExpansionCursor(ME, TU))) |
| 461 | return true; |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 462 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 463 | continue; |
| 464 | } |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 465 | |
| 466 | if (MacroDefinitionRecord *MD = dyn_cast<MacroDefinitionRecord>(PPE)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 467 | if (Visit(MakeMacroDefinitionCursor(MD, TU))) |
| 468 | return true; |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 469 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 470 | continue; |
| 471 | } |
| 472 | |
| 473 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) { |
| 474 | if (Visit(MakeInclusionDirectiveCursor(ID, TU))) |
| 475 | return true; |
| 476 | |
| 477 | continue; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | return false; |
| 482 | } |
| 483 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 484 | /// Visit the children of the given cursor. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 485 | /// |
| 486 | /// \returns true if the visitation should be aborted, false if it |
| 487 | /// should continue. |
| 488 | bool CursorVisitor::VisitChildren(CXCursor Cursor) { |
| 489 | if (clang_isReference(Cursor.kind) && |
| 490 | Cursor.kind != CXCursor_CXXBaseSpecifier) { |
| 491 | // By definition, references have no children. |
| 492 | return false; |
| 493 | } |
| 494 | |
| 495 | // Set the Parent field to Cursor, then back to its old value once we're |
| 496 | // done. |
| 497 | SetParentRAII SetParent(Parent, StmtParent, Cursor); |
| 498 | |
| 499 | if (clang_isDeclaration(Cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 500 | Decl *D = const_cast<Decl *>(getCursorDecl(Cursor)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 501 | if (!D) |
| 502 | return false; |
| 503 | |
| 504 | return VisitAttributes(D) || Visit(D); |
| 505 | } |
| 506 | |
| 507 | if (clang_isStatement(Cursor.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 508 | if (const Stmt *S = getCursorStmt(Cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 509 | return Visit(S); |
| 510 | |
| 511 | return false; |
| 512 | } |
| 513 | |
| 514 | if (clang_isExpression(Cursor.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 515 | if (const Expr *E = getCursorExpr(Cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 516 | return Visit(E); |
| 517 | |
| 518 | return false; |
| 519 | } |
| 520 | |
| 521 | if (clang_isTranslationUnit(Cursor.kind)) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 522 | CXTranslationUnit TU = getCursorTU(Cursor); |
| 523 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 524 | |
| 525 | int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast }; |
| 526 | for (unsigned I = 0; I != 2; ++I) { |
| 527 | if (VisitOrder[I]) { |
| 528 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && |
| 529 | RegionOfInterest.isInvalid()) { |
| 530 | for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(), |
| 531 | TLEnd = CXXUnit->top_level_end(); |
| 532 | TL != TLEnd; ++TL) { |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 533 | const Optional<bool> V = handleDeclForVisitation(*TL); |
| 534 | if (!V.hasValue()) |
| 535 | continue; |
| 536 | return V.getValue(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 537 | } |
| 538 | } else if (VisitDeclContext( |
| 539 | CXXUnit->getASTContext().getTranslationUnitDecl())) |
| 540 | return true; |
| 541 | continue; |
| 542 | } |
| 543 | |
| 544 | // Walk the preprocessing record. |
| 545 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) |
| 546 | visitPreprocessedEntitiesInRegion(); |
| 547 | } |
| 548 | |
| 549 | return false; |
| 550 | } |
| 551 | |
| 552 | if (Cursor.kind == CXCursor_CXXBaseSpecifier) { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 553 | if (const CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 554 | if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) { |
| 555 | return Visit(BaseTSInfo->getTypeLoc()); |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 561 | const IBOutletCollectionAttr *A = |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 562 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor)); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 563 | if (const ObjCObjectType *ObjT = A->getInterface()->getAs<ObjCObjectType>()) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 564 | return Visit(cxcursor::MakeCursorObjCClassRef( |
| 565 | ObjT->getInterface(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 566 | A->getInterfaceLoc()->getTypeLoc().getBeginLoc(), TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 569 | // If pointing inside a macro definition, check if the token is an identifier |
| 570 | // that was ever defined as a macro. In such a case, create a "pseudo" macro |
| 571 | // expansion cursor for that token. |
| 572 | SourceLocation BeginLoc = RegionOfInterest.getBegin(); |
| 573 | if (Cursor.kind == CXCursor_MacroDefinition && |
| 574 | BeginLoc == RegionOfInterest.getEnd()) { |
| 575 | SourceLocation Loc = AU->mapLocationToPreamble(BeginLoc); |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 576 | const MacroInfo *MI = |
| 577 | getMacroInfo(cxcursor::getCursorMacroDefinition(Cursor), TU); |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 578 | if (MacroDefinitionRecord *MacroDef = |
| 579 | checkForMacroInMacroDefinition(MI, Loc, TU)) |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 580 | return Visit(cxcursor::MakeMacroExpansionCursor(MacroDef, BeginLoc, TU)); |
| 581 | } |
| 582 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 583 | // Nothing to visit at the moment. |
| 584 | return false; |
| 585 | } |
| 586 | |
| 587 | bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { |
| 588 | if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten()) |
| 589 | if (Visit(TSInfo->getTypeLoc())) |
| 590 | return true; |
| 591 | |
| 592 | if (Stmt *Body = B->getBody()) |
| 593 | return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest)); |
| 594 | |
| 595 | return false; |
| 596 | } |
| 597 | |
Ted Kremenek | 0332558 | 2013-02-21 01:29:01 +0000 | [diff] [blame] | 598 | Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 599 | if (RegionOfInterest.isValid()) { |
| 600 | SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager()); |
| 601 | if (Range.isInvalid()) |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 602 | return None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 603 | |
| 604 | switch (CompareRegionOfInterest(Range)) { |
| 605 | case RangeBefore: |
| 606 | // This declaration comes before the region of interest; skip it. |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 607 | return None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 608 | |
| 609 | case RangeAfter: |
| 610 | // This declaration comes after the region of interest; we're done. |
| 611 | return false; |
| 612 | |
| 613 | case RangeOverlap: |
| 614 | // This declaration overlaps the region of interest; visit it. |
| 615 | break; |
| 616 | } |
| 617 | } |
| 618 | return true; |
| 619 | } |
| 620 | |
| 621 | bool CursorVisitor::VisitDeclContext(DeclContext *DC) { |
| 622 | DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 623 | |
| 624 | // FIXME: Eventually remove. This part of a hack to support proper |
| 625 | // iteration over all Decls contained lexically within an ObjC container. |
| 626 | SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I); |
| 627 | SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E); |
| 628 | |
| 629 | for ( ; I != E; ++I) { |
| 630 | Decl *D = *I; |
| 631 | if (D->getLexicalDeclContext() != DC) |
| 632 | continue; |
Adrian Prantl | 2073dd2 | 2019-11-04 14:28:14 -0800 | [diff] [blame] | 633 | // Filter out synthesized property accessor redeclarations. |
| 634 | if (isa<ObjCImplDecl>(DC)) |
| 635 | if (auto *OMD = dyn_cast<ObjCMethodDecl>(D)) |
| 636 | if (OMD->isSynthesizedAccessorStub()) |
| 637 | continue; |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 638 | const Optional<bool> V = handleDeclForVisitation(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 639 | if (!V.hasValue()) |
| 640 | continue; |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 641 | return V.getValue(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 642 | } |
| 643 | return false; |
| 644 | } |
| 645 | |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 646 | Optional<bool> CursorVisitor::handleDeclForVisitation(const Decl *D) { |
| 647 | CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest); |
| 648 | |
| 649 | // Ignore synthesized ivars here, otherwise if we have something like: |
| 650 | // @synthesize prop = _prop; |
| 651 | // and '_prop' is not declared, we will encounter a '_prop' ivar before |
| 652 | // encountering the 'prop' synthesize declaration and we will think that |
| 653 | // we passed the region-of-interest. |
| 654 | if (auto *ivarD = dyn_cast<ObjCIvarDecl>(D)) { |
| 655 | if (ivarD->getSynthesize()) |
| 656 | return None; |
| 657 | } |
| 658 | |
| 659 | // FIXME: ObjCClassRef/ObjCProtocolRef for forward class/protocol |
| 660 | // declarations is a mismatch with the compiler semantics. |
| 661 | if (Cursor.kind == CXCursor_ObjCInterfaceDecl) { |
| 662 | auto *ID = cast<ObjCInterfaceDecl>(D); |
| 663 | if (!ID->isThisDeclarationADefinition()) |
| 664 | Cursor = MakeCursorObjCClassRef(ID, ID->getLocation(), TU); |
| 665 | |
| 666 | } else if (Cursor.kind == CXCursor_ObjCProtocolDecl) { |
| 667 | auto *PD = cast<ObjCProtocolDecl>(D); |
| 668 | if (!PD->isThisDeclarationADefinition()) |
| 669 | Cursor = MakeCursorObjCProtocolRef(PD, PD->getLocation(), TU); |
| 670 | } |
| 671 | |
| 672 | const Optional<bool> V = shouldVisitCursor(Cursor); |
| 673 | if (!V.hasValue()) |
| 674 | return None; |
| 675 | if (!V.getValue()) |
| 676 | return false; |
| 677 | if (Visit(Cursor, true)) |
| 678 | return true; |
| 679 | return None; |
| 680 | } |
| 681 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 682 | bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 683 | llvm_unreachable("Translation units are visited directly by Visit()"); |
| 684 | } |
| 685 | |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 686 | bool CursorVisitor::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| 687 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 688 | return true; |
| 689 | |
| 690 | return Visit(MakeCXCursor(D->getTemplatedDecl(), TU, RegionOfInterest)); |
| 691 | } |
| 692 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 693 | bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 694 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 695 | return Visit(TSInfo->getTypeLoc()); |
| 696 | |
| 697 | return false; |
| 698 | } |
| 699 | |
| 700 | bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { |
| 701 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 702 | return Visit(TSInfo->getTypeLoc()); |
| 703 | |
| 704 | return false; |
| 705 | } |
| 706 | |
| 707 | bool CursorVisitor::VisitTagDecl(TagDecl *D) { |
| 708 | return VisitDeclContext(D); |
| 709 | } |
| 710 | |
| 711 | bool CursorVisitor::VisitClassTemplateSpecializationDecl( |
| 712 | ClassTemplateSpecializationDecl *D) { |
| 713 | bool ShouldVisitBody = false; |
| 714 | switch (D->getSpecializationKind()) { |
| 715 | case TSK_Undeclared: |
| 716 | case TSK_ImplicitInstantiation: |
| 717 | // Nothing to visit |
| 718 | return false; |
| 719 | |
| 720 | case TSK_ExplicitInstantiationDeclaration: |
| 721 | case TSK_ExplicitInstantiationDefinition: |
| 722 | break; |
| 723 | |
| 724 | case TSK_ExplicitSpecialization: |
| 725 | ShouldVisitBody = true; |
| 726 | break; |
| 727 | } |
| 728 | |
| 729 | // Visit the template arguments used in the specialization. |
| 730 | if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { |
| 731 | TypeLoc TL = SpecType->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 732 | if (TemplateSpecializationTypeLoc TSTLoc = |
| 733 | TL.getAs<TemplateSpecializationTypeLoc>()) { |
| 734 | for (unsigned I = 0, N = TSTLoc.getNumArgs(); I != N; ++I) |
| 735 | if (VisitTemplateArgumentLoc(TSTLoc.getArgLoc(I))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 736 | return true; |
| 737 | } |
| 738 | } |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 739 | |
| 740 | return ShouldVisitBody && VisitCXXRecordDecl(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( |
| 744 | ClassTemplatePartialSpecializationDecl *D) { |
| 745 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 746 | // before visiting these template parameters. |
| 747 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 748 | return true; |
| 749 | |
| 750 | // Visit the partial specialization arguments. |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 751 | const ASTTemplateArgumentListInfo *Info = D->getTemplateArgsAsWritten(); |
| 752 | const TemplateArgumentLoc *TemplateArgs = Info->getTemplateArgs(); |
| 753 | for (unsigned I = 0, N = Info->NumTemplateArgs; I != N; ++I) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 754 | if (VisitTemplateArgumentLoc(TemplateArgs[I])) |
| 755 | return true; |
| 756 | |
| 757 | return VisitCXXRecordDecl(D); |
| 758 | } |
| 759 | |
| 760 | bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
Saar Raz | ff1e0fc | 2020-01-15 02:48:42 +0200 | [diff] [blame] | 761 | if (const auto *TC = D->getTypeConstraint()) |
| 762 | if (Visit(MakeCXCursor(TC->getImmediatelyDeclaredConstraint(), StmtParent, |
| 763 | TU, RegionOfInterest))) |
| 764 | return true; |
| 765 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 766 | // Visit the default argument. |
| 767 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 768 | if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) |
| 769 | if (Visit(DefArg->getTypeLoc())) |
| 770 | return true; |
| 771 | |
| 772 | return false; |
| 773 | } |
| 774 | |
| 775 | bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 776 | if (Expr *Init = D->getInitExpr()) |
| 777 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
| 778 | return false; |
| 779 | } |
| 780 | |
| 781 | bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { |
Argyrios Kyrtzidis | 2ec7674 | 2013-04-05 21:04:10 +0000 | [diff] [blame] | 782 | unsigned NumParamList = DD->getNumTemplateParameterLists(); |
| 783 | for (unsigned i = 0; i < NumParamList; i++) { |
| 784 | TemplateParameterList* Params = DD->getTemplateParameterList(i); |
| 785 | if (VisitTemplateParameters(Params)) |
| 786 | return true; |
| 787 | } |
| 788 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 789 | if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) |
| 790 | if (Visit(TSInfo->getTypeLoc())) |
| 791 | return true; |
| 792 | |
| 793 | // Visit the nested-name-specifier, if present. |
| 794 | if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc()) |
| 795 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 796 | return true; |
| 797 | |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 798 | return false; |
| 799 | } |
| 800 | |
Ivan Donchevskii | 1d18713 | 2018-01-03 14:35:48 +0000 | [diff] [blame] | 801 | static bool HasTrailingReturnType(FunctionDecl *ND) { |
| 802 | const QualType Ty = ND->getType(); |
| 803 | if (const FunctionType *AFT = Ty->getAs<FunctionType>()) { |
| 804 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(AFT)) |
| 805 | return FT->hasTrailingReturn(); |
| 806 | } |
| 807 | |
| 808 | return false; |
| 809 | } |
| 810 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 811 | /// Compare two base or member initializers based on their source order. |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 812 | static int CompareCXXCtorInitializers(CXXCtorInitializer *const *X, |
| 813 | CXXCtorInitializer *const *Y) { |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 814 | return (*X)->getSourceOrder() - (*Y)->getSourceOrder(); |
| 815 | } |
| 816 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 817 | bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
Argyrios Kyrtzidis | 2ec7674 | 2013-04-05 21:04:10 +0000 | [diff] [blame] | 818 | unsigned NumParamList = ND->getNumTemplateParameterLists(); |
| 819 | for (unsigned i = 0; i < NumParamList; i++) { |
| 820 | TemplateParameterList* Params = ND->getTemplateParameterList(i); |
| 821 | if (VisitTemplateParameters(Params)) |
| 822 | return true; |
| 823 | } |
| 824 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 825 | if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { |
| 826 | // Visit the function declaration's syntactic components in the order |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 827 | // written. This requires a bit of work. |
| 828 | TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); |
| 829 | FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>(); |
Ivan Donchevskii | 1d18713 | 2018-01-03 14:35:48 +0000 | [diff] [blame] | 830 | const bool HasTrailingRT = HasTrailingReturnType(ND); |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 831 | |
| 832 | // If we have a function declared directly (without the use of a typedef), |
| 833 | // visit just the return type. Otherwise, just visit the function's type |
| 834 | // now. |
Ivan Donchevskii | 1d18713 | 2018-01-03 14:35:48 +0000 | [diff] [blame] | 835 | if ((FTL && !isa<CXXConversionDecl>(ND) && !HasTrailingRT && |
| 836 | Visit(FTL.getReturnLoc())) || |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 837 | (!FTL && Visit(TL))) |
| 838 | return true; |
Ivan Donchevskii | 1d18713 | 2018-01-03 14:35:48 +0000 | [diff] [blame] | 839 | |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 840 | // Visit the nested-name-specifier, if present. |
| 841 | if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc()) |
| 842 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 843 | return true; |
| 844 | |
| 845 | // Visit the declaration name. |
Argyrios Kyrtzidis | 4a4d2b4 | 2014-02-09 08:13:47 +0000 | [diff] [blame] | 846 | if (!isa<CXXDestructorDecl>(ND)) |
| 847 | if (VisitDeclarationNameInfo(ND->getNameInfo())) |
| 848 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 849 | |
| 850 | // FIXME: Visit explicitly-specified template arguments! |
| 851 | |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 852 | // Visit the function parameters, if we have a function type. |
| 853 | if (FTL && VisitFunctionTypeLoc(FTL, true)) |
| 854 | return true; |
Ivan Donchevskii | 1d18713 | 2018-01-03 14:35:48 +0000 | [diff] [blame] | 855 | |
| 856 | // Visit the function's trailing return type. |
| 857 | if (FTL && HasTrailingRT && Visit(FTL.getReturnLoc())) |
| 858 | return true; |
| 859 | |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 860 | // FIXME: Attributes? |
| 861 | } |
| 862 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 863 | if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) { |
| 864 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) { |
| 865 | // Find the initializers that were written in the source. |
| 866 | SmallVector<CXXCtorInitializer *, 4> WrittenInits; |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 867 | for (auto *I : Constructor->inits()) { |
| 868 | if (!I->isWritten()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 869 | continue; |
| 870 | |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 871 | WrittenInits.push_back(I); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | // Sort the initializers in source order |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 875 | llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(), |
| 876 | &CompareCXXCtorInitializers); |
| 877 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 878 | // Visit the initializers in source order |
| 879 | for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) { |
| 880 | CXXCtorInitializer *Init = WrittenInits[I]; |
| 881 | if (Init->isAnyMemberInitializer()) { |
| 882 | if (Visit(MakeCursorMemberRef(Init->getAnyMember(), |
| 883 | Init->getMemberLocation(), TU))) |
| 884 | return true; |
| 885 | } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) { |
| 886 | if (Visit(TInfo->getTypeLoc())) |
| 887 | return true; |
| 888 | } |
| 889 | |
| 890 | // Visit the initializer value. |
| 891 | if (Expr *Initializer = Init->getInit()) |
| 892 | if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest))) |
| 893 | return true; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
| 898 | return true; |
| 899 | } |
| 900 | |
| 901 | return false; |
| 902 | } |
| 903 | |
| 904 | bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { |
| 905 | if (VisitDeclaratorDecl(D)) |
| 906 | return true; |
| 907 | |
| 908 | if (Expr *BitWidth = D->getBitWidth()) |
| 909 | return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest)); |
| 910 | |
Benjamin Kramer | 99f9759 | 2017-11-15 12:20:41 +0000 | [diff] [blame] | 911 | if (Expr *Init = D->getInClassInitializer()) |
| 912 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
| 913 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 914 | return false; |
| 915 | } |
| 916 | |
| 917 | bool CursorVisitor::VisitVarDecl(VarDecl *D) { |
| 918 | if (VisitDeclaratorDecl(D)) |
| 919 | return true; |
| 920 | |
| 921 | if (Expr *Init = D->getInit()) |
| 922 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
| 923 | |
| 924 | return false; |
| 925 | } |
| 926 | |
| 927 | bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 928 | if (VisitDeclaratorDecl(D)) |
| 929 | return true; |
| 930 | |
| 931 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 932 | if (Expr *DefArg = D->getDefaultArgument()) |
| 933 | return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest)); |
| 934 | |
| 935 | return false; |
| 936 | } |
| 937 | |
| 938 | bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 939 | // FIXME: Visit the "outer" template parameter lists on the FunctionDecl |
| 940 | // before visiting these template parameters. |
| 941 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 942 | return true; |
| 943 | |
Jonathan Coe | 578ac7a | 2017-10-16 23:43:02 +0000 | [diff] [blame] | 944 | auto* FD = D->getTemplatedDecl(); |
| 945 | return VisitAttributes(FD) || VisitFunctionDecl(FD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 949 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 950 | // before visiting these template parameters. |
| 951 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 952 | return true; |
| 953 | |
Jonathan Coe | 578ac7a | 2017-10-16 23:43:02 +0000 | [diff] [blame] | 954 | auto* CD = D->getTemplatedDecl(); |
| 955 | return VisitAttributes(CD) || VisitCXXRecordDecl(CD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 959 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 960 | return true; |
| 961 | |
| 962 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && |
| 963 | VisitTemplateArgumentLoc(D->getDefaultArgument())) |
| 964 | return true; |
| 965 | |
| 966 | return false; |
| 967 | } |
| 968 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 969 | bool CursorVisitor::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { |
| 970 | // Visit the bound, if it's explicit. |
| 971 | if (D->hasExplicitBound()) { |
| 972 | if (auto TInfo = D->getTypeSourceInfo()) { |
| 973 | if (Visit(TInfo->getTypeLoc())) |
| 974 | return true; |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | return false; |
| 979 | } |
| 980 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 981 | bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 982 | if (TypeSourceInfo *TSInfo = ND->getReturnTypeSourceInfo()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 983 | if (Visit(TSInfo->getTypeLoc())) |
| 984 | return true; |
| 985 | |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 986 | for (const auto *P : ND->parameters()) { |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 987 | if (Visit(MakeCXCursor(P, TU, RegionOfInterest))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 988 | return true; |
| 989 | } |
| 990 | |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 991 | return ND->isThisDeclarationADefinition() && |
| 992 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | template <typename DeclIt> |
| 996 | static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current, |
| 997 | SourceManager &SM, SourceLocation EndLoc, |
| 998 | SmallVectorImpl<Decl *> &Decls) { |
| 999 | DeclIt next = *DI_current; |
| 1000 | while (++next != DE_current) { |
| 1001 | Decl *D_next = *next; |
| 1002 | if (!D_next) |
| 1003 | break; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1004 | SourceLocation L = D_next->getBeginLoc(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1005 | if (!L.isValid()) |
| 1006 | break; |
| 1007 | if (SM.isBeforeInTranslationUnit(L, EndLoc)) { |
| 1008 | *DI_current = next; |
| 1009 | Decls.push_back(D_next); |
| 1010 | continue; |
| 1011 | } |
| 1012 | break; |
| 1013 | } |
| 1014 | } |
| 1015 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1016 | bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
| 1017 | // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially |
| 1018 | // an @implementation can lexically contain Decls that are not properly |
| 1019 | // nested in the AST. When we identify such cases, we need to retrofit |
| 1020 | // this nesting here. |
| 1021 | if (!DI_current && !FileDI_current) |
| 1022 | return VisitDeclContext(D); |
| 1023 | |
| 1024 | // Scan the Decls that immediately come after the container |
| 1025 | // in the current DeclContext. If any fall within the |
| 1026 | // container's lexical region, stash them into a vector |
| 1027 | // for later processing. |
| 1028 | SmallVector<Decl *, 24> DeclsInContainer; |
| 1029 | SourceLocation EndLoc = D->getSourceRange().getEnd(); |
| 1030 | SourceManager &SM = AU->getSourceManager(); |
| 1031 | if (EndLoc.isValid()) { |
| 1032 | if (DI_current) { |
| 1033 | addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc, |
| 1034 | DeclsInContainer); |
| 1035 | } else { |
| 1036 | addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc, |
| 1037 | DeclsInContainer); |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | // The common case. |
| 1042 | if (DeclsInContainer.empty()) |
| 1043 | return VisitDeclContext(D); |
| 1044 | |
| 1045 | // Get all the Decls in the DeclContext, and sort them with the |
| 1046 | // additional ones we've collected. Then visit them. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1047 | for (auto *SubDecl : D->decls()) { |
| 1048 | if (!SubDecl || SubDecl->getLexicalDeclContext() != D || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1049 | SubDecl->getBeginLoc().isInvalid()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1050 | continue; |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1051 | DeclsInContainer.push_back(SubDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | // Now sort the Decls so that they appear in lexical order. |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 1055 | llvm::sort(DeclsInContainer, |
Mandeep Singh Grang | c205d8c | 2018-03-27 16:50:00 +0000 | [diff] [blame] | 1056 | [&SM](Decl *A, Decl *B) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1057 | SourceLocation L_A = A->getBeginLoc(); |
| 1058 | SourceLocation L_B = B->getBeginLoc(); |
| 1059 | return L_A != L_B ? SM.isBeforeInTranslationUnit(L_A, L_B) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1060 | : SM.isBeforeInTranslationUnit(A->getEndLoc(), |
| 1061 | B->getEndLoc()); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1062 | }); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1063 | |
| 1064 | // Now visit the decls. |
| 1065 | for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(), |
| 1066 | E = DeclsInContainer.end(); I != E; ++I) { |
| 1067 | CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest); |
Ted Kremenek | 0332558 | 2013-02-21 01:29:01 +0000 | [diff] [blame] | 1068 | const Optional<bool> &V = shouldVisitCursor(Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1069 | if (!V.hasValue()) |
| 1070 | continue; |
| 1071 | if (!V.getValue()) |
| 1072 | return false; |
| 1073 | if (Visit(Cursor, true)) |
| 1074 | return true; |
| 1075 | } |
| 1076 | return false; |
| 1077 | } |
| 1078 | |
| 1079 | bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 1080 | if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), |
| 1081 | TU))) |
| 1082 | return true; |
| 1083 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1084 | if (VisitObjCTypeParamList(ND->getTypeParamList())) |
| 1085 | return true; |
| 1086 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1087 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 1088 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 1089 | E = ND->protocol_end(); I != E; ++I, ++PL) |
| 1090 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1091 | return true; |
| 1092 | |
| 1093 | return VisitObjCContainerDecl(ND); |
| 1094 | } |
| 1095 | |
| 1096 | bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 1097 | if (!PID->isThisDeclarationADefinition()) |
| 1098 | return Visit(MakeCursorObjCProtocolRef(PID, PID->getLocation(), TU)); |
| 1099 | |
| 1100 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
| 1101 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 1102 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 1103 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1104 | return true; |
| 1105 | |
| 1106 | return VisitObjCContainerDecl(PID); |
| 1107 | } |
| 1108 | |
| 1109 | bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { |
| 1110 | if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc())) |
| 1111 | return true; |
| 1112 | |
| 1113 | // FIXME: This implements a workaround with @property declarations also being |
| 1114 | // installed in the DeclContext for the @interface. Eventually this code |
| 1115 | // should be removed. |
| 1116 | ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); |
| 1117 | if (!CDecl || !CDecl->IsClassExtension()) |
| 1118 | return false; |
| 1119 | |
| 1120 | ObjCInterfaceDecl *ID = CDecl->getClassInterface(); |
| 1121 | if (!ID) |
| 1122 | return false; |
| 1123 | |
| 1124 | IdentifierInfo *PropertyId = PD->getIdentifier(); |
| 1125 | ObjCPropertyDecl *prevDecl = |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 1126 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId, |
| 1127 | PD->getQueryKind()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1128 | |
| 1129 | if (!prevDecl) |
| 1130 | return false; |
| 1131 | |
| 1132 | // Visit synthesized methods since they will be skipped when visiting |
| 1133 | // the @interface. |
| 1134 | if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) |
| 1135 | if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl) |
| 1136 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
| 1137 | return true; |
| 1138 | |
| 1139 | if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) |
| 1140 | if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl) |
| 1141 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
| 1142 | return true; |
| 1143 | |
| 1144 | return false; |
| 1145 | } |
| 1146 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1147 | bool CursorVisitor::VisitObjCTypeParamList(ObjCTypeParamList *typeParamList) { |
| 1148 | if (!typeParamList) |
| 1149 | return false; |
| 1150 | |
| 1151 | for (auto *typeParam : *typeParamList) { |
| 1152 | // Visit the type parameter. |
| 1153 | if (Visit(MakeCXCursor(typeParam, TU, RegionOfInterest))) |
| 1154 | return true; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | return false; |
| 1158 | } |
| 1159 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1160 | bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 1161 | if (!D->isThisDeclarationADefinition()) { |
| 1162 | // Forward declaration is treated like a reference. |
| 1163 | return Visit(MakeCursorObjCClassRef(D, D->getLocation(), TU)); |
| 1164 | } |
| 1165 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1166 | // Objective-C type parameters. |
| 1167 | if (VisitObjCTypeParamList(D->getTypeParamListAsWritten())) |
| 1168 | return true; |
| 1169 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1170 | // Issue callbacks for super class. |
| 1171 | if (D->getSuperClass() && |
| 1172 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
| 1173 | D->getSuperClassLoc(), |
| 1174 | TU))) |
| 1175 | return true; |
| 1176 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1177 | if (TypeSourceInfo *SuperClassTInfo = D->getSuperClassTInfo()) |
| 1178 | if (Visit(SuperClassTInfo->getTypeLoc())) |
| 1179 | return true; |
| 1180 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1181 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1182 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 1183 | E = D->protocol_end(); I != E; ++I, ++PL) |
| 1184 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1185 | return true; |
| 1186 | |
| 1187 | return VisitObjCContainerDecl(D); |
| 1188 | } |
| 1189 | |
| 1190 | bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 1191 | return VisitObjCContainerDecl(D); |
| 1192 | } |
| 1193 | |
| 1194 | bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 1195 | // 'ID' could be null when dealing with invalid code. |
| 1196 | if (ObjCInterfaceDecl *ID = D->getClassInterface()) |
| 1197 | if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) |
| 1198 | return true; |
| 1199 | |
| 1200 | return VisitObjCImplDecl(D); |
| 1201 | } |
| 1202 | |
| 1203 | bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 1204 | #if 0 |
| 1205 | // Issue callbacks for super class. |
| 1206 | // FIXME: No source location information! |
| 1207 | if (D->getSuperClass() && |
| 1208 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
| 1209 | D->getSuperClassLoc(), |
| 1210 | TU))) |
| 1211 | return true; |
| 1212 | #endif |
| 1213 | |
| 1214 | return VisitObjCImplDecl(D); |
| 1215 | } |
| 1216 | |
| 1217 | bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) { |
| 1218 | if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl()) |
| 1219 | if (PD->isIvarNameSpecified()) |
| 1220 | return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU)); |
| 1221 | |
| 1222 | return false; |
| 1223 | } |
| 1224 | |
| 1225 | bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { |
| 1226 | return VisitDeclContext(D); |
| 1227 | } |
| 1228 | |
| 1229 | bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 1230 | // Visit nested-name-specifier. |
| 1231 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1232 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1233 | return true; |
| 1234 | |
| 1235 | return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), |
| 1236 | D->getTargetNameLoc(), TU)); |
| 1237 | } |
| 1238 | |
| 1239 | bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { |
| 1240 | // Visit nested-name-specifier. |
| 1241 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1242 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1243 | return true; |
| 1244 | } |
| 1245 | |
| 1246 | if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU))) |
| 1247 | return true; |
| 1248 | |
| 1249 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1250 | } |
| 1251 | |
| 1252 | bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 1253 | // Visit nested-name-specifier. |
| 1254 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1255 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1256 | return true; |
| 1257 | |
| 1258 | return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), |
| 1259 | D->getIdentLocation(), TU)); |
| 1260 | } |
| 1261 | |
| 1262 | bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
| 1263 | // Visit nested-name-specifier. |
| 1264 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1265 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1266 | return true; |
| 1267 | } |
| 1268 | |
| 1269 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1270 | } |
| 1271 | |
| 1272 | bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( |
| 1273 | UnresolvedUsingTypenameDecl *D) { |
| 1274 | // Visit nested-name-specifier. |
| 1275 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1276 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1277 | return true; |
| 1278 | |
| 1279 | return false; |
| 1280 | } |
| 1281 | |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 1282 | bool CursorVisitor::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 1283 | if (Visit(MakeCXCursor(D->getAssertExpr(), StmtParent, TU, RegionOfInterest))) |
| 1284 | return true; |
Richard Trieu | f3b7766 | 2016-09-13 01:37:01 +0000 | [diff] [blame] | 1285 | if (StringLiteral *Message = D->getMessage()) |
| 1286 | if (Visit(MakeCXCursor(Message, StmtParent, TU, RegionOfInterest))) |
| 1287 | return true; |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 1288 | return false; |
| 1289 | } |
| 1290 | |
Olivier Goffart | d211c64 | 2016-11-04 06:29:27 +0000 | [diff] [blame] | 1291 | bool CursorVisitor::VisitFriendDecl(FriendDecl *D) { |
| 1292 | if (NamedDecl *FriendD = D->getFriendDecl()) { |
| 1293 | if (Visit(MakeCXCursor(FriendD, TU, RegionOfInterest))) |
| 1294 | return true; |
| 1295 | } else if (TypeSourceInfo *TI = D->getFriendType()) { |
| 1296 | if (Visit(TI->getTypeLoc())) |
| 1297 | return true; |
| 1298 | } |
| 1299 | return false; |
| 1300 | } |
| 1301 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1302 | bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { |
| 1303 | switch (Name.getName().getNameKind()) { |
| 1304 | case clang::DeclarationName::Identifier: |
| 1305 | case clang::DeclarationName::CXXLiteralOperatorName: |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 1306 | case clang::DeclarationName::CXXDeductionGuideName: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1307 | case clang::DeclarationName::CXXOperatorName: |
| 1308 | case clang::DeclarationName::CXXUsingDirective: |
| 1309 | return false; |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 1310 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1311 | case clang::DeclarationName::CXXConstructorName: |
| 1312 | case clang::DeclarationName::CXXDestructorName: |
| 1313 | case clang::DeclarationName::CXXConversionFunctionName: |
| 1314 | if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) |
| 1315 | return Visit(TSInfo->getTypeLoc()); |
| 1316 | return false; |
| 1317 | |
| 1318 | case clang::DeclarationName::ObjCZeroArgSelector: |
| 1319 | case clang::DeclarationName::ObjCOneArgSelector: |
| 1320 | case clang::DeclarationName::ObjCMultiArgSelector: |
| 1321 | // FIXME: Per-identifier location info? |
| 1322 | return false; |
| 1323 | } |
| 1324 | |
| 1325 | llvm_unreachable("Invalid DeclarationName::Kind!"); |
| 1326 | } |
| 1327 | |
| 1328 | bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, |
| 1329 | SourceRange Range) { |
| 1330 | // FIXME: This whole routine is a hack to work around the lack of proper |
| 1331 | // source information in nested-name-specifiers (PR5791). Since we do have |
| 1332 | // a beginning source location, we can visit the first component of the |
| 1333 | // nested-name-specifier, if it's a single-token component. |
| 1334 | if (!NNS) |
| 1335 | return false; |
| 1336 | |
| 1337 | // Get the first component in the nested-name-specifier. |
| 1338 | while (NestedNameSpecifier *Prefix = NNS->getPrefix()) |
| 1339 | NNS = Prefix; |
| 1340 | |
| 1341 | switch (NNS->getKind()) { |
| 1342 | case NestedNameSpecifier::Namespace: |
| 1343 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), |
| 1344 | TU)); |
| 1345 | |
| 1346 | case NestedNameSpecifier::NamespaceAlias: |
| 1347 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1348 | Range.getBegin(), TU)); |
| 1349 | |
| 1350 | case NestedNameSpecifier::TypeSpec: { |
| 1351 | // If the type has a form where we know that the beginning of the source |
| 1352 | // range matches up with a reference cursor. Visit the appropriate reference |
| 1353 | // cursor. |
| 1354 | const Type *T = NNS->getAsType(); |
| 1355 | if (const TypedefType *Typedef = dyn_cast<TypedefType>(T)) |
| 1356 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); |
| 1357 | if (const TagType *Tag = dyn_cast<TagType>(T)) |
| 1358 | return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); |
| 1359 | if (const TemplateSpecializationType *TST |
| 1360 | = dyn_cast<TemplateSpecializationType>(T)) |
| 1361 | return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); |
| 1362 | break; |
| 1363 | } |
| 1364 | |
| 1365 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1366 | case NestedNameSpecifier::Global: |
| 1367 | case NestedNameSpecifier::Identifier: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 1368 | case NestedNameSpecifier::Super: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1369 | break; |
| 1370 | } |
| 1371 | |
| 1372 | return false; |
| 1373 | } |
| 1374 | |
| 1375 | bool |
| 1376 | CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 1377 | SmallVector<NestedNameSpecifierLoc, 4> Qualifiers; |
| 1378 | for (; Qualifier; Qualifier = Qualifier.getPrefix()) |
| 1379 | Qualifiers.push_back(Qualifier); |
| 1380 | |
| 1381 | while (!Qualifiers.empty()) { |
| 1382 | NestedNameSpecifierLoc Q = Qualifiers.pop_back_val(); |
| 1383 | NestedNameSpecifier *NNS = Q.getNestedNameSpecifier(); |
| 1384 | switch (NNS->getKind()) { |
| 1385 | case NestedNameSpecifier::Namespace: |
| 1386 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), |
| 1387 | Q.getLocalBeginLoc(), |
| 1388 | TU))) |
| 1389 | return true; |
| 1390 | |
| 1391 | break; |
| 1392 | |
| 1393 | case NestedNameSpecifier::NamespaceAlias: |
| 1394 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1395 | Q.getLocalBeginLoc(), |
| 1396 | TU))) |
| 1397 | return true; |
| 1398 | |
| 1399 | break; |
| 1400 | |
| 1401 | case NestedNameSpecifier::TypeSpec: |
| 1402 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1403 | if (Visit(Q.getTypeLoc())) |
| 1404 | return true; |
| 1405 | |
| 1406 | break; |
| 1407 | |
| 1408 | case NestedNameSpecifier::Global: |
| 1409 | case NestedNameSpecifier::Identifier: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 1410 | case NestedNameSpecifier::Super: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1411 | break; |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | return false; |
| 1416 | } |
| 1417 | |
| 1418 | bool CursorVisitor::VisitTemplateParameters( |
| 1419 | const TemplateParameterList *Params) { |
| 1420 | if (!Params) |
| 1421 | return false; |
| 1422 | |
| 1423 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 1424 | PEnd = Params->end(); |
| 1425 | P != PEnd; ++P) { |
| 1426 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
| 1427 | return true; |
| 1428 | } |
| 1429 | |
| 1430 | return false; |
| 1431 | } |
| 1432 | |
| 1433 | bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { |
| 1434 | switch (Name.getKind()) { |
| 1435 | case TemplateName::Template: |
| 1436 | return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); |
| 1437 | |
| 1438 | case TemplateName::OverloadedTemplate: |
| 1439 | // Visit the overloaded template set. |
| 1440 | if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU))) |
| 1441 | return true; |
| 1442 | |
| 1443 | return false; |
| 1444 | |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 1445 | case TemplateName::AssumedTemplate: |
| 1446 | // FIXME: Visit DeclarationName? |
| 1447 | return false; |
| 1448 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1449 | case TemplateName::DependentTemplate: |
| 1450 | // FIXME: Visit nested-name-specifier. |
| 1451 | return false; |
| 1452 | |
| 1453 | case TemplateName::QualifiedTemplate: |
| 1454 | // FIXME: Visit nested-name-specifier. |
| 1455 | return Visit(MakeCursorTemplateRef( |
| 1456 | Name.getAsQualifiedTemplateName()->getDecl(), |
| 1457 | Loc, TU)); |
| 1458 | |
| 1459 | case TemplateName::SubstTemplateTemplateParm: |
| 1460 | return Visit(MakeCursorTemplateRef( |
| 1461 | Name.getAsSubstTemplateTemplateParm()->getParameter(), |
| 1462 | Loc, TU)); |
| 1463 | |
| 1464 | case TemplateName::SubstTemplateTemplateParmPack: |
| 1465 | return Visit(MakeCursorTemplateRef( |
| 1466 | Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), |
| 1467 | Loc, TU)); |
| 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; |
| 1479 | |
| 1480 | case TemplateArgument::Type: |
| 1481 | if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) |
| 1482 | return Visit(TSInfo->getTypeLoc()); |
| 1483 | return false; |
| 1484 | |
| 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; |
| 1499 | |
| 1500 | case TemplateArgument::Template: |
| 1501 | case TemplateArgument::TemplateExpansion: |
| 1502 | if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc())) |
| 1503 | return true; |
| 1504 | |
| 1505 | return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(), |
| 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: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 1531 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 1532 | case BuiltinType::Id: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 1533 | #include "clang/Basic/OpenCLImageTypes.def" |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 1534 | #define EXT_OPAQUE_TYPE(ExtTYpe, Id, Ext) \ |
| 1535 | case BuiltinType::Id: |
| 1536 | #include "clang/Basic/OpenCLExtensionTypes.def" |
NAKAMURA Takumi | 288c42e | 2013-02-07 12:47:42 +0000 | [diff] [blame] | 1537 | case BuiltinType::OCLSampler: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1538 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1539 | case BuiltinType::OCLClkEvent: |
| 1540 | case BuiltinType::OCLQueue: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1541 | case BuiltinType::OCLReserveID: |
Richard Sandiford | eb485fb | 2019-08-09 08:52:54 +0000 | [diff] [blame] | 1542 | #define SVE_TYPE(Name, Id, SingletonId) \ |
| 1543 | case BuiltinType::Id: |
| 1544 | #include "clang/Basic/AArch64SVEACLETypes.def" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1545 | #define BUILTIN_TYPE(Id, SingletonId) |
| 1546 | #define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1547 | #define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1548 | #define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1549 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1550 | #include "clang/AST/BuiltinTypes.def" |
| 1551 | break; |
| 1552 | |
| 1553 | case BuiltinType::ObjCId: |
| 1554 | VisitType = Context.getObjCIdType(); |
| 1555 | break; |
| 1556 | |
| 1557 | case BuiltinType::ObjCClass: |
| 1558 | VisitType = Context.getObjCClassType(); |
| 1559 | break; |
| 1560 | |
| 1561 | case BuiltinType::ObjCSel: |
| 1562 | VisitType = Context.getObjCSelType(); |
| 1563 | break; |
| 1564 | } |
| 1565 | |
| 1566 | if (!VisitType.isNull()) { |
| 1567 | if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) |
| 1568 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), |
| 1569 | TU)); |
| 1570 | } |
| 1571 | |
| 1572 | return false; |
| 1573 | } |
| 1574 | |
| 1575 | bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 1576 | return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU)); |
| 1577 | } |
| 1578 | |
| 1579 | bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 1580 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1581 | } |
| 1582 | |
| 1583 | bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { |
| 1584 | if (TL.isDefinition()) |
| 1585 | return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest)); |
| 1586 | |
| 1587 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1588 | } |
| 1589 | |
| 1590 | bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1591 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1592 | } |
| 1593 | |
| 1594 | bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 1595 | return Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 1598 | bool CursorVisitor::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1599 | if (Visit(MakeCursorTypeRef(TL.getDecl(), TL.getBeginLoc(), TU))) |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 1600 | return true; |
| 1601 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1602 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1603 | TU))) |
| 1604 | return true; |
| 1605 | } |
| 1606 | |
| 1607 | return false; |
| 1608 | } |
| 1609 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1610 | bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 1611 | if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) |
| 1612 | return true; |
| 1613 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1614 | for (unsigned I = 0, N = TL.getNumTypeArgs(); I != N; ++I) { |
| 1615 | if (Visit(TL.getTypeArgTInfo(I)->getTypeLoc())) |
| 1616 | return true; |
| 1617 | } |
| 1618 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1619 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1620 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1621 | TU))) |
| 1622 | return true; |
| 1623 | } |
| 1624 | |
| 1625 | return false; |
| 1626 | } |
| 1627 | |
| 1628 | bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 1629 | return Visit(TL.getPointeeLoc()); |
| 1630 | } |
| 1631 | |
| 1632 | bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) { |
| 1633 | return Visit(TL.getInnerLoc()); |
| 1634 | } |
| 1635 | |
Leonard Chan | c72aaf6 | 2019-05-07 03:20:17 +0000 | [diff] [blame] | 1636 | bool CursorVisitor::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { |
| 1637 | return Visit(TL.getInnerLoc()); |
| 1638 | } |
| 1639 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1640 | bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1641 | return Visit(TL.getPointeeLoc()); |
| 1642 | } |
| 1643 | |
| 1644 | bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1645 | return Visit(TL.getPointeeLoc()); |
| 1646 | } |
| 1647 | |
| 1648 | bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1649 | return Visit(TL.getPointeeLoc()); |
| 1650 | } |
| 1651 | |
| 1652 | bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 1653 | return Visit(TL.getPointeeLoc()); |
| 1654 | } |
| 1655 | |
| 1656 | bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 1657 | return Visit(TL.getPointeeLoc()); |
| 1658 | } |
| 1659 | |
| 1660 | bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 1661 | return Visit(TL.getModifiedLoc()); |
| 1662 | } |
| 1663 | |
| 1664 | bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, |
| 1665 | bool SkipResultType) { |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 1666 | if (!SkipResultType && Visit(TL.getReturnLoc())) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1667 | return true; |
| 1668 | |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 1669 | for (unsigned I = 0, N = TL.getNumParams(); I != N; ++I) |
| 1670 | if (Decl *D = TL.getParam(I)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1671 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest))) |
| 1672 | return true; |
| 1673 | |
| 1674 | return false; |
| 1675 | } |
| 1676 | |
| 1677 | bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1678 | if (Visit(TL.getElementLoc())) |
| 1679 | return true; |
| 1680 | |
| 1681 | if (Expr *Size = TL.getSizeExpr()) |
| 1682 | return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest)); |
| 1683 | |
| 1684 | return false; |
| 1685 | } |
| 1686 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 1687 | bool CursorVisitor::VisitDecayedTypeLoc(DecayedTypeLoc TL) { |
| 1688 | return Visit(TL.getOriginalLoc()); |
| 1689 | } |
| 1690 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 1691 | bool CursorVisitor::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { |
| 1692 | return Visit(TL.getOriginalLoc()); |
| 1693 | } |
| 1694 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1695 | bool CursorVisitor::VisitDeducedTemplateSpecializationTypeLoc( |
| 1696 | DeducedTemplateSpecializationTypeLoc TL) { |
| 1697 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1698 | TL.getTemplateNameLoc())) |
| 1699 | return true; |
| 1700 | |
| 1701 | return false; |
| 1702 | } |
| 1703 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1704 | bool CursorVisitor::VisitTemplateSpecializationTypeLoc( |
| 1705 | TemplateSpecializationTypeLoc TL) { |
| 1706 | // Visit the template name. |
| 1707 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1708 | TL.getTemplateNameLoc())) |
| 1709 | return true; |
| 1710 | |
| 1711 | // Visit the template arguments. |
| 1712 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1713 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1714 | return true; |
| 1715 | |
| 1716 | return false; |
| 1717 | } |
| 1718 | |
| 1719 | bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 1720 | return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); |
| 1721 | } |
| 1722 | |
| 1723 | bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 1724 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1725 | return Visit(TSInfo->getTypeLoc()); |
| 1726 | |
| 1727 | return false; |
| 1728 | } |
| 1729 | |
| 1730 | bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 1731 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1732 | return Visit(TSInfo->getTypeLoc()); |
| 1733 | |
| 1734 | return false; |
| 1735 | } |
| 1736 | |
| 1737 | bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 1738 | return VisitNestedNameSpecifierLoc(TL.getQualifierLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc( |
| 1742 | DependentTemplateSpecializationTypeLoc TL) { |
| 1743 | // Visit the nested-name-specifier, if there is one. |
| 1744 | if (TL.getQualifierLoc() && |
| 1745 | VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1746 | return true; |
| 1747 | |
| 1748 | // Visit the template arguments. |
| 1749 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1750 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1751 | return true; |
| 1752 | |
| 1753 | return false; |
| 1754 | } |
| 1755 | |
| 1756 | bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 1757 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1758 | return true; |
| 1759 | |
| 1760 | return Visit(TL.getNamedTypeLoc()); |
| 1761 | } |
| 1762 | |
| 1763 | bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { |
| 1764 | return Visit(TL.getPatternLoc()); |
| 1765 | } |
| 1766 | |
| 1767 | bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 1768 | if (Expr *E = TL.getUnderlyingExpr()) |
| 1769 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1770 | |
| 1771 | return false; |
| 1772 | } |
| 1773 | |
| 1774 | bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 1775 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1776 | } |
| 1777 | |
| 1778 | bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) { |
| 1779 | return Visit(TL.getValueLoc()); |
| 1780 | } |
| 1781 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 1782 | bool CursorVisitor::VisitPipeTypeLoc(PipeTypeLoc TL) { |
| 1783 | return Visit(TL.getValueLoc()); |
| 1784 | } |
| 1785 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1786 | #define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \ |
| 1787 | bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \ |
| 1788 | return Visit##PARENT##Loc(TL); \ |
| 1789 | } |
| 1790 | |
| 1791 | DEFAULT_TYPELOC_IMPL(Complex, Type) |
| 1792 | DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType) |
| 1793 | DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType) |
| 1794 | DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType) |
| 1795 | DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType) |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 1796 | DEFAULT_TYPELOC_IMPL(DependentAddressSpace, Type) |
Erich Keane | f702b02 | 2018-07-13 19:46:04 +0000 | [diff] [blame] | 1797 | DEFAULT_TYPELOC_IMPL(DependentVector, Type) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1798 | DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type) |
| 1799 | DEFAULT_TYPELOC_IMPL(Vector, Type) |
| 1800 | DEFAULT_TYPELOC_IMPL(ExtVector, VectorType) |
| 1801 | DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType) |
| 1802 | DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType) |
| 1803 | DEFAULT_TYPELOC_IMPL(Record, TagType) |
| 1804 | DEFAULT_TYPELOC_IMPL(Enum, TagType) |
| 1805 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type) |
| 1806 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type) |
| 1807 | DEFAULT_TYPELOC_IMPL(Auto, Type) |
| 1808 | |
| 1809 | bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 1810 | // Visit the nested-name-specifier, if present. |
| 1811 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1812 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1813 | return true; |
| 1814 | |
| 1815 | if (D->isCompleteDefinition()) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1816 | for (const auto &I : D->bases()) { |
| 1817 | if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(&I, TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1818 | return true; |
| 1819 | } |
| 1820 | } |
| 1821 | |
| 1822 | return VisitTagDecl(D); |
| 1823 | } |
| 1824 | |
| 1825 | bool CursorVisitor::VisitAttributes(Decl *D) { |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 1826 | for (const auto *I : D->attrs()) |
Michael Wu | 40ff105 | 2018-08-03 05:20:23 +0000 | [diff] [blame] | 1827 | if ((TU->ParsingOptions & CXTranslationUnit_VisitImplicitAttributes || |
| 1828 | !I->isImplicit()) && |
| 1829 | Visit(MakeCXCursor(I, D, TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1830 | return true; |
| 1831 | |
| 1832 | return false; |
| 1833 | } |
| 1834 | |
| 1835 | //===----------------------------------------------------------------------===// |
| 1836 | // Data-recursive visitor methods. |
| 1837 | //===----------------------------------------------------------------------===// |
| 1838 | |
| 1839 | namespace { |
| 1840 | #define DEF_JOB(NAME, DATA, KIND)\ |
| 1841 | class NAME : public VisitorJob {\ |
| 1842 | public:\ |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1843 | NAME(const DATA *d, CXCursor parent) : \ |
| 1844 | VisitorJob(parent, VisitorJob::KIND, d) {} \ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1845 | static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\ |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1846 | const DATA *get() const { return static_cast<const DATA*>(data[0]); }\ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1847 | }; |
| 1848 | |
| 1849 | DEF_JOB(StmtVisit, Stmt, StmtVisitKind) |
| 1850 | DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind) |
| 1851 | DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind) |
| 1852 | DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1853 | DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind) |
| 1854 | DEF_JOB(LambdaExprParts, LambdaExpr, LambdaExprPartsKind) |
| 1855 | DEF_JOB(PostChildrenVisit, void, PostChildrenVisitKind) |
| 1856 | #undef DEF_JOB |
| 1857 | |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 1858 | class ExplicitTemplateArgsVisit : public VisitorJob { |
| 1859 | public: |
| 1860 | ExplicitTemplateArgsVisit(const TemplateArgumentLoc *Begin, |
| 1861 | const TemplateArgumentLoc *End, CXCursor parent) |
| 1862 | : VisitorJob(parent, VisitorJob::ExplicitTemplateArgsVisitKind, Begin, |
| 1863 | End) {} |
| 1864 | static bool classof(const VisitorJob *VJ) { |
| 1865 | return VJ->getKind() == ExplicitTemplateArgsVisitKind; |
| 1866 | } |
| 1867 | const TemplateArgumentLoc *begin() const { |
| 1868 | return static_cast<const TemplateArgumentLoc *>(data[0]); |
| 1869 | } |
| 1870 | const TemplateArgumentLoc *end() { |
| 1871 | return static_cast<const TemplateArgumentLoc *>(data[1]); |
| 1872 | } |
| 1873 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1874 | class DeclVisit : public VisitorJob { |
| 1875 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1876 | DeclVisit(const Decl *D, CXCursor parent, bool isFirst) : |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1877 | VisitorJob(parent, VisitorJob::DeclVisitKind, |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 1878 | D, isFirst ? (void*) 1 : (void*) nullptr) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1879 | static bool classof(const VisitorJob *VJ) { |
| 1880 | return VJ->getKind() == DeclVisitKind; |
| 1881 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1882 | const Decl *get() const { return static_cast<const Decl *>(data[0]); } |
Dmitri Gribenko | e5423a7 | 2015-03-23 19:23:50 +0000 | [diff] [blame] | 1883 | bool isFirst() const { return data[1] != nullptr; } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1884 | }; |
| 1885 | class TypeLocVisit : public VisitorJob { |
| 1886 | public: |
| 1887 | TypeLocVisit(TypeLoc tl, CXCursor parent) : |
| 1888 | VisitorJob(parent, VisitorJob::TypeLocVisitKind, |
| 1889 | tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {} |
| 1890 | |
| 1891 | static bool classof(const VisitorJob *VJ) { |
| 1892 | return VJ->getKind() == TypeLocVisitKind; |
| 1893 | } |
| 1894 | |
| 1895 | TypeLoc get() const { |
| 1896 | QualType T = QualType::getFromOpaquePtr(data[0]); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1897 | return TypeLoc(T, const_cast<void *>(data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1898 | } |
| 1899 | }; |
| 1900 | |
| 1901 | class LabelRefVisit : public VisitorJob { |
| 1902 | public: |
| 1903 | LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent) |
| 1904 | : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD, |
| 1905 | labelLoc.getPtrEncoding()) {} |
| 1906 | |
| 1907 | static bool classof(const VisitorJob *VJ) { |
| 1908 | return VJ->getKind() == VisitorJob::LabelRefVisitKind; |
| 1909 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1910 | const LabelDecl *get() const { |
| 1911 | return static_cast<const LabelDecl *>(data[0]); |
| 1912 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1913 | SourceLocation getLoc() const { |
| 1914 | return SourceLocation::getFromPtrEncoding(data[1]); } |
| 1915 | }; |
| 1916 | |
| 1917 | class NestedNameSpecifierLocVisit : public VisitorJob { |
| 1918 | public: |
| 1919 | NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent) |
| 1920 | : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind, |
| 1921 | Qualifier.getNestedNameSpecifier(), |
| 1922 | Qualifier.getOpaqueData()) { } |
| 1923 | |
| 1924 | static bool classof(const VisitorJob *VJ) { |
| 1925 | return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind; |
| 1926 | } |
| 1927 | |
| 1928 | NestedNameSpecifierLoc get() const { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1929 | return NestedNameSpecifierLoc( |
| 1930 | const_cast<NestedNameSpecifier *>( |
| 1931 | static_cast<const NestedNameSpecifier *>(data[0])), |
| 1932 | const_cast<void *>(data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1933 | } |
| 1934 | }; |
| 1935 | |
| 1936 | class DeclarationNameInfoVisit : public VisitorJob { |
| 1937 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1938 | DeclarationNameInfoVisit(const Stmt *S, CXCursor parent) |
Dmitri Gribenko | dd7dacf | 2013-02-03 13:19:54 +0000 | [diff] [blame] | 1939 | : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1940 | static bool classof(const VisitorJob *VJ) { |
| 1941 | return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind; |
| 1942 | } |
| 1943 | DeclarationNameInfo get() const { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1944 | const Stmt *S = static_cast<const Stmt *>(data[0]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1945 | switch (S->getStmtClass()) { |
| 1946 | default: |
| 1947 | llvm_unreachable("Unhandled Stmt"); |
| 1948 | case clang::Stmt::MSDependentExistsStmtClass: |
| 1949 | return cast<MSDependentExistsStmt>(S)->getNameInfo(); |
| 1950 | case Stmt::CXXDependentScopeMemberExprClass: |
| 1951 | return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo(); |
| 1952 | case Stmt::DependentScopeDeclRefExprClass: |
| 1953 | return cast<DependentScopeDeclRefExpr>(S)->getNameInfo(); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1954 | case Stmt::OMPCriticalDirectiveClass: |
| 1955 | return cast<OMPCriticalDirective>(S)->getDirectiveName(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1956 | } |
| 1957 | } |
| 1958 | }; |
| 1959 | class MemberRefVisit : public VisitorJob { |
| 1960 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1961 | MemberRefVisit(const FieldDecl *D, SourceLocation L, CXCursor parent) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1962 | : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D, |
| 1963 | L.getPtrEncoding()) {} |
| 1964 | static bool classof(const VisitorJob *VJ) { |
| 1965 | return VJ->getKind() == VisitorJob::MemberRefVisitKind; |
| 1966 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1967 | const FieldDecl *get() const { |
| 1968 | return static_cast<const FieldDecl *>(data[0]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1969 | } |
| 1970 | SourceLocation getLoc() const { |
| 1971 | return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]); |
| 1972 | } |
| 1973 | }; |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1974 | class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void> { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1975 | friend class OMPClauseEnqueue; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1976 | VisitorWorkList &WL; |
| 1977 | CXCursor Parent; |
| 1978 | public: |
| 1979 | EnqueueVisitor(VisitorWorkList &wl, CXCursor parent) |
| 1980 | : WL(wl), Parent(parent) {} |
| 1981 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1982 | void VisitAddrLabelExpr(const AddrLabelExpr *E); |
| 1983 | void VisitBlockExpr(const BlockExpr *B); |
| 1984 | void VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 1985 | void VisitCompoundStmt(const CompoundStmt *S); |
| 1986 | void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { /* Do nothing. */ } |
| 1987 | void VisitMSDependentExistsStmt(const MSDependentExistsStmt *S); |
| 1988 | void VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E); |
| 1989 | void VisitCXXNewExpr(const CXXNewExpr *E); |
| 1990 | void VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E); |
| 1991 | void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *E); |
| 1992 | void VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E); |
| 1993 | void VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *E); |
| 1994 | void VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
| 1995 | void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *E); |
| 1996 | void VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
| 1997 | void VisitCXXCatchStmt(const CXXCatchStmt *S); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 1998 | void VisitCXXForRangeStmt(const CXXForRangeStmt *S); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1999 | void VisitDeclRefExpr(const DeclRefExpr *D); |
| 2000 | void VisitDeclStmt(const DeclStmt *S); |
| 2001 | void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E); |
| 2002 | void VisitDesignatedInitExpr(const DesignatedInitExpr *E); |
| 2003 | void VisitExplicitCastExpr(const ExplicitCastExpr *E); |
| 2004 | void VisitForStmt(const ForStmt *FS); |
| 2005 | void VisitGotoStmt(const GotoStmt *GS); |
| 2006 | void VisitIfStmt(const IfStmt *If); |
| 2007 | void VisitInitListExpr(const InitListExpr *IE); |
| 2008 | void VisitMemberExpr(const MemberExpr *M); |
| 2009 | void VisitOffsetOfExpr(const OffsetOfExpr *E); |
| 2010 | void VisitObjCEncodeExpr(const ObjCEncodeExpr *E); |
| 2011 | void VisitObjCMessageExpr(const ObjCMessageExpr *M); |
| 2012 | void VisitOverloadExpr(const OverloadExpr *E); |
| 2013 | void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
| 2014 | void VisitStmt(const Stmt *S); |
| 2015 | void VisitSwitchStmt(const SwitchStmt *S); |
| 2016 | void VisitWhileStmt(const WhileStmt *W); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2017 | void VisitTypeTraitExpr(const TypeTraitExpr *E); |
| 2018 | void VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E); |
| 2019 | void VisitExpressionTraitExpr(const ExpressionTraitExpr *E); |
| 2020 | void VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U); |
| 2021 | void VisitVAArgExpr(const VAArgExpr *E); |
| 2022 | void VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
| 2023 | void VisitPseudoObjectExpr(const PseudoObjectExpr *E); |
| 2024 | void VisitOpaqueValueExpr(const OpaqueValueExpr *E); |
| 2025 | void VisitLambdaExpr(const LambdaExpr *E); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2026 | void VisitOMPExecutableDirective(const OMPExecutableDirective *D); |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2027 | void VisitOMPLoopDirective(const OMPLoopDirective *D); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2028 | void VisitOMPParallelDirective(const OMPParallelDirective *D); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2029 | void VisitOMPSimdDirective(const OMPSimdDirective *D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2030 | void VisitOMPForDirective(const OMPForDirective *D); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2031 | void VisitOMPForSimdDirective(const OMPForSimdDirective *D); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2032 | void VisitOMPSectionsDirective(const OMPSectionsDirective *D); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2033 | void VisitOMPSectionDirective(const OMPSectionDirective *D); |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 2034 | void VisitOMPSingleDirective(const OMPSingleDirective *D); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2035 | void VisitOMPMasterDirective(const OMPMasterDirective *D); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2036 | void VisitOMPCriticalDirective(const OMPCriticalDirective *D); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2037 | void VisitOMPParallelForDirective(const OMPParallelForDirective *D); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2038 | void VisitOMPParallelForSimdDirective(const OMPParallelForSimdDirective *D); |
cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 2039 | void VisitOMPParallelMasterDirective(const OMPParallelMasterDirective *D); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2040 | void VisitOMPParallelSectionsDirective(const OMPParallelSectionsDirective *D); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2041 | void VisitOMPTaskDirective(const OMPTaskDirective *D); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2042 | void VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 2043 | void VisitOMPBarrierDirective(const OMPBarrierDirective *D); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 2044 | void VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2045 | void VisitOMPTaskgroupDirective(const OMPTaskgroupDirective *D); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2046 | void |
| 2047 | VisitOMPCancellationPointDirective(const OMPCancellationPointDirective *D); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2048 | void VisitOMPCancelDirective(const OMPCancelDirective *D); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2049 | void VisitOMPFlushDirective(const OMPFlushDirective *D); |
Alexey Bataev | c112e94 | 2020-02-28 09:52:15 -0500 | [diff] [blame] | 2050 | void VisitOMPDepobjDirective(const OMPDepobjDirective *D); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2051 | void VisitOMPOrderedDirective(const OMPOrderedDirective *D); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2052 | void VisitOMPAtomicDirective(const OMPAtomicDirective *D); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 2053 | void VisitOMPTargetDirective(const OMPTargetDirective *D); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2054 | void VisitOMPTargetDataDirective(const OMPTargetDataDirective *D); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 2055 | void VisitOMPTargetEnterDataDirective(const OMPTargetEnterDataDirective *D); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 2056 | void VisitOMPTargetExitDataDirective(const OMPTargetExitDataDirective *D); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 2057 | void VisitOMPTargetParallelDirective(const OMPTargetParallelDirective *D); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 2058 | void |
| 2059 | VisitOMPTargetParallelForDirective(const OMPTargetParallelForDirective *D); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2060 | void VisitOMPTeamsDirective(const OMPTeamsDirective *D); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 2061 | void VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 2062 | void VisitOMPTaskLoopSimdDirective(const OMPTaskLoopSimdDirective *D); |
Alexey Bataev | 60e51c4 | 2019-10-10 20:13:02 +0000 | [diff] [blame] | 2063 | void VisitOMPMasterTaskLoopDirective(const OMPMasterTaskLoopDirective *D); |
Alexey Bataev | b8552ab | 2019-10-18 16:47:35 +0000 | [diff] [blame] | 2064 | void |
| 2065 | VisitOMPMasterTaskLoopSimdDirective(const OMPMasterTaskLoopSimdDirective *D); |
Alexey Bataev | 5bbcead | 2019-10-14 17:17:41 +0000 | [diff] [blame] | 2066 | void VisitOMPParallelMasterTaskLoopDirective( |
| 2067 | const OMPParallelMasterTaskLoopDirective *D); |
Alexey Bataev | 14a388f | 2019-10-25 10:27:13 -0400 | [diff] [blame] | 2068 | void VisitOMPParallelMasterTaskLoopSimdDirective( |
| 2069 | const OMPParallelMasterTaskLoopSimdDirective *D); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2070 | void VisitOMPDistributeDirective(const OMPDistributeDirective *D); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2071 | void VisitOMPDistributeParallelForDirective( |
| 2072 | const OMPDistributeParallelForDirective *D); |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2073 | void VisitOMPDistributeParallelForSimdDirective( |
| 2074 | const OMPDistributeParallelForSimdDirective *D); |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2075 | void VisitOMPDistributeSimdDirective(const OMPDistributeSimdDirective *D); |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 2076 | void VisitOMPTargetParallelForSimdDirective( |
| 2077 | const OMPTargetParallelForSimdDirective *D); |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2078 | void VisitOMPTargetSimdDirective(const OMPTargetSimdDirective *D); |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 2079 | void VisitOMPTeamsDistributeDirective(const OMPTeamsDistributeDirective *D); |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 2080 | void VisitOMPTeamsDistributeSimdDirective( |
| 2081 | const OMPTeamsDistributeSimdDirective *D); |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 2082 | void VisitOMPTeamsDistributeParallelForSimdDirective( |
| 2083 | const OMPTeamsDistributeParallelForSimdDirective *D); |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 2084 | void VisitOMPTeamsDistributeParallelForDirective( |
| 2085 | const OMPTeamsDistributeParallelForDirective *D); |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 2086 | void VisitOMPTargetTeamsDirective(const OMPTargetTeamsDirective *D); |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 2087 | void VisitOMPTargetTeamsDistributeDirective( |
| 2088 | const OMPTargetTeamsDistributeDirective *D); |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 2089 | void VisitOMPTargetTeamsDistributeParallelForDirective( |
| 2090 | const OMPTargetTeamsDistributeParallelForDirective *D); |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 2091 | void VisitOMPTargetTeamsDistributeParallelForSimdDirective( |
| 2092 | const OMPTargetTeamsDistributeParallelForSimdDirective *D); |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 2093 | void VisitOMPTargetTeamsDistributeSimdDirective( |
| 2094 | const OMPTargetTeamsDistributeSimdDirective *D); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2095 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2096 | private: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2097 | void AddDeclarationNameInfo(const Stmt *S); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2098 | void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier); |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2099 | void AddExplicitTemplateArgs(const TemplateArgumentLoc *A, |
| 2100 | unsigned NumTemplateArgs); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2101 | void AddMemberRef(const FieldDecl *D, SourceLocation L); |
| 2102 | void AddStmt(const Stmt *S); |
| 2103 | void AddDecl(const Decl *D, bool isFirst = true); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2104 | void AddTypeLoc(TypeSourceInfo *TI); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2105 | void EnqueueChildren(const Stmt *S); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2106 | void EnqueueChildren(const OMPClause *S); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2107 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2108 | } // end anonyous namespace |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2109 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2110 | void EnqueueVisitor::AddDeclarationNameInfo(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2111 | // 'S' should always be non-null, since it comes from the |
| 2112 | // statement we are visiting. |
| 2113 | WL.push_back(DeclarationNameInfoVisit(S, Parent)); |
| 2114 | } |
| 2115 | |
| 2116 | void |
| 2117 | EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 2118 | if (Qualifier) |
| 2119 | WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent)); |
| 2120 | } |
| 2121 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2122 | void EnqueueVisitor::AddStmt(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2123 | if (S) |
| 2124 | WL.push_back(StmtVisit(S, Parent)); |
| 2125 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2126 | void EnqueueVisitor::AddDecl(const Decl *D, bool isFirst) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2127 | if (D) |
| 2128 | WL.push_back(DeclVisit(D, Parent, isFirst)); |
| 2129 | } |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2130 | void EnqueueVisitor::AddExplicitTemplateArgs(const TemplateArgumentLoc *A, |
| 2131 | unsigned NumTemplateArgs) { |
| 2132 | WL.push_back(ExplicitTemplateArgsVisit(A, A + NumTemplateArgs, Parent)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2133 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2134 | void EnqueueVisitor::AddMemberRef(const FieldDecl *D, SourceLocation L) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2135 | if (D) |
| 2136 | WL.push_back(MemberRefVisit(D, L, Parent)); |
| 2137 | } |
| 2138 | void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) { |
| 2139 | if (TI) |
| 2140 | WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent)); |
| 2141 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2142 | void EnqueueVisitor::EnqueueChildren(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2143 | unsigned size = WL.size(); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2144 | for (const Stmt *SubStmt : S->children()) { |
| 2145 | AddStmt(SubStmt); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2146 | } |
| 2147 | if (size == WL.size()) |
| 2148 | return; |
| 2149 | // Now reverse the entries we just added. This will match the DFS |
| 2150 | // ordering performed by the worklist. |
| 2151 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2152 | std::reverse(I, E); |
| 2153 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2154 | namespace { |
| 2155 | class OMPClauseEnqueue : public ConstOMPClauseVisitor<OMPClauseEnqueue> { |
| 2156 | EnqueueVisitor *Visitor; |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2157 | /// Process clauses with list of variables. |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2158 | template <typename T> |
| 2159 | void VisitOMPClauseList(T *Node); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2160 | public: |
| 2161 | OMPClauseEnqueue(EnqueueVisitor *Visitor) : Visitor(Visitor) { } |
| 2162 | #define OPENMP_CLAUSE(Name, Class) \ |
| 2163 | void Visit##Class(const Class *C); |
| 2164 | #include "clang/Basic/OpenMPKinds.def" |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2165 | void VisitOMPClauseWithPreInit(const OMPClauseWithPreInit *C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2166 | void VisitOMPClauseWithPostUpdate(const OMPClauseWithPostUpdate *C); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2167 | }; |
| 2168 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2169 | void OMPClauseEnqueue::VisitOMPClauseWithPreInit( |
| 2170 | const OMPClauseWithPreInit *C) { |
| 2171 | Visitor->AddStmt(C->getPreInitStmt()); |
| 2172 | } |
| 2173 | |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2174 | void OMPClauseEnqueue::VisitOMPClauseWithPostUpdate( |
| 2175 | const OMPClauseWithPostUpdate *C) { |
Alexey Bataev | 37e594c | 2016-03-04 07:21:16 +0000 | [diff] [blame] | 2176 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2177 | Visitor->AddStmt(C->getPostUpdateExpr()); |
| 2178 | } |
| 2179 | |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2180 | void OMPClauseEnqueue::VisitOMPIfClause(const OMPIfClause *C) { |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 2181 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2182 | Visitor->AddStmt(C->getCondition()); |
| 2183 | } |
| 2184 | |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 2185 | void OMPClauseEnqueue::VisitOMPFinalClause(const OMPFinalClause *C) { |
| 2186 | Visitor->AddStmt(C->getCondition()); |
| 2187 | } |
| 2188 | |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2189 | void OMPClauseEnqueue::VisitOMPNumThreadsClause(const OMPNumThreadsClause *C) { |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 2190 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2191 | Visitor->AddStmt(C->getNumThreads()); |
| 2192 | } |
| 2193 | |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 2194 | void OMPClauseEnqueue::VisitOMPSafelenClause(const OMPSafelenClause *C) { |
| 2195 | Visitor->AddStmt(C->getSafelen()); |
| 2196 | } |
| 2197 | |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 2198 | void OMPClauseEnqueue::VisitOMPSimdlenClause(const OMPSimdlenClause *C) { |
| 2199 | Visitor->AddStmt(C->getSimdlen()); |
| 2200 | } |
| 2201 | |
Alexey Bataev | 9cc10fc | 2019-03-12 18:52:33 +0000 | [diff] [blame] | 2202 | void OMPClauseEnqueue::VisitOMPAllocatorClause(const OMPAllocatorClause *C) { |
| 2203 | Visitor->AddStmt(C->getAllocator()); |
| 2204 | } |
| 2205 | |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 2206 | void OMPClauseEnqueue::VisitOMPCollapseClause(const OMPCollapseClause *C) { |
| 2207 | Visitor->AddStmt(C->getNumForLoops()); |
| 2208 | } |
| 2209 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2210 | void OMPClauseEnqueue::VisitOMPDefaultClause(const OMPDefaultClause *C) { } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2211 | |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 2212 | void OMPClauseEnqueue::VisitOMPProcBindClause(const OMPProcBindClause *C) { } |
| 2213 | |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2214 | void OMPClauseEnqueue::VisitOMPScheduleClause(const OMPScheduleClause *C) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2215 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2216 | Visitor->AddStmt(C->getChunkSize()); |
| 2217 | } |
| 2218 | |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 2219 | void OMPClauseEnqueue::VisitOMPOrderedClause(const OMPOrderedClause *C) { |
| 2220 | Visitor->AddStmt(C->getNumForLoops()); |
| 2221 | } |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 2222 | |
Alexey Bataev | 0f0564b | 2020-03-17 09:17:42 -0400 | [diff] [blame^] | 2223 | void OMPClauseEnqueue::VisitOMPDetachClause(const OMPDetachClause *C) { |
| 2224 | Visitor->AddStmt(C->getEventHandler()); |
| 2225 | } |
| 2226 | |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 2227 | void OMPClauseEnqueue::VisitOMPNowaitClause(const OMPNowaitClause *) {} |
| 2228 | |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 2229 | void OMPClauseEnqueue::VisitOMPUntiedClause(const OMPUntiedClause *) {} |
| 2230 | |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 2231 | void OMPClauseEnqueue::VisitOMPMergeableClause(const OMPMergeableClause *) {} |
| 2232 | |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 2233 | void OMPClauseEnqueue::VisitOMPReadClause(const OMPReadClause *) {} |
| 2234 | |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 2235 | void OMPClauseEnqueue::VisitOMPWriteClause(const OMPWriteClause *) {} |
| 2236 | |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 2237 | void OMPClauseEnqueue::VisitOMPUpdateClause(const OMPUpdateClause *) {} |
| 2238 | |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 2239 | void OMPClauseEnqueue::VisitOMPCaptureClause(const OMPCaptureClause *) {} |
| 2240 | |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 2241 | void OMPClauseEnqueue::VisitOMPSeqCstClause(const OMPSeqCstClause *) {} |
| 2242 | |
Alexey Bataev | ea9166b | 2020-02-06 16:30:23 -0500 | [diff] [blame] | 2243 | void OMPClauseEnqueue::VisitOMPAcqRelClause(const OMPAcqRelClause *) {} |
| 2244 | |
Alexey Bataev | 04a830f | 2020-02-10 14:30:39 -0500 | [diff] [blame] | 2245 | void OMPClauseEnqueue::VisitOMPAcquireClause(const OMPAcquireClause *) {} |
| 2246 | |
Alexey Bataev | 9559834 | 2020-02-10 15:49:05 -0500 | [diff] [blame] | 2247 | void OMPClauseEnqueue::VisitOMPReleaseClause(const OMPReleaseClause *) {} |
| 2248 | |
Alexey Bataev | 9a8defc | 2020-02-11 11:10:43 -0500 | [diff] [blame] | 2249 | void OMPClauseEnqueue::VisitOMPRelaxedClause(const OMPRelaxedClause *) {} |
| 2250 | |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 2251 | void OMPClauseEnqueue::VisitOMPThreadsClause(const OMPThreadsClause *) {} |
| 2252 | |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 2253 | void OMPClauseEnqueue::VisitOMPSIMDClause(const OMPSIMDClause *) {} |
| 2254 | |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 2255 | void OMPClauseEnqueue::VisitOMPNogroupClause(const OMPNogroupClause *) {} |
| 2256 | |
Alexey Bataev | 375437a | 2020-03-02 14:21:20 -0500 | [diff] [blame] | 2257 | void OMPClauseEnqueue::VisitOMPDestroyClause(const OMPDestroyClause *) {} |
| 2258 | |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 2259 | void OMPClauseEnqueue::VisitOMPUnifiedAddressClause( |
| 2260 | const OMPUnifiedAddressClause *) {} |
| 2261 | |
Patrick Lyster | 4a370b9 | 2018-10-01 13:47:43 +0000 | [diff] [blame] | 2262 | void OMPClauseEnqueue::VisitOMPUnifiedSharedMemoryClause( |
| 2263 | const OMPUnifiedSharedMemoryClause *) {} |
| 2264 | |
Patrick Lyster | 6bdf63b | 2018-10-03 20:07:58 +0000 | [diff] [blame] | 2265 | void OMPClauseEnqueue::VisitOMPReverseOffloadClause( |
| 2266 | const OMPReverseOffloadClause *) {} |
| 2267 | |
Patrick Lyster | 3fe9e39 | 2018-10-11 14:41:10 +0000 | [diff] [blame] | 2268 | void OMPClauseEnqueue::VisitOMPDynamicAllocatorsClause( |
| 2269 | const OMPDynamicAllocatorsClause *) {} |
| 2270 | |
Patrick Lyster | 7a2a27c | 2018-11-02 12:18:11 +0000 | [diff] [blame] | 2271 | void OMPClauseEnqueue::VisitOMPAtomicDefaultMemOrderClause( |
| 2272 | const OMPAtomicDefaultMemOrderClause *) {} |
| 2273 | |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 2274 | void OMPClauseEnqueue::VisitOMPDeviceClause(const OMPDeviceClause *C) { |
| 2275 | Visitor->AddStmt(C->getDevice()); |
| 2276 | } |
| 2277 | |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 2278 | void OMPClauseEnqueue::VisitOMPNumTeamsClause(const OMPNumTeamsClause *C) { |
Arpith Chacko Jacob | bc12634 | 2017-01-25 11:28:18 +0000 | [diff] [blame] | 2279 | VisitOMPClauseWithPreInit(C); |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 2280 | Visitor->AddStmt(C->getNumTeams()); |
| 2281 | } |
| 2282 | |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 2283 | void OMPClauseEnqueue::VisitOMPThreadLimitClause(const OMPThreadLimitClause *C) { |
Arpith Chacko Jacob | 7ecc0b7 | 2017-01-25 11:44:35 +0000 | [diff] [blame] | 2284 | VisitOMPClauseWithPreInit(C); |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 2285 | Visitor->AddStmt(C->getThreadLimit()); |
| 2286 | } |
| 2287 | |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 2288 | void OMPClauseEnqueue::VisitOMPPriorityClause(const OMPPriorityClause *C) { |
| 2289 | Visitor->AddStmt(C->getPriority()); |
| 2290 | } |
| 2291 | |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 2292 | void OMPClauseEnqueue::VisitOMPGrainsizeClause(const OMPGrainsizeClause *C) { |
| 2293 | Visitor->AddStmt(C->getGrainsize()); |
| 2294 | } |
| 2295 | |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 2296 | void OMPClauseEnqueue::VisitOMPNumTasksClause(const OMPNumTasksClause *C) { |
| 2297 | Visitor->AddStmt(C->getNumTasks()); |
| 2298 | } |
| 2299 | |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 2300 | void OMPClauseEnqueue::VisitOMPHintClause(const OMPHintClause *C) { |
| 2301 | Visitor->AddStmt(C->getHint()); |
| 2302 | } |
| 2303 | |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2304 | template<typename T> |
| 2305 | void OMPClauseEnqueue::VisitOMPClauseList(T *Node) { |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2306 | for (const auto *I : Node->varlists()) { |
Aaron Ballman | 2205d2a | 2014-03-14 15:55:35 +0000 | [diff] [blame] | 2307 | Visitor->AddStmt(I); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2308 | } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2309 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2310 | |
Alexey Bataev | e04483e | 2019-03-27 14:14:31 +0000 | [diff] [blame] | 2311 | void OMPClauseEnqueue::VisitOMPAllocateClause(const OMPAllocateClause *C) { |
| 2312 | VisitOMPClauseList(C); |
| 2313 | Visitor->AddStmt(C->getAllocator()); |
| 2314 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2315 | void OMPClauseEnqueue::VisitOMPPrivateClause(const OMPPrivateClause *C) { |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2316 | VisitOMPClauseList(C); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2317 | for (const auto *E : C->private_copies()) { |
| 2318 | Visitor->AddStmt(E); |
| 2319 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2320 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2321 | void OMPClauseEnqueue::VisitOMPFirstprivateClause( |
| 2322 | const OMPFirstprivateClause *C) { |
| 2323 | VisitOMPClauseList(C); |
Alexey Bataev | 417089f | 2016-02-17 13:19:37 +0000 | [diff] [blame] | 2324 | VisitOMPClauseWithPreInit(C); |
| 2325 | for (const auto *E : C->private_copies()) { |
| 2326 | Visitor->AddStmt(E); |
| 2327 | } |
| 2328 | for (const auto *E : C->inits()) { |
| 2329 | Visitor->AddStmt(E); |
| 2330 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2331 | } |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2332 | void OMPClauseEnqueue::VisitOMPLastprivateClause( |
| 2333 | const OMPLastprivateClause *C) { |
| 2334 | VisitOMPClauseList(C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2335 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2336 | for (auto *E : C->private_copies()) { |
| 2337 | Visitor->AddStmt(E); |
| 2338 | } |
| 2339 | for (auto *E : C->source_exprs()) { |
| 2340 | Visitor->AddStmt(E); |
| 2341 | } |
| 2342 | for (auto *E : C->destination_exprs()) { |
| 2343 | Visitor->AddStmt(E); |
| 2344 | } |
| 2345 | for (auto *E : C->assignment_ops()) { |
| 2346 | Visitor->AddStmt(E); |
| 2347 | } |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2348 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2349 | void OMPClauseEnqueue::VisitOMPSharedClause(const OMPSharedClause *C) { |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2350 | VisitOMPClauseList(C); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2351 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2352 | void OMPClauseEnqueue::VisitOMPReductionClause(const OMPReductionClause *C) { |
| 2353 | VisitOMPClauseList(C); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2354 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 2355 | for (auto *E : C->privates()) { |
| 2356 | Visitor->AddStmt(E); |
| 2357 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2358 | for (auto *E : C->lhs_exprs()) { |
| 2359 | Visitor->AddStmt(E); |
| 2360 | } |
| 2361 | for (auto *E : C->rhs_exprs()) { |
| 2362 | Visitor->AddStmt(E); |
| 2363 | } |
| 2364 | for (auto *E : C->reduction_ops()) { |
| 2365 | Visitor->AddStmt(E); |
| 2366 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2367 | } |
Alexey Bataev | 169d96a | 2017-07-18 20:17:46 +0000 | [diff] [blame] | 2368 | void OMPClauseEnqueue::VisitOMPTaskReductionClause( |
| 2369 | const OMPTaskReductionClause *C) { |
| 2370 | VisitOMPClauseList(C); |
| 2371 | VisitOMPClauseWithPostUpdate(C); |
| 2372 | for (auto *E : C->privates()) { |
| 2373 | Visitor->AddStmt(E); |
| 2374 | } |
| 2375 | for (auto *E : C->lhs_exprs()) { |
| 2376 | Visitor->AddStmt(E); |
| 2377 | } |
| 2378 | for (auto *E : C->rhs_exprs()) { |
| 2379 | Visitor->AddStmt(E); |
| 2380 | } |
| 2381 | for (auto *E : C->reduction_ops()) { |
| 2382 | Visitor->AddStmt(E); |
| 2383 | } |
| 2384 | } |
Alexey Bataev | fa312f3 | 2017-07-21 18:48:21 +0000 | [diff] [blame] | 2385 | void OMPClauseEnqueue::VisitOMPInReductionClause( |
| 2386 | const OMPInReductionClause *C) { |
| 2387 | VisitOMPClauseList(C); |
| 2388 | VisitOMPClauseWithPostUpdate(C); |
| 2389 | for (auto *E : C->privates()) { |
| 2390 | Visitor->AddStmt(E); |
| 2391 | } |
| 2392 | for (auto *E : C->lhs_exprs()) { |
| 2393 | Visitor->AddStmt(E); |
| 2394 | } |
| 2395 | for (auto *E : C->rhs_exprs()) { |
| 2396 | Visitor->AddStmt(E); |
| 2397 | } |
| 2398 | for (auto *E : C->reduction_ops()) { |
| 2399 | Visitor->AddStmt(E); |
| 2400 | } |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 2401 | for (auto *E : C->taskgroup_descriptors()) |
| 2402 | Visitor->AddStmt(E); |
Alexey Bataev | fa312f3 | 2017-07-21 18:48:21 +0000 | [diff] [blame] | 2403 | } |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2404 | void OMPClauseEnqueue::VisitOMPLinearClause(const OMPLinearClause *C) { |
| 2405 | VisitOMPClauseList(C); |
Alexey Bataev | 78849fb | 2016-03-09 09:49:00 +0000 | [diff] [blame] | 2406 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 2407 | for (const auto *E : C->privates()) { |
| 2408 | Visitor->AddStmt(E); |
| 2409 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 2410 | for (const auto *E : C->inits()) { |
| 2411 | Visitor->AddStmt(E); |
| 2412 | } |
| 2413 | for (const auto *E : C->updates()) { |
| 2414 | Visitor->AddStmt(E); |
| 2415 | } |
| 2416 | for (const auto *E : C->finals()) { |
| 2417 | Visitor->AddStmt(E); |
| 2418 | } |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2419 | Visitor->AddStmt(C->getStep()); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 2420 | Visitor->AddStmt(C->getCalcStep()); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2421 | } |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 2422 | void OMPClauseEnqueue::VisitOMPAlignedClause(const OMPAlignedClause *C) { |
| 2423 | VisitOMPClauseList(C); |
| 2424 | Visitor->AddStmt(C->getAlignment()); |
| 2425 | } |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 2426 | void OMPClauseEnqueue::VisitOMPCopyinClause(const OMPCopyinClause *C) { |
| 2427 | VisitOMPClauseList(C); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 2428 | for (auto *E : C->source_exprs()) { |
| 2429 | Visitor->AddStmt(E); |
| 2430 | } |
| 2431 | for (auto *E : C->destination_exprs()) { |
| 2432 | Visitor->AddStmt(E); |
| 2433 | } |
| 2434 | for (auto *E : C->assignment_ops()) { |
| 2435 | Visitor->AddStmt(E); |
| 2436 | } |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 2437 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2438 | void |
| 2439 | OMPClauseEnqueue::VisitOMPCopyprivateClause(const OMPCopyprivateClause *C) { |
| 2440 | VisitOMPClauseList(C); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2441 | for (auto *E : C->source_exprs()) { |
| 2442 | Visitor->AddStmt(E); |
| 2443 | } |
| 2444 | for (auto *E : C->destination_exprs()) { |
| 2445 | Visitor->AddStmt(E); |
| 2446 | } |
| 2447 | for (auto *E : C->assignment_ops()) { |
| 2448 | Visitor->AddStmt(E); |
| 2449 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2450 | } |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2451 | void OMPClauseEnqueue::VisitOMPFlushClause(const OMPFlushClause *C) { |
| 2452 | VisitOMPClauseList(C); |
| 2453 | } |
Alexey Bataev | c112e94 | 2020-02-28 09:52:15 -0500 | [diff] [blame] | 2454 | void OMPClauseEnqueue::VisitOMPDepobjClause(const OMPDepobjClause *C) { |
| 2455 | Visitor->AddStmt(C->getDepobj()); |
| 2456 | } |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 2457 | void OMPClauseEnqueue::VisitOMPDependClause(const OMPDependClause *C) { |
| 2458 | VisitOMPClauseList(C); |
| 2459 | } |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 2460 | void OMPClauseEnqueue::VisitOMPMapClause(const OMPMapClause *C) { |
| 2461 | VisitOMPClauseList(C); |
| 2462 | } |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2463 | void OMPClauseEnqueue::VisitOMPDistScheduleClause( |
| 2464 | const OMPDistScheduleClause *C) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2465 | VisitOMPClauseWithPreInit(C); |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2466 | Visitor->AddStmt(C->getChunkSize()); |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2467 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2468 | void OMPClauseEnqueue::VisitOMPDefaultmapClause( |
| 2469 | const OMPDefaultmapClause * /*C*/) {} |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 2470 | void OMPClauseEnqueue::VisitOMPToClause(const OMPToClause *C) { |
| 2471 | VisitOMPClauseList(C); |
| 2472 | } |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 2473 | void OMPClauseEnqueue::VisitOMPFromClause(const OMPFromClause *C) { |
| 2474 | VisitOMPClauseList(C); |
| 2475 | } |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 2476 | void OMPClauseEnqueue::VisitOMPUseDevicePtrClause(const OMPUseDevicePtrClause *C) { |
| 2477 | VisitOMPClauseList(C); |
| 2478 | } |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 2479 | void OMPClauseEnqueue::VisitOMPIsDevicePtrClause(const OMPIsDevicePtrClause *C) { |
| 2480 | VisitOMPClauseList(C); |
| 2481 | } |
Alexey Bataev | b6e7084 | 2019-12-16 15:54:17 -0500 | [diff] [blame] | 2482 | void OMPClauseEnqueue::VisitOMPNontemporalClause( |
| 2483 | const OMPNontemporalClause *C) { |
| 2484 | VisitOMPClauseList(C); |
Alexey Bataev | 0860db9 | 2019-12-19 10:01:10 -0500 | [diff] [blame] | 2485 | for (const auto *E : C->private_refs()) |
| 2486 | Visitor->AddStmt(E); |
Alexey Bataev | b6e7084 | 2019-12-16 15:54:17 -0500 | [diff] [blame] | 2487 | } |
Alexey Bataev | cb8e691 | 2020-01-31 16:09:26 -0500 | [diff] [blame] | 2488 | void OMPClauseEnqueue::VisitOMPOrderClause(const OMPOrderClause *C) {} |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2489 | } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2490 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2491 | void EnqueueVisitor::EnqueueChildren(const OMPClause *S) { |
| 2492 | unsigned size = WL.size(); |
| 2493 | OMPClauseEnqueue Visitor(this); |
| 2494 | Visitor.Visit(S); |
| 2495 | if (size == WL.size()) |
| 2496 | return; |
| 2497 | // Now reverse the entries we just added. This will match the DFS |
| 2498 | // ordering performed by the worklist. |
| 2499 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2500 | std::reverse(I, E); |
| 2501 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2502 | void EnqueueVisitor::VisitAddrLabelExpr(const AddrLabelExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2503 | WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent)); |
| 2504 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2505 | void EnqueueVisitor::VisitBlockExpr(const BlockExpr *B) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2506 | AddDecl(B->getBlockDecl()); |
| 2507 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2508 | void EnqueueVisitor::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2509 | EnqueueChildren(E); |
| 2510 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2511 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2512 | void EnqueueVisitor::VisitCompoundStmt(const CompoundStmt *S) { |
Pete Cooper | 57d3f14 | 2015-07-30 17:22:52 +0000 | [diff] [blame] | 2513 | for (auto &I : llvm::reverse(S->body())) |
| 2514 | AddStmt(I); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2515 | } |
| 2516 | void EnqueueVisitor:: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2517 | VisitMSDependentExistsStmt(const MSDependentExistsStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2518 | AddStmt(S->getSubStmt()); |
| 2519 | AddDeclarationNameInfo(S); |
| 2520 | if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc()) |
| 2521 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2522 | } |
| 2523 | |
| 2524 | void EnqueueVisitor:: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2525 | VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2526 | if (E->hasExplicitTemplateArgs()) |
| 2527 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2528 | AddDeclarationNameInfo(E); |
| 2529 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 2530 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2531 | if (!E->isImplicitAccess()) |
| 2532 | AddStmt(E->getBase()); |
| 2533 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2534 | void EnqueueVisitor::VisitCXXNewExpr(const CXXNewExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2535 | // Enqueue the initializer , if any. |
| 2536 | AddStmt(E->getInitializer()); |
| 2537 | // Enqueue the array size, if any. |
Richard Smith | b9fb121 | 2019-05-06 03:47:15 +0000 | [diff] [blame] | 2538 | AddStmt(E->getArraySize().getValueOr(nullptr)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2539 | // Enqueue the allocated type. |
| 2540 | AddTypeLoc(E->getAllocatedTypeSourceInfo()); |
| 2541 | // Enqueue the placement arguments. |
| 2542 | for (unsigned I = E->getNumPlacementArgs(); I > 0; --I) |
| 2543 | AddStmt(E->getPlacementArg(I-1)); |
| 2544 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2545 | void EnqueueVisitor::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CE) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2546 | for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I) |
| 2547 | AddStmt(CE->getArg(I-1)); |
| 2548 | AddStmt(CE->getCallee()); |
| 2549 | AddStmt(CE->getArg(0)); |
| 2550 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2551 | void EnqueueVisitor::VisitCXXPseudoDestructorExpr( |
| 2552 | const CXXPseudoDestructorExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2553 | // Visit the name of the type being destroyed. |
| 2554 | AddTypeLoc(E->getDestroyedTypeInfo()); |
| 2555 | // Visit the scope type that looks disturbingly like the nested-name-specifier |
| 2556 | // but isn't. |
| 2557 | AddTypeLoc(E->getScopeTypeInfo()); |
| 2558 | // Visit the nested-name-specifier. |
| 2559 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 2560 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2561 | // Visit base expression. |
| 2562 | AddStmt(E->getBase()); |
| 2563 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2564 | void EnqueueVisitor::VisitCXXScalarValueInitExpr( |
| 2565 | const CXXScalarValueInitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2566 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2567 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2568 | void EnqueueVisitor::VisitCXXTemporaryObjectExpr( |
| 2569 | const CXXTemporaryObjectExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2570 | EnqueueChildren(E); |
| 2571 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2572 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2573 | void EnqueueVisitor::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2574 | EnqueueChildren(E); |
| 2575 | if (E->isTypeOperand()) |
| 2576 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 2577 | } |
| 2578 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2579 | void EnqueueVisitor::VisitCXXUnresolvedConstructExpr( |
| 2580 | const CXXUnresolvedConstructExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2581 | EnqueueChildren(E); |
| 2582 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2583 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2584 | void EnqueueVisitor::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2585 | EnqueueChildren(E); |
| 2586 | if (E->isTypeOperand()) |
| 2587 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 2588 | } |
| 2589 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2590 | void EnqueueVisitor::VisitCXXCatchStmt(const CXXCatchStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2591 | EnqueueChildren(S); |
| 2592 | AddDecl(S->getExceptionDecl()); |
| 2593 | } |
| 2594 | |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2595 | void EnqueueVisitor::VisitCXXForRangeStmt(const CXXForRangeStmt *S) { |
Argyrios Kyrtzidis | cde7069 | 2014-11-13 09:50:19 +0000 | [diff] [blame] | 2596 | AddStmt(S->getBody()); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2597 | AddStmt(S->getRangeInit()); |
Argyrios Kyrtzidis | cde7069 | 2014-11-13 09:50:19 +0000 | [diff] [blame] | 2598 | AddDecl(S->getLoopVariable()); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2601 | void EnqueueVisitor::VisitDeclRefExpr(const DeclRefExpr *DR) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2602 | if (DR->hasExplicitTemplateArgs()) |
| 2603 | AddExplicitTemplateArgs(DR->getTemplateArgs(), DR->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2604 | WL.push_back(DeclRefExprParts(DR, Parent)); |
| 2605 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2606 | void EnqueueVisitor::VisitDependentScopeDeclRefExpr( |
| 2607 | const DependentScopeDeclRefExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2608 | if (E->hasExplicitTemplateArgs()) |
| 2609 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2610 | AddDeclarationNameInfo(E); |
| 2611 | AddNestedNameSpecifierLoc(E->getQualifierLoc()); |
| 2612 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2613 | void EnqueueVisitor::VisitDeclStmt(const DeclStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2614 | unsigned size = WL.size(); |
| 2615 | bool isFirst = true; |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 2616 | for (const auto *D : S->decls()) { |
| 2617 | AddDecl(D, isFirst); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2618 | isFirst = false; |
| 2619 | } |
| 2620 | if (size == WL.size()) |
| 2621 | return; |
| 2622 | // Now reverse the entries we just added. This will match the DFS |
| 2623 | // ordering performed by the worklist. |
| 2624 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2625 | std::reverse(I, E); |
| 2626 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2627 | void EnqueueVisitor::VisitDesignatedInitExpr(const DesignatedInitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2628 | AddStmt(E->getInit()); |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2629 | for (const DesignatedInitExpr::Designator &D : |
| 2630 | llvm::reverse(E->designators())) { |
| 2631 | if (D.isFieldDesignator()) { |
| 2632 | if (FieldDecl *Field = D.getField()) |
| 2633 | AddMemberRef(Field, D.getFieldLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2634 | continue; |
| 2635 | } |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2636 | if (D.isArrayDesignator()) { |
| 2637 | AddStmt(E->getArrayIndex(D)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2638 | continue; |
| 2639 | } |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2640 | assert(D.isArrayRangeDesignator() && "Unknown designator kind"); |
| 2641 | AddStmt(E->getArrayRangeEnd(D)); |
| 2642 | AddStmt(E->getArrayRangeStart(D)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2643 | } |
| 2644 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2645 | void EnqueueVisitor::VisitExplicitCastExpr(const ExplicitCastExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2646 | EnqueueChildren(E); |
| 2647 | AddTypeLoc(E->getTypeInfoAsWritten()); |
| 2648 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2649 | void EnqueueVisitor::VisitForStmt(const ForStmt *FS) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2650 | AddStmt(FS->getBody()); |
| 2651 | AddStmt(FS->getInc()); |
| 2652 | AddStmt(FS->getCond()); |
| 2653 | AddDecl(FS->getConditionVariable()); |
| 2654 | AddStmt(FS->getInit()); |
| 2655 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2656 | void EnqueueVisitor::VisitGotoStmt(const GotoStmt *GS) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2657 | WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent)); |
| 2658 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2659 | void EnqueueVisitor::VisitIfStmt(const IfStmt *If) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2660 | AddStmt(If->getElse()); |
| 2661 | AddStmt(If->getThen()); |
| 2662 | AddStmt(If->getCond()); |
| 2663 | AddDecl(If->getConditionVariable()); |
| 2664 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2665 | void EnqueueVisitor::VisitInitListExpr(const InitListExpr *IE) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2666 | // We care about the syntactic form of the initializer list, only. |
| 2667 | if (InitListExpr *Syntactic = IE->getSyntacticForm()) |
| 2668 | IE = Syntactic; |
| 2669 | EnqueueChildren(IE); |
| 2670 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2671 | void EnqueueVisitor::VisitMemberExpr(const MemberExpr *M) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2672 | WL.push_back(MemberExprParts(M, Parent)); |
| 2673 | |
| 2674 | // If the base of the member access expression is an implicit 'this', don't |
| 2675 | // visit it. |
| 2676 | // FIXME: If we ever want to show these implicit accesses, this will be |
| 2677 | // unfortunate. However, clang_getCursor() relies on this behavior. |
Argyrios Kyrtzidis | 58d0e7a | 2015-03-13 04:40:07 +0000 | [diff] [blame] | 2678 | if (M->isImplicitAccess()) |
| 2679 | return; |
| 2680 | |
| 2681 | // Ignore base anonymous struct/union fields, otherwise they will shadow the |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 2682 | // real field that we are interested in. |
Argyrios Kyrtzidis | 58d0e7a | 2015-03-13 04:40:07 +0000 | [diff] [blame] | 2683 | if (auto *SubME = dyn_cast<MemberExpr>(M->getBase())) { |
| 2684 | if (auto *FD = dyn_cast_or_null<FieldDecl>(SubME->getMemberDecl())) { |
| 2685 | if (FD->isAnonymousStructOrUnion()) { |
| 2686 | AddStmt(SubME->getBase()); |
| 2687 | return; |
| 2688 | } |
| 2689 | } |
| 2690 | } |
| 2691 | |
| 2692 | AddStmt(M->getBase()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2693 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2694 | void EnqueueVisitor::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2695 | AddTypeLoc(E->getEncodedTypeSourceInfo()); |
| 2696 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2697 | void EnqueueVisitor::VisitObjCMessageExpr(const ObjCMessageExpr *M) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2698 | EnqueueChildren(M); |
| 2699 | AddTypeLoc(M->getClassReceiverTypeInfo()); |
| 2700 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2701 | void EnqueueVisitor::VisitOffsetOfExpr(const OffsetOfExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2702 | // Visit the components of the offsetof expression. |
| 2703 | for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2704 | const OffsetOfNode &Node = E->getComponent(I-1); |
| 2705 | switch (Node.getKind()) { |
| 2706 | case OffsetOfNode::Array: |
| 2707 | AddStmt(E->getIndexExpr(Node.getArrayExprIndex())); |
| 2708 | break; |
| 2709 | case OffsetOfNode::Field: |
| 2710 | AddMemberRef(Node.getField(), Node.getSourceRange().getEnd()); |
| 2711 | break; |
| 2712 | case OffsetOfNode::Identifier: |
| 2713 | case OffsetOfNode::Base: |
| 2714 | continue; |
| 2715 | } |
| 2716 | } |
| 2717 | // Visit the type into which we're computing the offset. |
| 2718 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2719 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2720 | void EnqueueVisitor::VisitOverloadExpr(const OverloadExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2721 | if (E->hasExplicitTemplateArgs()) |
| 2722 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2723 | WL.push_back(OverloadExprParts(E, Parent)); |
| 2724 | } |
| 2725 | void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr( |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2726 | const UnaryExprOrTypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2727 | EnqueueChildren(E); |
| 2728 | if (E->isArgumentType()) |
| 2729 | AddTypeLoc(E->getArgumentTypeInfo()); |
| 2730 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2731 | void EnqueueVisitor::VisitStmt(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2732 | EnqueueChildren(S); |
| 2733 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2734 | void EnqueueVisitor::VisitSwitchStmt(const SwitchStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2735 | AddStmt(S->getBody()); |
| 2736 | AddStmt(S->getCond()); |
| 2737 | AddDecl(S->getConditionVariable()); |
| 2738 | } |
| 2739 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2740 | void EnqueueVisitor::VisitWhileStmt(const WhileStmt *W) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2741 | AddStmt(W->getBody()); |
| 2742 | AddStmt(W->getCond()); |
| 2743 | AddDecl(W->getConditionVariable()); |
| 2744 | } |
| 2745 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2746 | void EnqueueVisitor::VisitTypeTraitExpr(const TypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2747 | for (unsigned I = E->getNumArgs(); I > 0; --I) |
| 2748 | AddTypeLoc(E->getArg(I-1)); |
| 2749 | } |
| 2750 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2751 | void EnqueueVisitor::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2752 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2753 | } |
| 2754 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2755 | void EnqueueVisitor::VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2756 | EnqueueChildren(E); |
| 2757 | } |
| 2758 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2759 | void EnqueueVisitor::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2760 | VisitOverloadExpr(U); |
| 2761 | if (!U->isImplicitAccess()) |
| 2762 | AddStmt(U->getBase()); |
| 2763 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2764 | void EnqueueVisitor::VisitVAArgExpr(const VAArgExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2765 | AddStmt(E->getSubExpr()); |
| 2766 | AddTypeLoc(E->getWrittenTypeInfo()); |
| 2767 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2768 | void EnqueueVisitor::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2769 | WL.push_back(SizeOfPackExprParts(E, Parent)); |
| 2770 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2771 | void EnqueueVisitor::VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2772 | // If the opaque value has a source expression, just transparently |
| 2773 | // visit that. This is useful for (e.g.) pseudo-object expressions. |
| 2774 | if (Expr *SourceExpr = E->getSourceExpr()) |
| 2775 | return Visit(SourceExpr); |
| 2776 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2777 | void EnqueueVisitor::VisitLambdaExpr(const LambdaExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2778 | AddStmt(E->getBody()); |
| 2779 | WL.push_back(LambdaExprParts(E, Parent)); |
| 2780 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2781 | void EnqueueVisitor::VisitPseudoObjectExpr(const PseudoObjectExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2782 | // Treat the expression like its syntactic form. |
| 2783 | Visit(E->getSyntacticForm()); |
| 2784 | } |
| 2785 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2786 | void EnqueueVisitor::VisitOMPExecutableDirective( |
| 2787 | const OMPExecutableDirective *D) { |
| 2788 | EnqueueChildren(D); |
| 2789 | for (ArrayRef<OMPClause *>::iterator I = D->clauses().begin(), |
| 2790 | E = D->clauses().end(); |
| 2791 | I != E; ++I) |
| 2792 | EnqueueChildren(*I); |
| 2793 | } |
| 2794 | |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2795 | void EnqueueVisitor::VisitOMPLoopDirective(const OMPLoopDirective *D) { |
| 2796 | VisitOMPExecutableDirective(D); |
| 2797 | } |
| 2798 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2799 | void EnqueueVisitor::VisitOMPParallelDirective(const OMPParallelDirective *D) { |
| 2800 | VisitOMPExecutableDirective(D); |
| 2801 | } |
| 2802 | |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2803 | void EnqueueVisitor::VisitOMPSimdDirective(const OMPSimdDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2804 | VisitOMPLoopDirective(D); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2805 | } |
| 2806 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2807 | void EnqueueVisitor::VisitOMPForDirective(const OMPForDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2808 | VisitOMPLoopDirective(D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2811 | void EnqueueVisitor::VisitOMPForSimdDirective(const OMPForSimdDirective *D) { |
| 2812 | VisitOMPLoopDirective(D); |
| 2813 | } |
| 2814 | |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2815 | void EnqueueVisitor::VisitOMPSectionsDirective(const OMPSectionsDirective *D) { |
| 2816 | VisitOMPExecutableDirective(D); |
| 2817 | } |
| 2818 | |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2819 | void EnqueueVisitor::VisitOMPSectionDirective(const OMPSectionDirective *D) { |
| 2820 | VisitOMPExecutableDirective(D); |
| 2821 | } |
| 2822 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 2823 | void EnqueueVisitor::VisitOMPSingleDirective(const OMPSingleDirective *D) { |
| 2824 | VisitOMPExecutableDirective(D); |
| 2825 | } |
| 2826 | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2827 | void EnqueueVisitor::VisitOMPMasterDirective(const OMPMasterDirective *D) { |
| 2828 | VisitOMPExecutableDirective(D); |
| 2829 | } |
| 2830 | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2831 | void EnqueueVisitor::VisitOMPCriticalDirective(const OMPCriticalDirective *D) { |
| 2832 | VisitOMPExecutableDirective(D); |
| 2833 | AddDeclarationNameInfo(D); |
| 2834 | } |
| 2835 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2836 | void |
| 2837 | EnqueueVisitor::VisitOMPParallelForDirective(const OMPParallelForDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2838 | VisitOMPLoopDirective(D); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2841 | void EnqueueVisitor::VisitOMPParallelForSimdDirective( |
| 2842 | const OMPParallelForSimdDirective *D) { |
| 2843 | VisitOMPLoopDirective(D); |
| 2844 | } |
| 2845 | |
cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 2846 | void EnqueueVisitor::VisitOMPParallelMasterDirective( |
| 2847 | const OMPParallelMasterDirective *D) { |
| 2848 | VisitOMPExecutableDirective(D); |
| 2849 | } |
| 2850 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2851 | void EnqueueVisitor::VisitOMPParallelSectionsDirective( |
| 2852 | const OMPParallelSectionsDirective *D) { |
| 2853 | VisitOMPExecutableDirective(D); |
| 2854 | } |
| 2855 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2856 | void EnqueueVisitor::VisitOMPTaskDirective(const OMPTaskDirective *D) { |
| 2857 | VisitOMPExecutableDirective(D); |
| 2858 | } |
| 2859 | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2860 | void |
| 2861 | EnqueueVisitor::VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D) { |
| 2862 | VisitOMPExecutableDirective(D); |
| 2863 | } |
| 2864 | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 2865 | void EnqueueVisitor::VisitOMPBarrierDirective(const OMPBarrierDirective *D) { |
| 2866 | VisitOMPExecutableDirective(D); |
| 2867 | } |
| 2868 | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 2869 | void EnqueueVisitor::VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D) { |
| 2870 | VisitOMPExecutableDirective(D); |
| 2871 | } |
| 2872 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2873 | void EnqueueVisitor::VisitOMPTaskgroupDirective( |
| 2874 | const OMPTaskgroupDirective *D) { |
| 2875 | VisitOMPExecutableDirective(D); |
Alexey Bataev | 3b1b895 | 2017-07-25 15:53:26 +0000 | [diff] [blame] | 2876 | if (const Expr *E = D->getReductionRef()) |
| 2877 | VisitStmt(E); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2880 | void EnqueueVisitor::VisitOMPFlushDirective(const OMPFlushDirective *D) { |
| 2881 | VisitOMPExecutableDirective(D); |
| 2882 | } |
| 2883 | |
Alexey Bataev | c112e94 | 2020-02-28 09:52:15 -0500 | [diff] [blame] | 2884 | void EnqueueVisitor::VisitOMPDepobjDirective(const OMPDepobjDirective *D) { |
| 2885 | VisitOMPExecutableDirective(D); |
| 2886 | } |
| 2887 | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2888 | void EnqueueVisitor::VisitOMPOrderedDirective(const OMPOrderedDirective *D) { |
| 2889 | VisitOMPExecutableDirective(D); |
| 2890 | } |
| 2891 | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2892 | void EnqueueVisitor::VisitOMPAtomicDirective(const OMPAtomicDirective *D) { |
| 2893 | VisitOMPExecutableDirective(D); |
| 2894 | } |
| 2895 | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 2896 | void EnqueueVisitor::VisitOMPTargetDirective(const OMPTargetDirective *D) { |
| 2897 | VisitOMPExecutableDirective(D); |
| 2898 | } |
| 2899 | |
Alexey Bataev | c112e94 | 2020-02-28 09:52:15 -0500 | [diff] [blame] | 2900 | void EnqueueVisitor::VisitOMPTargetDataDirective( |
| 2901 | const OMPTargetDataDirective *D) { |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2902 | VisitOMPExecutableDirective(D); |
| 2903 | } |
| 2904 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 2905 | void EnqueueVisitor::VisitOMPTargetEnterDataDirective( |
| 2906 | const OMPTargetEnterDataDirective *D) { |
| 2907 | VisitOMPExecutableDirective(D); |
| 2908 | } |
| 2909 | |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 2910 | void EnqueueVisitor::VisitOMPTargetExitDataDirective( |
| 2911 | const OMPTargetExitDataDirective *D) { |
| 2912 | VisitOMPExecutableDirective(D); |
| 2913 | } |
| 2914 | |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 2915 | void EnqueueVisitor::VisitOMPTargetParallelDirective( |
| 2916 | const OMPTargetParallelDirective *D) { |
| 2917 | VisitOMPExecutableDirective(D); |
| 2918 | } |
| 2919 | |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 2920 | void EnqueueVisitor::VisitOMPTargetParallelForDirective( |
| 2921 | const OMPTargetParallelForDirective *D) { |
| 2922 | VisitOMPLoopDirective(D); |
| 2923 | } |
| 2924 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2925 | void EnqueueVisitor::VisitOMPTeamsDirective(const OMPTeamsDirective *D) { |
| 2926 | VisitOMPExecutableDirective(D); |
| 2927 | } |
| 2928 | |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2929 | void EnqueueVisitor::VisitOMPCancellationPointDirective( |
| 2930 | const OMPCancellationPointDirective *D) { |
| 2931 | VisitOMPExecutableDirective(D); |
| 2932 | } |
| 2933 | |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2934 | void EnqueueVisitor::VisitOMPCancelDirective(const OMPCancelDirective *D) { |
| 2935 | VisitOMPExecutableDirective(D); |
| 2936 | } |
| 2937 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 2938 | void EnqueueVisitor::VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D) { |
| 2939 | VisitOMPLoopDirective(D); |
| 2940 | } |
| 2941 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 2942 | void EnqueueVisitor::VisitOMPTaskLoopSimdDirective( |
| 2943 | const OMPTaskLoopSimdDirective *D) { |
| 2944 | VisitOMPLoopDirective(D); |
| 2945 | } |
| 2946 | |
Alexey Bataev | 60e51c4 | 2019-10-10 20:13:02 +0000 | [diff] [blame] | 2947 | void EnqueueVisitor::VisitOMPMasterTaskLoopDirective( |
| 2948 | const OMPMasterTaskLoopDirective *D) { |
| 2949 | VisitOMPLoopDirective(D); |
| 2950 | } |
| 2951 | |
Alexey Bataev | b8552ab | 2019-10-18 16:47:35 +0000 | [diff] [blame] | 2952 | void EnqueueVisitor::VisitOMPMasterTaskLoopSimdDirective( |
| 2953 | const OMPMasterTaskLoopSimdDirective *D) { |
| 2954 | VisitOMPLoopDirective(D); |
| 2955 | } |
| 2956 | |
Alexey Bataev | 5bbcead | 2019-10-14 17:17:41 +0000 | [diff] [blame] | 2957 | void EnqueueVisitor::VisitOMPParallelMasterTaskLoopDirective( |
| 2958 | const OMPParallelMasterTaskLoopDirective *D) { |
| 2959 | VisitOMPLoopDirective(D); |
| 2960 | } |
| 2961 | |
Alexey Bataev | 14a388f | 2019-10-25 10:27:13 -0400 | [diff] [blame] | 2962 | void EnqueueVisitor::VisitOMPParallelMasterTaskLoopSimdDirective( |
| 2963 | const OMPParallelMasterTaskLoopSimdDirective *D) { |
| 2964 | VisitOMPLoopDirective(D); |
| 2965 | } |
| 2966 | |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2967 | void EnqueueVisitor::VisitOMPDistributeDirective( |
| 2968 | const OMPDistributeDirective *D) { |
| 2969 | VisitOMPLoopDirective(D); |
| 2970 | } |
| 2971 | |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2972 | void EnqueueVisitor::VisitOMPDistributeParallelForDirective( |
| 2973 | const OMPDistributeParallelForDirective *D) { |
| 2974 | VisitOMPLoopDirective(D); |
| 2975 | } |
| 2976 | |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2977 | void EnqueueVisitor::VisitOMPDistributeParallelForSimdDirective( |
| 2978 | const OMPDistributeParallelForSimdDirective *D) { |
| 2979 | VisitOMPLoopDirective(D); |
| 2980 | } |
| 2981 | |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2982 | void EnqueueVisitor::VisitOMPDistributeSimdDirective( |
| 2983 | const OMPDistributeSimdDirective *D) { |
| 2984 | VisitOMPLoopDirective(D); |
| 2985 | } |
| 2986 | |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 2987 | void EnqueueVisitor::VisitOMPTargetParallelForSimdDirective( |
| 2988 | const OMPTargetParallelForSimdDirective *D) { |
| 2989 | VisitOMPLoopDirective(D); |
| 2990 | } |
| 2991 | |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2992 | void EnqueueVisitor::VisitOMPTargetSimdDirective( |
| 2993 | const OMPTargetSimdDirective *D) { |
| 2994 | VisitOMPLoopDirective(D); |
| 2995 | } |
| 2996 | |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 2997 | void EnqueueVisitor::VisitOMPTeamsDistributeDirective( |
| 2998 | const OMPTeamsDistributeDirective *D) { |
| 2999 | VisitOMPLoopDirective(D); |
| 3000 | } |
| 3001 | |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 3002 | void EnqueueVisitor::VisitOMPTeamsDistributeSimdDirective( |
| 3003 | const OMPTeamsDistributeSimdDirective *D) { |
| 3004 | VisitOMPLoopDirective(D); |
| 3005 | } |
| 3006 | |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 3007 | void EnqueueVisitor::VisitOMPTeamsDistributeParallelForSimdDirective( |
| 3008 | const OMPTeamsDistributeParallelForSimdDirective *D) { |
| 3009 | VisitOMPLoopDirective(D); |
| 3010 | } |
| 3011 | |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 3012 | void EnqueueVisitor::VisitOMPTeamsDistributeParallelForDirective( |
| 3013 | const OMPTeamsDistributeParallelForDirective *D) { |
| 3014 | VisitOMPLoopDirective(D); |
| 3015 | } |
| 3016 | |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 3017 | void EnqueueVisitor::VisitOMPTargetTeamsDirective( |
| 3018 | const OMPTargetTeamsDirective *D) { |
| 3019 | VisitOMPExecutableDirective(D); |
| 3020 | } |
| 3021 | |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 3022 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeDirective( |
| 3023 | const OMPTargetTeamsDistributeDirective *D) { |
| 3024 | VisitOMPLoopDirective(D); |
| 3025 | } |
| 3026 | |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 3027 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeParallelForDirective( |
| 3028 | const OMPTargetTeamsDistributeParallelForDirective *D) { |
| 3029 | VisitOMPLoopDirective(D); |
| 3030 | } |
| 3031 | |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 3032 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeParallelForSimdDirective( |
| 3033 | const OMPTargetTeamsDistributeParallelForSimdDirective *D) { |
| 3034 | VisitOMPLoopDirective(D); |
| 3035 | } |
| 3036 | |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 3037 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeSimdDirective( |
| 3038 | const OMPTargetTeamsDistributeSimdDirective *D) { |
| 3039 | VisitOMPLoopDirective(D); |
| 3040 | } |
| 3041 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3042 | void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3043 | EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S); |
| 3044 | } |
| 3045 | |
| 3046 | bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { |
| 3047 | if (RegionOfInterest.isValid()) { |
| 3048 | SourceRange Range = getRawCursorExtent(C); |
| 3049 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 3050 | return false; |
| 3051 | } |
| 3052 | return true; |
| 3053 | } |
| 3054 | |
| 3055 | bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { |
| 3056 | while (!WL.empty()) { |
| 3057 | // Dequeue the worklist item. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 3058 | VisitorJob LI = WL.pop_back_val(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3059 | |
| 3060 | // Set the Parent field, then back to its old value once we're done. |
| 3061 | SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); |
| 3062 | |
| 3063 | switch (LI.getKind()) { |
| 3064 | case VisitorJob::DeclVisitKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3065 | const Decl *D = cast<DeclVisit>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3066 | if (!D) |
| 3067 | continue; |
| 3068 | |
| 3069 | // For now, perform default visitation for Decls. |
| 3070 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest, |
| 3071 | cast<DeclVisit>(&LI)->isFirst()))) |
| 3072 | return true; |
| 3073 | |
| 3074 | continue; |
| 3075 | } |
| 3076 | case VisitorJob::ExplicitTemplateArgsVisitKind: { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3077 | for (const TemplateArgumentLoc &Arg : |
| 3078 | *cast<ExplicitTemplateArgsVisit>(&LI)) { |
| 3079 | if (VisitTemplateArgumentLoc(Arg)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3080 | return true; |
| 3081 | } |
| 3082 | continue; |
| 3083 | } |
| 3084 | case VisitorJob::TypeLocVisitKind: { |
| 3085 | // Perform default visitation for TypeLocs. |
| 3086 | if (Visit(cast<TypeLocVisit>(&LI)->get())) |
| 3087 | return true; |
| 3088 | continue; |
| 3089 | } |
| 3090 | case VisitorJob::LabelRefVisitKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3091 | const LabelDecl *LS = cast<LabelRefVisit>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3092 | if (LabelStmt *stmt = LS->getStmt()) { |
| 3093 | if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(), |
| 3094 | TU))) { |
| 3095 | return true; |
| 3096 | } |
| 3097 | } |
| 3098 | continue; |
| 3099 | } |
| 3100 | |
| 3101 | case VisitorJob::NestedNameSpecifierLocVisitKind: { |
| 3102 | NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI); |
| 3103 | if (VisitNestedNameSpecifierLoc(V->get())) |
| 3104 | return true; |
| 3105 | continue; |
| 3106 | } |
| 3107 | |
| 3108 | case VisitorJob::DeclarationNameInfoVisitKind: { |
| 3109 | if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI) |
| 3110 | ->get())) |
| 3111 | return true; |
| 3112 | continue; |
| 3113 | } |
| 3114 | case VisitorJob::MemberRefVisitKind: { |
| 3115 | MemberRefVisit *V = cast<MemberRefVisit>(&LI); |
| 3116 | if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU))) |
| 3117 | return true; |
| 3118 | continue; |
| 3119 | } |
| 3120 | case VisitorJob::StmtVisitKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3121 | const Stmt *S = cast<StmtVisit>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3122 | if (!S) |
| 3123 | continue; |
| 3124 | |
| 3125 | // Update the current cursor. |
| 3126 | CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest); |
| 3127 | if (!IsInRegionOfInterest(Cursor)) |
| 3128 | continue; |
| 3129 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 3130 | case CXChildVisit_Break: return true; |
| 3131 | case CXChildVisit_Continue: break; |
| 3132 | case CXChildVisit_Recurse: |
| 3133 | if (PostChildrenVisitor) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3134 | WL.push_back(PostChildrenVisit(nullptr, Cursor)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3135 | EnqueueWorkList(WL, S); |
| 3136 | break; |
| 3137 | } |
| 3138 | continue; |
| 3139 | } |
| 3140 | case VisitorJob::MemberExprPartsKind: { |
| 3141 | // Handle the other pieces in the MemberExpr besides the base. |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3142 | const MemberExpr *M = cast<MemberExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3143 | |
| 3144 | // Visit the nested-name-specifier |
| 3145 | if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc()) |
| 3146 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 3147 | return true; |
| 3148 | |
| 3149 | // Visit the declaration name. |
| 3150 | if (VisitDeclarationNameInfo(M->getMemberNameInfo())) |
| 3151 | return true; |
| 3152 | |
| 3153 | // Visit the explicitly-specified template arguments, if any. |
| 3154 | if (M->hasExplicitTemplateArgs()) { |
| 3155 | for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), |
| 3156 | *ArgEnd = Arg + M->getNumTemplateArgs(); |
| 3157 | Arg != ArgEnd; ++Arg) { |
| 3158 | if (VisitTemplateArgumentLoc(*Arg)) |
| 3159 | return true; |
| 3160 | } |
| 3161 | } |
| 3162 | continue; |
| 3163 | } |
| 3164 | case VisitorJob::DeclRefExprPartsKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3165 | const DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3166 | // Visit nested-name-specifier, if present. |
| 3167 | if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc()) |
| 3168 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 3169 | return true; |
| 3170 | // Visit declaration name. |
| 3171 | if (VisitDeclarationNameInfo(DR->getNameInfo())) |
| 3172 | return true; |
| 3173 | continue; |
| 3174 | } |
| 3175 | case VisitorJob::OverloadExprPartsKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3176 | const OverloadExpr *O = cast<OverloadExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3177 | // Visit the nested-name-specifier. |
| 3178 | if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc()) |
| 3179 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 3180 | return true; |
| 3181 | // Visit the declaration name. |
| 3182 | if (VisitDeclarationNameInfo(O->getNameInfo())) |
| 3183 | return true; |
| 3184 | // Visit the overloaded declaration reference. |
| 3185 | if (Visit(MakeCursorOverloadedDeclRef(O, TU))) |
| 3186 | return true; |
| 3187 | continue; |
| 3188 | } |
| 3189 | case VisitorJob::SizeOfPackExprPartsKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3190 | const SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3191 | NamedDecl *Pack = E->getPack(); |
| 3192 | if (isa<TemplateTypeParmDecl>(Pack)) { |
| 3193 | if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack), |
| 3194 | E->getPackLoc(), TU))) |
| 3195 | return true; |
| 3196 | |
| 3197 | continue; |
| 3198 | } |
| 3199 | |
| 3200 | if (isa<TemplateTemplateParmDecl>(Pack)) { |
| 3201 | if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack), |
| 3202 | E->getPackLoc(), TU))) |
| 3203 | return true; |
| 3204 | |
| 3205 | continue; |
| 3206 | } |
| 3207 | |
| 3208 | // Non-type template parameter packs and function parameter packs are |
| 3209 | // treated like DeclRefExpr cursors. |
| 3210 | continue; |
| 3211 | } |
| 3212 | |
| 3213 | case VisitorJob::LambdaExprPartsKind: { |
Nikolai Kosjar | 2eebf4d9 | 2019-05-21 09:21:35 +0000 | [diff] [blame] | 3214 | // Visit non-init captures. |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3215 | const LambdaExpr *E = cast<LambdaExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3216 | for (LambdaExpr::capture_iterator C = E->explicit_capture_begin(), |
| 3217 | CEnd = E->explicit_capture_end(); |
| 3218 | C != CEnd; ++C) { |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 3219 | if (!C->capturesVariable()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3220 | continue; |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 3221 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3222 | if (Visit(MakeCursorVariableRef(C->getCapturedVar(), |
| 3223 | C->getLocation(), |
| 3224 | TU))) |
| 3225 | return true; |
| 3226 | } |
Nikolai Kosjar | 2eebf4d9 | 2019-05-21 09:21:35 +0000 | [diff] [blame] | 3227 | // Visit init captures |
| 3228 | for (auto InitExpr : E->capture_inits()) { |
| 3229 | if (Visit(InitExpr)) |
| 3230 | return true; |
| 3231 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3232 | |
Haojian Wu | ef87c26 | 2018-12-18 15:29:12 +0000 | [diff] [blame] | 3233 | TypeLoc TL = E->getCallOperator()->getTypeSourceInfo()->getTypeLoc(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3234 | // Visit parameters and return type, if present. |
Haojian Wu | ef87c26 | 2018-12-18 15:29:12 +0000 | [diff] [blame] | 3235 | if (FunctionTypeLoc Proto = TL.getAs<FunctionProtoTypeLoc>()) { |
| 3236 | if (E->hasExplicitParameters()) { |
| 3237 | // Visit parameters. |
| 3238 | for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) |
| 3239 | if (Visit(MakeCXCursor(Proto.getParam(I), TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3240 | return true; |
Haojian Wu | ef87c26 | 2018-12-18 15:29:12 +0000 | [diff] [blame] | 3241 | } |
| 3242 | if (E->hasExplicitResultType()) { |
| 3243 | // Visit result type. |
| 3244 | if (Visit(Proto.getReturnLoc())) |
| 3245 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3246 | } |
| 3247 | } |
| 3248 | break; |
| 3249 | } |
| 3250 | |
| 3251 | case VisitorJob::PostChildrenVisitKind: |
| 3252 | if (PostChildrenVisitor(Parent, ClientData)) |
| 3253 | return true; |
| 3254 | break; |
| 3255 | } |
| 3256 | } |
| 3257 | return false; |
| 3258 | } |
| 3259 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3260 | bool CursorVisitor::Visit(const Stmt *S) { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3261 | VisitorWorkList *WL = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3262 | if (!WorkListFreeList.empty()) { |
| 3263 | WL = WorkListFreeList.back(); |
| 3264 | WL->clear(); |
| 3265 | WorkListFreeList.pop_back(); |
| 3266 | } |
| 3267 | else { |
| 3268 | WL = new VisitorWorkList(); |
| 3269 | WorkListCache.push_back(WL); |
| 3270 | } |
| 3271 | EnqueueWorkList(*WL, S); |
| 3272 | bool result = RunVisitorWorkList(*WL); |
| 3273 | WorkListFreeList.push_back(WL); |
| 3274 | return result; |
| 3275 | } |
| 3276 | |
| 3277 | namespace { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3278 | typedef SmallVector<SourceRange, 4> RefNamePieces; |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3279 | RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr, |
| 3280 | const DeclarationNameInfo &NI, SourceRange QLoc, |
| 3281 | const SourceRange *TemplateArgsLoc = nullptr) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3282 | const bool WantQualifier = NameFlags & CXNameRange_WantQualifier; |
| 3283 | const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs; |
| 3284 | const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece; |
| 3285 | |
| 3286 | const DeclarationName::NameKind Kind = NI.getName().getNameKind(); |
| 3287 | |
| 3288 | RefNamePieces Pieces; |
| 3289 | |
| 3290 | if (WantQualifier && QLoc.isValid()) |
| 3291 | Pieces.push_back(QLoc); |
| 3292 | |
| 3293 | if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr) |
| 3294 | Pieces.push_back(NI.getLoc()); |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3295 | |
| 3296 | if (WantTemplateArgs && TemplateArgsLoc && TemplateArgsLoc->isValid()) |
| 3297 | Pieces.push_back(*TemplateArgsLoc); |
| 3298 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3299 | if (Kind == DeclarationName::CXXOperatorName) { |
| 3300 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 3301 | NI.getInfo().CXXOperatorName.BeginOpNameLoc)); |
| 3302 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 3303 | NI.getInfo().CXXOperatorName.EndOpNameLoc)); |
| 3304 | } |
| 3305 | |
| 3306 | if (WantSinglePiece) { |
| 3307 | SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd()); |
| 3308 | Pieces.clear(); |
| 3309 | Pieces.push_back(R); |
| 3310 | } |
| 3311 | |
| 3312 | return Pieces; |
| 3313 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3314 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3315 | |
| 3316 | //===----------------------------------------------------------------------===// |
| 3317 | // Misc. API hooks. |
| 3318 | //===----------------------------------------------------------------------===// |
| 3319 | |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 3320 | namespace { |
| 3321 | struct RegisterFatalErrorHandler { |
| 3322 | RegisterFatalErrorHandler() { |
Jan Korous | f7d2376 | 2019-09-12 22:55:55 +0000 | [diff] [blame] | 3323 | clang_install_aborting_llvm_fatal_error_handler(); |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 3324 | } |
| 3325 | }; |
| 3326 | } |
| 3327 | |
| 3328 | static llvm::ManagedStatic<RegisterFatalErrorHandler> RegisterFatalErrorHandlerOnce; |
| 3329 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3330 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 3331 | int displayDiagnostics) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3332 | // We use crash recovery to make some of our APIs more reliable, implicitly |
| 3333 | // enable it. |
Argyrios Kyrtzidis | 3701f54 | 2013-11-27 08:58:09 +0000 | [diff] [blame] | 3334 | if (!getenv("LIBCLANG_DISABLE_CRASH_RECOVERY")) |
| 3335 | llvm::CrashRecoveryContext::Enable(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3336 | |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 3337 | // Look through the managed static to trigger construction of the managed |
| 3338 | // static which registers our fatal error handler. This ensures it is only |
| 3339 | // registered once. |
| 3340 | (void)*RegisterFatalErrorHandlerOnce; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3341 | |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 3342 | // Initialize targets for clang module support. |
| 3343 | llvm::InitializeAllTargets(); |
| 3344 | llvm::InitializeAllTargetMCs(); |
| 3345 | llvm::InitializeAllAsmPrinters(); |
| 3346 | llvm::InitializeAllAsmParsers(); |
| 3347 | |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 3348 | CIndexer *CIdxr = new CIndexer(); |
| 3349 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3350 | if (excludeDeclarationsFromPCH) |
| 3351 | CIdxr->setOnlyLocalDecls(); |
| 3352 | if (displayDiagnostics) |
| 3353 | CIdxr->setDisplayDiagnostics(); |
| 3354 | |
| 3355 | if (getenv("LIBCLANG_BGPRIO_INDEX")) |
| 3356 | CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() | |
| 3357 | CXGlobalOpt_ThreadBackgroundPriorityForIndexing); |
| 3358 | if (getenv("LIBCLANG_BGPRIO_EDIT")) |
| 3359 | CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() | |
| 3360 | CXGlobalOpt_ThreadBackgroundPriorityForEditing); |
| 3361 | |
| 3362 | return CIdxr; |
| 3363 | } |
| 3364 | |
| 3365 | void clang_disposeIndex(CXIndex CIdx) { |
| 3366 | if (CIdx) |
| 3367 | delete static_cast<CIndexer *>(CIdx); |
| 3368 | } |
| 3369 | |
| 3370 | void clang_CXIndex_setGlobalOptions(CXIndex CIdx, unsigned options) { |
| 3371 | if (CIdx) |
| 3372 | static_cast<CIndexer *>(CIdx)->setCXGlobalOptFlags(options); |
| 3373 | } |
| 3374 | |
| 3375 | unsigned clang_CXIndex_getGlobalOptions(CXIndex CIdx) { |
| 3376 | if (CIdx) |
| 3377 | return static_cast<CIndexer *>(CIdx)->getCXGlobalOptFlags(); |
| 3378 | return 0; |
| 3379 | } |
| 3380 | |
Alex Lorenz | 0861579 | 2017-12-04 21:56:36 +0000 | [diff] [blame] | 3381 | void clang_CXIndex_setInvocationEmissionPathOption(CXIndex CIdx, |
| 3382 | const char *Path) { |
| 3383 | if (CIdx) |
| 3384 | static_cast<CIndexer *>(CIdx)->setInvocationEmissionPath(Path ? Path : ""); |
| 3385 | } |
| 3386 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3387 | void clang_toggleCrashRecovery(unsigned isEnabled) { |
| 3388 | if (isEnabled) |
| 3389 | llvm::CrashRecoveryContext::Enable(); |
| 3390 | else |
| 3391 | llvm::CrashRecoveryContext::Disable(); |
| 3392 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3393 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3394 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
| 3395 | const char *ast_filename) { |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3396 | CXTranslationUnit TU; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3397 | enum CXErrorCode Result = |
| 3398 | clang_createTranslationUnit2(CIdx, ast_filename, &TU); |
Reid Kleckner | fd48fc6 | 2014-02-12 23:56:20 +0000 | [diff] [blame] | 3399 | (void)Result; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3400 | assert((TU && Result == CXError_Success) || |
| 3401 | (!TU && Result != CXError_Success)); |
| 3402 | return TU; |
| 3403 | } |
| 3404 | |
| 3405 | enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx, |
| 3406 | const char *ast_filename, |
| 3407 | CXTranslationUnit *out_TU) { |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3408 | if (out_TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3409 | *out_TU = nullptr; |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3410 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3411 | if (!CIdx || !ast_filename || !out_TU) |
| 3412 | return CXError_InvalidArguments; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3413 | |
Argyrios Kyrtzidis | 2702101 | 2013-05-24 22:24:07 +0000 | [diff] [blame] | 3414 | LOG_FUNC_SECTION { |
| 3415 | *Log << ast_filename; |
| 3416 | } |
| 3417 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3418 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 3419 | FileSystemOptions FileSystemOpts; |
| 3420 | |
Justin Bogner | d512c1e | 2014-10-15 00:33:06 +0000 | [diff] [blame] | 3421 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags = |
| 3422 | CompilerInstance::createDiagnostics(new DiagnosticOptions()); |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 3423 | std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile( |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 3424 | ast_filename, CXXIdx->getPCHContainerOperations()->getRawReader(), |
| 3425 | ASTUnit::LoadEverything, Diags, |
Adrian Prantl | 6b21ab2 | 2015-08-27 19:46:20 +0000 | [diff] [blame] | 3426 | FileSystemOpts, /*UseDebugInfo=*/false, |
| 3427 | CXXIdx->getOnlyLocalDecls(), None, |
Nikolai Kosjar | 8edd8da | 2019-06-11 14:14:24 +0000 | [diff] [blame] | 3428 | CaptureDiagsKind::All, |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 3429 | /*AllowPCHWithCompilerErrors=*/true, |
| 3430 | /*UserFilesAreVolatile=*/true); |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 3431 | *out_TU = MakeCXTranslationUnit(CXXIdx, std::move(AU)); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3432 | return *out_TU ? CXError_Success : CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3433 | } |
| 3434 | |
| 3435 | unsigned clang_defaultEditingTranslationUnitOptions() { |
| 3436 | return CXTranslationUnit_PrecompiledPreamble | |
| 3437 | CXTranslationUnit_CacheCompletionResults; |
| 3438 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3439 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3440 | CXTranslationUnit |
| 3441 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 3442 | const char *source_filename, |
| 3443 | int num_command_line_args, |
| 3444 | const char * const *command_line_args, |
| 3445 | unsigned num_unsaved_files, |
| 3446 | struct CXUnsavedFile *unsaved_files) { |
| 3447 | unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord; |
| 3448 | return clang_parseTranslationUnit(CIdx, source_filename, |
| 3449 | command_line_args, num_command_line_args, |
| 3450 | unsaved_files, num_unsaved_files, |
| 3451 | Options); |
| 3452 | } |
| 3453 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3454 | static CXErrorCode |
| 3455 | clang_parseTranslationUnit_Impl(CXIndex CIdx, const char *source_filename, |
| 3456 | const char *const *command_line_args, |
| 3457 | int num_command_line_args, |
| 3458 | ArrayRef<CXUnsavedFile> unsaved_files, |
| 3459 | unsigned options, CXTranslationUnit *out_TU) { |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3460 | // Set up the initial return values. |
| 3461 | if (out_TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3462 | *out_TU = nullptr; |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3463 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3464 | // Check arguments. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3465 | if (!CIdx || !out_TU) |
| 3466 | return CXError_InvalidArguments; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3467 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3468 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 3469 | |
| 3470 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
| 3471 | setThreadBackgroundPriority(); |
| 3472 | |
| 3473 | bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3474 | bool CreatePreambleOnFirstParse = |
| 3475 | options & CXTranslationUnit_CreatePreambleOnFirstParse; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3476 | // FIXME: Add a flag for modules. |
| 3477 | TranslationUnitKind TUKind |
Argyrios Kyrtzidis | 735e92c | 2017-06-09 01:20:48 +0000 | [diff] [blame] | 3478 | = (options & (CXTranslationUnit_Incomplete | |
| 3479 | CXTranslationUnit_SingleFileParse))? TU_Prefix : TU_Complete; |
Alp Toker | 8c8a875 | 2013-12-03 06:53:35 +0000 | [diff] [blame] | 3480 | bool CacheCodeCompletionResults |
Ivan Donchevskii | f70d28b | 2018-05-17 09:15:22 +0000 | [diff] [blame] | 3481 | = options & CXTranslationUnit_CacheCompletionResults; |
| 3482 | bool IncludeBriefCommentsInCodeCompletion |
| 3483 | = options & CXTranslationUnit_IncludeBriefCommentsInCodeCompletion; |
Ivan Donchevskii | f70d28b | 2018-05-17 09:15:22 +0000 | [diff] [blame] | 3484 | bool SingleFileParse = options & CXTranslationUnit_SingleFileParse; |
| 3485 | bool ForSerialization = options & CXTranslationUnit_ForSerialization; |
Evgeny Mankov | 2ed2e62 | 2019-08-27 22:15:32 +0000 | [diff] [blame] | 3486 | bool RetainExcludedCB = options & |
| 3487 | CXTranslationUnit_RetainExcludedConditionalBlocks; |
Ivan Donchevskii | 6e89528 | 2018-05-17 09:24:37 +0000 | [diff] [blame] | 3488 | SkipFunctionBodiesScope SkipFunctionBodies = SkipFunctionBodiesScope::None; |
| 3489 | if (options & CXTranslationUnit_SkipFunctionBodies) { |
| 3490 | SkipFunctionBodies = |
| 3491 | (options & CXTranslationUnit_LimitSkipFunctionBodiesToPreamble) |
| 3492 | ? SkipFunctionBodiesScope::Preamble |
| 3493 | : SkipFunctionBodiesScope::PreambleAndMainFile; |
| 3494 | } |
Ivan Donchevskii | f70d28b | 2018-05-17 09:15:22 +0000 | [diff] [blame] | 3495 | |
| 3496 | // Configure the diagnostics. |
| 3497 | IntrusiveRefCntPtr<DiagnosticsEngine> |
Sean Silva | f1b49e2 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 3498 | Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3499 | |
Manuel Klimek | 016c024 | 2016-03-01 10:56:19 +0000 | [diff] [blame] | 3500 | if (options & CXTranslationUnit_KeepGoing) |
Ivan Donchevskii | 878271b | 2019-03-07 10:13:50 +0000 | [diff] [blame] | 3501 | Diags->setFatalsAsError(true); |
Manuel Klimek | 016c024 | 2016-03-01 10:56:19 +0000 | [diff] [blame] | 3502 | |
Nikolai Kosjar | 8edd8da | 2019-06-11 14:14:24 +0000 | [diff] [blame] | 3503 | CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::All; |
| 3504 | if (options & CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles) |
| 3505 | CaptureDiagnostics = CaptureDiagsKind::AllWithoutNonErrorsFromIncludes; |
| 3506 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3507 | // Recover resources if we crash before exiting this function. |
| 3508 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 3509 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 3510 | DiagCleanup(Diags.get()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3511 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3512 | std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles( |
| 3513 | new std::vector<ASTUnit::RemappedFile>()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3514 | |
| 3515 | // Recover resources if we crash before exiting this function. |
| 3516 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 3517 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 3518 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3519 | for (auto &UF : unsaved_files) { |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3520 | std::unique_ptr<llvm::MemoryBuffer> MB = |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3521 | llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3522 | RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3523 | } |
| 3524 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3525 | std::unique_ptr<std::vector<const char *>> Args( |
| 3526 | new std::vector<const char *>()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3527 | |
| 3528 | // Recover resources if we crash before exiting this method. |
| 3529 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > |
| 3530 | ArgsCleanup(Args.get()); |
| 3531 | |
| 3532 | // Since the Clang C library is primarily used by batch tools dealing with |
| 3533 | // (often very broken) source code, where spell-checking can have a |
| 3534 | // significant negative impact on performance (particularly when |
| 3535 | // precompiled headers are involved), we disable it by default. |
| 3536 | // Only do this if we haven't found a spell-checking-related argument. |
| 3537 | bool FoundSpellCheckingArgument = false; |
| 3538 | for (int I = 0; I != num_command_line_args; ++I) { |
| 3539 | if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || |
| 3540 | strcmp(command_line_args[I], "-fspell-checking") == 0) { |
| 3541 | FoundSpellCheckingArgument = true; |
| 3542 | break; |
| 3543 | } |
| 3544 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3545 | Args->insert(Args->end(), command_line_args, |
| 3546 | command_line_args + num_command_line_args); |
| 3547 | |
Benjamin Kramer | c02670e | 2015-11-18 16:14:27 +0000 | [diff] [blame] | 3548 | if (!FoundSpellCheckingArgument) |
| 3549 | Args->insert(Args->begin() + 1, "-fno-spell-checking"); |
| 3550 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3551 | // The 'source_filename' argument is optional. If the caller does not |
| 3552 | // specify it then it is assumed that the source file is specified |
| 3553 | // in the actual argument list. |
| 3554 | // Put the source file after command_line_args otherwise if '-x' flag is |
| 3555 | // present it will be unused. |
| 3556 | if (source_filename) |
| 3557 | Args->push_back(source_filename); |
| 3558 | |
| 3559 | // Do we need the detailed preprocessing record? |
| 3560 | if (options & CXTranslationUnit_DetailedPreprocessingRecord) { |
| 3561 | Args->push_back("-Xclang"); |
| 3562 | Args->push_back("-detailed-preprocessing-record"); |
| 3563 | } |
Alex Lorenz | cb00640 | 2017-04-27 13:47:03 +0000 | [diff] [blame] | 3564 | |
| 3565 | // Suppress any editor placeholder diagnostics. |
| 3566 | Args->push_back("-fallow-editor-placeholders"); |
| 3567 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3568 | unsigned NumErrors = Diags->getClient()->getNumErrors(); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3569 | std::unique_ptr<ASTUnit> ErrUnit; |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3570 | // Unless the user specified that they want the preamble on the first parse |
| 3571 | // set it up to be created on the first reparse. This makes the first parse |
| 3572 | // faster, trading for a slower (first) reparse. |
| 3573 | unsigned PrecompilePreambleAfterNParses = |
| 3574 | !PrecompilePreamble ? 0 : 2 - CreatePreambleOnFirstParse; |
Alex Lorenz | 0861579 | 2017-12-04 21:56:36 +0000 | [diff] [blame] | 3575 | |
Alex Lorenz | 0861579 | 2017-12-04 21:56:36 +0000 | [diff] [blame] | 3576 | LibclangInvocationReporter InvocationReporter( |
| 3577 | *CXXIdx, LibclangInvocationReporter::OperationKind::ParseOperation, |
Alex Lorenz | 690f0e2 | 2017-12-07 20:37:50 +0000 | [diff] [blame] | 3578 | options, llvm::makeArrayRef(*Args), /*InvocationArgs=*/None, |
| 3579 | unsaved_files); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3580 | std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCommandLine( |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 3581 | Args->data(), Args->data() + Args->size(), |
| 3582 | CXXIdx->getPCHContainerOperations(), Diags, |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3583 | CXXIdx->getClangResourcesPath(), CXXIdx->getOnlyLocalDecls(), |
Nikolai Kosjar | 8edd8da | 2019-06-11 14:14:24 +0000 | [diff] [blame] | 3584 | CaptureDiagnostics, *RemappedFiles.get(), |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3585 | /*RemappedFilesKeepOriginalName=*/true, PrecompilePreambleAfterNParses, |
| 3586 | TUKind, CacheCodeCompletionResults, IncludeBriefCommentsInCodeCompletion, |
Argyrios Kyrtzidis | 735e92c | 2017-06-09 01:20:48 +0000 | [diff] [blame] | 3587 | /*AllowPCHWithCompilerErrors=*/true, SkipFunctionBodies, SingleFileParse, |
Evgeny Mankov | 2ed2e62 | 2019-08-27 22:15:32 +0000 | [diff] [blame] | 3588 | /*UserFilesAreVolatile=*/true, ForSerialization, RetainExcludedCB, |
Argyrios Kyrtzidis | a3e2ff1 | 2015-11-20 03:36:21 +0000 | [diff] [blame] | 3589 | CXXIdx->getPCHContainerOperations()->getRawReader().getFormat(), |
| 3590 | &ErrUnit)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3591 | |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3592 | // Early failures in LoadFromCommandLine may return with ErrUnit unset. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3593 | if (!Unit && !ErrUnit) |
| 3594 | return CXError_ASTReadError; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3595 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3596 | if (NumErrors != Diags->getClient()->getNumErrors()) { |
| 3597 | // Make sure to check that 'Unit' is non-NULL. |
| 3598 | if (CXXIdx->getDisplayDiagnostics()) |
| 3599 | printDiagsToStderr(Unit ? Unit.get() : ErrUnit.get()); |
| 3600 | } |
| 3601 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3602 | if (isASTReadError(Unit ? Unit.get() : ErrUnit.get())) |
| 3603 | return CXError_ASTReadError; |
| 3604 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 3605 | *out_TU = MakeCXTranslationUnit(CXXIdx, std::move(Unit)); |
Alex Lorenz | 690f0e2 | 2017-12-07 20:37:50 +0000 | [diff] [blame] | 3606 | if (CXTranslationUnitImpl *TU = *out_TU) { |
| 3607 | TU->ParsingOptions = options; |
| 3608 | TU->Arguments.reserve(Args->size()); |
| 3609 | for (const char *Arg : *Args) |
| 3610 | TU->Arguments.push_back(Arg); |
| 3611 | return CXError_Success; |
| 3612 | } |
| 3613 | return CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3614 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3615 | |
| 3616 | CXTranslationUnit |
| 3617 | clang_parseTranslationUnit(CXIndex CIdx, |
| 3618 | const char *source_filename, |
| 3619 | const char *const *command_line_args, |
| 3620 | int num_command_line_args, |
| 3621 | struct CXUnsavedFile *unsaved_files, |
| 3622 | unsigned num_unsaved_files, |
| 3623 | unsigned options) { |
| 3624 | CXTranslationUnit TU; |
| 3625 | enum CXErrorCode Result = clang_parseTranslationUnit2( |
| 3626 | CIdx, source_filename, command_line_args, num_command_line_args, |
| 3627 | unsaved_files, num_unsaved_files, options, &TU); |
Reid Kleckner | 6eaf05a | 2014-02-13 01:19:59 +0000 | [diff] [blame] | 3628 | (void)Result; |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3629 | assert((TU && Result == CXError_Success) || |
| 3630 | (!TU && Result != CXError_Success)); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3631 | return TU; |
| 3632 | } |
| 3633 | |
| 3634 | enum CXErrorCode clang_parseTranslationUnit2( |
Benjamin Kramer | c02670e | 2015-11-18 16:14:27 +0000 | [diff] [blame] | 3635 | CXIndex CIdx, const char *source_filename, |
| 3636 | const char *const *command_line_args, int num_command_line_args, |
| 3637 | struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files, |
| 3638 | unsigned options, CXTranslationUnit *out_TU) { |
Alexandre Ganea | 471d060 | 2019-11-29 10:52:13 -0500 | [diff] [blame] | 3639 | noteBottomOfStack(); |
Benjamin Kramer | c02670e | 2015-11-18 16:14:27 +0000 | [diff] [blame] | 3640 | SmallVector<const char *, 4> Args; |
| 3641 | Args.push_back("clang"); |
| 3642 | Args.append(command_line_args, command_line_args + num_command_line_args); |
| 3643 | return clang_parseTranslationUnit2FullArgv( |
| 3644 | CIdx, source_filename, Args.data(), Args.size(), unsaved_files, |
| 3645 | num_unsaved_files, options, out_TU); |
| 3646 | } |
| 3647 | |
| 3648 | enum CXErrorCode clang_parseTranslationUnit2FullArgv( |
| 3649 | CXIndex CIdx, const char *source_filename, |
| 3650 | const char *const *command_line_args, int num_command_line_args, |
| 3651 | struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files, |
| 3652 | unsigned options, CXTranslationUnit *out_TU) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 3653 | LOG_FUNC_SECTION { |
| 3654 | *Log << source_filename << ": "; |
| 3655 | for (int i = 0; i != num_command_line_args; ++i) |
| 3656 | *Log << command_line_args[i] << " "; |
| 3657 | } |
| 3658 | |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3659 | if (num_unsaved_files && !unsaved_files) |
| 3660 | return CXError_InvalidArguments; |
| 3661 | |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 3662 | CXErrorCode result = CXError_Failure; |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3663 | auto ParseTranslationUnitImpl = [=, &result] { |
Alexandre Ganea | 471d060 | 2019-11-29 10:52:13 -0500 | [diff] [blame] | 3664 | noteBottomOfStack(); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3665 | result = clang_parseTranslationUnit_Impl( |
| 3666 | CIdx, source_filename, command_line_args, num_command_line_args, |
| 3667 | llvm::makeArrayRef(unsaved_files, num_unsaved_files), options, out_TU); |
| 3668 | }; |
Erik Verbruggen | 284848d | 2017-08-29 09:08:02 +0000 | [diff] [blame] | 3669 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3670 | llvm::CrashRecoveryContext CRC; |
| 3671 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3672 | if (!RunSafely(CRC, ParseTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3673 | fprintf(stderr, "libclang: crash detected during parsing: {\n"); |
| 3674 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 3675 | fprintf(stderr, " 'command_line_args' : ["); |
| 3676 | for (int i = 0; i != num_command_line_args; ++i) { |
| 3677 | if (i) |
| 3678 | fprintf(stderr, ", "); |
| 3679 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 3680 | } |
| 3681 | fprintf(stderr, "],\n"); |
| 3682 | fprintf(stderr, " 'unsaved_files' : ["); |
| 3683 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 3684 | if (i) |
| 3685 | fprintf(stderr, ", "); |
| 3686 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 3687 | unsaved_files[i].Length); |
| 3688 | } |
| 3689 | fprintf(stderr, "],\n"); |
| 3690 | fprintf(stderr, " 'options' : %d,\n", options); |
| 3691 | fprintf(stderr, "}\n"); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3692 | |
| 3693 | return CXError_Crashed; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3694 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3695 | if (CXTranslationUnit *TU = out_TU) |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3696 | PrintLibclangResourceUsage(*TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3697 | } |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 3698 | |
| 3699 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3700 | } |
| 3701 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3702 | CXString clang_Type_getObjCEncoding(CXType CT) { |
| 3703 | CXTranslationUnit tu = static_cast<CXTranslationUnit>(CT.data[1]); |
| 3704 | ASTContext &Ctx = getASTUnit(tu)->getASTContext(); |
| 3705 | std::string encoding; |
| 3706 | Ctx.getObjCEncodingForType(QualType::getFromOpaquePtr(CT.data[0]), |
| 3707 | encoding); |
| 3708 | |
| 3709 | return cxstring::createDup(encoding); |
| 3710 | } |
| 3711 | |
| 3712 | static const IdentifierInfo *getMacroIdentifier(CXCursor C) { |
| 3713 | if (C.kind == CXCursor_MacroDefinition) { |
| 3714 | if (const MacroDefinitionRecord *MDR = getCursorMacroDefinition(C)) |
| 3715 | return MDR->getName(); |
| 3716 | } else if (C.kind == CXCursor_MacroExpansion) { |
| 3717 | MacroExpansionCursor ME = getCursorMacroExpansion(C); |
| 3718 | return ME.getName(); |
| 3719 | } |
| 3720 | return nullptr; |
| 3721 | } |
| 3722 | |
| 3723 | unsigned clang_Cursor_isMacroFunctionLike(CXCursor C) { |
| 3724 | const IdentifierInfo *II = getMacroIdentifier(C); |
| 3725 | if (!II) { |
| 3726 | return false; |
| 3727 | } |
| 3728 | ASTUnit *ASTU = getCursorASTUnit(C); |
| 3729 | Preprocessor &PP = ASTU->getPreprocessor(); |
| 3730 | if (const MacroInfo *MI = PP.getMacroInfo(II)) |
| 3731 | return MI->isFunctionLike(); |
| 3732 | return false; |
| 3733 | } |
| 3734 | |
| 3735 | unsigned clang_Cursor_isMacroBuiltin(CXCursor C) { |
| 3736 | const IdentifierInfo *II = getMacroIdentifier(C); |
| 3737 | if (!II) { |
| 3738 | return false; |
| 3739 | } |
| 3740 | ASTUnit *ASTU = getCursorASTUnit(C); |
| 3741 | Preprocessor &PP = ASTU->getPreprocessor(); |
| 3742 | if (const MacroInfo *MI = PP.getMacroInfo(II)) |
| 3743 | return MI->isBuiltinMacro(); |
| 3744 | return false; |
| 3745 | } |
| 3746 | |
| 3747 | unsigned clang_Cursor_isFunctionInlined(CXCursor C) { |
| 3748 | const Decl *D = getCursorDecl(C); |
| 3749 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 3750 | if (!FD) { |
| 3751 | return false; |
| 3752 | } |
| 3753 | return FD->isInlined(); |
| 3754 | } |
| 3755 | |
| 3756 | static StringLiteral* getCFSTR_value(CallExpr *callExpr) { |
| 3757 | if (callExpr->getNumArgs() != 1) { |
| 3758 | return nullptr; |
| 3759 | } |
| 3760 | |
| 3761 | StringLiteral *S = nullptr; |
| 3762 | auto *arg = callExpr->getArg(0); |
| 3763 | if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) { |
| 3764 | ImplicitCastExpr *I = static_cast<ImplicitCastExpr *>(arg); |
| 3765 | auto *subExpr = I->getSubExprAsWritten(); |
| 3766 | |
| 3767 | if(subExpr->getStmtClass() != Stmt::StringLiteralClass){ |
| 3768 | return nullptr; |
| 3769 | } |
| 3770 | |
| 3771 | S = static_cast<StringLiteral *>(I->getSubExprAsWritten()); |
| 3772 | } else if (arg->getStmtClass() == Stmt::StringLiteralClass) { |
| 3773 | S = static_cast<StringLiteral *>(callExpr->getArg(0)); |
| 3774 | } else { |
| 3775 | return nullptr; |
| 3776 | } |
| 3777 | return S; |
| 3778 | } |
| 3779 | |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3780 | struct ExprEvalResult { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3781 | CXEvalResultKind EvalType; |
| 3782 | union { |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3783 | unsigned long long unsignedVal; |
| 3784 | long long intVal; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3785 | double floatVal; |
| 3786 | char *stringVal; |
| 3787 | } EvalData; |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3788 | bool IsUnsignedInt; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3789 | ~ExprEvalResult() { |
| 3790 | if (EvalType != CXEval_UnExposed && EvalType != CXEval_Float && |
| 3791 | EvalType != CXEval_Int) { |
Alex Lorenz | a19cb2e | 2019-01-08 23:28:37 +0000 | [diff] [blame] | 3792 | delete[] EvalData.stringVal; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3793 | } |
| 3794 | } |
| 3795 | }; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3796 | |
| 3797 | void clang_EvalResult_dispose(CXEvalResult E) { |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3798 | delete static_cast<ExprEvalResult *>(E); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3799 | } |
| 3800 | |
| 3801 | CXEvalResultKind clang_EvalResult_getKind(CXEvalResult E) { |
| 3802 | if (!E) { |
| 3803 | return CXEval_UnExposed; |
| 3804 | } |
| 3805 | return ((ExprEvalResult *)E)->EvalType; |
| 3806 | } |
| 3807 | |
| 3808 | int clang_EvalResult_getAsInt(CXEvalResult E) { |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3809 | return clang_EvalResult_getAsLongLong(E); |
| 3810 | } |
| 3811 | |
| 3812 | long long clang_EvalResult_getAsLongLong(CXEvalResult E) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3813 | if (!E) { |
| 3814 | return 0; |
| 3815 | } |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3816 | ExprEvalResult *Result = (ExprEvalResult*)E; |
| 3817 | if (Result->IsUnsignedInt) |
| 3818 | return Result->EvalData.unsignedVal; |
| 3819 | return Result->EvalData.intVal; |
| 3820 | } |
| 3821 | |
| 3822 | unsigned clang_EvalResult_isUnsignedInt(CXEvalResult E) { |
| 3823 | return ((ExprEvalResult *)E)->IsUnsignedInt; |
| 3824 | } |
| 3825 | |
| 3826 | unsigned long long clang_EvalResult_getAsUnsigned(CXEvalResult E) { |
| 3827 | if (!E) { |
| 3828 | return 0; |
| 3829 | } |
| 3830 | |
| 3831 | ExprEvalResult *Result = (ExprEvalResult*)E; |
| 3832 | if (Result->IsUnsignedInt) |
| 3833 | return Result->EvalData.unsignedVal; |
| 3834 | return Result->EvalData.intVal; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3835 | } |
| 3836 | |
| 3837 | double clang_EvalResult_getAsDouble(CXEvalResult E) { |
| 3838 | if (!E) { |
| 3839 | return 0; |
| 3840 | } |
| 3841 | return ((ExprEvalResult *)E)->EvalData.floatVal; |
| 3842 | } |
| 3843 | |
| 3844 | const char* clang_EvalResult_getAsStr(CXEvalResult E) { |
| 3845 | if (!E) { |
| 3846 | return nullptr; |
| 3847 | } |
| 3848 | return ((ExprEvalResult *)E)->EvalData.stringVal; |
| 3849 | } |
| 3850 | |
| 3851 | static const ExprEvalResult* evaluateExpr(Expr *expr, CXCursor C) { |
| 3852 | Expr::EvalResult ER; |
| 3853 | ASTContext &ctx = getCursorContext(C); |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3854 | if (!expr) |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3855 | return nullptr; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3856 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3857 | expr = expr->IgnoreParens(); |
Emilio Cobos Alvarez | 7437545 | 2019-07-09 14:27:01 +0000 | [diff] [blame] | 3858 | if (expr->isValueDependent()) |
| 3859 | return nullptr; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3860 | if (!expr->EvaluateAsRValue(ER, ctx)) |
| 3861 | return nullptr; |
| 3862 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3863 | QualType rettype; |
| 3864 | CallExpr *callExpr; |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 3865 | auto result = std::make_unique<ExprEvalResult>(); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3866 | result->EvalType = CXEval_UnExposed; |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3867 | result->IsUnsignedInt = false; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3868 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3869 | if (ER.Val.isInt()) { |
| 3870 | result->EvalType = CXEval_Int; |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3871 | |
| 3872 | auto& val = ER.Val.getInt(); |
| 3873 | if (val.isUnsigned()) { |
| 3874 | result->IsUnsignedInt = true; |
| 3875 | result->EvalData.unsignedVal = val.getZExtValue(); |
| 3876 | } else { |
| 3877 | result->EvalData.intVal = val.getExtValue(); |
| 3878 | } |
| 3879 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3880 | return result.release(); |
| 3881 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3882 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3883 | if (ER.Val.isFloat()) { |
| 3884 | llvm::SmallVector<char, 100> Buffer; |
| 3885 | ER.Val.getFloat().toString(Buffer); |
| 3886 | std::string floatStr(Buffer.data(), Buffer.size()); |
| 3887 | result->EvalType = CXEval_Float; |
| 3888 | bool ignored; |
| 3889 | llvm::APFloat apFloat = ER.Val.getFloat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3890 | apFloat.convert(llvm::APFloat::IEEEdouble(), |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3891 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 3892 | result->EvalData.floatVal = apFloat.convertToDouble(); |
| 3893 | return result.release(); |
| 3894 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3895 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3896 | if (expr->getStmtClass() == Stmt::ImplicitCastExprClass) { |
| 3897 | const ImplicitCastExpr *I = dyn_cast<ImplicitCastExpr>(expr); |
| 3898 | auto *subExpr = I->getSubExprAsWritten(); |
| 3899 | if (subExpr->getStmtClass() == Stmt::StringLiteralClass || |
| 3900 | subExpr->getStmtClass() == Stmt::ObjCStringLiteralClass) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3901 | const StringLiteral *StrE = nullptr; |
| 3902 | const ObjCStringLiteral *ObjCExpr; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3903 | ObjCExpr = dyn_cast<ObjCStringLiteral>(subExpr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3904 | |
| 3905 | if (ObjCExpr) { |
| 3906 | StrE = ObjCExpr->getString(); |
| 3907 | result->EvalType = CXEval_ObjCStrLiteral; |
| 3908 | } else { |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3909 | StrE = cast<StringLiteral>(I->getSubExprAsWritten()); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3910 | result->EvalType = CXEval_StrLiteral; |
| 3911 | } |
| 3912 | |
| 3913 | std::string strRef(StrE->getString().str()); |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3914 | result->EvalData.stringVal = new char[strRef.size() + 1]; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3915 | strncpy((char *)result->EvalData.stringVal, strRef.c_str(), |
| 3916 | strRef.size()); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3917 | result->EvalData.stringVal[strRef.size()] = '\0'; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3918 | return result.release(); |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3919 | } |
| 3920 | } else if (expr->getStmtClass() == Stmt::ObjCStringLiteralClass || |
| 3921 | expr->getStmtClass() == Stmt::StringLiteralClass) { |
| 3922 | const StringLiteral *StrE = nullptr; |
| 3923 | const ObjCStringLiteral *ObjCExpr; |
| 3924 | ObjCExpr = dyn_cast<ObjCStringLiteral>(expr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3925 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3926 | if (ObjCExpr) { |
| 3927 | StrE = ObjCExpr->getString(); |
| 3928 | result->EvalType = CXEval_ObjCStrLiteral; |
| 3929 | } else { |
| 3930 | StrE = cast<StringLiteral>(expr); |
| 3931 | result->EvalType = CXEval_StrLiteral; |
| 3932 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3933 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3934 | std::string strRef(StrE->getString().str()); |
| 3935 | result->EvalData.stringVal = new char[strRef.size() + 1]; |
| 3936 | strncpy((char *)result->EvalData.stringVal, strRef.c_str(), strRef.size()); |
| 3937 | result->EvalData.stringVal[strRef.size()] = '\0'; |
| 3938 | return result.release(); |
| 3939 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3940 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3941 | if (expr->getStmtClass() == Stmt::CStyleCastExprClass) { |
| 3942 | CStyleCastExpr *CC = static_cast<CStyleCastExpr *>(expr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3943 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3944 | rettype = CC->getType(); |
| 3945 | if (rettype.getAsString() == "CFStringRef" && |
| 3946 | CC->getSubExpr()->getStmtClass() == Stmt::CallExprClass) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3947 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3948 | callExpr = static_cast<CallExpr *>(CC->getSubExpr()); |
| 3949 | StringLiteral *S = getCFSTR_value(callExpr); |
| 3950 | if (S) { |
| 3951 | std::string strLiteral(S->getString().str()); |
| 3952 | result->EvalType = CXEval_CFStr; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3953 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3954 | result->EvalData.stringVal = new char[strLiteral.size() + 1]; |
| 3955 | strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(), |
| 3956 | strLiteral.size()); |
| 3957 | result->EvalData.stringVal[strLiteral.size()] = '\0'; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3958 | return result.release(); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3959 | } |
| 3960 | } |
| 3961 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3962 | } else if (expr->getStmtClass() == Stmt::CallExprClass) { |
| 3963 | callExpr = static_cast<CallExpr *>(expr); |
| 3964 | rettype = callExpr->getCallReturnType(ctx); |
| 3965 | |
| 3966 | if (rettype->isVectorType() || callExpr->getNumArgs() > 1) |
| 3967 | return nullptr; |
| 3968 | |
| 3969 | if (rettype->isIntegralType(ctx) || rettype->isRealFloatingType()) { |
| 3970 | if (callExpr->getNumArgs() == 1 && |
| 3971 | !callExpr->getArg(0)->getType()->isIntegralType(ctx)) |
| 3972 | return nullptr; |
| 3973 | } else if (rettype.getAsString() == "CFStringRef") { |
| 3974 | |
| 3975 | StringLiteral *S = getCFSTR_value(callExpr); |
| 3976 | if (S) { |
| 3977 | std::string strLiteral(S->getString().str()); |
| 3978 | result->EvalType = CXEval_CFStr; |
| 3979 | result->EvalData.stringVal = new char[strLiteral.size() + 1]; |
| 3980 | strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(), |
| 3981 | strLiteral.size()); |
| 3982 | result->EvalData.stringVal[strLiteral.size()] = '\0'; |
| 3983 | return result.release(); |
| 3984 | } |
| 3985 | } |
| 3986 | } else if (expr->getStmtClass() == Stmt::DeclRefExprClass) { |
| 3987 | DeclRefExpr *D = static_cast<DeclRefExpr *>(expr); |
| 3988 | ValueDecl *V = D->getDecl(); |
| 3989 | if (V->getKind() == Decl::Function) { |
| 3990 | std::string strName = V->getNameAsString(); |
| 3991 | result->EvalType = CXEval_Other; |
| 3992 | result->EvalData.stringVal = new char[strName.size() + 1]; |
| 3993 | strncpy(result->EvalData.stringVal, strName.c_str(), strName.size()); |
| 3994 | result->EvalData.stringVal[strName.size()] = '\0'; |
| 3995 | return result.release(); |
| 3996 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3997 | } |
| 3998 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3999 | return nullptr; |
| 4000 | } |
| 4001 | |
Alex Lorenz | 65317e1 | 2019-01-08 22:32:51 +0000 | [diff] [blame] | 4002 | static const Expr *evaluateDeclExpr(const Decl *D) { |
| 4003 | if (!D) |
Evgeniy Stepanov | 9b87149 | 2018-07-10 19:48:53 +0000 | [diff] [blame] | 4004 | return nullptr; |
Alex Lorenz | 65317e1 | 2019-01-08 22:32:51 +0000 | [diff] [blame] | 4005 | if (auto *Var = dyn_cast<VarDecl>(D)) |
| 4006 | return Var->getInit(); |
| 4007 | else if (auto *Field = dyn_cast<FieldDecl>(D)) |
| 4008 | return Field->getInClassInitializer(); |
| 4009 | return nullptr; |
| 4010 | } |
Evgeniy Stepanov | 6df47ce | 2018-07-10 19:49:07 +0000 | [diff] [blame] | 4011 | |
Alex Lorenz | 65317e1 | 2019-01-08 22:32:51 +0000 | [diff] [blame] | 4012 | static const Expr *evaluateCompoundStmtExpr(const CompoundStmt *CS) { |
| 4013 | assert(CS && "invalid compound statement"); |
| 4014 | for (auto *bodyIterator : CS->body()) { |
| 4015 | if (const auto *E = dyn_cast<Expr>(bodyIterator)) |
| 4016 | return E; |
Evgeniy Stepanov | 6df47ce | 2018-07-10 19:49:07 +0000 | [diff] [blame] | 4017 | } |
Alex Lorenz | c4cf96e | 2018-07-09 19:56:45 +0000 | [diff] [blame] | 4018 | return nullptr; |
| 4019 | } |
| 4020 | |
Alex Lorenz | 65317e1 | 2019-01-08 22:32:51 +0000 | [diff] [blame] | 4021 | CXEvalResult clang_Cursor_Evaluate(CXCursor C) { |
| 4022 | if (const Expr *E = |
| 4023 | clang_getCursorKind(C) == CXCursor_CompoundStmt |
| 4024 | ? evaluateCompoundStmtExpr(cast<CompoundStmt>(getCursorStmt(C))) |
| 4025 | : evaluateDeclExpr(getCursorDecl(C))) |
| 4026 | return const_cast<CXEvalResult>( |
| 4027 | reinterpret_cast<const void *>(evaluateExpr(const_cast<Expr *>(E), C))); |
| 4028 | return nullptr; |
| 4029 | } |
| 4030 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 4031 | unsigned clang_Cursor_hasAttrs(CXCursor C) { |
| 4032 | const Decl *D = getCursorDecl(C); |
| 4033 | if (!D) { |
| 4034 | return 0; |
| 4035 | } |
| 4036 | |
| 4037 | if (D->hasAttrs()) { |
| 4038 | return 1; |
| 4039 | } |
| 4040 | |
| 4041 | return 0; |
| 4042 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4043 | unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { |
| 4044 | return CXSaveTranslationUnit_None; |
| 4045 | } |
| 4046 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4047 | static CXSaveError clang_saveTranslationUnit_Impl(CXTranslationUnit TU, |
| 4048 | const char *FileName, |
| 4049 | unsigned options) { |
| 4050 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4051 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
| 4052 | setThreadBackgroundPriority(); |
| 4053 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4054 | bool hadError = cxtu::getASTUnit(TU)->Save(FileName); |
| 4055 | return hadError ? CXSaveError_Unknown : CXSaveError_None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4056 | } |
| 4057 | |
| 4058 | int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, |
| 4059 | unsigned options) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 4060 | LOG_FUNC_SECTION { |
| 4061 | *Log << TU << ' ' << FileName; |
| 4062 | } |
| 4063 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4064 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4065 | LOG_BAD_TU(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4066 | return CXSaveError_InvalidTU; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4067 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4068 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4069 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4070 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 4071 | if (!CXXUnit->hasSema()) |
| 4072 | return CXSaveError_InvalidTU; |
| 4073 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4074 | CXSaveError result; |
| 4075 | auto SaveTranslationUnitImpl = [=, &result]() { |
| 4076 | result = clang_saveTranslationUnit_Impl(TU, FileName, options); |
| 4077 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4078 | |
Erik Verbruggen | 3cc3911 | 2017-11-14 09:34:39 +0000 | [diff] [blame] | 4079 | if (!CXXUnit->getDiagnostics().hasUnrecoverableErrorOccurred()) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4080 | SaveTranslationUnitImpl(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4081 | |
| 4082 | if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 4083 | PrintLibclangResourceUsage(TU); |
| 4084 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4085 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4086 | } |
| 4087 | |
| 4088 | // We have an AST that has invalid nodes due to compiler errors. |
| 4089 | // Use a crash recovery thread for protection. |
| 4090 | |
| 4091 | llvm::CrashRecoveryContext CRC; |
| 4092 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4093 | if (!RunSafely(CRC, SaveTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4094 | fprintf(stderr, "libclang: crash detected during AST saving: {\n"); |
| 4095 | fprintf(stderr, " 'filename' : '%s'\n", FileName); |
| 4096 | fprintf(stderr, " 'options' : %d,\n", options); |
| 4097 | fprintf(stderr, "}\n"); |
| 4098 | |
| 4099 | return CXSaveError_Unknown; |
| 4100 | |
| 4101 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
| 4102 | PrintLibclangResourceUsage(TU); |
| 4103 | } |
| 4104 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4105 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4106 | } |
| 4107 | |
| 4108 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
| 4109 | if (CTUnit) { |
| 4110 | // If the translation unit has been marked as unsafe to free, just discard |
| 4111 | // it. |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 4112 | ASTUnit *Unit = cxtu::getASTUnit(CTUnit); |
| 4113 | if (Unit && Unit->isUnsafeToFree()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4114 | return; |
| 4115 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4116 | delete cxtu::getASTUnit(CTUnit); |
Dmitri Gribenko | b95b3f1 | 2013-01-26 22:44:19 +0000 | [diff] [blame] | 4117 | delete CTUnit->StringPool; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4118 | delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics); |
| 4119 | disposeOverridenCXCursorsPool(CTUnit->OverridenCursorsPool); |
Dmitri Gribenko | 9e60511 | 2013-11-13 22:16:51 +0000 | [diff] [blame] | 4120 | delete CTUnit->CommentToXML; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4121 | delete CTUnit; |
| 4122 | } |
| 4123 | } |
| 4124 | |
Erik Verbruggen | 346066b | 2017-05-30 14:25:54 +0000 | [diff] [blame] | 4125 | unsigned clang_suspendTranslationUnit(CXTranslationUnit CTUnit) { |
| 4126 | if (CTUnit) { |
| 4127 | ASTUnit *Unit = cxtu::getASTUnit(CTUnit); |
| 4128 | |
| 4129 | if (Unit && Unit->isUnsafeToFree()) |
| 4130 | return false; |
| 4131 | |
| 4132 | Unit->ResetForParse(); |
| 4133 | return true; |
| 4134 | } |
| 4135 | |
| 4136 | return false; |
| 4137 | } |
| 4138 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4139 | unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { |
| 4140 | return CXReparse_None; |
| 4141 | } |
| 4142 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4143 | static CXErrorCode |
| 4144 | clang_reparseTranslationUnit_Impl(CXTranslationUnit TU, |
| 4145 | ArrayRef<CXUnsavedFile> unsaved_files, |
| 4146 | unsigned options) { |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 4147 | // Check arguments. |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4148 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4149 | LOG_BAD_TU(TU); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4150 | return CXError_InvalidArguments; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 4151 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4152 | |
| 4153 | // Reset the associated diagnostics. |
| 4154 | delete static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 4155 | TU->Diagnostics = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4156 | |
Dmitri Gribenko | 183436e | 2013-01-26 21:49:50 +0000 | [diff] [blame] | 4157 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4158 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) |
| 4159 | setThreadBackgroundPriority(); |
| 4160 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4161 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4162 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 4163 | |
| 4164 | std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles( |
| 4165 | new std::vector<ASTUnit::RemappedFile>()); |
| 4166 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4167 | // Recover resources if we crash before exiting this function. |
| 4168 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 4169 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 4170 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4171 | for (auto &UF : unsaved_files) { |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 4172 | std::unique_ptr<llvm::MemoryBuffer> MB = |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 4173 | llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 4174 | RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4175 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 4176 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 4177 | if (!CXXUnit->Reparse(CXXIdx->getPCHContainerOperations(), |
| 4178 | *RemappedFiles.get())) |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4179 | return CXError_Success; |
| 4180 | if (isASTReadError(CXXUnit)) |
| 4181 | return CXError_ASTReadError; |
| 4182 | return CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4183 | } |
| 4184 | |
| 4185 | int clang_reparseTranslationUnit(CXTranslationUnit TU, |
| 4186 | unsigned num_unsaved_files, |
| 4187 | struct CXUnsavedFile *unsaved_files, |
| 4188 | unsigned options) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 4189 | LOG_FUNC_SECTION { |
| 4190 | *Log << TU; |
| 4191 | } |
| 4192 | |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 4193 | if (num_unsaved_files && !unsaved_files) |
| 4194 | return CXError_InvalidArguments; |
| 4195 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4196 | CXErrorCode result; |
| 4197 | auto ReparseTranslationUnitImpl = [=, &result]() { |
| 4198 | result = clang_reparseTranslationUnit_Impl( |
| 4199 | TU, llvm::makeArrayRef(unsaved_files, num_unsaved_files), options); |
| 4200 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4201 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4202 | llvm::CrashRecoveryContext CRC; |
| 4203 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4204 | if (!RunSafely(CRC, ReparseTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4205 | fprintf(stderr, "libclang: crash detected during reparsing\n"); |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4206 | cxtu::getASTUnit(TU)->setUnsafeToFree(true); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 4207 | return CXError_Crashed; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4208 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 4209 | PrintLibclangResourceUsage(TU); |
| 4210 | |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 4211 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
| 4214 | |
| 4215 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4216 | if (isNotUsableTU(CTUnit)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4217 | LOG_BAD_TU(CTUnit); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4218 | return cxstring::createEmpty(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4219 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4220 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4221 | ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4222 | return cxstring::createDup(CXXUnit->getOriginalSourceFileName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4223 | } |
| 4224 | |
| 4225 | CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4226 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4227 | LOG_BAD_TU(TU); |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 4228 | return clang_getNullCursor(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4229 | } |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 4230 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4231 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4232 | return MakeCXCursor(CXXUnit->getASTContext().getTranslationUnitDecl(), TU); |
| 4233 | } |
| 4234 | |
Emilio Cobos Alvarez | 485ad42 | 2017-04-28 15:56:39 +0000 | [diff] [blame] | 4235 | CXTargetInfo clang_getTranslationUnitTargetInfo(CXTranslationUnit CTUnit) { |
| 4236 | if (isNotUsableTU(CTUnit)) { |
| 4237 | LOG_BAD_TU(CTUnit); |
| 4238 | return nullptr; |
| 4239 | } |
| 4240 | |
| 4241 | CXTargetInfoImpl* impl = new CXTargetInfoImpl(); |
| 4242 | impl->TranslationUnit = CTUnit; |
| 4243 | return impl; |
| 4244 | } |
| 4245 | |
| 4246 | CXString clang_TargetInfo_getTriple(CXTargetInfo TargetInfo) { |
| 4247 | if (!TargetInfo) |
| 4248 | return cxstring::createEmpty(); |
| 4249 | |
| 4250 | CXTranslationUnit CTUnit = TargetInfo->TranslationUnit; |
| 4251 | assert(!isNotUsableTU(CTUnit) && |
| 4252 | "Unexpected unusable translation unit in TargetInfo"); |
| 4253 | |
| 4254 | ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); |
| 4255 | std::string Triple = |
| 4256 | CXXUnit->getASTContext().getTargetInfo().getTriple().normalize(); |
| 4257 | return cxstring::createDup(Triple); |
| 4258 | } |
| 4259 | |
| 4260 | int clang_TargetInfo_getPointerWidth(CXTargetInfo TargetInfo) { |
| 4261 | if (!TargetInfo) |
| 4262 | return -1; |
| 4263 | |
| 4264 | CXTranslationUnit CTUnit = TargetInfo->TranslationUnit; |
| 4265 | assert(!isNotUsableTU(CTUnit) && |
| 4266 | "Unexpected unusable translation unit in TargetInfo"); |
| 4267 | |
| 4268 | ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); |
| 4269 | return CXXUnit->getASTContext().getTargetInfo().getMaxPointerWidth(); |
| 4270 | } |
| 4271 | |
| 4272 | void clang_TargetInfo_dispose(CXTargetInfo TargetInfo) { |
| 4273 | if (!TargetInfo) |
| 4274 | return; |
| 4275 | |
| 4276 | delete TargetInfo; |
| 4277 | } |
| 4278 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4279 | //===----------------------------------------------------------------------===// |
| 4280 | // CXFile Operations. |
| 4281 | //===----------------------------------------------------------------------===// |
| 4282 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4283 | CXString clang_getFileName(CXFile SFile) { |
| 4284 | if (!SFile) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 4285 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4286 | |
| 4287 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4288 | return cxstring::createRef(FEnt->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | time_t clang_getFileTime(CXFile SFile) { |
| 4292 | if (!SFile) |
| 4293 | return 0; |
| 4294 | |
| 4295 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 4296 | return FEnt->getModificationTime(); |
| 4297 | } |
| 4298 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4299 | CXFile clang_getFile(CXTranslationUnit TU, const char *file_name) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4300 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4301 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 4302 | return nullptr; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4303 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4304 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4305 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4306 | |
| 4307 | FileManager &FMgr = CXXUnit->getFileManager(); |
Harlan Haskins | 8d323d1 | 2019-08-01 21:31:56 +0000 | [diff] [blame] | 4308 | auto File = FMgr.getFile(file_name); |
| 4309 | if (!File) |
| 4310 | return nullptr; |
| 4311 | return const_cast<FileEntry *>(*File); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4312 | } |
| 4313 | |
Erik Verbruggen | 3afa3ce | 2017-12-06 09:02:52 +0000 | [diff] [blame] | 4314 | const char *clang_getFileContents(CXTranslationUnit TU, CXFile file, |
| 4315 | size_t *size) { |
| 4316 | if (isNotUsableTU(TU)) { |
| 4317 | LOG_BAD_TU(TU); |
| 4318 | return nullptr; |
| 4319 | } |
| 4320 | |
| 4321 | const SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager(); |
| 4322 | FileID fid = SM.translateFile(static_cast<FileEntry *>(file)); |
| 4323 | bool Invalid = true; |
Nico Weber | 04347d8 | 2019-04-04 21:06:41 +0000 | [diff] [blame] | 4324 | const llvm::MemoryBuffer *buf = SM.getBuffer(fid, &Invalid); |
Erik Verbruggen | 3afa3ce | 2017-12-06 09:02:52 +0000 | [diff] [blame] | 4325 | if (Invalid) { |
| 4326 | if (size) |
| 4327 | *size = 0; |
| 4328 | return nullptr; |
| 4329 | } |
| 4330 | if (size) |
| 4331 | *size = buf->getBufferSize(); |
| 4332 | return buf->getBufferStart(); |
| 4333 | } |
| 4334 | |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4335 | unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit TU, |
| 4336 | CXFile file) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4337 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4338 | LOG_BAD_TU(TU); |
| 4339 | return 0; |
| 4340 | } |
| 4341 | |
| 4342 | if (!file) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4343 | return 0; |
| 4344 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4345 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4346 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
| 4347 | return CXXUnit->getPreprocessor().getHeaderSearchInfo() |
| 4348 | .isFileMultipleIncludeGuarded(FEnt); |
| 4349 | } |
| 4350 | |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4351 | int clang_getFileUniqueID(CXFile file, CXFileUniqueID *outID) { |
| 4352 | if (!file || !outID) |
| 4353 | return 1; |
| 4354 | |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4355 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 4356 | const llvm::sys::fs::UniqueID &ID = FEnt->getUniqueID(); |
| 4357 | outID->data[0] = ID.getDevice(); |
| 4358 | outID->data[1] = ID.getFile(); |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4359 | outID->data[2] = FEnt->getModificationTime(); |
| 4360 | return 0; |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
Argyrios Kyrtzidis | ac3997e | 2014-08-16 00:26:19 +0000 | [diff] [blame] | 4363 | int clang_File_isEqual(CXFile file1, CXFile file2) { |
| 4364 | if (file1 == file2) |
| 4365 | return true; |
| 4366 | |
| 4367 | if (!file1 || !file2) |
| 4368 | return false; |
| 4369 | |
| 4370 | FileEntry *FEnt1 = static_cast<FileEntry *>(file1); |
| 4371 | FileEntry *FEnt2 = static_cast<FileEntry *>(file2); |
| 4372 | return FEnt1->getUniqueID() == FEnt2->getUniqueID(); |
| 4373 | } |
| 4374 | |
Fangrui Song | e46ac5f | 2018-04-07 20:50:35 +0000 | [diff] [blame] | 4375 | CXString clang_File_tryGetRealPathName(CXFile SFile) { |
| 4376 | if (!SFile) |
| 4377 | return cxstring::createNull(); |
| 4378 | |
| 4379 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 4380 | return cxstring::createRef(FEnt->tryGetRealPathName()); |
| 4381 | } |
| 4382 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4383 | //===----------------------------------------------------------------------===// |
| 4384 | // CXCursor Operations. |
| 4385 | //===----------------------------------------------------------------------===// |
| 4386 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4387 | static const Decl *getDeclFromExpr(const Stmt *E) { |
| 4388 | if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4389 | return getDeclFromExpr(CE->getSubExpr()); |
| 4390 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4391 | if (const DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4392 | return RefExpr->getDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4393 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4394 | return ME->getMemberDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4395 | if (const ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4396 | return RE->getDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4397 | if (const ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4398 | if (PRE->isExplicitProperty()) |
| 4399 | return PRE->getExplicitProperty(); |
| 4400 | // It could be messaging both getter and setter as in: |
| 4401 | // ++myobj.myprop; |
| 4402 | // in which case prefer to associate the setter since it is less obvious |
| 4403 | // from inspecting the source that the setter is going to get called. |
| 4404 | if (PRE->isMessagingSetter()) |
| 4405 | return PRE->getImplicitPropertySetter(); |
| 4406 | return PRE->getImplicitPropertyGetter(); |
| 4407 | } |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4408 | if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4409 | return getDeclFromExpr(POE->getSyntacticForm()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4410 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4411 | if (Expr *Src = OVE->getSourceExpr()) |
| 4412 | return getDeclFromExpr(Src); |
| 4413 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4414 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4415 | return getDeclFromExpr(CE->getCallee()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4416 | if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4417 | if (!CE->isElidable()) |
| 4418 | return CE->getConstructor(); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4419 | if (const CXXInheritedCtorInitExpr *CE = |
| 4420 | dyn_cast<CXXInheritedCtorInitExpr>(E)) |
| 4421 | return CE->getConstructor(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4422 | if (const ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4423 | return OME->getMethodDecl(); |
| 4424 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4425 | if (const ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4426 | return PE->getProtocol(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4427 | if (const SubstNonTypeTemplateParmPackExpr *NTTP |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4428 | = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E)) |
| 4429 | return NTTP->getParameterPack(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4430 | if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4431 | if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || |
| 4432 | isa<ParmVarDecl>(SizeOfPack->getPack())) |
| 4433 | return SizeOfPack->getPack(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 4434 | |
| 4435 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4436 | } |
| 4437 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4438 | static SourceLocation getLocationFromExpr(const Expr *E) { |
| 4439 | if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4440 | return getLocationFromExpr(CE->getSubExpr()); |
| 4441 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4442 | if (const ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4443 | return /*FIXME:*/Msg->getLeftLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4444 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4445 | return DRE->getLocation(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4446 | if (const MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4447 | return Member->getMemberLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4448 | if (const ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4449 | return Ivar->getLocation(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4450 | if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4451 | return SizeOfPack->getPackLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4452 | if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4453 | return PropRef->getLocation(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4454 | |
| 4455 | return E->getBeginLoc(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
NAKAMURA Takumi | a01f4c3 | 2016-12-19 16:50:43 +0000 | [diff] [blame] | 4458 | extern "C" { |
| 4459 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4460 | unsigned clang_visitChildren(CXCursor parent, |
| 4461 | CXCursorVisitor visitor, |
| 4462 | CXClientData client_data) { |
| 4463 | CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, |
| 4464 | /*VisitPreprocessorLast=*/false); |
| 4465 | return CursorVis.VisitChildren(parent); |
| 4466 | } |
| 4467 | |
| 4468 | #ifndef __has_feature |
| 4469 | #define __has_feature(x) 0 |
| 4470 | #endif |
| 4471 | #if __has_feature(blocks) |
| 4472 | typedef enum CXChildVisitResult |
| 4473 | (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent); |
| 4474 | |
| 4475 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 4476 | CXClientData client_data) { |
| 4477 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 4478 | return block(cursor, parent); |
| 4479 | } |
| 4480 | #else |
| 4481 | // If we are compiled with a compiler that doesn't have native blocks support, |
| 4482 | // define and call the block manually, so the |
| 4483 | typedef struct _CXChildVisitResult |
| 4484 | { |
| 4485 | void *isa; |
| 4486 | int flags; |
| 4487 | int reserved; |
| 4488 | enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, |
| 4489 | CXCursor); |
| 4490 | } *CXCursorVisitorBlock; |
| 4491 | |
| 4492 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 4493 | CXClientData client_data) { |
| 4494 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 4495 | return block->invoke(block, cursor, parent); |
| 4496 | } |
| 4497 | #endif |
| 4498 | |
| 4499 | |
| 4500 | unsigned clang_visitChildrenWithBlock(CXCursor parent, |
| 4501 | CXCursorVisitorBlock block) { |
| 4502 | return clang_visitChildren(parent, visitWithBlock, block); |
| 4503 | } |
| 4504 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4505 | static CXString getDeclSpelling(const Decl *D) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4506 | if (!D) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4507 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4508 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4509 | const NamedDecl *ND = dyn_cast<NamedDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4510 | if (!ND) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4511 | if (const ObjCPropertyImplDecl *PropImpl = |
| 4512 | dyn_cast<ObjCPropertyImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4513 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4514 | return cxstring::createDup(Property->getIdentifier()->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4515 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4516 | if (const ImportDecl *ImportD = dyn_cast<ImportDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4517 | if (Module *Mod = ImportD->getImportedModule()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4518 | return cxstring::createDup(Mod->getFullModuleName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4519 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4520 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4523 | if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4524 | return cxstring::createDup(OMD->getSelector().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4525 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4526 | if (const ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4527 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 4528 | // and returns different names. NamedDecl returns the class name and |
| 4529 | // ObjCCategoryImplDecl returns the category name. |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4530 | return cxstring::createRef(CIMP->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4531 | |
| 4532 | if (isa<UsingDirectiveDecl>(D)) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4533 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4534 | |
| 4535 | SmallString<1024> S; |
| 4536 | llvm::raw_svector_ostream os(S); |
| 4537 | ND->printName(os); |
| 4538 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4539 | return cxstring::createDup(os.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4540 | } |
| 4541 | |
| 4542 | CXString clang_getCursorSpelling(CXCursor C) { |
| 4543 | if (clang_isTranslationUnit(C.kind)) |
Dmitri Gribenko | 2c173b4 | 2013-01-11 19:28:44 +0000 | [diff] [blame] | 4544 | return clang_getTranslationUnitSpelling(getCursorTU(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4545 | |
| 4546 | if (clang_isReference(C.kind)) { |
| 4547 | switch (C.kind) { |
| 4548 | case CXCursor_ObjCSuperClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4549 | const ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4550 | return cxstring::createRef(Super->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4551 | } |
| 4552 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4553 | const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4554 | return cxstring::createRef(Class->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4555 | } |
| 4556 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4557 | const ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4558 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4559 | return cxstring::createRef(OID->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4560 | } |
| 4561 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4562 | const CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4563 | return cxstring::createDup(B->getType().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4564 | } |
| 4565 | case CXCursor_TypeRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4566 | const TypeDecl *Type = getCursorTypeRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4567 | assert(Type && "Missing type decl"); |
| 4568 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4569 | return cxstring::createDup(getCursorContext(C).getTypeDeclType(Type). |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4570 | getAsString()); |
| 4571 | } |
| 4572 | case CXCursor_TemplateRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4573 | const TemplateDecl *Template = getCursorTemplateRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4574 | assert(Template && "Missing template decl"); |
| 4575 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4576 | return cxstring::createDup(Template->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4577 | } |
| 4578 | |
| 4579 | case CXCursor_NamespaceRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4580 | const NamedDecl *NS = getCursorNamespaceRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4581 | assert(NS && "Missing namespace decl"); |
| 4582 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4583 | return cxstring::createDup(NS->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4584 | } |
| 4585 | |
| 4586 | case CXCursor_MemberRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4587 | const FieldDecl *Field = getCursorMemberRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4588 | assert(Field && "Missing member decl"); |
| 4589 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4590 | return cxstring::createDup(Field->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4591 | } |
| 4592 | |
| 4593 | case CXCursor_LabelRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4594 | const LabelStmt *Label = getCursorLabelRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4595 | assert(Label && "Missing label"); |
| 4596 | |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4597 | return cxstring::createRef(Label->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4598 | } |
| 4599 | |
| 4600 | case CXCursor_OverloadedDeclRef: { |
| 4601 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4602 | if (const Decl *D = Storage.dyn_cast<const Decl *>()) { |
| 4603 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4604 | return cxstring::createDup(ND->getNameAsString()); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4605 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4606 | } |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4607 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4608 | return cxstring::createDup(E->getName().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4609 | OverloadedTemplateStorage *Ovl |
| 4610 | = Storage.get<OverloadedTemplateStorage*>(); |
| 4611 | if (Ovl->size() == 0) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4612 | return cxstring::createEmpty(); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4613 | return cxstring::createDup((*Ovl->begin())->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4614 | } |
| 4615 | |
| 4616 | case CXCursor_VariableRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4617 | const VarDecl *Var = getCursorVariableRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4618 | assert(Var && "Missing variable decl"); |
| 4619 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4620 | return cxstring::createDup(Var->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
| 4623 | default: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4624 | return cxstring::createRef("<not implemented>"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4625 | } |
| 4626 | } |
| 4627 | |
| 4628 | if (clang_isExpression(C.kind)) { |
Argyrios Kyrtzidis | 3227d86 | 2014-03-03 19:40:52 +0000 | [diff] [blame] | 4629 | const Expr *E = getCursorExpr(C); |
| 4630 | |
| 4631 | if (C.kind == CXCursor_ObjCStringLiteral || |
| 4632 | C.kind == CXCursor_StringLiteral) { |
| 4633 | const StringLiteral *SLit; |
| 4634 | if (const ObjCStringLiteral *OSL = dyn_cast<ObjCStringLiteral>(E)) { |
| 4635 | SLit = OSL->getString(); |
| 4636 | } else { |
| 4637 | SLit = cast<StringLiteral>(E); |
| 4638 | } |
| 4639 | SmallString<256> Buf; |
| 4640 | llvm::raw_svector_ostream OS(Buf); |
| 4641 | SLit->outputString(OS); |
| 4642 | return cxstring::createDup(OS.str()); |
| 4643 | } |
| 4644 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4645 | const Decl *D = getDeclFromExpr(getCursorExpr(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4646 | if (D) |
| 4647 | return getDeclSpelling(D); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4648 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4649 | } |
| 4650 | |
| 4651 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4652 | const Stmt *S = getCursorStmt(C); |
| 4653 | if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4654 | return cxstring::createRef(Label->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4655 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4656 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4657 | } |
| 4658 | |
| 4659 | if (C.kind == CXCursor_MacroExpansion) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4660 | return cxstring::createRef(getCursorMacroExpansion(C).getName() |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4661 | ->getNameStart()); |
| 4662 | |
| 4663 | if (C.kind == CXCursor_MacroDefinition) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4664 | return cxstring::createRef(getCursorMacroDefinition(C)->getName() |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4665 | ->getNameStart()); |
| 4666 | |
| 4667 | if (C.kind == CXCursor_InclusionDirective) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4668 | return cxstring::createDup(getCursorInclusionDirective(C)->getFileName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4669 | |
| 4670 | if (clang_isDeclaration(C.kind)) |
| 4671 | return getDeclSpelling(getCursorDecl(C)); |
| 4672 | |
| 4673 | if (C.kind == CXCursor_AnnotateAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 4674 | const AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C)); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4675 | return cxstring::createDup(AA->getAnnotation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4676 | } |
| 4677 | |
| 4678 | if (C.kind == CXCursor_AsmLabelAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 4679 | const AsmLabelAttr *AA = cast<AsmLabelAttr>(cxcursor::getCursorAttr(C)); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4680 | return cxstring::createDup(AA->getLabel()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 4683 | if (C.kind == CXCursor_PackedAttr) { |
| 4684 | return cxstring::createRef("packed"); |
| 4685 | } |
| 4686 | |
Saleem Abdulrasool | 79c6971 | 2015-09-05 18:53:43 +0000 | [diff] [blame] | 4687 | if (C.kind == CXCursor_VisibilityAttr) { |
| 4688 | const VisibilityAttr *AA = cast<VisibilityAttr>(cxcursor::getCursorAttr(C)); |
| 4689 | switch (AA->getVisibility()) { |
| 4690 | case VisibilityAttr::VisibilityType::Default: |
| 4691 | return cxstring::createRef("default"); |
| 4692 | case VisibilityAttr::VisibilityType::Hidden: |
| 4693 | return cxstring::createRef("hidden"); |
| 4694 | case VisibilityAttr::VisibilityType::Protected: |
| 4695 | return cxstring::createRef("protected"); |
| 4696 | } |
| 4697 | llvm_unreachable("unknown visibility type"); |
| 4698 | } |
| 4699 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4700 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4701 | } |
| 4702 | |
| 4703 | CXSourceRange clang_Cursor_getSpellingNameRange(CXCursor C, |
| 4704 | unsigned pieceIndex, |
| 4705 | unsigned options) { |
| 4706 | if (clang_Cursor_isNull(C)) |
| 4707 | return clang_getNullRange(); |
| 4708 | |
| 4709 | ASTContext &Ctx = getCursorContext(C); |
| 4710 | |
| 4711 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4712 | const Stmt *S = getCursorStmt(C); |
| 4713 | if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4714 | if (pieceIndex > 0) |
| 4715 | return clang_getNullRange(); |
| 4716 | return cxloc::translateSourceRange(Ctx, Label->getIdentLoc()); |
| 4717 | } |
| 4718 | |
| 4719 | return clang_getNullRange(); |
| 4720 | } |
| 4721 | |
| 4722 | if (C.kind == CXCursor_ObjCMessageExpr) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4723 | if (const ObjCMessageExpr * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4724 | ME = dyn_cast_or_null<ObjCMessageExpr>(getCursorExpr(C))) { |
| 4725 | if (pieceIndex >= ME->getNumSelectorLocs()) |
| 4726 | return clang_getNullRange(); |
| 4727 | return cxloc::translateSourceRange(Ctx, ME->getSelectorLoc(pieceIndex)); |
| 4728 | } |
| 4729 | } |
| 4730 | |
| 4731 | if (C.kind == CXCursor_ObjCInstanceMethodDecl || |
| 4732 | C.kind == CXCursor_ObjCClassMethodDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4733 | if (const ObjCMethodDecl * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4734 | MD = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(C))) { |
| 4735 | if (pieceIndex >= MD->getNumSelectorLocs()) |
| 4736 | return clang_getNullRange(); |
| 4737 | return cxloc::translateSourceRange(Ctx, MD->getSelectorLoc(pieceIndex)); |
| 4738 | } |
| 4739 | } |
| 4740 | |
| 4741 | if (C.kind == CXCursor_ObjCCategoryDecl || |
| 4742 | C.kind == CXCursor_ObjCCategoryImplDecl) { |
| 4743 | if (pieceIndex > 0) |
| 4744 | return clang_getNullRange(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4745 | if (const ObjCCategoryDecl * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4746 | CD = dyn_cast_or_null<ObjCCategoryDecl>(getCursorDecl(C))) |
| 4747 | return cxloc::translateSourceRange(Ctx, CD->getCategoryNameLoc()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4748 | if (const ObjCCategoryImplDecl * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4749 | CID = dyn_cast_or_null<ObjCCategoryImplDecl>(getCursorDecl(C))) |
| 4750 | return cxloc::translateSourceRange(Ctx, CID->getCategoryNameLoc()); |
| 4751 | } |
| 4752 | |
| 4753 | if (C.kind == CXCursor_ModuleImportDecl) { |
| 4754 | if (pieceIndex > 0) |
| 4755 | return clang_getNullRange(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4756 | if (const ImportDecl *ImportD = |
| 4757 | dyn_cast_or_null<ImportDecl>(getCursorDecl(C))) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4758 | ArrayRef<SourceLocation> Locs = ImportD->getIdentifierLocs(); |
| 4759 | if (!Locs.empty()) |
| 4760 | return cxloc::translateSourceRange(Ctx, |
| 4761 | SourceRange(Locs.front(), Locs.back())); |
| 4762 | } |
| 4763 | return clang_getNullRange(); |
| 4764 | } |
| 4765 | |
Argyrios Kyrtzidis | a2a1e53 | 2014-08-26 20:23:26 +0000 | [diff] [blame] | 4766 | if (C.kind == CXCursor_CXXMethod || C.kind == CXCursor_Destructor || |
Kevin Funk | 4be5d67 | 2016-12-20 09:56:56 +0000 | [diff] [blame] | 4767 | C.kind == CXCursor_ConversionFunction || |
| 4768 | C.kind == CXCursor_FunctionDecl) { |
Argyrios Kyrtzidis | a2a1e53 | 2014-08-26 20:23:26 +0000 | [diff] [blame] | 4769 | if (pieceIndex > 0) |
| 4770 | return clang_getNullRange(); |
| 4771 | if (const FunctionDecl *FD = |
| 4772 | dyn_cast_or_null<FunctionDecl>(getCursorDecl(C))) { |
| 4773 | DeclarationNameInfo FunctionName = FD->getNameInfo(); |
| 4774 | return cxloc::translateSourceRange(Ctx, FunctionName.getSourceRange()); |
| 4775 | } |
| 4776 | return clang_getNullRange(); |
| 4777 | } |
| 4778 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4779 | // FIXME: A CXCursor_InclusionDirective should give the location of the |
| 4780 | // filename, but we don't keep track of this. |
| 4781 | |
| 4782 | // FIXME: A CXCursor_AnnotateAttr should give the location of the annotation |
| 4783 | // but we don't keep track of this. |
| 4784 | |
| 4785 | // FIXME: A CXCursor_AsmLabelAttr should give the location of the label |
| 4786 | // but we don't keep track of this. |
| 4787 | |
| 4788 | // Default handling, give the location of the cursor. |
| 4789 | |
| 4790 | if (pieceIndex > 0) |
| 4791 | return clang_getNullRange(); |
| 4792 | |
| 4793 | CXSourceLocation CXLoc = clang_getCursorLocation(C); |
| 4794 | SourceLocation Loc = cxloc::translateSourceLocation(CXLoc); |
| 4795 | return cxloc::translateSourceRange(Ctx, Loc); |
| 4796 | } |
| 4797 | |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4798 | CXString clang_Cursor_getMangling(CXCursor C) { |
| 4799 | if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind)) |
| 4800 | return cxstring::createEmpty(); |
| 4801 | |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4802 | // Mangling only works for functions and variables. |
Eli Bendersky | 7975959 | 2014-08-01 15:01:10 +0000 | [diff] [blame] | 4803 | const Decl *D = getCursorDecl(C); |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4804 | if (!D || !(isa<FunctionDecl>(D) || isa<VarDecl>(D))) |
| 4805 | return cxstring::createEmpty(); |
| 4806 | |
Argyrios Kyrtzidis | ca741ce | 2016-02-14 22:30:14 +0000 | [diff] [blame] | 4807 | ASTContext &Ctx = D->getASTContext(); |
Jan Korous | 7e36ecd | 2019-09-05 20:33:52 +0000 | [diff] [blame] | 4808 | ASTNameGenerator ASTNameGen(Ctx); |
| 4809 | return cxstring::createDup(ASTNameGen.getName(D)); |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4810 | } |
| 4811 | |
Saleem Abdulrasool | 6003443 | 2015-11-12 03:57:22 +0000 | [diff] [blame] | 4812 | CXStringSet *clang_Cursor_getCXXManglings(CXCursor C) { |
| 4813 | if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind)) |
| 4814 | return nullptr; |
| 4815 | |
| 4816 | const Decl *D = getCursorDecl(C); |
| 4817 | if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D))) |
| 4818 | return nullptr; |
| 4819 | |
Argyrios Kyrtzidis | ca741ce | 2016-02-14 22:30:14 +0000 | [diff] [blame] | 4820 | ASTContext &Ctx = D->getASTContext(); |
Jan Korous | 7e36ecd | 2019-09-05 20:33:52 +0000 | [diff] [blame] | 4821 | ASTNameGenerator ASTNameGen(Ctx); |
| 4822 | std::vector<std::string> Manglings = ASTNameGen.getAllManglings(D); |
Saleem Abdulrasool | 6003443 | 2015-11-12 03:57:22 +0000 | [diff] [blame] | 4823 | return cxstring::createSet(Manglings); |
| 4824 | } |
| 4825 | |
Dave Lee | 1a532c9 | 2017-09-22 16:58:57 +0000 | [diff] [blame] | 4826 | CXStringSet *clang_Cursor_getObjCManglings(CXCursor C) { |
| 4827 | if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind)) |
| 4828 | return nullptr; |
| 4829 | |
| 4830 | const Decl *D = getCursorDecl(C); |
| 4831 | if (!(isa<ObjCInterfaceDecl>(D) || isa<ObjCImplementationDecl>(D))) |
| 4832 | return nullptr; |
| 4833 | |
| 4834 | ASTContext &Ctx = D->getASTContext(); |
Jan Korous | 7e36ecd | 2019-09-05 20:33:52 +0000 | [diff] [blame] | 4835 | ASTNameGenerator ASTNameGen(Ctx); |
| 4836 | std::vector<std::string> Manglings = ASTNameGen.getAllManglings(D); |
Dave Lee | 1a532c9 | 2017-09-22 16:58:57 +0000 | [diff] [blame] | 4837 | return cxstring::createSet(Manglings); |
| 4838 | } |
| 4839 | |
Jonathan Coe | 45ef503 | 2018-01-16 10:19:56 +0000 | [diff] [blame] | 4840 | CXPrintingPolicy clang_getCursorPrintingPolicy(CXCursor C) { |
| 4841 | if (clang_Cursor_isNull(C)) |
| 4842 | return 0; |
| 4843 | return new PrintingPolicy(getCursorContext(C).getPrintingPolicy()); |
| 4844 | } |
| 4845 | |
| 4846 | void clang_PrintingPolicy_dispose(CXPrintingPolicy Policy) { |
| 4847 | if (Policy) |
| 4848 | delete static_cast<PrintingPolicy *>(Policy); |
| 4849 | } |
| 4850 | |
| 4851 | unsigned |
| 4852 | clang_PrintingPolicy_getProperty(CXPrintingPolicy Policy, |
| 4853 | enum CXPrintingPolicyProperty Property) { |
| 4854 | if (!Policy) |
| 4855 | return 0; |
| 4856 | |
| 4857 | PrintingPolicy *P = static_cast<PrintingPolicy *>(Policy); |
| 4858 | switch (Property) { |
| 4859 | case CXPrintingPolicy_Indentation: |
| 4860 | return P->Indentation; |
| 4861 | case CXPrintingPolicy_SuppressSpecifiers: |
| 4862 | return P->SuppressSpecifiers; |
| 4863 | case CXPrintingPolicy_SuppressTagKeyword: |
| 4864 | return P->SuppressTagKeyword; |
| 4865 | case CXPrintingPolicy_IncludeTagDefinition: |
| 4866 | return P->IncludeTagDefinition; |
| 4867 | case CXPrintingPolicy_SuppressScope: |
| 4868 | return P->SuppressScope; |
| 4869 | case CXPrintingPolicy_SuppressUnwrittenScope: |
| 4870 | return P->SuppressUnwrittenScope; |
| 4871 | case CXPrintingPolicy_SuppressInitializers: |
| 4872 | return P->SuppressInitializers; |
| 4873 | case CXPrintingPolicy_ConstantArraySizeAsWritten: |
| 4874 | return P->ConstantArraySizeAsWritten; |
| 4875 | case CXPrintingPolicy_AnonymousTagLocations: |
| 4876 | return P->AnonymousTagLocations; |
| 4877 | case CXPrintingPolicy_SuppressStrongLifetime: |
| 4878 | return P->SuppressStrongLifetime; |
| 4879 | case CXPrintingPolicy_SuppressLifetimeQualifiers: |
| 4880 | return P->SuppressLifetimeQualifiers; |
| 4881 | case CXPrintingPolicy_SuppressTemplateArgsInCXXConstructors: |
| 4882 | return P->SuppressTemplateArgsInCXXConstructors; |
| 4883 | case CXPrintingPolicy_Bool: |
| 4884 | return P->Bool; |
| 4885 | case CXPrintingPolicy_Restrict: |
| 4886 | return P->Restrict; |
| 4887 | case CXPrintingPolicy_Alignof: |
| 4888 | return P->Alignof; |
| 4889 | case CXPrintingPolicy_UnderscoreAlignof: |
| 4890 | return P->UnderscoreAlignof; |
| 4891 | case CXPrintingPolicy_UseVoidForZeroParams: |
| 4892 | return P->UseVoidForZeroParams; |
| 4893 | case CXPrintingPolicy_TerseOutput: |
| 4894 | return P->TerseOutput; |
| 4895 | case CXPrintingPolicy_PolishForDeclaration: |
| 4896 | return P->PolishForDeclaration; |
| 4897 | case CXPrintingPolicy_Half: |
| 4898 | return P->Half; |
| 4899 | case CXPrintingPolicy_MSWChar: |
| 4900 | return P->MSWChar; |
| 4901 | case CXPrintingPolicy_IncludeNewlines: |
| 4902 | return P->IncludeNewlines; |
| 4903 | case CXPrintingPolicy_MSVCFormatting: |
| 4904 | return P->MSVCFormatting; |
| 4905 | case CXPrintingPolicy_ConstantsAsWritten: |
| 4906 | return P->ConstantsAsWritten; |
| 4907 | case CXPrintingPolicy_SuppressImplicitBase: |
| 4908 | return P->SuppressImplicitBase; |
| 4909 | case CXPrintingPolicy_FullyQualifiedName: |
| 4910 | return P->FullyQualifiedName; |
| 4911 | } |
| 4912 | |
| 4913 | assert(false && "Invalid CXPrintingPolicyProperty"); |
| 4914 | return 0; |
| 4915 | } |
| 4916 | |
| 4917 | void clang_PrintingPolicy_setProperty(CXPrintingPolicy Policy, |
| 4918 | enum CXPrintingPolicyProperty Property, |
| 4919 | unsigned Value) { |
| 4920 | if (!Policy) |
| 4921 | return; |
| 4922 | |
| 4923 | PrintingPolicy *P = static_cast<PrintingPolicy *>(Policy); |
| 4924 | switch (Property) { |
| 4925 | case CXPrintingPolicy_Indentation: |
| 4926 | P->Indentation = Value; |
| 4927 | return; |
| 4928 | case CXPrintingPolicy_SuppressSpecifiers: |
| 4929 | P->SuppressSpecifiers = Value; |
| 4930 | return; |
| 4931 | case CXPrintingPolicy_SuppressTagKeyword: |
| 4932 | P->SuppressTagKeyword = Value; |
| 4933 | return; |
| 4934 | case CXPrintingPolicy_IncludeTagDefinition: |
| 4935 | P->IncludeTagDefinition = Value; |
| 4936 | return; |
| 4937 | case CXPrintingPolicy_SuppressScope: |
| 4938 | P->SuppressScope = Value; |
| 4939 | return; |
| 4940 | case CXPrintingPolicy_SuppressUnwrittenScope: |
| 4941 | P->SuppressUnwrittenScope = Value; |
| 4942 | return; |
| 4943 | case CXPrintingPolicy_SuppressInitializers: |
| 4944 | P->SuppressInitializers = Value; |
| 4945 | return; |
| 4946 | case CXPrintingPolicy_ConstantArraySizeAsWritten: |
| 4947 | P->ConstantArraySizeAsWritten = Value; |
| 4948 | return; |
| 4949 | case CXPrintingPolicy_AnonymousTagLocations: |
| 4950 | P->AnonymousTagLocations = Value; |
| 4951 | return; |
| 4952 | case CXPrintingPolicy_SuppressStrongLifetime: |
| 4953 | P->SuppressStrongLifetime = Value; |
| 4954 | return; |
| 4955 | case CXPrintingPolicy_SuppressLifetimeQualifiers: |
| 4956 | P->SuppressLifetimeQualifiers = Value; |
| 4957 | return; |
| 4958 | case CXPrintingPolicy_SuppressTemplateArgsInCXXConstructors: |
| 4959 | P->SuppressTemplateArgsInCXXConstructors = Value; |
| 4960 | return; |
| 4961 | case CXPrintingPolicy_Bool: |
| 4962 | P->Bool = Value; |
| 4963 | return; |
| 4964 | case CXPrintingPolicy_Restrict: |
| 4965 | P->Restrict = Value; |
| 4966 | return; |
| 4967 | case CXPrintingPolicy_Alignof: |
| 4968 | P->Alignof = Value; |
| 4969 | return; |
| 4970 | case CXPrintingPolicy_UnderscoreAlignof: |
| 4971 | P->UnderscoreAlignof = Value; |
| 4972 | return; |
| 4973 | case CXPrintingPolicy_UseVoidForZeroParams: |
| 4974 | P->UseVoidForZeroParams = Value; |
| 4975 | return; |
| 4976 | case CXPrintingPolicy_TerseOutput: |
| 4977 | P->TerseOutput = Value; |
| 4978 | return; |
| 4979 | case CXPrintingPolicy_PolishForDeclaration: |
| 4980 | P->PolishForDeclaration = Value; |
| 4981 | return; |
| 4982 | case CXPrintingPolicy_Half: |
| 4983 | P->Half = Value; |
| 4984 | return; |
| 4985 | case CXPrintingPolicy_MSWChar: |
| 4986 | P->MSWChar = Value; |
| 4987 | return; |
| 4988 | case CXPrintingPolicy_IncludeNewlines: |
| 4989 | P->IncludeNewlines = Value; |
| 4990 | return; |
| 4991 | case CXPrintingPolicy_MSVCFormatting: |
| 4992 | P->MSVCFormatting = Value; |
| 4993 | return; |
| 4994 | case CXPrintingPolicy_ConstantsAsWritten: |
| 4995 | P->ConstantsAsWritten = Value; |
| 4996 | return; |
| 4997 | case CXPrintingPolicy_SuppressImplicitBase: |
| 4998 | P->SuppressImplicitBase = Value; |
| 4999 | return; |
| 5000 | case CXPrintingPolicy_FullyQualifiedName: |
| 5001 | P->FullyQualifiedName = Value; |
| 5002 | return; |
| 5003 | } |
| 5004 | |
| 5005 | assert(false && "Invalid CXPrintingPolicyProperty"); |
| 5006 | } |
| 5007 | |
| 5008 | CXString clang_getCursorPrettyPrinted(CXCursor C, CXPrintingPolicy cxPolicy) { |
| 5009 | if (clang_Cursor_isNull(C)) |
| 5010 | return cxstring::createEmpty(); |
| 5011 | |
| 5012 | if (clang_isDeclaration(C.kind)) { |
| 5013 | const Decl *D = getCursorDecl(C); |
| 5014 | if (!D) |
| 5015 | return cxstring::createEmpty(); |
| 5016 | |
| 5017 | SmallString<128> Str; |
| 5018 | llvm::raw_svector_ostream OS(Str); |
| 5019 | PrintingPolicy *UserPolicy = static_cast<PrintingPolicy *>(cxPolicy); |
| 5020 | D->print(OS, UserPolicy ? *UserPolicy |
| 5021 | : getCursorContext(C).getPrintingPolicy()); |
| 5022 | |
| 5023 | return cxstring::createDup(OS.str()); |
| 5024 | } |
| 5025 | |
| 5026 | return cxstring::createEmpty(); |
| 5027 | } |
| 5028 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5029 | CXString clang_getCursorDisplayName(CXCursor C) { |
| 5030 | if (!clang_isDeclaration(C.kind)) |
| 5031 | return clang_getCursorSpelling(C); |
| 5032 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5033 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5034 | if (!D) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 5035 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5036 | |
| 5037 | PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5038 | if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5039 | D = FunTmpl->getTemplatedDecl(); |
| 5040 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5041 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5042 | SmallString<64> Str; |
| 5043 | llvm::raw_svector_ostream OS(Str); |
| 5044 | OS << *Function; |
| 5045 | if (Function->getPrimaryTemplate()) |
| 5046 | OS << "<>"; |
| 5047 | OS << "("; |
| 5048 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { |
| 5049 | if (I) |
| 5050 | OS << ", "; |
| 5051 | OS << Function->getParamDecl(I)->getType().getAsString(Policy); |
| 5052 | } |
| 5053 | |
| 5054 | if (Function->isVariadic()) { |
| 5055 | if (Function->getNumParams()) |
| 5056 | OS << ", "; |
| 5057 | OS << "..."; |
| 5058 | } |
| 5059 | OS << ")"; |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 5060 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5061 | } |
| 5062 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5063 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5064 | SmallString<64> Str; |
| 5065 | llvm::raw_svector_ostream OS(Str); |
| 5066 | OS << *ClassTemplate; |
| 5067 | OS << "<"; |
| 5068 | TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); |
| 5069 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 5070 | if (I) |
| 5071 | OS << ", "; |
| 5072 | |
| 5073 | NamedDecl *Param = Params->getParam(I); |
| 5074 | if (Param->getIdentifier()) { |
| 5075 | OS << Param->getIdentifier()->getName(); |
| 5076 | continue; |
| 5077 | } |
| 5078 | |
| 5079 | // There is no parameter name, which makes this tricky. Try to come up |
| 5080 | // with something useful that isn't too long. |
| 5081 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Saar Raz | ff1e0fc | 2020-01-15 02:48:42 +0200 | [diff] [blame] | 5082 | if (const auto *TC = TTP->getTypeConstraint()) { |
| 5083 | TC->getConceptNameInfo().printName(OS, Policy); |
| 5084 | if (TC->hasExplicitTemplateArgs()) |
| 5085 | OS << "<...>"; |
| 5086 | } else |
| 5087 | OS << (TTP->wasDeclaredWithTypename()? "typename" : "class"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5088 | else if (NonTypeTemplateParmDecl *NTTP |
| 5089 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 5090 | OS << NTTP->getType().getAsString(Policy); |
| 5091 | else |
| 5092 | OS << "template<...> class"; |
| 5093 | } |
| 5094 | |
| 5095 | OS << ">"; |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 5096 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5097 | } |
| 5098 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5099 | if (const ClassTemplateSpecializationDecl *ClassSpec |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5100 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 5101 | // If the type was explicitly written, use that. |
| 5102 | if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 5103 | return cxstring::createDup(TSInfo->getType().getAsString(Policy)); |
Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame] | 5104 | |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 5105 | SmallString<128> Str; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5106 | llvm::raw_svector_ostream OS(Str); |
| 5107 | OS << *ClassSpec; |
Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame] | 5108 | printTemplateArgumentList(OS, ClassSpec->getTemplateArgs().asArray(), |
| 5109 | Policy); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 5110 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5111 | } |
| 5112 | |
| 5113 | return clang_getCursorSpelling(C); |
| 5114 | } |
| 5115 | |
| 5116 | CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
| 5117 | switch (Kind) { |
| 5118 | case CXCursor_FunctionDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5119 | return cxstring::createRef("FunctionDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5120 | case CXCursor_TypedefDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5121 | return cxstring::createRef("TypedefDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5122 | case CXCursor_EnumDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5123 | return cxstring::createRef("EnumDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5124 | case CXCursor_EnumConstantDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5125 | return cxstring::createRef("EnumConstantDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5126 | case CXCursor_StructDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5127 | return cxstring::createRef("StructDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5128 | case CXCursor_UnionDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5129 | return cxstring::createRef("UnionDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5130 | case CXCursor_ClassDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5131 | return cxstring::createRef("ClassDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5132 | case CXCursor_FieldDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5133 | return cxstring::createRef("FieldDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5134 | case CXCursor_VarDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5135 | return cxstring::createRef("VarDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5136 | case CXCursor_ParmDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5137 | return cxstring::createRef("ParmDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5138 | case CXCursor_ObjCInterfaceDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5139 | return cxstring::createRef("ObjCInterfaceDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5140 | case CXCursor_ObjCCategoryDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5141 | return cxstring::createRef("ObjCCategoryDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5142 | case CXCursor_ObjCProtocolDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5143 | return cxstring::createRef("ObjCProtocolDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5144 | case CXCursor_ObjCPropertyDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5145 | return cxstring::createRef("ObjCPropertyDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5146 | case CXCursor_ObjCIvarDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5147 | return cxstring::createRef("ObjCIvarDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5148 | case CXCursor_ObjCInstanceMethodDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5149 | return cxstring::createRef("ObjCInstanceMethodDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5150 | case CXCursor_ObjCClassMethodDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5151 | return cxstring::createRef("ObjCClassMethodDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5152 | case CXCursor_ObjCImplementationDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5153 | return cxstring::createRef("ObjCImplementationDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5154 | case CXCursor_ObjCCategoryImplDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5155 | return cxstring::createRef("ObjCCategoryImplDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5156 | case CXCursor_CXXMethod: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5157 | return cxstring::createRef("CXXMethod"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5158 | case CXCursor_UnexposedDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5159 | return cxstring::createRef("UnexposedDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5160 | case CXCursor_ObjCSuperClassRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5161 | return cxstring::createRef("ObjCSuperClassRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5162 | case CXCursor_ObjCProtocolRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5163 | return cxstring::createRef("ObjCProtocolRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5164 | case CXCursor_ObjCClassRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5165 | return cxstring::createRef("ObjCClassRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5166 | case CXCursor_TypeRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5167 | return cxstring::createRef("TypeRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5168 | case CXCursor_TemplateRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5169 | return cxstring::createRef("TemplateRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5170 | case CXCursor_NamespaceRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5171 | return cxstring::createRef("NamespaceRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5172 | case CXCursor_MemberRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5173 | return cxstring::createRef("MemberRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5174 | case CXCursor_LabelRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5175 | return cxstring::createRef("LabelRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5176 | case CXCursor_OverloadedDeclRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5177 | return cxstring::createRef("OverloadedDeclRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5178 | case CXCursor_VariableRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5179 | return cxstring::createRef("VariableRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5180 | case CXCursor_IntegerLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5181 | return cxstring::createRef("IntegerLiteral"); |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 5182 | case CXCursor_FixedPointLiteral: |
| 5183 | return cxstring::createRef("FixedPointLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5184 | case CXCursor_FloatingLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5185 | return cxstring::createRef("FloatingLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5186 | case CXCursor_ImaginaryLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5187 | return cxstring::createRef("ImaginaryLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5188 | case CXCursor_StringLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5189 | return cxstring::createRef("StringLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5190 | case CXCursor_CharacterLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5191 | return cxstring::createRef("CharacterLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5192 | case CXCursor_ParenExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5193 | return cxstring::createRef("ParenExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5194 | case CXCursor_UnaryOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5195 | return cxstring::createRef("UnaryOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5196 | case CXCursor_ArraySubscriptExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5197 | return cxstring::createRef("ArraySubscriptExpr"); |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 5198 | case CXCursor_OMPArraySectionExpr: |
| 5199 | return cxstring::createRef("OMPArraySectionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5200 | case CXCursor_BinaryOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5201 | return cxstring::createRef("BinaryOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5202 | case CXCursor_CompoundAssignOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5203 | return cxstring::createRef("CompoundAssignOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5204 | case CXCursor_ConditionalOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5205 | return cxstring::createRef("ConditionalOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5206 | case CXCursor_CStyleCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5207 | return cxstring::createRef("CStyleCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5208 | case CXCursor_CompoundLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5209 | return cxstring::createRef("CompoundLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5210 | case CXCursor_InitListExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5211 | return cxstring::createRef("InitListExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5212 | case CXCursor_AddrLabelExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5213 | return cxstring::createRef("AddrLabelExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5214 | case CXCursor_StmtExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5215 | return cxstring::createRef("StmtExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5216 | case CXCursor_GenericSelectionExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5217 | return cxstring::createRef("GenericSelectionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5218 | case CXCursor_GNUNullExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5219 | return cxstring::createRef("GNUNullExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5220 | case CXCursor_CXXStaticCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5221 | return cxstring::createRef("CXXStaticCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5222 | case CXCursor_CXXDynamicCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5223 | return cxstring::createRef("CXXDynamicCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5224 | case CXCursor_CXXReinterpretCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5225 | return cxstring::createRef("CXXReinterpretCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5226 | case CXCursor_CXXConstCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5227 | return cxstring::createRef("CXXConstCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5228 | case CXCursor_CXXFunctionalCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5229 | return cxstring::createRef("CXXFunctionalCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5230 | case CXCursor_CXXTypeidExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5231 | return cxstring::createRef("CXXTypeidExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5232 | case CXCursor_CXXBoolLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5233 | return cxstring::createRef("CXXBoolLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5234 | case CXCursor_CXXNullPtrLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5235 | return cxstring::createRef("CXXNullPtrLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5236 | case CXCursor_CXXThisExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5237 | return cxstring::createRef("CXXThisExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5238 | case CXCursor_CXXThrowExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5239 | return cxstring::createRef("CXXThrowExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5240 | case CXCursor_CXXNewExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5241 | return cxstring::createRef("CXXNewExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5242 | case CXCursor_CXXDeleteExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5243 | return cxstring::createRef("CXXDeleteExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5244 | case CXCursor_UnaryExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5245 | return cxstring::createRef("UnaryExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5246 | case CXCursor_ObjCStringLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5247 | return cxstring::createRef("ObjCStringLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5248 | case CXCursor_ObjCBoolLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5249 | return cxstring::createRef("ObjCBoolLiteralExpr"); |
Erik Pilkington | 29099de | 2016-07-16 00:35:23 +0000 | [diff] [blame] | 5250 | case CXCursor_ObjCAvailabilityCheckExpr: |
| 5251 | return cxstring::createRef("ObjCAvailabilityCheckExpr"); |
Argyrios Kyrtzidis | c2233be | 2013-04-23 17:57:17 +0000 | [diff] [blame] | 5252 | case CXCursor_ObjCSelfExpr: |
| 5253 | return cxstring::createRef("ObjCSelfExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5254 | case CXCursor_ObjCEncodeExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5255 | return cxstring::createRef("ObjCEncodeExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5256 | case CXCursor_ObjCSelectorExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5257 | return cxstring::createRef("ObjCSelectorExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5258 | case CXCursor_ObjCProtocolExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5259 | return cxstring::createRef("ObjCProtocolExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5260 | case CXCursor_ObjCBridgedCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5261 | return cxstring::createRef("ObjCBridgedCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5262 | case CXCursor_BlockExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5263 | return cxstring::createRef("BlockExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5264 | case CXCursor_PackExpansionExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5265 | return cxstring::createRef("PackExpansionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5266 | case CXCursor_SizeOfPackExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5267 | return cxstring::createRef("SizeOfPackExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5268 | case CXCursor_LambdaExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5269 | return cxstring::createRef("LambdaExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5270 | case CXCursor_UnexposedExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5271 | return cxstring::createRef("UnexposedExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5272 | case CXCursor_DeclRefExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5273 | return cxstring::createRef("DeclRefExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5274 | case CXCursor_MemberRefExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5275 | return cxstring::createRef("MemberRefExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5276 | case CXCursor_CallExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5277 | return cxstring::createRef("CallExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5278 | case CXCursor_ObjCMessageExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5279 | return cxstring::createRef("ObjCMessageExpr"); |
Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 5280 | case CXCursor_BuiltinBitCastExpr: |
| 5281 | return cxstring::createRef("BuiltinBitCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5282 | case CXCursor_UnexposedStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5283 | return cxstring::createRef("UnexposedStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5284 | case CXCursor_DeclStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5285 | return cxstring::createRef("DeclStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5286 | case CXCursor_LabelStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5287 | return cxstring::createRef("LabelStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5288 | case CXCursor_CompoundStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5289 | return cxstring::createRef("CompoundStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5290 | case CXCursor_CaseStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5291 | return cxstring::createRef("CaseStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5292 | case CXCursor_DefaultStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5293 | return cxstring::createRef("DefaultStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5294 | case CXCursor_IfStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5295 | return cxstring::createRef("IfStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5296 | case CXCursor_SwitchStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5297 | return cxstring::createRef("SwitchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5298 | case CXCursor_WhileStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5299 | return cxstring::createRef("WhileStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5300 | case CXCursor_DoStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5301 | return cxstring::createRef("DoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5302 | case CXCursor_ForStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5303 | return cxstring::createRef("ForStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5304 | case CXCursor_GotoStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5305 | return cxstring::createRef("GotoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5306 | case CXCursor_IndirectGotoStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5307 | return cxstring::createRef("IndirectGotoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5308 | case CXCursor_ContinueStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5309 | return cxstring::createRef("ContinueStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5310 | case CXCursor_BreakStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5311 | return cxstring::createRef("BreakStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5312 | case CXCursor_ReturnStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5313 | return cxstring::createRef("ReturnStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5314 | case CXCursor_GCCAsmStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5315 | return cxstring::createRef("GCCAsmStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5316 | case CXCursor_MSAsmStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5317 | return cxstring::createRef("MSAsmStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5318 | case CXCursor_ObjCAtTryStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5319 | return cxstring::createRef("ObjCAtTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5320 | case CXCursor_ObjCAtCatchStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5321 | return cxstring::createRef("ObjCAtCatchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5322 | case CXCursor_ObjCAtFinallyStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5323 | return cxstring::createRef("ObjCAtFinallyStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5324 | case CXCursor_ObjCAtThrowStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5325 | return cxstring::createRef("ObjCAtThrowStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5326 | case CXCursor_ObjCAtSynchronizedStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5327 | return cxstring::createRef("ObjCAtSynchronizedStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5328 | case CXCursor_ObjCAutoreleasePoolStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5329 | return cxstring::createRef("ObjCAutoreleasePoolStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5330 | case CXCursor_ObjCForCollectionStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5331 | return cxstring::createRef("ObjCForCollectionStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5332 | case CXCursor_CXXCatchStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5333 | return cxstring::createRef("CXXCatchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5334 | case CXCursor_CXXTryStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5335 | return cxstring::createRef("CXXTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5336 | case CXCursor_CXXForRangeStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5337 | return cxstring::createRef("CXXForRangeStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5338 | case CXCursor_SEHTryStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5339 | return cxstring::createRef("SEHTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5340 | case CXCursor_SEHExceptStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5341 | return cxstring::createRef("SEHExceptStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5342 | case CXCursor_SEHFinallyStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5343 | return cxstring::createRef("SEHFinallyStmt"); |
Nico Weber | 9b98207 | 2014-07-07 00:12:30 +0000 | [diff] [blame] | 5344 | case CXCursor_SEHLeaveStmt: |
| 5345 | return cxstring::createRef("SEHLeaveStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5346 | case CXCursor_NullStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5347 | return cxstring::createRef("NullStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5348 | case CXCursor_InvalidFile: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5349 | return cxstring::createRef("InvalidFile"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5350 | case CXCursor_InvalidCode: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5351 | return cxstring::createRef("InvalidCode"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5352 | case CXCursor_NoDeclFound: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5353 | return cxstring::createRef("NoDeclFound"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5354 | case CXCursor_NotImplemented: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5355 | return cxstring::createRef("NotImplemented"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5356 | case CXCursor_TranslationUnit: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5357 | return cxstring::createRef("TranslationUnit"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5358 | case CXCursor_UnexposedAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5359 | return cxstring::createRef("UnexposedAttr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5360 | case CXCursor_IBActionAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5361 | return cxstring::createRef("attribute(ibaction)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5362 | case CXCursor_IBOutletAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5363 | return cxstring::createRef("attribute(iboutlet)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5364 | case CXCursor_IBOutletCollectionAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5365 | return cxstring::createRef("attribute(iboutletcollection)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5366 | case CXCursor_CXXFinalAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5367 | return cxstring::createRef("attribute(final)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5368 | case CXCursor_CXXOverrideAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5369 | return cxstring::createRef("attribute(override)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5370 | case CXCursor_AnnotateAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5371 | return cxstring::createRef("attribute(annotate)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5372 | case CXCursor_AsmLabelAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5373 | return cxstring::createRef("asm label"); |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 5374 | case CXCursor_PackedAttr: |
| 5375 | return cxstring::createRef("attribute(packed)"); |
Joey Gouly | 8122838 | 2014-05-01 15:41:58 +0000 | [diff] [blame] | 5376 | case CXCursor_PureAttr: |
| 5377 | return cxstring::createRef("attribute(pure)"); |
| 5378 | case CXCursor_ConstAttr: |
| 5379 | return cxstring::createRef("attribute(const)"); |
| 5380 | case CXCursor_NoDuplicateAttr: |
| 5381 | return cxstring::createRef("attribute(noduplicate)"); |
Eli Bendersky | 2581e66 | 2014-05-28 19:29:58 +0000 | [diff] [blame] | 5382 | case CXCursor_CUDAConstantAttr: |
| 5383 | return cxstring::createRef("attribute(constant)"); |
| 5384 | case CXCursor_CUDADeviceAttr: |
| 5385 | return cxstring::createRef("attribute(device)"); |
| 5386 | case CXCursor_CUDAGlobalAttr: |
| 5387 | return cxstring::createRef("attribute(global)"); |
| 5388 | case CXCursor_CUDAHostAttr: |
| 5389 | return cxstring::createRef("attribute(host)"); |
Eli Bendersky | 9b07147 | 2014-08-08 14:59:00 +0000 | [diff] [blame] | 5390 | case CXCursor_CUDASharedAttr: |
| 5391 | return cxstring::createRef("attribute(shared)"); |
Saleem Abdulrasool | 79c6971 | 2015-09-05 18:53:43 +0000 | [diff] [blame] | 5392 | case CXCursor_VisibilityAttr: |
| 5393 | return cxstring::createRef("attribute(visibility)"); |
Saleem Abdulrasool | 8aa0b80 | 2015-12-10 18:45:18 +0000 | [diff] [blame] | 5394 | case CXCursor_DLLExport: |
| 5395 | return cxstring::createRef("attribute(dllexport)"); |
| 5396 | case CXCursor_DLLImport: |
| 5397 | return cxstring::createRef("attribute(dllimport)"); |
Michael Wu | d092d0b | 2018-08-03 05:03:22 +0000 | [diff] [blame] | 5398 | case CXCursor_NSReturnsRetained: |
| 5399 | return cxstring::createRef("attribute(ns_returns_retained)"); |
| 5400 | case CXCursor_NSReturnsNotRetained: |
| 5401 | return cxstring::createRef("attribute(ns_returns_not_retained)"); |
| 5402 | case CXCursor_NSReturnsAutoreleased: |
| 5403 | return cxstring::createRef("attribute(ns_returns_autoreleased)"); |
| 5404 | case CXCursor_NSConsumesSelf: |
| 5405 | return cxstring::createRef("attribute(ns_consumes_self)"); |
| 5406 | case CXCursor_NSConsumed: |
| 5407 | return cxstring::createRef("attribute(ns_consumed)"); |
| 5408 | case CXCursor_ObjCException: |
| 5409 | return cxstring::createRef("attribute(objc_exception)"); |
| 5410 | case CXCursor_ObjCNSObject: |
| 5411 | return cxstring::createRef("attribute(NSObject)"); |
| 5412 | case CXCursor_ObjCIndependentClass: |
| 5413 | return cxstring::createRef("attribute(objc_independent_class)"); |
| 5414 | case CXCursor_ObjCPreciseLifetime: |
| 5415 | return cxstring::createRef("attribute(objc_precise_lifetime)"); |
| 5416 | case CXCursor_ObjCReturnsInnerPointer: |
| 5417 | return cxstring::createRef("attribute(objc_returns_inner_pointer)"); |
| 5418 | case CXCursor_ObjCRequiresSuper: |
| 5419 | return cxstring::createRef("attribute(objc_requires_super)"); |
| 5420 | case CXCursor_ObjCRootClass: |
| 5421 | return cxstring::createRef("attribute(objc_root_class)"); |
| 5422 | case CXCursor_ObjCSubclassingRestricted: |
| 5423 | return cxstring::createRef("attribute(objc_subclassing_restricted)"); |
| 5424 | case CXCursor_ObjCExplicitProtocolImpl: |
| 5425 | return cxstring::createRef("attribute(objc_protocol_requires_explicit_implementation)"); |
| 5426 | case CXCursor_ObjCDesignatedInitializer: |
| 5427 | return cxstring::createRef("attribute(objc_designated_initializer)"); |
| 5428 | case CXCursor_ObjCRuntimeVisible: |
| 5429 | return cxstring::createRef("attribute(objc_runtime_visible)"); |
| 5430 | case CXCursor_ObjCBoxable: |
| 5431 | return cxstring::createRef("attribute(objc_boxable)"); |
Michael Wu | 58d837d | 2018-08-03 05:55:40 +0000 | [diff] [blame] | 5432 | case CXCursor_FlagEnum: |
| 5433 | return cxstring::createRef("attribute(flag_enum)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5434 | case CXCursor_PreprocessingDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5435 | return cxstring::createRef("preprocessing directive"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5436 | case CXCursor_MacroDefinition: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5437 | return cxstring::createRef("macro definition"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5438 | case CXCursor_MacroExpansion: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5439 | return cxstring::createRef("macro expansion"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5440 | case CXCursor_InclusionDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5441 | return cxstring::createRef("inclusion directive"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5442 | case CXCursor_Namespace: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5443 | return cxstring::createRef("Namespace"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5444 | case CXCursor_LinkageSpec: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5445 | return cxstring::createRef("LinkageSpec"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5446 | case CXCursor_CXXBaseSpecifier: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5447 | return cxstring::createRef("C++ base class specifier"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5448 | case CXCursor_Constructor: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5449 | return cxstring::createRef("CXXConstructor"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5450 | case CXCursor_Destructor: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5451 | return cxstring::createRef("CXXDestructor"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5452 | case CXCursor_ConversionFunction: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5453 | return cxstring::createRef("CXXConversion"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5454 | case CXCursor_TemplateTypeParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5455 | return cxstring::createRef("TemplateTypeParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5456 | case CXCursor_NonTypeTemplateParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5457 | return cxstring::createRef("NonTypeTemplateParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5458 | case CXCursor_TemplateTemplateParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5459 | return cxstring::createRef("TemplateTemplateParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5460 | case CXCursor_FunctionTemplate: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5461 | return cxstring::createRef("FunctionTemplate"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5462 | case CXCursor_ClassTemplate: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5463 | return cxstring::createRef("ClassTemplate"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5464 | case CXCursor_ClassTemplatePartialSpecialization: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5465 | return cxstring::createRef("ClassTemplatePartialSpecialization"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5466 | case CXCursor_NamespaceAlias: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5467 | return cxstring::createRef("NamespaceAlias"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5468 | case CXCursor_UsingDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5469 | return cxstring::createRef("UsingDirective"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5470 | case CXCursor_UsingDeclaration: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5471 | return cxstring::createRef("UsingDeclaration"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5472 | case CXCursor_TypeAliasDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5473 | return cxstring::createRef("TypeAliasDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5474 | case CXCursor_ObjCSynthesizeDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5475 | return cxstring::createRef("ObjCSynthesizeDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5476 | case CXCursor_ObjCDynamicDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5477 | return cxstring::createRef("ObjCDynamicDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5478 | case CXCursor_CXXAccessSpecifier: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5479 | return cxstring::createRef("CXXAccessSpecifier"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5480 | case CXCursor_ModuleImportDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5481 | return cxstring::createRef("ModuleImport"); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 5482 | case CXCursor_OMPParallelDirective: |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 5483 | return cxstring::createRef("OMPParallelDirective"); |
| 5484 | case CXCursor_OMPSimdDirective: |
| 5485 | return cxstring::createRef("OMPSimdDirective"); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5486 | case CXCursor_OMPForDirective: |
| 5487 | return cxstring::createRef("OMPForDirective"); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 5488 | case CXCursor_OMPForSimdDirective: |
| 5489 | return cxstring::createRef("OMPForSimdDirective"); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 5490 | case CXCursor_OMPSectionsDirective: |
| 5491 | return cxstring::createRef("OMPSectionsDirective"); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 5492 | case CXCursor_OMPSectionDirective: |
| 5493 | return cxstring::createRef("OMPSectionDirective"); |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 5494 | case CXCursor_OMPSingleDirective: |
| 5495 | return cxstring::createRef("OMPSingleDirective"); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 5496 | case CXCursor_OMPMasterDirective: |
| 5497 | return cxstring::createRef("OMPMasterDirective"); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 5498 | case CXCursor_OMPCriticalDirective: |
| 5499 | return cxstring::createRef("OMPCriticalDirective"); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 5500 | case CXCursor_OMPParallelForDirective: |
| 5501 | return cxstring::createRef("OMPParallelForDirective"); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 5502 | case CXCursor_OMPParallelForSimdDirective: |
| 5503 | return cxstring::createRef("OMPParallelForSimdDirective"); |
cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 5504 | case CXCursor_OMPParallelMasterDirective: |
| 5505 | return cxstring::createRef("OMPParallelMasterDirective"); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 5506 | case CXCursor_OMPParallelSectionsDirective: |
| 5507 | return cxstring::createRef("OMPParallelSectionsDirective"); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5508 | case CXCursor_OMPTaskDirective: |
| 5509 | return cxstring::createRef("OMPTaskDirective"); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 5510 | case CXCursor_OMPTaskyieldDirective: |
| 5511 | return cxstring::createRef("OMPTaskyieldDirective"); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 5512 | case CXCursor_OMPBarrierDirective: |
| 5513 | return cxstring::createRef("OMPBarrierDirective"); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 5514 | case CXCursor_OMPTaskwaitDirective: |
| 5515 | return cxstring::createRef("OMPTaskwaitDirective"); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 5516 | case CXCursor_OMPTaskgroupDirective: |
| 5517 | return cxstring::createRef("OMPTaskgroupDirective"); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 5518 | case CXCursor_OMPFlushDirective: |
| 5519 | return cxstring::createRef("OMPFlushDirective"); |
Alexey Bataev | c112e94 | 2020-02-28 09:52:15 -0500 | [diff] [blame] | 5520 | case CXCursor_OMPDepobjDirective: |
| 5521 | return cxstring::createRef("OMPDepobjDirective"); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 5522 | case CXCursor_OMPOrderedDirective: |
| 5523 | return cxstring::createRef("OMPOrderedDirective"); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 5524 | case CXCursor_OMPAtomicDirective: |
| 5525 | return cxstring::createRef("OMPAtomicDirective"); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 5526 | case CXCursor_OMPTargetDirective: |
| 5527 | return cxstring::createRef("OMPTargetDirective"); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 5528 | case CXCursor_OMPTargetDataDirective: |
| 5529 | return cxstring::createRef("OMPTargetDataDirective"); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 5530 | case CXCursor_OMPTargetEnterDataDirective: |
| 5531 | return cxstring::createRef("OMPTargetEnterDataDirective"); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 5532 | case CXCursor_OMPTargetExitDataDirective: |
| 5533 | return cxstring::createRef("OMPTargetExitDataDirective"); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 5534 | case CXCursor_OMPTargetParallelDirective: |
| 5535 | return cxstring::createRef("OMPTargetParallelDirective"); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 5536 | case CXCursor_OMPTargetParallelForDirective: |
| 5537 | return cxstring::createRef("OMPTargetParallelForDirective"); |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 5538 | case CXCursor_OMPTargetUpdateDirective: |
| 5539 | return cxstring::createRef("OMPTargetUpdateDirective"); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 5540 | case CXCursor_OMPTeamsDirective: |
| 5541 | return cxstring::createRef("OMPTeamsDirective"); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 5542 | case CXCursor_OMPCancellationPointDirective: |
| 5543 | return cxstring::createRef("OMPCancellationPointDirective"); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 5544 | case CXCursor_OMPCancelDirective: |
| 5545 | return cxstring::createRef("OMPCancelDirective"); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 5546 | case CXCursor_OMPTaskLoopDirective: |
| 5547 | return cxstring::createRef("OMPTaskLoopDirective"); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 5548 | case CXCursor_OMPTaskLoopSimdDirective: |
| 5549 | return cxstring::createRef("OMPTaskLoopSimdDirective"); |
Alexey Bataev | 60e51c4 | 2019-10-10 20:13:02 +0000 | [diff] [blame] | 5550 | case CXCursor_OMPMasterTaskLoopDirective: |
| 5551 | return cxstring::createRef("OMPMasterTaskLoopDirective"); |
Alexey Bataev | b8552ab | 2019-10-18 16:47:35 +0000 | [diff] [blame] | 5552 | case CXCursor_OMPMasterTaskLoopSimdDirective: |
| 5553 | return cxstring::createRef("OMPMasterTaskLoopSimdDirective"); |
Alexey Bataev | 5bbcead | 2019-10-14 17:17:41 +0000 | [diff] [blame] | 5554 | case CXCursor_OMPParallelMasterTaskLoopDirective: |
| 5555 | return cxstring::createRef("OMPParallelMasterTaskLoopDirective"); |
Alexey Bataev | 14a388f | 2019-10-25 10:27:13 -0400 | [diff] [blame] | 5556 | case CXCursor_OMPParallelMasterTaskLoopSimdDirective: |
| 5557 | return cxstring::createRef("OMPParallelMasterTaskLoopSimdDirective"); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 5558 | case CXCursor_OMPDistributeDirective: |
| 5559 | return cxstring::createRef("OMPDistributeDirective"); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 5560 | case CXCursor_OMPDistributeParallelForDirective: |
| 5561 | return cxstring::createRef("OMPDistributeParallelForDirective"); |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 5562 | case CXCursor_OMPDistributeParallelForSimdDirective: |
| 5563 | return cxstring::createRef("OMPDistributeParallelForSimdDirective"); |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 5564 | case CXCursor_OMPDistributeSimdDirective: |
| 5565 | return cxstring::createRef("OMPDistributeSimdDirective"); |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 5566 | case CXCursor_OMPTargetParallelForSimdDirective: |
| 5567 | return cxstring::createRef("OMPTargetParallelForSimdDirective"); |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 5568 | case CXCursor_OMPTargetSimdDirective: |
| 5569 | return cxstring::createRef("OMPTargetSimdDirective"); |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 5570 | case CXCursor_OMPTeamsDistributeDirective: |
| 5571 | return cxstring::createRef("OMPTeamsDistributeDirective"); |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 5572 | case CXCursor_OMPTeamsDistributeSimdDirective: |
| 5573 | return cxstring::createRef("OMPTeamsDistributeSimdDirective"); |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 5574 | case CXCursor_OMPTeamsDistributeParallelForSimdDirective: |
| 5575 | return cxstring::createRef("OMPTeamsDistributeParallelForSimdDirective"); |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 5576 | case CXCursor_OMPTeamsDistributeParallelForDirective: |
| 5577 | return cxstring::createRef("OMPTeamsDistributeParallelForDirective"); |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 5578 | case CXCursor_OMPTargetTeamsDirective: |
| 5579 | return cxstring::createRef("OMPTargetTeamsDirective"); |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 5580 | case CXCursor_OMPTargetTeamsDistributeDirective: |
| 5581 | return cxstring::createRef("OMPTargetTeamsDistributeDirective"); |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 5582 | case CXCursor_OMPTargetTeamsDistributeParallelForDirective: |
| 5583 | return cxstring::createRef("OMPTargetTeamsDistributeParallelForDirective"); |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 5584 | case CXCursor_OMPTargetTeamsDistributeParallelForSimdDirective: |
| 5585 | return cxstring::createRef( |
| 5586 | "OMPTargetTeamsDistributeParallelForSimdDirective"); |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 5587 | case CXCursor_OMPTargetTeamsDistributeSimdDirective: |
| 5588 | return cxstring::createRef("OMPTargetTeamsDistributeSimdDirective"); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5589 | case CXCursor_OverloadCandidate: |
| 5590 | return cxstring::createRef("OverloadCandidate"); |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 5591 | case CXCursor_TypeAliasTemplateDecl: |
| 5592 | return cxstring::createRef("TypeAliasTemplateDecl"); |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 5593 | case CXCursor_StaticAssert: |
| 5594 | return cxstring::createRef("StaticAssert"); |
Olivier Goffart | d211c64 | 2016-11-04 06:29:27 +0000 | [diff] [blame] | 5595 | case CXCursor_FriendDecl: |
Sven van Haastregt | dc2c930 | 2019-02-11 11:00:56 +0000 | [diff] [blame] | 5596 | return cxstring::createRef("FriendDecl"); |
| 5597 | case CXCursor_ConvergentAttr: |
| 5598 | return cxstring::createRef("attribute(convergent)"); |
Emilio Cobos Alvarez | 0a3fe50 | 2019-02-25 21:24:52 +0000 | [diff] [blame] | 5599 | case CXCursor_WarnUnusedAttr: |
| 5600 | return cxstring::createRef("attribute(warn_unused)"); |
| 5601 | case CXCursor_WarnUnusedResultAttr: |
| 5602 | return cxstring::createRef("attribute(warn_unused_result)"); |
Emilio Cobos Alvarez | cd74127 | 2019-03-13 16:16:54 +0000 | [diff] [blame] | 5603 | case CXCursor_AlignedAttr: |
| 5604 | return cxstring::createRef("attribute(aligned)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5605 | } |
| 5606 | |
| 5607 | llvm_unreachable("Unhandled CXCursorKind"); |
| 5608 | } |
| 5609 | |
| 5610 | struct GetCursorData { |
| 5611 | SourceLocation TokenBeginLoc; |
| 5612 | bool PointsAtMacroArgExpansion; |
| 5613 | bool VisitedObjCPropertyImplDecl; |
| 5614 | SourceLocation VisitedDeclaratorDeclStartLoc; |
| 5615 | CXCursor &BestCursor; |
| 5616 | |
| 5617 | GetCursorData(SourceManager &SM, |
| 5618 | SourceLocation tokenBegin, CXCursor &outputCursor) |
| 5619 | : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) { |
| 5620 | PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin); |
| 5621 | VisitedObjCPropertyImplDecl = false; |
| 5622 | } |
| 5623 | }; |
| 5624 | |
| 5625 | static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, |
| 5626 | CXCursor parent, |
| 5627 | CXClientData client_data) { |
| 5628 | GetCursorData *Data = static_cast<GetCursorData *>(client_data); |
| 5629 | CXCursor *BestCursor = &Data->BestCursor; |
| 5630 | |
| 5631 | // If we point inside a macro argument we should provide info of what the |
| 5632 | // token is so use the actual cursor, don't replace it with a macro expansion |
| 5633 | // cursor. |
| 5634 | if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion) |
| 5635 | return CXChildVisit_Recurse; |
| 5636 | |
| 5637 | if (clang_isDeclaration(cursor.kind)) { |
| 5638 | // Avoid having the implicit methods override the property decls. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5639 | if (const ObjCMethodDecl *MD |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5640 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 5641 | if (MD->isImplicit()) |
| 5642 | return CXChildVisit_Break; |
| 5643 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5644 | } else if (const ObjCInterfaceDecl *ID |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5645 | = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(cursor))) { |
| 5646 | // Check that when we have multiple @class references in the same line, |
| 5647 | // that later ones do not override the previous ones. |
| 5648 | // If we have: |
| 5649 | // @class Foo, Bar; |
| 5650 | // source ranges for both start at '@', so 'Bar' will end up overriding |
| 5651 | // 'Foo' even though the cursor location was at 'Foo'. |
| 5652 | if (BestCursor->kind == CXCursor_ObjCInterfaceDecl || |
| 5653 | BestCursor->kind == CXCursor_ObjCClassRef) |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5654 | if (const ObjCInterfaceDecl *PrevID |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5655 | = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(*BestCursor))){ |
| 5656 | if (PrevID != ID && |
| 5657 | !PrevID->isThisDeclarationADefinition() && |
| 5658 | !ID->isThisDeclarationADefinition()) |
| 5659 | return CXChildVisit_Break; |
| 5660 | } |
| 5661 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5662 | } else if (const DeclaratorDecl *DD |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5663 | = dyn_cast_or_null<DeclaratorDecl>(getCursorDecl(cursor))) { |
| 5664 | SourceLocation StartLoc = DD->getSourceRange().getBegin(); |
| 5665 | // Check that when we have multiple declarators in the same line, |
| 5666 | // that later ones do not override the previous ones. |
| 5667 | // If we have: |
| 5668 | // int Foo, Bar; |
| 5669 | // source ranges for both start at 'int', so 'Bar' will end up overriding |
| 5670 | // 'Foo' even though the cursor location was at 'Foo'. |
| 5671 | if (Data->VisitedDeclaratorDeclStartLoc == StartLoc) |
| 5672 | return CXChildVisit_Break; |
| 5673 | Data->VisitedDeclaratorDeclStartLoc = StartLoc; |
| 5674 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5675 | } else if (const ObjCPropertyImplDecl *PropImp |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5676 | = dyn_cast_or_null<ObjCPropertyImplDecl>(getCursorDecl(cursor))) { |
| 5677 | (void)PropImp; |
| 5678 | // Check that when we have multiple @synthesize in the same line, |
| 5679 | // that later ones do not override the previous ones. |
| 5680 | // If we have: |
| 5681 | // @synthesize Foo, Bar; |
| 5682 | // source ranges for both start at '@', so 'Bar' will end up overriding |
| 5683 | // 'Foo' even though the cursor location was at 'Foo'. |
| 5684 | if (Data->VisitedObjCPropertyImplDecl) |
| 5685 | return CXChildVisit_Break; |
| 5686 | Data->VisitedObjCPropertyImplDecl = true; |
| 5687 | } |
| 5688 | } |
| 5689 | |
| 5690 | if (clang_isExpression(cursor.kind) && |
| 5691 | clang_isDeclaration(BestCursor->kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5692 | if (const Decl *D = getCursorDecl(*BestCursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5693 | // Avoid having the cursor of an expression replace the declaration cursor |
| 5694 | // when the expression source range overlaps the declaration range. |
| 5695 | // This can happen for C++ constructor expressions whose range generally |
| 5696 | // include the variable declaration, e.g.: |
| 5697 | // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor. |
| 5698 | if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() && |
| 5699 | D->getLocation() == Data->TokenBeginLoc) |
| 5700 | return CXChildVisit_Break; |
| 5701 | } |
| 5702 | } |
| 5703 | |
| 5704 | // If our current best cursor is the construction of a temporary object, |
| 5705 | // don't replace that cursor with a type reference, because we want |
| 5706 | // clang_getCursor() to point at the constructor. |
| 5707 | if (clang_isExpression(BestCursor->kind) && |
| 5708 | isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && |
| 5709 | cursor.kind == CXCursor_TypeRef) { |
| 5710 | // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it |
| 5711 | // as having the actual point on the type reference. |
| 5712 | *BestCursor = getTypeRefedCallExprCursor(*BestCursor); |
| 5713 | return CXChildVisit_Recurse; |
| 5714 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 5715 | |
| 5716 | // If we already have an Objective-C superclass reference, don't |
| 5717 | // update it further. |
| 5718 | if (BestCursor->kind == CXCursor_ObjCSuperClassRef) |
| 5719 | return CXChildVisit_Break; |
| 5720 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5721 | *BestCursor = cursor; |
| 5722 | return CXChildVisit_Recurse; |
| 5723 | } |
| 5724 | |
| 5725 | CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 5726 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 5727 | LOG_BAD_TU(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5728 | return clang_getNullCursor(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 5729 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5730 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 5731 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5732 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 5733 | |
| 5734 | SourceLocation SLoc = cxloc::translateSourceLocation(Loc); |
| 5735 | CXCursor Result = cxcursor::getCursor(TU, SLoc); |
| 5736 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5737 | LOG_FUNC_SECTION { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5738 | CXFile SearchFile; |
| 5739 | unsigned SearchLine, SearchColumn; |
| 5740 | CXFile ResultFile; |
| 5741 | unsigned ResultLine, ResultColumn; |
| 5742 | CXString SearchFileName, ResultFileName, KindSpelling, USR; |
| 5743 | const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : ""; |
| 5744 | CXSourceLocation ResultLoc = clang_getCursorLocation(Result); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5745 | |
| 5746 | clang_getFileLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, |
| 5747 | nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5748 | clang_getFileLocation(ResultLoc, &ResultFile, &ResultLine, |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5749 | &ResultColumn, nullptr); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5750 | SearchFileName = clang_getFileName(SearchFile); |
| 5751 | ResultFileName = clang_getFileName(ResultFile); |
| 5752 | KindSpelling = clang_getCursorKindSpelling(Result.kind); |
| 5753 | USR = clang_getCursorUSR(Result); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5754 | *Log << llvm::format("(%s:%d:%d) = %s", |
| 5755 | clang_getCString(SearchFileName), SearchLine, SearchColumn, |
| 5756 | clang_getCString(KindSpelling)) |
| 5757 | << llvm::format("(%s:%d:%d):%s%s", |
| 5758 | clang_getCString(ResultFileName), ResultLine, ResultColumn, |
| 5759 | clang_getCString(USR), IsDef); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5760 | clang_disposeString(SearchFileName); |
| 5761 | clang_disposeString(ResultFileName); |
| 5762 | clang_disposeString(KindSpelling); |
| 5763 | clang_disposeString(USR); |
| 5764 | |
| 5765 | CXCursor Definition = clang_getCursorDefinition(Result); |
| 5766 | if (!clang_equalCursors(Definition, clang_getNullCursor())) { |
| 5767 | CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition); |
| 5768 | CXString DefinitionKindSpelling |
| 5769 | = clang_getCursorKindSpelling(Definition.kind); |
| 5770 | CXFile DefinitionFile; |
| 5771 | unsigned DefinitionLine, DefinitionColumn; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5772 | clang_getFileLocation(DefinitionLoc, &DefinitionFile, |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5773 | &DefinitionLine, &DefinitionColumn, nullptr); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5774 | CXString DefinitionFileName = clang_getFileName(DefinitionFile); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5775 | *Log << llvm::format(" -> %s(%s:%d:%d)", |
| 5776 | clang_getCString(DefinitionKindSpelling), |
| 5777 | clang_getCString(DefinitionFileName), |
| 5778 | DefinitionLine, DefinitionColumn); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5779 | clang_disposeString(DefinitionFileName); |
| 5780 | clang_disposeString(DefinitionKindSpelling); |
| 5781 | } |
| 5782 | } |
| 5783 | |
| 5784 | return Result; |
| 5785 | } |
| 5786 | |
| 5787 | CXCursor clang_getNullCursor(void) { |
| 5788 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
| 5789 | } |
| 5790 | |
| 5791 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5792 | // Clear out the "FirstInDeclGroup" part in a declaration cursor, since we |
| 5793 | // can't set consistently. For example, when visiting a DeclStmt we will set |
| 5794 | // it but we don't set it on the result of clang_getCursorDefinition for |
| 5795 | // a reference of the same declaration. |
| 5796 | // FIXME: Setting "FirstInDeclGroup" in CXCursors is a hack that only works |
| 5797 | // when visiting a DeclStmt currently, the AST should be enhanced to be able |
| 5798 | // to provide that kind of info. |
| 5799 | if (clang_isDeclaration(X.kind)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5800 | X.data[1] = nullptr; |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5801 | if (clang_isDeclaration(Y.kind)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5802 | Y.data[1] = nullptr; |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5803 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5804 | return X == Y; |
| 5805 | } |
| 5806 | |
| 5807 | unsigned clang_hashCursor(CXCursor C) { |
| 5808 | unsigned Index = 0; |
| 5809 | if (clang_isExpression(C.kind) || clang_isStatement(C.kind)) |
| 5810 | Index = 1; |
| 5811 | |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5812 | return llvm::DenseMapInfo<std::pair<unsigned, const void*> >::getHashValue( |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5813 | std::make_pair(C.kind, C.data[Index])); |
| 5814 | } |
| 5815 | |
| 5816 | unsigned clang_isInvalid(enum CXCursorKind K) { |
| 5817 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 5818 | } |
| 5819 | |
| 5820 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
| 5821 | return (K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl) || |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 5822 | (K >= CXCursor_FirstExtraDecl && K <= CXCursor_LastExtraDecl); |
| 5823 | } |
| 5824 | |
Ivan Donchevskii | 08ff910 | 2018-01-04 10:59:50 +0000 | [diff] [blame] | 5825 | unsigned clang_isInvalidDeclaration(CXCursor C) { |
| 5826 | if (clang_isDeclaration(C.kind)) { |
| 5827 | if (const Decl *D = getCursorDecl(C)) |
| 5828 | return D->isInvalidDecl(); |
| 5829 | } |
| 5830 | |
| 5831 | return 0; |
| 5832 | } |
| 5833 | |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 5834 | unsigned clang_isReference(enum CXCursorKind K) { |
| 5835 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 5836 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5837 | |
| 5838 | unsigned clang_isExpression(enum CXCursorKind K) { |
| 5839 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; |
| 5840 | } |
| 5841 | |
| 5842 | unsigned clang_isStatement(enum CXCursorKind K) { |
| 5843 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; |
| 5844 | } |
| 5845 | |
| 5846 | unsigned clang_isAttribute(enum CXCursorKind K) { |
| 5847 | return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr; |
| 5848 | } |
| 5849 | |
| 5850 | unsigned clang_isTranslationUnit(enum CXCursorKind K) { |
| 5851 | return K == CXCursor_TranslationUnit; |
| 5852 | } |
| 5853 | |
| 5854 | unsigned clang_isPreprocessing(enum CXCursorKind K) { |
| 5855 | return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; |
| 5856 | } |
| 5857 | |
| 5858 | unsigned clang_isUnexposed(enum CXCursorKind K) { |
| 5859 | switch (K) { |
| 5860 | case CXCursor_UnexposedDecl: |
| 5861 | case CXCursor_UnexposedExpr: |
| 5862 | case CXCursor_UnexposedStmt: |
| 5863 | case CXCursor_UnexposedAttr: |
| 5864 | return true; |
| 5865 | default: |
| 5866 | return false; |
| 5867 | } |
| 5868 | } |
| 5869 | |
| 5870 | CXCursorKind clang_getCursorKind(CXCursor C) { |
| 5871 | return C.kind; |
| 5872 | } |
| 5873 | |
| 5874 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 5875 | if (clang_isReference(C.kind)) { |
| 5876 | switch (C.kind) { |
| 5877 | case CXCursor_ObjCSuperClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5878 | std::pair<const ObjCInterfaceDecl *, SourceLocation> P |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5879 | = getCursorObjCSuperClassRef(C); |
| 5880 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5881 | } |
| 5882 | |
| 5883 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5884 | std::pair<const ObjCProtocolDecl *, SourceLocation> P |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5885 | = getCursorObjCProtocolRef(C); |
| 5886 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5887 | } |
| 5888 | |
| 5889 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5890 | std::pair<const ObjCInterfaceDecl *, SourceLocation> P |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5891 | = getCursorObjCClassRef(C); |
| 5892 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5893 | } |
| 5894 | |
| 5895 | case CXCursor_TypeRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5896 | std::pair<const TypeDecl *, SourceLocation> P = getCursorTypeRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5897 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5898 | } |
| 5899 | |
| 5900 | case CXCursor_TemplateRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5901 | std::pair<const TemplateDecl *, SourceLocation> P = |
| 5902 | getCursorTemplateRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5903 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5904 | } |
| 5905 | |
| 5906 | case CXCursor_NamespaceRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5907 | std::pair<const NamedDecl *, SourceLocation> P = getCursorNamespaceRef(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_MemberRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5912 | std::pair<const FieldDecl *, SourceLocation> P = getCursorMemberRef(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_VariableRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5917 | std::pair<const VarDecl *, SourceLocation> P = getCursorVariableRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5918 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5919 | } |
| 5920 | |
| 5921 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5922 | const CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5923 | if (!BaseSpec) |
| 5924 | return clang_getNullLocation(); |
| 5925 | |
| 5926 | if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) |
| 5927 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5928 | TSInfo->getTypeLoc().getBeginLoc()); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5929 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5930 | return cxloc::translateSourceLocation(getCursorContext(C), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5931 | BaseSpec->getBeginLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5932 | } |
| 5933 | |
| 5934 | case CXCursor_LabelRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5935 | std::pair<const LabelStmt *, SourceLocation> P = getCursorLabelRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5936 | return cxloc::translateSourceLocation(getCursorContext(C), P.second); |
| 5937 | } |
| 5938 | |
| 5939 | case CXCursor_OverloadedDeclRef: |
| 5940 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5941 | getCursorOverloadedDeclRef(C).second); |
| 5942 | |
| 5943 | default: |
| 5944 | // FIXME: Need a way to enumerate all non-reference cases. |
| 5945 | llvm_unreachable("Missed a reference kind"); |
| 5946 | } |
| 5947 | } |
| 5948 | |
| 5949 | if (clang_isExpression(C.kind)) |
| 5950 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5951 | getLocationFromExpr(getCursorExpr(C))); |
| 5952 | |
| 5953 | if (clang_isStatement(C.kind)) |
| 5954 | return cxloc::translateSourceLocation(getCursorContext(C), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5955 | getCursorStmt(C)->getBeginLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5956 | |
| 5957 | if (C.kind == CXCursor_PreprocessingDirective) { |
| 5958 | SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); |
| 5959 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5960 | } |
| 5961 | |
| 5962 | if (C.kind == CXCursor_MacroExpansion) { |
| 5963 | SourceLocation L |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 5964 | = cxcursor::getCursorMacroExpansion(C).getSourceRange().getBegin(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5965 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5966 | } |
| 5967 | |
| 5968 | if (C.kind == CXCursor_MacroDefinition) { |
| 5969 | SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); |
| 5970 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5971 | } |
| 5972 | |
| 5973 | if (C.kind == CXCursor_InclusionDirective) { |
| 5974 | SourceLocation L |
| 5975 | = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); |
| 5976 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5977 | } |
| 5978 | |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 5979 | if (clang_isAttribute(C.kind)) { |
| 5980 | SourceLocation L |
| 5981 | = cxcursor::getCursorAttr(C)->getLocation(); |
| 5982 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5983 | } |
| 5984 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5985 | if (!clang_isDeclaration(C.kind)) |
| 5986 | return clang_getNullLocation(); |
| 5987 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5988 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5989 | if (!D) |
| 5990 | return clang_getNullLocation(); |
| 5991 | |
| 5992 | SourceLocation Loc = D->getLocation(); |
| 5993 | // FIXME: Multiple variables declared in a single declaration |
| 5994 | // currently lack the information needed to correctly determine their |
| 5995 | // ranges when accounting for the type-specifier. We use context |
| 5996 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 5997 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5998 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5999 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 6000 | Loc = VD->getLocation(); |
| 6001 | } |
| 6002 | |
| 6003 | // For ObjC methods, give the start location of the method name. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6004 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6005 | Loc = MD->getSelectorStartLoc(); |
| 6006 | |
| 6007 | return cxloc::translateSourceLocation(getCursorContext(C), Loc); |
| 6008 | } |
| 6009 | |
NAKAMURA Takumi | a01f4c3 | 2016-12-19 16:50:43 +0000 | [diff] [blame] | 6010 | } // end extern "C" |
| 6011 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6012 | CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) { |
| 6013 | assert(TU); |
| 6014 | |
| 6015 | // Guard against an invalid SourceLocation, or we may assert in one |
| 6016 | // of the following calls. |
| 6017 | if (SLoc.isInvalid()) |
| 6018 | return clang_getNullCursor(); |
| 6019 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6020 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6021 | |
| 6022 | // Translate the given source location to make it point at the beginning of |
| 6023 | // the token under the cursor. |
| 6024 | SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), |
| 6025 | CXXUnit->getASTContext().getLangOpts()); |
| 6026 | |
| 6027 | CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 6028 | if (SLoc.isValid()) { |
| 6029 | GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result); |
| 6030 | CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData, |
| 6031 | /*VisitPreprocessorLast=*/true, |
| 6032 | /*VisitIncludedEntities=*/false, |
| 6033 | SourceLocation(SLoc)); |
| 6034 | CursorVis.visitFileRegion(); |
| 6035 | } |
| 6036 | |
| 6037 | return Result; |
| 6038 | } |
| 6039 | |
| 6040 | static SourceRange getRawCursorExtent(CXCursor C) { |
| 6041 | if (clang_isReference(C.kind)) { |
| 6042 | switch (C.kind) { |
| 6043 | case CXCursor_ObjCSuperClassRef: |
| 6044 | return getCursorObjCSuperClassRef(C).second; |
| 6045 | |
| 6046 | case CXCursor_ObjCProtocolRef: |
| 6047 | return getCursorObjCProtocolRef(C).second; |
| 6048 | |
| 6049 | case CXCursor_ObjCClassRef: |
| 6050 | return getCursorObjCClassRef(C).second; |
| 6051 | |
| 6052 | case CXCursor_TypeRef: |
| 6053 | return getCursorTypeRef(C).second; |
| 6054 | |
| 6055 | case CXCursor_TemplateRef: |
| 6056 | return getCursorTemplateRef(C).second; |
| 6057 | |
| 6058 | case CXCursor_NamespaceRef: |
| 6059 | return getCursorNamespaceRef(C).second; |
| 6060 | |
| 6061 | case CXCursor_MemberRef: |
| 6062 | return getCursorMemberRef(C).second; |
| 6063 | |
| 6064 | case CXCursor_CXXBaseSpecifier: |
| 6065 | return getCursorCXXBaseSpecifier(C)->getSourceRange(); |
| 6066 | |
| 6067 | case CXCursor_LabelRef: |
| 6068 | return getCursorLabelRef(C).second; |
| 6069 | |
| 6070 | case CXCursor_OverloadedDeclRef: |
| 6071 | return getCursorOverloadedDeclRef(C).second; |
| 6072 | |
| 6073 | case CXCursor_VariableRef: |
| 6074 | return getCursorVariableRef(C).second; |
| 6075 | |
| 6076 | default: |
| 6077 | // FIXME: Need a way to enumerate all non-reference cases. |
| 6078 | llvm_unreachable("Missed a reference kind"); |
| 6079 | } |
| 6080 | } |
| 6081 | |
| 6082 | if (clang_isExpression(C.kind)) |
| 6083 | return getCursorExpr(C)->getSourceRange(); |
| 6084 | |
| 6085 | if (clang_isStatement(C.kind)) |
| 6086 | return getCursorStmt(C)->getSourceRange(); |
| 6087 | |
| 6088 | if (clang_isAttribute(C.kind)) |
| 6089 | return getCursorAttr(C)->getRange(); |
| 6090 | |
| 6091 | if (C.kind == CXCursor_PreprocessingDirective) |
| 6092 | return cxcursor::getCursorPreprocessingDirective(C); |
| 6093 | |
| 6094 | if (C.kind == CXCursor_MacroExpansion) { |
| 6095 | ASTUnit *TU = getCursorASTUnit(C); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 6096 | SourceRange Range = cxcursor::getCursorMacroExpansion(C).getSourceRange(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6097 | return TU->mapRangeFromPreamble(Range); |
| 6098 | } |
| 6099 | |
| 6100 | if (C.kind == CXCursor_MacroDefinition) { |
| 6101 | ASTUnit *TU = getCursorASTUnit(C); |
| 6102 | SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange(); |
| 6103 | return TU->mapRangeFromPreamble(Range); |
| 6104 | } |
| 6105 | |
| 6106 | if (C.kind == CXCursor_InclusionDirective) { |
| 6107 | ASTUnit *TU = getCursorASTUnit(C); |
| 6108 | SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange(); |
| 6109 | return TU->mapRangeFromPreamble(Range); |
| 6110 | } |
| 6111 | |
| 6112 | if (C.kind == CXCursor_TranslationUnit) { |
| 6113 | ASTUnit *TU = getCursorASTUnit(C); |
| 6114 | FileID MainID = TU->getSourceManager().getMainFileID(); |
| 6115 | SourceLocation Start = TU->getSourceManager().getLocForStartOfFile(MainID); |
| 6116 | SourceLocation End = TU->getSourceManager().getLocForEndOfFile(MainID); |
| 6117 | return SourceRange(Start, End); |
| 6118 | } |
| 6119 | |
| 6120 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6121 | const Decl *D = cxcursor::getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6122 | if (!D) |
| 6123 | return SourceRange(); |
| 6124 | |
| 6125 | SourceRange R = D->getSourceRange(); |
| 6126 | // FIXME: Multiple variables declared in a single declaration |
| 6127 | // currently lack the information needed to correctly determine their |
| 6128 | // ranges when accounting for the type-specifier. We use context |
| 6129 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 6130 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6131 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6132 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 6133 | R.setBegin(VD->getLocation()); |
| 6134 | } |
| 6135 | return R; |
| 6136 | } |
| 6137 | return SourceRange(); |
| 6138 | } |
| 6139 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6140 | /// Retrieves the "raw" cursor extent, which is then extended to include |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6141 | /// the decl-specifier-seq for declarations. |
| 6142 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) { |
| 6143 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6144 | const Decl *D = cxcursor::getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6145 | if (!D) |
| 6146 | return SourceRange(); |
| 6147 | |
| 6148 | SourceRange R = D->getSourceRange(); |
| 6149 | |
| 6150 | // Adjust the start of the location for declarations preceded by |
| 6151 | // declaration specifiers. |
| 6152 | SourceLocation StartLoc; |
| 6153 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
| 6154 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6155 | StartLoc = TI->getTypeLoc().getBeginLoc(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6156 | } else if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6157 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6158 | StartLoc = TI->getTypeLoc().getBeginLoc(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6159 | } |
| 6160 | |
| 6161 | if (StartLoc.isValid() && R.getBegin().isValid() && |
| 6162 | SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin())) |
| 6163 | R.setBegin(StartLoc); |
| 6164 | |
| 6165 | // FIXME: Multiple variables declared in a single declaration |
| 6166 | // currently lack the information needed to correctly determine their |
| 6167 | // ranges when accounting for the type-specifier. We use context |
| 6168 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 6169 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6170 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6171 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 6172 | R.setBegin(VD->getLocation()); |
| 6173 | } |
| 6174 | |
| 6175 | return R; |
| 6176 | } |
| 6177 | |
| 6178 | return getRawCursorExtent(C); |
| 6179 | } |
| 6180 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6181 | CXSourceRange clang_getCursorExtent(CXCursor C) { |
| 6182 | SourceRange R = getRawCursorExtent(C); |
| 6183 | if (R.isInvalid()) |
| 6184 | return clang_getNullRange(); |
| 6185 | |
| 6186 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 6187 | } |
| 6188 | |
| 6189 | CXCursor clang_getCursorReferenced(CXCursor C) { |
| 6190 | if (clang_isInvalid(C.kind)) |
| 6191 | return clang_getNullCursor(); |
| 6192 | |
| 6193 | CXTranslationUnit tu = getCursorTU(C); |
| 6194 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6195 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6196 | if (!D) |
| 6197 | return clang_getNullCursor(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6198 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6199 | return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6200 | if (const ObjCPropertyImplDecl *PropImpl = |
| 6201 | dyn_cast<ObjCPropertyImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6202 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 6203 | return MakeCXCursor(Property, tu); |
| 6204 | |
| 6205 | return C; |
| 6206 | } |
| 6207 | |
| 6208 | if (clang_isExpression(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6209 | const Expr *E = getCursorExpr(C); |
| 6210 | const Decl *D = getDeclFromExpr(E); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6211 | if (D) { |
| 6212 | CXCursor declCursor = MakeCXCursor(D, tu); |
| 6213 | declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C), |
| 6214 | declCursor); |
| 6215 | return declCursor; |
| 6216 | } |
| 6217 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6218 | if (const OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6219 | return MakeCursorOverloadedDeclRef(Ovl, tu); |
| 6220 | |
| 6221 | return clang_getNullCursor(); |
| 6222 | } |
| 6223 | |
| 6224 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6225 | const Stmt *S = getCursorStmt(C); |
| 6226 | if (const GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6227 | if (LabelDecl *label = Goto->getLabel()) |
| 6228 | if (LabelStmt *labelS = label->getStmt()) |
| 6229 | return MakeCXCursor(labelS, getCursorDecl(C), tu); |
| 6230 | |
| 6231 | return clang_getNullCursor(); |
| 6232 | } |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 6233 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6234 | if (C.kind == CXCursor_MacroExpansion) { |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 6235 | if (const MacroDefinitionRecord *Def = |
| 6236 | getCursorMacroExpansion(C).getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6237 | return MakeMacroDefinitionCursor(Def, tu); |
| 6238 | } |
| 6239 | |
| 6240 | if (!clang_isReference(C.kind)) |
| 6241 | return clang_getNullCursor(); |
| 6242 | |
| 6243 | switch (C.kind) { |
| 6244 | case CXCursor_ObjCSuperClassRef: |
| 6245 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu); |
| 6246 | |
| 6247 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 6248 | const ObjCProtocolDecl *Prot = getCursorObjCProtocolRef(C).first; |
| 6249 | if (const ObjCProtocolDecl *Def = Prot->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6250 | return MakeCXCursor(Def, tu); |
| 6251 | |
| 6252 | return MakeCXCursor(Prot, tu); |
| 6253 | } |
| 6254 | |
| 6255 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 6256 | const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
| 6257 | if (const ObjCInterfaceDecl *Def = Class->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6258 | return MakeCXCursor(Def, tu); |
| 6259 | |
| 6260 | return MakeCXCursor(Class, tu); |
| 6261 | } |
| 6262 | |
| 6263 | case CXCursor_TypeRef: |
| 6264 | return MakeCXCursor(getCursorTypeRef(C).first, tu ); |
| 6265 | |
| 6266 | case CXCursor_TemplateRef: |
| 6267 | return MakeCXCursor(getCursorTemplateRef(C).first, tu ); |
| 6268 | |
| 6269 | case CXCursor_NamespaceRef: |
| 6270 | return MakeCXCursor(getCursorNamespaceRef(C).first, tu ); |
| 6271 | |
| 6272 | case CXCursor_MemberRef: |
| 6273 | return MakeCXCursor(getCursorMemberRef(C).first, tu ); |
| 6274 | |
| 6275 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 6276 | const CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6277 | return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), |
| 6278 | tu )); |
| 6279 | } |
| 6280 | |
| 6281 | case CXCursor_LabelRef: |
| 6282 | // FIXME: We end up faking the "parent" declaration here because we |
| 6283 | // don't want to make CXCursor larger. |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6284 | return MakeCXCursor(getCursorLabelRef(C).first, |
| 6285 | cxtu::getASTUnit(tu)->getASTContext() |
| 6286 | .getTranslationUnitDecl(), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6287 | tu); |
| 6288 | |
| 6289 | case CXCursor_OverloadedDeclRef: |
| 6290 | return C; |
| 6291 | |
| 6292 | case CXCursor_VariableRef: |
| 6293 | return MakeCXCursor(getCursorVariableRef(C).first, tu); |
| 6294 | |
| 6295 | default: |
| 6296 | // We would prefer to enumerate all non-reference cursor kinds here. |
| 6297 | llvm_unreachable("Unhandled reference cursor kind"); |
| 6298 | } |
| 6299 | } |
| 6300 | |
| 6301 | CXCursor clang_getCursorDefinition(CXCursor C) { |
| 6302 | if (clang_isInvalid(C.kind)) |
| 6303 | return clang_getNullCursor(); |
| 6304 | |
| 6305 | CXTranslationUnit TU = getCursorTU(C); |
| 6306 | |
| 6307 | bool WasReference = false; |
| 6308 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { |
| 6309 | C = clang_getCursorReferenced(C); |
| 6310 | WasReference = true; |
| 6311 | } |
| 6312 | |
| 6313 | if (C.kind == CXCursor_MacroExpansion) |
| 6314 | return clang_getCursorReferenced(C); |
| 6315 | |
| 6316 | if (!clang_isDeclaration(C.kind)) |
| 6317 | return clang_getNullCursor(); |
| 6318 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6319 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6320 | if (!D) |
| 6321 | return clang_getNullCursor(); |
| 6322 | |
| 6323 | switch (D->getKind()) { |
| 6324 | // Declaration kinds that don't really separate the notions of |
| 6325 | // declaration and definition. |
| 6326 | case Decl::Namespace: |
| 6327 | case Decl::Typedef: |
| 6328 | case Decl::TypeAlias: |
| 6329 | case Decl::TypeAliasTemplate: |
| 6330 | case Decl::TemplateTypeParm: |
| 6331 | case Decl::EnumConstant: |
| 6332 | case Decl::Field: |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 6333 | case Decl::Binding: |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 6334 | case Decl::MSProperty: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6335 | case Decl::IndirectField: |
| 6336 | case Decl::ObjCIvar: |
| 6337 | case Decl::ObjCAtDefsField: |
| 6338 | case Decl::ImplicitParam: |
| 6339 | case Decl::ParmVar: |
| 6340 | case Decl::NonTypeTemplateParm: |
| 6341 | case Decl::TemplateTemplateParm: |
| 6342 | case Decl::ObjCCategoryImpl: |
| 6343 | case Decl::ObjCImplementation: |
| 6344 | case Decl::AccessSpec: |
| 6345 | case Decl::LinkageSpec: |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 6346 | case Decl::Export: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6347 | case Decl::ObjCPropertyImpl: |
| 6348 | case Decl::FileScopeAsm: |
| 6349 | case Decl::StaticAssert: |
| 6350 | case Decl::Block: |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 6351 | case Decl::Captured: |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 6352 | case Decl::OMPCapturedExpr: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6353 | case Decl::Label: // FIXME: Is this right?? |
| 6354 | case Decl::ClassScopeFunctionSpecialization: |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 6355 | case Decl::CXXDeductionGuide: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6356 | case Decl::Import: |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 6357 | case Decl::OMPThreadPrivate: |
Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 6358 | case Decl::OMPAllocate: |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 6359 | case Decl::OMPDeclareReduction: |
Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 6360 | case Decl::OMPDeclareMapper: |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 6361 | case Decl::OMPRequires: |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 6362 | case Decl::ObjCTypeParam: |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 6363 | case Decl::BuiltinTemplate: |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 6364 | case Decl::PragmaComment: |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 6365 | case Decl::PragmaDetectMismatch: |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 6366 | case Decl::UsingPack: |
Saar Raz | d7aae33 | 2019-07-10 21:25:49 +0000 | [diff] [blame] | 6367 | case Decl::Concept: |
Tyker | b0561b3 | 2019-11-17 11:41:55 +0100 | [diff] [blame] | 6368 | case Decl::LifetimeExtendedTemporary: |
Saar Raz | a0f50d7 | 2020-01-18 09:11:43 +0200 | [diff] [blame] | 6369 | case Decl::RequiresExprBody: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6370 | return C; |
| 6371 | |
| 6372 | // Declaration kinds that don't make any sense here, but are |
| 6373 | // nonetheless harmless. |
David Blaikie | f005d3c | 2013-02-22 17:44:58 +0000 | [diff] [blame] | 6374 | case Decl::Empty: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6375 | case Decl::TranslationUnit: |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 6376 | case Decl::ExternCContext: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6377 | break; |
| 6378 | |
| 6379 | // Declaration kinds for which the definition is not resolvable. |
| 6380 | case Decl::UnresolvedUsingTypename: |
| 6381 | case Decl::UnresolvedUsingValue: |
| 6382 | break; |
| 6383 | |
| 6384 | case Decl::UsingDirective: |
| 6385 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), |
| 6386 | TU); |
| 6387 | |
| 6388 | case Decl::NamespaceAlias: |
| 6389 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU); |
| 6390 | |
| 6391 | case Decl::Enum: |
| 6392 | case Decl::Record: |
| 6393 | case Decl::CXXRecord: |
| 6394 | case Decl::ClassTemplateSpecialization: |
| 6395 | case Decl::ClassTemplatePartialSpecialization: |
| 6396 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) |
| 6397 | return MakeCXCursor(Def, TU); |
| 6398 | return clang_getNullCursor(); |
| 6399 | |
| 6400 | case Decl::Function: |
| 6401 | case Decl::CXXMethod: |
| 6402 | case Decl::CXXConstructor: |
| 6403 | case Decl::CXXDestructor: |
| 6404 | case Decl::CXXConversion: { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6405 | const FunctionDecl *Def = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6406 | if (cast<FunctionDecl>(D)->getBody(Def)) |
Dmitri Gribenko | 9c256e3 | 2013-01-14 00:46:27 +0000 | [diff] [blame] | 6407 | return MakeCXCursor(Def, TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6408 | return clang_getNullCursor(); |
| 6409 | } |
| 6410 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6411 | case Decl::Var: |
| 6412 | case Decl::VarTemplateSpecialization: |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 6413 | case Decl::VarTemplatePartialSpecialization: |
| 6414 | case Decl::Decomposition: { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6415 | // Ask the variable if it has a definition. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6416 | if (const VarDecl *Def = cast<VarDecl>(D)->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6417 | return MakeCXCursor(Def, TU); |
| 6418 | return clang_getNullCursor(); |
| 6419 | } |
| 6420 | |
| 6421 | case Decl::FunctionTemplate: { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6422 | const FunctionDecl *Def = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6423 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) |
| 6424 | return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU); |
| 6425 | return clang_getNullCursor(); |
| 6426 | } |
| 6427 | |
| 6428 | case Decl::ClassTemplate: { |
| 6429 | if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() |
| 6430 | ->getDefinition()) |
| 6431 | return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), |
| 6432 | TU); |
| 6433 | return clang_getNullCursor(); |
| 6434 | } |
| 6435 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6436 | case Decl::VarTemplate: { |
| 6437 | if (VarDecl *Def = |
| 6438 | cast<VarTemplateDecl>(D)->getTemplatedDecl()->getDefinition()) |
| 6439 | return MakeCXCursor(cast<VarDecl>(Def)->getDescribedVarTemplate(), TU); |
| 6440 | return clang_getNullCursor(); |
| 6441 | } |
| 6442 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6443 | case Decl::Using: |
| 6444 | return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), |
| 6445 | D->getLocation(), TU); |
| 6446 | |
| 6447 | case Decl::UsingShadow: |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6448 | case Decl::ConstructorUsingShadow: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6449 | return clang_getCursorDefinition( |
| 6450 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), |
| 6451 | TU)); |
| 6452 | |
| 6453 | case Decl::ObjCMethod: { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6454 | const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6455 | if (Method->isThisDeclarationADefinition()) |
| 6456 | return C; |
| 6457 | |
| 6458 | // Dig out the method definition in the associated |
| 6459 | // @implementation, if we have it. |
| 6460 | // FIXME: The ASTs should make finding the definition easier. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6461 | if (const ObjCInterfaceDecl *Class |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6462 | = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) |
| 6463 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) |
| 6464 | if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), |
| 6465 | Method->isInstanceMethod())) |
| 6466 | if (Def->isThisDeclarationADefinition()) |
| 6467 | return MakeCXCursor(Def, TU); |
| 6468 | |
| 6469 | return clang_getNullCursor(); |
| 6470 | } |
| 6471 | |
| 6472 | case Decl::ObjCCategory: |
| 6473 | if (ObjCCategoryImplDecl *Impl |
| 6474 | = cast<ObjCCategoryDecl>(D)->getImplementation()) |
| 6475 | return MakeCXCursor(Impl, TU); |
| 6476 | return clang_getNullCursor(); |
| 6477 | |
| 6478 | case Decl::ObjCProtocol: |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6479 | if (const ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(D)->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6480 | return MakeCXCursor(Def, TU); |
| 6481 | return clang_getNullCursor(); |
| 6482 | |
| 6483 | case Decl::ObjCInterface: { |
| 6484 | // There are two notions of a "definition" for an Objective-C |
| 6485 | // class: the interface and its implementation. When we resolved a |
| 6486 | // reference to an Objective-C class, produce the @interface as |
| 6487 | // the definition; when we were provided with the interface, |
| 6488 | // produce the @implementation as the definition. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6489 | const ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6490 | if (WasReference) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6491 | if (const ObjCInterfaceDecl *Def = IFace->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6492 | return MakeCXCursor(Def, TU); |
| 6493 | } else if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
| 6494 | return MakeCXCursor(Impl, TU); |
| 6495 | return clang_getNullCursor(); |
| 6496 | } |
| 6497 | |
| 6498 | case Decl::ObjCProperty: |
| 6499 | // FIXME: We don't really know where to find the |
| 6500 | // ObjCPropertyImplDecls that implement this property. |
| 6501 | return clang_getNullCursor(); |
| 6502 | |
| 6503 | case Decl::ObjCCompatibleAlias: |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6504 | if (const ObjCInterfaceDecl *Class |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6505 | = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6506 | if (const ObjCInterfaceDecl *Def = Class->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6507 | return MakeCXCursor(Def, TU); |
| 6508 | |
| 6509 | return clang_getNullCursor(); |
| 6510 | |
| 6511 | case Decl::Friend: |
| 6512 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) |
| 6513 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
| 6514 | return clang_getNullCursor(); |
| 6515 | |
| 6516 | case Decl::FriendTemplate: |
| 6517 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) |
| 6518 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
| 6519 | return clang_getNullCursor(); |
| 6520 | } |
| 6521 | |
| 6522 | return clang_getNullCursor(); |
| 6523 | } |
| 6524 | |
| 6525 | unsigned clang_isCursorDefinition(CXCursor C) { |
| 6526 | if (!clang_isDeclaration(C.kind)) |
| 6527 | return 0; |
| 6528 | |
| 6529 | return clang_getCursorDefinition(C) == C; |
| 6530 | } |
| 6531 | |
| 6532 | CXCursor clang_getCanonicalCursor(CXCursor C) { |
| 6533 | if (!clang_isDeclaration(C.kind)) |
| 6534 | return C; |
| 6535 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6536 | if (const Decl *D = getCursorDecl(C)) { |
| 6537 | if (const ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6538 | if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl()) |
| 6539 | return MakeCXCursor(CatD, getCursorTU(C)); |
| 6540 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6541 | if (const ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) |
| 6542 | if (const ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6543 | return MakeCXCursor(IFD, getCursorTU(C)); |
| 6544 | |
| 6545 | return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C)); |
| 6546 | } |
| 6547 | |
| 6548 | return C; |
| 6549 | } |
| 6550 | |
| 6551 | int clang_Cursor_getObjCSelectorIndex(CXCursor cursor) { |
| 6552 | return cxcursor::getSelectorIdentifierIndexAndLoc(cursor).first; |
| 6553 | } |
| 6554 | |
| 6555 | unsigned clang_getNumOverloadedDecls(CXCursor C) { |
| 6556 | if (C.kind != CXCursor_OverloadedDeclRef) |
| 6557 | return 0; |
| 6558 | |
| 6559 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6560 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6561 | return E->getNumDecls(); |
| 6562 | |
| 6563 | if (OverloadedTemplateStorage *S |
| 6564 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 6565 | return S->size(); |
| 6566 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6567 | const Decl *D = Storage.get<const Decl *>(); |
| 6568 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6569 | return Using->shadow_size(); |
| 6570 | |
| 6571 | return 0; |
| 6572 | } |
| 6573 | |
| 6574 | CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { |
| 6575 | if (cursor.kind != CXCursor_OverloadedDeclRef) |
| 6576 | return clang_getNullCursor(); |
| 6577 | |
| 6578 | if (index >= clang_getNumOverloadedDecls(cursor)) |
| 6579 | return clang_getNullCursor(); |
| 6580 | |
| 6581 | CXTranslationUnit TU = getCursorTU(cursor); |
| 6582 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6583 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6584 | return MakeCXCursor(E->decls_begin()[index], TU); |
| 6585 | |
| 6586 | if (OverloadedTemplateStorage *S |
| 6587 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 6588 | return MakeCXCursor(S->begin()[index], TU); |
| 6589 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6590 | const Decl *D = Storage.get<const Decl *>(); |
| 6591 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6592 | // FIXME: This is, unfortunately, linear time. |
| 6593 | UsingDecl::shadow_iterator Pos = Using->shadow_begin(); |
| 6594 | std::advance(Pos, index); |
| 6595 | return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU); |
| 6596 | } |
| 6597 | |
| 6598 | return clang_getNullCursor(); |
| 6599 | } |
| 6600 | |
| 6601 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
| 6602 | const char **startBuf, |
| 6603 | const char **endBuf, |
| 6604 | unsigned *startLine, |
| 6605 | unsigned *startColumn, |
| 6606 | unsigned *endLine, |
| 6607 | unsigned *endColumn) { |
| 6608 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6609 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(getCursorDecl(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6610 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
| 6611 | |
| 6612 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 6613 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 6614 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 6615 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 6616 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 6617 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 6618 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 6619 | } |
| 6620 | |
| 6621 | |
| 6622 | CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags, |
| 6623 | unsigned PieceIndex) { |
| 6624 | RefNamePieces Pieces; |
| 6625 | |
| 6626 | switch (C.kind) { |
| 6627 | case CXCursor_MemberRefExpr: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6628 | if (const MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6629 | Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(), |
| 6630 | E->getQualifierLoc().getSourceRange()); |
| 6631 | break; |
| 6632 | |
| 6633 | case CXCursor_DeclRefExpr: |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 6634 | if (const DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C))) { |
| 6635 | SourceRange TemplateArgLoc(E->getLAngleLoc(), E->getRAngleLoc()); |
| 6636 | Pieces = |
| 6637 | buildPieces(NameFlags, false, E->getNameInfo(), |
| 6638 | E->getQualifierLoc().getSourceRange(), &TemplateArgLoc); |
| 6639 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6640 | break; |
| 6641 | |
| 6642 | case CXCursor_CallExpr: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6643 | if (const CXXOperatorCallExpr *OCE = |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6644 | dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6645 | const Expr *Callee = OCE->getCallee(); |
| 6646 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6647 | Callee = ICE->getSubExpr(); |
| 6648 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6649 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6650 | Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(), |
| 6651 | DRE->getQualifierLoc().getSourceRange()); |
| 6652 | } |
| 6653 | break; |
| 6654 | |
| 6655 | default: |
| 6656 | break; |
| 6657 | } |
| 6658 | |
| 6659 | if (Pieces.empty()) { |
| 6660 | if (PieceIndex == 0) |
| 6661 | return clang_getCursorExtent(C); |
| 6662 | } else if (PieceIndex < Pieces.size()) { |
| 6663 | SourceRange R = Pieces[PieceIndex]; |
| 6664 | if (R.isValid()) |
| 6665 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 6666 | } |
| 6667 | |
| 6668 | return clang_getNullRange(); |
| 6669 | } |
| 6670 | |
| 6671 | void clang_enableStackTraces(void) { |
Richard Smith | dfed58a | 2016-06-09 00:53:41 +0000 | [diff] [blame] | 6672 | // FIXME: Provide an argv0 here so we can find llvm-symbolizer. |
| 6673 | llvm::sys::PrintStackTraceOnErrorSignal(StringRef()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6674 | } |
| 6675 | |
| 6676 | void clang_executeOnThread(void (*fn)(void*), void *user_data, |
| 6677 | unsigned stack_size) { |
Alexandre Ganea | 471d060 | 2019-11-29 10:52:13 -0500 | [diff] [blame] | 6678 | llvm::llvm_execute_on_thread(fn, user_data, |
| 6679 | stack_size == 0 |
| 6680 | ? clang::DesiredStackSize |
| 6681 | : llvm::Optional<unsigned>(stack_size)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6682 | } |
| 6683 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6684 | //===----------------------------------------------------------------------===// |
| 6685 | // Token-based Operations. |
| 6686 | //===----------------------------------------------------------------------===// |
| 6687 | |
| 6688 | /* CXToken layout: |
| 6689 | * int_data[0]: a CXTokenKind |
| 6690 | * int_data[1]: starting token location |
| 6691 | * int_data[2]: token length |
| 6692 | * int_data[3]: reserved |
| 6693 | * ptr_data: for identifiers and keywords, an IdentifierInfo*. |
| 6694 | * otherwise unused. |
| 6695 | */ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6696 | CXTokenKind clang_getTokenKind(CXToken CXTok) { |
| 6697 | return static_cast<CXTokenKind>(CXTok.int_data[0]); |
| 6698 | } |
| 6699 | |
| 6700 | CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { |
| 6701 | switch (clang_getTokenKind(CXTok)) { |
| 6702 | case CXToken_Identifier: |
| 6703 | case CXToken_Keyword: |
| 6704 | // We know we have an IdentifierInfo*, so use that. |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 6705 | return cxstring::createRef(static_cast<IdentifierInfo *>(CXTok.ptr_data) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6706 | ->getNameStart()); |
| 6707 | |
| 6708 | case CXToken_Literal: { |
| 6709 | // We have stashed the starting pointer in the ptr_data field. Use it. |
| 6710 | const char *Text = static_cast<const char *>(CXTok.ptr_data); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 6711 | return cxstring::createDup(StringRef(Text, CXTok.int_data[2])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6712 | } |
| 6713 | |
| 6714 | case CXToken_Punctuation: |
| 6715 | case CXToken_Comment: |
| 6716 | break; |
| 6717 | } |
| 6718 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6719 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6720 | LOG_BAD_TU(TU); |
| 6721 | return cxstring::createEmpty(); |
| 6722 | } |
| 6723 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6724 | // We have to find the starting buffer pointer the hard way, by |
| 6725 | // deconstructing the source location. |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6726 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6727 | if (!CXXUnit) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 6728 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6729 | |
| 6730 | SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); |
| 6731 | std::pair<FileID, unsigned> LocInfo |
| 6732 | = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc); |
| 6733 | bool Invalid = false; |
| 6734 | StringRef Buffer |
| 6735 | = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); |
| 6736 | if (Invalid) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 6737 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6738 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 6739 | return cxstring::createDup(Buffer.substr(LocInfo.second, CXTok.int_data[2])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6740 | } |
| 6741 | |
| 6742 | CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6743 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6744 | LOG_BAD_TU(TU); |
| 6745 | return clang_getNullLocation(); |
| 6746 | } |
| 6747 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6748 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6749 | if (!CXXUnit) |
| 6750 | return clang_getNullLocation(); |
| 6751 | |
| 6752 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), |
| 6753 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 6754 | } |
| 6755 | |
| 6756 | CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6757 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6758 | LOG_BAD_TU(TU); |
| 6759 | return clang_getNullRange(); |
| 6760 | } |
| 6761 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6762 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6763 | if (!CXXUnit) |
| 6764 | return clang_getNullRange(); |
| 6765 | |
| 6766 | return cxloc::translateSourceRange(CXXUnit->getASTContext(), |
| 6767 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 6768 | } |
| 6769 | |
| 6770 | static void getTokens(ASTUnit *CXXUnit, SourceRange Range, |
| 6771 | SmallVectorImpl<CXToken> &CXTokens) { |
| 6772 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 6773 | std::pair<FileID, unsigned> BeginLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6774 | = SourceMgr.getDecomposedSpellingLoc(Range.getBegin()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6775 | std::pair<FileID, unsigned> EndLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6776 | = SourceMgr.getDecomposedSpellingLoc(Range.getEnd()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6777 | |
| 6778 | // Cannot tokenize across files. |
| 6779 | if (BeginLocInfo.first != EndLocInfo.first) |
| 6780 | return; |
| 6781 | |
| 6782 | // Create a lexer |
| 6783 | bool Invalid = false; |
| 6784 | StringRef Buffer |
| 6785 | = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 6786 | if (Invalid) |
| 6787 | return; |
| 6788 | |
| 6789 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 6790 | CXXUnit->getASTContext().getLangOpts(), |
| 6791 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); |
| 6792 | Lex.SetCommentRetentionState(true); |
| 6793 | |
| 6794 | // Lex tokens until we hit the end of the range. |
| 6795 | const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; |
| 6796 | Token Tok; |
| 6797 | bool previousWasAt = false; |
| 6798 | do { |
| 6799 | // Lex the next token |
| 6800 | Lex.LexFromRawLexer(Tok); |
| 6801 | if (Tok.is(tok::eof)) |
| 6802 | break; |
| 6803 | |
| 6804 | // Initialize the CXToken. |
| 6805 | CXToken CXTok; |
| 6806 | |
| 6807 | // - Common fields |
| 6808 | CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); |
| 6809 | CXTok.int_data[2] = Tok.getLength(); |
| 6810 | CXTok.int_data[3] = 0; |
| 6811 | |
| 6812 | // - Kind-specific fields |
| 6813 | if (Tok.isLiteral()) { |
| 6814 | CXTok.int_data[0] = CXToken_Literal; |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 6815 | CXTok.ptr_data = const_cast<char *>(Tok.getLiteralData()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6816 | } else if (Tok.is(tok::raw_identifier)) { |
| 6817 | // Lookup the identifier to determine whether we have a keyword. |
| 6818 | IdentifierInfo *II |
| 6819 | = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok); |
| 6820 | |
| 6821 | if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { |
| 6822 | CXTok.int_data[0] = CXToken_Keyword; |
| 6823 | } |
| 6824 | else { |
| 6825 | CXTok.int_data[0] = Tok.is(tok::identifier) |
| 6826 | ? CXToken_Identifier |
| 6827 | : CXToken_Keyword; |
| 6828 | } |
| 6829 | CXTok.ptr_data = II; |
| 6830 | } else if (Tok.is(tok::comment)) { |
| 6831 | CXTok.int_data[0] = CXToken_Comment; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6832 | CXTok.ptr_data = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6833 | } else { |
| 6834 | CXTok.int_data[0] = CXToken_Punctuation; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6835 | CXTok.ptr_data = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6836 | } |
| 6837 | CXTokens.push_back(CXTok); |
| 6838 | previousWasAt = Tok.is(tok::at); |
Argyrios Kyrtzidis | c7c6a07 | 2016-11-09 23:58:39 +0000 | [diff] [blame] | 6839 | } while (Lex.getBufferLocation() < EffectiveBufferEnd); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6840 | } |
| 6841 | |
Ivan Donchevskii | 3957e48 | 2018-06-13 12:37:08 +0000 | [diff] [blame] | 6842 | CXToken *clang_getToken(CXTranslationUnit TU, CXSourceLocation Location) { |
| 6843 | LOG_FUNC_SECTION { |
| 6844 | *Log << TU << ' ' << Location; |
| 6845 | } |
| 6846 | |
| 6847 | if (isNotUsableTU(TU)) { |
| 6848 | LOG_BAD_TU(TU); |
| 6849 | return NULL; |
| 6850 | } |
| 6851 | |
| 6852 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
| 6853 | if (!CXXUnit) |
| 6854 | return NULL; |
| 6855 | |
| 6856 | SourceLocation Begin = cxloc::translateSourceLocation(Location); |
| 6857 | if (Begin.isInvalid()) |
| 6858 | return NULL; |
| 6859 | SourceManager &SM = CXXUnit->getSourceManager(); |
| 6860 | std::pair<FileID, unsigned> DecomposedEnd = SM.getDecomposedLoc(Begin); |
| 6861 | DecomposedEnd.second += Lexer::MeasureTokenLength(Begin, SM, CXXUnit->getLangOpts()); |
| 6862 | |
| 6863 | SourceLocation End = SM.getComposedLoc(DecomposedEnd.first, DecomposedEnd.second); |
| 6864 | |
| 6865 | SmallVector<CXToken, 32> CXTokens; |
| 6866 | getTokens(CXXUnit, SourceRange(Begin, End), CXTokens); |
| 6867 | |
| 6868 | if (CXTokens.empty()) |
| 6869 | return NULL; |
| 6870 | |
| 6871 | CXTokens.resize(1); |
| 6872 | CXToken *Token = static_cast<CXToken *>(llvm::safe_malloc(sizeof(CXToken))); |
| 6873 | |
| 6874 | memmove(Token, CXTokens.data(), sizeof(CXToken)); |
| 6875 | return Token; |
| 6876 | } |
| 6877 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6878 | void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, |
| 6879 | CXToken **Tokens, unsigned *NumTokens) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 6880 | LOG_FUNC_SECTION { |
| 6881 | *Log << TU << ' ' << Range; |
| 6882 | } |
| 6883 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6884 | if (Tokens) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6885 | *Tokens = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6886 | if (NumTokens) |
| 6887 | *NumTokens = 0; |
| 6888 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6889 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6890 | LOG_BAD_TU(TU); |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 6891 | return; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6892 | } |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 6893 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6894 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6895 | if (!CXXUnit || !Tokens || !NumTokens) |
| 6896 | return; |
| 6897 | |
| 6898 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 6899 | |
| 6900 | SourceRange R = cxloc::translateCXSourceRange(Range); |
| 6901 | if (R.isInvalid()) |
| 6902 | return; |
| 6903 | |
| 6904 | SmallVector<CXToken, 32> CXTokens; |
| 6905 | getTokens(CXXUnit, R, CXTokens); |
| 6906 | |
| 6907 | if (CXTokens.empty()) |
| 6908 | return; |
| 6909 | |
Serge Pavlov | 5252573 | 2018-02-21 02:02:39 +0000 | [diff] [blame] | 6910 | *Tokens = static_cast<CXToken *>( |
| 6911 | llvm::safe_malloc(sizeof(CXToken) * CXTokens.size())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6912 | memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); |
| 6913 | *NumTokens = CXTokens.size(); |
| 6914 | } |
| 6915 | |
| 6916 | void clang_disposeTokens(CXTranslationUnit TU, |
| 6917 | CXToken *Tokens, unsigned NumTokens) { |
| 6918 | free(Tokens); |
| 6919 | } |
| 6920 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6921 | //===----------------------------------------------------------------------===// |
| 6922 | // Token annotation APIs. |
| 6923 | //===----------------------------------------------------------------------===// |
| 6924 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6925 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 6926 | CXCursor parent, |
| 6927 | CXClientData client_data); |
| 6928 | static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor, |
| 6929 | CXClientData client_data); |
| 6930 | |
| 6931 | namespace { |
| 6932 | class AnnotateTokensWorker { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6933 | CXToken *Tokens; |
| 6934 | CXCursor *Cursors; |
| 6935 | unsigned NumTokens; |
| 6936 | unsigned TokIdx; |
| 6937 | unsigned PreprocessingTokIdx; |
| 6938 | CursorVisitor AnnotateVis; |
| 6939 | SourceManager &SrcMgr; |
| 6940 | bool HasContextSensitiveKeywords; |
| 6941 | |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 6942 | struct PostChildrenAction { |
| 6943 | CXCursor cursor; |
| 6944 | enum Action { Invalid, Ignore, Postpone } action; |
| 6945 | }; |
| 6946 | using PostChildrenActions = SmallVector<PostChildrenAction, 0>; |
| 6947 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6948 | struct PostChildrenInfo { |
| 6949 | CXCursor Cursor; |
| 6950 | SourceRange CursorRange; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6951 | unsigned BeforeReachingCursorIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6952 | unsigned BeforeChildrenTokenIdx; |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 6953 | PostChildrenActions ChildActions; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6954 | }; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6955 | SmallVector<PostChildrenInfo, 8> PostChildrenInfos; |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6956 | |
| 6957 | CXToken &getTok(unsigned Idx) { |
| 6958 | assert(Idx < NumTokens); |
| 6959 | return Tokens[Idx]; |
| 6960 | } |
| 6961 | const CXToken &getTok(unsigned Idx) const { |
| 6962 | assert(Idx < NumTokens); |
| 6963 | return Tokens[Idx]; |
| 6964 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6965 | bool MoreTokens() const { return TokIdx < NumTokens; } |
| 6966 | unsigned NextToken() const { return TokIdx; } |
| 6967 | void AdvanceToken() { ++TokIdx; } |
| 6968 | SourceLocation GetTokenLoc(unsigned tokI) { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6969 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6970 | } |
| 6971 | bool isFunctionMacroToken(unsigned tokI) const { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6972 | return getTok(tokI).int_data[3] != 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6973 | } |
| 6974 | SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6975 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[3]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6976 | } |
| 6977 | |
| 6978 | void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange); |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6979 | bool annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6980 | SourceRange); |
| 6981 | |
| 6982 | public: |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6983 | AnnotateTokensWorker(CXToken *tokens, CXCursor *cursors, unsigned numTokens, |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6984 | CXTranslationUnit TU, SourceRange RegionOfInterest) |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6985 | : Tokens(tokens), Cursors(cursors), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6986 | NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0), |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6987 | AnnotateVis(TU, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6988 | AnnotateTokensVisitor, this, |
| 6989 | /*VisitPreprocessorLast=*/true, |
| 6990 | /*VisitIncludedEntities=*/false, |
| 6991 | RegionOfInterest, |
| 6992 | /*VisitDeclsOnly=*/false, |
| 6993 | AnnotateTokensPostChildrenVisitor), |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6994 | SrcMgr(cxtu::getASTUnit(TU)->getSourceManager()), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6995 | HasContextSensitiveKeywords(false) { } |
| 6996 | |
| 6997 | void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } |
| 6998 | enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 6999 | bool IsIgnoredChildCursor(CXCursor cursor) const; |
| 7000 | PostChildrenActions DetermineChildActions(CXCursor Cursor) const; |
| 7001 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7002 | bool postVisitChildren(CXCursor cursor); |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7003 | void HandlePostPonedChildCursors(const PostChildrenInfo &Info); |
| 7004 | void HandlePostPonedChildCursor(CXCursor Cursor, unsigned StartTokenIndex); |
| 7005 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7006 | void AnnotateTokens(); |
| 7007 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7008 | /// Determine whether the annotator saw any cursors that have |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7009 | /// context-sensitive keywords. |
| 7010 | bool hasContextSensitiveKeywords() const { |
| 7011 | return HasContextSensitiveKeywords; |
| 7012 | } |
| 7013 | |
| 7014 | ~AnnotateTokensWorker() { |
| 7015 | assert(PostChildrenInfos.empty()); |
| 7016 | } |
| 7017 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7018 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7019 | |
| 7020 | void AnnotateTokensWorker::AnnotateTokens() { |
| 7021 | // Walk the AST within the region of interest, annotating tokens |
| 7022 | // along the way. |
| 7023 | AnnotateVis.visitFileRegion(); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7024 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7025 | |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7026 | bool AnnotateTokensWorker::IsIgnoredChildCursor(CXCursor cursor) const { |
| 7027 | if (PostChildrenInfos.empty()) |
| 7028 | return false; |
| 7029 | |
| 7030 | for (const auto &ChildAction : PostChildrenInfos.back().ChildActions) { |
| 7031 | if (ChildAction.cursor == cursor && |
| 7032 | ChildAction.action == PostChildrenAction::Ignore) { |
| 7033 | return true; |
| 7034 | } |
| 7035 | } |
| 7036 | |
| 7037 | return false; |
| 7038 | } |
| 7039 | |
| 7040 | const CXXOperatorCallExpr *GetSubscriptOrCallOperator(CXCursor Cursor) { |
| 7041 | if (!clang_isExpression(Cursor.kind)) |
| 7042 | return nullptr; |
| 7043 | |
| 7044 | const Expr *E = getCursorExpr(Cursor); |
| 7045 | if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 7046 | const OverloadedOperatorKind Kind = OCE->getOperator(); |
| 7047 | if (Kind == OO_Call || Kind == OO_Subscript) |
| 7048 | return OCE; |
| 7049 | } |
| 7050 | |
| 7051 | return nullptr; |
| 7052 | } |
| 7053 | |
| 7054 | AnnotateTokensWorker::PostChildrenActions |
| 7055 | AnnotateTokensWorker::DetermineChildActions(CXCursor Cursor) const { |
| 7056 | PostChildrenActions actions; |
| 7057 | |
| 7058 | // The DeclRefExpr of CXXOperatorCallExpr refering to the custom operator is |
| 7059 | // visited before the arguments to the operator call. For the Call and |
| 7060 | // Subscript operator the range of this DeclRefExpr includes the whole call |
| 7061 | // expression, so that all tokens in that range would be mapped to the |
| 7062 | // operator function, including the tokens of the arguments. To avoid that, |
| 7063 | // ensure to visit this DeclRefExpr as last node. |
| 7064 | if (const auto *OCE = GetSubscriptOrCallOperator(Cursor)) { |
| 7065 | const Expr *Callee = OCE->getCallee(); |
| 7066 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) { |
| 7067 | const Expr *SubExpr = ICE->getSubExpr(); |
| 7068 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(SubExpr)) { |
Fangrui Song | cabb36d | 2018-11-20 08:00:00 +0000 | [diff] [blame] | 7069 | const Decl *parentDecl = getCursorDecl(Cursor); |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7070 | CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor); |
| 7071 | |
| 7072 | // Visit the DeclRefExpr as last. |
| 7073 | CXCursor cxChild = MakeCXCursor(DRE, parentDecl, TU); |
| 7074 | actions.push_back({cxChild, PostChildrenAction::Postpone}); |
| 7075 | |
| 7076 | // The parent of the DeclRefExpr, an ImplicitCastExpr, has an equally |
| 7077 | // wide range as the DeclRefExpr. We can skip visiting this entirely. |
| 7078 | cxChild = MakeCXCursor(ICE, parentDecl, TU); |
| 7079 | actions.push_back({cxChild, PostChildrenAction::Ignore}); |
| 7080 | } |
| 7081 | } |
| 7082 | } |
| 7083 | |
| 7084 | return actions; |
| 7085 | } |
| 7086 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7087 | static inline void updateCursorAnnotation(CXCursor &Cursor, |
| 7088 | const CXCursor &updateC) { |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7089 | if (clang_isInvalid(updateC.kind) || !clang_isInvalid(Cursor.kind)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7090 | return; |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7091 | Cursor = updateC; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7092 | } |
| 7093 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7094 | /// It annotates and advances tokens with a cursor until the comparison |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7095 | //// between the cursor location and the source range is the same as |
| 7096 | /// \arg compResult. |
| 7097 | /// |
| 7098 | /// Pass RangeBefore to annotate tokens with a cursor until a range is reached. |
| 7099 | /// Pass RangeOverlap to annotate tokens inside a range. |
| 7100 | void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC, |
| 7101 | RangeComparisonResult compResult, |
| 7102 | SourceRange range) { |
| 7103 | while (MoreTokens()) { |
| 7104 | const unsigned I = NextToken(); |
| 7105 | if (isFunctionMacroToken(I)) |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7106 | if (!annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range)) |
| 7107 | return; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7108 | |
| 7109 | SourceLocation TokLoc = GetTokenLoc(I); |
| 7110 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7111 | updateCursorAnnotation(Cursors[I], updateC); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7112 | AdvanceToken(); |
| 7113 | continue; |
| 7114 | } |
| 7115 | break; |
| 7116 | } |
| 7117 | } |
| 7118 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7119 | /// Special annotation handling for macro argument tokens. |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7120 | /// \returns true if it advanced beyond all macro tokens, false otherwise. |
| 7121 | bool AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens( |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7122 | CXCursor updateC, |
| 7123 | RangeComparisonResult compResult, |
| 7124 | SourceRange range) { |
| 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; |
| 7138 | |
| 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 | |
| 7158 | enum CXChildVisitResult |
| 7159 | AnnotateTokensWorker::Visit(CXCursor cursor, 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) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7170 | if (const ObjCPropertyDecl *Property |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7171 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor))) |
| 7172 | HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0; |
| 7173 | } |
| 7174 | // Objective-C methods can have context-sensitive keywords. |
| 7175 | else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl || |
| 7176 | cursor.kind == CXCursor_ObjCClassMethodDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7177 | if (const ObjCMethodDecl *Method |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7178 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 7179 | if (Method->getObjCDeclQualifier()) |
| 7180 | HasContextSensitiveKeywords = true; |
| 7181 | else { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 7182 | for (const auto *P : Method->parameters()) { |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 7183 | if (P->getObjCDeclQualifier()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7184 | HasContextSensitiveKeywords = true; |
| 7185 | break; |
| 7186 | } |
| 7187 | } |
| 7188 | } |
| 7189 | } |
| 7190 | } |
| 7191 | // C++ methods can have context-sensitive keywords. |
| 7192 | else if (cursor.kind == CXCursor_CXXMethod) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7193 | if (const CXXMethodDecl *Method |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7194 | = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) { |
| 7195 | if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>()) |
| 7196 | HasContextSensitiveKeywords = true; |
| 7197 | } |
| 7198 | } |
| 7199 | // C++ classes can have context-sensitive keywords. |
| 7200 | else if (cursor.kind == CXCursor_StructDecl || |
| 7201 | cursor.kind == CXCursor_ClassDecl || |
| 7202 | cursor.kind == CXCursor_ClassTemplate || |
| 7203 | cursor.kind == CXCursor_ClassTemplatePartialSpecialization) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7204 | if (const Decl *D = getCursorDecl(cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7205 | if (D->hasAttr<FinalAttr>()) |
| 7206 | HasContextSensitiveKeywords = true; |
| 7207 | } |
| 7208 | } |
Argyrios Kyrtzidis | 990b386 | 2013-06-04 18:24:30 +0000 | [diff] [blame] | 7209 | |
| 7210 | // Don't override a property annotation with its getter/setter method. |
| 7211 | if (cursor.kind == CXCursor_ObjCInstanceMethodDecl && |
| 7212 | parent.kind == CXCursor_ObjCPropertyDecl) |
| 7213 | return CXChildVisit_Continue; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7214 | |
| 7215 | if (clang_isPreprocessing(cursor.kind)) { |
| 7216 | // Items in the preprocessing record are kept separate from items in |
| 7217 | // declarations, so we keep a separate token index. |
| 7218 | unsigned SavedTokIdx = TokIdx; |
| 7219 | TokIdx = PreprocessingTokIdx; |
| 7220 | |
| 7221 | // Skip tokens up until we catch up to the beginning of the preprocessing |
| 7222 | // entry. |
| 7223 | while (MoreTokens()) { |
| 7224 | const unsigned I = NextToken(); |
| 7225 | SourceLocation TokLoc = GetTokenLoc(I); |
| 7226 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 7227 | case RangeBefore: |
| 7228 | AdvanceToken(); |
| 7229 | continue; |
| 7230 | case RangeAfter: |
| 7231 | case RangeOverlap: |
| 7232 | break; |
| 7233 | } |
| 7234 | break; |
| 7235 | } |
| 7236 | |
| 7237 | // Look at all of the tokens within this range. |
| 7238 | while (MoreTokens()) { |
| 7239 | const unsigned I = NextToken(); |
| 7240 | SourceLocation TokLoc = GetTokenLoc(I); |
| 7241 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 7242 | case RangeBefore: |
| 7243 | llvm_unreachable("Infeasible"); |
| 7244 | case RangeAfter: |
| 7245 | break; |
| 7246 | case RangeOverlap: |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 7247 | // For macro expansions, just note where the beginning of the macro |
| 7248 | // expansion occurs. |
| 7249 | if (cursor.kind == CXCursor_MacroExpansion) { |
| 7250 | if (TokLoc == cursorRange.getBegin()) |
| 7251 | Cursors[I] = cursor; |
| 7252 | AdvanceToken(); |
| 7253 | break; |
| 7254 | } |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7255 | // We may have already annotated macro names inside macro definitions. |
| 7256 | if (Cursors[I].kind != CXCursor_MacroExpansion) |
| 7257 | Cursors[I] = cursor; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7258 | AdvanceToken(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7259 | continue; |
| 7260 | } |
| 7261 | break; |
| 7262 | } |
| 7263 | |
| 7264 | // Save the preprocessing token index; restore the non-preprocessing |
| 7265 | // token index. |
| 7266 | PreprocessingTokIdx = TokIdx; |
| 7267 | TokIdx = SavedTokIdx; |
| 7268 | return CXChildVisit_Recurse; |
| 7269 | } |
| 7270 | |
| 7271 | if (cursorRange.isInvalid()) |
| 7272 | return CXChildVisit_Continue; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7273 | |
| 7274 | unsigned BeforeReachingCursorIdx = NextToken(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7275 | const enum CXCursorKind cursorK = clang_getCursorKind(cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7276 | const enum CXCursorKind K = clang_getCursorKind(parent); |
| 7277 | const CXCursor updateC = |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7278 | (clang_isInvalid(K) || K == CXCursor_TranslationUnit || |
| 7279 | // Attributes are annotated out-of-order, skip tokens until we reach it. |
| 7280 | clang_isAttribute(cursor.kind)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7281 | ? clang_getNullCursor() : parent; |
| 7282 | |
| 7283 | annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange); |
| 7284 | |
| 7285 | // Avoid having the cursor of an expression "overwrite" the annotation of the |
| 7286 | // variable declaration that it belongs to. |
| 7287 | // This can happen for C++ constructor expressions whose range generally |
| 7288 | // include the variable declaration, e.g.: |
| 7289 | // 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] | 7290 | if (clang_isExpression(cursorK) && MoreTokens()) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 7291 | const Expr *E = getCursorExpr(cursor); |
Fangrui Song | cabb36d | 2018-11-20 08:00:00 +0000 | [diff] [blame] | 7292 | if (const Decl *D = getCursorDecl(cursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7293 | const unsigned I = NextToken(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7294 | if (E->getBeginLoc().isValid() && D->getLocation().isValid() && |
| 7295 | E->getBeginLoc() == D->getLocation() && |
| 7296 | E->getBeginLoc() == GetTokenLoc(I)) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7297 | updateCursorAnnotation(Cursors[I], updateC); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7298 | AdvanceToken(); |
| 7299 | } |
| 7300 | } |
| 7301 | } |
| 7302 | |
| 7303 | // Before recursing into the children keep some state that we are going |
| 7304 | // to use in the AnnotateTokensWorker::postVisitChildren callback to do some |
| 7305 | // extra work after the child nodes are visited. |
| 7306 | // Note that we don't call VisitChildren here to avoid traversing statements |
| 7307 | // code-recursively which can blow the stack. |
| 7308 | |
| 7309 | PostChildrenInfo Info; |
| 7310 | Info.Cursor = cursor; |
| 7311 | Info.CursorRange = cursorRange; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7312 | Info.BeforeReachingCursorIdx = BeforeReachingCursorIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7313 | Info.BeforeChildrenTokenIdx = NextToken(); |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7314 | Info.ChildActions = DetermineChildActions(cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7315 | PostChildrenInfos.push_back(Info); |
| 7316 | |
| 7317 | return CXChildVisit_Recurse; |
| 7318 | } |
| 7319 | |
| 7320 | bool AnnotateTokensWorker::postVisitChildren(CXCursor cursor) { |
| 7321 | if (PostChildrenInfos.empty()) |
| 7322 | return false; |
| 7323 | const PostChildrenInfo &Info = PostChildrenInfos.back(); |
| 7324 | if (!clang_equalCursors(Info.Cursor, cursor)) |
| 7325 | return false; |
| 7326 | |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7327 | HandlePostPonedChildCursors(Info); |
| 7328 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7329 | const unsigned BeforeChildren = Info.BeforeChildrenTokenIdx; |
| 7330 | const unsigned AfterChildren = NextToken(); |
| 7331 | SourceRange cursorRange = Info.CursorRange; |
| 7332 | |
| 7333 | // Scan the tokens that are at the end of the cursor, but are not captured |
| 7334 | // but the child cursors. |
| 7335 | annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange); |
| 7336 | |
| 7337 | // Scan the tokens that are at the beginning of the cursor, but are not |
| 7338 | // capture by the child cursors. |
| 7339 | for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { |
| 7340 | if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 7341 | break; |
| 7342 | |
| 7343 | Cursors[I] = cursor; |
| 7344 | } |
| 7345 | |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7346 | // Attributes are annotated out-of-order, rewind TokIdx to when we first |
| 7347 | // encountered the attribute cursor. |
| 7348 | if (clang_isAttribute(cursor.kind)) |
| 7349 | TokIdx = Info.BeforeReachingCursorIdx; |
| 7350 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7351 | PostChildrenInfos.pop_back(); |
| 7352 | return false; |
| 7353 | } |
| 7354 | |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7355 | void AnnotateTokensWorker::HandlePostPonedChildCursors( |
| 7356 | const PostChildrenInfo &Info) { |
| 7357 | for (const auto &ChildAction : Info.ChildActions) { |
| 7358 | if (ChildAction.action == PostChildrenAction::Postpone) { |
| 7359 | HandlePostPonedChildCursor(ChildAction.cursor, |
| 7360 | Info.BeforeChildrenTokenIdx); |
| 7361 | } |
| 7362 | } |
| 7363 | } |
| 7364 | |
| 7365 | void AnnotateTokensWorker::HandlePostPonedChildCursor( |
| 7366 | CXCursor Cursor, unsigned StartTokenIndex) { |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7367 | unsigned I = StartTokenIndex; |
| 7368 | |
| 7369 | // The bracket tokens of a Call or Subscript operator are mapped to |
| 7370 | // CallExpr/CXXOperatorCallExpr because we skipped visiting the corresponding |
| 7371 | // DeclRefExpr. Remap these tokens to the DeclRefExpr cursors. |
| 7372 | for (unsigned RefNameRangeNr = 0; I < NumTokens; RefNameRangeNr++) { |
Nikolai Kosjar | 2a647e7 | 2019-05-08 13:19:29 +0000 | [diff] [blame] | 7373 | const CXSourceRange CXRefNameRange = clang_getCursorReferenceNameRange( |
| 7374 | Cursor, CXNameRange_WantQualifier, RefNameRangeNr); |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7375 | if (clang_Range_isNull(CXRefNameRange)) |
| 7376 | break; // All ranges handled. |
| 7377 | |
| 7378 | SourceRange RefNameRange = cxloc::translateCXSourceRange(CXRefNameRange); |
| 7379 | while (I < NumTokens) { |
| 7380 | const SourceLocation TokenLocation = GetTokenLoc(I); |
| 7381 | if (!TokenLocation.isValid()) |
| 7382 | break; |
| 7383 | |
| 7384 | // Adapt the end range, because LocationCompare() reports |
| 7385 | // RangeOverlap even for the not-inclusive end location. |
| 7386 | const SourceLocation fixedEnd = |
| 7387 | RefNameRange.getEnd().getLocWithOffset(-1); |
| 7388 | RefNameRange = SourceRange(RefNameRange.getBegin(), fixedEnd); |
| 7389 | |
| 7390 | const RangeComparisonResult ComparisonResult = |
| 7391 | LocationCompare(SrcMgr, TokenLocation, RefNameRange); |
| 7392 | |
| 7393 | if (ComparisonResult == RangeOverlap) { |
| 7394 | Cursors[I++] = Cursor; |
| 7395 | } else if (ComparisonResult == RangeBefore) { |
| 7396 | ++I; // Not relevant token, check next one. |
| 7397 | } else if (ComparisonResult == RangeAfter) { |
| 7398 | break; // All tokens updated for current range, check next. |
| 7399 | } |
| 7400 | } |
| 7401 | } |
| 7402 | } |
| 7403 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7404 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 7405 | CXCursor parent, |
| 7406 | CXClientData client_data) { |
| 7407 | return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); |
| 7408 | } |
| 7409 | |
| 7410 | static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor, |
| 7411 | CXClientData client_data) { |
| 7412 | return static_cast<AnnotateTokensWorker*>(client_data)-> |
| 7413 | postVisitChildren(cursor); |
| 7414 | } |
| 7415 | |
| 7416 | namespace { |
| 7417 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7418 | /// Uses the macro expansions in the preprocessing record to find |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7419 | /// and mark tokens that are macro arguments. This info is used by the |
| 7420 | /// AnnotateTokensWorker. |
| 7421 | class MarkMacroArgTokensVisitor { |
| 7422 | SourceManager &SM; |
| 7423 | CXToken *Tokens; |
| 7424 | unsigned NumTokens; |
| 7425 | unsigned CurIdx; |
| 7426 | |
| 7427 | public: |
| 7428 | MarkMacroArgTokensVisitor(SourceManager &SM, |
| 7429 | CXToken *tokens, unsigned numTokens) |
| 7430 | : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { } |
| 7431 | |
| 7432 | CXChildVisitResult visit(CXCursor cursor, CXCursor parent) { |
| 7433 | if (cursor.kind != CXCursor_MacroExpansion) |
| 7434 | return CXChildVisit_Continue; |
| 7435 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7436 | SourceRange macroRange = getCursorMacroExpansion(cursor).getSourceRange(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7437 | if (macroRange.getBegin() == macroRange.getEnd()) |
| 7438 | return CXChildVisit_Continue; // it's not a function macro. |
| 7439 | |
| 7440 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 7441 | if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx), |
| 7442 | macroRange.getBegin())) |
| 7443 | break; |
| 7444 | } |
| 7445 | |
| 7446 | if (CurIdx == NumTokens) |
| 7447 | return CXChildVisit_Break; |
| 7448 | |
| 7449 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 7450 | SourceLocation tokLoc = getTokenLoc(CurIdx); |
| 7451 | if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd())) |
| 7452 | break; |
| 7453 | |
| 7454 | setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc)); |
| 7455 | } |
| 7456 | |
| 7457 | if (CurIdx == NumTokens) |
| 7458 | return CXChildVisit_Break; |
| 7459 | |
| 7460 | return CXChildVisit_Continue; |
| 7461 | } |
| 7462 | |
| 7463 | private: |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 7464 | CXToken &getTok(unsigned Idx) { |
| 7465 | assert(Idx < NumTokens); |
| 7466 | return Tokens[Idx]; |
| 7467 | } |
| 7468 | const CXToken &getTok(unsigned Idx) const { |
| 7469 | assert(Idx < NumTokens); |
| 7470 | return Tokens[Idx]; |
| 7471 | } |
| 7472 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7473 | SourceLocation getTokenLoc(unsigned tokI) { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 7474 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7475 | } |
| 7476 | |
| 7477 | void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) { |
| 7478 | // The third field is reserved and currently not used. Use it here |
| 7479 | // to mark macro arg expanded tokens with their expanded locations. |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 7480 | getTok(tokI).int_data[3] = loc.getRawEncoding(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7481 | } |
| 7482 | }; |
| 7483 | |
| 7484 | } // end anonymous namespace |
| 7485 | |
| 7486 | static CXChildVisitResult |
| 7487 | MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent, |
| 7488 | CXClientData client_data) { |
| 7489 | return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor, |
| 7490 | parent); |
| 7491 | } |
| 7492 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7493 | /// Used by \c annotatePreprocessorTokens. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7494 | /// \returns true if lexing was finished, false otherwise. |
| 7495 | static bool lexNext(Lexer &Lex, Token &Tok, |
| 7496 | unsigned &NextIdx, unsigned NumTokens) { |
| 7497 | if (NextIdx >= NumTokens) |
| 7498 | return true; |
| 7499 | |
| 7500 | ++NextIdx; |
| 7501 | Lex.LexFromRawLexer(Tok); |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 7502 | return Tok.is(tok::eof); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7503 | } |
| 7504 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7505 | static void annotatePreprocessorTokens(CXTranslationUnit TU, |
| 7506 | SourceRange RegionOfInterest, |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7507 | CXCursor *Cursors, |
| 7508 | CXToken *Tokens, |
| 7509 | unsigned NumTokens) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7510 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7511 | |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7512 | Preprocessor &PP = CXXUnit->getPreprocessor(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7513 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 7514 | std::pair<FileID, unsigned> BeginLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 7515 | = SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getBegin()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7516 | std::pair<FileID, unsigned> EndLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 7517 | = SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getEnd()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7518 | |
| 7519 | if (BeginLocInfo.first != EndLocInfo.first) |
| 7520 | return; |
| 7521 | |
| 7522 | StringRef Buffer; |
| 7523 | bool Invalid = false; |
| 7524 | Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 7525 | if (Buffer.empty() || Invalid) |
| 7526 | return; |
| 7527 | |
| 7528 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 7529 | CXXUnit->getASTContext().getLangOpts(), |
| 7530 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, |
| 7531 | Buffer.end()); |
| 7532 | Lex.SetCommentRetentionState(true); |
| 7533 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7534 | unsigned NextIdx = 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7535 | // Lex tokens in raw mode until we hit the end of the range, to avoid |
| 7536 | // entering #includes or expanding macros. |
| 7537 | while (true) { |
| 7538 | Token Tok; |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7539 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 7540 | break; |
| 7541 | unsigned TokIdx = NextIdx-1; |
| 7542 | assert(Tok.getLocation() == |
| 7543 | SourceLocation::getFromRawEncoding(Tokens[TokIdx].int_data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7544 | |
| 7545 | reprocess: |
| 7546 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7547 | // We have found a preprocessing directive. Annotate the tokens |
| 7548 | // appropriately. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7549 | // |
| 7550 | // FIXME: Some simple tests here could identify macro definitions and |
| 7551 | // #undefs, to provide specific cursor kinds for those. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7552 | |
| 7553 | SourceLocation BeginLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7554 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 7555 | break; |
| 7556 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7557 | MacroInfo *MI = nullptr; |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 7558 | if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "define") { |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7559 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 7560 | break; |
| 7561 | |
| 7562 | if (Tok.is(tok::raw_identifier)) { |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 7563 | IdentifierInfo &II = |
| 7564 | PP.getIdentifierTable().get(Tok.getRawIdentifier()); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7565 | SourceLocation MappedTokLoc = |
| 7566 | CXXUnit->mapLocationToPreamble(Tok.getLocation()); |
| 7567 | MI = getMacroInfo(II, MappedTokLoc, TU); |
| 7568 | } |
| 7569 | } |
| 7570 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7571 | bool finished = false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7572 | do { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7573 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) { |
| 7574 | finished = true; |
| 7575 | break; |
| 7576 | } |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7577 | // If we are in a macro definition, check if the token was ever a |
| 7578 | // macro name and annotate it if that's the case. |
| 7579 | if (MI) { |
| 7580 | SourceLocation SaveLoc = Tok.getLocation(); |
| 7581 | Tok.setLocation(CXXUnit->mapLocationToPreamble(SaveLoc)); |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 7582 | MacroDefinitionRecord *MacroDef = |
| 7583 | checkForMacroInMacroDefinition(MI, Tok, TU); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7584 | Tok.setLocation(SaveLoc); |
| 7585 | if (MacroDef) |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 7586 | Cursors[NextIdx - 1] = |
| 7587 | MakeMacroExpansionCursor(MacroDef, Tok.getLocation(), TU); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7588 | } |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7589 | } while (!Tok.isAtStartOfLine()); |
| 7590 | |
| 7591 | unsigned LastIdx = finished ? NextIdx-1 : NextIdx-2; |
| 7592 | assert(TokIdx <= LastIdx); |
| 7593 | SourceLocation EndLoc = |
| 7594 | SourceLocation::getFromRawEncoding(Tokens[LastIdx].int_data[1]); |
| 7595 | CXCursor Cursor = |
| 7596 | MakePreprocessingDirectiveCursor(SourceRange(BeginLoc, EndLoc), TU); |
| 7597 | |
| 7598 | for (; TokIdx <= LastIdx; ++TokIdx) |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7599 | updateCursorAnnotation(Cursors[TokIdx], Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7600 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7601 | if (finished) |
| 7602 | break; |
| 7603 | goto reprocess; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7604 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7605 | } |
| 7606 | } |
| 7607 | |
| 7608 | // This gets run a separate thread to avoid stack blowout. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 7609 | static void clang_annotateTokensImpl(CXTranslationUnit TU, ASTUnit *CXXUnit, |
| 7610 | CXToken *Tokens, unsigned NumTokens, |
| 7611 | CXCursor *Cursors) { |
Dmitri Gribenko | 183436e | 2013-01-26 21:49:50 +0000 | [diff] [blame] | 7612 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7613 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) |
| 7614 | setThreadBackgroundPriority(); |
| 7615 | |
| 7616 | // Determine the region of interest, which contains all of the tokens. |
| 7617 | SourceRange RegionOfInterest; |
| 7618 | RegionOfInterest.setBegin( |
| 7619 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0]))); |
| 7620 | RegionOfInterest.setEnd( |
| 7621 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, |
| 7622 | Tokens[NumTokens-1]))); |
| 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. |
| 7640 | MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(), |
| 7641 | Tokens, NumTokens); |
| 7642 | CursorVisitor MacroArgMarker(TU, |
| 7643 | MarkMacroArgTokensVisitorDelegate, &Visitor, |
| 7644 | /*VisitPreprocessorLast=*/true, |
| 7645 | /*VisitIncludedEntities=*/false, |
| 7646 | RegionOfInterest); |
| 7647 | MacroArgMarker.visitPreprocessedEntitiesInRegion(); |
| 7648 | } |
| 7649 | |
| 7650 | // Annotate all of the source locations in the region of interest that map to |
| 7651 | // a specific cursor. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7652 | AnnotateTokensWorker W(Tokens, Cursors, NumTokens, TU, RegionOfInterest); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7653 | |
| 7654 | // FIXME: We use a ridiculous stack size here because the data-recursion |
| 7655 | // algorithm uses a large stack frame than the non-data recursive version, |
| 7656 | // and AnnotationTokensWorker currently transforms the data-recursion |
| 7657 | // algorithm back into a traditional recursion by explicitly calling |
| 7658 | // VisitChildren(). We will need to remove this explicit recursive call. |
| 7659 | W.AnnotateTokens(); |
| 7660 | |
| 7661 | // If we ran into any entities that involve context-sensitive keywords, |
| 7662 | // take another pass through the tokens to mark them as such. |
| 7663 | if (W.hasContextSensitiveKeywords()) { |
| 7664 | for (unsigned I = 0; I != NumTokens; ++I) { |
| 7665 | if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier) |
| 7666 | continue; |
| 7667 | |
| 7668 | if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) { |
| 7669 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7670 | if (const ObjCPropertyDecl *Property |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7671 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) { |
| 7672 | if (Property->getPropertyAttributesAsWritten() != 0 && |
| 7673 | llvm::StringSwitch<bool>(II->getName()) |
| 7674 | .Case("readonly", true) |
| 7675 | .Case("assign", true) |
| 7676 | .Case("unsafe_unretained", true) |
| 7677 | .Case("readwrite", true) |
| 7678 | .Case("retain", true) |
| 7679 | .Case("copy", true) |
| 7680 | .Case("nonatomic", true) |
| 7681 | .Case("atomic", true) |
| 7682 | .Case("getter", true) |
| 7683 | .Case("setter", true) |
| 7684 | .Case("strong", true) |
| 7685 | .Case("weak", true) |
Manman Ren | 04fd4d8 | 2016-05-31 23:22:04 +0000 | [diff] [blame] | 7686 | .Case("class", true) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7687 | .Default(false)) |
| 7688 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 7689 | } |
| 7690 | continue; |
| 7691 | } |
| 7692 | |
| 7693 | if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl || |
| 7694 | Cursors[I].kind == CXCursor_ObjCClassMethodDecl) { |
| 7695 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 7696 | if (llvm::StringSwitch<bool>(II->getName()) |
| 7697 | .Case("in", true) |
| 7698 | .Case("out", true) |
| 7699 | .Case("inout", true) |
| 7700 | .Case("oneway", true) |
| 7701 | .Case("bycopy", true) |
| 7702 | .Case("byref", true) |
| 7703 | .Default(false)) |
| 7704 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 7705 | continue; |
| 7706 | } |
| 7707 | |
| 7708 | if (Cursors[I].kind == CXCursor_CXXFinalAttr || |
| 7709 | Cursors[I].kind == CXCursor_CXXOverrideAttr) { |
| 7710 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 7711 | continue; |
| 7712 | } |
| 7713 | } |
| 7714 | } |
| 7715 | } |
| 7716 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7717 | void clang_annotateTokens(CXTranslationUnit TU, |
| 7718 | CXToken *Tokens, unsigned NumTokens, |
| 7719 | CXCursor *Cursors) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7720 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7721 | LOG_BAD_TU(TU); |
| 7722 | return; |
| 7723 | } |
| 7724 | if (NumTokens == 0 || !Tokens || !Cursors) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7725 | LOG_FUNC_SECTION { *Log << "<null input>"; } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7726 | return; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7727 | } |
| 7728 | |
| 7729 | LOG_FUNC_SECTION { |
| 7730 | *Log << TU << ' '; |
| 7731 | CXSourceLocation bloc = clang_getTokenLocation(TU, Tokens[0]); |
| 7732 | CXSourceLocation eloc = clang_getTokenLocation(TU, Tokens[NumTokens-1]); |
| 7733 | *Log << clang_getRange(bloc, eloc); |
| 7734 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7735 | |
| 7736 | // Any token we don't specifically annotate will have a NULL cursor. |
| 7737 | CXCursor C = clang_getNullCursor(); |
| 7738 | for (unsigned I = 0; I != NumTokens; ++I) |
| 7739 | Cursors[I] = C; |
| 7740 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7741 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7742 | if (!CXXUnit) |
| 7743 | return; |
| 7744 | |
| 7745 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 7746 | |
| 7747 | auto AnnotateTokensImpl = [=]() { |
| 7748 | clang_annotateTokensImpl(TU, CXXUnit, Tokens, NumTokens, Cursors); |
| 7749 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7750 | llvm::CrashRecoveryContext CRC; |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 7751 | if (!RunSafely(CRC, AnnotateTokensImpl, GetSafetyThreadStackSize() * 2)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7752 | fprintf(stderr, "libclang: crash detected while annotating tokens\n"); |
| 7753 | } |
| 7754 | } |
| 7755 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7756 | //===----------------------------------------------------------------------===// |
| 7757 | // Operations for querying linkage of a cursor. |
| 7758 | //===----------------------------------------------------------------------===// |
| 7759 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7760 | CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { |
| 7761 | if (!clang_isDeclaration(cursor.kind)) |
| 7762 | return CXLinkage_Invalid; |
| 7763 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7764 | const Decl *D = cxcursor::getCursorDecl(cursor); |
| 7765 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7766 | switch (ND->getLinkageInternal()) { |
Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 7767 | case NoLinkage: |
| 7768 | case VisibleNoLinkage: return CXLinkage_NoLinkage; |
Richard Smith | af10ea2 | 2017-07-08 00:37:59 +0000 | [diff] [blame] | 7769 | case ModuleInternalLinkage: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7770 | case InternalLinkage: return CXLinkage_Internal; |
| 7771 | case UniqueExternalLinkage: return CXLinkage_UniqueExternal; |
Richard Smith | af10ea2 | 2017-07-08 00:37:59 +0000 | [diff] [blame] | 7772 | case ModuleLinkage: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7773 | case ExternalLinkage: return CXLinkage_External; |
| 7774 | }; |
| 7775 | |
| 7776 | return CXLinkage_Invalid; |
| 7777 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7778 | |
| 7779 | //===----------------------------------------------------------------------===// |
Ehsan Akhgari | b743de7 | 2016-05-31 15:55:51 +0000 | [diff] [blame] | 7780 | // Operations for querying visibility of a cursor. |
| 7781 | //===----------------------------------------------------------------------===// |
| 7782 | |
Ehsan Akhgari | b743de7 | 2016-05-31 15:55:51 +0000 | [diff] [blame] | 7783 | CXVisibilityKind clang_getCursorVisibility(CXCursor cursor) { |
| 7784 | if (!clang_isDeclaration(cursor.kind)) |
| 7785 | return CXVisibility_Invalid; |
| 7786 | |
| 7787 | const Decl *D = cxcursor::getCursorDecl(cursor); |
| 7788 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
| 7789 | switch (ND->getVisibility()) { |
| 7790 | case HiddenVisibility: return CXVisibility_Hidden; |
| 7791 | case ProtectedVisibility: return CXVisibility_Protected; |
| 7792 | case DefaultVisibility: return CXVisibility_Default; |
| 7793 | }; |
| 7794 | |
| 7795 | return CXVisibility_Invalid; |
| 7796 | } |
Ehsan Akhgari | b743de7 | 2016-05-31 15:55:51 +0000 | [diff] [blame] | 7797 | |
| 7798 | //===----------------------------------------------------------------------===// |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7799 | // Operations for querying language of a cursor. |
| 7800 | //===----------------------------------------------------------------------===// |
| 7801 | |
| 7802 | static CXLanguageKind getDeclLanguage(const Decl *D) { |
| 7803 | if (!D) |
| 7804 | return CXLanguage_C; |
| 7805 | |
| 7806 | switch (D->getKind()) { |
| 7807 | default: |
| 7808 | break; |
| 7809 | case Decl::ImplicitParam: |
| 7810 | case Decl::ObjCAtDefsField: |
| 7811 | case Decl::ObjCCategory: |
| 7812 | case Decl::ObjCCategoryImpl: |
| 7813 | case Decl::ObjCCompatibleAlias: |
| 7814 | case Decl::ObjCImplementation: |
| 7815 | case Decl::ObjCInterface: |
| 7816 | case Decl::ObjCIvar: |
| 7817 | case Decl::ObjCMethod: |
| 7818 | case Decl::ObjCProperty: |
| 7819 | case Decl::ObjCPropertyImpl: |
| 7820 | case Decl::ObjCProtocol: |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 7821 | case Decl::ObjCTypeParam: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7822 | return CXLanguage_ObjC; |
| 7823 | case Decl::CXXConstructor: |
| 7824 | case Decl::CXXConversion: |
| 7825 | case Decl::CXXDestructor: |
| 7826 | case Decl::CXXMethod: |
| 7827 | case Decl::CXXRecord: |
| 7828 | case Decl::ClassTemplate: |
| 7829 | case Decl::ClassTemplatePartialSpecialization: |
| 7830 | case Decl::ClassTemplateSpecialization: |
| 7831 | case Decl::Friend: |
| 7832 | case Decl::FriendTemplate: |
| 7833 | case Decl::FunctionTemplate: |
| 7834 | case Decl::LinkageSpec: |
| 7835 | case Decl::Namespace: |
| 7836 | case Decl::NamespaceAlias: |
| 7837 | case Decl::NonTypeTemplateParm: |
| 7838 | case Decl::StaticAssert: |
| 7839 | case Decl::TemplateTemplateParm: |
| 7840 | case Decl::TemplateTypeParm: |
| 7841 | case Decl::UnresolvedUsingTypename: |
| 7842 | case Decl::UnresolvedUsingValue: |
| 7843 | case Decl::Using: |
| 7844 | case Decl::UsingDirective: |
| 7845 | case Decl::UsingShadow: |
| 7846 | return CXLanguage_CPlusPlus; |
| 7847 | } |
| 7848 | |
| 7849 | return CXLanguage_C; |
| 7850 | } |
| 7851 | |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7852 | static CXAvailabilityKind getCursorAvailabilityForDecl(const Decl *D) { |
| 7853 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()) |
Manuel Klimek | 8e3a7ed | 2015-09-25 17:53:16 +0000 | [diff] [blame] | 7854 | return CXAvailability_NotAvailable; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7855 | |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7856 | switch (D->getAvailability()) { |
| 7857 | case AR_Available: |
| 7858 | case AR_NotYetIntroduced: |
| 7859 | if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(D)) |
Benjamin Kramer | 656363d | 2013-10-15 18:53:18 +0000 | [diff] [blame] | 7860 | return getCursorAvailabilityForDecl( |
| 7861 | cast<Decl>(EnumConst->getDeclContext())); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7862 | return CXAvailability_Available; |
| 7863 | |
| 7864 | case AR_Deprecated: |
| 7865 | return CXAvailability_Deprecated; |
| 7866 | |
| 7867 | case AR_Unavailable: |
| 7868 | return CXAvailability_NotAvailable; |
| 7869 | } |
Benjamin Kramer | 656363d | 2013-10-15 18:53:18 +0000 | [diff] [blame] | 7870 | |
| 7871 | llvm_unreachable("Unknown availability kind!"); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7872 | } |
| 7873 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7874 | enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { |
| 7875 | if (clang_isDeclaration(cursor.kind)) |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7876 | if (const Decl *D = cxcursor::getCursorDecl(cursor)) |
| 7877 | return getCursorAvailabilityForDecl(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7878 | |
| 7879 | return CXAvailability_Available; |
| 7880 | } |
| 7881 | |
| 7882 | static CXVersion convertVersion(VersionTuple In) { |
| 7883 | CXVersion Out = { -1, -1, -1 }; |
| 7884 | if (In.empty()) |
| 7885 | return Out; |
| 7886 | |
| 7887 | Out.Major = In.getMajor(); |
| 7888 | |
NAKAMURA Takumi | c2b5d1f | 2013-02-21 02:32:34 +0000 | [diff] [blame] | 7889 | Optional<unsigned> Minor = In.getMinor(); |
| 7890 | if (Minor.hasValue()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7891 | Out.Minor = *Minor; |
| 7892 | else |
| 7893 | return Out; |
| 7894 | |
NAKAMURA Takumi | c2b5d1f | 2013-02-21 02:32:34 +0000 | [diff] [blame] | 7895 | Optional<unsigned> Subminor = In.getSubminor(); |
| 7896 | if (Subminor.hasValue()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7897 | Out.Subminor = *Subminor; |
| 7898 | |
| 7899 | return Out; |
| 7900 | } |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7901 | |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7902 | static void getCursorPlatformAvailabilityForDecl( |
| 7903 | const Decl *D, int *always_deprecated, CXString *deprecated_message, |
| 7904 | int *always_unavailable, CXString *unavailable_message, |
| 7905 | SmallVectorImpl<AvailabilityAttr *> &AvailabilityAttrs) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7906 | bool HadAvailAttr = false; |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 7907 | for (auto A : D->attrs()) { |
| 7908 | if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(A)) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7909 | HadAvailAttr = true; |
| 7910 | if (always_deprecated) |
| 7911 | *always_deprecated = 1; |
Nico Weber | aacf031 | 2014-04-24 05:16:45 +0000 | [diff] [blame] | 7912 | if (deprecated_message) { |
Argyrios Kyrtzidis | edfe07f | 2014-04-24 06:05:40 +0000 | [diff] [blame] | 7913 | clang_disposeString(*deprecated_message); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7914 | *deprecated_message = cxstring::createDup(Deprecated->getMessage()); |
Nico Weber | aacf031 | 2014-04-24 05:16:45 +0000 | [diff] [blame] | 7915 | } |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7916 | continue; |
| 7917 | } |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7918 | |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 7919 | if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(A)) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7920 | HadAvailAttr = true; |
| 7921 | if (always_unavailable) |
| 7922 | *always_unavailable = 1; |
| 7923 | if (unavailable_message) { |
Argyrios Kyrtzidis | edfe07f | 2014-04-24 06:05:40 +0000 | [diff] [blame] | 7924 | clang_disposeString(*unavailable_message); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7925 | *unavailable_message = cxstring::createDup(Unavailable->getMessage()); |
| 7926 | } |
| 7927 | continue; |
| 7928 | } |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7929 | |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 7930 | if (AvailabilityAttr *Avail = dyn_cast<AvailabilityAttr>(A)) { |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7931 | AvailabilityAttrs.push_back(Avail); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7932 | HadAvailAttr = true; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7933 | } |
| 7934 | } |
| 7935 | |
| 7936 | if (!HadAvailAttr) |
| 7937 | if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(D)) |
| 7938 | return getCursorPlatformAvailabilityForDecl( |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7939 | cast<Decl>(EnumConst->getDeclContext()), always_deprecated, |
| 7940 | deprecated_message, always_unavailable, unavailable_message, |
| 7941 | AvailabilityAttrs); |
| 7942 | |
| 7943 | if (AvailabilityAttrs.empty()) |
| 7944 | return; |
| 7945 | |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 7946 | llvm::sort(AvailabilityAttrs, |
Mandeep Singh Grang | c205d8c | 2018-03-27 16:50:00 +0000 | [diff] [blame] | 7947 | [](AvailabilityAttr *LHS, AvailabilityAttr *RHS) { |
| 7948 | return LHS->getPlatform()->getName() < |
| 7949 | RHS->getPlatform()->getName(); |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 7950 | }); |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7951 | ASTContext &Ctx = D->getASTContext(); |
| 7952 | auto It = std::unique( |
| 7953 | AvailabilityAttrs.begin(), AvailabilityAttrs.end(), |
| 7954 | [&Ctx](AvailabilityAttr *LHS, AvailabilityAttr *RHS) { |
| 7955 | if (LHS->getPlatform() != RHS->getPlatform()) |
| 7956 | return false; |
| 7957 | |
| 7958 | if (LHS->getIntroduced() == RHS->getIntroduced() && |
| 7959 | LHS->getDeprecated() == RHS->getDeprecated() && |
| 7960 | LHS->getObsoleted() == RHS->getObsoleted() && |
| 7961 | LHS->getMessage() == RHS->getMessage() && |
| 7962 | LHS->getReplacement() == RHS->getReplacement()) |
| 7963 | return true; |
| 7964 | |
| 7965 | if ((!LHS->getIntroduced().empty() && !RHS->getIntroduced().empty()) || |
| 7966 | (!LHS->getDeprecated().empty() && !RHS->getDeprecated().empty()) || |
| 7967 | (!LHS->getObsoleted().empty() && !RHS->getObsoleted().empty())) |
| 7968 | return false; |
| 7969 | |
| 7970 | if (LHS->getIntroduced().empty() && !RHS->getIntroduced().empty()) |
| 7971 | LHS->setIntroduced(Ctx, RHS->getIntroduced()); |
| 7972 | |
| 7973 | if (LHS->getDeprecated().empty() && !RHS->getDeprecated().empty()) { |
| 7974 | LHS->setDeprecated(Ctx, RHS->getDeprecated()); |
| 7975 | if (LHS->getMessage().empty()) |
| 7976 | LHS->setMessage(Ctx, RHS->getMessage()); |
| 7977 | if (LHS->getReplacement().empty()) |
| 7978 | LHS->setReplacement(Ctx, RHS->getReplacement()); |
| 7979 | } |
| 7980 | |
| 7981 | if (LHS->getObsoleted().empty() && !RHS->getObsoleted().empty()) { |
| 7982 | LHS->setObsoleted(Ctx, RHS->getObsoleted()); |
| 7983 | if (LHS->getMessage().empty()) |
| 7984 | LHS->setMessage(Ctx, RHS->getMessage()); |
| 7985 | if (LHS->getReplacement().empty()) |
| 7986 | LHS->setReplacement(Ctx, RHS->getReplacement()); |
| 7987 | } |
| 7988 | |
| 7989 | return true; |
| 7990 | }); |
| 7991 | AvailabilityAttrs.erase(It, AvailabilityAttrs.end()); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7992 | } |
| 7993 | |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7994 | int clang_getCursorPlatformAvailability(CXCursor cursor, int *always_deprecated, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7995 | CXString *deprecated_message, |
| 7996 | int *always_unavailable, |
| 7997 | CXString *unavailable_message, |
| 7998 | CXPlatformAvailability *availability, |
| 7999 | int availability_size) { |
| 8000 | if (always_deprecated) |
| 8001 | *always_deprecated = 0; |
| 8002 | if (deprecated_message) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 8003 | *deprecated_message = cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8004 | if (always_unavailable) |
| 8005 | *always_unavailable = 0; |
| 8006 | if (unavailable_message) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 8007 | *unavailable_message = cxstring::createEmpty(); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 8008 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8009 | if (!clang_isDeclaration(cursor.kind)) |
| 8010 | return 0; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 8011 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8012 | const Decl *D = cxcursor::getCursorDecl(cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8013 | if (!D) |
| 8014 | return 0; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 8015 | |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 8016 | SmallVector<AvailabilityAttr *, 8> AvailabilityAttrs; |
| 8017 | getCursorPlatformAvailabilityForDecl(D, always_deprecated, deprecated_message, |
| 8018 | always_unavailable, unavailable_message, |
| 8019 | AvailabilityAttrs); |
| 8020 | for (const auto &Avail : |
| 8021 | llvm::enumerate(llvm::makeArrayRef(AvailabilityAttrs) |
| 8022 | .take_front(availability_size))) { |
| 8023 | availability[Avail.index()].Platform = |
| 8024 | cxstring::createDup(Avail.value()->getPlatform()->getName()); |
| 8025 | availability[Avail.index()].Introduced = |
| 8026 | convertVersion(Avail.value()->getIntroduced()); |
| 8027 | availability[Avail.index()].Deprecated = |
| 8028 | convertVersion(Avail.value()->getDeprecated()); |
| 8029 | availability[Avail.index()].Obsoleted = |
| 8030 | convertVersion(Avail.value()->getObsoleted()); |
| 8031 | availability[Avail.index()].Unavailable = Avail.value()->getUnavailable(); |
| 8032 | availability[Avail.index()].Message = |
| 8033 | cxstring::createDup(Avail.value()->getMessage()); |
| 8034 | } |
| 8035 | |
| 8036 | return AvailabilityAttrs.size(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8037 | } |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 8038 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8039 | void clang_disposeCXPlatformAvailability(CXPlatformAvailability *availability) { |
| 8040 | clang_disposeString(availability->Platform); |
| 8041 | clang_disposeString(availability->Message); |
| 8042 | } |
| 8043 | |
| 8044 | CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { |
| 8045 | if (clang_isDeclaration(cursor.kind)) |
| 8046 | return getDeclLanguage(cxcursor::getCursorDecl(cursor)); |
| 8047 | |
| 8048 | return CXLanguage_Invalid; |
| 8049 | } |
| 8050 | |
Saleem Abdulrasool | 50bc565 | 2017-09-13 02:15:09 +0000 | [diff] [blame] | 8051 | CXTLSKind clang_getCursorTLSKind(CXCursor cursor) { |
| 8052 | const Decl *D = cxcursor::getCursorDecl(cursor); |
| 8053 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 8054 | switch (VD->getTLSKind()) { |
| 8055 | case VarDecl::TLS_None: |
| 8056 | return CXTLS_None; |
| 8057 | case VarDecl::TLS_Dynamic: |
| 8058 | return CXTLS_Dynamic; |
| 8059 | case VarDecl::TLS_Static: |
| 8060 | return CXTLS_Static; |
| 8061 | } |
| 8062 | } |
| 8063 | |
| 8064 | return CXTLS_None; |
| 8065 | } |
| 8066 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8067 | /// If the given cursor is the "templated" declaration |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 8068 | /// describing a class or function template, return the class or |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8069 | /// function template. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8070 | static const Decl *maybeGetTemplateCursor(const Decl *D) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8071 | if (!D) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8072 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8073 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8074 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8075 | if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate()) |
| 8076 | return FunTmpl; |
| 8077 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8078 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8079 | if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate()) |
| 8080 | return ClassTmpl; |
| 8081 | |
| 8082 | return D; |
| 8083 | } |
| 8084 | |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 8085 | |
| 8086 | enum CX_StorageClass clang_Cursor_getStorageClass(CXCursor C) { |
| 8087 | StorageClass sc = SC_None; |
| 8088 | const Decl *D = getCursorDecl(C); |
| 8089 | if (D) { |
| 8090 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 8091 | sc = FD->getStorageClass(); |
| 8092 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 8093 | sc = VD->getStorageClass(); |
| 8094 | } else { |
| 8095 | return CX_SC_Invalid; |
| 8096 | } |
| 8097 | } else { |
| 8098 | return CX_SC_Invalid; |
| 8099 | } |
| 8100 | switch (sc) { |
| 8101 | case SC_None: |
| 8102 | return CX_SC_None; |
| 8103 | case SC_Extern: |
| 8104 | return CX_SC_Extern; |
| 8105 | case SC_Static: |
| 8106 | return CX_SC_Static; |
| 8107 | case SC_PrivateExtern: |
| 8108 | return CX_SC_PrivateExtern; |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 8109 | case SC_Auto: |
| 8110 | return CX_SC_Auto; |
| 8111 | case SC_Register: |
| 8112 | return CX_SC_Register; |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 8113 | } |
Kaelyn Takata | ab61e70 | 2014-10-15 18:03:26 +0000 | [diff] [blame] | 8114 | llvm_unreachable("Unhandled storage class!"); |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 8115 | } |
| 8116 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8117 | CXCursor clang_getCursorSemanticParent(CXCursor cursor) { |
| 8118 | if (clang_isDeclaration(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8119 | if (const Decl *D = getCursorDecl(cursor)) { |
| 8120 | const DeclContext *DC = D->getDeclContext(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8121 | if (!DC) |
| 8122 | return clang_getNullCursor(); |
| 8123 | |
| 8124 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 8125 | getCursorTU(cursor)); |
| 8126 | } |
| 8127 | } |
| 8128 | |
| 8129 | if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8130 | if (const Decl *D = getCursorDecl(cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8131 | return MakeCXCursor(D, getCursorTU(cursor)); |
| 8132 | } |
| 8133 | |
| 8134 | return clang_getNullCursor(); |
| 8135 | } |
| 8136 | |
| 8137 | CXCursor clang_getCursorLexicalParent(CXCursor cursor) { |
| 8138 | if (clang_isDeclaration(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8139 | if (const Decl *D = getCursorDecl(cursor)) { |
| 8140 | const DeclContext *DC = D->getLexicalDeclContext(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8141 | if (!DC) |
| 8142 | return clang_getNullCursor(); |
| 8143 | |
| 8144 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 8145 | getCursorTU(cursor)); |
| 8146 | } |
| 8147 | } |
| 8148 | |
| 8149 | // FIXME: Note that we can't easily compute the lexical context of a |
| 8150 | // statement or expression, so we return nothing. |
| 8151 | return clang_getNullCursor(); |
| 8152 | } |
| 8153 | |
| 8154 | CXFile clang_getIncludedFile(CXCursor cursor) { |
| 8155 | if (cursor.kind != CXCursor_InclusionDirective) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8156 | return nullptr; |
| 8157 | |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 8158 | const InclusionDirective *ID = getCursorInclusionDirective(cursor); |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 8159 | return const_cast<FileEntry *>(ID->getFile()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8160 | } |
| 8161 | |
Argyrios Kyrtzidis | 9adfd8a | 2013-04-18 22:15:49 +0000 | [diff] [blame] | 8162 | unsigned clang_Cursor_getObjCPropertyAttributes(CXCursor C, unsigned reserved) { |
| 8163 | if (C.kind != CXCursor_ObjCPropertyDecl) |
| 8164 | return CXObjCPropertyAttr_noattr; |
| 8165 | |
| 8166 | unsigned Result = CXObjCPropertyAttr_noattr; |
| 8167 | const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(getCursorDecl(C)); |
| 8168 | ObjCPropertyDecl::PropertyAttributeKind Attr = |
| 8169 | PD->getPropertyAttributesAsWritten(); |
| 8170 | |
| 8171 | #define SET_CXOBJCPROP_ATTR(A) \ |
| 8172 | if (Attr & ObjCPropertyDecl::OBJC_PR_##A) \ |
| 8173 | Result |= CXObjCPropertyAttr_##A |
| 8174 | SET_CXOBJCPROP_ATTR(readonly); |
| 8175 | SET_CXOBJCPROP_ATTR(getter); |
| 8176 | SET_CXOBJCPROP_ATTR(assign); |
| 8177 | SET_CXOBJCPROP_ATTR(readwrite); |
| 8178 | SET_CXOBJCPROP_ATTR(retain); |
| 8179 | SET_CXOBJCPROP_ATTR(copy); |
| 8180 | SET_CXOBJCPROP_ATTR(nonatomic); |
| 8181 | SET_CXOBJCPROP_ATTR(setter); |
| 8182 | SET_CXOBJCPROP_ATTR(atomic); |
| 8183 | SET_CXOBJCPROP_ATTR(weak); |
| 8184 | SET_CXOBJCPROP_ATTR(strong); |
| 8185 | SET_CXOBJCPROP_ATTR(unsafe_unretained); |
Manman Ren | 04fd4d8 | 2016-05-31 23:22:04 +0000 | [diff] [blame] | 8186 | SET_CXOBJCPROP_ATTR(class); |
Argyrios Kyrtzidis | 9adfd8a | 2013-04-18 22:15:49 +0000 | [diff] [blame] | 8187 | #undef SET_CXOBJCPROP_ATTR |
| 8188 | |
| 8189 | return Result; |
| 8190 | } |
| 8191 | |
Michael Wu | 6e88f53 | 2018-08-03 05:38:29 +0000 | [diff] [blame] | 8192 | CXString clang_Cursor_getObjCPropertyGetterName(CXCursor C) { |
| 8193 | if (C.kind != CXCursor_ObjCPropertyDecl) |
| 8194 | return cxstring::createNull(); |
| 8195 | |
| 8196 | const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(getCursorDecl(C)); |
| 8197 | Selector sel = PD->getGetterName(); |
| 8198 | if (sel.isNull()) |
| 8199 | return cxstring::createNull(); |
| 8200 | |
| 8201 | return cxstring::createDup(sel.getAsString()); |
| 8202 | } |
| 8203 | |
| 8204 | CXString clang_Cursor_getObjCPropertySetterName(CXCursor C) { |
| 8205 | if (C.kind != CXCursor_ObjCPropertyDecl) |
| 8206 | return cxstring::createNull(); |
| 8207 | |
| 8208 | const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(getCursorDecl(C)); |
| 8209 | Selector sel = PD->getSetterName(); |
| 8210 | if (sel.isNull()) |
| 8211 | return cxstring::createNull(); |
| 8212 | |
| 8213 | return cxstring::createDup(sel.getAsString()); |
| 8214 | } |
| 8215 | |
Argyrios Kyrtzidis | 9d9bc01 | 2013-04-18 23:29:12 +0000 | [diff] [blame] | 8216 | unsigned clang_Cursor_getObjCDeclQualifiers(CXCursor C) { |
| 8217 | if (!clang_isDeclaration(C.kind)) |
| 8218 | return CXObjCDeclQualifier_None; |
| 8219 | |
| 8220 | Decl::ObjCDeclQualifier QT = Decl::OBJC_TQ_None; |
| 8221 | const Decl *D = getCursorDecl(C); |
| 8222 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 8223 | QT = MD->getObjCDeclQualifier(); |
| 8224 | else if (const ParmVarDecl *PD = dyn_cast<ParmVarDecl>(D)) |
| 8225 | QT = PD->getObjCDeclQualifier(); |
| 8226 | if (QT == Decl::OBJC_TQ_None) |
| 8227 | return CXObjCDeclQualifier_None; |
| 8228 | |
| 8229 | unsigned Result = CXObjCDeclQualifier_None; |
| 8230 | if (QT & Decl::OBJC_TQ_In) Result |= CXObjCDeclQualifier_In; |
| 8231 | if (QT & Decl::OBJC_TQ_Inout) Result |= CXObjCDeclQualifier_Inout; |
| 8232 | if (QT & Decl::OBJC_TQ_Out) Result |= CXObjCDeclQualifier_Out; |
| 8233 | if (QT & Decl::OBJC_TQ_Bycopy) Result |= CXObjCDeclQualifier_Bycopy; |
| 8234 | if (QT & Decl::OBJC_TQ_Byref) Result |= CXObjCDeclQualifier_Byref; |
| 8235 | if (QT & Decl::OBJC_TQ_Oneway) Result |= CXObjCDeclQualifier_Oneway; |
| 8236 | |
| 8237 | return Result; |
| 8238 | } |
| 8239 | |
Argyrios Kyrtzidis | 7b50fc5 | 2013-07-05 20:44:37 +0000 | [diff] [blame] | 8240 | unsigned clang_Cursor_isObjCOptional(CXCursor C) { |
| 8241 | if (!clang_isDeclaration(C.kind)) |
| 8242 | return 0; |
| 8243 | |
| 8244 | const Decl *D = getCursorDecl(C); |
| 8245 | if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 8246 | return PD->getPropertyImplementation() == ObjCPropertyDecl::Optional; |
| 8247 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 8248 | return MD->getImplementationControl() == ObjCMethodDecl::Optional; |
| 8249 | |
| 8250 | return 0; |
| 8251 | } |
| 8252 | |
Argyrios Kyrtzidis | 23814e4 | 2013-04-18 23:53:05 +0000 | [diff] [blame] | 8253 | unsigned clang_Cursor_isVariadic(CXCursor C) { |
| 8254 | if (!clang_isDeclaration(C.kind)) |
| 8255 | return 0; |
| 8256 | |
| 8257 | const Decl *D = getCursorDecl(C); |
| 8258 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 8259 | return FD->isVariadic(); |
| 8260 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 8261 | return MD->isVariadic(); |
| 8262 | |
| 8263 | return 0; |
| 8264 | } |
| 8265 | |
Argyrios Kyrtzidis | 0381cc7 | 2017-05-10 15:10:36 +0000 | [diff] [blame] | 8266 | unsigned clang_Cursor_isExternalSymbol(CXCursor C, |
| 8267 | CXString *language, CXString *definedIn, |
| 8268 | unsigned *isGenerated) { |
| 8269 | if (!clang_isDeclaration(C.kind)) |
| 8270 | return 0; |
| 8271 | |
| 8272 | const Decl *D = getCursorDecl(C); |
| 8273 | |
Argyrios Kyrtzidis | 11d7048 | 2017-05-20 04:11:33 +0000 | [diff] [blame] | 8274 | if (auto *attr = D->getExternalSourceSymbolAttr()) { |
Argyrios Kyrtzidis | 0381cc7 | 2017-05-10 15:10:36 +0000 | [diff] [blame] | 8275 | if (language) |
| 8276 | *language = cxstring::createDup(attr->getLanguage()); |
| 8277 | if (definedIn) |
| 8278 | *definedIn = cxstring::createDup(attr->getDefinedIn()); |
| 8279 | if (isGenerated) |
| 8280 | *isGenerated = attr->getGeneratedDeclaration(); |
| 8281 | return 1; |
| 8282 | } |
| 8283 | return 0; |
| 8284 | } |
| 8285 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8286 | CXSourceRange clang_Cursor_getCommentRange(CXCursor C) { |
| 8287 | if (!clang_isDeclaration(C.kind)) |
| 8288 | return clang_getNullRange(); |
| 8289 | |
| 8290 | const Decl *D = getCursorDecl(C); |
| 8291 | ASTContext &Context = getCursorContext(C); |
| 8292 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 8293 | if (!RC) |
| 8294 | return clang_getNullRange(); |
| 8295 | |
| 8296 | return cxloc::translateSourceRange(Context, RC->getSourceRange()); |
| 8297 | } |
| 8298 | |
| 8299 | CXString clang_Cursor_getRawCommentText(CXCursor C) { |
| 8300 | if (!clang_isDeclaration(C.kind)) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 8301 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8302 | |
| 8303 | const Decl *D = getCursorDecl(C); |
| 8304 | ASTContext &Context = getCursorContext(C); |
| 8305 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 8306 | StringRef RawText = RC ? RC->getRawText(Context.getSourceManager()) : |
| 8307 | StringRef(); |
| 8308 | |
| 8309 | // Don't duplicate the string because RawText points directly into source |
| 8310 | // code. |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 8311 | return cxstring::createRef(RawText); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8312 | } |
| 8313 | |
| 8314 | CXString clang_Cursor_getBriefCommentText(CXCursor C) { |
| 8315 | if (!clang_isDeclaration(C.kind)) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 8316 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8317 | |
| 8318 | const Decl *D = getCursorDecl(C); |
| 8319 | const ASTContext &Context = getCursorContext(C); |
| 8320 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 8321 | |
| 8322 | if (RC) { |
| 8323 | StringRef BriefText = RC->getBriefText(Context); |
| 8324 | |
| 8325 | // Don't duplicate the string because RawComment ensures that this memory |
| 8326 | // will not go away. |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 8327 | return cxstring::createRef(BriefText); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8328 | } |
| 8329 | |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 8330 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8331 | } |
| 8332 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8333 | CXModule clang_Cursor_getModule(CXCursor C) { |
| 8334 | if (C.kind == CXCursor_ModuleImportDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8335 | if (const ImportDecl *ImportD = |
| 8336 | dyn_cast_or_null<ImportDecl>(getCursorDecl(C))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8337 | return ImportD->getImportedModule(); |
| 8338 | } |
| 8339 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8340 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8341 | } |
| 8342 | |
Argyrios Kyrtzidis | f6d49c3 | 2014-05-14 23:14:37 +0000 | [diff] [blame] | 8343 | CXModule clang_getModuleForFile(CXTranslationUnit TU, CXFile File) { |
| 8344 | if (isNotUsableTU(TU)) { |
| 8345 | LOG_BAD_TU(TU); |
| 8346 | return nullptr; |
| 8347 | } |
| 8348 | if (!File) |
| 8349 | return nullptr; |
| 8350 | FileEntry *FE = static_cast<FileEntry *>(File); |
| 8351 | |
| 8352 | ASTUnit &Unit = *cxtu::getASTUnit(TU); |
| 8353 | HeaderSearch &HS = Unit.getPreprocessor().getHeaderSearchInfo(); |
| 8354 | ModuleMap::KnownHeader Header = HS.findModuleForHeader(FE); |
| 8355 | |
Richard Smith | feb54b6 | 2014-10-23 02:01:19 +0000 | [diff] [blame] | 8356 | return Header.getModule(); |
Argyrios Kyrtzidis | f6d49c3 | 2014-05-14 23:14:37 +0000 | [diff] [blame] | 8357 | } |
| 8358 | |
Argyrios Kyrtzidis | 12fdb9e | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 8359 | CXFile clang_Module_getASTFile(CXModule CXMod) { |
| 8360 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8361 | return nullptr; |
Argyrios Kyrtzidis | 12fdb9e | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 8362 | Module *Mod = static_cast<Module*>(CXMod); |
| 8363 | return const_cast<FileEntry *>(Mod->getASTFile()); |
| 8364 | } |
| 8365 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8366 | CXModule clang_Module_getParent(CXModule CXMod) { |
| 8367 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8368 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8369 | Module *Mod = static_cast<Module*>(CXMod); |
| 8370 | return Mod->Parent; |
| 8371 | } |
| 8372 | |
| 8373 | CXString clang_Module_getName(CXModule CXMod) { |
| 8374 | if (!CXMod) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 8375 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8376 | Module *Mod = static_cast<Module*>(CXMod); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 8377 | return cxstring::createDup(Mod->Name); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8378 | } |
| 8379 | |
| 8380 | CXString clang_Module_getFullName(CXModule CXMod) { |
| 8381 | if (!CXMod) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 8382 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8383 | Module *Mod = static_cast<Module*>(CXMod); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 8384 | return cxstring::createDup(Mod->getFullModuleName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8385 | } |
| 8386 | |
Argyrios Kyrtzidis | 884337f | 2014-05-15 04:44:25 +0000 | [diff] [blame] | 8387 | int clang_Module_isSystem(CXModule CXMod) { |
| 8388 | if (!CXMod) |
| 8389 | return 0; |
| 8390 | Module *Mod = static_cast<Module*>(CXMod); |
| 8391 | return Mod->IsSystem; |
| 8392 | } |
| 8393 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 8394 | unsigned clang_Module_getNumTopLevelHeaders(CXTranslationUnit TU, |
| 8395 | CXModule CXMod) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 8396 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 8397 | LOG_BAD_TU(TU); |
| 8398 | return 0; |
| 8399 | } |
| 8400 | if (!CXMod) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8401 | return 0; |
| 8402 | Module *Mod = static_cast<Module*>(CXMod); |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 8403 | FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager(); |
| 8404 | ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr); |
| 8405 | return TopHeaders.size(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8406 | } |
| 8407 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 8408 | CXFile clang_Module_getTopLevelHeader(CXTranslationUnit TU, |
| 8409 | CXModule CXMod, unsigned Index) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 8410 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 8411 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8412 | return nullptr; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 8413 | } |
| 8414 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8415 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8416 | Module *Mod = static_cast<Module*>(CXMod); |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 8417 | FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8418 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 8419 | ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr); |
| 8420 | if (Index < TopHeaders.size()) |
| 8421 | return const_cast<FileEntry *>(TopHeaders[Index]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8422 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8423 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8424 | } |
| 8425 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8426 | //===----------------------------------------------------------------------===// |
| 8427 | // C++ AST instrospection. |
| 8428 | //===----------------------------------------------------------------------===// |
| 8429 | |
Jonathan Coe | 2956535 | 2016-04-27 12:48:25 +0000 | [diff] [blame] | 8430 | unsigned clang_CXXConstructor_isDefaultConstructor(CXCursor C) { |
| 8431 | if (!clang_isDeclaration(C.kind)) |
| 8432 | return 0; |
| 8433 | |
| 8434 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8435 | const CXXConstructorDecl *Constructor = |
| 8436 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 8437 | return (Constructor && Constructor->isDefaultConstructor()) ? 1 : 0; |
| 8438 | } |
| 8439 | |
| 8440 | unsigned clang_CXXConstructor_isCopyConstructor(CXCursor C) { |
| 8441 | if (!clang_isDeclaration(C.kind)) |
| 8442 | return 0; |
| 8443 | |
| 8444 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8445 | const CXXConstructorDecl *Constructor = |
| 8446 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 8447 | return (Constructor && Constructor->isCopyConstructor()) ? 1 : 0; |
| 8448 | } |
| 8449 | |
| 8450 | unsigned clang_CXXConstructor_isMoveConstructor(CXCursor C) { |
| 8451 | if (!clang_isDeclaration(C.kind)) |
| 8452 | return 0; |
| 8453 | |
| 8454 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8455 | const CXXConstructorDecl *Constructor = |
| 8456 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 8457 | return (Constructor && Constructor->isMoveConstructor()) ? 1 : 0; |
| 8458 | } |
| 8459 | |
| 8460 | unsigned clang_CXXConstructor_isConvertingConstructor(CXCursor C) { |
| 8461 | if (!clang_isDeclaration(C.kind)) |
| 8462 | return 0; |
| 8463 | |
| 8464 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8465 | const CXXConstructorDecl *Constructor = |
| 8466 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 8467 | // Passing 'false' excludes constructors marked 'explicit'. |
| 8468 | return (Constructor && Constructor->isConvertingConstructor(false)) ? 1 : 0; |
| 8469 | } |
| 8470 | |
Saleem Abdulrasool | 6ea75db | 2015-10-27 15:50:22 +0000 | [diff] [blame] | 8471 | unsigned clang_CXXField_isMutable(CXCursor C) { |
| 8472 | if (!clang_isDeclaration(C.kind)) |
| 8473 | return 0; |
| 8474 | |
| 8475 | if (const auto D = cxcursor::getCursorDecl(C)) |
| 8476 | if (const auto FD = dyn_cast_or_null<FieldDecl>(D)) |
| 8477 | return FD->isMutable() ? 1 : 0; |
| 8478 | return 0; |
| 8479 | } |
| 8480 | |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 8481 | unsigned clang_CXXMethod_isPureVirtual(CXCursor C) { |
| 8482 | if (!clang_isDeclaration(C.kind)) |
| 8483 | return 0; |
| 8484 | |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 8485 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 8486 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8487 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 8488 | return (Method && Method->isVirtual() && Method->isPure()) ? 1 : 0; |
| 8489 | } |
| 8490 | |
Dmitri Gribenko | e570ede | 2014-04-07 14:59:13 +0000 | [diff] [blame] | 8491 | unsigned clang_CXXMethod_isConst(CXCursor C) { |
| 8492 | if (!clang_isDeclaration(C.kind)) |
| 8493 | return 0; |
| 8494 | |
| 8495 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8496 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8497 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 8498 | return (Method && Method->getMethodQualifiers().hasConst()) ? 1 : 0; |
Dmitri Gribenko | e570ede | 2014-04-07 14:59:13 +0000 | [diff] [blame] | 8499 | } |
| 8500 | |
Jonathan Coe | 2956535 | 2016-04-27 12:48:25 +0000 | [diff] [blame] | 8501 | unsigned clang_CXXMethod_isDefaulted(CXCursor C) { |
| 8502 | if (!clang_isDeclaration(C.kind)) |
| 8503 | return 0; |
| 8504 | |
| 8505 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8506 | const CXXMethodDecl *Method = |
| 8507 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
| 8508 | return (Method && Method->isDefaulted()) ? 1 : 0; |
| 8509 | } |
| 8510 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8511 | unsigned clang_CXXMethod_isStatic(CXCursor C) { |
| 8512 | if (!clang_isDeclaration(C.kind)) |
| 8513 | return 0; |
| 8514 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8515 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 8516 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8517 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8518 | return (Method && Method->isStatic()) ? 1 : 0; |
| 8519 | } |
| 8520 | |
| 8521 | unsigned clang_CXXMethod_isVirtual(CXCursor C) { |
| 8522 | if (!clang_isDeclaration(C.kind)) |
| 8523 | return 0; |
| 8524 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8525 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 8526 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8527 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8528 | return (Method && Method->isVirtual()) ? 1 : 0; |
| 8529 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8530 | |
Alex Lorenz | 34ccadc | 2017-12-14 22:01:50 +0000 | [diff] [blame] | 8531 | unsigned clang_CXXRecord_isAbstract(CXCursor C) { |
| 8532 | if (!clang_isDeclaration(C.kind)) |
| 8533 | return 0; |
| 8534 | |
| 8535 | const auto *D = cxcursor::getCursorDecl(C); |
| 8536 | const auto *RD = dyn_cast_or_null<CXXRecordDecl>(D); |
| 8537 | if (RD) |
| 8538 | RD = RD->getDefinition(); |
| 8539 | return (RD && RD->isAbstract()) ? 1 : 0; |
| 8540 | } |
| 8541 | |
Alex Lorenz | ff7f42e | 2017-07-12 11:35:11 +0000 | [diff] [blame] | 8542 | unsigned clang_EnumDecl_isScoped(CXCursor C) { |
| 8543 | if (!clang_isDeclaration(C.kind)) |
| 8544 | return 0; |
| 8545 | |
| 8546 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8547 | auto *Enum = dyn_cast_or_null<EnumDecl>(D); |
| 8548 | return (Enum && Enum->isScoped()) ? 1 : 0; |
| 8549 | } |
| 8550 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8551 | //===----------------------------------------------------------------------===// |
| 8552 | // Attribute introspection. |
| 8553 | //===----------------------------------------------------------------------===// |
| 8554 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8555 | CXType clang_getIBOutletCollectionType(CXCursor C) { |
| 8556 | if (C.kind != CXCursor_IBOutletCollectionAttr) |
| 8557 | return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C)); |
| 8558 | |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 8559 | const IBOutletCollectionAttr *A = |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8560 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); |
| 8561 | |
| 8562 | return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C)); |
| 8563 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8564 | |
| 8565 | //===----------------------------------------------------------------------===// |
| 8566 | // Inspecting memory usage. |
| 8567 | //===----------------------------------------------------------------------===// |
| 8568 | |
| 8569 | typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries; |
| 8570 | |
| 8571 | static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries, |
| 8572 | enum CXTUResourceUsageKind k, |
| 8573 | unsigned long amount) { |
| 8574 | CXTUResourceUsageEntry entry = { k, amount }; |
| 8575 | entries.push_back(entry); |
| 8576 | } |
| 8577 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8578 | const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) { |
| 8579 | const char *str = ""; |
| 8580 | switch (kind) { |
| 8581 | case CXTUResourceUsage_AST: |
| 8582 | str = "ASTContext: expressions, declarations, and types"; |
| 8583 | break; |
| 8584 | case CXTUResourceUsage_Identifiers: |
| 8585 | str = "ASTContext: identifiers"; |
| 8586 | break; |
| 8587 | case CXTUResourceUsage_Selectors: |
| 8588 | str = "ASTContext: selectors"; |
| 8589 | break; |
| 8590 | case CXTUResourceUsage_GlobalCompletionResults: |
| 8591 | str = "Code completion: cached global results"; |
| 8592 | break; |
| 8593 | case CXTUResourceUsage_SourceManagerContentCache: |
| 8594 | str = "SourceManager: content cache allocator"; |
| 8595 | break; |
| 8596 | case CXTUResourceUsage_AST_SideTables: |
| 8597 | str = "ASTContext: side tables"; |
| 8598 | break; |
| 8599 | case CXTUResourceUsage_SourceManager_Membuffer_Malloc: |
| 8600 | str = "SourceManager: malloc'ed memory buffers"; |
| 8601 | break; |
| 8602 | case CXTUResourceUsage_SourceManager_Membuffer_MMap: |
| 8603 | str = "SourceManager: mmap'ed memory buffers"; |
| 8604 | break; |
| 8605 | case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc: |
| 8606 | str = "ExternalASTSource: malloc'ed memory buffers"; |
| 8607 | break; |
| 8608 | case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap: |
| 8609 | str = "ExternalASTSource: mmap'ed memory buffers"; |
| 8610 | break; |
| 8611 | case CXTUResourceUsage_Preprocessor: |
| 8612 | str = "Preprocessor: malloc'ed memory"; |
| 8613 | break; |
| 8614 | case CXTUResourceUsage_PreprocessingRecord: |
| 8615 | str = "Preprocessor: PreprocessingRecord"; |
| 8616 | break; |
| 8617 | case CXTUResourceUsage_SourceManager_DataStructures: |
| 8618 | str = "SourceManager: data structures and tables"; |
| 8619 | break; |
| 8620 | case CXTUResourceUsage_Preprocessor_HeaderSearch: |
| 8621 | str = "Preprocessor: header search tables"; |
| 8622 | break; |
| 8623 | } |
| 8624 | return str; |
| 8625 | } |
| 8626 | |
| 8627 | CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 8628 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 8629 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8630 | CXTUResourceUsage usage = { (void*) nullptr, 0, nullptr }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8631 | return usage; |
| 8632 | } |
| 8633 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8634 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 8635 | std::unique_ptr<MemUsageEntries> entries(new MemUsageEntries()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8636 | ASTContext &astContext = astUnit->getASTContext(); |
| 8637 | |
| 8638 | // How much memory is used by AST nodes and types? |
| 8639 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST, |
| 8640 | (unsigned long) astContext.getASTAllocatedMemory()); |
| 8641 | |
| 8642 | // How much memory is used by identifiers? |
| 8643 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers, |
| 8644 | (unsigned long) astContext.Idents.getAllocator().getTotalMemory()); |
| 8645 | |
| 8646 | // How much memory is used for selectors? |
| 8647 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors, |
| 8648 | (unsigned long) astContext.Selectors.getTotalMemory()); |
| 8649 | |
| 8650 | // How much memory is used by ASTContext's side tables? |
| 8651 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables, |
| 8652 | (unsigned long) astContext.getSideTableAllocatedMemory()); |
| 8653 | |
| 8654 | // How much memory is used for caching global code completion results? |
| 8655 | unsigned long completionBytes = 0; |
| 8656 | if (GlobalCodeCompletionAllocator *completionAllocator = |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 8657 | astUnit->getCachedCompletionAllocator().get()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8658 | completionBytes = completionAllocator->getTotalMemory(); |
| 8659 | } |
| 8660 | createCXTUResourceUsageEntry(*entries, |
| 8661 | CXTUResourceUsage_GlobalCompletionResults, |
| 8662 | completionBytes); |
| 8663 | |
| 8664 | // How much memory is being used by SourceManager's content cache? |
| 8665 | createCXTUResourceUsageEntry(*entries, |
| 8666 | CXTUResourceUsage_SourceManagerContentCache, |
| 8667 | (unsigned long) astContext.getSourceManager().getContentCacheSize()); |
| 8668 | |
| 8669 | // How much memory is being used by the MemoryBuffer's in SourceManager? |
| 8670 | const SourceManager::MemoryBufferSizes &srcBufs = |
| 8671 | astUnit->getSourceManager().getMemoryBufferSizes(); |
| 8672 | |
| 8673 | createCXTUResourceUsageEntry(*entries, |
| 8674 | CXTUResourceUsage_SourceManager_Membuffer_Malloc, |
| 8675 | (unsigned long) srcBufs.malloc_bytes); |
| 8676 | createCXTUResourceUsageEntry(*entries, |
| 8677 | CXTUResourceUsage_SourceManager_Membuffer_MMap, |
| 8678 | (unsigned long) srcBufs.mmap_bytes); |
| 8679 | createCXTUResourceUsageEntry(*entries, |
| 8680 | CXTUResourceUsage_SourceManager_DataStructures, |
| 8681 | (unsigned long) astContext.getSourceManager() |
| 8682 | .getDataStructureSizes()); |
| 8683 | |
| 8684 | // How much memory is being used by the ExternalASTSource? |
| 8685 | if (ExternalASTSource *esrc = astContext.getExternalSource()) { |
| 8686 | const ExternalASTSource::MemoryBufferSizes &sizes = |
| 8687 | esrc->getMemoryBufferSizes(); |
| 8688 | |
| 8689 | createCXTUResourceUsageEntry(*entries, |
| 8690 | CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc, |
| 8691 | (unsigned long) sizes.malloc_bytes); |
| 8692 | createCXTUResourceUsageEntry(*entries, |
| 8693 | CXTUResourceUsage_ExternalASTSource_Membuffer_MMap, |
| 8694 | (unsigned long) sizes.mmap_bytes); |
| 8695 | } |
| 8696 | |
| 8697 | // How much memory is being used by the Preprocessor? |
| 8698 | Preprocessor &pp = astUnit->getPreprocessor(); |
| 8699 | createCXTUResourceUsageEntry(*entries, |
| 8700 | CXTUResourceUsage_Preprocessor, |
| 8701 | pp.getTotalMemory()); |
| 8702 | |
| 8703 | if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) { |
| 8704 | createCXTUResourceUsageEntry(*entries, |
| 8705 | CXTUResourceUsage_PreprocessingRecord, |
| 8706 | pRec->getTotalMemory()); |
| 8707 | } |
| 8708 | |
| 8709 | createCXTUResourceUsageEntry(*entries, |
| 8710 | CXTUResourceUsage_Preprocessor_HeaderSearch, |
| 8711 | pp.getHeaderSearchInfo().getTotalMemory()); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8712 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8713 | CXTUResourceUsage usage = { (void*) entries.get(), |
| 8714 | (unsigned) entries->size(), |
Alexander Kornienko | 6ee521c | 2015-01-23 15:36:10 +0000 | [diff] [blame] | 8715 | !entries->empty() ? &(*entries)[0] : nullptr }; |
Eric Fiselier | e95fc44 | 2016-11-14 07:03:50 +0000 | [diff] [blame] | 8716 | (void)entries.release(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8717 | return usage; |
| 8718 | } |
| 8719 | |
| 8720 | void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) { |
| 8721 | if (usage.data) |
| 8722 | delete (MemUsageEntries*) usage.data; |
| 8723 | } |
| 8724 | |
Argyrios Kyrtzidis | 0e282ef | 2013-12-06 18:55:45 +0000 | [diff] [blame] | 8725 | CXSourceRangeList *clang_getSkippedRanges(CXTranslationUnit TU, CXFile file) { |
| 8726 | CXSourceRangeList *skipped = new CXSourceRangeList; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8727 | skipped->count = 0; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8728 | skipped->ranges = nullptr; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8729 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 8730 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 8731 | LOG_BAD_TU(TU); |
| 8732 | return skipped; |
| 8733 | } |
| 8734 | |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8735 | if (!file) |
| 8736 | return skipped; |
| 8737 | |
| 8738 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
| 8739 | PreprocessingRecord *ppRec = astUnit->getPreprocessor().getPreprocessingRecord(); |
| 8740 | if (!ppRec) |
| 8741 | return skipped; |
| 8742 | |
| 8743 | ASTContext &Ctx = astUnit->getASTContext(); |
| 8744 | SourceManager &sm = Ctx.getSourceManager(); |
| 8745 | FileEntry *fileEntry = static_cast<FileEntry *>(file); |
| 8746 | FileID wantedFileID = sm.translateFile(fileEntry); |
Cameron Desrochers | b60f1b6 | 2018-01-15 19:14:16 +0000 | [diff] [blame] | 8747 | bool isMainFile = wantedFileID == sm.getMainFileID(); |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8748 | |
| 8749 | const std::vector<SourceRange> &SkippedRanges = ppRec->getSkippedRanges(); |
| 8750 | std::vector<SourceRange> wantedRanges; |
| 8751 | for (std::vector<SourceRange>::const_iterator i = SkippedRanges.begin(), ei = SkippedRanges.end(); |
| 8752 | i != ei; ++i) { |
| 8753 | if (sm.getFileID(i->getBegin()) == wantedFileID || sm.getFileID(i->getEnd()) == wantedFileID) |
| 8754 | wantedRanges.push_back(*i); |
Cameron Desrochers | b60f1b6 | 2018-01-15 19:14:16 +0000 | [diff] [blame] | 8755 | else if (isMainFile && (astUnit->isInPreambleFileID(i->getBegin()) || astUnit->isInPreambleFileID(i->getEnd()))) |
| 8756 | wantedRanges.push_back(*i); |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8757 | } |
| 8758 | |
| 8759 | skipped->count = wantedRanges.size(); |
| 8760 | skipped->ranges = new CXSourceRange[skipped->count]; |
| 8761 | for (unsigned i = 0, ei = skipped->count; i != ei; ++i) |
| 8762 | skipped->ranges[i] = cxloc::translateSourceRange(Ctx, wantedRanges[i]); |
| 8763 | |
| 8764 | return skipped; |
| 8765 | } |
| 8766 | |
Cameron Desrochers | d809128 | 2016-08-18 15:43:55 +0000 | [diff] [blame] | 8767 | CXSourceRangeList *clang_getAllSkippedRanges(CXTranslationUnit TU) { |
| 8768 | CXSourceRangeList *skipped = new CXSourceRangeList; |
| 8769 | skipped->count = 0; |
| 8770 | skipped->ranges = nullptr; |
| 8771 | |
| 8772 | if (isNotUsableTU(TU)) { |
| 8773 | LOG_BAD_TU(TU); |
| 8774 | return skipped; |
| 8775 | } |
| 8776 | |
| 8777 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
| 8778 | PreprocessingRecord *ppRec = astUnit->getPreprocessor().getPreprocessingRecord(); |
| 8779 | if (!ppRec) |
| 8780 | return skipped; |
| 8781 | |
| 8782 | ASTContext &Ctx = astUnit->getASTContext(); |
| 8783 | |
| 8784 | const std::vector<SourceRange> &SkippedRanges = ppRec->getSkippedRanges(); |
| 8785 | |
| 8786 | skipped->count = SkippedRanges.size(); |
| 8787 | skipped->ranges = new CXSourceRange[skipped->count]; |
| 8788 | for (unsigned i = 0, ei = skipped->count; i != ei; ++i) |
| 8789 | skipped->ranges[i] = cxloc::translateSourceRange(Ctx, SkippedRanges[i]); |
| 8790 | |
| 8791 | return skipped; |
| 8792 | } |
| 8793 | |
Argyrios Kyrtzidis | 0e282ef | 2013-12-06 18:55:45 +0000 | [diff] [blame] | 8794 | void clang_disposeSourceRangeList(CXSourceRangeList *ranges) { |
| 8795 | if (ranges) { |
| 8796 | delete[] ranges->ranges; |
| 8797 | delete ranges; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8798 | } |
| 8799 | } |
| 8800 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8801 | void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) { |
| 8802 | CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU); |
| 8803 | for (unsigned I = 0; I != Usage.numEntries; ++I) |
| 8804 | fprintf(stderr, " %s: %lu\n", |
| 8805 | clang_getTUResourceUsageName(Usage.entries[I].kind), |
| 8806 | Usage.entries[I].amount); |
| 8807 | |
| 8808 | clang_disposeCXTUResourceUsage(Usage); |
| 8809 | } |
| 8810 | |
| 8811 | //===----------------------------------------------------------------------===// |
| 8812 | // Misc. utility functions. |
| 8813 | //===----------------------------------------------------------------------===// |
| 8814 | |
Richard Smith | 0a7b297 | 2018-07-03 21:34:13 +0000 | [diff] [blame] | 8815 | /// Default to using our desired 8 MB stack size on "safety" threads. |
| 8816 | static unsigned SafetyStackThreadSize = DesiredStackSize; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8817 | |
| 8818 | namespace clang { |
| 8819 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 8820 | bool RunSafely(llvm::CrashRecoveryContext &CRC, llvm::function_ref<void()> Fn, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8821 | unsigned Size) { |
| 8822 | if (!Size) |
| 8823 | Size = GetSafetyThreadStackSize(); |
Erik Verbruggen | 3cc3911 | 2017-11-14 09:34:39 +0000 | [diff] [blame] | 8824 | if (Size && !getenv("LIBCLANG_NOTHREADS")) |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 8825 | return CRC.RunSafelyOnThread(Fn, Size); |
| 8826 | return CRC.RunSafely(Fn); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8827 | } |
| 8828 | |
| 8829 | unsigned GetSafetyThreadStackSize() { |
| 8830 | return SafetyStackThreadSize; |
| 8831 | } |
| 8832 | |
| 8833 | void SetSafetyThreadStackSize(unsigned Value) { |
| 8834 | SafetyStackThreadSize = Value; |
| 8835 | } |
| 8836 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 8837 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8838 | |
| 8839 | void clang::setThreadBackgroundPriority() { |
| 8840 | if (getenv("LIBCLANG_BGPRIO_DISABLE")) |
| 8841 | return; |
| 8842 | |
Nico Weber | 18cfd9f | 2019-04-21 19:18:41 +0000 | [diff] [blame] | 8843 | #if LLVM_ENABLE_THREADS |
Kadir Cetinkaya | b8f82ca | 2019-04-18 13:49:20 +0000 | [diff] [blame] | 8844 | llvm::set_thread_priority(llvm::ThreadPriority::Background); |
Nico Weber | 18cfd9f | 2019-04-21 19:18:41 +0000 | [diff] [blame] | 8845 | #endif |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8846 | } |
| 8847 | |
| 8848 | void cxindex::printDiagsToStderr(ASTUnit *Unit) { |
| 8849 | if (!Unit) |
| 8850 | return; |
| 8851 | |
| 8852 | for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), |
| 8853 | DEnd = Unit->stored_diag_end(); |
| 8854 | D != DEnd; ++D) { |
Ben Langmuir | 749323f | 2014-04-22 17:40:12 +0000 | [diff] [blame] | 8855 | CXStoredDiagnostic Diag(*D, Unit->getLangOpts()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8856 | CXString Msg = clang_formatDiagnostic(&Diag, |
| 8857 | clang_defaultDiagnosticDisplayOptions()); |
| 8858 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 8859 | clang_disposeString(Msg); |
| 8860 | } |
Nico Weber | 1865df4 | 2018-04-27 19:11:14 +0000 | [diff] [blame] | 8861 | #ifdef _WIN32 |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8862 | // On Windows, force a flush, since there may be multiple copies of |
| 8863 | // stderr and stdout in the file system, all with different buffers |
| 8864 | // but writing to the same device. |
| 8865 | fflush(stderr); |
| 8866 | #endif |
| 8867 | } |
| 8868 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8869 | MacroInfo *cxindex::getMacroInfo(const IdentifierInfo &II, |
| 8870 | SourceLocation MacroDefLoc, |
| 8871 | CXTranslationUnit TU){ |
| 8872 | if (MacroDefLoc.isInvalid() || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8873 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8874 | if (!II.hadMacroDefinition()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8875 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8876 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8877 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 8878 | Preprocessor &PP = Unit->getPreprocessor(); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 8879 | MacroDirective *MD = PP.getLocalMacroDirectiveHistory(&II); |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 8880 | if (MD) { |
| 8881 | for (MacroDirective::DefInfo |
| 8882 | Def = MD->getDefinition(); Def; Def = Def.getPreviousDefinition()) { |
| 8883 | if (MacroDefLoc == Def.getMacroInfo()->getDefinitionLoc()) |
| 8884 | return Def.getMacroInfo(); |
| 8885 | } |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8886 | } |
| 8887 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8888 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8889 | } |
| 8890 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 8891 | const MacroInfo *cxindex::getMacroInfo(const MacroDefinitionRecord *MacroDef, |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 8892 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8893 | if (!MacroDef || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8894 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8895 | const IdentifierInfo *II = MacroDef->getName(); |
| 8896 | if (!II) |
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 | return getMacroInfo(*II, MacroDef->getLocation(), TU); |
| 8900 | } |
| 8901 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 8902 | MacroDefinitionRecord * |
| 8903 | cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, const Token &Tok, |
| 8904 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8905 | if (!MI || !TU) |
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 | if (Tok.isNot(tok::raw_identifier)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8908 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8909 | |
| 8910 | if (MI->getNumTokens() == 0) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8911 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8912 | SourceRange DefRange(MI->getReplacementToken(0).getLocation(), |
| 8913 | MI->getDefinitionEndLoc()); |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8914 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8915 | |
| 8916 | // Check that the token is inside the definition and not its argument list. |
| 8917 | SourceManager &SM = Unit->getSourceManager(); |
| 8918 | if (SM.isBeforeInTranslationUnit(Tok.getLocation(), DefRange.getBegin())) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8919 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8920 | if (SM.isBeforeInTranslationUnit(DefRange.getEnd(), Tok.getLocation())) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8921 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8922 | |
| 8923 | Preprocessor &PP = Unit->getPreprocessor(); |
| 8924 | PreprocessingRecord *PPRec = PP.getPreprocessingRecord(); |
| 8925 | if (!PPRec) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8926 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8927 | |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 8928 | IdentifierInfo &II = PP.getIdentifierTable().get(Tok.getRawIdentifier()); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8929 | if (!II.hadMacroDefinition()) |
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 | // Check that the identifier is not one of the macro arguments. |
Faisal Vali | ac506d7 | 2017-07-17 17:18:43 +0000 | [diff] [blame] | 8933 | 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] | 8934 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8935 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 8936 | MacroDirective *InnerMD = PP.getLocalMacroDirectiveHistory(&II); |
Argyrios Kyrtzidis | 09c9e81 | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 8937 | if (!InnerMD) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8938 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8939 | |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 8940 | return PPRec->findMacroDefinition(InnerMD->getMacroInfo()); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8941 | } |
| 8942 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 8943 | MacroDefinitionRecord * |
| 8944 | cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, SourceLocation Loc, |
| 8945 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8946 | if (Loc.isInvalid() || !MI || !TU) |
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 | |
| 8949 | if (MI->getNumTokens() == 0) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8950 | return nullptr; |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8951 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8952 | Preprocessor &PP = Unit->getPreprocessor(); |
| 8953 | if (!PP.getPreprocessingRecord()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8954 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8955 | Loc = Unit->getSourceManager().getSpellingLoc(Loc); |
| 8956 | Token Tok; |
| 8957 | if (PP.getRawToken(Loc, Tok)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8958 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8959 | |
| 8960 | return checkForMacroInMacroDefinition(MI, Tok, TU); |
| 8961 | } |
| 8962 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8963 | CXString clang_getClangVersion() { |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 8964 | return cxstring::createDup(getClangFullVersion()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8965 | } |
| 8966 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8967 | Logger &cxindex::Logger::operator<<(CXTranslationUnit TU) { |
| 8968 | if (TU) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8969 | if (ASTUnit *Unit = cxtu::getASTUnit(TU)) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8970 | LogOS << '<' << Unit->getMainFileName() << '>'; |
Argyrios Kyrtzidis | 37f2ab4 | 2013-03-05 20:21:14 +0000 | [diff] [blame] | 8971 | if (Unit->isMainFileAST()) |
| 8972 | LogOS << " (" << Unit->getASTFileName() << ')'; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8973 | return *this; |
| 8974 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 8975 | } else { |
| 8976 | LogOS << "<NULL TU>"; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8977 | } |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8978 | return *this; |
| 8979 | } |
| 8980 | |
Argyrios Kyrtzidis | ba4b5f8 | 2013-03-08 02:32:26 +0000 | [diff] [blame] | 8981 | Logger &cxindex::Logger::operator<<(const FileEntry *FE) { |
| 8982 | *this << FE->getName(); |
| 8983 | return *this; |
| 8984 | } |
| 8985 | |
| 8986 | Logger &cxindex::Logger::operator<<(CXCursor cursor) { |
| 8987 | CXString cursorName = clang_getCursorDisplayName(cursor); |
| 8988 | *this << cursorName << "@" << clang_getCursorLocation(cursor); |
| 8989 | clang_disposeString(cursorName); |
| 8990 | return *this; |
| 8991 | } |
| 8992 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8993 | Logger &cxindex::Logger::operator<<(CXSourceLocation Loc) { |
| 8994 | CXFile File; |
| 8995 | unsigned Line, Column; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8996 | clang_getFileLocation(Loc, &File, &Line, &Column, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8997 | CXString FileName = clang_getFileName(File); |
| 8998 | *this << llvm::format("(%s:%d:%d)", clang_getCString(FileName), Line, Column); |
| 8999 | clang_disposeString(FileName); |
| 9000 | return *this; |
| 9001 | } |
| 9002 | |
| 9003 | Logger &cxindex::Logger::operator<<(CXSourceRange range) { |
| 9004 | CXSourceLocation BLoc = clang_getRangeStart(range); |
| 9005 | CXSourceLocation ELoc = clang_getRangeEnd(range); |
| 9006 | |
| 9007 | CXFile BFile; |
| 9008 | unsigned BLine, BColumn; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 9009 | clang_getFileLocation(BLoc, &BFile, &BLine, &BColumn, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9010 | |
| 9011 | CXFile EFile; |
| 9012 | unsigned ELine, EColumn; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 9013 | clang_getFileLocation(ELoc, &EFile, &ELine, &EColumn, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9014 | |
| 9015 | CXString BFileName = clang_getFileName(BFile); |
| 9016 | if (BFile == EFile) { |
| 9017 | *this << llvm::format("[%s %d:%d-%d:%d]", clang_getCString(BFileName), |
| 9018 | BLine, BColumn, ELine, EColumn); |
| 9019 | } else { |
| 9020 | CXString EFileName = clang_getFileName(EFile); |
| 9021 | *this << llvm::format("[%s:%d:%d - ", clang_getCString(BFileName), |
| 9022 | BLine, BColumn) |
| 9023 | << llvm::format("%s:%d:%d]", clang_getCString(EFileName), |
| 9024 | ELine, EColumn); |
| 9025 | clang_disposeString(EFileName); |
| 9026 | } |
| 9027 | clang_disposeString(BFileName); |
| 9028 | return *this; |
| 9029 | } |
| 9030 | |
| 9031 | Logger &cxindex::Logger::operator<<(CXString Str) { |
| 9032 | *this << clang_getCString(Str); |
| 9033 | return *this; |
| 9034 | } |
| 9035 | |
| 9036 | Logger &cxindex::Logger::operator<<(const llvm::format_object_base &Fmt) { |
| 9037 | LogOS << Fmt; |
| 9038 | return *this; |
| 9039 | } |
| 9040 | |
Benjamin Kramer | 762bc33 | 2019-08-07 14:44:40 +0000 | [diff] [blame] | 9041 | static llvm::ManagedStatic<std::mutex> LoggingMutex; |
Chandler Carruth | 37ad258 | 2014-06-27 15:14:39 +0000 | [diff] [blame] | 9042 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9043 | cxindex::Logger::~Logger() { |
Benjamin Kramer | 762bc33 | 2019-08-07 14:44:40 +0000 | [diff] [blame] | 9044 | std::lock_guard<std::mutex> L(*LoggingMutex); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9045 | |
| 9046 | static llvm::TimeRecord sBeginTR = llvm::TimeRecord::getCurrentTime(); |
| 9047 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9048 | raw_ostream &OS = llvm::errs(); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9049 | OS << "[libclang:" << Name << ':'; |
| 9050 | |
Alp Toker | 1a86ad2 | 2014-07-06 06:24:00 +0000 | [diff] [blame] | 9051 | #ifdef USE_DARWIN_THREADS |
| 9052 | // TODO: Portability. |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9053 | mach_port_t tid = pthread_mach_thread_np(pthread_self()); |
| 9054 | OS << tid << ':'; |
| 9055 | #endif |
| 9056 | |
| 9057 | llvm::TimeRecord TR = llvm::TimeRecord::getCurrentTime(); |
| 9058 | OS << llvm::format("%7.4f] ", TR.getWallTime() - sBeginTR.getWallTime()); |
Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 9059 | OS << Msg << '\n'; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9060 | |
| 9061 | if (Trace) { |
Zachary Turner | 1fe2a8d | 2015-03-05 19:15:09 +0000 | [diff] [blame] | 9062 | llvm::sys::PrintStackTrace(OS); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9063 | OS << "--------------------------------------------------\n"; |
| 9064 | } |
| 9065 | } |
Ivan Donchevskii | c592913 | 2018-12-10 15:58:50 +0000 | [diff] [blame] | 9066 | |
| 9067 | #ifdef CLANG_TOOL_EXTRA_BUILD |
| 9068 | // This anchor is used to force the linker to link the clang-tidy plugin. |
| 9069 | extern volatile int ClangTidyPluginAnchorSource; |
| 9070 | static int LLVM_ATTRIBUTE_UNUSED ClangTidyPluginAnchorDestination = |
| 9071 | ClangTidyPluginAnchorSource; |
| 9072 | |
| 9073 | // This anchor is used to force the linker to link the clang-include-fixer |
| 9074 | // plugin. |
| 9075 | extern volatile int ClangIncludeFixerPluginAnchorSource; |
| 9076 | static int LLVM_ATTRIBUTE_UNUSED ClangIncludeFixerPluginAnchorDestination = |
| 9077 | ClangIncludeFixerPluginAnchorSource; |
| 9078 | #endif |