Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1 | //===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the main API hooks in the Clang-C Source Indexing |
| 11 | // library. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 15 | #include "CIndexDiagnostic.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 16 | #include "CIndexer.h" |
Chandler Carruth | 4b41745 | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 17 | #include "CLog.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 18 | #include "CXCursor.h" |
| 19 | #include "CXSourceLocation.h" |
| 20 | #include "CXString.h" |
| 21 | #include "CXTranslationUnit.h" |
| 22 | #include "CXType.h" |
| 23 | #include "CursorVisitor.h" |
David Blaikie | 0a4e61f | 2013-09-13 18:32:52 +0000 | [diff] [blame] | 24 | #include "clang/AST/Attr.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 25 | #include "clang/AST/StmtVisitor.h" |
| 26 | #include "clang/Basic/Diagnostic.h" |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 27 | #include "clang/Basic/DiagnosticCategories.h" |
| 28 | #include "clang/Basic/DiagnosticIDs.h" |
Emilio Cobos Alvarez | 485ad42 | 2017-04-28 15:56:39 +0000 | [diff] [blame] | 29 | #include "clang/Basic/TargetInfo.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 30 | #include "clang/Basic/Version.h" |
| 31 | #include "clang/Frontend/ASTUnit.h" |
| 32 | #include "clang/Frontend/CompilerInstance.h" |
| 33 | #include "clang/Frontend/FrontendDiagnostic.h" |
Argyrios Kyrtzidis | ca741ce | 2016-02-14 22:30:14 +0000 | [diff] [blame] | 34 | #include "clang/Index/CodegenNameGenerator.h" |
Dmitri Gribenko | 9e60511 | 2013-11-13 22:16:51 +0000 | [diff] [blame] | 35 | #include "clang/Index/CommentToXML.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 36 | #include "clang/Lex/HeaderSearch.h" |
| 37 | #include "clang/Lex/Lexer.h" |
| 38 | #include "clang/Lex/PreprocessingRecord.h" |
| 39 | #include "clang/Lex/Preprocessor.h" |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 40 | #include "clang/Serialization/SerializationDiagnostic.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/Optional.h" |
| 42 | #include "llvm/ADT/STLExtras.h" |
| 43 | #include "llvm/ADT/StringSwitch.h" |
Alp Toker | 1d257e1 | 2014-06-04 03:28:55 +0000 | [diff] [blame] | 44 | #include "llvm/Config/llvm-config.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Compiler.h" |
| 46 | #include "llvm/Support/CrashRecoveryContext.h" |
Chandler Carruth | 4b41745 | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Format.h" |
Chandler Carruth | 37ad258 | 2014-06-27 15:14:39 +0000 | [diff] [blame] | 48 | #include "llvm/Support/ManagedStatic.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 49 | #include "llvm/Support/MemoryBuffer.h" |
| 50 | #include "llvm/Support/Mutex.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Program.h" |
| 52 | #include "llvm/Support/SaveAndRestore.h" |
| 53 | #include "llvm/Support/Signals.h" |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 54 | #include "llvm/Support/TargetSelect.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 55 | #include "llvm/Support/Threading.h" |
| 56 | #include "llvm/Support/Timer.h" |
| 57 | #include "llvm/Support/raw_ostream.h" |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 58 | |
Alp Toker | 1a86ad2 | 2014-07-06 06:24:00 +0000 | [diff] [blame] | 59 | #if LLVM_ENABLE_THREADS != 0 && defined(__APPLE__) |
| 60 | #define USE_DARWIN_THREADS |
| 61 | #endif |
| 62 | |
| 63 | #ifdef USE_DARWIN_THREADS |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 64 | #include <pthread.h> |
| 65 | #endif |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 66 | |
| 67 | using namespace clang; |
| 68 | using namespace clang::cxcursor; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 69 | using namespace clang::cxtu; |
| 70 | using namespace clang::cxindex; |
| 71 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 72 | CXTranslationUnit cxtu::MakeCXTranslationUnit(CIndexer *CIdx, |
| 73 | std::unique_ptr<ASTUnit> AU) { |
Dmitri Gribenko | d36209e | 2013-01-26 21:32:42 +0000 | [diff] [blame] | 74 | if (!AU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 75 | return nullptr; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 76 | assert(CIdx); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 77 | CXTranslationUnit D = new CXTranslationUnitImpl(); |
| 78 | D->CIdx = CIdx; |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 79 | D->TheASTUnit = AU.release(); |
Dmitri Gribenko | 7489521 | 2013-02-03 13:52:47 +0000 | [diff] [blame] | 80 | D->StringPool = new cxstring::CXStringPool(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 81 | D->Diagnostics = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 82 | D->OverridenCursorsPool = createOverridenCXCursorsPool(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 83 | D->CommentToXML = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 84 | return D; |
| 85 | } |
| 86 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 87 | bool cxtu::isASTReadError(ASTUnit *AU) { |
| 88 | for (ASTUnit::stored_diag_iterator D = AU->stored_diag_begin(), |
| 89 | DEnd = AU->stored_diag_end(); |
| 90 | D != DEnd; ++D) { |
| 91 | if (D->getLevel() >= DiagnosticsEngine::Error && |
| 92 | DiagnosticIDs::getCategoryNumberForDiag(D->getID()) == |
| 93 | diag::DiagCat_AST_Deserialization_Issue) |
| 94 | return true; |
| 95 | } |
| 96 | return false; |
| 97 | } |
| 98 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 99 | cxtu::CXTUOwner::~CXTUOwner() { |
| 100 | if (TU) |
| 101 | clang_disposeTranslationUnit(TU); |
| 102 | } |
| 103 | |
| 104 | /// \brief Compare two source ranges to determine their relative position in |
| 105 | /// the translation unit. |
| 106 | static RangeComparisonResult RangeCompare(SourceManager &SM, |
| 107 | SourceRange R1, |
| 108 | SourceRange R2) { |
| 109 | assert(R1.isValid() && "First range is invalid?"); |
| 110 | assert(R2.isValid() && "Second range is invalid?"); |
| 111 | if (R1.getEnd() != R2.getBegin() && |
| 112 | SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin())) |
| 113 | return RangeBefore; |
| 114 | if (R2.getEnd() != R1.getBegin() && |
| 115 | SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin())) |
| 116 | return RangeAfter; |
| 117 | return RangeOverlap; |
| 118 | } |
| 119 | |
| 120 | /// \brief Determine if a source location falls within, before, or after a |
| 121 | /// a given source range. |
| 122 | static RangeComparisonResult LocationCompare(SourceManager &SM, |
| 123 | SourceLocation L, SourceRange R) { |
| 124 | assert(R.isValid() && "First range is invalid?"); |
| 125 | assert(L.isValid() && "Second range is invalid?"); |
| 126 | if (L == R.getBegin() || L == R.getEnd()) |
| 127 | return RangeOverlap; |
| 128 | if (SM.isBeforeInTranslationUnit(L, R.getBegin())) |
| 129 | return RangeBefore; |
| 130 | if (SM.isBeforeInTranslationUnit(R.getEnd(), L)) |
| 131 | return RangeAfter; |
| 132 | return RangeOverlap; |
| 133 | } |
| 134 | |
| 135 | /// \brief Translate a Clang source range into a CIndex source range. |
| 136 | /// |
| 137 | /// Clang internally represents ranges where the end location points to the |
| 138 | /// start of the token at the end. However, for external clients it is more |
| 139 | /// useful to have a CXSourceRange be a proper half-open interval. This routine |
| 140 | /// does the appropriate translation. |
| 141 | CXSourceRange cxloc::translateSourceRange(const SourceManager &SM, |
| 142 | const LangOptions &LangOpts, |
| 143 | const CharSourceRange &R) { |
| 144 | // We want the last character in this location, so we will adjust the |
| 145 | // location accordingly. |
| 146 | SourceLocation EndLoc = R.getEnd(); |
| 147 | if (EndLoc.isValid() && EndLoc.isMacroID() && !SM.isMacroArgExpansion(EndLoc)) |
| 148 | EndLoc = SM.getExpansionRange(EndLoc).second; |
Yaron Keren | 8b56366 | 2015-10-03 10:46:20 +0000 | [diff] [blame] | 149 | if (R.isTokenRange() && EndLoc.isValid()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 150 | unsigned Length = Lexer::MeasureTokenLength(SM.getSpellingLoc(EndLoc), |
| 151 | SM, LangOpts); |
| 152 | EndLoc = EndLoc.getLocWithOffset(Length); |
| 153 | } |
| 154 | |
Bill Wendling | eade362 | 2013-01-23 08:25:41 +0000 | [diff] [blame] | 155 | CXSourceRange Result = { |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 156 | { &SM, &LangOpts }, |
Bill Wendling | eade362 | 2013-01-23 08:25:41 +0000 | [diff] [blame] | 157 | R.getBegin().getRawEncoding(), |
| 158 | EndLoc.getRawEncoding() |
| 159 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 160 | return Result; |
| 161 | } |
| 162 | |
| 163 | //===----------------------------------------------------------------------===// |
| 164 | // Cursor visitor. |
| 165 | //===----------------------------------------------------------------------===// |
| 166 | |
| 167 | static SourceRange getRawCursorExtent(CXCursor C); |
| 168 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr); |
| 169 | |
| 170 | |
| 171 | RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { |
| 172 | return RangeCompare(AU->getSourceManager(), R, RegionOfInterest); |
| 173 | } |
| 174 | |
| 175 | /// \brief Visit the given cursor and, if requested by the visitor, |
| 176 | /// its children. |
| 177 | /// |
| 178 | /// \param Cursor the cursor to visit. |
| 179 | /// |
| 180 | /// \param CheckedRegionOfInterest if true, then the caller already checked |
| 181 | /// that this cursor is within the region of interest. |
| 182 | /// |
| 183 | /// \returns true if the visitation should be aborted, false if it |
| 184 | /// should continue. |
| 185 | bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { |
| 186 | if (clang_isInvalid(Cursor.kind)) |
| 187 | return false; |
| 188 | |
| 189 | if (clang_isDeclaration(Cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 190 | const Decl *D = getCursorDecl(Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 191 | if (!D) { |
| 192 | assert(0 && "Invalid declaration cursor"); |
| 193 | return true; // abort. |
| 194 | } |
| 195 | |
| 196 | // Ignore implicit declarations, unless it's an objc method because |
| 197 | // currently we should report implicit methods for properties when indexing. |
| 198 | if (D->isImplicit() && !isa<ObjCMethodDecl>(D)) |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | // If we have a range of interest, and this cursor doesn't intersect with it, |
| 203 | // we're done. |
| 204 | if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { |
| 205 | SourceRange Range = getRawCursorExtent(Cursor); |
| 206 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 211 | case CXChildVisit_Break: |
| 212 | return true; |
| 213 | |
| 214 | case CXChildVisit_Continue: |
| 215 | return false; |
| 216 | |
| 217 | case CXChildVisit_Recurse: { |
| 218 | bool ret = VisitChildren(Cursor); |
| 219 | if (PostChildrenVisitor) |
| 220 | if (PostChildrenVisitor(Cursor, ClientData)) |
| 221 | return true; |
| 222 | return ret; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | llvm_unreachable("Invalid CXChildVisitResult!"); |
| 227 | } |
| 228 | |
| 229 | static bool visitPreprocessedEntitiesInRange(SourceRange R, |
| 230 | PreprocessingRecord &PPRec, |
| 231 | CursorVisitor &Visitor) { |
| 232 | SourceManager &SM = Visitor.getASTUnit()->getSourceManager(); |
| 233 | FileID FID; |
| 234 | |
| 235 | if (!Visitor.shouldVisitIncludedEntities()) { |
| 236 | // If the begin/end of the range lie in the same FileID, do the optimization |
| 237 | // where we skip preprocessed entities that do not come from the same FileID. |
| 238 | FID = SM.getFileID(SM.getFileLoc(R.getBegin())); |
| 239 | if (FID != SM.getFileID(SM.getFileLoc(R.getEnd()))) |
| 240 | FID = FileID(); |
| 241 | } |
| 242 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 243 | const auto &Entities = PPRec.getPreprocessedEntitiesInRange(R); |
| 244 | return Visitor.visitPreprocessedEntities(Entities.begin(), Entities.end(), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 245 | PPRec, FID); |
| 246 | } |
| 247 | |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 248 | bool CursorVisitor::visitFileRegion() { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 249 | if (RegionOfInterest.isInvalid()) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 250 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 251 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 252 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 253 | SourceManager &SM = Unit->getSourceManager(); |
| 254 | |
| 255 | std::pair<FileID, unsigned> |
| 256 | Begin = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getBegin())), |
| 257 | End = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getEnd())); |
| 258 | |
| 259 | if (End.first != Begin.first) { |
| 260 | // If the end does not reside in the same file, try to recover by |
| 261 | // picking the end of the file of begin location. |
| 262 | End.first = Begin.first; |
| 263 | End.second = SM.getFileIDSize(Begin.first); |
| 264 | } |
| 265 | |
| 266 | assert(Begin.first == End.first); |
| 267 | if (Begin.second > End.second) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 268 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 269 | |
| 270 | FileID File = Begin.first; |
| 271 | unsigned Offset = Begin.second; |
| 272 | unsigned Length = End.second - Begin.second; |
| 273 | |
| 274 | if (!VisitDeclsOnly && !VisitPreprocessorLast) |
| 275 | if (visitPreprocessedEntitiesInRegion()) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 276 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 277 | |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 278 | if (visitDeclsFromFileRegion(File, Offset, Length)) |
| 279 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 280 | |
| 281 | if (!VisitDeclsOnly && VisitPreprocessorLast) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 282 | return visitPreprocessedEntitiesInRegion(); |
| 283 | |
| 284 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static bool isInLexicalContext(Decl *D, DeclContext *DC) { |
| 288 | if (!DC) |
| 289 | return false; |
| 290 | |
| 291 | for (DeclContext *DeclDC = D->getLexicalDeclContext(); |
| 292 | DeclDC; DeclDC = DeclDC->getLexicalParent()) { |
| 293 | if (DeclDC == DC) |
| 294 | return true; |
| 295 | } |
| 296 | return false; |
| 297 | } |
| 298 | |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 299 | bool CursorVisitor::visitDeclsFromFileRegion(FileID File, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 300 | unsigned Offset, unsigned Length) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 301 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 302 | SourceManager &SM = Unit->getSourceManager(); |
| 303 | SourceRange Range = RegionOfInterest; |
| 304 | |
| 305 | SmallVector<Decl *, 16> Decls; |
| 306 | Unit->findFileRegionDecls(File, Offset, Length, Decls); |
| 307 | |
| 308 | // If we didn't find any file level decls for the file, try looking at the |
| 309 | // file that it was included from. |
| 310 | while (Decls.empty() || Decls.front()->isTopLevelDeclInObjCContainer()) { |
| 311 | bool Invalid = false; |
| 312 | const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid); |
| 313 | if (Invalid) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 314 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 315 | |
| 316 | SourceLocation Outer; |
| 317 | if (SLEntry.isFile()) |
| 318 | Outer = SLEntry.getFile().getIncludeLoc(); |
| 319 | else |
| 320 | Outer = SLEntry.getExpansion().getExpansionLocStart(); |
| 321 | if (Outer.isInvalid()) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 322 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 323 | |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 324 | std::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 325 | Length = 0; |
| 326 | Unit->findFileRegionDecls(File, Offset, Length, Decls); |
| 327 | } |
| 328 | |
| 329 | assert(!Decls.empty()); |
| 330 | |
| 331 | bool VisitedAtLeastOnce = false; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 332 | DeclContext *CurDC = nullptr; |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 333 | SmallVectorImpl<Decl *>::iterator DIt = Decls.begin(); |
| 334 | for (SmallVectorImpl<Decl *>::iterator DE = Decls.end(); DIt != DE; ++DIt) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 335 | Decl *D = *DIt; |
| 336 | if (D->getSourceRange().isInvalid()) |
| 337 | continue; |
| 338 | |
| 339 | if (isInLexicalContext(D, CurDC)) |
| 340 | continue; |
| 341 | |
| 342 | CurDC = dyn_cast<DeclContext>(D); |
| 343 | |
| 344 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
| 345 | if (!TD->isFreeStanding()) |
| 346 | continue; |
| 347 | |
| 348 | RangeComparisonResult CompRes = RangeCompare(SM, D->getSourceRange(),Range); |
| 349 | if (CompRes == RangeBefore) |
| 350 | continue; |
| 351 | if (CompRes == RangeAfter) |
| 352 | break; |
| 353 | |
| 354 | assert(CompRes == RangeOverlap); |
| 355 | VisitedAtLeastOnce = true; |
| 356 | |
| 357 | if (isa<ObjCContainerDecl>(D)) { |
| 358 | FileDI_current = &DIt; |
| 359 | FileDE_current = DE; |
| 360 | } else { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 361 | FileDI_current = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true)) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 365 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | if (VisitedAtLeastOnce) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 369 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 370 | |
| 371 | // No Decls overlapped with the range. Move up the lexical context until there |
| 372 | // is a context that contains the range or we reach the translation unit |
| 373 | // level. |
| 374 | DeclContext *DC = DIt == Decls.begin() ? (*DIt)->getLexicalDeclContext() |
| 375 | : (*(DIt-1))->getLexicalDeclContext(); |
| 376 | |
| 377 | while (DC && !DC->isTranslationUnit()) { |
| 378 | Decl *D = cast<Decl>(DC); |
| 379 | SourceRange CurDeclRange = D->getSourceRange(); |
| 380 | if (CurDeclRange.isInvalid()) |
| 381 | break; |
| 382 | |
| 383 | if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) { |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 384 | if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true)) |
| 385 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | DC = D->getLexicalDeclContext(); |
| 389 | } |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 390 | |
| 391 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | bool CursorVisitor::visitPreprocessedEntitiesInRegion() { |
| 395 | if (!AU->getPreprocessor().getPreprocessingRecord()) |
| 396 | return false; |
| 397 | |
| 398 | PreprocessingRecord &PPRec |
| 399 | = *AU->getPreprocessor().getPreprocessingRecord(); |
| 400 | SourceManager &SM = AU->getSourceManager(); |
| 401 | |
| 402 | if (RegionOfInterest.isValid()) { |
| 403 | SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest); |
| 404 | SourceLocation B = MappedRange.getBegin(); |
| 405 | SourceLocation E = MappedRange.getEnd(); |
| 406 | |
| 407 | if (AU->isInPreambleFileID(B)) { |
| 408 | if (SM.isLoadedSourceLocation(E)) |
| 409 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), |
| 410 | PPRec, *this); |
| 411 | |
| 412 | // Beginning of range lies in the preamble but it also extends beyond |
| 413 | // it into the main file. Split the range into 2 parts, one covering |
| 414 | // the preamble and another covering the main file. This allows subsequent |
| 415 | // calls to visitPreprocessedEntitiesInRange to accept a source range that |
| 416 | // lies in the same FileID, allowing it to skip preprocessed entities that |
| 417 | // do not come from the same FileID. |
| 418 | bool breaked = |
| 419 | visitPreprocessedEntitiesInRange( |
| 420 | SourceRange(B, AU->getEndOfPreambleFileID()), |
| 421 | PPRec, *this); |
| 422 | if (breaked) return true; |
| 423 | return visitPreprocessedEntitiesInRange( |
| 424 | SourceRange(AU->getStartOfMainFileID(), E), |
| 425 | PPRec, *this); |
| 426 | } |
| 427 | |
| 428 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this); |
| 429 | } |
| 430 | |
| 431 | bool OnlyLocalDecls |
| 432 | = !AU->isMainFileAST() && AU->getOnlyLocalDecls(); |
| 433 | |
| 434 | if (OnlyLocalDecls) |
| 435 | return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(), |
| 436 | PPRec); |
| 437 | |
| 438 | return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec); |
| 439 | } |
| 440 | |
| 441 | template<typename InputIterator> |
| 442 | bool CursorVisitor::visitPreprocessedEntities(InputIterator First, |
| 443 | InputIterator Last, |
| 444 | PreprocessingRecord &PPRec, |
| 445 | FileID FID) { |
| 446 | for (; First != Last; ++First) { |
| 447 | if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID)) |
| 448 | continue; |
| 449 | |
| 450 | PreprocessedEntity *PPE = *First; |
Argyrios Kyrtzidis | 1030f26 | 2013-05-07 20:37:17 +0000 | [diff] [blame] | 451 | if (!PPE) |
| 452 | continue; |
| 453 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 454 | if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) { |
| 455 | if (Visit(MakeMacroExpansionCursor(ME, TU))) |
| 456 | return true; |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 457 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 458 | continue; |
| 459 | } |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 460 | |
| 461 | if (MacroDefinitionRecord *MD = dyn_cast<MacroDefinitionRecord>(PPE)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 462 | if (Visit(MakeMacroDefinitionCursor(MD, TU))) |
| 463 | return true; |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 464 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 465 | continue; |
| 466 | } |
| 467 | |
| 468 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) { |
| 469 | if (Visit(MakeInclusionDirectiveCursor(ID, TU))) |
| 470 | return true; |
| 471 | |
| 472 | continue; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | /// \brief Visit the children of the given cursor. |
| 480 | /// |
| 481 | /// \returns true if the visitation should be aborted, false if it |
| 482 | /// should continue. |
| 483 | bool CursorVisitor::VisitChildren(CXCursor Cursor) { |
| 484 | if (clang_isReference(Cursor.kind) && |
| 485 | Cursor.kind != CXCursor_CXXBaseSpecifier) { |
| 486 | // By definition, references have no children. |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | // Set the Parent field to Cursor, then back to its old value once we're |
| 491 | // done. |
| 492 | SetParentRAII SetParent(Parent, StmtParent, Cursor); |
| 493 | |
| 494 | if (clang_isDeclaration(Cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 495 | Decl *D = const_cast<Decl *>(getCursorDecl(Cursor)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 496 | if (!D) |
| 497 | return false; |
| 498 | |
| 499 | return VisitAttributes(D) || Visit(D); |
| 500 | } |
| 501 | |
| 502 | if (clang_isStatement(Cursor.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 503 | if (const Stmt *S = getCursorStmt(Cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 504 | return Visit(S); |
| 505 | |
| 506 | return false; |
| 507 | } |
| 508 | |
| 509 | if (clang_isExpression(Cursor.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 510 | if (const Expr *E = getCursorExpr(Cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 511 | return Visit(E); |
| 512 | |
| 513 | return false; |
| 514 | } |
| 515 | |
| 516 | if (clang_isTranslationUnit(Cursor.kind)) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 517 | CXTranslationUnit TU = getCursorTU(Cursor); |
| 518 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 519 | |
| 520 | int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast }; |
| 521 | for (unsigned I = 0; I != 2; ++I) { |
| 522 | if (VisitOrder[I]) { |
| 523 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && |
| 524 | RegionOfInterest.isInvalid()) { |
| 525 | for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(), |
| 526 | TLEnd = CXXUnit->top_level_end(); |
| 527 | TL != TLEnd; ++TL) { |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 528 | const Optional<bool> V = handleDeclForVisitation(*TL); |
| 529 | if (!V.hasValue()) |
| 530 | continue; |
| 531 | return V.getValue(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 532 | } |
| 533 | } else if (VisitDeclContext( |
| 534 | CXXUnit->getASTContext().getTranslationUnitDecl())) |
| 535 | return true; |
| 536 | continue; |
| 537 | } |
| 538 | |
| 539 | // Walk the preprocessing record. |
| 540 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) |
| 541 | visitPreprocessedEntitiesInRegion(); |
| 542 | } |
| 543 | |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | if (Cursor.kind == CXCursor_CXXBaseSpecifier) { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 548 | if (const CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 549 | if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) { |
| 550 | return Visit(BaseTSInfo->getTypeLoc()); |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 556 | const IBOutletCollectionAttr *A = |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 557 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor)); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 558 | if (const ObjCObjectType *ObjT = A->getInterface()->getAs<ObjCObjectType>()) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 559 | return Visit(cxcursor::MakeCursorObjCClassRef( |
| 560 | ObjT->getInterface(), |
| 561 | A->getInterfaceLoc()->getTypeLoc().getLocStart(), TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 564 | // If pointing inside a macro definition, check if the token is an identifier |
| 565 | // that was ever defined as a macro. In such a case, create a "pseudo" macro |
| 566 | // expansion cursor for that token. |
| 567 | SourceLocation BeginLoc = RegionOfInterest.getBegin(); |
| 568 | if (Cursor.kind == CXCursor_MacroDefinition && |
| 569 | BeginLoc == RegionOfInterest.getEnd()) { |
| 570 | SourceLocation Loc = AU->mapLocationToPreamble(BeginLoc); |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 571 | const MacroInfo *MI = |
| 572 | getMacroInfo(cxcursor::getCursorMacroDefinition(Cursor), TU); |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 573 | if (MacroDefinitionRecord *MacroDef = |
| 574 | checkForMacroInMacroDefinition(MI, Loc, TU)) |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 575 | return Visit(cxcursor::MakeMacroExpansionCursor(MacroDef, BeginLoc, TU)); |
| 576 | } |
| 577 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 578 | // Nothing to visit at the moment. |
| 579 | return false; |
| 580 | } |
| 581 | |
| 582 | bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { |
| 583 | if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten()) |
| 584 | if (Visit(TSInfo->getTypeLoc())) |
| 585 | return true; |
| 586 | |
| 587 | if (Stmt *Body = B->getBody()) |
| 588 | return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest)); |
| 589 | |
| 590 | return false; |
| 591 | } |
| 592 | |
Ted Kremenek | 0332558 | 2013-02-21 01:29:01 +0000 | [diff] [blame] | 593 | Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 594 | if (RegionOfInterest.isValid()) { |
| 595 | SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager()); |
| 596 | if (Range.isInvalid()) |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 597 | return None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 598 | |
| 599 | switch (CompareRegionOfInterest(Range)) { |
| 600 | case RangeBefore: |
| 601 | // This declaration comes before the region of interest; skip it. |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 602 | return None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 603 | |
| 604 | case RangeAfter: |
| 605 | // This declaration comes after the region of interest; we're done. |
| 606 | return false; |
| 607 | |
| 608 | case RangeOverlap: |
| 609 | // This declaration overlaps the region of interest; visit it. |
| 610 | break; |
| 611 | } |
| 612 | } |
| 613 | return true; |
| 614 | } |
| 615 | |
| 616 | bool CursorVisitor::VisitDeclContext(DeclContext *DC) { |
| 617 | DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 618 | |
| 619 | // FIXME: Eventually remove. This part of a hack to support proper |
| 620 | // iteration over all Decls contained lexically within an ObjC container. |
| 621 | SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I); |
| 622 | SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E); |
| 623 | |
| 624 | for ( ; I != E; ++I) { |
| 625 | Decl *D = *I; |
| 626 | if (D->getLexicalDeclContext() != DC) |
| 627 | continue; |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 628 | const Optional<bool> V = handleDeclForVisitation(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 629 | if (!V.hasValue()) |
| 630 | continue; |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 631 | return V.getValue(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 632 | } |
| 633 | return false; |
| 634 | } |
| 635 | |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 636 | Optional<bool> CursorVisitor::handleDeclForVisitation(const Decl *D) { |
| 637 | CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest); |
| 638 | |
| 639 | // Ignore synthesized ivars here, otherwise if we have something like: |
| 640 | // @synthesize prop = _prop; |
| 641 | // and '_prop' is not declared, we will encounter a '_prop' ivar before |
| 642 | // encountering the 'prop' synthesize declaration and we will think that |
| 643 | // we passed the region-of-interest. |
| 644 | if (auto *ivarD = dyn_cast<ObjCIvarDecl>(D)) { |
| 645 | if (ivarD->getSynthesize()) |
| 646 | return None; |
| 647 | } |
| 648 | |
| 649 | // FIXME: ObjCClassRef/ObjCProtocolRef for forward class/protocol |
| 650 | // declarations is a mismatch with the compiler semantics. |
| 651 | if (Cursor.kind == CXCursor_ObjCInterfaceDecl) { |
| 652 | auto *ID = cast<ObjCInterfaceDecl>(D); |
| 653 | if (!ID->isThisDeclarationADefinition()) |
| 654 | Cursor = MakeCursorObjCClassRef(ID, ID->getLocation(), TU); |
| 655 | |
| 656 | } else if (Cursor.kind == CXCursor_ObjCProtocolDecl) { |
| 657 | auto *PD = cast<ObjCProtocolDecl>(D); |
| 658 | if (!PD->isThisDeclarationADefinition()) |
| 659 | Cursor = MakeCursorObjCProtocolRef(PD, PD->getLocation(), TU); |
| 660 | } |
| 661 | |
| 662 | const Optional<bool> V = shouldVisitCursor(Cursor); |
| 663 | if (!V.hasValue()) |
| 664 | return None; |
| 665 | if (!V.getValue()) |
| 666 | return false; |
| 667 | if (Visit(Cursor, true)) |
| 668 | return true; |
| 669 | return None; |
| 670 | } |
| 671 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 672 | bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 673 | llvm_unreachable("Translation units are visited directly by Visit()"); |
| 674 | } |
| 675 | |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 676 | bool CursorVisitor::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| 677 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 678 | return true; |
| 679 | |
| 680 | return Visit(MakeCXCursor(D->getTemplatedDecl(), TU, RegionOfInterest)); |
| 681 | } |
| 682 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 683 | bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 684 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 685 | return Visit(TSInfo->getTypeLoc()); |
| 686 | |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { |
| 691 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 692 | return Visit(TSInfo->getTypeLoc()); |
| 693 | |
| 694 | return false; |
| 695 | } |
| 696 | |
| 697 | bool CursorVisitor::VisitTagDecl(TagDecl *D) { |
| 698 | return VisitDeclContext(D); |
| 699 | } |
| 700 | |
| 701 | bool CursorVisitor::VisitClassTemplateSpecializationDecl( |
| 702 | ClassTemplateSpecializationDecl *D) { |
| 703 | bool ShouldVisitBody = false; |
| 704 | switch (D->getSpecializationKind()) { |
| 705 | case TSK_Undeclared: |
| 706 | case TSK_ImplicitInstantiation: |
| 707 | // Nothing to visit |
| 708 | return false; |
| 709 | |
| 710 | case TSK_ExplicitInstantiationDeclaration: |
| 711 | case TSK_ExplicitInstantiationDefinition: |
| 712 | break; |
| 713 | |
| 714 | case TSK_ExplicitSpecialization: |
| 715 | ShouldVisitBody = true; |
| 716 | break; |
| 717 | } |
| 718 | |
| 719 | // Visit the template arguments used in the specialization. |
| 720 | if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { |
| 721 | TypeLoc TL = SpecType->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 722 | if (TemplateSpecializationTypeLoc TSTLoc = |
| 723 | TL.getAs<TemplateSpecializationTypeLoc>()) { |
| 724 | for (unsigned I = 0, N = TSTLoc.getNumArgs(); I != N; ++I) |
| 725 | if (VisitTemplateArgumentLoc(TSTLoc.getArgLoc(I))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 726 | return true; |
| 727 | } |
| 728 | } |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 729 | |
| 730 | return ShouldVisitBody && VisitCXXRecordDecl(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( |
| 734 | ClassTemplatePartialSpecializationDecl *D) { |
| 735 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 736 | // before visiting these template parameters. |
| 737 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 738 | return true; |
| 739 | |
| 740 | // Visit the partial specialization arguments. |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 741 | const ASTTemplateArgumentListInfo *Info = D->getTemplateArgsAsWritten(); |
| 742 | const TemplateArgumentLoc *TemplateArgs = Info->getTemplateArgs(); |
| 743 | for (unsigned I = 0, N = Info->NumTemplateArgs; I != N; ++I) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 744 | if (VisitTemplateArgumentLoc(TemplateArgs[I])) |
| 745 | return true; |
| 746 | |
| 747 | return VisitCXXRecordDecl(D); |
| 748 | } |
| 749 | |
| 750 | bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
| 751 | // Visit the default argument. |
| 752 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 753 | if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) |
| 754 | if (Visit(DefArg->getTypeLoc())) |
| 755 | return true; |
| 756 | |
| 757 | return false; |
| 758 | } |
| 759 | |
| 760 | bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 761 | if (Expr *Init = D->getInitExpr()) |
| 762 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
| 763 | return false; |
| 764 | } |
| 765 | |
| 766 | bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { |
Argyrios Kyrtzidis | 2ec7674 | 2013-04-05 21:04:10 +0000 | [diff] [blame] | 767 | unsigned NumParamList = DD->getNumTemplateParameterLists(); |
| 768 | for (unsigned i = 0; i < NumParamList; i++) { |
| 769 | TemplateParameterList* Params = DD->getTemplateParameterList(i); |
| 770 | if (VisitTemplateParameters(Params)) |
| 771 | return true; |
| 772 | } |
| 773 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 774 | if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) |
| 775 | if (Visit(TSInfo->getTypeLoc())) |
| 776 | return true; |
| 777 | |
| 778 | // Visit the nested-name-specifier, if present. |
| 779 | if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc()) |
| 780 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 781 | return true; |
| 782 | |
| 783 | return false; |
| 784 | } |
| 785 | |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 786 | /// \brief Compare two base or member initializers based on their source order. |
| 787 | static int CompareCXXCtorInitializers(CXXCtorInitializer *const *X, |
| 788 | CXXCtorInitializer *const *Y) { |
| 789 | return (*X)->getSourceOrder() - (*Y)->getSourceOrder(); |
| 790 | } |
| 791 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 792 | bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
Argyrios Kyrtzidis | 2ec7674 | 2013-04-05 21:04:10 +0000 | [diff] [blame] | 793 | unsigned NumParamList = ND->getNumTemplateParameterLists(); |
| 794 | for (unsigned i = 0; i < NumParamList; i++) { |
| 795 | TemplateParameterList* Params = ND->getTemplateParameterList(i); |
| 796 | if (VisitTemplateParameters(Params)) |
| 797 | return true; |
| 798 | } |
| 799 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 800 | if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { |
| 801 | // Visit the function declaration's syntactic components in the order |
| 802 | // written. This requires a bit of work. |
| 803 | TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 804 | FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 805 | |
| 806 | // If we have a function declared directly (without the use of a typedef), |
| 807 | // visit just the return type. Otherwise, just visit the function's type |
| 808 | // now. |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 809 | if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL.getReturnLoc())) || |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 810 | (!FTL && Visit(TL))) |
| 811 | return true; |
| 812 | |
| 813 | // Visit the nested-name-specifier, if present. |
| 814 | if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc()) |
| 815 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 816 | return true; |
| 817 | |
| 818 | // Visit the declaration name. |
Argyrios Kyrtzidis | 4a4d2b4 | 2014-02-09 08:13:47 +0000 | [diff] [blame] | 819 | if (!isa<CXXDestructorDecl>(ND)) |
| 820 | if (VisitDeclarationNameInfo(ND->getNameInfo())) |
| 821 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 822 | |
| 823 | // FIXME: Visit explicitly-specified template arguments! |
| 824 | |
| 825 | // Visit the function parameters, if we have a function type. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 826 | if (FTL && VisitFunctionTypeLoc(FTL, true)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 827 | return true; |
| 828 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 829 | // FIXME: Attributes? |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) { |
| 833 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) { |
| 834 | // Find the initializers that were written in the source. |
| 835 | SmallVector<CXXCtorInitializer *, 4> WrittenInits; |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 836 | for (auto *I : Constructor->inits()) { |
| 837 | if (!I->isWritten()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 838 | continue; |
| 839 | |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 840 | WrittenInits.push_back(I); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | // Sort the initializers in source order |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 844 | llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(), |
| 845 | &CompareCXXCtorInitializers); |
| 846 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 847 | // Visit the initializers in source order |
| 848 | for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) { |
| 849 | CXXCtorInitializer *Init = WrittenInits[I]; |
| 850 | if (Init->isAnyMemberInitializer()) { |
| 851 | if (Visit(MakeCursorMemberRef(Init->getAnyMember(), |
| 852 | Init->getMemberLocation(), TU))) |
| 853 | return true; |
| 854 | } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) { |
| 855 | if (Visit(TInfo->getTypeLoc())) |
| 856 | return true; |
| 857 | } |
| 858 | |
| 859 | // Visit the initializer value. |
| 860 | if (Expr *Initializer = Init->getInit()) |
| 861 | if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest))) |
| 862 | return true; |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
| 867 | return true; |
| 868 | } |
| 869 | |
| 870 | return false; |
| 871 | } |
| 872 | |
| 873 | bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { |
| 874 | if (VisitDeclaratorDecl(D)) |
| 875 | return true; |
| 876 | |
| 877 | if (Expr *BitWidth = D->getBitWidth()) |
| 878 | return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest)); |
| 879 | |
Benjamin Kramer | 99f9759 | 2017-11-15 12:20:41 +0000 | [diff] [blame] | 880 | if (Expr *Init = D->getInClassInitializer()) |
| 881 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
| 882 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 883 | return false; |
| 884 | } |
| 885 | |
| 886 | bool CursorVisitor::VisitVarDecl(VarDecl *D) { |
| 887 | if (VisitDeclaratorDecl(D)) |
| 888 | return true; |
| 889 | |
| 890 | if (Expr *Init = D->getInit()) |
| 891 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
| 892 | |
| 893 | return false; |
| 894 | } |
| 895 | |
| 896 | bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 897 | if (VisitDeclaratorDecl(D)) |
| 898 | return true; |
| 899 | |
| 900 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 901 | if (Expr *DefArg = D->getDefaultArgument()) |
| 902 | return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest)); |
| 903 | |
| 904 | return false; |
| 905 | } |
| 906 | |
| 907 | bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 908 | // FIXME: Visit the "outer" template parameter lists on the FunctionDecl |
| 909 | // before visiting these template parameters. |
| 910 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 911 | return true; |
| 912 | |
Jonathan Coe | 578ac7a | 2017-10-16 23:43:02 +0000 | [diff] [blame] | 913 | auto* FD = D->getTemplatedDecl(); |
| 914 | return VisitAttributes(FD) || VisitFunctionDecl(FD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 918 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 919 | // before visiting these template parameters. |
| 920 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 921 | return true; |
| 922 | |
Jonathan Coe | 578ac7a | 2017-10-16 23:43:02 +0000 | [diff] [blame] | 923 | auto* CD = D->getTemplatedDecl(); |
| 924 | return VisitAttributes(CD) || VisitCXXRecordDecl(CD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 928 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 929 | return true; |
| 930 | |
| 931 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && |
| 932 | VisitTemplateArgumentLoc(D->getDefaultArgument())) |
| 933 | return true; |
| 934 | |
| 935 | return false; |
| 936 | } |
| 937 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 938 | bool CursorVisitor::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { |
| 939 | // Visit the bound, if it's explicit. |
| 940 | if (D->hasExplicitBound()) { |
| 941 | if (auto TInfo = D->getTypeSourceInfo()) { |
| 942 | if (Visit(TInfo->getTypeLoc())) |
| 943 | return true; |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | return false; |
| 948 | } |
| 949 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 950 | bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 951 | if (TypeSourceInfo *TSInfo = ND->getReturnTypeSourceInfo()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 952 | if (Visit(TSInfo->getTypeLoc())) |
| 953 | return true; |
| 954 | |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 955 | for (const auto *P : ND->parameters()) { |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 956 | if (Visit(MakeCXCursor(P, TU, RegionOfInterest))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 957 | return true; |
| 958 | } |
| 959 | |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 960 | return ND->isThisDeclarationADefinition() && |
| 961 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | template <typename DeclIt> |
| 965 | static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current, |
| 966 | SourceManager &SM, SourceLocation EndLoc, |
| 967 | SmallVectorImpl<Decl *> &Decls) { |
| 968 | DeclIt next = *DI_current; |
| 969 | while (++next != DE_current) { |
| 970 | Decl *D_next = *next; |
| 971 | if (!D_next) |
| 972 | break; |
| 973 | SourceLocation L = D_next->getLocStart(); |
| 974 | if (!L.isValid()) |
| 975 | break; |
| 976 | if (SM.isBeforeInTranslationUnit(L, EndLoc)) { |
| 977 | *DI_current = next; |
| 978 | Decls.push_back(D_next); |
| 979 | continue; |
| 980 | } |
| 981 | break; |
| 982 | } |
| 983 | } |
| 984 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 985 | bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
| 986 | // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially |
| 987 | // an @implementation can lexically contain Decls that are not properly |
| 988 | // nested in the AST. When we identify such cases, we need to retrofit |
| 989 | // this nesting here. |
| 990 | if (!DI_current && !FileDI_current) |
| 991 | return VisitDeclContext(D); |
| 992 | |
| 993 | // Scan the Decls that immediately come after the container |
| 994 | // in the current DeclContext. If any fall within the |
| 995 | // container's lexical region, stash them into a vector |
| 996 | // for later processing. |
| 997 | SmallVector<Decl *, 24> DeclsInContainer; |
| 998 | SourceLocation EndLoc = D->getSourceRange().getEnd(); |
| 999 | SourceManager &SM = AU->getSourceManager(); |
| 1000 | if (EndLoc.isValid()) { |
| 1001 | if (DI_current) { |
| 1002 | addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc, |
| 1003 | DeclsInContainer); |
| 1004 | } else { |
| 1005 | addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc, |
| 1006 | DeclsInContainer); |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | // The common case. |
| 1011 | if (DeclsInContainer.empty()) |
| 1012 | return VisitDeclContext(D); |
| 1013 | |
| 1014 | // Get all the Decls in the DeclContext, and sort them with the |
| 1015 | // additional ones we've collected. Then visit them. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1016 | for (auto *SubDecl : D->decls()) { |
| 1017 | if (!SubDecl || SubDecl->getLexicalDeclContext() != D || |
| 1018 | SubDecl->getLocStart().isInvalid()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1019 | continue; |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1020 | DeclsInContainer.push_back(SubDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | // Now sort the Decls so that they appear in lexical order. |
| 1024 | std::sort(DeclsInContainer.begin(), DeclsInContainer.end(), |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 1025 | [&SM](Decl *A, Decl *B) { |
| 1026 | SourceLocation L_A = A->getLocStart(); |
| 1027 | SourceLocation L_B = B->getLocStart(); |
| 1028 | assert(L_A.isValid() && L_B.isValid()); |
| 1029 | return SM.isBeforeInTranslationUnit(L_A, L_B); |
| 1030 | }); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1031 | |
| 1032 | // Now visit the decls. |
| 1033 | for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(), |
| 1034 | E = DeclsInContainer.end(); I != E; ++I) { |
| 1035 | CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest); |
Ted Kremenek | 0332558 | 2013-02-21 01:29:01 +0000 | [diff] [blame] | 1036 | const Optional<bool> &V = shouldVisitCursor(Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1037 | if (!V.hasValue()) |
| 1038 | continue; |
| 1039 | if (!V.getValue()) |
| 1040 | return false; |
| 1041 | if (Visit(Cursor, true)) |
| 1042 | return true; |
| 1043 | } |
| 1044 | return false; |
| 1045 | } |
| 1046 | |
| 1047 | bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 1048 | if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), |
| 1049 | TU))) |
| 1050 | return true; |
| 1051 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1052 | if (VisitObjCTypeParamList(ND->getTypeParamList())) |
| 1053 | return true; |
| 1054 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1055 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 1056 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 1057 | E = ND->protocol_end(); I != E; ++I, ++PL) |
| 1058 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1059 | return true; |
| 1060 | |
| 1061 | return VisitObjCContainerDecl(ND); |
| 1062 | } |
| 1063 | |
| 1064 | bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 1065 | if (!PID->isThisDeclarationADefinition()) |
| 1066 | return Visit(MakeCursorObjCProtocolRef(PID, PID->getLocation(), TU)); |
| 1067 | |
| 1068 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
| 1069 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 1070 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 1071 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1072 | return true; |
| 1073 | |
| 1074 | return VisitObjCContainerDecl(PID); |
| 1075 | } |
| 1076 | |
| 1077 | bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { |
| 1078 | if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc())) |
| 1079 | return true; |
| 1080 | |
| 1081 | // FIXME: This implements a workaround with @property declarations also being |
| 1082 | // installed in the DeclContext for the @interface. Eventually this code |
| 1083 | // should be removed. |
| 1084 | ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); |
| 1085 | if (!CDecl || !CDecl->IsClassExtension()) |
| 1086 | return false; |
| 1087 | |
| 1088 | ObjCInterfaceDecl *ID = CDecl->getClassInterface(); |
| 1089 | if (!ID) |
| 1090 | return false; |
| 1091 | |
| 1092 | IdentifierInfo *PropertyId = PD->getIdentifier(); |
| 1093 | ObjCPropertyDecl *prevDecl = |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 1094 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId, |
| 1095 | PD->getQueryKind()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1096 | |
| 1097 | if (!prevDecl) |
| 1098 | return false; |
| 1099 | |
| 1100 | // Visit synthesized methods since they will be skipped when visiting |
| 1101 | // the @interface. |
| 1102 | if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) |
| 1103 | if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl) |
| 1104 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
| 1105 | return true; |
| 1106 | |
| 1107 | if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) |
| 1108 | if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl) |
| 1109 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
| 1110 | return true; |
| 1111 | |
| 1112 | return false; |
| 1113 | } |
| 1114 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1115 | bool CursorVisitor::VisitObjCTypeParamList(ObjCTypeParamList *typeParamList) { |
| 1116 | if (!typeParamList) |
| 1117 | return false; |
| 1118 | |
| 1119 | for (auto *typeParam : *typeParamList) { |
| 1120 | // Visit the type parameter. |
| 1121 | if (Visit(MakeCXCursor(typeParam, TU, RegionOfInterest))) |
| 1122 | return true; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | return false; |
| 1126 | } |
| 1127 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1128 | bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 1129 | if (!D->isThisDeclarationADefinition()) { |
| 1130 | // Forward declaration is treated like a reference. |
| 1131 | return Visit(MakeCursorObjCClassRef(D, D->getLocation(), TU)); |
| 1132 | } |
| 1133 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1134 | // Objective-C type parameters. |
| 1135 | if (VisitObjCTypeParamList(D->getTypeParamListAsWritten())) |
| 1136 | return true; |
| 1137 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1138 | // Issue callbacks for super class. |
| 1139 | if (D->getSuperClass() && |
| 1140 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
| 1141 | D->getSuperClassLoc(), |
| 1142 | TU))) |
| 1143 | return true; |
| 1144 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1145 | if (TypeSourceInfo *SuperClassTInfo = D->getSuperClassTInfo()) |
| 1146 | if (Visit(SuperClassTInfo->getTypeLoc())) |
| 1147 | return true; |
| 1148 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1149 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1150 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 1151 | E = D->protocol_end(); I != E; ++I, ++PL) |
| 1152 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1153 | return true; |
| 1154 | |
| 1155 | return VisitObjCContainerDecl(D); |
| 1156 | } |
| 1157 | |
| 1158 | bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 1159 | return VisitObjCContainerDecl(D); |
| 1160 | } |
| 1161 | |
| 1162 | bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 1163 | // 'ID' could be null when dealing with invalid code. |
| 1164 | if (ObjCInterfaceDecl *ID = D->getClassInterface()) |
| 1165 | if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) |
| 1166 | return true; |
| 1167 | |
| 1168 | return VisitObjCImplDecl(D); |
| 1169 | } |
| 1170 | |
| 1171 | bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 1172 | #if 0 |
| 1173 | // Issue callbacks for super class. |
| 1174 | // FIXME: No source location information! |
| 1175 | if (D->getSuperClass() && |
| 1176 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
| 1177 | D->getSuperClassLoc(), |
| 1178 | TU))) |
| 1179 | return true; |
| 1180 | #endif |
| 1181 | |
| 1182 | return VisitObjCImplDecl(D); |
| 1183 | } |
| 1184 | |
| 1185 | bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) { |
| 1186 | if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl()) |
| 1187 | if (PD->isIvarNameSpecified()) |
| 1188 | return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU)); |
| 1189 | |
| 1190 | return false; |
| 1191 | } |
| 1192 | |
| 1193 | bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { |
| 1194 | return VisitDeclContext(D); |
| 1195 | } |
| 1196 | |
| 1197 | bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 1198 | // Visit nested-name-specifier. |
| 1199 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1200 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1201 | return true; |
| 1202 | |
| 1203 | return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), |
| 1204 | D->getTargetNameLoc(), TU)); |
| 1205 | } |
| 1206 | |
| 1207 | bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { |
| 1208 | // Visit nested-name-specifier. |
| 1209 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1210 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1211 | return true; |
| 1212 | } |
| 1213 | |
| 1214 | if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU))) |
| 1215 | return true; |
| 1216 | |
| 1217 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1218 | } |
| 1219 | |
| 1220 | bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 1221 | // Visit nested-name-specifier. |
| 1222 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1223 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1224 | return true; |
| 1225 | |
| 1226 | return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), |
| 1227 | D->getIdentLocation(), TU)); |
| 1228 | } |
| 1229 | |
| 1230 | bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
| 1231 | // Visit nested-name-specifier. |
| 1232 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1233 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1234 | return true; |
| 1235 | } |
| 1236 | |
| 1237 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1238 | } |
| 1239 | |
| 1240 | bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( |
| 1241 | UnresolvedUsingTypenameDecl *D) { |
| 1242 | // Visit nested-name-specifier. |
| 1243 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1244 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1245 | return true; |
| 1246 | |
| 1247 | return false; |
| 1248 | } |
| 1249 | |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 1250 | bool CursorVisitor::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 1251 | if (Visit(MakeCXCursor(D->getAssertExpr(), StmtParent, TU, RegionOfInterest))) |
| 1252 | return true; |
Richard Trieu | f3b7766 | 2016-09-13 01:37:01 +0000 | [diff] [blame] | 1253 | if (StringLiteral *Message = D->getMessage()) |
| 1254 | if (Visit(MakeCXCursor(Message, StmtParent, TU, RegionOfInterest))) |
| 1255 | return true; |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 1256 | return false; |
| 1257 | } |
| 1258 | |
Olivier Goffart | d211c64 | 2016-11-04 06:29:27 +0000 | [diff] [blame] | 1259 | bool CursorVisitor::VisitFriendDecl(FriendDecl *D) { |
| 1260 | if (NamedDecl *FriendD = D->getFriendDecl()) { |
| 1261 | if (Visit(MakeCXCursor(FriendD, TU, RegionOfInterest))) |
| 1262 | return true; |
| 1263 | } else if (TypeSourceInfo *TI = D->getFriendType()) { |
| 1264 | if (Visit(TI->getTypeLoc())) |
| 1265 | return true; |
| 1266 | } |
| 1267 | return false; |
| 1268 | } |
| 1269 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1270 | bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { |
| 1271 | switch (Name.getName().getNameKind()) { |
| 1272 | case clang::DeclarationName::Identifier: |
| 1273 | case clang::DeclarationName::CXXLiteralOperatorName: |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 1274 | case clang::DeclarationName::CXXDeductionGuideName: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1275 | case clang::DeclarationName::CXXOperatorName: |
| 1276 | case clang::DeclarationName::CXXUsingDirective: |
| 1277 | return false; |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 1278 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1279 | case clang::DeclarationName::CXXConstructorName: |
| 1280 | case clang::DeclarationName::CXXDestructorName: |
| 1281 | case clang::DeclarationName::CXXConversionFunctionName: |
| 1282 | if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) |
| 1283 | return Visit(TSInfo->getTypeLoc()); |
| 1284 | return false; |
| 1285 | |
| 1286 | case clang::DeclarationName::ObjCZeroArgSelector: |
| 1287 | case clang::DeclarationName::ObjCOneArgSelector: |
| 1288 | case clang::DeclarationName::ObjCMultiArgSelector: |
| 1289 | // FIXME: Per-identifier location info? |
| 1290 | return false; |
| 1291 | } |
| 1292 | |
| 1293 | llvm_unreachable("Invalid DeclarationName::Kind!"); |
| 1294 | } |
| 1295 | |
| 1296 | bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, |
| 1297 | SourceRange Range) { |
| 1298 | // FIXME: This whole routine is a hack to work around the lack of proper |
| 1299 | // source information in nested-name-specifiers (PR5791). Since we do have |
| 1300 | // a beginning source location, we can visit the first component of the |
| 1301 | // nested-name-specifier, if it's a single-token component. |
| 1302 | if (!NNS) |
| 1303 | return false; |
| 1304 | |
| 1305 | // Get the first component in the nested-name-specifier. |
| 1306 | while (NestedNameSpecifier *Prefix = NNS->getPrefix()) |
| 1307 | NNS = Prefix; |
| 1308 | |
| 1309 | switch (NNS->getKind()) { |
| 1310 | case NestedNameSpecifier::Namespace: |
| 1311 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), |
| 1312 | TU)); |
| 1313 | |
| 1314 | case NestedNameSpecifier::NamespaceAlias: |
| 1315 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1316 | Range.getBegin(), TU)); |
| 1317 | |
| 1318 | case NestedNameSpecifier::TypeSpec: { |
| 1319 | // If the type has a form where we know that the beginning of the source |
| 1320 | // range matches up with a reference cursor. Visit the appropriate reference |
| 1321 | // cursor. |
| 1322 | const Type *T = NNS->getAsType(); |
| 1323 | if (const TypedefType *Typedef = dyn_cast<TypedefType>(T)) |
| 1324 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); |
| 1325 | if (const TagType *Tag = dyn_cast<TagType>(T)) |
| 1326 | return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); |
| 1327 | if (const TemplateSpecializationType *TST |
| 1328 | = dyn_cast<TemplateSpecializationType>(T)) |
| 1329 | return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); |
| 1330 | break; |
| 1331 | } |
| 1332 | |
| 1333 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1334 | case NestedNameSpecifier::Global: |
| 1335 | case NestedNameSpecifier::Identifier: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 1336 | case NestedNameSpecifier::Super: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1337 | break; |
| 1338 | } |
| 1339 | |
| 1340 | return false; |
| 1341 | } |
| 1342 | |
| 1343 | bool |
| 1344 | CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 1345 | SmallVector<NestedNameSpecifierLoc, 4> Qualifiers; |
| 1346 | for (; Qualifier; Qualifier = Qualifier.getPrefix()) |
| 1347 | Qualifiers.push_back(Qualifier); |
| 1348 | |
| 1349 | while (!Qualifiers.empty()) { |
| 1350 | NestedNameSpecifierLoc Q = Qualifiers.pop_back_val(); |
| 1351 | NestedNameSpecifier *NNS = Q.getNestedNameSpecifier(); |
| 1352 | switch (NNS->getKind()) { |
| 1353 | case NestedNameSpecifier::Namespace: |
| 1354 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), |
| 1355 | Q.getLocalBeginLoc(), |
| 1356 | TU))) |
| 1357 | return true; |
| 1358 | |
| 1359 | break; |
| 1360 | |
| 1361 | case NestedNameSpecifier::NamespaceAlias: |
| 1362 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1363 | Q.getLocalBeginLoc(), |
| 1364 | TU))) |
| 1365 | return true; |
| 1366 | |
| 1367 | break; |
| 1368 | |
| 1369 | case NestedNameSpecifier::TypeSpec: |
| 1370 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1371 | if (Visit(Q.getTypeLoc())) |
| 1372 | return true; |
| 1373 | |
| 1374 | break; |
| 1375 | |
| 1376 | case NestedNameSpecifier::Global: |
| 1377 | case NestedNameSpecifier::Identifier: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 1378 | case NestedNameSpecifier::Super: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1379 | break; |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | return false; |
| 1384 | } |
| 1385 | |
| 1386 | bool CursorVisitor::VisitTemplateParameters( |
| 1387 | const TemplateParameterList *Params) { |
| 1388 | if (!Params) |
| 1389 | return false; |
| 1390 | |
| 1391 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 1392 | PEnd = Params->end(); |
| 1393 | P != PEnd; ++P) { |
| 1394 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
| 1395 | return true; |
| 1396 | } |
| 1397 | |
| 1398 | return false; |
| 1399 | } |
| 1400 | |
| 1401 | bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { |
| 1402 | switch (Name.getKind()) { |
| 1403 | case TemplateName::Template: |
| 1404 | return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); |
| 1405 | |
| 1406 | case TemplateName::OverloadedTemplate: |
| 1407 | // Visit the overloaded template set. |
| 1408 | if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU))) |
| 1409 | return true; |
| 1410 | |
| 1411 | return false; |
| 1412 | |
| 1413 | case TemplateName::DependentTemplate: |
| 1414 | // FIXME: Visit nested-name-specifier. |
| 1415 | return false; |
| 1416 | |
| 1417 | case TemplateName::QualifiedTemplate: |
| 1418 | // FIXME: Visit nested-name-specifier. |
| 1419 | return Visit(MakeCursorTemplateRef( |
| 1420 | Name.getAsQualifiedTemplateName()->getDecl(), |
| 1421 | Loc, TU)); |
| 1422 | |
| 1423 | case TemplateName::SubstTemplateTemplateParm: |
| 1424 | return Visit(MakeCursorTemplateRef( |
| 1425 | Name.getAsSubstTemplateTemplateParm()->getParameter(), |
| 1426 | Loc, TU)); |
| 1427 | |
| 1428 | case TemplateName::SubstTemplateTemplateParmPack: |
| 1429 | return Visit(MakeCursorTemplateRef( |
| 1430 | Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), |
| 1431 | Loc, TU)); |
| 1432 | } |
| 1433 | |
| 1434 | llvm_unreachable("Invalid TemplateName::Kind!"); |
| 1435 | } |
| 1436 | |
| 1437 | bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { |
| 1438 | switch (TAL.getArgument().getKind()) { |
| 1439 | case TemplateArgument::Null: |
| 1440 | case TemplateArgument::Integral: |
| 1441 | case TemplateArgument::Pack: |
| 1442 | return false; |
| 1443 | |
| 1444 | case TemplateArgument::Type: |
| 1445 | if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) |
| 1446 | return Visit(TSInfo->getTypeLoc()); |
| 1447 | return false; |
| 1448 | |
| 1449 | case TemplateArgument::Declaration: |
| 1450 | if (Expr *E = TAL.getSourceDeclExpression()) |
| 1451 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
| 1452 | return false; |
| 1453 | |
| 1454 | case TemplateArgument::NullPtr: |
| 1455 | if (Expr *E = TAL.getSourceNullPtrExpression()) |
| 1456 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
| 1457 | return false; |
| 1458 | |
| 1459 | case TemplateArgument::Expression: |
| 1460 | if (Expr *E = TAL.getSourceExpression()) |
| 1461 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
| 1462 | return false; |
| 1463 | |
| 1464 | case TemplateArgument::Template: |
| 1465 | case TemplateArgument::TemplateExpansion: |
| 1466 | if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc())) |
| 1467 | return true; |
| 1468 | |
| 1469 | return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(), |
| 1470 | TAL.getTemplateNameLoc()); |
| 1471 | } |
| 1472 | |
| 1473 | llvm_unreachable("Invalid TemplateArgument::Kind!"); |
| 1474 | } |
| 1475 | |
| 1476 | bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 1477 | return VisitDeclContext(D); |
| 1478 | } |
| 1479 | |
| 1480 | bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1481 | return Visit(TL.getUnqualifiedLoc()); |
| 1482 | } |
| 1483 | |
| 1484 | bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
| 1485 | ASTContext &Context = AU->getASTContext(); |
| 1486 | |
| 1487 | // Some builtin types (such as Objective-C's "id", "sel", and |
| 1488 | // "Class") have associated declarations. Create cursors for those. |
| 1489 | QualType VisitType; |
| 1490 | switch (TL.getTypePtr()->getKind()) { |
| 1491 | |
| 1492 | case BuiltinType::Void: |
| 1493 | case BuiltinType::NullPtr: |
| 1494 | case BuiltinType::Dependent: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 1495 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 1496 | case BuiltinType::Id: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 1497 | #include "clang/Basic/OpenCLImageTypes.def" |
NAKAMURA Takumi | 288c42e | 2013-02-07 12:47:42 +0000 | [diff] [blame] | 1498 | case BuiltinType::OCLSampler: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1499 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1500 | case BuiltinType::OCLClkEvent: |
| 1501 | case BuiltinType::OCLQueue: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1502 | case BuiltinType::OCLReserveID: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1503 | #define BUILTIN_TYPE(Id, SingletonId) |
| 1504 | #define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1505 | #define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1506 | #define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1507 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1508 | #include "clang/AST/BuiltinTypes.def" |
| 1509 | break; |
| 1510 | |
| 1511 | case BuiltinType::ObjCId: |
| 1512 | VisitType = Context.getObjCIdType(); |
| 1513 | break; |
| 1514 | |
| 1515 | case BuiltinType::ObjCClass: |
| 1516 | VisitType = Context.getObjCClassType(); |
| 1517 | break; |
| 1518 | |
| 1519 | case BuiltinType::ObjCSel: |
| 1520 | VisitType = Context.getObjCSelType(); |
| 1521 | break; |
| 1522 | } |
| 1523 | |
| 1524 | if (!VisitType.isNull()) { |
| 1525 | if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) |
| 1526 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), |
| 1527 | TU)); |
| 1528 | } |
| 1529 | |
| 1530 | return false; |
| 1531 | } |
| 1532 | |
| 1533 | bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 1534 | return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU)); |
| 1535 | } |
| 1536 | |
| 1537 | bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 1538 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1539 | } |
| 1540 | |
| 1541 | bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { |
| 1542 | if (TL.isDefinition()) |
| 1543 | return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest)); |
| 1544 | |
| 1545 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1546 | } |
| 1547 | |
| 1548 | bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1549 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1550 | } |
| 1551 | |
| 1552 | bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 1553 | return Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 1556 | bool CursorVisitor::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { |
| 1557 | if (Visit(MakeCursorTypeRef(TL.getDecl(), TL.getLocStart(), TU))) |
| 1558 | return true; |
| 1559 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1560 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1561 | TU))) |
| 1562 | return true; |
| 1563 | } |
| 1564 | |
| 1565 | return false; |
| 1566 | } |
| 1567 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1568 | bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 1569 | if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) |
| 1570 | return true; |
| 1571 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1572 | for (unsigned I = 0, N = TL.getNumTypeArgs(); I != N; ++I) { |
| 1573 | if (Visit(TL.getTypeArgTInfo(I)->getTypeLoc())) |
| 1574 | return true; |
| 1575 | } |
| 1576 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1577 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1578 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1579 | TU))) |
| 1580 | return true; |
| 1581 | } |
| 1582 | |
| 1583 | return false; |
| 1584 | } |
| 1585 | |
| 1586 | bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 1587 | return Visit(TL.getPointeeLoc()); |
| 1588 | } |
| 1589 | |
| 1590 | bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) { |
| 1591 | return Visit(TL.getInnerLoc()); |
| 1592 | } |
| 1593 | |
| 1594 | bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1595 | return Visit(TL.getPointeeLoc()); |
| 1596 | } |
| 1597 | |
| 1598 | bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1599 | return Visit(TL.getPointeeLoc()); |
| 1600 | } |
| 1601 | |
| 1602 | bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1603 | return Visit(TL.getPointeeLoc()); |
| 1604 | } |
| 1605 | |
| 1606 | bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 1607 | return Visit(TL.getPointeeLoc()); |
| 1608 | } |
| 1609 | |
| 1610 | bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 1611 | return Visit(TL.getPointeeLoc()); |
| 1612 | } |
| 1613 | |
| 1614 | bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 1615 | return Visit(TL.getModifiedLoc()); |
| 1616 | } |
| 1617 | |
| 1618 | bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, |
| 1619 | bool SkipResultType) { |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 1620 | if (!SkipResultType && Visit(TL.getReturnLoc())) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1621 | return true; |
| 1622 | |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 1623 | for (unsigned I = 0, N = TL.getNumParams(); I != N; ++I) |
| 1624 | if (Decl *D = TL.getParam(I)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1625 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest))) |
| 1626 | return true; |
| 1627 | |
| 1628 | return false; |
| 1629 | } |
| 1630 | |
| 1631 | bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1632 | if (Visit(TL.getElementLoc())) |
| 1633 | return true; |
| 1634 | |
| 1635 | if (Expr *Size = TL.getSizeExpr()) |
| 1636 | return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest)); |
| 1637 | |
| 1638 | return false; |
| 1639 | } |
| 1640 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 1641 | bool CursorVisitor::VisitDecayedTypeLoc(DecayedTypeLoc TL) { |
| 1642 | return Visit(TL.getOriginalLoc()); |
| 1643 | } |
| 1644 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 1645 | bool CursorVisitor::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { |
| 1646 | return Visit(TL.getOriginalLoc()); |
| 1647 | } |
| 1648 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1649 | bool CursorVisitor::VisitDeducedTemplateSpecializationTypeLoc( |
| 1650 | DeducedTemplateSpecializationTypeLoc TL) { |
| 1651 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1652 | TL.getTemplateNameLoc())) |
| 1653 | return true; |
| 1654 | |
| 1655 | return false; |
| 1656 | } |
| 1657 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1658 | bool CursorVisitor::VisitTemplateSpecializationTypeLoc( |
| 1659 | TemplateSpecializationTypeLoc TL) { |
| 1660 | // Visit the template name. |
| 1661 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1662 | TL.getTemplateNameLoc())) |
| 1663 | return true; |
| 1664 | |
| 1665 | // Visit the template arguments. |
| 1666 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1667 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1668 | return true; |
| 1669 | |
| 1670 | return false; |
| 1671 | } |
| 1672 | |
| 1673 | bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 1674 | return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); |
| 1675 | } |
| 1676 | |
| 1677 | bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 1678 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1679 | return Visit(TSInfo->getTypeLoc()); |
| 1680 | |
| 1681 | return false; |
| 1682 | } |
| 1683 | |
| 1684 | bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 1685 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1686 | return Visit(TSInfo->getTypeLoc()); |
| 1687 | |
| 1688 | return false; |
| 1689 | } |
| 1690 | |
| 1691 | bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 1692 | return VisitNestedNameSpecifierLoc(TL.getQualifierLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc( |
| 1696 | DependentTemplateSpecializationTypeLoc TL) { |
| 1697 | // Visit the nested-name-specifier, if there is one. |
| 1698 | if (TL.getQualifierLoc() && |
| 1699 | VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1700 | return true; |
| 1701 | |
| 1702 | // Visit the template arguments. |
| 1703 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1704 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1705 | return true; |
| 1706 | |
| 1707 | return false; |
| 1708 | } |
| 1709 | |
| 1710 | bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 1711 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1712 | return true; |
| 1713 | |
| 1714 | return Visit(TL.getNamedTypeLoc()); |
| 1715 | } |
| 1716 | |
| 1717 | bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { |
| 1718 | return Visit(TL.getPatternLoc()); |
| 1719 | } |
| 1720 | |
| 1721 | bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 1722 | if (Expr *E = TL.getUnderlyingExpr()) |
| 1723 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1724 | |
| 1725 | return false; |
| 1726 | } |
| 1727 | |
| 1728 | bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 1729 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1730 | } |
| 1731 | |
| 1732 | bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) { |
| 1733 | return Visit(TL.getValueLoc()); |
| 1734 | } |
| 1735 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 1736 | bool CursorVisitor::VisitPipeTypeLoc(PipeTypeLoc TL) { |
| 1737 | return Visit(TL.getValueLoc()); |
| 1738 | } |
| 1739 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1740 | #define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \ |
| 1741 | bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \ |
| 1742 | return Visit##PARENT##Loc(TL); \ |
| 1743 | } |
| 1744 | |
| 1745 | DEFAULT_TYPELOC_IMPL(Complex, Type) |
| 1746 | DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType) |
| 1747 | DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType) |
| 1748 | DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType) |
| 1749 | DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType) |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 1750 | DEFAULT_TYPELOC_IMPL(DependentAddressSpace, Type) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1751 | DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type) |
| 1752 | DEFAULT_TYPELOC_IMPL(Vector, Type) |
| 1753 | DEFAULT_TYPELOC_IMPL(ExtVector, VectorType) |
| 1754 | DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType) |
| 1755 | DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType) |
| 1756 | DEFAULT_TYPELOC_IMPL(Record, TagType) |
| 1757 | DEFAULT_TYPELOC_IMPL(Enum, TagType) |
| 1758 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type) |
| 1759 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type) |
| 1760 | DEFAULT_TYPELOC_IMPL(Auto, Type) |
| 1761 | |
| 1762 | bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 1763 | // Visit the nested-name-specifier, if present. |
| 1764 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1765 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1766 | return true; |
| 1767 | |
| 1768 | if (D->isCompleteDefinition()) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1769 | for (const auto &I : D->bases()) { |
| 1770 | if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(&I, TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1771 | return true; |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | return VisitTagDecl(D); |
| 1776 | } |
| 1777 | |
| 1778 | bool CursorVisitor::VisitAttributes(Decl *D) { |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 1779 | for (const auto *I : D->attrs()) |
| 1780 | if (Visit(MakeCXCursor(I, D, TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1781 | return true; |
| 1782 | |
| 1783 | return false; |
| 1784 | } |
| 1785 | |
| 1786 | //===----------------------------------------------------------------------===// |
| 1787 | // Data-recursive visitor methods. |
| 1788 | //===----------------------------------------------------------------------===// |
| 1789 | |
| 1790 | namespace { |
| 1791 | #define DEF_JOB(NAME, DATA, KIND)\ |
| 1792 | class NAME : public VisitorJob {\ |
| 1793 | public:\ |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1794 | NAME(const DATA *d, CXCursor parent) : \ |
| 1795 | VisitorJob(parent, VisitorJob::KIND, d) {} \ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1796 | static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\ |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1797 | const DATA *get() const { return static_cast<const DATA*>(data[0]); }\ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1798 | }; |
| 1799 | |
| 1800 | DEF_JOB(StmtVisit, Stmt, StmtVisitKind) |
| 1801 | DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind) |
| 1802 | DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind) |
| 1803 | DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1804 | DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind) |
| 1805 | DEF_JOB(LambdaExprParts, LambdaExpr, LambdaExprPartsKind) |
| 1806 | DEF_JOB(PostChildrenVisit, void, PostChildrenVisitKind) |
| 1807 | #undef DEF_JOB |
| 1808 | |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 1809 | class ExplicitTemplateArgsVisit : public VisitorJob { |
| 1810 | public: |
| 1811 | ExplicitTemplateArgsVisit(const TemplateArgumentLoc *Begin, |
| 1812 | const TemplateArgumentLoc *End, CXCursor parent) |
| 1813 | : VisitorJob(parent, VisitorJob::ExplicitTemplateArgsVisitKind, Begin, |
| 1814 | End) {} |
| 1815 | static bool classof(const VisitorJob *VJ) { |
| 1816 | return VJ->getKind() == ExplicitTemplateArgsVisitKind; |
| 1817 | } |
| 1818 | const TemplateArgumentLoc *begin() const { |
| 1819 | return static_cast<const TemplateArgumentLoc *>(data[0]); |
| 1820 | } |
| 1821 | const TemplateArgumentLoc *end() { |
| 1822 | return static_cast<const TemplateArgumentLoc *>(data[1]); |
| 1823 | } |
| 1824 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1825 | class DeclVisit : public VisitorJob { |
| 1826 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1827 | DeclVisit(const Decl *D, CXCursor parent, bool isFirst) : |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1828 | VisitorJob(parent, VisitorJob::DeclVisitKind, |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 1829 | D, isFirst ? (void*) 1 : (void*) nullptr) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1830 | static bool classof(const VisitorJob *VJ) { |
| 1831 | return VJ->getKind() == DeclVisitKind; |
| 1832 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1833 | const Decl *get() const { return static_cast<const Decl *>(data[0]); } |
Dmitri Gribenko | e5423a7 | 2015-03-23 19:23:50 +0000 | [diff] [blame] | 1834 | bool isFirst() const { return data[1] != nullptr; } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1835 | }; |
| 1836 | class TypeLocVisit : public VisitorJob { |
| 1837 | public: |
| 1838 | TypeLocVisit(TypeLoc tl, CXCursor parent) : |
| 1839 | VisitorJob(parent, VisitorJob::TypeLocVisitKind, |
| 1840 | tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {} |
| 1841 | |
| 1842 | static bool classof(const VisitorJob *VJ) { |
| 1843 | return VJ->getKind() == TypeLocVisitKind; |
| 1844 | } |
| 1845 | |
| 1846 | TypeLoc get() const { |
| 1847 | QualType T = QualType::getFromOpaquePtr(data[0]); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1848 | return TypeLoc(T, const_cast<void *>(data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1849 | } |
| 1850 | }; |
| 1851 | |
| 1852 | class LabelRefVisit : public VisitorJob { |
| 1853 | public: |
| 1854 | LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent) |
| 1855 | : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD, |
| 1856 | labelLoc.getPtrEncoding()) {} |
| 1857 | |
| 1858 | static bool classof(const VisitorJob *VJ) { |
| 1859 | return VJ->getKind() == VisitorJob::LabelRefVisitKind; |
| 1860 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1861 | const LabelDecl *get() const { |
| 1862 | return static_cast<const LabelDecl *>(data[0]); |
| 1863 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1864 | SourceLocation getLoc() const { |
| 1865 | return SourceLocation::getFromPtrEncoding(data[1]); } |
| 1866 | }; |
| 1867 | |
| 1868 | class NestedNameSpecifierLocVisit : public VisitorJob { |
| 1869 | public: |
| 1870 | NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent) |
| 1871 | : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind, |
| 1872 | Qualifier.getNestedNameSpecifier(), |
| 1873 | Qualifier.getOpaqueData()) { } |
| 1874 | |
| 1875 | static bool classof(const VisitorJob *VJ) { |
| 1876 | return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind; |
| 1877 | } |
| 1878 | |
| 1879 | NestedNameSpecifierLoc get() const { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1880 | return NestedNameSpecifierLoc( |
| 1881 | const_cast<NestedNameSpecifier *>( |
| 1882 | static_cast<const NestedNameSpecifier *>(data[0])), |
| 1883 | const_cast<void *>(data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1884 | } |
| 1885 | }; |
| 1886 | |
| 1887 | class DeclarationNameInfoVisit : public VisitorJob { |
| 1888 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1889 | DeclarationNameInfoVisit(const Stmt *S, CXCursor parent) |
Dmitri Gribenko | dd7dacf | 2013-02-03 13:19:54 +0000 | [diff] [blame] | 1890 | : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1891 | static bool classof(const VisitorJob *VJ) { |
| 1892 | return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind; |
| 1893 | } |
| 1894 | DeclarationNameInfo get() const { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1895 | const Stmt *S = static_cast<const Stmt *>(data[0]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1896 | switch (S->getStmtClass()) { |
| 1897 | default: |
| 1898 | llvm_unreachable("Unhandled Stmt"); |
| 1899 | case clang::Stmt::MSDependentExistsStmtClass: |
| 1900 | return cast<MSDependentExistsStmt>(S)->getNameInfo(); |
| 1901 | case Stmt::CXXDependentScopeMemberExprClass: |
| 1902 | return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo(); |
| 1903 | case Stmt::DependentScopeDeclRefExprClass: |
| 1904 | return cast<DependentScopeDeclRefExpr>(S)->getNameInfo(); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1905 | case Stmt::OMPCriticalDirectiveClass: |
| 1906 | return cast<OMPCriticalDirective>(S)->getDirectiveName(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1907 | } |
| 1908 | } |
| 1909 | }; |
| 1910 | class MemberRefVisit : public VisitorJob { |
| 1911 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1912 | MemberRefVisit(const FieldDecl *D, SourceLocation L, CXCursor parent) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1913 | : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D, |
| 1914 | L.getPtrEncoding()) {} |
| 1915 | static bool classof(const VisitorJob *VJ) { |
| 1916 | return VJ->getKind() == VisitorJob::MemberRefVisitKind; |
| 1917 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1918 | const FieldDecl *get() const { |
| 1919 | return static_cast<const FieldDecl *>(data[0]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1920 | } |
| 1921 | SourceLocation getLoc() const { |
| 1922 | return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]); |
| 1923 | } |
| 1924 | }; |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1925 | class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void> { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1926 | friend class OMPClauseEnqueue; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1927 | VisitorWorkList &WL; |
| 1928 | CXCursor Parent; |
| 1929 | public: |
| 1930 | EnqueueVisitor(VisitorWorkList &wl, CXCursor parent) |
| 1931 | : WL(wl), Parent(parent) {} |
| 1932 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1933 | void VisitAddrLabelExpr(const AddrLabelExpr *E); |
| 1934 | void VisitBlockExpr(const BlockExpr *B); |
| 1935 | void VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 1936 | void VisitCompoundStmt(const CompoundStmt *S); |
| 1937 | void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { /* Do nothing. */ } |
| 1938 | void VisitMSDependentExistsStmt(const MSDependentExistsStmt *S); |
| 1939 | void VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E); |
| 1940 | void VisitCXXNewExpr(const CXXNewExpr *E); |
| 1941 | void VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E); |
| 1942 | void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *E); |
| 1943 | void VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E); |
| 1944 | void VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *E); |
| 1945 | void VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
| 1946 | void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *E); |
| 1947 | void VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
| 1948 | void VisitCXXCatchStmt(const CXXCatchStmt *S); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 1949 | void VisitCXXForRangeStmt(const CXXForRangeStmt *S); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1950 | void VisitDeclRefExpr(const DeclRefExpr *D); |
| 1951 | void VisitDeclStmt(const DeclStmt *S); |
| 1952 | void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E); |
| 1953 | void VisitDesignatedInitExpr(const DesignatedInitExpr *E); |
| 1954 | void VisitExplicitCastExpr(const ExplicitCastExpr *E); |
| 1955 | void VisitForStmt(const ForStmt *FS); |
| 1956 | void VisitGotoStmt(const GotoStmt *GS); |
| 1957 | void VisitIfStmt(const IfStmt *If); |
| 1958 | void VisitInitListExpr(const InitListExpr *IE); |
| 1959 | void VisitMemberExpr(const MemberExpr *M); |
| 1960 | void VisitOffsetOfExpr(const OffsetOfExpr *E); |
| 1961 | void VisitObjCEncodeExpr(const ObjCEncodeExpr *E); |
| 1962 | void VisitObjCMessageExpr(const ObjCMessageExpr *M); |
| 1963 | void VisitOverloadExpr(const OverloadExpr *E); |
| 1964 | void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
| 1965 | void VisitStmt(const Stmt *S); |
| 1966 | void VisitSwitchStmt(const SwitchStmt *S); |
| 1967 | void VisitWhileStmt(const WhileStmt *W); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1968 | void VisitTypeTraitExpr(const TypeTraitExpr *E); |
| 1969 | void VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E); |
| 1970 | void VisitExpressionTraitExpr(const ExpressionTraitExpr *E); |
| 1971 | void VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U); |
| 1972 | void VisitVAArgExpr(const VAArgExpr *E); |
| 1973 | void VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
| 1974 | void VisitPseudoObjectExpr(const PseudoObjectExpr *E); |
| 1975 | void VisitOpaqueValueExpr(const OpaqueValueExpr *E); |
| 1976 | void VisitLambdaExpr(const LambdaExpr *E); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1977 | void VisitOMPExecutableDirective(const OMPExecutableDirective *D); |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 1978 | void VisitOMPLoopDirective(const OMPLoopDirective *D); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1979 | void VisitOMPParallelDirective(const OMPParallelDirective *D); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 1980 | void VisitOMPSimdDirective(const OMPSimdDirective *D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1981 | void VisitOMPForDirective(const OMPForDirective *D); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1982 | void VisitOMPForSimdDirective(const OMPForSimdDirective *D); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 1983 | void VisitOMPSectionsDirective(const OMPSectionsDirective *D); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1984 | void VisitOMPSectionDirective(const OMPSectionDirective *D); |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 1985 | void VisitOMPSingleDirective(const OMPSingleDirective *D); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1986 | void VisitOMPMasterDirective(const OMPMasterDirective *D); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1987 | void VisitOMPCriticalDirective(const OMPCriticalDirective *D); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1988 | void VisitOMPParallelForDirective(const OMPParallelForDirective *D); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1989 | void VisitOMPParallelForSimdDirective(const OMPParallelForSimdDirective *D); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1990 | void VisitOMPParallelSectionsDirective(const OMPParallelSectionsDirective *D); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1991 | void VisitOMPTaskDirective(const OMPTaskDirective *D); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1992 | void VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 1993 | void VisitOMPBarrierDirective(const OMPBarrierDirective *D); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 1994 | void VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1995 | void VisitOMPTaskgroupDirective(const OMPTaskgroupDirective *D); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 1996 | void |
| 1997 | VisitOMPCancellationPointDirective(const OMPCancellationPointDirective *D); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 1998 | void VisitOMPCancelDirective(const OMPCancelDirective *D); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 1999 | void VisitOMPFlushDirective(const OMPFlushDirective *D); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2000 | void VisitOMPOrderedDirective(const OMPOrderedDirective *D); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2001 | void VisitOMPAtomicDirective(const OMPAtomicDirective *D); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 2002 | void VisitOMPTargetDirective(const OMPTargetDirective *D); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2003 | void VisitOMPTargetDataDirective(const OMPTargetDataDirective *D); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 2004 | void VisitOMPTargetEnterDataDirective(const OMPTargetEnterDataDirective *D); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 2005 | void VisitOMPTargetExitDataDirective(const OMPTargetExitDataDirective *D); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 2006 | void VisitOMPTargetParallelDirective(const OMPTargetParallelDirective *D); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 2007 | void |
| 2008 | VisitOMPTargetParallelForDirective(const OMPTargetParallelForDirective *D); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2009 | void VisitOMPTeamsDirective(const OMPTeamsDirective *D); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 2010 | void VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 2011 | void VisitOMPTaskLoopSimdDirective(const OMPTaskLoopSimdDirective *D); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2012 | void VisitOMPDistributeDirective(const OMPDistributeDirective *D); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2013 | void VisitOMPDistributeParallelForDirective( |
| 2014 | const OMPDistributeParallelForDirective *D); |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2015 | void VisitOMPDistributeParallelForSimdDirective( |
| 2016 | const OMPDistributeParallelForSimdDirective *D); |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2017 | void VisitOMPDistributeSimdDirective(const OMPDistributeSimdDirective *D); |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 2018 | void VisitOMPTargetParallelForSimdDirective( |
| 2019 | const OMPTargetParallelForSimdDirective *D); |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2020 | void VisitOMPTargetSimdDirective(const OMPTargetSimdDirective *D); |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 2021 | void VisitOMPTeamsDistributeDirective(const OMPTeamsDistributeDirective *D); |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 2022 | void VisitOMPTeamsDistributeSimdDirective( |
| 2023 | const OMPTeamsDistributeSimdDirective *D); |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 2024 | void VisitOMPTeamsDistributeParallelForSimdDirective( |
| 2025 | const OMPTeamsDistributeParallelForSimdDirective *D); |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 2026 | void VisitOMPTeamsDistributeParallelForDirective( |
| 2027 | const OMPTeamsDistributeParallelForDirective *D); |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 2028 | void VisitOMPTargetTeamsDirective(const OMPTargetTeamsDirective *D); |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 2029 | void VisitOMPTargetTeamsDistributeDirective( |
| 2030 | const OMPTargetTeamsDistributeDirective *D); |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 2031 | void VisitOMPTargetTeamsDistributeParallelForDirective( |
| 2032 | const OMPTargetTeamsDistributeParallelForDirective *D); |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 2033 | void VisitOMPTargetTeamsDistributeParallelForSimdDirective( |
| 2034 | const OMPTargetTeamsDistributeParallelForSimdDirective *D); |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 2035 | void VisitOMPTargetTeamsDistributeSimdDirective( |
| 2036 | const OMPTargetTeamsDistributeSimdDirective *D); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2037 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2038 | private: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2039 | void AddDeclarationNameInfo(const Stmt *S); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2040 | void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier); |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2041 | void AddExplicitTemplateArgs(const TemplateArgumentLoc *A, |
| 2042 | unsigned NumTemplateArgs); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2043 | void AddMemberRef(const FieldDecl *D, SourceLocation L); |
| 2044 | void AddStmt(const Stmt *S); |
| 2045 | void AddDecl(const Decl *D, bool isFirst = true); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2046 | void AddTypeLoc(TypeSourceInfo *TI); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2047 | void EnqueueChildren(const Stmt *S); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2048 | void EnqueueChildren(const OMPClause *S); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2049 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2050 | } // end anonyous namespace |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2051 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2052 | void EnqueueVisitor::AddDeclarationNameInfo(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2053 | // 'S' should always be non-null, since it comes from the |
| 2054 | // statement we are visiting. |
| 2055 | WL.push_back(DeclarationNameInfoVisit(S, Parent)); |
| 2056 | } |
| 2057 | |
| 2058 | void |
| 2059 | EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 2060 | if (Qualifier) |
| 2061 | WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent)); |
| 2062 | } |
| 2063 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2064 | void EnqueueVisitor::AddStmt(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2065 | if (S) |
| 2066 | WL.push_back(StmtVisit(S, Parent)); |
| 2067 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2068 | void EnqueueVisitor::AddDecl(const Decl *D, bool isFirst) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2069 | if (D) |
| 2070 | WL.push_back(DeclVisit(D, Parent, isFirst)); |
| 2071 | } |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2072 | void EnqueueVisitor::AddExplicitTemplateArgs(const TemplateArgumentLoc *A, |
| 2073 | unsigned NumTemplateArgs) { |
| 2074 | WL.push_back(ExplicitTemplateArgsVisit(A, A + NumTemplateArgs, Parent)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2075 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2076 | void EnqueueVisitor::AddMemberRef(const FieldDecl *D, SourceLocation L) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2077 | if (D) |
| 2078 | WL.push_back(MemberRefVisit(D, L, Parent)); |
| 2079 | } |
| 2080 | void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) { |
| 2081 | if (TI) |
| 2082 | WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent)); |
| 2083 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2084 | void EnqueueVisitor::EnqueueChildren(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2085 | unsigned size = WL.size(); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2086 | for (const Stmt *SubStmt : S->children()) { |
| 2087 | AddStmt(SubStmt); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2088 | } |
| 2089 | if (size == WL.size()) |
| 2090 | return; |
| 2091 | // Now reverse the entries we just added. This will match the DFS |
| 2092 | // ordering performed by the worklist. |
| 2093 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2094 | std::reverse(I, E); |
| 2095 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2096 | namespace { |
| 2097 | class OMPClauseEnqueue : public ConstOMPClauseVisitor<OMPClauseEnqueue> { |
| 2098 | EnqueueVisitor *Visitor; |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2099 | /// \brief Process clauses with list of variables. |
| 2100 | template <typename T> |
| 2101 | void VisitOMPClauseList(T *Node); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2102 | public: |
| 2103 | OMPClauseEnqueue(EnqueueVisitor *Visitor) : Visitor(Visitor) { } |
| 2104 | #define OPENMP_CLAUSE(Name, Class) \ |
| 2105 | void Visit##Class(const Class *C); |
| 2106 | #include "clang/Basic/OpenMPKinds.def" |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2107 | void VisitOMPClauseWithPreInit(const OMPClauseWithPreInit *C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2108 | void VisitOMPClauseWithPostUpdate(const OMPClauseWithPostUpdate *C); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2109 | }; |
| 2110 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2111 | void OMPClauseEnqueue::VisitOMPClauseWithPreInit( |
| 2112 | const OMPClauseWithPreInit *C) { |
| 2113 | Visitor->AddStmt(C->getPreInitStmt()); |
| 2114 | } |
| 2115 | |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2116 | void OMPClauseEnqueue::VisitOMPClauseWithPostUpdate( |
| 2117 | const OMPClauseWithPostUpdate *C) { |
Alexey Bataev | 37e594c | 2016-03-04 07:21:16 +0000 | [diff] [blame] | 2118 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2119 | Visitor->AddStmt(C->getPostUpdateExpr()); |
| 2120 | } |
| 2121 | |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2122 | void OMPClauseEnqueue::VisitOMPIfClause(const OMPIfClause *C) { |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 2123 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2124 | Visitor->AddStmt(C->getCondition()); |
| 2125 | } |
| 2126 | |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 2127 | void OMPClauseEnqueue::VisitOMPFinalClause(const OMPFinalClause *C) { |
| 2128 | Visitor->AddStmt(C->getCondition()); |
| 2129 | } |
| 2130 | |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2131 | void OMPClauseEnqueue::VisitOMPNumThreadsClause(const OMPNumThreadsClause *C) { |
Arpith Chacko Jacob | 33c849a | 2017-01-25 00:57:16 +0000 | [diff] [blame] | 2132 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2133 | Visitor->AddStmt(C->getNumThreads()); |
| 2134 | } |
| 2135 | |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 2136 | void OMPClauseEnqueue::VisitOMPSafelenClause(const OMPSafelenClause *C) { |
| 2137 | Visitor->AddStmt(C->getSafelen()); |
| 2138 | } |
| 2139 | |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 2140 | void OMPClauseEnqueue::VisitOMPSimdlenClause(const OMPSimdlenClause *C) { |
| 2141 | Visitor->AddStmt(C->getSimdlen()); |
| 2142 | } |
| 2143 | |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 2144 | void OMPClauseEnqueue::VisitOMPCollapseClause(const OMPCollapseClause *C) { |
| 2145 | Visitor->AddStmt(C->getNumForLoops()); |
| 2146 | } |
| 2147 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2148 | void OMPClauseEnqueue::VisitOMPDefaultClause(const OMPDefaultClause *C) { } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2149 | |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 2150 | void OMPClauseEnqueue::VisitOMPProcBindClause(const OMPProcBindClause *C) { } |
| 2151 | |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2152 | void OMPClauseEnqueue::VisitOMPScheduleClause(const OMPScheduleClause *C) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2153 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2154 | Visitor->AddStmt(C->getChunkSize()); |
| 2155 | } |
| 2156 | |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 2157 | void OMPClauseEnqueue::VisitOMPOrderedClause(const OMPOrderedClause *C) { |
| 2158 | Visitor->AddStmt(C->getNumForLoops()); |
| 2159 | } |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 2160 | |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 2161 | void OMPClauseEnqueue::VisitOMPNowaitClause(const OMPNowaitClause *) {} |
| 2162 | |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 2163 | void OMPClauseEnqueue::VisitOMPUntiedClause(const OMPUntiedClause *) {} |
| 2164 | |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 2165 | void OMPClauseEnqueue::VisitOMPMergeableClause(const OMPMergeableClause *) {} |
| 2166 | |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 2167 | void OMPClauseEnqueue::VisitOMPReadClause(const OMPReadClause *) {} |
| 2168 | |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 2169 | void OMPClauseEnqueue::VisitOMPWriteClause(const OMPWriteClause *) {} |
| 2170 | |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 2171 | void OMPClauseEnqueue::VisitOMPUpdateClause(const OMPUpdateClause *) {} |
| 2172 | |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 2173 | void OMPClauseEnqueue::VisitOMPCaptureClause(const OMPCaptureClause *) {} |
| 2174 | |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 2175 | void OMPClauseEnqueue::VisitOMPSeqCstClause(const OMPSeqCstClause *) {} |
| 2176 | |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 2177 | void OMPClauseEnqueue::VisitOMPThreadsClause(const OMPThreadsClause *) {} |
| 2178 | |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 2179 | void OMPClauseEnqueue::VisitOMPSIMDClause(const OMPSIMDClause *) {} |
| 2180 | |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 2181 | void OMPClauseEnqueue::VisitOMPNogroupClause(const OMPNogroupClause *) {} |
| 2182 | |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 2183 | void OMPClauseEnqueue::VisitOMPDeviceClause(const OMPDeviceClause *C) { |
| 2184 | Visitor->AddStmt(C->getDevice()); |
| 2185 | } |
| 2186 | |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 2187 | void OMPClauseEnqueue::VisitOMPNumTeamsClause(const OMPNumTeamsClause *C) { |
Arpith Chacko Jacob | bc12634 | 2017-01-25 11:28:18 +0000 | [diff] [blame] | 2188 | VisitOMPClauseWithPreInit(C); |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 2189 | Visitor->AddStmt(C->getNumTeams()); |
| 2190 | } |
| 2191 | |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 2192 | void OMPClauseEnqueue::VisitOMPThreadLimitClause(const OMPThreadLimitClause *C) { |
Arpith Chacko Jacob | 7ecc0b7 | 2017-01-25 11:44:35 +0000 | [diff] [blame] | 2193 | VisitOMPClauseWithPreInit(C); |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 2194 | Visitor->AddStmt(C->getThreadLimit()); |
| 2195 | } |
| 2196 | |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 2197 | void OMPClauseEnqueue::VisitOMPPriorityClause(const OMPPriorityClause *C) { |
| 2198 | Visitor->AddStmt(C->getPriority()); |
| 2199 | } |
| 2200 | |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 2201 | void OMPClauseEnqueue::VisitOMPGrainsizeClause(const OMPGrainsizeClause *C) { |
| 2202 | Visitor->AddStmt(C->getGrainsize()); |
| 2203 | } |
| 2204 | |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 2205 | void OMPClauseEnqueue::VisitOMPNumTasksClause(const OMPNumTasksClause *C) { |
| 2206 | Visitor->AddStmt(C->getNumTasks()); |
| 2207 | } |
| 2208 | |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 2209 | void OMPClauseEnqueue::VisitOMPHintClause(const OMPHintClause *C) { |
| 2210 | Visitor->AddStmt(C->getHint()); |
| 2211 | } |
| 2212 | |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2213 | template<typename T> |
| 2214 | void OMPClauseEnqueue::VisitOMPClauseList(T *Node) { |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2215 | for (const auto *I : Node->varlists()) { |
Aaron Ballman | 2205d2a | 2014-03-14 15:55:35 +0000 | [diff] [blame] | 2216 | Visitor->AddStmt(I); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2217 | } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2218 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2219 | |
| 2220 | void OMPClauseEnqueue::VisitOMPPrivateClause(const OMPPrivateClause *C) { |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2221 | VisitOMPClauseList(C); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2222 | for (const auto *E : C->private_copies()) { |
| 2223 | Visitor->AddStmt(E); |
| 2224 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2225 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2226 | void OMPClauseEnqueue::VisitOMPFirstprivateClause( |
| 2227 | const OMPFirstprivateClause *C) { |
| 2228 | VisitOMPClauseList(C); |
Alexey Bataev | 417089f | 2016-02-17 13:19:37 +0000 | [diff] [blame] | 2229 | VisitOMPClauseWithPreInit(C); |
| 2230 | for (const auto *E : C->private_copies()) { |
| 2231 | Visitor->AddStmt(E); |
| 2232 | } |
| 2233 | for (const auto *E : C->inits()) { |
| 2234 | Visitor->AddStmt(E); |
| 2235 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2236 | } |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2237 | void OMPClauseEnqueue::VisitOMPLastprivateClause( |
| 2238 | const OMPLastprivateClause *C) { |
| 2239 | VisitOMPClauseList(C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2240 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2241 | for (auto *E : C->private_copies()) { |
| 2242 | Visitor->AddStmt(E); |
| 2243 | } |
| 2244 | for (auto *E : C->source_exprs()) { |
| 2245 | Visitor->AddStmt(E); |
| 2246 | } |
| 2247 | for (auto *E : C->destination_exprs()) { |
| 2248 | Visitor->AddStmt(E); |
| 2249 | } |
| 2250 | for (auto *E : C->assignment_ops()) { |
| 2251 | Visitor->AddStmt(E); |
| 2252 | } |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2253 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2254 | void OMPClauseEnqueue::VisitOMPSharedClause(const OMPSharedClause *C) { |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2255 | VisitOMPClauseList(C); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2256 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2257 | void OMPClauseEnqueue::VisitOMPReductionClause(const OMPReductionClause *C) { |
| 2258 | VisitOMPClauseList(C); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2259 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 2260 | for (auto *E : C->privates()) { |
| 2261 | Visitor->AddStmt(E); |
| 2262 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2263 | for (auto *E : C->lhs_exprs()) { |
| 2264 | Visitor->AddStmt(E); |
| 2265 | } |
| 2266 | for (auto *E : C->rhs_exprs()) { |
| 2267 | Visitor->AddStmt(E); |
| 2268 | } |
| 2269 | for (auto *E : C->reduction_ops()) { |
| 2270 | Visitor->AddStmt(E); |
| 2271 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2272 | } |
Alexey Bataev | 169d96a | 2017-07-18 20:17:46 +0000 | [diff] [blame] | 2273 | void OMPClauseEnqueue::VisitOMPTaskReductionClause( |
| 2274 | const OMPTaskReductionClause *C) { |
| 2275 | VisitOMPClauseList(C); |
| 2276 | VisitOMPClauseWithPostUpdate(C); |
| 2277 | for (auto *E : C->privates()) { |
| 2278 | Visitor->AddStmt(E); |
| 2279 | } |
| 2280 | for (auto *E : C->lhs_exprs()) { |
| 2281 | Visitor->AddStmt(E); |
| 2282 | } |
| 2283 | for (auto *E : C->rhs_exprs()) { |
| 2284 | Visitor->AddStmt(E); |
| 2285 | } |
| 2286 | for (auto *E : C->reduction_ops()) { |
| 2287 | Visitor->AddStmt(E); |
| 2288 | } |
| 2289 | } |
Alexey Bataev | fa312f3 | 2017-07-21 18:48:21 +0000 | [diff] [blame] | 2290 | void OMPClauseEnqueue::VisitOMPInReductionClause( |
| 2291 | const OMPInReductionClause *C) { |
| 2292 | VisitOMPClauseList(C); |
| 2293 | VisitOMPClauseWithPostUpdate(C); |
| 2294 | for (auto *E : C->privates()) { |
| 2295 | Visitor->AddStmt(E); |
| 2296 | } |
| 2297 | for (auto *E : C->lhs_exprs()) { |
| 2298 | Visitor->AddStmt(E); |
| 2299 | } |
| 2300 | for (auto *E : C->rhs_exprs()) { |
| 2301 | Visitor->AddStmt(E); |
| 2302 | } |
| 2303 | for (auto *E : C->reduction_ops()) { |
| 2304 | Visitor->AddStmt(E); |
| 2305 | } |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 2306 | for (auto *E : C->taskgroup_descriptors()) |
| 2307 | Visitor->AddStmt(E); |
Alexey Bataev | fa312f3 | 2017-07-21 18:48:21 +0000 | [diff] [blame] | 2308 | } |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2309 | void OMPClauseEnqueue::VisitOMPLinearClause(const OMPLinearClause *C) { |
| 2310 | VisitOMPClauseList(C); |
Alexey Bataev | 78849fb | 2016-03-09 09:49:00 +0000 | [diff] [blame] | 2311 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 2312 | for (const auto *E : C->privates()) { |
| 2313 | Visitor->AddStmt(E); |
| 2314 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 2315 | for (const auto *E : C->inits()) { |
| 2316 | Visitor->AddStmt(E); |
| 2317 | } |
| 2318 | for (const auto *E : C->updates()) { |
| 2319 | Visitor->AddStmt(E); |
| 2320 | } |
| 2321 | for (const auto *E : C->finals()) { |
| 2322 | Visitor->AddStmt(E); |
| 2323 | } |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2324 | Visitor->AddStmt(C->getStep()); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 2325 | Visitor->AddStmt(C->getCalcStep()); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2326 | } |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 2327 | void OMPClauseEnqueue::VisitOMPAlignedClause(const OMPAlignedClause *C) { |
| 2328 | VisitOMPClauseList(C); |
| 2329 | Visitor->AddStmt(C->getAlignment()); |
| 2330 | } |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 2331 | void OMPClauseEnqueue::VisitOMPCopyinClause(const OMPCopyinClause *C) { |
| 2332 | VisitOMPClauseList(C); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 2333 | for (auto *E : C->source_exprs()) { |
| 2334 | Visitor->AddStmt(E); |
| 2335 | } |
| 2336 | for (auto *E : C->destination_exprs()) { |
| 2337 | Visitor->AddStmt(E); |
| 2338 | } |
| 2339 | for (auto *E : C->assignment_ops()) { |
| 2340 | Visitor->AddStmt(E); |
| 2341 | } |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 2342 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2343 | void |
| 2344 | OMPClauseEnqueue::VisitOMPCopyprivateClause(const OMPCopyprivateClause *C) { |
| 2345 | VisitOMPClauseList(C); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2346 | for (auto *E : C->source_exprs()) { |
| 2347 | Visitor->AddStmt(E); |
| 2348 | } |
| 2349 | for (auto *E : C->destination_exprs()) { |
| 2350 | Visitor->AddStmt(E); |
| 2351 | } |
| 2352 | for (auto *E : C->assignment_ops()) { |
| 2353 | Visitor->AddStmt(E); |
| 2354 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2355 | } |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2356 | void OMPClauseEnqueue::VisitOMPFlushClause(const OMPFlushClause *C) { |
| 2357 | VisitOMPClauseList(C); |
| 2358 | } |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 2359 | void OMPClauseEnqueue::VisitOMPDependClause(const OMPDependClause *C) { |
| 2360 | VisitOMPClauseList(C); |
| 2361 | } |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 2362 | void OMPClauseEnqueue::VisitOMPMapClause(const OMPMapClause *C) { |
| 2363 | VisitOMPClauseList(C); |
| 2364 | } |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2365 | void OMPClauseEnqueue::VisitOMPDistScheduleClause( |
| 2366 | const OMPDistScheduleClause *C) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2367 | VisitOMPClauseWithPreInit(C); |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2368 | Visitor->AddStmt(C->getChunkSize()); |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2369 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2370 | void OMPClauseEnqueue::VisitOMPDefaultmapClause( |
| 2371 | const OMPDefaultmapClause * /*C*/) {} |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 2372 | void OMPClauseEnqueue::VisitOMPToClause(const OMPToClause *C) { |
| 2373 | VisitOMPClauseList(C); |
| 2374 | } |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 2375 | void OMPClauseEnqueue::VisitOMPFromClause(const OMPFromClause *C) { |
| 2376 | VisitOMPClauseList(C); |
| 2377 | } |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 2378 | void OMPClauseEnqueue::VisitOMPUseDevicePtrClause(const OMPUseDevicePtrClause *C) { |
| 2379 | VisitOMPClauseList(C); |
| 2380 | } |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 2381 | void OMPClauseEnqueue::VisitOMPIsDevicePtrClause(const OMPIsDevicePtrClause *C) { |
| 2382 | VisitOMPClauseList(C); |
| 2383 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2384 | } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2385 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2386 | void EnqueueVisitor::EnqueueChildren(const OMPClause *S) { |
| 2387 | unsigned size = WL.size(); |
| 2388 | OMPClauseEnqueue Visitor(this); |
| 2389 | Visitor.Visit(S); |
| 2390 | if (size == WL.size()) |
| 2391 | return; |
| 2392 | // Now reverse the entries we just added. This will match the DFS |
| 2393 | // ordering performed by the worklist. |
| 2394 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2395 | std::reverse(I, E); |
| 2396 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2397 | void EnqueueVisitor::VisitAddrLabelExpr(const AddrLabelExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2398 | WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent)); |
| 2399 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2400 | void EnqueueVisitor::VisitBlockExpr(const BlockExpr *B) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2401 | AddDecl(B->getBlockDecl()); |
| 2402 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2403 | void EnqueueVisitor::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2404 | EnqueueChildren(E); |
| 2405 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2406 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2407 | void EnqueueVisitor::VisitCompoundStmt(const CompoundStmt *S) { |
Pete Cooper | 57d3f14 | 2015-07-30 17:22:52 +0000 | [diff] [blame] | 2408 | for (auto &I : llvm::reverse(S->body())) |
| 2409 | AddStmt(I); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2410 | } |
| 2411 | void EnqueueVisitor:: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2412 | VisitMSDependentExistsStmt(const MSDependentExistsStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2413 | AddStmt(S->getSubStmt()); |
| 2414 | AddDeclarationNameInfo(S); |
| 2415 | if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc()) |
| 2416 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2417 | } |
| 2418 | |
| 2419 | void EnqueueVisitor:: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2420 | VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2421 | if (E->hasExplicitTemplateArgs()) |
| 2422 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2423 | AddDeclarationNameInfo(E); |
| 2424 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 2425 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2426 | if (!E->isImplicitAccess()) |
| 2427 | AddStmt(E->getBase()); |
| 2428 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2429 | void EnqueueVisitor::VisitCXXNewExpr(const CXXNewExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2430 | // Enqueue the initializer , if any. |
| 2431 | AddStmt(E->getInitializer()); |
| 2432 | // Enqueue the array size, if any. |
| 2433 | AddStmt(E->getArraySize()); |
| 2434 | // Enqueue the allocated type. |
| 2435 | AddTypeLoc(E->getAllocatedTypeSourceInfo()); |
| 2436 | // Enqueue the placement arguments. |
| 2437 | for (unsigned I = E->getNumPlacementArgs(); I > 0; --I) |
| 2438 | AddStmt(E->getPlacementArg(I-1)); |
| 2439 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2440 | void EnqueueVisitor::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CE) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2441 | for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I) |
| 2442 | AddStmt(CE->getArg(I-1)); |
| 2443 | AddStmt(CE->getCallee()); |
| 2444 | AddStmt(CE->getArg(0)); |
| 2445 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2446 | void EnqueueVisitor::VisitCXXPseudoDestructorExpr( |
| 2447 | const CXXPseudoDestructorExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2448 | // Visit the name of the type being destroyed. |
| 2449 | AddTypeLoc(E->getDestroyedTypeInfo()); |
| 2450 | // Visit the scope type that looks disturbingly like the nested-name-specifier |
| 2451 | // but isn't. |
| 2452 | AddTypeLoc(E->getScopeTypeInfo()); |
| 2453 | // Visit the nested-name-specifier. |
| 2454 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 2455 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2456 | // Visit base expression. |
| 2457 | AddStmt(E->getBase()); |
| 2458 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2459 | void EnqueueVisitor::VisitCXXScalarValueInitExpr( |
| 2460 | const CXXScalarValueInitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2461 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2462 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2463 | void EnqueueVisitor::VisitCXXTemporaryObjectExpr( |
| 2464 | const CXXTemporaryObjectExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2465 | EnqueueChildren(E); |
| 2466 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2467 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2468 | void EnqueueVisitor::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2469 | EnqueueChildren(E); |
| 2470 | if (E->isTypeOperand()) |
| 2471 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 2472 | } |
| 2473 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2474 | void EnqueueVisitor::VisitCXXUnresolvedConstructExpr( |
| 2475 | const CXXUnresolvedConstructExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2476 | EnqueueChildren(E); |
| 2477 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2478 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2479 | void EnqueueVisitor::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2480 | EnqueueChildren(E); |
| 2481 | if (E->isTypeOperand()) |
| 2482 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 2483 | } |
| 2484 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2485 | void EnqueueVisitor::VisitCXXCatchStmt(const CXXCatchStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2486 | EnqueueChildren(S); |
| 2487 | AddDecl(S->getExceptionDecl()); |
| 2488 | } |
| 2489 | |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2490 | void EnqueueVisitor::VisitCXXForRangeStmt(const CXXForRangeStmt *S) { |
Argyrios Kyrtzidis | cde7069 | 2014-11-13 09:50:19 +0000 | [diff] [blame] | 2491 | AddStmt(S->getBody()); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2492 | AddStmt(S->getRangeInit()); |
Argyrios Kyrtzidis | cde7069 | 2014-11-13 09:50:19 +0000 | [diff] [blame] | 2493 | AddDecl(S->getLoopVariable()); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2496 | void EnqueueVisitor::VisitDeclRefExpr(const DeclRefExpr *DR) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2497 | if (DR->hasExplicitTemplateArgs()) |
| 2498 | AddExplicitTemplateArgs(DR->getTemplateArgs(), DR->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2499 | WL.push_back(DeclRefExprParts(DR, Parent)); |
| 2500 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2501 | void EnqueueVisitor::VisitDependentScopeDeclRefExpr( |
| 2502 | const DependentScopeDeclRefExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2503 | if (E->hasExplicitTemplateArgs()) |
| 2504 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2505 | AddDeclarationNameInfo(E); |
| 2506 | AddNestedNameSpecifierLoc(E->getQualifierLoc()); |
| 2507 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2508 | void EnqueueVisitor::VisitDeclStmt(const DeclStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2509 | unsigned size = WL.size(); |
| 2510 | bool isFirst = true; |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 2511 | for (const auto *D : S->decls()) { |
| 2512 | AddDecl(D, isFirst); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2513 | isFirst = false; |
| 2514 | } |
| 2515 | if (size == WL.size()) |
| 2516 | return; |
| 2517 | // Now reverse the entries we just added. This will match the DFS |
| 2518 | // ordering performed by the worklist. |
| 2519 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2520 | std::reverse(I, E); |
| 2521 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2522 | void EnqueueVisitor::VisitDesignatedInitExpr(const DesignatedInitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2523 | AddStmt(E->getInit()); |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2524 | for (const DesignatedInitExpr::Designator &D : |
| 2525 | llvm::reverse(E->designators())) { |
| 2526 | if (D.isFieldDesignator()) { |
| 2527 | if (FieldDecl *Field = D.getField()) |
| 2528 | AddMemberRef(Field, D.getFieldLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2529 | continue; |
| 2530 | } |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2531 | if (D.isArrayDesignator()) { |
| 2532 | AddStmt(E->getArrayIndex(D)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2533 | continue; |
| 2534 | } |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2535 | assert(D.isArrayRangeDesignator() && "Unknown designator kind"); |
| 2536 | AddStmt(E->getArrayRangeEnd(D)); |
| 2537 | AddStmt(E->getArrayRangeStart(D)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2538 | } |
| 2539 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2540 | void EnqueueVisitor::VisitExplicitCastExpr(const ExplicitCastExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2541 | EnqueueChildren(E); |
| 2542 | AddTypeLoc(E->getTypeInfoAsWritten()); |
| 2543 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2544 | void EnqueueVisitor::VisitForStmt(const ForStmt *FS) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2545 | AddStmt(FS->getBody()); |
| 2546 | AddStmt(FS->getInc()); |
| 2547 | AddStmt(FS->getCond()); |
| 2548 | AddDecl(FS->getConditionVariable()); |
| 2549 | AddStmt(FS->getInit()); |
| 2550 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2551 | void EnqueueVisitor::VisitGotoStmt(const GotoStmt *GS) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2552 | WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent)); |
| 2553 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2554 | void EnqueueVisitor::VisitIfStmt(const IfStmt *If) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2555 | AddStmt(If->getElse()); |
| 2556 | AddStmt(If->getThen()); |
| 2557 | AddStmt(If->getCond()); |
| 2558 | AddDecl(If->getConditionVariable()); |
| 2559 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2560 | void EnqueueVisitor::VisitInitListExpr(const InitListExpr *IE) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2561 | // We care about the syntactic form of the initializer list, only. |
| 2562 | if (InitListExpr *Syntactic = IE->getSyntacticForm()) |
| 2563 | IE = Syntactic; |
| 2564 | EnqueueChildren(IE); |
| 2565 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2566 | void EnqueueVisitor::VisitMemberExpr(const MemberExpr *M) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2567 | WL.push_back(MemberExprParts(M, Parent)); |
| 2568 | |
| 2569 | // If the base of the member access expression is an implicit 'this', don't |
| 2570 | // visit it. |
| 2571 | // FIXME: If we ever want to show these implicit accesses, this will be |
| 2572 | // unfortunate. However, clang_getCursor() relies on this behavior. |
Argyrios Kyrtzidis | 58d0e7a | 2015-03-13 04:40:07 +0000 | [diff] [blame] | 2573 | if (M->isImplicitAccess()) |
| 2574 | return; |
| 2575 | |
| 2576 | // Ignore base anonymous struct/union fields, otherwise they will shadow the |
| 2577 | // real field that that we are interested in. |
| 2578 | if (auto *SubME = dyn_cast<MemberExpr>(M->getBase())) { |
| 2579 | if (auto *FD = dyn_cast_or_null<FieldDecl>(SubME->getMemberDecl())) { |
| 2580 | if (FD->isAnonymousStructOrUnion()) { |
| 2581 | AddStmt(SubME->getBase()); |
| 2582 | return; |
| 2583 | } |
| 2584 | } |
| 2585 | } |
| 2586 | |
| 2587 | AddStmt(M->getBase()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2588 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2589 | void EnqueueVisitor::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2590 | AddTypeLoc(E->getEncodedTypeSourceInfo()); |
| 2591 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2592 | void EnqueueVisitor::VisitObjCMessageExpr(const ObjCMessageExpr *M) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2593 | EnqueueChildren(M); |
| 2594 | AddTypeLoc(M->getClassReceiverTypeInfo()); |
| 2595 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2596 | void EnqueueVisitor::VisitOffsetOfExpr(const OffsetOfExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2597 | // Visit the components of the offsetof expression. |
| 2598 | for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2599 | const OffsetOfNode &Node = E->getComponent(I-1); |
| 2600 | switch (Node.getKind()) { |
| 2601 | case OffsetOfNode::Array: |
| 2602 | AddStmt(E->getIndexExpr(Node.getArrayExprIndex())); |
| 2603 | break; |
| 2604 | case OffsetOfNode::Field: |
| 2605 | AddMemberRef(Node.getField(), Node.getSourceRange().getEnd()); |
| 2606 | break; |
| 2607 | case OffsetOfNode::Identifier: |
| 2608 | case OffsetOfNode::Base: |
| 2609 | continue; |
| 2610 | } |
| 2611 | } |
| 2612 | // Visit the type into which we're computing the offset. |
| 2613 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2614 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2615 | void EnqueueVisitor::VisitOverloadExpr(const OverloadExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2616 | if (E->hasExplicitTemplateArgs()) |
| 2617 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2618 | WL.push_back(OverloadExprParts(E, Parent)); |
| 2619 | } |
| 2620 | void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr( |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2621 | const UnaryExprOrTypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2622 | EnqueueChildren(E); |
| 2623 | if (E->isArgumentType()) |
| 2624 | AddTypeLoc(E->getArgumentTypeInfo()); |
| 2625 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2626 | void EnqueueVisitor::VisitStmt(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2627 | EnqueueChildren(S); |
| 2628 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2629 | void EnqueueVisitor::VisitSwitchStmt(const SwitchStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2630 | AddStmt(S->getBody()); |
| 2631 | AddStmt(S->getCond()); |
| 2632 | AddDecl(S->getConditionVariable()); |
| 2633 | } |
| 2634 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2635 | void EnqueueVisitor::VisitWhileStmt(const WhileStmt *W) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2636 | AddStmt(W->getBody()); |
| 2637 | AddStmt(W->getCond()); |
| 2638 | AddDecl(W->getConditionVariable()); |
| 2639 | } |
| 2640 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2641 | void EnqueueVisitor::VisitTypeTraitExpr(const TypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2642 | for (unsigned I = E->getNumArgs(); I > 0; --I) |
| 2643 | AddTypeLoc(E->getArg(I-1)); |
| 2644 | } |
| 2645 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2646 | void EnqueueVisitor::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2647 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2648 | } |
| 2649 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2650 | void EnqueueVisitor::VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2651 | EnqueueChildren(E); |
| 2652 | } |
| 2653 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2654 | void EnqueueVisitor::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2655 | VisitOverloadExpr(U); |
| 2656 | if (!U->isImplicitAccess()) |
| 2657 | AddStmt(U->getBase()); |
| 2658 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2659 | void EnqueueVisitor::VisitVAArgExpr(const VAArgExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2660 | AddStmt(E->getSubExpr()); |
| 2661 | AddTypeLoc(E->getWrittenTypeInfo()); |
| 2662 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2663 | void EnqueueVisitor::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2664 | WL.push_back(SizeOfPackExprParts(E, Parent)); |
| 2665 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2666 | void EnqueueVisitor::VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2667 | // If the opaque value has a source expression, just transparently |
| 2668 | // visit that. This is useful for (e.g.) pseudo-object expressions. |
| 2669 | if (Expr *SourceExpr = E->getSourceExpr()) |
| 2670 | return Visit(SourceExpr); |
| 2671 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2672 | void EnqueueVisitor::VisitLambdaExpr(const LambdaExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2673 | AddStmt(E->getBody()); |
| 2674 | WL.push_back(LambdaExprParts(E, Parent)); |
| 2675 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2676 | void EnqueueVisitor::VisitPseudoObjectExpr(const PseudoObjectExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2677 | // Treat the expression like its syntactic form. |
| 2678 | Visit(E->getSyntacticForm()); |
| 2679 | } |
| 2680 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2681 | void EnqueueVisitor::VisitOMPExecutableDirective( |
| 2682 | const OMPExecutableDirective *D) { |
| 2683 | EnqueueChildren(D); |
| 2684 | for (ArrayRef<OMPClause *>::iterator I = D->clauses().begin(), |
| 2685 | E = D->clauses().end(); |
| 2686 | I != E; ++I) |
| 2687 | EnqueueChildren(*I); |
| 2688 | } |
| 2689 | |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2690 | void EnqueueVisitor::VisitOMPLoopDirective(const OMPLoopDirective *D) { |
| 2691 | VisitOMPExecutableDirective(D); |
| 2692 | } |
| 2693 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2694 | void EnqueueVisitor::VisitOMPParallelDirective(const OMPParallelDirective *D) { |
| 2695 | VisitOMPExecutableDirective(D); |
| 2696 | } |
| 2697 | |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2698 | void EnqueueVisitor::VisitOMPSimdDirective(const OMPSimdDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2699 | VisitOMPLoopDirective(D); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2702 | void EnqueueVisitor::VisitOMPForDirective(const OMPForDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2703 | VisitOMPLoopDirective(D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2706 | void EnqueueVisitor::VisitOMPForSimdDirective(const OMPForSimdDirective *D) { |
| 2707 | VisitOMPLoopDirective(D); |
| 2708 | } |
| 2709 | |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2710 | void EnqueueVisitor::VisitOMPSectionsDirective(const OMPSectionsDirective *D) { |
| 2711 | VisitOMPExecutableDirective(D); |
| 2712 | } |
| 2713 | |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2714 | void EnqueueVisitor::VisitOMPSectionDirective(const OMPSectionDirective *D) { |
| 2715 | VisitOMPExecutableDirective(D); |
| 2716 | } |
| 2717 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 2718 | void EnqueueVisitor::VisitOMPSingleDirective(const OMPSingleDirective *D) { |
| 2719 | VisitOMPExecutableDirective(D); |
| 2720 | } |
| 2721 | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2722 | void EnqueueVisitor::VisitOMPMasterDirective(const OMPMasterDirective *D) { |
| 2723 | VisitOMPExecutableDirective(D); |
| 2724 | } |
| 2725 | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2726 | void EnqueueVisitor::VisitOMPCriticalDirective(const OMPCriticalDirective *D) { |
| 2727 | VisitOMPExecutableDirective(D); |
| 2728 | AddDeclarationNameInfo(D); |
| 2729 | } |
| 2730 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2731 | void |
| 2732 | EnqueueVisitor::VisitOMPParallelForDirective(const OMPParallelForDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2733 | VisitOMPLoopDirective(D); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2736 | void EnqueueVisitor::VisitOMPParallelForSimdDirective( |
| 2737 | const OMPParallelForSimdDirective *D) { |
| 2738 | VisitOMPLoopDirective(D); |
| 2739 | } |
| 2740 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2741 | void EnqueueVisitor::VisitOMPParallelSectionsDirective( |
| 2742 | const OMPParallelSectionsDirective *D) { |
| 2743 | VisitOMPExecutableDirective(D); |
| 2744 | } |
| 2745 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2746 | void EnqueueVisitor::VisitOMPTaskDirective(const OMPTaskDirective *D) { |
| 2747 | VisitOMPExecutableDirective(D); |
| 2748 | } |
| 2749 | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2750 | void |
| 2751 | EnqueueVisitor::VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D) { |
| 2752 | VisitOMPExecutableDirective(D); |
| 2753 | } |
| 2754 | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 2755 | void EnqueueVisitor::VisitOMPBarrierDirective(const OMPBarrierDirective *D) { |
| 2756 | VisitOMPExecutableDirective(D); |
| 2757 | } |
| 2758 | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 2759 | void EnqueueVisitor::VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D) { |
| 2760 | VisitOMPExecutableDirective(D); |
| 2761 | } |
| 2762 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2763 | void EnqueueVisitor::VisitOMPTaskgroupDirective( |
| 2764 | const OMPTaskgroupDirective *D) { |
| 2765 | VisitOMPExecutableDirective(D); |
Alexey Bataev | 3b1b895 | 2017-07-25 15:53:26 +0000 | [diff] [blame] | 2766 | if (const Expr *E = D->getReductionRef()) |
| 2767 | VisitStmt(E); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2770 | void EnqueueVisitor::VisitOMPFlushDirective(const OMPFlushDirective *D) { |
| 2771 | VisitOMPExecutableDirective(D); |
| 2772 | } |
| 2773 | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2774 | void EnqueueVisitor::VisitOMPOrderedDirective(const OMPOrderedDirective *D) { |
| 2775 | VisitOMPExecutableDirective(D); |
| 2776 | } |
| 2777 | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2778 | void EnqueueVisitor::VisitOMPAtomicDirective(const OMPAtomicDirective *D) { |
| 2779 | VisitOMPExecutableDirective(D); |
| 2780 | } |
| 2781 | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 2782 | void EnqueueVisitor::VisitOMPTargetDirective(const OMPTargetDirective *D) { |
| 2783 | VisitOMPExecutableDirective(D); |
| 2784 | } |
| 2785 | |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2786 | void EnqueueVisitor::VisitOMPTargetDataDirective(const |
| 2787 | OMPTargetDataDirective *D) { |
| 2788 | VisitOMPExecutableDirective(D); |
| 2789 | } |
| 2790 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 2791 | void EnqueueVisitor::VisitOMPTargetEnterDataDirective( |
| 2792 | const OMPTargetEnterDataDirective *D) { |
| 2793 | VisitOMPExecutableDirective(D); |
| 2794 | } |
| 2795 | |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 2796 | void EnqueueVisitor::VisitOMPTargetExitDataDirective( |
| 2797 | const OMPTargetExitDataDirective *D) { |
| 2798 | VisitOMPExecutableDirective(D); |
| 2799 | } |
| 2800 | |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 2801 | void EnqueueVisitor::VisitOMPTargetParallelDirective( |
| 2802 | const OMPTargetParallelDirective *D) { |
| 2803 | VisitOMPExecutableDirective(D); |
| 2804 | } |
| 2805 | |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 2806 | void EnqueueVisitor::VisitOMPTargetParallelForDirective( |
| 2807 | const OMPTargetParallelForDirective *D) { |
| 2808 | VisitOMPLoopDirective(D); |
| 2809 | } |
| 2810 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2811 | void EnqueueVisitor::VisitOMPTeamsDirective(const OMPTeamsDirective *D) { |
| 2812 | VisitOMPExecutableDirective(D); |
| 2813 | } |
| 2814 | |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2815 | void EnqueueVisitor::VisitOMPCancellationPointDirective( |
| 2816 | const OMPCancellationPointDirective *D) { |
| 2817 | VisitOMPExecutableDirective(D); |
| 2818 | } |
| 2819 | |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2820 | void EnqueueVisitor::VisitOMPCancelDirective(const OMPCancelDirective *D) { |
| 2821 | VisitOMPExecutableDirective(D); |
| 2822 | } |
| 2823 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 2824 | void EnqueueVisitor::VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D) { |
| 2825 | VisitOMPLoopDirective(D); |
| 2826 | } |
| 2827 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 2828 | void EnqueueVisitor::VisitOMPTaskLoopSimdDirective( |
| 2829 | const OMPTaskLoopSimdDirective *D) { |
| 2830 | VisitOMPLoopDirective(D); |
| 2831 | } |
| 2832 | |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2833 | void EnqueueVisitor::VisitOMPDistributeDirective( |
| 2834 | const OMPDistributeDirective *D) { |
| 2835 | VisitOMPLoopDirective(D); |
| 2836 | } |
| 2837 | |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2838 | void EnqueueVisitor::VisitOMPDistributeParallelForDirective( |
| 2839 | const OMPDistributeParallelForDirective *D) { |
| 2840 | VisitOMPLoopDirective(D); |
| 2841 | } |
| 2842 | |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2843 | void EnqueueVisitor::VisitOMPDistributeParallelForSimdDirective( |
| 2844 | const OMPDistributeParallelForSimdDirective *D) { |
| 2845 | VisitOMPLoopDirective(D); |
| 2846 | } |
| 2847 | |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2848 | void EnqueueVisitor::VisitOMPDistributeSimdDirective( |
| 2849 | const OMPDistributeSimdDirective *D) { |
| 2850 | VisitOMPLoopDirective(D); |
| 2851 | } |
| 2852 | |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 2853 | void EnqueueVisitor::VisitOMPTargetParallelForSimdDirective( |
| 2854 | const OMPTargetParallelForSimdDirective *D) { |
| 2855 | VisitOMPLoopDirective(D); |
| 2856 | } |
| 2857 | |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2858 | void EnqueueVisitor::VisitOMPTargetSimdDirective( |
| 2859 | const OMPTargetSimdDirective *D) { |
| 2860 | VisitOMPLoopDirective(D); |
| 2861 | } |
| 2862 | |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 2863 | void EnqueueVisitor::VisitOMPTeamsDistributeDirective( |
| 2864 | const OMPTeamsDistributeDirective *D) { |
| 2865 | VisitOMPLoopDirective(D); |
| 2866 | } |
| 2867 | |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 2868 | void EnqueueVisitor::VisitOMPTeamsDistributeSimdDirective( |
| 2869 | const OMPTeamsDistributeSimdDirective *D) { |
| 2870 | VisitOMPLoopDirective(D); |
| 2871 | } |
| 2872 | |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 2873 | void EnqueueVisitor::VisitOMPTeamsDistributeParallelForSimdDirective( |
| 2874 | const OMPTeamsDistributeParallelForSimdDirective *D) { |
| 2875 | VisitOMPLoopDirective(D); |
| 2876 | } |
| 2877 | |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 2878 | void EnqueueVisitor::VisitOMPTeamsDistributeParallelForDirective( |
| 2879 | const OMPTeamsDistributeParallelForDirective *D) { |
| 2880 | VisitOMPLoopDirective(D); |
| 2881 | } |
| 2882 | |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 2883 | void EnqueueVisitor::VisitOMPTargetTeamsDirective( |
| 2884 | const OMPTargetTeamsDirective *D) { |
| 2885 | VisitOMPExecutableDirective(D); |
| 2886 | } |
| 2887 | |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 2888 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeDirective( |
| 2889 | const OMPTargetTeamsDistributeDirective *D) { |
| 2890 | VisitOMPLoopDirective(D); |
| 2891 | } |
| 2892 | |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 2893 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeParallelForDirective( |
| 2894 | const OMPTargetTeamsDistributeParallelForDirective *D) { |
| 2895 | VisitOMPLoopDirective(D); |
| 2896 | } |
| 2897 | |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 2898 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeParallelForSimdDirective( |
| 2899 | const OMPTargetTeamsDistributeParallelForSimdDirective *D) { |
| 2900 | VisitOMPLoopDirective(D); |
| 2901 | } |
| 2902 | |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 2903 | void EnqueueVisitor::VisitOMPTargetTeamsDistributeSimdDirective( |
| 2904 | const OMPTargetTeamsDistributeSimdDirective *D) { |
| 2905 | VisitOMPLoopDirective(D); |
| 2906 | } |
| 2907 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2908 | void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2909 | EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S); |
| 2910 | } |
| 2911 | |
| 2912 | bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { |
| 2913 | if (RegionOfInterest.isValid()) { |
| 2914 | SourceRange Range = getRawCursorExtent(C); |
| 2915 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 2916 | return false; |
| 2917 | } |
| 2918 | return true; |
| 2919 | } |
| 2920 | |
| 2921 | bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { |
| 2922 | while (!WL.empty()) { |
| 2923 | // Dequeue the worklist item. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2924 | VisitorJob LI = WL.pop_back_val(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2925 | |
| 2926 | // Set the Parent field, then back to its old value once we're done. |
| 2927 | SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); |
| 2928 | |
| 2929 | switch (LI.getKind()) { |
| 2930 | case VisitorJob::DeclVisitKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2931 | const Decl *D = cast<DeclVisit>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2932 | if (!D) |
| 2933 | continue; |
| 2934 | |
| 2935 | // For now, perform default visitation for Decls. |
| 2936 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest, |
| 2937 | cast<DeclVisit>(&LI)->isFirst()))) |
| 2938 | return true; |
| 2939 | |
| 2940 | continue; |
| 2941 | } |
| 2942 | case VisitorJob::ExplicitTemplateArgsVisitKind: { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2943 | for (const TemplateArgumentLoc &Arg : |
| 2944 | *cast<ExplicitTemplateArgsVisit>(&LI)) { |
| 2945 | if (VisitTemplateArgumentLoc(Arg)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2946 | return true; |
| 2947 | } |
| 2948 | continue; |
| 2949 | } |
| 2950 | case VisitorJob::TypeLocVisitKind: { |
| 2951 | // Perform default visitation for TypeLocs. |
| 2952 | if (Visit(cast<TypeLocVisit>(&LI)->get())) |
| 2953 | return true; |
| 2954 | continue; |
| 2955 | } |
| 2956 | case VisitorJob::LabelRefVisitKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2957 | const LabelDecl *LS = cast<LabelRefVisit>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2958 | if (LabelStmt *stmt = LS->getStmt()) { |
| 2959 | if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(), |
| 2960 | TU))) { |
| 2961 | return true; |
| 2962 | } |
| 2963 | } |
| 2964 | continue; |
| 2965 | } |
| 2966 | |
| 2967 | case VisitorJob::NestedNameSpecifierLocVisitKind: { |
| 2968 | NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI); |
| 2969 | if (VisitNestedNameSpecifierLoc(V->get())) |
| 2970 | return true; |
| 2971 | continue; |
| 2972 | } |
| 2973 | |
| 2974 | case VisitorJob::DeclarationNameInfoVisitKind: { |
| 2975 | if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI) |
| 2976 | ->get())) |
| 2977 | return true; |
| 2978 | continue; |
| 2979 | } |
| 2980 | case VisitorJob::MemberRefVisitKind: { |
| 2981 | MemberRefVisit *V = cast<MemberRefVisit>(&LI); |
| 2982 | if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU))) |
| 2983 | return true; |
| 2984 | continue; |
| 2985 | } |
| 2986 | case VisitorJob::StmtVisitKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2987 | const Stmt *S = cast<StmtVisit>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2988 | if (!S) |
| 2989 | continue; |
| 2990 | |
| 2991 | // Update the current cursor. |
| 2992 | CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest); |
| 2993 | if (!IsInRegionOfInterest(Cursor)) |
| 2994 | continue; |
| 2995 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 2996 | case CXChildVisit_Break: return true; |
| 2997 | case CXChildVisit_Continue: break; |
| 2998 | case CXChildVisit_Recurse: |
| 2999 | if (PostChildrenVisitor) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3000 | WL.push_back(PostChildrenVisit(nullptr, Cursor)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3001 | EnqueueWorkList(WL, S); |
| 3002 | break; |
| 3003 | } |
| 3004 | continue; |
| 3005 | } |
| 3006 | case VisitorJob::MemberExprPartsKind: { |
| 3007 | // Handle the other pieces in the MemberExpr besides the base. |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3008 | const MemberExpr *M = cast<MemberExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3009 | |
| 3010 | // Visit the nested-name-specifier |
| 3011 | if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc()) |
| 3012 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 3013 | return true; |
| 3014 | |
| 3015 | // Visit the declaration name. |
| 3016 | if (VisitDeclarationNameInfo(M->getMemberNameInfo())) |
| 3017 | return true; |
| 3018 | |
| 3019 | // Visit the explicitly-specified template arguments, if any. |
| 3020 | if (M->hasExplicitTemplateArgs()) { |
| 3021 | for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), |
| 3022 | *ArgEnd = Arg + M->getNumTemplateArgs(); |
| 3023 | Arg != ArgEnd; ++Arg) { |
| 3024 | if (VisitTemplateArgumentLoc(*Arg)) |
| 3025 | return true; |
| 3026 | } |
| 3027 | } |
| 3028 | continue; |
| 3029 | } |
| 3030 | case VisitorJob::DeclRefExprPartsKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3031 | const DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3032 | // Visit nested-name-specifier, if present. |
| 3033 | if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc()) |
| 3034 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 3035 | return true; |
| 3036 | // Visit declaration name. |
| 3037 | if (VisitDeclarationNameInfo(DR->getNameInfo())) |
| 3038 | return true; |
| 3039 | continue; |
| 3040 | } |
| 3041 | case VisitorJob::OverloadExprPartsKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3042 | const OverloadExpr *O = cast<OverloadExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3043 | // Visit the nested-name-specifier. |
| 3044 | if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc()) |
| 3045 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 3046 | return true; |
| 3047 | // Visit the declaration name. |
| 3048 | if (VisitDeclarationNameInfo(O->getNameInfo())) |
| 3049 | return true; |
| 3050 | // Visit the overloaded declaration reference. |
| 3051 | if (Visit(MakeCursorOverloadedDeclRef(O, TU))) |
| 3052 | return true; |
| 3053 | continue; |
| 3054 | } |
| 3055 | case VisitorJob::SizeOfPackExprPartsKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3056 | const SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3057 | NamedDecl *Pack = E->getPack(); |
| 3058 | if (isa<TemplateTypeParmDecl>(Pack)) { |
| 3059 | if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack), |
| 3060 | E->getPackLoc(), TU))) |
| 3061 | return true; |
| 3062 | |
| 3063 | continue; |
| 3064 | } |
| 3065 | |
| 3066 | if (isa<TemplateTemplateParmDecl>(Pack)) { |
| 3067 | if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack), |
| 3068 | E->getPackLoc(), TU))) |
| 3069 | return true; |
| 3070 | |
| 3071 | continue; |
| 3072 | } |
| 3073 | |
| 3074 | // Non-type template parameter packs and function parameter packs are |
| 3075 | // treated like DeclRefExpr cursors. |
| 3076 | continue; |
| 3077 | } |
| 3078 | |
| 3079 | case VisitorJob::LambdaExprPartsKind: { |
| 3080 | // Visit captures. |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3081 | const LambdaExpr *E = cast<LambdaExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3082 | for (LambdaExpr::capture_iterator C = E->explicit_capture_begin(), |
| 3083 | CEnd = E->explicit_capture_end(); |
| 3084 | C != CEnd; ++C) { |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 3085 | // FIXME: Lambda init-captures. |
| 3086 | if (!C->capturesVariable()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3087 | continue; |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 3088 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3089 | if (Visit(MakeCursorVariableRef(C->getCapturedVar(), |
| 3090 | C->getLocation(), |
| 3091 | TU))) |
| 3092 | return true; |
| 3093 | } |
| 3094 | |
| 3095 | // Visit parameters and return type, if present. |
| 3096 | if (E->hasExplicitParameters() || E->hasExplicitResultType()) { |
| 3097 | TypeLoc TL = E->getCallOperator()->getTypeSourceInfo()->getTypeLoc(); |
| 3098 | if (E->hasExplicitParameters() && E->hasExplicitResultType()) { |
| 3099 | // Visit the whole type. |
| 3100 | if (Visit(TL)) |
| 3101 | return true; |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3102 | } else if (FunctionProtoTypeLoc Proto = |
| 3103 | TL.getAs<FunctionProtoTypeLoc>()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3104 | if (E->hasExplicitParameters()) { |
| 3105 | // Visit parameters. |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3106 | for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) |
| 3107 | if (Visit(MakeCXCursor(Proto.getParam(I), TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3108 | return true; |
| 3109 | } else { |
| 3110 | // Visit result type. |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 3111 | if (Visit(Proto.getReturnLoc())) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3112 | return true; |
| 3113 | } |
| 3114 | } |
| 3115 | } |
| 3116 | break; |
| 3117 | } |
| 3118 | |
| 3119 | case VisitorJob::PostChildrenVisitKind: |
| 3120 | if (PostChildrenVisitor(Parent, ClientData)) |
| 3121 | return true; |
| 3122 | break; |
| 3123 | } |
| 3124 | } |
| 3125 | return false; |
| 3126 | } |
| 3127 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3128 | bool CursorVisitor::Visit(const Stmt *S) { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3129 | VisitorWorkList *WL = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3130 | if (!WorkListFreeList.empty()) { |
| 3131 | WL = WorkListFreeList.back(); |
| 3132 | WL->clear(); |
| 3133 | WorkListFreeList.pop_back(); |
| 3134 | } |
| 3135 | else { |
| 3136 | WL = new VisitorWorkList(); |
| 3137 | WorkListCache.push_back(WL); |
| 3138 | } |
| 3139 | EnqueueWorkList(*WL, S); |
| 3140 | bool result = RunVisitorWorkList(*WL); |
| 3141 | WorkListFreeList.push_back(WL); |
| 3142 | return result; |
| 3143 | } |
| 3144 | |
| 3145 | namespace { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3146 | typedef SmallVector<SourceRange, 4> RefNamePieces; |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3147 | RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr, |
| 3148 | const DeclarationNameInfo &NI, SourceRange QLoc, |
| 3149 | const SourceRange *TemplateArgsLoc = nullptr) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3150 | const bool WantQualifier = NameFlags & CXNameRange_WantQualifier; |
| 3151 | const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs; |
| 3152 | const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece; |
| 3153 | |
| 3154 | const DeclarationName::NameKind Kind = NI.getName().getNameKind(); |
| 3155 | |
| 3156 | RefNamePieces Pieces; |
| 3157 | |
| 3158 | if (WantQualifier && QLoc.isValid()) |
| 3159 | Pieces.push_back(QLoc); |
| 3160 | |
| 3161 | if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr) |
| 3162 | Pieces.push_back(NI.getLoc()); |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3163 | |
| 3164 | if (WantTemplateArgs && TemplateArgsLoc && TemplateArgsLoc->isValid()) |
| 3165 | Pieces.push_back(*TemplateArgsLoc); |
| 3166 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3167 | if (Kind == DeclarationName::CXXOperatorName) { |
| 3168 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 3169 | NI.getInfo().CXXOperatorName.BeginOpNameLoc)); |
| 3170 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 3171 | NI.getInfo().CXXOperatorName.EndOpNameLoc)); |
| 3172 | } |
| 3173 | |
| 3174 | if (WantSinglePiece) { |
| 3175 | SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd()); |
| 3176 | Pieces.clear(); |
| 3177 | Pieces.push_back(R); |
| 3178 | } |
| 3179 | |
| 3180 | return Pieces; |
| 3181 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3182 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3183 | |
| 3184 | //===----------------------------------------------------------------------===// |
| 3185 | // Misc. API hooks. |
| 3186 | //===----------------------------------------------------------------------===// |
| 3187 | |
Chad Rosier | 05c71aa | 2013-03-27 18:28:23 +0000 | [diff] [blame] | 3188 | static void fatal_error_handler(void *user_data, const std::string& reason, |
| 3189 | bool gen_crash_diag) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3190 | // Write the result out to stderr avoiding errs() because raw_ostreams can |
| 3191 | // call report_fatal_error. |
| 3192 | fprintf(stderr, "LIBCLANG FATAL ERROR: %s\n", reason.c_str()); |
| 3193 | ::abort(); |
| 3194 | } |
| 3195 | |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 3196 | namespace { |
| 3197 | struct RegisterFatalErrorHandler { |
| 3198 | RegisterFatalErrorHandler() { |
| 3199 | llvm::install_fatal_error_handler(fatal_error_handler, nullptr); |
| 3200 | } |
| 3201 | }; |
| 3202 | } |
| 3203 | |
| 3204 | static llvm::ManagedStatic<RegisterFatalErrorHandler> RegisterFatalErrorHandlerOnce; |
| 3205 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3206 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 3207 | int displayDiagnostics) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3208 | // We use crash recovery to make some of our APIs more reliable, implicitly |
| 3209 | // enable it. |
Argyrios Kyrtzidis | 3701f54 | 2013-11-27 08:58:09 +0000 | [diff] [blame] | 3210 | if (!getenv("LIBCLANG_DISABLE_CRASH_RECOVERY")) |
| 3211 | llvm::CrashRecoveryContext::Enable(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3212 | |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 3213 | // Look through the managed static to trigger construction of the managed |
| 3214 | // static which registers our fatal error handler. This ensures it is only |
| 3215 | // registered once. |
| 3216 | (void)*RegisterFatalErrorHandlerOnce; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3217 | |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 3218 | // Initialize targets for clang module support. |
| 3219 | llvm::InitializeAllTargets(); |
| 3220 | llvm::InitializeAllTargetMCs(); |
| 3221 | llvm::InitializeAllAsmPrinters(); |
| 3222 | llvm::InitializeAllAsmParsers(); |
| 3223 | |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 3224 | CIndexer *CIdxr = new CIndexer(); |
| 3225 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3226 | if (excludeDeclarationsFromPCH) |
| 3227 | CIdxr->setOnlyLocalDecls(); |
| 3228 | if (displayDiagnostics) |
| 3229 | CIdxr->setDisplayDiagnostics(); |
| 3230 | |
| 3231 | if (getenv("LIBCLANG_BGPRIO_INDEX")) |
| 3232 | CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() | |
| 3233 | CXGlobalOpt_ThreadBackgroundPriorityForIndexing); |
| 3234 | if (getenv("LIBCLANG_BGPRIO_EDIT")) |
| 3235 | CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() | |
| 3236 | CXGlobalOpt_ThreadBackgroundPriorityForEditing); |
| 3237 | |
| 3238 | return CIdxr; |
| 3239 | } |
| 3240 | |
| 3241 | void clang_disposeIndex(CXIndex CIdx) { |
| 3242 | if (CIdx) |
| 3243 | delete static_cast<CIndexer *>(CIdx); |
| 3244 | } |
| 3245 | |
| 3246 | void clang_CXIndex_setGlobalOptions(CXIndex CIdx, unsigned options) { |
| 3247 | if (CIdx) |
| 3248 | static_cast<CIndexer *>(CIdx)->setCXGlobalOptFlags(options); |
| 3249 | } |
| 3250 | |
| 3251 | unsigned clang_CXIndex_getGlobalOptions(CXIndex CIdx) { |
| 3252 | if (CIdx) |
| 3253 | return static_cast<CIndexer *>(CIdx)->getCXGlobalOptFlags(); |
| 3254 | return 0; |
| 3255 | } |
| 3256 | |
| 3257 | void clang_toggleCrashRecovery(unsigned isEnabled) { |
| 3258 | if (isEnabled) |
| 3259 | llvm::CrashRecoveryContext::Enable(); |
| 3260 | else |
| 3261 | llvm::CrashRecoveryContext::Disable(); |
| 3262 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3263 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3264 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
| 3265 | const char *ast_filename) { |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3266 | CXTranslationUnit TU; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3267 | enum CXErrorCode Result = |
| 3268 | clang_createTranslationUnit2(CIdx, ast_filename, &TU); |
Reid Kleckner | fd48fc6 | 2014-02-12 23:56:20 +0000 | [diff] [blame] | 3269 | (void)Result; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3270 | assert((TU && Result == CXError_Success) || |
| 3271 | (!TU && Result != CXError_Success)); |
| 3272 | return TU; |
| 3273 | } |
| 3274 | |
| 3275 | enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx, |
| 3276 | const char *ast_filename, |
| 3277 | CXTranslationUnit *out_TU) { |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3278 | if (out_TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3279 | *out_TU = nullptr; |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3280 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3281 | if (!CIdx || !ast_filename || !out_TU) |
| 3282 | return CXError_InvalidArguments; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3283 | |
Argyrios Kyrtzidis | 2702101 | 2013-05-24 22:24:07 +0000 | [diff] [blame] | 3284 | LOG_FUNC_SECTION { |
| 3285 | *Log << ast_filename; |
| 3286 | } |
| 3287 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3288 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 3289 | FileSystemOptions FileSystemOpts; |
| 3290 | |
Justin Bogner | d512c1e | 2014-10-15 00:33:06 +0000 | [diff] [blame] | 3291 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags = |
| 3292 | CompilerInstance::createDiagnostics(new DiagnosticOptions()); |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 3293 | std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile( |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 3294 | ast_filename, CXXIdx->getPCHContainerOperations()->getRawReader(), |
| 3295 | ASTUnit::LoadEverything, Diags, |
Adrian Prantl | 6b21ab2 | 2015-08-27 19:46:20 +0000 | [diff] [blame] | 3296 | FileSystemOpts, /*UseDebugInfo=*/false, |
| 3297 | CXXIdx->getOnlyLocalDecls(), None, |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 3298 | /*CaptureDiagnostics=*/true, |
| 3299 | /*AllowPCHWithCompilerErrors=*/true, |
| 3300 | /*UserFilesAreVolatile=*/true); |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 3301 | *out_TU = MakeCXTranslationUnit(CXXIdx, std::move(AU)); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3302 | return *out_TU ? CXError_Success : CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3303 | } |
| 3304 | |
| 3305 | unsigned clang_defaultEditingTranslationUnitOptions() { |
| 3306 | return CXTranslationUnit_PrecompiledPreamble | |
| 3307 | CXTranslationUnit_CacheCompletionResults; |
| 3308 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3309 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3310 | CXTranslationUnit |
| 3311 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 3312 | const char *source_filename, |
| 3313 | int num_command_line_args, |
| 3314 | const char * const *command_line_args, |
| 3315 | unsigned num_unsaved_files, |
| 3316 | struct CXUnsavedFile *unsaved_files) { |
| 3317 | unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord; |
| 3318 | return clang_parseTranslationUnit(CIdx, source_filename, |
| 3319 | command_line_args, num_command_line_args, |
| 3320 | unsaved_files, num_unsaved_files, |
| 3321 | Options); |
| 3322 | } |
| 3323 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3324 | static CXErrorCode |
| 3325 | clang_parseTranslationUnit_Impl(CXIndex CIdx, const char *source_filename, |
| 3326 | const char *const *command_line_args, |
| 3327 | int num_command_line_args, |
| 3328 | ArrayRef<CXUnsavedFile> unsaved_files, |
| 3329 | unsigned options, CXTranslationUnit *out_TU) { |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3330 | // Set up the initial return values. |
| 3331 | if (out_TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3332 | *out_TU = nullptr; |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3333 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3334 | // Check arguments. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3335 | if (!CIdx || !out_TU) |
| 3336 | return CXError_InvalidArguments; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3337 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3338 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 3339 | |
| 3340 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
| 3341 | setThreadBackgroundPriority(); |
| 3342 | |
| 3343 | bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3344 | bool CreatePreambleOnFirstParse = |
| 3345 | options & CXTranslationUnit_CreatePreambleOnFirstParse; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3346 | // FIXME: Add a flag for modules. |
| 3347 | TranslationUnitKind TUKind |
Argyrios Kyrtzidis | 735e92c | 2017-06-09 01:20:48 +0000 | [diff] [blame] | 3348 | = (options & (CXTranslationUnit_Incomplete | |
| 3349 | CXTranslationUnit_SingleFileParse))? TU_Prefix : TU_Complete; |
Alp Toker | 8c8a875 | 2013-12-03 06:53:35 +0000 | [diff] [blame] | 3350 | bool CacheCodeCompletionResults |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3351 | = options & CXTranslationUnit_CacheCompletionResults; |
| 3352 | bool IncludeBriefCommentsInCodeCompletion |
| 3353 | = options & CXTranslationUnit_IncludeBriefCommentsInCodeCompletion; |
| 3354 | bool SkipFunctionBodies = options & CXTranslationUnit_SkipFunctionBodies; |
Argyrios Kyrtzidis | 735e92c | 2017-06-09 01:20:48 +0000 | [diff] [blame] | 3355 | bool SingleFileParse = options & CXTranslationUnit_SingleFileParse; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3356 | bool ForSerialization = options & CXTranslationUnit_ForSerialization; |
| 3357 | |
| 3358 | // Configure the diagnostics. |
| 3359 | IntrusiveRefCntPtr<DiagnosticsEngine> |
Sean Silva | f1b49e2 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 3360 | Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3361 | |
Manuel Klimek | 016c024 | 2016-03-01 10:56:19 +0000 | [diff] [blame] | 3362 | if (options & CXTranslationUnit_KeepGoing) |
Richard Smith | e37391c | 2017-05-03 00:28:49 +0000 | [diff] [blame] | 3363 | Diags->setSuppressAfterFatalError(false); |
Manuel Klimek | 016c024 | 2016-03-01 10:56:19 +0000 | [diff] [blame] | 3364 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3365 | // Recover resources if we crash before exiting this function. |
| 3366 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 3367 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 3368 | DiagCleanup(Diags.get()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3369 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3370 | std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles( |
| 3371 | new std::vector<ASTUnit::RemappedFile>()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3372 | |
| 3373 | // Recover resources if we crash before exiting this function. |
| 3374 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 3375 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 3376 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3377 | for (auto &UF : unsaved_files) { |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3378 | std::unique_ptr<llvm::MemoryBuffer> MB = |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3379 | llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3380 | RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3381 | } |
| 3382 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3383 | std::unique_ptr<std::vector<const char *>> Args( |
| 3384 | new std::vector<const char *>()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3385 | |
| 3386 | // Recover resources if we crash before exiting this method. |
| 3387 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > |
| 3388 | ArgsCleanup(Args.get()); |
| 3389 | |
| 3390 | // Since the Clang C library is primarily used by batch tools dealing with |
| 3391 | // (often very broken) source code, where spell-checking can have a |
| 3392 | // significant negative impact on performance (particularly when |
| 3393 | // precompiled headers are involved), we disable it by default. |
| 3394 | // Only do this if we haven't found a spell-checking-related argument. |
| 3395 | bool FoundSpellCheckingArgument = false; |
| 3396 | for (int I = 0; I != num_command_line_args; ++I) { |
| 3397 | if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || |
| 3398 | strcmp(command_line_args[I], "-fspell-checking") == 0) { |
| 3399 | FoundSpellCheckingArgument = true; |
| 3400 | break; |
| 3401 | } |
| 3402 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3403 | Args->insert(Args->end(), command_line_args, |
| 3404 | command_line_args + num_command_line_args); |
| 3405 | |
Benjamin Kramer | c02670e | 2015-11-18 16:14:27 +0000 | [diff] [blame] | 3406 | if (!FoundSpellCheckingArgument) |
| 3407 | Args->insert(Args->begin() + 1, "-fno-spell-checking"); |
| 3408 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3409 | // The 'source_filename' argument is optional. If the caller does not |
| 3410 | // specify it then it is assumed that the source file is specified |
| 3411 | // in the actual argument list. |
| 3412 | // Put the source file after command_line_args otherwise if '-x' flag is |
| 3413 | // present it will be unused. |
| 3414 | if (source_filename) |
| 3415 | Args->push_back(source_filename); |
| 3416 | |
| 3417 | // Do we need the detailed preprocessing record? |
| 3418 | if (options & CXTranslationUnit_DetailedPreprocessingRecord) { |
| 3419 | Args->push_back("-Xclang"); |
| 3420 | Args->push_back("-detailed-preprocessing-record"); |
| 3421 | } |
Alex Lorenz | cb00640 | 2017-04-27 13:47:03 +0000 | [diff] [blame] | 3422 | |
| 3423 | // Suppress any editor placeholder diagnostics. |
| 3424 | Args->push_back("-fallow-editor-placeholders"); |
| 3425 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3426 | unsigned NumErrors = Diags->getClient()->getNumErrors(); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3427 | std::unique_ptr<ASTUnit> ErrUnit; |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3428 | // Unless the user specified that they want the preamble on the first parse |
| 3429 | // set it up to be created on the first reparse. This makes the first parse |
| 3430 | // faster, trading for a slower (first) reparse. |
| 3431 | unsigned PrecompilePreambleAfterNParses = |
| 3432 | !PrecompilePreamble ? 0 : 2 - CreatePreambleOnFirstParse; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3433 | std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCommandLine( |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 3434 | Args->data(), Args->data() + Args->size(), |
| 3435 | CXXIdx->getPCHContainerOperations(), Diags, |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3436 | CXXIdx->getClangResourcesPath(), CXXIdx->getOnlyLocalDecls(), |
| 3437 | /*CaptureDiagnostics=*/true, *RemappedFiles.get(), |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3438 | /*RemappedFilesKeepOriginalName=*/true, PrecompilePreambleAfterNParses, |
| 3439 | TUKind, CacheCodeCompletionResults, IncludeBriefCommentsInCodeCompletion, |
Argyrios Kyrtzidis | 735e92c | 2017-06-09 01:20:48 +0000 | [diff] [blame] | 3440 | /*AllowPCHWithCompilerErrors=*/true, SkipFunctionBodies, SingleFileParse, |
Argyrios Kyrtzidis | a3e2ff1 | 2015-11-20 03:36:21 +0000 | [diff] [blame] | 3441 | /*UserFilesAreVolatile=*/true, ForSerialization, |
| 3442 | CXXIdx->getPCHContainerOperations()->getRawReader().getFormat(), |
| 3443 | &ErrUnit)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3444 | |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3445 | // Early failures in LoadFromCommandLine may return with ErrUnit unset. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3446 | if (!Unit && !ErrUnit) |
| 3447 | return CXError_ASTReadError; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3448 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3449 | if (NumErrors != Diags->getClient()->getNumErrors()) { |
| 3450 | // Make sure to check that 'Unit' is non-NULL. |
| 3451 | if (CXXIdx->getDisplayDiagnostics()) |
| 3452 | printDiagsToStderr(Unit ? Unit.get() : ErrUnit.get()); |
| 3453 | } |
| 3454 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3455 | if (isASTReadError(Unit ? Unit.get() : ErrUnit.get())) |
| 3456 | return CXError_ASTReadError; |
| 3457 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 3458 | *out_TU = MakeCXTranslationUnit(CXXIdx, std::move(Unit)); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3459 | return *out_TU ? CXError_Success : CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3460 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3461 | |
| 3462 | CXTranslationUnit |
| 3463 | clang_parseTranslationUnit(CXIndex CIdx, |
| 3464 | const char *source_filename, |
| 3465 | const char *const *command_line_args, |
| 3466 | int num_command_line_args, |
| 3467 | struct CXUnsavedFile *unsaved_files, |
| 3468 | unsigned num_unsaved_files, |
| 3469 | unsigned options) { |
| 3470 | CXTranslationUnit TU; |
| 3471 | enum CXErrorCode Result = clang_parseTranslationUnit2( |
| 3472 | CIdx, source_filename, command_line_args, num_command_line_args, |
| 3473 | unsaved_files, num_unsaved_files, options, &TU); |
Reid Kleckner | 6eaf05a | 2014-02-13 01:19:59 +0000 | [diff] [blame] | 3474 | (void)Result; |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3475 | assert((TU && Result == CXError_Success) || |
| 3476 | (!TU && Result != CXError_Success)); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3477 | return TU; |
| 3478 | } |
| 3479 | |
| 3480 | enum CXErrorCode clang_parseTranslationUnit2( |
Benjamin Kramer | c02670e | 2015-11-18 16:14:27 +0000 | [diff] [blame] | 3481 | CXIndex CIdx, const char *source_filename, |
| 3482 | const char *const *command_line_args, int num_command_line_args, |
| 3483 | struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files, |
| 3484 | unsigned options, CXTranslationUnit *out_TU) { |
| 3485 | SmallVector<const char *, 4> Args; |
| 3486 | Args.push_back("clang"); |
| 3487 | Args.append(command_line_args, command_line_args + num_command_line_args); |
| 3488 | return clang_parseTranslationUnit2FullArgv( |
| 3489 | CIdx, source_filename, Args.data(), Args.size(), unsaved_files, |
| 3490 | num_unsaved_files, options, out_TU); |
| 3491 | } |
| 3492 | |
| 3493 | enum CXErrorCode clang_parseTranslationUnit2FullArgv( |
| 3494 | CXIndex CIdx, const char *source_filename, |
| 3495 | const char *const *command_line_args, int num_command_line_args, |
| 3496 | struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files, |
| 3497 | unsigned options, CXTranslationUnit *out_TU) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 3498 | LOG_FUNC_SECTION { |
| 3499 | *Log << source_filename << ": "; |
| 3500 | for (int i = 0; i != num_command_line_args; ++i) |
| 3501 | *Log << command_line_args[i] << " "; |
| 3502 | } |
| 3503 | |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3504 | if (num_unsaved_files && !unsaved_files) |
| 3505 | return CXError_InvalidArguments; |
| 3506 | |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 3507 | CXErrorCode result = CXError_Failure; |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3508 | auto ParseTranslationUnitImpl = [=, &result] { |
| 3509 | result = clang_parseTranslationUnit_Impl( |
| 3510 | CIdx, source_filename, command_line_args, num_command_line_args, |
| 3511 | llvm::makeArrayRef(unsaved_files, num_unsaved_files), options, out_TU); |
| 3512 | }; |
Erik Verbruggen | 284848d | 2017-08-29 09:08:02 +0000 | [diff] [blame] | 3513 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3514 | llvm::CrashRecoveryContext CRC; |
| 3515 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3516 | if (!RunSafely(CRC, ParseTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3517 | fprintf(stderr, "libclang: crash detected during parsing: {\n"); |
| 3518 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 3519 | fprintf(stderr, " 'command_line_args' : ["); |
| 3520 | for (int i = 0; i != num_command_line_args; ++i) { |
| 3521 | if (i) |
| 3522 | fprintf(stderr, ", "); |
| 3523 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 3524 | } |
| 3525 | fprintf(stderr, "],\n"); |
| 3526 | fprintf(stderr, " 'unsaved_files' : ["); |
| 3527 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 3528 | if (i) |
| 3529 | fprintf(stderr, ", "); |
| 3530 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 3531 | unsaved_files[i].Length); |
| 3532 | } |
| 3533 | fprintf(stderr, "],\n"); |
| 3534 | fprintf(stderr, " 'options' : %d,\n", options); |
| 3535 | fprintf(stderr, "}\n"); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3536 | |
| 3537 | return CXError_Crashed; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3538 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3539 | if (CXTranslationUnit *TU = out_TU) |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3540 | PrintLibclangResourceUsage(*TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3541 | } |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 3542 | |
| 3543 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3544 | } |
| 3545 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3546 | CXString clang_Type_getObjCEncoding(CXType CT) { |
| 3547 | CXTranslationUnit tu = static_cast<CXTranslationUnit>(CT.data[1]); |
| 3548 | ASTContext &Ctx = getASTUnit(tu)->getASTContext(); |
| 3549 | std::string encoding; |
| 3550 | Ctx.getObjCEncodingForType(QualType::getFromOpaquePtr(CT.data[0]), |
| 3551 | encoding); |
| 3552 | |
| 3553 | return cxstring::createDup(encoding); |
| 3554 | } |
| 3555 | |
| 3556 | static const IdentifierInfo *getMacroIdentifier(CXCursor C) { |
| 3557 | if (C.kind == CXCursor_MacroDefinition) { |
| 3558 | if (const MacroDefinitionRecord *MDR = getCursorMacroDefinition(C)) |
| 3559 | return MDR->getName(); |
| 3560 | } else if (C.kind == CXCursor_MacroExpansion) { |
| 3561 | MacroExpansionCursor ME = getCursorMacroExpansion(C); |
| 3562 | return ME.getName(); |
| 3563 | } |
| 3564 | return nullptr; |
| 3565 | } |
| 3566 | |
| 3567 | unsigned clang_Cursor_isMacroFunctionLike(CXCursor C) { |
| 3568 | const IdentifierInfo *II = getMacroIdentifier(C); |
| 3569 | if (!II) { |
| 3570 | return false; |
| 3571 | } |
| 3572 | ASTUnit *ASTU = getCursorASTUnit(C); |
| 3573 | Preprocessor &PP = ASTU->getPreprocessor(); |
| 3574 | if (const MacroInfo *MI = PP.getMacroInfo(II)) |
| 3575 | return MI->isFunctionLike(); |
| 3576 | return false; |
| 3577 | } |
| 3578 | |
| 3579 | unsigned clang_Cursor_isMacroBuiltin(CXCursor C) { |
| 3580 | const IdentifierInfo *II = getMacroIdentifier(C); |
| 3581 | if (!II) { |
| 3582 | return false; |
| 3583 | } |
| 3584 | ASTUnit *ASTU = getCursorASTUnit(C); |
| 3585 | Preprocessor &PP = ASTU->getPreprocessor(); |
| 3586 | if (const MacroInfo *MI = PP.getMacroInfo(II)) |
| 3587 | return MI->isBuiltinMacro(); |
| 3588 | return false; |
| 3589 | } |
| 3590 | |
| 3591 | unsigned clang_Cursor_isFunctionInlined(CXCursor C) { |
| 3592 | const Decl *D = getCursorDecl(C); |
| 3593 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 3594 | if (!FD) { |
| 3595 | return false; |
| 3596 | } |
| 3597 | return FD->isInlined(); |
| 3598 | } |
| 3599 | |
| 3600 | static StringLiteral* getCFSTR_value(CallExpr *callExpr) { |
| 3601 | if (callExpr->getNumArgs() != 1) { |
| 3602 | return nullptr; |
| 3603 | } |
| 3604 | |
| 3605 | StringLiteral *S = nullptr; |
| 3606 | auto *arg = callExpr->getArg(0); |
| 3607 | if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) { |
| 3608 | ImplicitCastExpr *I = static_cast<ImplicitCastExpr *>(arg); |
| 3609 | auto *subExpr = I->getSubExprAsWritten(); |
| 3610 | |
| 3611 | if(subExpr->getStmtClass() != Stmt::StringLiteralClass){ |
| 3612 | return nullptr; |
| 3613 | } |
| 3614 | |
| 3615 | S = static_cast<StringLiteral *>(I->getSubExprAsWritten()); |
| 3616 | } else if (arg->getStmtClass() == Stmt::StringLiteralClass) { |
| 3617 | S = static_cast<StringLiteral *>(callExpr->getArg(0)); |
| 3618 | } else { |
| 3619 | return nullptr; |
| 3620 | } |
| 3621 | return S; |
| 3622 | } |
| 3623 | |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3624 | struct ExprEvalResult { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3625 | CXEvalResultKind EvalType; |
| 3626 | union { |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3627 | unsigned long long unsignedVal; |
| 3628 | long long intVal; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3629 | double floatVal; |
| 3630 | char *stringVal; |
| 3631 | } EvalData; |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3632 | bool IsUnsignedInt; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3633 | ~ExprEvalResult() { |
| 3634 | if (EvalType != CXEval_UnExposed && EvalType != CXEval_Float && |
| 3635 | EvalType != CXEval_Int) { |
| 3636 | delete EvalData.stringVal; |
| 3637 | } |
| 3638 | } |
| 3639 | }; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3640 | |
| 3641 | void clang_EvalResult_dispose(CXEvalResult E) { |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3642 | delete static_cast<ExprEvalResult *>(E); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3643 | } |
| 3644 | |
| 3645 | CXEvalResultKind clang_EvalResult_getKind(CXEvalResult E) { |
| 3646 | if (!E) { |
| 3647 | return CXEval_UnExposed; |
| 3648 | } |
| 3649 | return ((ExprEvalResult *)E)->EvalType; |
| 3650 | } |
| 3651 | |
| 3652 | int clang_EvalResult_getAsInt(CXEvalResult E) { |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3653 | return clang_EvalResult_getAsLongLong(E); |
| 3654 | } |
| 3655 | |
| 3656 | long long clang_EvalResult_getAsLongLong(CXEvalResult E) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3657 | if (!E) { |
| 3658 | return 0; |
| 3659 | } |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3660 | ExprEvalResult *Result = (ExprEvalResult*)E; |
| 3661 | if (Result->IsUnsignedInt) |
| 3662 | return Result->EvalData.unsignedVal; |
| 3663 | return Result->EvalData.intVal; |
| 3664 | } |
| 3665 | |
| 3666 | unsigned clang_EvalResult_isUnsignedInt(CXEvalResult E) { |
| 3667 | return ((ExprEvalResult *)E)->IsUnsignedInt; |
| 3668 | } |
| 3669 | |
| 3670 | unsigned long long clang_EvalResult_getAsUnsigned(CXEvalResult E) { |
| 3671 | if (!E) { |
| 3672 | return 0; |
| 3673 | } |
| 3674 | |
| 3675 | ExprEvalResult *Result = (ExprEvalResult*)E; |
| 3676 | if (Result->IsUnsignedInt) |
| 3677 | return Result->EvalData.unsignedVal; |
| 3678 | return Result->EvalData.intVal; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3679 | } |
| 3680 | |
| 3681 | double clang_EvalResult_getAsDouble(CXEvalResult E) { |
| 3682 | if (!E) { |
| 3683 | return 0; |
| 3684 | } |
| 3685 | return ((ExprEvalResult *)E)->EvalData.floatVal; |
| 3686 | } |
| 3687 | |
| 3688 | const char* clang_EvalResult_getAsStr(CXEvalResult E) { |
| 3689 | if (!E) { |
| 3690 | return nullptr; |
| 3691 | } |
| 3692 | return ((ExprEvalResult *)E)->EvalData.stringVal; |
| 3693 | } |
| 3694 | |
| 3695 | static const ExprEvalResult* evaluateExpr(Expr *expr, CXCursor C) { |
| 3696 | Expr::EvalResult ER; |
| 3697 | ASTContext &ctx = getCursorContext(C); |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3698 | if (!expr) |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3699 | return nullptr; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3700 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3701 | expr = expr->IgnoreParens(); |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3702 | if (!expr->EvaluateAsRValue(ER, ctx)) |
| 3703 | return nullptr; |
| 3704 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3705 | QualType rettype; |
| 3706 | CallExpr *callExpr; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3707 | auto result = llvm::make_unique<ExprEvalResult>(); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3708 | result->EvalType = CXEval_UnExposed; |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3709 | result->IsUnsignedInt = false; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3710 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3711 | if (ER.Val.isInt()) { |
| 3712 | result->EvalType = CXEval_Int; |
Argyrios Kyrtzidis | 5dda112 | 2016-12-01 23:41:27 +0000 | [diff] [blame] | 3713 | |
| 3714 | auto& val = ER.Val.getInt(); |
| 3715 | if (val.isUnsigned()) { |
| 3716 | result->IsUnsignedInt = true; |
| 3717 | result->EvalData.unsignedVal = val.getZExtValue(); |
| 3718 | } else { |
| 3719 | result->EvalData.intVal = val.getExtValue(); |
| 3720 | } |
| 3721 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3722 | return result.release(); |
| 3723 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3724 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3725 | if (ER.Val.isFloat()) { |
| 3726 | llvm::SmallVector<char, 100> Buffer; |
| 3727 | ER.Val.getFloat().toString(Buffer); |
| 3728 | std::string floatStr(Buffer.data(), Buffer.size()); |
| 3729 | result->EvalType = CXEval_Float; |
| 3730 | bool ignored; |
| 3731 | llvm::APFloat apFloat = ER.Val.getFloat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 3732 | apFloat.convert(llvm::APFloat::IEEEdouble(), |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3733 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 3734 | result->EvalData.floatVal = apFloat.convertToDouble(); |
| 3735 | return result.release(); |
| 3736 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3737 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3738 | if (expr->getStmtClass() == Stmt::ImplicitCastExprClass) { |
| 3739 | const ImplicitCastExpr *I = dyn_cast<ImplicitCastExpr>(expr); |
| 3740 | auto *subExpr = I->getSubExprAsWritten(); |
| 3741 | if (subExpr->getStmtClass() == Stmt::StringLiteralClass || |
| 3742 | subExpr->getStmtClass() == Stmt::ObjCStringLiteralClass) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3743 | const StringLiteral *StrE = nullptr; |
| 3744 | const ObjCStringLiteral *ObjCExpr; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3745 | ObjCExpr = dyn_cast<ObjCStringLiteral>(subExpr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3746 | |
| 3747 | if (ObjCExpr) { |
| 3748 | StrE = ObjCExpr->getString(); |
| 3749 | result->EvalType = CXEval_ObjCStrLiteral; |
| 3750 | } else { |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3751 | StrE = cast<StringLiteral>(I->getSubExprAsWritten()); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3752 | result->EvalType = CXEval_StrLiteral; |
| 3753 | } |
| 3754 | |
| 3755 | std::string strRef(StrE->getString().str()); |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3756 | result->EvalData.stringVal = new char[strRef.size() + 1]; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3757 | strncpy((char *)result->EvalData.stringVal, strRef.c_str(), |
| 3758 | strRef.size()); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3759 | result->EvalData.stringVal[strRef.size()] = '\0'; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3760 | return result.release(); |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3761 | } |
| 3762 | } else if (expr->getStmtClass() == Stmt::ObjCStringLiteralClass || |
| 3763 | expr->getStmtClass() == Stmt::StringLiteralClass) { |
| 3764 | const StringLiteral *StrE = nullptr; |
| 3765 | const ObjCStringLiteral *ObjCExpr; |
| 3766 | ObjCExpr = dyn_cast<ObjCStringLiteral>(expr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3767 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3768 | if (ObjCExpr) { |
| 3769 | StrE = ObjCExpr->getString(); |
| 3770 | result->EvalType = CXEval_ObjCStrLiteral; |
| 3771 | } else { |
| 3772 | StrE = cast<StringLiteral>(expr); |
| 3773 | result->EvalType = CXEval_StrLiteral; |
| 3774 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3775 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3776 | std::string strRef(StrE->getString().str()); |
| 3777 | result->EvalData.stringVal = new char[strRef.size() + 1]; |
| 3778 | strncpy((char *)result->EvalData.stringVal, strRef.c_str(), strRef.size()); |
| 3779 | result->EvalData.stringVal[strRef.size()] = '\0'; |
| 3780 | return result.release(); |
| 3781 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3782 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3783 | if (expr->getStmtClass() == Stmt::CStyleCastExprClass) { |
| 3784 | CStyleCastExpr *CC = static_cast<CStyleCastExpr *>(expr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3785 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3786 | rettype = CC->getType(); |
| 3787 | if (rettype.getAsString() == "CFStringRef" && |
| 3788 | CC->getSubExpr()->getStmtClass() == Stmt::CallExprClass) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3789 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3790 | callExpr = static_cast<CallExpr *>(CC->getSubExpr()); |
| 3791 | StringLiteral *S = getCFSTR_value(callExpr); |
| 3792 | if (S) { |
| 3793 | std::string strLiteral(S->getString().str()); |
| 3794 | result->EvalType = CXEval_CFStr; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3795 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3796 | result->EvalData.stringVal = new char[strLiteral.size() + 1]; |
| 3797 | strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(), |
| 3798 | strLiteral.size()); |
| 3799 | result->EvalData.stringVal[strLiteral.size()] = '\0'; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3800 | return result.release(); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3801 | } |
| 3802 | } |
| 3803 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3804 | } else if (expr->getStmtClass() == Stmt::CallExprClass) { |
| 3805 | callExpr = static_cast<CallExpr *>(expr); |
| 3806 | rettype = callExpr->getCallReturnType(ctx); |
| 3807 | |
| 3808 | if (rettype->isVectorType() || callExpr->getNumArgs() > 1) |
| 3809 | return nullptr; |
| 3810 | |
| 3811 | if (rettype->isIntegralType(ctx) || rettype->isRealFloatingType()) { |
| 3812 | if (callExpr->getNumArgs() == 1 && |
| 3813 | !callExpr->getArg(0)->getType()->isIntegralType(ctx)) |
| 3814 | return nullptr; |
| 3815 | } else if (rettype.getAsString() == "CFStringRef") { |
| 3816 | |
| 3817 | StringLiteral *S = getCFSTR_value(callExpr); |
| 3818 | if (S) { |
| 3819 | std::string strLiteral(S->getString().str()); |
| 3820 | result->EvalType = CXEval_CFStr; |
| 3821 | result->EvalData.stringVal = new char[strLiteral.size() + 1]; |
| 3822 | strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(), |
| 3823 | strLiteral.size()); |
| 3824 | result->EvalData.stringVal[strLiteral.size()] = '\0'; |
| 3825 | return result.release(); |
| 3826 | } |
| 3827 | } |
| 3828 | } else if (expr->getStmtClass() == Stmt::DeclRefExprClass) { |
| 3829 | DeclRefExpr *D = static_cast<DeclRefExpr *>(expr); |
| 3830 | ValueDecl *V = D->getDecl(); |
| 3831 | if (V->getKind() == Decl::Function) { |
| 3832 | std::string strName = V->getNameAsString(); |
| 3833 | result->EvalType = CXEval_Other; |
| 3834 | result->EvalData.stringVal = new char[strName.size() + 1]; |
| 3835 | strncpy(result->EvalData.stringVal, strName.c_str(), strName.size()); |
| 3836 | result->EvalData.stringVal[strName.size()] = '\0'; |
| 3837 | return result.release(); |
| 3838 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3839 | } |
| 3840 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3841 | return nullptr; |
| 3842 | } |
| 3843 | |
| 3844 | CXEvalResult clang_Cursor_Evaluate(CXCursor C) { |
| 3845 | const Decl *D = getCursorDecl(C); |
| 3846 | if (D) { |
| 3847 | const Expr *expr = nullptr; |
| 3848 | if (auto *Var = dyn_cast<VarDecl>(D)) { |
| 3849 | expr = Var->getInit(); |
| 3850 | } else if (auto *Field = dyn_cast<FieldDecl>(D)) { |
| 3851 | expr = Field->getInClassInitializer(); |
| 3852 | } |
| 3853 | if (expr) |
Aaron Ballman | 01dc157 | 2016-01-20 15:25:30 +0000 | [diff] [blame] | 3854 | return const_cast<CXEvalResult>(reinterpret_cast<const void *>( |
| 3855 | evaluateExpr(const_cast<Expr *>(expr), C))); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3856 | return nullptr; |
| 3857 | } |
| 3858 | |
| 3859 | const CompoundStmt *compoundStmt = dyn_cast_or_null<CompoundStmt>(getCursorStmt(C)); |
| 3860 | if (compoundStmt) { |
| 3861 | Expr *expr = nullptr; |
| 3862 | for (auto *bodyIterator : compoundStmt->body()) { |
| 3863 | if ((expr = dyn_cast<Expr>(bodyIterator))) { |
| 3864 | break; |
| 3865 | } |
| 3866 | } |
| 3867 | if (expr) |
Aaron Ballman | 01dc157 | 2016-01-20 15:25:30 +0000 | [diff] [blame] | 3868 | return const_cast<CXEvalResult>( |
| 3869 | reinterpret_cast<const void *>(evaluateExpr(expr, C))); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3870 | } |
| 3871 | return nullptr; |
| 3872 | } |
| 3873 | |
| 3874 | unsigned clang_Cursor_hasAttrs(CXCursor C) { |
| 3875 | const Decl *D = getCursorDecl(C); |
| 3876 | if (!D) { |
| 3877 | return 0; |
| 3878 | } |
| 3879 | |
| 3880 | if (D->hasAttrs()) { |
| 3881 | return 1; |
| 3882 | } |
| 3883 | |
| 3884 | return 0; |
| 3885 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3886 | unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { |
| 3887 | return CXSaveTranslationUnit_None; |
| 3888 | } |
| 3889 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3890 | static CXSaveError clang_saveTranslationUnit_Impl(CXTranslationUnit TU, |
| 3891 | const char *FileName, |
| 3892 | unsigned options) { |
| 3893 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3894 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
| 3895 | setThreadBackgroundPriority(); |
| 3896 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3897 | bool hadError = cxtu::getASTUnit(TU)->Save(FileName); |
| 3898 | return hadError ? CXSaveError_Unknown : CXSaveError_None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3899 | } |
| 3900 | |
| 3901 | int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, |
| 3902 | unsigned options) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 3903 | LOG_FUNC_SECTION { |
| 3904 | *Log << TU << ' ' << FileName; |
| 3905 | } |
| 3906 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 3907 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3908 | LOG_BAD_TU(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3909 | return CXSaveError_InvalidTU; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3910 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3911 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3912 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3913 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 3914 | if (!CXXUnit->hasSema()) |
| 3915 | return CXSaveError_InvalidTU; |
| 3916 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3917 | CXSaveError result; |
| 3918 | auto SaveTranslationUnitImpl = [=, &result]() { |
| 3919 | result = clang_saveTranslationUnit_Impl(TU, FileName, options); |
| 3920 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3921 | |
Erik Verbruggen | 3cc3911 | 2017-11-14 09:34:39 +0000 | [diff] [blame] | 3922 | if (!CXXUnit->getDiagnostics().hasUnrecoverableErrorOccurred()) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3923 | SaveTranslationUnitImpl(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3924 | |
| 3925 | if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 3926 | PrintLibclangResourceUsage(TU); |
| 3927 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3928 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
| 3931 | // We have an AST that has invalid nodes due to compiler errors. |
| 3932 | // Use a crash recovery thread for protection. |
| 3933 | |
| 3934 | llvm::CrashRecoveryContext CRC; |
| 3935 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3936 | if (!RunSafely(CRC, SaveTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3937 | fprintf(stderr, "libclang: crash detected during AST saving: {\n"); |
| 3938 | fprintf(stderr, " 'filename' : '%s'\n", FileName); |
| 3939 | fprintf(stderr, " 'options' : %d,\n", options); |
| 3940 | fprintf(stderr, "}\n"); |
| 3941 | |
| 3942 | return CXSaveError_Unknown; |
| 3943 | |
| 3944 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
| 3945 | PrintLibclangResourceUsage(TU); |
| 3946 | } |
| 3947 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3948 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3949 | } |
| 3950 | |
| 3951 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
| 3952 | if (CTUnit) { |
| 3953 | // If the translation unit has been marked as unsafe to free, just discard |
| 3954 | // it. |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3955 | ASTUnit *Unit = cxtu::getASTUnit(CTUnit); |
| 3956 | if (Unit && Unit->isUnsafeToFree()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3957 | return; |
| 3958 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3959 | delete cxtu::getASTUnit(CTUnit); |
Dmitri Gribenko | b95b3f1 | 2013-01-26 22:44:19 +0000 | [diff] [blame] | 3960 | delete CTUnit->StringPool; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3961 | delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics); |
| 3962 | disposeOverridenCXCursorsPool(CTUnit->OverridenCursorsPool); |
Dmitri Gribenko | 9e60511 | 2013-11-13 22:16:51 +0000 | [diff] [blame] | 3963 | delete CTUnit->CommentToXML; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3964 | delete CTUnit; |
| 3965 | } |
| 3966 | } |
| 3967 | |
Erik Verbruggen | 346066b | 2017-05-30 14:25:54 +0000 | [diff] [blame] | 3968 | unsigned clang_suspendTranslationUnit(CXTranslationUnit CTUnit) { |
| 3969 | if (CTUnit) { |
| 3970 | ASTUnit *Unit = cxtu::getASTUnit(CTUnit); |
| 3971 | |
| 3972 | if (Unit && Unit->isUnsafeToFree()) |
| 3973 | return false; |
| 3974 | |
| 3975 | Unit->ResetForParse(); |
| 3976 | return true; |
| 3977 | } |
| 3978 | |
| 3979 | return false; |
| 3980 | } |
| 3981 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3982 | unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { |
| 3983 | return CXReparse_None; |
| 3984 | } |
| 3985 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3986 | static CXErrorCode |
| 3987 | clang_reparseTranslationUnit_Impl(CXTranslationUnit TU, |
| 3988 | ArrayRef<CXUnsavedFile> unsaved_files, |
| 3989 | unsigned options) { |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3990 | // Check arguments. |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 3991 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3992 | LOG_BAD_TU(TU); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3993 | return CXError_InvalidArguments; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3994 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3995 | |
| 3996 | // Reset the associated diagnostics. |
| 3997 | delete static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3998 | TU->Diagnostics = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3999 | |
Dmitri Gribenko | 183436e | 2013-01-26 21:49:50 +0000 | [diff] [blame] | 4000 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4001 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) |
| 4002 | setThreadBackgroundPriority(); |
| 4003 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4004 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4005 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 4006 | |
| 4007 | std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles( |
| 4008 | new std::vector<ASTUnit::RemappedFile>()); |
| 4009 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4010 | // Recover resources if we crash before exiting this function. |
| 4011 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 4012 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 4013 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4014 | for (auto &UF : unsaved_files) { |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 4015 | std::unique_ptr<llvm::MemoryBuffer> MB = |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 4016 | llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 4017 | RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4018 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 4019 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 4020 | if (!CXXUnit->Reparse(CXXIdx->getPCHContainerOperations(), |
| 4021 | *RemappedFiles.get())) |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4022 | return CXError_Success; |
| 4023 | if (isASTReadError(CXXUnit)) |
| 4024 | return CXError_ASTReadError; |
| 4025 | return CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
| 4028 | int clang_reparseTranslationUnit(CXTranslationUnit TU, |
| 4029 | unsigned num_unsaved_files, |
| 4030 | struct CXUnsavedFile *unsaved_files, |
| 4031 | unsigned options) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 4032 | LOG_FUNC_SECTION { |
| 4033 | *Log << TU; |
| 4034 | } |
| 4035 | |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 4036 | if (num_unsaved_files && !unsaved_files) |
| 4037 | return CXError_InvalidArguments; |
| 4038 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4039 | CXErrorCode result; |
| 4040 | auto ReparseTranslationUnitImpl = [=, &result]() { |
| 4041 | result = clang_reparseTranslationUnit_Impl( |
| 4042 | TU, llvm::makeArrayRef(unsaved_files, num_unsaved_files), options); |
| 4043 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4044 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4045 | llvm::CrashRecoveryContext CRC; |
| 4046 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 4047 | if (!RunSafely(CRC, ReparseTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4048 | fprintf(stderr, "libclang: crash detected during reparsing\n"); |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4049 | cxtu::getASTUnit(TU)->setUnsafeToFree(true); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 4050 | return CXError_Crashed; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4051 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 4052 | PrintLibclangResourceUsage(TU); |
| 4053 | |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 4054 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4055 | } |
| 4056 | |
| 4057 | |
| 4058 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4059 | if (isNotUsableTU(CTUnit)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4060 | LOG_BAD_TU(CTUnit); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4061 | return cxstring::createEmpty(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4062 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4063 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4064 | ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4065 | return cxstring::createDup(CXXUnit->getOriginalSourceFileName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4066 | } |
| 4067 | |
| 4068 | CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4069 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4070 | LOG_BAD_TU(TU); |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 4071 | return clang_getNullCursor(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4072 | } |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 4073 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4074 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4075 | return MakeCXCursor(CXXUnit->getASTContext().getTranslationUnitDecl(), TU); |
| 4076 | } |
| 4077 | |
Emilio Cobos Alvarez | 485ad42 | 2017-04-28 15:56:39 +0000 | [diff] [blame] | 4078 | CXTargetInfo clang_getTranslationUnitTargetInfo(CXTranslationUnit CTUnit) { |
| 4079 | if (isNotUsableTU(CTUnit)) { |
| 4080 | LOG_BAD_TU(CTUnit); |
| 4081 | return nullptr; |
| 4082 | } |
| 4083 | |
| 4084 | CXTargetInfoImpl* impl = new CXTargetInfoImpl(); |
| 4085 | impl->TranslationUnit = CTUnit; |
| 4086 | return impl; |
| 4087 | } |
| 4088 | |
| 4089 | CXString clang_TargetInfo_getTriple(CXTargetInfo TargetInfo) { |
| 4090 | if (!TargetInfo) |
| 4091 | return cxstring::createEmpty(); |
| 4092 | |
| 4093 | CXTranslationUnit CTUnit = TargetInfo->TranslationUnit; |
| 4094 | assert(!isNotUsableTU(CTUnit) && |
| 4095 | "Unexpected unusable translation unit in TargetInfo"); |
| 4096 | |
| 4097 | ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); |
| 4098 | std::string Triple = |
| 4099 | CXXUnit->getASTContext().getTargetInfo().getTriple().normalize(); |
| 4100 | return cxstring::createDup(Triple); |
| 4101 | } |
| 4102 | |
| 4103 | int clang_TargetInfo_getPointerWidth(CXTargetInfo TargetInfo) { |
| 4104 | if (!TargetInfo) |
| 4105 | return -1; |
| 4106 | |
| 4107 | CXTranslationUnit CTUnit = TargetInfo->TranslationUnit; |
| 4108 | assert(!isNotUsableTU(CTUnit) && |
| 4109 | "Unexpected unusable translation unit in TargetInfo"); |
| 4110 | |
| 4111 | ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); |
| 4112 | return CXXUnit->getASTContext().getTargetInfo().getMaxPointerWidth(); |
| 4113 | } |
| 4114 | |
| 4115 | void clang_TargetInfo_dispose(CXTargetInfo TargetInfo) { |
| 4116 | if (!TargetInfo) |
| 4117 | return; |
| 4118 | |
| 4119 | delete TargetInfo; |
| 4120 | } |
| 4121 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4122 | //===----------------------------------------------------------------------===// |
| 4123 | // CXFile Operations. |
| 4124 | //===----------------------------------------------------------------------===// |
| 4125 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4126 | CXString clang_getFileName(CXFile SFile) { |
| 4127 | if (!SFile) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 4128 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4129 | |
| 4130 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4131 | return cxstring::createRef(FEnt->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
| 4134 | time_t clang_getFileTime(CXFile SFile) { |
| 4135 | if (!SFile) |
| 4136 | return 0; |
| 4137 | |
| 4138 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 4139 | return FEnt->getModificationTime(); |
| 4140 | } |
| 4141 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4142 | CXFile clang_getFile(CXTranslationUnit TU, const char *file_name) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4143 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4144 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 4145 | return nullptr; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4146 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4147 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4148 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4149 | |
| 4150 | FileManager &FMgr = CXXUnit->getFileManager(); |
| 4151 | return const_cast<FileEntry *>(FMgr.getFile(file_name)); |
| 4152 | } |
| 4153 | |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4154 | unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit TU, |
| 4155 | CXFile file) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4156 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4157 | LOG_BAD_TU(TU); |
| 4158 | return 0; |
| 4159 | } |
| 4160 | |
| 4161 | if (!file) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4162 | return 0; |
| 4163 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4164 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4165 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
| 4166 | return CXXUnit->getPreprocessor().getHeaderSearchInfo() |
| 4167 | .isFileMultipleIncludeGuarded(FEnt); |
| 4168 | } |
| 4169 | |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4170 | int clang_getFileUniqueID(CXFile file, CXFileUniqueID *outID) { |
| 4171 | if (!file || !outID) |
| 4172 | return 1; |
| 4173 | |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4174 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 4175 | const llvm::sys::fs::UniqueID &ID = FEnt->getUniqueID(); |
| 4176 | outID->data[0] = ID.getDevice(); |
| 4177 | outID->data[1] = ID.getFile(); |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4178 | outID->data[2] = FEnt->getModificationTime(); |
| 4179 | return 0; |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 4180 | } |
| 4181 | |
Argyrios Kyrtzidis | ac3997e | 2014-08-16 00:26:19 +0000 | [diff] [blame] | 4182 | int clang_File_isEqual(CXFile file1, CXFile file2) { |
| 4183 | if (file1 == file2) |
| 4184 | return true; |
| 4185 | |
| 4186 | if (!file1 || !file2) |
| 4187 | return false; |
| 4188 | |
| 4189 | FileEntry *FEnt1 = static_cast<FileEntry *>(file1); |
| 4190 | FileEntry *FEnt2 = static_cast<FileEntry *>(file2); |
| 4191 | return FEnt1->getUniqueID() == FEnt2->getUniqueID(); |
| 4192 | } |
| 4193 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4194 | //===----------------------------------------------------------------------===// |
| 4195 | // CXCursor Operations. |
| 4196 | //===----------------------------------------------------------------------===// |
| 4197 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4198 | static const Decl *getDeclFromExpr(const Stmt *E) { |
| 4199 | if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4200 | return getDeclFromExpr(CE->getSubExpr()); |
| 4201 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4202 | if (const DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4203 | return RefExpr->getDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4204 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4205 | return ME->getMemberDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4206 | if (const ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4207 | return RE->getDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4208 | if (const ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4209 | if (PRE->isExplicitProperty()) |
| 4210 | return PRE->getExplicitProperty(); |
| 4211 | // It could be messaging both getter and setter as in: |
| 4212 | // ++myobj.myprop; |
| 4213 | // in which case prefer to associate the setter since it is less obvious |
| 4214 | // from inspecting the source that the setter is going to get called. |
| 4215 | if (PRE->isMessagingSetter()) |
| 4216 | return PRE->getImplicitPropertySetter(); |
| 4217 | return PRE->getImplicitPropertyGetter(); |
| 4218 | } |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4219 | if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4220 | return getDeclFromExpr(POE->getSyntacticForm()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4221 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4222 | if (Expr *Src = OVE->getSourceExpr()) |
| 4223 | return getDeclFromExpr(Src); |
| 4224 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4225 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4226 | return getDeclFromExpr(CE->getCallee()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4227 | if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4228 | if (!CE->isElidable()) |
| 4229 | return CE->getConstructor(); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4230 | if (const CXXInheritedCtorInitExpr *CE = |
| 4231 | dyn_cast<CXXInheritedCtorInitExpr>(E)) |
| 4232 | return CE->getConstructor(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4233 | if (const ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4234 | return OME->getMethodDecl(); |
| 4235 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4236 | if (const ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4237 | return PE->getProtocol(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4238 | if (const SubstNonTypeTemplateParmPackExpr *NTTP |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4239 | = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E)) |
| 4240 | return NTTP->getParameterPack(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4241 | if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4242 | if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || |
| 4243 | isa<ParmVarDecl>(SizeOfPack->getPack())) |
| 4244 | return SizeOfPack->getPack(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 4245 | |
| 4246 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4247 | } |
| 4248 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4249 | static SourceLocation getLocationFromExpr(const Expr *E) { |
| 4250 | if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4251 | return getLocationFromExpr(CE->getSubExpr()); |
| 4252 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4253 | if (const ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4254 | return /*FIXME:*/Msg->getLeftLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4255 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4256 | return DRE->getLocation(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4257 | if (const MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4258 | return Member->getMemberLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4259 | if (const ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4260 | return Ivar->getLocation(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4261 | if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4262 | return SizeOfPack->getPackLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4263 | if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4264 | return PropRef->getLocation(); |
| 4265 | |
| 4266 | return E->getLocStart(); |
| 4267 | } |
| 4268 | |
NAKAMURA Takumi | a01f4c3 | 2016-12-19 16:50:43 +0000 | [diff] [blame] | 4269 | extern "C" { |
| 4270 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4271 | unsigned clang_visitChildren(CXCursor parent, |
| 4272 | CXCursorVisitor visitor, |
| 4273 | CXClientData client_data) { |
| 4274 | CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, |
| 4275 | /*VisitPreprocessorLast=*/false); |
| 4276 | return CursorVis.VisitChildren(parent); |
| 4277 | } |
| 4278 | |
| 4279 | #ifndef __has_feature |
| 4280 | #define __has_feature(x) 0 |
| 4281 | #endif |
| 4282 | #if __has_feature(blocks) |
| 4283 | typedef enum CXChildVisitResult |
| 4284 | (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent); |
| 4285 | |
| 4286 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 4287 | CXClientData client_data) { |
| 4288 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 4289 | return block(cursor, parent); |
| 4290 | } |
| 4291 | #else |
| 4292 | // If we are compiled with a compiler that doesn't have native blocks support, |
| 4293 | // define and call the block manually, so the |
| 4294 | typedef struct _CXChildVisitResult |
| 4295 | { |
| 4296 | void *isa; |
| 4297 | int flags; |
| 4298 | int reserved; |
| 4299 | enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, |
| 4300 | CXCursor); |
| 4301 | } *CXCursorVisitorBlock; |
| 4302 | |
| 4303 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 4304 | CXClientData client_data) { |
| 4305 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 4306 | return block->invoke(block, cursor, parent); |
| 4307 | } |
| 4308 | #endif |
| 4309 | |
| 4310 | |
| 4311 | unsigned clang_visitChildrenWithBlock(CXCursor parent, |
| 4312 | CXCursorVisitorBlock block) { |
| 4313 | return clang_visitChildren(parent, visitWithBlock, block); |
| 4314 | } |
| 4315 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4316 | static CXString getDeclSpelling(const Decl *D) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4317 | if (!D) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4318 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4319 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4320 | const NamedDecl *ND = dyn_cast<NamedDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4321 | if (!ND) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4322 | if (const ObjCPropertyImplDecl *PropImpl = |
| 4323 | dyn_cast<ObjCPropertyImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4324 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4325 | return cxstring::createDup(Property->getIdentifier()->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4326 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4327 | if (const ImportDecl *ImportD = dyn_cast<ImportDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4328 | if (Module *Mod = ImportD->getImportedModule()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4329 | return cxstring::createDup(Mod->getFullModuleName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4330 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4331 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4334 | if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4335 | return cxstring::createDup(OMD->getSelector().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4336 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4337 | if (const ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4338 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 4339 | // and returns different names. NamedDecl returns the class name and |
| 4340 | // ObjCCategoryImplDecl returns the category name. |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4341 | return cxstring::createRef(CIMP->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4342 | |
| 4343 | if (isa<UsingDirectiveDecl>(D)) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4344 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4345 | |
| 4346 | SmallString<1024> S; |
| 4347 | llvm::raw_svector_ostream os(S); |
| 4348 | ND->printName(os); |
| 4349 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4350 | return cxstring::createDup(os.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4351 | } |
| 4352 | |
| 4353 | CXString clang_getCursorSpelling(CXCursor C) { |
| 4354 | if (clang_isTranslationUnit(C.kind)) |
Dmitri Gribenko | 2c173b4 | 2013-01-11 19:28:44 +0000 | [diff] [blame] | 4355 | return clang_getTranslationUnitSpelling(getCursorTU(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4356 | |
| 4357 | if (clang_isReference(C.kind)) { |
| 4358 | switch (C.kind) { |
| 4359 | case CXCursor_ObjCSuperClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4360 | const ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4361 | return cxstring::createRef(Super->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4362 | } |
| 4363 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4364 | const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4365 | return cxstring::createRef(Class->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4366 | } |
| 4367 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4368 | const ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4369 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4370 | return cxstring::createRef(OID->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4371 | } |
| 4372 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4373 | const CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4374 | return cxstring::createDup(B->getType().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4375 | } |
| 4376 | case CXCursor_TypeRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4377 | const TypeDecl *Type = getCursorTypeRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4378 | assert(Type && "Missing type decl"); |
| 4379 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4380 | return cxstring::createDup(getCursorContext(C).getTypeDeclType(Type). |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4381 | getAsString()); |
| 4382 | } |
| 4383 | case CXCursor_TemplateRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4384 | const TemplateDecl *Template = getCursorTemplateRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4385 | assert(Template && "Missing template decl"); |
| 4386 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4387 | return cxstring::createDup(Template->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4388 | } |
| 4389 | |
| 4390 | case CXCursor_NamespaceRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4391 | const NamedDecl *NS = getCursorNamespaceRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4392 | assert(NS && "Missing namespace decl"); |
| 4393 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4394 | return cxstring::createDup(NS->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4395 | } |
| 4396 | |
| 4397 | case CXCursor_MemberRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4398 | const FieldDecl *Field = getCursorMemberRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4399 | assert(Field && "Missing member decl"); |
| 4400 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4401 | return cxstring::createDup(Field->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4402 | } |
| 4403 | |
| 4404 | case CXCursor_LabelRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4405 | const LabelStmt *Label = getCursorLabelRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4406 | assert(Label && "Missing label"); |
| 4407 | |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4408 | return cxstring::createRef(Label->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4409 | } |
| 4410 | |
| 4411 | case CXCursor_OverloadedDeclRef: { |
| 4412 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4413 | if (const Decl *D = Storage.dyn_cast<const Decl *>()) { |
| 4414 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4415 | return cxstring::createDup(ND->getNameAsString()); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4416 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4417 | } |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4418 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4419 | return cxstring::createDup(E->getName().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4420 | OverloadedTemplateStorage *Ovl |
| 4421 | = Storage.get<OverloadedTemplateStorage*>(); |
| 4422 | if (Ovl->size() == 0) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4423 | return cxstring::createEmpty(); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4424 | return cxstring::createDup((*Ovl->begin())->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4425 | } |
| 4426 | |
| 4427 | case CXCursor_VariableRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4428 | const VarDecl *Var = getCursorVariableRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4429 | assert(Var && "Missing variable decl"); |
| 4430 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4431 | return cxstring::createDup(Var->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4432 | } |
| 4433 | |
| 4434 | default: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4435 | return cxstring::createRef("<not implemented>"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4436 | } |
| 4437 | } |
| 4438 | |
| 4439 | if (clang_isExpression(C.kind)) { |
Argyrios Kyrtzidis | 3227d86 | 2014-03-03 19:40:52 +0000 | [diff] [blame] | 4440 | const Expr *E = getCursorExpr(C); |
| 4441 | |
| 4442 | if (C.kind == CXCursor_ObjCStringLiteral || |
| 4443 | C.kind == CXCursor_StringLiteral) { |
| 4444 | const StringLiteral *SLit; |
| 4445 | if (const ObjCStringLiteral *OSL = dyn_cast<ObjCStringLiteral>(E)) { |
| 4446 | SLit = OSL->getString(); |
| 4447 | } else { |
| 4448 | SLit = cast<StringLiteral>(E); |
| 4449 | } |
| 4450 | SmallString<256> Buf; |
| 4451 | llvm::raw_svector_ostream OS(Buf); |
| 4452 | SLit->outputString(OS); |
| 4453 | return cxstring::createDup(OS.str()); |
| 4454 | } |
| 4455 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4456 | const Decl *D = getDeclFromExpr(getCursorExpr(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4457 | if (D) |
| 4458 | return getDeclSpelling(D); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4459 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4460 | } |
| 4461 | |
| 4462 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4463 | const Stmt *S = getCursorStmt(C); |
| 4464 | if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4465 | return cxstring::createRef(Label->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4466 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4467 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4468 | } |
| 4469 | |
| 4470 | if (C.kind == CXCursor_MacroExpansion) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4471 | return cxstring::createRef(getCursorMacroExpansion(C).getName() |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4472 | ->getNameStart()); |
| 4473 | |
| 4474 | if (C.kind == CXCursor_MacroDefinition) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4475 | return cxstring::createRef(getCursorMacroDefinition(C)->getName() |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4476 | ->getNameStart()); |
| 4477 | |
| 4478 | if (C.kind == CXCursor_InclusionDirective) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4479 | return cxstring::createDup(getCursorInclusionDirective(C)->getFileName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4480 | |
| 4481 | if (clang_isDeclaration(C.kind)) |
| 4482 | return getDeclSpelling(getCursorDecl(C)); |
| 4483 | |
| 4484 | if (C.kind == CXCursor_AnnotateAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 4485 | const AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C)); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4486 | return cxstring::createDup(AA->getAnnotation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | if (C.kind == CXCursor_AsmLabelAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 4490 | const AsmLabelAttr *AA = cast<AsmLabelAttr>(cxcursor::getCursorAttr(C)); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4491 | return cxstring::createDup(AA->getLabel()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4492 | } |
| 4493 | |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 4494 | if (C.kind == CXCursor_PackedAttr) { |
| 4495 | return cxstring::createRef("packed"); |
| 4496 | } |
| 4497 | |
Saleem Abdulrasool | 79c6971 | 2015-09-05 18:53:43 +0000 | [diff] [blame] | 4498 | if (C.kind == CXCursor_VisibilityAttr) { |
| 4499 | const VisibilityAttr *AA = cast<VisibilityAttr>(cxcursor::getCursorAttr(C)); |
| 4500 | switch (AA->getVisibility()) { |
| 4501 | case VisibilityAttr::VisibilityType::Default: |
| 4502 | return cxstring::createRef("default"); |
| 4503 | case VisibilityAttr::VisibilityType::Hidden: |
| 4504 | return cxstring::createRef("hidden"); |
| 4505 | case VisibilityAttr::VisibilityType::Protected: |
| 4506 | return cxstring::createRef("protected"); |
| 4507 | } |
| 4508 | llvm_unreachable("unknown visibility type"); |
| 4509 | } |
| 4510 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4511 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | CXSourceRange clang_Cursor_getSpellingNameRange(CXCursor C, |
| 4515 | unsigned pieceIndex, |
| 4516 | unsigned options) { |
| 4517 | if (clang_Cursor_isNull(C)) |
| 4518 | return clang_getNullRange(); |
| 4519 | |
| 4520 | ASTContext &Ctx = getCursorContext(C); |
| 4521 | |
| 4522 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4523 | const Stmt *S = getCursorStmt(C); |
| 4524 | if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4525 | if (pieceIndex > 0) |
| 4526 | return clang_getNullRange(); |
| 4527 | return cxloc::translateSourceRange(Ctx, Label->getIdentLoc()); |
| 4528 | } |
| 4529 | |
| 4530 | return clang_getNullRange(); |
| 4531 | } |
| 4532 | |
| 4533 | if (C.kind == CXCursor_ObjCMessageExpr) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4534 | if (const ObjCMessageExpr * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4535 | ME = dyn_cast_or_null<ObjCMessageExpr>(getCursorExpr(C))) { |
| 4536 | if (pieceIndex >= ME->getNumSelectorLocs()) |
| 4537 | return clang_getNullRange(); |
| 4538 | return cxloc::translateSourceRange(Ctx, ME->getSelectorLoc(pieceIndex)); |
| 4539 | } |
| 4540 | } |
| 4541 | |
| 4542 | if (C.kind == CXCursor_ObjCInstanceMethodDecl || |
| 4543 | C.kind == CXCursor_ObjCClassMethodDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4544 | if (const ObjCMethodDecl * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4545 | MD = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(C))) { |
| 4546 | if (pieceIndex >= MD->getNumSelectorLocs()) |
| 4547 | return clang_getNullRange(); |
| 4548 | return cxloc::translateSourceRange(Ctx, MD->getSelectorLoc(pieceIndex)); |
| 4549 | } |
| 4550 | } |
| 4551 | |
| 4552 | if (C.kind == CXCursor_ObjCCategoryDecl || |
| 4553 | C.kind == CXCursor_ObjCCategoryImplDecl) { |
| 4554 | if (pieceIndex > 0) |
| 4555 | return clang_getNullRange(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4556 | if (const ObjCCategoryDecl * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4557 | CD = dyn_cast_or_null<ObjCCategoryDecl>(getCursorDecl(C))) |
| 4558 | return cxloc::translateSourceRange(Ctx, CD->getCategoryNameLoc()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4559 | if (const ObjCCategoryImplDecl * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4560 | CID = dyn_cast_or_null<ObjCCategoryImplDecl>(getCursorDecl(C))) |
| 4561 | return cxloc::translateSourceRange(Ctx, CID->getCategoryNameLoc()); |
| 4562 | } |
| 4563 | |
| 4564 | if (C.kind == CXCursor_ModuleImportDecl) { |
| 4565 | if (pieceIndex > 0) |
| 4566 | return clang_getNullRange(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4567 | if (const ImportDecl *ImportD = |
| 4568 | dyn_cast_or_null<ImportDecl>(getCursorDecl(C))) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4569 | ArrayRef<SourceLocation> Locs = ImportD->getIdentifierLocs(); |
| 4570 | if (!Locs.empty()) |
| 4571 | return cxloc::translateSourceRange(Ctx, |
| 4572 | SourceRange(Locs.front(), Locs.back())); |
| 4573 | } |
| 4574 | return clang_getNullRange(); |
| 4575 | } |
| 4576 | |
Argyrios Kyrtzidis | a2a1e53 | 2014-08-26 20:23:26 +0000 | [diff] [blame] | 4577 | if (C.kind == CXCursor_CXXMethod || C.kind == CXCursor_Destructor || |
Kevin Funk | 4be5d67 | 2016-12-20 09:56:56 +0000 | [diff] [blame] | 4578 | C.kind == CXCursor_ConversionFunction || |
| 4579 | C.kind == CXCursor_FunctionDecl) { |
Argyrios Kyrtzidis | a2a1e53 | 2014-08-26 20:23:26 +0000 | [diff] [blame] | 4580 | if (pieceIndex > 0) |
| 4581 | return clang_getNullRange(); |
| 4582 | if (const FunctionDecl *FD = |
| 4583 | dyn_cast_or_null<FunctionDecl>(getCursorDecl(C))) { |
| 4584 | DeclarationNameInfo FunctionName = FD->getNameInfo(); |
| 4585 | return cxloc::translateSourceRange(Ctx, FunctionName.getSourceRange()); |
| 4586 | } |
| 4587 | return clang_getNullRange(); |
| 4588 | } |
| 4589 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4590 | // FIXME: A CXCursor_InclusionDirective should give the location of the |
| 4591 | // filename, but we don't keep track of this. |
| 4592 | |
| 4593 | // FIXME: A CXCursor_AnnotateAttr should give the location of the annotation |
| 4594 | // but we don't keep track of this. |
| 4595 | |
| 4596 | // FIXME: A CXCursor_AsmLabelAttr should give the location of the label |
| 4597 | // but we don't keep track of this. |
| 4598 | |
| 4599 | // Default handling, give the location of the cursor. |
| 4600 | |
| 4601 | if (pieceIndex > 0) |
| 4602 | return clang_getNullRange(); |
| 4603 | |
| 4604 | CXSourceLocation CXLoc = clang_getCursorLocation(C); |
| 4605 | SourceLocation Loc = cxloc::translateSourceLocation(CXLoc); |
| 4606 | return cxloc::translateSourceRange(Ctx, Loc); |
| 4607 | } |
| 4608 | |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4609 | CXString clang_Cursor_getMangling(CXCursor C) { |
| 4610 | if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind)) |
| 4611 | return cxstring::createEmpty(); |
| 4612 | |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4613 | // Mangling only works for functions and variables. |
Eli Bendersky | 7975959 | 2014-08-01 15:01:10 +0000 | [diff] [blame] | 4614 | const Decl *D = getCursorDecl(C); |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4615 | if (!D || !(isa<FunctionDecl>(D) || isa<VarDecl>(D))) |
| 4616 | return cxstring::createEmpty(); |
| 4617 | |
Argyrios Kyrtzidis | ca741ce | 2016-02-14 22:30:14 +0000 | [diff] [blame] | 4618 | ASTContext &Ctx = D->getASTContext(); |
| 4619 | index::CodegenNameGenerator CGNameGen(Ctx); |
| 4620 | return cxstring::createDup(CGNameGen.getName(D)); |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
Saleem Abdulrasool | 6003443 | 2015-11-12 03:57:22 +0000 | [diff] [blame] | 4623 | CXStringSet *clang_Cursor_getCXXManglings(CXCursor C) { |
| 4624 | if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind)) |
| 4625 | return nullptr; |
| 4626 | |
| 4627 | const Decl *D = getCursorDecl(C); |
| 4628 | if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D))) |
| 4629 | return nullptr; |
| 4630 | |
Argyrios Kyrtzidis | ca741ce | 2016-02-14 22:30:14 +0000 | [diff] [blame] | 4631 | ASTContext &Ctx = D->getASTContext(); |
| 4632 | index::CodegenNameGenerator CGNameGen(Ctx); |
| 4633 | std::vector<std::string> Manglings = CGNameGen.getAllManglings(D); |
Saleem Abdulrasool | 6003443 | 2015-11-12 03:57:22 +0000 | [diff] [blame] | 4634 | return cxstring::createSet(Manglings); |
| 4635 | } |
| 4636 | |
Dave Lee | 1a532c9 | 2017-09-22 16:58:57 +0000 | [diff] [blame] | 4637 | CXStringSet *clang_Cursor_getObjCManglings(CXCursor C) { |
| 4638 | if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind)) |
| 4639 | return nullptr; |
| 4640 | |
| 4641 | const Decl *D = getCursorDecl(C); |
| 4642 | if (!(isa<ObjCInterfaceDecl>(D) || isa<ObjCImplementationDecl>(D))) |
| 4643 | return nullptr; |
| 4644 | |
| 4645 | ASTContext &Ctx = D->getASTContext(); |
| 4646 | index::CodegenNameGenerator CGNameGen(Ctx); |
| 4647 | std::vector<std::string> Manglings = CGNameGen.getAllManglings(D); |
| 4648 | return cxstring::createSet(Manglings); |
| 4649 | } |
| 4650 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4651 | CXString clang_getCursorDisplayName(CXCursor C) { |
| 4652 | if (!clang_isDeclaration(C.kind)) |
| 4653 | return clang_getCursorSpelling(C); |
| 4654 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4655 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4656 | if (!D) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4657 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4658 | |
| 4659 | PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4660 | if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4661 | D = FunTmpl->getTemplatedDecl(); |
| 4662 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4663 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4664 | SmallString<64> Str; |
| 4665 | llvm::raw_svector_ostream OS(Str); |
| 4666 | OS << *Function; |
| 4667 | if (Function->getPrimaryTemplate()) |
| 4668 | OS << "<>"; |
| 4669 | OS << "("; |
| 4670 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { |
| 4671 | if (I) |
| 4672 | OS << ", "; |
| 4673 | OS << Function->getParamDecl(I)->getType().getAsString(Policy); |
| 4674 | } |
| 4675 | |
| 4676 | if (Function->isVariadic()) { |
| 4677 | if (Function->getNumParams()) |
| 4678 | OS << ", "; |
| 4679 | OS << "..."; |
| 4680 | } |
| 4681 | OS << ")"; |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4682 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4683 | } |
| 4684 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4685 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4686 | SmallString<64> Str; |
| 4687 | llvm::raw_svector_ostream OS(Str); |
| 4688 | OS << *ClassTemplate; |
| 4689 | OS << "<"; |
| 4690 | TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); |
| 4691 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 4692 | if (I) |
| 4693 | OS << ", "; |
| 4694 | |
| 4695 | NamedDecl *Param = Params->getParam(I); |
| 4696 | if (Param->getIdentifier()) { |
| 4697 | OS << Param->getIdentifier()->getName(); |
| 4698 | continue; |
| 4699 | } |
| 4700 | |
| 4701 | // There is no parameter name, which makes this tricky. Try to come up |
| 4702 | // with something useful that isn't too long. |
| 4703 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 4704 | OS << (TTP->wasDeclaredWithTypename()? "typename" : "class"); |
| 4705 | else if (NonTypeTemplateParmDecl *NTTP |
| 4706 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 4707 | OS << NTTP->getType().getAsString(Policy); |
| 4708 | else |
| 4709 | OS << "template<...> class"; |
| 4710 | } |
| 4711 | |
| 4712 | OS << ">"; |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4713 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4714 | } |
| 4715 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4716 | if (const ClassTemplateSpecializationDecl *ClassSpec |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4717 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 4718 | // If the type was explicitly written, use that. |
| 4719 | if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4720 | return cxstring::createDup(TSInfo->getType().getAsString(Policy)); |
Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame^] | 4721 | |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 4722 | SmallString<128> Str; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4723 | llvm::raw_svector_ostream OS(Str); |
| 4724 | OS << *ClassSpec; |
Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame^] | 4725 | printTemplateArgumentList(OS, ClassSpec->getTemplateArgs().asArray(), |
| 4726 | Policy); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4727 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4728 | } |
| 4729 | |
| 4730 | return clang_getCursorSpelling(C); |
| 4731 | } |
| 4732 | |
| 4733 | CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
| 4734 | switch (Kind) { |
| 4735 | case CXCursor_FunctionDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4736 | return cxstring::createRef("FunctionDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4737 | case CXCursor_TypedefDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4738 | return cxstring::createRef("TypedefDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4739 | case CXCursor_EnumDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4740 | return cxstring::createRef("EnumDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4741 | case CXCursor_EnumConstantDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4742 | return cxstring::createRef("EnumConstantDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4743 | case CXCursor_StructDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4744 | return cxstring::createRef("StructDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4745 | case CXCursor_UnionDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4746 | return cxstring::createRef("UnionDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4747 | case CXCursor_ClassDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4748 | return cxstring::createRef("ClassDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4749 | case CXCursor_FieldDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4750 | return cxstring::createRef("FieldDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4751 | case CXCursor_VarDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4752 | return cxstring::createRef("VarDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4753 | case CXCursor_ParmDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4754 | return cxstring::createRef("ParmDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4755 | case CXCursor_ObjCInterfaceDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4756 | return cxstring::createRef("ObjCInterfaceDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4757 | case CXCursor_ObjCCategoryDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4758 | return cxstring::createRef("ObjCCategoryDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4759 | case CXCursor_ObjCProtocolDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4760 | return cxstring::createRef("ObjCProtocolDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4761 | case CXCursor_ObjCPropertyDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4762 | return cxstring::createRef("ObjCPropertyDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4763 | case CXCursor_ObjCIvarDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4764 | return cxstring::createRef("ObjCIvarDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4765 | case CXCursor_ObjCInstanceMethodDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4766 | return cxstring::createRef("ObjCInstanceMethodDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4767 | case CXCursor_ObjCClassMethodDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4768 | return cxstring::createRef("ObjCClassMethodDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4769 | case CXCursor_ObjCImplementationDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4770 | return cxstring::createRef("ObjCImplementationDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4771 | case CXCursor_ObjCCategoryImplDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4772 | return cxstring::createRef("ObjCCategoryImplDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4773 | case CXCursor_CXXMethod: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4774 | return cxstring::createRef("CXXMethod"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4775 | case CXCursor_UnexposedDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4776 | return cxstring::createRef("UnexposedDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4777 | case CXCursor_ObjCSuperClassRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4778 | return cxstring::createRef("ObjCSuperClassRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4779 | case CXCursor_ObjCProtocolRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4780 | return cxstring::createRef("ObjCProtocolRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4781 | case CXCursor_ObjCClassRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4782 | return cxstring::createRef("ObjCClassRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4783 | case CXCursor_TypeRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4784 | return cxstring::createRef("TypeRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4785 | case CXCursor_TemplateRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4786 | return cxstring::createRef("TemplateRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4787 | case CXCursor_NamespaceRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4788 | return cxstring::createRef("NamespaceRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4789 | case CXCursor_MemberRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4790 | return cxstring::createRef("MemberRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4791 | case CXCursor_LabelRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4792 | return cxstring::createRef("LabelRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4793 | case CXCursor_OverloadedDeclRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4794 | return cxstring::createRef("OverloadedDeclRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4795 | case CXCursor_VariableRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4796 | return cxstring::createRef("VariableRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4797 | case CXCursor_IntegerLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4798 | return cxstring::createRef("IntegerLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4799 | case CXCursor_FloatingLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4800 | return cxstring::createRef("FloatingLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4801 | case CXCursor_ImaginaryLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4802 | return cxstring::createRef("ImaginaryLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4803 | case CXCursor_StringLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4804 | return cxstring::createRef("StringLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4805 | case CXCursor_CharacterLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4806 | return cxstring::createRef("CharacterLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4807 | case CXCursor_ParenExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4808 | return cxstring::createRef("ParenExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4809 | case CXCursor_UnaryOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4810 | return cxstring::createRef("UnaryOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4811 | case CXCursor_ArraySubscriptExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4812 | return cxstring::createRef("ArraySubscriptExpr"); |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 4813 | case CXCursor_OMPArraySectionExpr: |
| 4814 | return cxstring::createRef("OMPArraySectionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4815 | case CXCursor_BinaryOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4816 | return cxstring::createRef("BinaryOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4817 | case CXCursor_CompoundAssignOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4818 | return cxstring::createRef("CompoundAssignOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4819 | case CXCursor_ConditionalOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4820 | return cxstring::createRef("ConditionalOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4821 | case CXCursor_CStyleCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4822 | return cxstring::createRef("CStyleCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4823 | case CXCursor_CompoundLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4824 | return cxstring::createRef("CompoundLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4825 | case CXCursor_InitListExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4826 | return cxstring::createRef("InitListExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4827 | case CXCursor_AddrLabelExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4828 | return cxstring::createRef("AddrLabelExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4829 | case CXCursor_StmtExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4830 | return cxstring::createRef("StmtExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4831 | case CXCursor_GenericSelectionExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4832 | return cxstring::createRef("GenericSelectionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4833 | case CXCursor_GNUNullExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4834 | return cxstring::createRef("GNUNullExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4835 | case CXCursor_CXXStaticCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4836 | return cxstring::createRef("CXXStaticCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4837 | case CXCursor_CXXDynamicCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4838 | return cxstring::createRef("CXXDynamicCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4839 | case CXCursor_CXXReinterpretCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4840 | return cxstring::createRef("CXXReinterpretCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4841 | case CXCursor_CXXConstCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4842 | return cxstring::createRef("CXXConstCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4843 | case CXCursor_CXXFunctionalCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4844 | return cxstring::createRef("CXXFunctionalCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4845 | case CXCursor_CXXTypeidExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4846 | return cxstring::createRef("CXXTypeidExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4847 | case CXCursor_CXXBoolLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4848 | return cxstring::createRef("CXXBoolLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4849 | case CXCursor_CXXNullPtrLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4850 | return cxstring::createRef("CXXNullPtrLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4851 | case CXCursor_CXXThisExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4852 | return cxstring::createRef("CXXThisExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4853 | case CXCursor_CXXThrowExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4854 | return cxstring::createRef("CXXThrowExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4855 | case CXCursor_CXXNewExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4856 | return cxstring::createRef("CXXNewExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4857 | case CXCursor_CXXDeleteExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4858 | return cxstring::createRef("CXXDeleteExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4859 | case CXCursor_UnaryExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4860 | return cxstring::createRef("UnaryExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4861 | case CXCursor_ObjCStringLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4862 | return cxstring::createRef("ObjCStringLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4863 | case CXCursor_ObjCBoolLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4864 | return cxstring::createRef("ObjCBoolLiteralExpr"); |
Erik Pilkington | 29099de | 2016-07-16 00:35:23 +0000 | [diff] [blame] | 4865 | case CXCursor_ObjCAvailabilityCheckExpr: |
| 4866 | return cxstring::createRef("ObjCAvailabilityCheckExpr"); |
Argyrios Kyrtzidis | c2233be | 2013-04-23 17:57:17 +0000 | [diff] [blame] | 4867 | case CXCursor_ObjCSelfExpr: |
| 4868 | return cxstring::createRef("ObjCSelfExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4869 | case CXCursor_ObjCEncodeExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4870 | return cxstring::createRef("ObjCEncodeExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4871 | case CXCursor_ObjCSelectorExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4872 | return cxstring::createRef("ObjCSelectorExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4873 | case CXCursor_ObjCProtocolExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4874 | return cxstring::createRef("ObjCProtocolExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4875 | case CXCursor_ObjCBridgedCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4876 | return cxstring::createRef("ObjCBridgedCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4877 | case CXCursor_BlockExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4878 | return cxstring::createRef("BlockExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4879 | case CXCursor_PackExpansionExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4880 | return cxstring::createRef("PackExpansionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4881 | case CXCursor_SizeOfPackExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4882 | return cxstring::createRef("SizeOfPackExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4883 | case CXCursor_LambdaExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4884 | return cxstring::createRef("LambdaExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4885 | case CXCursor_UnexposedExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4886 | return cxstring::createRef("UnexposedExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4887 | case CXCursor_DeclRefExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4888 | return cxstring::createRef("DeclRefExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4889 | case CXCursor_MemberRefExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4890 | return cxstring::createRef("MemberRefExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4891 | case CXCursor_CallExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4892 | return cxstring::createRef("CallExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4893 | case CXCursor_ObjCMessageExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4894 | return cxstring::createRef("ObjCMessageExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4895 | case CXCursor_UnexposedStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4896 | return cxstring::createRef("UnexposedStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4897 | case CXCursor_DeclStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4898 | return cxstring::createRef("DeclStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4899 | case CXCursor_LabelStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4900 | return cxstring::createRef("LabelStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4901 | case CXCursor_CompoundStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4902 | return cxstring::createRef("CompoundStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4903 | case CXCursor_CaseStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4904 | return cxstring::createRef("CaseStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4905 | case CXCursor_DefaultStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4906 | return cxstring::createRef("DefaultStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4907 | case CXCursor_IfStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4908 | return cxstring::createRef("IfStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4909 | case CXCursor_SwitchStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4910 | return cxstring::createRef("SwitchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4911 | case CXCursor_WhileStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4912 | return cxstring::createRef("WhileStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4913 | case CXCursor_DoStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4914 | return cxstring::createRef("DoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4915 | case CXCursor_ForStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4916 | return cxstring::createRef("ForStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4917 | case CXCursor_GotoStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4918 | return cxstring::createRef("GotoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4919 | case CXCursor_IndirectGotoStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4920 | return cxstring::createRef("IndirectGotoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4921 | case CXCursor_ContinueStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4922 | return cxstring::createRef("ContinueStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4923 | case CXCursor_BreakStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4924 | return cxstring::createRef("BreakStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4925 | case CXCursor_ReturnStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4926 | return cxstring::createRef("ReturnStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4927 | case CXCursor_GCCAsmStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4928 | return cxstring::createRef("GCCAsmStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4929 | case CXCursor_MSAsmStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4930 | return cxstring::createRef("MSAsmStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4931 | case CXCursor_ObjCAtTryStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4932 | return cxstring::createRef("ObjCAtTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4933 | case CXCursor_ObjCAtCatchStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4934 | return cxstring::createRef("ObjCAtCatchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4935 | case CXCursor_ObjCAtFinallyStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4936 | return cxstring::createRef("ObjCAtFinallyStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4937 | case CXCursor_ObjCAtThrowStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4938 | return cxstring::createRef("ObjCAtThrowStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4939 | case CXCursor_ObjCAtSynchronizedStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4940 | return cxstring::createRef("ObjCAtSynchronizedStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4941 | case CXCursor_ObjCAutoreleasePoolStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4942 | return cxstring::createRef("ObjCAutoreleasePoolStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4943 | case CXCursor_ObjCForCollectionStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4944 | return cxstring::createRef("ObjCForCollectionStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4945 | case CXCursor_CXXCatchStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4946 | return cxstring::createRef("CXXCatchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4947 | case CXCursor_CXXTryStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4948 | return cxstring::createRef("CXXTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4949 | case CXCursor_CXXForRangeStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4950 | return cxstring::createRef("CXXForRangeStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4951 | case CXCursor_SEHTryStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4952 | return cxstring::createRef("SEHTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4953 | case CXCursor_SEHExceptStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4954 | return cxstring::createRef("SEHExceptStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4955 | case CXCursor_SEHFinallyStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4956 | return cxstring::createRef("SEHFinallyStmt"); |
Nico Weber | 9b98207 | 2014-07-07 00:12:30 +0000 | [diff] [blame] | 4957 | case CXCursor_SEHLeaveStmt: |
| 4958 | return cxstring::createRef("SEHLeaveStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4959 | case CXCursor_NullStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4960 | return cxstring::createRef("NullStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4961 | case CXCursor_InvalidFile: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4962 | return cxstring::createRef("InvalidFile"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4963 | case CXCursor_InvalidCode: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4964 | return cxstring::createRef("InvalidCode"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4965 | case CXCursor_NoDeclFound: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4966 | return cxstring::createRef("NoDeclFound"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4967 | case CXCursor_NotImplemented: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4968 | return cxstring::createRef("NotImplemented"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4969 | case CXCursor_TranslationUnit: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4970 | return cxstring::createRef("TranslationUnit"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4971 | case CXCursor_UnexposedAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4972 | return cxstring::createRef("UnexposedAttr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4973 | case CXCursor_IBActionAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4974 | return cxstring::createRef("attribute(ibaction)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4975 | case CXCursor_IBOutletAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4976 | return cxstring::createRef("attribute(iboutlet)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4977 | case CXCursor_IBOutletCollectionAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4978 | return cxstring::createRef("attribute(iboutletcollection)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4979 | case CXCursor_CXXFinalAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4980 | return cxstring::createRef("attribute(final)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4981 | case CXCursor_CXXOverrideAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4982 | return cxstring::createRef("attribute(override)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4983 | case CXCursor_AnnotateAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4984 | return cxstring::createRef("attribute(annotate)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4985 | case CXCursor_AsmLabelAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4986 | return cxstring::createRef("asm label"); |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 4987 | case CXCursor_PackedAttr: |
| 4988 | return cxstring::createRef("attribute(packed)"); |
Joey Gouly | 8122838 | 2014-05-01 15:41:58 +0000 | [diff] [blame] | 4989 | case CXCursor_PureAttr: |
| 4990 | return cxstring::createRef("attribute(pure)"); |
| 4991 | case CXCursor_ConstAttr: |
| 4992 | return cxstring::createRef("attribute(const)"); |
| 4993 | case CXCursor_NoDuplicateAttr: |
| 4994 | return cxstring::createRef("attribute(noduplicate)"); |
Eli Bendersky | 2581e66 | 2014-05-28 19:29:58 +0000 | [diff] [blame] | 4995 | case CXCursor_CUDAConstantAttr: |
| 4996 | return cxstring::createRef("attribute(constant)"); |
| 4997 | case CXCursor_CUDADeviceAttr: |
| 4998 | return cxstring::createRef("attribute(device)"); |
| 4999 | case CXCursor_CUDAGlobalAttr: |
| 5000 | return cxstring::createRef("attribute(global)"); |
| 5001 | case CXCursor_CUDAHostAttr: |
| 5002 | return cxstring::createRef("attribute(host)"); |
Eli Bendersky | 9b07147 | 2014-08-08 14:59:00 +0000 | [diff] [blame] | 5003 | case CXCursor_CUDASharedAttr: |
| 5004 | return cxstring::createRef("attribute(shared)"); |
Saleem Abdulrasool | 79c6971 | 2015-09-05 18:53:43 +0000 | [diff] [blame] | 5005 | case CXCursor_VisibilityAttr: |
| 5006 | return cxstring::createRef("attribute(visibility)"); |
Saleem Abdulrasool | 8aa0b80 | 2015-12-10 18:45:18 +0000 | [diff] [blame] | 5007 | case CXCursor_DLLExport: |
| 5008 | return cxstring::createRef("attribute(dllexport)"); |
| 5009 | case CXCursor_DLLImport: |
| 5010 | return cxstring::createRef("attribute(dllimport)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5011 | case CXCursor_PreprocessingDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5012 | return cxstring::createRef("preprocessing directive"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5013 | case CXCursor_MacroDefinition: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5014 | return cxstring::createRef("macro definition"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5015 | case CXCursor_MacroExpansion: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5016 | return cxstring::createRef("macro expansion"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5017 | case CXCursor_InclusionDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5018 | return cxstring::createRef("inclusion directive"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5019 | case CXCursor_Namespace: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5020 | return cxstring::createRef("Namespace"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5021 | case CXCursor_LinkageSpec: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5022 | return cxstring::createRef("LinkageSpec"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5023 | case CXCursor_CXXBaseSpecifier: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5024 | return cxstring::createRef("C++ base class specifier"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5025 | case CXCursor_Constructor: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5026 | return cxstring::createRef("CXXConstructor"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5027 | case CXCursor_Destructor: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5028 | return cxstring::createRef("CXXDestructor"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5029 | case CXCursor_ConversionFunction: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5030 | return cxstring::createRef("CXXConversion"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5031 | case CXCursor_TemplateTypeParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5032 | return cxstring::createRef("TemplateTypeParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5033 | case CXCursor_NonTypeTemplateParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5034 | return cxstring::createRef("NonTypeTemplateParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5035 | case CXCursor_TemplateTemplateParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5036 | return cxstring::createRef("TemplateTemplateParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5037 | case CXCursor_FunctionTemplate: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5038 | return cxstring::createRef("FunctionTemplate"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5039 | case CXCursor_ClassTemplate: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5040 | return cxstring::createRef("ClassTemplate"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5041 | case CXCursor_ClassTemplatePartialSpecialization: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5042 | return cxstring::createRef("ClassTemplatePartialSpecialization"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5043 | case CXCursor_NamespaceAlias: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5044 | return cxstring::createRef("NamespaceAlias"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5045 | case CXCursor_UsingDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5046 | return cxstring::createRef("UsingDirective"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5047 | case CXCursor_UsingDeclaration: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5048 | return cxstring::createRef("UsingDeclaration"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5049 | case CXCursor_TypeAliasDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5050 | return cxstring::createRef("TypeAliasDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5051 | case CXCursor_ObjCSynthesizeDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5052 | return cxstring::createRef("ObjCSynthesizeDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5053 | case CXCursor_ObjCDynamicDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5054 | return cxstring::createRef("ObjCDynamicDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5055 | case CXCursor_CXXAccessSpecifier: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5056 | return cxstring::createRef("CXXAccessSpecifier"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5057 | case CXCursor_ModuleImportDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5058 | return cxstring::createRef("ModuleImport"); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 5059 | case CXCursor_OMPParallelDirective: |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 5060 | return cxstring::createRef("OMPParallelDirective"); |
| 5061 | case CXCursor_OMPSimdDirective: |
| 5062 | return cxstring::createRef("OMPSimdDirective"); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 5063 | case CXCursor_OMPForDirective: |
| 5064 | return cxstring::createRef("OMPForDirective"); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 5065 | case CXCursor_OMPForSimdDirective: |
| 5066 | return cxstring::createRef("OMPForSimdDirective"); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 5067 | case CXCursor_OMPSectionsDirective: |
| 5068 | return cxstring::createRef("OMPSectionsDirective"); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 5069 | case CXCursor_OMPSectionDirective: |
| 5070 | return cxstring::createRef("OMPSectionDirective"); |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 5071 | case CXCursor_OMPSingleDirective: |
| 5072 | return cxstring::createRef("OMPSingleDirective"); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 5073 | case CXCursor_OMPMasterDirective: |
| 5074 | return cxstring::createRef("OMPMasterDirective"); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 5075 | case CXCursor_OMPCriticalDirective: |
| 5076 | return cxstring::createRef("OMPCriticalDirective"); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 5077 | case CXCursor_OMPParallelForDirective: |
| 5078 | return cxstring::createRef("OMPParallelForDirective"); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 5079 | case CXCursor_OMPParallelForSimdDirective: |
| 5080 | return cxstring::createRef("OMPParallelForSimdDirective"); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 5081 | case CXCursor_OMPParallelSectionsDirective: |
| 5082 | return cxstring::createRef("OMPParallelSectionsDirective"); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 5083 | case CXCursor_OMPTaskDirective: |
| 5084 | return cxstring::createRef("OMPTaskDirective"); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 5085 | case CXCursor_OMPTaskyieldDirective: |
| 5086 | return cxstring::createRef("OMPTaskyieldDirective"); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 5087 | case CXCursor_OMPBarrierDirective: |
| 5088 | return cxstring::createRef("OMPBarrierDirective"); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 5089 | case CXCursor_OMPTaskwaitDirective: |
| 5090 | return cxstring::createRef("OMPTaskwaitDirective"); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 5091 | case CXCursor_OMPTaskgroupDirective: |
| 5092 | return cxstring::createRef("OMPTaskgroupDirective"); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 5093 | case CXCursor_OMPFlushDirective: |
| 5094 | return cxstring::createRef("OMPFlushDirective"); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 5095 | case CXCursor_OMPOrderedDirective: |
| 5096 | return cxstring::createRef("OMPOrderedDirective"); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 5097 | case CXCursor_OMPAtomicDirective: |
| 5098 | return cxstring::createRef("OMPAtomicDirective"); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 5099 | case CXCursor_OMPTargetDirective: |
| 5100 | return cxstring::createRef("OMPTargetDirective"); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 5101 | case CXCursor_OMPTargetDataDirective: |
| 5102 | return cxstring::createRef("OMPTargetDataDirective"); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 5103 | case CXCursor_OMPTargetEnterDataDirective: |
| 5104 | return cxstring::createRef("OMPTargetEnterDataDirective"); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 5105 | case CXCursor_OMPTargetExitDataDirective: |
| 5106 | return cxstring::createRef("OMPTargetExitDataDirective"); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 5107 | case CXCursor_OMPTargetParallelDirective: |
| 5108 | return cxstring::createRef("OMPTargetParallelDirective"); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 5109 | case CXCursor_OMPTargetParallelForDirective: |
| 5110 | return cxstring::createRef("OMPTargetParallelForDirective"); |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 5111 | case CXCursor_OMPTargetUpdateDirective: |
| 5112 | return cxstring::createRef("OMPTargetUpdateDirective"); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 5113 | case CXCursor_OMPTeamsDirective: |
| 5114 | return cxstring::createRef("OMPTeamsDirective"); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 5115 | case CXCursor_OMPCancellationPointDirective: |
| 5116 | return cxstring::createRef("OMPCancellationPointDirective"); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 5117 | case CXCursor_OMPCancelDirective: |
| 5118 | return cxstring::createRef("OMPCancelDirective"); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 5119 | case CXCursor_OMPTaskLoopDirective: |
| 5120 | return cxstring::createRef("OMPTaskLoopDirective"); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 5121 | case CXCursor_OMPTaskLoopSimdDirective: |
| 5122 | return cxstring::createRef("OMPTaskLoopSimdDirective"); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 5123 | case CXCursor_OMPDistributeDirective: |
| 5124 | return cxstring::createRef("OMPDistributeDirective"); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 5125 | case CXCursor_OMPDistributeParallelForDirective: |
| 5126 | return cxstring::createRef("OMPDistributeParallelForDirective"); |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 5127 | case CXCursor_OMPDistributeParallelForSimdDirective: |
| 5128 | return cxstring::createRef("OMPDistributeParallelForSimdDirective"); |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 5129 | case CXCursor_OMPDistributeSimdDirective: |
| 5130 | return cxstring::createRef("OMPDistributeSimdDirective"); |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 5131 | case CXCursor_OMPTargetParallelForSimdDirective: |
| 5132 | return cxstring::createRef("OMPTargetParallelForSimdDirective"); |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 5133 | case CXCursor_OMPTargetSimdDirective: |
| 5134 | return cxstring::createRef("OMPTargetSimdDirective"); |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 5135 | case CXCursor_OMPTeamsDistributeDirective: |
| 5136 | return cxstring::createRef("OMPTeamsDistributeDirective"); |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 5137 | case CXCursor_OMPTeamsDistributeSimdDirective: |
| 5138 | return cxstring::createRef("OMPTeamsDistributeSimdDirective"); |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 5139 | case CXCursor_OMPTeamsDistributeParallelForSimdDirective: |
| 5140 | return cxstring::createRef("OMPTeamsDistributeParallelForSimdDirective"); |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 5141 | case CXCursor_OMPTeamsDistributeParallelForDirective: |
| 5142 | return cxstring::createRef("OMPTeamsDistributeParallelForDirective"); |
Kelvin Li | bf594a5 | 2016-12-17 05:48:59 +0000 | [diff] [blame] | 5143 | case CXCursor_OMPTargetTeamsDirective: |
| 5144 | return cxstring::createRef("OMPTargetTeamsDirective"); |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 5145 | case CXCursor_OMPTargetTeamsDistributeDirective: |
| 5146 | return cxstring::createRef("OMPTargetTeamsDistributeDirective"); |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 5147 | case CXCursor_OMPTargetTeamsDistributeParallelForDirective: |
| 5148 | return cxstring::createRef("OMPTargetTeamsDistributeParallelForDirective"); |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 5149 | case CXCursor_OMPTargetTeamsDistributeParallelForSimdDirective: |
| 5150 | return cxstring::createRef( |
| 5151 | "OMPTargetTeamsDistributeParallelForSimdDirective"); |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 5152 | case CXCursor_OMPTargetTeamsDistributeSimdDirective: |
| 5153 | return cxstring::createRef("OMPTargetTeamsDistributeSimdDirective"); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 5154 | case CXCursor_OverloadCandidate: |
| 5155 | return cxstring::createRef("OverloadCandidate"); |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 5156 | case CXCursor_TypeAliasTemplateDecl: |
| 5157 | return cxstring::createRef("TypeAliasTemplateDecl"); |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 5158 | case CXCursor_StaticAssert: |
| 5159 | return cxstring::createRef("StaticAssert"); |
Olivier Goffart | d211c64 | 2016-11-04 06:29:27 +0000 | [diff] [blame] | 5160 | case CXCursor_FriendDecl: |
| 5161 | return cxstring::createRef("FriendDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5162 | } |
| 5163 | |
| 5164 | llvm_unreachable("Unhandled CXCursorKind"); |
| 5165 | } |
| 5166 | |
| 5167 | struct GetCursorData { |
| 5168 | SourceLocation TokenBeginLoc; |
| 5169 | bool PointsAtMacroArgExpansion; |
| 5170 | bool VisitedObjCPropertyImplDecl; |
| 5171 | SourceLocation VisitedDeclaratorDeclStartLoc; |
| 5172 | CXCursor &BestCursor; |
| 5173 | |
| 5174 | GetCursorData(SourceManager &SM, |
| 5175 | SourceLocation tokenBegin, CXCursor &outputCursor) |
| 5176 | : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) { |
| 5177 | PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin); |
| 5178 | VisitedObjCPropertyImplDecl = false; |
| 5179 | } |
| 5180 | }; |
| 5181 | |
| 5182 | static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, |
| 5183 | CXCursor parent, |
| 5184 | CXClientData client_data) { |
| 5185 | GetCursorData *Data = static_cast<GetCursorData *>(client_data); |
| 5186 | CXCursor *BestCursor = &Data->BestCursor; |
| 5187 | |
| 5188 | // If we point inside a macro argument we should provide info of what the |
| 5189 | // token is so use the actual cursor, don't replace it with a macro expansion |
| 5190 | // cursor. |
| 5191 | if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion) |
| 5192 | return CXChildVisit_Recurse; |
| 5193 | |
| 5194 | if (clang_isDeclaration(cursor.kind)) { |
| 5195 | // Avoid having the implicit methods override the property decls. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5196 | if (const ObjCMethodDecl *MD |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5197 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 5198 | if (MD->isImplicit()) |
| 5199 | return CXChildVisit_Break; |
| 5200 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5201 | } else if (const ObjCInterfaceDecl *ID |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5202 | = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(cursor))) { |
| 5203 | // Check that when we have multiple @class references in the same line, |
| 5204 | // that later ones do not override the previous ones. |
| 5205 | // If we have: |
| 5206 | // @class Foo, Bar; |
| 5207 | // source ranges for both start at '@', so 'Bar' will end up overriding |
| 5208 | // 'Foo' even though the cursor location was at 'Foo'. |
| 5209 | if (BestCursor->kind == CXCursor_ObjCInterfaceDecl || |
| 5210 | BestCursor->kind == CXCursor_ObjCClassRef) |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5211 | if (const ObjCInterfaceDecl *PrevID |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5212 | = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(*BestCursor))){ |
| 5213 | if (PrevID != ID && |
| 5214 | !PrevID->isThisDeclarationADefinition() && |
| 5215 | !ID->isThisDeclarationADefinition()) |
| 5216 | return CXChildVisit_Break; |
| 5217 | } |
| 5218 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5219 | } else if (const DeclaratorDecl *DD |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5220 | = dyn_cast_or_null<DeclaratorDecl>(getCursorDecl(cursor))) { |
| 5221 | SourceLocation StartLoc = DD->getSourceRange().getBegin(); |
| 5222 | // Check that when we have multiple declarators in the same line, |
| 5223 | // that later ones do not override the previous ones. |
| 5224 | // If we have: |
| 5225 | // int Foo, Bar; |
| 5226 | // source ranges for both start at 'int', so 'Bar' will end up overriding |
| 5227 | // 'Foo' even though the cursor location was at 'Foo'. |
| 5228 | if (Data->VisitedDeclaratorDeclStartLoc == StartLoc) |
| 5229 | return CXChildVisit_Break; |
| 5230 | Data->VisitedDeclaratorDeclStartLoc = StartLoc; |
| 5231 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5232 | } else if (const ObjCPropertyImplDecl *PropImp |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5233 | = dyn_cast_or_null<ObjCPropertyImplDecl>(getCursorDecl(cursor))) { |
| 5234 | (void)PropImp; |
| 5235 | // Check that when we have multiple @synthesize in the same line, |
| 5236 | // that later ones do not override the previous ones. |
| 5237 | // If we have: |
| 5238 | // @synthesize Foo, Bar; |
| 5239 | // source ranges for both start at '@', so 'Bar' will end up overriding |
| 5240 | // 'Foo' even though the cursor location was at 'Foo'. |
| 5241 | if (Data->VisitedObjCPropertyImplDecl) |
| 5242 | return CXChildVisit_Break; |
| 5243 | Data->VisitedObjCPropertyImplDecl = true; |
| 5244 | } |
| 5245 | } |
| 5246 | |
| 5247 | if (clang_isExpression(cursor.kind) && |
| 5248 | clang_isDeclaration(BestCursor->kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5249 | if (const Decl *D = getCursorDecl(*BestCursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5250 | // Avoid having the cursor of an expression replace the declaration cursor |
| 5251 | // when the expression source range overlaps the declaration range. |
| 5252 | // This can happen for C++ constructor expressions whose range generally |
| 5253 | // include the variable declaration, e.g.: |
| 5254 | // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor. |
| 5255 | if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() && |
| 5256 | D->getLocation() == Data->TokenBeginLoc) |
| 5257 | return CXChildVisit_Break; |
| 5258 | } |
| 5259 | } |
| 5260 | |
| 5261 | // If our current best cursor is the construction of a temporary object, |
| 5262 | // don't replace that cursor with a type reference, because we want |
| 5263 | // clang_getCursor() to point at the constructor. |
| 5264 | if (clang_isExpression(BestCursor->kind) && |
| 5265 | isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && |
| 5266 | cursor.kind == CXCursor_TypeRef) { |
| 5267 | // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it |
| 5268 | // as having the actual point on the type reference. |
| 5269 | *BestCursor = getTypeRefedCallExprCursor(*BestCursor); |
| 5270 | return CXChildVisit_Recurse; |
| 5271 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 5272 | |
| 5273 | // If we already have an Objective-C superclass reference, don't |
| 5274 | // update it further. |
| 5275 | if (BestCursor->kind == CXCursor_ObjCSuperClassRef) |
| 5276 | return CXChildVisit_Break; |
| 5277 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5278 | *BestCursor = cursor; |
| 5279 | return CXChildVisit_Recurse; |
| 5280 | } |
| 5281 | |
| 5282 | CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 5283 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 5284 | LOG_BAD_TU(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5285 | return clang_getNullCursor(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 5286 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5287 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 5288 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5289 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 5290 | |
| 5291 | SourceLocation SLoc = cxloc::translateSourceLocation(Loc); |
| 5292 | CXCursor Result = cxcursor::getCursor(TU, SLoc); |
| 5293 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5294 | LOG_FUNC_SECTION { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5295 | CXFile SearchFile; |
| 5296 | unsigned SearchLine, SearchColumn; |
| 5297 | CXFile ResultFile; |
| 5298 | unsigned ResultLine, ResultColumn; |
| 5299 | CXString SearchFileName, ResultFileName, KindSpelling, USR; |
| 5300 | const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : ""; |
| 5301 | CXSourceLocation ResultLoc = clang_getCursorLocation(Result); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5302 | |
| 5303 | clang_getFileLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, |
| 5304 | nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5305 | clang_getFileLocation(ResultLoc, &ResultFile, &ResultLine, |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5306 | &ResultColumn, nullptr); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5307 | SearchFileName = clang_getFileName(SearchFile); |
| 5308 | ResultFileName = clang_getFileName(ResultFile); |
| 5309 | KindSpelling = clang_getCursorKindSpelling(Result.kind); |
| 5310 | USR = clang_getCursorUSR(Result); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5311 | *Log << llvm::format("(%s:%d:%d) = %s", |
| 5312 | clang_getCString(SearchFileName), SearchLine, SearchColumn, |
| 5313 | clang_getCString(KindSpelling)) |
| 5314 | << llvm::format("(%s:%d:%d):%s%s", |
| 5315 | clang_getCString(ResultFileName), ResultLine, ResultColumn, |
| 5316 | clang_getCString(USR), IsDef); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5317 | clang_disposeString(SearchFileName); |
| 5318 | clang_disposeString(ResultFileName); |
| 5319 | clang_disposeString(KindSpelling); |
| 5320 | clang_disposeString(USR); |
| 5321 | |
| 5322 | CXCursor Definition = clang_getCursorDefinition(Result); |
| 5323 | if (!clang_equalCursors(Definition, clang_getNullCursor())) { |
| 5324 | CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition); |
| 5325 | CXString DefinitionKindSpelling |
| 5326 | = clang_getCursorKindSpelling(Definition.kind); |
| 5327 | CXFile DefinitionFile; |
| 5328 | unsigned DefinitionLine, DefinitionColumn; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5329 | clang_getFileLocation(DefinitionLoc, &DefinitionFile, |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5330 | &DefinitionLine, &DefinitionColumn, nullptr); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5331 | CXString DefinitionFileName = clang_getFileName(DefinitionFile); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5332 | *Log << llvm::format(" -> %s(%s:%d:%d)", |
| 5333 | clang_getCString(DefinitionKindSpelling), |
| 5334 | clang_getCString(DefinitionFileName), |
| 5335 | DefinitionLine, DefinitionColumn); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5336 | clang_disposeString(DefinitionFileName); |
| 5337 | clang_disposeString(DefinitionKindSpelling); |
| 5338 | } |
| 5339 | } |
| 5340 | |
| 5341 | return Result; |
| 5342 | } |
| 5343 | |
| 5344 | CXCursor clang_getNullCursor(void) { |
| 5345 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
| 5346 | } |
| 5347 | |
| 5348 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5349 | // Clear out the "FirstInDeclGroup" part in a declaration cursor, since we |
| 5350 | // can't set consistently. For example, when visiting a DeclStmt we will set |
| 5351 | // it but we don't set it on the result of clang_getCursorDefinition for |
| 5352 | // a reference of the same declaration. |
| 5353 | // FIXME: Setting "FirstInDeclGroup" in CXCursors is a hack that only works |
| 5354 | // when visiting a DeclStmt currently, the AST should be enhanced to be able |
| 5355 | // to provide that kind of info. |
| 5356 | if (clang_isDeclaration(X.kind)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5357 | X.data[1] = nullptr; |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5358 | if (clang_isDeclaration(Y.kind)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5359 | Y.data[1] = nullptr; |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5360 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5361 | return X == Y; |
| 5362 | } |
| 5363 | |
| 5364 | unsigned clang_hashCursor(CXCursor C) { |
| 5365 | unsigned Index = 0; |
| 5366 | if (clang_isExpression(C.kind) || clang_isStatement(C.kind)) |
| 5367 | Index = 1; |
| 5368 | |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5369 | return llvm::DenseMapInfo<std::pair<unsigned, const void*> >::getHashValue( |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5370 | std::make_pair(C.kind, C.data[Index])); |
| 5371 | } |
| 5372 | |
| 5373 | unsigned clang_isInvalid(enum CXCursorKind K) { |
| 5374 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 5375 | } |
| 5376 | |
| 5377 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
| 5378 | return (K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl) || |
| 5379 | (K >= CXCursor_FirstExtraDecl && K <= CXCursor_LastExtraDecl); |
| 5380 | } |
| 5381 | |
| 5382 | unsigned clang_isReference(enum CXCursorKind K) { |
| 5383 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 5384 | } |
| 5385 | |
| 5386 | unsigned clang_isExpression(enum CXCursorKind K) { |
| 5387 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; |
| 5388 | } |
| 5389 | |
| 5390 | unsigned clang_isStatement(enum CXCursorKind K) { |
| 5391 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; |
| 5392 | } |
| 5393 | |
| 5394 | unsigned clang_isAttribute(enum CXCursorKind K) { |
| 5395 | return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr; |
| 5396 | } |
| 5397 | |
| 5398 | unsigned clang_isTranslationUnit(enum CXCursorKind K) { |
| 5399 | return K == CXCursor_TranslationUnit; |
| 5400 | } |
| 5401 | |
| 5402 | unsigned clang_isPreprocessing(enum CXCursorKind K) { |
| 5403 | return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; |
| 5404 | } |
| 5405 | |
| 5406 | unsigned clang_isUnexposed(enum CXCursorKind K) { |
| 5407 | switch (K) { |
| 5408 | case CXCursor_UnexposedDecl: |
| 5409 | case CXCursor_UnexposedExpr: |
| 5410 | case CXCursor_UnexposedStmt: |
| 5411 | case CXCursor_UnexposedAttr: |
| 5412 | return true; |
| 5413 | default: |
| 5414 | return false; |
| 5415 | } |
| 5416 | } |
| 5417 | |
| 5418 | CXCursorKind clang_getCursorKind(CXCursor C) { |
| 5419 | return C.kind; |
| 5420 | } |
| 5421 | |
| 5422 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 5423 | if (clang_isReference(C.kind)) { |
| 5424 | switch (C.kind) { |
| 5425 | case CXCursor_ObjCSuperClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5426 | std::pair<const ObjCInterfaceDecl *, SourceLocation> P |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5427 | = getCursorObjCSuperClassRef(C); |
| 5428 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5429 | } |
| 5430 | |
| 5431 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5432 | std::pair<const ObjCProtocolDecl *, SourceLocation> P |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5433 | = getCursorObjCProtocolRef(C); |
| 5434 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5435 | } |
| 5436 | |
| 5437 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5438 | std::pair<const ObjCInterfaceDecl *, SourceLocation> P |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5439 | = getCursorObjCClassRef(C); |
| 5440 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5441 | } |
| 5442 | |
| 5443 | case CXCursor_TypeRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5444 | std::pair<const TypeDecl *, SourceLocation> P = getCursorTypeRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5445 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5446 | } |
| 5447 | |
| 5448 | case CXCursor_TemplateRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5449 | std::pair<const TemplateDecl *, SourceLocation> P = |
| 5450 | getCursorTemplateRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5451 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5452 | } |
| 5453 | |
| 5454 | case CXCursor_NamespaceRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5455 | std::pair<const NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5456 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5457 | } |
| 5458 | |
| 5459 | case CXCursor_MemberRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5460 | std::pair<const FieldDecl *, SourceLocation> P = getCursorMemberRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5461 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5462 | } |
| 5463 | |
| 5464 | case CXCursor_VariableRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5465 | std::pair<const VarDecl *, SourceLocation> P = getCursorVariableRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5466 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5467 | } |
| 5468 | |
| 5469 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5470 | const CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5471 | if (!BaseSpec) |
| 5472 | return clang_getNullLocation(); |
| 5473 | |
| 5474 | if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) |
| 5475 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5476 | TSInfo->getTypeLoc().getBeginLoc()); |
| 5477 | |
| 5478 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5479 | BaseSpec->getLocStart()); |
| 5480 | } |
| 5481 | |
| 5482 | case CXCursor_LabelRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5483 | std::pair<const LabelStmt *, SourceLocation> P = getCursorLabelRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5484 | return cxloc::translateSourceLocation(getCursorContext(C), P.second); |
| 5485 | } |
| 5486 | |
| 5487 | case CXCursor_OverloadedDeclRef: |
| 5488 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5489 | getCursorOverloadedDeclRef(C).second); |
| 5490 | |
| 5491 | default: |
| 5492 | // FIXME: Need a way to enumerate all non-reference cases. |
| 5493 | llvm_unreachable("Missed a reference kind"); |
| 5494 | } |
| 5495 | } |
| 5496 | |
| 5497 | if (clang_isExpression(C.kind)) |
| 5498 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5499 | getLocationFromExpr(getCursorExpr(C))); |
| 5500 | |
| 5501 | if (clang_isStatement(C.kind)) |
| 5502 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5503 | getCursorStmt(C)->getLocStart()); |
| 5504 | |
| 5505 | if (C.kind == CXCursor_PreprocessingDirective) { |
| 5506 | SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); |
| 5507 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5508 | } |
| 5509 | |
| 5510 | if (C.kind == CXCursor_MacroExpansion) { |
| 5511 | SourceLocation L |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 5512 | = cxcursor::getCursorMacroExpansion(C).getSourceRange().getBegin(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5513 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5514 | } |
| 5515 | |
| 5516 | if (C.kind == CXCursor_MacroDefinition) { |
| 5517 | SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); |
| 5518 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5519 | } |
| 5520 | |
| 5521 | if (C.kind == CXCursor_InclusionDirective) { |
| 5522 | SourceLocation L |
| 5523 | = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); |
| 5524 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5525 | } |
| 5526 | |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 5527 | if (clang_isAttribute(C.kind)) { |
| 5528 | SourceLocation L |
| 5529 | = cxcursor::getCursorAttr(C)->getLocation(); |
| 5530 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5531 | } |
| 5532 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5533 | if (!clang_isDeclaration(C.kind)) |
| 5534 | return clang_getNullLocation(); |
| 5535 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5536 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5537 | if (!D) |
| 5538 | return clang_getNullLocation(); |
| 5539 | |
| 5540 | SourceLocation Loc = D->getLocation(); |
| 5541 | // FIXME: Multiple variables declared in a single declaration |
| 5542 | // currently lack the information needed to correctly determine their |
| 5543 | // ranges when accounting for the type-specifier. We use context |
| 5544 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 5545 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5546 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5547 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 5548 | Loc = VD->getLocation(); |
| 5549 | } |
| 5550 | |
| 5551 | // For ObjC methods, give the start location of the method name. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5552 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5553 | Loc = MD->getSelectorStartLoc(); |
| 5554 | |
| 5555 | return cxloc::translateSourceLocation(getCursorContext(C), Loc); |
| 5556 | } |
| 5557 | |
NAKAMURA Takumi | a01f4c3 | 2016-12-19 16:50:43 +0000 | [diff] [blame] | 5558 | } // end extern "C" |
| 5559 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5560 | CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) { |
| 5561 | assert(TU); |
| 5562 | |
| 5563 | // Guard against an invalid SourceLocation, or we may assert in one |
| 5564 | // of the following calls. |
| 5565 | if (SLoc.isInvalid()) |
| 5566 | return clang_getNullCursor(); |
| 5567 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 5568 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5569 | |
| 5570 | // Translate the given source location to make it point at the beginning of |
| 5571 | // the token under the cursor. |
| 5572 | SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), |
| 5573 | CXXUnit->getASTContext().getLangOpts()); |
| 5574 | |
| 5575 | CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 5576 | if (SLoc.isValid()) { |
| 5577 | GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result); |
| 5578 | CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData, |
| 5579 | /*VisitPreprocessorLast=*/true, |
| 5580 | /*VisitIncludedEntities=*/false, |
| 5581 | SourceLocation(SLoc)); |
| 5582 | CursorVis.visitFileRegion(); |
| 5583 | } |
| 5584 | |
| 5585 | return Result; |
| 5586 | } |
| 5587 | |
| 5588 | static SourceRange getRawCursorExtent(CXCursor C) { |
| 5589 | if (clang_isReference(C.kind)) { |
| 5590 | switch (C.kind) { |
| 5591 | case CXCursor_ObjCSuperClassRef: |
| 5592 | return getCursorObjCSuperClassRef(C).second; |
| 5593 | |
| 5594 | case CXCursor_ObjCProtocolRef: |
| 5595 | return getCursorObjCProtocolRef(C).second; |
| 5596 | |
| 5597 | case CXCursor_ObjCClassRef: |
| 5598 | return getCursorObjCClassRef(C).second; |
| 5599 | |
| 5600 | case CXCursor_TypeRef: |
| 5601 | return getCursorTypeRef(C).second; |
| 5602 | |
| 5603 | case CXCursor_TemplateRef: |
| 5604 | return getCursorTemplateRef(C).second; |
| 5605 | |
| 5606 | case CXCursor_NamespaceRef: |
| 5607 | return getCursorNamespaceRef(C).second; |
| 5608 | |
| 5609 | case CXCursor_MemberRef: |
| 5610 | return getCursorMemberRef(C).second; |
| 5611 | |
| 5612 | case CXCursor_CXXBaseSpecifier: |
| 5613 | return getCursorCXXBaseSpecifier(C)->getSourceRange(); |
| 5614 | |
| 5615 | case CXCursor_LabelRef: |
| 5616 | return getCursorLabelRef(C).second; |
| 5617 | |
| 5618 | case CXCursor_OverloadedDeclRef: |
| 5619 | return getCursorOverloadedDeclRef(C).second; |
| 5620 | |
| 5621 | case CXCursor_VariableRef: |
| 5622 | return getCursorVariableRef(C).second; |
| 5623 | |
| 5624 | default: |
| 5625 | // FIXME: Need a way to enumerate all non-reference cases. |
| 5626 | llvm_unreachable("Missed a reference kind"); |
| 5627 | } |
| 5628 | } |
| 5629 | |
| 5630 | if (clang_isExpression(C.kind)) |
| 5631 | return getCursorExpr(C)->getSourceRange(); |
| 5632 | |
| 5633 | if (clang_isStatement(C.kind)) |
| 5634 | return getCursorStmt(C)->getSourceRange(); |
| 5635 | |
| 5636 | if (clang_isAttribute(C.kind)) |
| 5637 | return getCursorAttr(C)->getRange(); |
| 5638 | |
| 5639 | if (C.kind == CXCursor_PreprocessingDirective) |
| 5640 | return cxcursor::getCursorPreprocessingDirective(C); |
| 5641 | |
| 5642 | if (C.kind == CXCursor_MacroExpansion) { |
| 5643 | ASTUnit *TU = getCursorASTUnit(C); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 5644 | SourceRange Range = cxcursor::getCursorMacroExpansion(C).getSourceRange(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5645 | return TU->mapRangeFromPreamble(Range); |
| 5646 | } |
| 5647 | |
| 5648 | if (C.kind == CXCursor_MacroDefinition) { |
| 5649 | ASTUnit *TU = getCursorASTUnit(C); |
| 5650 | SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange(); |
| 5651 | return TU->mapRangeFromPreamble(Range); |
| 5652 | } |
| 5653 | |
| 5654 | if (C.kind == CXCursor_InclusionDirective) { |
| 5655 | ASTUnit *TU = getCursorASTUnit(C); |
| 5656 | SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange(); |
| 5657 | return TU->mapRangeFromPreamble(Range); |
| 5658 | } |
| 5659 | |
| 5660 | if (C.kind == CXCursor_TranslationUnit) { |
| 5661 | ASTUnit *TU = getCursorASTUnit(C); |
| 5662 | FileID MainID = TU->getSourceManager().getMainFileID(); |
| 5663 | SourceLocation Start = TU->getSourceManager().getLocForStartOfFile(MainID); |
| 5664 | SourceLocation End = TU->getSourceManager().getLocForEndOfFile(MainID); |
| 5665 | return SourceRange(Start, End); |
| 5666 | } |
| 5667 | |
| 5668 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5669 | const Decl *D = cxcursor::getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5670 | if (!D) |
| 5671 | return SourceRange(); |
| 5672 | |
| 5673 | SourceRange R = D->getSourceRange(); |
| 5674 | // FIXME: Multiple variables declared in a single declaration |
| 5675 | // currently lack the information needed to correctly determine their |
| 5676 | // ranges when accounting for the type-specifier. We use context |
| 5677 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 5678 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5679 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5680 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 5681 | R.setBegin(VD->getLocation()); |
| 5682 | } |
| 5683 | return R; |
| 5684 | } |
| 5685 | return SourceRange(); |
| 5686 | } |
| 5687 | |
| 5688 | /// \brief Retrieves the "raw" cursor extent, which is then extended to include |
| 5689 | /// the decl-specifier-seq for declarations. |
| 5690 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) { |
| 5691 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5692 | const Decl *D = cxcursor::getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5693 | if (!D) |
| 5694 | return SourceRange(); |
| 5695 | |
| 5696 | SourceRange R = D->getSourceRange(); |
| 5697 | |
| 5698 | // Adjust the start of the location for declarations preceded by |
| 5699 | // declaration specifiers. |
| 5700 | SourceLocation StartLoc; |
| 5701 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
| 5702 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
| 5703 | StartLoc = TI->getTypeLoc().getLocStart(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5704 | } else if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5705 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
| 5706 | StartLoc = TI->getTypeLoc().getLocStart(); |
| 5707 | } |
| 5708 | |
| 5709 | if (StartLoc.isValid() && R.getBegin().isValid() && |
| 5710 | SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin())) |
| 5711 | R.setBegin(StartLoc); |
| 5712 | |
| 5713 | // FIXME: Multiple variables declared in a single declaration |
| 5714 | // currently lack the information needed to correctly determine their |
| 5715 | // ranges when accounting for the type-specifier. We use context |
| 5716 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 5717 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5718 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5719 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 5720 | R.setBegin(VD->getLocation()); |
| 5721 | } |
| 5722 | |
| 5723 | return R; |
| 5724 | } |
| 5725 | |
| 5726 | return getRawCursorExtent(C); |
| 5727 | } |
| 5728 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5729 | CXSourceRange clang_getCursorExtent(CXCursor C) { |
| 5730 | SourceRange R = getRawCursorExtent(C); |
| 5731 | if (R.isInvalid()) |
| 5732 | return clang_getNullRange(); |
| 5733 | |
| 5734 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 5735 | } |
| 5736 | |
| 5737 | CXCursor clang_getCursorReferenced(CXCursor C) { |
| 5738 | if (clang_isInvalid(C.kind)) |
| 5739 | return clang_getNullCursor(); |
| 5740 | |
| 5741 | CXTranslationUnit tu = getCursorTU(C); |
| 5742 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5743 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5744 | if (!D) |
| 5745 | return clang_getNullCursor(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5746 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5747 | return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5748 | if (const ObjCPropertyImplDecl *PropImpl = |
| 5749 | dyn_cast<ObjCPropertyImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5750 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 5751 | return MakeCXCursor(Property, tu); |
| 5752 | |
| 5753 | return C; |
| 5754 | } |
| 5755 | |
| 5756 | if (clang_isExpression(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5757 | const Expr *E = getCursorExpr(C); |
| 5758 | const Decl *D = getDeclFromExpr(E); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5759 | if (D) { |
| 5760 | CXCursor declCursor = MakeCXCursor(D, tu); |
| 5761 | declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C), |
| 5762 | declCursor); |
| 5763 | return declCursor; |
| 5764 | } |
| 5765 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5766 | if (const OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5767 | return MakeCursorOverloadedDeclRef(Ovl, tu); |
| 5768 | |
| 5769 | return clang_getNullCursor(); |
| 5770 | } |
| 5771 | |
| 5772 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 5773 | const Stmt *S = getCursorStmt(C); |
| 5774 | if (const GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5775 | if (LabelDecl *label = Goto->getLabel()) |
| 5776 | if (LabelStmt *labelS = label->getStmt()) |
| 5777 | return MakeCXCursor(labelS, getCursorDecl(C), tu); |
| 5778 | |
| 5779 | return clang_getNullCursor(); |
| 5780 | } |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 5781 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5782 | if (C.kind == CXCursor_MacroExpansion) { |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 5783 | if (const MacroDefinitionRecord *Def = |
| 5784 | getCursorMacroExpansion(C).getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5785 | return MakeMacroDefinitionCursor(Def, tu); |
| 5786 | } |
| 5787 | |
| 5788 | if (!clang_isReference(C.kind)) |
| 5789 | return clang_getNullCursor(); |
| 5790 | |
| 5791 | switch (C.kind) { |
| 5792 | case CXCursor_ObjCSuperClassRef: |
| 5793 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu); |
| 5794 | |
| 5795 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5796 | const ObjCProtocolDecl *Prot = getCursorObjCProtocolRef(C).first; |
| 5797 | if (const ObjCProtocolDecl *Def = Prot->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5798 | return MakeCXCursor(Def, tu); |
| 5799 | |
| 5800 | return MakeCXCursor(Prot, tu); |
| 5801 | } |
| 5802 | |
| 5803 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5804 | const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
| 5805 | if (const ObjCInterfaceDecl *Def = Class->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5806 | return MakeCXCursor(Def, tu); |
| 5807 | |
| 5808 | return MakeCXCursor(Class, tu); |
| 5809 | } |
| 5810 | |
| 5811 | case CXCursor_TypeRef: |
| 5812 | return MakeCXCursor(getCursorTypeRef(C).first, tu ); |
| 5813 | |
| 5814 | case CXCursor_TemplateRef: |
| 5815 | return MakeCXCursor(getCursorTemplateRef(C).first, tu ); |
| 5816 | |
| 5817 | case CXCursor_NamespaceRef: |
| 5818 | return MakeCXCursor(getCursorNamespaceRef(C).first, tu ); |
| 5819 | |
| 5820 | case CXCursor_MemberRef: |
| 5821 | return MakeCXCursor(getCursorMemberRef(C).first, tu ); |
| 5822 | |
| 5823 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5824 | const CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5825 | return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), |
| 5826 | tu )); |
| 5827 | } |
| 5828 | |
| 5829 | case CXCursor_LabelRef: |
| 5830 | // FIXME: We end up faking the "parent" declaration here because we |
| 5831 | // don't want to make CXCursor larger. |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 5832 | return MakeCXCursor(getCursorLabelRef(C).first, |
| 5833 | cxtu::getASTUnit(tu)->getASTContext() |
| 5834 | .getTranslationUnitDecl(), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5835 | tu); |
| 5836 | |
| 5837 | case CXCursor_OverloadedDeclRef: |
| 5838 | return C; |
| 5839 | |
| 5840 | case CXCursor_VariableRef: |
| 5841 | return MakeCXCursor(getCursorVariableRef(C).first, tu); |
| 5842 | |
| 5843 | default: |
| 5844 | // We would prefer to enumerate all non-reference cursor kinds here. |
| 5845 | llvm_unreachable("Unhandled reference cursor kind"); |
| 5846 | } |
| 5847 | } |
| 5848 | |
| 5849 | CXCursor clang_getCursorDefinition(CXCursor C) { |
| 5850 | if (clang_isInvalid(C.kind)) |
| 5851 | return clang_getNullCursor(); |
| 5852 | |
| 5853 | CXTranslationUnit TU = getCursorTU(C); |
| 5854 | |
| 5855 | bool WasReference = false; |
| 5856 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { |
| 5857 | C = clang_getCursorReferenced(C); |
| 5858 | WasReference = true; |
| 5859 | } |
| 5860 | |
| 5861 | if (C.kind == CXCursor_MacroExpansion) |
| 5862 | return clang_getCursorReferenced(C); |
| 5863 | |
| 5864 | if (!clang_isDeclaration(C.kind)) |
| 5865 | return clang_getNullCursor(); |
| 5866 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5867 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5868 | if (!D) |
| 5869 | return clang_getNullCursor(); |
| 5870 | |
| 5871 | switch (D->getKind()) { |
| 5872 | // Declaration kinds that don't really separate the notions of |
| 5873 | // declaration and definition. |
| 5874 | case Decl::Namespace: |
| 5875 | case Decl::Typedef: |
| 5876 | case Decl::TypeAlias: |
| 5877 | case Decl::TypeAliasTemplate: |
| 5878 | case Decl::TemplateTypeParm: |
| 5879 | case Decl::EnumConstant: |
| 5880 | case Decl::Field: |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 5881 | case Decl::Binding: |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5882 | case Decl::MSProperty: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5883 | case Decl::IndirectField: |
| 5884 | case Decl::ObjCIvar: |
| 5885 | case Decl::ObjCAtDefsField: |
| 5886 | case Decl::ImplicitParam: |
| 5887 | case Decl::ParmVar: |
| 5888 | case Decl::NonTypeTemplateParm: |
| 5889 | case Decl::TemplateTemplateParm: |
| 5890 | case Decl::ObjCCategoryImpl: |
| 5891 | case Decl::ObjCImplementation: |
| 5892 | case Decl::AccessSpec: |
| 5893 | case Decl::LinkageSpec: |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 5894 | case Decl::Export: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5895 | case Decl::ObjCPropertyImpl: |
| 5896 | case Decl::FileScopeAsm: |
| 5897 | case Decl::StaticAssert: |
| 5898 | case Decl::Block: |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 5899 | case Decl::Captured: |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 5900 | case Decl::OMPCapturedExpr: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5901 | case Decl::Label: // FIXME: Is this right?? |
| 5902 | case Decl::ClassScopeFunctionSpecialization: |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 5903 | case Decl::CXXDeductionGuide: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5904 | case Decl::Import: |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 5905 | case Decl::OMPThreadPrivate: |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 5906 | case Decl::OMPDeclareReduction: |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 5907 | case Decl::ObjCTypeParam: |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 5908 | case Decl::BuiltinTemplate: |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 5909 | case Decl::PragmaComment: |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 5910 | case Decl::PragmaDetectMismatch: |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 5911 | case Decl::UsingPack: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5912 | return C; |
| 5913 | |
| 5914 | // Declaration kinds that don't make any sense here, but are |
| 5915 | // nonetheless harmless. |
David Blaikie | f005d3c | 2013-02-22 17:44:58 +0000 | [diff] [blame] | 5916 | case Decl::Empty: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5917 | case Decl::TranslationUnit: |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 5918 | case Decl::ExternCContext: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5919 | break; |
| 5920 | |
| 5921 | // Declaration kinds for which the definition is not resolvable. |
| 5922 | case Decl::UnresolvedUsingTypename: |
| 5923 | case Decl::UnresolvedUsingValue: |
| 5924 | break; |
| 5925 | |
| 5926 | case Decl::UsingDirective: |
| 5927 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), |
| 5928 | TU); |
| 5929 | |
| 5930 | case Decl::NamespaceAlias: |
| 5931 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU); |
| 5932 | |
| 5933 | case Decl::Enum: |
| 5934 | case Decl::Record: |
| 5935 | case Decl::CXXRecord: |
| 5936 | case Decl::ClassTemplateSpecialization: |
| 5937 | case Decl::ClassTemplatePartialSpecialization: |
| 5938 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) |
| 5939 | return MakeCXCursor(Def, TU); |
| 5940 | return clang_getNullCursor(); |
| 5941 | |
| 5942 | case Decl::Function: |
| 5943 | case Decl::CXXMethod: |
| 5944 | case Decl::CXXConstructor: |
| 5945 | case Decl::CXXDestructor: |
| 5946 | case Decl::CXXConversion: { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5947 | const FunctionDecl *Def = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5948 | if (cast<FunctionDecl>(D)->getBody(Def)) |
Dmitri Gribenko | 9c256e3 | 2013-01-14 00:46:27 +0000 | [diff] [blame] | 5949 | return MakeCXCursor(Def, TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5950 | return clang_getNullCursor(); |
| 5951 | } |
| 5952 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5953 | case Decl::Var: |
| 5954 | case Decl::VarTemplateSpecialization: |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 5955 | case Decl::VarTemplatePartialSpecialization: |
| 5956 | case Decl::Decomposition: { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5957 | // Ask the variable if it has a definition. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5958 | if (const VarDecl *Def = cast<VarDecl>(D)->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5959 | return MakeCXCursor(Def, TU); |
| 5960 | return clang_getNullCursor(); |
| 5961 | } |
| 5962 | |
| 5963 | case Decl::FunctionTemplate: { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5964 | const FunctionDecl *Def = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5965 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) |
| 5966 | return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU); |
| 5967 | return clang_getNullCursor(); |
| 5968 | } |
| 5969 | |
| 5970 | case Decl::ClassTemplate: { |
| 5971 | if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() |
| 5972 | ->getDefinition()) |
| 5973 | return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), |
| 5974 | TU); |
| 5975 | return clang_getNullCursor(); |
| 5976 | } |
| 5977 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5978 | case Decl::VarTemplate: { |
| 5979 | if (VarDecl *Def = |
| 5980 | cast<VarTemplateDecl>(D)->getTemplatedDecl()->getDefinition()) |
| 5981 | return MakeCXCursor(cast<VarDecl>(Def)->getDescribedVarTemplate(), TU); |
| 5982 | return clang_getNullCursor(); |
| 5983 | } |
| 5984 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5985 | case Decl::Using: |
| 5986 | return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), |
| 5987 | D->getLocation(), TU); |
| 5988 | |
| 5989 | case Decl::UsingShadow: |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 5990 | case Decl::ConstructorUsingShadow: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5991 | return clang_getCursorDefinition( |
| 5992 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), |
| 5993 | TU)); |
| 5994 | |
| 5995 | case Decl::ObjCMethod: { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5996 | const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5997 | if (Method->isThisDeclarationADefinition()) |
| 5998 | return C; |
| 5999 | |
| 6000 | // Dig out the method definition in the associated |
| 6001 | // @implementation, if we have it. |
| 6002 | // FIXME: The ASTs should make finding the definition easier. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6003 | if (const ObjCInterfaceDecl *Class |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6004 | = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) |
| 6005 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) |
| 6006 | if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), |
| 6007 | Method->isInstanceMethod())) |
| 6008 | if (Def->isThisDeclarationADefinition()) |
| 6009 | return MakeCXCursor(Def, TU); |
| 6010 | |
| 6011 | return clang_getNullCursor(); |
| 6012 | } |
| 6013 | |
| 6014 | case Decl::ObjCCategory: |
| 6015 | if (ObjCCategoryImplDecl *Impl |
| 6016 | = cast<ObjCCategoryDecl>(D)->getImplementation()) |
| 6017 | return MakeCXCursor(Impl, TU); |
| 6018 | return clang_getNullCursor(); |
| 6019 | |
| 6020 | case Decl::ObjCProtocol: |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6021 | if (const ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(D)->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6022 | return MakeCXCursor(Def, TU); |
| 6023 | return clang_getNullCursor(); |
| 6024 | |
| 6025 | case Decl::ObjCInterface: { |
| 6026 | // There are two notions of a "definition" for an Objective-C |
| 6027 | // class: the interface and its implementation. When we resolved a |
| 6028 | // reference to an Objective-C class, produce the @interface as |
| 6029 | // the definition; when we were provided with the interface, |
| 6030 | // produce the @implementation as the definition. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6031 | const ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6032 | if (WasReference) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6033 | if (const ObjCInterfaceDecl *Def = IFace->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6034 | return MakeCXCursor(Def, TU); |
| 6035 | } else if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
| 6036 | return MakeCXCursor(Impl, TU); |
| 6037 | return clang_getNullCursor(); |
| 6038 | } |
| 6039 | |
| 6040 | case Decl::ObjCProperty: |
| 6041 | // FIXME: We don't really know where to find the |
| 6042 | // ObjCPropertyImplDecls that implement this property. |
| 6043 | return clang_getNullCursor(); |
| 6044 | |
| 6045 | case Decl::ObjCCompatibleAlias: |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6046 | if (const ObjCInterfaceDecl *Class |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6047 | = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6048 | if (const ObjCInterfaceDecl *Def = Class->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6049 | return MakeCXCursor(Def, TU); |
| 6050 | |
| 6051 | return clang_getNullCursor(); |
| 6052 | |
| 6053 | case Decl::Friend: |
| 6054 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) |
| 6055 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
| 6056 | return clang_getNullCursor(); |
| 6057 | |
| 6058 | case Decl::FriendTemplate: |
| 6059 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) |
| 6060 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
| 6061 | return clang_getNullCursor(); |
| 6062 | } |
| 6063 | |
| 6064 | return clang_getNullCursor(); |
| 6065 | } |
| 6066 | |
| 6067 | unsigned clang_isCursorDefinition(CXCursor C) { |
| 6068 | if (!clang_isDeclaration(C.kind)) |
| 6069 | return 0; |
| 6070 | |
| 6071 | return clang_getCursorDefinition(C) == C; |
| 6072 | } |
| 6073 | |
| 6074 | CXCursor clang_getCanonicalCursor(CXCursor C) { |
| 6075 | if (!clang_isDeclaration(C.kind)) |
| 6076 | return C; |
| 6077 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6078 | if (const Decl *D = getCursorDecl(C)) { |
| 6079 | if (const ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6080 | if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl()) |
| 6081 | return MakeCXCursor(CatD, getCursorTU(C)); |
| 6082 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6083 | if (const ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) |
| 6084 | if (const ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6085 | return MakeCXCursor(IFD, getCursorTU(C)); |
| 6086 | |
| 6087 | return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C)); |
| 6088 | } |
| 6089 | |
| 6090 | return C; |
| 6091 | } |
| 6092 | |
| 6093 | int clang_Cursor_getObjCSelectorIndex(CXCursor cursor) { |
| 6094 | return cxcursor::getSelectorIdentifierIndexAndLoc(cursor).first; |
| 6095 | } |
| 6096 | |
| 6097 | unsigned clang_getNumOverloadedDecls(CXCursor C) { |
| 6098 | if (C.kind != CXCursor_OverloadedDeclRef) |
| 6099 | return 0; |
| 6100 | |
| 6101 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6102 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6103 | return E->getNumDecls(); |
| 6104 | |
| 6105 | if (OverloadedTemplateStorage *S |
| 6106 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 6107 | return S->size(); |
| 6108 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6109 | const Decl *D = Storage.get<const Decl *>(); |
| 6110 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6111 | return Using->shadow_size(); |
| 6112 | |
| 6113 | return 0; |
| 6114 | } |
| 6115 | |
| 6116 | CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { |
| 6117 | if (cursor.kind != CXCursor_OverloadedDeclRef) |
| 6118 | return clang_getNullCursor(); |
| 6119 | |
| 6120 | if (index >= clang_getNumOverloadedDecls(cursor)) |
| 6121 | return clang_getNullCursor(); |
| 6122 | |
| 6123 | CXTranslationUnit TU = getCursorTU(cursor); |
| 6124 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6125 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6126 | return MakeCXCursor(E->decls_begin()[index], TU); |
| 6127 | |
| 6128 | if (OverloadedTemplateStorage *S |
| 6129 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 6130 | return MakeCXCursor(S->begin()[index], TU); |
| 6131 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6132 | const Decl *D = Storage.get<const Decl *>(); |
| 6133 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6134 | // FIXME: This is, unfortunately, linear time. |
| 6135 | UsingDecl::shadow_iterator Pos = Using->shadow_begin(); |
| 6136 | std::advance(Pos, index); |
| 6137 | return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU); |
| 6138 | } |
| 6139 | |
| 6140 | return clang_getNullCursor(); |
| 6141 | } |
| 6142 | |
| 6143 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
| 6144 | const char **startBuf, |
| 6145 | const char **endBuf, |
| 6146 | unsigned *startLine, |
| 6147 | unsigned *startColumn, |
| 6148 | unsigned *endLine, |
| 6149 | unsigned *endColumn) { |
| 6150 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6151 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(getCursorDecl(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6152 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
| 6153 | |
| 6154 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 6155 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 6156 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 6157 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 6158 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 6159 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 6160 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 6161 | } |
| 6162 | |
| 6163 | |
| 6164 | CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags, |
| 6165 | unsigned PieceIndex) { |
| 6166 | RefNamePieces Pieces; |
| 6167 | |
| 6168 | switch (C.kind) { |
| 6169 | case CXCursor_MemberRefExpr: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6170 | if (const MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6171 | Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(), |
| 6172 | E->getQualifierLoc().getSourceRange()); |
| 6173 | break; |
| 6174 | |
| 6175 | case CXCursor_DeclRefExpr: |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 6176 | if (const DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C))) { |
| 6177 | SourceRange TemplateArgLoc(E->getLAngleLoc(), E->getRAngleLoc()); |
| 6178 | Pieces = |
| 6179 | buildPieces(NameFlags, false, E->getNameInfo(), |
| 6180 | E->getQualifierLoc().getSourceRange(), &TemplateArgLoc); |
| 6181 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6182 | break; |
| 6183 | |
| 6184 | case CXCursor_CallExpr: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6185 | if (const CXXOperatorCallExpr *OCE = |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6186 | dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6187 | const Expr *Callee = OCE->getCallee(); |
| 6188 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6189 | Callee = ICE->getSubExpr(); |
| 6190 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6191 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6192 | Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(), |
| 6193 | DRE->getQualifierLoc().getSourceRange()); |
| 6194 | } |
| 6195 | break; |
| 6196 | |
| 6197 | default: |
| 6198 | break; |
| 6199 | } |
| 6200 | |
| 6201 | if (Pieces.empty()) { |
| 6202 | if (PieceIndex == 0) |
| 6203 | return clang_getCursorExtent(C); |
| 6204 | } else if (PieceIndex < Pieces.size()) { |
| 6205 | SourceRange R = Pieces[PieceIndex]; |
| 6206 | if (R.isValid()) |
| 6207 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 6208 | } |
| 6209 | |
| 6210 | return clang_getNullRange(); |
| 6211 | } |
| 6212 | |
| 6213 | void clang_enableStackTraces(void) { |
Richard Smith | dfed58a | 2016-06-09 00:53:41 +0000 | [diff] [blame] | 6214 | // FIXME: Provide an argv0 here so we can find llvm-symbolizer. |
| 6215 | llvm::sys::PrintStackTraceOnErrorSignal(StringRef()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6216 | } |
| 6217 | |
| 6218 | void clang_executeOnThread(void (*fn)(void*), void *user_data, |
| 6219 | unsigned stack_size) { |
| 6220 | llvm::llvm_execute_on_thread(fn, user_data, stack_size); |
| 6221 | } |
| 6222 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6223 | //===----------------------------------------------------------------------===// |
| 6224 | // Token-based Operations. |
| 6225 | //===----------------------------------------------------------------------===// |
| 6226 | |
| 6227 | /* CXToken layout: |
| 6228 | * int_data[0]: a CXTokenKind |
| 6229 | * int_data[1]: starting token location |
| 6230 | * int_data[2]: token length |
| 6231 | * int_data[3]: reserved |
| 6232 | * ptr_data: for identifiers and keywords, an IdentifierInfo*. |
| 6233 | * otherwise unused. |
| 6234 | */ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6235 | CXTokenKind clang_getTokenKind(CXToken CXTok) { |
| 6236 | return static_cast<CXTokenKind>(CXTok.int_data[0]); |
| 6237 | } |
| 6238 | |
| 6239 | CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { |
| 6240 | switch (clang_getTokenKind(CXTok)) { |
| 6241 | case CXToken_Identifier: |
| 6242 | case CXToken_Keyword: |
| 6243 | // We know we have an IdentifierInfo*, so use that. |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 6244 | return cxstring::createRef(static_cast<IdentifierInfo *>(CXTok.ptr_data) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6245 | ->getNameStart()); |
| 6246 | |
| 6247 | case CXToken_Literal: { |
| 6248 | // We have stashed the starting pointer in the ptr_data field. Use it. |
| 6249 | const char *Text = static_cast<const char *>(CXTok.ptr_data); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 6250 | return cxstring::createDup(StringRef(Text, CXTok.int_data[2])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6251 | } |
| 6252 | |
| 6253 | case CXToken_Punctuation: |
| 6254 | case CXToken_Comment: |
| 6255 | break; |
| 6256 | } |
| 6257 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6258 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6259 | LOG_BAD_TU(TU); |
| 6260 | return cxstring::createEmpty(); |
| 6261 | } |
| 6262 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6263 | // We have to find the starting buffer pointer the hard way, by |
| 6264 | // deconstructing the source location. |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6265 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6266 | if (!CXXUnit) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 6267 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6268 | |
| 6269 | SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); |
| 6270 | std::pair<FileID, unsigned> LocInfo |
| 6271 | = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc); |
| 6272 | bool Invalid = false; |
| 6273 | StringRef Buffer |
| 6274 | = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); |
| 6275 | if (Invalid) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 6276 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6277 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 6278 | return cxstring::createDup(Buffer.substr(LocInfo.second, CXTok.int_data[2])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6279 | } |
| 6280 | |
| 6281 | CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6282 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6283 | LOG_BAD_TU(TU); |
| 6284 | return clang_getNullLocation(); |
| 6285 | } |
| 6286 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6287 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6288 | if (!CXXUnit) |
| 6289 | return clang_getNullLocation(); |
| 6290 | |
| 6291 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), |
| 6292 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 6293 | } |
| 6294 | |
| 6295 | CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6296 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6297 | LOG_BAD_TU(TU); |
| 6298 | return clang_getNullRange(); |
| 6299 | } |
| 6300 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6301 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6302 | if (!CXXUnit) |
| 6303 | return clang_getNullRange(); |
| 6304 | |
| 6305 | return cxloc::translateSourceRange(CXXUnit->getASTContext(), |
| 6306 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 6307 | } |
| 6308 | |
| 6309 | static void getTokens(ASTUnit *CXXUnit, SourceRange Range, |
| 6310 | SmallVectorImpl<CXToken> &CXTokens) { |
| 6311 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 6312 | std::pair<FileID, unsigned> BeginLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6313 | = SourceMgr.getDecomposedSpellingLoc(Range.getBegin()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6314 | std::pair<FileID, unsigned> EndLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6315 | = SourceMgr.getDecomposedSpellingLoc(Range.getEnd()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6316 | |
| 6317 | // Cannot tokenize across files. |
| 6318 | if (BeginLocInfo.first != EndLocInfo.first) |
| 6319 | return; |
| 6320 | |
| 6321 | // Create a lexer |
| 6322 | bool Invalid = false; |
| 6323 | StringRef Buffer |
| 6324 | = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 6325 | if (Invalid) |
| 6326 | return; |
| 6327 | |
| 6328 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 6329 | CXXUnit->getASTContext().getLangOpts(), |
| 6330 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); |
| 6331 | Lex.SetCommentRetentionState(true); |
| 6332 | |
| 6333 | // Lex tokens until we hit the end of the range. |
| 6334 | const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; |
| 6335 | Token Tok; |
| 6336 | bool previousWasAt = false; |
| 6337 | do { |
| 6338 | // Lex the next token |
| 6339 | Lex.LexFromRawLexer(Tok); |
| 6340 | if (Tok.is(tok::eof)) |
| 6341 | break; |
| 6342 | |
| 6343 | // Initialize the CXToken. |
| 6344 | CXToken CXTok; |
| 6345 | |
| 6346 | // - Common fields |
| 6347 | CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); |
| 6348 | CXTok.int_data[2] = Tok.getLength(); |
| 6349 | CXTok.int_data[3] = 0; |
| 6350 | |
| 6351 | // - Kind-specific fields |
| 6352 | if (Tok.isLiteral()) { |
| 6353 | CXTok.int_data[0] = CXToken_Literal; |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 6354 | CXTok.ptr_data = const_cast<char *>(Tok.getLiteralData()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6355 | } else if (Tok.is(tok::raw_identifier)) { |
| 6356 | // Lookup the identifier to determine whether we have a keyword. |
| 6357 | IdentifierInfo *II |
| 6358 | = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok); |
| 6359 | |
| 6360 | if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { |
| 6361 | CXTok.int_data[0] = CXToken_Keyword; |
| 6362 | } |
| 6363 | else { |
| 6364 | CXTok.int_data[0] = Tok.is(tok::identifier) |
| 6365 | ? CXToken_Identifier |
| 6366 | : CXToken_Keyword; |
| 6367 | } |
| 6368 | CXTok.ptr_data = II; |
| 6369 | } else if (Tok.is(tok::comment)) { |
| 6370 | CXTok.int_data[0] = CXToken_Comment; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6371 | CXTok.ptr_data = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6372 | } else { |
| 6373 | CXTok.int_data[0] = CXToken_Punctuation; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6374 | CXTok.ptr_data = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6375 | } |
| 6376 | CXTokens.push_back(CXTok); |
| 6377 | previousWasAt = Tok.is(tok::at); |
Argyrios Kyrtzidis | c7c6a07 | 2016-11-09 23:58:39 +0000 | [diff] [blame] | 6378 | } while (Lex.getBufferLocation() < EffectiveBufferEnd); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6379 | } |
| 6380 | |
| 6381 | void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, |
| 6382 | CXToken **Tokens, unsigned *NumTokens) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 6383 | LOG_FUNC_SECTION { |
| 6384 | *Log << TU << ' ' << Range; |
| 6385 | } |
| 6386 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6387 | if (Tokens) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6388 | *Tokens = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6389 | if (NumTokens) |
| 6390 | *NumTokens = 0; |
| 6391 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6392 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6393 | LOG_BAD_TU(TU); |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 6394 | return; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6395 | } |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 6396 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6397 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6398 | if (!CXXUnit || !Tokens || !NumTokens) |
| 6399 | return; |
| 6400 | |
| 6401 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 6402 | |
| 6403 | SourceRange R = cxloc::translateCXSourceRange(Range); |
| 6404 | if (R.isInvalid()) |
| 6405 | return; |
| 6406 | |
| 6407 | SmallVector<CXToken, 32> CXTokens; |
| 6408 | getTokens(CXXUnit, R, CXTokens); |
| 6409 | |
| 6410 | if (CXTokens.empty()) |
| 6411 | return; |
| 6412 | |
| 6413 | *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size()); |
| 6414 | memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); |
| 6415 | *NumTokens = CXTokens.size(); |
| 6416 | } |
| 6417 | |
| 6418 | void clang_disposeTokens(CXTranslationUnit TU, |
| 6419 | CXToken *Tokens, unsigned NumTokens) { |
| 6420 | free(Tokens); |
| 6421 | } |
| 6422 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6423 | //===----------------------------------------------------------------------===// |
| 6424 | // Token annotation APIs. |
| 6425 | //===----------------------------------------------------------------------===// |
| 6426 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6427 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 6428 | CXCursor parent, |
| 6429 | CXClientData client_data); |
| 6430 | static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor, |
| 6431 | CXClientData client_data); |
| 6432 | |
| 6433 | namespace { |
| 6434 | class AnnotateTokensWorker { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6435 | CXToken *Tokens; |
| 6436 | CXCursor *Cursors; |
| 6437 | unsigned NumTokens; |
| 6438 | unsigned TokIdx; |
| 6439 | unsigned PreprocessingTokIdx; |
| 6440 | CursorVisitor AnnotateVis; |
| 6441 | SourceManager &SrcMgr; |
| 6442 | bool HasContextSensitiveKeywords; |
| 6443 | |
| 6444 | struct PostChildrenInfo { |
| 6445 | CXCursor Cursor; |
| 6446 | SourceRange CursorRange; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6447 | unsigned BeforeReachingCursorIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6448 | unsigned BeforeChildrenTokenIdx; |
| 6449 | }; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6450 | SmallVector<PostChildrenInfo, 8> PostChildrenInfos; |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6451 | |
| 6452 | CXToken &getTok(unsigned Idx) { |
| 6453 | assert(Idx < NumTokens); |
| 6454 | return Tokens[Idx]; |
| 6455 | } |
| 6456 | const CXToken &getTok(unsigned Idx) const { |
| 6457 | assert(Idx < NumTokens); |
| 6458 | return Tokens[Idx]; |
| 6459 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6460 | bool MoreTokens() const { return TokIdx < NumTokens; } |
| 6461 | unsigned NextToken() const { return TokIdx; } |
| 6462 | void AdvanceToken() { ++TokIdx; } |
| 6463 | SourceLocation GetTokenLoc(unsigned tokI) { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6464 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6465 | } |
| 6466 | bool isFunctionMacroToken(unsigned tokI) const { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6467 | return getTok(tokI).int_data[3] != 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6468 | } |
| 6469 | SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6470 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[3]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6471 | } |
| 6472 | |
| 6473 | void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange); |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6474 | bool annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6475 | SourceRange); |
| 6476 | |
| 6477 | public: |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6478 | AnnotateTokensWorker(CXToken *tokens, CXCursor *cursors, unsigned numTokens, |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6479 | CXTranslationUnit TU, SourceRange RegionOfInterest) |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6480 | : Tokens(tokens), Cursors(cursors), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6481 | NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0), |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6482 | AnnotateVis(TU, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6483 | AnnotateTokensVisitor, this, |
| 6484 | /*VisitPreprocessorLast=*/true, |
| 6485 | /*VisitIncludedEntities=*/false, |
| 6486 | RegionOfInterest, |
| 6487 | /*VisitDeclsOnly=*/false, |
| 6488 | AnnotateTokensPostChildrenVisitor), |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6489 | SrcMgr(cxtu::getASTUnit(TU)->getSourceManager()), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6490 | HasContextSensitiveKeywords(false) { } |
| 6491 | |
| 6492 | void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } |
| 6493 | enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); |
| 6494 | bool postVisitChildren(CXCursor cursor); |
| 6495 | void AnnotateTokens(); |
| 6496 | |
| 6497 | /// \brief Determine whether the annotator saw any cursors that have |
| 6498 | /// context-sensitive keywords. |
| 6499 | bool hasContextSensitiveKeywords() const { |
| 6500 | return HasContextSensitiveKeywords; |
| 6501 | } |
| 6502 | |
| 6503 | ~AnnotateTokensWorker() { |
| 6504 | assert(PostChildrenInfos.empty()); |
| 6505 | } |
| 6506 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6507 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6508 | |
| 6509 | void AnnotateTokensWorker::AnnotateTokens() { |
| 6510 | // Walk the AST within the region of interest, annotating tokens |
| 6511 | // along the way. |
| 6512 | AnnotateVis.visitFileRegion(); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6513 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6514 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6515 | static inline void updateCursorAnnotation(CXCursor &Cursor, |
| 6516 | const CXCursor &updateC) { |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6517 | if (clang_isInvalid(updateC.kind) || !clang_isInvalid(Cursor.kind)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6518 | return; |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6519 | Cursor = updateC; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6520 | } |
| 6521 | |
| 6522 | /// \brief It annotates and advances tokens with a cursor until the comparison |
| 6523 | //// between the cursor location and the source range is the same as |
| 6524 | /// \arg compResult. |
| 6525 | /// |
| 6526 | /// Pass RangeBefore to annotate tokens with a cursor until a range is reached. |
| 6527 | /// Pass RangeOverlap to annotate tokens inside a range. |
| 6528 | void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC, |
| 6529 | RangeComparisonResult compResult, |
| 6530 | SourceRange range) { |
| 6531 | while (MoreTokens()) { |
| 6532 | const unsigned I = NextToken(); |
| 6533 | if (isFunctionMacroToken(I)) |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6534 | if (!annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range)) |
| 6535 | return; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6536 | |
| 6537 | SourceLocation TokLoc = GetTokenLoc(I); |
| 6538 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6539 | updateCursorAnnotation(Cursors[I], updateC); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6540 | AdvanceToken(); |
| 6541 | continue; |
| 6542 | } |
| 6543 | break; |
| 6544 | } |
| 6545 | } |
| 6546 | |
| 6547 | /// \brief Special annotation handling for macro argument tokens. |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6548 | /// \returns true if it advanced beyond all macro tokens, false otherwise. |
| 6549 | bool AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens( |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6550 | CXCursor updateC, |
| 6551 | RangeComparisonResult compResult, |
| 6552 | SourceRange range) { |
| 6553 | assert(MoreTokens()); |
| 6554 | assert(isFunctionMacroToken(NextToken()) && |
| 6555 | "Should be called only for macro arg tokens"); |
| 6556 | |
| 6557 | // This works differently than annotateAndAdvanceTokens; because expanded |
| 6558 | // macro arguments can have arbitrary translation-unit source order, we do not |
| 6559 | // advance the token index one by one until a token fails the range test. |
| 6560 | // We only advance once past all of the macro arg tokens if all of them |
| 6561 | // pass the range test. If one of them fails we keep the token index pointing |
| 6562 | // at the start of the macro arg tokens so that the failing token will be |
| 6563 | // annotated by a subsequent annotation try. |
| 6564 | |
| 6565 | bool atLeastOneCompFail = false; |
| 6566 | |
| 6567 | unsigned I = NextToken(); |
| 6568 | for (; I < NumTokens && isFunctionMacroToken(I); ++I) { |
| 6569 | SourceLocation TokLoc = getFunctionMacroTokenLoc(I); |
| 6570 | if (TokLoc.isFileID()) |
| 6571 | continue; // not macro arg token, it's parens or comma. |
| 6572 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 6573 | if (clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 6574 | Cursors[I] = updateC; |
| 6575 | } else |
| 6576 | atLeastOneCompFail = true; |
| 6577 | } |
| 6578 | |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6579 | if (atLeastOneCompFail) |
| 6580 | return false; |
| 6581 | |
| 6582 | TokIdx = I; // All of the tokens were handled, advance beyond all of them. |
| 6583 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6584 | } |
| 6585 | |
| 6586 | enum CXChildVisitResult |
| 6587 | AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6588 | SourceRange cursorRange = getRawCursorExtent(cursor); |
| 6589 | if (cursorRange.isInvalid()) |
| 6590 | return CXChildVisit_Recurse; |
| 6591 | |
| 6592 | if (!HasContextSensitiveKeywords) { |
| 6593 | // Objective-C properties can have context-sensitive keywords. |
| 6594 | if (cursor.kind == CXCursor_ObjCPropertyDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6595 | if (const ObjCPropertyDecl *Property |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6596 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor))) |
| 6597 | HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0; |
| 6598 | } |
| 6599 | // Objective-C methods can have context-sensitive keywords. |
| 6600 | else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl || |
| 6601 | cursor.kind == CXCursor_ObjCClassMethodDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6602 | if (const ObjCMethodDecl *Method |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6603 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 6604 | if (Method->getObjCDeclQualifier()) |
| 6605 | HasContextSensitiveKeywords = true; |
| 6606 | else { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 6607 | for (const auto *P : Method->parameters()) { |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 6608 | if (P->getObjCDeclQualifier()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6609 | HasContextSensitiveKeywords = true; |
| 6610 | break; |
| 6611 | } |
| 6612 | } |
| 6613 | } |
| 6614 | } |
| 6615 | } |
| 6616 | // C++ methods can have context-sensitive keywords. |
| 6617 | else if (cursor.kind == CXCursor_CXXMethod) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6618 | if (const CXXMethodDecl *Method |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6619 | = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) { |
| 6620 | if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>()) |
| 6621 | HasContextSensitiveKeywords = true; |
| 6622 | } |
| 6623 | } |
| 6624 | // C++ classes can have context-sensitive keywords. |
| 6625 | else if (cursor.kind == CXCursor_StructDecl || |
| 6626 | cursor.kind == CXCursor_ClassDecl || |
| 6627 | cursor.kind == CXCursor_ClassTemplate || |
| 6628 | cursor.kind == CXCursor_ClassTemplatePartialSpecialization) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6629 | if (const Decl *D = getCursorDecl(cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6630 | if (D->hasAttr<FinalAttr>()) |
| 6631 | HasContextSensitiveKeywords = true; |
| 6632 | } |
| 6633 | } |
Argyrios Kyrtzidis | 990b386 | 2013-06-04 18:24:30 +0000 | [diff] [blame] | 6634 | |
| 6635 | // Don't override a property annotation with its getter/setter method. |
| 6636 | if (cursor.kind == CXCursor_ObjCInstanceMethodDecl && |
| 6637 | parent.kind == CXCursor_ObjCPropertyDecl) |
| 6638 | return CXChildVisit_Continue; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6639 | |
| 6640 | if (clang_isPreprocessing(cursor.kind)) { |
| 6641 | // Items in the preprocessing record are kept separate from items in |
| 6642 | // declarations, so we keep a separate token index. |
| 6643 | unsigned SavedTokIdx = TokIdx; |
| 6644 | TokIdx = PreprocessingTokIdx; |
| 6645 | |
| 6646 | // Skip tokens up until we catch up to the beginning of the preprocessing |
| 6647 | // entry. |
| 6648 | while (MoreTokens()) { |
| 6649 | const unsigned I = NextToken(); |
| 6650 | SourceLocation TokLoc = GetTokenLoc(I); |
| 6651 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 6652 | case RangeBefore: |
| 6653 | AdvanceToken(); |
| 6654 | continue; |
| 6655 | case RangeAfter: |
| 6656 | case RangeOverlap: |
| 6657 | break; |
| 6658 | } |
| 6659 | break; |
| 6660 | } |
| 6661 | |
| 6662 | // Look at all of the tokens within this range. |
| 6663 | while (MoreTokens()) { |
| 6664 | const unsigned I = NextToken(); |
| 6665 | SourceLocation TokLoc = GetTokenLoc(I); |
| 6666 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 6667 | case RangeBefore: |
| 6668 | llvm_unreachable("Infeasible"); |
| 6669 | case RangeAfter: |
| 6670 | break; |
| 6671 | case RangeOverlap: |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6672 | // For macro expansions, just note where the beginning of the macro |
| 6673 | // expansion occurs. |
| 6674 | if (cursor.kind == CXCursor_MacroExpansion) { |
| 6675 | if (TokLoc == cursorRange.getBegin()) |
| 6676 | Cursors[I] = cursor; |
| 6677 | AdvanceToken(); |
| 6678 | break; |
| 6679 | } |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6680 | // We may have already annotated macro names inside macro definitions. |
| 6681 | if (Cursors[I].kind != CXCursor_MacroExpansion) |
| 6682 | Cursors[I] = cursor; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6683 | AdvanceToken(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6684 | continue; |
| 6685 | } |
| 6686 | break; |
| 6687 | } |
| 6688 | |
| 6689 | // Save the preprocessing token index; restore the non-preprocessing |
| 6690 | // token index. |
| 6691 | PreprocessingTokIdx = TokIdx; |
| 6692 | TokIdx = SavedTokIdx; |
| 6693 | return CXChildVisit_Recurse; |
| 6694 | } |
| 6695 | |
| 6696 | if (cursorRange.isInvalid()) |
| 6697 | return CXChildVisit_Continue; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6698 | |
| 6699 | unsigned BeforeReachingCursorIdx = NextToken(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6700 | const enum CXCursorKind cursorK = clang_getCursorKind(cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6701 | const enum CXCursorKind K = clang_getCursorKind(parent); |
| 6702 | const CXCursor updateC = |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6703 | (clang_isInvalid(K) || K == CXCursor_TranslationUnit || |
| 6704 | // Attributes are annotated out-of-order, skip tokens until we reach it. |
| 6705 | clang_isAttribute(cursor.kind)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6706 | ? clang_getNullCursor() : parent; |
| 6707 | |
| 6708 | annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange); |
| 6709 | |
| 6710 | // Avoid having the cursor of an expression "overwrite" the annotation of the |
| 6711 | // variable declaration that it belongs to. |
| 6712 | // This can happen for C++ constructor expressions whose range generally |
| 6713 | // include the variable declaration, e.g.: |
| 6714 | // 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] | 6715 | if (clang_isExpression(cursorK) && MoreTokens()) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6716 | const Expr *E = getCursorExpr(cursor); |
Dmitri Gribenko | a169118 | 2013-01-26 18:12:08 +0000 | [diff] [blame] | 6717 | if (const Decl *D = getCursorParentDecl(cursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6718 | const unsigned I = NextToken(); |
| 6719 | if (E->getLocStart().isValid() && D->getLocation().isValid() && |
| 6720 | E->getLocStart() == D->getLocation() && |
| 6721 | E->getLocStart() == GetTokenLoc(I)) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6722 | updateCursorAnnotation(Cursors[I], updateC); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6723 | AdvanceToken(); |
| 6724 | } |
| 6725 | } |
| 6726 | } |
| 6727 | |
| 6728 | // Before recursing into the children keep some state that we are going |
| 6729 | // to use in the AnnotateTokensWorker::postVisitChildren callback to do some |
| 6730 | // extra work after the child nodes are visited. |
| 6731 | // Note that we don't call VisitChildren here to avoid traversing statements |
| 6732 | // code-recursively which can blow the stack. |
| 6733 | |
| 6734 | PostChildrenInfo Info; |
| 6735 | Info.Cursor = cursor; |
| 6736 | Info.CursorRange = cursorRange; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6737 | Info.BeforeReachingCursorIdx = BeforeReachingCursorIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6738 | Info.BeforeChildrenTokenIdx = NextToken(); |
| 6739 | PostChildrenInfos.push_back(Info); |
| 6740 | |
| 6741 | return CXChildVisit_Recurse; |
| 6742 | } |
| 6743 | |
| 6744 | bool AnnotateTokensWorker::postVisitChildren(CXCursor cursor) { |
| 6745 | if (PostChildrenInfos.empty()) |
| 6746 | return false; |
| 6747 | const PostChildrenInfo &Info = PostChildrenInfos.back(); |
| 6748 | if (!clang_equalCursors(Info.Cursor, cursor)) |
| 6749 | return false; |
| 6750 | |
| 6751 | const unsigned BeforeChildren = Info.BeforeChildrenTokenIdx; |
| 6752 | const unsigned AfterChildren = NextToken(); |
| 6753 | SourceRange cursorRange = Info.CursorRange; |
| 6754 | |
| 6755 | // Scan the tokens that are at the end of the cursor, but are not captured |
| 6756 | // but the child cursors. |
| 6757 | annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange); |
| 6758 | |
| 6759 | // Scan the tokens that are at the beginning of the cursor, but are not |
| 6760 | // capture by the child cursors. |
| 6761 | for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { |
| 6762 | if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 6763 | break; |
| 6764 | |
| 6765 | Cursors[I] = cursor; |
| 6766 | } |
| 6767 | |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6768 | // Attributes are annotated out-of-order, rewind TokIdx to when we first |
| 6769 | // encountered the attribute cursor. |
| 6770 | if (clang_isAttribute(cursor.kind)) |
| 6771 | TokIdx = Info.BeforeReachingCursorIdx; |
| 6772 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6773 | PostChildrenInfos.pop_back(); |
| 6774 | return false; |
| 6775 | } |
| 6776 | |
| 6777 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 6778 | CXCursor parent, |
| 6779 | CXClientData client_data) { |
| 6780 | return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); |
| 6781 | } |
| 6782 | |
| 6783 | static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor, |
| 6784 | CXClientData client_data) { |
| 6785 | return static_cast<AnnotateTokensWorker*>(client_data)-> |
| 6786 | postVisitChildren(cursor); |
| 6787 | } |
| 6788 | |
| 6789 | namespace { |
| 6790 | |
| 6791 | /// \brief Uses the macro expansions in the preprocessing record to find |
| 6792 | /// and mark tokens that are macro arguments. This info is used by the |
| 6793 | /// AnnotateTokensWorker. |
| 6794 | class MarkMacroArgTokensVisitor { |
| 6795 | SourceManager &SM; |
| 6796 | CXToken *Tokens; |
| 6797 | unsigned NumTokens; |
| 6798 | unsigned CurIdx; |
| 6799 | |
| 6800 | public: |
| 6801 | MarkMacroArgTokensVisitor(SourceManager &SM, |
| 6802 | CXToken *tokens, unsigned numTokens) |
| 6803 | : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { } |
| 6804 | |
| 6805 | CXChildVisitResult visit(CXCursor cursor, CXCursor parent) { |
| 6806 | if (cursor.kind != CXCursor_MacroExpansion) |
| 6807 | return CXChildVisit_Continue; |
| 6808 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 6809 | SourceRange macroRange = getCursorMacroExpansion(cursor).getSourceRange(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6810 | if (macroRange.getBegin() == macroRange.getEnd()) |
| 6811 | return CXChildVisit_Continue; // it's not a function macro. |
| 6812 | |
| 6813 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 6814 | if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx), |
| 6815 | macroRange.getBegin())) |
| 6816 | break; |
| 6817 | } |
| 6818 | |
| 6819 | if (CurIdx == NumTokens) |
| 6820 | return CXChildVisit_Break; |
| 6821 | |
| 6822 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 6823 | SourceLocation tokLoc = getTokenLoc(CurIdx); |
| 6824 | if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd())) |
| 6825 | break; |
| 6826 | |
| 6827 | setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc)); |
| 6828 | } |
| 6829 | |
| 6830 | if (CurIdx == NumTokens) |
| 6831 | return CXChildVisit_Break; |
| 6832 | |
| 6833 | return CXChildVisit_Continue; |
| 6834 | } |
| 6835 | |
| 6836 | private: |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6837 | CXToken &getTok(unsigned Idx) { |
| 6838 | assert(Idx < NumTokens); |
| 6839 | return Tokens[Idx]; |
| 6840 | } |
| 6841 | const CXToken &getTok(unsigned Idx) const { |
| 6842 | assert(Idx < NumTokens); |
| 6843 | return Tokens[Idx]; |
| 6844 | } |
| 6845 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6846 | SourceLocation getTokenLoc(unsigned tokI) { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6847 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6848 | } |
| 6849 | |
| 6850 | void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) { |
| 6851 | // The third field is reserved and currently not used. Use it here |
| 6852 | // to mark macro arg expanded tokens with their expanded locations. |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6853 | getTok(tokI).int_data[3] = loc.getRawEncoding(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6854 | } |
| 6855 | }; |
| 6856 | |
| 6857 | } // end anonymous namespace |
| 6858 | |
| 6859 | static CXChildVisitResult |
| 6860 | MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent, |
| 6861 | CXClientData client_data) { |
| 6862 | return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor, |
| 6863 | parent); |
| 6864 | } |
| 6865 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6866 | /// \brief Used by \c annotatePreprocessorTokens. |
| 6867 | /// \returns true if lexing was finished, false otherwise. |
| 6868 | static bool lexNext(Lexer &Lex, Token &Tok, |
| 6869 | unsigned &NextIdx, unsigned NumTokens) { |
| 6870 | if (NextIdx >= NumTokens) |
| 6871 | return true; |
| 6872 | |
| 6873 | ++NextIdx; |
| 6874 | Lex.LexFromRawLexer(Tok); |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 6875 | return Tok.is(tok::eof); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6876 | } |
| 6877 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6878 | static void annotatePreprocessorTokens(CXTranslationUnit TU, |
| 6879 | SourceRange RegionOfInterest, |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6880 | CXCursor *Cursors, |
| 6881 | CXToken *Tokens, |
| 6882 | unsigned NumTokens) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6883 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6884 | |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6885 | Preprocessor &PP = CXXUnit->getPreprocessor(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6886 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 6887 | std::pair<FileID, unsigned> BeginLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6888 | = SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getBegin()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6889 | std::pair<FileID, unsigned> EndLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6890 | = SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getEnd()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6891 | |
| 6892 | if (BeginLocInfo.first != EndLocInfo.first) |
| 6893 | return; |
| 6894 | |
| 6895 | StringRef Buffer; |
| 6896 | bool Invalid = false; |
| 6897 | Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 6898 | if (Buffer.empty() || Invalid) |
| 6899 | return; |
| 6900 | |
| 6901 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 6902 | CXXUnit->getASTContext().getLangOpts(), |
| 6903 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, |
| 6904 | Buffer.end()); |
| 6905 | Lex.SetCommentRetentionState(true); |
| 6906 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6907 | unsigned NextIdx = 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6908 | // Lex tokens in raw mode until we hit the end of the range, to avoid |
| 6909 | // entering #includes or expanding macros. |
| 6910 | while (true) { |
| 6911 | Token Tok; |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6912 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 6913 | break; |
| 6914 | unsigned TokIdx = NextIdx-1; |
| 6915 | assert(Tok.getLocation() == |
| 6916 | SourceLocation::getFromRawEncoding(Tokens[TokIdx].int_data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6917 | |
| 6918 | reprocess: |
| 6919 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6920 | // We have found a preprocessing directive. Annotate the tokens |
| 6921 | // appropriately. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6922 | // |
| 6923 | // FIXME: Some simple tests here could identify macro definitions and |
| 6924 | // #undefs, to provide specific cursor kinds for those. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6925 | |
| 6926 | SourceLocation BeginLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6927 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 6928 | break; |
| 6929 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6930 | MacroInfo *MI = nullptr; |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 6931 | if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "define") { |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6932 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 6933 | break; |
| 6934 | |
| 6935 | if (Tok.is(tok::raw_identifier)) { |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 6936 | IdentifierInfo &II = |
| 6937 | PP.getIdentifierTable().get(Tok.getRawIdentifier()); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6938 | SourceLocation MappedTokLoc = |
| 6939 | CXXUnit->mapLocationToPreamble(Tok.getLocation()); |
| 6940 | MI = getMacroInfo(II, MappedTokLoc, TU); |
| 6941 | } |
| 6942 | } |
| 6943 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6944 | bool finished = false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6945 | do { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6946 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) { |
| 6947 | finished = true; |
| 6948 | break; |
| 6949 | } |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6950 | // If we are in a macro definition, check if the token was ever a |
| 6951 | // macro name and annotate it if that's the case. |
| 6952 | if (MI) { |
| 6953 | SourceLocation SaveLoc = Tok.getLocation(); |
| 6954 | Tok.setLocation(CXXUnit->mapLocationToPreamble(SaveLoc)); |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 6955 | MacroDefinitionRecord *MacroDef = |
| 6956 | checkForMacroInMacroDefinition(MI, Tok, TU); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6957 | Tok.setLocation(SaveLoc); |
| 6958 | if (MacroDef) |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 6959 | Cursors[NextIdx - 1] = |
| 6960 | MakeMacroExpansionCursor(MacroDef, Tok.getLocation(), TU); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6961 | } |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6962 | } while (!Tok.isAtStartOfLine()); |
| 6963 | |
| 6964 | unsigned LastIdx = finished ? NextIdx-1 : NextIdx-2; |
| 6965 | assert(TokIdx <= LastIdx); |
| 6966 | SourceLocation EndLoc = |
| 6967 | SourceLocation::getFromRawEncoding(Tokens[LastIdx].int_data[1]); |
| 6968 | CXCursor Cursor = |
| 6969 | MakePreprocessingDirectiveCursor(SourceRange(BeginLoc, EndLoc), TU); |
| 6970 | |
| 6971 | for (; TokIdx <= LastIdx; ++TokIdx) |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6972 | updateCursorAnnotation(Cursors[TokIdx], Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6973 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6974 | if (finished) |
| 6975 | break; |
| 6976 | goto reprocess; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6977 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6978 | } |
| 6979 | } |
| 6980 | |
| 6981 | // This gets run a separate thread to avoid stack blowout. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 6982 | static void clang_annotateTokensImpl(CXTranslationUnit TU, ASTUnit *CXXUnit, |
| 6983 | CXToken *Tokens, unsigned NumTokens, |
| 6984 | CXCursor *Cursors) { |
Dmitri Gribenko | 183436e | 2013-01-26 21:49:50 +0000 | [diff] [blame] | 6985 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6986 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) |
| 6987 | setThreadBackgroundPriority(); |
| 6988 | |
| 6989 | // Determine the region of interest, which contains all of the tokens. |
| 6990 | SourceRange RegionOfInterest; |
| 6991 | RegionOfInterest.setBegin( |
| 6992 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0]))); |
| 6993 | RegionOfInterest.setEnd( |
| 6994 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, |
| 6995 | Tokens[NumTokens-1]))); |
| 6996 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6997 | // Relex the tokens within the source range to look for preprocessing |
| 6998 | // directives. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6999 | annotatePreprocessorTokens(TU, RegionOfInterest, Cursors, Tokens, NumTokens); |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 7000 | |
| 7001 | // If begin location points inside a macro argument, set it to the expansion |
| 7002 | // location so we can have the full context when annotating semantically. |
| 7003 | { |
| 7004 | SourceManager &SM = CXXUnit->getSourceManager(); |
| 7005 | SourceLocation Loc = |
| 7006 | SM.getMacroArgExpandedLocation(RegionOfInterest.getBegin()); |
| 7007 | if (Loc.isMacroID()) |
| 7008 | RegionOfInterest.setBegin(SM.getExpansionLoc(Loc)); |
| 7009 | } |
| 7010 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7011 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) { |
| 7012 | // Search and mark tokens that are macro argument expansions. |
| 7013 | MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(), |
| 7014 | Tokens, NumTokens); |
| 7015 | CursorVisitor MacroArgMarker(TU, |
| 7016 | MarkMacroArgTokensVisitorDelegate, &Visitor, |
| 7017 | /*VisitPreprocessorLast=*/true, |
| 7018 | /*VisitIncludedEntities=*/false, |
| 7019 | RegionOfInterest); |
| 7020 | MacroArgMarker.visitPreprocessedEntitiesInRegion(); |
| 7021 | } |
| 7022 | |
| 7023 | // Annotate all of the source locations in the region of interest that map to |
| 7024 | // a specific cursor. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7025 | AnnotateTokensWorker W(Tokens, Cursors, NumTokens, TU, RegionOfInterest); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7026 | |
| 7027 | // FIXME: We use a ridiculous stack size here because the data-recursion |
| 7028 | // algorithm uses a large stack frame than the non-data recursive version, |
| 7029 | // and AnnotationTokensWorker currently transforms the data-recursion |
| 7030 | // algorithm back into a traditional recursion by explicitly calling |
| 7031 | // VisitChildren(). We will need to remove this explicit recursive call. |
| 7032 | W.AnnotateTokens(); |
| 7033 | |
| 7034 | // If we ran into any entities that involve context-sensitive keywords, |
| 7035 | // take another pass through the tokens to mark them as such. |
| 7036 | if (W.hasContextSensitiveKeywords()) { |
| 7037 | for (unsigned I = 0; I != NumTokens; ++I) { |
| 7038 | if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier) |
| 7039 | continue; |
| 7040 | |
| 7041 | if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) { |
| 7042 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7043 | if (const ObjCPropertyDecl *Property |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7044 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) { |
| 7045 | if (Property->getPropertyAttributesAsWritten() != 0 && |
| 7046 | llvm::StringSwitch<bool>(II->getName()) |
| 7047 | .Case("readonly", true) |
| 7048 | .Case("assign", true) |
| 7049 | .Case("unsafe_unretained", true) |
| 7050 | .Case("readwrite", true) |
| 7051 | .Case("retain", true) |
| 7052 | .Case("copy", true) |
| 7053 | .Case("nonatomic", true) |
| 7054 | .Case("atomic", true) |
| 7055 | .Case("getter", true) |
| 7056 | .Case("setter", true) |
| 7057 | .Case("strong", true) |
| 7058 | .Case("weak", true) |
Manman Ren | 04fd4d8 | 2016-05-31 23:22:04 +0000 | [diff] [blame] | 7059 | .Case("class", true) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7060 | .Default(false)) |
| 7061 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 7062 | } |
| 7063 | continue; |
| 7064 | } |
| 7065 | |
| 7066 | if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl || |
| 7067 | Cursors[I].kind == CXCursor_ObjCClassMethodDecl) { |
| 7068 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 7069 | if (llvm::StringSwitch<bool>(II->getName()) |
| 7070 | .Case("in", true) |
| 7071 | .Case("out", true) |
| 7072 | .Case("inout", true) |
| 7073 | .Case("oneway", true) |
| 7074 | .Case("bycopy", true) |
| 7075 | .Case("byref", true) |
| 7076 | .Default(false)) |
| 7077 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 7078 | continue; |
| 7079 | } |
| 7080 | |
| 7081 | if (Cursors[I].kind == CXCursor_CXXFinalAttr || |
| 7082 | Cursors[I].kind == CXCursor_CXXOverrideAttr) { |
| 7083 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 7084 | continue; |
| 7085 | } |
| 7086 | } |
| 7087 | } |
| 7088 | } |
| 7089 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7090 | void clang_annotateTokens(CXTranslationUnit TU, |
| 7091 | CXToken *Tokens, unsigned NumTokens, |
| 7092 | CXCursor *Cursors) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7093 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7094 | LOG_BAD_TU(TU); |
| 7095 | return; |
| 7096 | } |
| 7097 | if (NumTokens == 0 || !Tokens || !Cursors) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7098 | LOG_FUNC_SECTION { *Log << "<null input>"; } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7099 | return; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7100 | } |
| 7101 | |
| 7102 | LOG_FUNC_SECTION { |
| 7103 | *Log << TU << ' '; |
| 7104 | CXSourceLocation bloc = clang_getTokenLocation(TU, Tokens[0]); |
| 7105 | CXSourceLocation eloc = clang_getTokenLocation(TU, Tokens[NumTokens-1]); |
| 7106 | *Log << clang_getRange(bloc, eloc); |
| 7107 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7108 | |
| 7109 | // Any token we don't specifically annotate will have a NULL cursor. |
| 7110 | CXCursor C = clang_getNullCursor(); |
| 7111 | for (unsigned I = 0; I != NumTokens; ++I) |
| 7112 | Cursors[I] = C; |
| 7113 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7114 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7115 | if (!CXXUnit) |
| 7116 | return; |
| 7117 | |
| 7118 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 7119 | |
| 7120 | auto AnnotateTokensImpl = [=]() { |
| 7121 | clang_annotateTokensImpl(TU, CXXUnit, Tokens, NumTokens, Cursors); |
| 7122 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7123 | llvm::CrashRecoveryContext CRC; |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 7124 | if (!RunSafely(CRC, AnnotateTokensImpl, GetSafetyThreadStackSize() * 2)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7125 | fprintf(stderr, "libclang: crash detected while annotating tokens\n"); |
| 7126 | } |
| 7127 | } |
| 7128 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7129 | //===----------------------------------------------------------------------===// |
| 7130 | // Operations for querying linkage of a cursor. |
| 7131 | //===----------------------------------------------------------------------===// |
| 7132 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7133 | CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { |
| 7134 | if (!clang_isDeclaration(cursor.kind)) |
| 7135 | return CXLinkage_Invalid; |
| 7136 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7137 | const Decl *D = cxcursor::getCursorDecl(cursor); |
| 7138 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7139 | switch (ND->getLinkageInternal()) { |
Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 7140 | case NoLinkage: |
| 7141 | case VisibleNoLinkage: return CXLinkage_NoLinkage; |
Richard Smith | af10ea2 | 2017-07-08 00:37:59 +0000 | [diff] [blame] | 7142 | case ModuleInternalLinkage: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7143 | case InternalLinkage: return CXLinkage_Internal; |
| 7144 | case UniqueExternalLinkage: return CXLinkage_UniqueExternal; |
Richard Smith | af10ea2 | 2017-07-08 00:37:59 +0000 | [diff] [blame] | 7145 | case ModuleLinkage: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7146 | case ExternalLinkage: return CXLinkage_External; |
| 7147 | }; |
| 7148 | |
| 7149 | return CXLinkage_Invalid; |
| 7150 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7151 | |
| 7152 | //===----------------------------------------------------------------------===// |
Ehsan Akhgari | b743de7 | 2016-05-31 15:55:51 +0000 | [diff] [blame] | 7153 | // Operations for querying visibility of a cursor. |
| 7154 | //===----------------------------------------------------------------------===// |
| 7155 | |
Ehsan Akhgari | b743de7 | 2016-05-31 15:55:51 +0000 | [diff] [blame] | 7156 | CXVisibilityKind clang_getCursorVisibility(CXCursor cursor) { |
| 7157 | if (!clang_isDeclaration(cursor.kind)) |
| 7158 | return CXVisibility_Invalid; |
| 7159 | |
| 7160 | const Decl *D = cxcursor::getCursorDecl(cursor); |
| 7161 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
| 7162 | switch (ND->getVisibility()) { |
| 7163 | case HiddenVisibility: return CXVisibility_Hidden; |
| 7164 | case ProtectedVisibility: return CXVisibility_Protected; |
| 7165 | case DefaultVisibility: return CXVisibility_Default; |
| 7166 | }; |
| 7167 | |
| 7168 | return CXVisibility_Invalid; |
| 7169 | } |
Ehsan Akhgari | b743de7 | 2016-05-31 15:55:51 +0000 | [diff] [blame] | 7170 | |
| 7171 | //===----------------------------------------------------------------------===// |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7172 | // Operations for querying language of a cursor. |
| 7173 | //===----------------------------------------------------------------------===// |
| 7174 | |
| 7175 | static CXLanguageKind getDeclLanguage(const Decl *D) { |
| 7176 | if (!D) |
| 7177 | return CXLanguage_C; |
| 7178 | |
| 7179 | switch (D->getKind()) { |
| 7180 | default: |
| 7181 | break; |
| 7182 | case Decl::ImplicitParam: |
| 7183 | case Decl::ObjCAtDefsField: |
| 7184 | case Decl::ObjCCategory: |
| 7185 | case Decl::ObjCCategoryImpl: |
| 7186 | case Decl::ObjCCompatibleAlias: |
| 7187 | case Decl::ObjCImplementation: |
| 7188 | case Decl::ObjCInterface: |
| 7189 | case Decl::ObjCIvar: |
| 7190 | case Decl::ObjCMethod: |
| 7191 | case Decl::ObjCProperty: |
| 7192 | case Decl::ObjCPropertyImpl: |
| 7193 | case Decl::ObjCProtocol: |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 7194 | case Decl::ObjCTypeParam: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7195 | return CXLanguage_ObjC; |
| 7196 | case Decl::CXXConstructor: |
| 7197 | case Decl::CXXConversion: |
| 7198 | case Decl::CXXDestructor: |
| 7199 | case Decl::CXXMethod: |
| 7200 | case Decl::CXXRecord: |
| 7201 | case Decl::ClassTemplate: |
| 7202 | case Decl::ClassTemplatePartialSpecialization: |
| 7203 | case Decl::ClassTemplateSpecialization: |
| 7204 | case Decl::Friend: |
| 7205 | case Decl::FriendTemplate: |
| 7206 | case Decl::FunctionTemplate: |
| 7207 | case Decl::LinkageSpec: |
| 7208 | case Decl::Namespace: |
| 7209 | case Decl::NamespaceAlias: |
| 7210 | case Decl::NonTypeTemplateParm: |
| 7211 | case Decl::StaticAssert: |
| 7212 | case Decl::TemplateTemplateParm: |
| 7213 | case Decl::TemplateTypeParm: |
| 7214 | case Decl::UnresolvedUsingTypename: |
| 7215 | case Decl::UnresolvedUsingValue: |
| 7216 | case Decl::Using: |
| 7217 | case Decl::UsingDirective: |
| 7218 | case Decl::UsingShadow: |
| 7219 | return CXLanguage_CPlusPlus; |
| 7220 | } |
| 7221 | |
| 7222 | return CXLanguage_C; |
| 7223 | } |
| 7224 | |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7225 | static CXAvailabilityKind getCursorAvailabilityForDecl(const Decl *D) { |
| 7226 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()) |
Manuel Klimek | 8e3a7ed | 2015-09-25 17:53:16 +0000 | [diff] [blame] | 7227 | return CXAvailability_NotAvailable; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7228 | |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7229 | switch (D->getAvailability()) { |
| 7230 | case AR_Available: |
| 7231 | case AR_NotYetIntroduced: |
| 7232 | if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(D)) |
Benjamin Kramer | 656363d | 2013-10-15 18:53:18 +0000 | [diff] [blame] | 7233 | return getCursorAvailabilityForDecl( |
| 7234 | cast<Decl>(EnumConst->getDeclContext())); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7235 | return CXAvailability_Available; |
| 7236 | |
| 7237 | case AR_Deprecated: |
| 7238 | return CXAvailability_Deprecated; |
| 7239 | |
| 7240 | case AR_Unavailable: |
| 7241 | return CXAvailability_NotAvailable; |
| 7242 | } |
Benjamin Kramer | 656363d | 2013-10-15 18:53:18 +0000 | [diff] [blame] | 7243 | |
| 7244 | llvm_unreachable("Unknown availability kind!"); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7245 | } |
| 7246 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7247 | enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { |
| 7248 | if (clang_isDeclaration(cursor.kind)) |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7249 | if (const Decl *D = cxcursor::getCursorDecl(cursor)) |
| 7250 | return getCursorAvailabilityForDecl(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7251 | |
| 7252 | return CXAvailability_Available; |
| 7253 | } |
| 7254 | |
| 7255 | static CXVersion convertVersion(VersionTuple In) { |
| 7256 | CXVersion Out = { -1, -1, -1 }; |
| 7257 | if (In.empty()) |
| 7258 | return Out; |
| 7259 | |
| 7260 | Out.Major = In.getMajor(); |
| 7261 | |
NAKAMURA Takumi | c2b5d1f | 2013-02-21 02:32:34 +0000 | [diff] [blame] | 7262 | Optional<unsigned> Minor = In.getMinor(); |
| 7263 | if (Minor.hasValue()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7264 | Out.Minor = *Minor; |
| 7265 | else |
| 7266 | return Out; |
| 7267 | |
NAKAMURA Takumi | c2b5d1f | 2013-02-21 02:32:34 +0000 | [diff] [blame] | 7268 | Optional<unsigned> Subminor = In.getSubminor(); |
| 7269 | if (Subminor.hasValue()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7270 | Out.Subminor = *Subminor; |
| 7271 | |
| 7272 | return Out; |
| 7273 | } |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7274 | |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7275 | static void getCursorPlatformAvailabilityForDecl( |
| 7276 | const Decl *D, int *always_deprecated, CXString *deprecated_message, |
| 7277 | int *always_unavailable, CXString *unavailable_message, |
| 7278 | SmallVectorImpl<AvailabilityAttr *> &AvailabilityAttrs) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7279 | bool HadAvailAttr = false; |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 7280 | for (auto A : D->attrs()) { |
| 7281 | if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(A)) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7282 | HadAvailAttr = true; |
| 7283 | if (always_deprecated) |
| 7284 | *always_deprecated = 1; |
Nico Weber | aacf031 | 2014-04-24 05:16:45 +0000 | [diff] [blame] | 7285 | if (deprecated_message) { |
Argyrios Kyrtzidis | edfe07f | 2014-04-24 06:05:40 +0000 | [diff] [blame] | 7286 | clang_disposeString(*deprecated_message); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7287 | *deprecated_message = cxstring::createDup(Deprecated->getMessage()); |
Nico Weber | aacf031 | 2014-04-24 05:16:45 +0000 | [diff] [blame] | 7288 | } |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7289 | continue; |
| 7290 | } |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7291 | |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 7292 | if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(A)) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7293 | HadAvailAttr = true; |
| 7294 | if (always_unavailable) |
| 7295 | *always_unavailable = 1; |
| 7296 | if (unavailable_message) { |
Argyrios Kyrtzidis | edfe07f | 2014-04-24 06:05:40 +0000 | [diff] [blame] | 7297 | clang_disposeString(*unavailable_message); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7298 | *unavailable_message = cxstring::createDup(Unavailable->getMessage()); |
| 7299 | } |
| 7300 | continue; |
| 7301 | } |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7302 | |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 7303 | if (AvailabilityAttr *Avail = dyn_cast<AvailabilityAttr>(A)) { |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7304 | AvailabilityAttrs.push_back(Avail); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7305 | HadAvailAttr = true; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7306 | } |
| 7307 | } |
| 7308 | |
| 7309 | if (!HadAvailAttr) |
| 7310 | if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(D)) |
| 7311 | return getCursorPlatformAvailabilityForDecl( |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7312 | cast<Decl>(EnumConst->getDeclContext()), always_deprecated, |
| 7313 | deprecated_message, always_unavailable, unavailable_message, |
| 7314 | AvailabilityAttrs); |
| 7315 | |
| 7316 | if (AvailabilityAttrs.empty()) |
| 7317 | return; |
| 7318 | |
| 7319 | std::sort(AvailabilityAttrs.begin(), AvailabilityAttrs.end(), |
| 7320 | [](AvailabilityAttr *LHS, AvailabilityAttr *RHS) { |
Reid Kleckner | e6cde14 | 2017-08-04 21:52:25 +0000 | [diff] [blame] | 7321 | return LHS->getPlatform()->getName() < |
| 7322 | RHS->getPlatform()->getName(); |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7323 | }); |
| 7324 | ASTContext &Ctx = D->getASTContext(); |
| 7325 | auto It = std::unique( |
| 7326 | AvailabilityAttrs.begin(), AvailabilityAttrs.end(), |
| 7327 | [&Ctx](AvailabilityAttr *LHS, AvailabilityAttr *RHS) { |
| 7328 | if (LHS->getPlatform() != RHS->getPlatform()) |
| 7329 | return false; |
| 7330 | |
| 7331 | if (LHS->getIntroduced() == RHS->getIntroduced() && |
| 7332 | LHS->getDeprecated() == RHS->getDeprecated() && |
| 7333 | LHS->getObsoleted() == RHS->getObsoleted() && |
| 7334 | LHS->getMessage() == RHS->getMessage() && |
| 7335 | LHS->getReplacement() == RHS->getReplacement()) |
| 7336 | return true; |
| 7337 | |
| 7338 | if ((!LHS->getIntroduced().empty() && !RHS->getIntroduced().empty()) || |
| 7339 | (!LHS->getDeprecated().empty() && !RHS->getDeprecated().empty()) || |
| 7340 | (!LHS->getObsoleted().empty() && !RHS->getObsoleted().empty())) |
| 7341 | return false; |
| 7342 | |
| 7343 | if (LHS->getIntroduced().empty() && !RHS->getIntroduced().empty()) |
| 7344 | LHS->setIntroduced(Ctx, RHS->getIntroduced()); |
| 7345 | |
| 7346 | if (LHS->getDeprecated().empty() && !RHS->getDeprecated().empty()) { |
| 7347 | LHS->setDeprecated(Ctx, RHS->getDeprecated()); |
| 7348 | if (LHS->getMessage().empty()) |
| 7349 | LHS->setMessage(Ctx, RHS->getMessage()); |
| 7350 | if (LHS->getReplacement().empty()) |
| 7351 | LHS->setReplacement(Ctx, RHS->getReplacement()); |
| 7352 | } |
| 7353 | |
| 7354 | if (LHS->getObsoleted().empty() && !RHS->getObsoleted().empty()) { |
| 7355 | LHS->setObsoleted(Ctx, RHS->getObsoleted()); |
| 7356 | if (LHS->getMessage().empty()) |
| 7357 | LHS->setMessage(Ctx, RHS->getMessage()); |
| 7358 | if (LHS->getReplacement().empty()) |
| 7359 | LHS->setReplacement(Ctx, RHS->getReplacement()); |
| 7360 | } |
| 7361 | |
| 7362 | return true; |
| 7363 | }); |
| 7364 | AvailabilityAttrs.erase(It, AvailabilityAttrs.end()); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7365 | } |
| 7366 | |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7367 | int clang_getCursorPlatformAvailability(CXCursor cursor, int *always_deprecated, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7368 | CXString *deprecated_message, |
| 7369 | int *always_unavailable, |
| 7370 | CXString *unavailable_message, |
| 7371 | CXPlatformAvailability *availability, |
| 7372 | int availability_size) { |
| 7373 | if (always_deprecated) |
| 7374 | *always_deprecated = 0; |
| 7375 | if (deprecated_message) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 7376 | *deprecated_message = cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7377 | if (always_unavailable) |
| 7378 | *always_unavailable = 0; |
| 7379 | if (unavailable_message) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 7380 | *unavailable_message = cxstring::createEmpty(); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7381 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7382 | if (!clang_isDeclaration(cursor.kind)) |
| 7383 | return 0; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7384 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7385 | const Decl *D = cxcursor::getCursorDecl(cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7386 | if (!D) |
| 7387 | return 0; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7388 | |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7389 | SmallVector<AvailabilityAttr *, 8> AvailabilityAttrs; |
| 7390 | getCursorPlatformAvailabilityForDecl(D, always_deprecated, deprecated_message, |
| 7391 | always_unavailable, unavailable_message, |
| 7392 | AvailabilityAttrs); |
| 7393 | for (const auto &Avail : |
| 7394 | llvm::enumerate(llvm::makeArrayRef(AvailabilityAttrs) |
| 7395 | .take_front(availability_size))) { |
| 7396 | availability[Avail.index()].Platform = |
| 7397 | cxstring::createDup(Avail.value()->getPlatform()->getName()); |
| 7398 | availability[Avail.index()].Introduced = |
| 7399 | convertVersion(Avail.value()->getIntroduced()); |
| 7400 | availability[Avail.index()].Deprecated = |
| 7401 | convertVersion(Avail.value()->getDeprecated()); |
| 7402 | availability[Avail.index()].Obsoleted = |
| 7403 | convertVersion(Avail.value()->getObsoleted()); |
| 7404 | availability[Avail.index()].Unavailable = Avail.value()->getUnavailable(); |
| 7405 | availability[Avail.index()].Message = |
| 7406 | cxstring::createDup(Avail.value()->getMessage()); |
| 7407 | } |
| 7408 | |
| 7409 | return AvailabilityAttrs.size(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7410 | } |
Alex Lorenz | 1345ea2 | 2017-06-12 19:06:30 +0000 | [diff] [blame] | 7411 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7412 | void clang_disposeCXPlatformAvailability(CXPlatformAvailability *availability) { |
| 7413 | clang_disposeString(availability->Platform); |
| 7414 | clang_disposeString(availability->Message); |
| 7415 | } |
| 7416 | |
| 7417 | CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { |
| 7418 | if (clang_isDeclaration(cursor.kind)) |
| 7419 | return getDeclLanguage(cxcursor::getCursorDecl(cursor)); |
| 7420 | |
| 7421 | return CXLanguage_Invalid; |
| 7422 | } |
| 7423 | |
Saleem Abdulrasool | 50bc565 | 2017-09-13 02:15:09 +0000 | [diff] [blame] | 7424 | CXTLSKind clang_getCursorTLSKind(CXCursor cursor) { |
| 7425 | const Decl *D = cxcursor::getCursorDecl(cursor); |
| 7426 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7427 | switch (VD->getTLSKind()) { |
| 7428 | case VarDecl::TLS_None: |
| 7429 | return CXTLS_None; |
| 7430 | case VarDecl::TLS_Dynamic: |
| 7431 | return CXTLS_Dynamic; |
| 7432 | case VarDecl::TLS_Static: |
| 7433 | return CXTLS_Static; |
| 7434 | } |
| 7435 | } |
| 7436 | |
| 7437 | return CXTLS_None; |
| 7438 | } |
| 7439 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7440 | /// \brief If the given cursor is the "templated" declaration |
| 7441 | /// descibing a class or function template, return the class or |
| 7442 | /// function template. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7443 | static const Decl *maybeGetTemplateCursor(const Decl *D) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7444 | if (!D) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7445 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7446 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7447 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7448 | if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate()) |
| 7449 | return FunTmpl; |
| 7450 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7451 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7452 | if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate()) |
| 7453 | return ClassTmpl; |
| 7454 | |
| 7455 | return D; |
| 7456 | } |
| 7457 | |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 7458 | |
| 7459 | enum CX_StorageClass clang_Cursor_getStorageClass(CXCursor C) { |
| 7460 | StorageClass sc = SC_None; |
| 7461 | const Decl *D = getCursorDecl(C); |
| 7462 | if (D) { |
| 7463 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7464 | sc = FD->getStorageClass(); |
| 7465 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7466 | sc = VD->getStorageClass(); |
| 7467 | } else { |
| 7468 | return CX_SC_Invalid; |
| 7469 | } |
| 7470 | } else { |
| 7471 | return CX_SC_Invalid; |
| 7472 | } |
| 7473 | switch (sc) { |
| 7474 | case SC_None: |
| 7475 | return CX_SC_None; |
| 7476 | case SC_Extern: |
| 7477 | return CX_SC_Extern; |
| 7478 | case SC_Static: |
| 7479 | return CX_SC_Static; |
| 7480 | case SC_PrivateExtern: |
| 7481 | return CX_SC_PrivateExtern; |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 7482 | case SC_Auto: |
| 7483 | return CX_SC_Auto; |
| 7484 | case SC_Register: |
| 7485 | return CX_SC_Register; |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 7486 | } |
Kaelyn Takata | ab61e70 | 2014-10-15 18:03:26 +0000 | [diff] [blame] | 7487 | llvm_unreachable("Unhandled storage class!"); |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 7488 | } |
| 7489 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7490 | CXCursor clang_getCursorSemanticParent(CXCursor cursor) { |
| 7491 | if (clang_isDeclaration(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7492 | if (const Decl *D = getCursorDecl(cursor)) { |
| 7493 | const DeclContext *DC = D->getDeclContext(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7494 | if (!DC) |
| 7495 | return clang_getNullCursor(); |
| 7496 | |
| 7497 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 7498 | getCursorTU(cursor)); |
| 7499 | } |
| 7500 | } |
| 7501 | |
| 7502 | if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7503 | if (const Decl *D = getCursorDecl(cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7504 | return MakeCXCursor(D, getCursorTU(cursor)); |
| 7505 | } |
| 7506 | |
| 7507 | return clang_getNullCursor(); |
| 7508 | } |
| 7509 | |
| 7510 | CXCursor clang_getCursorLexicalParent(CXCursor cursor) { |
| 7511 | if (clang_isDeclaration(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7512 | if (const Decl *D = getCursorDecl(cursor)) { |
| 7513 | const DeclContext *DC = D->getLexicalDeclContext(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7514 | if (!DC) |
| 7515 | return clang_getNullCursor(); |
| 7516 | |
| 7517 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 7518 | getCursorTU(cursor)); |
| 7519 | } |
| 7520 | } |
| 7521 | |
| 7522 | // FIXME: Note that we can't easily compute the lexical context of a |
| 7523 | // statement or expression, so we return nothing. |
| 7524 | return clang_getNullCursor(); |
| 7525 | } |
| 7526 | |
| 7527 | CXFile clang_getIncludedFile(CXCursor cursor) { |
| 7528 | if (cursor.kind != CXCursor_InclusionDirective) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7529 | return nullptr; |
| 7530 | |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 7531 | const InclusionDirective *ID = getCursorInclusionDirective(cursor); |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 7532 | return const_cast<FileEntry *>(ID->getFile()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7533 | } |
| 7534 | |
Argyrios Kyrtzidis | 9adfd8a | 2013-04-18 22:15:49 +0000 | [diff] [blame] | 7535 | unsigned clang_Cursor_getObjCPropertyAttributes(CXCursor C, unsigned reserved) { |
| 7536 | if (C.kind != CXCursor_ObjCPropertyDecl) |
| 7537 | return CXObjCPropertyAttr_noattr; |
| 7538 | |
| 7539 | unsigned Result = CXObjCPropertyAttr_noattr; |
| 7540 | const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(getCursorDecl(C)); |
| 7541 | ObjCPropertyDecl::PropertyAttributeKind Attr = |
| 7542 | PD->getPropertyAttributesAsWritten(); |
| 7543 | |
| 7544 | #define SET_CXOBJCPROP_ATTR(A) \ |
| 7545 | if (Attr & ObjCPropertyDecl::OBJC_PR_##A) \ |
| 7546 | Result |= CXObjCPropertyAttr_##A |
| 7547 | SET_CXOBJCPROP_ATTR(readonly); |
| 7548 | SET_CXOBJCPROP_ATTR(getter); |
| 7549 | SET_CXOBJCPROP_ATTR(assign); |
| 7550 | SET_CXOBJCPROP_ATTR(readwrite); |
| 7551 | SET_CXOBJCPROP_ATTR(retain); |
| 7552 | SET_CXOBJCPROP_ATTR(copy); |
| 7553 | SET_CXOBJCPROP_ATTR(nonatomic); |
| 7554 | SET_CXOBJCPROP_ATTR(setter); |
| 7555 | SET_CXOBJCPROP_ATTR(atomic); |
| 7556 | SET_CXOBJCPROP_ATTR(weak); |
| 7557 | SET_CXOBJCPROP_ATTR(strong); |
| 7558 | SET_CXOBJCPROP_ATTR(unsafe_unretained); |
Manman Ren | 04fd4d8 | 2016-05-31 23:22:04 +0000 | [diff] [blame] | 7559 | SET_CXOBJCPROP_ATTR(class); |
Argyrios Kyrtzidis | 9adfd8a | 2013-04-18 22:15:49 +0000 | [diff] [blame] | 7560 | #undef SET_CXOBJCPROP_ATTR |
| 7561 | |
| 7562 | return Result; |
| 7563 | } |
| 7564 | |
Argyrios Kyrtzidis | 9d9bc01 | 2013-04-18 23:29:12 +0000 | [diff] [blame] | 7565 | unsigned clang_Cursor_getObjCDeclQualifiers(CXCursor C) { |
| 7566 | if (!clang_isDeclaration(C.kind)) |
| 7567 | return CXObjCDeclQualifier_None; |
| 7568 | |
| 7569 | Decl::ObjCDeclQualifier QT = Decl::OBJC_TQ_None; |
| 7570 | const Decl *D = getCursorDecl(C); |
| 7571 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 7572 | QT = MD->getObjCDeclQualifier(); |
| 7573 | else if (const ParmVarDecl *PD = dyn_cast<ParmVarDecl>(D)) |
| 7574 | QT = PD->getObjCDeclQualifier(); |
| 7575 | if (QT == Decl::OBJC_TQ_None) |
| 7576 | return CXObjCDeclQualifier_None; |
| 7577 | |
| 7578 | unsigned Result = CXObjCDeclQualifier_None; |
| 7579 | if (QT & Decl::OBJC_TQ_In) Result |= CXObjCDeclQualifier_In; |
| 7580 | if (QT & Decl::OBJC_TQ_Inout) Result |= CXObjCDeclQualifier_Inout; |
| 7581 | if (QT & Decl::OBJC_TQ_Out) Result |= CXObjCDeclQualifier_Out; |
| 7582 | if (QT & Decl::OBJC_TQ_Bycopy) Result |= CXObjCDeclQualifier_Bycopy; |
| 7583 | if (QT & Decl::OBJC_TQ_Byref) Result |= CXObjCDeclQualifier_Byref; |
| 7584 | if (QT & Decl::OBJC_TQ_Oneway) Result |= CXObjCDeclQualifier_Oneway; |
| 7585 | |
| 7586 | return Result; |
| 7587 | } |
| 7588 | |
Argyrios Kyrtzidis | 7b50fc5 | 2013-07-05 20:44:37 +0000 | [diff] [blame] | 7589 | unsigned clang_Cursor_isObjCOptional(CXCursor C) { |
| 7590 | if (!clang_isDeclaration(C.kind)) |
| 7591 | return 0; |
| 7592 | |
| 7593 | const Decl *D = getCursorDecl(C); |
| 7594 | if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 7595 | return PD->getPropertyImplementation() == ObjCPropertyDecl::Optional; |
| 7596 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 7597 | return MD->getImplementationControl() == ObjCMethodDecl::Optional; |
| 7598 | |
| 7599 | return 0; |
| 7600 | } |
| 7601 | |
Argyrios Kyrtzidis | 23814e4 | 2013-04-18 23:53:05 +0000 | [diff] [blame] | 7602 | unsigned clang_Cursor_isVariadic(CXCursor C) { |
| 7603 | if (!clang_isDeclaration(C.kind)) |
| 7604 | return 0; |
| 7605 | |
| 7606 | const Decl *D = getCursorDecl(C); |
| 7607 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 7608 | return FD->isVariadic(); |
| 7609 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 7610 | return MD->isVariadic(); |
| 7611 | |
| 7612 | return 0; |
| 7613 | } |
| 7614 | |
Argyrios Kyrtzidis | 0381cc7 | 2017-05-10 15:10:36 +0000 | [diff] [blame] | 7615 | unsigned clang_Cursor_isExternalSymbol(CXCursor C, |
| 7616 | CXString *language, CXString *definedIn, |
| 7617 | unsigned *isGenerated) { |
| 7618 | if (!clang_isDeclaration(C.kind)) |
| 7619 | return 0; |
| 7620 | |
| 7621 | const Decl *D = getCursorDecl(C); |
| 7622 | |
Argyrios Kyrtzidis | 11d7048 | 2017-05-20 04:11:33 +0000 | [diff] [blame] | 7623 | if (auto *attr = D->getExternalSourceSymbolAttr()) { |
Argyrios Kyrtzidis | 0381cc7 | 2017-05-10 15:10:36 +0000 | [diff] [blame] | 7624 | if (language) |
| 7625 | *language = cxstring::createDup(attr->getLanguage()); |
| 7626 | if (definedIn) |
| 7627 | *definedIn = cxstring::createDup(attr->getDefinedIn()); |
| 7628 | if (isGenerated) |
| 7629 | *isGenerated = attr->getGeneratedDeclaration(); |
| 7630 | return 1; |
| 7631 | } |
| 7632 | return 0; |
| 7633 | } |
| 7634 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7635 | CXSourceRange clang_Cursor_getCommentRange(CXCursor C) { |
| 7636 | if (!clang_isDeclaration(C.kind)) |
| 7637 | return clang_getNullRange(); |
| 7638 | |
| 7639 | const Decl *D = getCursorDecl(C); |
| 7640 | ASTContext &Context = getCursorContext(C); |
| 7641 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 7642 | if (!RC) |
| 7643 | return clang_getNullRange(); |
| 7644 | |
| 7645 | return cxloc::translateSourceRange(Context, RC->getSourceRange()); |
| 7646 | } |
| 7647 | |
| 7648 | CXString clang_Cursor_getRawCommentText(CXCursor C) { |
| 7649 | if (!clang_isDeclaration(C.kind)) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 7650 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7651 | |
| 7652 | const Decl *D = getCursorDecl(C); |
| 7653 | ASTContext &Context = getCursorContext(C); |
| 7654 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 7655 | StringRef RawText = RC ? RC->getRawText(Context.getSourceManager()) : |
| 7656 | StringRef(); |
| 7657 | |
| 7658 | // Don't duplicate the string because RawText points directly into source |
| 7659 | // code. |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 7660 | return cxstring::createRef(RawText); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7661 | } |
| 7662 | |
| 7663 | CXString clang_Cursor_getBriefCommentText(CXCursor C) { |
| 7664 | if (!clang_isDeclaration(C.kind)) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 7665 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7666 | |
| 7667 | const Decl *D = getCursorDecl(C); |
| 7668 | const ASTContext &Context = getCursorContext(C); |
| 7669 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 7670 | |
| 7671 | if (RC) { |
| 7672 | StringRef BriefText = RC->getBriefText(Context); |
| 7673 | |
| 7674 | // Don't duplicate the string because RawComment ensures that this memory |
| 7675 | // will not go away. |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 7676 | return cxstring::createRef(BriefText); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7677 | } |
| 7678 | |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 7679 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7680 | } |
| 7681 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7682 | CXModule clang_Cursor_getModule(CXCursor C) { |
| 7683 | if (C.kind == CXCursor_ModuleImportDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7684 | if (const ImportDecl *ImportD = |
| 7685 | dyn_cast_or_null<ImportDecl>(getCursorDecl(C))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7686 | return ImportD->getImportedModule(); |
| 7687 | } |
| 7688 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7689 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7690 | } |
| 7691 | |
Argyrios Kyrtzidis | f6d49c3 | 2014-05-14 23:14:37 +0000 | [diff] [blame] | 7692 | CXModule clang_getModuleForFile(CXTranslationUnit TU, CXFile File) { |
| 7693 | if (isNotUsableTU(TU)) { |
| 7694 | LOG_BAD_TU(TU); |
| 7695 | return nullptr; |
| 7696 | } |
| 7697 | if (!File) |
| 7698 | return nullptr; |
| 7699 | FileEntry *FE = static_cast<FileEntry *>(File); |
| 7700 | |
| 7701 | ASTUnit &Unit = *cxtu::getASTUnit(TU); |
| 7702 | HeaderSearch &HS = Unit.getPreprocessor().getHeaderSearchInfo(); |
| 7703 | ModuleMap::KnownHeader Header = HS.findModuleForHeader(FE); |
| 7704 | |
Richard Smith | feb54b6 | 2014-10-23 02:01:19 +0000 | [diff] [blame] | 7705 | return Header.getModule(); |
Argyrios Kyrtzidis | f6d49c3 | 2014-05-14 23:14:37 +0000 | [diff] [blame] | 7706 | } |
| 7707 | |
Argyrios Kyrtzidis | 12fdb9e | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 7708 | CXFile clang_Module_getASTFile(CXModule CXMod) { |
| 7709 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7710 | return nullptr; |
Argyrios Kyrtzidis | 12fdb9e | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 7711 | Module *Mod = static_cast<Module*>(CXMod); |
| 7712 | return const_cast<FileEntry *>(Mod->getASTFile()); |
| 7713 | } |
| 7714 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7715 | CXModule clang_Module_getParent(CXModule CXMod) { |
| 7716 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7717 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7718 | Module *Mod = static_cast<Module*>(CXMod); |
| 7719 | return Mod->Parent; |
| 7720 | } |
| 7721 | |
| 7722 | CXString clang_Module_getName(CXModule CXMod) { |
| 7723 | if (!CXMod) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 7724 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7725 | Module *Mod = static_cast<Module*>(CXMod); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 7726 | return cxstring::createDup(Mod->Name); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7727 | } |
| 7728 | |
| 7729 | CXString clang_Module_getFullName(CXModule CXMod) { |
| 7730 | if (!CXMod) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 7731 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7732 | Module *Mod = static_cast<Module*>(CXMod); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 7733 | return cxstring::createDup(Mod->getFullModuleName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7734 | } |
| 7735 | |
Argyrios Kyrtzidis | 884337f | 2014-05-15 04:44:25 +0000 | [diff] [blame] | 7736 | int clang_Module_isSystem(CXModule CXMod) { |
| 7737 | if (!CXMod) |
| 7738 | return 0; |
| 7739 | Module *Mod = static_cast<Module*>(CXMod); |
| 7740 | return Mod->IsSystem; |
| 7741 | } |
| 7742 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7743 | unsigned clang_Module_getNumTopLevelHeaders(CXTranslationUnit TU, |
| 7744 | CXModule CXMod) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7745 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7746 | LOG_BAD_TU(TU); |
| 7747 | return 0; |
| 7748 | } |
| 7749 | if (!CXMod) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7750 | return 0; |
| 7751 | Module *Mod = static_cast<Module*>(CXMod); |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7752 | FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager(); |
| 7753 | ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr); |
| 7754 | return TopHeaders.size(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7755 | } |
| 7756 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7757 | CXFile clang_Module_getTopLevelHeader(CXTranslationUnit TU, |
| 7758 | CXModule CXMod, unsigned Index) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7759 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7760 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7761 | return nullptr; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7762 | } |
| 7763 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7764 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7765 | Module *Mod = static_cast<Module*>(CXMod); |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7766 | FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7767 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7768 | ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr); |
| 7769 | if (Index < TopHeaders.size()) |
| 7770 | return const_cast<FileEntry *>(TopHeaders[Index]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7771 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7772 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7773 | } |
| 7774 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7775 | //===----------------------------------------------------------------------===// |
| 7776 | // C++ AST instrospection. |
| 7777 | //===----------------------------------------------------------------------===// |
| 7778 | |
Jonathan Coe | 2956535 | 2016-04-27 12:48:25 +0000 | [diff] [blame] | 7779 | unsigned clang_CXXConstructor_isDefaultConstructor(CXCursor C) { |
| 7780 | if (!clang_isDeclaration(C.kind)) |
| 7781 | return 0; |
| 7782 | |
| 7783 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7784 | const CXXConstructorDecl *Constructor = |
| 7785 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 7786 | return (Constructor && Constructor->isDefaultConstructor()) ? 1 : 0; |
| 7787 | } |
| 7788 | |
| 7789 | unsigned clang_CXXConstructor_isCopyConstructor(CXCursor C) { |
| 7790 | if (!clang_isDeclaration(C.kind)) |
| 7791 | return 0; |
| 7792 | |
| 7793 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7794 | const CXXConstructorDecl *Constructor = |
| 7795 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 7796 | return (Constructor && Constructor->isCopyConstructor()) ? 1 : 0; |
| 7797 | } |
| 7798 | |
| 7799 | unsigned clang_CXXConstructor_isMoveConstructor(CXCursor C) { |
| 7800 | if (!clang_isDeclaration(C.kind)) |
| 7801 | return 0; |
| 7802 | |
| 7803 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7804 | const CXXConstructorDecl *Constructor = |
| 7805 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 7806 | return (Constructor && Constructor->isMoveConstructor()) ? 1 : 0; |
| 7807 | } |
| 7808 | |
| 7809 | unsigned clang_CXXConstructor_isConvertingConstructor(CXCursor C) { |
| 7810 | if (!clang_isDeclaration(C.kind)) |
| 7811 | return 0; |
| 7812 | |
| 7813 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7814 | const CXXConstructorDecl *Constructor = |
| 7815 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 7816 | // Passing 'false' excludes constructors marked 'explicit'. |
| 7817 | return (Constructor && Constructor->isConvertingConstructor(false)) ? 1 : 0; |
| 7818 | } |
| 7819 | |
Saleem Abdulrasool | 6ea75db | 2015-10-27 15:50:22 +0000 | [diff] [blame] | 7820 | unsigned clang_CXXField_isMutable(CXCursor C) { |
| 7821 | if (!clang_isDeclaration(C.kind)) |
| 7822 | return 0; |
| 7823 | |
| 7824 | if (const auto D = cxcursor::getCursorDecl(C)) |
| 7825 | if (const auto FD = dyn_cast_or_null<FieldDecl>(D)) |
| 7826 | return FD->isMutable() ? 1 : 0; |
| 7827 | return 0; |
| 7828 | } |
| 7829 | |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 7830 | unsigned clang_CXXMethod_isPureVirtual(CXCursor C) { |
| 7831 | if (!clang_isDeclaration(C.kind)) |
| 7832 | return 0; |
| 7833 | |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 7834 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 7835 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7836 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 7837 | return (Method && Method->isVirtual() && Method->isPure()) ? 1 : 0; |
| 7838 | } |
| 7839 | |
Dmitri Gribenko | e570ede | 2014-04-07 14:59:13 +0000 | [diff] [blame] | 7840 | unsigned clang_CXXMethod_isConst(CXCursor C) { |
| 7841 | if (!clang_isDeclaration(C.kind)) |
| 7842 | return 0; |
| 7843 | |
| 7844 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7845 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7846 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Dmitri Gribenko | e570ede | 2014-04-07 14:59:13 +0000 | [diff] [blame] | 7847 | return (Method && (Method->getTypeQualifiers() & Qualifiers::Const)) ? 1 : 0; |
| 7848 | } |
| 7849 | |
Jonathan Coe | 2956535 | 2016-04-27 12:48:25 +0000 | [diff] [blame] | 7850 | unsigned clang_CXXMethod_isDefaulted(CXCursor C) { |
| 7851 | if (!clang_isDeclaration(C.kind)) |
| 7852 | return 0; |
| 7853 | |
| 7854 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7855 | const CXXMethodDecl *Method = |
| 7856 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
| 7857 | return (Method && Method->isDefaulted()) ? 1 : 0; |
| 7858 | } |
| 7859 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7860 | unsigned clang_CXXMethod_isStatic(CXCursor C) { |
| 7861 | if (!clang_isDeclaration(C.kind)) |
| 7862 | return 0; |
| 7863 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7864 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 7865 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7866 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7867 | return (Method && Method->isStatic()) ? 1 : 0; |
| 7868 | } |
| 7869 | |
| 7870 | unsigned clang_CXXMethod_isVirtual(CXCursor C) { |
| 7871 | if (!clang_isDeclaration(C.kind)) |
| 7872 | return 0; |
| 7873 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7874 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 7875 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7876 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7877 | return (Method && Method->isVirtual()) ? 1 : 0; |
| 7878 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7879 | |
Alex Lorenz | ff7f42e | 2017-07-12 11:35:11 +0000 | [diff] [blame] | 7880 | unsigned clang_EnumDecl_isScoped(CXCursor C) { |
| 7881 | if (!clang_isDeclaration(C.kind)) |
| 7882 | return 0; |
| 7883 | |
| 7884 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7885 | auto *Enum = dyn_cast_or_null<EnumDecl>(D); |
| 7886 | return (Enum && Enum->isScoped()) ? 1 : 0; |
| 7887 | } |
| 7888 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7889 | //===----------------------------------------------------------------------===// |
| 7890 | // Attribute introspection. |
| 7891 | //===----------------------------------------------------------------------===// |
| 7892 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7893 | CXType clang_getIBOutletCollectionType(CXCursor C) { |
| 7894 | if (C.kind != CXCursor_IBOutletCollectionAttr) |
| 7895 | return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C)); |
| 7896 | |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 7897 | const IBOutletCollectionAttr *A = |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7898 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); |
| 7899 | |
| 7900 | return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C)); |
| 7901 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7902 | |
| 7903 | //===----------------------------------------------------------------------===// |
| 7904 | // Inspecting memory usage. |
| 7905 | //===----------------------------------------------------------------------===// |
| 7906 | |
| 7907 | typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries; |
| 7908 | |
| 7909 | static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries, |
| 7910 | enum CXTUResourceUsageKind k, |
| 7911 | unsigned long amount) { |
| 7912 | CXTUResourceUsageEntry entry = { k, amount }; |
| 7913 | entries.push_back(entry); |
| 7914 | } |
| 7915 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7916 | const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) { |
| 7917 | const char *str = ""; |
| 7918 | switch (kind) { |
| 7919 | case CXTUResourceUsage_AST: |
| 7920 | str = "ASTContext: expressions, declarations, and types"; |
| 7921 | break; |
| 7922 | case CXTUResourceUsage_Identifiers: |
| 7923 | str = "ASTContext: identifiers"; |
| 7924 | break; |
| 7925 | case CXTUResourceUsage_Selectors: |
| 7926 | str = "ASTContext: selectors"; |
| 7927 | break; |
| 7928 | case CXTUResourceUsage_GlobalCompletionResults: |
| 7929 | str = "Code completion: cached global results"; |
| 7930 | break; |
| 7931 | case CXTUResourceUsage_SourceManagerContentCache: |
| 7932 | str = "SourceManager: content cache allocator"; |
| 7933 | break; |
| 7934 | case CXTUResourceUsage_AST_SideTables: |
| 7935 | str = "ASTContext: side tables"; |
| 7936 | break; |
| 7937 | case CXTUResourceUsage_SourceManager_Membuffer_Malloc: |
| 7938 | str = "SourceManager: malloc'ed memory buffers"; |
| 7939 | break; |
| 7940 | case CXTUResourceUsage_SourceManager_Membuffer_MMap: |
| 7941 | str = "SourceManager: mmap'ed memory buffers"; |
| 7942 | break; |
| 7943 | case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc: |
| 7944 | str = "ExternalASTSource: malloc'ed memory buffers"; |
| 7945 | break; |
| 7946 | case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap: |
| 7947 | str = "ExternalASTSource: mmap'ed memory buffers"; |
| 7948 | break; |
| 7949 | case CXTUResourceUsage_Preprocessor: |
| 7950 | str = "Preprocessor: malloc'ed memory"; |
| 7951 | break; |
| 7952 | case CXTUResourceUsage_PreprocessingRecord: |
| 7953 | str = "Preprocessor: PreprocessingRecord"; |
| 7954 | break; |
| 7955 | case CXTUResourceUsage_SourceManager_DataStructures: |
| 7956 | str = "SourceManager: data structures and tables"; |
| 7957 | break; |
| 7958 | case CXTUResourceUsage_Preprocessor_HeaderSearch: |
| 7959 | str = "Preprocessor: header search tables"; |
| 7960 | break; |
| 7961 | } |
| 7962 | return str; |
| 7963 | } |
| 7964 | |
| 7965 | CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7966 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7967 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7968 | CXTUResourceUsage usage = { (void*) nullptr, 0, nullptr }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7969 | return usage; |
| 7970 | } |
| 7971 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7972 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 7973 | std::unique_ptr<MemUsageEntries> entries(new MemUsageEntries()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7974 | ASTContext &astContext = astUnit->getASTContext(); |
| 7975 | |
| 7976 | // How much memory is used by AST nodes and types? |
| 7977 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST, |
| 7978 | (unsigned long) astContext.getASTAllocatedMemory()); |
| 7979 | |
| 7980 | // How much memory is used by identifiers? |
| 7981 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers, |
| 7982 | (unsigned long) astContext.Idents.getAllocator().getTotalMemory()); |
| 7983 | |
| 7984 | // How much memory is used for selectors? |
| 7985 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors, |
| 7986 | (unsigned long) astContext.Selectors.getTotalMemory()); |
| 7987 | |
| 7988 | // How much memory is used by ASTContext's side tables? |
| 7989 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables, |
| 7990 | (unsigned long) astContext.getSideTableAllocatedMemory()); |
| 7991 | |
| 7992 | // How much memory is used for caching global code completion results? |
| 7993 | unsigned long completionBytes = 0; |
| 7994 | if (GlobalCodeCompletionAllocator *completionAllocator = |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 7995 | astUnit->getCachedCompletionAllocator().get()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7996 | completionBytes = completionAllocator->getTotalMemory(); |
| 7997 | } |
| 7998 | createCXTUResourceUsageEntry(*entries, |
| 7999 | CXTUResourceUsage_GlobalCompletionResults, |
| 8000 | completionBytes); |
| 8001 | |
| 8002 | // How much memory is being used by SourceManager's content cache? |
| 8003 | createCXTUResourceUsageEntry(*entries, |
| 8004 | CXTUResourceUsage_SourceManagerContentCache, |
| 8005 | (unsigned long) astContext.getSourceManager().getContentCacheSize()); |
| 8006 | |
| 8007 | // How much memory is being used by the MemoryBuffer's in SourceManager? |
| 8008 | const SourceManager::MemoryBufferSizes &srcBufs = |
| 8009 | astUnit->getSourceManager().getMemoryBufferSizes(); |
| 8010 | |
| 8011 | createCXTUResourceUsageEntry(*entries, |
| 8012 | CXTUResourceUsage_SourceManager_Membuffer_Malloc, |
| 8013 | (unsigned long) srcBufs.malloc_bytes); |
| 8014 | createCXTUResourceUsageEntry(*entries, |
| 8015 | CXTUResourceUsage_SourceManager_Membuffer_MMap, |
| 8016 | (unsigned long) srcBufs.mmap_bytes); |
| 8017 | createCXTUResourceUsageEntry(*entries, |
| 8018 | CXTUResourceUsage_SourceManager_DataStructures, |
| 8019 | (unsigned long) astContext.getSourceManager() |
| 8020 | .getDataStructureSizes()); |
| 8021 | |
| 8022 | // How much memory is being used by the ExternalASTSource? |
| 8023 | if (ExternalASTSource *esrc = astContext.getExternalSource()) { |
| 8024 | const ExternalASTSource::MemoryBufferSizes &sizes = |
| 8025 | esrc->getMemoryBufferSizes(); |
| 8026 | |
| 8027 | createCXTUResourceUsageEntry(*entries, |
| 8028 | CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc, |
| 8029 | (unsigned long) sizes.malloc_bytes); |
| 8030 | createCXTUResourceUsageEntry(*entries, |
| 8031 | CXTUResourceUsage_ExternalASTSource_Membuffer_MMap, |
| 8032 | (unsigned long) sizes.mmap_bytes); |
| 8033 | } |
| 8034 | |
| 8035 | // How much memory is being used by the Preprocessor? |
| 8036 | Preprocessor &pp = astUnit->getPreprocessor(); |
| 8037 | createCXTUResourceUsageEntry(*entries, |
| 8038 | CXTUResourceUsage_Preprocessor, |
| 8039 | pp.getTotalMemory()); |
| 8040 | |
| 8041 | if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) { |
| 8042 | createCXTUResourceUsageEntry(*entries, |
| 8043 | CXTUResourceUsage_PreprocessingRecord, |
| 8044 | pRec->getTotalMemory()); |
| 8045 | } |
| 8046 | |
| 8047 | createCXTUResourceUsageEntry(*entries, |
| 8048 | CXTUResourceUsage_Preprocessor_HeaderSearch, |
| 8049 | pp.getHeaderSearchInfo().getTotalMemory()); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8050 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8051 | CXTUResourceUsage usage = { (void*) entries.get(), |
| 8052 | (unsigned) entries->size(), |
Alexander Kornienko | 6ee521c | 2015-01-23 15:36:10 +0000 | [diff] [blame] | 8053 | !entries->empty() ? &(*entries)[0] : nullptr }; |
Eric Fiselier | e95fc44 | 2016-11-14 07:03:50 +0000 | [diff] [blame] | 8054 | (void)entries.release(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8055 | return usage; |
| 8056 | } |
| 8057 | |
| 8058 | void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) { |
| 8059 | if (usage.data) |
| 8060 | delete (MemUsageEntries*) usage.data; |
| 8061 | } |
| 8062 | |
Argyrios Kyrtzidis | 0e282ef | 2013-12-06 18:55:45 +0000 | [diff] [blame] | 8063 | CXSourceRangeList *clang_getSkippedRanges(CXTranslationUnit TU, CXFile file) { |
| 8064 | CXSourceRangeList *skipped = new CXSourceRangeList; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8065 | skipped->count = 0; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8066 | skipped->ranges = nullptr; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8067 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 8068 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 8069 | LOG_BAD_TU(TU); |
| 8070 | return skipped; |
| 8071 | } |
| 8072 | |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8073 | if (!file) |
| 8074 | return skipped; |
| 8075 | |
| 8076 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
| 8077 | PreprocessingRecord *ppRec = astUnit->getPreprocessor().getPreprocessingRecord(); |
| 8078 | if (!ppRec) |
| 8079 | return skipped; |
| 8080 | |
| 8081 | ASTContext &Ctx = astUnit->getASTContext(); |
| 8082 | SourceManager &sm = Ctx.getSourceManager(); |
| 8083 | FileEntry *fileEntry = static_cast<FileEntry *>(file); |
| 8084 | FileID wantedFileID = sm.translateFile(fileEntry); |
| 8085 | |
| 8086 | const std::vector<SourceRange> &SkippedRanges = ppRec->getSkippedRanges(); |
| 8087 | std::vector<SourceRange> wantedRanges; |
| 8088 | for (std::vector<SourceRange>::const_iterator i = SkippedRanges.begin(), ei = SkippedRanges.end(); |
| 8089 | i != ei; ++i) { |
| 8090 | if (sm.getFileID(i->getBegin()) == wantedFileID || sm.getFileID(i->getEnd()) == wantedFileID) |
| 8091 | wantedRanges.push_back(*i); |
| 8092 | } |
| 8093 | |
| 8094 | skipped->count = wantedRanges.size(); |
| 8095 | skipped->ranges = new CXSourceRange[skipped->count]; |
| 8096 | for (unsigned i = 0, ei = skipped->count; i != ei; ++i) |
| 8097 | skipped->ranges[i] = cxloc::translateSourceRange(Ctx, wantedRanges[i]); |
| 8098 | |
| 8099 | return skipped; |
| 8100 | } |
| 8101 | |
Cameron Desrochers | d809128 | 2016-08-18 15:43:55 +0000 | [diff] [blame] | 8102 | CXSourceRangeList *clang_getAllSkippedRanges(CXTranslationUnit TU) { |
| 8103 | CXSourceRangeList *skipped = new CXSourceRangeList; |
| 8104 | skipped->count = 0; |
| 8105 | skipped->ranges = nullptr; |
| 8106 | |
| 8107 | if (isNotUsableTU(TU)) { |
| 8108 | LOG_BAD_TU(TU); |
| 8109 | return skipped; |
| 8110 | } |
| 8111 | |
| 8112 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
| 8113 | PreprocessingRecord *ppRec = astUnit->getPreprocessor().getPreprocessingRecord(); |
| 8114 | if (!ppRec) |
| 8115 | return skipped; |
| 8116 | |
| 8117 | ASTContext &Ctx = astUnit->getASTContext(); |
| 8118 | |
| 8119 | const std::vector<SourceRange> &SkippedRanges = ppRec->getSkippedRanges(); |
| 8120 | |
| 8121 | skipped->count = SkippedRanges.size(); |
| 8122 | skipped->ranges = new CXSourceRange[skipped->count]; |
| 8123 | for (unsigned i = 0, ei = skipped->count; i != ei; ++i) |
| 8124 | skipped->ranges[i] = cxloc::translateSourceRange(Ctx, SkippedRanges[i]); |
| 8125 | |
| 8126 | return skipped; |
| 8127 | } |
| 8128 | |
Argyrios Kyrtzidis | 0e282ef | 2013-12-06 18:55:45 +0000 | [diff] [blame] | 8129 | void clang_disposeSourceRangeList(CXSourceRangeList *ranges) { |
| 8130 | if (ranges) { |
| 8131 | delete[] ranges->ranges; |
| 8132 | delete ranges; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 8133 | } |
| 8134 | } |
| 8135 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8136 | void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) { |
| 8137 | CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU); |
| 8138 | for (unsigned I = 0; I != Usage.numEntries; ++I) |
| 8139 | fprintf(stderr, " %s: %lu\n", |
| 8140 | clang_getTUResourceUsageName(Usage.entries[I].kind), |
| 8141 | Usage.entries[I].amount); |
| 8142 | |
| 8143 | clang_disposeCXTUResourceUsage(Usage); |
| 8144 | } |
| 8145 | |
| 8146 | //===----------------------------------------------------------------------===// |
| 8147 | // Misc. utility functions. |
| 8148 | //===----------------------------------------------------------------------===// |
| 8149 | |
| 8150 | /// Default to using an 8 MB stack size on "safety" threads. |
| 8151 | static unsigned SafetyStackThreadSize = 8 << 20; |
| 8152 | |
| 8153 | namespace clang { |
| 8154 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 8155 | bool RunSafely(llvm::CrashRecoveryContext &CRC, llvm::function_ref<void()> Fn, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8156 | unsigned Size) { |
| 8157 | if (!Size) |
| 8158 | Size = GetSafetyThreadStackSize(); |
Erik Verbruggen | 3cc3911 | 2017-11-14 09:34:39 +0000 | [diff] [blame] | 8159 | if (Size && !getenv("LIBCLANG_NOTHREADS")) |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 8160 | return CRC.RunSafelyOnThread(Fn, Size); |
| 8161 | return CRC.RunSafely(Fn); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8162 | } |
| 8163 | |
| 8164 | unsigned GetSafetyThreadStackSize() { |
| 8165 | return SafetyStackThreadSize; |
| 8166 | } |
| 8167 | |
| 8168 | void SetSafetyThreadStackSize(unsigned Value) { |
| 8169 | SafetyStackThreadSize = Value; |
| 8170 | } |
| 8171 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 8172 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8173 | |
| 8174 | void clang::setThreadBackgroundPriority() { |
| 8175 | if (getenv("LIBCLANG_BGPRIO_DISABLE")) |
| 8176 | return; |
| 8177 | |
Alp Toker | 1a86ad2 | 2014-07-06 06:24:00 +0000 | [diff] [blame] | 8178 | #ifdef USE_DARWIN_THREADS |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8179 | setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG); |
| 8180 | #endif |
| 8181 | } |
| 8182 | |
| 8183 | void cxindex::printDiagsToStderr(ASTUnit *Unit) { |
| 8184 | if (!Unit) |
| 8185 | return; |
| 8186 | |
| 8187 | for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), |
| 8188 | DEnd = Unit->stored_diag_end(); |
| 8189 | D != DEnd; ++D) { |
Ben Langmuir | 749323f | 2014-04-22 17:40:12 +0000 | [diff] [blame] | 8190 | CXStoredDiagnostic Diag(*D, Unit->getLangOpts()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8191 | CXString Msg = clang_formatDiagnostic(&Diag, |
| 8192 | clang_defaultDiagnosticDisplayOptions()); |
| 8193 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 8194 | clang_disposeString(Msg); |
| 8195 | } |
| 8196 | #ifdef LLVM_ON_WIN32 |
| 8197 | // On Windows, force a flush, since there may be multiple copies of |
| 8198 | // stderr and stdout in the file system, all with different buffers |
| 8199 | // but writing to the same device. |
| 8200 | fflush(stderr); |
| 8201 | #endif |
| 8202 | } |
| 8203 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8204 | MacroInfo *cxindex::getMacroInfo(const IdentifierInfo &II, |
| 8205 | SourceLocation MacroDefLoc, |
| 8206 | CXTranslationUnit TU){ |
| 8207 | if (MacroDefLoc.isInvalid() || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8208 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8209 | if (!II.hadMacroDefinition()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8210 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8211 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8212 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 8213 | Preprocessor &PP = Unit->getPreprocessor(); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 8214 | MacroDirective *MD = PP.getLocalMacroDirectiveHistory(&II); |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 8215 | if (MD) { |
| 8216 | for (MacroDirective::DefInfo |
| 8217 | Def = MD->getDefinition(); Def; Def = Def.getPreviousDefinition()) { |
| 8218 | if (MacroDefLoc == Def.getMacroInfo()->getDefinitionLoc()) |
| 8219 | return Def.getMacroInfo(); |
| 8220 | } |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8221 | } |
| 8222 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8223 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8224 | } |
| 8225 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 8226 | const MacroInfo *cxindex::getMacroInfo(const MacroDefinitionRecord *MacroDef, |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 8227 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8228 | if (!MacroDef || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8229 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8230 | const IdentifierInfo *II = MacroDef->getName(); |
| 8231 | if (!II) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8232 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8233 | |
| 8234 | return getMacroInfo(*II, MacroDef->getLocation(), TU); |
| 8235 | } |
| 8236 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 8237 | MacroDefinitionRecord * |
| 8238 | cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, const Token &Tok, |
| 8239 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8240 | if (!MI || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8241 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8242 | if (Tok.isNot(tok::raw_identifier)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8243 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8244 | |
| 8245 | if (MI->getNumTokens() == 0) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8246 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8247 | SourceRange DefRange(MI->getReplacementToken(0).getLocation(), |
| 8248 | MI->getDefinitionEndLoc()); |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8249 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8250 | |
| 8251 | // Check that the token is inside the definition and not its argument list. |
| 8252 | SourceManager &SM = Unit->getSourceManager(); |
| 8253 | if (SM.isBeforeInTranslationUnit(Tok.getLocation(), DefRange.getBegin())) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8254 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8255 | if (SM.isBeforeInTranslationUnit(DefRange.getEnd(), Tok.getLocation())) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8256 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8257 | |
| 8258 | Preprocessor &PP = Unit->getPreprocessor(); |
| 8259 | PreprocessingRecord *PPRec = PP.getPreprocessingRecord(); |
| 8260 | if (!PPRec) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8261 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8262 | |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 8263 | IdentifierInfo &II = PP.getIdentifierTable().get(Tok.getRawIdentifier()); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8264 | if (!II.hadMacroDefinition()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8265 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8266 | |
| 8267 | // Check that the identifier is not one of the macro arguments. |
Faisal Vali | ac506d7 | 2017-07-17 17:18:43 +0000 | [diff] [blame] | 8268 | 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] | 8269 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8270 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 8271 | MacroDirective *InnerMD = PP.getLocalMacroDirectiveHistory(&II); |
Argyrios Kyrtzidis | 09c9e81 | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 8272 | if (!InnerMD) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8273 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8274 | |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 8275 | return PPRec->findMacroDefinition(InnerMD->getMacroInfo()); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8276 | } |
| 8277 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 8278 | MacroDefinitionRecord * |
| 8279 | cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, SourceLocation Loc, |
| 8280 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8281 | if (Loc.isInvalid() || !MI || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8282 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8283 | |
| 8284 | if (MI->getNumTokens() == 0) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8285 | return nullptr; |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8286 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8287 | Preprocessor &PP = Unit->getPreprocessor(); |
| 8288 | if (!PP.getPreprocessingRecord()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8289 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8290 | Loc = Unit->getSourceManager().getSpellingLoc(Loc); |
| 8291 | Token Tok; |
| 8292 | if (PP.getRawToken(Loc, Tok)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8293 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 8294 | |
| 8295 | return checkForMacroInMacroDefinition(MI, Tok, TU); |
| 8296 | } |
| 8297 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8298 | CXString clang_getClangVersion() { |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 8299 | return cxstring::createDup(getClangFullVersion()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 8300 | } |
| 8301 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8302 | Logger &cxindex::Logger::operator<<(CXTranslationUnit TU) { |
| 8303 | if (TU) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 8304 | if (ASTUnit *Unit = cxtu::getASTUnit(TU)) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8305 | LogOS << '<' << Unit->getMainFileName() << '>'; |
Argyrios Kyrtzidis | 37f2ab4 | 2013-03-05 20:21:14 +0000 | [diff] [blame] | 8306 | if (Unit->isMainFileAST()) |
| 8307 | LogOS << " (" << Unit->getASTFileName() << ')'; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8308 | return *this; |
| 8309 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 8310 | } else { |
| 8311 | LogOS << "<NULL TU>"; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8312 | } |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8313 | return *this; |
| 8314 | } |
| 8315 | |
Argyrios Kyrtzidis | ba4b5f8 | 2013-03-08 02:32:26 +0000 | [diff] [blame] | 8316 | Logger &cxindex::Logger::operator<<(const FileEntry *FE) { |
| 8317 | *this << FE->getName(); |
| 8318 | return *this; |
| 8319 | } |
| 8320 | |
| 8321 | Logger &cxindex::Logger::operator<<(CXCursor cursor) { |
| 8322 | CXString cursorName = clang_getCursorDisplayName(cursor); |
| 8323 | *this << cursorName << "@" << clang_getCursorLocation(cursor); |
| 8324 | clang_disposeString(cursorName); |
| 8325 | return *this; |
| 8326 | } |
| 8327 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8328 | Logger &cxindex::Logger::operator<<(CXSourceLocation Loc) { |
| 8329 | CXFile File; |
| 8330 | unsigned Line, Column; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8331 | clang_getFileLocation(Loc, &File, &Line, &Column, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8332 | CXString FileName = clang_getFileName(File); |
| 8333 | *this << llvm::format("(%s:%d:%d)", clang_getCString(FileName), Line, Column); |
| 8334 | clang_disposeString(FileName); |
| 8335 | return *this; |
| 8336 | } |
| 8337 | |
| 8338 | Logger &cxindex::Logger::operator<<(CXSourceRange range) { |
| 8339 | CXSourceLocation BLoc = clang_getRangeStart(range); |
| 8340 | CXSourceLocation ELoc = clang_getRangeEnd(range); |
| 8341 | |
| 8342 | CXFile BFile; |
| 8343 | unsigned BLine, BColumn; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8344 | clang_getFileLocation(BLoc, &BFile, &BLine, &BColumn, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8345 | |
| 8346 | CXFile EFile; |
| 8347 | unsigned ELine, EColumn; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8348 | clang_getFileLocation(ELoc, &EFile, &ELine, &EColumn, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8349 | |
| 8350 | CXString BFileName = clang_getFileName(BFile); |
| 8351 | if (BFile == EFile) { |
| 8352 | *this << llvm::format("[%s %d:%d-%d:%d]", clang_getCString(BFileName), |
| 8353 | BLine, BColumn, ELine, EColumn); |
| 8354 | } else { |
| 8355 | CXString EFileName = clang_getFileName(EFile); |
| 8356 | *this << llvm::format("[%s:%d:%d - ", clang_getCString(BFileName), |
| 8357 | BLine, BColumn) |
| 8358 | << llvm::format("%s:%d:%d]", clang_getCString(EFileName), |
| 8359 | ELine, EColumn); |
| 8360 | clang_disposeString(EFileName); |
| 8361 | } |
| 8362 | clang_disposeString(BFileName); |
| 8363 | return *this; |
| 8364 | } |
| 8365 | |
| 8366 | Logger &cxindex::Logger::operator<<(CXString Str) { |
| 8367 | *this << clang_getCString(Str); |
| 8368 | return *this; |
| 8369 | } |
| 8370 | |
| 8371 | Logger &cxindex::Logger::operator<<(const llvm::format_object_base &Fmt) { |
| 8372 | LogOS << Fmt; |
| 8373 | return *this; |
| 8374 | } |
| 8375 | |
Chandler Carruth | 37ad258 | 2014-06-27 15:14:39 +0000 | [diff] [blame] | 8376 | static llvm::ManagedStatic<llvm::sys::Mutex> LoggingMutex; |
| 8377 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8378 | cxindex::Logger::~Logger() { |
Chandler Carruth | 37ad258 | 2014-06-27 15:14:39 +0000 | [diff] [blame] | 8379 | llvm::sys::ScopedLock L(*LoggingMutex); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8380 | |
| 8381 | static llvm::TimeRecord sBeginTR = llvm::TimeRecord::getCurrentTime(); |
| 8382 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8383 | raw_ostream &OS = llvm::errs(); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8384 | OS << "[libclang:" << Name << ':'; |
| 8385 | |
Alp Toker | 1a86ad2 | 2014-07-06 06:24:00 +0000 | [diff] [blame] | 8386 | #ifdef USE_DARWIN_THREADS |
| 8387 | // TODO: Portability. |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8388 | mach_port_t tid = pthread_mach_thread_np(pthread_self()); |
| 8389 | OS << tid << ':'; |
| 8390 | #endif |
| 8391 | |
| 8392 | llvm::TimeRecord TR = llvm::TimeRecord::getCurrentTime(); |
| 8393 | OS << llvm::format("%7.4f] ", TR.getWallTime() - sBeginTR.getWallTime()); |
Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 8394 | OS << Msg << '\n'; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8395 | |
| 8396 | if (Trace) { |
Zachary Turner | 1fe2a8d | 2015-03-05 19:15:09 +0000 | [diff] [blame] | 8397 | llvm::sys::PrintStackTrace(OS); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8398 | OS << "--------------------------------------------------\n"; |
| 8399 | } |
| 8400 | } |
Benjamin Kramer | c1ffdab | 2016-03-03 08:58:18 +0000 | [diff] [blame] | 8401 | |
| 8402 | #ifdef CLANG_TOOL_EXTRA_BUILD |
| 8403 | // This anchor is used to force the linker to link the clang-tidy plugin. |
| 8404 | extern volatile int ClangTidyPluginAnchorSource; |
| 8405 | static int LLVM_ATTRIBUTE_UNUSED ClangTidyPluginAnchorDestination = |
| 8406 | ClangTidyPluginAnchorSource; |
Benjamin Kramer | 9eba735 | 2016-11-17 15:22:36 +0000 | [diff] [blame] | 8407 | |
| 8408 | // This anchor is used to force the linker to link the clang-include-fixer |
| 8409 | // plugin. |
| 8410 | extern volatile int ClangIncludeFixerPluginAnchorSource; |
| 8411 | static int LLVM_ATTRIBUTE_UNUSED ClangIncludeFixerPluginAnchorDestination = |
| 8412 | ClangIncludeFixerPluginAnchorSource; |
Benjamin Kramer | c1ffdab | 2016-03-03 08:58:18 +0000 | [diff] [blame] | 8413 | #endif |