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" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 26 | #include "clang/AST/StmtVisitor.h" |
| 27 | #include "clang/Basic/Diagnostic.h" |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 28 | #include "clang/Basic/DiagnosticCategories.h" |
| 29 | #include "clang/Basic/DiagnosticIDs.h" |
Richard Smith | 0a7b297 | 2018-07-03 21:34:13 +0000 | [diff] [blame] | 30 | #include "clang/Basic/Stack.h" |
Emilio Cobos Alvarez | 485ad42 | 2017-04-28 15:56:39 +0000 | [diff] [blame] | 31 | #include "clang/Basic/TargetInfo.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 32 | #include "clang/Basic/Version.h" |
| 33 | #include "clang/Frontend/ASTUnit.h" |
| 34 | #include "clang/Frontend/CompilerInstance.h" |
Dmitri Gribenko | 9e60511 | 2013-11-13 22:16:51 +0000 | [diff] [blame] | 35 | #include "clang/Index/CommentToXML.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 36 | #include "clang/Lex/HeaderSearch.h" |
| 37 | #include "clang/Lex/Lexer.h" |
| 38 | #include "clang/Lex/PreprocessingRecord.h" |
| 39 | #include "clang/Lex/Preprocessor.h" |
| 40 | #include "llvm/ADT/Optional.h" |
| 41 | #include "llvm/ADT/STLExtras.h" |
| 42 | #include "llvm/ADT/StringSwitch.h" |
Alp Toker | 1d257e1 | 2014-06-04 03:28:55 +0000 | [diff] [blame] | 43 | #include "llvm/Config/llvm-config.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Compiler.h" |
| 45 | #include "llvm/Support/CrashRecoveryContext.h" |
Chandler Carruth | 4b41745 | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Format.h" |
Chandler Carruth | 37ad258 | 2014-06-27 15:14:39 +0000 | [diff] [blame] | 47 | #include "llvm/Support/ManagedStatic.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 48 | #include "llvm/Support/MemoryBuffer.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Program.h" |
| 50 | #include "llvm/Support/SaveAndRestore.h" |
| 51 | #include "llvm/Support/Signals.h" |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 52 | #include "llvm/Support/TargetSelect.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 53 | #include "llvm/Support/Threading.h" |
| 54 | #include "llvm/Support/Timer.h" |
| 55 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 762bc33 | 2019-08-07 14:44:40 +0000 | [diff] [blame] | 56 | #include <mutex> |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 57 | |
Alp Toker | 1a86ad2 | 2014-07-06 06:24:00 +0000 | [diff] [blame] | 58 | #if LLVM_ENABLE_THREADS != 0 && defined(__APPLE__) |
| 59 | #define USE_DARWIN_THREADS |
| 60 | #endif |
| 61 | |
| 62 | #ifdef USE_DARWIN_THREADS |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 63 | #include <pthread.h> |
| 64 | #endif |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 65 | |
| 66 | using namespace clang; |
| 67 | using namespace clang::cxcursor; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 68 | using namespace clang::cxtu; |
| 69 | using namespace clang::cxindex; |
| 70 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 71 | CXTranslationUnit cxtu::MakeCXTranslationUnit(CIndexer *CIdx, |
| 72 | std::unique_ptr<ASTUnit> AU) { |
Dmitri Gribenko | d36209e | 2013-01-26 21:32:42 +0000 | [diff] [blame] | 73 | if (!AU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 74 | return nullptr; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 75 | assert(CIdx); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 76 | CXTranslationUnit D = new CXTranslationUnitImpl(); |
| 77 | D->CIdx = CIdx; |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 78 | D->TheASTUnit = AU.release(); |
Dmitri Gribenko | 7489521 | 2013-02-03 13:52:47 +0000 | [diff] [blame] | 79 | D->StringPool = new cxstring::CXStringPool(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 80 | D->Diagnostics = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 81 | D->OverridenCursorsPool = createOverridenCXCursorsPool(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 82 | D->CommentToXML = nullptr; |
Alex Lorenz | 690f0e2 | 2017-12-07 20:37:50 +0000 | [diff] [blame] | 83 | D->ParsingOptions = 0; |
| 84 | D->Arguments = {}; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 85 | return D; |
| 86 | } |
| 87 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 88 | bool cxtu::isASTReadError(ASTUnit *AU) { |
| 89 | for (ASTUnit::stored_diag_iterator D = AU->stored_diag_begin(), |
| 90 | DEnd = AU->stored_diag_end(); |
| 91 | D != DEnd; ++D) { |
| 92 | if (D->getLevel() >= DiagnosticsEngine::Error && |
| 93 | DiagnosticIDs::getCategoryNumberForDiag(D->getID()) == |
| 94 | diag::DiagCat_AST_Deserialization_Issue) |
| 95 | return true; |
| 96 | } |
| 97 | return false; |
| 98 | } |
| 99 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 100 | cxtu::CXTUOwner::~CXTUOwner() { |
| 101 | if (TU) |
| 102 | clang_disposeTranslationUnit(TU); |
| 103 | } |
| 104 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 105 | /// Compare two source ranges to determine their relative position in |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 106 | /// the translation unit. |
| 107 | static RangeComparisonResult RangeCompare(SourceManager &SM, |
| 108 | SourceRange R1, |
| 109 | SourceRange R2) { |
| 110 | assert(R1.isValid() && "First range is invalid?"); |
| 111 | assert(R2.isValid() && "Second range is invalid?"); |
| 112 | if (R1.getEnd() != R2.getBegin() && |
| 113 | SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin())) |
| 114 | return RangeBefore; |
| 115 | if (R2.getEnd() != R1.getBegin() && |
| 116 | SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin())) |
| 117 | return RangeAfter; |
| 118 | return RangeOverlap; |
| 119 | } |
| 120 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 121 | /// Determine if a source location falls within, before, or after a |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 122 | /// a given source range. |
| 123 | static RangeComparisonResult LocationCompare(SourceManager &SM, |
| 124 | SourceLocation L, SourceRange R) { |
| 125 | assert(R.isValid() && "First range is invalid?"); |
| 126 | assert(L.isValid() && "Second range is invalid?"); |
| 127 | if (L == R.getBegin() || L == R.getEnd()) |
| 128 | return RangeOverlap; |
| 129 | if (SM.isBeforeInTranslationUnit(L, R.getBegin())) |
| 130 | return RangeBefore; |
| 131 | if (SM.isBeforeInTranslationUnit(R.getEnd(), L)) |
| 132 | return RangeAfter; |
| 133 | return RangeOverlap; |
| 134 | } |
| 135 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 136 | /// Translate a Clang source range into a CIndex source range. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 137 | /// |
| 138 | /// Clang internally represents ranges where the end location points to the |
| 139 | /// start of the token at the end. However, for external clients it is more |
| 140 | /// useful to have a CXSourceRange be a proper half-open interval. This routine |
| 141 | /// does the appropriate translation. |
| 142 | CXSourceRange cxloc::translateSourceRange(const SourceManager &SM, |
| 143 | const LangOptions &LangOpts, |
| 144 | const CharSourceRange &R) { |
| 145 | // We want the last character in this location, so we will adjust the |
| 146 | // location accordingly. |
| 147 | SourceLocation EndLoc = R.getEnd(); |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 148 | bool IsTokenRange = R.isTokenRange(); |
| 149 | if (EndLoc.isValid() && EndLoc.isMacroID() && !SM.isMacroArgExpansion(EndLoc)) { |
| 150 | CharSourceRange Expansion = SM.getExpansionRange(EndLoc); |
| 151 | EndLoc = Expansion.getEnd(); |
| 152 | IsTokenRange = Expansion.isTokenRange(); |
| 153 | } |
| 154 | if (IsTokenRange && EndLoc.isValid()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 155 | unsigned Length = Lexer::MeasureTokenLength(SM.getSpellingLoc(EndLoc), |
| 156 | SM, LangOpts); |
| 157 | EndLoc = EndLoc.getLocWithOffset(Length); |
| 158 | } |
| 159 | |
Bill Wendling | eade362 | 2013-01-23 08:25:41 +0000 | [diff] [blame] | 160 | CXSourceRange Result = { |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 161 | { &SM, &LangOpts }, |
Bill Wendling | eade362 | 2013-01-23 08:25:41 +0000 | [diff] [blame] | 162 | R.getBegin().getRawEncoding(), |
| 163 | EndLoc.getRawEncoding() |
| 164 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 165 | return Result; |
| 166 | } |
| 167 | |
| 168 | //===----------------------------------------------------------------------===// |
| 169 | // Cursor visitor. |
| 170 | //===----------------------------------------------------------------------===// |
| 171 | |
| 172 | static SourceRange getRawCursorExtent(CXCursor C); |
| 173 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr); |
| 174 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 175 | RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { |
| 176 | return RangeCompare(AU->getSourceManager(), R, RegionOfInterest); |
| 177 | } |
| 178 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 179 | /// Visit the given cursor and, if requested by the visitor, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 180 | /// its children. |
| 181 | /// |
| 182 | /// \param Cursor the cursor to visit. |
| 183 | /// |
| 184 | /// \param CheckedRegionOfInterest if true, then the caller already checked |
| 185 | /// that this cursor is within the region of interest. |
| 186 | /// |
| 187 | /// \returns true if the visitation should be aborted, false if it |
| 188 | /// should continue. |
| 189 | bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { |
| 190 | if (clang_isInvalid(Cursor.kind)) |
| 191 | return false; |
| 192 | |
| 193 | if (clang_isDeclaration(Cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 194 | const Decl *D = getCursorDecl(Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 195 | if (!D) { |
| 196 | assert(0 && "Invalid declaration cursor"); |
| 197 | return true; // abort. |
| 198 | } |
| 199 | |
| 200 | // Ignore implicit declarations, unless it's an objc method because |
| 201 | // currently we should report implicit methods for properties when indexing. |
| 202 | if (D->isImplicit() && !isa<ObjCMethodDecl>(D)) |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | // If we have a range of interest, and this cursor doesn't intersect with it, |
| 207 | // we're done. |
| 208 | if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { |
| 209 | SourceRange Range = getRawCursorExtent(Cursor); |
| 210 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 215 | case CXChildVisit_Break: |
| 216 | return true; |
| 217 | |
| 218 | case CXChildVisit_Continue: |
| 219 | return false; |
| 220 | |
| 221 | case CXChildVisit_Recurse: { |
| 222 | bool ret = VisitChildren(Cursor); |
| 223 | if (PostChildrenVisitor) |
| 224 | if (PostChildrenVisitor(Cursor, ClientData)) |
| 225 | return true; |
| 226 | return ret; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | llvm_unreachable("Invalid CXChildVisitResult!"); |
| 231 | } |
| 232 | |
| 233 | static bool visitPreprocessedEntitiesInRange(SourceRange R, |
| 234 | PreprocessingRecord &PPRec, |
| 235 | CursorVisitor &Visitor) { |
| 236 | SourceManager &SM = Visitor.getASTUnit()->getSourceManager(); |
| 237 | FileID FID; |
| 238 | |
| 239 | if (!Visitor.shouldVisitIncludedEntities()) { |
| 240 | // If the begin/end of the range lie in the same FileID, do the optimization |
| 241 | // where we skip preprocessed entities that do not come from the same FileID. |
| 242 | FID = SM.getFileID(SM.getFileLoc(R.getBegin())); |
| 243 | if (FID != SM.getFileID(SM.getFileLoc(R.getEnd()))) |
| 244 | FID = FileID(); |
| 245 | } |
| 246 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 247 | const auto &Entities = PPRec.getPreprocessedEntitiesInRange(R); |
| 248 | return Visitor.visitPreprocessedEntities(Entities.begin(), Entities.end(), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 249 | PPRec, FID); |
| 250 | } |
| 251 | |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 252 | bool CursorVisitor::visitFileRegion() { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 253 | if (RegionOfInterest.isInvalid()) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 254 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 255 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 256 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 257 | SourceManager &SM = Unit->getSourceManager(); |
| 258 | |
| 259 | std::pair<FileID, unsigned> |
| 260 | Begin = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getBegin())), |
| 261 | End = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getEnd())); |
| 262 | |
| 263 | if (End.first != Begin.first) { |
| 264 | // If the end does not reside in the same file, try to recover by |
| 265 | // picking the end of the file of begin location. |
| 266 | End.first = Begin.first; |
| 267 | End.second = SM.getFileIDSize(Begin.first); |
| 268 | } |
| 269 | |
| 270 | assert(Begin.first == End.first); |
| 271 | if (Begin.second > End.second) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 272 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 273 | |
| 274 | FileID File = Begin.first; |
| 275 | unsigned Offset = Begin.second; |
| 276 | unsigned Length = End.second - Begin.second; |
| 277 | |
| 278 | if (!VisitDeclsOnly && !VisitPreprocessorLast) |
| 279 | if (visitPreprocessedEntitiesInRegion()) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 280 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 281 | |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 282 | if (visitDeclsFromFileRegion(File, Offset, Length)) |
| 283 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 284 | |
| 285 | if (!VisitDeclsOnly && VisitPreprocessorLast) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 286 | return visitPreprocessedEntitiesInRegion(); |
| 287 | |
| 288 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static bool isInLexicalContext(Decl *D, DeclContext *DC) { |
| 292 | if (!DC) |
| 293 | return false; |
| 294 | |
| 295 | for (DeclContext *DeclDC = D->getLexicalDeclContext(); |
| 296 | DeclDC; DeclDC = DeclDC->getLexicalParent()) { |
| 297 | if (DeclDC == DC) |
| 298 | return true; |
| 299 | } |
| 300 | return false; |
| 301 | } |
| 302 | |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 303 | bool CursorVisitor::visitDeclsFromFileRegion(FileID File, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 304 | unsigned Offset, unsigned Length) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 305 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 306 | SourceManager &SM = Unit->getSourceManager(); |
| 307 | SourceRange Range = RegionOfInterest; |
| 308 | |
| 309 | SmallVector<Decl *, 16> Decls; |
| 310 | Unit->findFileRegionDecls(File, Offset, Length, Decls); |
| 311 | |
| 312 | // If we didn't find any file level decls for the file, try looking at the |
| 313 | // file that it was included from. |
| 314 | while (Decls.empty() || Decls.front()->isTopLevelDeclInObjCContainer()) { |
| 315 | bool Invalid = false; |
| 316 | const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid); |
| 317 | if (Invalid) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 318 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 319 | |
| 320 | SourceLocation Outer; |
| 321 | if (SLEntry.isFile()) |
| 322 | Outer = SLEntry.getFile().getIncludeLoc(); |
| 323 | else |
| 324 | Outer = SLEntry.getExpansion().getExpansionLocStart(); |
| 325 | if (Outer.isInvalid()) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 326 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 327 | |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 328 | std::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 329 | Length = 0; |
| 330 | Unit->findFileRegionDecls(File, Offset, Length, Decls); |
| 331 | } |
| 332 | |
| 333 | assert(!Decls.empty()); |
| 334 | |
| 335 | bool VisitedAtLeastOnce = false; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 336 | DeclContext *CurDC = nullptr; |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 337 | SmallVectorImpl<Decl *>::iterator DIt = Decls.begin(); |
| 338 | for (SmallVectorImpl<Decl *>::iterator DE = Decls.end(); DIt != DE; ++DIt) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 339 | Decl *D = *DIt; |
| 340 | if (D->getSourceRange().isInvalid()) |
| 341 | continue; |
| 342 | |
| 343 | if (isInLexicalContext(D, CurDC)) |
| 344 | continue; |
| 345 | |
| 346 | CurDC = dyn_cast<DeclContext>(D); |
| 347 | |
| 348 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
| 349 | if (!TD->isFreeStanding()) |
| 350 | continue; |
| 351 | |
| 352 | RangeComparisonResult CompRes = RangeCompare(SM, D->getSourceRange(),Range); |
| 353 | if (CompRes == RangeBefore) |
| 354 | continue; |
| 355 | if (CompRes == RangeAfter) |
| 356 | break; |
| 357 | |
| 358 | assert(CompRes == RangeOverlap); |
| 359 | VisitedAtLeastOnce = true; |
| 360 | |
| 361 | if (isa<ObjCContainerDecl>(D)) { |
| 362 | FileDI_current = &DIt; |
| 363 | FileDE_current = DE; |
| 364 | } else { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 365 | FileDI_current = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true)) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 369 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | if (VisitedAtLeastOnce) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 373 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 374 | |
| 375 | // No Decls overlapped with the range. Move up the lexical context until there |
| 376 | // is a context that contains the range or we reach the translation unit |
| 377 | // level. |
| 378 | DeclContext *DC = DIt == Decls.begin() ? (*DIt)->getLexicalDeclContext() |
| 379 | : (*(DIt-1))->getLexicalDeclContext(); |
| 380 | |
| 381 | while (DC && !DC->isTranslationUnit()) { |
| 382 | Decl *D = cast<Decl>(DC); |
| 383 | SourceRange CurDeclRange = D->getSourceRange(); |
| 384 | if (CurDeclRange.isInvalid()) |
| 385 | break; |
| 386 | |
| 387 | if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) { |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 388 | if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true)) |
| 389 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | DC = D->getLexicalDeclContext(); |
| 393 | } |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 394 | |
| 395 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | bool CursorVisitor::visitPreprocessedEntitiesInRegion() { |
| 399 | if (!AU->getPreprocessor().getPreprocessingRecord()) |
| 400 | return false; |
| 401 | |
| 402 | PreprocessingRecord &PPRec |
| 403 | = *AU->getPreprocessor().getPreprocessingRecord(); |
| 404 | SourceManager &SM = AU->getSourceManager(); |
| 405 | |
| 406 | if (RegionOfInterest.isValid()) { |
| 407 | SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest); |
| 408 | SourceLocation B = MappedRange.getBegin(); |
| 409 | SourceLocation E = MappedRange.getEnd(); |
| 410 | |
| 411 | if (AU->isInPreambleFileID(B)) { |
| 412 | if (SM.isLoadedSourceLocation(E)) |
| 413 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), |
| 414 | PPRec, *this); |
| 415 | |
| 416 | // Beginning of range lies in the preamble but it also extends beyond |
| 417 | // it into the main file. Split the range into 2 parts, one covering |
| 418 | // the preamble and another covering the main file. This allows subsequent |
| 419 | // calls to visitPreprocessedEntitiesInRange to accept a source range that |
| 420 | // lies in the same FileID, allowing it to skip preprocessed entities that |
| 421 | // do not come from the same FileID. |
| 422 | bool breaked = |
| 423 | visitPreprocessedEntitiesInRange( |
| 424 | SourceRange(B, AU->getEndOfPreambleFileID()), |
| 425 | PPRec, *this); |
| 426 | if (breaked) return true; |
| 427 | return visitPreprocessedEntitiesInRange( |
| 428 | SourceRange(AU->getStartOfMainFileID(), E), |
| 429 | PPRec, *this); |
| 430 | } |
| 431 | |
| 432 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this); |
| 433 | } |
| 434 | |
| 435 | bool OnlyLocalDecls |
| 436 | = !AU->isMainFileAST() && AU->getOnlyLocalDecls(); |
| 437 | |
| 438 | if (OnlyLocalDecls) |
| 439 | return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(), |
| 440 | PPRec); |
| 441 | |
| 442 | return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec); |
| 443 | } |
| 444 | |
| 445 | template<typename InputIterator> |
| 446 | bool CursorVisitor::visitPreprocessedEntities(InputIterator First, |
| 447 | InputIterator Last, |
| 448 | PreprocessingRecord &PPRec, |
| 449 | FileID FID) { |
| 450 | for (; First != Last; ++First) { |
| 451 | if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID)) |
| 452 | continue; |
| 453 | |
| 454 | PreprocessedEntity *PPE = *First; |
Argyrios Kyrtzidis | 1030f26 | 2013-05-07 20:37:17 +0000 | [diff] [blame] | 455 | if (!PPE) |
| 456 | continue; |
| 457 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 458 | if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) { |
| 459 | if (Visit(MakeMacroExpansionCursor(ME, TU))) |
| 460 | return true; |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 461 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 462 | continue; |
| 463 | } |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 464 | |
| 465 | if (MacroDefinitionRecord *MD = dyn_cast<MacroDefinitionRecord>(PPE)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 466 | if (Visit(MakeMacroDefinitionCursor(MD, TU))) |
| 467 | return true; |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 468 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 469 | continue; |
| 470 | } |
| 471 | |
| 472 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) { |
| 473 | if (Visit(MakeInclusionDirectiveCursor(ID, TU))) |
| 474 | return true; |
| 475 | |
| 476 | continue; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | return false; |
| 481 | } |
| 482 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 483 | /// Visit the children of the given cursor. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 484 | /// |
| 485 | /// \returns true if the visitation should be aborted, false if it |
| 486 | /// should continue. |
| 487 | bool CursorVisitor::VisitChildren(CXCursor Cursor) { |
| 488 | if (clang_isReference(Cursor.kind) && |
| 489 | Cursor.kind != CXCursor_CXXBaseSpecifier) { |
| 490 | // By definition, references have no children. |
| 491 | return false; |
| 492 | } |
| 493 | |
| 494 | // Set the Parent field to Cursor, then back to its old value once we're |
| 495 | // done. |
| 496 | SetParentRAII SetParent(Parent, StmtParent, Cursor); |
| 497 | |
| 498 | if (clang_isDeclaration(Cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 499 | Decl *D = const_cast<Decl *>(getCursorDecl(Cursor)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 500 | if (!D) |
| 501 | return false; |
| 502 | |
| 503 | return VisitAttributes(D) || Visit(D); |
| 504 | } |
| 505 | |
| 506 | if (clang_isStatement(Cursor.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 507 | if (const Stmt *S = getCursorStmt(Cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 508 | return Visit(S); |
| 509 | |
| 510 | return false; |
| 511 | } |
| 512 | |
| 513 | if (clang_isExpression(Cursor.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 514 | if (const Expr *E = getCursorExpr(Cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 515 | return Visit(E); |
| 516 | |
| 517 | return false; |
| 518 | } |
| 519 | |
| 520 | if (clang_isTranslationUnit(Cursor.kind)) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 521 | CXTranslationUnit TU = getCursorTU(Cursor); |
| 522 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 523 | |
| 524 | int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast }; |
| 525 | for (unsigned I = 0; I != 2; ++I) { |
| 526 | if (VisitOrder[I]) { |
| 527 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && |
| 528 | RegionOfInterest.isInvalid()) { |
| 529 | for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(), |
| 530 | TLEnd = CXXUnit->top_level_end(); |
| 531 | TL != TLEnd; ++TL) { |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 532 | const Optional<bool> V = handleDeclForVisitation(*TL); |
| 533 | if (!V.hasValue()) |
| 534 | continue; |
| 535 | return V.getValue(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 536 | } |
| 537 | } else if (VisitDeclContext( |
| 538 | CXXUnit->getASTContext().getTranslationUnitDecl())) |
| 539 | return true; |
| 540 | continue; |
| 541 | } |
| 542 | |
| 543 | // Walk the preprocessing record. |
| 544 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) |
| 545 | visitPreprocessedEntitiesInRegion(); |
| 546 | } |
| 547 | |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | if (Cursor.kind == CXCursor_CXXBaseSpecifier) { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 552 | if (const CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 553 | if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) { |
| 554 | return Visit(BaseTSInfo->getTypeLoc()); |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 560 | const IBOutletCollectionAttr *A = |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 561 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor)); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 562 | if (const ObjCObjectType *ObjT = A->getInterface()->getAs<ObjCObjectType>()) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 563 | return Visit(cxcursor::MakeCursorObjCClassRef( |
| 564 | ObjT->getInterface(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 565 | A->getInterfaceLoc()->getTypeLoc().getBeginLoc(), TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 568 | // If pointing inside a macro definition, check if the token is an identifier |
| 569 | // that was ever defined as a macro. In such a case, create a "pseudo" macro |
| 570 | // expansion cursor for that token. |
| 571 | SourceLocation BeginLoc = RegionOfInterest.getBegin(); |
| 572 | if (Cursor.kind == CXCursor_MacroDefinition && |
| 573 | BeginLoc == RegionOfInterest.getEnd()) { |
| 574 | SourceLocation Loc = AU->mapLocationToPreamble(BeginLoc); |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 575 | const MacroInfo *MI = |
| 576 | getMacroInfo(cxcursor::getCursorMacroDefinition(Cursor), TU); |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 577 | if (MacroDefinitionRecord *MacroDef = |
| 578 | checkForMacroInMacroDefinition(MI, Loc, TU)) |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 579 | return Visit(cxcursor::MakeMacroExpansionCursor(MacroDef, BeginLoc, TU)); |
| 580 | } |
| 581 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 582 | // Nothing to visit at the moment. |
| 583 | return false; |
| 584 | } |
| 585 | |
| 586 | bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { |
| 587 | if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten()) |
| 588 | if (Visit(TSInfo->getTypeLoc())) |
| 589 | return true; |
| 590 | |
| 591 | if (Stmt *Body = B->getBody()) |
| 592 | return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest)); |
| 593 | |
| 594 | return false; |
| 595 | } |
| 596 | |
Ted Kremenek | 0332558 | 2013-02-21 01:29:01 +0000 | [diff] [blame] | 597 | Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 598 | if (RegionOfInterest.isValid()) { |
| 599 | SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager()); |
| 600 | if (Range.isInvalid()) |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 601 | return None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 602 | |
| 603 | switch (CompareRegionOfInterest(Range)) { |
| 604 | case RangeBefore: |
| 605 | // This declaration comes before the region of interest; skip it. |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 606 | return None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 607 | |
| 608 | case RangeAfter: |
| 609 | // This declaration comes after the region of interest; we're done. |
| 610 | return false; |
| 611 | |
| 612 | case RangeOverlap: |
| 613 | // This declaration overlaps the region of interest; visit it. |
| 614 | break; |
| 615 | } |
| 616 | } |
| 617 | return true; |
| 618 | } |
| 619 | |
| 620 | bool CursorVisitor::VisitDeclContext(DeclContext *DC) { |
| 621 | DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 622 | |
| 623 | // FIXME: Eventually remove. This part of a hack to support proper |
| 624 | // iteration over all Decls contained lexically within an ObjC container. |
| 625 | SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I); |
| 626 | SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E); |
| 627 | |
| 628 | for ( ; I != E; ++I) { |
| 629 | Decl *D = *I; |
| 630 | if (D->getLexicalDeclContext() != DC) |
| 631 | continue; |
Adrian Prantl | 2073dd2 | 2019-11-04 14:28:14 -0800 | [diff] [blame] | 632 | // Filter out synthesized property accessor redeclarations. |
| 633 | if (isa<ObjCImplDecl>(DC)) |
| 634 | if (auto *OMD = dyn_cast<ObjCMethodDecl>(D)) |
| 635 | if (OMD->isSynthesizedAccessorStub()) |
| 636 | continue; |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 637 | const Optional<bool> V = handleDeclForVisitation(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 638 | if (!V.hasValue()) |
| 639 | continue; |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 640 | return V.getValue(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 641 | } |
| 642 | return false; |
| 643 | } |
| 644 | |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 645 | Optional<bool> CursorVisitor::handleDeclForVisitation(const Decl *D) { |
| 646 | CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest); |
| 647 | |
| 648 | // Ignore synthesized ivars here, otherwise if we have something like: |
| 649 | // @synthesize prop = _prop; |
| 650 | // and '_prop' is not declared, we will encounter a '_prop' ivar before |
| 651 | // encountering the 'prop' synthesize declaration and we will think that |
| 652 | // we passed the region-of-interest. |
| 653 | if (auto *ivarD = dyn_cast<ObjCIvarDecl>(D)) { |
| 654 | if (ivarD->getSynthesize()) |
| 655 | return None; |
| 656 | } |
| 657 | |
| 658 | // FIXME: ObjCClassRef/ObjCProtocolRef for forward class/protocol |
| 659 | // declarations is a mismatch with the compiler semantics. |
| 660 | if (Cursor.kind == CXCursor_ObjCInterfaceDecl) { |
| 661 | auto *ID = cast<ObjCInterfaceDecl>(D); |
| 662 | if (!ID->isThisDeclarationADefinition()) |
| 663 | Cursor = MakeCursorObjCClassRef(ID, ID->getLocation(), TU); |
| 664 | |
| 665 | } else if (Cursor.kind == CXCursor_ObjCProtocolDecl) { |
| 666 | auto *PD = cast<ObjCProtocolDecl>(D); |
| 667 | if (!PD->isThisDeclarationADefinition()) |
| 668 | Cursor = MakeCursorObjCProtocolRef(PD, PD->getLocation(), TU); |
| 669 | } |
| 670 | |
| 671 | const Optional<bool> V = shouldVisitCursor(Cursor); |
| 672 | if (!V.hasValue()) |
| 673 | return None; |
| 674 | if (!V.getValue()) |
| 675 | return false; |
| 676 | if (Visit(Cursor, true)) |
| 677 | return true; |
| 678 | return None; |
| 679 | } |
| 680 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 681 | bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 682 | llvm_unreachable("Translation units are visited directly by Visit()"); |
| 683 | } |
| 684 | |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 685 | bool CursorVisitor::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| 686 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 687 | return true; |
| 688 | |
| 689 | return Visit(MakeCXCursor(D->getTemplatedDecl(), TU, RegionOfInterest)); |
| 690 | } |
| 691 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 692 | bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 693 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 694 | return Visit(TSInfo->getTypeLoc()); |
| 695 | |
| 696 | return false; |
| 697 | } |
| 698 | |
| 699 | bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { |
| 700 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 701 | return Visit(TSInfo->getTypeLoc()); |
| 702 | |
| 703 | return false; |
| 704 | } |
| 705 | |
| 706 | bool CursorVisitor::VisitTagDecl(TagDecl *D) { |
| 707 | return VisitDeclContext(D); |
| 708 | } |
| 709 | |
| 710 | bool CursorVisitor::VisitClassTemplateSpecializationDecl( |
| 711 | ClassTemplateSpecializationDecl *D) { |
| 712 | bool ShouldVisitBody = false; |
| 713 | switch (D->getSpecializationKind()) { |
| 714 | case TSK_Undeclared: |
| 715 | case TSK_ImplicitInstantiation: |
| 716 | // Nothing to visit |
| 717 | return false; |
| 718 | |
| 719 | case TSK_ExplicitInstantiationDeclaration: |
| 720 | case TSK_ExplicitInstantiationDefinition: |
| 721 | break; |
| 722 | |
| 723 | case TSK_ExplicitSpecialization: |
| 724 | ShouldVisitBody = true; |
| 725 | break; |
| 726 | } |
| 727 | |
| 728 | // Visit the template arguments used in the specialization. |
| 729 | if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { |
| 730 | TypeLoc TL = SpecType->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 731 | if (TemplateSpecializationTypeLoc TSTLoc = |
| 732 | TL.getAs<TemplateSpecializationTypeLoc>()) { |
| 733 | for (unsigned I = 0, N = TSTLoc.getNumArgs(); I != N; ++I) |
| 734 | if (VisitTemplateArgumentLoc(TSTLoc.getArgLoc(I))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 735 | return true; |
| 736 | } |
| 737 | } |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 738 | |
| 739 | return ShouldVisitBody && VisitCXXRecordDecl(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( |
| 743 | ClassTemplatePartialSpecializationDecl *D) { |
| 744 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 745 | // before visiting these template parameters. |
| 746 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 747 | return true; |
| 748 | |
| 749 | // Visit the partial specialization arguments. |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 750 | const ASTTemplateArgumentListInfo *Info = D->getTemplateArgsAsWritten(); |
| 751 | const TemplateArgumentLoc *TemplateArgs = Info->getTemplateArgs(); |
| 752 | for (unsigned I = 0, N = Info->NumTemplateArgs; I != N; ++I) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 753 | if (VisitTemplateArgumentLoc(TemplateArgs[I])) |
| 754 | return true; |
| 755 | |
| 756 | return VisitCXXRecordDecl(D); |
| 757 | } |
| 758 | |
| 759 | bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
Saar Raz | ff1e0fc | 2020-01-15 02:48:42 +0200 | [diff] [blame] | 760 | if (const auto *TC = D->getTypeConstraint()) |
| 761 | if (Visit(MakeCXCursor(TC->getImmediatelyDeclaredConstraint(), StmtParent, |
| 762 | TU, RegionOfInterest))) |
| 763 | return true; |
| 764 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 765 | // Visit the default argument. |
| 766 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 767 | if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) |
| 768 | if (Visit(DefArg->getTypeLoc())) |
| 769 | return true; |
| 770 | |
| 771 | return false; |
| 772 | } |
| 773 | |
| 774 | bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 775 | if (Expr *Init = D->getInitExpr()) |
| 776 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
| 777 | return false; |
| 778 | } |
| 779 | |
| 780 | bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { |
Argyrios Kyrtzidis | 2ec7674 | 2013-04-05 21:04:10 +0000 | [diff] [blame] | 781 | unsigned NumParamList = DD->getNumTemplateParameterLists(); |
| 782 | for (unsigned i = 0; i < NumParamList; i++) { |
| 783 | TemplateParameterList* Params = DD->getTemplateParameterList(i); |
| 784 | if (VisitTemplateParameters(Params)) |
| 785 | return true; |
| 786 | } |
| 787 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 788 | if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) |
| 789 | if (Visit(TSInfo->getTypeLoc())) |
| 790 | return true; |
| 791 | |
| 792 | // Visit the nested-name-specifier, if present. |
| 793 | if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc()) |
| 794 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 795 | return true; |
| 796 | |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 797 | return false; |
| 798 | } |
| 799 | |
Ivan Donchevskii | 1d18713 | 2018-01-03 14:35:48 +0000 | [diff] [blame] | 800 | static bool HasTrailingReturnType(FunctionDecl *ND) { |
| 801 | const QualType Ty = ND->getType(); |
| 802 | if (const FunctionType *AFT = Ty->getAs<FunctionType>()) { |
| 803 | if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(AFT)) |
| 804 | return FT->hasTrailingReturn(); |
| 805 | } |
| 806 | |
| 807 | return false; |
| 808 | } |
| 809 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 810 | /// Compare two base or member initializers based on their source order. |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 811 | static int CompareCXXCtorInitializers(CXXCtorInitializer *const *X, |
| 812 | CXXCtorInitializer *const *Y) { |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 813 | return (*X)->getSourceOrder() - (*Y)->getSourceOrder(); |
| 814 | } |
| 815 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 816 | bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
Argyrios Kyrtzidis | 2ec7674 | 2013-04-05 21:04:10 +0000 | [diff] [blame] | 817 | unsigned NumParamList = ND->getNumTemplateParameterLists(); |
| 818 | for (unsigned i = 0; i < NumParamList; i++) { |
| 819 | TemplateParameterList* Params = ND->getTemplateParameterList(i); |
| 820 | if (VisitTemplateParameters(Params)) |
| 821 | return true; |
| 822 | } |
| 823 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 824 | if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { |
| 825 | // Visit the function declaration's syntactic components in the order |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 826 | // written. This requires a bit of work. |
| 827 | TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); |
| 828 | FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>(); |
Ivan Donchevskii | 1d18713 | 2018-01-03 14:35:48 +0000 | [diff] [blame] | 829 | const bool HasTrailingRT = HasTrailingReturnType(ND); |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 830 | |
| 831 | // If we have a function declared directly (without the use of a typedef), |
| 832 | // visit just the return type. Otherwise, just visit the function's type |
| 833 | // now. |
Ivan Donchevskii | 1d18713 | 2018-01-03 14:35:48 +0000 | [diff] [blame] | 834 | if ((FTL && !isa<CXXConversionDecl>(ND) && !HasTrailingRT && |
| 835 | Visit(FTL.getReturnLoc())) || |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 836 | (!FTL && Visit(TL))) |
| 837 | return true; |
Ivan Donchevskii | 1d18713 | 2018-01-03 14:35:48 +0000 | [diff] [blame] | 838 | |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 839 | // Visit the nested-name-specifier, if present. |
| 840 | if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc()) |
| 841 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 842 | return true; |
| 843 | |
| 844 | // Visit the declaration name. |
Argyrios Kyrtzidis | 4a4d2b4 | 2014-02-09 08:13:47 +0000 | [diff] [blame] | 845 | if (!isa<CXXDestructorDecl>(ND)) |
| 846 | if (VisitDeclarationNameInfo(ND->getNameInfo())) |
| 847 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 848 | |
| 849 | // FIXME: Visit explicitly-specified template arguments! |
| 850 | |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 851 | // Visit the function parameters, if we have a function type. |
| 852 | if (FTL && VisitFunctionTypeLoc(FTL, true)) |
| 853 | return true; |
Ivan Donchevskii | 1d18713 | 2018-01-03 14:35:48 +0000 | [diff] [blame] | 854 | |
| 855 | // Visit the function's trailing return type. |
| 856 | if (FTL && HasTrailingRT && Visit(FTL.getReturnLoc())) |
| 857 | return true; |
| 858 | |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 859 | // FIXME: Attributes? |
| 860 | } |
| 861 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 862 | if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) { |
| 863 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) { |
| 864 | // Find the initializers that were written in the source. |
| 865 | SmallVector<CXXCtorInitializer *, 4> WrittenInits; |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 866 | for (auto *I : Constructor->inits()) { |
| 867 | if (!I->isWritten()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 868 | continue; |
| 869 | |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 870 | WrittenInits.push_back(I); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | // Sort the initializers in source order |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 874 | llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(), |
| 875 | &CompareCXXCtorInitializers); |
| 876 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 877 | // Visit the initializers in source order |
| 878 | for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) { |
| 879 | CXXCtorInitializer *Init = WrittenInits[I]; |
| 880 | if (Init->isAnyMemberInitializer()) { |
| 881 | if (Visit(MakeCursorMemberRef(Init->getAnyMember(), |
| 882 | Init->getMemberLocation(), TU))) |
| 883 | return true; |
| 884 | } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) { |
| 885 | if (Visit(TInfo->getTypeLoc())) |
| 886 | return true; |
| 887 | } |
| 888 | |
| 889 | // Visit the initializer value. |
| 890 | if (Expr *Initializer = Init->getInit()) |
| 891 | if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest))) |
| 892 | return true; |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
| 897 | return true; |
| 898 | } |
| 899 | |
| 900 | return false; |
| 901 | } |
| 902 | |
| 903 | bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { |
| 904 | if (VisitDeclaratorDecl(D)) |
| 905 | return true; |
| 906 | |
| 907 | if (Expr *BitWidth = D->getBitWidth()) |
| 908 | return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest)); |
| 909 | |
Benjamin Kramer | 99f9759 | 2017-11-15 12:20:41 +0000 | [diff] [blame] | 910 | if (Expr *Init = D->getInClassInitializer()) |
| 911 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
| 912 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 913 | return false; |
| 914 | } |
| 915 | |
| 916 | bool CursorVisitor::VisitVarDecl(VarDecl *D) { |
| 917 | if (VisitDeclaratorDecl(D)) |
| 918 | return true; |
| 919 | |
| 920 | if (Expr *Init = D->getInit()) |
| 921 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
| 922 | |
| 923 | return false; |
| 924 | } |
| 925 | |
| 926 | bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 927 | if (VisitDeclaratorDecl(D)) |
| 928 | return true; |
| 929 | |
| 930 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 931 | if (Expr *DefArg = D->getDefaultArgument()) |
| 932 | return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest)); |
| 933 | |
| 934 | return false; |
| 935 | } |
| 936 | |
| 937 | bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 938 | // FIXME: Visit the "outer" template parameter lists on the FunctionDecl |
| 939 | // before visiting these template parameters. |
| 940 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 941 | return true; |
| 942 | |
Jonathan Coe | 578ac7a | 2017-10-16 23:43:02 +0000 | [diff] [blame] | 943 | auto* FD = D->getTemplatedDecl(); |
| 944 | return VisitAttributes(FD) || VisitFunctionDecl(FD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 948 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 949 | // before visiting these template parameters. |
| 950 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 951 | return true; |
| 952 | |
Jonathan Coe | 578ac7a | 2017-10-16 23:43:02 +0000 | [diff] [blame] | 953 | auto* CD = D->getTemplatedDecl(); |
| 954 | return VisitAttributes(CD) || VisitCXXRecordDecl(CD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 958 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 959 | return true; |
| 960 | |
| 961 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && |
| 962 | VisitTemplateArgumentLoc(D->getDefaultArgument())) |
| 963 | return true; |
| 964 | |
| 965 | return false; |
| 966 | } |
| 967 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 968 | bool CursorVisitor::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { |
| 969 | // Visit the bound, if it's explicit. |
| 970 | if (D->hasExplicitBound()) { |
| 971 | if (auto TInfo = D->getTypeSourceInfo()) { |
| 972 | if (Visit(TInfo->getTypeLoc())) |
| 973 | return true; |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | return false; |
| 978 | } |
| 979 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 980 | bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 981 | if (TypeSourceInfo *TSInfo = ND->getReturnTypeSourceInfo()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 982 | if (Visit(TSInfo->getTypeLoc())) |
| 983 | return true; |
| 984 | |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 985 | for (const auto *P : ND->parameters()) { |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 986 | if (Visit(MakeCXCursor(P, TU, RegionOfInterest))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 987 | return true; |
| 988 | } |
| 989 | |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 990 | return ND->isThisDeclarationADefinition() && |
| 991 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | template <typename DeclIt> |
| 995 | static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current, |
| 996 | SourceManager &SM, SourceLocation EndLoc, |
| 997 | SmallVectorImpl<Decl *> &Decls) { |
| 998 | DeclIt next = *DI_current; |
| 999 | while (++next != DE_current) { |
| 1000 | Decl *D_next = *next; |
| 1001 | if (!D_next) |
| 1002 | break; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1003 | SourceLocation L = D_next->getBeginLoc(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1004 | if (!L.isValid()) |
| 1005 | break; |
| 1006 | if (SM.isBeforeInTranslationUnit(L, EndLoc)) { |
| 1007 | *DI_current = next; |
| 1008 | Decls.push_back(D_next); |
| 1009 | continue; |
| 1010 | } |
| 1011 | break; |
| 1012 | } |
| 1013 | } |
| 1014 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1015 | bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
| 1016 | // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially |
| 1017 | // an @implementation can lexically contain Decls that are not properly |
| 1018 | // nested in the AST. When we identify such cases, we need to retrofit |
| 1019 | // this nesting here. |
| 1020 | if (!DI_current && !FileDI_current) |
| 1021 | return VisitDeclContext(D); |
| 1022 | |
| 1023 | // Scan the Decls that immediately come after the container |
| 1024 | // in the current DeclContext. If any fall within the |
| 1025 | // container's lexical region, stash them into a vector |
| 1026 | // for later processing. |
| 1027 | SmallVector<Decl *, 24> DeclsInContainer; |
| 1028 | SourceLocation EndLoc = D->getSourceRange().getEnd(); |
| 1029 | SourceManager &SM = AU->getSourceManager(); |
| 1030 | if (EndLoc.isValid()) { |
| 1031 | if (DI_current) { |
| 1032 | addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc, |
| 1033 | DeclsInContainer); |
| 1034 | } else { |
| 1035 | addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc, |
| 1036 | DeclsInContainer); |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | // The common case. |
| 1041 | if (DeclsInContainer.empty()) |
| 1042 | return VisitDeclContext(D); |
| 1043 | |
| 1044 | // Get all the Decls in the DeclContext, and sort them with the |
| 1045 | // additional ones we've collected. Then visit them. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1046 | for (auto *SubDecl : D->decls()) { |
| 1047 | if (!SubDecl || SubDecl->getLexicalDeclContext() != D || |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1048 | SubDecl->getBeginLoc().isInvalid()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1049 | continue; |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1050 | DeclsInContainer.push_back(SubDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | // Now sort the Decls so that they appear in lexical order. |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 1054 | llvm::sort(DeclsInContainer, |
Mandeep Singh Grang | c205d8c | 2018-03-27 16:50:00 +0000 | [diff] [blame] | 1055 | [&SM](Decl *A, Decl *B) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1056 | SourceLocation L_A = A->getBeginLoc(); |
| 1057 | SourceLocation L_B = B->getBeginLoc(); |
| 1058 | return L_A != L_B ? SM.isBeforeInTranslationUnit(L_A, L_B) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1059 | : SM.isBeforeInTranslationUnit(A->getEndLoc(), |
| 1060 | B->getEndLoc()); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1061 | }); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1062 | |
| 1063 | // Now visit the decls. |
| 1064 | for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(), |
| 1065 | E = DeclsInContainer.end(); I != E; ++I) { |
| 1066 | CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest); |
Ted Kremenek | 0332558 | 2013-02-21 01:29:01 +0000 | [diff] [blame] | 1067 | const Optional<bool> &V = shouldVisitCursor(Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1068 | if (!V.hasValue()) |
| 1069 | continue; |
| 1070 | if (!V.getValue()) |
| 1071 | return false; |
| 1072 | if (Visit(Cursor, true)) |
| 1073 | return true; |
| 1074 | } |
| 1075 | return false; |
| 1076 | } |
| 1077 | |
| 1078 | bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 1079 | if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), |
| 1080 | TU))) |
| 1081 | return true; |
| 1082 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1083 | if (VisitObjCTypeParamList(ND->getTypeParamList())) |
| 1084 | return true; |
| 1085 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1086 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 1087 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 1088 | E = ND->protocol_end(); I != E; ++I, ++PL) |
| 1089 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1090 | return true; |
| 1091 | |
| 1092 | return VisitObjCContainerDecl(ND); |
| 1093 | } |
| 1094 | |
| 1095 | bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 1096 | if (!PID->isThisDeclarationADefinition()) |
| 1097 | return Visit(MakeCursorObjCProtocolRef(PID, PID->getLocation(), TU)); |
| 1098 | |
| 1099 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
| 1100 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 1101 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 1102 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1103 | return true; |
| 1104 | |
| 1105 | return VisitObjCContainerDecl(PID); |
| 1106 | } |
| 1107 | |
| 1108 | bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { |
| 1109 | if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc())) |
| 1110 | return true; |
| 1111 | |
| 1112 | // FIXME: This implements a workaround with @property declarations also being |
| 1113 | // installed in the DeclContext for the @interface. Eventually this code |
| 1114 | // should be removed. |
| 1115 | ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); |
| 1116 | if (!CDecl || !CDecl->IsClassExtension()) |
| 1117 | return false; |
| 1118 | |
| 1119 | ObjCInterfaceDecl *ID = CDecl->getClassInterface(); |
| 1120 | if (!ID) |
| 1121 | return false; |
| 1122 | |
| 1123 | IdentifierInfo *PropertyId = PD->getIdentifier(); |
| 1124 | ObjCPropertyDecl *prevDecl = |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 1125 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId, |
| 1126 | PD->getQueryKind()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1127 | |
| 1128 | if (!prevDecl) |
| 1129 | return false; |
| 1130 | |
| 1131 | // Visit synthesized methods since they will be skipped when visiting |
| 1132 | // the @interface. |
| 1133 | if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) |
| 1134 | if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl) |
| 1135 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
| 1136 | return true; |
| 1137 | |
| 1138 | if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) |
| 1139 | if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl) |
| 1140 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
| 1141 | return true; |
| 1142 | |
| 1143 | return false; |
| 1144 | } |
| 1145 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1146 | bool CursorVisitor::VisitObjCTypeParamList(ObjCTypeParamList *typeParamList) { |
| 1147 | if (!typeParamList) |
| 1148 | return false; |
| 1149 | |
| 1150 | for (auto *typeParam : *typeParamList) { |
| 1151 | // Visit the type parameter. |
| 1152 | if (Visit(MakeCXCursor(typeParam, TU, RegionOfInterest))) |
| 1153 | return true; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | return false; |
| 1157 | } |
| 1158 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1159 | bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 1160 | if (!D->isThisDeclarationADefinition()) { |
| 1161 | // Forward declaration is treated like a reference. |
| 1162 | return Visit(MakeCursorObjCClassRef(D, D->getLocation(), TU)); |
| 1163 | } |
| 1164 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1165 | // Objective-C type parameters. |
| 1166 | if (VisitObjCTypeParamList(D->getTypeParamListAsWritten())) |
| 1167 | return true; |
| 1168 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1169 | // Issue callbacks for super class. |
| 1170 | if (D->getSuperClass() && |
| 1171 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
| 1172 | D->getSuperClassLoc(), |
| 1173 | TU))) |
| 1174 | return true; |
| 1175 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1176 | if (TypeSourceInfo *SuperClassTInfo = D->getSuperClassTInfo()) |
| 1177 | if (Visit(SuperClassTInfo->getTypeLoc())) |
| 1178 | return true; |
| 1179 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1180 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1181 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 1182 | E = D->protocol_end(); I != E; ++I, ++PL) |
| 1183 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1184 | return true; |
| 1185 | |
| 1186 | return VisitObjCContainerDecl(D); |
| 1187 | } |
| 1188 | |
| 1189 | bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 1190 | return VisitObjCContainerDecl(D); |
| 1191 | } |
| 1192 | |
| 1193 | bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 1194 | // 'ID' could be null when dealing with invalid code. |
| 1195 | if (ObjCInterfaceDecl *ID = D->getClassInterface()) |
| 1196 | if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) |
| 1197 | return true; |
| 1198 | |
| 1199 | return VisitObjCImplDecl(D); |
| 1200 | } |
| 1201 | |
| 1202 | bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 1203 | #if 0 |
| 1204 | // Issue callbacks for super class. |
| 1205 | // FIXME: No source location information! |
| 1206 | if (D->getSuperClass() && |
| 1207 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
| 1208 | D->getSuperClassLoc(), |
| 1209 | TU))) |
| 1210 | return true; |
| 1211 | #endif |
| 1212 | |
| 1213 | return VisitObjCImplDecl(D); |
| 1214 | } |
| 1215 | |
| 1216 | bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) { |
| 1217 | if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl()) |
| 1218 | if (PD->isIvarNameSpecified()) |
| 1219 | return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU)); |
| 1220 | |
| 1221 | return false; |
| 1222 | } |
| 1223 | |
| 1224 | bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { |
| 1225 | return VisitDeclContext(D); |
| 1226 | } |
| 1227 | |
| 1228 | bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 1229 | // Visit nested-name-specifier. |
| 1230 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1231 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1232 | return true; |
| 1233 | |
| 1234 | return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), |
| 1235 | D->getTargetNameLoc(), TU)); |
| 1236 | } |
| 1237 | |
| 1238 | bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { |
| 1239 | // Visit nested-name-specifier. |
| 1240 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1241 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1242 | return true; |
| 1243 | } |
| 1244 | |
| 1245 | if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU))) |
| 1246 | return true; |
| 1247 | |
| 1248 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1249 | } |
| 1250 | |
| 1251 | bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 1252 | // Visit nested-name-specifier. |
| 1253 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1254 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1255 | return true; |
| 1256 | |
| 1257 | return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), |
| 1258 | D->getIdentLocation(), TU)); |
| 1259 | } |
| 1260 | |
| 1261 | bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
| 1262 | // Visit nested-name-specifier. |
| 1263 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1264 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1265 | return true; |
| 1266 | } |
| 1267 | |
| 1268 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1269 | } |
| 1270 | |
| 1271 | bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( |
| 1272 | UnresolvedUsingTypenameDecl *D) { |
| 1273 | // Visit nested-name-specifier. |
| 1274 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1275 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1276 | return true; |
| 1277 | |
| 1278 | return false; |
| 1279 | } |
| 1280 | |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 1281 | bool CursorVisitor::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 1282 | if (Visit(MakeCXCursor(D->getAssertExpr(), StmtParent, TU, RegionOfInterest))) |
| 1283 | return true; |
Richard Trieu | f3b7766 | 2016-09-13 01:37:01 +0000 | [diff] [blame] | 1284 | if (StringLiteral *Message = D->getMessage()) |
| 1285 | if (Visit(MakeCXCursor(Message, StmtParent, TU, RegionOfInterest))) |
| 1286 | return true; |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 1287 | return false; |
| 1288 | } |
| 1289 | |
Olivier Goffart | d211c64 | 2016-11-04 06:29:27 +0000 | [diff] [blame] | 1290 | bool CursorVisitor::VisitFriendDecl(FriendDecl *D) { |
| 1291 | if (NamedDecl *FriendD = D->getFriendDecl()) { |
| 1292 | if (Visit(MakeCXCursor(FriendD, TU, RegionOfInterest))) |
| 1293 | return true; |
| 1294 | } else if (TypeSourceInfo *TI = D->getFriendType()) { |
| 1295 | if (Visit(TI->getTypeLoc())) |
| 1296 | return true; |
| 1297 | } |
| 1298 | return false; |
| 1299 | } |
| 1300 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1301 | bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { |
| 1302 | switch (Name.getName().getNameKind()) { |
| 1303 | case clang::DeclarationName::Identifier: |
| 1304 | case clang::DeclarationName::CXXLiteralOperatorName: |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 1305 | case clang::DeclarationName::CXXDeductionGuideName: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1306 | case clang::DeclarationName::CXXOperatorName: |
| 1307 | case clang::DeclarationName::CXXUsingDirective: |
| 1308 | return false; |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 1309 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1310 | case clang::DeclarationName::CXXConstructorName: |
| 1311 | case clang::DeclarationName::CXXDestructorName: |
| 1312 | case clang::DeclarationName::CXXConversionFunctionName: |
| 1313 | if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) |
| 1314 | return Visit(TSInfo->getTypeLoc()); |
| 1315 | return false; |
| 1316 | |
| 1317 | case clang::DeclarationName::ObjCZeroArgSelector: |
| 1318 | case clang::DeclarationName::ObjCOneArgSelector: |
| 1319 | case clang::DeclarationName::ObjCMultiArgSelector: |
| 1320 | // FIXME: Per-identifier location info? |
| 1321 | return false; |
| 1322 | } |
| 1323 | |
| 1324 | llvm_unreachable("Invalid DeclarationName::Kind!"); |
| 1325 | } |
| 1326 | |
| 1327 | bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, |
| 1328 | SourceRange Range) { |
| 1329 | // FIXME: This whole routine is a hack to work around the lack of proper |
| 1330 | // source information in nested-name-specifiers (PR5791). Since we do have |
| 1331 | // a beginning source location, we can visit the first component of the |
| 1332 | // nested-name-specifier, if it's a single-token component. |
| 1333 | if (!NNS) |
| 1334 | return false; |
| 1335 | |
| 1336 | // Get the first component in the nested-name-specifier. |
| 1337 | while (NestedNameSpecifier *Prefix = NNS->getPrefix()) |
| 1338 | NNS = Prefix; |
| 1339 | |
| 1340 | switch (NNS->getKind()) { |
| 1341 | case NestedNameSpecifier::Namespace: |
| 1342 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), |
| 1343 | TU)); |
| 1344 | |
| 1345 | case NestedNameSpecifier::NamespaceAlias: |
| 1346 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1347 | Range.getBegin(), TU)); |
| 1348 | |
| 1349 | case NestedNameSpecifier::TypeSpec: { |
| 1350 | // If the type has a form where we know that the beginning of the source |
| 1351 | // range matches up with a reference cursor. Visit the appropriate reference |
| 1352 | // cursor. |
| 1353 | const Type *T = NNS->getAsType(); |
| 1354 | if (const TypedefType *Typedef = dyn_cast<TypedefType>(T)) |
| 1355 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); |
| 1356 | if (const TagType *Tag = dyn_cast<TagType>(T)) |
| 1357 | return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); |
| 1358 | if (const TemplateSpecializationType *TST |
| 1359 | = dyn_cast<TemplateSpecializationType>(T)) |
| 1360 | return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); |
| 1361 | break; |
| 1362 | } |
| 1363 | |
| 1364 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1365 | case NestedNameSpecifier::Global: |
| 1366 | case NestedNameSpecifier::Identifier: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 1367 | case NestedNameSpecifier::Super: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1368 | break; |
| 1369 | } |
| 1370 | |
| 1371 | return false; |
| 1372 | } |
| 1373 | |
| 1374 | bool |
| 1375 | CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 1376 | SmallVector<NestedNameSpecifierLoc, 4> Qualifiers; |
| 1377 | for (; Qualifier; Qualifier = Qualifier.getPrefix()) |
| 1378 | Qualifiers.push_back(Qualifier); |
| 1379 | |
| 1380 | while (!Qualifiers.empty()) { |
| 1381 | NestedNameSpecifierLoc Q = Qualifiers.pop_back_val(); |
| 1382 | NestedNameSpecifier *NNS = Q.getNestedNameSpecifier(); |
| 1383 | switch (NNS->getKind()) { |
| 1384 | case NestedNameSpecifier::Namespace: |
| 1385 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), |
| 1386 | Q.getLocalBeginLoc(), |
| 1387 | TU))) |
| 1388 | return true; |
| 1389 | |
| 1390 | break; |
| 1391 | |
| 1392 | case NestedNameSpecifier::NamespaceAlias: |
| 1393 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1394 | Q.getLocalBeginLoc(), |
| 1395 | TU))) |
| 1396 | return true; |
| 1397 | |
| 1398 | break; |
| 1399 | |
| 1400 | case NestedNameSpecifier::TypeSpec: |
| 1401 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1402 | if (Visit(Q.getTypeLoc())) |
| 1403 | return true; |
| 1404 | |
| 1405 | break; |
| 1406 | |
| 1407 | case NestedNameSpecifier::Global: |
| 1408 | case NestedNameSpecifier::Identifier: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 1409 | case NestedNameSpecifier::Super: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1410 | break; |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | return false; |
| 1415 | } |
| 1416 | |
| 1417 | bool CursorVisitor::VisitTemplateParameters( |
| 1418 | const TemplateParameterList *Params) { |
| 1419 | if (!Params) |
| 1420 | return false; |
| 1421 | |
| 1422 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 1423 | PEnd = Params->end(); |
| 1424 | P != PEnd; ++P) { |
| 1425 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
| 1426 | return true; |
| 1427 | } |
| 1428 | |
| 1429 | return false; |
| 1430 | } |
| 1431 | |
| 1432 | bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { |
| 1433 | switch (Name.getKind()) { |
| 1434 | case TemplateName::Template: |
| 1435 | return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); |
| 1436 | |
| 1437 | case TemplateName::OverloadedTemplate: |
| 1438 | // Visit the overloaded template set. |
| 1439 | if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU))) |
| 1440 | return true; |
| 1441 | |
| 1442 | return false; |
| 1443 | |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 1444 | case TemplateName::AssumedTemplate: |
| 1445 | // FIXME: Visit DeclarationName? |
| 1446 | return false; |
| 1447 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1448 | case TemplateName::DependentTemplate: |
| 1449 | // FIXME: Visit nested-name-specifier. |
| 1450 | return false; |
| 1451 | |
| 1452 | case TemplateName::QualifiedTemplate: |
| 1453 | // FIXME: Visit nested-name-specifier. |
| 1454 | return Visit(MakeCursorTemplateRef( |
| 1455 | Name.getAsQualifiedTemplateName()->getDecl(), |
| 1456 | Loc, TU)); |
| 1457 | |
| 1458 | case TemplateName::SubstTemplateTemplateParm: |
| 1459 | return Visit(MakeCursorTemplateRef( |
| 1460 | Name.getAsSubstTemplateTemplateParm()->getParameter(), |
| 1461 | Loc, TU)); |
| 1462 | |
| 1463 | case TemplateName::SubstTemplateTemplateParmPack: |
| 1464 | return Visit(MakeCursorTemplateRef( |
| 1465 | Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), |
| 1466 | Loc, TU)); |
| 1467 | } |
| 1468 | |
| 1469 | llvm_unreachable("Invalid TemplateName::Kind!"); |
| 1470 | } |
| 1471 | |
| 1472 | bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { |
| 1473 | switch (TAL.getArgument().getKind()) { |
| 1474 | case TemplateArgument::Null: |
| 1475 | case TemplateArgument::Integral: |
| 1476 | case TemplateArgument::Pack: |
| 1477 | return false; |
| 1478 | |
| 1479 | case TemplateArgument::Type: |
| 1480 | if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) |
| 1481 | return Visit(TSInfo->getTypeLoc()); |
| 1482 | return false; |
| 1483 | |
| 1484 | case TemplateArgument::Declaration: |
| 1485 | if (Expr *E = TAL.getSourceDeclExpression()) |
| 1486 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
| 1487 | return false; |
| 1488 | |
| 1489 | case TemplateArgument::NullPtr: |
| 1490 | if (Expr *E = TAL.getSourceNullPtrExpression()) |
| 1491 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
| 1492 | return false; |
| 1493 | |
| 1494 | case TemplateArgument::Expression: |
| 1495 | if (Expr *E = TAL.getSourceExpression()) |
| 1496 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
| 1497 | return false; |
| 1498 | |
| 1499 | case TemplateArgument::Template: |
| 1500 | case TemplateArgument::TemplateExpansion: |
| 1501 | if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc())) |
| 1502 | return true; |
| 1503 | |
| 1504 | return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(), |
| 1505 | TAL.getTemplateNameLoc()); |
| 1506 | } |
| 1507 | |
| 1508 | llvm_unreachable("Invalid TemplateArgument::Kind!"); |
| 1509 | } |
| 1510 | |
| 1511 | bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 1512 | return VisitDeclContext(D); |
| 1513 | } |
| 1514 | |
| 1515 | bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1516 | return Visit(TL.getUnqualifiedLoc()); |
| 1517 | } |
| 1518 | |
| 1519 | bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
| 1520 | ASTContext &Context = AU->getASTContext(); |
| 1521 | |
| 1522 | // Some builtin types (such as Objective-C's "id", "sel", and |
| 1523 | // "Class") have associated declarations. Create cursors for those. |
| 1524 | QualType VisitType; |
| 1525 | switch (TL.getTypePtr()->getKind()) { |
| 1526 | |
| 1527 | case BuiltinType::Void: |
| 1528 | case BuiltinType::NullPtr: |
| 1529 | case BuiltinType::Dependent: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 1530 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 1531 | case BuiltinType::Id: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 1532 | #include "clang/Basic/OpenCLImageTypes.def" |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 1533 | #define EXT_OPAQUE_TYPE(ExtTYpe, Id, Ext) \ |
| 1534 | case BuiltinType::Id: |
| 1535 | #include "clang/Basic/OpenCLExtensionTypes.def" |
NAKAMURA Takumi | 288c42e | 2013-02-07 12:47:42 +0000 | [diff] [blame] | 1536 | case BuiltinType::OCLSampler: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1537 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1538 | case BuiltinType::OCLClkEvent: |
| 1539 | case BuiltinType::OCLQueue: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1540 | case BuiltinType::OCLReserveID: |
Richard Sandiford | eb485fb | 2019-08-09 08:52:54 +0000 | [diff] [blame] | 1541 | #define SVE_TYPE(Name, Id, SingletonId) \ |
| 1542 | case BuiltinType::Id: |
| 1543 | #include "clang/Basic/AArch64SVEACLETypes.def" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1544 | #define BUILTIN_TYPE(Id, SingletonId) |
| 1545 | #define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1546 | #define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1547 | #define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1548 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1549 | #include "clang/AST/BuiltinTypes.def" |
| 1550 | break; |
| 1551 | |
| 1552 | case BuiltinType::ObjCId: |
| 1553 | VisitType = Context.getObjCIdType(); |
| 1554 | break; |
| 1555 | |
| 1556 | case BuiltinType::ObjCClass: |
| 1557 | VisitType = Context.getObjCClassType(); |
| 1558 | break; |
| 1559 | |
| 1560 | case BuiltinType::ObjCSel: |
| 1561 | VisitType = Context.getObjCSelType(); |
| 1562 | break; |
| 1563 | } |
| 1564 | |
| 1565 | if (!VisitType.isNull()) { |
| 1566 | if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) |
| 1567 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), |
| 1568 | TU)); |
| 1569 | } |
| 1570 | |
| 1571 | return false; |
| 1572 | } |
| 1573 | |
| 1574 | bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 1575 | return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU)); |
| 1576 | } |
| 1577 | |
| 1578 | bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 1579 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1580 | } |
| 1581 | |
| 1582 | bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { |
| 1583 | if (TL.isDefinition()) |
| 1584 | return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest)); |
| 1585 | |
| 1586 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1587 | } |
| 1588 | |
| 1589 | bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1590 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1591 | } |
| 1592 | |
| 1593 | bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 1594 | return Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 1597 | bool CursorVisitor::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1598 | if (Visit(MakeCursorTypeRef(TL.getDecl(), TL.getBeginLoc(), TU))) |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 1599 | return true; |
| 1600 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1601 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1602 | TU))) |
| 1603 | return true; |
| 1604 | } |
| 1605 | |
| 1606 | return false; |
| 1607 | } |
| 1608 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1609 | bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 1610 | if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) |
| 1611 | return true; |
| 1612 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1613 | for (unsigned I = 0, N = TL.getNumTypeArgs(); I != N; ++I) { |
| 1614 | if (Visit(TL.getTypeArgTInfo(I)->getTypeLoc())) |
| 1615 | return true; |
| 1616 | } |
| 1617 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1618 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1619 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1620 | TU))) |
| 1621 | return true; |
| 1622 | } |
| 1623 | |
| 1624 | return false; |
| 1625 | } |
| 1626 | |
| 1627 | bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 1628 | return Visit(TL.getPointeeLoc()); |
| 1629 | } |
| 1630 | |
| 1631 | bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) { |
| 1632 | return Visit(TL.getInnerLoc()); |
| 1633 | } |
| 1634 | |
Leonard Chan | c72aaf6 | 2019-05-07 03:20:17 +0000 | [diff] [blame] | 1635 | bool CursorVisitor::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { |
| 1636 | return Visit(TL.getInnerLoc()); |
| 1637 | } |
| 1638 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1639 | bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1640 | return Visit(TL.getPointeeLoc()); |
| 1641 | } |
| 1642 | |
| 1643 | bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1644 | return Visit(TL.getPointeeLoc()); |
| 1645 | } |
| 1646 | |
| 1647 | bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1648 | return Visit(TL.getPointeeLoc()); |
| 1649 | } |
| 1650 | |
| 1651 | bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 1652 | return Visit(TL.getPointeeLoc()); |
| 1653 | } |
| 1654 | |
| 1655 | bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 1656 | return Visit(TL.getPointeeLoc()); |
| 1657 | } |
| 1658 | |
| 1659 | bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 1660 | return Visit(TL.getModifiedLoc()); |
| 1661 | } |
| 1662 | |
| 1663 | bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, |
| 1664 | bool SkipResultType) { |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 1665 | if (!SkipResultType && Visit(TL.getReturnLoc())) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1666 | return true; |
| 1667 | |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 1668 | for (unsigned I = 0, N = TL.getNumParams(); I != N; ++I) |
| 1669 | if (Decl *D = TL.getParam(I)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1670 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest))) |
| 1671 | return true; |
| 1672 | |
| 1673 | return false; |
| 1674 | } |
| 1675 | |
| 1676 | bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1677 | if (Visit(TL.getElementLoc())) |
| 1678 | return true; |
| 1679 | |
| 1680 | if (Expr *Size = TL.getSizeExpr()) |
| 1681 | return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest)); |
| 1682 | |
| 1683 | return false; |
| 1684 | } |
| 1685 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 1686 | bool CursorVisitor::VisitDecayedTypeLoc(DecayedTypeLoc TL) { |
| 1687 | return Visit(TL.getOriginalLoc()); |
| 1688 | } |
| 1689 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 1690 | bool CursorVisitor::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { |
| 1691 | return Visit(TL.getOriginalLoc()); |
| 1692 | } |
| 1693 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1694 | bool CursorVisitor::VisitDeducedTemplateSpecializationTypeLoc( |
| 1695 | DeducedTemplateSpecializationTypeLoc TL) { |
| 1696 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1697 | TL.getTemplateNameLoc())) |
| 1698 | return true; |
| 1699 | |
| 1700 | return false; |
| 1701 | } |
| 1702 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1703 | bool CursorVisitor::VisitTemplateSpecializationTypeLoc( |
| 1704 | TemplateSpecializationTypeLoc TL) { |
| 1705 | // Visit the template name. |
| 1706 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1707 | TL.getTemplateNameLoc())) |
| 1708 | return true; |
| 1709 | |
| 1710 | // Visit the template arguments. |
| 1711 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1712 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1713 | return true; |
| 1714 | |
| 1715 | return false; |
| 1716 | } |
| 1717 | |
| 1718 | bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 1719 | return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); |
| 1720 | } |
| 1721 | |
| 1722 | bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 1723 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1724 | return Visit(TSInfo->getTypeLoc()); |
| 1725 | |
| 1726 | return false; |
| 1727 | } |
| 1728 | |
| 1729 | bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 1730 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1731 | return Visit(TSInfo->getTypeLoc()); |
| 1732 | |
| 1733 | return false; |
| 1734 | } |
| 1735 | |
| 1736 | bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 1737 | return VisitNestedNameSpecifierLoc(TL.getQualifierLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc( |
| 1741 | DependentTemplateSpecializationTypeLoc TL) { |
| 1742 | // Visit the nested-name-specifier, if there is one. |
| 1743 | if (TL.getQualifierLoc() && |
| 1744 | VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1745 | return true; |
| 1746 | |
| 1747 | // Visit the template arguments. |
| 1748 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1749 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1750 | return true; |
| 1751 | |
| 1752 | return false; |
| 1753 | } |
| 1754 | |
| 1755 | bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 1756 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1757 | return true; |
| 1758 | |
| 1759 | return Visit(TL.getNamedTypeLoc()); |
| 1760 | } |
| 1761 | |
| 1762 | bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { |
| 1763 | return Visit(TL.getPatternLoc()); |
| 1764 | } |
| 1765 | |
| 1766 | bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 1767 | if (Expr *E = TL.getUnderlyingExpr()) |
| 1768 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1769 | |
| 1770 | return false; |
| 1771 | } |
| 1772 | |
| 1773 | bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 1774 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1775 | } |
| 1776 | |
| 1777 | bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) { |
| 1778 | return Visit(TL.getValueLoc()); |
| 1779 | } |
| 1780 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 1781 | bool CursorVisitor::VisitPipeTypeLoc(PipeTypeLoc TL) { |
| 1782 | return Visit(TL.getValueLoc()); |
| 1783 | } |
| 1784 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1785 | #define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \ |
| 1786 | bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \ |
| 1787 | return Visit##PARENT##Loc(TL); \ |
| 1788 | } |
| 1789 | |
| 1790 | DEFAULT_TYPELOC_IMPL(Complex, Type) |
| 1791 | DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType) |
| 1792 | DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType) |
| 1793 | DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType) |
| 1794 | DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType) |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 1795 | DEFAULT_TYPELOC_IMPL(DependentAddressSpace, Type) |
Erich Keane | f702b02 | 2018-07-13 19:46:04 +0000 | [diff] [blame] | 1796 | DEFAULT_TYPELOC_IMPL(DependentVector, Type) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1797 | DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type) |
| 1798 | DEFAULT_TYPELOC_IMPL(Vector, Type) |
| 1799 | DEFAULT_TYPELOC_IMPL(ExtVector, VectorType) |
| 1800 | DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType) |
| 1801 | DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType) |
| 1802 | DEFAULT_TYPELOC_IMPL(Record, TagType) |
| 1803 | DEFAULT_TYPELOC_IMPL(Enum, TagType) |
| 1804 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type) |
| 1805 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type) |
| 1806 | DEFAULT_TYPELOC_IMPL(Auto, Type) |
| 1807 | |
| 1808 | bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 1809 | // Visit the nested-name-specifier, if present. |
| 1810 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1811 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1812 | return true; |
| 1813 | |
| 1814 | if (D->isCompleteDefinition()) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1815 | for (const auto &I : D->bases()) { |
| 1816 | if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(&I, TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1817 | return true; |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | return VisitTagDecl(D); |
| 1822 | } |
| 1823 | |
| 1824 | bool CursorVisitor::VisitAttributes(Decl *D) { |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 1825 | for (const auto *I : D->attrs()) |
Michael Wu | 40ff105 | 2018-08-03 05:20:23 +0000 | [diff] [blame] | 1826 | if ((TU->ParsingOptions & CXTranslationUnit_VisitImplicitAttributes || |
| 1827 | !I->isImplicit()) && |
| 1828 | Visit(MakeCXCursor(I, D, TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1829 | return true; |
| 1830 | |
| 1831 | return false; |
| 1832 | } |
| 1833 | |
| 1834 | //===----------------------------------------------------------------------===// |
| 1835 | // Data-recursive visitor methods. |
| 1836 | //===----------------------------------------------------------------------===// |
| 1837 | |
| 1838 | namespace { |
| 1839 | #define DEF_JOB(NAME, DATA, KIND)\ |
| 1840 | class NAME : public VisitorJob {\ |
| 1841 | public:\ |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1842 | NAME(const DATA *d, CXCursor parent) : \ |
| 1843 | VisitorJob(parent, VisitorJob::KIND, d) {} \ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1844 | static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\ |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1845 | const DATA *get() const { return static_cast<const DATA*>(data[0]); }\ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1846 | }; |
| 1847 | |
| 1848 | DEF_JOB(StmtVisit, Stmt, StmtVisitKind) |
| 1849 | DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind) |
| 1850 | DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind) |
| 1851 | DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1852 | DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind) |
| 1853 | DEF_JOB(LambdaExprParts, LambdaExpr, LambdaExprPartsKind) |
| 1854 | DEF_JOB(PostChildrenVisit, void, PostChildrenVisitKind) |
| 1855 | #undef DEF_JOB |
| 1856 | |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 1857 | class ExplicitTemplateArgsVisit : public VisitorJob { |
| 1858 | public: |
| 1859 | ExplicitTemplateArgsVisit(const TemplateArgumentLoc *Begin, |
| 1860 | const TemplateArgumentLoc *End, CXCursor parent) |
| 1861 | : VisitorJob(parent, VisitorJob::ExplicitTemplateArgsVisitKind, Begin, |
| 1862 | End) {} |
| 1863 | static bool classof(const VisitorJob *VJ) { |
| 1864 | return VJ->getKind() == ExplicitTemplateArgsVisitKind; |
| 1865 | } |
| 1866 | const TemplateArgumentLoc *begin() const { |
| 1867 | return static_cast<const TemplateArgumentLoc *>(data[0]); |
| 1868 | } |
| 1869 | const TemplateArgumentLoc *end() { |
| 1870 | return static_cast<const TemplateArgumentLoc *>(data[1]); |
| 1871 | } |
| 1872 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1873 | class DeclVisit : public VisitorJob { |
| 1874 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1875 | DeclVisit(const Decl *D, CXCursor parent, bool isFirst) : |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1876 | VisitorJob(parent, VisitorJob::DeclVisitKind, |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 1877 | D, isFirst ? (void*) 1 : (void*) nullptr) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1878 | static bool classof(const VisitorJob *VJ) { |
| 1879 | return VJ->getKind() == DeclVisitKind; |
| 1880 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1881 | const Decl *get() const { return static_cast<const Decl *>(data[0]); } |
Dmitri Gribenko | e5423a7 | 2015-03-23 19:23:50 +0000 | [diff] [blame] | 1882 | bool isFirst() const { return data[1] != nullptr; } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1883 | }; |
| 1884 | class TypeLocVisit : public VisitorJob { |
| 1885 | public: |
| 1886 | TypeLocVisit(TypeLoc tl, CXCursor parent) : |
| 1887 | VisitorJob(parent, VisitorJob::TypeLocVisitKind, |
| 1888 | tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {} |
| 1889 | |
| 1890 | static bool classof(const VisitorJob *VJ) { |
| 1891 | return VJ->getKind() == TypeLocVisitKind; |
| 1892 | } |
| 1893 | |
| 1894 | TypeLoc get() const { |
| 1895 | QualType T = QualType::getFromOpaquePtr(data[0]); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1896 | return TypeLoc(T, const_cast<void *>(data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1897 | } |
| 1898 | }; |
| 1899 | |
| 1900 | class LabelRefVisit : public VisitorJob { |
| 1901 | public: |
| 1902 | LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent) |
| 1903 | : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD, |
| 1904 | labelLoc.getPtrEncoding()) {} |
| 1905 | |
| 1906 | static bool classof(const VisitorJob *VJ) { |
| 1907 | return VJ->getKind() == VisitorJob::LabelRefVisitKind; |
| 1908 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1909 | const LabelDecl *get() const { |
| 1910 | return static_cast<const LabelDecl *>(data[0]); |
| 1911 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1912 | SourceLocation getLoc() const { |
| 1913 | return SourceLocation::getFromPtrEncoding(data[1]); } |
| 1914 | }; |
| 1915 | |
| 1916 | class NestedNameSpecifierLocVisit : public VisitorJob { |
| 1917 | public: |
| 1918 | NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent) |
| 1919 | : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind, |
| 1920 | Qualifier.getNestedNameSpecifier(), |
| 1921 | Qualifier.getOpaqueData()) { } |
| 1922 | |
| 1923 | static bool classof(const VisitorJob *VJ) { |
| 1924 | return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind; |
| 1925 | } |
| 1926 | |
| 1927 | NestedNameSpecifierLoc get() const { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1928 | return NestedNameSpecifierLoc( |
| 1929 | const_cast<NestedNameSpecifier *>( |
| 1930 | static_cast<const NestedNameSpecifier *>(data[0])), |
| 1931 | const_cast<void *>(data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1932 | } |
| 1933 | }; |
| 1934 | |
| 1935 | class DeclarationNameInfoVisit : public VisitorJob { |
| 1936 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1937 | DeclarationNameInfoVisit(const Stmt *S, CXCursor parent) |
Dmitri Gribenko | dd7dacf | 2013-02-03 13:19:54 +0000 | [diff] [blame] | 1938 | : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1939 | static bool classof(const VisitorJob *VJ) { |
| 1940 | return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind; |
| 1941 | } |
| 1942 | DeclarationNameInfo get() const { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1943 | const Stmt *S = static_cast<const Stmt *>(data[0]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1944 | switch (S->getStmtClass()) { |
| 1945 | default: |
| 1946 | llvm_unreachable("Unhandled Stmt"); |
| 1947 | case clang::Stmt::MSDependentExistsStmtClass: |
| 1948 | return cast<MSDependentExistsStmt>(S)->getNameInfo(); |
| 1949 | case Stmt::CXXDependentScopeMemberExprClass: |
| 1950 | return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo(); |
| 1951 | case Stmt::DependentScopeDeclRefExprClass: |
| 1952 | return cast<DependentScopeDeclRefExpr>(S)->getNameInfo(); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1953 | case Stmt::OMPCriticalDirectiveClass: |
| 1954 | return cast<OMPCriticalDirective>(S)->getDirectiveName(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1955 | } |
| 1956 | } |
| 1957 | }; |
| 1958 | class MemberRefVisit : public VisitorJob { |
| 1959 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1960 | MemberRefVisit(const FieldDecl *D, SourceLocation L, CXCursor parent) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1961 | : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D, |
| 1962 | L.getPtrEncoding()) {} |
| 1963 | static bool classof(const VisitorJob *VJ) { |
| 1964 | return VJ->getKind() == VisitorJob::MemberRefVisitKind; |
| 1965 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1966 | const FieldDecl *get() const { |
| 1967 | return static_cast<const FieldDecl *>(data[0]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1968 | } |
| 1969 | SourceLocation getLoc() const { |
| 1970 | return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]); |
| 1971 | } |
| 1972 | }; |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1973 | class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void> { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1974 | friend class OMPClauseEnqueue; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1975 | VisitorWorkList &WL; |
| 1976 | CXCursor Parent; |
| 1977 | public: |
| 1978 | EnqueueVisitor(VisitorWorkList &wl, CXCursor parent) |
| 1979 | : WL(wl), Parent(parent) {} |
| 1980 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1981 | void VisitAddrLabelExpr(const AddrLabelExpr *E); |
| 1982 | void VisitBlockExpr(const BlockExpr *B); |
| 1983 | void VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 1984 | void VisitCompoundStmt(const CompoundStmt *S); |
| 1985 | void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { /* Do nothing. */ } |
| 1986 | void VisitMSDependentExistsStmt(const MSDependentExistsStmt *S); |
| 1987 | void VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E); |
| 1988 | void VisitCXXNewExpr(const CXXNewExpr *E); |
| 1989 | void VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E); |
| 1990 | void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *E); |
| 1991 | void VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E); |
| 1992 | void VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *E); |
| 1993 | void VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
| 1994 | void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *E); |
| 1995 | void VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
| 1996 | void VisitCXXCatchStmt(const CXXCatchStmt *S); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 1997 | void VisitCXXForRangeStmt(const CXXForRangeStmt *S); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1998 | void VisitDeclRefExpr(const DeclRefExpr *D); |
| 1999 | void VisitDeclStmt(const DeclStmt *S); |
| 2000 | void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E); |
| 2001 | void VisitDesignatedInitExpr(const DesignatedInitExpr *E); |
| 2002 | void VisitExplicitCastExpr(const ExplicitCastExpr *E); |
| 2003 | void VisitForStmt(const ForStmt *FS); |
| 2004 | void VisitGotoStmt(const GotoStmt *GS); |
| 2005 | void VisitIfStmt(const IfStmt *If); |
| 2006 | void VisitInitListExpr(const InitListExpr *IE); |
| 2007 | void VisitMemberExpr(const MemberExpr *M); |
| 2008 | void VisitOffsetOfExpr(const OffsetOfExpr *E); |
| 2009 | void VisitObjCEncodeExpr(const ObjCEncodeExpr *E); |
| 2010 | void VisitObjCMessageExpr(const ObjCMessageExpr *M); |
| 2011 | void VisitOverloadExpr(const OverloadExpr *E); |
| 2012 | void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
| 2013 | void VisitStmt(const Stmt *S); |
| 2014 | void VisitSwitchStmt(const SwitchStmt *S); |
| 2015 | void VisitWhileStmt(const WhileStmt *W); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2016 | void VisitTypeTraitExpr(const TypeTraitExpr *E); |
| 2017 | void VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E); |
| 2018 | void VisitExpressionTraitExpr(const ExpressionTraitExpr *E); |
| 2019 | void VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U); |
| 2020 | void VisitVAArgExpr(const VAArgExpr *E); |
| 2021 | void VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
| 2022 | void VisitPseudoObjectExpr(const PseudoObjectExpr *E); |
| 2023 | void VisitOpaqueValueExpr(const OpaqueValueExpr *E); |
| 2024 | void VisitLambdaExpr(const LambdaExpr *E); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2025 | void VisitOMPExecutableDirective(const OMPExecutableDirective *D); |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2026 | void VisitOMPLoopDirective(const OMPLoopDirective *D); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2027 | void VisitOMPParallelDirective(const OMPParallelDirective *D); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2028 | void VisitOMPSimdDirective(const OMPSimdDirective *D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2029 | void VisitOMPForDirective(const OMPForDirective *D); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2030 | void VisitOMPForSimdDirective(const OMPForSimdDirective *D); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2031 | void VisitOMPSectionsDirective(const OMPSectionsDirective *D); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2032 | void VisitOMPSectionDirective(const OMPSectionDirective *D); |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 2033 | void VisitOMPSingleDirective(const OMPSingleDirective *D); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2034 | void VisitOMPMasterDirective(const OMPMasterDirective *D); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2035 | void VisitOMPCriticalDirective(const OMPCriticalDirective *D); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2036 | void VisitOMPParallelForDirective(const OMPParallelForDirective *D); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2037 | void VisitOMPParallelForSimdDirective(const OMPParallelForSimdDirective *D); |
cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 2038 | void VisitOMPParallelMasterDirective(const OMPParallelMasterDirective *D); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2039 | void VisitOMPParallelSectionsDirective(const OMPParallelSectionsDirective *D); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2040 | void VisitOMPTaskDirective(const OMPTaskDirective *D); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2041 | void VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 2042 | void VisitOMPBarrierDirective(const OMPBarrierDirective *D); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 2043 | void VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2044 | void VisitOMPTaskgroupDirective(const OMPTaskgroupDirective *D); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2045 | void |
| 2046 | VisitOMPCancellationPointDirective(const OMPCancellationPointDirective *D); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2047 | void VisitOMPCancelDirective(const OMPCancelDirective *D); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2048 | void VisitOMPFlushDirective(const OMPFlushDirective *D); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2049 | void VisitOMPOrderedDirective(const OMPOrderedDirective *D); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2050 | void VisitOMPAtomicDirective(const OMPAtomicDirective *D); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 2051 | void VisitOMPTargetDirective(const OMPTargetDirective *D); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2052 | void VisitOMPTargetDataDirective(const OMPTargetDataDirective *D); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 2053 | void VisitOMPTargetEnterDataDirective(const OMPTargetEnterDataDirective *D); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 2054 | void VisitOMPTargetExitDataDirective(const OMPTargetExitDataDirective *D); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 2055 | void VisitOMPTargetParallelDirective(const OMPTargetParallelDirective *D); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 2056 | void |
| 2057 | VisitOMPTargetParallelForDirective(const OMPTargetParallelForDirective *D); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2058 | void VisitOMPTeamsDirective(const OMPTeamsDirective *D); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 2059 | void VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 2060 | void VisitOMPTaskLoopSimdDirective(const OMPTaskLoopSimdDirective *D); |
Alexey Bataev | 60e51c4 | 2019-10-10 20:13:02 +0000 | [diff] [blame] | 2061 | void VisitOMPMasterTaskLoopDirective(const OMPMasterTaskLoopDirective *D); |
Alexey Bataev | b8552ab | 2019-10-18 16:47:35 +0000 | [diff] [blame] | 2062 | void |
| 2063 | VisitOMPMasterTaskLoopSimdDirective(const OMPMasterTaskLoopSimdDirective *D); |
Alexey Bataev | 5bbcead | 2019-10-14 17:17:41 +0000 | [diff] [blame] | 2064 | void VisitOMPParallelMasterTaskLoopDirective( |
| 2065 | const OMPParallelMasterTaskLoopDirective *D); |
Alexey Bataev | 14a388f | 2019-10-25 10:27:13 -0400 | [diff] [blame] | 2066 | void VisitOMPParallelMasterTaskLoopSimdDirective( |
| 2067 | const OMPParallelMasterTaskLoopSimdDirective *D); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2068 | void VisitOMPDistributeDirective(const OMPDistributeDirective *D); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2069 | void VisitOMPDistributeParallelForDirective( |
| 2070 | const OMPDistributeParallelForDirective *D); |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2071 | void VisitOMPDistributeParallelForSimdDirective( |
| 2072 | const OMPDistributeParallelForSimdDirective *D); |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2073 | void VisitOMPDistributeSimdDirective(const OMPDistributeSimdDirective *D); |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 2074 | void VisitOMPTargetParallelForSimdDirective( |
| 2075 | const OMPTargetParallelForSimdDirective *D); |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2076 | void VisitOMPTargetSimdDirective(const OMPTargetSimdDirective *D); |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 2077 | void VisitOMPTeamsDistributeDirective(const OMPTeamsDistributeDirective *D); |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 2078 | void VisitOMPTeamsDistributeSimdDirective( |
| 2079 | const OMPTeamsDistributeSimdDirective *D); |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 2080 | void VisitOMPTeamsDistributeParallelForSimdDirective( |
| 2081 | const OMPTeamsDistributeParallelForSimdDirective *D); |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 2082 | void VisitOMPTeamsDistributeParallelForDirective( |
| 2083 | const OMPTeamsDistributeParallelForDirective *D); |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 2084 | void VisitOMPTargetTeamsDirective(const OMPTargetTeamsDirective *D); |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 2085 | void VisitOMPTargetTeamsDistributeDirective( |
| 2086 | const OMPTargetTeamsDistributeDirective *D); |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 2087 | void VisitOMPTargetTeamsDistributeParallelForDirective( |
| 2088 | const OMPTargetTeamsDistributeParallelForDirective *D); |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 2089 | void VisitOMPTargetTeamsDistributeParallelForSimdDirective( |
| 2090 | const OMPTargetTeamsDistributeParallelForSimdDirective *D); |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 2091 | void VisitOMPTargetTeamsDistributeSimdDirective( |
| 2092 | const OMPTargetTeamsDistributeSimdDirective *D); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2093 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2094 | private: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2095 | void AddDeclarationNameInfo(const Stmt *S); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2096 | void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier); |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2097 | void AddExplicitTemplateArgs(const TemplateArgumentLoc *A, |
| 2098 | unsigned NumTemplateArgs); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2099 | void AddMemberRef(const FieldDecl *D, SourceLocation L); |
| 2100 | void AddStmt(const Stmt *S); |
| 2101 | void AddDecl(const Decl *D, bool isFirst = true); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2102 | void AddTypeLoc(TypeSourceInfo *TI); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2103 | void EnqueueChildren(const Stmt *S); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2104 | void EnqueueChildren(const OMPClause *S); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2105 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2106 | } // end anonyous namespace |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2107 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2108 | void EnqueueVisitor::AddDeclarationNameInfo(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2109 | // 'S' should always be non-null, since it comes from the |
| 2110 | // statement we are visiting. |
| 2111 | WL.push_back(DeclarationNameInfoVisit(S, Parent)); |
| 2112 | } |
| 2113 | |
| 2114 | void |
| 2115 | EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 2116 | if (Qualifier) |
| 2117 | WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent)); |
| 2118 | } |
| 2119 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2120 | void EnqueueVisitor::AddStmt(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2121 | if (S) |
| 2122 | WL.push_back(StmtVisit(S, Parent)); |
| 2123 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2124 | void EnqueueVisitor::AddDecl(const Decl *D, bool isFirst) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2125 | if (D) |
| 2126 | WL.push_back(DeclVisit(D, Parent, isFirst)); |
| 2127 | } |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2128 | void EnqueueVisitor::AddExplicitTemplateArgs(const TemplateArgumentLoc *A, |
| 2129 | unsigned NumTemplateArgs) { |
| 2130 | WL.push_back(ExplicitTemplateArgsVisit(A, A + NumTemplateArgs, Parent)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2131 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2132 | void EnqueueVisitor::AddMemberRef(const FieldDecl *D, SourceLocation L) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2133 | if (D) |
| 2134 | WL.push_back(MemberRefVisit(D, L, Parent)); |
| 2135 | } |
| 2136 | void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) { |
| 2137 | if (TI) |
| 2138 | WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent)); |
| 2139 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2140 | void EnqueueVisitor::EnqueueChildren(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2141 | unsigned size = WL.size(); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2142 | for (const Stmt *SubStmt : S->children()) { |
| 2143 | AddStmt(SubStmt); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2144 | } |
| 2145 | if (size == WL.size()) |
| 2146 | return; |
| 2147 | // Now reverse the entries we just added. This will match the DFS |
| 2148 | // ordering performed by the worklist. |
| 2149 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2150 | std::reverse(I, E); |
| 2151 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2152 | namespace { |
| 2153 | class OMPClauseEnqueue : public ConstOMPClauseVisitor<OMPClauseEnqueue> { |
| 2154 | EnqueueVisitor *Visitor; |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2155 | /// Process clauses with list of variables. |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2156 | template <typename T> |
| 2157 | void VisitOMPClauseList(T *Node); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2158 | public: |
| 2159 | OMPClauseEnqueue(EnqueueVisitor *Visitor) : Visitor(Visitor) { } |
| 2160 | #define OPENMP_CLAUSE(Name, Class) \ |
| 2161 | void Visit##Class(const Class *C); |
| 2162 | #include "clang/Basic/OpenMPKinds.def" |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2163 | void VisitOMPClauseWithPreInit(const OMPClauseWithPreInit *C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2164 | void VisitOMPClauseWithPostUpdate(const OMPClauseWithPostUpdate *C); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2165 | }; |
| 2166 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2167 | void OMPClauseEnqueue::VisitOMPClauseWithPreInit( |
| 2168 | const OMPClauseWithPreInit *C) { |
| 2169 | Visitor->AddStmt(C->getPreInitStmt()); |
| 2170 | } |
| 2171 | |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2172 | void OMPClauseEnqueue::VisitOMPClauseWithPostUpdate( |
| 2173 | const OMPClauseWithPostUpdate *C) { |
Alexey Bataev | 37e594c | 2016-03-04 07:21:16 +0000 | [diff] [blame] | 2174 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2175 | Visitor->AddStmt(C->getPostUpdateExpr()); |
| 2176 | } |
| 2177 | |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2178 | void OMPClauseEnqueue::VisitOMPIfClause(const OMPIfClause *C) { |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 2179 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2180 | Visitor->AddStmt(C->getCondition()); |
| 2181 | } |
| 2182 | |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 2183 | void OMPClauseEnqueue::VisitOMPFinalClause(const OMPFinalClause *C) { |
| 2184 | Visitor->AddStmt(C->getCondition()); |
| 2185 | } |
| 2186 | |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2187 | void OMPClauseEnqueue::VisitOMPNumThreadsClause(const OMPNumThreadsClause *C) { |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 2188 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2189 | Visitor->AddStmt(C->getNumThreads()); |
| 2190 | } |
| 2191 | |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 2192 | void OMPClauseEnqueue::VisitOMPSafelenClause(const OMPSafelenClause *C) { |
| 2193 | Visitor->AddStmt(C->getSafelen()); |
| 2194 | } |
| 2195 | |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 2196 | void OMPClauseEnqueue::VisitOMPSimdlenClause(const OMPSimdlenClause *C) { |
| 2197 | Visitor->AddStmt(C->getSimdlen()); |
| 2198 | } |
| 2199 | |
Alexey Bataev | 9cc10fc | 2019-03-12 18:52:33 +0000 | [diff] [blame] | 2200 | void OMPClauseEnqueue::VisitOMPAllocatorClause(const OMPAllocatorClause *C) { |
| 2201 | Visitor->AddStmt(C->getAllocator()); |
| 2202 | } |
| 2203 | |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 2204 | void OMPClauseEnqueue::VisitOMPCollapseClause(const OMPCollapseClause *C) { |
| 2205 | Visitor->AddStmt(C->getNumForLoops()); |
| 2206 | } |
| 2207 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2208 | void OMPClauseEnqueue::VisitOMPDefaultClause(const OMPDefaultClause *C) { } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2209 | |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 2210 | void OMPClauseEnqueue::VisitOMPProcBindClause(const OMPProcBindClause *C) { } |
| 2211 | |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2212 | void OMPClauseEnqueue::VisitOMPScheduleClause(const OMPScheduleClause *C) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2213 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2214 | Visitor->AddStmt(C->getChunkSize()); |
| 2215 | } |
| 2216 | |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 2217 | void OMPClauseEnqueue::VisitOMPOrderedClause(const OMPOrderedClause *C) { |
| 2218 | Visitor->AddStmt(C->getNumForLoops()); |
| 2219 | } |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 2220 | |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 2221 | void OMPClauseEnqueue::VisitOMPNowaitClause(const OMPNowaitClause *) {} |
| 2222 | |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 2223 | void OMPClauseEnqueue::VisitOMPUntiedClause(const OMPUntiedClause *) {} |
| 2224 | |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 2225 | void OMPClauseEnqueue::VisitOMPMergeableClause(const OMPMergeableClause *) {} |
| 2226 | |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 2227 | void OMPClauseEnqueue::VisitOMPReadClause(const OMPReadClause *) {} |
| 2228 | |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 2229 | void OMPClauseEnqueue::VisitOMPWriteClause(const OMPWriteClause *) {} |
| 2230 | |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 2231 | void OMPClauseEnqueue::VisitOMPUpdateClause(const OMPUpdateClause *) {} |
| 2232 | |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 2233 | void OMPClauseEnqueue::VisitOMPCaptureClause(const OMPCaptureClause *) {} |
| 2234 | |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 2235 | void OMPClauseEnqueue::VisitOMPSeqCstClause(const OMPSeqCstClause *) {} |
| 2236 | |
Alexey Bataev | ea9166b | 2020-02-06 16:30:23 -0500 | [diff] [blame^] | 2237 | void OMPClauseEnqueue::VisitOMPAcqRelClause(const OMPAcqRelClause *) {} |
| 2238 | |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 2239 | void OMPClauseEnqueue::VisitOMPThreadsClause(const OMPThreadsClause *) {} |
| 2240 | |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 2241 | void OMPClauseEnqueue::VisitOMPSIMDClause(const OMPSIMDClause *) {} |
| 2242 | |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 2243 | void OMPClauseEnqueue::VisitOMPNogroupClause(const OMPNogroupClause *) {} |
| 2244 | |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 2245 | void OMPClauseEnqueue::VisitOMPUnifiedAddressClause( |
| 2246 | const OMPUnifiedAddressClause *) {} |
| 2247 | |
Patrick Lyster | 4a370b9 | 2018-10-01 13:47:43 +0000 | [diff] [blame] | 2248 | void OMPClauseEnqueue::VisitOMPUnifiedSharedMemoryClause( |
| 2249 | const OMPUnifiedSharedMemoryClause *) {} |
| 2250 | |
Patrick Lyster | 6bdf63b | 2018-10-03 20:07:58 +0000 | [diff] [blame] | 2251 | void OMPClauseEnqueue::VisitOMPReverseOffloadClause( |
| 2252 | const OMPReverseOffloadClause *) {} |
| 2253 | |
Patrick Lyster | 3fe9e39 | 2018-10-11 14:41:10 +0000 | [diff] [blame] | 2254 | void OMPClauseEnqueue::VisitOMPDynamicAllocatorsClause( |
| 2255 | const OMPDynamicAllocatorsClause *) {} |
| 2256 | |
Patrick Lyster | 7a2a27c | 2018-11-02 12:18:11 +0000 | [diff] [blame] | 2257 | void OMPClauseEnqueue::VisitOMPAtomicDefaultMemOrderClause( |
| 2258 | const OMPAtomicDefaultMemOrderClause *) {} |
| 2259 | |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 2260 | void OMPClauseEnqueue::VisitOMPDeviceClause(const OMPDeviceClause *C) { |
| 2261 | Visitor->AddStmt(C->getDevice()); |
| 2262 | } |
| 2263 | |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 2264 | void OMPClauseEnqueue::VisitOMPNumTeamsClause(const OMPNumTeamsClause *C) { |
Arpith Chacko Jacob | bc12634 | 2017-01-25 11:28:18 +0000 | [diff] [blame] | 2265 | VisitOMPClauseWithPreInit(C); |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 2266 | Visitor->AddStmt(C->getNumTeams()); |
| 2267 | } |
| 2268 | |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 2269 | void OMPClauseEnqueue::VisitOMPThreadLimitClause(const OMPThreadLimitClause *C) { |
Arpith Chacko Jacob | 7ecc0b7 | 2017-01-25 11:44:35 +0000 | [diff] [blame] | 2270 | VisitOMPClauseWithPreInit(C); |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 2271 | Visitor->AddStmt(C->getThreadLimit()); |
| 2272 | } |
| 2273 | |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 2274 | void OMPClauseEnqueue::VisitOMPPriorityClause(const OMPPriorityClause *C) { |
| 2275 | Visitor->AddStmt(C->getPriority()); |
| 2276 | } |
| 2277 | |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 2278 | void OMPClauseEnqueue::VisitOMPGrainsizeClause(const OMPGrainsizeClause *C) { |
| 2279 | Visitor->AddStmt(C->getGrainsize()); |
| 2280 | } |
| 2281 | |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 2282 | void OMPClauseEnqueue::VisitOMPNumTasksClause(const OMPNumTasksClause *C) { |
| 2283 | Visitor->AddStmt(C->getNumTasks()); |
| 2284 | } |
| 2285 | |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 2286 | void OMPClauseEnqueue::VisitOMPHintClause(const OMPHintClause *C) { |
| 2287 | Visitor->AddStmt(C->getHint()); |
| 2288 | } |
| 2289 | |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2290 | template<typename T> |
| 2291 | void OMPClauseEnqueue::VisitOMPClauseList(T *Node) { |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2292 | for (const auto *I : Node->varlists()) { |
Aaron Ballman | 2205d2a | 2014-03-14 15:55:35 +0000 | [diff] [blame] | 2293 | Visitor->AddStmt(I); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2294 | } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2295 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2296 | |
Alexey Bataev | e04483e | 2019-03-27 14:14:31 +0000 | [diff] [blame] | 2297 | void OMPClauseEnqueue::VisitOMPAllocateClause(const OMPAllocateClause *C) { |
| 2298 | VisitOMPClauseList(C); |
| 2299 | Visitor->AddStmt(C->getAllocator()); |
| 2300 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2301 | void OMPClauseEnqueue::VisitOMPPrivateClause(const OMPPrivateClause *C) { |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2302 | VisitOMPClauseList(C); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2303 | for (const auto *E : C->private_copies()) { |
| 2304 | Visitor->AddStmt(E); |
| 2305 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2306 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2307 | void OMPClauseEnqueue::VisitOMPFirstprivateClause( |
| 2308 | const OMPFirstprivateClause *C) { |
| 2309 | VisitOMPClauseList(C); |
Alexey Bataev | 417089f | 2016-02-17 13:19:37 +0000 | [diff] [blame] | 2310 | VisitOMPClauseWithPreInit(C); |
| 2311 | for (const auto *E : C->private_copies()) { |
| 2312 | Visitor->AddStmt(E); |
| 2313 | } |
| 2314 | for (const auto *E : C->inits()) { |
| 2315 | Visitor->AddStmt(E); |
| 2316 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2317 | } |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2318 | void OMPClauseEnqueue::VisitOMPLastprivateClause( |
| 2319 | const OMPLastprivateClause *C) { |
| 2320 | VisitOMPClauseList(C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2321 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2322 | for (auto *E : C->private_copies()) { |
| 2323 | Visitor->AddStmt(E); |
| 2324 | } |
| 2325 | for (auto *E : C->source_exprs()) { |
| 2326 | Visitor->AddStmt(E); |
| 2327 | } |
| 2328 | for (auto *E : C->destination_exprs()) { |
| 2329 | Visitor->AddStmt(E); |
| 2330 | } |
| 2331 | for (auto *E : C->assignment_ops()) { |
| 2332 | Visitor->AddStmt(E); |
| 2333 | } |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2334 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2335 | void OMPClauseEnqueue::VisitOMPSharedClause(const OMPSharedClause *C) { |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2336 | VisitOMPClauseList(C); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2337 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2338 | void OMPClauseEnqueue::VisitOMPReductionClause(const OMPReductionClause *C) { |
| 2339 | VisitOMPClauseList(C); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2340 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 2341 | for (auto *E : C->privates()) { |
| 2342 | Visitor->AddStmt(E); |
| 2343 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2344 | for (auto *E : C->lhs_exprs()) { |
| 2345 | Visitor->AddStmt(E); |
| 2346 | } |
| 2347 | for (auto *E : C->rhs_exprs()) { |
| 2348 | Visitor->AddStmt(E); |
| 2349 | } |
| 2350 | for (auto *E : C->reduction_ops()) { |
| 2351 | Visitor->AddStmt(E); |
| 2352 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2353 | } |
Alexey Bataev | 169d96a | 2017-07-18 20:17:46 +0000 | [diff] [blame] | 2354 | void OMPClauseEnqueue::VisitOMPTaskReductionClause( |
| 2355 | const OMPTaskReductionClause *C) { |
| 2356 | VisitOMPClauseList(C); |
| 2357 | VisitOMPClauseWithPostUpdate(C); |
| 2358 | for (auto *E : C->privates()) { |
| 2359 | Visitor->AddStmt(E); |
| 2360 | } |
| 2361 | for (auto *E : C->lhs_exprs()) { |
| 2362 | Visitor->AddStmt(E); |
| 2363 | } |
| 2364 | for (auto *E : C->rhs_exprs()) { |
| 2365 | Visitor->AddStmt(E); |
| 2366 | } |
| 2367 | for (auto *E : C->reduction_ops()) { |
| 2368 | Visitor->AddStmt(E); |
| 2369 | } |
| 2370 | } |
Alexey Bataev | fa312f3 | 2017-07-21 18:48:21 +0000 | [diff] [blame] | 2371 | void OMPClauseEnqueue::VisitOMPInReductionClause( |
| 2372 | const OMPInReductionClause *C) { |
| 2373 | VisitOMPClauseList(C); |
| 2374 | VisitOMPClauseWithPostUpdate(C); |
| 2375 | for (auto *E : C->privates()) { |
| 2376 | Visitor->AddStmt(E); |
| 2377 | } |
| 2378 | for (auto *E : C->lhs_exprs()) { |
| 2379 | Visitor->AddStmt(E); |
| 2380 | } |
| 2381 | for (auto *E : C->rhs_exprs()) { |
| 2382 | Visitor->AddStmt(E); |
| 2383 | } |
| 2384 | for (auto *E : C->reduction_ops()) { |
| 2385 | Visitor->AddStmt(E); |
| 2386 | } |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 2387 | for (auto *E : C->taskgroup_descriptors()) |
| 2388 | Visitor->AddStmt(E); |
Alexey Bataev | fa312f3 | 2017-07-21 18:48:21 +0000 | [diff] [blame] | 2389 | } |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2390 | void OMPClauseEnqueue::VisitOMPLinearClause(const OMPLinearClause *C) { |
| 2391 | VisitOMPClauseList(C); |
Alexey Bataev | 78849fb | 2016-03-09 09:49:00 +0000 | [diff] [blame] | 2392 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 2393 | for (const auto *E : C->privates()) { |
| 2394 | Visitor->AddStmt(E); |
| 2395 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 2396 | for (const auto *E : C->inits()) { |
| 2397 | Visitor->AddStmt(E); |
| 2398 | } |
| 2399 | for (const auto *E : C->updates()) { |
| 2400 | Visitor->AddStmt(E); |
| 2401 | } |
| 2402 | for (const auto *E : C->finals()) { |
| 2403 | Visitor->AddStmt(E); |
| 2404 | } |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2405 | Visitor->AddStmt(C->getStep()); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 2406 | Visitor->AddStmt(C->getCalcStep()); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2407 | } |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 2408 | void OMPClauseEnqueue::VisitOMPAlignedClause(const OMPAlignedClause *C) { |
| 2409 | VisitOMPClauseList(C); |
| 2410 | Visitor->AddStmt(C->getAlignment()); |
| 2411 | } |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 2412 | void OMPClauseEnqueue::VisitOMPCopyinClause(const OMPCopyinClause *C) { |
| 2413 | VisitOMPClauseList(C); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 2414 | for (auto *E : C->source_exprs()) { |
| 2415 | Visitor->AddStmt(E); |
| 2416 | } |
| 2417 | for (auto *E : C->destination_exprs()) { |
| 2418 | Visitor->AddStmt(E); |
| 2419 | } |
| 2420 | for (auto *E : C->assignment_ops()) { |
| 2421 | Visitor->AddStmt(E); |
| 2422 | } |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 2423 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2424 | void |
| 2425 | OMPClauseEnqueue::VisitOMPCopyprivateClause(const OMPCopyprivateClause *C) { |
| 2426 | VisitOMPClauseList(C); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2427 | for (auto *E : C->source_exprs()) { |
| 2428 | Visitor->AddStmt(E); |
| 2429 | } |
| 2430 | for (auto *E : C->destination_exprs()) { |
| 2431 | Visitor->AddStmt(E); |
| 2432 | } |
| 2433 | for (auto *E : C->assignment_ops()) { |
| 2434 | Visitor->AddStmt(E); |
| 2435 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2436 | } |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2437 | void OMPClauseEnqueue::VisitOMPFlushClause(const OMPFlushClause *C) { |
| 2438 | VisitOMPClauseList(C); |
| 2439 | } |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 2440 | void OMPClauseEnqueue::VisitOMPDependClause(const OMPDependClause *C) { |
| 2441 | VisitOMPClauseList(C); |
| 2442 | } |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 2443 | void OMPClauseEnqueue::VisitOMPMapClause(const OMPMapClause *C) { |
| 2444 | VisitOMPClauseList(C); |
| 2445 | } |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2446 | void OMPClauseEnqueue::VisitOMPDistScheduleClause( |
| 2447 | const OMPDistScheduleClause *C) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2448 | VisitOMPClauseWithPreInit(C); |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2449 | Visitor->AddStmt(C->getChunkSize()); |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2450 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2451 | void OMPClauseEnqueue::VisitOMPDefaultmapClause( |
| 2452 | const OMPDefaultmapClause * /*C*/) {} |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 2453 | void OMPClauseEnqueue::VisitOMPToClause(const OMPToClause *C) { |
| 2454 | VisitOMPClauseList(C); |
| 2455 | } |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 2456 | void OMPClauseEnqueue::VisitOMPFromClause(const OMPFromClause *C) { |
| 2457 | VisitOMPClauseList(C); |
| 2458 | } |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 2459 | void OMPClauseEnqueue::VisitOMPUseDevicePtrClause(const OMPUseDevicePtrClause *C) { |
| 2460 | VisitOMPClauseList(C); |
| 2461 | } |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 2462 | void OMPClauseEnqueue::VisitOMPIsDevicePtrClause(const OMPIsDevicePtrClause *C) { |
| 2463 | VisitOMPClauseList(C); |
| 2464 | } |
Alexey Bataev | b6e7084 | 2019-12-16 15:54:17 -0500 | [diff] [blame] | 2465 | void OMPClauseEnqueue::VisitOMPNontemporalClause( |
| 2466 | const OMPNontemporalClause *C) { |
| 2467 | VisitOMPClauseList(C); |
Alexey Bataev | 0860db9 | 2019-12-19 10:01:10 -0500 | [diff] [blame] | 2468 | for (const auto *E : C->private_refs()) |
| 2469 | Visitor->AddStmt(E); |
Alexey Bataev | b6e7084 | 2019-12-16 15:54:17 -0500 | [diff] [blame] | 2470 | } |
Alexey Bataev | cb8e691 | 2020-01-31 16:09:26 -0500 | [diff] [blame] | 2471 | void OMPClauseEnqueue::VisitOMPOrderClause(const OMPOrderClause *C) {} |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2472 | } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2473 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2474 | void EnqueueVisitor::EnqueueChildren(const OMPClause *S) { |
| 2475 | unsigned size = WL.size(); |
| 2476 | OMPClauseEnqueue Visitor(this); |
| 2477 | Visitor.Visit(S); |
| 2478 | if (size == WL.size()) |
| 2479 | return; |
| 2480 | // Now reverse the entries we just added. This will match the DFS |
| 2481 | // ordering performed by the worklist. |
| 2482 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2483 | std::reverse(I, E); |
| 2484 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2485 | void EnqueueVisitor::VisitAddrLabelExpr(const AddrLabelExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2486 | WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent)); |
| 2487 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2488 | void EnqueueVisitor::VisitBlockExpr(const BlockExpr *B) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2489 | AddDecl(B->getBlockDecl()); |
| 2490 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2491 | void EnqueueVisitor::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2492 | EnqueueChildren(E); |
| 2493 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2494 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2495 | void EnqueueVisitor::VisitCompoundStmt(const CompoundStmt *S) { |
Pete Cooper | 57d3f14 | 2015-07-30 17:22:52 +0000 | [diff] [blame] | 2496 | for (auto &I : llvm::reverse(S->body())) |
| 2497 | AddStmt(I); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2498 | } |
| 2499 | void EnqueueVisitor:: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2500 | VisitMSDependentExistsStmt(const MSDependentExistsStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2501 | AddStmt(S->getSubStmt()); |
| 2502 | AddDeclarationNameInfo(S); |
| 2503 | if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc()) |
| 2504 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2505 | } |
| 2506 | |
| 2507 | void EnqueueVisitor:: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2508 | VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2509 | if (E->hasExplicitTemplateArgs()) |
| 2510 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2511 | AddDeclarationNameInfo(E); |
| 2512 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 2513 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2514 | if (!E->isImplicitAccess()) |
| 2515 | AddStmt(E->getBase()); |
| 2516 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2517 | void EnqueueVisitor::VisitCXXNewExpr(const CXXNewExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2518 | // Enqueue the initializer , if any. |
| 2519 | AddStmt(E->getInitializer()); |
| 2520 | // Enqueue the array size, if any. |
Richard Smith | b9fb121 | 2019-05-06 03:47:15 +0000 | [diff] [blame] | 2521 | AddStmt(E->getArraySize().getValueOr(nullptr)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2522 | // Enqueue the allocated type. |
| 2523 | AddTypeLoc(E->getAllocatedTypeSourceInfo()); |
| 2524 | // Enqueue the placement arguments. |
| 2525 | for (unsigned I = E->getNumPlacementArgs(); I > 0; --I) |
| 2526 | AddStmt(E->getPlacementArg(I-1)); |
| 2527 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2528 | void EnqueueVisitor::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CE) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2529 | for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I) |
| 2530 | AddStmt(CE->getArg(I-1)); |
| 2531 | AddStmt(CE->getCallee()); |
| 2532 | AddStmt(CE->getArg(0)); |
| 2533 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2534 | void EnqueueVisitor::VisitCXXPseudoDestructorExpr( |
| 2535 | const CXXPseudoDestructorExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2536 | // Visit the name of the type being destroyed. |
| 2537 | AddTypeLoc(E->getDestroyedTypeInfo()); |
| 2538 | // Visit the scope type that looks disturbingly like the nested-name-specifier |
| 2539 | // but isn't. |
| 2540 | AddTypeLoc(E->getScopeTypeInfo()); |
| 2541 | // Visit the nested-name-specifier. |
| 2542 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 2543 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2544 | // Visit base expression. |
| 2545 | AddStmt(E->getBase()); |
| 2546 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2547 | void EnqueueVisitor::VisitCXXScalarValueInitExpr( |
| 2548 | const CXXScalarValueInitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2549 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2550 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2551 | void EnqueueVisitor::VisitCXXTemporaryObjectExpr( |
| 2552 | const CXXTemporaryObjectExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2553 | EnqueueChildren(E); |
| 2554 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2555 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2556 | void EnqueueVisitor::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2557 | EnqueueChildren(E); |
| 2558 | if (E->isTypeOperand()) |
| 2559 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 2560 | } |
| 2561 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2562 | void EnqueueVisitor::VisitCXXUnresolvedConstructExpr( |
| 2563 | const CXXUnresolvedConstructExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2564 | EnqueueChildren(E); |
| 2565 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2566 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2567 | void EnqueueVisitor::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2568 | EnqueueChildren(E); |
| 2569 | if (E->isTypeOperand()) |
| 2570 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 2571 | } |
| 2572 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2573 | void EnqueueVisitor::VisitCXXCatchStmt(const CXXCatchStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2574 | EnqueueChildren(S); |
| 2575 | AddDecl(S->getExceptionDecl()); |
| 2576 | } |
| 2577 | |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2578 | void EnqueueVisitor::VisitCXXForRangeStmt(const CXXForRangeStmt *S) { |
Argyrios Kyrtzidis | cde7069 | 2014-11-13 09:50:19 +0000 | [diff] [blame] | 2579 | AddStmt(S->getBody()); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2580 | AddStmt(S->getRangeInit()); |
Argyrios Kyrtzidis | cde7069 | 2014-11-13 09:50:19 +0000 | [diff] [blame] | 2581 | AddDecl(S->getLoopVariable()); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2584 | void EnqueueVisitor::VisitDeclRefExpr(const DeclRefExpr *DR) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2585 | if (DR->hasExplicitTemplateArgs()) |
| 2586 | AddExplicitTemplateArgs(DR->getTemplateArgs(), DR->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2587 | WL.push_back(DeclRefExprParts(DR, Parent)); |
| 2588 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2589 | void EnqueueVisitor::VisitDependentScopeDeclRefExpr( |
| 2590 | const DependentScopeDeclRefExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2591 | if (E->hasExplicitTemplateArgs()) |
| 2592 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2593 | AddDeclarationNameInfo(E); |
| 2594 | AddNestedNameSpecifierLoc(E->getQualifierLoc()); |
| 2595 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2596 | void EnqueueVisitor::VisitDeclStmt(const DeclStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2597 | unsigned size = WL.size(); |
| 2598 | bool isFirst = true; |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 2599 | for (const auto *D : S->decls()) { |
| 2600 | AddDecl(D, isFirst); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2601 | isFirst = false; |
| 2602 | } |
| 2603 | if (size == WL.size()) |
| 2604 | return; |
| 2605 | // Now reverse the entries we just added. This will match the DFS |
| 2606 | // ordering performed by the worklist. |
| 2607 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2608 | std::reverse(I, E); |
| 2609 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2610 | void EnqueueVisitor::VisitDesignatedInitExpr(const DesignatedInitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2611 | AddStmt(E->getInit()); |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2612 | for (const DesignatedInitExpr::Designator &D : |
| 2613 | llvm::reverse(E->designators())) { |
| 2614 | if (D.isFieldDesignator()) { |
| 2615 | if (FieldDecl *Field = D.getField()) |
| 2616 | AddMemberRef(Field, D.getFieldLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2617 | continue; |
| 2618 | } |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2619 | if (D.isArrayDesignator()) { |
| 2620 | AddStmt(E->getArrayIndex(D)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2621 | continue; |
| 2622 | } |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2623 | assert(D.isArrayRangeDesignator() && "Unknown designator kind"); |
| 2624 | AddStmt(E->getArrayRangeEnd(D)); |
| 2625 | AddStmt(E->getArrayRangeStart(D)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2626 | } |
| 2627 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2628 | void EnqueueVisitor::VisitExplicitCastExpr(const ExplicitCastExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2629 | EnqueueChildren(E); |
| 2630 | AddTypeLoc(E->getTypeInfoAsWritten()); |
| 2631 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2632 | void EnqueueVisitor::VisitForStmt(const ForStmt *FS) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2633 | AddStmt(FS->getBody()); |
| 2634 | AddStmt(FS->getInc()); |
| 2635 | AddStmt(FS->getCond()); |
| 2636 | AddDecl(FS->getConditionVariable()); |
| 2637 | AddStmt(FS->getInit()); |
| 2638 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2639 | void EnqueueVisitor::VisitGotoStmt(const GotoStmt *GS) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2640 | WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent)); |
| 2641 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2642 | void EnqueueVisitor::VisitIfStmt(const IfStmt *If) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2643 | AddStmt(If->getElse()); |
| 2644 | AddStmt(If->getThen()); |
| 2645 | AddStmt(If->getCond()); |
| 2646 | AddDecl(If->getConditionVariable()); |
| 2647 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2648 | void EnqueueVisitor::VisitInitListExpr(const InitListExpr *IE) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2649 | // We care about the syntactic form of the initializer list, only. |
| 2650 | if (InitListExpr *Syntactic = IE->getSyntacticForm()) |
| 2651 | IE = Syntactic; |
| 2652 | EnqueueChildren(IE); |
| 2653 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2654 | void EnqueueVisitor::VisitMemberExpr(const MemberExpr *M) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2655 | WL.push_back(MemberExprParts(M, Parent)); |
| 2656 | |
| 2657 | // If the base of the member access expression is an implicit 'this', don't |
| 2658 | // visit it. |
| 2659 | // FIXME: If we ever want to show these implicit accesses, this will be |
| 2660 | // unfortunate. However, clang_getCursor() relies on this behavior. |
Argyrios Kyrtzidis | 58d0e7a | 2015-03-13 04:40:07 +0000 | [diff] [blame] | 2661 | if (M->isImplicitAccess()) |
| 2662 | return; |
| 2663 | |
| 2664 | // Ignore base anonymous struct/union fields, otherwise they will shadow the |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 2665 | // real field that we are interested in. |
Argyrios Kyrtzidis | 58d0e7a | 2015-03-13 04:40:07 +0000 | [diff] [blame] | 2666 | if (auto *SubME = dyn_cast<MemberExpr>(M->getBase())) { |
| 2667 | if (auto *FD = dyn_cast_or_null<FieldDecl>(SubME->getMemberDecl())) { |
| 2668 | if (FD->isAnonymousStructOrUnion()) { |
| 2669 | AddStmt(SubME->getBase()); |
| 2670 | return; |
| 2671 | } |
| 2672 | } |
| 2673 | } |
| 2674 | |
| 2675 | AddStmt(M->getBase()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2676 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2677 | void EnqueueVisitor::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2678 | AddTypeLoc(E->getEncodedTypeSourceInfo()); |
| 2679 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2680 | void EnqueueVisitor::VisitObjCMessageExpr(const ObjCMessageExpr *M) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2681 | EnqueueChildren(M); |
| 2682 | AddTypeLoc(M->getClassReceiverTypeInfo()); |
| 2683 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2684 | void EnqueueVisitor::VisitOffsetOfExpr(const OffsetOfExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2685 | // Visit the components of the offsetof expression. |
| 2686 | for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2687 | const OffsetOfNode &Node = E->getComponent(I-1); |
| 2688 | switch (Node.getKind()) { |
| 2689 | case OffsetOfNode::Array: |
| 2690 | AddStmt(E->getIndexExpr(Node.getArrayExprIndex())); |
| 2691 | break; |
| 2692 | case OffsetOfNode::Field: |
| 2693 | AddMemberRef(Node.getField(), Node.getSourceRange().getEnd()); |
| 2694 | break; |
| 2695 | case OffsetOfNode::Identifier: |
| 2696 | case OffsetOfNode::Base: |
| 2697 | continue; |
| 2698 | } |
| 2699 | } |
| 2700 | // Visit the type into which we're computing the offset. |
| 2701 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2702 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2703 | void EnqueueVisitor::VisitOverloadExpr(const OverloadExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2704 | if (E->hasExplicitTemplateArgs()) |
| 2705 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2706 | WL.push_back(OverloadExprParts(E, Parent)); |
| 2707 | } |
| 2708 | void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr( |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2709 | const UnaryExprOrTypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2710 | EnqueueChildren(E); |
| 2711 | if (E->isArgumentType()) |
| 2712 | AddTypeLoc(E->getArgumentTypeInfo()); |
| 2713 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2714 | void EnqueueVisitor::VisitStmt(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2715 | EnqueueChildren(S); |
| 2716 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2717 | void EnqueueVisitor::VisitSwitchStmt(const SwitchStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2718 | AddStmt(S->getBody()); |
| 2719 | AddStmt(S->getCond()); |
| 2720 | AddDecl(S->getConditionVariable()); |
| 2721 | } |
| 2722 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2723 | void EnqueueVisitor::VisitWhileStmt(const WhileStmt *W) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2724 | AddStmt(W->getBody()); |
| 2725 | AddStmt(W->getCond()); |
| 2726 | AddDecl(W->getConditionVariable()); |
| 2727 | } |
| 2728 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2729 | void EnqueueVisitor::VisitTypeTraitExpr(const TypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2730 | for (unsigned I = E->getNumArgs(); I > 0; --I) |
| 2731 | AddTypeLoc(E->getArg(I-1)); |
| 2732 | } |
| 2733 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2734 | void EnqueueVisitor::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2735 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2736 | } |
| 2737 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2738 | void EnqueueVisitor::VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2739 | EnqueueChildren(E); |
| 2740 | } |
| 2741 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2742 | void EnqueueVisitor::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2743 | VisitOverloadExpr(U); |
| 2744 | if (!U->isImplicitAccess()) |
| 2745 | AddStmt(U->getBase()); |
| 2746 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2747 | void EnqueueVisitor::VisitVAArgExpr(const VAArgExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2748 | AddStmt(E->getSubExpr()); |
| 2749 | AddTypeLoc(E->getWrittenTypeInfo()); |
| 2750 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2751 | void EnqueueVisitor::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2752 | WL.push_back(SizeOfPackExprParts(E, Parent)); |
| 2753 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2754 | void EnqueueVisitor::VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2755 | // If the opaque value has a source expression, just transparently |
| 2756 | // visit that. This is useful for (e.g.) pseudo-object expressions. |
| 2757 | if (Expr *SourceExpr = E->getSourceExpr()) |
| 2758 | return Visit(SourceExpr); |
| 2759 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2760 | void EnqueueVisitor::VisitLambdaExpr(const LambdaExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2761 | AddStmt(E->getBody()); |
| 2762 | WL.push_back(LambdaExprParts(E, Parent)); |
| 2763 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2764 | void EnqueueVisitor::VisitPseudoObjectExpr(const PseudoObjectExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2765 | // Treat the expression like its syntactic form. |
| 2766 | Visit(E->getSyntacticForm()); |
| 2767 | } |
| 2768 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2769 | void EnqueueVisitor::VisitOMPExecutableDirective( |
| 2770 | const OMPExecutableDirective *D) { |
| 2771 | EnqueueChildren(D); |
| 2772 | for (ArrayRef<OMPClause *>::iterator I = D->clauses().begin(), |
| 2773 | E = D->clauses().end(); |
| 2774 | I != E; ++I) |
| 2775 | EnqueueChildren(*I); |
| 2776 | } |
| 2777 | |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2778 | void EnqueueVisitor::VisitOMPLoopDirective(const OMPLoopDirective *D) { |
| 2779 | VisitOMPExecutableDirective(D); |
| 2780 | } |
| 2781 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2782 | void EnqueueVisitor::VisitOMPParallelDirective(const OMPParallelDirective *D) { |
| 2783 | VisitOMPExecutableDirective(D); |
| 2784 | } |
| 2785 | |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2786 | void EnqueueVisitor::VisitOMPSimdDirective(const OMPSimdDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2787 | VisitOMPLoopDirective(D); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2790 | void EnqueueVisitor::VisitOMPForDirective(const OMPForDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2791 | VisitOMPLoopDirective(D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2794 | void EnqueueVisitor::VisitOMPForSimdDirective(const OMPForSimdDirective *D) { |
| 2795 | VisitOMPLoopDirective(D); |
| 2796 | } |
| 2797 | |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2798 | void EnqueueVisitor::VisitOMPSectionsDirective(const OMPSectionsDirective *D) { |
| 2799 | VisitOMPExecutableDirective(D); |
| 2800 | } |
| 2801 | |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2802 | void EnqueueVisitor::VisitOMPSectionDirective(const OMPSectionDirective *D) { |
| 2803 | VisitOMPExecutableDirective(D); |
| 2804 | } |
| 2805 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 2806 | void EnqueueVisitor::VisitOMPSingleDirective(const OMPSingleDirective *D) { |
| 2807 | VisitOMPExecutableDirective(D); |
| 2808 | } |
| 2809 | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2810 | void EnqueueVisitor::VisitOMPMasterDirective(const OMPMasterDirective *D) { |
| 2811 | VisitOMPExecutableDirective(D); |
| 2812 | } |
| 2813 | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2814 | void EnqueueVisitor::VisitOMPCriticalDirective(const OMPCriticalDirective *D) { |
| 2815 | VisitOMPExecutableDirective(D); |
| 2816 | AddDeclarationNameInfo(D); |
| 2817 | } |
| 2818 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2819 | void |
| 2820 | EnqueueVisitor::VisitOMPParallelForDirective(const OMPParallelForDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2821 | VisitOMPLoopDirective(D); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2824 | void EnqueueVisitor::VisitOMPParallelForSimdDirective( |
| 2825 | const OMPParallelForSimdDirective *D) { |
| 2826 | VisitOMPLoopDirective(D); |
| 2827 | } |
| 2828 | |
cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 2829 | void EnqueueVisitor::VisitOMPParallelMasterDirective( |
| 2830 | const OMPParallelMasterDirective *D) { |
| 2831 | VisitOMPExecutableDirective(D); |
| 2832 | } |
| 2833 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2834 | void EnqueueVisitor::VisitOMPParallelSectionsDirective( |
| 2835 | const OMPParallelSectionsDirective *D) { |
| 2836 | VisitOMPExecutableDirective(D); |
| 2837 | } |
| 2838 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2839 | void EnqueueVisitor::VisitOMPTaskDirective(const OMPTaskDirective *D) { |
| 2840 | VisitOMPExecutableDirective(D); |
| 2841 | } |
| 2842 | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2843 | void |
| 2844 | EnqueueVisitor::VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D) { |
| 2845 | VisitOMPExecutableDirective(D); |
| 2846 | } |
| 2847 | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 2848 | void EnqueueVisitor::VisitOMPBarrierDirective(const OMPBarrierDirective *D) { |
| 2849 | VisitOMPExecutableDirective(D); |
| 2850 | } |
| 2851 | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 2852 | void EnqueueVisitor::VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D) { |
| 2853 | VisitOMPExecutableDirective(D); |
| 2854 | } |
| 2855 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2856 | void EnqueueVisitor::VisitOMPTaskgroupDirective( |
| 2857 | const OMPTaskgroupDirective *D) { |
| 2858 | VisitOMPExecutableDirective(D); |
Alexey Bataev | 3b1b895 | 2017-07-25 15:53:26 +0000 | [diff] [blame] | 2859 | if (const Expr *E = D->getReductionRef()) |
| 2860 | VisitStmt(E); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2863 | void EnqueueVisitor::VisitOMPFlushDirective(const OMPFlushDirective *D) { |
| 2864 | VisitOMPExecutableDirective(D); |
| 2865 | } |
| 2866 | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2867 | void EnqueueVisitor::VisitOMPOrderedDirective(const OMPOrderedDirective *D) { |
| 2868 | VisitOMPExecutableDirective(D); |
| 2869 | } |
| 2870 | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2871 | void EnqueueVisitor::VisitOMPAtomicDirective(const OMPAtomicDirective *D) { |
| 2872 | VisitOMPExecutableDirective(D); |
| 2873 | } |
| 2874 | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 2875 | void EnqueueVisitor::VisitOMPTargetDirective(const OMPTargetDirective *D) { |
| 2876 | VisitOMPExecutableDirective(D); |
| 2877 | } |
| 2878 | |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2879 | void EnqueueVisitor::VisitOMPTargetDataDirective(const |
| 2880 | OMPTargetDataDirective *D) { |
| 2881 | VisitOMPExecutableDirective(D); |
| 2882 | } |
| 2883 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 2884 | void EnqueueVisitor::VisitOMPTargetEnterDataDirective( |
| 2885 | const OMPTargetEnterDataDirective *D) { |
| 2886 | VisitOMPExecutableDirective(D); |
| 2887 | } |
| 2888 | |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 2889 | void EnqueueVisitor::VisitOMPTargetExitDataDirective( |
| 2890 | const OMPTargetExitDataDirective *D) { |
| 2891 | VisitOMPExecutableDirective(D); |
| 2892 | } |
| 2893 | |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 2894 | void EnqueueVisitor::VisitOMPTargetParallelDirective( |
| 2895 | const OMPTargetParallelDirective *D) { |
| 2896 | VisitOMPExecutableDirective(D); |
| 2897 | } |
| 2898 | |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 2899 | void EnqueueVisitor::VisitOMPTargetParallelForDirective( |
| 2900 | const OMPTargetParallelForDirective *D) { |
| 2901 | VisitOMPLoopDirective(D); |
| 2902 | } |
| 2903 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2904 | void EnqueueVisitor::VisitOMPTeamsDirective(const OMPTeamsDirective *D) { |
| 2905 | VisitOMPExecutableDirective(D); |
| 2906 | } |
| 2907 | |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2908 | void EnqueueVisitor::VisitOMPCancellationPointDirective( |
| 2909 | const OMPCancellationPointDirective *D) { |
| 2910 | VisitOMPExecutableDirective(D); |
| 2911 | } |
| 2912 | |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2913 | void EnqueueVisitor::VisitOMPCancelDirective(const OMPCancelDirective *D) { |
| 2914 | VisitOMPExecutableDirective(D); |
| 2915 | } |
| 2916 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 2917 | void EnqueueVisitor::VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D) { |
| 2918 | VisitOMPLoopDirective(D); |
| 2919 | } |
| 2920 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 2921 | void EnqueueVisitor::VisitOMPTaskLoopSimdDirective( |
| 2922 | const OMPTaskLoopSimdDirective *D) { |
| 2923 | VisitOMPLoopDirective(D); |
| 2924 | } |
| 2925 | |
Alexey Bataev | 60e51c4 | 2019-10-10 20:13:02 +0000 | [diff] [blame] | 2926 | void EnqueueVisitor::VisitOMPMasterTaskLoopDirective( |
| 2927 | const OMPMasterTaskLoopDirective *D) { |
| 2928 | VisitOMPLoopDirective(D); |
| 2929 | } |
| 2930 | |
Alexey Bataev | b8552ab | 2019-10-18 16:47:35 +0000 | [diff] [blame] | 2931 | void EnqueueVisitor::VisitOMPMasterTaskLoopSimdDirective( |
| 2932 | const OMPMasterTaskLoopSimdDirective *D) { |
| 2933 | VisitOMPLoopDirective(D); |
| 2934 | } |
| 2935 | |
Alexey Bataev | 5bbcead | 2019-10-14 17:17:41 +0000 | [diff] [blame] | 2936 | void EnqueueVisitor::VisitOMPParallelMasterTaskLoopDirective( |
| 2937 | const OMPParallelMasterTaskLoopDirective *D) { |
| 2938 | VisitOMPLoopDirective(D); |
| 2939 | } |
| 2940 | |
Alexey Bataev | 14a388f | 2019-10-25 10:27:13 -0400 | [diff] [blame] | 2941 | void EnqueueVisitor::VisitOMPParallelMasterTaskLoopSimdDirective( |
| 2942 | const OMPParallelMasterTaskLoopSimdDirective *D) { |
| 2943 | VisitOMPLoopDirective(D); |
| 2944 | } |
| 2945 | |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2946 | void EnqueueVisitor::VisitOMPDistributeDirective( |
| 2947 | const OMPDistributeDirective *D) { |
| 2948 | VisitOMPLoopDirective(D); |
| 2949 | } |
| 2950 | |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2951 | void EnqueueVisitor::VisitOMPDistributeParallelForDirective( |
| 2952 | const OMPDistributeParallelForDirective *D) { |
| 2953 | VisitOMPLoopDirective(D); |
| 2954 | } |
| 2955 | |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2956 | void EnqueueVisitor::VisitOMPDistributeParallelForSimdDirective( |
| 2957 | const OMPDistributeParallelForSimdDirective *D) { |
| 2958 | VisitOMPLoopDirective(D); |
| 2959 | } |
| 2960 | |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2961 | void EnqueueVisitor::VisitOMPDistributeSimdDirective( |
| 2962 | const OMPDistributeSimdDirective *D) { |
| 2963 | VisitOMPLoopDirective(D); |
| 2964 | } |
| 2965 | |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 2966 | void EnqueueVisitor::VisitOMPTargetParallelForSimdDirective( |
| 2967 | const OMPTargetParallelForSimdDirective *D) { |
| 2968 | VisitOMPLoopDirective(D); |
| 2969 | } |
| 2970 | |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2971 | void EnqueueVisitor::VisitOMPTargetSimdDirective( |
| 2972 | const OMPTargetSimdDirective *D) { |
| 2973 | VisitOMPLoopDirective(D); |
| 2974 | } |
| 2975 | |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 2976 | void EnqueueVisitor::VisitOMPTeamsDistributeDirective( |
| 2977 | const OMPTeamsDistributeDirective *D) { |
| 2978 | VisitOMPLoopDirective(D); |
| 2979 | } |
| 2980 | |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 2981 | void EnqueueVisitor::VisitOMPTeamsDistributeSimdDirective( |
| 2982 | const OMPTeamsDistributeSimdDirective *D) { |
| 2983 | VisitOMPLoopDirective(D); |
| 2984 | } |
| 2985 | |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 2986 | void EnqueueVisitor::VisitOMPTeamsDistributeParallelForSimdDirective( |
| 2987 | const OMPTeamsDistributeParallelForSimdDirective *D) { |
| 2988 | VisitOMPLoopDirective(D); |
| 2989 | } |
| 2990 | |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 2991 | void EnqueueVisitor::VisitOMPTeamsDistributeParallelForDirective( |
| 2992 | const OMPTeamsDistributeParallelForDirective *D) { |
| 2993 | VisitOMPLoopDirective(D); |
| 2994 | } |
| 2995 | |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 2996 | void EnqueueVisitor::VisitOMPTargetTeamsDirective( |
| 2997 | const OMPTargetTeamsDirective *D) { |
| 2998 | VisitOMPExecutableDirective(D); |
| 2999 | } |
| 3000 | |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 3001 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeDirective( |
| 3002 | const OMPTargetTeamsDistributeDirective *D) { |
| 3003 | VisitOMPLoopDirective(D); |
| 3004 | } |
| 3005 | |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 3006 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeParallelForDirective( |
| 3007 | const OMPTargetTeamsDistributeParallelForDirective *D) { |
| 3008 | VisitOMPLoopDirective(D); |
| 3009 | } |
| 3010 | |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 3011 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeParallelForSimdDirective( |
| 3012 | const OMPTargetTeamsDistributeParallelForSimdDirective *D) { |
| 3013 | VisitOMPLoopDirective(D); |
| 3014 | } |
| 3015 | |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 3016 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeSimdDirective( |
| 3017 | const OMPTargetTeamsDistributeSimdDirective *D) { |
| 3018 | VisitOMPLoopDirective(D); |
| 3019 | } |
| 3020 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3021 | void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3022 | EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S); |
| 3023 | } |
| 3024 | |
| 3025 | bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { |
| 3026 | if (RegionOfInterest.isValid()) { |
| 3027 | SourceRange Range = getRawCursorExtent(C); |
| 3028 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 3029 | return false; |
| 3030 | } |
| 3031 | return true; |
| 3032 | } |
| 3033 | |
| 3034 | bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { |
| 3035 | while (!WL.empty()) { |
| 3036 | // Dequeue the worklist item. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 3037 | VisitorJob LI = WL.pop_back_val(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3038 | |
| 3039 | // Set the Parent field, then back to its old value once we're done. |
| 3040 | SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); |
| 3041 | |
| 3042 | switch (LI.getKind()) { |
| 3043 | case VisitorJob::DeclVisitKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3044 | const Decl *D = cast<DeclVisit>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3045 | if (!D) |
| 3046 | continue; |
| 3047 | |
| 3048 | // For now, perform default visitation for Decls. |
| 3049 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest, |
| 3050 | cast<DeclVisit>(&LI)->isFirst()))) |
| 3051 | return true; |
| 3052 | |
| 3053 | continue; |
| 3054 | } |
| 3055 | case VisitorJob::ExplicitTemplateArgsVisitKind: { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3056 | for (const TemplateArgumentLoc &Arg : |
| 3057 | *cast<ExplicitTemplateArgsVisit>(&LI)) { |
| 3058 | if (VisitTemplateArgumentLoc(Arg)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3059 | return true; |
| 3060 | } |
| 3061 | continue; |
| 3062 | } |
| 3063 | case VisitorJob::TypeLocVisitKind: { |
| 3064 | // Perform default visitation for TypeLocs. |
| 3065 | if (Visit(cast<TypeLocVisit>(&LI)->get())) |
| 3066 | return true; |
| 3067 | continue; |
| 3068 | } |
| 3069 | case VisitorJob::LabelRefVisitKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3070 | const LabelDecl *LS = cast<LabelRefVisit>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3071 | if (LabelStmt *stmt = LS->getStmt()) { |
| 3072 | if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(), |
| 3073 | TU))) { |
| 3074 | return true; |
| 3075 | } |
| 3076 | } |
| 3077 | continue; |
| 3078 | } |
| 3079 | |
| 3080 | case VisitorJob::NestedNameSpecifierLocVisitKind: { |
| 3081 | NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI); |
| 3082 | if (VisitNestedNameSpecifierLoc(V->get())) |
| 3083 | return true; |
| 3084 | continue; |
| 3085 | } |
| 3086 | |
| 3087 | case VisitorJob::DeclarationNameInfoVisitKind: { |
| 3088 | if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI) |
| 3089 | ->get())) |
| 3090 | return true; |
| 3091 | continue; |
| 3092 | } |
| 3093 | case VisitorJob::MemberRefVisitKind: { |
| 3094 | MemberRefVisit *V = cast<MemberRefVisit>(&LI); |
| 3095 | if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU))) |
| 3096 | return true; |
| 3097 | continue; |
| 3098 | } |
| 3099 | case VisitorJob::StmtVisitKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3100 | const Stmt *S = cast<StmtVisit>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3101 | if (!S) |
| 3102 | continue; |
| 3103 | |
| 3104 | // Update the current cursor. |
| 3105 | CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest); |
| 3106 | if (!IsInRegionOfInterest(Cursor)) |
| 3107 | continue; |
| 3108 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 3109 | case CXChildVisit_Break: return true; |
| 3110 | case CXChildVisit_Continue: break; |
| 3111 | case CXChildVisit_Recurse: |
| 3112 | if (PostChildrenVisitor) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3113 | WL.push_back(PostChildrenVisit(nullptr, Cursor)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3114 | EnqueueWorkList(WL, S); |
| 3115 | break; |
| 3116 | } |
| 3117 | continue; |
| 3118 | } |
| 3119 | case VisitorJob::MemberExprPartsKind: { |
| 3120 | // Handle the other pieces in the MemberExpr besides the base. |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3121 | const MemberExpr *M = cast<MemberExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3122 | |
| 3123 | // Visit the nested-name-specifier |
| 3124 | if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc()) |
| 3125 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 3126 | return true; |
| 3127 | |
| 3128 | // Visit the declaration name. |
| 3129 | if (VisitDeclarationNameInfo(M->getMemberNameInfo())) |
| 3130 | return true; |
| 3131 | |
| 3132 | // Visit the explicitly-specified template arguments, if any. |
| 3133 | if (M->hasExplicitTemplateArgs()) { |
| 3134 | for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), |
| 3135 | *ArgEnd = Arg + M->getNumTemplateArgs(); |
| 3136 | Arg != ArgEnd; ++Arg) { |
| 3137 | if (VisitTemplateArgumentLoc(*Arg)) |
| 3138 | return true; |
| 3139 | } |
| 3140 | } |
| 3141 | continue; |
| 3142 | } |
| 3143 | case VisitorJob::DeclRefExprPartsKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3144 | const DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3145 | // Visit nested-name-specifier, if present. |
| 3146 | if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc()) |
| 3147 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 3148 | return true; |
| 3149 | // Visit declaration name. |
| 3150 | if (VisitDeclarationNameInfo(DR->getNameInfo())) |
| 3151 | return true; |
| 3152 | continue; |
| 3153 | } |
| 3154 | case VisitorJob::OverloadExprPartsKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3155 | const OverloadExpr *O = cast<OverloadExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3156 | // Visit the nested-name-specifier. |
| 3157 | if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc()) |
| 3158 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 3159 | return true; |
| 3160 | // Visit the declaration name. |
| 3161 | if (VisitDeclarationNameInfo(O->getNameInfo())) |
| 3162 | return true; |
| 3163 | // Visit the overloaded declaration reference. |
| 3164 | if (Visit(MakeCursorOverloadedDeclRef(O, TU))) |
| 3165 | return true; |
| 3166 | continue; |
| 3167 | } |
| 3168 | case VisitorJob::SizeOfPackExprPartsKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3169 | const SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3170 | NamedDecl *Pack = E->getPack(); |
| 3171 | if (isa<TemplateTypeParmDecl>(Pack)) { |
| 3172 | if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack), |
| 3173 | E->getPackLoc(), TU))) |
| 3174 | return true; |
| 3175 | |
| 3176 | continue; |
| 3177 | } |
| 3178 | |
| 3179 | if (isa<TemplateTemplateParmDecl>(Pack)) { |
| 3180 | if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack), |
| 3181 | E->getPackLoc(), TU))) |
| 3182 | return true; |
| 3183 | |
| 3184 | continue; |
| 3185 | } |
| 3186 | |
| 3187 | // Non-type template parameter packs and function parameter packs are |
| 3188 | // treated like DeclRefExpr cursors. |
| 3189 | continue; |
| 3190 | } |
| 3191 | |
| 3192 | case VisitorJob::LambdaExprPartsKind: { |
Nikolai Kosjar | 2eebf4d9 | 2019-05-21 09:21:35 +0000 | [diff] [blame] | 3193 | // Visit non-init captures. |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3194 | const LambdaExpr *E = cast<LambdaExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3195 | for (LambdaExpr::capture_iterator C = E->explicit_capture_begin(), |
| 3196 | CEnd = E->explicit_capture_end(); |
| 3197 | C != CEnd; ++C) { |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 3198 | if (!C->capturesVariable()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3199 | continue; |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 3200 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3201 | if (Visit(MakeCursorVariableRef(C->getCapturedVar(), |
| 3202 | C->getLocation(), |
| 3203 | TU))) |
| 3204 | return true; |
| 3205 | } |
Nikolai Kosjar | 2eebf4d9 | 2019-05-21 09:21:35 +0000 | [diff] [blame] | 3206 | // Visit init captures |
| 3207 | for (auto InitExpr : E->capture_inits()) { |
| 3208 | if (Visit(InitExpr)) |
| 3209 | return true; |
| 3210 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3211 | |
Haojian Wu | ef87c26 | 2018-12-18 15:29:12 +0000 | [diff] [blame] | 3212 | TypeLoc TL = E->getCallOperator()->getTypeSourceInfo()->getTypeLoc(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3213 | // Visit parameters and return type, if present. |
Haojian Wu | ef87c26 | 2018-12-18 15:29:12 +0000 | [diff] [blame] | 3214 | if (FunctionTypeLoc Proto = TL.getAs<FunctionProtoTypeLoc>()) { |
| 3215 | if (E->hasExplicitParameters()) { |
| 3216 | // Visit parameters. |
| 3217 | for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) |
| 3218 | if (Visit(MakeCXCursor(Proto.getParam(I), TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3219 | return true; |
Haojian Wu | ef87c26 | 2018-12-18 15:29:12 +0000 | [diff] [blame] | 3220 | } |
| 3221 | if (E->hasExplicitResultType()) { |
| 3222 | // Visit result type. |
| 3223 | if (Visit(Proto.getReturnLoc())) |
| 3224 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3225 | } |
| 3226 | } |
| 3227 | break; |
| 3228 | } |
| 3229 | |
| 3230 | case VisitorJob::PostChildrenVisitKind: |
| 3231 | if (PostChildrenVisitor(Parent, ClientData)) |
| 3232 | return true; |
| 3233 | break; |
| 3234 | } |
| 3235 | } |
| 3236 | return false; |
| 3237 | } |
| 3238 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3239 | bool CursorVisitor::Visit(const Stmt *S) { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3240 | VisitorWorkList *WL = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3241 | if (!WorkListFreeList.empty()) { |
| 3242 | WL = WorkListFreeList.back(); |
| 3243 | WL->clear(); |
| 3244 | WorkListFreeList.pop_back(); |
| 3245 | } |
| 3246 | else { |
| 3247 | WL = new VisitorWorkList(); |
| 3248 | WorkListCache.push_back(WL); |
| 3249 | } |
| 3250 | EnqueueWorkList(*WL, S); |
| 3251 | bool result = RunVisitorWorkList(*WL); |
| 3252 | WorkListFreeList.push_back(WL); |
| 3253 | return result; |
| 3254 | } |
| 3255 | |
| 3256 | namespace { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3257 | typedef SmallVector<SourceRange, 4> RefNamePieces; |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3258 | RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr, |
| 3259 | const DeclarationNameInfo &NI, SourceRange QLoc, |
| 3260 | const SourceRange *TemplateArgsLoc = nullptr) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3261 | const bool WantQualifier = NameFlags & CXNameRange_WantQualifier; |
| 3262 | const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs; |
| 3263 | const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece; |
| 3264 | |
| 3265 | const DeclarationName::NameKind Kind = NI.getName().getNameKind(); |
| 3266 | |
| 3267 | RefNamePieces Pieces; |
| 3268 | |
| 3269 | if (WantQualifier && QLoc.isValid()) |
| 3270 | Pieces.push_back(QLoc); |
| 3271 | |
| 3272 | if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr) |
| 3273 | Pieces.push_back(NI.getLoc()); |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3274 | |
| 3275 | if (WantTemplateArgs && TemplateArgsLoc && TemplateArgsLoc->isValid()) |
| 3276 | Pieces.push_back(*TemplateArgsLoc); |
| 3277 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3278 | if (Kind == DeclarationName::CXXOperatorName) { |
| 3279 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 3280 | NI.getInfo().CXXOperatorName.BeginOpNameLoc)); |
| 3281 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 3282 | NI.getInfo().CXXOperatorName.EndOpNameLoc)); |
| 3283 | } |
| 3284 | |
| 3285 | if (WantSinglePiece) { |
| 3286 | SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd()); |
| 3287 | Pieces.clear(); |
| 3288 | Pieces.push_back(R); |
| 3289 | } |
| 3290 | |
| 3291 | return Pieces; |
| 3292 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3293 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3294 | |
| 3295 | //===----------------------------------------------------------------------===// |
| 3296 | // Misc. API hooks. |
| 3297 | //===----------------------------------------------------------------------===// |
| 3298 | |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 3299 | namespace { |
| 3300 | struct RegisterFatalErrorHandler { |
| 3301 | RegisterFatalErrorHandler() { |
Jan Korous | f7d2376 | 2019-09-12 22:55:55 +0000 | [diff] [blame] | 3302 | clang_install_aborting_llvm_fatal_error_handler(); |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 3303 | } |
| 3304 | }; |
| 3305 | } |
| 3306 | |
| 3307 | static llvm::ManagedStatic<RegisterFatalErrorHandler> RegisterFatalErrorHandlerOnce; |
| 3308 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3309 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 3310 | int displayDiagnostics) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3311 | // We use crash recovery to make some of our APIs more reliable, implicitly |
| 3312 | // enable it. |
Argyrios Kyrtzidis | 3701f54 | 2013-11-27 08:58:09 +0000 | [diff] [blame] | 3313 | if (!getenv("LIBCLANG_DISABLE_CRASH_RECOVERY")) |
| 3314 | llvm::CrashRecoveryContext::Enable(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3315 | |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 3316 | // Look through the managed static to trigger construction of the managed |
| 3317 | // static which registers our fatal error handler. This ensures it is only |
| 3318 | // registered once. |
| 3319 | (void)*RegisterFatalErrorHandlerOnce; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3320 | |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 3321 | // Initialize targets for clang module support. |
| 3322 | llvm::InitializeAllTargets(); |
| 3323 | llvm::InitializeAllTargetMCs(); |
| 3324 | llvm::InitializeAllAsmPrinters(); |
| 3325 | llvm::InitializeAllAsmParsers(); |
| 3326 | |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 3327 | CIndexer *CIdxr = new CIndexer(); |
| 3328 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3329 | if (excludeDeclarationsFromPCH) |
| 3330 | CIdxr->setOnlyLocalDecls(); |
| 3331 | if (displayDiagnostics) |
| 3332 | CIdxr->setDisplayDiagnostics(); |
| 3333 | |
| 3334 | if (getenv("LIBCLANG_BGPRIO_INDEX")) |
| 3335 | CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() | |
| 3336 | CXGlobalOpt_ThreadBackgroundPriorityForIndexing); |
| 3337 | if (getenv("LIBCLANG_BGPRIO_EDIT")) |
| 3338 | CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() | |
| 3339 | CXGlobalOpt_ThreadBackgroundPriorityForEditing); |
| 3340 | |
| 3341 | return CIdxr; |
| 3342 | } |
| 3343 | |
| 3344 | void clang_disposeIndex(CXIndex CIdx) { |
| 3345 | if (CIdx) |
| 3346 | delete static_cast<CIndexer *>(CIdx); |
| 3347 | } |
| 3348 | |
| 3349 | void clang_CXIndex_setGlobalOptions(CXIndex CIdx, unsigned options) { |
| 3350 | if (CIdx) |
| 3351 | static_cast<CIndexer *>(CIdx)->setCXGlobalOptFlags(options); |
| 3352 | } |
| 3353 | |
| 3354 | unsigned clang_CXIndex_getGlobalOptions(CXIndex CIdx) { |
| 3355 | if (CIdx) |
| 3356 | return static_cast<CIndexer *>(CIdx)->getCXGlobalOptFlags(); |
| 3357 | return 0; |
| 3358 | } |
| 3359 | |
Alex Lorenz | 0861579 | 2017-12-04 21:56:36 +0000 | [diff] [blame] | 3360 | void clang_CXIndex_setInvocationEmissionPathOption(CXIndex CIdx, |
| 3361 | const char *Path) { |
| 3362 | if (CIdx) |
| 3363 | static_cast<CIndexer *>(CIdx)->setInvocationEmissionPath(Path ? Path : ""); |
| 3364 | } |
| 3365 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3366 | void clang_toggleCrashRecovery(unsigned isEnabled) { |
| 3367 | if (isEnabled) |
| 3368 | llvm::CrashRecoveryContext::Enable(); |
| 3369 | else |
| 3370 | llvm::CrashRecoveryContext::Disable(); |
| 3371 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3372 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3373 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
| 3374 | const char *ast_filename) { |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3375 | CXTranslationUnit TU; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3376 | enum CXErrorCode Result = |
| 3377 | clang_createTranslationUnit2(CIdx, ast_filename, &TU); |
Reid Kleckner | fd48fc6 | 2014-02-12 23:56:20 +0000 | [diff] [blame] | 3378 | (void)Result; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3379 | assert((TU && Result == CXError_Success) || |
| 3380 | (!TU && Result != CXError_Success)); |
| 3381 | return TU; |
| 3382 | } |
| 3383 | |
| 3384 | enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx, |
| 3385 | const char *ast_filename, |
| 3386 | CXTranslationUnit *out_TU) { |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3387 | if (out_TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3388 | *out_TU = nullptr; |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3389 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3390 | if (!CIdx || !ast_filename || !out_TU) |
| 3391 | return CXError_InvalidArguments; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3392 | |
Argyrios Kyrtzidis | 2702101 | 2013-05-24 22:24:07 +0000 | [diff] [blame] | 3393 | LOG_FUNC_SECTION { |
| 3394 | *Log << ast_filename; |
| 3395 | } |
| 3396 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3397 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 3398 | FileSystemOptions FileSystemOpts; |
| 3399 | |
Justin Bogner | d512c1e | 2014-10-15 00:33:06 +0000 | [diff] [blame] | 3400 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags = |
| 3401 | CompilerInstance::createDiagnostics(new DiagnosticOptions()); |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 3402 | std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile( |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 3403 | ast_filename, CXXIdx->getPCHContainerOperations()->getRawReader(), |
| 3404 | ASTUnit::LoadEverything, Diags, |
Adrian Prantl | 6b21ab2 | 2015-08-27 19:46:20 +0000 | [diff] [blame] | 3405 | FileSystemOpts, /*UseDebugInfo=*/false, |
| 3406 | CXXIdx->getOnlyLocalDecls(), None, |
Nikolai Kosjar | 8edd8da | 2019-06-11 14:14:24 +0000 | [diff] [blame] | 3407 | CaptureDiagsKind::All, |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 3408 | /*AllowPCHWithCompilerErrors=*/true, |
| 3409 | /*UserFilesAreVolatile=*/true); |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 3410 | *out_TU = MakeCXTranslationUnit(CXXIdx, std::move(AU)); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3411 | return *out_TU ? CXError_Success : CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | unsigned clang_defaultEditingTranslationUnitOptions() { |
| 3415 | return CXTranslationUnit_PrecompiledPreamble | |
| 3416 | CXTranslationUnit_CacheCompletionResults; |
| 3417 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3418 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3419 | CXTranslationUnit |
| 3420 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 3421 | const char *source_filename, |
| 3422 | int num_command_line_args, |
| 3423 | const char * const *command_line_args, |
| 3424 | unsigned num_unsaved_files, |
| 3425 | struct CXUnsavedFile *unsaved_files) { |
| 3426 | unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord; |
| 3427 | return clang_parseTranslationUnit(CIdx, source_filename, |
| 3428 | command_line_args, num_command_line_args, |
| 3429 | unsaved_files, num_unsaved_files, |
| 3430 | Options); |
| 3431 | } |
| 3432 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3433 | static CXErrorCode |
| 3434 | clang_parseTranslationUnit_Impl(CXIndex CIdx, const char *source_filename, |
| 3435 | const char *const *command_line_args, |
| 3436 | int num_command_line_args, |
| 3437 | ArrayRef<CXUnsavedFile> unsaved_files, |
| 3438 | unsigned options, CXTranslationUnit *out_TU) { |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3439 | // Set up the initial return values. |
| 3440 | if (out_TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3441 | *out_TU = nullptr; |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3442 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3443 | // Check arguments. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3444 | if (!CIdx || !out_TU) |
| 3445 | return CXError_InvalidArguments; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3446 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3447 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 3448 | |
| 3449 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
| 3450 | setThreadBackgroundPriority(); |
| 3451 | |
| 3452 | bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3453 | bool CreatePreambleOnFirstParse = |
| 3454 | options & CXTranslationUnit_CreatePreambleOnFirstParse; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3455 | // FIXME: Add a flag for modules. |
| 3456 | TranslationUnitKind TUKind |
Argyrios Kyrtzidis | 735e92c | 2017-06-09 01:20:48 +0000 | [diff] [blame] | 3457 | = (options & (CXTranslationUnit_Incomplete | |
| 3458 | CXTranslationUnit_SingleFileParse))? TU_Prefix : TU_Complete; |
Alp Toker | 8c8a875 | 2013-12-03 06:53:35 +0000 | [diff] [blame] | 3459 | bool CacheCodeCompletionResults |
Ivan Donchevskii | f70d28b | 2018-05-17 09:15:22 +0000 | [diff] [blame] | 3460 | = options & CXTranslationUnit_CacheCompletionResults; |
| 3461 | bool IncludeBriefCommentsInCodeCompletion |
| 3462 | = options & CXTranslationUnit_IncludeBriefCommentsInCodeCompletion; |
Ivan Donchevskii | f70d28b | 2018-05-17 09:15:22 +0000 | [diff] [blame] | 3463 | bool SingleFileParse = options & CXTranslationUnit_SingleFileParse; |
| 3464 | bool ForSerialization = options & CXTranslationUnit_ForSerialization; |
Evgeny Mankov | 2ed2e62 | 2019-08-27 22:15:32 +0000 | [diff] [blame] | 3465 | bool RetainExcludedCB = options & |
| 3466 | CXTranslationUnit_RetainExcludedConditionalBlocks; |
Ivan Donchevskii | 6e89528 | 2018-05-17 09:24:37 +0000 | [diff] [blame] | 3467 | SkipFunctionBodiesScope SkipFunctionBodies = SkipFunctionBodiesScope::None; |
| 3468 | if (options & CXTranslationUnit_SkipFunctionBodies) { |
| 3469 | SkipFunctionBodies = |
| 3470 | (options & CXTranslationUnit_LimitSkipFunctionBodiesToPreamble) |
| 3471 | ? SkipFunctionBodiesScope::Preamble |
| 3472 | : SkipFunctionBodiesScope::PreambleAndMainFile; |
| 3473 | } |
Ivan Donchevskii | f70d28b | 2018-05-17 09:15:22 +0000 | [diff] [blame] | 3474 | |
| 3475 | // Configure the diagnostics. |
| 3476 | IntrusiveRefCntPtr<DiagnosticsEngine> |
Sean Silva | f1b49e2 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 3477 | Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3478 | |
Manuel Klimek | 016c024 | 2016-03-01 10:56:19 +0000 | [diff] [blame] | 3479 | if (options & CXTranslationUnit_KeepGoing) |
Ivan Donchevskii | 878271b | 2019-03-07 10:13:50 +0000 | [diff] [blame] | 3480 | Diags->setFatalsAsError(true); |
Manuel Klimek | 016c024 | 2016-03-01 10:56:19 +0000 | [diff] [blame] | 3481 | |
Nikolai Kosjar | 8edd8da | 2019-06-11 14:14:24 +0000 | [diff] [blame] | 3482 | CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::All; |
| 3483 | if (options & CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles) |
| 3484 | CaptureDiagnostics = CaptureDiagsKind::AllWithoutNonErrorsFromIncludes; |
| 3485 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3486 | // Recover resources if we crash before exiting this function. |
| 3487 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 3488 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 3489 | DiagCleanup(Diags.get()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3490 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3491 | std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles( |
| 3492 | new std::vector<ASTUnit::RemappedFile>()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3493 | |
| 3494 | // Recover resources if we crash before exiting this function. |
| 3495 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 3496 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 3497 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3498 | for (auto &UF : unsaved_files) { |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3499 | std::unique_ptr<llvm::MemoryBuffer> MB = |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3500 | llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3501 | RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3504 | std::unique_ptr<std::vector<const char *>> Args( |
| 3505 | new std::vector<const char *>()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3506 | |
| 3507 | // Recover resources if we crash before exiting this method. |
| 3508 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > |
| 3509 | ArgsCleanup(Args.get()); |
| 3510 | |
| 3511 | // Since the Clang C library is primarily used by batch tools dealing with |
| 3512 | // (often very broken) source code, where spell-checking can have a |
| 3513 | // significant negative impact on performance (particularly when |
| 3514 | // precompiled headers are involved), we disable it by default. |
| 3515 | // Only do this if we haven't found a spell-checking-related argument. |
| 3516 | bool FoundSpellCheckingArgument = false; |
| 3517 | for (int I = 0; I != num_command_line_args; ++I) { |
| 3518 | if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || |
| 3519 | strcmp(command_line_args[I], "-fspell-checking") == 0) { |
| 3520 | FoundSpellCheckingArgument = true; |
| 3521 | break; |
| 3522 | } |
| 3523 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3524 | Args->insert(Args->end(), command_line_args, |
| 3525 | command_line_args + num_command_line_args); |
| 3526 | |
Benjamin Kramer | c02670e | 2015-11-18 16:14:27 +0000 | [diff] [blame] | 3527 | if (!FoundSpellCheckingArgument) |
| 3528 | Args->insert(Args->begin() + 1, "-fno-spell-checking"); |
| 3529 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3530 | // The 'source_filename' argument is optional. If the caller does not |
| 3531 | // specify it then it is assumed that the source file is specified |
| 3532 | // in the actual argument list. |
| 3533 | // Put the source file after command_line_args otherwise if '-x' flag is |
| 3534 | // present it will be unused. |
| 3535 | if (source_filename) |
| 3536 | Args->push_back(source_filename); |
| 3537 | |
| 3538 | // Do we need the detailed preprocessing record? |
| 3539 | if (options & CXTranslationUnit_DetailedPreprocessingRecord) { |
| 3540 | Args->push_back("-Xclang"); |
| 3541 | Args->push_back("-detailed-preprocessing-record"); |
| 3542 | } |
Alex Lorenz | cb00640 | 2017-04-27 13:47:03 +0000 | [diff] [blame] | 3543 | |
| 3544 | // Suppress any editor placeholder diagnostics. |
| 3545 | Args->push_back("-fallow-editor-placeholders"); |
| 3546 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3547 | unsigned NumErrors = Diags->getClient()->getNumErrors(); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3548 | std::unique_ptr<ASTUnit> ErrUnit; |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3549 | // Unless the user specified that they want the preamble on the first parse |
| 3550 | // set it up to be created on the first reparse. This makes the first parse |
| 3551 | // faster, trading for a slower (first) reparse. |
| 3552 | unsigned PrecompilePreambleAfterNParses = |
| 3553 | !PrecompilePreamble ? 0 : 2 - CreatePreambleOnFirstParse; |
Alex Lorenz | 0861579 | 2017-12-04 21:56:36 +0000 | [diff] [blame] | 3554 | |
Alex Lorenz | 0861579 | 2017-12-04 21:56:36 +0000 | [diff] [blame] | 3555 | LibclangInvocationReporter InvocationReporter( |
| 3556 | *CXXIdx, LibclangInvocationReporter::OperationKind::ParseOperation, |
Alex Lorenz | 690f0e2 | 2017-12-07 20:37:50 +0000 | [diff] [blame] | 3557 | options, llvm::makeArrayRef(*Args), /*InvocationArgs=*/None, |
| 3558 | unsaved_files); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3559 | std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCommandLine( |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 3560 | Args->data(), Args->data() + Args->size(), |
| 3561 | CXXIdx->getPCHContainerOperations(), Diags, |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3562 | CXXIdx->getClangResourcesPath(), CXXIdx->getOnlyLocalDecls(), |
Nikolai Kosjar | 8edd8da | 2019-06-11 14:14:24 +0000 | [diff] [blame] | 3563 | CaptureDiagnostics, *RemappedFiles.get(), |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3564 | /*RemappedFilesKeepOriginalName=*/true, PrecompilePreambleAfterNParses, |
| 3565 | TUKind, CacheCodeCompletionResults, IncludeBriefCommentsInCodeCompletion, |
Argyrios Kyrtzidis | 735e92c | 2017-06-09 01:20:48 +0000 | [diff] [blame] | 3566 | /*AllowPCHWithCompilerErrors=*/true, SkipFunctionBodies, SingleFileParse, |
Evgeny Mankov | 2ed2e62 | 2019-08-27 22:15:32 +0000 | [diff] [blame] | 3567 | /*UserFilesAreVolatile=*/true, ForSerialization, RetainExcludedCB, |
Argyrios Kyrtzidis | a3e2ff1 | 2015-11-20 03:36:21 +0000 | [diff] [blame] | 3568 | CXXIdx->getPCHContainerOperations()->getRawReader().getFormat(), |
| 3569 | &ErrUnit)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3570 | |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3571 | // Early failures in LoadFromCommandLine may return with ErrUnit unset. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3572 | if (!Unit && !ErrUnit) |
| 3573 | return CXError_ASTReadError; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3574 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3575 | if (NumErrors != Diags->getClient()->getNumErrors()) { |
| 3576 | // Make sure to check that 'Unit' is non-NULL. |
| 3577 | if (CXXIdx->getDisplayDiagnostics()) |
| 3578 | printDiagsToStderr(Unit ? Unit.get() : ErrUnit.get()); |
| 3579 | } |
| 3580 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3581 | if (isASTReadError(Unit ? Unit.get() : ErrUnit.get())) |
| 3582 | return CXError_ASTReadError; |
| 3583 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 3584 | *out_TU = MakeCXTranslationUnit(CXXIdx, std::move(Unit)); |
Alex Lorenz | 690f0e2 | 2017-12-07 20:37:50 +0000 | [diff] [blame] | 3585 | if (CXTranslationUnitImpl *TU = *out_TU) { |
| 3586 | TU->ParsingOptions = options; |
| 3587 | TU->Arguments.reserve(Args->size()); |
| 3588 | for (const char *Arg : *Args) |
| 3589 | TU->Arguments.push_back(Arg); |
| 3590 | return CXError_Success; |
| 3591 | } |
| 3592 | return CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3593 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3594 | |
| 3595 | CXTranslationUnit |
| 3596 | clang_parseTranslationUnit(CXIndex CIdx, |
| 3597 | const char *source_filename, |
| 3598 | const char *const *command_line_args, |
| 3599 | int num_command_line_args, |
| 3600 | struct CXUnsavedFile *unsaved_files, |
| 3601 | unsigned num_unsaved_files, |
| 3602 | unsigned options) { |
| 3603 | CXTranslationUnit TU; |
| 3604 | enum CXErrorCode Result = clang_parseTranslationUnit2( |
| 3605 | CIdx, source_filename, command_line_args, num_command_line_args, |
| 3606 | unsaved_files, num_unsaved_files, options, &TU); |
Reid Kleckner | 6eaf05a | 2014-02-13 01:19:59 +0000 | [diff] [blame] | 3607 | (void)Result; |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3608 | assert((TU && Result == CXError_Success) || |
| 3609 | (!TU && Result != CXError_Success)); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3610 | return TU; |
| 3611 | } |
| 3612 | |
| 3613 | enum CXErrorCode clang_parseTranslationUnit2( |
Benjamin Kramer | c02670e | 2015-11-18 16:14:27 +0000 | [diff] [blame] | 3614 | CXIndex CIdx, const char *source_filename, |
| 3615 | const char *const *command_line_args, int num_command_line_args, |
| 3616 | struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files, |
| 3617 | unsigned options, CXTranslationUnit *out_TU) { |
Alexandre Ganea | 471d060 | 2019-11-29 10:52:13 -0500 | [diff] [blame] | 3618 | noteBottomOfStack(); |
Benjamin Kramer | c02670e | 2015-11-18 16:14:27 +0000 | [diff] [blame] | 3619 | SmallVector<const char *, 4> Args; |
| 3620 | Args.push_back("clang"); |
| 3621 | Args.append(command_line_args, command_line_args + num_command_line_args); |
| 3622 | return clang_parseTranslationUnit2FullArgv( |
| 3623 | CIdx, source_filename, Args.data(), Args.size(), unsaved_files, |
| 3624 | num_unsaved_files, options, out_TU); |
| 3625 | } |
| 3626 | |
| 3627 | enum CXErrorCode clang_parseTranslationUnit2FullArgv( |
| 3628 | CXIndex CIdx, const char *source_filename, |
| 3629 | const char *const *command_line_args, int num_command_line_args, |
| 3630 | struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files, |
| 3631 | unsigned options, CXTranslationUnit *out_TU) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 3632 | LOG_FUNC_SECTION { |
| 3633 | *Log << source_filename << ": "; |
| 3634 | for (int i = 0; i != num_command_line_args; ++i) |
| 3635 | *Log << command_line_args[i] << " "; |
| 3636 | } |
| 3637 | |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3638 | if (num_unsaved_files && !unsaved_files) |
| 3639 | return CXError_InvalidArguments; |
| 3640 | |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 3641 | CXErrorCode result = CXError_Failure; |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3642 | auto ParseTranslationUnitImpl = [=, &result] { |
Alexandre Ganea | 471d060 | 2019-11-29 10:52:13 -0500 | [diff] [blame] | 3643 | noteBottomOfStack(); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3644 | result = clang_parseTranslationUnit_Impl( |
| 3645 | CIdx, source_filename, command_line_args, num_command_line_args, |
| 3646 | llvm::makeArrayRef(unsaved_files, num_unsaved_files), options, out_TU); |
| 3647 | }; |
Erik Verbruggen | 284848d | 2017-08-29 09:08:02 +0000 | [diff] [blame] | 3648 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3649 | llvm::CrashRecoveryContext CRC; |
| 3650 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3651 | if (!RunSafely(CRC, ParseTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3652 | fprintf(stderr, "libclang: crash detected during parsing: {\n"); |
| 3653 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 3654 | fprintf(stderr, " 'command_line_args' : ["); |
| 3655 | for (int i = 0; i != num_command_line_args; ++i) { |
| 3656 | if (i) |
| 3657 | fprintf(stderr, ", "); |
| 3658 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 3659 | } |
| 3660 | fprintf(stderr, "],\n"); |
| 3661 | fprintf(stderr, " 'unsaved_files' : ["); |
| 3662 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 3663 | if (i) |
| 3664 | fprintf(stderr, ", "); |
| 3665 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 3666 | unsaved_files[i].Length); |
| 3667 | } |
| 3668 | fprintf(stderr, "],\n"); |
| 3669 | fprintf(stderr, " 'options' : %d,\n", options); |
| 3670 | fprintf(stderr, "}\n"); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3671 | |
| 3672 | return CXError_Crashed; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3673 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3674 | if (CXTranslationUnit *TU = out_TU) |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3675 | PrintLibclangResourceUsage(*TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3676 | } |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 3677 | |
| 3678 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3679 | } |
| 3680 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3681 | CXString clang_Type_getObjCEncoding(CXType CT) { |
| 3682 | CXTranslationUnit tu = static_cast<CXTranslationUnit>(CT.data[1]); |
| 3683 | ASTContext &Ctx = getASTUnit(tu)->getASTContext(); |
| 3684 | std::string encoding; |
| 3685 | Ctx.getObjCEncodingForType(QualType::getFromOpaquePtr(CT.data[0]), |
| 3686 | encoding); |
| 3687 | |
| 3688 | return cxstring::createDup(encoding); |
| 3689 | } |
| 3690 | |
| 3691 | static const IdentifierInfo *getMacroIdentifier(CXCursor C) { |
| 3692 | if (C.kind == CXCursor_MacroDefinition) { |
| 3693 | if (const MacroDefinitionRecord *MDR = getCursorMacroDefinition(C)) |
| 3694 | return MDR->getName(); |
| 3695 | } else if (C.kind == CXCursor_MacroExpansion) { |
| 3696 | MacroExpansionCursor ME = getCursorMacroExpansion(C); |
| 3697 | return ME.getName(); |
| 3698 | } |
| 3699 | return nullptr; |
| 3700 | } |
| 3701 | |
| 3702 | unsigned clang_Cursor_isMacroFunctionLike(CXCursor C) { |
| 3703 | const IdentifierInfo *II = getMacroIdentifier(C); |
| 3704 | if (!II) { |
| 3705 | return false; |
| 3706 | } |
| 3707 | ASTUnit *ASTU = getCursorASTUnit(C); |
| 3708 | Preprocessor &PP = ASTU->getPreprocessor(); |
| 3709 | if (const MacroInfo *MI = PP.getMacroInfo(II)) |
| 3710 | return MI->isFunctionLike(); |
| 3711 | return false; |
| 3712 | } |
| 3713 | |
| 3714 | unsigned clang_Cursor_isMacroBuiltin(CXCursor C) { |
| 3715 | const IdentifierInfo *II = getMacroIdentifier(C); |
| 3716 | if (!II) { |
| 3717 | return false; |
| 3718 | } |
| 3719 | ASTUnit *ASTU = getCursorASTUnit(C); |
| 3720 | Preprocessor &PP = ASTU->getPreprocessor(); |
| 3721 | if (const MacroInfo *MI = PP.getMacroInfo(II)) |
| 3722 | return MI->isBuiltinMacro(); |
| 3723 | return false; |
| 3724 | } |
| 3725 | |
| 3726 | unsigned clang_Cursor_isFunctionInlined(CXCursor C) { |
| 3727 | const Decl *D = getCursorDecl(C); |
| 3728 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 3729 | if (!FD) { |
| 3730 | return false; |
| 3731 | } |
| 3732 | return FD->isInlined(); |
| 3733 | } |
| 3734 | |
| 3735 | static StringLiteral* getCFSTR_value(CallExpr *callExpr) { |
| 3736 | if (callExpr->getNumArgs() != 1) { |
| 3737 | return nullptr; |
| 3738 | } |
| 3739 | |
| 3740 | StringLiteral *S = nullptr; |
| 3741 | auto *arg = callExpr->getArg(0); |
| 3742 | if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) { |
| 3743 | ImplicitCastExpr *I = static_cast<ImplicitCastExpr *>(arg); |
| 3744 | auto *subExpr = I->getSubExprAsWritten(); |
| 3745 | |
| 3746 | if(subExpr->getStmtClass() != Stmt::StringLiteralClass){ |
| 3747 | return nullptr; |
| 3748 | } |
| 3749 | |
| 3750 | S = static_cast<StringLiteral *>(I->getSubExprAsWritten()); |
| 3751 | } else if (arg->getStmtClass() == Stmt::StringLiteralClass) { |
| 3752 | S = static_cast<StringLiteral *>(callExpr->getArg(0)); |
| 3753 | } else { |
| 3754 | return nullptr; |
| 3755 | } |
| 3756 | return S; |
| 3757 | } |
| 3758 | |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3759 | struct ExprEvalResult { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3760 | CXEvalResultKind EvalType; |
| 3761 | union { |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3762 | unsigned long long unsignedVal; |
| 3763 | long long intVal; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3764 | double floatVal; |
| 3765 | char *stringVal; |
| 3766 | } EvalData; |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3767 | bool IsUnsignedInt; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3768 | ~ExprEvalResult() { |
| 3769 | if (EvalType != CXEval_UnExposed && EvalType != CXEval_Float && |
| 3770 | EvalType != CXEval_Int) { |
Alex Lorenz | a19cb2e | 2019-01-08 23:28:37 +0000 | [diff] [blame] | 3771 | delete[] EvalData.stringVal; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3772 | } |
| 3773 | } |
| 3774 | }; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3775 | |
| 3776 | void clang_EvalResult_dispose(CXEvalResult E) { |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3777 | delete static_cast<ExprEvalResult *>(E); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
| 3780 | CXEvalResultKind clang_EvalResult_getKind(CXEvalResult E) { |
| 3781 | if (!E) { |
| 3782 | return CXEval_UnExposed; |
| 3783 | } |
| 3784 | return ((ExprEvalResult *)E)->EvalType; |
| 3785 | } |
| 3786 | |
| 3787 | int clang_EvalResult_getAsInt(CXEvalResult E) { |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3788 | return clang_EvalResult_getAsLongLong(E); |
| 3789 | } |
| 3790 | |
| 3791 | long long clang_EvalResult_getAsLongLong(CXEvalResult E) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3792 | if (!E) { |
| 3793 | return 0; |
| 3794 | } |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3795 | ExprEvalResult *Result = (ExprEvalResult*)E; |
| 3796 | if (Result->IsUnsignedInt) |
| 3797 | return Result->EvalData.unsignedVal; |
| 3798 | return Result->EvalData.intVal; |
| 3799 | } |
| 3800 | |
| 3801 | unsigned clang_EvalResult_isUnsignedInt(CXEvalResult E) { |
| 3802 | return ((ExprEvalResult *)E)->IsUnsignedInt; |
| 3803 | } |
| 3804 | |
| 3805 | unsigned long long clang_EvalResult_getAsUnsigned(CXEvalResult E) { |
| 3806 | if (!E) { |
| 3807 | return 0; |
| 3808 | } |
| 3809 | |
| 3810 | ExprEvalResult *Result = (ExprEvalResult*)E; |
| 3811 | if (Result->IsUnsignedInt) |
| 3812 | return Result->EvalData.unsignedVal; |
| 3813 | return Result->EvalData.intVal; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3814 | } |
| 3815 | |
| 3816 | double clang_EvalResult_getAsDouble(CXEvalResult E) { |
| 3817 | if (!E) { |
| 3818 | return 0; |
| 3819 | } |
| 3820 | return ((ExprEvalResult *)E)->EvalData.floatVal; |
| 3821 | } |
| 3822 | |
| 3823 | const char* clang_EvalResult_getAsStr(CXEvalResult E) { |
| 3824 | if (!E) { |
| 3825 | return nullptr; |
| 3826 | } |
| 3827 | return ((ExprEvalResult *)E)->EvalData.stringVal; |
| 3828 | } |
| 3829 | |
| 3830 | static const ExprEvalResult* evaluateExpr(Expr *expr, CXCursor C) { |
| 3831 | Expr::EvalResult ER; |
| 3832 | ASTContext &ctx = getCursorContext(C); |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3833 | if (!expr) |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3834 | return nullptr; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3835 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3836 | expr = expr->IgnoreParens(); |
Emilio Cobos Alvarez | 7437545 | 2019-07-09 14:27:01 +0000 | [diff] [blame] | 3837 | if (expr->isValueDependent()) |
| 3838 | return nullptr; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3839 | if (!expr->EvaluateAsRValue(ER, ctx)) |
| 3840 | return nullptr; |
| 3841 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3842 | QualType rettype; |
| 3843 | CallExpr *callExpr; |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 3844 | auto result = std::make_unique<ExprEvalResult>(); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3845 | result->EvalType = CXEval_UnExposed; |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3846 | result->IsUnsignedInt = false; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3847 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3848 | if (ER.Val.isInt()) { |
| 3849 | result->EvalType = CXEval_Int; |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3850 | |
| 3851 | auto& val = ER.Val.getInt(); |
| 3852 | if (val.isUnsigned()) { |
| 3853 | result->IsUnsignedInt = true; |
| 3854 | result->EvalData.unsignedVal = val.getZExtValue(); |
| 3855 | } else { |
| 3856 | result->EvalData.intVal = val.getExtValue(); |
| 3857 | } |
| 3858 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3859 | return result.release(); |
| 3860 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3861 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3862 | if (ER.Val.isFloat()) { |
| 3863 | llvm::SmallVector<char, 100> Buffer; |
| 3864 | ER.Val.getFloat().toString(Buffer); |
| 3865 | std::string floatStr(Buffer.data(), Buffer.size()); |
| 3866 | result->EvalType = CXEval_Float; |
| 3867 | bool ignored; |
| 3868 | llvm::APFloat apFloat = ER.Val.getFloat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3869 | apFloat.convert(llvm::APFloat::IEEEdouble(), |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3870 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 3871 | result->EvalData.floatVal = apFloat.convertToDouble(); |
| 3872 | return result.release(); |
| 3873 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3874 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3875 | if (expr->getStmtClass() == Stmt::ImplicitCastExprClass) { |
| 3876 | const ImplicitCastExpr *I = dyn_cast<ImplicitCastExpr>(expr); |
| 3877 | auto *subExpr = I->getSubExprAsWritten(); |
| 3878 | if (subExpr->getStmtClass() == Stmt::StringLiteralClass || |
| 3879 | subExpr->getStmtClass() == Stmt::ObjCStringLiteralClass) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3880 | const StringLiteral *StrE = nullptr; |
| 3881 | const ObjCStringLiteral *ObjCExpr; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3882 | ObjCExpr = dyn_cast<ObjCStringLiteral>(subExpr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3883 | |
| 3884 | if (ObjCExpr) { |
| 3885 | StrE = ObjCExpr->getString(); |
| 3886 | result->EvalType = CXEval_ObjCStrLiteral; |
| 3887 | } else { |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3888 | StrE = cast<StringLiteral>(I->getSubExprAsWritten()); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3889 | result->EvalType = CXEval_StrLiteral; |
| 3890 | } |
| 3891 | |
| 3892 | std::string strRef(StrE->getString().str()); |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3893 | result->EvalData.stringVal = new char[strRef.size() + 1]; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3894 | strncpy((char *)result->EvalData.stringVal, strRef.c_str(), |
| 3895 | strRef.size()); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3896 | result->EvalData.stringVal[strRef.size()] = '\0'; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3897 | return result.release(); |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3898 | } |
| 3899 | } else if (expr->getStmtClass() == Stmt::ObjCStringLiteralClass || |
| 3900 | expr->getStmtClass() == Stmt::StringLiteralClass) { |
| 3901 | const StringLiteral *StrE = nullptr; |
| 3902 | const ObjCStringLiteral *ObjCExpr; |
| 3903 | ObjCExpr = dyn_cast<ObjCStringLiteral>(expr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3904 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3905 | if (ObjCExpr) { |
| 3906 | StrE = ObjCExpr->getString(); |
| 3907 | result->EvalType = CXEval_ObjCStrLiteral; |
| 3908 | } else { |
| 3909 | StrE = cast<StringLiteral>(expr); |
| 3910 | result->EvalType = CXEval_StrLiteral; |
| 3911 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3912 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3913 | std::string strRef(StrE->getString().str()); |
| 3914 | result->EvalData.stringVal = new char[strRef.size() + 1]; |
| 3915 | strncpy((char *)result->EvalData.stringVal, strRef.c_str(), strRef.size()); |
| 3916 | result->EvalData.stringVal[strRef.size()] = '\0'; |
| 3917 | return result.release(); |
| 3918 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3919 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3920 | if (expr->getStmtClass() == Stmt::CStyleCastExprClass) { |
| 3921 | CStyleCastExpr *CC = static_cast<CStyleCastExpr *>(expr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3922 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3923 | rettype = CC->getType(); |
| 3924 | if (rettype.getAsString() == "CFStringRef" && |
| 3925 | CC->getSubExpr()->getStmtClass() == Stmt::CallExprClass) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3926 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3927 | callExpr = static_cast<CallExpr *>(CC->getSubExpr()); |
| 3928 | StringLiteral *S = getCFSTR_value(callExpr); |
| 3929 | if (S) { |
| 3930 | std::string strLiteral(S->getString().str()); |
| 3931 | result->EvalType = CXEval_CFStr; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3932 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3933 | result->EvalData.stringVal = new char[strLiteral.size() + 1]; |
| 3934 | strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(), |
| 3935 | strLiteral.size()); |
| 3936 | result->EvalData.stringVal[strLiteral.size()] = '\0'; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3937 | return result.release(); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3938 | } |
| 3939 | } |
| 3940 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3941 | } else if (expr->getStmtClass() == Stmt::CallExprClass) { |
| 3942 | callExpr = static_cast<CallExpr *>(expr); |
| 3943 | rettype = callExpr->getCallReturnType(ctx); |
| 3944 | |
| 3945 | if (rettype->isVectorType() || callExpr->getNumArgs() > 1) |
| 3946 | return nullptr; |
| 3947 | |
| 3948 | if (rettype->isIntegralType(ctx) || rettype->isRealFloatingType()) { |
| 3949 | if (callExpr->getNumArgs() == 1 && |
| 3950 | !callExpr->getArg(0)->getType()->isIntegralType(ctx)) |
| 3951 | return nullptr; |
| 3952 | } else if (rettype.getAsString() == "CFStringRef") { |
| 3953 | |
| 3954 | StringLiteral *S = getCFSTR_value(callExpr); |
| 3955 | if (S) { |
| 3956 | std::string strLiteral(S->getString().str()); |
| 3957 | result->EvalType = CXEval_CFStr; |
| 3958 | result->EvalData.stringVal = new char[strLiteral.size() + 1]; |
| 3959 | strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(), |
| 3960 | strLiteral.size()); |
| 3961 | result->EvalData.stringVal[strLiteral.size()] = '\0'; |
| 3962 | return result.release(); |
| 3963 | } |
| 3964 | } |
| 3965 | } else if (expr->getStmtClass() == Stmt::DeclRefExprClass) { |
| 3966 | DeclRefExpr *D = static_cast<DeclRefExpr *>(expr); |
| 3967 | ValueDecl *V = D->getDecl(); |
| 3968 | if (V->getKind() == Decl::Function) { |
| 3969 | std::string strName = V->getNameAsString(); |
| 3970 | result->EvalType = CXEval_Other; |
| 3971 | result->EvalData.stringVal = new char[strName.size() + 1]; |
| 3972 | strncpy(result->EvalData.stringVal, strName.c_str(), strName.size()); |
| 3973 | result->EvalData.stringVal[strName.size()] = '\0'; |
| 3974 | return result.release(); |
| 3975 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3978 | return nullptr; |
| 3979 | } |
| 3980 | |
Alex Lorenz | 65317e1 | 2019-01-08 22:32:51 +0000 | [diff] [blame] | 3981 | static const Expr *evaluateDeclExpr(const Decl *D) { |
| 3982 | if (!D) |
Evgeniy Stepanov | 9b87149 | 2018-07-10 19:48:53 +0000 | [diff] [blame] | 3983 | return nullptr; |
Alex Lorenz | 65317e1 | 2019-01-08 22:32:51 +0000 | [diff] [blame] | 3984 | if (auto *Var = dyn_cast<VarDecl>(D)) |
| 3985 | return Var->getInit(); |
| 3986 | else if (auto *Field = dyn_cast<FieldDecl>(D)) |
| 3987 | return Field->getInClassInitializer(); |
| 3988 | return nullptr; |
| 3989 | } |
Evgeniy Stepanov | 6df47ce | 2018-07-10 19:49:07 +0000 | [diff] [blame] | 3990 | |
Alex Lorenz | 65317e1 | 2019-01-08 22:32:51 +0000 | [diff] [blame] | 3991 | static const Expr *evaluateCompoundStmtExpr(const CompoundStmt *CS) { |
| 3992 | assert(CS && "invalid compound statement"); |
| 3993 | for (auto *bodyIterator : CS->body()) { |
| 3994 | if (const auto *E = dyn_cast<Expr>(bodyIterator)) |
| 3995 | return E; |
Evgeniy Stepanov | 6df47ce | 2018-07-10 19:49:07 +0000 | [diff] [blame] | 3996 | } |
Alex Lorenz | c4cf96e | 2018-07-09 19:56:45 +0000 | [diff] [blame] | 3997 | return nullptr; |
| 3998 | } |
| 3999 | |
Alex Lorenz | 65317e1 | 2019-01-08 22:32:51 +0000 | [diff] [blame] | 4000 | CXEvalResult clang_Cursor_Evaluate(CXCursor C) { |
| 4001 | if (const Expr *E = |
| 4002 | clang_getCursorKind(C) == CXCursor_CompoundStmt |
| 4003 | ? evaluateCompoundStmtExpr(cast<CompoundStmt>(getCursorStmt(C))) |
| 4004 | : evaluateDeclExpr(getCursorDecl(C))) |
| 4005 | return const_cast<CXEvalResult>( |
| 4006 | reinterpret_cast<const void *>(evaluateExpr(const_cast<Expr *>(E), C))); |
| 4007 | return nullptr; |
| 4008 | } |
| 4009 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 4010 | unsigned clang_Cursor_hasAttrs(CXCursor C) { |
| 4011 | const Decl *D = getCursorDecl(C); |
| 4012 | if (!D) { |
| 4013 | return 0; |
| 4014 | } |
| 4015 | |
| 4016 | if (D->hasAttrs()) { |
| 4017 | return 1; |
| 4018 | } |
| 4019 | |
| 4020 | return 0; |
| 4021 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4022 | unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { |
| 4023 | return CXSaveTranslationUnit_None; |
| 4024 | } |
| 4025 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4026 | static CXSaveError clang_saveTranslationUnit_Impl(CXTranslationUnit TU, |
| 4027 | const char *FileName, |
| 4028 | unsigned options) { |
| 4029 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4030 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
| 4031 | setThreadBackgroundPriority(); |
| 4032 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4033 | bool hadError = cxtu::getASTUnit(TU)->Save(FileName); |
| 4034 | return hadError ? CXSaveError_Unknown : CXSaveError_None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
| 4037 | int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, |
| 4038 | unsigned options) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 4039 | LOG_FUNC_SECTION { |
| 4040 | *Log << TU << ' ' << FileName; |
| 4041 | } |
| 4042 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4043 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4044 | LOG_BAD_TU(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4045 | return CXSaveError_InvalidTU; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4046 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4047 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4048 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4049 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 4050 | if (!CXXUnit->hasSema()) |
| 4051 | return CXSaveError_InvalidTU; |
| 4052 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4053 | CXSaveError result; |
| 4054 | auto SaveTranslationUnitImpl = [=, &result]() { |
| 4055 | result = clang_saveTranslationUnit_Impl(TU, FileName, options); |
| 4056 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4057 | |
Erik Verbruggen | 3cc3911 | 2017-11-14 09:34:39 +0000 | [diff] [blame] | 4058 | if (!CXXUnit->getDiagnostics().hasUnrecoverableErrorOccurred()) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4059 | SaveTranslationUnitImpl(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4060 | |
| 4061 | if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 4062 | PrintLibclangResourceUsage(TU); |
| 4063 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4064 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4065 | } |
| 4066 | |
| 4067 | // We have an AST that has invalid nodes due to compiler errors. |
| 4068 | // Use a crash recovery thread for protection. |
| 4069 | |
| 4070 | llvm::CrashRecoveryContext CRC; |
| 4071 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4072 | if (!RunSafely(CRC, SaveTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4073 | fprintf(stderr, "libclang: crash detected during AST saving: {\n"); |
| 4074 | fprintf(stderr, " 'filename' : '%s'\n", FileName); |
| 4075 | fprintf(stderr, " 'options' : %d,\n", options); |
| 4076 | fprintf(stderr, "}\n"); |
| 4077 | |
| 4078 | return CXSaveError_Unknown; |
| 4079 | |
| 4080 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
| 4081 | PrintLibclangResourceUsage(TU); |
| 4082 | } |
| 4083 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4084 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4085 | } |
| 4086 | |
| 4087 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
| 4088 | if (CTUnit) { |
| 4089 | // If the translation unit has been marked as unsafe to free, just discard |
| 4090 | // it. |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 4091 | ASTUnit *Unit = cxtu::getASTUnit(CTUnit); |
| 4092 | if (Unit && Unit->isUnsafeToFree()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4093 | return; |
| 4094 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4095 | delete cxtu::getASTUnit(CTUnit); |
Dmitri Gribenko | b95b3f1 | 2013-01-26 22:44:19 +0000 | [diff] [blame] | 4096 | delete CTUnit->StringPool; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4097 | delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics); |
| 4098 | disposeOverridenCXCursorsPool(CTUnit->OverridenCursorsPool); |
Dmitri Gribenko | 9e60511 | 2013-11-13 22:16:51 +0000 | [diff] [blame] | 4099 | delete CTUnit->CommentToXML; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4100 | delete CTUnit; |
| 4101 | } |
| 4102 | } |
| 4103 | |
Erik Verbruggen | 346066b | 2017-05-30 14:25:54 +0000 | [diff] [blame] | 4104 | unsigned clang_suspendTranslationUnit(CXTranslationUnit CTUnit) { |
| 4105 | if (CTUnit) { |
| 4106 | ASTUnit *Unit = cxtu::getASTUnit(CTUnit); |
| 4107 | |
| 4108 | if (Unit && Unit->isUnsafeToFree()) |
| 4109 | return false; |
| 4110 | |
| 4111 | Unit->ResetForParse(); |
| 4112 | return true; |
| 4113 | } |
| 4114 | |
| 4115 | return false; |
| 4116 | } |
| 4117 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4118 | unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { |
| 4119 | return CXReparse_None; |
| 4120 | } |
| 4121 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4122 | static CXErrorCode |
| 4123 | clang_reparseTranslationUnit_Impl(CXTranslationUnit TU, |
| 4124 | ArrayRef<CXUnsavedFile> unsaved_files, |
| 4125 | unsigned options) { |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 4126 | // Check arguments. |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4127 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4128 | LOG_BAD_TU(TU); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4129 | return CXError_InvalidArguments; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 4130 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4131 | |
| 4132 | // Reset the associated diagnostics. |
| 4133 | delete static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 4134 | TU->Diagnostics = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4135 | |
Dmitri Gribenko | 183436e | 2013-01-26 21:49:50 +0000 | [diff] [blame] | 4136 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4137 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) |
| 4138 | setThreadBackgroundPriority(); |
| 4139 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4140 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4141 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 4142 | |
| 4143 | std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles( |
| 4144 | new std::vector<ASTUnit::RemappedFile>()); |
| 4145 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4146 | // Recover resources if we crash before exiting this function. |
| 4147 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 4148 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 4149 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4150 | for (auto &UF : unsaved_files) { |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 4151 | std::unique_ptr<llvm::MemoryBuffer> MB = |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 4152 | llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 4153 | RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4154 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 4155 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 4156 | if (!CXXUnit->Reparse(CXXIdx->getPCHContainerOperations(), |
| 4157 | *RemappedFiles.get())) |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4158 | return CXError_Success; |
| 4159 | if (isASTReadError(CXXUnit)) |
| 4160 | return CXError_ASTReadError; |
| 4161 | return CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
| 4164 | int clang_reparseTranslationUnit(CXTranslationUnit TU, |
| 4165 | unsigned num_unsaved_files, |
| 4166 | struct CXUnsavedFile *unsaved_files, |
| 4167 | unsigned options) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 4168 | LOG_FUNC_SECTION { |
| 4169 | *Log << TU; |
| 4170 | } |
| 4171 | |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 4172 | if (num_unsaved_files && !unsaved_files) |
| 4173 | return CXError_InvalidArguments; |
| 4174 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4175 | CXErrorCode result; |
| 4176 | auto ReparseTranslationUnitImpl = [=, &result]() { |
| 4177 | result = clang_reparseTranslationUnit_Impl( |
| 4178 | TU, llvm::makeArrayRef(unsaved_files, num_unsaved_files), options); |
| 4179 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4180 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4181 | llvm::CrashRecoveryContext CRC; |
| 4182 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4183 | if (!RunSafely(CRC, ReparseTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4184 | fprintf(stderr, "libclang: crash detected during reparsing\n"); |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4185 | cxtu::getASTUnit(TU)->setUnsafeToFree(true); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 4186 | return CXError_Crashed; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4187 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 4188 | PrintLibclangResourceUsage(TU); |
| 4189 | |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 4190 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | |
| 4194 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4195 | if (isNotUsableTU(CTUnit)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4196 | LOG_BAD_TU(CTUnit); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4197 | return cxstring::createEmpty(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4198 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4199 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4200 | ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4201 | return cxstring::createDup(CXXUnit->getOriginalSourceFileName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4202 | } |
| 4203 | |
| 4204 | CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4205 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4206 | LOG_BAD_TU(TU); |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 4207 | return clang_getNullCursor(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4208 | } |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 4209 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4210 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4211 | return MakeCXCursor(CXXUnit->getASTContext().getTranslationUnitDecl(), TU); |
| 4212 | } |
| 4213 | |
Emilio Cobos Alvarez | 485ad42 | 2017-04-28 15:56:39 +0000 | [diff] [blame] | 4214 | CXTargetInfo clang_getTranslationUnitTargetInfo(CXTranslationUnit CTUnit) { |
| 4215 | if (isNotUsableTU(CTUnit)) { |
| 4216 | LOG_BAD_TU(CTUnit); |
| 4217 | return nullptr; |
| 4218 | } |
| 4219 | |
| 4220 | CXTargetInfoImpl* impl = new CXTargetInfoImpl(); |
| 4221 | impl->TranslationUnit = CTUnit; |
| 4222 | return impl; |
| 4223 | } |
| 4224 | |
| 4225 | CXString clang_TargetInfo_getTriple(CXTargetInfo TargetInfo) { |
| 4226 | if (!TargetInfo) |
| 4227 | return cxstring::createEmpty(); |
| 4228 | |
| 4229 | CXTranslationUnit CTUnit = TargetInfo->TranslationUnit; |
| 4230 | assert(!isNotUsableTU(CTUnit) && |
| 4231 | "Unexpected unusable translation unit in TargetInfo"); |
| 4232 | |
| 4233 | ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); |
| 4234 | std::string Triple = |
| 4235 | CXXUnit->getASTContext().getTargetInfo().getTriple().normalize(); |
| 4236 | return cxstring::createDup(Triple); |
| 4237 | } |
| 4238 | |
| 4239 | int clang_TargetInfo_getPointerWidth(CXTargetInfo TargetInfo) { |
| 4240 | if (!TargetInfo) |
| 4241 | return -1; |
| 4242 | |
| 4243 | CXTranslationUnit CTUnit = TargetInfo->TranslationUnit; |
| 4244 | assert(!isNotUsableTU(CTUnit) && |
| 4245 | "Unexpected unusable translation unit in TargetInfo"); |
| 4246 | |
| 4247 | ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); |
| 4248 | return CXXUnit->getASTContext().getTargetInfo().getMaxPointerWidth(); |
| 4249 | } |
| 4250 | |
| 4251 | void clang_TargetInfo_dispose(CXTargetInfo TargetInfo) { |
| 4252 | if (!TargetInfo) |
| 4253 | return; |
| 4254 | |
| 4255 | delete TargetInfo; |
| 4256 | } |
| 4257 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4258 | //===----------------------------------------------------------------------===// |
| 4259 | // CXFile Operations. |
| 4260 | //===----------------------------------------------------------------------===// |
| 4261 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4262 | CXString clang_getFileName(CXFile SFile) { |
| 4263 | if (!SFile) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 4264 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4265 | |
| 4266 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4267 | return cxstring::createRef(FEnt->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
| 4270 | time_t clang_getFileTime(CXFile SFile) { |
| 4271 | if (!SFile) |
| 4272 | return 0; |
| 4273 | |
| 4274 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 4275 | return FEnt->getModificationTime(); |
| 4276 | } |
| 4277 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4278 | CXFile clang_getFile(CXTranslationUnit TU, const char *file_name) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4279 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4280 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 4281 | return nullptr; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4282 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4283 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4284 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4285 | |
| 4286 | FileManager &FMgr = CXXUnit->getFileManager(); |
Harlan Haskins | 8d323d1 | 2019-08-01 21:31:56 +0000 | [diff] [blame] | 4287 | auto File = FMgr.getFile(file_name); |
| 4288 | if (!File) |
| 4289 | return nullptr; |
| 4290 | return const_cast<FileEntry *>(*File); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4291 | } |
| 4292 | |
Erik Verbruggen | 3afa3ce | 2017-12-06 09:02:52 +0000 | [diff] [blame] | 4293 | const char *clang_getFileContents(CXTranslationUnit TU, CXFile file, |
| 4294 | size_t *size) { |
| 4295 | if (isNotUsableTU(TU)) { |
| 4296 | LOG_BAD_TU(TU); |
| 4297 | return nullptr; |
| 4298 | } |
| 4299 | |
| 4300 | const SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager(); |
| 4301 | FileID fid = SM.translateFile(static_cast<FileEntry *>(file)); |
| 4302 | bool Invalid = true; |
Nico Weber | 04347d8 | 2019-04-04 21:06:41 +0000 | [diff] [blame] | 4303 | const llvm::MemoryBuffer *buf = SM.getBuffer(fid, &Invalid); |
Erik Verbruggen | 3afa3ce | 2017-12-06 09:02:52 +0000 | [diff] [blame] | 4304 | if (Invalid) { |
| 4305 | if (size) |
| 4306 | *size = 0; |
| 4307 | return nullptr; |
| 4308 | } |
| 4309 | if (size) |
| 4310 | *size = buf->getBufferSize(); |
| 4311 | return buf->getBufferStart(); |
| 4312 | } |
| 4313 | |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4314 | unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit TU, |
| 4315 | CXFile file) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4316 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4317 | LOG_BAD_TU(TU); |
| 4318 | return 0; |
| 4319 | } |
| 4320 | |
| 4321 | if (!file) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4322 | return 0; |
| 4323 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4324 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4325 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
| 4326 | return CXXUnit->getPreprocessor().getHeaderSearchInfo() |
| 4327 | .isFileMultipleIncludeGuarded(FEnt); |
| 4328 | } |
| 4329 | |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4330 | int clang_getFileUniqueID(CXFile file, CXFileUniqueID *outID) { |
| 4331 | if (!file || !outID) |
| 4332 | return 1; |
| 4333 | |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4334 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 4335 | const llvm::sys::fs::UniqueID &ID = FEnt->getUniqueID(); |
| 4336 | outID->data[0] = ID.getDevice(); |
| 4337 | outID->data[1] = ID.getFile(); |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4338 | outID->data[2] = FEnt->getModificationTime(); |
| 4339 | return 0; |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4340 | } |
| 4341 | |
Argyrios Kyrtzidis | ac3997e | 2014-08-16 00:26:19 +0000 | [diff] [blame] | 4342 | int clang_File_isEqual(CXFile file1, CXFile file2) { |
| 4343 | if (file1 == file2) |
| 4344 | return true; |
| 4345 | |
| 4346 | if (!file1 || !file2) |
| 4347 | return false; |
| 4348 | |
| 4349 | FileEntry *FEnt1 = static_cast<FileEntry *>(file1); |
| 4350 | FileEntry *FEnt2 = static_cast<FileEntry *>(file2); |
| 4351 | return FEnt1->getUniqueID() == FEnt2->getUniqueID(); |
| 4352 | } |
| 4353 | |
Fangrui Song | e46ac5f | 2018-04-07 20:50:35 +0000 | [diff] [blame] | 4354 | CXString clang_File_tryGetRealPathName(CXFile SFile) { |
| 4355 | if (!SFile) |
| 4356 | return cxstring::createNull(); |
| 4357 | |
| 4358 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 4359 | return cxstring::createRef(FEnt->tryGetRealPathName()); |
| 4360 | } |
| 4361 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4362 | //===----------------------------------------------------------------------===// |
| 4363 | // CXCursor Operations. |
| 4364 | //===----------------------------------------------------------------------===// |
| 4365 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4366 | static const Decl *getDeclFromExpr(const Stmt *E) { |
| 4367 | if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4368 | return getDeclFromExpr(CE->getSubExpr()); |
| 4369 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4370 | if (const DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4371 | return RefExpr->getDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4372 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4373 | return ME->getMemberDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4374 | if (const ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4375 | return RE->getDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4376 | if (const ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4377 | if (PRE->isExplicitProperty()) |
| 4378 | return PRE->getExplicitProperty(); |
| 4379 | // It could be messaging both getter and setter as in: |
| 4380 | // ++myobj.myprop; |
| 4381 | // in which case prefer to associate the setter since it is less obvious |
| 4382 | // from inspecting the source that the setter is going to get called. |
| 4383 | if (PRE->isMessagingSetter()) |
| 4384 | return PRE->getImplicitPropertySetter(); |
| 4385 | return PRE->getImplicitPropertyGetter(); |
| 4386 | } |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4387 | if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4388 | return getDeclFromExpr(POE->getSyntacticForm()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4389 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4390 | if (Expr *Src = OVE->getSourceExpr()) |
| 4391 | return getDeclFromExpr(Src); |
| 4392 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4393 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4394 | return getDeclFromExpr(CE->getCallee()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4395 | if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4396 | if (!CE->isElidable()) |
| 4397 | return CE->getConstructor(); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4398 | if (const CXXInheritedCtorInitExpr *CE = |
| 4399 | dyn_cast<CXXInheritedCtorInitExpr>(E)) |
| 4400 | return CE->getConstructor(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4401 | if (const ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4402 | return OME->getMethodDecl(); |
| 4403 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4404 | if (const ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4405 | return PE->getProtocol(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4406 | if (const SubstNonTypeTemplateParmPackExpr *NTTP |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4407 | = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E)) |
| 4408 | return NTTP->getParameterPack(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4409 | if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4410 | if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || |
| 4411 | isa<ParmVarDecl>(SizeOfPack->getPack())) |
| 4412 | return SizeOfPack->getPack(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 4413 | |
| 4414 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4415 | } |
| 4416 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4417 | static SourceLocation getLocationFromExpr(const Expr *E) { |
| 4418 | if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4419 | return getLocationFromExpr(CE->getSubExpr()); |
| 4420 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4421 | if (const ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4422 | return /*FIXME:*/Msg->getLeftLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4423 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4424 | return DRE->getLocation(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4425 | if (const MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4426 | return Member->getMemberLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4427 | if (const ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4428 | return Ivar->getLocation(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4429 | if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4430 | return SizeOfPack->getPackLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4431 | if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4432 | return PropRef->getLocation(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4433 | |
| 4434 | return E->getBeginLoc(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
NAKAMURA Takumi | a01f4c3 | 2016-12-19 16:50:43 +0000 | [diff] [blame] | 4437 | extern "C" { |
| 4438 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4439 | unsigned clang_visitChildren(CXCursor parent, |
| 4440 | CXCursorVisitor visitor, |
| 4441 | CXClientData client_data) { |
| 4442 | CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, |
| 4443 | /*VisitPreprocessorLast=*/false); |
| 4444 | return CursorVis.VisitChildren(parent); |
| 4445 | } |
| 4446 | |
| 4447 | #ifndef __has_feature |
| 4448 | #define __has_feature(x) 0 |
| 4449 | #endif |
| 4450 | #if __has_feature(blocks) |
| 4451 | typedef enum CXChildVisitResult |
| 4452 | (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent); |
| 4453 | |
| 4454 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 4455 | CXClientData client_data) { |
| 4456 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 4457 | return block(cursor, parent); |
| 4458 | } |
| 4459 | #else |
| 4460 | // If we are compiled with a compiler that doesn't have native blocks support, |
| 4461 | // define and call the block manually, so the |
| 4462 | typedef struct _CXChildVisitResult |
| 4463 | { |
| 4464 | void *isa; |
| 4465 | int flags; |
| 4466 | int reserved; |
| 4467 | enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, |
| 4468 | CXCursor); |
| 4469 | } *CXCursorVisitorBlock; |
| 4470 | |
| 4471 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 4472 | CXClientData client_data) { |
| 4473 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 4474 | return block->invoke(block, cursor, parent); |
| 4475 | } |
| 4476 | #endif |
| 4477 | |
| 4478 | |
| 4479 | unsigned clang_visitChildrenWithBlock(CXCursor parent, |
| 4480 | CXCursorVisitorBlock block) { |
| 4481 | return clang_visitChildren(parent, visitWithBlock, block); |
| 4482 | } |
| 4483 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4484 | static CXString getDeclSpelling(const Decl *D) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4485 | if (!D) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4486 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4487 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4488 | const NamedDecl *ND = dyn_cast<NamedDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4489 | if (!ND) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4490 | if (const ObjCPropertyImplDecl *PropImpl = |
| 4491 | dyn_cast<ObjCPropertyImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4492 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4493 | return cxstring::createDup(Property->getIdentifier()->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4494 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4495 | if (const ImportDecl *ImportD = dyn_cast<ImportDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4496 | if (Module *Mod = ImportD->getImportedModule()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4497 | return cxstring::createDup(Mod->getFullModuleName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4498 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4499 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4500 | } |
| 4501 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4502 | if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4503 | return cxstring::createDup(OMD->getSelector().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4504 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4505 | if (const ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4506 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 4507 | // and returns different names. NamedDecl returns the class name and |
| 4508 | // ObjCCategoryImplDecl returns the category name. |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4509 | return cxstring::createRef(CIMP->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4510 | |
| 4511 | if (isa<UsingDirectiveDecl>(D)) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4512 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4513 | |
| 4514 | SmallString<1024> S; |
| 4515 | llvm::raw_svector_ostream os(S); |
| 4516 | ND->printName(os); |
| 4517 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4518 | return cxstring::createDup(os.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4519 | } |
| 4520 | |
| 4521 | CXString clang_getCursorSpelling(CXCursor C) { |
| 4522 | if (clang_isTranslationUnit(C.kind)) |
Dmitri Gribenko | 2c173b4 | 2013-01-11 19:28:44 +0000 | [diff] [blame] | 4523 | return clang_getTranslationUnitSpelling(getCursorTU(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4524 | |
| 4525 | if (clang_isReference(C.kind)) { |
| 4526 | switch (C.kind) { |
| 4527 | case CXCursor_ObjCSuperClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4528 | const ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4529 | return cxstring::createRef(Super->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4530 | } |
| 4531 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4532 | const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4533 | return cxstring::createRef(Class->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4534 | } |
| 4535 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4536 | const ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4537 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4538 | return cxstring::createRef(OID->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4539 | } |
| 4540 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4541 | const CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4542 | return cxstring::createDup(B->getType().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4543 | } |
| 4544 | case CXCursor_TypeRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4545 | const TypeDecl *Type = getCursorTypeRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4546 | assert(Type && "Missing type decl"); |
| 4547 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4548 | return cxstring::createDup(getCursorContext(C).getTypeDeclType(Type). |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4549 | getAsString()); |
| 4550 | } |
| 4551 | case CXCursor_TemplateRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4552 | const TemplateDecl *Template = getCursorTemplateRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4553 | assert(Template && "Missing template decl"); |
| 4554 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4555 | return cxstring::createDup(Template->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | case CXCursor_NamespaceRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4559 | const NamedDecl *NS = getCursorNamespaceRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4560 | assert(NS && "Missing namespace decl"); |
| 4561 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4562 | return cxstring::createDup(NS->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4563 | } |
| 4564 | |
| 4565 | case CXCursor_MemberRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4566 | const FieldDecl *Field = getCursorMemberRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4567 | assert(Field && "Missing member decl"); |
| 4568 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4569 | return cxstring::createDup(Field->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4570 | } |
| 4571 | |
| 4572 | case CXCursor_LabelRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4573 | const LabelStmt *Label = getCursorLabelRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4574 | assert(Label && "Missing label"); |
| 4575 | |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4576 | return cxstring::createRef(Label->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4577 | } |
| 4578 | |
| 4579 | case CXCursor_OverloadedDeclRef: { |
| 4580 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4581 | if (const Decl *D = Storage.dyn_cast<const Decl *>()) { |
| 4582 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4583 | return cxstring::createDup(ND->getNameAsString()); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4584 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4585 | } |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4586 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4587 | return cxstring::createDup(E->getName().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4588 | OverloadedTemplateStorage *Ovl |
| 4589 | = Storage.get<OverloadedTemplateStorage*>(); |
| 4590 | if (Ovl->size() == 0) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4591 | return cxstring::createEmpty(); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4592 | return cxstring::createDup((*Ovl->begin())->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | case CXCursor_VariableRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4596 | const VarDecl *Var = getCursorVariableRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4597 | assert(Var && "Missing variable decl"); |
| 4598 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4599 | return cxstring::createDup(Var->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4600 | } |
| 4601 | |
| 4602 | default: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4603 | return cxstring::createRef("<not implemented>"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4604 | } |
| 4605 | } |
| 4606 | |
| 4607 | if (clang_isExpression(C.kind)) { |
Argyrios Kyrtzidis | 3227d86 | 2014-03-03 19:40:52 +0000 | [diff] [blame] | 4608 | const Expr *E = getCursorExpr(C); |
| 4609 | |
| 4610 | if (C.kind == CXCursor_ObjCStringLiteral || |
| 4611 | C.kind == CXCursor_StringLiteral) { |
| 4612 | const StringLiteral *SLit; |
| 4613 | if (const ObjCStringLiteral *OSL = dyn_cast<ObjCStringLiteral>(E)) { |
| 4614 | SLit = OSL->getString(); |
| 4615 | } else { |
| 4616 | SLit = cast<StringLiteral>(E); |
| 4617 | } |
| 4618 | SmallString<256> Buf; |
| 4619 | llvm::raw_svector_ostream OS(Buf); |
| 4620 | SLit->outputString(OS); |
| 4621 | return cxstring::createDup(OS.str()); |
| 4622 | } |
| 4623 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4624 | const Decl *D = getDeclFromExpr(getCursorExpr(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4625 | if (D) |
| 4626 | return getDeclSpelling(D); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4627 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4631 | const Stmt *S = getCursorStmt(C); |
| 4632 | if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4633 | return cxstring::createRef(Label->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4634 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4635 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4636 | } |
| 4637 | |
| 4638 | if (C.kind == CXCursor_MacroExpansion) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4639 | return cxstring::createRef(getCursorMacroExpansion(C).getName() |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4640 | ->getNameStart()); |
| 4641 | |
| 4642 | if (C.kind == CXCursor_MacroDefinition) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4643 | return cxstring::createRef(getCursorMacroDefinition(C)->getName() |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4644 | ->getNameStart()); |
| 4645 | |
| 4646 | if (C.kind == CXCursor_InclusionDirective) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4647 | return cxstring::createDup(getCursorInclusionDirective(C)->getFileName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4648 | |
| 4649 | if (clang_isDeclaration(C.kind)) |
| 4650 | return getDeclSpelling(getCursorDecl(C)); |
| 4651 | |
| 4652 | if (C.kind == CXCursor_AnnotateAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 4653 | const AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C)); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4654 | return cxstring::createDup(AA->getAnnotation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4655 | } |
| 4656 | |
| 4657 | if (C.kind == CXCursor_AsmLabelAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 4658 | const AsmLabelAttr *AA = cast<AsmLabelAttr>(cxcursor::getCursorAttr(C)); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4659 | return cxstring::createDup(AA->getLabel()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4660 | } |
| 4661 | |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 4662 | if (C.kind == CXCursor_PackedAttr) { |
| 4663 | return cxstring::createRef("packed"); |
| 4664 | } |
| 4665 | |
Saleem Abdulrasool | 79c6971 | 2015-09-05 18:53:43 +0000 | [diff] [blame] | 4666 | if (C.kind == CXCursor_VisibilityAttr) { |
| 4667 | const VisibilityAttr *AA = cast<VisibilityAttr>(cxcursor::getCursorAttr(C)); |
| 4668 | switch (AA->getVisibility()) { |
| 4669 | case VisibilityAttr::VisibilityType::Default: |
| 4670 | return cxstring::createRef("default"); |
| 4671 | case VisibilityAttr::VisibilityType::Hidden: |
| 4672 | return cxstring::createRef("hidden"); |
| 4673 | case VisibilityAttr::VisibilityType::Protected: |
| 4674 | return cxstring::createRef("protected"); |
| 4675 | } |
| 4676 | llvm_unreachable("unknown visibility type"); |
| 4677 | } |
| 4678 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4679 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4680 | } |
| 4681 | |
| 4682 | CXSourceRange clang_Cursor_getSpellingNameRange(CXCursor C, |
| 4683 | unsigned pieceIndex, |
| 4684 | unsigned options) { |
| 4685 | if (clang_Cursor_isNull(C)) |
| 4686 | return clang_getNullRange(); |
| 4687 | |
| 4688 | ASTContext &Ctx = getCursorContext(C); |
| 4689 | |
| 4690 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4691 | const Stmt *S = getCursorStmt(C); |
| 4692 | if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4693 | if (pieceIndex > 0) |
| 4694 | return clang_getNullRange(); |
| 4695 | return cxloc::translateSourceRange(Ctx, Label->getIdentLoc()); |
| 4696 | } |
| 4697 | |
| 4698 | return clang_getNullRange(); |
| 4699 | } |
| 4700 | |
| 4701 | if (C.kind == CXCursor_ObjCMessageExpr) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4702 | if (const ObjCMessageExpr * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4703 | ME = dyn_cast_or_null<ObjCMessageExpr>(getCursorExpr(C))) { |
| 4704 | if (pieceIndex >= ME->getNumSelectorLocs()) |
| 4705 | return clang_getNullRange(); |
| 4706 | return cxloc::translateSourceRange(Ctx, ME->getSelectorLoc(pieceIndex)); |
| 4707 | } |
| 4708 | } |
| 4709 | |
| 4710 | if (C.kind == CXCursor_ObjCInstanceMethodDecl || |
| 4711 | C.kind == CXCursor_ObjCClassMethodDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4712 | if (const ObjCMethodDecl * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4713 | MD = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(C))) { |
| 4714 | if (pieceIndex >= MD->getNumSelectorLocs()) |
| 4715 | return clang_getNullRange(); |
| 4716 | return cxloc::translateSourceRange(Ctx, MD->getSelectorLoc(pieceIndex)); |
| 4717 | } |
| 4718 | } |
| 4719 | |
| 4720 | if (C.kind == CXCursor_ObjCCategoryDecl || |
| 4721 | C.kind == CXCursor_ObjCCategoryImplDecl) { |
| 4722 | if (pieceIndex > 0) |
| 4723 | return clang_getNullRange(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4724 | if (const ObjCCategoryDecl * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4725 | CD = dyn_cast_or_null<ObjCCategoryDecl>(getCursorDecl(C))) |
| 4726 | return cxloc::translateSourceRange(Ctx, CD->getCategoryNameLoc()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4727 | if (const ObjCCategoryImplDecl * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4728 | CID = dyn_cast_or_null<ObjCCategoryImplDecl>(getCursorDecl(C))) |
| 4729 | return cxloc::translateSourceRange(Ctx, CID->getCategoryNameLoc()); |
| 4730 | } |
| 4731 | |
| 4732 | if (C.kind == CXCursor_ModuleImportDecl) { |
| 4733 | if (pieceIndex > 0) |
| 4734 | return clang_getNullRange(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4735 | if (const ImportDecl *ImportD = |
| 4736 | dyn_cast_or_null<ImportDecl>(getCursorDecl(C))) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4737 | ArrayRef<SourceLocation> Locs = ImportD->getIdentifierLocs(); |
| 4738 | if (!Locs.empty()) |
| 4739 | return cxloc::translateSourceRange(Ctx, |
| 4740 | SourceRange(Locs.front(), Locs.back())); |
| 4741 | } |
| 4742 | return clang_getNullRange(); |
| 4743 | } |
| 4744 | |
Argyrios Kyrtzidis | a2a1e53 | 2014-08-26 20:23:26 +0000 | [diff] [blame] | 4745 | if (C.kind == CXCursor_CXXMethod || C.kind == CXCursor_Destructor || |
Kevin Funk | 4be5d67 | 2016-12-20 09:56:56 +0000 | [diff] [blame] | 4746 | C.kind == CXCursor_ConversionFunction || |
| 4747 | C.kind == CXCursor_FunctionDecl) { |
Argyrios Kyrtzidis | a2a1e53 | 2014-08-26 20:23:26 +0000 | [diff] [blame] | 4748 | if (pieceIndex > 0) |
| 4749 | return clang_getNullRange(); |
| 4750 | if (const FunctionDecl *FD = |
| 4751 | dyn_cast_or_null<FunctionDecl>(getCursorDecl(C))) { |
| 4752 | DeclarationNameInfo FunctionName = FD->getNameInfo(); |
| 4753 | return cxloc::translateSourceRange(Ctx, FunctionName.getSourceRange()); |
| 4754 | } |
| 4755 | return clang_getNullRange(); |
| 4756 | } |
| 4757 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4758 | // FIXME: A CXCursor_InclusionDirective should give the location of the |
| 4759 | // filename, but we don't keep track of this. |
| 4760 | |
| 4761 | // FIXME: A CXCursor_AnnotateAttr should give the location of the annotation |
| 4762 | // but we don't keep track of this. |
| 4763 | |
| 4764 | // FIXME: A CXCursor_AsmLabelAttr should give the location of the label |
| 4765 | // but we don't keep track of this. |
| 4766 | |
| 4767 | // Default handling, give the location of the cursor. |
| 4768 | |
| 4769 | if (pieceIndex > 0) |
| 4770 | return clang_getNullRange(); |
| 4771 | |
| 4772 | CXSourceLocation CXLoc = clang_getCursorLocation(C); |
| 4773 | SourceLocation Loc = cxloc::translateSourceLocation(CXLoc); |
| 4774 | return cxloc::translateSourceRange(Ctx, Loc); |
| 4775 | } |
| 4776 | |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4777 | CXString clang_Cursor_getMangling(CXCursor C) { |
| 4778 | if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind)) |
| 4779 | return cxstring::createEmpty(); |
| 4780 | |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4781 | // Mangling only works for functions and variables. |
Eli Bendersky | 7975959 | 2014-08-01 15:01:10 +0000 | [diff] [blame] | 4782 | const Decl *D = getCursorDecl(C); |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4783 | if (!D || !(isa<FunctionDecl>(D) || isa<VarDecl>(D))) |
| 4784 | return cxstring::createEmpty(); |
| 4785 | |
Argyrios Kyrtzidis | ca741ce | 2016-02-14 22:30:14 +0000 | [diff] [blame] | 4786 | ASTContext &Ctx = D->getASTContext(); |
Jan Korous | 7e36ecd | 2019-09-05 20:33:52 +0000 | [diff] [blame] | 4787 | ASTNameGenerator ASTNameGen(Ctx); |
| 4788 | return cxstring::createDup(ASTNameGen.getName(D)); |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4789 | } |
| 4790 | |
Saleem Abdulrasool | 6003443 | 2015-11-12 03:57:22 +0000 | [diff] [blame] | 4791 | CXStringSet *clang_Cursor_getCXXManglings(CXCursor C) { |
| 4792 | if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind)) |
| 4793 | return nullptr; |
| 4794 | |
| 4795 | const Decl *D = getCursorDecl(C); |
| 4796 | if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D))) |
| 4797 | return nullptr; |
| 4798 | |
Argyrios Kyrtzidis | ca741ce | 2016-02-14 22:30:14 +0000 | [diff] [blame] | 4799 | ASTContext &Ctx = D->getASTContext(); |
Jan Korous | 7e36ecd | 2019-09-05 20:33:52 +0000 | [diff] [blame] | 4800 | ASTNameGenerator ASTNameGen(Ctx); |
| 4801 | std::vector<std::string> Manglings = ASTNameGen.getAllManglings(D); |
Saleem Abdulrasool | 6003443 | 2015-11-12 03:57:22 +0000 | [diff] [blame] | 4802 | return cxstring::createSet(Manglings); |
| 4803 | } |
| 4804 | |
Dave Lee | 1a532c9 | 2017-09-22 16:58:57 +0000 | [diff] [blame] | 4805 | CXStringSet *clang_Cursor_getObjCManglings(CXCursor C) { |
| 4806 | if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind)) |
| 4807 | return nullptr; |
| 4808 | |
| 4809 | const Decl *D = getCursorDecl(C); |
| 4810 | if (!(isa<ObjCInterfaceDecl>(D) || isa<ObjCImplementationDecl>(D))) |
| 4811 | return nullptr; |
| 4812 | |
| 4813 | ASTContext &Ctx = D->getASTContext(); |
Jan Korous | 7e36ecd | 2019-09-05 20:33:52 +0000 | [diff] [blame] | 4814 | ASTNameGenerator ASTNameGen(Ctx); |
| 4815 | std::vector<std::string> Manglings = ASTNameGen.getAllManglings(D); |
Dave Lee | 1a532c9 | 2017-09-22 16:58:57 +0000 | [diff] [blame] | 4816 | return cxstring::createSet(Manglings); |
| 4817 | } |
| 4818 | |
Jonathan Coe | 45ef503 | 2018-01-16 10:19:56 +0000 | [diff] [blame] | 4819 | CXPrintingPolicy clang_getCursorPrintingPolicy(CXCursor C) { |
| 4820 | if (clang_Cursor_isNull(C)) |
| 4821 | return 0; |
| 4822 | return new PrintingPolicy(getCursorContext(C).getPrintingPolicy()); |
| 4823 | } |
| 4824 | |
| 4825 | void clang_PrintingPolicy_dispose(CXPrintingPolicy Policy) { |
| 4826 | if (Policy) |
| 4827 | delete static_cast<PrintingPolicy *>(Policy); |
| 4828 | } |
| 4829 | |
| 4830 | unsigned |
| 4831 | clang_PrintingPolicy_getProperty(CXPrintingPolicy Policy, |
| 4832 | enum CXPrintingPolicyProperty Property) { |
| 4833 | if (!Policy) |
| 4834 | return 0; |
| 4835 | |
| 4836 | PrintingPolicy *P = static_cast<PrintingPolicy *>(Policy); |
| 4837 | switch (Property) { |
| 4838 | case CXPrintingPolicy_Indentation: |
| 4839 | return P->Indentation; |
| 4840 | case CXPrintingPolicy_SuppressSpecifiers: |
| 4841 | return P->SuppressSpecifiers; |
| 4842 | case CXPrintingPolicy_SuppressTagKeyword: |
| 4843 | return P->SuppressTagKeyword; |
| 4844 | case CXPrintingPolicy_IncludeTagDefinition: |
| 4845 | return P->IncludeTagDefinition; |
| 4846 | case CXPrintingPolicy_SuppressScope: |
| 4847 | return P->SuppressScope; |
| 4848 | case CXPrintingPolicy_SuppressUnwrittenScope: |
| 4849 | return P->SuppressUnwrittenScope; |
| 4850 | case CXPrintingPolicy_SuppressInitializers: |
| 4851 | return P->SuppressInitializers; |
| 4852 | case CXPrintingPolicy_ConstantArraySizeAsWritten: |
| 4853 | return P->ConstantArraySizeAsWritten; |
| 4854 | case CXPrintingPolicy_AnonymousTagLocations: |
| 4855 | return P->AnonymousTagLocations; |
| 4856 | case CXPrintingPolicy_SuppressStrongLifetime: |
| 4857 | return P->SuppressStrongLifetime; |
| 4858 | case CXPrintingPolicy_SuppressLifetimeQualifiers: |
| 4859 | return P->SuppressLifetimeQualifiers; |
| 4860 | case CXPrintingPolicy_SuppressTemplateArgsInCXXConstructors: |
| 4861 | return P->SuppressTemplateArgsInCXXConstructors; |
| 4862 | case CXPrintingPolicy_Bool: |
| 4863 | return P->Bool; |
| 4864 | case CXPrintingPolicy_Restrict: |
| 4865 | return P->Restrict; |
| 4866 | case CXPrintingPolicy_Alignof: |
| 4867 | return P->Alignof; |
| 4868 | case CXPrintingPolicy_UnderscoreAlignof: |
| 4869 | return P->UnderscoreAlignof; |
| 4870 | case CXPrintingPolicy_UseVoidForZeroParams: |
| 4871 | return P->UseVoidForZeroParams; |
| 4872 | case CXPrintingPolicy_TerseOutput: |
| 4873 | return P->TerseOutput; |
| 4874 | case CXPrintingPolicy_PolishForDeclaration: |
| 4875 | return P->PolishForDeclaration; |
| 4876 | case CXPrintingPolicy_Half: |
| 4877 | return P->Half; |
| 4878 | case CXPrintingPolicy_MSWChar: |
| 4879 | return P->MSWChar; |
| 4880 | case CXPrintingPolicy_IncludeNewlines: |
| 4881 | return P->IncludeNewlines; |
| 4882 | case CXPrintingPolicy_MSVCFormatting: |
| 4883 | return P->MSVCFormatting; |
| 4884 | case CXPrintingPolicy_ConstantsAsWritten: |
| 4885 | return P->ConstantsAsWritten; |
| 4886 | case CXPrintingPolicy_SuppressImplicitBase: |
| 4887 | return P->SuppressImplicitBase; |
| 4888 | case CXPrintingPolicy_FullyQualifiedName: |
| 4889 | return P->FullyQualifiedName; |
| 4890 | } |
| 4891 | |
| 4892 | assert(false && "Invalid CXPrintingPolicyProperty"); |
| 4893 | return 0; |
| 4894 | } |
| 4895 | |
| 4896 | void clang_PrintingPolicy_setProperty(CXPrintingPolicy Policy, |
| 4897 | enum CXPrintingPolicyProperty Property, |
| 4898 | unsigned Value) { |
| 4899 | if (!Policy) |
| 4900 | return; |
| 4901 | |
| 4902 | PrintingPolicy *P = static_cast<PrintingPolicy *>(Policy); |
| 4903 | switch (Property) { |
| 4904 | case CXPrintingPolicy_Indentation: |
| 4905 | P->Indentation = Value; |
| 4906 | return; |
| 4907 | case CXPrintingPolicy_SuppressSpecifiers: |
| 4908 | P->SuppressSpecifiers = Value; |
| 4909 | return; |
| 4910 | case CXPrintingPolicy_SuppressTagKeyword: |
| 4911 | P->SuppressTagKeyword = Value; |
| 4912 | return; |
| 4913 | case CXPrintingPolicy_IncludeTagDefinition: |
| 4914 | P->IncludeTagDefinition = Value; |
| 4915 | return; |
| 4916 | case CXPrintingPolicy_SuppressScope: |
| 4917 | P->SuppressScope = Value; |
| 4918 | return; |
| 4919 | case CXPrintingPolicy_SuppressUnwrittenScope: |
| 4920 | P->SuppressUnwrittenScope = Value; |
| 4921 | return; |
| 4922 | case CXPrintingPolicy_SuppressInitializers: |
| 4923 | P->SuppressInitializers = Value; |
| 4924 | return; |
| 4925 | case CXPrintingPolicy_ConstantArraySizeAsWritten: |
| 4926 | P->ConstantArraySizeAsWritten = Value; |
| 4927 | return; |
| 4928 | case CXPrintingPolicy_AnonymousTagLocations: |
| 4929 | P->AnonymousTagLocations = Value; |
| 4930 | return; |
| 4931 | case CXPrintingPolicy_SuppressStrongLifetime: |
| 4932 | P->SuppressStrongLifetime = Value; |
| 4933 | return; |
| 4934 | case CXPrintingPolicy_SuppressLifetimeQualifiers: |
| 4935 | P->SuppressLifetimeQualifiers = Value; |
| 4936 | return; |
| 4937 | case CXPrintingPolicy_SuppressTemplateArgsInCXXConstructors: |
| 4938 | P->SuppressTemplateArgsInCXXConstructors = Value; |
| 4939 | return; |
| 4940 | case CXPrintingPolicy_Bool: |
| 4941 | P->Bool = Value; |
| 4942 | return; |
| 4943 | case CXPrintingPolicy_Restrict: |
| 4944 | P->Restrict = Value; |
| 4945 | return; |
| 4946 | case CXPrintingPolicy_Alignof: |
| 4947 | P->Alignof = Value; |
| 4948 | return; |
| 4949 | case CXPrintingPolicy_UnderscoreAlignof: |
| 4950 | P->UnderscoreAlignof = Value; |
| 4951 | return; |
| 4952 | case CXPrintingPolicy_UseVoidForZeroParams: |
| 4953 | P->UseVoidForZeroParams = Value; |
| 4954 | return; |
| 4955 | case CXPrintingPolicy_TerseOutput: |
| 4956 | P->TerseOutput = Value; |
| 4957 | return; |
| 4958 | case CXPrintingPolicy_PolishForDeclaration: |
| 4959 | P->PolishForDeclaration = Value; |
| 4960 | return; |
| 4961 | case CXPrintingPolicy_Half: |
| 4962 | P->Half = Value; |
| 4963 | return; |
| 4964 | case CXPrintingPolicy_MSWChar: |
| 4965 | P->MSWChar = Value; |
| 4966 | return; |
| 4967 | case CXPrintingPolicy_IncludeNewlines: |
| 4968 | P->IncludeNewlines = Value; |
| 4969 | return; |
| 4970 | case CXPrintingPolicy_MSVCFormatting: |
| 4971 | P->MSVCFormatting = Value; |
| 4972 | return; |
| 4973 | case CXPrintingPolicy_ConstantsAsWritten: |
| 4974 | P->ConstantsAsWritten = Value; |
| 4975 | return; |
| 4976 | case CXPrintingPolicy_SuppressImplicitBase: |
| 4977 | P->SuppressImplicitBase = Value; |
| 4978 | return; |
| 4979 | case CXPrintingPolicy_FullyQualifiedName: |
| 4980 | P->FullyQualifiedName = Value; |
| 4981 | return; |
| 4982 | } |
| 4983 | |
| 4984 | assert(false && "Invalid CXPrintingPolicyProperty"); |
| 4985 | } |
| 4986 | |
| 4987 | CXString clang_getCursorPrettyPrinted(CXCursor C, CXPrintingPolicy cxPolicy) { |
| 4988 | if (clang_Cursor_isNull(C)) |
| 4989 | return cxstring::createEmpty(); |
| 4990 | |
| 4991 | if (clang_isDeclaration(C.kind)) { |
| 4992 | const Decl *D = getCursorDecl(C); |
| 4993 | if (!D) |
| 4994 | return cxstring::createEmpty(); |
| 4995 | |
| 4996 | SmallString<128> Str; |
| 4997 | llvm::raw_svector_ostream OS(Str); |
| 4998 | PrintingPolicy *UserPolicy = static_cast<PrintingPolicy *>(cxPolicy); |
| 4999 | D->print(OS, UserPolicy ? *UserPolicy |
| 5000 | : getCursorContext(C).getPrintingPolicy()); |
| 5001 | |
| 5002 | return cxstring::createDup(OS.str()); |
| 5003 | } |
| 5004 | |
| 5005 | return cxstring::createEmpty(); |
| 5006 | } |
| 5007 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5008 | CXString clang_getCursorDisplayName(CXCursor C) { |
| 5009 | if (!clang_isDeclaration(C.kind)) |
| 5010 | return clang_getCursorSpelling(C); |
| 5011 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5012 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5013 | if (!D) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 5014 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5015 | |
| 5016 | PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5017 | if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5018 | D = FunTmpl->getTemplatedDecl(); |
| 5019 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5020 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5021 | SmallString<64> Str; |
| 5022 | llvm::raw_svector_ostream OS(Str); |
| 5023 | OS << *Function; |
| 5024 | if (Function->getPrimaryTemplate()) |
| 5025 | OS << "<>"; |
| 5026 | OS << "("; |
| 5027 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { |
| 5028 | if (I) |
| 5029 | OS << ", "; |
| 5030 | OS << Function->getParamDecl(I)->getType().getAsString(Policy); |
| 5031 | } |
| 5032 | |
| 5033 | if (Function->isVariadic()) { |
| 5034 | if (Function->getNumParams()) |
| 5035 | OS << ", "; |
| 5036 | OS << "..."; |
| 5037 | } |
| 5038 | OS << ")"; |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 5039 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5040 | } |
| 5041 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5042 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5043 | SmallString<64> Str; |
| 5044 | llvm::raw_svector_ostream OS(Str); |
| 5045 | OS << *ClassTemplate; |
| 5046 | OS << "<"; |
| 5047 | TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); |
| 5048 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 5049 | if (I) |
| 5050 | OS << ", "; |
| 5051 | |
| 5052 | NamedDecl *Param = Params->getParam(I); |
| 5053 | if (Param->getIdentifier()) { |
| 5054 | OS << Param->getIdentifier()->getName(); |
| 5055 | continue; |
| 5056 | } |
| 5057 | |
| 5058 | // There is no parameter name, which makes this tricky. Try to come up |
| 5059 | // with something useful that isn't too long. |
| 5060 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Saar Raz | ff1e0fc | 2020-01-15 02:48:42 +0200 | [diff] [blame] | 5061 | if (const auto *TC = TTP->getTypeConstraint()) { |
| 5062 | TC->getConceptNameInfo().printName(OS, Policy); |
| 5063 | if (TC->hasExplicitTemplateArgs()) |
| 5064 | OS << "<...>"; |
| 5065 | } else |
| 5066 | OS << (TTP->wasDeclaredWithTypename()? "typename" : "class"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5067 | else if (NonTypeTemplateParmDecl *NTTP |
| 5068 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 5069 | OS << NTTP->getType().getAsString(Policy); |
| 5070 | else |
| 5071 | OS << "template<...> class"; |
| 5072 | } |
| 5073 | |
| 5074 | OS << ">"; |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 5075 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5078 | if (const ClassTemplateSpecializationDecl *ClassSpec |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5079 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 5080 | // If the type was explicitly written, use that. |
| 5081 | if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 5082 | return cxstring::createDup(TSInfo->getType().getAsString(Policy)); |
Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame] | 5083 | |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 5084 | SmallString<128> Str; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5085 | llvm::raw_svector_ostream OS(Str); |
| 5086 | OS << *ClassSpec; |
Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame] | 5087 | printTemplateArgumentList(OS, ClassSpec->getTemplateArgs().asArray(), |
| 5088 | Policy); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 5089 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5090 | } |
| 5091 | |
| 5092 | return clang_getCursorSpelling(C); |
| 5093 | } |
| 5094 | |
| 5095 | CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
| 5096 | switch (Kind) { |
| 5097 | case CXCursor_FunctionDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5098 | return cxstring::createRef("FunctionDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5099 | case CXCursor_TypedefDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5100 | return cxstring::createRef("TypedefDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5101 | case CXCursor_EnumDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5102 | return cxstring::createRef("EnumDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5103 | case CXCursor_EnumConstantDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5104 | return cxstring::createRef("EnumConstantDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5105 | case CXCursor_StructDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5106 | return cxstring::createRef("StructDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5107 | case CXCursor_UnionDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5108 | return cxstring::createRef("UnionDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5109 | case CXCursor_ClassDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5110 | return cxstring::createRef("ClassDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5111 | case CXCursor_FieldDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5112 | return cxstring::createRef("FieldDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5113 | case CXCursor_VarDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5114 | return cxstring::createRef("VarDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5115 | case CXCursor_ParmDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5116 | return cxstring::createRef("ParmDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5117 | case CXCursor_ObjCInterfaceDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5118 | return cxstring::createRef("ObjCInterfaceDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5119 | case CXCursor_ObjCCategoryDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5120 | return cxstring::createRef("ObjCCategoryDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5121 | case CXCursor_ObjCProtocolDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5122 | return cxstring::createRef("ObjCProtocolDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5123 | case CXCursor_ObjCPropertyDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5124 | return cxstring::createRef("ObjCPropertyDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5125 | case CXCursor_ObjCIvarDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5126 | return cxstring::createRef("ObjCIvarDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5127 | case CXCursor_ObjCInstanceMethodDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5128 | return cxstring::createRef("ObjCInstanceMethodDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5129 | case CXCursor_ObjCClassMethodDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5130 | return cxstring::createRef("ObjCClassMethodDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5131 | case CXCursor_ObjCImplementationDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5132 | return cxstring::createRef("ObjCImplementationDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5133 | case CXCursor_ObjCCategoryImplDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5134 | return cxstring::createRef("ObjCCategoryImplDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5135 | case CXCursor_CXXMethod: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5136 | return cxstring::createRef("CXXMethod"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5137 | case CXCursor_UnexposedDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5138 | return cxstring::createRef("UnexposedDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5139 | case CXCursor_ObjCSuperClassRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5140 | return cxstring::createRef("ObjCSuperClassRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5141 | case CXCursor_ObjCProtocolRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5142 | return cxstring::createRef("ObjCProtocolRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5143 | case CXCursor_ObjCClassRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5144 | return cxstring::createRef("ObjCClassRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5145 | case CXCursor_TypeRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5146 | return cxstring::createRef("TypeRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5147 | case CXCursor_TemplateRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5148 | return cxstring::createRef("TemplateRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5149 | case CXCursor_NamespaceRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5150 | return cxstring::createRef("NamespaceRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5151 | case CXCursor_MemberRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5152 | return cxstring::createRef("MemberRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5153 | case CXCursor_LabelRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5154 | return cxstring::createRef("LabelRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5155 | case CXCursor_OverloadedDeclRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5156 | return cxstring::createRef("OverloadedDeclRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5157 | case CXCursor_VariableRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5158 | return cxstring::createRef("VariableRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5159 | case CXCursor_IntegerLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5160 | return cxstring::createRef("IntegerLiteral"); |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 5161 | case CXCursor_FixedPointLiteral: |
| 5162 | return cxstring::createRef("FixedPointLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5163 | case CXCursor_FloatingLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5164 | return cxstring::createRef("FloatingLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5165 | case CXCursor_ImaginaryLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5166 | return cxstring::createRef("ImaginaryLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5167 | case CXCursor_StringLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5168 | return cxstring::createRef("StringLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5169 | case CXCursor_CharacterLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5170 | return cxstring::createRef("CharacterLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5171 | case CXCursor_ParenExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5172 | return cxstring::createRef("ParenExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5173 | case CXCursor_UnaryOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5174 | return cxstring::createRef("UnaryOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5175 | case CXCursor_ArraySubscriptExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5176 | return cxstring::createRef("ArraySubscriptExpr"); |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 5177 | case CXCursor_OMPArraySectionExpr: |
| 5178 | return cxstring::createRef("OMPArraySectionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5179 | case CXCursor_BinaryOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5180 | return cxstring::createRef("BinaryOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5181 | case CXCursor_CompoundAssignOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5182 | return cxstring::createRef("CompoundAssignOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5183 | case CXCursor_ConditionalOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5184 | return cxstring::createRef("ConditionalOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5185 | case CXCursor_CStyleCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5186 | return cxstring::createRef("CStyleCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5187 | case CXCursor_CompoundLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5188 | return cxstring::createRef("CompoundLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5189 | case CXCursor_InitListExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5190 | return cxstring::createRef("InitListExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5191 | case CXCursor_AddrLabelExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5192 | return cxstring::createRef("AddrLabelExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5193 | case CXCursor_StmtExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5194 | return cxstring::createRef("StmtExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5195 | case CXCursor_GenericSelectionExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5196 | return cxstring::createRef("GenericSelectionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5197 | case CXCursor_GNUNullExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5198 | return cxstring::createRef("GNUNullExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5199 | case CXCursor_CXXStaticCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5200 | return cxstring::createRef("CXXStaticCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5201 | case CXCursor_CXXDynamicCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5202 | return cxstring::createRef("CXXDynamicCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5203 | case CXCursor_CXXReinterpretCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5204 | return cxstring::createRef("CXXReinterpretCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5205 | case CXCursor_CXXConstCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5206 | return cxstring::createRef("CXXConstCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5207 | case CXCursor_CXXFunctionalCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5208 | return cxstring::createRef("CXXFunctionalCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5209 | case CXCursor_CXXTypeidExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5210 | return cxstring::createRef("CXXTypeidExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5211 | case CXCursor_CXXBoolLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5212 | return cxstring::createRef("CXXBoolLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5213 | case CXCursor_CXXNullPtrLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5214 | return cxstring::createRef("CXXNullPtrLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5215 | case CXCursor_CXXThisExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5216 | return cxstring::createRef("CXXThisExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5217 | case CXCursor_CXXThrowExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5218 | return cxstring::createRef("CXXThrowExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5219 | case CXCursor_CXXNewExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5220 | return cxstring::createRef("CXXNewExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5221 | case CXCursor_CXXDeleteExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5222 | return cxstring::createRef("CXXDeleteExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5223 | case CXCursor_UnaryExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5224 | return cxstring::createRef("UnaryExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5225 | case CXCursor_ObjCStringLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5226 | return cxstring::createRef("ObjCStringLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5227 | case CXCursor_ObjCBoolLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5228 | return cxstring::createRef("ObjCBoolLiteralExpr"); |
Erik Pilkington | 29099de | 2016-07-16 00:35:23 +0000 | [diff] [blame] | 5229 | case CXCursor_ObjCAvailabilityCheckExpr: |
| 5230 | return cxstring::createRef("ObjCAvailabilityCheckExpr"); |
Argyrios Kyrtzidis | c2233be | 2013-04-23 17:57:17 +0000 | [diff] [blame] | 5231 | case CXCursor_ObjCSelfExpr: |
| 5232 | return cxstring::createRef("ObjCSelfExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5233 | case CXCursor_ObjCEncodeExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5234 | return cxstring::createRef("ObjCEncodeExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5235 | case CXCursor_ObjCSelectorExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5236 | return cxstring::createRef("ObjCSelectorExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5237 | case CXCursor_ObjCProtocolExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5238 | return cxstring::createRef("ObjCProtocolExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5239 | case CXCursor_ObjCBridgedCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5240 | return cxstring::createRef("ObjCBridgedCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5241 | case CXCursor_BlockExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5242 | return cxstring::createRef("BlockExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5243 | case CXCursor_PackExpansionExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5244 | return cxstring::createRef("PackExpansionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5245 | case CXCursor_SizeOfPackExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5246 | return cxstring::createRef("SizeOfPackExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5247 | case CXCursor_LambdaExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5248 | return cxstring::createRef("LambdaExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5249 | case CXCursor_UnexposedExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5250 | return cxstring::createRef("UnexposedExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5251 | case CXCursor_DeclRefExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5252 | return cxstring::createRef("DeclRefExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5253 | case CXCursor_MemberRefExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5254 | return cxstring::createRef("MemberRefExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5255 | case CXCursor_CallExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5256 | return cxstring::createRef("CallExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5257 | case CXCursor_ObjCMessageExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5258 | return cxstring::createRef("ObjCMessageExpr"); |
Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 5259 | case CXCursor_BuiltinBitCastExpr: |
| 5260 | return cxstring::createRef("BuiltinBitCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5261 | case CXCursor_UnexposedStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5262 | return cxstring::createRef("UnexposedStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5263 | case CXCursor_DeclStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5264 | return cxstring::createRef("DeclStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5265 | case CXCursor_LabelStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5266 | return cxstring::createRef("LabelStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5267 | case CXCursor_CompoundStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5268 | return cxstring::createRef("CompoundStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5269 | case CXCursor_CaseStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5270 | return cxstring::createRef("CaseStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5271 | case CXCursor_DefaultStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5272 | return cxstring::createRef("DefaultStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5273 | case CXCursor_IfStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5274 | return cxstring::createRef("IfStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5275 | case CXCursor_SwitchStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5276 | return cxstring::createRef("SwitchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5277 | case CXCursor_WhileStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5278 | return cxstring::createRef("WhileStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5279 | case CXCursor_DoStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5280 | return cxstring::createRef("DoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5281 | case CXCursor_ForStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5282 | return cxstring::createRef("ForStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5283 | case CXCursor_GotoStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5284 | return cxstring::createRef("GotoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5285 | case CXCursor_IndirectGotoStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5286 | return cxstring::createRef("IndirectGotoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5287 | case CXCursor_ContinueStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5288 | return cxstring::createRef("ContinueStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5289 | case CXCursor_BreakStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5290 | return cxstring::createRef("BreakStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5291 | case CXCursor_ReturnStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5292 | return cxstring::createRef("ReturnStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5293 | case CXCursor_GCCAsmStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5294 | return cxstring::createRef("GCCAsmStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5295 | case CXCursor_MSAsmStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5296 | return cxstring::createRef("MSAsmStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5297 | case CXCursor_ObjCAtTryStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5298 | return cxstring::createRef("ObjCAtTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5299 | case CXCursor_ObjCAtCatchStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5300 | return cxstring::createRef("ObjCAtCatchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5301 | case CXCursor_ObjCAtFinallyStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5302 | return cxstring::createRef("ObjCAtFinallyStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5303 | case CXCursor_ObjCAtThrowStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5304 | return cxstring::createRef("ObjCAtThrowStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5305 | case CXCursor_ObjCAtSynchronizedStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5306 | return cxstring::createRef("ObjCAtSynchronizedStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5307 | case CXCursor_ObjCAutoreleasePoolStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5308 | return cxstring::createRef("ObjCAutoreleasePoolStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5309 | case CXCursor_ObjCForCollectionStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5310 | return cxstring::createRef("ObjCForCollectionStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5311 | case CXCursor_CXXCatchStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5312 | return cxstring::createRef("CXXCatchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5313 | case CXCursor_CXXTryStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5314 | return cxstring::createRef("CXXTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5315 | case CXCursor_CXXForRangeStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5316 | return cxstring::createRef("CXXForRangeStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5317 | case CXCursor_SEHTryStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5318 | return cxstring::createRef("SEHTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5319 | case CXCursor_SEHExceptStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5320 | return cxstring::createRef("SEHExceptStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5321 | case CXCursor_SEHFinallyStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5322 | return cxstring::createRef("SEHFinallyStmt"); |
Nico Weber | 9b98207 | 2014-07-07 00:12:30 +0000 | [diff] [blame] | 5323 | case CXCursor_SEHLeaveStmt: |
| 5324 | return cxstring::createRef("SEHLeaveStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5325 | case CXCursor_NullStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5326 | return cxstring::createRef("NullStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5327 | case CXCursor_InvalidFile: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5328 | return cxstring::createRef("InvalidFile"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5329 | case CXCursor_InvalidCode: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5330 | return cxstring::createRef("InvalidCode"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5331 | case CXCursor_NoDeclFound: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5332 | return cxstring::createRef("NoDeclFound"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5333 | case CXCursor_NotImplemented: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5334 | return cxstring::createRef("NotImplemented"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5335 | case CXCursor_TranslationUnit: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5336 | return cxstring::createRef("TranslationUnit"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5337 | case CXCursor_UnexposedAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5338 | return cxstring::createRef("UnexposedAttr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5339 | case CXCursor_IBActionAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5340 | return cxstring::createRef("attribute(ibaction)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5341 | case CXCursor_IBOutletAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5342 | return cxstring::createRef("attribute(iboutlet)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5343 | case CXCursor_IBOutletCollectionAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5344 | return cxstring::createRef("attribute(iboutletcollection)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5345 | case CXCursor_CXXFinalAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5346 | return cxstring::createRef("attribute(final)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5347 | case CXCursor_CXXOverrideAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5348 | return cxstring::createRef("attribute(override)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5349 | case CXCursor_AnnotateAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5350 | return cxstring::createRef("attribute(annotate)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5351 | case CXCursor_AsmLabelAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5352 | return cxstring::createRef("asm label"); |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 5353 | case CXCursor_PackedAttr: |
| 5354 | return cxstring::createRef("attribute(packed)"); |
Joey Gouly | 8122838 | 2014-05-01 15:41:58 +0000 | [diff] [blame] | 5355 | case CXCursor_PureAttr: |
| 5356 | return cxstring::createRef("attribute(pure)"); |
| 5357 | case CXCursor_ConstAttr: |
| 5358 | return cxstring::createRef("attribute(const)"); |
| 5359 | case CXCursor_NoDuplicateAttr: |
| 5360 | return cxstring::createRef("attribute(noduplicate)"); |
Eli Bendersky | 2581e66 | 2014-05-28 19:29:58 +0000 | [diff] [blame] | 5361 | case CXCursor_CUDAConstantAttr: |
| 5362 | return cxstring::createRef("attribute(constant)"); |
| 5363 | case CXCursor_CUDADeviceAttr: |
| 5364 | return cxstring::createRef("attribute(device)"); |
| 5365 | case CXCursor_CUDAGlobalAttr: |
| 5366 | return cxstring::createRef("attribute(global)"); |
| 5367 | case CXCursor_CUDAHostAttr: |
| 5368 | return cxstring::createRef("attribute(host)"); |
Eli Bendersky | 9b07147 | 2014-08-08 14:59:00 +0000 | [diff] [blame] | 5369 | case CXCursor_CUDASharedAttr: |
| 5370 | return cxstring::createRef("attribute(shared)"); |
Saleem Abdulrasool | 79c6971 | 2015-09-05 18:53:43 +0000 | [diff] [blame] | 5371 | case CXCursor_VisibilityAttr: |
| 5372 | return cxstring::createRef("attribute(visibility)"); |
Saleem Abdulrasool | 8aa0b80 | 2015-12-10 18:45:18 +0000 | [diff] [blame] | 5373 | case CXCursor_DLLExport: |
| 5374 | return cxstring::createRef("attribute(dllexport)"); |
| 5375 | case CXCursor_DLLImport: |
| 5376 | return cxstring::createRef("attribute(dllimport)"); |
Michael Wu | d092d0b | 2018-08-03 05:03:22 +0000 | [diff] [blame] | 5377 | case CXCursor_NSReturnsRetained: |
| 5378 | return cxstring::createRef("attribute(ns_returns_retained)"); |
| 5379 | case CXCursor_NSReturnsNotRetained: |
| 5380 | return cxstring::createRef("attribute(ns_returns_not_retained)"); |
| 5381 | case CXCursor_NSReturnsAutoreleased: |
| 5382 | return cxstring::createRef("attribute(ns_returns_autoreleased)"); |
| 5383 | case CXCursor_NSConsumesSelf: |
| 5384 | return cxstring::createRef("attribute(ns_consumes_self)"); |
| 5385 | case CXCursor_NSConsumed: |
| 5386 | return cxstring::createRef("attribute(ns_consumed)"); |
| 5387 | case CXCursor_ObjCException: |
| 5388 | return cxstring::createRef("attribute(objc_exception)"); |
| 5389 | case CXCursor_ObjCNSObject: |
| 5390 | return cxstring::createRef("attribute(NSObject)"); |
| 5391 | case CXCursor_ObjCIndependentClass: |
| 5392 | return cxstring::createRef("attribute(objc_independent_class)"); |
| 5393 | case CXCursor_ObjCPreciseLifetime: |
| 5394 | return cxstring::createRef("attribute(objc_precise_lifetime)"); |
| 5395 | case CXCursor_ObjCReturnsInnerPointer: |
| 5396 | return cxstring::createRef("attribute(objc_returns_inner_pointer)"); |
| 5397 | case CXCursor_ObjCRequiresSuper: |
| 5398 | return cxstring::createRef("attribute(objc_requires_super)"); |
| 5399 | case CXCursor_ObjCRootClass: |
| 5400 | return cxstring::createRef("attribute(objc_root_class)"); |
| 5401 | case CXCursor_ObjCSubclassingRestricted: |
| 5402 | return cxstring::createRef("attribute(objc_subclassing_restricted)"); |
| 5403 | case CXCursor_ObjCExplicitProtocolImpl: |
| 5404 | return cxstring::createRef("attribute(objc_protocol_requires_explicit_implementation)"); |
| 5405 | case CXCursor_ObjCDesignatedInitializer: |
| 5406 | return cxstring::createRef("attribute(objc_designated_initializer)"); |
| 5407 | case CXCursor_ObjCRuntimeVisible: |
| 5408 | return cxstring::createRef("attribute(objc_runtime_visible)"); |
| 5409 | case CXCursor_ObjCBoxable: |
| 5410 | return cxstring::createRef("attribute(objc_boxable)"); |
Michael Wu | 58d837d | 2018-08-03 05:55:40 +0000 | [diff] [blame] | 5411 | case CXCursor_FlagEnum: |
| 5412 | return cxstring::createRef("attribute(flag_enum)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5413 | case CXCursor_PreprocessingDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5414 | return cxstring::createRef("preprocessing directive"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5415 | case CXCursor_MacroDefinition: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5416 | return cxstring::createRef("macro definition"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5417 | case CXCursor_MacroExpansion: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5418 | return cxstring::createRef("macro expansion"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5419 | case CXCursor_InclusionDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5420 | return cxstring::createRef("inclusion directive"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5421 | case CXCursor_Namespace: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5422 | return cxstring::createRef("Namespace"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5423 | case CXCursor_LinkageSpec: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5424 | return cxstring::createRef("LinkageSpec"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5425 | case CXCursor_CXXBaseSpecifier: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5426 | return cxstring::createRef("C++ base class specifier"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5427 | case CXCursor_Constructor: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5428 | return cxstring::createRef("CXXConstructor"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5429 | case CXCursor_Destructor: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5430 | return cxstring::createRef("CXXDestructor"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5431 | case CXCursor_ConversionFunction: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5432 | return cxstring::createRef("CXXConversion"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5433 | case CXCursor_TemplateTypeParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5434 | return cxstring::createRef("TemplateTypeParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5435 | case CXCursor_NonTypeTemplateParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5436 | return cxstring::createRef("NonTypeTemplateParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5437 | case CXCursor_TemplateTemplateParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5438 | return cxstring::createRef("TemplateTemplateParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5439 | case CXCursor_FunctionTemplate: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5440 | return cxstring::createRef("FunctionTemplate"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5441 | case CXCursor_ClassTemplate: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5442 | return cxstring::createRef("ClassTemplate"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5443 | case CXCursor_ClassTemplatePartialSpecialization: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5444 | return cxstring::createRef("ClassTemplatePartialSpecialization"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5445 | case CXCursor_NamespaceAlias: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5446 | return cxstring::createRef("NamespaceAlias"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5447 | case CXCursor_UsingDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5448 | return cxstring::createRef("UsingDirective"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5449 | case CXCursor_UsingDeclaration: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5450 | return cxstring::createRef("UsingDeclaration"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5451 | case CXCursor_TypeAliasDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5452 | return cxstring::createRef("TypeAliasDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5453 | case CXCursor_ObjCSynthesizeDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5454 | return cxstring::createRef("ObjCSynthesizeDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5455 | case CXCursor_ObjCDynamicDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5456 | return cxstring::createRef("ObjCDynamicDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5457 | case CXCursor_CXXAccessSpecifier: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5458 | return cxstring::createRef("CXXAccessSpecifier"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5459 | case CXCursor_ModuleImportDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5460 | return cxstring::createRef("ModuleImport"); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 5461 | case CXCursor_OMPParallelDirective: |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 5462 | return cxstring::createRef("OMPParallelDirective"); |
| 5463 | case CXCursor_OMPSimdDirective: |
| 5464 | return cxstring::createRef("OMPSimdDirective"); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5465 | case CXCursor_OMPForDirective: |
| 5466 | return cxstring::createRef("OMPForDirective"); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 5467 | case CXCursor_OMPForSimdDirective: |
| 5468 | return cxstring::createRef("OMPForSimdDirective"); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 5469 | case CXCursor_OMPSectionsDirective: |
| 5470 | return cxstring::createRef("OMPSectionsDirective"); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 5471 | case CXCursor_OMPSectionDirective: |
| 5472 | return cxstring::createRef("OMPSectionDirective"); |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 5473 | case CXCursor_OMPSingleDirective: |
| 5474 | return cxstring::createRef("OMPSingleDirective"); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 5475 | case CXCursor_OMPMasterDirective: |
| 5476 | return cxstring::createRef("OMPMasterDirective"); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 5477 | case CXCursor_OMPCriticalDirective: |
| 5478 | return cxstring::createRef("OMPCriticalDirective"); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 5479 | case CXCursor_OMPParallelForDirective: |
| 5480 | return cxstring::createRef("OMPParallelForDirective"); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 5481 | case CXCursor_OMPParallelForSimdDirective: |
| 5482 | return cxstring::createRef("OMPParallelForSimdDirective"); |
cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 5483 | case CXCursor_OMPParallelMasterDirective: |
| 5484 | return cxstring::createRef("OMPParallelMasterDirective"); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 5485 | case CXCursor_OMPParallelSectionsDirective: |
| 5486 | return cxstring::createRef("OMPParallelSectionsDirective"); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5487 | case CXCursor_OMPTaskDirective: |
| 5488 | return cxstring::createRef("OMPTaskDirective"); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 5489 | case CXCursor_OMPTaskyieldDirective: |
| 5490 | return cxstring::createRef("OMPTaskyieldDirective"); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 5491 | case CXCursor_OMPBarrierDirective: |
| 5492 | return cxstring::createRef("OMPBarrierDirective"); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 5493 | case CXCursor_OMPTaskwaitDirective: |
| 5494 | return cxstring::createRef("OMPTaskwaitDirective"); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 5495 | case CXCursor_OMPTaskgroupDirective: |
| 5496 | return cxstring::createRef("OMPTaskgroupDirective"); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 5497 | case CXCursor_OMPFlushDirective: |
| 5498 | return cxstring::createRef("OMPFlushDirective"); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 5499 | case CXCursor_OMPOrderedDirective: |
| 5500 | return cxstring::createRef("OMPOrderedDirective"); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 5501 | case CXCursor_OMPAtomicDirective: |
| 5502 | return cxstring::createRef("OMPAtomicDirective"); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 5503 | case CXCursor_OMPTargetDirective: |
| 5504 | return cxstring::createRef("OMPTargetDirective"); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 5505 | case CXCursor_OMPTargetDataDirective: |
| 5506 | return cxstring::createRef("OMPTargetDataDirective"); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 5507 | case CXCursor_OMPTargetEnterDataDirective: |
| 5508 | return cxstring::createRef("OMPTargetEnterDataDirective"); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 5509 | case CXCursor_OMPTargetExitDataDirective: |
| 5510 | return cxstring::createRef("OMPTargetExitDataDirective"); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 5511 | case CXCursor_OMPTargetParallelDirective: |
| 5512 | return cxstring::createRef("OMPTargetParallelDirective"); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 5513 | case CXCursor_OMPTargetParallelForDirective: |
| 5514 | return cxstring::createRef("OMPTargetParallelForDirective"); |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 5515 | case CXCursor_OMPTargetUpdateDirective: |
| 5516 | return cxstring::createRef("OMPTargetUpdateDirective"); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 5517 | case CXCursor_OMPTeamsDirective: |
| 5518 | return cxstring::createRef("OMPTeamsDirective"); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 5519 | case CXCursor_OMPCancellationPointDirective: |
| 5520 | return cxstring::createRef("OMPCancellationPointDirective"); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 5521 | case CXCursor_OMPCancelDirective: |
| 5522 | return cxstring::createRef("OMPCancelDirective"); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 5523 | case CXCursor_OMPTaskLoopDirective: |
| 5524 | return cxstring::createRef("OMPTaskLoopDirective"); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 5525 | case CXCursor_OMPTaskLoopSimdDirective: |
| 5526 | return cxstring::createRef("OMPTaskLoopSimdDirective"); |
Alexey Bataev | 60e51c4 | 2019-10-10 20:13:02 +0000 | [diff] [blame] | 5527 | case CXCursor_OMPMasterTaskLoopDirective: |
| 5528 | return cxstring::createRef("OMPMasterTaskLoopDirective"); |
Alexey Bataev | b8552ab | 2019-10-18 16:47:35 +0000 | [diff] [blame] | 5529 | case CXCursor_OMPMasterTaskLoopSimdDirective: |
| 5530 | return cxstring::createRef("OMPMasterTaskLoopSimdDirective"); |
Alexey Bataev | 5bbcead | 2019-10-14 17:17:41 +0000 | [diff] [blame] | 5531 | case CXCursor_OMPParallelMasterTaskLoopDirective: |
| 5532 | return cxstring::createRef("OMPParallelMasterTaskLoopDirective"); |
Alexey Bataev | 14a388f | 2019-10-25 10:27:13 -0400 | [diff] [blame] | 5533 | case CXCursor_OMPParallelMasterTaskLoopSimdDirective: |
| 5534 | return cxstring::createRef("OMPParallelMasterTaskLoopSimdDirective"); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 5535 | case CXCursor_OMPDistributeDirective: |
| 5536 | return cxstring::createRef("OMPDistributeDirective"); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 5537 | case CXCursor_OMPDistributeParallelForDirective: |
| 5538 | return cxstring::createRef("OMPDistributeParallelForDirective"); |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 5539 | case CXCursor_OMPDistributeParallelForSimdDirective: |
| 5540 | return cxstring::createRef("OMPDistributeParallelForSimdDirective"); |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 5541 | case CXCursor_OMPDistributeSimdDirective: |
| 5542 | return cxstring::createRef("OMPDistributeSimdDirective"); |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 5543 | case CXCursor_OMPTargetParallelForSimdDirective: |
| 5544 | return cxstring::createRef("OMPTargetParallelForSimdDirective"); |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 5545 | case CXCursor_OMPTargetSimdDirective: |
| 5546 | return cxstring::createRef("OMPTargetSimdDirective"); |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 5547 | case CXCursor_OMPTeamsDistributeDirective: |
| 5548 | return cxstring::createRef("OMPTeamsDistributeDirective"); |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 5549 | case CXCursor_OMPTeamsDistributeSimdDirective: |
| 5550 | return cxstring::createRef("OMPTeamsDistributeSimdDirective"); |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 5551 | case CXCursor_OMPTeamsDistributeParallelForSimdDirective: |
| 5552 | return cxstring::createRef("OMPTeamsDistributeParallelForSimdDirective"); |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 5553 | case CXCursor_OMPTeamsDistributeParallelForDirective: |
| 5554 | return cxstring::createRef("OMPTeamsDistributeParallelForDirective"); |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 5555 | case CXCursor_OMPTargetTeamsDirective: |
| 5556 | return cxstring::createRef("OMPTargetTeamsDirective"); |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 5557 | case CXCursor_OMPTargetTeamsDistributeDirective: |
| 5558 | return cxstring::createRef("OMPTargetTeamsDistributeDirective"); |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 5559 | case CXCursor_OMPTargetTeamsDistributeParallelForDirective: |
| 5560 | return cxstring::createRef("OMPTargetTeamsDistributeParallelForDirective"); |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 5561 | case CXCursor_OMPTargetTeamsDistributeParallelForSimdDirective: |
| 5562 | return cxstring::createRef( |
| 5563 | "OMPTargetTeamsDistributeParallelForSimdDirective"); |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 5564 | case CXCursor_OMPTargetTeamsDistributeSimdDirective: |
| 5565 | return cxstring::createRef("OMPTargetTeamsDistributeSimdDirective"); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5566 | case CXCursor_OverloadCandidate: |
| 5567 | return cxstring::createRef("OverloadCandidate"); |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 5568 | case CXCursor_TypeAliasTemplateDecl: |
| 5569 | return cxstring::createRef("TypeAliasTemplateDecl"); |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 5570 | case CXCursor_StaticAssert: |
| 5571 | return cxstring::createRef("StaticAssert"); |
Olivier Goffart | d211c64 | 2016-11-04 06:29:27 +0000 | [diff] [blame] | 5572 | case CXCursor_FriendDecl: |
Sven van Haastregt | dc2c930 | 2019-02-11 11:00:56 +0000 | [diff] [blame] | 5573 | return cxstring::createRef("FriendDecl"); |
| 5574 | case CXCursor_ConvergentAttr: |
| 5575 | return cxstring::createRef("attribute(convergent)"); |
Emilio Cobos Alvarez | 0a3fe50 | 2019-02-25 21:24:52 +0000 | [diff] [blame] | 5576 | case CXCursor_WarnUnusedAttr: |
| 5577 | return cxstring::createRef("attribute(warn_unused)"); |
| 5578 | case CXCursor_WarnUnusedResultAttr: |
| 5579 | return cxstring::createRef("attribute(warn_unused_result)"); |
Emilio Cobos Alvarez | cd74127 | 2019-03-13 16:16:54 +0000 | [diff] [blame] | 5580 | case CXCursor_AlignedAttr: |
| 5581 | return cxstring::createRef("attribute(aligned)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | llvm_unreachable("Unhandled CXCursorKind"); |
| 5585 | } |
| 5586 | |
| 5587 | struct GetCursorData { |
| 5588 | SourceLocation TokenBeginLoc; |
| 5589 | bool PointsAtMacroArgExpansion; |
| 5590 | bool VisitedObjCPropertyImplDecl; |
| 5591 | SourceLocation VisitedDeclaratorDeclStartLoc; |
| 5592 | CXCursor &BestCursor; |
| 5593 | |
| 5594 | GetCursorData(SourceManager &SM, |
| 5595 | SourceLocation tokenBegin, CXCursor &outputCursor) |
| 5596 | : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) { |
| 5597 | PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin); |
| 5598 | VisitedObjCPropertyImplDecl = false; |
| 5599 | } |
| 5600 | }; |
| 5601 | |
| 5602 | static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, |
| 5603 | CXCursor parent, |
| 5604 | CXClientData client_data) { |
| 5605 | GetCursorData *Data = static_cast<GetCursorData *>(client_data); |
| 5606 | CXCursor *BestCursor = &Data->BestCursor; |
| 5607 | |
| 5608 | // If we point inside a macro argument we should provide info of what the |
| 5609 | // token is so use the actual cursor, don't replace it with a macro expansion |
| 5610 | // cursor. |
| 5611 | if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion) |
| 5612 | return CXChildVisit_Recurse; |
| 5613 | |
| 5614 | if (clang_isDeclaration(cursor.kind)) { |
| 5615 | // Avoid having the implicit methods override the property decls. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5616 | if (const ObjCMethodDecl *MD |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5617 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 5618 | if (MD->isImplicit()) |
| 5619 | return CXChildVisit_Break; |
| 5620 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5621 | } else if (const ObjCInterfaceDecl *ID |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5622 | = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(cursor))) { |
| 5623 | // Check that when we have multiple @class references in the same line, |
| 5624 | // that later ones do not override the previous ones. |
| 5625 | // If we have: |
| 5626 | // @class Foo, Bar; |
| 5627 | // source ranges for both start at '@', so 'Bar' will end up overriding |
| 5628 | // 'Foo' even though the cursor location was at 'Foo'. |
| 5629 | if (BestCursor->kind == CXCursor_ObjCInterfaceDecl || |
| 5630 | BestCursor->kind == CXCursor_ObjCClassRef) |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5631 | if (const ObjCInterfaceDecl *PrevID |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5632 | = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(*BestCursor))){ |
| 5633 | if (PrevID != ID && |
| 5634 | !PrevID->isThisDeclarationADefinition() && |
| 5635 | !ID->isThisDeclarationADefinition()) |
| 5636 | return CXChildVisit_Break; |
| 5637 | } |
| 5638 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5639 | } else if (const DeclaratorDecl *DD |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5640 | = dyn_cast_or_null<DeclaratorDecl>(getCursorDecl(cursor))) { |
| 5641 | SourceLocation StartLoc = DD->getSourceRange().getBegin(); |
| 5642 | // Check that when we have multiple declarators in the same line, |
| 5643 | // that later ones do not override the previous ones. |
| 5644 | // If we have: |
| 5645 | // int Foo, Bar; |
| 5646 | // source ranges for both start at 'int', so 'Bar' will end up overriding |
| 5647 | // 'Foo' even though the cursor location was at 'Foo'. |
| 5648 | if (Data->VisitedDeclaratorDeclStartLoc == StartLoc) |
| 5649 | return CXChildVisit_Break; |
| 5650 | Data->VisitedDeclaratorDeclStartLoc = StartLoc; |
| 5651 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5652 | } else if (const ObjCPropertyImplDecl *PropImp |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5653 | = dyn_cast_or_null<ObjCPropertyImplDecl>(getCursorDecl(cursor))) { |
| 5654 | (void)PropImp; |
| 5655 | // Check that when we have multiple @synthesize in the same line, |
| 5656 | // that later ones do not override the previous ones. |
| 5657 | // If we have: |
| 5658 | // @synthesize Foo, Bar; |
| 5659 | // source ranges for both start at '@', so 'Bar' will end up overriding |
| 5660 | // 'Foo' even though the cursor location was at 'Foo'. |
| 5661 | if (Data->VisitedObjCPropertyImplDecl) |
| 5662 | return CXChildVisit_Break; |
| 5663 | Data->VisitedObjCPropertyImplDecl = true; |
| 5664 | } |
| 5665 | } |
| 5666 | |
| 5667 | if (clang_isExpression(cursor.kind) && |
| 5668 | clang_isDeclaration(BestCursor->kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5669 | if (const Decl *D = getCursorDecl(*BestCursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5670 | // Avoid having the cursor of an expression replace the declaration cursor |
| 5671 | // when the expression source range overlaps the declaration range. |
| 5672 | // This can happen for C++ constructor expressions whose range generally |
| 5673 | // include the variable declaration, e.g.: |
| 5674 | // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor. |
| 5675 | if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() && |
| 5676 | D->getLocation() == Data->TokenBeginLoc) |
| 5677 | return CXChildVisit_Break; |
| 5678 | } |
| 5679 | } |
| 5680 | |
| 5681 | // If our current best cursor is the construction of a temporary object, |
| 5682 | // don't replace that cursor with a type reference, because we want |
| 5683 | // clang_getCursor() to point at the constructor. |
| 5684 | if (clang_isExpression(BestCursor->kind) && |
| 5685 | isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && |
| 5686 | cursor.kind == CXCursor_TypeRef) { |
| 5687 | // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it |
| 5688 | // as having the actual point on the type reference. |
| 5689 | *BestCursor = getTypeRefedCallExprCursor(*BestCursor); |
| 5690 | return CXChildVisit_Recurse; |
| 5691 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 5692 | |
| 5693 | // If we already have an Objective-C superclass reference, don't |
| 5694 | // update it further. |
| 5695 | if (BestCursor->kind == CXCursor_ObjCSuperClassRef) |
| 5696 | return CXChildVisit_Break; |
| 5697 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5698 | *BestCursor = cursor; |
| 5699 | return CXChildVisit_Recurse; |
| 5700 | } |
| 5701 | |
| 5702 | CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 5703 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 5704 | LOG_BAD_TU(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5705 | return clang_getNullCursor(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 5706 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5707 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 5708 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5709 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 5710 | |
| 5711 | SourceLocation SLoc = cxloc::translateSourceLocation(Loc); |
| 5712 | CXCursor Result = cxcursor::getCursor(TU, SLoc); |
| 5713 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5714 | LOG_FUNC_SECTION { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5715 | CXFile SearchFile; |
| 5716 | unsigned SearchLine, SearchColumn; |
| 5717 | CXFile ResultFile; |
| 5718 | unsigned ResultLine, ResultColumn; |
| 5719 | CXString SearchFileName, ResultFileName, KindSpelling, USR; |
| 5720 | const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : ""; |
| 5721 | CXSourceLocation ResultLoc = clang_getCursorLocation(Result); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5722 | |
| 5723 | clang_getFileLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, |
| 5724 | nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5725 | clang_getFileLocation(ResultLoc, &ResultFile, &ResultLine, |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5726 | &ResultColumn, nullptr); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5727 | SearchFileName = clang_getFileName(SearchFile); |
| 5728 | ResultFileName = clang_getFileName(ResultFile); |
| 5729 | KindSpelling = clang_getCursorKindSpelling(Result.kind); |
| 5730 | USR = clang_getCursorUSR(Result); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5731 | *Log << llvm::format("(%s:%d:%d) = %s", |
| 5732 | clang_getCString(SearchFileName), SearchLine, SearchColumn, |
| 5733 | clang_getCString(KindSpelling)) |
| 5734 | << llvm::format("(%s:%d:%d):%s%s", |
| 5735 | clang_getCString(ResultFileName), ResultLine, ResultColumn, |
| 5736 | clang_getCString(USR), IsDef); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5737 | clang_disposeString(SearchFileName); |
| 5738 | clang_disposeString(ResultFileName); |
| 5739 | clang_disposeString(KindSpelling); |
| 5740 | clang_disposeString(USR); |
| 5741 | |
| 5742 | CXCursor Definition = clang_getCursorDefinition(Result); |
| 5743 | if (!clang_equalCursors(Definition, clang_getNullCursor())) { |
| 5744 | CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition); |
| 5745 | CXString DefinitionKindSpelling |
| 5746 | = clang_getCursorKindSpelling(Definition.kind); |
| 5747 | CXFile DefinitionFile; |
| 5748 | unsigned DefinitionLine, DefinitionColumn; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5749 | clang_getFileLocation(DefinitionLoc, &DefinitionFile, |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5750 | &DefinitionLine, &DefinitionColumn, nullptr); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5751 | CXString DefinitionFileName = clang_getFileName(DefinitionFile); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5752 | *Log << llvm::format(" -> %s(%s:%d:%d)", |
| 5753 | clang_getCString(DefinitionKindSpelling), |
| 5754 | clang_getCString(DefinitionFileName), |
| 5755 | DefinitionLine, DefinitionColumn); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5756 | clang_disposeString(DefinitionFileName); |
| 5757 | clang_disposeString(DefinitionKindSpelling); |
| 5758 | } |
| 5759 | } |
| 5760 | |
| 5761 | return Result; |
| 5762 | } |
| 5763 | |
| 5764 | CXCursor clang_getNullCursor(void) { |
| 5765 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
| 5766 | } |
| 5767 | |
| 5768 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5769 | // Clear out the "FirstInDeclGroup" part in a declaration cursor, since we |
| 5770 | // can't set consistently. For example, when visiting a DeclStmt we will set |
| 5771 | // it but we don't set it on the result of clang_getCursorDefinition for |
| 5772 | // a reference of the same declaration. |
| 5773 | // FIXME: Setting "FirstInDeclGroup" in CXCursors is a hack that only works |
| 5774 | // when visiting a DeclStmt currently, the AST should be enhanced to be able |
| 5775 | // to provide that kind of info. |
| 5776 | if (clang_isDeclaration(X.kind)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5777 | X.data[1] = nullptr; |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5778 | if (clang_isDeclaration(Y.kind)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5779 | Y.data[1] = nullptr; |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5780 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5781 | return X == Y; |
| 5782 | } |
| 5783 | |
| 5784 | unsigned clang_hashCursor(CXCursor C) { |
| 5785 | unsigned Index = 0; |
| 5786 | if (clang_isExpression(C.kind) || clang_isStatement(C.kind)) |
| 5787 | Index = 1; |
| 5788 | |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5789 | return llvm::DenseMapInfo<std::pair<unsigned, const void*> >::getHashValue( |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5790 | std::make_pair(C.kind, C.data[Index])); |
| 5791 | } |
| 5792 | |
| 5793 | unsigned clang_isInvalid(enum CXCursorKind K) { |
| 5794 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 5795 | } |
| 5796 | |
| 5797 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
| 5798 | return (K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl) || |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 5799 | (K >= CXCursor_FirstExtraDecl && K <= CXCursor_LastExtraDecl); |
| 5800 | } |
| 5801 | |
Ivan Donchevskii | 08ff910 | 2018-01-04 10:59:50 +0000 | [diff] [blame] | 5802 | unsigned clang_isInvalidDeclaration(CXCursor C) { |
| 5803 | if (clang_isDeclaration(C.kind)) { |
| 5804 | if (const Decl *D = getCursorDecl(C)) |
| 5805 | return D->isInvalidDecl(); |
| 5806 | } |
| 5807 | |
| 5808 | return 0; |
| 5809 | } |
| 5810 | |
Ivan Donchevskii | 1c27b15 | 2018-01-03 10:33:21 +0000 | [diff] [blame] | 5811 | unsigned clang_isReference(enum CXCursorKind K) { |
| 5812 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 5813 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5814 | |
| 5815 | unsigned clang_isExpression(enum CXCursorKind K) { |
| 5816 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; |
| 5817 | } |
| 5818 | |
| 5819 | unsigned clang_isStatement(enum CXCursorKind K) { |
| 5820 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; |
| 5821 | } |
| 5822 | |
| 5823 | unsigned clang_isAttribute(enum CXCursorKind K) { |
| 5824 | return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr; |
| 5825 | } |
| 5826 | |
| 5827 | unsigned clang_isTranslationUnit(enum CXCursorKind K) { |
| 5828 | return K == CXCursor_TranslationUnit; |
| 5829 | } |
| 5830 | |
| 5831 | unsigned clang_isPreprocessing(enum CXCursorKind K) { |
| 5832 | return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; |
| 5833 | } |
| 5834 | |
| 5835 | unsigned clang_isUnexposed(enum CXCursorKind K) { |
| 5836 | switch (K) { |
| 5837 | case CXCursor_UnexposedDecl: |
| 5838 | case CXCursor_UnexposedExpr: |
| 5839 | case CXCursor_UnexposedStmt: |
| 5840 | case CXCursor_UnexposedAttr: |
| 5841 | return true; |
| 5842 | default: |
| 5843 | return false; |
| 5844 | } |
| 5845 | } |
| 5846 | |
| 5847 | CXCursorKind clang_getCursorKind(CXCursor C) { |
| 5848 | return C.kind; |
| 5849 | } |
| 5850 | |
| 5851 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 5852 | if (clang_isReference(C.kind)) { |
| 5853 | switch (C.kind) { |
| 5854 | case CXCursor_ObjCSuperClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5855 | std::pair<const ObjCInterfaceDecl *, SourceLocation> P |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5856 | = getCursorObjCSuperClassRef(C); |
| 5857 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5858 | } |
| 5859 | |
| 5860 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5861 | std::pair<const ObjCProtocolDecl *, SourceLocation> P |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5862 | = getCursorObjCProtocolRef(C); |
| 5863 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5864 | } |
| 5865 | |
| 5866 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5867 | std::pair<const ObjCInterfaceDecl *, SourceLocation> P |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5868 | = getCursorObjCClassRef(C); |
| 5869 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5870 | } |
| 5871 | |
| 5872 | case CXCursor_TypeRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5873 | std::pair<const TypeDecl *, SourceLocation> P = getCursorTypeRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5874 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5875 | } |
| 5876 | |
| 5877 | case CXCursor_TemplateRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5878 | std::pair<const TemplateDecl *, SourceLocation> P = |
| 5879 | getCursorTemplateRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5880 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5881 | } |
| 5882 | |
| 5883 | case CXCursor_NamespaceRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5884 | std::pair<const NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5885 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5886 | } |
| 5887 | |
| 5888 | case CXCursor_MemberRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5889 | std::pair<const FieldDecl *, SourceLocation> P = getCursorMemberRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5890 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5891 | } |
| 5892 | |
| 5893 | case CXCursor_VariableRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5894 | std::pair<const VarDecl *, SourceLocation> P = getCursorVariableRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5895 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5896 | } |
| 5897 | |
| 5898 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5899 | const CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5900 | if (!BaseSpec) |
| 5901 | return clang_getNullLocation(); |
| 5902 | |
| 5903 | if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) |
| 5904 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5905 | TSInfo->getTypeLoc().getBeginLoc()); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5906 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5907 | return cxloc::translateSourceLocation(getCursorContext(C), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5908 | BaseSpec->getBeginLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5909 | } |
| 5910 | |
| 5911 | case CXCursor_LabelRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5912 | std::pair<const LabelStmt *, SourceLocation> P = getCursorLabelRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5913 | return cxloc::translateSourceLocation(getCursorContext(C), P.second); |
| 5914 | } |
| 5915 | |
| 5916 | case CXCursor_OverloadedDeclRef: |
| 5917 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5918 | getCursorOverloadedDeclRef(C).second); |
| 5919 | |
| 5920 | default: |
| 5921 | // FIXME: Need a way to enumerate all non-reference cases. |
| 5922 | llvm_unreachable("Missed a reference kind"); |
| 5923 | } |
| 5924 | } |
| 5925 | |
| 5926 | if (clang_isExpression(C.kind)) |
| 5927 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5928 | getLocationFromExpr(getCursorExpr(C))); |
| 5929 | |
| 5930 | if (clang_isStatement(C.kind)) |
| 5931 | return cxloc::translateSourceLocation(getCursorContext(C), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5932 | getCursorStmt(C)->getBeginLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5933 | |
| 5934 | if (C.kind == CXCursor_PreprocessingDirective) { |
| 5935 | SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); |
| 5936 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5937 | } |
| 5938 | |
| 5939 | if (C.kind == CXCursor_MacroExpansion) { |
| 5940 | SourceLocation L |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 5941 | = cxcursor::getCursorMacroExpansion(C).getSourceRange().getBegin(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5942 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5943 | } |
| 5944 | |
| 5945 | if (C.kind == CXCursor_MacroDefinition) { |
| 5946 | SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); |
| 5947 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5948 | } |
| 5949 | |
| 5950 | if (C.kind == CXCursor_InclusionDirective) { |
| 5951 | SourceLocation L |
| 5952 | = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); |
| 5953 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5954 | } |
| 5955 | |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 5956 | if (clang_isAttribute(C.kind)) { |
| 5957 | SourceLocation L |
| 5958 | = cxcursor::getCursorAttr(C)->getLocation(); |
| 5959 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5960 | } |
| 5961 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5962 | if (!clang_isDeclaration(C.kind)) |
| 5963 | return clang_getNullLocation(); |
| 5964 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5965 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5966 | if (!D) |
| 5967 | return clang_getNullLocation(); |
| 5968 | |
| 5969 | SourceLocation Loc = D->getLocation(); |
| 5970 | // FIXME: Multiple variables declared in a single declaration |
| 5971 | // currently lack the information needed to correctly determine their |
| 5972 | // ranges when accounting for the type-specifier. We use context |
| 5973 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 5974 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5975 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5976 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 5977 | Loc = VD->getLocation(); |
| 5978 | } |
| 5979 | |
| 5980 | // For ObjC methods, give the start location of the method name. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5981 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5982 | Loc = MD->getSelectorStartLoc(); |
| 5983 | |
| 5984 | return cxloc::translateSourceLocation(getCursorContext(C), Loc); |
| 5985 | } |
| 5986 | |
NAKAMURA Takumi | a01f4c3 | 2016-12-19 16:50:43 +0000 | [diff] [blame] | 5987 | } // end extern "C" |
| 5988 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5989 | CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) { |
| 5990 | assert(TU); |
| 5991 | |
| 5992 | // Guard against an invalid SourceLocation, or we may assert in one |
| 5993 | // of the following calls. |
| 5994 | if (SLoc.isInvalid()) |
| 5995 | return clang_getNullCursor(); |
| 5996 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 5997 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5998 | |
| 5999 | // Translate the given source location to make it point at the beginning of |
| 6000 | // the token under the cursor. |
| 6001 | SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), |
| 6002 | CXXUnit->getASTContext().getLangOpts()); |
| 6003 | |
| 6004 | CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 6005 | if (SLoc.isValid()) { |
| 6006 | GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result); |
| 6007 | CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData, |
| 6008 | /*VisitPreprocessorLast=*/true, |
| 6009 | /*VisitIncludedEntities=*/false, |
| 6010 | SourceLocation(SLoc)); |
| 6011 | CursorVis.visitFileRegion(); |
| 6012 | } |
| 6013 | |
| 6014 | return Result; |
| 6015 | } |
| 6016 | |
| 6017 | static SourceRange getRawCursorExtent(CXCursor C) { |
| 6018 | if (clang_isReference(C.kind)) { |
| 6019 | switch (C.kind) { |
| 6020 | case CXCursor_ObjCSuperClassRef: |
| 6021 | return getCursorObjCSuperClassRef(C).second; |
| 6022 | |
| 6023 | case CXCursor_ObjCProtocolRef: |
| 6024 | return getCursorObjCProtocolRef(C).second; |
| 6025 | |
| 6026 | case CXCursor_ObjCClassRef: |
| 6027 | return getCursorObjCClassRef(C).second; |
| 6028 | |
| 6029 | case CXCursor_TypeRef: |
| 6030 | return getCursorTypeRef(C).second; |
| 6031 | |
| 6032 | case CXCursor_TemplateRef: |
| 6033 | return getCursorTemplateRef(C).second; |
| 6034 | |
| 6035 | case CXCursor_NamespaceRef: |
| 6036 | return getCursorNamespaceRef(C).second; |
| 6037 | |
| 6038 | case CXCursor_MemberRef: |
| 6039 | return getCursorMemberRef(C).second; |
| 6040 | |
| 6041 | case CXCursor_CXXBaseSpecifier: |
| 6042 | return getCursorCXXBaseSpecifier(C)->getSourceRange(); |
| 6043 | |
| 6044 | case CXCursor_LabelRef: |
| 6045 | return getCursorLabelRef(C).second; |
| 6046 | |
| 6047 | case CXCursor_OverloadedDeclRef: |
| 6048 | return getCursorOverloadedDeclRef(C).second; |
| 6049 | |
| 6050 | case CXCursor_VariableRef: |
| 6051 | return getCursorVariableRef(C).second; |
| 6052 | |
| 6053 | default: |
| 6054 | // FIXME: Need a way to enumerate all non-reference cases. |
| 6055 | llvm_unreachable("Missed a reference kind"); |
| 6056 | } |
| 6057 | } |
| 6058 | |
| 6059 | if (clang_isExpression(C.kind)) |
| 6060 | return getCursorExpr(C)->getSourceRange(); |
| 6061 | |
| 6062 | if (clang_isStatement(C.kind)) |
| 6063 | return getCursorStmt(C)->getSourceRange(); |
| 6064 | |
| 6065 | if (clang_isAttribute(C.kind)) |
| 6066 | return getCursorAttr(C)->getRange(); |
| 6067 | |
| 6068 | if (C.kind == CXCursor_PreprocessingDirective) |
| 6069 | return cxcursor::getCursorPreprocessingDirective(C); |
| 6070 | |
| 6071 | if (C.kind == CXCursor_MacroExpansion) { |
| 6072 | ASTUnit *TU = getCursorASTUnit(C); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 6073 | SourceRange Range = cxcursor::getCursorMacroExpansion(C).getSourceRange(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6074 | return TU->mapRangeFromPreamble(Range); |
| 6075 | } |
| 6076 | |
| 6077 | if (C.kind == CXCursor_MacroDefinition) { |
| 6078 | ASTUnit *TU = getCursorASTUnit(C); |
| 6079 | SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange(); |
| 6080 | return TU->mapRangeFromPreamble(Range); |
| 6081 | } |
| 6082 | |
| 6083 | if (C.kind == CXCursor_InclusionDirective) { |
| 6084 | ASTUnit *TU = getCursorASTUnit(C); |
| 6085 | SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange(); |
| 6086 | return TU->mapRangeFromPreamble(Range); |
| 6087 | } |
| 6088 | |
| 6089 | if (C.kind == CXCursor_TranslationUnit) { |
| 6090 | ASTUnit *TU = getCursorASTUnit(C); |
| 6091 | FileID MainID = TU->getSourceManager().getMainFileID(); |
| 6092 | SourceLocation Start = TU->getSourceManager().getLocForStartOfFile(MainID); |
| 6093 | SourceLocation End = TU->getSourceManager().getLocForEndOfFile(MainID); |
| 6094 | return SourceRange(Start, End); |
| 6095 | } |
| 6096 | |
| 6097 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6098 | const Decl *D = cxcursor::getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6099 | if (!D) |
| 6100 | return SourceRange(); |
| 6101 | |
| 6102 | SourceRange R = D->getSourceRange(); |
| 6103 | // FIXME: Multiple variables declared in a single declaration |
| 6104 | // currently lack the information needed to correctly determine their |
| 6105 | // ranges when accounting for the type-specifier. We use context |
| 6106 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 6107 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6108 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6109 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 6110 | R.setBegin(VD->getLocation()); |
| 6111 | } |
| 6112 | return R; |
| 6113 | } |
| 6114 | return SourceRange(); |
| 6115 | } |
| 6116 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6117 | /// Retrieves the "raw" cursor extent, which is then extended to include |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6118 | /// the decl-specifier-seq for declarations. |
| 6119 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) { |
| 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 | |
| 6127 | // Adjust the start of the location for declarations preceded by |
| 6128 | // declaration specifiers. |
| 6129 | SourceLocation StartLoc; |
| 6130 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
| 6131 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6132 | StartLoc = TI->getTypeLoc().getBeginLoc(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6133 | } else if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6134 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 6135 | StartLoc = TI->getTypeLoc().getBeginLoc(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6136 | } |
| 6137 | |
| 6138 | if (StartLoc.isValid() && R.getBegin().isValid() && |
| 6139 | SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin())) |
| 6140 | R.setBegin(StartLoc); |
| 6141 | |
| 6142 | // FIXME: Multiple variables declared in a single declaration |
| 6143 | // currently lack the information needed to correctly determine their |
| 6144 | // ranges when accounting for the type-specifier. We use context |
| 6145 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 6146 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6147 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6148 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 6149 | R.setBegin(VD->getLocation()); |
| 6150 | } |
| 6151 | |
| 6152 | return R; |
| 6153 | } |
| 6154 | |
| 6155 | return getRawCursorExtent(C); |
| 6156 | } |
| 6157 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6158 | CXSourceRange clang_getCursorExtent(CXCursor C) { |
| 6159 | SourceRange R = getRawCursorExtent(C); |
| 6160 | if (R.isInvalid()) |
| 6161 | return clang_getNullRange(); |
| 6162 | |
| 6163 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 6164 | } |
| 6165 | |
| 6166 | CXCursor clang_getCursorReferenced(CXCursor C) { |
| 6167 | if (clang_isInvalid(C.kind)) |
| 6168 | return clang_getNullCursor(); |
| 6169 | |
| 6170 | CXTranslationUnit tu = getCursorTU(C); |
| 6171 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6172 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6173 | if (!D) |
| 6174 | return clang_getNullCursor(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6175 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6176 | return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6177 | if (const ObjCPropertyImplDecl *PropImpl = |
| 6178 | dyn_cast<ObjCPropertyImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6179 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 6180 | return MakeCXCursor(Property, tu); |
| 6181 | |
| 6182 | return C; |
| 6183 | } |
| 6184 | |
| 6185 | if (clang_isExpression(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6186 | const Expr *E = getCursorExpr(C); |
| 6187 | const Decl *D = getDeclFromExpr(E); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6188 | if (D) { |
| 6189 | CXCursor declCursor = MakeCXCursor(D, tu); |
| 6190 | declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C), |
| 6191 | declCursor); |
| 6192 | return declCursor; |
| 6193 | } |
| 6194 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6195 | if (const OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6196 | return MakeCursorOverloadedDeclRef(Ovl, tu); |
| 6197 | |
| 6198 | return clang_getNullCursor(); |
| 6199 | } |
| 6200 | |
| 6201 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6202 | const Stmt *S = getCursorStmt(C); |
| 6203 | if (const GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6204 | if (LabelDecl *label = Goto->getLabel()) |
| 6205 | if (LabelStmt *labelS = label->getStmt()) |
| 6206 | return MakeCXCursor(labelS, getCursorDecl(C), tu); |
| 6207 | |
| 6208 | return clang_getNullCursor(); |
| 6209 | } |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 6210 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6211 | if (C.kind == CXCursor_MacroExpansion) { |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 6212 | if (const MacroDefinitionRecord *Def = |
| 6213 | getCursorMacroExpansion(C).getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6214 | return MakeMacroDefinitionCursor(Def, tu); |
| 6215 | } |
| 6216 | |
| 6217 | if (!clang_isReference(C.kind)) |
| 6218 | return clang_getNullCursor(); |
| 6219 | |
| 6220 | switch (C.kind) { |
| 6221 | case CXCursor_ObjCSuperClassRef: |
| 6222 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu); |
| 6223 | |
| 6224 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 6225 | const ObjCProtocolDecl *Prot = getCursorObjCProtocolRef(C).first; |
| 6226 | if (const ObjCProtocolDecl *Def = Prot->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6227 | return MakeCXCursor(Def, tu); |
| 6228 | |
| 6229 | return MakeCXCursor(Prot, tu); |
| 6230 | } |
| 6231 | |
| 6232 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 6233 | const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
| 6234 | if (const ObjCInterfaceDecl *Def = Class->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6235 | return MakeCXCursor(Def, tu); |
| 6236 | |
| 6237 | return MakeCXCursor(Class, tu); |
| 6238 | } |
| 6239 | |
| 6240 | case CXCursor_TypeRef: |
| 6241 | return MakeCXCursor(getCursorTypeRef(C).first, tu ); |
| 6242 | |
| 6243 | case CXCursor_TemplateRef: |
| 6244 | return MakeCXCursor(getCursorTemplateRef(C).first, tu ); |
| 6245 | |
| 6246 | case CXCursor_NamespaceRef: |
| 6247 | return MakeCXCursor(getCursorNamespaceRef(C).first, tu ); |
| 6248 | |
| 6249 | case CXCursor_MemberRef: |
| 6250 | return MakeCXCursor(getCursorMemberRef(C).first, tu ); |
| 6251 | |
| 6252 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 6253 | const CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6254 | return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), |
| 6255 | tu )); |
| 6256 | } |
| 6257 | |
| 6258 | case CXCursor_LabelRef: |
| 6259 | // FIXME: We end up faking the "parent" declaration here because we |
| 6260 | // don't want to make CXCursor larger. |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6261 | return MakeCXCursor(getCursorLabelRef(C).first, |
| 6262 | cxtu::getASTUnit(tu)->getASTContext() |
| 6263 | .getTranslationUnitDecl(), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6264 | tu); |
| 6265 | |
| 6266 | case CXCursor_OverloadedDeclRef: |
| 6267 | return C; |
| 6268 | |
| 6269 | case CXCursor_VariableRef: |
| 6270 | return MakeCXCursor(getCursorVariableRef(C).first, tu); |
| 6271 | |
| 6272 | default: |
| 6273 | // We would prefer to enumerate all non-reference cursor kinds here. |
| 6274 | llvm_unreachable("Unhandled reference cursor kind"); |
| 6275 | } |
| 6276 | } |
| 6277 | |
| 6278 | CXCursor clang_getCursorDefinition(CXCursor C) { |
| 6279 | if (clang_isInvalid(C.kind)) |
| 6280 | return clang_getNullCursor(); |
| 6281 | |
| 6282 | CXTranslationUnit TU = getCursorTU(C); |
| 6283 | |
| 6284 | bool WasReference = false; |
| 6285 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { |
| 6286 | C = clang_getCursorReferenced(C); |
| 6287 | WasReference = true; |
| 6288 | } |
| 6289 | |
| 6290 | if (C.kind == CXCursor_MacroExpansion) |
| 6291 | return clang_getCursorReferenced(C); |
| 6292 | |
| 6293 | if (!clang_isDeclaration(C.kind)) |
| 6294 | return clang_getNullCursor(); |
| 6295 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6296 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6297 | if (!D) |
| 6298 | return clang_getNullCursor(); |
| 6299 | |
| 6300 | switch (D->getKind()) { |
| 6301 | // Declaration kinds that don't really separate the notions of |
| 6302 | // declaration and definition. |
| 6303 | case Decl::Namespace: |
| 6304 | case Decl::Typedef: |
| 6305 | case Decl::TypeAlias: |
| 6306 | case Decl::TypeAliasTemplate: |
| 6307 | case Decl::TemplateTypeParm: |
| 6308 | case Decl::EnumConstant: |
| 6309 | case Decl::Field: |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 6310 | case Decl::Binding: |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 6311 | case Decl::MSProperty: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6312 | case Decl::IndirectField: |
| 6313 | case Decl::ObjCIvar: |
| 6314 | case Decl::ObjCAtDefsField: |
| 6315 | case Decl::ImplicitParam: |
| 6316 | case Decl::ParmVar: |
| 6317 | case Decl::NonTypeTemplateParm: |
| 6318 | case Decl::TemplateTemplateParm: |
| 6319 | case Decl::ObjCCategoryImpl: |
| 6320 | case Decl::ObjCImplementation: |
| 6321 | case Decl::AccessSpec: |
| 6322 | case Decl::LinkageSpec: |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 6323 | case Decl::Export: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6324 | case Decl::ObjCPropertyImpl: |
| 6325 | case Decl::FileScopeAsm: |
| 6326 | case Decl::StaticAssert: |
| 6327 | case Decl::Block: |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 6328 | case Decl::Captured: |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 6329 | case Decl::OMPCapturedExpr: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6330 | case Decl::Label: // FIXME: Is this right?? |
| 6331 | case Decl::ClassScopeFunctionSpecialization: |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 6332 | case Decl::CXXDeductionGuide: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6333 | case Decl::Import: |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 6334 | case Decl::OMPThreadPrivate: |
Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 6335 | case Decl::OMPAllocate: |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 6336 | case Decl::OMPDeclareReduction: |
Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 6337 | case Decl::OMPDeclareMapper: |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 6338 | case Decl::OMPRequires: |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 6339 | case Decl::ObjCTypeParam: |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 6340 | case Decl::BuiltinTemplate: |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 6341 | case Decl::PragmaComment: |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 6342 | case Decl::PragmaDetectMismatch: |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 6343 | case Decl::UsingPack: |
Saar Raz | d7aae33 | 2019-07-10 21:25:49 +0000 | [diff] [blame] | 6344 | case Decl::Concept: |
Tyker | b0561b3 | 2019-11-17 11:41:55 +0100 | [diff] [blame] | 6345 | case Decl::LifetimeExtendedTemporary: |
Saar Raz | a0f50d7 | 2020-01-18 09:11:43 +0200 | [diff] [blame] | 6346 | case Decl::RequiresExprBody: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6347 | return C; |
| 6348 | |
| 6349 | // Declaration kinds that don't make any sense here, but are |
| 6350 | // nonetheless harmless. |
David Blaikie | f005d3c | 2013-02-22 17:44:58 +0000 | [diff] [blame] | 6351 | case Decl::Empty: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6352 | case Decl::TranslationUnit: |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 6353 | case Decl::ExternCContext: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6354 | break; |
| 6355 | |
| 6356 | // Declaration kinds for which the definition is not resolvable. |
| 6357 | case Decl::UnresolvedUsingTypename: |
| 6358 | case Decl::UnresolvedUsingValue: |
| 6359 | break; |
| 6360 | |
| 6361 | case Decl::UsingDirective: |
| 6362 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), |
| 6363 | TU); |
| 6364 | |
| 6365 | case Decl::NamespaceAlias: |
| 6366 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU); |
| 6367 | |
| 6368 | case Decl::Enum: |
| 6369 | case Decl::Record: |
| 6370 | case Decl::CXXRecord: |
| 6371 | case Decl::ClassTemplateSpecialization: |
| 6372 | case Decl::ClassTemplatePartialSpecialization: |
| 6373 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) |
| 6374 | return MakeCXCursor(Def, TU); |
| 6375 | return clang_getNullCursor(); |
| 6376 | |
| 6377 | case Decl::Function: |
| 6378 | case Decl::CXXMethod: |
| 6379 | case Decl::CXXConstructor: |
| 6380 | case Decl::CXXDestructor: |
| 6381 | case Decl::CXXConversion: { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6382 | const FunctionDecl *Def = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6383 | if (cast<FunctionDecl>(D)->getBody(Def)) |
Dmitri Gribenko | 9c256e3 | 2013-01-14 00:46:27 +0000 | [diff] [blame] | 6384 | return MakeCXCursor(Def, TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6385 | return clang_getNullCursor(); |
| 6386 | } |
| 6387 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6388 | case Decl::Var: |
| 6389 | case Decl::VarTemplateSpecialization: |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 6390 | case Decl::VarTemplatePartialSpecialization: |
| 6391 | case Decl::Decomposition: { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6392 | // Ask the variable if it has a definition. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6393 | if (const VarDecl *Def = cast<VarDecl>(D)->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6394 | return MakeCXCursor(Def, TU); |
| 6395 | return clang_getNullCursor(); |
| 6396 | } |
| 6397 | |
| 6398 | case Decl::FunctionTemplate: { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6399 | const FunctionDecl *Def = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6400 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) |
| 6401 | return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU); |
| 6402 | return clang_getNullCursor(); |
| 6403 | } |
| 6404 | |
| 6405 | case Decl::ClassTemplate: { |
| 6406 | if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() |
| 6407 | ->getDefinition()) |
| 6408 | return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), |
| 6409 | TU); |
| 6410 | return clang_getNullCursor(); |
| 6411 | } |
| 6412 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 6413 | case Decl::VarTemplate: { |
| 6414 | if (VarDecl *Def = |
| 6415 | cast<VarTemplateDecl>(D)->getTemplatedDecl()->getDefinition()) |
| 6416 | return MakeCXCursor(cast<VarDecl>(Def)->getDescribedVarTemplate(), TU); |
| 6417 | return clang_getNullCursor(); |
| 6418 | } |
| 6419 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6420 | case Decl::Using: |
| 6421 | return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), |
| 6422 | D->getLocation(), TU); |
| 6423 | |
| 6424 | case Decl::UsingShadow: |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6425 | case Decl::ConstructorUsingShadow: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6426 | return clang_getCursorDefinition( |
| 6427 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), |
| 6428 | TU)); |
| 6429 | |
| 6430 | case Decl::ObjCMethod: { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6431 | const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6432 | if (Method->isThisDeclarationADefinition()) |
| 6433 | return C; |
| 6434 | |
| 6435 | // Dig out the method definition in the associated |
| 6436 | // @implementation, if we have it. |
| 6437 | // FIXME: The ASTs should make finding the definition easier. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6438 | if (const ObjCInterfaceDecl *Class |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6439 | = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) |
| 6440 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) |
| 6441 | if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), |
| 6442 | Method->isInstanceMethod())) |
| 6443 | if (Def->isThisDeclarationADefinition()) |
| 6444 | return MakeCXCursor(Def, TU); |
| 6445 | |
| 6446 | return clang_getNullCursor(); |
| 6447 | } |
| 6448 | |
| 6449 | case Decl::ObjCCategory: |
| 6450 | if (ObjCCategoryImplDecl *Impl |
| 6451 | = cast<ObjCCategoryDecl>(D)->getImplementation()) |
| 6452 | return MakeCXCursor(Impl, TU); |
| 6453 | return clang_getNullCursor(); |
| 6454 | |
| 6455 | case Decl::ObjCProtocol: |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6456 | if (const ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(D)->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6457 | return MakeCXCursor(Def, TU); |
| 6458 | return clang_getNullCursor(); |
| 6459 | |
| 6460 | case Decl::ObjCInterface: { |
| 6461 | // There are two notions of a "definition" for an Objective-C |
| 6462 | // class: the interface and its implementation. When we resolved a |
| 6463 | // reference to an Objective-C class, produce the @interface as |
| 6464 | // the definition; when we were provided with the interface, |
| 6465 | // produce the @implementation as the definition. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6466 | const ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6467 | if (WasReference) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6468 | if (const ObjCInterfaceDecl *Def = IFace->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6469 | return MakeCXCursor(Def, TU); |
| 6470 | } else if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
| 6471 | return MakeCXCursor(Impl, TU); |
| 6472 | return clang_getNullCursor(); |
| 6473 | } |
| 6474 | |
| 6475 | case Decl::ObjCProperty: |
| 6476 | // FIXME: We don't really know where to find the |
| 6477 | // ObjCPropertyImplDecls that implement this property. |
| 6478 | return clang_getNullCursor(); |
| 6479 | |
| 6480 | case Decl::ObjCCompatibleAlias: |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6481 | if (const ObjCInterfaceDecl *Class |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6482 | = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6483 | if (const ObjCInterfaceDecl *Def = Class->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6484 | return MakeCXCursor(Def, TU); |
| 6485 | |
| 6486 | return clang_getNullCursor(); |
| 6487 | |
| 6488 | case Decl::Friend: |
| 6489 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) |
| 6490 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
| 6491 | return clang_getNullCursor(); |
| 6492 | |
| 6493 | case Decl::FriendTemplate: |
| 6494 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) |
| 6495 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
| 6496 | return clang_getNullCursor(); |
| 6497 | } |
| 6498 | |
| 6499 | return clang_getNullCursor(); |
| 6500 | } |
| 6501 | |
| 6502 | unsigned clang_isCursorDefinition(CXCursor C) { |
| 6503 | if (!clang_isDeclaration(C.kind)) |
| 6504 | return 0; |
| 6505 | |
| 6506 | return clang_getCursorDefinition(C) == C; |
| 6507 | } |
| 6508 | |
| 6509 | CXCursor clang_getCanonicalCursor(CXCursor C) { |
| 6510 | if (!clang_isDeclaration(C.kind)) |
| 6511 | return C; |
| 6512 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6513 | if (const Decl *D = getCursorDecl(C)) { |
| 6514 | if (const ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6515 | if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl()) |
| 6516 | return MakeCXCursor(CatD, getCursorTU(C)); |
| 6517 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6518 | if (const ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) |
| 6519 | if (const ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6520 | return MakeCXCursor(IFD, getCursorTU(C)); |
| 6521 | |
| 6522 | return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C)); |
| 6523 | } |
| 6524 | |
| 6525 | return C; |
| 6526 | } |
| 6527 | |
| 6528 | int clang_Cursor_getObjCSelectorIndex(CXCursor cursor) { |
| 6529 | return cxcursor::getSelectorIdentifierIndexAndLoc(cursor).first; |
| 6530 | } |
| 6531 | |
| 6532 | unsigned clang_getNumOverloadedDecls(CXCursor C) { |
| 6533 | if (C.kind != CXCursor_OverloadedDeclRef) |
| 6534 | return 0; |
| 6535 | |
| 6536 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6537 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6538 | return E->getNumDecls(); |
| 6539 | |
| 6540 | if (OverloadedTemplateStorage *S |
| 6541 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 6542 | return S->size(); |
| 6543 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6544 | const Decl *D = Storage.get<const Decl *>(); |
| 6545 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6546 | return Using->shadow_size(); |
| 6547 | |
| 6548 | return 0; |
| 6549 | } |
| 6550 | |
| 6551 | CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { |
| 6552 | if (cursor.kind != CXCursor_OverloadedDeclRef) |
| 6553 | return clang_getNullCursor(); |
| 6554 | |
| 6555 | if (index >= clang_getNumOverloadedDecls(cursor)) |
| 6556 | return clang_getNullCursor(); |
| 6557 | |
| 6558 | CXTranslationUnit TU = getCursorTU(cursor); |
| 6559 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).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 MakeCXCursor(E->decls_begin()[index], TU); |
| 6562 | |
| 6563 | if (OverloadedTemplateStorage *S |
| 6564 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 6565 | return MakeCXCursor(S->begin()[index], TU); |
| 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 | // FIXME: This is, unfortunately, linear time. |
| 6570 | UsingDecl::shadow_iterator Pos = Using->shadow_begin(); |
| 6571 | std::advance(Pos, index); |
| 6572 | return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU); |
| 6573 | } |
| 6574 | |
| 6575 | return clang_getNullCursor(); |
| 6576 | } |
| 6577 | |
| 6578 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
| 6579 | const char **startBuf, |
| 6580 | const char **endBuf, |
| 6581 | unsigned *startLine, |
| 6582 | unsigned *startColumn, |
| 6583 | unsigned *endLine, |
| 6584 | unsigned *endColumn) { |
| 6585 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6586 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(getCursorDecl(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6587 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
| 6588 | |
| 6589 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 6590 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 6591 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 6592 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 6593 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 6594 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 6595 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 6596 | } |
| 6597 | |
| 6598 | |
| 6599 | CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags, |
| 6600 | unsigned PieceIndex) { |
| 6601 | RefNamePieces Pieces; |
| 6602 | |
| 6603 | switch (C.kind) { |
| 6604 | case CXCursor_MemberRefExpr: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6605 | if (const MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6606 | Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(), |
| 6607 | E->getQualifierLoc().getSourceRange()); |
| 6608 | break; |
| 6609 | |
| 6610 | case CXCursor_DeclRefExpr: |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 6611 | if (const DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C))) { |
| 6612 | SourceRange TemplateArgLoc(E->getLAngleLoc(), E->getRAngleLoc()); |
| 6613 | Pieces = |
| 6614 | buildPieces(NameFlags, false, E->getNameInfo(), |
| 6615 | E->getQualifierLoc().getSourceRange(), &TemplateArgLoc); |
| 6616 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6617 | break; |
| 6618 | |
| 6619 | case CXCursor_CallExpr: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6620 | if (const CXXOperatorCallExpr *OCE = |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6621 | dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6622 | const Expr *Callee = OCE->getCallee(); |
| 6623 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6624 | Callee = ICE->getSubExpr(); |
| 6625 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6626 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6627 | Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(), |
| 6628 | DRE->getQualifierLoc().getSourceRange()); |
| 6629 | } |
| 6630 | break; |
| 6631 | |
| 6632 | default: |
| 6633 | break; |
| 6634 | } |
| 6635 | |
| 6636 | if (Pieces.empty()) { |
| 6637 | if (PieceIndex == 0) |
| 6638 | return clang_getCursorExtent(C); |
| 6639 | } else if (PieceIndex < Pieces.size()) { |
| 6640 | SourceRange R = Pieces[PieceIndex]; |
| 6641 | if (R.isValid()) |
| 6642 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 6643 | } |
| 6644 | |
| 6645 | return clang_getNullRange(); |
| 6646 | } |
| 6647 | |
| 6648 | void clang_enableStackTraces(void) { |
Richard Smith | dfed58a | 2016-06-09 00:53:41 +0000 | [diff] [blame] | 6649 | // FIXME: Provide an argv0 here so we can find llvm-symbolizer. |
| 6650 | llvm::sys::PrintStackTraceOnErrorSignal(StringRef()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6651 | } |
| 6652 | |
| 6653 | void clang_executeOnThread(void (*fn)(void*), void *user_data, |
| 6654 | unsigned stack_size) { |
Alexandre Ganea | 471d060 | 2019-11-29 10:52:13 -0500 | [diff] [blame] | 6655 | llvm::llvm_execute_on_thread(fn, user_data, |
| 6656 | stack_size == 0 |
| 6657 | ? clang::DesiredStackSize |
| 6658 | : llvm::Optional<unsigned>(stack_size)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6659 | } |
| 6660 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6661 | //===----------------------------------------------------------------------===// |
| 6662 | // Token-based Operations. |
| 6663 | //===----------------------------------------------------------------------===// |
| 6664 | |
| 6665 | /* CXToken layout: |
| 6666 | * int_data[0]: a CXTokenKind |
| 6667 | * int_data[1]: starting token location |
| 6668 | * int_data[2]: token length |
| 6669 | * int_data[3]: reserved |
| 6670 | * ptr_data: for identifiers and keywords, an IdentifierInfo*. |
| 6671 | * otherwise unused. |
| 6672 | */ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6673 | CXTokenKind clang_getTokenKind(CXToken CXTok) { |
| 6674 | return static_cast<CXTokenKind>(CXTok.int_data[0]); |
| 6675 | } |
| 6676 | |
| 6677 | CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { |
| 6678 | switch (clang_getTokenKind(CXTok)) { |
| 6679 | case CXToken_Identifier: |
| 6680 | case CXToken_Keyword: |
| 6681 | // We know we have an IdentifierInfo*, so use that. |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 6682 | return cxstring::createRef(static_cast<IdentifierInfo *>(CXTok.ptr_data) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6683 | ->getNameStart()); |
| 6684 | |
| 6685 | case CXToken_Literal: { |
| 6686 | // We have stashed the starting pointer in the ptr_data field. Use it. |
| 6687 | const char *Text = static_cast<const char *>(CXTok.ptr_data); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 6688 | return cxstring::createDup(StringRef(Text, CXTok.int_data[2])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6689 | } |
| 6690 | |
| 6691 | case CXToken_Punctuation: |
| 6692 | case CXToken_Comment: |
| 6693 | break; |
| 6694 | } |
| 6695 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6696 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6697 | LOG_BAD_TU(TU); |
| 6698 | return cxstring::createEmpty(); |
| 6699 | } |
| 6700 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6701 | // We have to find the starting buffer pointer the hard way, by |
| 6702 | // deconstructing the source location. |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6703 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6704 | if (!CXXUnit) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 6705 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6706 | |
| 6707 | SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); |
| 6708 | std::pair<FileID, unsigned> LocInfo |
| 6709 | = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc); |
| 6710 | bool Invalid = false; |
| 6711 | StringRef Buffer |
| 6712 | = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); |
| 6713 | if (Invalid) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 6714 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6715 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 6716 | return cxstring::createDup(Buffer.substr(LocInfo.second, CXTok.int_data[2])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6717 | } |
| 6718 | |
| 6719 | CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6720 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6721 | LOG_BAD_TU(TU); |
| 6722 | return clang_getNullLocation(); |
| 6723 | } |
| 6724 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6725 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6726 | if (!CXXUnit) |
| 6727 | return clang_getNullLocation(); |
| 6728 | |
| 6729 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), |
| 6730 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 6731 | } |
| 6732 | |
| 6733 | CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6734 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6735 | LOG_BAD_TU(TU); |
| 6736 | return clang_getNullRange(); |
| 6737 | } |
| 6738 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6739 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6740 | if (!CXXUnit) |
| 6741 | return clang_getNullRange(); |
| 6742 | |
| 6743 | return cxloc::translateSourceRange(CXXUnit->getASTContext(), |
| 6744 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 6745 | } |
| 6746 | |
| 6747 | static void getTokens(ASTUnit *CXXUnit, SourceRange Range, |
| 6748 | SmallVectorImpl<CXToken> &CXTokens) { |
| 6749 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 6750 | std::pair<FileID, unsigned> BeginLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6751 | = SourceMgr.getDecomposedSpellingLoc(Range.getBegin()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6752 | std::pair<FileID, unsigned> EndLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6753 | = SourceMgr.getDecomposedSpellingLoc(Range.getEnd()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6754 | |
| 6755 | // Cannot tokenize across files. |
| 6756 | if (BeginLocInfo.first != EndLocInfo.first) |
| 6757 | return; |
| 6758 | |
| 6759 | // Create a lexer |
| 6760 | bool Invalid = false; |
| 6761 | StringRef Buffer |
| 6762 | = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 6763 | if (Invalid) |
| 6764 | return; |
| 6765 | |
| 6766 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 6767 | CXXUnit->getASTContext().getLangOpts(), |
| 6768 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); |
| 6769 | Lex.SetCommentRetentionState(true); |
| 6770 | |
| 6771 | // Lex tokens until we hit the end of the range. |
| 6772 | const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; |
| 6773 | Token Tok; |
| 6774 | bool previousWasAt = false; |
| 6775 | do { |
| 6776 | // Lex the next token |
| 6777 | Lex.LexFromRawLexer(Tok); |
| 6778 | if (Tok.is(tok::eof)) |
| 6779 | break; |
| 6780 | |
| 6781 | // Initialize the CXToken. |
| 6782 | CXToken CXTok; |
| 6783 | |
| 6784 | // - Common fields |
| 6785 | CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); |
| 6786 | CXTok.int_data[2] = Tok.getLength(); |
| 6787 | CXTok.int_data[3] = 0; |
| 6788 | |
| 6789 | // - Kind-specific fields |
| 6790 | if (Tok.isLiteral()) { |
| 6791 | CXTok.int_data[0] = CXToken_Literal; |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 6792 | CXTok.ptr_data = const_cast<char *>(Tok.getLiteralData()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6793 | } else if (Tok.is(tok::raw_identifier)) { |
| 6794 | // Lookup the identifier to determine whether we have a keyword. |
| 6795 | IdentifierInfo *II |
| 6796 | = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok); |
| 6797 | |
| 6798 | if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { |
| 6799 | CXTok.int_data[0] = CXToken_Keyword; |
| 6800 | } |
| 6801 | else { |
| 6802 | CXTok.int_data[0] = Tok.is(tok::identifier) |
| 6803 | ? CXToken_Identifier |
| 6804 | : CXToken_Keyword; |
| 6805 | } |
| 6806 | CXTok.ptr_data = II; |
| 6807 | } else if (Tok.is(tok::comment)) { |
| 6808 | CXTok.int_data[0] = CXToken_Comment; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6809 | CXTok.ptr_data = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6810 | } else { |
| 6811 | CXTok.int_data[0] = CXToken_Punctuation; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6812 | CXTok.ptr_data = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6813 | } |
| 6814 | CXTokens.push_back(CXTok); |
| 6815 | previousWasAt = Tok.is(tok::at); |
Argyrios Kyrtzidis | c7c6a07 | 2016-11-09 23:58:39 +0000 | [diff] [blame] | 6816 | } while (Lex.getBufferLocation() < EffectiveBufferEnd); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6817 | } |
| 6818 | |
Ivan Donchevskii | 3957e48 | 2018-06-13 12:37:08 +0000 | [diff] [blame] | 6819 | CXToken *clang_getToken(CXTranslationUnit TU, CXSourceLocation Location) { |
| 6820 | LOG_FUNC_SECTION { |
| 6821 | *Log << TU << ' ' << Location; |
| 6822 | } |
| 6823 | |
| 6824 | if (isNotUsableTU(TU)) { |
| 6825 | LOG_BAD_TU(TU); |
| 6826 | return NULL; |
| 6827 | } |
| 6828 | |
| 6829 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
| 6830 | if (!CXXUnit) |
| 6831 | return NULL; |
| 6832 | |
| 6833 | SourceLocation Begin = cxloc::translateSourceLocation(Location); |
| 6834 | if (Begin.isInvalid()) |
| 6835 | return NULL; |
| 6836 | SourceManager &SM = CXXUnit->getSourceManager(); |
| 6837 | std::pair<FileID, unsigned> DecomposedEnd = SM.getDecomposedLoc(Begin); |
| 6838 | DecomposedEnd.second += Lexer::MeasureTokenLength(Begin, SM, CXXUnit->getLangOpts()); |
| 6839 | |
| 6840 | SourceLocation End = SM.getComposedLoc(DecomposedEnd.first, DecomposedEnd.second); |
| 6841 | |
| 6842 | SmallVector<CXToken, 32> CXTokens; |
| 6843 | getTokens(CXXUnit, SourceRange(Begin, End), CXTokens); |
| 6844 | |
| 6845 | if (CXTokens.empty()) |
| 6846 | return NULL; |
| 6847 | |
| 6848 | CXTokens.resize(1); |
| 6849 | CXToken *Token = static_cast<CXToken *>(llvm::safe_malloc(sizeof(CXToken))); |
| 6850 | |
| 6851 | memmove(Token, CXTokens.data(), sizeof(CXToken)); |
| 6852 | return Token; |
| 6853 | } |
| 6854 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6855 | void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, |
| 6856 | CXToken **Tokens, unsigned *NumTokens) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 6857 | LOG_FUNC_SECTION { |
| 6858 | *Log << TU << ' ' << Range; |
| 6859 | } |
| 6860 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6861 | if (Tokens) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6862 | *Tokens = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6863 | if (NumTokens) |
| 6864 | *NumTokens = 0; |
| 6865 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6866 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6867 | LOG_BAD_TU(TU); |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 6868 | return; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6869 | } |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 6870 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6871 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6872 | if (!CXXUnit || !Tokens || !NumTokens) |
| 6873 | return; |
| 6874 | |
| 6875 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 6876 | |
| 6877 | SourceRange R = cxloc::translateCXSourceRange(Range); |
| 6878 | if (R.isInvalid()) |
| 6879 | return; |
| 6880 | |
| 6881 | SmallVector<CXToken, 32> CXTokens; |
| 6882 | getTokens(CXXUnit, R, CXTokens); |
| 6883 | |
| 6884 | if (CXTokens.empty()) |
| 6885 | return; |
| 6886 | |
Serge Pavlov | 5252573 | 2018-02-21 02:02:39 +0000 | [diff] [blame] | 6887 | *Tokens = static_cast<CXToken *>( |
| 6888 | llvm::safe_malloc(sizeof(CXToken) * CXTokens.size())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6889 | memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); |
| 6890 | *NumTokens = CXTokens.size(); |
| 6891 | } |
| 6892 | |
| 6893 | void clang_disposeTokens(CXTranslationUnit TU, |
| 6894 | CXToken *Tokens, unsigned NumTokens) { |
| 6895 | free(Tokens); |
| 6896 | } |
| 6897 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6898 | //===----------------------------------------------------------------------===// |
| 6899 | // Token annotation APIs. |
| 6900 | //===----------------------------------------------------------------------===// |
| 6901 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6902 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 6903 | CXCursor parent, |
| 6904 | CXClientData client_data); |
| 6905 | static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor, |
| 6906 | CXClientData client_data); |
| 6907 | |
| 6908 | namespace { |
| 6909 | class AnnotateTokensWorker { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6910 | CXToken *Tokens; |
| 6911 | CXCursor *Cursors; |
| 6912 | unsigned NumTokens; |
| 6913 | unsigned TokIdx; |
| 6914 | unsigned PreprocessingTokIdx; |
| 6915 | CursorVisitor AnnotateVis; |
| 6916 | SourceManager &SrcMgr; |
| 6917 | bool HasContextSensitiveKeywords; |
| 6918 | |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 6919 | struct PostChildrenAction { |
| 6920 | CXCursor cursor; |
| 6921 | enum Action { Invalid, Ignore, Postpone } action; |
| 6922 | }; |
| 6923 | using PostChildrenActions = SmallVector<PostChildrenAction, 0>; |
| 6924 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6925 | struct PostChildrenInfo { |
| 6926 | CXCursor Cursor; |
| 6927 | SourceRange CursorRange; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6928 | unsigned BeforeReachingCursorIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6929 | unsigned BeforeChildrenTokenIdx; |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 6930 | PostChildrenActions ChildActions; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6931 | }; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6932 | SmallVector<PostChildrenInfo, 8> PostChildrenInfos; |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6933 | |
| 6934 | CXToken &getTok(unsigned Idx) { |
| 6935 | assert(Idx < NumTokens); |
| 6936 | return Tokens[Idx]; |
| 6937 | } |
| 6938 | const CXToken &getTok(unsigned Idx) const { |
| 6939 | assert(Idx < NumTokens); |
| 6940 | return Tokens[Idx]; |
| 6941 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6942 | bool MoreTokens() const { return TokIdx < NumTokens; } |
| 6943 | unsigned NextToken() const { return TokIdx; } |
| 6944 | void AdvanceToken() { ++TokIdx; } |
| 6945 | SourceLocation GetTokenLoc(unsigned tokI) { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6946 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6947 | } |
| 6948 | bool isFunctionMacroToken(unsigned tokI) const { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6949 | return getTok(tokI).int_data[3] != 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6950 | } |
| 6951 | SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6952 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[3]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6953 | } |
| 6954 | |
| 6955 | void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange); |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6956 | bool annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6957 | SourceRange); |
| 6958 | |
| 6959 | public: |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6960 | AnnotateTokensWorker(CXToken *tokens, CXCursor *cursors, unsigned numTokens, |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6961 | CXTranslationUnit TU, SourceRange RegionOfInterest) |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6962 | : Tokens(tokens), Cursors(cursors), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6963 | NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0), |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6964 | AnnotateVis(TU, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6965 | AnnotateTokensVisitor, this, |
| 6966 | /*VisitPreprocessorLast=*/true, |
| 6967 | /*VisitIncludedEntities=*/false, |
| 6968 | RegionOfInterest, |
| 6969 | /*VisitDeclsOnly=*/false, |
| 6970 | AnnotateTokensPostChildrenVisitor), |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6971 | SrcMgr(cxtu::getASTUnit(TU)->getSourceManager()), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6972 | HasContextSensitiveKeywords(false) { } |
| 6973 | |
| 6974 | void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } |
| 6975 | enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 6976 | bool IsIgnoredChildCursor(CXCursor cursor) const; |
| 6977 | PostChildrenActions DetermineChildActions(CXCursor Cursor) const; |
| 6978 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6979 | bool postVisitChildren(CXCursor cursor); |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 6980 | void HandlePostPonedChildCursors(const PostChildrenInfo &Info); |
| 6981 | void HandlePostPonedChildCursor(CXCursor Cursor, unsigned StartTokenIndex); |
| 6982 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6983 | void AnnotateTokens(); |
| 6984 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 6985 | /// Determine whether the annotator saw any cursors that have |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6986 | /// context-sensitive keywords. |
| 6987 | bool hasContextSensitiveKeywords() const { |
| 6988 | return HasContextSensitiveKeywords; |
| 6989 | } |
| 6990 | |
| 6991 | ~AnnotateTokensWorker() { |
| 6992 | assert(PostChildrenInfos.empty()); |
| 6993 | } |
| 6994 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6995 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6996 | |
| 6997 | void AnnotateTokensWorker::AnnotateTokens() { |
| 6998 | // Walk the AST within the region of interest, annotating tokens |
| 6999 | // along the way. |
| 7000 | AnnotateVis.visitFileRegion(); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7001 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7002 | |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7003 | bool AnnotateTokensWorker::IsIgnoredChildCursor(CXCursor cursor) const { |
| 7004 | if (PostChildrenInfos.empty()) |
| 7005 | return false; |
| 7006 | |
| 7007 | for (const auto &ChildAction : PostChildrenInfos.back().ChildActions) { |
| 7008 | if (ChildAction.cursor == cursor && |
| 7009 | ChildAction.action == PostChildrenAction::Ignore) { |
| 7010 | return true; |
| 7011 | } |
| 7012 | } |
| 7013 | |
| 7014 | return false; |
| 7015 | } |
| 7016 | |
| 7017 | const CXXOperatorCallExpr *GetSubscriptOrCallOperator(CXCursor Cursor) { |
| 7018 | if (!clang_isExpression(Cursor.kind)) |
| 7019 | return nullptr; |
| 7020 | |
| 7021 | const Expr *E = getCursorExpr(Cursor); |
| 7022 | if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 7023 | const OverloadedOperatorKind Kind = OCE->getOperator(); |
| 7024 | if (Kind == OO_Call || Kind == OO_Subscript) |
| 7025 | return OCE; |
| 7026 | } |
| 7027 | |
| 7028 | return nullptr; |
| 7029 | } |
| 7030 | |
| 7031 | AnnotateTokensWorker::PostChildrenActions |
| 7032 | AnnotateTokensWorker::DetermineChildActions(CXCursor Cursor) const { |
| 7033 | PostChildrenActions actions; |
| 7034 | |
| 7035 | // The DeclRefExpr of CXXOperatorCallExpr refering to the custom operator is |
| 7036 | // visited before the arguments to the operator call. For the Call and |
| 7037 | // Subscript operator the range of this DeclRefExpr includes the whole call |
| 7038 | // expression, so that all tokens in that range would be mapped to the |
| 7039 | // operator function, including the tokens of the arguments. To avoid that, |
| 7040 | // ensure to visit this DeclRefExpr as last node. |
| 7041 | if (const auto *OCE = GetSubscriptOrCallOperator(Cursor)) { |
| 7042 | const Expr *Callee = OCE->getCallee(); |
| 7043 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) { |
| 7044 | const Expr *SubExpr = ICE->getSubExpr(); |
| 7045 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(SubExpr)) { |
Fangrui Song | cabb36d | 2018-11-20 08:00:00 +0000 | [diff] [blame] | 7046 | const Decl *parentDecl = getCursorDecl(Cursor); |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7047 | CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor); |
| 7048 | |
| 7049 | // Visit the DeclRefExpr as last. |
| 7050 | CXCursor cxChild = MakeCXCursor(DRE, parentDecl, TU); |
| 7051 | actions.push_back({cxChild, PostChildrenAction::Postpone}); |
| 7052 | |
| 7053 | // The parent of the DeclRefExpr, an ImplicitCastExpr, has an equally |
| 7054 | // wide range as the DeclRefExpr. We can skip visiting this entirely. |
| 7055 | cxChild = MakeCXCursor(ICE, parentDecl, TU); |
| 7056 | actions.push_back({cxChild, PostChildrenAction::Ignore}); |
| 7057 | } |
| 7058 | } |
| 7059 | } |
| 7060 | |
| 7061 | return actions; |
| 7062 | } |
| 7063 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7064 | static inline void updateCursorAnnotation(CXCursor &Cursor, |
| 7065 | const CXCursor &updateC) { |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7066 | if (clang_isInvalid(updateC.kind) || !clang_isInvalid(Cursor.kind)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7067 | return; |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7068 | Cursor = updateC; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7069 | } |
| 7070 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7071 | /// It annotates and advances tokens with a cursor until the comparison |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7072 | //// between the cursor location and the source range is the same as |
| 7073 | /// \arg compResult. |
| 7074 | /// |
| 7075 | /// Pass RangeBefore to annotate tokens with a cursor until a range is reached. |
| 7076 | /// Pass RangeOverlap to annotate tokens inside a range. |
| 7077 | void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC, |
| 7078 | RangeComparisonResult compResult, |
| 7079 | SourceRange range) { |
| 7080 | while (MoreTokens()) { |
| 7081 | const unsigned I = NextToken(); |
| 7082 | if (isFunctionMacroToken(I)) |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7083 | if (!annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range)) |
| 7084 | return; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7085 | |
| 7086 | SourceLocation TokLoc = GetTokenLoc(I); |
| 7087 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7088 | updateCursorAnnotation(Cursors[I], updateC); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7089 | AdvanceToken(); |
| 7090 | continue; |
| 7091 | } |
| 7092 | break; |
| 7093 | } |
| 7094 | } |
| 7095 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7096 | /// Special annotation handling for macro argument tokens. |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7097 | /// \returns true if it advanced beyond all macro tokens, false otherwise. |
| 7098 | bool AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens( |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7099 | CXCursor updateC, |
| 7100 | RangeComparisonResult compResult, |
| 7101 | SourceRange range) { |
| 7102 | assert(MoreTokens()); |
| 7103 | assert(isFunctionMacroToken(NextToken()) && |
| 7104 | "Should be called only for macro arg tokens"); |
| 7105 | |
| 7106 | // This works differently than annotateAndAdvanceTokens; because expanded |
| 7107 | // macro arguments can have arbitrary translation-unit source order, we do not |
| 7108 | // advance the token index one by one until a token fails the range test. |
| 7109 | // We only advance once past all of the macro arg tokens if all of them |
| 7110 | // pass the range test. If one of them fails we keep the token index pointing |
| 7111 | // at the start of the macro arg tokens so that the failing token will be |
| 7112 | // annotated by a subsequent annotation try. |
| 7113 | |
| 7114 | bool atLeastOneCompFail = false; |
| 7115 | |
| 7116 | unsigned I = NextToken(); |
| 7117 | for (; I < NumTokens && isFunctionMacroToken(I); ++I) { |
| 7118 | SourceLocation TokLoc = getFunctionMacroTokenLoc(I); |
| 7119 | if (TokLoc.isFileID()) |
| 7120 | continue; // not macro arg token, it's parens or comma. |
| 7121 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 7122 | if (clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 7123 | Cursors[I] = updateC; |
| 7124 | } else |
| 7125 | atLeastOneCompFail = true; |
| 7126 | } |
| 7127 | |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7128 | if (atLeastOneCompFail) |
| 7129 | return false; |
| 7130 | |
| 7131 | TokIdx = I; // All of the tokens were handled, advance beyond all of them. |
| 7132 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7133 | } |
| 7134 | |
| 7135 | enum CXChildVisitResult |
| 7136 | AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7137 | SourceRange cursorRange = getRawCursorExtent(cursor); |
| 7138 | if (cursorRange.isInvalid()) |
| 7139 | return CXChildVisit_Recurse; |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7140 | |
| 7141 | if (IsIgnoredChildCursor(cursor)) |
| 7142 | return CXChildVisit_Continue; |
| 7143 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7144 | if (!HasContextSensitiveKeywords) { |
| 7145 | // Objective-C properties can have context-sensitive keywords. |
| 7146 | if (cursor.kind == CXCursor_ObjCPropertyDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7147 | if (const ObjCPropertyDecl *Property |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7148 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor))) |
| 7149 | HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0; |
| 7150 | } |
| 7151 | // Objective-C methods can have context-sensitive keywords. |
| 7152 | else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl || |
| 7153 | cursor.kind == CXCursor_ObjCClassMethodDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7154 | if (const ObjCMethodDecl *Method |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7155 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 7156 | if (Method->getObjCDeclQualifier()) |
| 7157 | HasContextSensitiveKeywords = true; |
| 7158 | else { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 7159 | for (const auto *P : Method->parameters()) { |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 7160 | if (P->getObjCDeclQualifier()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7161 | HasContextSensitiveKeywords = true; |
| 7162 | break; |
| 7163 | } |
| 7164 | } |
| 7165 | } |
| 7166 | } |
| 7167 | } |
| 7168 | // C++ methods can have context-sensitive keywords. |
| 7169 | else if (cursor.kind == CXCursor_CXXMethod) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7170 | if (const CXXMethodDecl *Method |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7171 | = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) { |
| 7172 | if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>()) |
| 7173 | HasContextSensitiveKeywords = true; |
| 7174 | } |
| 7175 | } |
| 7176 | // C++ classes can have context-sensitive keywords. |
| 7177 | else if (cursor.kind == CXCursor_StructDecl || |
| 7178 | cursor.kind == CXCursor_ClassDecl || |
| 7179 | cursor.kind == CXCursor_ClassTemplate || |
| 7180 | cursor.kind == CXCursor_ClassTemplatePartialSpecialization) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7181 | if (const Decl *D = getCursorDecl(cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7182 | if (D->hasAttr<FinalAttr>()) |
| 7183 | HasContextSensitiveKeywords = true; |
| 7184 | } |
| 7185 | } |
Argyrios Kyrtzidis | 990b386 | 2013-06-04 18:24:30 +0000 | [diff] [blame] | 7186 | |
| 7187 | // Don't override a property annotation with its getter/setter method. |
| 7188 | if (cursor.kind == CXCursor_ObjCInstanceMethodDecl && |
| 7189 | parent.kind == CXCursor_ObjCPropertyDecl) |
| 7190 | return CXChildVisit_Continue; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7191 | |
| 7192 | if (clang_isPreprocessing(cursor.kind)) { |
| 7193 | // Items in the preprocessing record are kept separate from items in |
| 7194 | // declarations, so we keep a separate token index. |
| 7195 | unsigned SavedTokIdx = TokIdx; |
| 7196 | TokIdx = PreprocessingTokIdx; |
| 7197 | |
| 7198 | // Skip tokens up until we catch up to the beginning of the preprocessing |
| 7199 | // entry. |
| 7200 | while (MoreTokens()) { |
| 7201 | const unsigned I = NextToken(); |
| 7202 | SourceLocation TokLoc = GetTokenLoc(I); |
| 7203 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 7204 | case RangeBefore: |
| 7205 | AdvanceToken(); |
| 7206 | continue; |
| 7207 | case RangeAfter: |
| 7208 | case RangeOverlap: |
| 7209 | break; |
| 7210 | } |
| 7211 | break; |
| 7212 | } |
| 7213 | |
| 7214 | // Look at all of the tokens within this range. |
| 7215 | while (MoreTokens()) { |
| 7216 | const unsigned I = NextToken(); |
| 7217 | SourceLocation TokLoc = GetTokenLoc(I); |
| 7218 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 7219 | case RangeBefore: |
| 7220 | llvm_unreachable("Infeasible"); |
| 7221 | case RangeAfter: |
| 7222 | break; |
| 7223 | case RangeOverlap: |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 7224 | // For macro expansions, just note where the beginning of the macro |
| 7225 | // expansion occurs. |
| 7226 | if (cursor.kind == CXCursor_MacroExpansion) { |
| 7227 | if (TokLoc == cursorRange.getBegin()) |
| 7228 | Cursors[I] = cursor; |
| 7229 | AdvanceToken(); |
| 7230 | break; |
| 7231 | } |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7232 | // We may have already annotated macro names inside macro definitions. |
| 7233 | if (Cursors[I].kind != CXCursor_MacroExpansion) |
| 7234 | Cursors[I] = cursor; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7235 | AdvanceToken(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7236 | continue; |
| 7237 | } |
| 7238 | break; |
| 7239 | } |
| 7240 | |
| 7241 | // Save the preprocessing token index; restore the non-preprocessing |
| 7242 | // token index. |
| 7243 | PreprocessingTokIdx = TokIdx; |
| 7244 | TokIdx = SavedTokIdx; |
| 7245 | return CXChildVisit_Recurse; |
| 7246 | } |
| 7247 | |
| 7248 | if (cursorRange.isInvalid()) |
| 7249 | return CXChildVisit_Continue; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7250 | |
| 7251 | unsigned BeforeReachingCursorIdx = NextToken(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7252 | const enum CXCursorKind cursorK = clang_getCursorKind(cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7253 | const enum CXCursorKind K = clang_getCursorKind(parent); |
| 7254 | const CXCursor updateC = |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7255 | (clang_isInvalid(K) || K == CXCursor_TranslationUnit || |
| 7256 | // Attributes are annotated out-of-order, skip tokens until we reach it. |
| 7257 | clang_isAttribute(cursor.kind)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7258 | ? clang_getNullCursor() : parent; |
| 7259 | |
| 7260 | annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange); |
| 7261 | |
| 7262 | // Avoid having the cursor of an expression "overwrite" the annotation of the |
| 7263 | // variable declaration that it belongs to. |
| 7264 | // This can happen for C++ constructor expressions whose range generally |
| 7265 | // include the variable declaration, e.g.: |
| 7266 | // 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] | 7267 | if (clang_isExpression(cursorK) && MoreTokens()) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 7268 | const Expr *E = getCursorExpr(cursor); |
Fangrui Song | cabb36d | 2018-11-20 08:00:00 +0000 | [diff] [blame] | 7269 | if (const Decl *D = getCursorDecl(cursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7270 | const unsigned I = NextToken(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7271 | if (E->getBeginLoc().isValid() && D->getLocation().isValid() && |
| 7272 | E->getBeginLoc() == D->getLocation() && |
| 7273 | E->getBeginLoc() == GetTokenLoc(I)) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7274 | updateCursorAnnotation(Cursors[I], updateC); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7275 | AdvanceToken(); |
| 7276 | } |
| 7277 | } |
| 7278 | } |
| 7279 | |
| 7280 | // Before recursing into the children keep some state that we are going |
| 7281 | // to use in the AnnotateTokensWorker::postVisitChildren callback to do some |
| 7282 | // extra work after the child nodes are visited. |
| 7283 | // Note that we don't call VisitChildren here to avoid traversing statements |
| 7284 | // code-recursively which can blow the stack. |
| 7285 | |
| 7286 | PostChildrenInfo Info; |
| 7287 | Info.Cursor = cursor; |
| 7288 | Info.CursorRange = cursorRange; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7289 | Info.BeforeReachingCursorIdx = BeforeReachingCursorIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7290 | Info.BeforeChildrenTokenIdx = NextToken(); |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7291 | Info.ChildActions = DetermineChildActions(cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7292 | PostChildrenInfos.push_back(Info); |
| 7293 | |
| 7294 | return CXChildVisit_Recurse; |
| 7295 | } |
| 7296 | |
| 7297 | bool AnnotateTokensWorker::postVisitChildren(CXCursor cursor) { |
| 7298 | if (PostChildrenInfos.empty()) |
| 7299 | return false; |
| 7300 | const PostChildrenInfo &Info = PostChildrenInfos.back(); |
| 7301 | if (!clang_equalCursors(Info.Cursor, cursor)) |
| 7302 | return false; |
| 7303 | |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7304 | HandlePostPonedChildCursors(Info); |
| 7305 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7306 | const unsigned BeforeChildren = Info.BeforeChildrenTokenIdx; |
| 7307 | const unsigned AfterChildren = NextToken(); |
| 7308 | SourceRange cursorRange = Info.CursorRange; |
| 7309 | |
| 7310 | // Scan the tokens that are at the end of the cursor, but are not captured |
| 7311 | // but the child cursors. |
| 7312 | annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange); |
| 7313 | |
| 7314 | // Scan the tokens that are at the beginning of the cursor, but are not |
| 7315 | // capture by the child cursors. |
| 7316 | for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { |
| 7317 | if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 7318 | break; |
| 7319 | |
| 7320 | Cursors[I] = cursor; |
| 7321 | } |
| 7322 | |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 7323 | // Attributes are annotated out-of-order, rewind TokIdx to when we first |
| 7324 | // encountered the attribute cursor. |
| 7325 | if (clang_isAttribute(cursor.kind)) |
| 7326 | TokIdx = Info.BeforeReachingCursorIdx; |
| 7327 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7328 | PostChildrenInfos.pop_back(); |
| 7329 | return false; |
| 7330 | } |
| 7331 | |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7332 | void AnnotateTokensWorker::HandlePostPonedChildCursors( |
| 7333 | const PostChildrenInfo &Info) { |
| 7334 | for (const auto &ChildAction : Info.ChildActions) { |
| 7335 | if (ChildAction.action == PostChildrenAction::Postpone) { |
| 7336 | HandlePostPonedChildCursor(ChildAction.cursor, |
| 7337 | Info.BeforeChildrenTokenIdx); |
| 7338 | } |
| 7339 | } |
| 7340 | } |
| 7341 | |
| 7342 | void AnnotateTokensWorker::HandlePostPonedChildCursor( |
| 7343 | CXCursor Cursor, unsigned StartTokenIndex) { |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7344 | unsigned I = StartTokenIndex; |
| 7345 | |
| 7346 | // The bracket tokens of a Call or Subscript operator are mapped to |
| 7347 | // CallExpr/CXXOperatorCallExpr because we skipped visiting the corresponding |
| 7348 | // DeclRefExpr. Remap these tokens to the DeclRefExpr cursors. |
| 7349 | for (unsigned RefNameRangeNr = 0; I < NumTokens; RefNameRangeNr++) { |
Nikolai Kosjar | 2a647e7 | 2019-05-08 13:19:29 +0000 | [diff] [blame] | 7350 | const CXSourceRange CXRefNameRange = clang_getCursorReferenceNameRange( |
| 7351 | Cursor, CXNameRange_WantQualifier, RefNameRangeNr); |
Ivan Donchevskii | b3ae2bc | 2018-08-23 09:48:11 +0000 | [diff] [blame] | 7352 | if (clang_Range_isNull(CXRefNameRange)) |
| 7353 | break; // All ranges handled. |
| 7354 | |
| 7355 | SourceRange RefNameRange = cxloc::translateCXSourceRange(CXRefNameRange); |
| 7356 | while (I < NumTokens) { |
| 7357 | const SourceLocation TokenLocation = GetTokenLoc(I); |
| 7358 | if (!TokenLocation.isValid()) |
| 7359 | break; |
| 7360 | |
| 7361 | // Adapt the end range, because LocationCompare() reports |
| 7362 | // RangeOverlap even for the not-inclusive end location. |
| 7363 | const SourceLocation fixedEnd = |
| 7364 | RefNameRange.getEnd().getLocWithOffset(-1); |
| 7365 | RefNameRange = SourceRange(RefNameRange.getBegin(), fixedEnd); |
| 7366 | |
| 7367 | const RangeComparisonResult ComparisonResult = |
| 7368 | LocationCompare(SrcMgr, TokenLocation, RefNameRange); |
| 7369 | |
| 7370 | if (ComparisonResult == RangeOverlap) { |
| 7371 | Cursors[I++] = Cursor; |
| 7372 | } else if (ComparisonResult == RangeBefore) { |
| 7373 | ++I; // Not relevant token, check next one. |
| 7374 | } else if (ComparisonResult == RangeAfter) { |
| 7375 | break; // All tokens updated for current range, check next. |
| 7376 | } |
| 7377 | } |
| 7378 | } |
| 7379 | } |
| 7380 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7381 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 7382 | CXCursor parent, |
| 7383 | CXClientData client_data) { |
| 7384 | return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); |
| 7385 | } |
| 7386 | |
| 7387 | static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor, |
| 7388 | CXClientData client_data) { |
| 7389 | return static_cast<AnnotateTokensWorker*>(client_data)-> |
| 7390 | postVisitChildren(cursor); |
| 7391 | } |
| 7392 | |
| 7393 | namespace { |
| 7394 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7395 | /// Uses the macro expansions in the preprocessing record to find |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7396 | /// and mark tokens that are macro arguments. This info is used by the |
| 7397 | /// AnnotateTokensWorker. |
| 7398 | class MarkMacroArgTokensVisitor { |
| 7399 | SourceManager &SM; |
| 7400 | CXToken *Tokens; |
| 7401 | unsigned NumTokens; |
| 7402 | unsigned CurIdx; |
| 7403 | |
| 7404 | public: |
| 7405 | MarkMacroArgTokensVisitor(SourceManager &SM, |
| 7406 | CXToken *tokens, unsigned numTokens) |
| 7407 | : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { } |
| 7408 | |
| 7409 | CXChildVisitResult visit(CXCursor cursor, CXCursor parent) { |
| 7410 | if (cursor.kind != CXCursor_MacroExpansion) |
| 7411 | return CXChildVisit_Continue; |
| 7412 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7413 | SourceRange macroRange = getCursorMacroExpansion(cursor).getSourceRange(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7414 | if (macroRange.getBegin() == macroRange.getEnd()) |
| 7415 | return CXChildVisit_Continue; // it's not a function macro. |
| 7416 | |
| 7417 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 7418 | if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx), |
| 7419 | macroRange.getBegin())) |
| 7420 | break; |
| 7421 | } |
| 7422 | |
| 7423 | if (CurIdx == NumTokens) |
| 7424 | return CXChildVisit_Break; |
| 7425 | |
| 7426 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 7427 | SourceLocation tokLoc = getTokenLoc(CurIdx); |
| 7428 | if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd())) |
| 7429 | break; |
| 7430 | |
| 7431 | setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc)); |
| 7432 | } |
| 7433 | |
| 7434 | if (CurIdx == NumTokens) |
| 7435 | return CXChildVisit_Break; |
| 7436 | |
| 7437 | return CXChildVisit_Continue; |
| 7438 | } |
| 7439 | |
| 7440 | private: |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 7441 | CXToken &getTok(unsigned Idx) { |
| 7442 | assert(Idx < NumTokens); |
| 7443 | return Tokens[Idx]; |
| 7444 | } |
| 7445 | const CXToken &getTok(unsigned Idx) const { |
| 7446 | assert(Idx < NumTokens); |
| 7447 | return Tokens[Idx]; |
| 7448 | } |
| 7449 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7450 | SourceLocation getTokenLoc(unsigned tokI) { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 7451 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7452 | } |
| 7453 | |
| 7454 | void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) { |
| 7455 | // The third field is reserved and currently not used. Use it here |
| 7456 | // to mark macro arg expanded tokens with their expanded locations. |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 7457 | getTok(tokI).int_data[3] = loc.getRawEncoding(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7458 | } |
| 7459 | }; |
| 7460 | |
| 7461 | } // end anonymous namespace |
| 7462 | |
| 7463 | static CXChildVisitResult |
| 7464 | MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent, |
| 7465 | CXClientData client_data) { |
| 7466 | return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor, |
| 7467 | parent); |
| 7468 | } |
| 7469 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7470 | /// Used by \c annotatePreprocessorTokens. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7471 | /// \returns true if lexing was finished, false otherwise. |
| 7472 | static bool lexNext(Lexer &Lex, Token &Tok, |
| 7473 | unsigned &NextIdx, unsigned NumTokens) { |
| 7474 | if (NextIdx >= NumTokens) |
| 7475 | return true; |
| 7476 | |
| 7477 | ++NextIdx; |
| 7478 | Lex.LexFromRawLexer(Tok); |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 7479 | return Tok.is(tok::eof); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7480 | } |
| 7481 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7482 | static void annotatePreprocessorTokens(CXTranslationUnit TU, |
| 7483 | SourceRange RegionOfInterest, |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7484 | CXCursor *Cursors, |
| 7485 | CXToken *Tokens, |
| 7486 | unsigned NumTokens) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7487 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7488 | |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7489 | Preprocessor &PP = CXXUnit->getPreprocessor(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7490 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 7491 | std::pair<FileID, unsigned> BeginLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 7492 | = SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getBegin()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7493 | std::pair<FileID, unsigned> EndLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 7494 | = SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getEnd()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7495 | |
| 7496 | if (BeginLocInfo.first != EndLocInfo.first) |
| 7497 | return; |
| 7498 | |
| 7499 | StringRef Buffer; |
| 7500 | bool Invalid = false; |
| 7501 | Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 7502 | if (Buffer.empty() || Invalid) |
| 7503 | return; |
| 7504 | |
| 7505 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 7506 | CXXUnit->getASTContext().getLangOpts(), |
| 7507 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, |
| 7508 | Buffer.end()); |
| 7509 | Lex.SetCommentRetentionState(true); |
| 7510 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7511 | unsigned NextIdx = 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7512 | // Lex tokens in raw mode until we hit the end of the range, to avoid |
| 7513 | // entering #includes or expanding macros. |
| 7514 | while (true) { |
| 7515 | Token Tok; |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7516 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 7517 | break; |
| 7518 | unsigned TokIdx = NextIdx-1; |
| 7519 | assert(Tok.getLocation() == |
| 7520 | SourceLocation::getFromRawEncoding(Tokens[TokIdx].int_data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7521 | |
| 7522 | reprocess: |
| 7523 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7524 | // We have found a preprocessing directive. Annotate the tokens |
| 7525 | // appropriately. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7526 | // |
| 7527 | // FIXME: Some simple tests here could identify macro definitions and |
| 7528 | // #undefs, to provide specific cursor kinds for those. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7529 | |
| 7530 | SourceLocation BeginLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7531 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 7532 | break; |
| 7533 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7534 | MacroInfo *MI = nullptr; |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 7535 | if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "define") { |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7536 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 7537 | break; |
| 7538 | |
| 7539 | if (Tok.is(tok::raw_identifier)) { |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 7540 | IdentifierInfo &II = |
| 7541 | PP.getIdentifierTable().get(Tok.getRawIdentifier()); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7542 | SourceLocation MappedTokLoc = |
| 7543 | CXXUnit->mapLocationToPreamble(Tok.getLocation()); |
| 7544 | MI = getMacroInfo(II, MappedTokLoc, TU); |
| 7545 | } |
| 7546 | } |
| 7547 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7548 | bool finished = false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7549 | do { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7550 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) { |
| 7551 | finished = true; |
| 7552 | break; |
| 7553 | } |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7554 | // If we are in a macro definition, check if the token was ever a |
| 7555 | // macro name and annotate it if that's the case. |
| 7556 | if (MI) { |
| 7557 | SourceLocation SaveLoc = Tok.getLocation(); |
| 7558 | Tok.setLocation(CXXUnit->mapLocationToPreamble(SaveLoc)); |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 7559 | MacroDefinitionRecord *MacroDef = |
| 7560 | checkForMacroInMacroDefinition(MI, Tok, TU); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7561 | Tok.setLocation(SaveLoc); |
| 7562 | if (MacroDef) |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 7563 | Cursors[NextIdx - 1] = |
| 7564 | MakeMacroExpansionCursor(MacroDef, Tok.getLocation(), TU); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7565 | } |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7566 | } while (!Tok.isAtStartOfLine()); |
| 7567 | |
| 7568 | unsigned LastIdx = finished ? NextIdx-1 : NextIdx-2; |
| 7569 | assert(TokIdx <= LastIdx); |
| 7570 | SourceLocation EndLoc = |
| 7571 | SourceLocation::getFromRawEncoding(Tokens[LastIdx].int_data[1]); |
| 7572 | CXCursor Cursor = |
| 7573 | MakePreprocessingDirectiveCursor(SourceRange(BeginLoc, EndLoc), TU); |
| 7574 | |
| 7575 | for (; TokIdx <= LastIdx; ++TokIdx) |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 7576 | updateCursorAnnotation(Cursors[TokIdx], Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7577 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7578 | if (finished) |
| 7579 | break; |
| 7580 | goto reprocess; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7581 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7582 | } |
| 7583 | } |
| 7584 | |
| 7585 | // This gets run a separate thread to avoid stack blowout. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 7586 | static void clang_annotateTokensImpl(CXTranslationUnit TU, ASTUnit *CXXUnit, |
| 7587 | CXToken *Tokens, unsigned NumTokens, |
| 7588 | CXCursor *Cursors) { |
Dmitri Gribenko | 183436e | 2013-01-26 21:49:50 +0000 | [diff] [blame] | 7589 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7590 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) |
| 7591 | setThreadBackgroundPriority(); |
| 7592 | |
| 7593 | // Determine the region of interest, which contains all of the tokens. |
| 7594 | SourceRange RegionOfInterest; |
| 7595 | RegionOfInterest.setBegin( |
| 7596 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0]))); |
| 7597 | RegionOfInterest.setEnd( |
| 7598 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, |
| 7599 | Tokens[NumTokens-1]))); |
| 7600 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7601 | // Relex the tokens within the source range to look for preprocessing |
| 7602 | // directives. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7603 | annotatePreprocessorTokens(TU, RegionOfInterest, Cursors, Tokens, NumTokens); |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 7604 | |
| 7605 | // If begin location points inside a macro argument, set it to the expansion |
| 7606 | // location so we can have the full context when annotating semantically. |
| 7607 | { |
| 7608 | SourceManager &SM = CXXUnit->getSourceManager(); |
| 7609 | SourceLocation Loc = |
| 7610 | SM.getMacroArgExpandedLocation(RegionOfInterest.getBegin()); |
| 7611 | if (Loc.isMacroID()) |
| 7612 | RegionOfInterest.setBegin(SM.getExpansionLoc(Loc)); |
| 7613 | } |
| 7614 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7615 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) { |
| 7616 | // Search and mark tokens that are macro argument expansions. |
| 7617 | MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(), |
| 7618 | Tokens, NumTokens); |
| 7619 | CursorVisitor MacroArgMarker(TU, |
| 7620 | MarkMacroArgTokensVisitorDelegate, &Visitor, |
| 7621 | /*VisitPreprocessorLast=*/true, |
| 7622 | /*VisitIncludedEntities=*/false, |
| 7623 | RegionOfInterest); |
| 7624 | MacroArgMarker.visitPreprocessedEntitiesInRegion(); |
| 7625 | } |
| 7626 | |
| 7627 | // Annotate all of the source locations in the region of interest that map to |
| 7628 | // a specific cursor. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7629 | AnnotateTokensWorker W(Tokens, Cursors, NumTokens, TU, RegionOfInterest); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7630 | |
| 7631 | // FIXME: We use a ridiculous stack size here because the data-recursion |
| 7632 | // algorithm uses a large stack frame than the non-data recursive version, |
| 7633 | // and AnnotationTokensWorker currently transforms the data-recursion |
| 7634 | // algorithm back into a traditional recursion by explicitly calling |
| 7635 | // VisitChildren(). We will need to remove this explicit recursive call. |
| 7636 | W.AnnotateTokens(); |
| 7637 | |
| 7638 | // If we ran into any entities that involve context-sensitive keywords, |
| 7639 | // take another pass through the tokens to mark them as such. |
| 7640 | if (W.hasContextSensitiveKeywords()) { |
| 7641 | for (unsigned I = 0; I != NumTokens; ++I) { |
| 7642 | if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier) |
| 7643 | continue; |
| 7644 | |
| 7645 | if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) { |
| 7646 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7647 | if (const ObjCPropertyDecl *Property |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7648 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) { |
| 7649 | if (Property->getPropertyAttributesAsWritten() != 0 && |
| 7650 | llvm::StringSwitch<bool>(II->getName()) |
| 7651 | .Case("readonly", true) |
| 7652 | .Case("assign", true) |
| 7653 | .Case("unsafe_unretained", true) |
| 7654 | .Case("readwrite", true) |
| 7655 | .Case("retain", true) |
| 7656 | .Case("copy", true) |
| 7657 | .Case("nonatomic", true) |
| 7658 | .Case("atomic", true) |
| 7659 | .Case("getter", true) |
| 7660 | .Case("setter", true) |
| 7661 | .Case("strong", true) |
| 7662 | .Case("weak", true) |
Manman Ren | 04fd4d8 | 2016-05-31 23:22:04 +0000 | [diff] [blame] | 7663 | .Case("class", true) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7664 | .Default(false)) |
| 7665 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 7666 | } |
| 7667 | continue; |
| 7668 | } |
| 7669 | |
| 7670 | if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl || |
| 7671 | Cursors[I].kind == CXCursor_ObjCClassMethodDecl) { |
| 7672 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 7673 | if (llvm::StringSwitch<bool>(II->getName()) |
| 7674 | .Case("in", true) |
| 7675 | .Case("out", true) |
| 7676 | .Case("inout", true) |
| 7677 | .Case("oneway", true) |
| 7678 | .Case("bycopy", true) |
| 7679 | .Case("byref", true) |
| 7680 | .Default(false)) |
| 7681 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 7682 | continue; |
| 7683 | } |
| 7684 | |
| 7685 | if (Cursors[I].kind == CXCursor_CXXFinalAttr || |
| 7686 | Cursors[I].kind == CXCursor_CXXOverrideAttr) { |
| 7687 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 7688 | continue; |
| 7689 | } |
| 7690 | } |
| 7691 | } |
| 7692 | } |
| 7693 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7694 | void clang_annotateTokens(CXTranslationUnit TU, |
| 7695 | CXToken *Tokens, unsigned NumTokens, |
| 7696 | CXCursor *Cursors) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7697 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7698 | LOG_BAD_TU(TU); |
| 7699 | return; |
| 7700 | } |
| 7701 | if (NumTokens == 0 || !Tokens || !Cursors) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7702 | LOG_FUNC_SECTION { *Log << "<null input>"; } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7703 | return; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7704 | } |
| 7705 | |
| 7706 | LOG_FUNC_SECTION { |
| 7707 | *Log << TU << ' '; |
| 7708 | CXSourceLocation bloc = clang_getTokenLocation(TU, Tokens[0]); |
| 7709 | CXSourceLocation eloc = clang_getTokenLocation(TU, Tokens[NumTokens-1]); |
| 7710 | *Log << clang_getRange(bloc, eloc); |
| 7711 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7712 | |
| 7713 | // Any token we don't specifically annotate will have a NULL cursor. |
| 7714 | CXCursor C = clang_getNullCursor(); |
| 7715 | for (unsigned I = 0; I != NumTokens; ++I) |
| 7716 | Cursors[I] = C; |
| 7717 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7718 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7719 | if (!CXXUnit) |
| 7720 | return; |
| 7721 | |
| 7722 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 7723 | |
| 7724 | auto AnnotateTokensImpl = [=]() { |
| 7725 | clang_annotateTokensImpl(TU, CXXUnit, Tokens, NumTokens, Cursors); |
| 7726 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7727 | llvm::CrashRecoveryContext CRC; |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 7728 | if (!RunSafely(CRC, AnnotateTokensImpl, GetSafetyThreadStackSize() * 2)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7729 | fprintf(stderr, "libclang: crash detected while annotating tokens\n"); |
| 7730 | } |
| 7731 | } |
| 7732 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7733 | //===----------------------------------------------------------------------===// |
| 7734 | // Operations for querying linkage of a cursor. |
| 7735 | //===----------------------------------------------------------------------===// |
| 7736 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7737 | CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { |
| 7738 | if (!clang_isDeclaration(cursor.kind)) |
| 7739 | return CXLinkage_Invalid; |
| 7740 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7741 | const Decl *D = cxcursor::getCursorDecl(cursor); |
| 7742 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7743 | switch (ND->getLinkageInternal()) { |
Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 7744 | case NoLinkage: |
| 7745 | case VisibleNoLinkage: return CXLinkage_NoLinkage; |
Richard Smith | af10ea2 | 2017-07-08 00:37:59 +0000 | [diff] [blame] | 7746 | case ModuleInternalLinkage: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7747 | case InternalLinkage: return CXLinkage_Internal; |
| 7748 | case UniqueExternalLinkage: return CXLinkage_UniqueExternal; |
Richard Smith | af10ea2 | 2017-07-08 00:37:59 +0000 | [diff] [blame] | 7749 | case ModuleLinkage: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7750 | case ExternalLinkage: return CXLinkage_External; |
| 7751 | }; |
| 7752 | |
| 7753 | return CXLinkage_Invalid; |
| 7754 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7755 | |
| 7756 | //===----------------------------------------------------------------------===// |
Ehsan Akhgari | b743de7 | 2016-05-31 15:55:51 +0000 | [diff] [blame] | 7757 | // Operations for querying visibility of a cursor. |
| 7758 | //===----------------------------------------------------------------------===// |
| 7759 | |
Ehsan Akhgari | b743de7 | 2016-05-31 15:55:51 +0000 | [diff] [blame] | 7760 | CXVisibilityKind clang_getCursorVisibility(CXCursor cursor) { |
| 7761 | if (!clang_isDeclaration(cursor.kind)) |
| 7762 | return CXVisibility_Invalid; |
| 7763 | |
| 7764 | const Decl *D = cxcursor::getCursorDecl(cursor); |
| 7765 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
| 7766 | switch (ND->getVisibility()) { |
| 7767 | case HiddenVisibility: return CXVisibility_Hidden; |
| 7768 | case ProtectedVisibility: return CXVisibility_Protected; |
| 7769 | case DefaultVisibility: return CXVisibility_Default; |
| 7770 | }; |
| 7771 | |
| 7772 | return CXVisibility_Invalid; |
| 7773 | } |
Ehsan Akhgari | b743de7 | 2016-05-31 15:55:51 +0000 | [diff] [blame] | 7774 | |
| 7775 | //===----------------------------------------------------------------------===// |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7776 | // Operations for querying language of a cursor. |
| 7777 | //===----------------------------------------------------------------------===// |
| 7778 | |
| 7779 | static CXLanguageKind getDeclLanguage(const Decl *D) { |
| 7780 | if (!D) |
| 7781 | return CXLanguage_C; |
| 7782 | |
| 7783 | switch (D->getKind()) { |
| 7784 | default: |
| 7785 | break; |
| 7786 | case Decl::ImplicitParam: |
| 7787 | case Decl::ObjCAtDefsField: |
| 7788 | case Decl::ObjCCategory: |
| 7789 | case Decl::ObjCCategoryImpl: |
| 7790 | case Decl::ObjCCompatibleAlias: |
| 7791 | case Decl::ObjCImplementation: |
| 7792 | case Decl::ObjCInterface: |
| 7793 | case Decl::ObjCIvar: |
| 7794 | case Decl::ObjCMethod: |
| 7795 | case Decl::ObjCProperty: |
| 7796 | case Decl::ObjCPropertyImpl: |
| 7797 | case Decl::ObjCProtocol: |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 7798 | case Decl::ObjCTypeParam: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7799 | return CXLanguage_ObjC; |
| 7800 | case Decl::CXXConstructor: |
| 7801 | case Decl::CXXConversion: |
| 7802 | case Decl::CXXDestructor: |
| 7803 | case Decl::CXXMethod: |
| 7804 | case Decl::CXXRecord: |
| 7805 | case Decl::ClassTemplate: |
| 7806 | case Decl::ClassTemplatePartialSpecialization: |
| 7807 | case Decl::ClassTemplateSpecialization: |
| 7808 | case Decl::Friend: |
| 7809 | case Decl::FriendTemplate: |
| 7810 | case Decl::FunctionTemplate: |
| 7811 | case Decl::LinkageSpec: |
| 7812 | case Decl::Namespace: |
| 7813 | case Decl::NamespaceAlias: |
| 7814 | case Decl::NonTypeTemplateParm: |
| 7815 | case Decl::StaticAssert: |
| 7816 | case Decl::TemplateTemplateParm: |
| 7817 | case Decl::TemplateTypeParm: |
| 7818 | case Decl::UnresolvedUsingTypename: |
| 7819 | case Decl::UnresolvedUsingValue: |
| 7820 | case Decl::Using: |
| 7821 | case Decl::UsingDirective: |
| 7822 | case Decl::UsingShadow: |
| 7823 | return CXLanguage_CPlusPlus; |
| 7824 | } |
| 7825 | |
| 7826 | return CXLanguage_C; |
| 7827 | } |
| 7828 | |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7829 | static CXAvailabilityKind getCursorAvailabilityForDecl(const Decl *D) { |
| 7830 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()) |
Manuel Klimek | 8e3a7ed | 2015-09-25 17:53:16 +0000 | [diff] [blame] | 7831 | return CXAvailability_NotAvailable; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7832 | |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7833 | switch (D->getAvailability()) { |
| 7834 | case AR_Available: |
| 7835 | case AR_NotYetIntroduced: |
| 7836 | if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(D)) |
Benjamin Kramer | 656363d | 2013-10-15 18:53:18 +0000 | [diff] [blame] | 7837 | return getCursorAvailabilityForDecl( |
| 7838 | cast<Decl>(EnumConst->getDeclContext())); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7839 | return CXAvailability_Available; |
| 7840 | |
| 7841 | case AR_Deprecated: |
| 7842 | return CXAvailability_Deprecated; |
| 7843 | |
| 7844 | case AR_Unavailable: |
| 7845 | return CXAvailability_NotAvailable; |
| 7846 | } |
Benjamin Kramer | 656363d | 2013-10-15 18:53:18 +0000 | [diff] [blame] | 7847 | |
| 7848 | llvm_unreachable("Unknown availability kind!"); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7849 | } |
| 7850 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7851 | enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { |
| 7852 | if (clang_isDeclaration(cursor.kind)) |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7853 | if (const Decl *D = cxcursor::getCursorDecl(cursor)) |
| 7854 | return getCursorAvailabilityForDecl(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7855 | |
| 7856 | return CXAvailability_Available; |
| 7857 | } |
| 7858 | |
| 7859 | static CXVersion convertVersion(VersionTuple In) { |
| 7860 | CXVersion Out = { -1, -1, -1 }; |
| 7861 | if (In.empty()) |
| 7862 | return Out; |
| 7863 | |
| 7864 | Out.Major = In.getMajor(); |
| 7865 | |
NAKAMURA Takumi | c2b5d1f | 2013-02-21 02:32:34 +0000 | [diff] [blame] | 7866 | Optional<unsigned> Minor = In.getMinor(); |
| 7867 | if (Minor.hasValue()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7868 | Out.Minor = *Minor; |
| 7869 | else |
| 7870 | return Out; |
| 7871 | |
NAKAMURA Takumi | c2b5d1f | 2013-02-21 02:32:34 +0000 | [diff] [blame] | 7872 | Optional<unsigned> Subminor = In.getSubminor(); |
| 7873 | if (Subminor.hasValue()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7874 | Out.Subminor = *Subminor; |
| 7875 | |
| 7876 | return Out; |
| 7877 | } |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7878 | |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7879 | static void getCursorPlatformAvailabilityForDecl( |
| 7880 | const Decl *D, int *always_deprecated, CXString *deprecated_message, |
| 7881 | int *always_unavailable, CXString *unavailable_message, |
| 7882 | SmallVectorImpl<AvailabilityAttr *> &AvailabilityAttrs) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7883 | bool HadAvailAttr = false; |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 7884 | for (auto A : D->attrs()) { |
| 7885 | if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(A)) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7886 | HadAvailAttr = true; |
| 7887 | if (always_deprecated) |
| 7888 | *always_deprecated = 1; |
Nico Weber | aacf031 | 2014-04-24 05:16:45 +0000 | [diff] [blame] | 7889 | if (deprecated_message) { |
Argyrios Kyrtzidis | edfe07f | 2014-04-24 06:05:40 +0000 | [diff] [blame] | 7890 | clang_disposeString(*deprecated_message); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7891 | *deprecated_message = cxstring::createDup(Deprecated->getMessage()); |
Nico Weber | aacf031 | 2014-04-24 05:16:45 +0000 | [diff] [blame] | 7892 | } |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7893 | continue; |
| 7894 | } |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7895 | |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 7896 | if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(A)) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7897 | HadAvailAttr = true; |
| 7898 | if (always_unavailable) |
| 7899 | *always_unavailable = 1; |
| 7900 | if (unavailable_message) { |
Argyrios Kyrtzidis | edfe07f | 2014-04-24 06:05:40 +0000 | [diff] [blame] | 7901 | clang_disposeString(*unavailable_message); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7902 | *unavailable_message = cxstring::createDup(Unavailable->getMessage()); |
| 7903 | } |
| 7904 | continue; |
| 7905 | } |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7906 | |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 7907 | if (AvailabilityAttr *Avail = dyn_cast<AvailabilityAttr>(A)) { |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7908 | AvailabilityAttrs.push_back(Avail); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7909 | HadAvailAttr = true; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7910 | } |
| 7911 | } |
| 7912 | |
| 7913 | if (!HadAvailAttr) |
| 7914 | if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(D)) |
| 7915 | return getCursorPlatformAvailabilityForDecl( |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7916 | cast<Decl>(EnumConst->getDeclContext()), always_deprecated, |
| 7917 | deprecated_message, always_unavailable, unavailable_message, |
| 7918 | AvailabilityAttrs); |
| 7919 | |
| 7920 | if (AvailabilityAttrs.empty()) |
| 7921 | return; |
| 7922 | |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 7923 | llvm::sort(AvailabilityAttrs, |
Mandeep Singh Grang | c205d8c | 2018-03-27 16:50:00 +0000 | [diff] [blame] | 7924 | [](AvailabilityAttr *LHS, AvailabilityAttr *RHS) { |
| 7925 | return LHS->getPlatform()->getName() < |
| 7926 | RHS->getPlatform()->getName(); |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 7927 | }); |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7928 | ASTContext &Ctx = D->getASTContext(); |
| 7929 | auto It = std::unique( |
| 7930 | AvailabilityAttrs.begin(), AvailabilityAttrs.end(), |
| 7931 | [&Ctx](AvailabilityAttr *LHS, AvailabilityAttr *RHS) { |
| 7932 | if (LHS->getPlatform() != RHS->getPlatform()) |
| 7933 | return false; |
| 7934 | |
| 7935 | if (LHS->getIntroduced() == RHS->getIntroduced() && |
| 7936 | LHS->getDeprecated() == RHS->getDeprecated() && |
| 7937 | LHS->getObsoleted() == RHS->getObsoleted() && |
| 7938 | LHS->getMessage() == RHS->getMessage() && |
| 7939 | LHS->getReplacement() == RHS->getReplacement()) |
| 7940 | return true; |
| 7941 | |
| 7942 | if ((!LHS->getIntroduced().empty() && !RHS->getIntroduced().empty()) || |
| 7943 | (!LHS->getDeprecated().empty() && !RHS->getDeprecated().empty()) || |
| 7944 | (!LHS->getObsoleted().empty() && !RHS->getObsoleted().empty())) |
| 7945 | return false; |
| 7946 | |
| 7947 | if (LHS->getIntroduced().empty() && !RHS->getIntroduced().empty()) |
| 7948 | LHS->setIntroduced(Ctx, RHS->getIntroduced()); |
| 7949 | |
| 7950 | if (LHS->getDeprecated().empty() && !RHS->getDeprecated().empty()) { |
| 7951 | LHS->setDeprecated(Ctx, RHS->getDeprecated()); |
| 7952 | if (LHS->getMessage().empty()) |
| 7953 | LHS->setMessage(Ctx, RHS->getMessage()); |
| 7954 | if (LHS->getReplacement().empty()) |
| 7955 | LHS->setReplacement(Ctx, RHS->getReplacement()); |
| 7956 | } |
| 7957 | |
| 7958 | if (LHS->getObsoleted().empty() && !RHS->getObsoleted().empty()) { |
| 7959 | LHS->setObsoleted(Ctx, RHS->getObsoleted()); |
| 7960 | if (LHS->getMessage().empty()) |
| 7961 | LHS->setMessage(Ctx, RHS->getMessage()); |
| 7962 | if (LHS->getReplacement().empty()) |
| 7963 | LHS->setReplacement(Ctx, RHS->getReplacement()); |
| 7964 | } |
| 7965 | |
| 7966 | return true; |
| 7967 | }); |
| 7968 | AvailabilityAttrs.erase(It, AvailabilityAttrs.end()); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7969 | } |
| 7970 | |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7971 | int clang_getCursorPlatformAvailability(CXCursor cursor, int *always_deprecated, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7972 | CXString *deprecated_message, |
| 7973 | int *always_unavailable, |
| 7974 | CXString *unavailable_message, |
| 7975 | CXPlatformAvailability *availability, |
| 7976 | int availability_size) { |
| 7977 | if (always_deprecated) |
| 7978 | *always_deprecated = 0; |
| 7979 | if (deprecated_message) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 7980 | *deprecated_message = cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7981 | if (always_unavailable) |
| 7982 | *always_unavailable = 0; |
| 7983 | if (unavailable_message) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 7984 | *unavailable_message = cxstring::createEmpty(); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7985 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7986 | if (!clang_isDeclaration(cursor.kind)) |
| 7987 | return 0; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7988 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7989 | const Decl *D = cxcursor::getCursorDecl(cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7990 | if (!D) |
| 7991 | return 0; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7992 | |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7993 | SmallVector<AvailabilityAttr *, 8> AvailabilityAttrs; |
| 7994 | getCursorPlatformAvailabilityForDecl(D, always_deprecated, deprecated_message, |
| 7995 | always_unavailable, unavailable_message, |
| 7996 | AvailabilityAttrs); |
| 7997 | for (const auto &Avail : |
| 7998 | llvm::enumerate(llvm::makeArrayRef(AvailabilityAttrs) |
| 7999 | .take_front(availability_size))) { |
| 8000 | availability[Avail.index()].Platform = |
| 8001 | cxstring::createDup(Avail.value()->getPlatform()->getName()); |
| 8002 | availability[Avail.index()].Introduced = |
| 8003 | convertVersion(Avail.value()->getIntroduced()); |
| 8004 | availability[Avail.index()].Deprecated = |
| 8005 | convertVersion(Avail.value()->getDeprecated()); |
| 8006 | availability[Avail.index()].Obsoleted = |
| 8007 | convertVersion(Avail.value()->getObsoleted()); |
| 8008 | availability[Avail.index()].Unavailable = Avail.value()->getUnavailable(); |
| 8009 | availability[Avail.index()].Message = |
| 8010 | cxstring::createDup(Avail.value()->getMessage()); |
| 8011 | } |
| 8012 | |
| 8013 | return AvailabilityAttrs.size(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8014 | } |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 8015 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8016 | void clang_disposeCXPlatformAvailability(CXPlatformAvailability *availability) { |
| 8017 | clang_disposeString(availability->Platform); |
| 8018 | clang_disposeString(availability->Message); |
| 8019 | } |
| 8020 | |
| 8021 | CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { |
| 8022 | if (clang_isDeclaration(cursor.kind)) |
| 8023 | return getDeclLanguage(cxcursor::getCursorDecl(cursor)); |
| 8024 | |
| 8025 | return CXLanguage_Invalid; |
| 8026 | } |
| 8027 | |
Saleem Abdulrasool | 50bc565 | 2017-09-13 02:15:09 +0000 | [diff] [blame] | 8028 | CXTLSKind clang_getCursorTLSKind(CXCursor cursor) { |
| 8029 | const Decl *D = cxcursor::getCursorDecl(cursor); |
| 8030 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 8031 | switch (VD->getTLSKind()) { |
| 8032 | case VarDecl::TLS_None: |
| 8033 | return CXTLS_None; |
| 8034 | case VarDecl::TLS_Dynamic: |
| 8035 | return CXTLS_Dynamic; |
| 8036 | case VarDecl::TLS_Static: |
| 8037 | return CXTLS_Static; |
| 8038 | } |
| 8039 | } |
| 8040 | |
| 8041 | return CXTLS_None; |
| 8042 | } |
| 8043 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8044 | /// If the given cursor is the "templated" declaration |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 8045 | /// describing a class or function template, return the class or |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8046 | /// function template. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8047 | static const Decl *maybeGetTemplateCursor(const Decl *D) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8048 | if (!D) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8049 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8050 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8051 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8052 | if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate()) |
| 8053 | return FunTmpl; |
| 8054 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8055 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8056 | if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate()) |
| 8057 | return ClassTmpl; |
| 8058 | |
| 8059 | return D; |
| 8060 | } |
| 8061 | |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 8062 | |
| 8063 | enum CX_StorageClass clang_Cursor_getStorageClass(CXCursor C) { |
| 8064 | StorageClass sc = SC_None; |
| 8065 | const Decl *D = getCursorDecl(C); |
| 8066 | if (D) { |
| 8067 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 8068 | sc = FD->getStorageClass(); |
| 8069 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 8070 | sc = VD->getStorageClass(); |
| 8071 | } else { |
| 8072 | return CX_SC_Invalid; |
| 8073 | } |
| 8074 | } else { |
| 8075 | return CX_SC_Invalid; |
| 8076 | } |
| 8077 | switch (sc) { |
| 8078 | case SC_None: |
| 8079 | return CX_SC_None; |
| 8080 | case SC_Extern: |
| 8081 | return CX_SC_Extern; |
| 8082 | case SC_Static: |
| 8083 | return CX_SC_Static; |
| 8084 | case SC_PrivateExtern: |
| 8085 | return CX_SC_PrivateExtern; |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 8086 | case SC_Auto: |
| 8087 | return CX_SC_Auto; |
| 8088 | case SC_Register: |
| 8089 | return CX_SC_Register; |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 8090 | } |
Kaelyn Takata | ab61e70 | 2014-10-15 18:03:26 +0000 | [diff] [blame] | 8091 | llvm_unreachable("Unhandled storage class!"); |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 8092 | } |
| 8093 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8094 | CXCursor clang_getCursorSemanticParent(CXCursor cursor) { |
| 8095 | if (clang_isDeclaration(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8096 | if (const Decl *D = getCursorDecl(cursor)) { |
| 8097 | const DeclContext *DC = D->getDeclContext(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8098 | if (!DC) |
| 8099 | return clang_getNullCursor(); |
| 8100 | |
| 8101 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 8102 | getCursorTU(cursor)); |
| 8103 | } |
| 8104 | } |
| 8105 | |
| 8106 | if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8107 | if (const Decl *D = getCursorDecl(cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8108 | return MakeCXCursor(D, getCursorTU(cursor)); |
| 8109 | } |
| 8110 | |
| 8111 | return clang_getNullCursor(); |
| 8112 | } |
| 8113 | |
| 8114 | CXCursor clang_getCursorLexicalParent(CXCursor cursor) { |
| 8115 | if (clang_isDeclaration(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8116 | if (const Decl *D = getCursorDecl(cursor)) { |
| 8117 | const DeclContext *DC = D->getLexicalDeclContext(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8118 | if (!DC) |
| 8119 | return clang_getNullCursor(); |
| 8120 | |
| 8121 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 8122 | getCursorTU(cursor)); |
| 8123 | } |
| 8124 | } |
| 8125 | |
| 8126 | // FIXME: Note that we can't easily compute the lexical context of a |
| 8127 | // statement or expression, so we return nothing. |
| 8128 | return clang_getNullCursor(); |
| 8129 | } |
| 8130 | |
| 8131 | CXFile clang_getIncludedFile(CXCursor cursor) { |
| 8132 | if (cursor.kind != CXCursor_InclusionDirective) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8133 | return nullptr; |
| 8134 | |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 8135 | const InclusionDirective *ID = getCursorInclusionDirective(cursor); |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 8136 | return const_cast<FileEntry *>(ID->getFile()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8137 | } |
| 8138 | |
Argyrios Kyrtzidis | 9adfd8a | 2013-04-18 22:15:49 +0000 | [diff] [blame] | 8139 | unsigned clang_Cursor_getObjCPropertyAttributes(CXCursor C, unsigned reserved) { |
| 8140 | if (C.kind != CXCursor_ObjCPropertyDecl) |
| 8141 | return CXObjCPropertyAttr_noattr; |
| 8142 | |
| 8143 | unsigned Result = CXObjCPropertyAttr_noattr; |
| 8144 | const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(getCursorDecl(C)); |
| 8145 | ObjCPropertyDecl::PropertyAttributeKind Attr = |
| 8146 | PD->getPropertyAttributesAsWritten(); |
| 8147 | |
| 8148 | #define SET_CXOBJCPROP_ATTR(A) \ |
| 8149 | if (Attr & ObjCPropertyDecl::OBJC_PR_##A) \ |
| 8150 | Result |= CXObjCPropertyAttr_##A |
| 8151 | SET_CXOBJCPROP_ATTR(readonly); |
| 8152 | SET_CXOBJCPROP_ATTR(getter); |
| 8153 | SET_CXOBJCPROP_ATTR(assign); |
| 8154 | SET_CXOBJCPROP_ATTR(readwrite); |
| 8155 | SET_CXOBJCPROP_ATTR(retain); |
| 8156 | SET_CXOBJCPROP_ATTR(copy); |
| 8157 | SET_CXOBJCPROP_ATTR(nonatomic); |
| 8158 | SET_CXOBJCPROP_ATTR(setter); |
| 8159 | SET_CXOBJCPROP_ATTR(atomic); |
| 8160 | SET_CXOBJCPROP_ATTR(weak); |
| 8161 | SET_CXOBJCPROP_ATTR(strong); |
| 8162 | SET_CXOBJCPROP_ATTR(unsafe_unretained); |
Manman Ren | 04fd4d8 | 2016-05-31 23:22:04 +0000 | [diff] [blame] | 8163 | SET_CXOBJCPROP_ATTR(class); |
Argyrios Kyrtzidis | 9adfd8a | 2013-04-18 22:15:49 +0000 | [diff] [blame] | 8164 | #undef SET_CXOBJCPROP_ATTR |
| 8165 | |
| 8166 | return Result; |
| 8167 | } |
| 8168 | |
Michael Wu | 6e88f53 | 2018-08-03 05:38:29 +0000 | [diff] [blame] | 8169 | CXString clang_Cursor_getObjCPropertyGetterName(CXCursor C) { |
| 8170 | if (C.kind != CXCursor_ObjCPropertyDecl) |
| 8171 | return cxstring::createNull(); |
| 8172 | |
| 8173 | const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(getCursorDecl(C)); |
| 8174 | Selector sel = PD->getGetterName(); |
| 8175 | if (sel.isNull()) |
| 8176 | return cxstring::createNull(); |
| 8177 | |
| 8178 | return cxstring::createDup(sel.getAsString()); |
| 8179 | } |
| 8180 | |
| 8181 | CXString clang_Cursor_getObjCPropertySetterName(CXCursor C) { |
| 8182 | if (C.kind != CXCursor_ObjCPropertyDecl) |
| 8183 | return cxstring::createNull(); |
| 8184 | |
| 8185 | const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(getCursorDecl(C)); |
| 8186 | Selector sel = PD->getSetterName(); |
| 8187 | if (sel.isNull()) |
| 8188 | return cxstring::createNull(); |
| 8189 | |
| 8190 | return cxstring::createDup(sel.getAsString()); |
| 8191 | } |
| 8192 | |
Argyrios Kyrtzidis | 9d9bc01 | 2013-04-18 23:29:12 +0000 | [diff] [blame] | 8193 | unsigned clang_Cursor_getObjCDeclQualifiers(CXCursor C) { |
| 8194 | if (!clang_isDeclaration(C.kind)) |
| 8195 | return CXObjCDeclQualifier_None; |
| 8196 | |
| 8197 | Decl::ObjCDeclQualifier QT = Decl::OBJC_TQ_None; |
| 8198 | const Decl *D = getCursorDecl(C); |
| 8199 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 8200 | QT = MD->getObjCDeclQualifier(); |
| 8201 | else if (const ParmVarDecl *PD = dyn_cast<ParmVarDecl>(D)) |
| 8202 | QT = PD->getObjCDeclQualifier(); |
| 8203 | if (QT == Decl::OBJC_TQ_None) |
| 8204 | return CXObjCDeclQualifier_None; |
| 8205 | |
| 8206 | unsigned Result = CXObjCDeclQualifier_None; |
| 8207 | if (QT & Decl::OBJC_TQ_In) Result |= CXObjCDeclQualifier_In; |
| 8208 | if (QT & Decl::OBJC_TQ_Inout) Result |= CXObjCDeclQualifier_Inout; |
| 8209 | if (QT & Decl::OBJC_TQ_Out) Result |= CXObjCDeclQualifier_Out; |
| 8210 | if (QT & Decl::OBJC_TQ_Bycopy) Result |= CXObjCDeclQualifier_Bycopy; |
| 8211 | if (QT & Decl::OBJC_TQ_Byref) Result |= CXObjCDeclQualifier_Byref; |
| 8212 | if (QT & Decl::OBJC_TQ_Oneway) Result |= CXObjCDeclQualifier_Oneway; |
| 8213 | |
| 8214 | return Result; |
| 8215 | } |
| 8216 | |
Argyrios Kyrtzidis | 7b50fc5 | 2013-07-05 20:44:37 +0000 | [diff] [blame] | 8217 | unsigned clang_Cursor_isObjCOptional(CXCursor C) { |
| 8218 | if (!clang_isDeclaration(C.kind)) |
| 8219 | return 0; |
| 8220 | |
| 8221 | const Decl *D = getCursorDecl(C); |
| 8222 | if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 8223 | return PD->getPropertyImplementation() == ObjCPropertyDecl::Optional; |
| 8224 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 8225 | return MD->getImplementationControl() == ObjCMethodDecl::Optional; |
| 8226 | |
| 8227 | return 0; |
| 8228 | } |
| 8229 | |
Argyrios Kyrtzidis | 23814e4 | 2013-04-18 23:53:05 +0000 | [diff] [blame] | 8230 | unsigned clang_Cursor_isVariadic(CXCursor C) { |
| 8231 | if (!clang_isDeclaration(C.kind)) |
| 8232 | return 0; |
| 8233 | |
| 8234 | const Decl *D = getCursorDecl(C); |
| 8235 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 8236 | return FD->isVariadic(); |
| 8237 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 8238 | return MD->isVariadic(); |
| 8239 | |
| 8240 | return 0; |
| 8241 | } |
| 8242 | |
Argyrios Kyrtzidis | 0381cc7 | 2017-05-10 15:10:36 +0000 | [diff] [blame] | 8243 | unsigned clang_Cursor_isExternalSymbol(CXCursor C, |
| 8244 | CXString *language, CXString *definedIn, |
| 8245 | unsigned *isGenerated) { |
| 8246 | if (!clang_isDeclaration(C.kind)) |
| 8247 | return 0; |
| 8248 | |
| 8249 | const Decl *D = getCursorDecl(C); |
| 8250 | |
Argyrios Kyrtzidis | 11d7048 | 2017-05-20 04:11:33 +0000 | [diff] [blame] | 8251 | if (auto *attr = D->getExternalSourceSymbolAttr()) { |
Argyrios Kyrtzidis | 0381cc7 | 2017-05-10 15:10:36 +0000 | [diff] [blame] | 8252 | if (language) |
| 8253 | *language = cxstring::createDup(attr->getLanguage()); |
| 8254 | if (definedIn) |
| 8255 | *definedIn = cxstring::createDup(attr->getDefinedIn()); |
| 8256 | if (isGenerated) |
| 8257 | *isGenerated = attr->getGeneratedDeclaration(); |
| 8258 | return 1; |
| 8259 | } |
| 8260 | return 0; |
| 8261 | } |
| 8262 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8263 | CXSourceRange clang_Cursor_getCommentRange(CXCursor C) { |
| 8264 | if (!clang_isDeclaration(C.kind)) |
| 8265 | return clang_getNullRange(); |
| 8266 | |
| 8267 | const Decl *D = getCursorDecl(C); |
| 8268 | ASTContext &Context = getCursorContext(C); |
| 8269 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 8270 | if (!RC) |
| 8271 | return clang_getNullRange(); |
| 8272 | |
| 8273 | return cxloc::translateSourceRange(Context, RC->getSourceRange()); |
| 8274 | } |
| 8275 | |
| 8276 | CXString clang_Cursor_getRawCommentText(CXCursor C) { |
| 8277 | if (!clang_isDeclaration(C.kind)) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 8278 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8279 | |
| 8280 | const Decl *D = getCursorDecl(C); |
| 8281 | ASTContext &Context = getCursorContext(C); |
| 8282 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 8283 | StringRef RawText = RC ? RC->getRawText(Context.getSourceManager()) : |
| 8284 | StringRef(); |
| 8285 | |
| 8286 | // Don't duplicate the string because RawText points directly into source |
| 8287 | // code. |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 8288 | return cxstring::createRef(RawText); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8289 | } |
| 8290 | |
| 8291 | CXString clang_Cursor_getBriefCommentText(CXCursor C) { |
| 8292 | if (!clang_isDeclaration(C.kind)) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 8293 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8294 | |
| 8295 | const Decl *D = getCursorDecl(C); |
| 8296 | const ASTContext &Context = getCursorContext(C); |
| 8297 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 8298 | |
| 8299 | if (RC) { |
| 8300 | StringRef BriefText = RC->getBriefText(Context); |
| 8301 | |
| 8302 | // Don't duplicate the string because RawComment ensures that this memory |
| 8303 | // will not go away. |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 8304 | return cxstring::createRef(BriefText); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8305 | } |
| 8306 | |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 8307 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8308 | } |
| 8309 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8310 | CXModule clang_Cursor_getModule(CXCursor C) { |
| 8311 | if (C.kind == CXCursor_ModuleImportDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8312 | if (const ImportDecl *ImportD = |
| 8313 | dyn_cast_or_null<ImportDecl>(getCursorDecl(C))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8314 | return ImportD->getImportedModule(); |
| 8315 | } |
| 8316 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8317 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8318 | } |
| 8319 | |
Argyrios Kyrtzidis | f6d49c3 | 2014-05-14 23:14:37 +0000 | [diff] [blame] | 8320 | CXModule clang_getModuleForFile(CXTranslationUnit TU, CXFile File) { |
| 8321 | if (isNotUsableTU(TU)) { |
| 8322 | LOG_BAD_TU(TU); |
| 8323 | return nullptr; |
| 8324 | } |
| 8325 | if (!File) |
| 8326 | return nullptr; |
| 8327 | FileEntry *FE = static_cast<FileEntry *>(File); |
| 8328 | |
| 8329 | ASTUnit &Unit = *cxtu::getASTUnit(TU); |
| 8330 | HeaderSearch &HS = Unit.getPreprocessor().getHeaderSearchInfo(); |
| 8331 | ModuleMap::KnownHeader Header = HS.findModuleForHeader(FE); |
| 8332 | |
Richard Smith | feb54b6 | 2014-10-23 02:01:19 +0000 | [diff] [blame] | 8333 | return Header.getModule(); |
Argyrios Kyrtzidis | f6d49c3 | 2014-05-14 23:14:37 +0000 | [diff] [blame] | 8334 | } |
| 8335 | |
Argyrios Kyrtzidis | 12fdb9e | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 8336 | CXFile clang_Module_getASTFile(CXModule CXMod) { |
| 8337 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8338 | return nullptr; |
Argyrios Kyrtzidis | 12fdb9e | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 8339 | Module *Mod = static_cast<Module*>(CXMod); |
| 8340 | return const_cast<FileEntry *>(Mod->getASTFile()); |
| 8341 | } |
| 8342 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8343 | CXModule clang_Module_getParent(CXModule CXMod) { |
| 8344 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8345 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8346 | Module *Mod = static_cast<Module*>(CXMod); |
| 8347 | return Mod->Parent; |
| 8348 | } |
| 8349 | |
| 8350 | CXString clang_Module_getName(CXModule CXMod) { |
| 8351 | if (!CXMod) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 8352 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8353 | Module *Mod = static_cast<Module*>(CXMod); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 8354 | return cxstring::createDup(Mod->Name); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8355 | } |
| 8356 | |
| 8357 | CXString clang_Module_getFullName(CXModule CXMod) { |
| 8358 | if (!CXMod) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 8359 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8360 | Module *Mod = static_cast<Module*>(CXMod); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 8361 | return cxstring::createDup(Mod->getFullModuleName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8362 | } |
| 8363 | |
Argyrios Kyrtzidis | 884337f | 2014-05-15 04:44:25 +0000 | [diff] [blame] | 8364 | int clang_Module_isSystem(CXModule CXMod) { |
| 8365 | if (!CXMod) |
| 8366 | return 0; |
| 8367 | Module *Mod = static_cast<Module*>(CXMod); |
| 8368 | return Mod->IsSystem; |
| 8369 | } |
| 8370 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 8371 | unsigned clang_Module_getNumTopLevelHeaders(CXTranslationUnit TU, |
| 8372 | CXModule CXMod) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 8373 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 8374 | LOG_BAD_TU(TU); |
| 8375 | return 0; |
| 8376 | } |
| 8377 | if (!CXMod) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8378 | return 0; |
| 8379 | Module *Mod = static_cast<Module*>(CXMod); |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 8380 | FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager(); |
| 8381 | ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr); |
| 8382 | return TopHeaders.size(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8383 | } |
| 8384 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 8385 | CXFile clang_Module_getTopLevelHeader(CXTranslationUnit TU, |
| 8386 | CXModule CXMod, unsigned Index) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 8387 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 8388 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8389 | return nullptr; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 8390 | } |
| 8391 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8392 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8393 | Module *Mod = static_cast<Module*>(CXMod); |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 8394 | FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8395 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 8396 | ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr); |
| 8397 | if (Index < TopHeaders.size()) |
| 8398 | return const_cast<FileEntry *>(TopHeaders[Index]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8399 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8400 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8401 | } |
| 8402 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8403 | //===----------------------------------------------------------------------===// |
| 8404 | // C++ AST instrospection. |
| 8405 | //===----------------------------------------------------------------------===// |
| 8406 | |
Jonathan Coe | 2956535 | 2016-04-27 12:48:25 +0000 | [diff] [blame] | 8407 | unsigned clang_CXXConstructor_isDefaultConstructor(CXCursor C) { |
| 8408 | if (!clang_isDeclaration(C.kind)) |
| 8409 | return 0; |
| 8410 | |
| 8411 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8412 | const CXXConstructorDecl *Constructor = |
| 8413 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 8414 | return (Constructor && Constructor->isDefaultConstructor()) ? 1 : 0; |
| 8415 | } |
| 8416 | |
| 8417 | unsigned clang_CXXConstructor_isCopyConstructor(CXCursor C) { |
| 8418 | if (!clang_isDeclaration(C.kind)) |
| 8419 | return 0; |
| 8420 | |
| 8421 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8422 | const CXXConstructorDecl *Constructor = |
| 8423 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 8424 | return (Constructor && Constructor->isCopyConstructor()) ? 1 : 0; |
| 8425 | } |
| 8426 | |
| 8427 | unsigned clang_CXXConstructor_isMoveConstructor(CXCursor C) { |
| 8428 | if (!clang_isDeclaration(C.kind)) |
| 8429 | return 0; |
| 8430 | |
| 8431 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8432 | const CXXConstructorDecl *Constructor = |
| 8433 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 8434 | return (Constructor && Constructor->isMoveConstructor()) ? 1 : 0; |
| 8435 | } |
| 8436 | |
| 8437 | unsigned clang_CXXConstructor_isConvertingConstructor(CXCursor C) { |
| 8438 | if (!clang_isDeclaration(C.kind)) |
| 8439 | return 0; |
| 8440 | |
| 8441 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8442 | const CXXConstructorDecl *Constructor = |
| 8443 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 8444 | // Passing 'false' excludes constructors marked 'explicit'. |
| 8445 | return (Constructor && Constructor->isConvertingConstructor(false)) ? 1 : 0; |
| 8446 | } |
| 8447 | |
Saleem Abdulrasool | 6ea75db | 2015-10-27 15:50:22 +0000 | [diff] [blame] | 8448 | unsigned clang_CXXField_isMutable(CXCursor C) { |
| 8449 | if (!clang_isDeclaration(C.kind)) |
| 8450 | return 0; |
| 8451 | |
| 8452 | if (const auto D = cxcursor::getCursorDecl(C)) |
| 8453 | if (const auto FD = dyn_cast_or_null<FieldDecl>(D)) |
| 8454 | return FD->isMutable() ? 1 : 0; |
| 8455 | return 0; |
| 8456 | } |
| 8457 | |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 8458 | unsigned clang_CXXMethod_isPureVirtual(CXCursor C) { |
| 8459 | if (!clang_isDeclaration(C.kind)) |
| 8460 | return 0; |
| 8461 | |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 8462 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 8463 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8464 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 8465 | return (Method && Method->isVirtual() && Method->isPure()) ? 1 : 0; |
| 8466 | } |
| 8467 | |
Dmitri Gribenko | e570ede | 2014-04-07 14:59:13 +0000 | [diff] [blame] | 8468 | unsigned clang_CXXMethod_isConst(CXCursor C) { |
| 8469 | if (!clang_isDeclaration(C.kind)) |
| 8470 | return 0; |
| 8471 | |
| 8472 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8473 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8474 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 8475 | return (Method && Method->getMethodQualifiers().hasConst()) ? 1 : 0; |
Dmitri Gribenko | e570ede | 2014-04-07 14:59:13 +0000 | [diff] [blame] | 8476 | } |
| 8477 | |
Jonathan Coe | 2956535 | 2016-04-27 12:48:25 +0000 | [diff] [blame] | 8478 | unsigned clang_CXXMethod_isDefaulted(CXCursor C) { |
| 8479 | if (!clang_isDeclaration(C.kind)) |
| 8480 | return 0; |
| 8481 | |
| 8482 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8483 | const CXXMethodDecl *Method = |
| 8484 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
| 8485 | return (Method && Method->isDefaulted()) ? 1 : 0; |
| 8486 | } |
| 8487 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8488 | unsigned clang_CXXMethod_isStatic(CXCursor C) { |
| 8489 | if (!clang_isDeclaration(C.kind)) |
| 8490 | return 0; |
| 8491 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8492 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 8493 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8494 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8495 | return (Method && Method->isStatic()) ? 1 : 0; |
| 8496 | } |
| 8497 | |
| 8498 | unsigned clang_CXXMethod_isVirtual(CXCursor C) { |
| 8499 | if (!clang_isDeclaration(C.kind)) |
| 8500 | return 0; |
| 8501 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 8502 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 8503 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8504 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8505 | return (Method && Method->isVirtual()) ? 1 : 0; |
| 8506 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8507 | |
Alex Lorenz | 34ccadc | 2017-12-14 22:01:50 +0000 | [diff] [blame] | 8508 | unsigned clang_CXXRecord_isAbstract(CXCursor C) { |
| 8509 | if (!clang_isDeclaration(C.kind)) |
| 8510 | return 0; |
| 8511 | |
| 8512 | const auto *D = cxcursor::getCursorDecl(C); |
| 8513 | const auto *RD = dyn_cast_or_null<CXXRecordDecl>(D); |
| 8514 | if (RD) |
| 8515 | RD = RD->getDefinition(); |
| 8516 | return (RD && RD->isAbstract()) ? 1 : 0; |
| 8517 | } |
| 8518 | |
Alex Lorenz | ff7f42e | 2017-07-12 11:35:11 +0000 | [diff] [blame] | 8519 | unsigned clang_EnumDecl_isScoped(CXCursor C) { |
| 8520 | if (!clang_isDeclaration(C.kind)) |
| 8521 | return 0; |
| 8522 | |
| 8523 | const Decl *D = cxcursor::getCursorDecl(C); |
| 8524 | auto *Enum = dyn_cast_or_null<EnumDecl>(D); |
| 8525 | return (Enum && Enum->isScoped()) ? 1 : 0; |
| 8526 | } |
| 8527 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8528 | //===----------------------------------------------------------------------===// |
| 8529 | // Attribute introspection. |
| 8530 | //===----------------------------------------------------------------------===// |
| 8531 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8532 | CXType clang_getIBOutletCollectionType(CXCursor C) { |
| 8533 | if (C.kind != CXCursor_IBOutletCollectionAttr) |
| 8534 | return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C)); |
| 8535 | |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 8536 | const IBOutletCollectionAttr *A = |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8537 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); |
| 8538 | |
| 8539 | return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C)); |
| 8540 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8541 | |
| 8542 | //===----------------------------------------------------------------------===// |
| 8543 | // Inspecting memory usage. |
| 8544 | //===----------------------------------------------------------------------===// |
| 8545 | |
| 8546 | typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries; |
| 8547 | |
| 8548 | static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries, |
| 8549 | enum CXTUResourceUsageKind k, |
| 8550 | unsigned long amount) { |
| 8551 | CXTUResourceUsageEntry entry = { k, amount }; |
| 8552 | entries.push_back(entry); |
| 8553 | } |
| 8554 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8555 | const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) { |
| 8556 | const char *str = ""; |
| 8557 | switch (kind) { |
| 8558 | case CXTUResourceUsage_AST: |
| 8559 | str = "ASTContext: expressions, declarations, and types"; |
| 8560 | break; |
| 8561 | case CXTUResourceUsage_Identifiers: |
| 8562 | str = "ASTContext: identifiers"; |
| 8563 | break; |
| 8564 | case CXTUResourceUsage_Selectors: |
| 8565 | str = "ASTContext: selectors"; |
| 8566 | break; |
| 8567 | case CXTUResourceUsage_GlobalCompletionResults: |
| 8568 | str = "Code completion: cached global results"; |
| 8569 | break; |
| 8570 | case CXTUResourceUsage_SourceManagerContentCache: |
| 8571 | str = "SourceManager: content cache allocator"; |
| 8572 | break; |
| 8573 | case CXTUResourceUsage_AST_SideTables: |
| 8574 | str = "ASTContext: side tables"; |
| 8575 | break; |
| 8576 | case CXTUResourceUsage_SourceManager_Membuffer_Malloc: |
| 8577 | str = "SourceManager: malloc'ed memory buffers"; |
| 8578 | break; |
| 8579 | case CXTUResourceUsage_SourceManager_Membuffer_MMap: |
| 8580 | str = "SourceManager: mmap'ed memory buffers"; |
| 8581 | break; |
| 8582 | case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc: |
| 8583 | str = "ExternalASTSource: malloc'ed memory buffers"; |
| 8584 | break; |
| 8585 | case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap: |
| 8586 | str = "ExternalASTSource: mmap'ed memory buffers"; |
| 8587 | break; |
| 8588 | case CXTUResourceUsage_Preprocessor: |
| 8589 | str = "Preprocessor: malloc'ed memory"; |
| 8590 | break; |
| 8591 | case CXTUResourceUsage_PreprocessingRecord: |
| 8592 | str = "Preprocessor: PreprocessingRecord"; |
| 8593 | break; |
| 8594 | case CXTUResourceUsage_SourceManager_DataStructures: |
| 8595 | str = "SourceManager: data structures and tables"; |
| 8596 | break; |
| 8597 | case CXTUResourceUsage_Preprocessor_HeaderSearch: |
| 8598 | str = "Preprocessor: header search tables"; |
| 8599 | break; |
| 8600 | } |
| 8601 | return str; |
| 8602 | } |
| 8603 | |
| 8604 | CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 8605 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 8606 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8607 | CXTUResourceUsage usage = { (void*) nullptr, 0, nullptr }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8608 | return usage; |
| 8609 | } |
| 8610 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8611 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 8612 | std::unique_ptr<MemUsageEntries> entries(new MemUsageEntries()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8613 | ASTContext &astContext = astUnit->getASTContext(); |
| 8614 | |
| 8615 | // How much memory is used by AST nodes and types? |
| 8616 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST, |
| 8617 | (unsigned long) astContext.getASTAllocatedMemory()); |
| 8618 | |
| 8619 | // How much memory is used by identifiers? |
| 8620 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers, |
| 8621 | (unsigned long) astContext.Idents.getAllocator().getTotalMemory()); |
| 8622 | |
| 8623 | // How much memory is used for selectors? |
| 8624 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors, |
| 8625 | (unsigned long) astContext.Selectors.getTotalMemory()); |
| 8626 | |
| 8627 | // How much memory is used by ASTContext's side tables? |
| 8628 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables, |
| 8629 | (unsigned long) astContext.getSideTableAllocatedMemory()); |
| 8630 | |
| 8631 | // How much memory is used for caching global code completion results? |
| 8632 | unsigned long completionBytes = 0; |
| 8633 | if (GlobalCodeCompletionAllocator *completionAllocator = |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 8634 | astUnit->getCachedCompletionAllocator().get()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8635 | completionBytes = completionAllocator->getTotalMemory(); |
| 8636 | } |
| 8637 | createCXTUResourceUsageEntry(*entries, |
| 8638 | CXTUResourceUsage_GlobalCompletionResults, |
| 8639 | completionBytes); |
| 8640 | |
| 8641 | // How much memory is being used by SourceManager's content cache? |
| 8642 | createCXTUResourceUsageEntry(*entries, |
| 8643 | CXTUResourceUsage_SourceManagerContentCache, |
| 8644 | (unsigned long) astContext.getSourceManager().getContentCacheSize()); |
| 8645 | |
| 8646 | // How much memory is being used by the MemoryBuffer's in SourceManager? |
| 8647 | const SourceManager::MemoryBufferSizes &srcBufs = |
| 8648 | astUnit->getSourceManager().getMemoryBufferSizes(); |
| 8649 | |
| 8650 | createCXTUResourceUsageEntry(*entries, |
| 8651 | CXTUResourceUsage_SourceManager_Membuffer_Malloc, |
| 8652 | (unsigned long) srcBufs.malloc_bytes); |
| 8653 | createCXTUResourceUsageEntry(*entries, |
| 8654 | CXTUResourceUsage_SourceManager_Membuffer_MMap, |
| 8655 | (unsigned long) srcBufs.mmap_bytes); |
| 8656 | createCXTUResourceUsageEntry(*entries, |
| 8657 | CXTUResourceUsage_SourceManager_DataStructures, |
| 8658 | (unsigned long) astContext.getSourceManager() |
| 8659 | .getDataStructureSizes()); |
| 8660 | |
| 8661 | // How much memory is being used by the ExternalASTSource? |
| 8662 | if (ExternalASTSource *esrc = astContext.getExternalSource()) { |
| 8663 | const ExternalASTSource::MemoryBufferSizes &sizes = |
| 8664 | esrc->getMemoryBufferSizes(); |
| 8665 | |
| 8666 | createCXTUResourceUsageEntry(*entries, |
| 8667 | CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc, |
| 8668 | (unsigned long) sizes.malloc_bytes); |
| 8669 | createCXTUResourceUsageEntry(*entries, |
| 8670 | CXTUResourceUsage_ExternalASTSource_Membuffer_MMap, |
| 8671 | (unsigned long) sizes.mmap_bytes); |
| 8672 | } |
| 8673 | |
| 8674 | // How much memory is being used by the Preprocessor? |
| 8675 | Preprocessor &pp = astUnit->getPreprocessor(); |
| 8676 | createCXTUResourceUsageEntry(*entries, |
| 8677 | CXTUResourceUsage_Preprocessor, |
| 8678 | pp.getTotalMemory()); |
| 8679 | |
| 8680 | if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) { |
| 8681 | createCXTUResourceUsageEntry(*entries, |
| 8682 | CXTUResourceUsage_PreprocessingRecord, |
| 8683 | pRec->getTotalMemory()); |
| 8684 | } |
| 8685 | |
| 8686 | createCXTUResourceUsageEntry(*entries, |
| 8687 | CXTUResourceUsage_Preprocessor_HeaderSearch, |
| 8688 | pp.getHeaderSearchInfo().getTotalMemory()); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8689 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8690 | CXTUResourceUsage usage = { (void*) entries.get(), |
| 8691 | (unsigned) entries->size(), |
Alexander Kornienko | 6ee521c | 2015-01-23 15:36:10 +0000 | [diff] [blame] | 8692 | !entries->empty() ? &(*entries)[0] : nullptr }; |
Eric Fiselier | e95fc44 | 2016-11-14 07:03:50 +0000 | [diff] [blame] | 8693 | (void)entries.release(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8694 | return usage; |
| 8695 | } |
| 8696 | |
| 8697 | void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) { |
| 8698 | if (usage.data) |
| 8699 | delete (MemUsageEntries*) usage.data; |
| 8700 | } |
| 8701 | |
Argyrios Kyrtzidis | 0e282ef | 2013-12-06 18:55:45 +0000 | [diff] [blame] | 8702 | CXSourceRangeList *clang_getSkippedRanges(CXTranslationUnit TU, CXFile file) { |
| 8703 | CXSourceRangeList *skipped = new CXSourceRangeList; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8704 | skipped->count = 0; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8705 | skipped->ranges = nullptr; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8706 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 8707 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 8708 | LOG_BAD_TU(TU); |
| 8709 | return skipped; |
| 8710 | } |
| 8711 | |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8712 | if (!file) |
| 8713 | return skipped; |
| 8714 | |
| 8715 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
| 8716 | PreprocessingRecord *ppRec = astUnit->getPreprocessor().getPreprocessingRecord(); |
| 8717 | if (!ppRec) |
| 8718 | return skipped; |
| 8719 | |
| 8720 | ASTContext &Ctx = astUnit->getASTContext(); |
| 8721 | SourceManager &sm = Ctx.getSourceManager(); |
| 8722 | FileEntry *fileEntry = static_cast<FileEntry *>(file); |
| 8723 | FileID wantedFileID = sm.translateFile(fileEntry); |
Cameron Desrochers | b60f1b6 | 2018-01-15 19:14:16 +0000 | [diff] [blame] | 8724 | bool isMainFile = wantedFileID == sm.getMainFileID(); |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8725 | |
| 8726 | const std::vector<SourceRange> &SkippedRanges = ppRec->getSkippedRanges(); |
| 8727 | std::vector<SourceRange> wantedRanges; |
| 8728 | for (std::vector<SourceRange>::const_iterator i = SkippedRanges.begin(), ei = SkippedRanges.end(); |
| 8729 | i != ei; ++i) { |
| 8730 | if (sm.getFileID(i->getBegin()) == wantedFileID || sm.getFileID(i->getEnd()) == wantedFileID) |
| 8731 | wantedRanges.push_back(*i); |
Cameron Desrochers | b60f1b6 | 2018-01-15 19:14:16 +0000 | [diff] [blame] | 8732 | else if (isMainFile && (astUnit->isInPreambleFileID(i->getBegin()) || astUnit->isInPreambleFileID(i->getEnd()))) |
| 8733 | wantedRanges.push_back(*i); |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8734 | } |
| 8735 | |
| 8736 | skipped->count = wantedRanges.size(); |
| 8737 | skipped->ranges = new CXSourceRange[skipped->count]; |
| 8738 | for (unsigned i = 0, ei = skipped->count; i != ei; ++i) |
| 8739 | skipped->ranges[i] = cxloc::translateSourceRange(Ctx, wantedRanges[i]); |
| 8740 | |
| 8741 | return skipped; |
| 8742 | } |
| 8743 | |
Cameron Desrochers | d809128 | 2016-08-18 15:43:55 +0000 | [diff] [blame] | 8744 | CXSourceRangeList *clang_getAllSkippedRanges(CXTranslationUnit TU) { |
| 8745 | CXSourceRangeList *skipped = new CXSourceRangeList; |
| 8746 | skipped->count = 0; |
| 8747 | skipped->ranges = nullptr; |
| 8748 | |
| 8749 | if (isNotUsableTU(TU)) { |
| 8750 | LOG_BAD_TU(TU); |
| 8751 | return skipped; |
| 8752 | } |
| 8753 | |
| 8754 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
| 8755 | PreprocessingRecord *ppRec = astUnit->getPreprocessor().getPreprocessingRecord(); |
| 8756 | if (!ppRec) |
| 8757 | return skipped; |
| 8758 | |
| 8759 | ASTContext &Ctx = astUnit->getASTContext(); |
| 8760 | |
| 8761 | const std::vector<SourceRange> &SkippedRanges = ppRec->getSkippedRanges(); |
| 8762 | |
| 8763 | skipped->count = SkippedRanges.size(); |
| 8764 | skipped->ranges = new CXSourceRange[skipped->count]; |
| 8765 | for (unsigned i = 0, ei = skipped->count; i != ei; ++i) |
| 8766 | skipped->ranges[i] = cxloc::translateSourceRange(Ctx, SkippedRanges[i]); |
| 8767 | |
| 8768 | return skipped; |
| 8769 | } |
| 8770 | |
Argyrios Kyrtzidis | 0e282ef | 2013-12-06 18:55:45 +0000 | [diff] [blame] | 8771 | void clang_disposeSourceRangeList(CXSourceRangeList *ranges) { |
| 8772 | if (ranges) { |
| 8773 | delete[] ranges->ranges; |
| 8774 | delete ranges; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8775 | } |
| 8776 | } |
| 8777 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8778 | void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) { |
| 8779 | CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU); |
| 8780 | for (unsigned I = 0; I != Usage.numEntries; ++I) |
| 8781 | fprintf(stderr, " %s: %lu\n", |
| 8782 | clang_getTUResourceUsageName(Usage.entries[I].kind), |
| 8783 | Usage.entries[I].amount); |
| 8784 | |
| 8785 | clang_disposeCXTUResourceUsage(Usage); |
| 8786 | } |
| 8787 | |
| 8788 | //===----------------------------------------------------------------------===// |
| 8789 | // Misc. utility functions. |
| 8790 | //===----------------------------------------------------------------------===// |
| 8791 | |
Richard Smith | 0a7b297 | 2018-07-03 21:34:13 +0000 | [diff] [blame] | 8792 | /// Default to using our desired 8 MB stack size on "safety" threads. |
| 8793 | static unsigned SafetyStackThreadSize = DesiredStackSize; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8794 | |
| 8795 | namespace clang { |
| 8796 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 8797 | bool RunSafely(llvm::CrashRecoveryContext &CRC, llvm::function_ref<void()> Fn, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8798 | unsigned Size) { |
| 8799 | if (!Size) |
| 8800 | Size = GetSafetyThreadStackSize(); |
Erik Verbruggen | 3cc3911 | 2017-11-14 09:34:39 +0000 | [diff] [blame] | 8801 | if (Size && !getenv("LIBCLANG_NOTHREADS")) |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 8802 | return CRC.RunSafelyOnThread(Fn, Size); |
| 8803 | return CRC.RunSafely(Fn); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8804 | } |
| 8805 | |
| 8806 | unsigned GetSafetyThreadStackSize() { |
| 8807 | return SafetyStackThreadSize; |
| 8808 | } |
| 8809 | |
| 8810 | void SetSafetyThreadStackSize(unsigned Value) { |
| 8811 | SafetyStackThreadSize = Value; |
| 8812 | } |
| 8813 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 8814 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8815 | |
| 8816 | void clang::setThreadBackgroundPriority() { |
| 8817 | if (getenv("LIBCLANG_BGPRIO_DISABLE")) |
| 8818 | return; |
| 8819 | |
Nico Weber | 18cfd9f | 2019-04-21 19:18:41 +0000 | [diff] [blame] | 8820 | #if LLVM_ENABLE_THREADS |
Kadir Cetinkaya | b8f82ca | 2019-04-18 13:49:20 +0000 | [diff] [blame] | 8821 | llvm::set_thread_priority(llvm::ThreadPriority::Background); |
Nico Weber | 18cfd9f | 2019-04-21 19:18:41 +0000 | [diff] [blame] | 8822 | #endif |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8823 | } |
| 8824 | |
| 8825 | void cxindex::printDiagsToStderr(ASTUnit *Unit) { |
| 8826 | if (!Unit) |
| 8827 | return; |
| 8828 | |
| 8829 | for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), |
| 8830 | DEnd = Unit->stored_diag_end(); |
| 8831 | D != DEnd; ++D) { |
Ben Langmuir | 749323f | 2014-04-22 17:40:12 +0000 | [diff] [blame] | 8832 | CXStoredDiagnostic Diag(*D, Unit->getLangOpts()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8833 | CXString Msg = clang_formatDiagnostic(&Diag, |
| 8834 | clang_defaultDiagnosticDisplayOptions()); |
| 8835 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 8836 | clang_disposeString(Msg); |
| 8837 | } |
Nico Weber | 1865df4 | 2018-04-27 19:11:14 +0000 | [diff] [blame] | 8838 | #ifdef _WIN32 |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8839 | // On Windows, force a flush, since there may be multiple copies of |
| 8840 | // stderr and stdout in the file system, all with different buffers |
| 8841 | // but writing to the same device. |
| 8842 | fflush(stderr); |
| 8843 | #endif |
| 8844 | } |
| 8845 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8846 | MacroInfo *cxindex::getMacroInfo(const IdentifierInfo &II, |
| 8847 | SourceLocation MacroDefLoc, |
| 8848 | CXTranslationUnit TU){ |
| 8849 | if (MacroDefLoc.isInvalid() || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8850 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8851 | if (!II.hadMacroDefinition()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8852 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8853 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8854 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 8855 | Preprocessor &PP = Unit->getPreprocessor(); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 8856 | MacroDirective *MD = PP.getLocalMacroDirectiveHistory(&II); |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 8857 | if (MD) { |
| 8858 | for (MacroDirective::DefInfo |
| 8859 | Def = MD->getDefinition(); Def; Def = Def.getPreviousDefinition()) { |
| 8860 | if (MacroDefLoc == Def.getMacroInfo()->getDefinitionLoc()) |
| 8861 | return Def.getMacroInfo(); |
| 8862 | } |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8863 | } |
| 8864 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8865 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8866 | } |
| 8867 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 8868 | const MacroInfo *cxindex::getMacroInfo(const MacroDefinitionRecord *MacroDef, |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 8869 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8870 | if (!MacroDef || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8871 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8872 | const IdentifierInfo *II = MacroDef->getName(); |
| 8873 | if (!II) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8874 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8875 | |
| 8876 | return getMacroInfo(*II, MacroDef->getLocation(), TU); |
| 8877 | } |
| 8878 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 8879 | MacroDefinitionRecord * |
| 8880 | cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, const Token &Tok, |
| 8881 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8882 | if (!MI || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8883 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8884 | if (Tok.isNot(tok::raw_identifier)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8885 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8886 | |
| 8887 | if (MI->getNumTokens() == 0) |
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 | SourceRange DefRange(MI->getReplacementToken(0).getLocation(), |
| 8890 | MI->getDefinitionEndLoc()); |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8891 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8892 | |
| 8893 | // Check that the token is inside the definition and not its argument list. |
| 8894 | SourceManager &SM = Unit->getSourceManager(); |
| 8895 | if (SM.isBeforeInTranslationUnit(Tok.getLocation(), DefRange.getBegin())) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8896 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8897 | if (SM.isBeforeInTranslationUnit(DefRange.getEnd(), Tok.getLocation())) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8898 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8899 | |
| 8900 | Preprocessor &PP = Unit->getPreprocessor(); |
| 8901 | PreprocessingRecord *PPRec = PP.getPreprocessingRecord(); |
| 8902 | if (!PPRec) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8903 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8904 | |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 8905 | IdentifierInfo &II = PP.getIdentifierTable().get(Tok.getRawIdentifier()); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8906 | if (!II.hadMacroDefinition()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8907 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8908 | |
| 8909 | // Check that the identifier is not one of the macro arguments. |
Faisal Vali | ac506d7 | 2017-07-17 17:18:43 +0000 | [diff] [blame] | 8910 | 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] | 8911 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8912 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 8913 | MacroDirective *InnerMD = PP.getLocalMacroDirectiveHistory(&II); |
Argyrios Kyrtzidis | 09c9e81 | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 8914 | if (!InnerMD) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8915 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8916 | |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 8917 | return PPRec->findMacroDefinition(InnerMD->getMacroInfo()); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8918 | } |
| 8919 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 8920 | MacroDefinitionRecord * |
| 8921 | cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, SourceLocation Loc, |
| 8922 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8923 | if (Loc.isInvalid() || !MI || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8924 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8925 | |
| 8926 | if (MI->getNumTokens() == 0) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8927 | return nullptr; |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8928 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8929 | Preprocessor &PP = Unit->getPreprocessor(); |
| 8930 | if (!PP.getPreprocessingRecord()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8931 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8932 | Loc = Unit->getSourceManager().getSpellingLoc(Loc); |
| 8933 | Token Tok; |
| 8934 | if (PP.getRawToken(Loc, Tok)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8935 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8936 | |
| 8937 | return checkForMacroInMacroDefinition(MI, Tok, TU); |
| 8938 | } |
| 8939 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8940 | CXString clang_getClangVersion() { |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 8941 | return cxstring::createDup(getClangFullVersion()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8942 | } |
| 8943 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8944 | Logger &cxindex::Logger::operator<<(CXTranslationUnit TU) { |
| 8945 | if (TU) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8946 | if (ASTUnit *Unit = cxtu::getASTUnit(TU)) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8947 | LogOS << '<' << Unit->getMainFileName() << '>'; |
Argyrios Kyrtzidis | 37f2ab4 | 2013-03-05 20:21:14 +0000 | [diff] [blame] | 8948 | if (Unit->isMainFileAST()) |
| 8949 | LogOS << " (" << Unit->getASTFileName() << ')'; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8950 | return *this; |
| 8951 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 8952 | } else { |
| 8953 | LogOS << "<NULL TU>"; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8954 | } |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8955 | return *this; |
| 8956 | } |
| 8957 | |
Argyrios Kyrtzidis | ba4b5f8 | 2013-03-08 02:32:26 +0000 | [diff] [blame] | 8958 | Logger &cxindex::Logger::operator<<(const FileEntry *FE) { |
| 8959 | *this << FE->getName(); |
| 8960 | return *this; |
| 8961 | } |
| 8962 | |
| 8963 | Logger &cxindex::Logger::operator<<(CXCursor cursor) { |
| 8964 | CXString cursorName = clang_getCursorDisplayName(cursor); |
| 8965 | *this << cursorName << "@" << clang_getCursorLocation(cursor); |
| 8966 | clang_disposeString(cursorName); |
| 8967 | return *this; |
| 8968 | } |
| 8969 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8970 | Logger &cxindex::Logger::operator<<(CXSourceLocation Loc) { |
| 8971 | CXFile File; |
| 8972 | unsigned Line, Column; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8973 | clang_getFileLocation(Loc, &File, &Line, &Column, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8974 | CXString FileName = clang_getFileName(File); |
| 8975 | *this << llvm::format("(%s:%d:%d)", clang_getCString(FileName), Line, Column); |
| 8976 | clang_disposeString(FileName); |
| 8977 | return *this; |
| 8978 | } |
| 8979 | |
| 8980 | Logger &cxindex::Logger::operator<<(CXSourceRange range) { |
| 8981 | CXSourceLocation BLoc = clang_getRangeStart(range); |
| 8982 | CXSourceLocation ELoc = clang_getRangeEnd(range); |
| 8983 | |
| 8984 | CXFile BFile; |
| 8985 | unsigned BLine, BColumn; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8986 | clang_getFileLocation(BLoc, &BFile, &BLine, &BColumn, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8987 | |
| 8988 | CXFile EFile; |
| 8989 | unsigned ELine, EColumn; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8990 | clang_getFileLocation(ELoc, &EFile, &ELine, &EColumn, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8991 | |
| 8992 | CXString BFileName = clang_getFileName(BFile); |
| 8993 | if (BFile == EFile) { |
| 8994 | *this << llvm::format("[%s %d:%d-%d:%d]", clang_getCString(BFileName), |
| 8995 | BLine, BColumn, ELine, EColumn); |
| 8996 | } else { |
| 8997 | CXString EFileName = clang_getFileName(EFile); |
| 8998 | *this << llvm::format("[%s:%d:%d - ", clang_getCString(BFileName), |
| 8999 | BLine, BColumn) |
| 9000 | << llvm::format("%s:%d:%d]", clang_getCString(EFileName), |
| 9001 | ELine, EColumn); |
| 9002 | clang_disposeString(EFileName); |
| 9003 | } |
| 9004 | clang_disposeString(BFileName); |
| 9005 | return *this; |
| 9006 | } |
| 9007 | |
| 9008 | Logger &cxindex::Logger::operator<<(CXString Str) { |
| 9009 | *this << clang_getCString(Str); |
| 9010 | return *this; |
| 9011 | } |
| 9012 | |
| 9013 | Logger &cxindex::Logger::operator<<(const llvm::format_object_base &Fmt) { |
| 9014 | LogOS << Fmt; |
| 9015 | return *this; |
| 9016 | } |
| 9017 | |
Benjamin Kramer | 762bc33 | 2019-08-07 14:44:40 +0000 | [diff] [blame] | 9018 | static llvm::ManagedStatic<std::mutex> LoggingMutex; |
Chandler Carruth | 37ad258 | 2014-06-27 15:14:39 +0000 | [diff] [blame] | 9019 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9020 | cxindex::Logger::~Logger() { |
Benjamin Kramer | 762bc33 | 2019-08-07 14:44:40 +0000 | [diff] [blame] | 9021 | std::lock_guard<std::mutex> L(*LoggingMutex); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9022 | |
| 9023 | static llvm::TimeRecord sBeginTR = llvm::TimeRecord::getCurrentTime(); |
| 9024 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9025 | raw_ostream &OS = llvm::errs(); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9026 | OS << "[libclang:" << Name << ':'; |
| 9027 | |
Alp Toker | 1a86ad2 | 2014-07-06 06:24:00 +0000 | [diff] [blame] | 9028 | #ifdef USE_DARWIN_THREADS |
| 9029 | // TODO: Portability. |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9030 | mach_port_t tid = pthread_mach_thread_np(pthread_self()); |
| 9031 | OS << tid << ':'; |
| 9032 | #endif |
| 9033 | |
| 9034 | llvm::TimeRecord TR = llvm::TimeRecord::getCurrentTime(); |
| 9035 | OS << llvm::format("%7.4f] ", TR.getWallTime() - sBeginTR.getWallTime()); |
Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 9036 | OS << Msg << '\n'; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9037 | |
| 9038 | if (Trace) { |
Zachary Turner | 1fe2a8d | 2015-03-05 19:15:09 +0000 | [diff] [blame] | 9039 | llvm::sys::PrintStackTrace(OS); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 9040 | OS << "--------------------------------------------------\n"; |
| 9041 | } |
| 9042 | } |
Ivan Donchevskii | c592913 | 2018-12-10 15:58:50 +0000 | [diff] [blame] | 9043 | |
| 9044 | #ifdef CLANG_TOOL_EXTRA_BUILD |
| 9045 | // This anchor is used to force the linker to link the clang-tidy plugin. |
| 9046 | extern volatile int ClangTidyPluginAnchorSource; |
| 9047 | static int LLVM_ATTRIBUTE_UNUSED ClangTidyPluginAnchorDestination = |
| 9048 | ClangTidyPluginAnchorSource; |
| 9049 | |
| 9050 | // This anchor is used to force the linker to link the clang-include-fixer |
| 9051 | // plugin. |
| 9052 | extern volatile int ClangIncludeFixerPluginAnchorSource; |
| 9053 | static int LLVM_ATTRIBUTE_UNUSED ClangIncludeFixerPluginAnchorDestination = |
| 9054 | ClangIncludeFixerPluginAnchorSource; |
| 9055 | #endif |