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" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 29 | #include "clang/Basic/Version.h" |
| 30 | #include "clang/Frontend/ASTUnit.h" |
| 31 | #include "clang/Frontend/CompilerInstance.h" |
| 32 | #include "clang/Frontend/FrontendDiagnostic.h" |
Argyrios Kyrtzidis | ca741ce | 2016-02-14 22:30:14 +0000 | [diff] [blame] | 33 | #include "clang/Index/CodegenNameGenerator.h" |
Dmitri Gribenko | 9e60511 | 2013-11-13 22:16:51 +0000 | [diff] [blame] | 34 | #include "clang/Index/CommentToXML.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 35 | #include "clang/Lex/HeaderSearch.h" |
| 36 | #include "clang/Lex/Lexer.h" |
| 37 | #include "clang/Lex/PreprocessingRecord.h" |
| 38 | #include "clang/Lex/Preprocessor.h" |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 39 | #include "clang/Serialization/SerializationDiagnostic.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/Optional.h" |
| 41 | #include "llvm/ADT/STLExtras.h" |
| 42 | #include "llvm/ADT/StringSwitch.h" |
Alp Toker | 1d257e1 | 2014-06-04 03:28:55 +0000 | [diff] [blame] | 43 | #include "llvm/Config/llvm-config.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Compiler.h" |
| 45 | #include "llvm/Support/CrashRecoveryContext.h" |
Chandler Carruth | 4b41745 | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Format.h" |
Chandler Carruth | 37ad258 | 2014-06-27 15:14:39 +0000 | [diff] [blame] | 47 | #include "llvm/Support/ManagedStatic.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 48 | #include "llvm/Support/MemoryBuffer.h" |
| 49 | #include "llvm/Support/Mutex.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Program.h" |
| 51 | #include "llvm/Support/SaveAndRestore.h" |
| 52 | #include "llvm/Support/Signals.h" |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 53 | #include "llvm/Support/TargetSelect.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 54 | #include "llvm/Support/Threading.h" |
| 55 | #include "llvm/Support/Timer.h" |
| 56 | #include "llvm/Support/raw_ostream.h" |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 57 | |
Alp Toker | 1a86ad2 | 2014-07-06 06:24:00 +0000 | [diff] [blame] | 58 | #if LLVM_ENABLE_THREADS != 0 && defined(__APPLE__) |
| 59 | #define USE_DARWIN_THREADS |
| 60 | #endif |
| 61 | |
| 62 | #ifdef USE_DARWIN_THREADS |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 63 | #include <pthread.h> |
| 64 | #endif |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 65 | |
| 66 | using namespace clang; |
| 67 | using namespace clang::cxcursor; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 68 | using namespace clang::cxtu; |
| 69 | using namespace clang::cxindex; |
| 70 | |
Dmitri Gribenko | d36209e | 2013-01-26 21:32:42 +0000 | [diff] [blame] | 71 | CXTranslationUnit cxtu::MakeCXTranslationUnit(CIndexer *CIdx, ASTUnit *AU) { |
| 72 | if (!AU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 73 | return nullptr; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 74 | assert(CIdx); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 75 | CXTranslationUnit D = new CXTranslationUnitImpl(); |
| 76 | D->CIdx = CIdx; |
Dmitri Gribenko | d36209e | 2013-01-26 21:32:42 +0000 | [diff] [blame] | 77 | D->TheASTUnit = AU; |
Dmitri Gribenko | 7489521 | 2013-02-03 13:52:47 +0000 | [diff] [blame] | 78 | D->StringPool = new cxstring::CXStringPool(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 79 | D->Diagnostics = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 80 | D->OverridenCursorsPool = createOverridenCXCursorsPool(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 81 | D->CommentToXML = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 82 | return D; |
| 83 | } |
| 84 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 85 | bool cxtu::isASTReadError(ASTUnit *AU) { |
| 86 | for (ASTUnit::stored_diag_iterator D = AU->stored_diag_begin(), |
| 87 | DEnd = AU->stored_diag_end(); |
| 88 | D != DEnd; ++D) { |
| 89 | if (D->getLevel() >= DiagnosticsEngine::Error && |
| 90 | DiagnosticIDs::getCategoryNumberForDiag(D->getID()) == |
| 91 | diag::DiagCat_AST_Deserialization_Issue) |
| 92 | return true; |
| 93 | } |
| 94 | return false; |
| 95 | } |
| 96 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 97 | cxtu::CXTUOwner::~CXTUOwner() { |
| 98 | if (TU) |
| 99 | clang_disposeTranslationUnit(TU); |
| 100 | } |
| 101 | |
| 102 | /// \brief Compare two source ranges to determine their relative position in |
| 103 | /// the translation unit. |
| 104 | static RangeComparisonResult RangeCompare(SourceManager &SM, |
| 105 | SourceRange R1, |
| 106 | SourceRange R2) { |
| 107 | assert(R1.isValid() && "First range is invalid?"); |
| 108 | assert(R2.isValid() && "Second range is invalid?"); |
| 109 | if (R1.getEnd() != R2.getBegin() && |
| 110 | SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin())) |
| 111 | return RangeBefore; |
| 112 | if (R2.getEnd() != R1.getBegin() && |
| 113 | SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin())) |
| 114 | return RangeAfter; |
| 115 | return RangeOverlap; |
| 116 | } |
| 117 | |
| 118 | /// \brief Determine if a source location falls within, before, or after a |
| 119 | /// a given source range. |
| 120 | static RangeComparisonResult LocationCompare(SourceManager &SM, |
| 121 | SourceLocation L, SourceRange R) { |
| 122 | assert(R.isValid() && "First range is invalid?"); |
| 123 | assert(L.isValid() && "Second range is invalid?"); |
| 124 | if (L == R.getBegin() || L == R.getEnd()) |
| 125 | return RangeOverlap; |
| 126 | if (SM.isBeforeInTranslationUnit(L, R.getBegin())) |
| 127 | return RangeBefore; |
| 128 | if (SM.isBeforeInTranslationUnit(R.getEnd(), L)) |
| 129 | return RangeAfter; |
| 130 | return RangeOverlap; |
| 131 | } |
| 132 | |
| 133 | /// \brief Translate a Clang source range into a CIndex source range. |
| 134 | /// |
| 135 | /// Clang internally represents ranges where the end location points to the |
| 136 | /// start of the token at the end. However, for external clients it is more |
| 137 | /// useful to have a CXSourceRange be a proper half-open interval. This routine |
| 138 | /// does the appropriate translation. |
| 139 | CXSourceRange cxloc::translateSourceRange(const SourceManager &SM, |
| 140 | const LangOptions &LangOpts, |
| 141 | const CharSourceRange &R) { |
| 142 | // We want the last character in this location, so we will adjust the |
| 143 | // location accordingly. |
| 144 | SourceLocation EndLoc = R.getEnd(); |
| 145 | if (EndLoc.isValid() && EndLoc.isMacroID() && !SM.isMacroArgExpansion(EndLoc)) |
| 146 | EndLoc = SM.getExpansionRange(EndLoc).second; |
Yaron Keren | 8b56366 | 2015-10-03 10:46:20 +0000 | [diff] [blame] | 147 | if (R.isTokenRange() && EndLoc.isValid()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 148 | unsigned Length = Lexer::MeasureTokenLength(SM.getSpellingLoc(EndLoc), |
| 149 | SM, LangOpts); |
| 150 | EndLoc = EndLoc.getLocWithOffset(Length); |
| 151 | } |
| 152 | |
Bill Wendling | eade362 | 2013-01-23 08:25:41 +0000 | [diff] [blame] | 153 | CXSourceRange Result = { |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 154 | { &SM, &LangOpts }, |
Bill Wendling | eade362 | 2013-01-23 08:25:41 +0000 | [diff] [blame] | 155 | R.getBegin().getRawEncoding(), |
| 156 | EndLoc.getRawEncoding() |
| 157 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 158 | return Result; |
| 159 | } |
| 160 | |
| 161 | //===----------------------------------------------------------------------===// |
| 162 | // Cursor visitor. |
| 163 | //===----------------------------------------------------------------------===// |
| 164 | |
| 165 | static SourceRange getRawCursorExtent(CXCursor C); |
| 166 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr); |
| 167 | |
| 168 | |
| 169 | RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) { |
| 170 | return RangeCompare(AU->getSourceManager(), R, RegionOfInterest); |
| 171 | } |
| 172 | |
| 173 | /// \brief Visit the given cursor and, if requested by the visitor, |
| 174 | /// its children. |
| 175 | /// |
| 176 | /// \param Cursor the cursor to visit. |
| 177 | /// |
| 178 | /// \param CheckedRegionOfInterest if true, then the caller already checked |
| 179 | /// that this cursor is within the region of interest. |
| 180 | /// |
| 181 | /// \returns true if the visitation should be aborted, false if it |
| 182 | /// should continue. |
| 183 | bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) { |
| 184 | if (clang_isInvalid(Cursor.kind)) |
| 185 | return false; |
| 186 | |
| 187 | if (clang_isDeclaration(Cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 188 | const Decl *D = getCursorDecl(Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 189 | if (!D) { |
| 190 | assert(0 && "Invalid declaration cursor"); |
| 191 | return true; // abort. |
| 192 | } |
| 193 | |
| 194 | // Ignore implicit declarations, unless it's an objc method because |
| 195 | // currently we should report implicit methods for properties when indexing. |
| 196 | if (D->isImplicit() && !isa<ObjCMethodDecl>(D)) |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | // If we have a range of interest, and this cursor doesn't intersect with it, |
| 201 | // we're done. |
| 202 | if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) { |
| 203 | SourceRange Range = getRawCursorExtent(Cursor); |
| 204 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 209 | case CXChildVisit_Break: |
| 210 | return true; |
| 211 | |
| 212 | case CXChildVisit_Continue: |
| 213 | return false; |
| 214 | |
| 215 | case CXChildVisit_Recurse: { |
| 216 | bool ret = VisitChildren(Cursor); |
| 217 | if (PostChildrenVisitor) |
| 218 | if (PostChildrenVisitor(Cursor, ClientData)) |
| 219 | return true; |
| 220 | return ret; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | llvm_unreachable("Invalid CXChildVisitResult!"); |
| 225 | } |
| 226 | |
| 227 | static bool visitPreprocessedEntitiesInRange(SourceRange R, |
| 228 | PreprocessingRecord &PPRec, |
| 229 | CursorVisitor &Visitor) { |
| 230 | SourceManager &SM = Visitor.getASTUnit()->getSourceManager(); |
| 231 | FileID FID; |
| 232 | |
| 233 | if (!Visitor.shouldVisitIncludedEntities()) { |
| 234 | // If the begin/end of the range lie in the same FileID, do the optimization |
| 235 | // where we skip preprocessed entities that do not come from the same FileID. |
| 236 | FID = SM.getFileID(SM.getFileLoc(R.getBegin())); |
| 237 | if (FID != SM.getFileID(SM.getFileLoc(R.getEnd()))) |
| 238 | FID = FileID(); |
| 239 | } |
| 240 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 241 | const auto &Entities = PPRec.getPreprocessedEntitiesInRange(R); |
| 242 | return Visitor.visitPreprocessedEntities(Entities.begin(), Entities.end(), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 243 | PPRec, FID); |
| 244 | } |
| 245 | |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 246 | bool CursorVisitor::visitFileRegion() { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 247 | if (RegionOfInterest.isInvalid()) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 248 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 249 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 250 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 251 | SourceManager &SM = Unit->getSourceManager(); |
| 252 | |
| 253 | std::pair<FileID, unsigned> |
| 254 | Begin = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getBegin())), |
| 255 | End = SM.getDecomposedLoc(SM.getFileLoc(RegionOfInterest.getEnd())); |
| 256 | |
| 257 | if (End.first != Begin.first) { |
| 258 | // If the end does not reside in the same file, try to recover by |
| 259 | // picking the end of the file of begin location. |
| 260 | End.first = Begin.first; |
| 261 | End.second = SM.getFileIDSize(Begin.first); |
| 262 | } |
| 263 | |
| 264 | assert(Begin.first == End.first); |
| 265 | if (Begin.second > End.second) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 266 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 267 | |
| 268 | FileID File = Begin.first; |
| 269 | unsigned Offset = Begin.second; |
| 270 | unsigned Length = End.second - Begin.second; |
| 271 | |
| 272 | if (!VisitDeclsOnly && !VisitPreprocessorLast) |
| 273 | if (visitPreprocessedEntitiesInRegion()) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 274 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 275 | |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 276 | if (visitDeclsFromFileRegion(File, Offset, Length)) |
| 277 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 278 | |
| 279 | if (!VisitDeclsOnly && VisitPreprocessorLast) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 280 | return visitPreprocessedEntitiesInRegion(); |
| 281 | |
| 282 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static bool isInLexicalContext(Decl *D, DeclContext *DC) { |
| 286 | if (!DC) |
| 287 | return false; |
| 288 | |
| 289 | for (DeclContext *DeclDC = D->getLexicalDeclContext(); |
| 290 | DeclDC; DeclDC = DeclDC->getLexicalParent()) { |
| 291 | if (DeclDC == DC) |
| 292 | return true; |
| 293 | } |
| 294 | return false; |
| 295 | } |
| 296 | |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 297 | bool CursorVisitor::visitDeclsFromFileRegion(FileID File, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 298 | unsigned Offset, unsigned Length) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 299 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 300 | SourceManager &SM = Unit->getSourceManager(); |
| 301 | SourceRange Range = RegionOfInterest; |
| 302 | |
| 303 | SmallVector<Decl *, 16> Decls; |
| 304 | Unit->findFileRegionDecls(File, Offset, Length, Decls); |
| 305 | |
| 306 | // If we didn't find any file level decls for the file, try looking at the |
| 307 | // file that it was included from. |
| 308 | while (Decls.empty() || Decls.front()->isTopLevelDeclInObjCContainer()) { |
| 309 | bool Invalid = false; |
| 310 | const SrcMgr::SLocEntry &SLEntry = SM.getSLocEntry(File, &Invalid); |
| 311 | if (Invalid) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 312 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 313 | |
| 314 | SourceLocation Outer; |
| 315 | if (SLEntry.isFile()) |
| 316 | Outer = SLEntry.getFile().getIncludeLoc(); |
| 317 | else |
| 318 | Outer = SLEntry.getExpansion().getExpansionLocStart(); |
| 319 | if (Outer.isInvalid()) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 320 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 321 | |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 322 | std::tie(File, Offset) = SM.getDecomposedExpansionLoc(Outer); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 323 | Length = 0; |
| 324 | Unit->findFileRegionDecls(File, Offset, Length, Decls); |
| 325 | } |
| 326 | |
| 327 | assert(!Decls.empty()); |
| 328 | |
| 329 | bool VisitedAtLeastOnce = false; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 330 | DeclContext *CurDC = nullptr; |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 331 | SmallVectorImpl<Decl *>::iterator DIt = Decls.begin(); |
| 332 | for (SmallVectorImpl<Decl *>::iterator DE = Decls.end(); DIt != DE; ++DIt) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 333 | Decl *D = *DIt; |
| 334 | if (D->getSourceRange().isInvalid()) |
| 335 | continue; |
| 336 | |
| 337 | if (isInLexicalContext(D, CurDC)) |
| 338 | continue; |
| 339 | |
| 340 | CurDC = dyn_cast<DeclContext>(D); |
| 341 | |
| 342 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
| 343 | if (!TD->isFreeStanding()) |
| 344 | continue; |
| 345 | |
| 346 | RangeComparisonResult CompRes = RangeCompare(SM, D->getSourceRange(),Range); |
| 347 | if (CompRes == RangeBefore) |
| 348 | continue; |
| 349 | if (CompRes == RangeAfter) |
| 350 | break; |
| 351 | |
| 352 | assert(CompRes == RangeOverlap); |
| 353 | VisitedAtLeastOnce = true; |
| 354 | |
| 355 | if (isa<ObjCContainerDecl>(D)) { |
| 356 | FileDI_current = &DIt; |
| 357 | FileDE_current = DE; |
| 358 | } else { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 359 | FileDI_current = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true)) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 363 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | if (VisitedAtLeastOnce) |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 367 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 368 | |
| 369 | // No Decls overlapped with the range. Move up the lexical context until there |
| 370 | // is a context that contains the range or we reach the translation unit |
| 371 | // level. |
| 372 | DeclContext *DC = DIt == Decls.begin() ? (*DIt)->getLexicalDeclContext() |
| 373 | : (*(DIt-1))->getLexicalDeclContext(); |
| 374 | |
| 375 | while (DC && !DC->isTranslationUnit()) { |
| 376 | Decl *D = cast<Decl>(DC); |
| 377 | SourceRange CurDeclRange = D->getSourceRange(); |
| 378 | if (CurDeclRange.isInvalid()) |
| 379 | break; |
| 380 | |
| 381 | if (RangeCompare(SM, CurDeclRange, Range) == RangeOverlap) { |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 382 | if (Visit(MakeCXCursor(D, TU, Range), /*CheckedRegionOfInterest=*/true)) |
| 383 | return true; // visitation break. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | DC = D->getLexicalDeclContext(); |
| 387 | } |
Argyrios Kyrtzidis | 951f61f | 2013-03-08 20:42:33 +0000 | [diff] [blame] | 388 | |
| 389 | return false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | bool CursorVisitor::visitPreprocessedEntitiesInRegion() { |
| 393 | if (!AU->getPreprocessor().getPreprocessingRecord()) |
| 394 | return false; |
| 395 | |
| 396 | PreprocessingRecord &PPRec |
| 397 | = *AU->getPreprocessor().getPreprocessingRecord(); |
| 398 | SourceManager &SM = AU->getSourceManager(); |
| 399 | |
| 400 | if (RegionOfInterest.isValid()) { |
| 401 | SourceRange MappedRange = AU->mapRangeToPreamble(RegionOfInterest); |
| 402 | SourceLocation B = MappedRange.getBegin(); |
| 403 | SourceLocation E = MappedRange.getEnd(); |
| 404 | |
| 405 | if (AU->isInPreambleFileID(B)) { |
| 406 | if (SM.isLoadedSourceLocation(E)) |
| 407 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), |
| 408 | PPRec, *this); |
| 409 | |
| 410 | // Beginning of range lies in the preamble but it also extends beyond |
| 411 | // it into the main file. Split the range into 2 parts, one covering |
| 412 | // the preamble and another covering the main file. This allows subsequent |
| 413 | // calls to visitPreprocessedEntitiesInRange to accept a source range that |
| 414 | // lies in the same FileID, allowing it to skip preprocessed entities that |
| 415 | // do not come from the same FileID. |
| 416 | bool breaked = |
| 417 | visitPreprocessedEntitiesInRange( |
| 418 | SourceRange(B, AU->getEndOfPreambleFileID()), |
| 419 | PPRec, *this); |
| 420 | if (breaked) return true; |
| 421 | return visitPreprocessedEntitiesInRange( |
| 422 | SourceRange(AU->getStartOfMainFileID(), E), |
| 423 | PPRec, *this); |
| 424 | } |
| 425 | |
| 426 | return visitPreprocessedEntitiesInRange(SourceRange(B, E), PPRec, *this); |
| 427 | } |
| 428 | |
| 429 | bool OnlyLocalDecls |
| 430 | = !AU->isMainFileAST() && AU->getOnlyLocalDecls(); |
| 431 | |
| 432 | if (OnlyLocalDecls) |
| 433 | return visitPreprocessedEntities(PPRec.local_begin(), PPRec.local_end(), |
| 434 | PPRec); |
| 435 | |
| 436 | return visitPreprocessedEntities(PPRec.begin(), PPRec.end(), PPRec); |
| 437 | } |
| 438 | |
| 439 | template<typename InputIterator> |
| 440 | bool CursorVisitor::visitPreprocessedEntities(InputIterator First, |
| 441 | InputIterator Last, |
| 442 | PreprocessingRecord &PPRec, |
| 443 | FileID FID) { |
| 444 | for (; First != Last; ++First) { |
| 445 | if (!FID.isInvalid() && !PPRec.isEntityInFileID(First, FID)) |
| 446 | continue; |
| 447 | |
| 448 | PreprocessedEntity *PPE = *First; |
Argyrios Kyrtzidis | 1030f26 | 2013-05-07 20:37:17 +0000 | [diff] [blame] | 449 | if (!PPE) |
| 450 | continue; |
| 451 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 452 | if (MacroExpansion *ME = dyn_cast<MacroExpansion>(PPE)) { |
| 453 | if (Visit(MakeMacroExpansionCursor(ME, TU))) |
| 454 | return true; |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 455 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 456 | continue; |
| 457 | } |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 458 | |
| 459 | if (MacroDefinitionRecord *MD = dyn_cast<MacroDefinitionRecord>(PPE)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 460 | if (Visit(MakeMacroDefinitionCursor(MD, TU))) |
| 461 | return true; |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 462 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 463 | continue; |
| 464 | } |
| 465 | |
| 466 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) { |
| 467 | if (Visit(MakeInclusionDirectiveCursor(ID, TU))) |
| 468 | return true; |
| 469 | |
| 470 | continue; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | /// \brief Visit the children of the given cursor. |
| 478 | /// |
| 479 | /// \returns true if the visitation should be aborted, false if it |
| 480 | /// should continue. |
| 481 | bool CursorVisitor::VisitChildren(CXCursor Cursor) { |
| 482 | if (clang_isReference(Cursor.kind) && |
| 483 | Cursor.kind != CXCursor_CXXBaseSpecifier) { |
| 484 | // By definition, references have no children. |
| 485 | return false; |
| 486 | } |
| 487 | |
| 488 | // Set the Parent field to Cursor, then back to its old value once we're |
| 489 | // done. |
| 490 | SetParentRAII SetParent(Parent, StmtParent, Cursor); |
| 491 | |
| 492 | if (clang_isDeclaration(Cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 493 | Decl *D = const_cast<Decl *>(getCursorDecl(Cursor)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 494 | if (!D) |
| 495 | return false; |
| 496 | |
| 497 | return VisitAttributes(D) || Visit(D); |
| 498 | } |
| 499 | |
| 500 | if (clang_isStatement(Cursor.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 501 | if (const Stmt *S = getCursorStmt(Cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 502 | return Visit(S); |
| 503 | |
| 504 | return false; |
| 505 | } |
| 506 | |
| 507 | if (clang_isExpression(Cursor.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 508 | if (const Expr *E = getCursorExpr(Cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 509 | return Visit(E); |
| 510 | |
| 511 | return false; |
| 512 | } |
| 513 | |
| 514 | if (clang_isTranslationUnit(Cursor.kind)) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 515 | CXTranslationUnit TU = getCursorTU(Cursor); |
| 516 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 517 | |
| 518 | int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast }; |
| 519 | for (unsigned I = 0; I != 2; ++I) { |
| 520 | if (VisitOrder[I]) { |
| 521 | if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() && |
| 522 | RegionOfInterest.isInvalid()) { |
| 523 | for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(), |
| 524 | TLEnd = CXXUnit->top_level_end(); |
| 525 | TL != TLEnd; ++TL) { |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 526 | const Optional<bool> V = handleDeclForVisitation(*TL); |
| 527 | if (!V.hasValue()) |
| 528 | continue; |
| 529 | return V.getValue(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 530 | } |
| 531 | } else if (VisitDeclContext( |
| 532 | CXXUnit->getASTContext().getTranslationUnitDecl())) |
| 533 | return true; |
| 534 | continue; |
| 535 | } |
| 536 | |
| 537 | // Walk the preprocessing record. |
| 538 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) |
| 539 | visitPreprocessedEntitiesInRegion(); |
| 540 | } |
| 541 | |
| 542 | return false; |
| 543 | } |
| 544 | |
| 545 | if (Cursor.kind == CXCursor_CXXBaseSpecifier) { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 546 | if (const CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 547 | if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) { |
| 548 | return Visit(BaseTSInfo->getTypeLoc()); |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 554 | const IBOutletCollectionAttr *A = |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 555 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor)); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 556 | if (const ObjCObjectType *ObjT = A->getInterface()->getAs<ObjCObjectType>()) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 557 | return Visit(cxcursor::MakeCursorObjCClassRef( |
| 558 | ObjT->getInterface(), |
| 559 | A->getInterfaceLoc()->getTypeLoc().getLocStart(), TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 562 | // If pointing inside a macro definition, check if the token is an identifier |
| 563 | // that was ever defined as a macro. In such a case, create a "pseudo" macro |
| 564 | // expansion cursor for that token. |
| 565 | SourceLocation BeginLoc = RegionOfInterest.getBegin(); |
| 566 | if (Cursor.kind == CXCursor_MacroDefinition && |
| 567 | BeginLoc == RegionOfInterest.getEnd()) { |
| 568 | SourceLocation Loc = AU->mapLocationToPreamble(BeginLoc); |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 569 | const MacroInfo *MI = |
| 570 | getMacroInfo(cxcursor::getCursorMacroDefinition(Cursor), TU); |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 571 | if (MacroDefinitionRecord *MacroDef = |
| 572 | checkForMacroInMacroDefinition(MI, Loc, TU)) |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 573 | return Visit(cxcursor::MakeMacroExpansionCursor(MacroDef, BeginLoc, TU)); |
| 574 | } |
| 575 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 576 | // Nothing to visit at the moment. |
| 577 | return false; |
| 578 | } |
| 579 | |
| 580 | bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { |
| 581 | if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten()) |
| 582 | if (Visit(TSInfo->getTypeLoc())) |
| 583 | return true; |
| 584 | |
| 585 | if (Stmt *Body = B->getBody()) |
| 586 | return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest)); |
| 587 | |
| 588 | return false; |
| 589 | } |
| 590 | |
Ted Kremenek | 0332558 | 2013-02-21 01:29:01 +0000 | [diff] [blame] | 591 | Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 592 | if (RegionOfInterest.isValid()) { |
| 593 | SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager()); |
| 594 | if (Range.isInvalid()) |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 595 | return None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 596 | |
| 597 | switch (CompareRegionOfInterest(Range)) { |
| 598 | case RangeBefore: |
| 599 | // This declaration comes before the region of interest; skip it. |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 600 | return None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 601 | |
| 602 | case RangeAfter: |
| 603 | // This declaration comes after the region of interest; we're done. |
| 604 | return false; |
| 605 | |
| 606 | case RangeOverlap: |
| 607 | // This declaration overlaps the region of interest; visit it. |
| 608 | break; |
| 609 | } |
| 610 | } |
| 611 | return true; |
| 612 | } |
| 613 | |
| 614 | bool CursorVisitor::VisitDeclContext(DeclContext *DC) { |
| 615 | DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 616 | |
| 617 | // FIXME: Eventually remove. This part of a hack to support proper |
| 618 | // iteration over all Decls contained lexically within an ObjC container. |
| 619 | SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I); |
| 620 | SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E); |
| 621 | |
| 622 | for ( ; I != E; ++I) { |
| 623 | Decl *D = *I; |
| 624 | if (D->getLexicalDeclContext() != DC) |
| 625 | continue; |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 626 | const Optional<bool> V = handleDeclForVisitation(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 627 | if (!V.hasValue()) |
| 628 | continue; |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 629 | return V.getValue(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 630 | } |
| 631 | return false; |
| 632 | } |
| 633 | |
Argyrios Kyrtzidis | e7c9104 | 2016-07-01 19:10:54 +0000 | [diff] [blame] | 634 | Optional<bool> CursorVisitor::handleDeclForVisitation(const Decl *D) { |
| 635 | CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest); |
| 636 | |
| 637 | // Ignore synthesized ivars here, otherwise if we have something like: |
| 638 | // @synthesize prop = _prop; |
| 639 | // and '_prop' is not declared, we will encounter a '_prop' ivar before |
| 640 | // encountering the 'prop' synthesize declaration and we will think that |
| 641 | // we passed the region-of-interest. |
| 642 | if (auto *ivarD = dyn_cast<ObjCIvarDecl>(D)) { |
| 643 | if (ivarD->getSynthesize()) |
| 644 | return None; |
| 645 | } |
| 646 | |
| 647 | // FIXME: ObjCClassRef/ObjCProtocolRef for forward class/protocol |
| 648 | // declarations is a mismatch with the compiler semantics. |
| 649 | if (Cursor.kind == CXCursor_ObjCInterfaceDecl) { |
| 650 | auto *ID = cast<ObjCInterfaceDecl>(D); |
| 651 | if (!ID->isThisDeclarationADefinition()) |
| 652 | Cursor = MakeCursorObjCClassRef(ID, ID->getLocation(), TU); |
| 653 | |
| 654 | } else if (Cursor.kind == CXCursor_ObjCProtocolDecl) { |
| 655 | auto *PD = cast<ObjCProtocolDecl>(D); |
| 656 | if (!PD->isThisDeclarationADefinition()) |
| 657 | Cursor = MakeCursorObjCProtocolRef(PD, PD->getLocation(), TU); |
| 658 | } |
| 659 | |
| 660 | const Optional<bool> V = shouldVisitCursor(Cursor); |
| 661 | if (!V.hasValue()) |
| 662 | return None; |
| 663 | if (!V.getValue()) |
| 664 | return false; |
| 665 | if (Visit(Cursor, true)) |
| 666 | return true; |
| 667 | return None; |
| 668 | } |
| 669 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 670 | bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 671 | llvm_unreachable("Translation units are visited directly by Visit()"); |
| 672 | } |
| 673 | |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 674 | bool CursorVisitor::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| 675 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 676 | return true; |
| 677 | |
| 678 | return Visit(MakeCXCursor(D->getTemplatedDecl(), TU, RegionOfInterest)); |
| 679 | } |
| 680 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 681 | bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 682 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 683 | return Visit(TSInfo->getTypeLoc()); |
| 684 | |
| 685 | return false; |
| 686 | } |
| 687 | |
| 688 | bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { |
| 689 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 690 | return Visit(TSInfo->getTypeLoc()); |
| 691 | |
| 692 | return false; |
| 693 | } |
| 694 | |
| 695 | bool CursorVisitor::VisitTagDecl(TagDecl *D) { |
| 696 | return VisitDeclContext(D); |
| 697 | } |
| 698 | |
| 699 | bool CursorVisitor::VisitClassTemplateSpecializationDecl( |
| 700 | ClassTemplateSpecializationDecl *D) { |
| 701 | bool ShouldVisitBody = false; |
| 702 | switch (D->getSpecializationKind()) { |
| 703 | case TSK_Undeclared: |
| 704 | case TSK_ImplicitInstantiation: |
| 705 | // Nothing to visit |
| 706 | return false; |
| 707 | |
| 708 | case TSK_ExplicitInstantiationDeclaration: |
| 709 | case TSK_ExplicitInstantiationDefinition: |
| 710 | break; |
| 711 | |
| 712 | case TSK_ExplicitSpecialization: |
| 713 | ShouldVisitBody = true; |
| 714 | break; |
| 715 | } |
| 716 | |
| 717 | // Visit the template arguments used in the specialization. |
| 718 | if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { |
| 719 | TypeLoc TL = SpecType->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 720 | if (TemplateSpecializationTypeLoc TSTLoc = |
| 721 | TL.getAs<TemplateSpecializationTypeLoc>()) { |
| 722 | for (unsigned I = 0, N = TSTLoc.getNumArgs(); I != N; ++I) |
| 723 | if (VisitTemplateArgumentLoc(TSTLoc.getArgLoc(I))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 724 | return true; |
| 725 | } |
| 726 | } |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 727 | |
| 728 | return ShouldVisitBody && VisitCXXRecordDecl(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( |
| 732 | ClassTemplatePartialSpecializationDecl *D) { |
| 733 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 734 | // before visiting these template parameters. |
| 735 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 736 | return true; |
| 737 | |
| 738 | // Visit the partial specialization arguments. |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 739 | const ASTTemplateArgumentListInfo *Info = D->getTemplateArgsAsWritten(); |
| 740 | const TemplateArgumentLoc *TemplateArgs = Info->getTemplateArgs(); |
| 741 | for (unsigned I = 0, N = Info->NumTemplateArgs; I != N; ++I) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 742 | if (VisitTemplateArgumentLoc(TemplateArgs[I])) |
| 743 | return true; |
| 744 | |
| 745 | return VisitCXXRecordDecl(D); |
| 746 | } |
| 747 | |
| 748 | bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
| 749 | // Visit the default argument. |
| 750 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 751 | if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) |
| 752 | if (Visit(DefArg->getTypeLoc())) |
| 753 | return true; |
| 754 | |
| 755 | return false; |
| 756 | } |
| 757 | |
| 758 | bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 759 | if (Expr *Init = D->getInitExpr()) |
| 760 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
| 761 | return false; |
| 762 | } |
| 763 | |
| 764 | bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { |
Argyrios Kyrtzidis | 2ec7674 | 2013-04-05 21:04:10 +0000 | [diff] [blame] | 765 | unsigned NumParamList = DD->getNumTemplateParameterLists(); |
| 766 | for (unsigned i = 0; i < NumParamList; i++) { |
| 767 | TemplateParameterList* Params = DD->getTemplateParameterList(i); |
| 768 | if (VisitTemplateParameters(Params)) |
| 769 | return true; |
| 770 | } |
| 771 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 772 | if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) |
| 773 | if (Visit(TSInfo->getTypeLoc())) |
| 774 | return true; |
| 775 | |
| 776 | // Visit the nested-name-specifier, if present. |
| 777 | if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc()) |
| 778 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 779 | return true; |
| 780 | |
| 781 | return false; |
| 782 | } |
| 783 | |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 784 | /// \brief Compare two base or member initializers based on their source order. |
| 785 | static int CompareCXXCtorInitializers(CXXCtorInitializer *const *X, |
| 786 | CXXCtorInitializer *const *Y) { |
| 787 | return (*X)->getSourceOrder() - (*Y)->getSourceOrder(); |
| 788 | } |
| 789 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 790 | bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
Argyrios Kyrtzidis | 2ec7674 | 2013-04-05 21:04:10 +0000 | [diff] [blame] | 791 | unsigned NumParamList = ND->getNumTemplateParameterLists(); |
| 792 | for (unsigned i = 0; i < NumParamList; i++) { |
| 793 | TemplateParameterList* Params = ND->getTemplateParameterList(i); |
| 794 | if (VisitTemplateParameters(Params)) |
| 795 | return true; |
| 796 | } |
| 797 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 798 | if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { |
| 799 | // Visit the function declaration's syntactic components in the order |
| 800 | // written. This requires a bit of work. |
| 801 | TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 802 | FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 803 | |
| 804 | // If we have a function declared directly (without the use of a typedef), |
| 805 | // visit just the return type. Otherwise, just visit the function's type |
| 806 | // now. |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 807 | if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL.getReturnLoc())) || |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 808 | (!FTL && Visit(TL))) |
| 809 | return true; |
| 810 | |
| 811 | // Visit the nested-name-specifier, if present. |
| 812 | if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc()) |
| 813 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 814 | return true; |
| 815 | |
| 816 | // Visit the declaration name. |
Argyrios Kyrtzidis | 4a4d2b4 | 2014-02-09 08:13:47 +0000 | [diff] [blame] | 817 | if (!isa<CXXDestructorDecl>(ND)) |
| 818 | if (VisitDeclarationNameInfo(ND->getNameInfo())) |
| 819 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 820 | |
| 821 | // FIXME: Visit explicitly-specified template arguments! |
| 822 | |
| 823 | // Visit the function parameters, if we have a function type. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 824 | if (FTL && VisitFunctionTypeLoc(FTL, true)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 825 | return true; |
| 826 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 827 | // FIXME: Attributes? |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) { |
| 831 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) { |
| 832 | // Find the initializers that were written in the source. |
| 833 | SmallVector<CXXCtorInitializer *, 4> WrittenInits; |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 834 | for (auto *I : Constructor->inits()) { |
| 835 | if (!I->isWritten()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 836 | continue; |
| 837 | |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 838 | WrittenInits.push_back(I); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | // Sort the initializers in source order |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 842 | llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(), |
| 843 | &CompareCXXCtorInitializers); |
| 844 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 845 | // Visit the initializers in source order |
| 846 | for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) { |
| 847 | CXXCtorInitializer *Init = WrittenInits[I]; |
| 848 | if (Init->isAnyMemberInitializer()) { |
| 849 | if (Visit(MakeCursorMemberRef(Init->getAnyMember(), |
| 850 | Init->getMemberLocation(), TU))) |
| 851 | return true; |
| 852 | } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) { |
| 853 | if (Visit(TInfo->getTypeLoc())) |
| 854 | return true; |
| 855 | } |
| 856 | |
| 857 | // Visit the initializer value. |
| 858 | if (Expr *Initializer = Init->getInit()) |
| 859 | if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest))) |
| 860 | return true; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
| 865 | return true; |
| 866 | } |
| 867 | |
| 868 | return false; |
| 869 | } |
| 870 | |
| 871 | bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { |
| 872 | if (VisitDeclaratorDecl(D)) |
| 873 | return true; |
| 874 | |
| 875 | if (Expr *BitWidth = D->getBitWidth()) |
| 876 | return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest)); |
| 877 | |
| 878 | return false; |
| 879 | } |
| 880 | |
| 881 | bool CursorVisitor::VisitVarDecl(VarDecl *D) { |
| 882 | if (VisitDeclaratorDecl(D)) |
| 883 | return true; |
| 884 | |
| 885 | if (Expr *Init = D->getInit()) |
| 886 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
| 887 | |
| 888 | return false; |
| 889 | } |
| 890 | |
| 891 | bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 892 | if (VisitDeclaratorDecl(D)) |
| 893 | return true; |
| 894 | |
| 895 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 896 | if (Expr *DefArg = D->getDefaultArgument()) |
| 897 | return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest)); |
| 898 | |
| 899 | return false; |
| 900 | } |
| 901 | |
| 902 | bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 903 | // FIXME: Visit the "outer" template parameter lists on the FunctionDecl |
| 904 | // before visiting these template parameters. |
| 905 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 906 | return true; |
| 907 | |
| 908 | return VisitFunctionDecl(D->getTemplatedDecl()); |
| 909 | } |
| 910 | |
| 911 | bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 912 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 913 | // before visiting these template parameters. |
| 914 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 915 | return true; |
| 916 | |
| 917 | return VisitCXXRecordDecl(D->getTemplatedDecl()); |
| 918 | } |
| 919 | |
| 920 | bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 921 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 922 | return true; |
| 923 | |
| 924 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && |
| 925 | VisitTemplateArgumentLoc(D->getDefaultArgument())) |
| 926 | return true; |
| 927 | |
| 928 | return false; |
| 929 | } |
| 930 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 931 | bool CursorVisitor::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { |
| 932 | // Visit the bound, if it's explicit. |
| 933 | if (D->hasExplicitBound()) { |
| 934 | if (auto TInfo = D->getTypeSourceInfo()) { |
| 935 | if (Visit(TInfo->getTypeLoc())) |
| 936 | return true; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | return false; |
| 941 | } |
| 942 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 943 | bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 944 | if (TypeSourceInfo *TSInfo = ND->getReturnTypeSourceInfo()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 945 | if (Visit(TSInfo->getTypeLoc())) |
| 946 | return true; |
| 947 | |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 948 | for (const auto *P : ND->parameters()) { |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 949 | if (Visit(MakeCXCursor(P, TU, RegionOfInterest))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 950 | return true; |
| 951 | } |
| 952 | |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 953 | return ND->isThisDeclarationADefinition() && |
| 954 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | template <typename DeclIt> |
| 958 | static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current, |
| 959 | SourceManager &SM, SourceLocation EndLoc, |
| 960 | SmallVectorImpl<Decl *> &Decls) { |
| 961 | DeclIt next = *DI_current; |
| 962 | while (++next != DE_current) { |
| 963 | Decl *D_next = *next; |
| 964 | if (!D_next) |
| 965 | break; |
| 966 | SourceLocation L = D_next->getLocStart(); |
| 967 | if (!L.isValid()) |
| 968 | break; |
| 969 | if (SM.isBeforeInTranslationUnit(L, EndLoc)) { |
| 970 | *DI_current = next; |
| 971 | Decls.push_back(D_next); |
| 972 | continue; |
| 973 | } |
| 974 | break; |
| 975 | } |
| 976 | } |
| 977 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 978 | bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
| 979 | // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially |
| 980 | // an @implementation can lexically contain Decls that are not properly |
| 981 | // nested in the AST. When we identify such cases, we need to retrofit |
| 982 | // this nesting here. |
| 983 | if (!DI_current && !FileDI_current) |
| 984 | return VisitDeclContext(D); |
| 985 | |
| 986 | // Scan the Decls that immediately come after the container |
| 987 | // in the current DeclContext. If any fall within the |
| 988 | // container's lexical region, stash them into a vector |
| 989 | // for later processing. |
| 990 | SmallVector<Decl *, 24> DeclsInContainer; |
| 991 | SourceLocation EndLoc = D->getSourceRange().getEnd(); |
| 992 | SourceManager &SM = AU->getSourceManager(); |
| 993 | if (EndLoc.isValid()) { |
| 994 | if (DI_current) { |
| 995 | addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc, |
| 996 | DeclsInContainer); |
| 997 | } else { |
| 998 | addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc, |
| 999 | DeclsInContainer); |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | // The common case. |
| 1004 | if (DeclsInContainer.empty()) |
| 1005 | return VisitDeclContext(D); |
| 1006 | |
| 1007 | // Get all the Decls in the DeclContext, and sort them with the |
| 1008 | // additional ones we've collected. Then visit them. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1009 | for (auto *SubDecl : D->decls()) { |
| 1010 | if (!SubDecl || SubDecl->getLexicalDeclContext() != D || |
| 1011 | SubDecl->getLocStart().isInvalid()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1012 | continue; |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1013 | DeclsInContainer.push_back(SubDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | // Now sort the Decls so that they appear in lexical order. |
| 1017 | std::sort(DeclsInContainer.begin(), DeclsInContainer.end(), |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 1018 | [&SM](Decl *A, Decl *B) { |
| 1019 | SourceLocation L_A = A->getLocStart(); |
| 1020 | SourceLocation L_B = B->getLocStart(); |
| 1021 | assert(L_A.isValid() && L_B.isValid()); |
| 1022 | return SM.isBeforeInTranslationUnit(L_A, L_B); |
| 1023 | }); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1024 | |
| 1025 | // Now visit the decls. |
| 1026 | for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(), |
| 1027 | E = DeclsInContainer.end(); I != E; ++I) { |
| 1028 | CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest); |
Ted Kremenek | 0332558 | 2013-02-21 01:29:01 +0000 | [diff] [blame] | 1029 | const Optional<bool> &V = shouldVisitCursor(Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1030 | if (!V.hasValue()) |
| 1031 | continue; |
| 1032 | if (!V.getValue()) |
| 1033 | return false; |
| 1034 | if (Visit(Cursor, true)) |
| 1035 | return true; |
| 1036 | } |
| 1037 | return false; |
| 1038 | } |
| 1039 | |
| 1040 | bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 1041 | if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), |
| 1042 | TU))) |
| 1043 | return true; |
| 1044 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1045 | if (VisitObjCTypeParamList(ND->getTypeParamList())) |
| 1046 | return true; |
| 1047 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1048 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 1049 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 1050 | E = ND->protocol_end(); I != E; ++I, ++PL) |
| 1051 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1052 | return true; |
| 1053 | |
| 1054 | return VisitObjCContainerDecl(ND); |
| 1055 | } |
| 1056 | |
| 1057 | bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 1058 | if (!PID->isThisDeclarationADefinition()) |
| 1059 | return Visit(MakeCursorObjCProtocolRef(PID, PID->getLocation(), TU)); |
| 1060 | |
| 1061 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
| 1062 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 1063 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 1064 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1065 | return true; |
| 1066 | |
| 1067 | return VisitObjCContainerDecl(PID); |
| 1068 | } |
| 1069 | |
| 1070 | bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { |
| 1071 | if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc())) |
| 1072 | return true; |
| 1073 | |
| 1074 | // FIXME: This implements a workaround with @property declarations also being |
| 1075 | // installed in the DeclContext for the @interface. Eventually this code |
| 1076 | // should be removed. |
| 1077 | ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); |
| 1078 | if (!CDecl || !CDecl->IsClassExtension()) |
| 1079 | return false; |
| 1080 | |
| 1081 | ObjCInterfaceDecl *ID = CDecl->getClassInterface(); |
| 1082 | if (!ID) |
| 1083 | return false; |
| 1084 | |
| 1085 | IdentifierInfo *PropertyId = PD->getIdentifier(); |
| 1086 | ObjCPropertyDecl *prevDecl = |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 1087 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId, |
| 1088 | PD->getQueryKind()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1089 | |
| 1090 | if (!prevDecl) |
| 1091 | return false; |
| 1092 | |
| 1093 | // Visit synthesized methods since they will be skipped when visiting |
| 1094 | // the @interface. |
| 1095 | if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) |
| 1096 | if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl) |
| 1097 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
| 1098 | return true; |
| 1099 | |
| 1100 | if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) |
| 1101 | if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl) |
| 1102 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
| 1103 | return true; |
| 1104 | |
| 1105 | return false; |
| 1106 | } |
| 1107 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1108 | bool CursorVisitor::VisitObjCTypeParamList(ObjCTypeParamList *typeParamList) { |
| 1109 | if (!typeParamList) |
| 1110 | return false; |
| 1111 | |
| 1112 | for (auto *typeParam : *typeParamList) { |
| 1113 | // Visit the type parameter. |
| 1114 | if (Visit(MakeCXCursor(typeParam, TU, RegionOfInterest))) |
| 1115 | return true; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | return false; |
| 1119 | } |
| 1120 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1121 | bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 1122 | if (!D->isThisDeclarationADefinition()) { |
| 1123 | // Forward declaration is treated like a reference. |
| 1124 | return Visit(MakeCursorObjCClassRef(D, D->getLocation(), TU)); |
| 1125 | } |
| 1126 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1127 | // Objective-C type parameters. |
| 1128 | if (VisitObjCTypeParamList(D->getTypeParamListAsWritten())) |
| 1129 | return true; |
| 1130 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1131 | // Issue callbacks for super class. |
| 1132 | if (D->getSuperClass() && |
| 1133 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
| 1134 | D->getSuperClassLoc(), |
| 1135 | TU))) |
| 1136 | return true; |
| 1137 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1138 | if (TypeSourceInfo *SuperClassTInfo = D->getSuperClassTInfo()) |
| 1139 | if (Visit(SuperClassTInfo->getTypeLoc())) |
| 1140 | return true; |
| 1141 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1142 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1143 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 1144 | E = D->protocol_end(); I != E; ++I, ++PL) |
| 1145 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1146 | return true; |
| 1147 | |
| 1148 | return VisitObjCContainerDecl(D); |
| 1149 | } |
| 1150 | |
| 1151 | bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 1152 | return VisitObjCContainerDecl(D); |
| 1153 | } |
| 1154 | |
| 1155 | bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 1156 | // 'ID' could be null when dealing with invalid code. |
| 1157 | if (ObjCInterfaceDecl *ID = D->getClassInterface()) |
| 1158 | if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) |
| 1159 | return true; |
| 1160 | |
| 1161 | return VisitObjCImplDecl(D); |
| 1162 | } |
| 1163 | |
| 1164 | bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 1165 | #if 0 |
| 1166 | // Issue callbacks for super class. |
| 1167 | // FIXME: No source location information! |
| 1168 | if (D->getSuperClass() && |
| 1169 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
| 1170 | D->getSuperClassLoc(), |
| 1171 | TU))) |
| 1172 | return true; |
| 1173 | #endif |
| 1174 | |
| 1175 | return VisitObjCImplDecl(D); |
| 1176 | } |
| 1177 | |
| 1178 | bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) { |
| 1179 | if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl()) |
| 1180 | if (PD->isIvarNameSpecified()) |
| 1181 | return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU)); |
| 1182 | |
| 1183 | return false; |
| 1184 | } |
| 1185 | |
| 1186 | bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { |
| 1187 | return VisitDeclContext(D); |
| 1188 | } |
| 1189 | |
| 1190 | bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 1191 | // Visit nested-name-specifier. |
| 1192 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1193 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1194 | return true; |
| 1195 | |
| 1196 | return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), |
| 1197 | D->getTargetNameLoc(), TU)); |
| 1198 | } |
| 1199 | |
| 1200 | bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { |
| 1201 | // Visit nested-name-specifier. |
| 1202 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1203 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1204 | return true; |
| 1205 | } |
| 1206 | |
| 1207 | if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU))) |
| 1208 | return true; |
| 1209 | |
| 1210 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1211 | } |
| 1212 | |
| 1213 | bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 1214 | // Visit nested-name-specifier. |
| 1215 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1216 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1217 | return true; |
| 1218 | |
| 1219 | return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), |
| 1220 | D->getIdentLocation(), TU)); |
| 1221 | } |
| 1222 | |
| 1223 | bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
| 1224 | // Visit nested-name-specifier. |
| 1225 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1226 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1227 | return true; |
| 1228 | } |
| 1229 | |
| 1230 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1231 | } |
| 1232 | |
| 1233 | bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( |
| 1234 | UnresolvedUsingTypenameDecl *D) { |
| 1235 | // Visit nested-name-specifier. |
| 1236 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1237 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1238 | return true; |
| 1239 | |
| 1240 | return false; |
| 1241 | } |
| 1242 | |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 1243 | bool CursorVisitor::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 1244 | if (Visit(MakeCXCursor(D->getAssertExpr(), StmtParent, TU, RegionOfInterest))) |
| 1245 | return true; |
| 1246 | if (Visit(MakeCXCursor(D->getMessage(), StmtParent, TU, RegionOfInterest))) |
| 1247 | return true; |
| 1248 | return false; |
| 1249 | } |
| 1250 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1251 | bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { |
| 1252 | switch (Name.getName().getNameKind()) { |
| 1253 | case clang::DeclarationName::Identifier: |
| 1254 | case clang::DeclarationName::CXXLiteralOperatorName: |
| 1255 | case clang::DeclarationName::CXXOperatorName: |
| 1256 | case clang::DeclarationName::CXXUsingDirective: |
| 1257 | return false; |
| 1258 | |
| 1259 | case clang::DeclarationName::CXXConstructorName: |
| 1260 | case clang::DeclarationName::CXXDestructorName: |
| 1261 | case clang::DeclarationName::CXXConversionFunctionName: |
| 1262 | if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) |
| 1263 | return Visit(TSInfo->getTypeLoc()); |
| 1264 | return false; |
| 1265 | |
| 1266 | case clang::DeclarationName::ObjCZeroArgSelector: |
| 1267 | case clang::DeclarationName::ObjCOneArgSelector: |
| 1268 | case clang::DeclarationName::ObjCMultiArgSelector: |
| 1269 | // FIXME: Per-identifier location info? |
| 1270 | return false; |
| 1271 | } |
| 1272 | |
| 1273 | llvm_unreachable("Invalid DeclarationName::Kind!"); |
| 1274 | } |
| 1275 | |
| 1276 | bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, |
| 1277 | SourceRange Range) { |
| 1278 | // FIXME: This whole routine is a hack to work around the lack of proper |
| 1279 | // source information in nested-name-specifiers (PR5791). Since we do have |
| 1280 | // a beginning source location, we can visit the first component of the |
| 1281 | // nested-name-specifier, if it's a single-token component. |
| 1282 | if (!NNS) |
| 1283 | return false; |
| 1284 | |
| 1285 | // Get the first component in the nested-name-specifier. |
| 1286 | while (NestedNameSpecifier *Prefix = NNS->getPrefix()) |
| 1287 | NNS = Prefix; |
| 1288 | |
| 1289 | switch (NNS->getKind()) { |
| 1290 | case NestedNameSpecifier::Namespace: |
| 1291 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), |
| 1292 | TU)); |
| 1293 | |
| 1294 | case NestedNameSpecifier::NamespaceAlias: |
| 1295 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1296 | Range.getBegin(), TU)); |
| 1297 | |
| 1298 | case NestedNameSpecifier::TypeSpec: { |
| 1299 | // If the type has a form where we know that the beginning of the source |
| 1300 | // range matches up with a reference cursor. Visit the appropriate reference |
| 1301 | // cursor. |
| 1302 | const Type *T = NNS->getAsType(); |
| 1303 | if (const TypedefType *Typedef = dyn_cast<TypedefType>(T)) |
| 1304 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); |
| 1305 | if (const TagType *Tag = dyn_cast<TagType>(T)) |
| 1306 | return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); |
| 1307 | if (const TemplateSpecializationType *TST |
| 1308 | = dyn_cast<TemplateSpecializationType>(T)) |
| 1309 | return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); |
| 1310 | break; |
| 1311 | } |
| 1312 | |
| 1313 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1314 | case NestedNameSpecifier::Global: |
| 1315 | case NestedNameSpecifier::Identifier: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 1316 | case NestedNameSpecifier::Super: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1317 | break; |
| 1318 | } |
| 1319 | |
| 1320 | return false; |
| 1321 | } |
| 1322 | |
| 1323 | bool |
| 1324 | CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 1325 | SmallVector<NestedNameSpecifierLoc, 4> Qualifiers; |
| 1326 | for (; Qualifier; Qualifier = Qualifier.getPrefix()) |
| 1327 | Qualifiers.push_back(Qualifier); |
| 1328 | |
| 1329 | while (!Qualifiers.empty()) { |
| 1330 | NestedNameSpecifierLoc Q = Qualifiers.pop_back_val(); |
| 1331 | NestedNameSpecifier *NNS = Q.getNestedNameSpecifier(); |
| 1332 | switch (NNS->getKind()) { |
| 1333 | case NestedNameSpecifier::Namespace: |
| 1334 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), |
| 1335 | Q.getLocalBeginLoc(), |
| 1336 | TU))) |
| 1337 | return true; |
| 1338 | |
| 1339 | break; |
| 1340 | |
| 1341 | case NestedNameSpecifier::NamespaceAlias: |
| 1342 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1343 | Q.getLocalBeginLoc(), |
| 1344 | TU))) |
| 1345 | return true; |
| 1346 | |
| 1347 | break; |
| 1348 | |
| 1349 | case NestedNameSpecifier::TypeSpec: |
| 1350 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1351 | if (Visit(Q.getTypeLoc())) |
| 1352 | return true; |
| 1353 | |
| 1354 | break; |
| 1355 | |
| 1356 | case NestedNameSpecifier::Global: |
| 1357 | case NestedNameSpecifier::Identifier: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 1358 | case NestedNameSpecifier::Super: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1359 | break; |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | return false; |
| 1364 | } |
| 1365 | |
| 1366 | bool CursorVisitor::VisitTemplateParameters( |
| 1367 | const TemplateParameterList *Params) { |
| 1368 | if (!Params) |
| 1369 | return false; |
| 1370 | |
| 1371 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 1372 | PEnd = Params->end(); |
| 1373 | P != PEnd; ++P) { |
| 1374 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
| 1375 | return true; |
| 1376 | } |
| 1377 | |
| 1378 | return false; |
| 1379 | } |
| 1380 | |
| 1381 | bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { |
| 1382 | switch (Name.getKind()) { |
| 1383 | case TemplateName::Template: |
| 1384 | return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); |
| 1385 | |
| 1386 | case TemplateName::OverloadedTemplate: |
| 1387 | // Visit the overloaded template set. |
| 1388 | if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU))) |
| 1389 | return true; |
| 1390 | |
| 1391 | return false; |
| 1392 | |
| 1393 | case TemplateName::DependentTemplate: |
| 1394 | // FIXME: Visit nested-name-specifier. |
| 1395 | return false; |
| 1396 | |
| 1397 | case TemplateName::QualifiedTemplate: |
| 1398 | // FIXME: Visit nested-name-specifier. |
| 1399 | return Visit(MakeCursorTemplateRef( |
| 1400 | Name.getAsQualifiedTemplateName()->getDecl(), |
| 1401 | Loc, TU)); |
| 1402 | |
| 1403 | case TemplateName::SubstTemplateTemplateParm: |
| 1404 | return Visit(MakeCursorTemplateRef( |
| 1405 | Name.getAsSubstTemplateTemplateParm()->getParameter(), |
| 1406 | Loc, TU)); |
| 1407 | |
| 1408 | case TemplateName::SubstTemplateTemplateParmPack: |
| 1409 | return Visit(MakeCursorTemplateRef( |
| 1410 | Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), |
| 1411 | Loc, TU)); |
| 1412 | } |
| 1413 | |
| 1414 | llvm_unreachable("Invalid TemplateName::Kind!"); |
| 1415 | } |
| 1416 | |
| 1417 | bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { |
| 1418 | switch (TAL.getArgument().getKind()) { |
| 1419 | case TemplateArgument::Null: |
| 1420 | case TemplateArgument::Integral: |
| 1421 | case TemplateArgument::Pack: |
| 1422 | return false; |
| 1423 | |
| 1424 | case TemplateArgument::Type: |
| 1425 | if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) |
| 1426 | return Visit(TSInfo->getTypeLoc()); |
| 1427 | return false; |
| 1428 | |
| 1429 | case TemplateArgument::Declaration: |
| 1430 | if (Expr *E = TAL.getSourceDeclExpression()) |
| 1431 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
| 1432 | return false; |
| 1433 | |
| 1434 | case TemplateArgument::NullPtr: |
| 1435 | if (Expr *E = TAL.getSourceNullPtrExpression()) |
| 1436 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
| 1437 | return false; |
| 1438 | |
| 1439 | case TemplateArgument::Expression: |
| 1440 | if (Expr *E = TAL.getSourceExpression()) |
| 1441 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
| 1442 | return false; |
| 1443 | |
| 1444 | case TemplateArgument::Template: |
| 1445 | case TemplateArgument::TemplateExpansion: |
| 1446 | if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc())) |
| 1447 | return true; |
| 1448 | |
| 1449 | return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(), |
| 1450 | TAL.getTemplateNameLoc()); |
| 1451 | } |
| 1452 | |
| 1453 | llvm_unreachable("Invalid TemplateArgument::Kind!"); |
| 1454 | } |
| 1455 | |
| 1456 | bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 1457 | return VisitDeclContext(D); |
| 1458 | } |
| 1459 | |
| 1460 | bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1461 | return Visit(TL.getUnqualifiedLoc()); |
| 1462 | } |
| 1463 | |
| 1464 | bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
| 1465 | ASTContext &Context = AU->getASTContext(); |
| 1466 | |
| 1467 | // Some builtin types (such as Objective-C's "id", "sel", and |
| 1468 | // "Class") have associated declarations. Create cursors for those. |
| 1469 | QualType VisitType; |
| 1470 | switch (TL.getTypePtr()->getKind()) { |
| 1471 | |
| 1472 | case BuiltinType::Void: |
| 1473 | case BuiltinType::NullPtr: |
| 1474 | case BuiltinType::Dependent: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 1475 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 1476 | case BuiltinType::Id: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 1477 | #include "clang/Basic/OpenCLImageTypes.def" |
NAKAMURA Takumi | 288c42e | 2013-02-07 12:47:42 +0000 | [diff] [blame] | 1478 | case BuiltinType::OCLSampler: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1479 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1480 | case BuiltinType::OCLClkEvent: |
| 1481 | case BuiltinType::OCLQueue: |
| 1482 | case BuiltinType::OCLNDRange: |
| 1483 | case BuiltinType::OCLReserveID: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1484 | #define BUILTIN_TYPE(Id, SingletonId) |
| 1485 | #define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1486 | #define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1487 | #define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1488 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1489 | #include "clang/AST/BuiltinTypes.def" |
| 1490 | break; |
| 1491 | |
| 1492 | case BuiltinType::ObjCId: |
| 1493 | VisitType = Context.getObjCIdType(); |
| 1494 | break; |
| 1495 | |
| 1496 | case BuiltinType::ObjCClass: |
| 1497 | VisitType = Context.getObjCClassType(); |
| 1498 | break; |
| 1499 | |
| 1500 | case BuiltinType::ObjCSel: |
| 1501 | VisitType = Context.getObjCSelType(); |
| 1502 | break; |
| 1503 | } |
| 1504 | |
| 1505 | if (!VisitType.isNull()) { |
| 1506 | if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) |
| 1507 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), |
| 1508 | TU)); |
| 1509 | } |
| 1510 | |
| 1511 | return false; |
| 1512 | } |
| 1513 | |
| 1514 | bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 1515 | return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU)); |
| 1516 | } |
| 1517 | |
| 1518 | bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 1519 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1520 | } |
| 1521 | |
| 1522 | bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { |
| 1523 | if (TL.isDefinition()) |
| 1524 | return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest)); |
| 1525 | |
| 1526 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1527 | } |
| 1528 | |
| 1529 | bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1530 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1531 | } |
| 1532 | |
| 1533 | bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 1534 | return Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 1538 | if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) |
| 1539 | return true; |
| 1540 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1541 | for (unsigned I = 0, N = TL.getNumTypeArgs(); I != N; ++I) { |
| 1542 | if (Visit(TL.getTypeArgTInfo(I)->getTypeLoc())) |
| 1543 | return true; |
| 1544 | } |
| 1545 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1546 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1547 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1548 | TU))) |
| 1549 | return true; |
| 1550 | } |
| 1551 | |
| 1552 | return false; |
| 1553 | } |
| 1554 | |
| 1555 | bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 1556 | return Visit(TL.getPointeeLoc()); |
| 1557 | } |
| 1558 | |
| 1559 | bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) { |
| 1560 | return Visit(TL.getInnerLoc()); |
| 1561 | } |
| 1562 | |
| 1563 | bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1564 | return Visit(TL.getPointeeLoc()); |
| 1565 | } |
| 1566 | |
| 1567 | bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1568 | return Visit(TL.getPointeeLoc()); |
| 1569 | } |
| 1570 | |
| 1571 | bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1572 | return Visit(TL.getPointeeLoc()); |
| 1573 | } |
| 1574 | |
| 1575 | bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 1576 | return Visit(TL.getPointeeLoc()); |
| 1577 | } |
| 1578 | |
| 1579 | bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 1580 | return Visit(TL.getPointeeLoc()); |
| 1581 | } |
| 1582 | |
| 1583 | bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 1584 | return Visit(TL.getModifiedLoc()); |
| 1585 | } |
| 1586 | |
| 1587 | bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, |
| 1588 | bool SkipResultType) { |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 1589 | if (!SkipResultType && Visit(TL.getReturnLoc())) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1590 | return true; |
| 1591 | |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 1592 | for (unsigned I = 0, N = TL.getNumParams(); I != N; ++I) |
| 1593 | if (Decl *D = TL.getParam(I)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1594 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest))) |
| 1595 | return true; |
| 1596 | |
| 1597 | return false; |
| 1598 | } |
| 1599 | |
| 1600 | bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1601 | if (Visit(TL.getElementLoc())) |
| 1602 | return true; |
| 1603 | |
| 1604 | if (Expr *Size = TL.getSizeExpr()) |
| 1605 | return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest)); |
| 1606 | |
| 1607 | return false; |
| 1608 | } |
| 1609 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 1610 | bool CursorVisitor::VisitDecayedTypeLoc(DecayedTypeLoc TL) { |
| 1611 | return Visit(TL.getOriginalLoc()); |
| 1612 | } |
| 1613 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 1614 | bool CursorVisitor::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { |
| 1615 | return Visit(TL.getOriginalLoc()); |
| 1616 | } |
| 1617 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1618 | bool CursorVisitor::VisitTemplateSpecializationTypeLoc( |
| 1619 | TemplateSpecializationTypeLoc TL) { |
| 1620 | // Visit the template name. |
| 1621 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1622 | TL.getTemplateNameLoc())) |
| 1623 | return true; |
| 1624 | |
| 1625 | // Visit the template arguments. |
| 1626 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1627 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1628 | return true; |
| 1629 | |
| 1630 | return false; |
| 1631 | } |
| 1632 | |
| 1633 | bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 1634 | return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); |
| 1635 | } |
| 1636 | |
| 1637 | bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 1638 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1639 | return Visit(TSInfo->getTypeLoc()); |
| 1640 | |
| 1641 | return false; |
| 1642 | } |
| 1643 | |
| 1644 | bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 1645 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1646 | return Visit(TSInfo->getTypeLoc()); |
| 1647 | |
| 1648 | return false; |
| 1649 | } |
| 1650 | |
| 1651 | bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 1652 | return VisitNestedNameSpecifierLoc(TL.getQualifierLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc( |
| 1656 | DependentTemplateSpecializationTypeLoc TL) { |
| 1657 | // Visit the nested-name-specifier, if there is one. |
| 1658 | if (TL.getQualifierLoc() && |
| 1659 | VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1660 | return true; |
| 1661 | |
| 1662 | // Visit the template arguments. |
| 1663 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1664 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1665 | return true; |
| 1666 | |
| 1667 | return false; |
| 1668 | } |
| 1669 | |
| 1670 | bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 1671 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1672 | return true; |
| 1673 | |
| 1674 | return Visit(TL.getNamedTypeLoc()); |
| 1675 | } |
| 1676 | |
| 1677 | bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { |
| 1678 | return Visit(TL.getPatternLoc()); |
| 1679 | } |
| 1680 | |
| 1681 | bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 1682 | if (Expr *E = TL.getUnderlyingExpr()) |
| 1683 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1684 | |
| 1685 | return false; |
| 1686 | } |
| 1687 | |
| 1688 | bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 1689 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1690 | } |
| 1691 | |
| 1692 | bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) { |
| 1693 | return Visit(TL.getValueLoc()); |
| 1694 | } |
| 1695 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 1696 | bool CursorVisitor::VisitPipeTypeLoc(PipeTypeLoc TL) { |
| 1697 | return Visit(TL.getValueLoc()); |
| 1698 | } |
| 1699 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1700 | #define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \ |
| 1701 | bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \ |
| 1702 | return Visit##PARENT##Loc(TL); \ |
| 1703 | } |
| 1704 | |
| 1705 | DEFAULT_TYPELOC_IMPL(Complex, Type) |
| 1706 | DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType) |
| 1707 | DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType) |
| 1708 | DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType) |
| 1709 | DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType) |
| 1710 | DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type) |
| 1711 | DEFAULT_TYPELOC_IMPL(Vector, Type) |
| 1712 | DEFAULT_TYPELOC_IMPL(ExtVector, VectorType) |
| 1713 | DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType) |
| 1714 | DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType) |
| 1715 | DEFAULT_TYPELOC_IMPL(Record, TagType) |
| 1716 | DEFAULT_TYPELOC_IMPL(Enum, TagType) |
| 1717 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type) |
| 1718 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type) |
| 1719 | DEFAULT_TYPELOC_IMPL(Auto, Type) |
| 1720 | |
| 1721 | bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 1722 | // Visit the nested-name-specifier, if present. |
| 1723 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1724 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1725 | return true; |
| 1726 | |
| 1727 | if (D->isCompleteDefinition()) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1728 | for (const auto &I : D->bases()) { |
| 1729 | if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(&I, TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1730 | return true; |
| 1731 | } |
| 1732 | } |
| 1733 | |
| 1734 | return VisitTagDecl(D); |
| 1735 | } |
| 1736 | |
| 1737 | bool CursorVisitor::VisitAttributes(Decl *D) { |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 1738 | for (const auto *I : D->attrs()) |
| 1739 | if (Visit(MakeCXCursor(I, D, TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1740 | return true; |
| 1741 | |
| 1742 | return false; |
| 1743 | } |
| 1744 | |
| 1745 | //===----------------------------------------------------------------------===// |
| 1746 | // Data-recursive visitor methods. |
| 1747 | //===----------------------------------------------------------------------===// |
| 1748 | |
| 1749 | namespace { |
| 1750 | #define DEF_JOB(NAME, DATA, KIND)\ |
| 1751 | class NAME : public VisitorJob {\ |
| 1752 | public:\ |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1753 | NAME(const DATA *d, CXCursor parent) : \ |
| 1754 | VisitorJob(parent, VisitorJob::KIND, d) {} \ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1755 | static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\ |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1756 | const DATA *get() const { return static_cast<const DATA*>(data[0]); }\ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1757 | }; |
| 1758 | |
| 1759 | DEF_JOB(StmtVisit, Stmt, StmtVisitKind) |
| 1760 | DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind) |
| 1761 | DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind) |
| 1762 | DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1763 | DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind) |
| 1764 | DEF_JOB(LambdaExprParts, LambdaExpr, LambdaExprPartsKind) |
| 1765 | DEF_JOB(PostChildrenVisit, void, PostChildrenVisitKind) |
| 1766 | #undef DEF_JOB |
| 1767 | |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 1768 | class ExplicitTemplateArgsVisit : public VisitorJob { |
| 1769 | public: |
| 1770 | ExplicitTemplateArgsVisit(const TemplateArgumentLoc *Begin, |
| 1771 | const TemplateArgumentLoc *End, CXCursor parent) |
| 1772 | : VisitorJob(parent, VisitorJob::ExplicitTemplateArgsVisitKind, Begin, |
| 1773 | End) {} |
| 1774 | static bool classof(const VisitorJob *VJ) { |
| 1775 | return VJ->getKind() == ExplicitTemplateArgsVisitKind; |
| 1776 | } |
| 1777 | const TemplateArgumentLoc *begin() const { |
| 1778 | return static_cast<const TemplateArgumentLoc *>(data[0]); |
| 1779 | } |
| 1780 | const TemplateArgumentLoc *end() { |
| 1781 | return static_cast<const TemplateArgumentLoc *>(data[1]); |
| 1782 | } |
| 1783 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1784 | class DeclVisit : public VisitorJob { |
| 1785 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1786 | DeclVisit(const Decl *D, CXCursor parent, bool isFirst) : |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1787 | VisitorJob(parent, VisitorJob::DeclVisitKind, |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 1788 | D, isFirst ? (void*) 1 : (void*) nullptr) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1789 | static bool classof(const VisitorJob *VJ) { |
| 1790 | return VJ->getKind() == DeclVisitKind; |
| 1791 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1792 | const Decl *get() const { return static_cast<const Decl *>(data[0]); } |
Dmitri Gribenko | e5423a7 | 2015-03-23 19:23:50 +0000 | [diff] [blame] | 1793 | bool isFirst() const { return data[1] != nullptr; } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1794 | }; |
| 1795 | class TypeLocVisit : public VisitorJob { |
| 1796 | public: |
| 1797 | TypeLocVisit(TypeLoc tl, CXCursor parent) : |
| 1798 | VisitorJob(parent, VisitorJob::TypeLocVisitKind, |
| 1799 | tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {} |
| 1800 | |
| 1801 | static bool classof(const VisitorJob *VJ) { |
| 1802 | return VJ->getKind() == TypeLocVisitKind; |
| 1803 | } |
| 1804 | |
| 1805 | TypeLoc get() const { |
| 1806 | QualType T = QualType::getFromOpaquePtr(data[0]); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1807 | return TypeLoc(T, const_cast<void *>(data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1808 | } |
| 1809 | }; |
| 1810 | |
| 1811 | class LabelRefVisit : public VisitorJob { |
| 1812 | public: |
| 1813 | LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent) |
| 1814 | : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD, |
| 1815 | labelLoc.getPtrEncoding()) {} |
| 1816 | |
| 1817 | static bool classof(const VisitorJob *VJ) { |
| 1818 | return VJ->getKind() == VisitorJob::LabelRefVisitKind; |
| 1819 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1820 | const LabelDecl *get() const { |
| 1821 | return static_cast<const LabelDecl *>(data[0]); |
| 1822 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1823 | SourceLocation getLoc() const { |
| 1824 | return SourceLocation::getFromPtrEncoding(data[1]); } |
| 1825 | }; |
| 1826 | |
| 1827 | class NestedNameSpecifierLocVisit : public VisitorJob { |
| 1828 | public: |
| 1829 | NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent) |
| 1830 | : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind, |
| 1831 | Qualifier.getNestedNameSpecifier(), |
| 1832 | Qualifier.getOpaqueData()) { } |
| 1833 | |
| 1834 | static bool classof(const VisitorJob *VJ) { |
| 1835 | return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind; |
| 1836 | } |
| 1837 | |
| 1838 | NestedNameSpecifierLoc get() const { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1839 | return NestedNameSpecifierLoc( |
| 1840 | const_cast<NestedNameSpecifier *>( |
| 1841 | static_cast<const NestedNameSpecifier *>(data[0])), |
| 1842 | const_cast<void *>(data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1843 | } |
| 1844 | }; |
| 1845 | |
| 1846 | class DeclarationNameInfoVisit : public VisitorJob { |
| 1847 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1848 | DeclarationNameInfoVisit(const Stmt *S, CXCursor parent) |
Dmitri Gribenko | dd7dacf | 2013-02-03 13:19:54 +0000 | [diff] [blame] | 1849 | : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1850 | static bool classof(const VisitorJob *VJ) { |
| 1851 | return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind; |
| 1852 | } |
| 1853 | DeclarationNameInfo get() const { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1854 | const Stmt *S = static_cast<const Stmt *>(data[0]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1855 | switch (S->getStmtClass()) { |
| 1856 | default: |
| 1857 | llvm_unreachable("Unhandled Stmt"); |
| 1858 | case clang::Stmt::MSDependentExistsStmtClass: |
| 1859 | return cast<MSDependentExistsStmt>(S)->getNameInfo(); |
| 1860 | case Stmt::CXXDependentScopeMemberExprClass: |
| 1861 | return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo(); |
| 1862 | case Stmt::DependentScopeDeclRefExprClass: |
| 1863 | return cast<DependentScopeDeclRefExpr>(S)->getNameInfo(); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1864 | case Stmt::OMPCriticalDirectiveClass: |
| 1865 | return cast<OMPCriticalDirective>(S)->getDirectiveName(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1866 | } |
| 1867 | } |
| 1868 | }; |
| 1869 | class MemberRefVisit : public VisitorJob { |
| 1870 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1871 | MemberRefVisit(const FieldDecl *D, SourceLocation L, CXCursor parent) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1872 | : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D, |
| 1873 | L.getPtrEncoding()) {} |
| 1874 | static bool classof(const VisitorJob *VJ) { |
| 1875 | return VJ->getKind() == VisitorJob::MemberRefVisitKind; |
| 1876 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1877 | const FieldDecl *get() const { |
| 1878 | return static_cast<const FieldDecl *>(data[0]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1879 | } |
| 1880 | SourceLocation getLoc() const { |
| 1881 | return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]); |
| 1882 | } |
| 1883 | }; |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1884 | class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void> { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1885 | friend class OMPClauseEnqueue; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1886 | VisitorWorkList &WL; |
| 1887 | CXCursor Parent; |
| 1888 | public: |
| 1889 | EnqueueVisitor(VisitorWorkList &wl, CXCursor parent) |
| 1890 | : WL(wl), Parent(parent) {} |
| 1891 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1892 | void VisitAddrLabelExpr(const AddrLabelExpr *E); |
| 1893 | void VisitBlockExpr(const BlockExpr *B); |
| 1894 | void VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 1895 | void VisitCompoundStmt(const CompoundStmt *S); |
| 1896 | void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { /* Do nothing. */ } |
| 1897 | void VisitMSDependentExistsStmt(const MSDependentExistsStmt *S); |
| 1898 | void VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E); |
| 1899 | void VisitCXXNewExpr(const CXXNewExpr *E); |
| 1900 | void VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E); |
| 1901 | void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *E); |
| 1902 | void VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E); |
| 1903 | void VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *E); |
| 1904 | void VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
| 1905 | void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *E); |
| 1906 | void VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
| 1907 | void VisitCXXCatchStmt(const CXXCatchStmt *S); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 1908 | void VisitCXXForRangeStmt(const CXXForRangeStmt *S); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1909 | void VisitDeclRefExpr(const DeclRefExpr *D); |
| 1910 | void VisitDeclStmt(const DeclStmt *S); |
| 1911 | void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E); |
| 1912 | void VisitDesignatedInitExpr(const DesignatedInitExpr *E); |
| 1913 | void VisitExplicitCastExpr(const ExplicitCastExpr *E); |
| 1914 | void VisitForStmt(const ForStmt *FS); |
| 1915 | void VisitGotoStmt(const GotoStmt *GS); |
| 1916 | void VisitIfStmt(const IfStmt *If); |
| 1917 | void VisitInitListExpr(const InitListExpr *IE); |
| 1918 | void VisitMemberExpr(const MemberExpr *M); |
| 1919 | void VisitOffsetOfExpr(const OffsetOfExpr *E); |
| 1920 | void VisitObjCEncodeExpr(const ObjCEncodeExpr *E); |
| 1921 | void VisitObjCMessageExpr(const ObjCMessageExpr *M); |
| 1922 | void VisitOverloadExpr(const OverloadExpr *E); |
| 1923 | void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
| 1924 | void VisitStmt(const Stmt *S); |
| 1925 | void VisitSwitchStmt(const SwitchStmt *S); |
| 1926 | void VisitWhileStmt(const WhileStmt *W); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1927 | void VisitTypeTraitExpr(const TypeTraitExpr *E); |
| 1928 | void VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E); |
| 1929 | void VisitExpressionTraitExpr(const ExpressionTraitExpr *E); |
| 1930 | void VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U); |
| 1931 | void VisitVAArgExpr(const VAArgExpr *E); |
| 1932 | void VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
| 1933 | void VisitPseudoObjectExpr(const PseudoObjectExpr *E); |
| 1934 | void VisitOpaqueValueExpr(const OpaqueValueExpr *E); |
| 1935 | void VisitLambdaExpr(const LambdaExpr *E); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1936 | void VisitOMPExecutableDirective(const OMPExecutableDirective *D); |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 1937 | void VisitOMPLoopDirective(const OMPLoopDirective *D); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1938 | void VisitOMPParallelDirective(const OMPParallelDirective *D); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 1939 | void VisitOMPSimdDirective(const OMPSimdDirective *D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1940 | void VisitOMPForDirective(const OMPForDirective *D); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1941 | void VisitOMPForSimdDirective(const OMPForSimdDirective *D); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 1942 | void VisitOMPSectionsDirective(const OMPSectionsDirective *D); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1943 | void VisitOMPSectionDirective(const OMPSectionDirective *D); |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 1944 | void VisitOMPSingleDirective(const OMPSingleDirective *D); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1945 | void VisitOMPMasterDirective(const OMPMasterDirective *D); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1946 | void VisitOMPCriticalDirective(const OMPCriticalDirective *D); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1947 | void VisitOMPParallelForDirective(const OMPParallelForDirective *D); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1948 | void VisitOMPParallelForSimdDirective(const OMPParallelForSimdDirective *D); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1949 | void VisitOMPParallelSectionsDirective(const OMPParallelSectionsDirective *D); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1950 | void VisitOMPTaskDirective(const OMPTaskDirective *D); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1951 | void VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 1952 | void VisitOMPBarrierDirective(const OMPBarrierDirective *D); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 1953 | void VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1954 | void VisitOMPTaskgroupDirective(const OMPTaskgroupDirective *D); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 1955 | void |
| 1956 | VisitOMPCancellationPointDirective(const OMPCancellationPointDirective *D); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 1957 | void VisitOMPCancelDirective(const OMPCancelDirective *D); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 1958 | void VisitOMPFlushDirective(const OMPFlushDirective *D); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1959 | void VisitOMPOrderedDirective(const OMPOrderedDirective *D); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1960 | void VisitOMPAtomicDirective(const OMPAtomicDirective *D); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1961 | void VisitOMPTargetDirective(const OMPTargetDirective *D); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 1962 | void VisitOMPTargetDataDirective(const OMPTargetDataDirective *D); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 1963 | void VisitOMPTargetEnterDataDirective(const OMPTargetEnterDataDirective *D); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 1964 | void VisitOMPTargetExitDataDirective(const OMPTargetExitDataDirective *D); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 1965 | void VisitOMPTargetParallelDirective(const OMPTargetParallelDirective *D); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 1966 | void |
| 1967 | VisitOMPTargetParallelForDirective(const OMPTargetParallelForDirective *D); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1968 | void VisitOMPTeamsDirective(const OMPTeamsDirective *D); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 1969 | void VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 1970 | void VisitOMPTaskLoopSimdDirective(const OMPTaskLoopSimdDirective *D); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 1971 | void VisitOMPDistributeDirective(const OMPDistributeDirective *D); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 1972 | void VisitOMPDistributeParallelForDirective( |
| 1973 | const OMPDistributeParallelForDirective *D); |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 1974 | void VisitOMPDistributeParallelForSimdDirective( |
| 1975 | const OMPDistributeParallelForSimdDirective *D); |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 1976 | void VisitOMPDistributeSimdDirective(const OMPDistributeSimdDirective *D); |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 1977 | void VisitOMPTargetParallelForSimdDirective( |
| 1978 | const OMPTargetParallelForSimdDirective *D); |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 1979 | void VisitOMPTargetSimdDirective(const OMPTargetSimdDirective *D); |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 1980 | void VisitOMPTeamsDistributeDirective(const OMPTeamsDistributeDirective *D); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1981 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1982 | private: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1983 | void AddDeclarationNameInfo(const Stmt *S); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1984 | void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier); |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 1985 | void AddExplicitTemplateArgs(const TemplateArgumentLoc *A, |
| 1986 | unsigned NumTemplateArgs); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1987 | void AddMemberRef(const FieldDecl *D, SourceLocation L); |
| 1988 | void AddStmt(const Stmt *S); |
| 1989 | void AddDecl(const Decl *D, bool isFirst = true); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1990 | void AddTypeLoc(TypeSourceInfo *TI); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1991 | void EnqueueChildren(const Stmt *S); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1992 | void EnqueueChildren(const OMPClause *S); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1993 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1994 | } // end anonyous namespace |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1995 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1996 | void EnqueueVisitor::AddDeclarationNameInfo(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1997 | // 'S' should always be non-null, since it comes from the |
| 1998 | // statement we are visiting. |
| 1999 | WL.push_back(DeclarationNameInfoVisit(S, Parent)); |
| 2000 | } |
| 2001 | |
| 2002 | void |
| 2003 | EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 2004 | if (Qualifier) |
| 2005 | WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent)); |
| 2006 | } |
| 2007 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2008 | void EnqueueVisitor::AddStmt(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2009 | if (S) |
| 2010 | WL.push_back(StmtVisit(S, Parent)); |
| 2011 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2012 | void EnqueueVisitor::AddDecl(const Decl *D, bool isFirst) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2013 | if (D) |
| 2014 | WL.push_back(DeclVisit(D, Parent, isFirst)); |
| 2015 | } |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2016 | void EnqueueVisitor::AddExplicitTemplateArgs(const TemplateArgumentLoc *A, |
| 2017 | unsigned NumTemplateArgs) { |
| 2018 | WL.push_back(ExplicitTemplateArgsVisit(A, A + NumTemplateArgs, Parent)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2019 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2020 | void EnqueueVisitor::AddMemberRef(const FieldDecl *D, SourceLocation L) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2021 | if (D) |
| 2022 | WL.push_back(MemberRefVisit(D, L, Parent)); |
| 2023 | } |
| 2024 | void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) { |
| 2025 | if (TI) |
| 2026 | WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent)); |
| 2027 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2028 | void EnqueueVisitor::EnqueueChildren(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2029 | unsigned size = WL.size(); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2030 | for (const Stmt *SubStmt : S->children()) { |
| 2031 | AddStmt(SubStmt); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2032 | } |
| 2033 | if (size == WL.size()) |
| 2034 | return; |
| 2035 | // Now reverse the entries we just added. This will match the DFS |
| 2036 | // ordering performed by the worklist. |
| 2037 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2038 | std::reverse(I, E); |
| 2039 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2040 | namespace { |
| 2041 | class OMPClauseEnqueue : public ConstOMPClauseVisitor<OMPClauseEnqueue> { |
| 2042 | EnqueueVisitor *Visitor; |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2043 | /// \brief Process clauses with list of variables. |
| 2044 | template <typename T> |
| 2045 | void VisitOMPClauseList(T *Node); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2046 | public: |
| 2047 | OMPClauseEnqueue(EnqueueVisitor *Visitor) : Visitor(Visitor) { } |
| 2048 | #define OPENMP_CLAUSE(Name, Class) \ |
| 2049 | void Visit##Class(const Class *C); |
| 2050 | #include "clang/Basic/OpenMPKinds.def" |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2051 | void VisitOMPClauseWithPreInit(const OMPClauseWithPreInit *C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2052 | void VisitOMPClauseWithPostUpdate(const OMPClauseWithPostUpdate *C); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2053 | }; |
| 2054 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2055 | void OMPClauseEnqueue::VisitOMPClauseWithPreInit( |
| 2056 | const OMPClauseWithPreInit *C) { |
| 2057 | Visitor->AddStmt(C->getPreInitStmt()); |
| 2058 | } |
| 2059 | |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2060 | void OMPClauseEnqueue::VisitOMPClauseWithPostUpdate( |
| 2061 | const OMPClauseWithPostUpdate *C) { |
Alexey Bataev | 37e594c | 2016-03-04 07:21:16 +0000 | [diff] [blame] | 2062 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2063 | Visitor->AddStmt(C->getPostUpdateExpr()); |
| 2064 | } |
| 2065 | |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2066 | void OMPClauseEnqueue::VisitOMPIfClause(const OMPIfClause *C) { |
| 2067 | Visitor->AddStmt(C->getCondition()); |
| 2068 | } |
| 2069 | |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 2070 | void OMPClauseEnqueue::VisitOMPFinalClause(const OMPFinalClause *C) { |
| 2071 | Visitor->AddStmt(C->getCondition()); |
| 2072 | } |
| 2073 | |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2074 | void OMPClauseEnqueue::VisitOMPNumThreadsClause(const OMPNumThreadsClause *C) { |
| 2075 | Visitor->AddStmt(C->getNumThreads()); |
| 2076 | } |
| 2077 | |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 2078 | void OMPClauseEnqueue::VisitOMPSafelenClause(const OMPSafelenClause *C) { |
| 2079 | Visitor->AddStmt(C->getSafelen()); |
| 2080 | } |
| 2081 | |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 2082 | void OMPClauseEnqueue::VisitOMPSimdlenClause(const OMPSimdlenClause *C) { |
| 2083 | Visitor->AddStmt(C->getSimdlen()); |
| 2084 | } |
| 2085 | |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 2086 | void OMPClauseEnqueue::VisitOMPCollapseClause(const OMPCollapseClause *C) { |
| 2087 | Visitor->AddStmt(C->getNumForLoops()); |
| 2088 | } |
| 2089 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2090 | void OMPClauseEnqueue::VisitOMPDefaultClause(const OMPDefaultClause *C) { } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2091 | |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 2092 | void OMPClauseEnqueue::VisitOMPProcBindClause(const OMPProcBindClause *C) { } |
| 2093 | |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2094 | void OMPClauseEnqueue::VisitOMPScheduleClause(const OMPScheduleClause *C) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2095 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2096 | Visitor->AddStmt(C->getChunkSize()); |
| 2097 | } |
| 2098 | |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 2099 | void OMPClauseEnqueue::VisitOMPOrderedClause(const OMPOrderedClause *C) { |
| 2100 | Visitor->AddStmt(C->getNumForLoops()); |
| 2101 | } |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 2102 | |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 2103 | void OMPClauseEnqueue::VisitOMPNowaitClause(const OMPNowaitClause *) {} |
| 2104 | |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 2105 | void OMPClauseEnqueue::VisitOMPUntiedClause(const OMPUntiedClause *) {} |
| 2106 | |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 2107 | void OMPClauseEnqueue::VisitOMPMergeableClause(const OMPMergeableClause *) {} |
| 2108 | |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 2109 | void OMPClauseEnqueue::VisitOMPReadClause(const OMPReadClause *) {} |
| 2110 | |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 2111 | void OMPClauseEnqueue::VisitOMPWriteClause(const OMPWriteClause *) {} |
| 2112 | |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 2113 | void OMPClauseEnqueue::VisitOMPUpdateClause(const OMPUpdateClause *) {} |
| 2114 | |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 2115 | void OMPClauseEnqueue::VisitOMPCaptureClause(const OMPCaptureClause *) {} |
| 2116 | |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 2117 | void OMPClauseEnqueue::VisitOMPSeqCstClause(const OMPSeqCstClause *) {} |
| 2118 | |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 2119 | void OMPClauseEnqueue::VisitOMPThreadsClause(const OMPThreadsClause *) {} |
| 2120 | |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 2121 | void OMPClauseEnqueue::VisitOMPSIMDClause(const OMPSIMDClause *) {} |
| 2122 | |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 2123 | void OMPClauseEnqueue::VisitOMPNogroupClause(const OMPNogroupClause *) {} |
| 2124 | |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 2125 | void OMPClauseEnqueue::VisitOMPDeviceClause(const OMPDeviceClause *C) { |
| 2126 | Visitor->AddStmt(C->getDevice()); |
| 2127 | } |
| 2128 | |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 2129 | void OMPClauseEnqueue::VisitOMPNumTeamsClause(const OMPNumTeamsClause *C) { |
| 2130 | Visitor->AddStmt(C->getNumTeams()); |
| 2131 | } |
| 2132 | |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 2133 | void OMPClauseEnqueue::VisitOMPThreadLimitClause(const OMPThreadLimitClause *C) { |
| 2134 | Visitor->AddStmt(C->getThreadLimit()); |
| 2135 | } |
| 2136 | |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 2137 | void OMPClauseEnqueue::VisitOMPPriorityClause(const OMPPriorityClause *C) { |
| 2138 | Visitor->AddStmt(C->getPriority()); |
| 2139 | } |
| 2140 | |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 2141 | void OMPClauseEnqueue::VisitOMPGrainsizeClause(const OMPGrainsizeClause *C) { |
| 2142 | Visitor->AddStmt(C->getGrainsize()); |
| 2143 | } |
| 2144 | |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 2145 | void OMPClauseEnqueue::VisitOMPNumTasksClause(const OMPNumTasksClause *C) { |
| 2146 | Visitor->AddStmt(C->getNumTasks()); |
| 2147 | } |
| 2148 | |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 2149 | void OMPClauseEnqueue::VisitOMPHintClause(const OMPHintClause *C) { |
| 2150 | Visitor->AddStmt(C->getHint()); |
| 2151 | } |
| 2152 | |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2153 | template<typename T> |
| 2154 | void OMPClauseEnqueue::VisitOMPClauseList(T *Node) { |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2155 | for (const auto *I : Node->varlists()) { |
Aaron Ballman | 2205d2a | 2014-03-14 15:55:35 +0000 | [diff] [blame] | 2156 | Visitor->AddStmt(I); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2157 | } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2158 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2159 | |
| 2160 | void OMPClauseEnqueue::VisitOMPPrivateClause(const OMPPrivateClause *C) { |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2161 | VisitOMPClauseList(C); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2162 | for (const auto *E : C->private_copies()) { |
| 2163 | Visitor->AddStmt(E); |
| 2164 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2165 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2166 | void OMPClauseEnqueue::VisitOMPFirstprivateClause( |
| 2167 | const OMPFirstprivateClause *C) { |
| 2168 | VisitOMPClauseList(C); |
Alexey Bataev | 417089f | 2016-02-17 13:19:37 +0000 | [diff] [blame] | 2169 | VisitOMPClauseWithPreInit(C); |
| 2170 | for (const auto *E : C->private_copies()) { |
| 2171 | Visitor->AddStmt(E); |
| 2172 | } |
| 2173 | for (const auto *E : C->inits()) { |
| 2174 | Visitor->AddStmt(E); |
| 2175 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2176 | } |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2177 | void OMPClauseEnqueue::VisitOMPLastprivateClause( |
| 2178 | const OMPLastprivateClause *C) { |
| 2179 | VisitOMPClauseList(C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2180 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2181 | for (auto *E : C->private_copies()) { |
| 2182 | Visitor->AddStmt(E); |
| 2183 | } |
| 2184 | for (auto *E : C->source_exprs()) { |
| 2185 | Visitor->AddStmt(E); |
| 2186 | } |
| 2187 | for (auto *E : C->destination_exprs()) { |
| 2188 | Visitor->AddStmt(E); |
| 2189 | } |
| 2190 | for (auto *E : C->assignment_ops()) { |
| 2191 | Visitor->AddStmt(E); |
| 2192 | } |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2193 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2194 | void OMPClauseEnqueue::VisitOMPSharedClause(const OMPSharedClause *C) { |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2195 | VisitOMPClauseList(C); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2196 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2197 | void OMPClauseEnqueue::VisitOMPReductionClause(const OMPReductionClause *C) { |
| 2198 | VisitOMPClauseList(C); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2199 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 2200 | for (auto *E : C->privates()) { |
| 2201 | Visitor->AddStmt(E); |
| 2202 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2203 | for (auto *E : C->lhs_exprs()) { |
| 2204 | Visitor->AddStmt(E); |
| 2205 | } |
| 2206 | for (auto *E : C->rhs_exprs()) { |
| 2207 | Visitor->AddStmt(E); |
| 2208 | } |
| 2209 | for (auto *E : C->reduction_ops()) { |
| 2210 | Visitor->AddStmt(E); |
| 2211 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2212 | } |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2213 | void OMPClauseEnqueue::VisitOMPLinearClause(const OMPLinearClause *C) { |
| 2214 | VisitOMPClauseList(C); |
Alexey Bataev | 78849fb | 2016-03-09 09:49:00 +0000 | [diff] [blame] | 2215 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 2216 | for (const auto *E : C->privates()) { |
| 2217 | Visitor->AddStmt(E); |
| 2218 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 2219 | for (const auto *E : C->inits()) { |
| 2220 | Visitor->AddStmt(E); |
| 2221 | } |
| 2222 | for (const auto *E : C->updates()) { |
| 2223 | Visitor->AddStmt(E); |
| 2224 | } |
| 2225 | for (const auto *E : C->finals()) { |
| 2226 | Visitor->AddStmt(E); |
| 2227 | } |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2228 | Visitor->AddStmt(C->getStep()); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 2229 | Visitor->AddStmt(C->getCalcStep()); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2230 | } |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 2231 | void OMPClauseEnqueue::VisitOMPAlignedClause(const OMPAlignedClause *C) { |
| 2232 | VisitOMPClauseList(C); |
| 2233 | Visitor->AddStmt(C->getAlignment()); |
| 2234 | } |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 2235 | void OMPClauseEnqueue::VisitOMPCopyinClause(const OMPCopyinClause *C) { |
| 2236 | VisitOMPClauseList(C); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 2237 | for (auto *E : C->source_exprs()) { |
| 2238 | Visitor->AddStmt(E); |
| 2239 | } |
| 2240 | for (auto *E : C->destination_exprs()) { |
| 2241 | Visitor->AddStmt(E); |
| 2242 | } |
| 2243 | for (auto *E : C->assignment_ops()) { |
| 2244 | Visitor->AddStmt(E); |
| 2245 | } |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 2246 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2247 | void |
| 2248 | OMPClauseEnqueue::VisitOMPCopyprivateClause(const OMPCopyprivateClause *C) { |
| 2249 | VisitOMPClauseList(C); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2250 | for (auto *E : C->source_exprs()) { |
| 2251 | Visitor->AddStmt(E); |
| 2252 | } |
| 2253 | for (auto *E : C->destination_exprs()) { |
| 2254 | Visitor->AddStmt(E); |
| 2255 | } |
| 2256 | for (auto *E : C->assignment_ops()) { |
| 2257 | Visitor->AddStmt(E); |
| 2258 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2259 | } |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2260 | void OMPClauseEnqueue::VisitOMPFlushClause(const OMPFlushClause *C) { |
| 2261 | VisitOMPClauseList(C); |
| 2262 | } |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 2263 | void OMPClauseEnqueue::VisitOMPDependClause(const OMPDependClause *C) { |
| 2264 | VisitOMPClauseList(C); |
| 2265 | } |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 2266 | void OMPClauseEnqueue::VisitOMPMapClause(const OMPMapClause *C) { |
| 2267 | VisitOMPClauseList(C); |
| 2268 | } |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2269 | void OMPClauseEnqueue::VisitOMPDistScheduleClause( |
| 2270 | const OMPDistScheduleClause *C) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2271 | VisitOMPClauseWithPreInit(C); |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2272 | Visitor->AddStmt(C->getChunkSize()); |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2273 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2274 | void OMPClauseEnqueue::VisitOMPDefaultmapClause( |
| 2275 | const OMPDefaultmapClause * /*C*/) {} |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 2276 | void OMPClauseEnqueue::VisitOMPToClause(const OMPToClause *C) { |
| 2277 | VisitOMPClauseList(C); |
| 2278 | } |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 2279 | void OMPClauseEnqueue::VisitOMPFromClause(const OMPFromClause *C) { |
| 2280 | VisitOMPClauseList(C); |
| 2281 | } |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 2282 | void OMPClauseEnqueue::VisitOMPUseDevicePtrClause(const OMPUseDevicePtrClause *C) { |
| 2283 | VisitOMPClauseList(C); |
| 2284 | } |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 2285 | void OMPClauseEnqueue::VisitOMPIsDevicePtrClause(const OMPIsDevicePtrClause *C) { |
| 2286 | VisitOMPClauseList(C); |
| 2287 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2288 | } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2289 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2290 | void EnqueueVisitor::EnqueueChildren(const OMPClause *S) { |
| 2291 | unsigned size = WL.size(); |
| 2292 | OMPClauseEnqueue Visitor(this); |
| 2293 | Visitor.Visit(S); |
| 2294 | if (size == WL.size()) |
| 2295 | return; |
| 2296 | // Now reverse the entries we just added. This will match the DFS |
| 2297 | // ordering performed by the worklist. |
| 2298 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2299 | std::reverse(I, E); |
| 2300 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2301 | void EnqueueVisitor::VisitAddrLabelExpr(const AddrLabelExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2302 | WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent)); |
| 2303 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2304 | void EnqueueVisitor::VisitBlockExpr(const BlockExpr *B) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2305 | AddDecl(B->getBlockDecl()); |
| 2306 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2307 | void EnqueueVisitor::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2308 | EnqueueChildren(E); |
| 2309 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2310 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2311 | void EnqueueVisitor::VisitCompoundStmt(const CompoundStmt *S) { |
Pete Cooper | 57d3f14 | 2015-07-30 17:22:52 +0000 | [diff] [blame] | 2312 | for (auto &I : llvm::reverse(S->body())) |
| 2313 | AddStmt(I); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2314 | } |
| 2315 | void EnqueueVisitor:: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2316 | VisitMSDependentExistsStmt(const MSDependentExistsStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2317 | AddStmt(S->getSubStmt()); |
| 2318 | AddDeclarationNameInfo(S); |
| 2319 | if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc()) |
| 2320 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2321 | } |
| 2322 | |
| 2323 | void EnqueueVisitor:: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2324 | VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2325 | if (E->hasExplicitTemplateArgs()) |
| 2326 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2327 | AddDeclarationNameInfo(E); |
| 2328 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 2329 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2330 | if (!E->isImplicitAccess()) |
| 2331 | AddStmt(E->getBase()); |
| 2332 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2333 | void EnqueueVisitor::VisitCXXNewExpr(const CXXNewExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2334 | // Enqueue the initializer , if any. |
| 2335 | AddStmt(E->getInitializer()); |
| 2336 | // Enqueue the array size, if any. |
| 2337 | AddStmt(E->getArraySize()); |
| 2338 | // Enqueue the allocated type. |
| 2339 | AddTypeLoc(E->getAllocatedTypeSourceInfo()); |
| 2340 | // Enqueue the placement arguments. |
| 2341 | for (unsigned I = E->getNumPlacementArgs(); I > 0; --I) |
| 2342 | AddStmt(E->getPlacementArg(I-1)); |
| 2343 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2344 | void EnqueueVisitor::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CE) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2345 | for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I) |
| 2346 | AddStmt(CE->getArg(I-1)); |
| 2347 | AddStmt(CE->getCallee()); |
| 2348 | AddStmt(CE->getArg(0)); |
| 2349 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2350 | void EnqueueVisitor::VisitCXXPseudoDestructorExpr( |
| 2351 | const CXXPseudoDestructorExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2352 | // Visit the name of the type being destroyed. |
| 2353 | AddTypeLoc(E->getDestroyedTypeInfo()); |
| 2354 | // Visit the scope type that looks disturbingly like the nested-name-specifier |
| 2355 | // but isn't. |
| 2356 | AddTypeLoc(E->getScopeTypeInfo()); |
| 2357 | // Visit the nested-name-specifier. |
| 2358 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 2359 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2360 | // Visit base expression. |
| 2361 | AddStmt(E->getBase()); |
| 2362 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2363 | void EnqueueVisitor::VisitCXXScalarValueInitExpr( |
| 2364 | const CXXScalarValueInitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2365 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2366 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2367 | void EnqueueVisitor::VisitCXXTemporaryObjectExpr( |
| 2368 | const CXXTemporaryObjectExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2369 | EnqueueChildren(E); |
| 2370 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2371 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2372 | void EnqueueVisitor::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2373 | EnqueueChildren(E); |
| 2374 | if (E->isTypeOperand()) |
| 2375 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 2376 | } |
| 2377 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2378 | void EnqueueVisitor::VisitCXXUnresolvedConstructExpr( |
| 2379 | const CXXUnresolvedConstructExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2380 | EnqueueChildren(E); |
| 2381 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2382 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2383 | void EnqueueVisitor::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2384 | EnqueueChildren(E); |
| 2385 | if (E->isTypeOperand()) |
| 2386 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 2387 | } |
| 2388 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2389 | void EnqueueVisitor::VisitCXXCatchStmt(const CXXCatchStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2390 | EnqueueChildren(S); |
| 2391 | AddDecl(S->getExceptionDecl()); |
| 2392 | } |
| 2393 | |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2394 | void EnqueueVisitor::VisitCXXForRangeStmt(const CXXForRangeStmt *S) { |
Argyrios Kyrtzidis | cde7069 | 2014-11-13 09:50:19 +0000 | [diff] [blame] | 2395 | AddStmt(S->getBody()); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2396 | AddStmt(S->getRangeInit()); |
Argyrios Kyrtzidis | cde7069 | 2014-11-13 09:50:19 +0000 | [diff] [blame] | 2397 | AddDecl(S->getLoopVariable()); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2400 | void EnqueueVisitor::VisitDeclRefExpr(const DeclRefExpr *DR) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2401 | if (DR->hasExplicitTemplateArgs()) |
| 2402 | AddExplicitTemplateArgs(DR->getTemplateArgs(), DR->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2403 | WL.push_back(DeclRefExprParts(DR, Parent)); |
| 2404 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2405 | void EnqueueVisitor::VisitDependentScopeDeclRefExpr( |
| 2406 | const DependentScopeDeclRefExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2407 | if (E->hasExplicitTemplateArgs()) |
| 2408 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2409 | AddDeclarationNameInfo(E); |
| 2410 | AddNestedNameSpecifierLoc(E->getQualifierLoc()); |
| 2411 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2412 | void EnqueueVisitor::VisitDeclStmt(const DeclStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2413 | unsigned size = WL.size(); |
| 2414 | bool isFirst = true; |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 2415 | for (const auto *D : S->decls()) { |
| 2416 | AddDecl(D, isFirst); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2417 | isFirst = false; |
| 2418 | } |
| 2419 | if (size == WL.size()) |
| 2420 | return; |
| 2421 | // Now reverse the entries we just added. This will match the DFS |
| 2422 | // ordering performed by the worklist. |
| 2423 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2424 | std::reverse(I, E); |
| 2425 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2426 | void EnqueueVisitor::VisitDesignatedInitExpr(const DesignatedInitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2427 | AddStmt(E->getInit()); |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2428 | for (const DesignatedInitExpr::Designator &D : |
| 2429 | llvm::reverse(E->designators())) { |
| 2430 | if (D.isFieldDesignator()) { |
| 2431 | if (FieldDecl *Field = D.getField()) |
| 2432 | AddMemberRef(Field, D.getFieldLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2433 | continue; |
| 2434 | } |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2435 | if (D.isArrayDesignator()) { |
| 2436 | AddStmt(E->getArrayIndex(D)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2437 | continue; |
| 2438 | } |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 2439 | assert(D.isArrayRangeDesignator() && "Unknown designator kind"); |
| 2440 | AddStmt(E->getArrayRangeEnd(D)); |
| 2441 | AddStmt(E->getArrayRangeStart(D)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2442 | } |
| 2443 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2444 | void EnqueueVisitor::VisitExplicitCastExpr(const ExplicitCastExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2445 | EnqueueChildren(E); |
| 2446 | AddTypeLoc(E->getTypeInfoAsWritten()); |
| 2447 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2448 | void EnqueueVisitor::VisitForStmt(const ForStmt *FS) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2449 | AddStmt(FS->getBody()); |
| 2450 | AddStmt(FS->getInc()); |
| 2451 | AddStmt(FS->getCond()); |
| 2452 | AddDecl(FS->getConditionVariable()); |
| 2453 | AddStmt(FS->getInit()); |
| 2454 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2455 | void EnqueueVisitor::VisitGotoStmt(const GotoStmt *GS) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2456 | WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent)); |
| 2457 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2458 | void EnqueueVisitor::VisitIfStmt(const IfStmt *If) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2459 | AddStmt(If->getElse()); |
| 2460 | AddStmt(If->getThen()); |
| 2461 | AddStmt(If->getCond()); |
| 2462 | AddDecl(If->getConditionVariable()); |
| 2463 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2464 | void EnqueueVisitor::VisitInitListExpr(const InitListExpr *IE) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2465 | // We care about the syntactic form of the initializer list, only. |
| 2466 | if (InitListExpr *Syntactic = IE->getSyntacticForm()) |
| 2467 | IE = Syntactic; |
| 2468 | EnqueueChildren(IE); |
| 2469 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2470 | void EnqueueVisitor::VisitMemberExpr(const MemberExpr *M) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2471 | WL.push_back(MemberExprParts(M, Parent)); |
| 2472 | |
| 2473 | // If the base of the member access expression is an implicit 'this', don't |
| 2474 | // visit it. |
| 2475 | // FIXME: If we ever want to show these implicit accesses, this will be |
| 2476 | // unfortunate. However, clang_getCursor() relies on this behavior. |
Argyrios Kyrtzidis | 58d0e7a | 2015-03-13 04:40:07 +0000 | [diff] [blame] | 2477 | if (M->isImplicitAccess()) |
| 2478 | return; |
| 2479 | |
| 2480 | // Ignore base anonymous struct/union fields, otherwise they will shadow the |
| 2481 | // real field that that we are interested in. |
| 2482 | if (auto *SubME = dyn_cast<MemberExpr>(M->getBase())) { |
| 2483 | if (auto *FD = dyn_cast_or_null<FieldDecl>(SubME->getMemberDecl())) { |
| 2484 | if (FD->isAnonymousStructOrUnion()) { |
| 2485 | AddStmt(SubME->getBase()); |
| 2486 | return; |
| 2487 | } |
| 2488 | } |
| 2489 | } |
| 2490 | |
| 2491 | AddStmt(M->getBase()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2492 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2493 | void EnqueueVisitor::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2494 | AddTypeLoc(E->getEncodedTypeSourceInfo()); |
| 2495 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2496 | void EnqueueVisitor::VisitObjCMessageExpr(const ObjCMessageExpr *M) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2497 | EnqueueChildren(M); |
| 2498 | AddTypeLoc(M->getClassReceiverTypeInfo()); |
| 2499 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2500 | void EnqueueVisitor::VisitOffsetOfExpr(const OffsetOfExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2501 | // Visit the components of the offsetof expression. |
| 2502 | for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2503 | const OffsetOfNode &Node = E->getComponent(I-1); |
| 2504 | switch (Node.getKind()) { |
| 2505 | case OffsetOfNode::Array: |
| 2506 | AddStmt(E->getIndexExpr(Node.getArrayExprIndex())); |
| 2507 | break; |
| 2508 | case OffsetOfNode::Field: |
| 2509 | AddMemberRef(Node.getField(), Node.getSourceRange().getEnd()); |
| 2510 | break; |
| 2511 | case OffsetOfNode::Identifier: |
| 2512 | case OffsetOfNode::Base: |
| 2513 | continue; |
| 2514 | } |
| 2515 | } |
| 2516 | // Visit the type into which we're computing the offset. |
| 2517 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2518 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2519 | void EnqueueVisitor::VisitOverloadExpr(const OverloadExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2520 | if (E->hasExplicitTemplateArgs()) |
| 2521 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2522 | WL.push_back(OverloadExprParts(E, Parent)); |
| 2523 | } |
| 2524 | void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr( |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2525 | const UnaryExprOrTypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2526 | EnqueueChildren(E); |
| 2527 | if (E->isArgumentType()) |
| 2528 | AddTypeLoc(E->getArgumentTypeInfo()); |
| 2529 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2530 | void EnqueueVisitor::VisitStmt(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2531 | EnqueueChildren(S); |
| 2532 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2533 | void EnqueueVisitor::VisitSwitchStmt(const SwitchStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2534 | AddStmt(S->getBody()); |
| 2535 | AddStmt(S->getCond()); |
| 2536 | AddDecl(S->getConditionVariable()); |
| 2537 | } |
| 2538 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2539 | void EnqueueVisitor::VisitWhileStmt(const WhileStmt *W) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2540 | AddStmt(W->getBody()); |
| 2541 | AddStmt(W->getCond()); |
| 2542 | AddDecl(W->getConditionVariable()); |
| 2543 | } |
| 2544 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2545 | void EnqueueVisitor::VisitTypeTraitExpr(const TypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2546 | for (unsigned I = E->getNumArgs(); I > 0; --I) |
| 2547 | AddTypeLoc(E->getArg(I-1)); |
| 2548 | } |
| 2549 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2550 | void EnqueueVisitor::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2551 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2552 | } |
| 2553 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2554 | void EnqueueVisitor::VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2555 | EnqueueChildren(E); |
| 2556 | } |
| 2557 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2558 | void EnqueueVisitor::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2559 | VisitOverloadExpr(U); |
| 2560 | if (!U->isImplicitAccess()) |
| 2561 | AddStmt(U->getBase()); |
| 2562 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2563 | void EnqueueVisitor::VisitVAArgExpr(const VAArgExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2564 | AddStmt(E->getSubExpr()); |
| 2565 | AddTypeLoc(E->getWrittenTypeInfo()); |
| 2566 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2567 | void EnqueueVisitor::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2568 | WL.push_back(SizeOfPackExprParts(E, Parent)); |
| 2569 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2570 | void EnqueueVisitor::VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2571 | // If the opaque value has a source expression, just transparently |
| 2572 | // visit that. This is useful for (e.g.) pseudo-object expressions. |
| 2573 | if (Expr *SourceExpr = E->getSourceExpr()) |
| 2574 | return Visit(SourceExpr); |
| 2575 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2576 | void EnqueueVisitor::VisitLambdaExpr(const LambdaExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2577 | AddStmt(E->getBody()); |
| 2578 | WL.push_back(LambdaExprParts(E, Parent)); |
| 2579 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2580 | void EnqueueVisitor::VisitPseudoObjectExpr(const PseudoObjectExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2581 | // Treat the expression like its syntactic form. |
| 2582 | Visit(E->getSyntacticForm()); |
| 2583 | } |
| 2584 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2585 | void EnqueueVisitor::VisitOMPExecutableDirective( |
| 2586 | const OMPExecutableDirective *D) { |
| 2587 | EnqueueChildren(D); |
| 2588 | for (ArrayRef<OMPClause *>::iterator I = D->clauses().begin(), |
| 2589 | E = D->clauses().end(); |
| 2590 | I != E; ++I) |
| 2591 | EnqueueChildren(*I); |
| 2592 | } |
| 2593 | |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2594 | void EnqueueVisitor::VisitOMPLoopDirective(const OMPLoopDirective *D) { |
| 2595 | VisitOMPExecutableDirective(D); |
| 2596 | } |
| 2597 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2598 | void EnqueueVisitor::VisitOMPParallelDirective(const OMPParallelDirective *D) { |
| 2599 | VisitOMPExecutableDirective(D); |
| 2600 | } |
| 2601 | |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2602 | void EnqueueVisitor::VisitOMPSimdDirective(const OMPSimdDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2603 | VisitOMPLoopDirective(D); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2606 | void EnqueueVisitor::VisitOMPForDirective(const OMPForDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2607 | VisitOMPLoopDirective(D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2610 | void EnqueueVisitor::VisitOMPForSimdDirective(const OMPForSimdDirective *D) { |
| 2611 | VisitOMPLoopDirective(D); |
| 2612 | } |
| 2613 | |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2614 | void EnqueueVisitor::VisitOMPSectionsDirective(const OMPSectionsDirective *D) { |
| 2615 | VisitOMPExecutableDirective(D); |
| 2616 | } |
| 2617 | |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2618 | void EnqueueVisitor::VisitOMPSectionDirective(const OMPSectionDirective *D) { |
| 2619 | VisitOMPExecutableDirective(D); |
| 2620 | } |
| 2621 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 2622 | void EnqueueVisitor::VisitOMPSingleDirective(const OMPSingleDirective *D) { |
| 2623 | VisitOMPExecutableDirective(D); |
| 2624 | } |
| 2625 | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2626 | void EnqueueVisitor::VisitOMPMasterDirective(const OMPMasterDirective *D) { |
| 2627 | VisitOMPExecutableDirective(D); |
| 2628 | } |
| 2629 | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2630 | void EnqueueVisitor::VisitOMPCriticalDirective(const OMPCriticalDirective *D) { |
| 2631 | VisitOMPExecutableDirective(D); |
| 2632 | AddDeclarationNameInfo(D); |
| 2633 | } |
| 2634 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2635 | void |
| 2636 | EnqueueVisitor::VisitOMPParallelForDirective(const OMPParallelForDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2637 | VisitOMPLoopDirective(D); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2640 | void EnqueueVisitor::VisitOMPParallelForSimdDirective( |
| 2641 | const OMPParallelForSimdDirective *D) { |
| 2642 | VisitOMPLoopDirective(D); |
| 2643 | } |
| 2644 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2645 | void EnqueueVisitor::VisitOMPParallelSectionsDirective( |
| 2646 | const OMPParallelSectionsDirective *D) { |
| 2647 | VisitOMPExecutableDirective(D); |
| 2648 | } |
| 2649 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2650 | void EnqueueVisitor::VisitOMPTaskDirective(const OMPTaskDirective *D) { |
| 2651 | VisitOMPExecutableDirective(D); |
| 2652 | } |
| 2653 | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2654 | void |
| 2655 | EnqueueVisitor::VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D) { |
| 2656 | VisitOMPExecutableDirective(D); |
| 2657 | } |
| 2658 | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 2659 | void EnqueueVisitor::VisitOMPBarrierDirective(const OMPBarrierDirective *D) { |
| 2660 | VisitOMPExecutableDirective(D); |
| 2661 | } |
| 2662 | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 2663 | void EnqueueVisitor::VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D) { |
| 2664 | VisitOMPExecutableDirective(D); |
| 2665 | } |
| 2666 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2667 | void EnqueueVisitor::VisitOMPTaskgroupDirective( |
| 2668 | const OMPTaskgroupDirective *D) { |
| 2669 | VisitOMPExecutableDirective(D); |
| 2670 | } |
| 2671 | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2672 | void EnqueueVisitor::VisitOMPFlushDirective(const OMPFlushDirective *D) { |
| 2673 | VisitOMPExecutableDirective(D); |
| 2674 | } |
| 2675 | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2676 | void EnqueueVisitor::VisitOMPOrderedDirective(const OMPOrderedDirective *D) { |
| 2677 | VisitOMPExecutableDirective(D); |
| 2678 | } |
| 2679 | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2680 | void EnqueueVisitor::VisitOMPAtomicDirective(const OMPAtomicDirective *D) { |
| 2681 | VisitOMPExecutableDirective(D); |
| 2682 | } |
| 2683 | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 2684 | void EnqueueVisitor::VisitOMPTargetDirective(const OMPTargetDirective *D) { |
| 2685 | VisitOMPExecutableDirective(D); |
| 2686 | } |
| 2687 | |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2688 | void EnqueueVisitor::VisitOMPTargetDataDirective(const |
| 2689 | OMPTargetDataDirective *D) { |
| 2690 | VisitOMPExecutableDirective(D); |
| 2691 | } |
| 2692 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 2693 | void EnqueueVisitor::VisitOMPTargetEnterDataDirective( |
| 2694 | const OMPTargetEnterDataDirective *D) { |
| 2695 | VisitOMPExecutableDirective(D); |
| 2696 | } |
| 2697 | |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 2698 | void EnqueueVisitor::VisitOMPTargetExitDataDirective( |
| 2699 | const OMPTargetExitDataDirective *D) { |
| 2700 | VisitOMPExecutableDirective(D); |
| 2701 | } |
| 2702 | |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 2703 | void EnqueueVisitor::VisitOMPTargetParallelDirective( |
| 2704 | const OMPTargetParallelDirective *D) { |
| 2705 | VisitOMPExecutableDirective(D); |
| 2706 | } |
| 2707 | |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 2708 | void EnqueueVisitor::VisitOMPTargetParallelForDirective( |
| 2709 | const OMPTargetParallelForDirective *D) { |
| 2710 | VisitOMPLoopDirective(D); |
| 2711 | } |
| 2712 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2713 | void EnqueueVisitor::VisitOMPTeamsDirective(const OMPTeamsDirective *D) { |
| 2714 | VisitOMPExecutableDirective(D); |
| 2715 | } |
| 2716 | |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2717 | void EnqueueVisitor::VisitOMPCancellationPointDirective( |
| 2718 | const OMPCancellationPointDirective *D) { |
| 2719 | VisitOMPExecutableDirective(D); |
| 2720 | } |
| 2721 | |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2722 | void EnqueueVisitor::VisitOMPCancelDirective(const OMPCancelDirective *D) { |
| 2723 | VisitOMPExecutableDirective(D); |
| 2724 | } |
| 2725 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 2726 | void EnqueueVisitor::VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D) { |
| 2727 | VisitOMPLoopDirective(D); |
| 2728 | } |
| 2729 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 2730 | void EnqueueVisitor::VisitOMPTaskLoopSimdDirective( |
| 2731 | const OMPTaskLoopSimdDirective *D) { |
| 2732 | VisitOMPLoopDirective(D); |
| 2733 | } |
| 2734 | |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2735 | void EnqueueVisitor::VisitOMPDistributeDirective( |
| 2736 | const OMPDistributeDirective *D) { |
| 2737 | VisitOMPLoopDirective(D); |
| 2738 | } |
| 2739 | |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2740 | void EnqueueVisitor::VisitOMPDistributeParallelForDirective( |
| 2741 | const OMPDistributeParallelForDirective *D) { |
| 2742 | VisitOMPLoopDirective(D); |
| 2743 | } |
| 2744 | |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2745 | void EnqueueVisitor::VisitOMPDistributeParallelForSimdDirective( |
| 2746 | const OMPDistributeParallelForSimdDirective *D) { |
| 2747 | VisitOMPLoopDirective(D); |
| 2748 | } |
| 2749 | |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2750 | void EnqueueVisitor::VisitOMPDistributeSimdDirective( |
| 2751 | const OMPDistributeSimdDirective *D) { |
| 2752 | VisitOMPLoopDirective(D); |
| 2753 | } |
| 2754 | |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 2755 | void EnqueueVisitor::VisitOMPTargetParallelForSimdDirective( |
| 2756 | const OMPTargetParallelForSimdDirective *D) { |
| 2757 | VisitOMPLoopDirective(D); |
| 2758 | } |
| 2759 | |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2760 | void EnqueueVisitor::VisitOMPTargetSimdDirective( |
| 2761 | const OMPTargetSimdDirective *D) { |
| 2762 | VisitOMPLoopDirective(D); |
| 2763 | } |
| 2764 | |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 2765 | void EnqueueVisitor::VisitOMPTeamsDistributeDirective( |
| 2766 | const OMPTeamsDistributeDirective *D) { |
| 2767 | VisitOMPLoopDirective(D); |
| 2768 | } |
| 2769 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2770 | void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2771 | EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S); |
| 2772 | } |
| 2773 | |
| 2774 | bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { |
| 2775 | if (RegionOfInterest.isValid()) { |
| 2776 | SourceRange Range = getRawCursorExtent(C); |
| 2777 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 2778 | return false; |
| 2779 | } |
| 2780 | return true; |
| 2781 | } |
| 2782 | |
| 2783 | bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { |
| 2784 | while (!WL.empty()) { |
| 2785 | // Dequeue the worklist item. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2786 | VisitorJob LI = WL.pop_back_val(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2787 | |
| 2788 | // Set the Parent field, then back to its old value once we're done. |
| 2789 | SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); |
| 2790 | |
| 2791 | switch (LI.getKind()) { |
| 2792 | case VisitorJob::DeclVisitKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2793 | const Decl *D = cast<DeclVisit>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2794 | if (!D) |
| 2795 | continue; |
| 2796 | |
| 2797 | // For now, perform default visitation for Decls. |
| 2798 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest, |
| 2799 | cast<DeclVisit>(&LI)->isFirst()))) |
| 2800 | return true; |
| 2801 | |
| 2802 | continue; |
| 2803 | } |
| 2804 | case VisitorJob::ExplicitTemplateArgsVisitKind: { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2805 | for (const TemplateArgumentLoc &Arg : |
| 2806 | *cast<ExplicitTemplateArgsVisit>(&LI)) { |
| 2807 | if (VisitTemplateArgumentLoc(Arg)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2808 | return true; |
| 2809 | } |
| 2810 | continue; |
| 2811 | } |
| 2812 | case VisitorJob::TypeLocVisitKind: { |
| 2813 | // Perform default visitation for TypeLocs. |
| 2814 | if (Visit(cast<TypeLocVisit>(&LI)->get())) |
| 2815 | return true; |
| 2816 | continue; |
| 2817 | } |
| 2818 | case VisitorJob::LabelRefVisitKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2819 | const LabelDecl *LS = cast<LabelRefVisit>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2820 | if (LabelStmt *stmt = LS->getStmt()) { |
| 2821 | if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(), |
| 2822 | TU))) { |
| 2823 | return true; |
| 2824 | } |
| 2825 | } |
| 2826 | continue; |
| 2827 | } |
| 2828 | |
| 2829 | case VisitorJob::NestedNameSpecifierLocVisitKind: { |
| 2830 | NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI); |
| 2831 | if (VisitNestedNameSpecifierLoc(V->get())) |
| 2832 | return true; |
| 2833 | continue; |
| 2834 | } |
| 2835 | |
| 2836 | case VisitorJob::DeclarationNameInfoVisitKind: { |
| 2837 | if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI) |
| 2838 | ->get())) |
| 2839 | return true; |
| 2840 | continue; |
| 2841 | } |
| 2842 | case VisitorJob::MemberRefVisitKind: { |
| 2843 | MemberRefVisit *V = cast<MemberRefVisit>(&LI); |
| 2844 | if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU))) |
| 2845 | return true; |
| 2846 | continue; |
| 2847 | } |
| 2848 | case VisitorJob::StmtVisitKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2849 | const Stmt *S = cast<StmtVisit>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2850 | if (!S) |
| 2851 | continue; |
| 2852 | |
| 2853 | // Update the current cursor. |
| 2854 | CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest); |
| 2855 | if (!IsInRegionOfInterest(Cursor)) |
| 2856 | continue; |
| 2857 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 2858 | case CXChildVisit_Break: return true; |
| 2859 | case CXChildVisit_Continue: break; |
| 2860 | case CXChildVisit_Recurse: |
| 2861 | if (PostChildrenVisitor) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 2862 | WL.push_back(PostChildrenVisit(nullptr, Cursor)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2863 | EnqueueWorkList(WL, S); |
| 2864 | break; |
| 2865 | } |
| 2866 | continue; |
| 2867 | } |
| 2868 | case VisitorJob::MemberExprPartsKind: { |
| 2869 | // Handle the other pieces in the MemberExpr besides the base. |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2870 | const MemberExpr *M = cast<MemberExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2871 | |
| 2872 | // Visit the nested-name-specifier |
| 2873 | if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc()) |
| 2874 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 2875 | return true; |
| 2876 | |
| 2877 | // Visit the declaration name. |
| 2878 | if (VisitDeclarationNameInfo(M->getMemberNameInfo())) |
| 2879 | return true; |
| 2880 | |
| 2881 | // Visit the explicitly-specified template arguments, if any. |
| 2882 | if (M->hasExplicitTemplateArgs()) { |
| 2883 | for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), |
| 2884 | *ArgEnd = Arg + M->getNumTemplateArgs(); |
| 2885 | Arg != ArgEnd; ++Arg) { |
| 2886 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2887 | return true; |
| 2888 | } |
| 2889 | } |
| 2890 | continue; |
| 2891 | } |
| 2892 | case VisitorJob::DeclRefExprPartsKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2893 | const DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2894 | // Visit nested-name-specifier, if present. |
| 2895 | if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc()) |
| 2896 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 2897 | return true; |
| 2898 | // Visit declaration name. |
| 2899 | if (VisitDeclarationNameInfo(DR->getNameInfo())) |
| 2900 | return true; |
| 2901 | continue; |
| 2902 | } |
| 2903 | case VisitorJob::OverloadExprPartsKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2904 | const OverloadExpr *O = cast<OverloadExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2905 | // Visit the nested-name-specifier. |
| 2906 | if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc()) |
| 2907 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 2908 | return true; |
| 2909 | // Visit the declaration name. |
| 2910 | if (VisitDeclarationNameInfo(O->getNameInfo())) |
| 2911 | return true; |
| 2912 | // Visit the overloaded declaration reference. |
| 2913 | if (Visit(MakeCursorOverloadedDeclRef(O, TU))) |
| 2914 | return true; |
| 2915 | continue; |
| 2916 | } |
| 2917 | case VisitorJob::SizeOfPackExprPartsKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2918 | const SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2919 | NamedDecl *Pack = E->getPack(); |
| 2920 | if (isa<TemplateTypeParmDecl>(Pack)) { |
| 2921 | if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack), |
| 2922 | E->getPackLoc(), TU))) |
| 2923 | return true; |
| 2924 | |
| 2925 | continue; |
| 2926 | } |
| 2927 | |
| 2928 | if (isa<TemplateTemplateParmDecl>(Pack)) { |
| 2929 | if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack), |
| 2930 | E->getPackLoc(), TU))) |
| 2931 | return true; |
| 2932 | |
| 2933 | continue; |
| 2934 | } |
| 2935 | |
| 2936 | // Non-type template parameter packs and function parameter packs are |
| 2937 | // treated like DeclRefExpr cursors. |
| 2938 | continue; |
| 2939 | } |
| 2940 | |
| 2941 | case VisitorJob::LambdaExprPartsKind: { |
| 2942 | // Visit captures. |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2943 | const LambdaExpr *E = cast<LambdaExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2944 | for (LambdaExpr::capture_iterator C = E->explicit_capture_begin(), |
| 2945 | CEnd = E->explicit_capture_end(); |
| 2946 | C != CEnd; ++C) { |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 2947 | // FIXME: Lambda init-captures. |
| 2948 | if (!C->capturesVariable()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2949 | continue; |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 2950 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2951 | if (Visit(MakeCursorVariableRef(C->getCapturedVar(), |
| 2952 | C->getLocation(), |
| 2953 | TU))) |
| 2954 | return true; |
| 2955 | } |
| 2956 | |
| 2957 | // Visit parameters and return type, if present. |
| 2958 | if (E->hasExplicitParameters() || E->hasExplicitResultType()) { |
| 2959 | TypeLoc TL = E->getCallOperator()->getTypeSourceInfo()->getTypeLoc(); |
| 2960 | if (E->hasExplicitParameters() && E->hasExplicitResultType()) { |
| 2961 | // Visit the whole type. |
| 2962 | if (Visit(TL)) |
| 2963 | return true; |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2964 | } else if (FunctionProtoTypeLoc Proto = |
| 2965 | TL.getAs<FunctionProtoTypeLoc>()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2966 | if (E->hasExplicitParameters()) { |
| 2967 | // Visit parameters. |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2968 | for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) |
| 2969 | if (Visit(MakeCXCursor(Proto.getParam(I), TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2970 | return true; |
| 2971 | } else { |
| 2972 | // Visit result type. |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 2973 | if (Visit(Proto.getReturnLoc())) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2974 | return true; |
| 2975 | } |
| 2976 | } |
| 2977 | } |
| 2978 | break; |
| 2979 | } |
| 2980 | |
| 2981 | case VisitorJob::PostChildrenVisitKind: |
| 2982 | if (PostChildrenVisitor(Parent, ClientData)) |
| 2983 | return true; |
| 2984 | break; |
| 2985 | } |
| 2986 | } |
| 2987 | return false; |
| 2988 | } |
| 2989 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2990 | bool CursorVisitor::Visit(const Stmt *S) { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 2991 | VisitorWorkList *WL = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2992 | if (!WorkListFreeList.empty()) { |
| 2993 | WL = WorkListFreeList.back(); |
| 2994 | WL->clear(); |
| 2995 | WorkListFreeList.pop_back(); |
| 2996 | } |
| 2997 | else { |
| 2998 | WL = new VisitorWorkList(); |
| 2999 | WorkListCache.push_back(WL); |
| 3000 | } |
| 3001 | EnqueueWorkList(*WL, S); |
| 3002 | bool result = RunVisitorWorkList(*WL); |
| 3003 | WorkListFreeList.push_back(WL); |
| 3004 | return result; |
| 3005 | } |
| 3006 | |
| 3007 | namespace { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3008 | typedef SmallVector<SourceRange, 4> RefNamePieces; |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3009 | RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr, |
| 3010 | const DeclarationNameInfo &NI, SourceRange QLoc, |
| 3011 | const SourceRange *TemplateArgsLoc = nullptr) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3012 | const bool WantQualifier = NameFlags & CXNameRange_WantQualifier; |
| 3013 | const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs; |
| 3014 | const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece; |
| 3015 | |
| 3016 | const DeclarationName::NameKind Kind = NI.getName().getNameKind(); |
| 3017 | |
| 3018 | RefNamePieces Pieces; |
| 3019 | |
| 3020 | if (WantQualifier && QLoc.isValid()) |
| 3021 | Pieces.push_back(QLoc); |
| 3022 | |
| 3023 | if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr) |
| 3024 | Pieces.push_back(NI.getLoc()); |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3025 | |
| 3026 | if (WantTemplateArgs && TemplateArgsLoc && TemplateArgsLoc->isValid()) |
| 3027 | Pieces.push_back(*TemplateArgsLoc); |
| 3028 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3029 | if (Kind == DeclarationName::CXXOperatorName) { |
| 3030 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 3031 | NI.getInfo().CXXOperatorName.BeginOpNameLoc)); |
| 3032 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 3033 | NI.getInfo().CXXOperatorName.EndOpNameLoc)); |
| 3034 | } |
| 3035 | |
| 3036 | if (WantSinglePiece) { |
| 3037 | SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd()); |
| 3038 | Pieces.clear(); |
| 3039 | Pieces.push_back(R); |
| 3040 | } |
| 3041 | |
| 3042 | return Pieces; |
| 3043 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3044 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3045 | |
| 3046 | //===----------------------------------------------------------------------===// |
| 3047 | // Misc. API hooks. |
| 3048 | //===----------------------------------------------------------------------===// |
| 3049 | |
Chad Rosier | 05c71aa | 2013-03-27 18:28:23 +0000 | [diff] [blame] | 3050 | static void fatal_error_handler(void *user_data, const std::string& reason, |
| 3051 | bool gen_crash_diag) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3052 | // Write the result out to stderr avoiding errs() because raw_ostreams can |
| 3053 | // call report_fatal_error. |
| 3054 | fprintf(stderr, "LIBCLANG FATAL ERROR: %s\n", reason.c_str()); |
| 3055 | ::abort(); |
| 3056 | } |
| 3057 | |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 3058 | namespace { |
| 3059 | struct RegisterFatalErrorHandler { |
| 3060 | RegisterFatalErrorHandler() { |
| 3061 | llvm::install_fatal_error_handler(fatal_error_handler, nullptr); |
| 3062 | } |
| 3063 | }; |
| 3064 | } |
| 3065 | |
| 3066 | static llvm::ManagedStatic<RegisterFatalErrorHandler> RegisterFatalErrorHandlerOnce; |
| 3067 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3068 | extern "C" { |
| 3069 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 3070 | int displayDiagnostics) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3071 | // We use crash recovery to make some of our APIs more reliable, implicitly |
| 3072 | // enable it. |
Argyrios Kyrtzidis | 3701f54 | 2013-11-27 08:58:09 +0000 | [diff] [blame] | 3073 | if (!getenv("LIBCLANG_DISABLE_CRASH_RECOVERY")) |
| 3074 | llvm::CrashRecoveryContext::Enable(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3075 | |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 3076 | // Look through the managed static to trigger construction of the managed |
| 3077 | // static which registers our fatal error handler. This ensures it is only |
| 3078 | // registered once. |
| 3079 | (void)*RegisterFatalErrorHandlerOnce; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3080 | |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 3081 | // Initialize targets for clang module support. |
| 3082 | llvm::InitializeAllTargets(); |
| 3083 | llvm::InitializeAllTargetMCs(); |
| 3084 | llvm::InitializeAllAsmPrinters(); |
| 3085 | llvm::InitializeAllAsmParsers(); |
| 3086 | |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 3087 | CIndexer *CIdxr = new CIndexer(); |
| 3088 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3089 | if (excludeDeclarationsFromPCH) |
| 3090 | CIdxr->setOnlyLocalDecls(); |
| 3091 | if (displayDiagnostics) |
| 3092 | CIdxr->setDisplayDiagnostics(); |
| 3093 | |
| 3094 | if (getenv("LIBCLANG_BGPRIO_INDEX")) |
| 3095 | CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() | |
| 3096 | CXGlobalOpt_ThreadBackgroundPriorityForIndexing); |
| 3097 | if (getenv("LIBCLANG_BGPRIO_EDIT")) |
| 3098 | CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() | |
| 3099 | CXGlobalOpt_ThreadBackgroundPriorityForEditing); |
| 3100 | |
| 3101 | return CIdxr; |
| 3102 | } |
| 3103 | |
| 3104 | void clang_disposeIndex(CXIndex CIdx) { |
| 3105 | if (CIdx) |
| 3106 | delete static_cast<CIndexer *>(CIdx); |
| 3107 | } |
| 3108 | |
| 3109 | void clang_CXIndex_setGlobalOptions(CXIndex CIdx, unsigned options) { |
| 3110 | if (CIdx) |
| 3111 | static_cast<CIndexer *>(CIdx)->setCXGlobalOptFlags(options); |
| 3112 | } |
| 3113 | |
| 3114 | unsigned clang_CXIndex_getGlobalOptions(CXIndex CIdx) { |
| 3115 | if (CIdx) |
| 3116 | return static_cast<CIndexer *>(CIdx)->getCXGlobalOptFlags(); |
| 3117 | return 0; |
| 3118 | } |
| 3119 | |
| 3120 | void clang_toggleCrashRecovery(unsigned isEnabled) { |
| 3121 | if (isEnabled) |
| 3122 | llvm::CrashRecoveryContext::Enable(); |
| 3123 | else |
| 3124 | llvm::CrashRecoveryContext::Disable(); |
| 3125 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3126 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3127 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
| 3128 | const char *ast_filename) { |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3129 | CXTranslationUnit TU; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3130 | enum CXErrorCode Result = |
| 3131 | clang_createTranslationUnit2(CIdx, ast_filename, &TU); |
Reid Kleckner | fd48fc6 | 2014-02-12 23:56:20 +0000 | [diff] [blame] | 3132 | (void)Result; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3133 | assert((TU && Result == CXError_Success) || |
| 3134 | (!TU && Result != CXError_Success)); |
| 3135 | return TU; |
| 3136 | } |
| 3137 | |
| 3138 | enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx, |
| 3139 | const char *ast_filename, |
| 3140 | CXTranslationUnit *out_TU) { |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3141 | if (out_TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3142 | *out_TU = nullptr; |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3143 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3144 | if (!CIdx || !ast_filename || !out_TU) |
| 3145 | return CXError_InvalidArguments; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3146 | |
Argyrios Kyrtzidis | 2702101 | 2013-05-24 22:24:07 +0000 | [diff] [blame] | 3147 | LOG_FUNC_SECTION { |
| 3148 | *Log << ast_filename; |
| 3149 | } |
| 3150 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3151 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 3152 | FileSystemOptions FileSystemOpts; |
| 3153 | |
Justin Bogner | d512c1e | 2014-10-15 00:33:06 +0000 | [diff] [blame] | 3154 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags = |
| 3155 | CompilerInstance::createDiagnostics(new DiagnosticOptions()); |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 3156 | std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile( |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 3157 | ast_filename, CXXIdx->getPCHContainerOperations()->getRawReader(), Diags, |
Adrian Prantl | 6b21ab2 | 2015-08-27 19:46:20 +0000 | [diff] [blame] | 3158 | FileSystemOpts, /*UseDebugInfo=*/false, |
| 3159 | CXXIdx->getOnlyLocalDecls(), None, |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 3160 | /*CaptureDiagnostics=*/true, |
| 3161 | /*AllowPCHWithCompilerErrors=*/true, |
| 3162 | /*UserFilesAreVolatile=*/true); |
| 3163 | *out_TU = MakeCXTranslationUnit(CXXIdx, AU.release()); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3164 | return *out_TU ? CXError_Success : CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3165 | } |
| 3166 | |
| 3167 | unsigned clang_defaultEditingTranslationUnitOptions() { |
| 3168 | return CXTranslationUnit_PrecompiledPreamble | |
| 3169 | CXTranslationUnit_CacheCompletionResults; |
| 3170 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3171 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3172 | CXTranslationUnit |
| 3173 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 3174 | const char *source_filename, |
| 3175 | int num_command_line_args, |
| 3176 | const char * const *command_line_args, |
| 3177 | unsigned num_unsaved_files, |
| 3178 | struct CXUnsavedFile *unsaved_files) { |
| 3179 | unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord; |
| 3180 | return clang_parseTranslationUnit(CIdx, source_filename, |
| 3181 | command_line_args, num_command_line_args, |
| 3182 | unsaved_files, num_unsaved_files, |
| 3183 | Options); |
| 3184 | } |
| 3185 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3186 | static CXErrorCode |
| 3187 | clang_parseTranslationUnit_Impl(CXIndex CIdx, const char *source_filename, |
| 3188 | const char *const *command_line_args, |
| 3189 | int num_command_line_args, |
| 3190 | ArrayRef<CXUnsavedFile> unsaved_files, |
| 3191 | unsigned options, CXTranslationUnit *out_TU) { |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3192 | // Set up the initial return values. |
| 3193 | if (out_TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3194 | *out_TU = nullptr; |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3195 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3196 | // Check arguments. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3197 | if (!CIdx || !out_TU) |
| 3198 | return CXError_InvalidArguments; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3199 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3200 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 3201 | |
| 3202 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
| 3203 | setThreadBackgroundPriority(); |
| 3204 | |
| 3205 | bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3206 | bool CreatePreambleOnFirstParse = |
| 3207 | options & CXTranslationUnit_CreatePreambleOnFirstParse; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3208 | // FIXME: Add a flag for modules. |
| 3209 | TranslationUnitKind TUKind |
| 3210 | = (options & CXTranslationUnit_Incomplete)? TU_Prefix : TU_Complete; |
Alp Toker | 8c8a875 | 2013-12-03 06:53:35 +0000 | [diff] [blame] | 3211 | bool CacheCodeCompletionResults |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3212 | = options & CXTranslationUnit_CacheCompletionResults; |
| 3213 | bool IncludeBriefCommentsInCodeCompletion |
| 3214 | = options & CXTranslationUnit_IncludeBriefCommentsInCodeCompletion; |
| 3215 | bool SkipFunctionBodies = options & CXTranslationUnit_SkipFunctionBodies; |
| 3216 | bool ForSerialization = options & CXTranslationUnit_ForSerialization; |
| 3217 | |
| 3218 | // Configure the diagnostics. |
| 3219 | IntrusiveRefCntPtr<DiagnosticsEngine> |
Sean Silva | f1b49e2 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 3220 | Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3221 | |
Manuel Klimek | 016c024 | 2016-03-01 10:56:19 +0000 | [diff] [blame] | 3222 | if (options & CXTranslationUnit_KeepGoing) |
| 3223 | Diags->setFatalsAsError(true); |
| 3224 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3225 | // Recover resources if we crash before exiting this function. |
| 3226 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 3227 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 3228 | DiagCleanup(Diags.get()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3229 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3230 | std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles( |
| 3231 | new std::vector<ASTUnit::RemappedFile>()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3232 | |
| 3233 | // Recover resources if we crash before exiting this function. |
| 3234 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 3235 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 3236 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3237 | for (auto &UF : unsaved_files) { |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3238 | std::unique_ptr<llvm::MemoryBuffer> MB = |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3239 | llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3240 | RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3241 | } |
| 3242 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3243 | std::unique_ptr<std::vector<const char *>> Args( |
| 3244 | new std::vector<const char *>()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3245 | |
| 3246 | // Recover resources if we crash before exiting this method. |
| 3247 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > |
| 3248 | ArgsCleanup(Args.get()); |
| 3249 | |
| 3250 | // Since the Clang C library is primarily used by batch tools dealing with |
| 3251 | // (often very broken) source code, where spell-checking can have a |
| 3252 | // significant negative impact on performance (particularly when |
| 3253 | // precompiled headers are involved), we disable it by default. |
| 3254 | // Only do this if we haven't found a spell-checking-related argument. |
| 3255 | bool FoundSpellCheckingArgument = false; |
| 3256 | for (int I = 0; I != num_command_line_args; ++I) { |
| 3257 | if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || |
| 3258 | strcmp(command_line_args[I], "-fspell-checking") == 0) { |
| 3259 | FoundSpellCheckingArgument = true; |
| 3260 | break; |
| 3261 | } |
| 3262 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3263 | Args->insert(Args->end(), command_line_args, |
| 3264 | command_line_args + num_command_line_args); |
| 3265 | |
Benjamin Kramer | c02670e | 2015-11-18 16:14:27 +0000 | [diff] [blame] | 3266 | if (!FoundSpellCheckingArgument) |
| 3267 | Args->insert(Args->begin() + 1, "-fno-spell-checking"); |
| 3268 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3269 | // The 'source_filename' argument is optional. If the caller does not |
| 3270 | // specify it then it is assumed that the source file is specified |
| 3271 | // in the actual argument list. |
| 3272 | // Put the source file after command_line_args otherwise if '-x' flag is |
| 3273 | // present it will be unused. |
| 3274 | if (source_filename) |
| 3275 | Args->push_back(source_filename); |
| 3276 | |
| 3277 | // Do we need the detailed preprocessing record? |
| 3278 | if (options & CXTranslationUnit_DetailedPreprocessingRecord) { |
| 3279 | Args->push_back("-Xclang"); |
| 3280 | Args->push_back("-detailed-preprocessing-record"); |
| 3281 | } |
| 3282 | |
| 3283 | unsigned NumErrors = Diags->getClient()->getNumErrors(); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3284 | std::unique_ptr<ASTUnit> ErrUnit; |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3285 | // Unless the user specified that they want the preamble on the first parse |
| 3286 | // set it up to be created on the first reparse. This makes the first parse |
| 3287 | // faster, trading for a slower (first) reparse. |
| 3288 | unsigned PrecompilePreambleAfterNParses = |
| 3289 | !PrecompilePreamble ? 0 : 2 - CreatePreambleOnFirstParse; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3290 | std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCommandLine( |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 3291 | Args->data(), Args->data() + Args->size(), |
| 3292 | CXXIdx->getPCHContainerOperations(), Diags, |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3293 | CXXIdx->getClangResourcesPath(), CXXIdx->getOnlyLocalDecls(), |
| 3294 | /*CaptureDiagnostics=*/true, *RemappedFiles.get(), |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3295 | /*RemappedFilesKeepOriginalName=*/true, PrecompilePreambleAfterNParses, |
| 3296 | TUKind, CacheCodeCompletionResults, IncludeBriefCommentsInCodeCompletion, |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3297 | /*AllowPCHWithCompilerErrors=*/true, SkipFunctionBodies, |
Argyrios Kyrtzidis | a3e2ff1 | 2015-11-20 03:36:21 +0000 | [diff] [blame] | 3298 | /*UserFilesAreVolatile=*/true, ForSerialization, |
| 3299 | CXXIdx->getPCHContainerOperations()->getRawReader().getFormat(), |
| 3300 | &ErrUnit)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3301 | |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3302 | // Early failures in LoadFromCommandLine may return with ErrUnit unset. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3303 | if (!Unit && !ErrUnit) |
| 3304 | return CXError_ASTReadError; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3305 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3306 | if (NumErrors != Diags->getClient()->getNumErrors()) { |
| 3307 | // Make sure to check that 'Unit' is non-NULL. |
| 3308 | if (CXXIdx->getDisplayDiagnostics()) |
| 3309 | printDiagsToStderr(Unit ? Unit.get() : ErrUnit.get()); |
| 3310 | } |
| 3311 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3312 | if (isASTReadError(Unit ? Unit.get() : ErrUnit.get())) |
| 3313 | return CXError_ASTReadError; |
| 3314 | |
| 3315 | *out_TU = MakeCXTranslationUnit(CXXIdx, Unit.release()); |
| 3316 | return *out_TU ? CXError_Success : CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3317 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3318 | |
| 3319 | CXTranslationUnit |
| 3320 | clang_parseTranslationUnit(CXIndex CIdx, |
| 3321 | const char *source_filename, |
| 3322 | const char *const *command_line_args, |
| 3323 | int num_command_line_args, |
| 3324 | struct CXUnsavedFile *unsaved_files, |
| 3325 | unsigned num_unsaved_files, |
| 3326 | unsigned options) { |
| 3327 | CXTranslationUnit TU; |
| 3328 | enum CXErrorCode Result = clang_parseTranslationUnit2( |
| 3329 | CIdx, source_filename, command_line_args, num_command_line_args, |
| 3330 | unsaved_files, num_unsaved_files, options, &TU); |
Reid Kleckner | 6eaf05a | 2014-02-13 01:19:59 +0000 | [diff] [blame] | 3331 | (void)Result; |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3332 | assert((TU && Result == CXError_Success) || |
| 3333 | (!TU && Result != CXError_Success)); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3334 | return TU; |
| 3335 | } |
| 3336 | |
| 3337 | enum CXErrorCode clang_parseTranslationUnit2( |
Benjamin Kramer | c02670e | 2015-11-18 16:14:27 +0000 | [diff] [blame] | 3338 | CXIndex CIdx, const char *source_filename, |
| 3339 | const char *const *command_line_args, int num_command_line_args, |
| 3340 | struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files, |
| 3341 | unsigned options, CXTranslationUnit *out_TU) { |
| 3342 | SmallVector<const char *, 4> Args; |
| 3343 | Args.push_back("clang"); |
| 3344 | Args.append(command_line_args, command_line_args + num_command_line_args); |
| 3345 | return clang_parseTranslationUnit2FullArgv( |
| 3346 | CIdx, source_filename, Args.data(), Args.size(), unsaved_files, |
| 3347 | num_unsaved_files, options, out_TU); |
| 3348 | } |
| 3349 | |
| 3350 | enum CXErrorCode clang_parseTranslationUnit2FullArgv( |
| 3351 | CXIndex CIdx, const char *source_filename, |
| 3352 | const char *const *command_line_args, int num_command_line_args, |
| 3353 | struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files, |
| 3354 | unsigned options, CXTranslationUnit *out_TU) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 3355 | LOG_FUNC_SECTION { |
| 3356 | *Log << source_filename << ": "; |
| 3357 | for (int i = 0; i != num_command_line_args; ++i) |
| 3358 | *Log << command_line_args[i] << " "; |
| 3359 | } |
| 3360 | |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3361 | if (num_unsaved_files && !unsaved_files) |
| 3362 | return CXError_InvalidArguments; |
| 3363 | |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 3364 | CXErrorCode result = CXError_Failure; |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3365 | auto ParseTranslationUnitImpl = [=, &result] { |
| 3366 | result = clang_parseTranslationUnit_Impl( |
| 3367 | CIdx, source_filename, command_line_args, num_command_line_args, |
| 3368 | llvm::makeArrayRef(unsaved_files, num_unsaved_files), options, out_TU); |
| 3369 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3370 | llvm::CrashRecoveryContext CRC; |
| 3371 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3372 | if (!RunSafely(CRC, ParseTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3373 | fprintf(stderr, "libclang: crash detected during parsing: {\n"); |
| 3374 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 3375 | fprintf(stderr, " 'command_line_args' : ["); |
| 3376 | for (int i = 0; i != num_command_line_args; ++i) { |
| 3377 | if (i) |
| 3378 | fprintf(stderr, ", "); |
| 3379 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 3380 | } |
| 3381 | fprintf(stderr, "],\n"); |
| 3382 | fprintf(stderr, " 'unsaved_files' : ["); |
| 3383 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 3384 | if (i) |
| 3385 | fprintf(stderr, ", "); |
| 3386 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 3387 | unsaved_files[i].Length); |
| 3388 | } |
| 3389 | fprintf(stderr, "],\n"); |
| 3390 | fprintf(stderr, " 'options' : %d,\n", options); |
| 3391 | fprintf(stderr, "}\n"); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3392 | |
| 3393 | return CXError_Crashed; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3394 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3395 | if (CXTranslationUnit *TU = out_TU) |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3396 | PrintLibclangResourceUsage(*TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3397 | } |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 3398 | |
| 3399 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3402 | CXString clang_Type_getObjCEncoding(CXType CT) { |
| 3403 | CXTranslationUnit tu = static_cast<CXTranslationUnit>(CT.data[1]); |
| 3404 | ASTContext &Ctx = getASTUnit(tu)->getASTContext(); |
| 3405 | std::string encoding; |
| 3406 | Ctx.getObjCEncodingForType(QualType::getFromOpaquePtr(CT.data[0]), |
| 3407 | encoding); |
| 3408 | |
| 3409 | return cxstring::createDup(encoding); |
| 3410 | } |
| 3411 | |
| 3412 | static const IdentifierInfo *getMacroIdentifier(CXCursor C) { |
| 3413 | if (C.kind == CXCursor_MacroDefinition) { |
| 3414 | if (const MacroDefinitionRecord *MDR = getCursorMacroDefinition(C)) |
| 3415 | return MDR->getName(); |
| 3416 | } else if (C.kind == CXCursor_MacroExpansion) { |
| 3417 | MacroExpansionCursor ME = getCursorMacroExpansion(C); |
| 3418 | return ME.getName(); |
| 3419 | } |
| 3420 | return nullptr; |
| 3421 | } |
| 3422 | |
| 3423 | unsigned clang_Cursor_isMacroFunctionLike(CXCursor C) { |
| 3424 | const IdentifierInfo *II = getMacroIdentifier(C); |
| 3425 | if (!II) { |
| 3426 | return false; |
| 3427 | } |
| 3428 | ASTUnit *ASTU = getCursorASTUnit(C); |
| 3429 | Preprocessor &PP = ASTU->getPreprocessor(); |
| 3430 | if (const MacroInfo *MI = PP.getMacroInfo(II)) |
| 3431 | return MI->isFunctionLike(); |
| 3432 | return false; |
| 3433 | } |
| 3434 | |
| 3435 | unsigned clang_Cursor_isMacroBuiltin(CXCursor C) { |
| 3436 | const IdentifierInfo *II = getMacroIdentifier(C); |
| 3437 | if (!II) { |
| 3438 | return false; |
| 3439 | } |
| 3440 | ASTUnit *ASTU = getCursorASTUnit(C); |
| 3441 | Preprocessor &PP = ASTU->getPreprocessor(); |
| 3442 | if (const MacroInfo *MI = PP.getMacroInfo(II)) |
| 3443 | return MI->isBuiltinMacro(); |
| 3444 | return false; |
| 3445 | } |
| 3446 | |
| 3447 | unsigned clang_Cursor_isFunctionInlined(CXCursor C) { |
| 3448 | const Decl *D = getCursorDecl(C); |
| 3449 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 3450 | if (!FD) { |
| 3451 | return false; |
| 3452 | } |
| 3453 | return FD->isInlined(); |
| 3454 | } |
| 3455 | |
| 3456 | static StringLiteral* getCFSTR_value(CallExpr *callExpr) { |
| 3457 | if (callExpr->getNumArgs() != 1) { |
| 3458 | return nullptr; |
| 3459 | } |
| 3460 | |
| 3461 | StringLiteral *S = nullptr; |
| 3462 | auto *arg = callExpr->getArg(0); |
| 3463 | if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) { |
| 3464 | ImplicitCastExpr *I = static_cast<ImplicitCastExpr *>(arg); |
| 3465 | auto *subExpr = I->getSubExprAsWritten(); |
| 3466 | |
| 3467 | if(subExpr->getStmtClass() != Stmt::StringLiteralClass){ |
| 3468 | return nullptr; |
| 3469 | } |
| 3470 | |
| 3471 | S = static_cast<StringLiteral *>(I->getSubExprAsWritten()); |
| 3472 | } else if (arg->getStmtClass() == Stmt::StringLiteralClass) { |
| 3473 | S = static_cast<StringLiteral *>(callExpr->getArg(0)); |
| 3474 | } else { |
| 3475 | return nullptr; |
| 3476 | } |
| 3477 | return S; |
| 3478 | } |
| 3479 | |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3480 | struct ExprEvalResult { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3481 | CXEvalResultKind EvalType; |
| 3482 | union { |
| 3483 | int intVal; |
| 3484 | double floatVal; |
| 3485 | char *stringVal; |
| 3486 | } EvalData; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3487 | ~ExprEvalResult() { |
| 3488 | if (EvalType != CXEval_UnExposed && EvalType != CXEval_Float && |
| 3489 | EvalType != CXEval_Int) { |
| 3490 | delete EvalData.stringVal; |
| 3491 | } |
| 3492 | } |
| 3493 | }; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3494 | |
| 3495 | void clang_EvalResult_dispose(CXEvalResult E) { |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3496 | delete static_cast<ExprEvalResult *>(E); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3497 | } |
| 3498 | |
| 3499 | CXEvalResultKind clang_EvalResult_getKind(CXEvalResult E) { |
| 3500 | if (!E) { |
| 3501 | return CXEval_UnExposed; |
| 3502 | } |
| 3503 | return ((ExprEvalResult *)E)->EvalType; |
| 3504 | } |
| 3505 | |
| 3506 | int clang_EvalResult_getAsInt(CXEvalResult E) { |
| 3507 | if (!E) { |
| 3508 | return 0; |
| 3509 | } |
| 3510 | return ((ExprEvalResult *)E)->EvalData.intVal; |
| 3511 | } |
| 3512 | |
| 3513 | double clang_EvalResult_getAsDouble(CXEvalResult E) { |
| 3514 | if (!E) { |
| 3515 | return 0; |
| 3516 | } |
| 3517 | return ((ExprEvalResult *)E)->EvalData.floatVal; |
| 3518 | } |
| 3519 | |
| 3520 | const char* clang_EvalResult_getAsStr(CXEvalResult E) { |
| 3521 | if (!E) { |
| 3522 | return nullptr; |
| 3523 | } |
| 3524 | return ((ExprEvalResult *)E)->EvalData.stringVal; |
| 3525 | } |
| 3526 | |
| 3527 | static const ExprEvalResult* evaluateExpr(Expr *expr, CXCursor C) { |
| 3528 | Expr::EvalResult ER; |
| 3529 | ASTContext &ctx = getCursorContext(C); |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3530 | if (!expr) |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3531 | return nullptr; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3532 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3533 | expr = expr->IgnoreParens(); |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3534 | if (!expr->EvaluateAsRValue(ER, ctx)) |
| 3535 | return nullptr; |
| 3536 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3537 | QualType rettype; |
| 3538 | CallExpr *callExpr; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3539 | auto result = llvm::make_unique<ExprEvalResult>(); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3540 | result->EvalType = CXEval_UnExposed; |
| 3541 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3542 | if (ER.Val.isInt()) { |
| 3543 | result->EvalType = CXEval_Int; |
| 3544 | result->EvalData.intVal = ER.Val.getInt().getExtValue(); |
| 3545 | return result.release(); |
| 3546 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3547 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3548 | if (ER.Val.isFloat()) { |
| 3549 | llvm::SmallVector<char, 100> Buffer; |
| 3550 | ER.Val.getFloat().toString(Buffer); |
| 3551 | std::string floatStr(Buffer.data(), Buffer.size()); |
| 3552 | result->EvalType = CXEval_Float; |
| 3553 | bool ignored; |
| 3554 | llvm::APFloat apFloat = ER.Val.getFloat(); |
| 3555 | apFloat.convert(llvm::APFloat::IEEEdouble, |
| 3556 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 3557 | result->EvalData.floatVal = apFloat.convertToDouble(); |
| 3558 | return result.release(); |
| 3559 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3560 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3561 | if (expr->getStmtClass() == Stmt::ImplicitCastExprClass) { |
| 3562 | const ImplicitCastExpr *I = dyn_cast<ImplicitCastExpr>(expr); |
| 3563 | auto *subExpr = I->getSubExprAsWritten(); |
| 3564 | if (subExpr->getStmtClass() == Stmt::StringLiteralClass || |
| 3565 | subExpr->getStmtClass() == Stmt::ObjCStringLiteralClass) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3566 | const StringLiteral *StrE = nullptr; |
| 3567 | const ObjCStringLiteral *ObjCExpr; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3568 | ObjCExpr = dyn_cast<ObjCStringLiteral>(subExpr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3569 | |
| 3570 | if (ObjCExpr) { |
| 3571 | StrE = ObjCExpr->getString(); |
| 3572 | result->EvalType = CXEval_ObjCStrLiteral; |
| 3573 | } else { |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3574 | StrE = cast<StringLiteral>(I->getSubExprAsWritten()); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3575 | result->EvalType = CXEval_StrLiteral; |
| 3576 | } |
| 3577 | |
| 3578 | std::string strRef(StrE->getString().str()); |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3579 | result->EvalData.stringVal = new char[strRef.size() + 1]; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3580 | strncpy((char *)result->EvalData.stringVal, strRef.c_str(), |
| 3581 | strRef.size()); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3582 | result->EvalData.stringVal[strRef.size()] = '\0'; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3583 | return result.release(); |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3584 | } |
| 3585 | } else if (expr->getStmtClass() == Stmt::ObjCStringLiteralClass || |
| 3586 | expr->getStmtClass() == Stmt::StringLiteralClass) { |
| 3587 | const StringLiteral *StrE = nullptr; |
| 3588 | const ObjCStringLiteral *ObjCExpr; |
| 3589 | ObjCExpr = dyn_cast<ObjCStringLiteral>(expr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3590 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3591 | if (ObjCExpr) { |
| 3592 | StrE = ObjCExpr->getString(); |
| 3593 | result->EvalType = CXEval_ObjCStrLiteral; |
| 3594 | } else { |
| 3595 | StrE = cast<StringLiteral>(expr); |
| 3596 | result->EvalType = CXEval_StrLiteral; |
| 3597 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3598 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3599 | std::string strRef(StrE->getString().str()); |
| 3600 | result->EvalData.stringVal = new char[strRef.size() + 1]; |
| 3601 | strncpy((char *)result->EvalData.stringVal, strRef.c_str(), strRef.size()); |
| 3602 | result->EvalData.stringVal[strRef.size()] = '\0'; |
| 3603 | return result.release(); |
| 3604 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3605 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3606 | if (expr->getStmtClass() == Stmt::CStyleCastExprClass) { |
| 3607 | CStyleCastExpr *CC = static_cast<CStyleCastExpr *>(expr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3608 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3609 | rettype = CC->getType(); |
| 3610 | if (rettype.getAsString() == "CFStringRef" && |
| 3611 | CC->getSubExpr()->getStmtClass() == Stmt::CallExprClass) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3612 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3613 | callExpr = static_cast<CallExpr *>(CC->getSubExpr()); |
| 3614 | StringLiteral *S = getCFSTR_value(callExpr); |
| 3615 | if (S) { |
| 3616 | std::string strLiteral(S->getString().str()); |
| 3617 | result->EvalType = CXEval_CFStr; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3618 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3619 | result->EvalData.stringVal = new char[strLiteral.size() + 1]; |
| 3620 | strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(), |
| 3621 | strLiteral.size()); |
| 3622 | result->EvalData.stringVal[strLiteral.size()] = '\0'; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3623 | return result.release(); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3624 | } |
| 3625 | } |
| 3626 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3627 | } else if (expr->getStmtClass() == Stmt::CallExprClass) { |
| 3628 | callExpr = static_cast<CallExpr *>(expr); |
| 3629 | rettype = callExpr->getCallReturnType(ctx); |
| 3630 | |
| 3631 | if (rettype->isVectorType() || callExpr->getNumArgs() > 1) |
| 3632 | return nullptr; |
| 3633 | |
| 3634 | if (rettype->isIntegralType(ctx) || rettype->isRealFloatingType()) { |
| 3635 | if (callExpr->getNumArgs() == 1 && |
| 3636 | !callExpr->getArg(0)->getType()->isIntegralType(ctx)) |
| 3637 | return nullptr; |
| 3638 | } else if (rettype.getAsString() == "CFStringRef") { |
| 3639 | |
| 3640 | StringLiteral *S = getCFSTR_value(callExpr); |
| 3641 | if (S) { |
| 3642 | std::string strLiteral(S->getString().str()); |
| 3643 | result->EvalType = CXEval_CFStr; |
| 3644 | result->EvalData.stringVal = new char[strLiteral.size() + 1]; |
| 3645 | strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(), |
| 3646 | strLiteral.size()); |
| 3647 | result->EvalData.stringVal[strLiteral.size()] = '\0'; |
| 3648 | return result.release(); |
| 3649 | } |
| 3650 | } |
| 3651 | } else if (expr->getStmtClass() == Stmt::DeclRefExprClass) { |
| 3652 | DeclRefExpr *D = static_cast<DeclRefExpr *>(expr); |
| 3653 | ValueDecl *V = D->getDecl(); |
| 3654 | if (V->getKind() == Decl::Function) { |
| 3655 | std::string strName = V->getNameAsString(); |
| 3656 | result->EvalType = CXEval_Other; |
| 3657 | result->EvalData.stringVal = new char[strName.size() + 1]; |
| 3658 | strncpy(result->EvalData.stringVal, strName.c_str(), strName.size()); |
| 3659 | result->EvalData.stringVal[strName.size()] = '\0'; |
| 3660 | return result.release(); |
| 3661 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3662 | } |
| 3663 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3664 | return nullptr; |
| 3665 | } |
| 3666 | |
| 3667 | CXEvalResult clang_Cursor_Evaluate(CXCursor C) { |
| 3668 | const Decl *D = getCursorDecl(C); |
| 3669 | if (D) { |
| 3670 | const Expr *expr = nullptr; |
| 3671 | if (auto *Var = dyn_cast<VarDecl>(D)) { |
| 3672 | expr = Var->getInit(); |
| 3673 | } else if (auto *Field = dyn_cast<FieldDecl>(D)) { |
| 3674 | expr = Field->getInClassInitializer(); |
| 3675 | } |
| 3676 | if (expr) |
Aaron Ballman | 01dc157 | 2016-01-20 15:25:30 +0000 | [diff] [blame] | 3677 | return const_cast<CXEvalResult>(reinterpret_cast<const void *>( |
| 3678 | evaluateExpr(const_cast<Expr *>(expr), C))); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3679 | return nullptr; |
| 3680 | } |
| 3681 | |
| 3682 | const CompoundStmt *compoundStmt = dyn_cast_or_null<CompoundStmt>(getCursorStmt(C)); |
| 3683 | if (compoundStmt) { |
| 3684 | Expr *expr = nullptr; |
| 3685 | for (auto *bodyIterator : compoundStmt->body()) { |
| 3686 | if ((expr = dyn_cast<Expr>(bodyIterator))) { |
| 3687 | break; |
| 3688 | } |
| 3689 | } |
| 3690 | if (expr) |
Aaron Ballman | 01dc157 | 2016-01-20 15:25:30 +0000 | [diff] [blame] | 3691 | return const_cast<CXEvalResult>( |
| 3692 | reinterpret_cast<const void *>(evaluateExpr(expr, C))); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3693 | } |
| 3694 | return nullptr; |
| 3695 | } |
| 3696 | |
| 3697 | unsigned clang_Cursor_hasAttrs(CXCursor C) { |
| 3698 | const Decl *D = getCursorDecl(C); |
| 3699 | if (!D) { |
| 3700 | return 0; |
| 3701 | } |
| 3702 | |
| 3703 | if (D->hasAttrs()) { |
| 3704 | return 1; |
| 3705 | } |
| 3706 | |
| 3707 | return 0; |
| 3708 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3709 | unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { |
| 3710 | return CXSaveTranslationUnit_None; |
| 3711 | } |
| 3712 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3713 | static CXSaveError clang_saveTranslationUnit_Impl(CXTranslationUnit TU, |
| 3714 | const char *FileName, |
| 3715 | unsigned options) { |
| 3716 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3717 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
| 3718 | setThreadBackgroundPriority(); |
| 3719 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3720 | bool hadError = cxtu::getASTUnit(TU)->Save(FileName); |
| 3721 | return hadError ? CXSaveError_Unknown : CXSaveError_None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3722 | } |
| 3723 | |
| 3724 | int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, |
| 3725 | unsigned options) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 3726 | LOG_FUNC_SECTION { |
| 3727 | *Log << TU << ' ' << FileName; |
| 3728 | } |
| 3729 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 3730 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3731 | LOG_BAD_TU(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3732 | return CXSaveError_InvalidTU; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3733 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3734 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3735 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3736 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 3737 | if (!CXXUnit->hasSema()) |
| 3738 | return CXSaveError_InvalidTU; |
| 3739 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3740 | CXSaveError result; |
| 3741 | auto SaveTranslationUnitImpl = [=, &result]() { |
| 3742 | result = clang_saveTranslationUnit_Impl(TU, FileName, options); |
| 3743 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3744 | |
| 3745 | if (!CXXUnit->getDiagnostics().hasUnrecoverableErrorOccurred() || |
| 3746 | getenv("LIBCLANG_NOTHREADS")) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3747 | SaveTranslationUnitImpl(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3748 | |
| 3749 | if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 3750 | PrintLibclangResourceUsage(TU); |
| 3751 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3752 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3753 | } |
| 3754 | |
| 3755 | // We have an AST that has invalid nodes due to compiler errors. |
| 3756 | // Use a crash recovery thread for protection. |
| 3757 | |
| 3758 | llvm::CrashRecoveryContext CRC; |
| 3759 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3760 | if (!RunSafely(CRC, SaveTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3761 | fprintf(stderr, "libclang: crash detected during AST saving: {\n"); |
| 3762 | fprintf(stderr, " 'filename' : '%s'\n", FileName); |
| 3763 | fprintf(stderr, " 'options' : %d,\n", options); |
| 3764 | fprintf(stderr, "}\n"); |
| 3765 | |
| 3766 | return CXSaveError_Unknown; |
| 3767 | |
| 3768 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
| 3769 | PrintLibclangResourceUsage(TU); |
| 3770 | } |
| 3771 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3772 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3773 | } |
| 3774 | |
| 3775 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
| 3776 | if (CTUnit) { |
| 3777 | // If the translation unit has been marked as unsafe to free, just discard |
| 3778 | // it. |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3779 | ASTUnit *Unit = cxtu::getASTUnit(CTUnit); |
| 3780 | if (Unit && Unit->isUnsafeToFree()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3781 | return; |
| 3782 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3783 | delete cxtu::getASTUnit(CTUnit); |
Dmitri Gribenko | b95b3f1 | 2013-01-26 22:44:19 +0000 | [diff] [blame] | 3784 | delete CTUnit->StringPool; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3785 | delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics); |
| 3786 | disposeOverridenCXCursorsPool(CTUnit->OverridenCursorsPool); |
Dmitri Gribenko | 9e60511 | 2013-11-13 22:16:51 +0000 | [diff] [blame] | 3787 | delete CTUnit->CommentToXML; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3788 | delete CTUnit; |
| 3789 | } |
| 3790 | } |
| 3791 | |
| 3792 | unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { |
| 3793 | return CXReparse_None; |
| 3794 | } |
| 3795 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3796 | static CXErrorCode |
| 3797 | clang_reparseTranslationUnit_Impl(CXTranslationUnit TU, |
| 3798 | ArrayRef<CXUnsavedFile> unsaved_files, |
| 3799 | unsigned options) { |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3800 | // Check arguments. |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 3801 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3802 | LOG_BAD_TU(TU); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3803 | return CXError_InvalidArguments; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3804 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3805 | |
| 3806 | // Reset the associated diagnostics. |
| 3807 | delete static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3808 | TU->Diagnostics = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3809 | |
Dmitri Gribenko | 183436e | 2013-01-26 21:49:50 +0000 | [diff] [blame] | 3810 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3811 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) |
| 3812 | setThreadBackgroundPriority(); |
| 3813 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3814 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3815 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3816 | |
| 3817 | std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles( |
| 3818 | new std::vector<ASTUnit::RemappedFile>()); |
| 3819 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3820 | // Recover resources if we crash before exiting this function. |
| 3821 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 3822 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3823 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3824 | for (auto &UF : unsaved_files) { |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3825 | std::unique_ptr<llvm::MemoryBuffer> MB = |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3826 | llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3827 | RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3828 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3829 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 3830 | if (!CXXUnit->Reparse(CXXIdx->getPCHContainerOperations(), |
| 3831 | *RemappedFiles.get())) |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3832 | return CXError_Success; |
| 3833 | if (isASTReadError(CXXUnit)) |
| 3834 | return CXError_ASTReadError; |
| 3835 | return CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3836 | } |
| 3837 | |
| 3838 | int clang_reparseTranslationUnit(CXTranslationUnit TU, |
| 3839 | unsigned num_unsaved_files, |
| 3840 | struct CXUnsavedFile *unsaved_files, |
| 3841 | unsigned options) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 3842 | LOG_FUNC_SECTION { |
| 3843 | *Log << TU; |
| 3844 | } |
| 3845 | |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3846 | if (num_unsaved_files && !unsaved_files) |
| 3847 | return CXError_InvalidArguments; |
| 3848 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3849 | CXErrorCode result; |
| 3850 | auto ReparseTranslationUnitImpl = [=, &result]() { |
| 3851 | result = clang_reparseTranslationUnit_Impl( |
| 3852 | TU, llvm::makeArrayRef(unsaved_files, num_unsaved_files), options); |
| 3853 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3854 | |
| 3855 | if (getenv("LIBCLANG_NOTHREADS")) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3856 | ReparseTranslationUnitImpl(); |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 3857 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3858 | } |
| 3859 | |
| 3860 | llvm::CrashRecoveryContext CRC; |
| 3861 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3862 | if (!RunSafely(CRC, ReparseTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3863 | fprintf(stderr, "libclang: crash detected during reparsing\n"); |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3864 | cxtu::getASTUnit(TU)->setUnsafeToFree(true); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3865 | return CXError_Crashed; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3866 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 3867 | PrintLibclangResourceUsage(TU); |
| 3868 | |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 3869 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
| 3872 | |
| 3873 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 3874 | if (isNotUsableTU(CTUnit)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3875 | LOG_BAD_TU(CTUnit); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 3876 | return cxstring::createEmpty(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3877 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3878 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3879 | ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 3880 | return cxstring::createDup(CXXUnit->getOriginalSourceFileName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3881 | } |
| 3882 | |
| 3883 | CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 3884 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3885 | LOG_BAD_TU(TU); |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 3886 | return clang_getNullCursor(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3887 | } |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 3888 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3889 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3890 | return MakeCXCursor(CXXUnit->getASTContext().getTranslationUnitDecl(), TU); |
| 3891 | } |
| 3892 | |
| 3893 | } // end: extern "C" |
| 3894 | |
| 3895 | //===----------------------------------------------------------------------===// |
| 3896 | // CXFile Operations. |
| 3897 | //===----------------------------------------------------------------------===// |
| 3898 | |
| 3899 | extern "C" { |
| 3900 | CXString clang_getFileName(CXFile SFile) { |
| 3901 | if (!SFile) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 3902 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3903 | |
| 3904 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 3905 | return cxstring::createRef(FEnt->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3906 | } |
| 3907 | |
| 3908 | time_t clang_getFileTime(CXFile SFile) { |
| 3909 | if (!SFile) |
| 3910 | return 0; |
| 3911 | |
| 3912 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 3913 | return FEnt->getModificationTime(); |
| 3914 | } |
| 3915 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3916 | CXFile clang_getFile(CXTranslationUnit TU, const char *file_name) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 3917 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3918 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3919 | return nullptr; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3920 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3921 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3922 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3923 | |
| 3924 | FileManager &FMgr = CXXUnit->getFileManager(); |
| 3925 | return const_cast<FileEntry *>(FMgr.getFile(file_name)); |
| 3926 | } |
| 3927 | |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3928 | unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit TU, |
| 3929 | CXFile file) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 3930 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3931 | LOG_BAD_TU(TU); |
| 3932 | return 0; |
| 3933 | } |
| 3934 | |
| 3935 | if (!file) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3936 | return 0; |
| 3937 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3938 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3939 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
| 3940 | return CXXUnit->getPreprocessor().getHeaderSearchInfo() |
| 3941 | .isFileMultipleIncludeGuarded(FEnt); |
| 3942 | } |
| 3943 | |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 3944 | int clang_getFileUniqueID(CXFile file, CXFileUniqueID *outID) { |
| 3945 | if (!file || !outID) |
| 3946 | return 1; |
| 3947 | |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 3948 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 3949 | const llvm::sys::fs::UniqueID &ID = FEnt->getUniqueID(); |
| 3950 | outID->data[0] = ID.getDevice(); |
| 3951 | outID->data[1] = ID.getFile(); |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 3952 | outID->data[2] = FEnt->getModificationTime(); |
| 3953 | return 0; |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 3954 | } |
| 3955 | |
Argyrios Kyrtzidis | ac3997e | 2014-08-16 00:26:19 +0000 | [diff] [blame] | 3956 | int clang_File_isEqual(CXFile file1, CXFile file2) { |
| 3957 | if (file1 == file2) |
| 3958 | return true; |
| 3959 | |
| 3960 | if (!file1 || !file2) |
| 3961 | return false; |
| 3962 | |
| 3963 | FileEntry *FEnt1 = static_cast<FileEntry *>(file1); |
| 3964 | FileEntry *FEnt2 = static_cast<FileEntry *>(file2); |
| 3965 | return FEnt1->getUniqueID() == FEnt2->getUniqueID(); |
| 3966 | } |
| 3967 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3968 | } // end: extern "C" |
| 3969 | |
| 3970 | //===----------------------------------------------------------------------===// |
| 3971 | // CXCursor Operations. |
| 3972 | //===----------------------------------------------------------------------===// |
| 3973 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3974 | static const Decl *getDeclFromExpr(const Stmt *E) { |
| 3975 | if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3976 | return getDeclFromExpr(CE->getSubExpr()); |
| 3977 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3978 | if (const DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3979 | return RefExpr->getDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3980 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3981 | return ME->getMemberDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3982 | if (const ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3983 | return RE->getDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3984 | if (const ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3985 | if (PRE->isExplicitProperty()) |
| 3986 | return PRE->getExplicitProperty(); |
| 3987 | // It could be messaging both getter and setter as in: |
| 3988 | // ++myobj.myprop; |
| 3989 | // in which case prefer to associate the setter since it is less obvious |
| 3990 | // from inspecting the source that the setter is going to get called. |
| 3991 | if (PRE->isMessagingSetter()) |
| 3992 | return PRE->getImplicitPropertySetter(); |
| 3993 | return PRE->getImplicitPropertyGetter(); |
| 3994 | } |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3995 | if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3996 | return getDeclFromExpr(POE->getSyntacticForm()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3997 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3998 | if (Expr *Src = OVE->getSourceExpr()) |
| 3999 | return getDeclFromExpr(Src); |
| 4000 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4001 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4002 | return getDeclFromExpr(CE->getCallee()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4003 | if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4004 | if (!CE->isElidable()) |
| 4005 | return CE->getConstructor(); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4006 | if (const CXXInheritedCtorInitExpr *CE = |
| 4007 | dyn_cast<CXXInheritedCtorInitExpr>(E)) |
| 4008 | return CE->getConstructor(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4009 | if (const ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4010 | return OME->getMethodDecl(); |
| 4011 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4012 | if (const ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4013 | return PE->getProtocol(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4014 | if (const SubstNonTypeTemplateParmPackExpr *NTTP |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4015 | = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E)) |
| 4016 | return NTTP->getParameterPack(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4017 | if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4018 | if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || |
| 4019 | isa<ParmVarDecl>(SizeOfPack->getPack())) |
| 4020 | return SizeOfPack->getPack(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 4021 | |
| 4022 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4025 | static SourceLocation getLocationFromExpr(const Expr *E) { |
| 4026 | if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4027 | return getLocationFromExpr(CE->getSubExpr()); |
| 4028 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4029 | if (const ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4030 | return /*FIXME:*/Msg->getLeftLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4031 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4032 | return DRE->getLocation(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4033 | if (const MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4034 | return Member->getMemberLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4035 | if (const ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4036 | return Ivar->getLocation(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4037 | if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4038 | return SizeOfPack->getPackLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4039 | if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4040 | return PropRef->getLocation(); |
| 4041 | |
| 4042 | return E->getLocStart(); |
| 4043 | } |
| 4044 | |
| 4045 | extern "C" { |
| 4046 | |
| 4047 | unsigned clang_visitChildren(CXCursor parent, |
| 4048 | CXCursorVisitor visitor, |
| 4049 | CXClientData client_data) { |
| 4050 | CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, |
| 4051 | /*VisitPreprocessorLast=*/false); |
| 4052 | return CursorVis.VisitChildren(parent); |
| 4053 | } |
| 4054 | |
| 4055 | #ifndef __has_feature |
| 4056 | #define __has_feature(x) 0 |
| 4057 | #endif |
| 4058 | #if __has_feature(blocks) |
| 4059 | typedef enum CXChildVisitResult |
| 4060 | (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent); |
| 4061 | |
| 4062 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 4063 | CXClientData client_data) { |
| 4064 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 4065 | return block(cursor, parent); |
| 4066 | } |
| 4067 | #else |
| 4068 | // If we are compiled with a compiler that doesn't have native blocks support, |
| 4069 | // define and call the block manually, so the |
| 4070 | typedef struct _CXChildVisitResult |
| 4071 | { |
| 4072 | void *isa; |
| 4073 | int flags; |
| 4074 | int reserved; |
| 4075 | enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, |
| 4076 | CXCursor); |
| 4077 | } *CXCursorVisitorBlock; |
| 4078 | |
| 4079 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 4080 | CXClientData client_data) { |
| 4081 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 4082 | return block->invoke(block, cursor, parent); |
| 4083 | } |
| 4084 | #endif |
| 4085 | |
| 4086 | |
| 4087 | unsigned clang_visitChildrenWithBlock(CXCursor parent, |
| 4088 | CXCursorVisitorBlock block) { |
| 4089 | return clang_visitChildren(parent, visitWithBlock, block); |
| 4090 | } |
| 4091 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4092 | static CXString getDeclSpelling(const Decl *D) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4093 | if (!D) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4094 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4095 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4096 | const NamedDecl *ND = dyn_cast<NamedDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4097 | if (!ND) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4098 | if (const ObjCPropertyImplDecl *PropImpl = |
| 4099 | dyn_cast<ObjCPropertyImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4100 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4101 | return cxstring::createDup(Property->getIdentifier()->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4102 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4103 | if (const ImportDecl *ImportD = dyn_cast<ImportDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4104 | if (Module *Mod = ImportD->getImportedModule()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4105 | return cxstring::createDup(Mod->getFullModuleName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4106 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4107 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4108 | } |
| 4109 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4110 | if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4111 | return cxstring::createDup(OMD->getSelector().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4112 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4113 | if (const ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4114 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 4115 | // and returns different names. NamedDecl returns the class name and |
| 4116 | // ObjCCategoryImplDecl returns the category name. |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4117 | return cxstring::createRef(CIMP->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4118 | |
| 4119 | if (isa<UsingDirectiveDecl>(D)) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4120 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4121 | |
| 4122 | SmallString<1024> S; |
| 4123 | llvm::raw_svector_ostream os(S); |
| 4124 | ND->printName(os); |
| 4125 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4126 | return cxstring::createDup(os.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4127 | } |
| 4128 | |
| 4129 | CXString clang_getCursorSpelling(CXCursor C) { |
| 4130 | if (clang_isTranslationUnit(C.kind)) |
Dmitri Gribenko | 2c173b4 | 2013-01-11 19:28:44 +0000 | [diff] [blame] | 4131 | return clang_getTranslationUnitSpelling(getCursorTU(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4132 | |
| 4133 | if (clang_isReference(C.kind)) { |
| 4134 | switch (C.kind) { |
| 4135 | case CXCursor_ObjCSuperClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4136 | const ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4137 | return cxstring::createRef(Super->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4138 | } |
| 4139 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4140 | const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4141 | return cxstring::createRef(Class->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4142 | } |
| 4143 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4144 | const ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4145 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4146 | return cxstring::createRef(OID->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4147 | } |
| 4148 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4149 | const CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4150 | return cxstring::createDup(B->getType().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4151 | } |
| 4152 | case CXCursor_TypeRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4153 | const TypeDecl *Type = getCursorTypeRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4154 | assert(Type && "Missing type decl"); |
| 4155 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4156 | return cxstring::createDup(getCursorContext(C).getTypeDeclType(Type). |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4157 | getAsString()); |
| 4158 | } |
| 4159 | case CXCursor_TemplateRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4160 | const TemplateDecl *Template = getCursorTemplateRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4161 | assert(Template && "Missing template decl"); |
| 4162 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4163 | return cxstring::createDup(Template->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4164 | } |
| 4165 | |
| 4166 | case CXCursor_NamespaceRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4167 | const NamedDecl *NS = getCursorNamespaceRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4168 | assert(NS && "Missing namespace decl"); |
| 4169 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4170 | return cxstring::createDup(NS->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4171 | } |
| 4172 | |
| 4173 | case CXCursor_MemberRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4174 | const FieldDecl *Field = getCursorMemberRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4175 | assert(Field && "Missing member decl"); |
| 4176 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4177 | return cxstring::createDup(Field->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
| 4180 | case CXCursor_LabelRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4181 | const LabelStmt *Label = getCursorLabelRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4182 | assert(Label && "Missing label"); |
| 4183 | |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4184 | return cxstring::createRef(Label->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
| 4187 | case CXCursor_OverloadedDeclRef: { |
| 4188 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4189 | if (const Decl *D = Storage.dyn_cast<const Decl *>()) { |
| 4190 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4191 | return cxstring::createDup(ND->getNameAsString()); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4192 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4193 | } |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4194 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4195 | return cxstring::createDup(E->getName().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4196 | OverloadedTemplateStorage *Ovl |
| 4197 | = Storage.get<OverloadedTemplateStorage*>(); |
| 4198 | if (Ovl->size() == 0) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4199 | return cxstring::createEmpty(); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4200 | return cxstring::createDup((*Ovl->begin())->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4201 | } |
| 4202 | |
| 4203 | case CXCursor_VariableRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4204 | const VarDecl *Var = getCursorVariableRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4205 | assert(Var && "Missing variable decl"); |
| 4206 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4207 | return cxstring::createDup(Var->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4208 | } |
| 4209 | |
| 4210 | default: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4211 | return cxstring::createRef("<not implemented>"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4212 | } |
| 4213 | } |
| 4214 | |
| 4215 | if (clang_isExpression(C.kind)) { |
Argyrios Kyrtzidis | 3227d86 | 2014-03-03 19:40:52 +0000 | [diff] [blame] | 4216 | const Expr *E = getCursorExpr(C); |
| 4217 | |
| 4218 | if (C.kind == CXCursor_ObjCStringLiteral || |
| 4219 | C.kind == CXCursor_StringLiteral) { |
| 4220 | const StringLiteral *SLit; |
| 4221 | if (const ObjCStringLiteral *OSL = dyn_cast<ObjCStringLiteral>(E)) { |
| 4222 | SLit = OSL->getString(); |
| 4223 | } else { |
| 4224 | SLit = cast<StringLiteral>(E); |
| 4225 | } |
| 4226 | SmallString<256> Buf; |
| 4227 | llvm::raw_svector_ostream OS(Buf); |
| 4228 | SLit->outputString(OS); |
| 4229 | return cxstring::createDup(OS.str()); |
| 4230 | } |
| 4231 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4232 | const Decl *D = getDeclFromExpr(getCursorExpr(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4233 | if (D) |
| 4234 | return getDeclSpelling(D); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4235 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
| 4238 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4239 | const Stmt *S = getCursorStmt(C); |
| 4240 | if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4241 | return cxstring::createRef(Label->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4242 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4243 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4244 | } |
| 4245 | |
| 4246 | if (C.kind == CXCursor_MacroExpansion) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4247 | return cxstring::createRef(getCursorMacroExpansion(C).getName() |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4248 | ->getNameStart()); |
| 4249 | |
| 4250 | if (C.kind == CXCursor_MacroDefinition) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4251 | return cxstring::createRef(getCursorMacroDefinition(C)->getName() |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4252 | ->getNameStart()); |
| 4253 | |
| 4254 | if (C.kind == CXCursor_InclusionDirective) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4255 | return cxstring::createDup(getCursorInclusionDirective(C)->getFileName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4256 | |
| 4257 | if (clang_isDeclaration(C.kind)) |
| 4258 | return getDeclSpelling(getCursorDecl(C)); |
| 4259 | |
| 4260 | if (C.kind == CXCursor_AnnotateAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 4261 | const AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C)); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4262 | return cxstring::createDup(AA->getAnnotation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
| 4265 | if (C.kind == CXCursor_AsmLabelAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 4266 | const AsmLabelAttr *AA = cast<AsmLabelAttr>(cxcursor::getCursorAttr(C)); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4267 | return cxstring::createDup(AA->getLabel()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 4270 | if (C.kind == CXCursor_PackedAttr) { |
| 4271 | return cxstring::createRef("packed"); |
| 4272 | } |
| 4273 | |
Saleem Abdulrasool | 79c6971 | 2015-09-05 18:53:43 +0000 | [diff] [blame] | 4274 | if (C.kind == CXCursor_VisibilityAttr) { |
| 4275 | const VisibilityAttr *AA = cast<VisibilityAttr>(cxcursor::getCursorAttr(C)); |
| 4276 | switch (AA->getVisibility()) { |
| 4277 | case VisibilityAttr::VisibilityType::Default: |
| 4278 | return cxstring::createRef("default"); |
| 4279 | case VisibilityAttr::VisibilityType::Hidden: |
| 4280 | return cxstring::createRef("hidden"); |
| 4281 | case VisibilityAttr::VisibilityType::Protected: |
| 4282 | return cxstring::createRef("protected"); |
| 4283 | } |
| 4284 | llvm_unreachable("unknown visibility type"); |
| 4285 | } |
| 4286 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4287 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | CXSourceRange clang_Cursor_getSpellingNameRange(CXCursor C, |
| 4291 | unsigned pieceIndex, |
| 4292 | unsigned options) { |
| 4293 | if (clang_Cursor_isNull(C)) |
| 4294 | return clang_getNullRange(); |
| 4295 | |
| 4296 | ASTContext &Ctx = getCursorContext(C); |
| 4297 | |
| 4298 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4299 | const Stmt *S = getCursorStmt(C); |
| 4300 | if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4301 | if (pieceIndex > 0) |
| 4302 | return clang_getNullRange(); |
| 4303 | return cxloc::translateSourceRange(Ctx, Label->getIdentLoc()); |
| 4304 | } |
| 4305 | |
| 4306 | return clang_getNullRange(); |
| 4307 | } |
| 4308 | |
| 4309 | if (C.kind == CXCursor_ObjCMessageExpr) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4310 | if (const ObjCMessageExpr * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4311 | ME = dyn_cast_or_null<ObjCMessageExpr>(getCursorExpr(C))) { |
| 4312 | if (pieceIndex >= ME->getNumSelectorLocs()) |
| 4313 | return clang_getNullRange(); |
| 4314 | return cxloc::translateSourceRange(Ctx, ME->getSelectorLoc(pieceIndex)); |
| 4315 | } |
| 4316 | } |
| 4317 | |
| 4318 | if (C.kind == CXCursor_ObjCInstanceMethodDecl || |
| 4319 | C.kind == CXCursor_ObjCClassMethodDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4320 | if (const ObjCMethodDecl * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4321 | MD = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(C))) { |
| 4322 | if (pieceIndex >= MD->getNumSelectorLocs()) |
| 4323 | return clang_getNullRange(); |
| 4324 | return cxloc::translateSourceRange(Ctx, MD->getSelectorLoc(pieceIndex)); |
| 4325 | } |
| 4326 | } |
| 4327 | |
| 4328 | if (C.kind == CXCursor_ObjCCategoryDecl || |
| 4329 | C.kind == CXCursor_ObjCCategoryImplDecl) { |
| 4330 | if (pieceIndex > 0) |
| 4331 | return clang_getNullRange(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4332 | if (const ObjCCategoryDecl * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4333 | CD = dyn_cast_or_null<ObjCCategoryDecl>(getCursorDecl(C))) |
| 4334 | return cxloc::translateSourceRange(Ctx, CD->getCategoryNameLoc()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4335 | if (const ObjCCategoryImplDecl * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4336 | CID = dyn_cast_or_null<ObjCCategoryImplDecl>(getCursorDecl(C))) |
| 4337 | return cxloc::translateSourceRange(Ctx, CID->getCategoryNameLoc()); |
| 4338 | } |
| 4339 | |
| 4340 | if (C.kind == CXCursor_ModuleImportDecl) { |
| 4341 | if (pieceIndex > 0) |
| 4342 | return clang_getNullRange(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4343 | if (const ImportDecl *ImportD = |
| 4344 | dyn_cast_or_null<ImportDecl>(getCursorDecl(C))) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4345 | ArrayRef<SourceLocation> Locs = ImportD->getIdentifierLocs(); |
| 4346 | if (!Locs.empty()) |
| 4347 | return cxloc::translateSourceRange(Ctx, |
| 4348 | SourceRange(Locs.front(), Locs.back())); |
| 4349 | } |
| 4350 | return clang_getNullRange(); |
| 4351 | } |
| 4352 | |
Argyrios Kyrtzidis | a2a1e53 | 2014-08-26 20:23:26 +0000 | [diff] [blame] | 4353 | if (C.kind == CXCursor_CXXMethod || C.kind == CXCursor_Destructor || |
| 4354 | C.kind == CXCursor_ConversionFunction) { |
| 4355 | if (pieceIndex > 0) |
| 4356 | return clang_getNullRange(); |
| 4357 | if (const FunctionDecl *FD = |
| 4358 | dyn_cast_or_null<FunctionDecl>(getCursorDecl(C))) { |
| 4359 | DeclarationNameInfo FunctionName = FD->getNameInfo(); |
| 4360 | return cxloc::translateSourceRange(Ctx, FunctionName.getSourceRange()); |
| 4361 | } |
| 4362 | return clang_getNullRange(); |
| 4363 | } |
| 4364 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4365 | // FIXME: A CXCursor_InclusionDirective should give the location of the |
| 4366 | // filename, but we don't keep track of this. |
| 4367 | |
| 4368 | // FIXME: A CXCursor_AnnotateAttr should give the location of the annotation |
| 4369 | // but we don't keep track of this. |
| 4370 | |
| 4371 | // FIXME: A CXCursor_AsmLabelAttr should give the location of the label |
| 4372 | // but we don't keep track of this. |
| 4373 | |
| 4374 | // Default handling, give the location of the cursor. |
| 4375 | |
| 4376 | if (pieceIndex > 0) |
| 4377 | return clang_getNullRange(); |
| 4378 | |
| 4379 | CXSourceLocation CXLoc = clang_getCursorLocation(C); |
| 4380 | SourceLocation Loc = cxloc::translateSourceLocation(CXLoc); |
| 4381 | return cxloc::translateSourceRange(Ctx, Loc); |
| 4382 | } |
| 4383 | |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4384 | CXString clang_Cursor_getMangling(CXCursor C) { |
| 4385 | if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind)) |
| 4386 | return cxstring::createEmpty(); |
| 4387 | |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4388 | // Mangling only works for functions and variables. |
Eli Bendersky | 7975959 | 2014-08-01 15:01:10 +0000 | [diff] [blame] | 4389 | const Decl *D = getCursorDecl(C); |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4390 | if (!D || !(isa<FunctionDecl>(D) || isa<VarDecl>(D))) |
| 4391 | return cxstring::createEmpty(); |
| 4392 | |
Argyrios Kyrtzidis | ca741ce | 2016-02-14 22:30:14 +0000 | [diff] [blame] | 4393 | ASTContext &Ctx = D->getASTContext(); |
| 4394 | index::CodegenNameGenerator CGNameGen(Ctx); |
| 4395 | return cxstring::createDup(CGNameGen.getName(D)); |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4396 | } |
| 4397 | |
Saleem Abdulrasool | 6003443 | 2015-11-12 03:57:22 +0000 | [diff] [blame] | 4398 | CXStringSet *clang_Cursor_getCXXManglings(CXCursor C) { |
| 4399 | if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind)) |
| 4400 | return nullptr; |
| 4401 | |
| 4402 | const Decl *D = getCursorDecl(C); |
| 4403 | if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D))) |
| 4404 | return nullptr; |
| 4405 | |
Argyrios Kyrtzidis | ca741ce | 2016-02-14 22:30:14 +0000 | [diff] [blame] | 4406 | ASTContext &Ctx = D->getASTContext(); |
| 4407 | index::CodegenNameGenerator CGNameGen(Ctx); |
| 4408 | std::vector<std::string> Manglings = CGNameGen.getAllManglings(D); |
Saleem Abdulrasool | 6003443 | 2015-11-12 03:57:22 +0000 | [diff] [blame] | 4409 | return cxstring::createSet(Manglings); |
| 4410 | } |
| 4411 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4412 | CXString clang_getCursorDisplayName(CXCursor C) { |
| 4413 | if (!clang_isDeclaration(C.kind)) |
| 4414 | return clang_getCursorSpelling(C); |
| 4415 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4416 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4417 | if (!D) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4418 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4419 | |
| 4420 | PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4421 | if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4422 | D = FunTmpl->getTemplatedDecl(); |
| 4423 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4424 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4425 | SmallString<64> Str; |
| 4426 | llvm::raw_svector_ostream OS(Str); |
| 4427 | OS << *Function; |
| 4428 | if (Function->getPrimaryTemplate()) |
| 4429 | OS << "<>"; |
| 4430 | OS << "("; |
| 4431 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { |
| 4432 | if (I) |
| 4433 | OS << ", "; |
| 4434 | OS << Function->getParamDecl(I)->getType().getAsString(Policy); |
| 4435 | } |
| 4436 | |
| 4437 | if (Function->isVariadic()) { |
| 4438 | if (Function->getNumParams()) |
| 4439 | OS << ", "; |
| 4440 | OS << "..."; |
| 4441 | } |
| 4442 | OS << ")"; |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4443 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4444 | } |
| 4445 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4446 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4447 | SmallString<64> Str; |
| 4448 | llvm::raw_svector_ostream OS(Str); |
| 4449 | OS << *ClassTemplate; |
| 4450 | OS << "<"; |
| 4451 | TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); |
| 4452 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 4453 | if (I) |
| 4454 | OS << ", "; |
| 4455 | |
| 4456 | NamedDecl *Param = Params->getParam(I); |
| 4457 | if (Param->getIdentifier()) { |
| 4458 | OS << Param->getIdentifier()->getName(); |
| 4459 | continue; |
| 4460 | } |
| 4461 | |
| 4462 | // There is no parameter name, which makes this tricky. Try to come up |
| 4463 | // with something useful that isn't too long. |
| 4464 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 4465 | OS << (TTP->wasDeclaredWithTypename()? "typename" : "class"); |
| 4466 | else if (NonTypeTemplateParmDecl *NTTP |
| 4467 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 4468 | OS << NTTP->getType().getAsString(Policy); |
| 4469 | else |
| 4470 | OS << "template<...> class"; |
| 4471 | } |
| 4472 | |
| 4473 | OS << ">"; |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4474 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4475 | } |
| 4476 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4477 | if (const ClassTemplateSpecializationDecl *ClassSpec |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4478 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 4479 | // If the type was explicitly written, use that. |
| 4480 | if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4481 | return cxstring::createDup(TSInfo->getType().getAsString(Policy)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4482 | |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 4483 | SmallString<128> Str; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4484 | llvm::raw_svector_ostream OS(Str); |
| 4485 | OS << *ClassSpec; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4486 | TemplateSpecializationType::PrintTemplateArgumentList( |
| 4487 | OS, ClassSpec->getTemplateArgs().asArray(), Policy); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4488 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4489 | } |
| 4490 | |
| 4491 | return clang_getCursorSpelling(C); |
| 4492 | } |
| 4493 | |
| 4494 | CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
| 4495 | switch (Kind) { |
| 4496 | case CXCursor_FunctionDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4497 | return cxstring::createRef("FunctionDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4498 | case CXCursor_TypedefDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4499 | return cxstring::createRef("TypedefDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4500 | case CXCursor_EnumDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4501 | return cxstring::createRef("EnumDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4502 | case CXCursor_EnumConstantDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4503 | return cxstring::createRef("EnumConstantDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4504 | case CXCursor_StructDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4505 | return cxstring::createRef("StructDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4506 | case CXCursor_UnionDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4507 | return cxstring::createRef("UnionDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4508 | case CXCursor_ClassDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4509 | return cxstring::createRef("ClassDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4510 | case CXCursor_FieldDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4511 | return cxstring::createRef("FieldDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4512 | case CXCursor_VarDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4513 | return cxstring::createRef("VarDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4514 | case CXCursor_ParmDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4515 | return cxstring::createRef("ParmDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4516 | case CXCursor_ObjCInterfaceDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4517 | return cxstring::createRef("ObjCInterfaceDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4518 | case CXCursor_ObjCCategoryDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4519 | return cxstring::createRef("ObjCCategoryDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4520 | case CXCursor_ObjCProtocolDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4521 | return cxstring::createRef("ObjCProtocolDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4522 | case CXCursor_ObjCPropertyDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4523 | return cxstring::createRef("ObjCPropertyDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4524 | case CXCursor_ObjCIvarDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4525 | return cxstring::createRef("ObjCIvarDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4526 | case CXCursor_ObjCInstanceMethodDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4527 | return cxstring::createRef("ObjCInstanceMethodDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4528 | case CXCursor_ObjCClassMethodDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4529 | return cxstring::createRef("ObjCClassMethodDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4530 | case CXCursor_ObjCImplementationDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4531 | return cxstring::createRef("ObjCImplementationDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4532 | case CXCursor_ObjCCategoryImplDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4533 | return cxstring::createRef("ObjCCategoryImplDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4534 | case CXCursor_CXXMethod: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4535 | return cxstring::createRef("CXXMethod"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4536 | case CXCursor_UnexposedDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4537 | return cxstring::createRef("UnexposedDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4538 | case CXCursor_ObjCSuperClassRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4539 | return cxstring::createRef("ObjCSuperClassRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4540 | case CXCursor_ObjCProtocolRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4541 | return cxstring::createRef("ObjCProtocolRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4542 | case CXCursor_ObjCClassRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4543 | return cxstring::createRef("ObjCClassRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4544 | case CXCursor_TypeRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4545 | return cxstring::createRef("TypeRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4546 | case CXCursor_TemplateRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4547 | return cxstring::createRef("TemplateRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4548 | case CXCursor_NamespaceRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4549 | return cxstring::createRef("NamespaceRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4550 | case CXCursor_MemberRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4551 | return cxstring::createRef("MemberRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4552 | case CXCursor_LabelRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4553 | return cxstring::createRef("LabelRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4554 | case CXCursor_OverloadedDeclRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4555 | return cxstring::createRef("OverloadedDeclRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4556 | case CXCursor_VariableRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4557 | return cxstring::createRef("VariableRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4558 | case CXCursor_IntegerLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4559 | return cxstring::createRef("IntegerLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4560 | case CXCursor_FloatingLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4561 | return cxstring::createRef("FloatingLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4562 | case CXCursor_ImaginaryLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4563 | return cxstring::createRef("ImaginaryLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4564 | case CXCursor_StringLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4565 | return cxstring::createRef("StringLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4566 | case CXCursor_CharacterLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4567 | return cxstring::createRef("CharacterLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4568 | case CXCursor_ParenExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4569 | return cxstring::createRef("ParenExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4570 | case CXCursor_UnaryOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4571 | return cxstring::createRef("UnaryOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4572 | case CXCursor_ArraySubscriptExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4573 | return cxstring::createRef("ArraySubscriptExpr"); |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 4574 | case CXCursor_OMPArraySectionExpr: |
| 4575 | return cxstring::createRef("OMPArraySectionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4576 | case CXCursor_BinaryOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4577 | return cxstring::createRef("BinaryOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4578 | case CXCursor_CompoundAssignOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4579 | return cxstring::createRef("CompoundAssignOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4580 | case CXCursor_ConditionalOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4581 | return cxstring::createRef("ConditionalOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4582 | case CXCursor_CStyleCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4583 | return cxstring::createRef("CStyleCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4584 | case CXCursor_CompoundLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4585 | return cxstring::createRef("CompoundLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4586 | case CXCursor_InitListExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4587 | return cxstring::createRef("InitListExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4588 | case CXCursor_AddrLabelExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4589 | return cxstring::createRef("AddrLabelExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4590 | case CXCursor_StmtExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4591 | return cxstring::createRef("StmtExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4592 | case CXCursor_GenericSelectionExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4593 | return cxstring::createRef("GenericSelectionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4594 | case CXCursor_GNUNullExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4595 | return cxstring::createRef("GNUNullExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4596 | case CXCursor_CXXStaticCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4597 | return cxstring::createRef("CXXStaticCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4598 | case CXCursor_CXXDynamicCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4599 | return cxstring::createRef("CXXDynamicCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4600 | case CXCursor_CXXReinterpretCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4601 | return cxstring::createRef("CXXReinterpretCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4602 | case CXCursor_CXXConstCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4603 | return cxstring::createRef("CXXConstCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4604 | case CXCursor_CXXFunctionalCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4605 | return cxstring::createRef("CXXFunctionalCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4606 | case CXCursor_CXXTypeidExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4607 | return cxstring::createRef("CXXTypeidExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4608 | case CXCursor_CXXBoolLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4609 | return cxstring::createRef("CXXBoolLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4610 | case CXCursor_CXXNullPtrLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4611 | return cxstring::createRef("CXXNullPtrLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4612 | case CXCursor_CXXThisExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4613 | return cxstring::createRef("CXXThisExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4614 | case CXCursor_CXXThrowExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4615 | return cxstring::createRef("CXXThrowExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4616 | case CXCursor_CXXNewExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4617 | return cxstring::createRef("CXXNewExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4618 | case CXCursor_CXXDeleteExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4619 | return cxstring::createRef("CXXDeleteExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4620 | case CXCursor_UnaryExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4621 | return cxstring::createRef("UnaryExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4622 | case CXCursor_ObjCStringLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4623 | return cxstring::createRef("ObjCStringLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4624 | case CXCursor_ObjCBoolLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4625 | return cxstring::createRef("ObjCBoolLiteralExpr"); |
Erik Pilkington | 29099de | 2016-07-16 00:35:23 +0000 | [diff] [blame] | 4626 | case CXCursor_ObjCAvailabilityCheckExpr: |
| 4627 | return cxstring::createRef("ObjCAvailabilityCheckExpr"); |
Argyrios Kyrtzidis | c2233be | 2013-04-23 17:57:17 +0000 | [diff] [blame] | 4628 | case CXCursor_ObjCSelfExpr: |
| 4629 | return cxstring::createRef("ObjCSelfExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4630 | case CXCursor_ObjCEncodeExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4631 | return cxstring::createRef("ObjCEncodeExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4632 | case CXCursor_ObjCSelectorExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4633 | return cxstring::createRef("ObjCSelectorExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4634 | case CXCursor_ObjCProtocolExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4635 | return cxstring::createRef("ObjCProtocolExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4636 | case CXCursor_ObjCBridgedCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4637 | return cxstring::createRef("ObjCBridgedCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4638 | case CXCursor_BlockExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4639 | return cxstring::createRef("BlockExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4640 | case CXCursor_PackExpansionExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4641 | return cxstring::createRef("PackExpansionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4642 | case CXCursor_SizeOfPackExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4643 | return cxstring::createRef("SizeOfPackExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4644 | case CXCursor_LambdaExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4645 | return cxstring::createRef("LambdaExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4646 | case CXCursor_UnexposedExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4647 | return cxstring::createRef("UnexposedExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4648 | case CXCursor_DeclRefExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4649 | return cxstring::createRef("DeclRefExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4650 | case CXCursor_MemberRefExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4651 | return cxstring::createRef("MemberRefExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4652 | case CXCursor_CallExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4653 | return cxstring::createRef("CallExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4654 | case CXCursor_ObjCMessageExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4655 | return cxstring::createRef("ObjCMessageExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4656 | case CXCursor_UnexposedStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4657 | return cxstring::createRef("UnexposedStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4658 | case CXCursor_DeclStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4659 | return cxstring::createRef("DeclStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4660 | case CXCursor_LabelStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4661 | return cxstring::createRef("LabelStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4662 | case CXCursor_CompoundStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4663 | return cxstring::createRef("CompoundStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4664 | case CXCursor_CaseStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4665 | return cxstring::createRef("CaseStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4666 | case CXCursor_DefaultStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4667 | return cxstring::createRef("DefaultStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4668 | case CXCursor_IfStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4669 | return cxstring::createRef("IfStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4670 | case CXCursor_SwitchStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4671 | return cxstring::createRef("SwitchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4672 | case CXCursor_WhileStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4673 | return cxstring::createRef("WhileStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4674 | case CXCursor_DoStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4675 | return cxstring::createRef("DoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4676 | case CXCursor_ForStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4677 | return cxstring::createRef("ForStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4678 | case CXCursor_GotoStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4679 | return cxstring::createRef("GotoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4680 | case CXCursor_IndirectGotoStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4681 | return cxstring::createRef("IndirectGotoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4682 | case CXCursor_ContinueStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4683 | return cxstring::createRef("ContinueStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4684 | case CXCursor_BreakStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4685 | return cxstring::createRef("BreakStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4686 | case CXCursor_ReturnStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4687 | return cxstring::createRef("ReturnStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4688 | case CXCursor_GCCAsmStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4689 | return cxstring::createRef("GCCAsmStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4690 | case CXCursor_MSAsmStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4691 | return cxstring::createRef("MSAsmStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4692 | case CXCursor_ObjCAtTryStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4693 | return cxstring::createRef("ObjCAtTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4694 | case CXCursor_ObjCAtCatchStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4695 | return cxstring::createRef("ObjCAtCatchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4696 | case CXCursor_ObjCAtFinallyStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4697 | return cxstring::createRef("ObjCAtFinallyStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4698 | case CXCursor_ObjCAtThrowStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4699 | return cxstring::createRef("ObjCAtThrowStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4700 | case CXCursor_ObjCAtSynchronizedStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4701 | return cxstring::createRef("ObjCAtSynchronizedStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4702 | case CXCursor_ObjCAutoreleasePoolStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4703 | return cxstring::createRef("ObjCAutoreleasePoolStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4704 | case CXCursor_ObjCForCollectionStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4705 | return cxstring::createRef("ObjCForCollectionStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4706 | case CXCursor_CXXCatchStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4707 | return cxstring::createRef("CXXCatchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4708 | case CXCursor_CXXTryStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4709 | return cxstring::createRef("CXXTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4710 | case CXCursor_CXXForRangeStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4711 | return cxstring::createRef("CXXForRangeStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4712 | case CXCursor_SEHTryStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4713 | return cxstring::createRef("SEHTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4714 | case CXCursor_SEHExceptStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4715 | return cxstring::createRef("SEHExceptStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4716 | case CXCursor_SEHFinallyStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4717 | return cxstring::createRef("SEHFinallyStmt"); |
Nico Weber | 9b98207 | 2014-07-07 00:12:30 +0000 | [diff] [blame] | 4718 | case CXCursor_SEHLeaveStmt: |
| 4719 | return cxstring::createRef("SEHLeaveStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4720 | case CXCursor_NullStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4721 | return cxstring::createRef("NullStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4722 | case CXCursor_InvalidFile: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4723 | return cxstring::createRef("InvalidFile"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4724 | case CXCursor_InvalidCode: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4725 | return cxstring::createRef("InvalidCode"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4726 | case CXCursor_NoDeclFound: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4727 | return cxstring::createRef("NoDeclFound"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4728 | case CXCursor_NotImplemented: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4729 | return cxstring::createRef("NotImplemented"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4730 | case CXCursor_TranslationUnit: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4731 | return cxstring::createRef("TranslationUnit"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4732 | case CXCursor_UnexposedAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4733 | return cxstring::createRef("UnexposedAttr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4734 | case CXCursor_IBActionAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4735 | return cxstring::createRef("attribute(ibaction)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4736 | case CXCursor_IBOutletAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4737 | return cxstring::createRef("attribute(iboutlet)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4738 | case CXCursor_IBOutletCollectionAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4739 | return cxstring::createRef("attribute(iboutletcollection)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4740 | case CXCursor_CXXFinalAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4741 | return cxstring::createRef("attribute(final)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4742 | case CXCursor_CXXOverrideAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4743 | return cxstring::createRef("attribute(override)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4744 | case CXCursor_AnnotateAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4745 | return cxstring::createRef("attribute(annotate)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4746 | case CXCursor_AsmLabelAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4747 | return cxstring::createRef("asm label"); |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 4748 | case CXCursor_PackedAttr: |
| 4749 | return cxstring::createRef("attribute(packed)"); |
Joey Gouly | 8122838 | 2014-05-01 15:41:58 +0000 | [diff] [blame] | 4750 | case CXCursor_PureAttr: |
| 4751 | return cxstring::createRef("attribute(pure)"); |
| 4752 | case CXCursor_ConstAttr: |
| 4753 | return cxstring::createRef("attribute(const)"); |
| 4754 | case CXCursor_NoDuplicateAttr: |
| 4755 | return cxstring::createRef("attribute(noduplicate)"); |
Eli Bendersky | 2581e66 | 2014-05-28 19:29:58 +0000 | [diff] [blame] | 4756 | case CXCursor_CUDAConstantAttr: |
| 4757 | return cxstring::createRef("attribute(constant)"); |
| 4758 | case CXCursor_CUDADeviceAttr: |
| 4759 | return cxstring::createRef("attribute(device)"); |
| 4760 | case CXCursor_CUDAGlobalAttr: |
| 4761 | return cxstring::createRef("attribute(global)"); |
| 4762 | case CXCursor_CUDAHostAttr: |
| 4763 | return cxstring::createRef("attribute(host)"); |
Eli Bendersky | 9b07147 | 2014-08-08 14:59:00 +0000 | [diff] [blame] | 4764 | case CXCursor_CUDASharedAttr: |
| 4765 | return cxstring::createRef("attribute(shared)"); |
Saleem Abdulrasool | 79c6971 | 2015-09-05 18:53:43 +0000 | [diff] [blame] | 4766 | case CXCursor_VisibilityAttr: |
| 4767 | return cxstring::createRef("attribute(visibility)"); |
Saleem Abdulrasool | 8aa0b80 | 2015-12-10 18:45:18 +0000 | [diff] [blame] | 4768 | case CXCursor_DLLExport: |
| 4769 | return cxstring::createRef("attribute(dllexport)"); |
| 4770 | case CXCursor_DLLImport: |
| 4771 | return cxstring::createRef("attribute(dllimport)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4772 | case CXCursor_PreprocessingDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4773 | return cxstring::createRef("preprocessing directive"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4774 | case CXCursor_MacroDefinition: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4775 | return cxstring::createRef("macro definition"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4776 | case CXCursor_MacroExpansion: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4777 | return cxstring::createRef("macro expansion"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4778 | case CXCursor_InclusionDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4779 | return cxstring::createRef("inclusion directive"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4780 | case CXCursor_Namespace: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4781 | return cxstring::createRef("Namespace"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4782 | case CXCursor_LinkageSpec: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4783 | return cxstring::createRef("LinkageSpec"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4784 | case CXCursor_CXXBaseSpecifier: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4785 | return cxstring::createRef("C++ base class specifier"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4786 | case CXCursor_Constructor: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4787 | return cxstring::createRef("CXXConstructor"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4788 | case CXCursor_Destructor: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4789 | return cxstring::createRef("CXXDestructor"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4790 | case CXCursor_ConversionFunction: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4791 | return cxstring::createRef("CXXConversion"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4792 | case CXCursor_TemplateTypeParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4793 | return cxstring::createRef("TemplateTypeParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4794 | case CXCursor_NonTypeTemplateParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4795 | return cxstring::createRef("NonTypeTemplateParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4796 | case CXCursor_TemplateTemplateParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4797 | return cxstring::createRef("TemplateTemplateParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4798 | case CXCursor_FunctionTemplate: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4799 | return cxstring::createRef("FunctionTemplate"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4800 | case CXCursor_ClassTemplate: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4801 | return cxstring::createRef("ClassTemplate"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4802 | case CXCursor_ClassTemplatePartialSpecialization: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4803 | return cxstring::createRef("ClassTemplatePartialSpecialization"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4804 | case CXCursor_NamespaceAlias: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4805 | return cxstring::createRef("NamespaceAlias"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4806 | case CXCursor_UsingDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4807 | return cxstring::createRef("UsingDirective"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4808 | case CXCursor_UsingDeclaration: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4809 | return cxstring::createRef("UsingDeclaration"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4810 | case CXCursor_TypeAliasDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4811 | return cxstring::createRef("TypeAliasDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4812 | case CXCursor_ObjCSynthesizeDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4813 | return cxstring::createRef("ObjCSynthesizeDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4814 | case CXCursor_ObjCDynamicDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4815 | return cxstring::createRef("ObjCDynamicDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4816 | case CXCursor_CXXAccessSpecifier: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4817 | return cxstring::createRef("CXXAccessSpecifier"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4818 | case CXCursor_ModuleImportDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4819 | return cxstring::createRef("ModuleImport"); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4820 | case CXCursor_OMPParallelDirective: |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 4821 | return cxstring::createRef("OMPParallelDirective"); |
| 4822 | case CXCursor_OMPSimdDirective: |
| 4823 | return cxstring::createRef("OMPSimdDirective"); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 4824 | case CXCursor_OMPForDirective: |
| 4825 | return cxstring::createRef("OMPForDirective"); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 4826 | case CXCursor_OMPForSimdDirective: |
| 4827 | return cxstring::createRef("OMPForSimdDirective"); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 4828 | case CXCursor_OMPSectionsDirective: |
| 4829 | return cxstring::createRef("OMPSectionsDirective"); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 4830 | case CXCursor_OMPSectionDirective: |
| 4831 | return cxstring::createRef("OMPSectionDirective"); |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 4832 | case CXCursor_OMPSingleDirective: |
| 4833 | return cxstring::createRef("OMPSingleDirective"); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 4834 | case CXCursor_OMPMasterDirective: |
| 4835 | return cxstring::createRef("OMPMasterDirective"); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 4836 | case CXCursor_OMPCriticalDirective: |
| 4837 | return cxstring::createRef("OMPCriticalDirective"); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 4838 | case CXCursor_OMPParallelForDirective: |
| 4839 | return cxstring::createRef("OMPParallelForDirective"); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 4840 | case CXCursor_OMPParallelForSimdDirective: |
| 4841 | return cxstring::createRef("OMPParallelForSimdDirective"); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 4842 | case CXCursor_OMPParallelSectionsDirective: |
| 4843 | return cxstring::createRef("OMPParallelSectionsDirective"); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 4844 | case CXCursor_OMPTaskDirective: |
| 4845 | return cxstring::createRef("OMPTaskDirective"); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 4846 | case CXCursor_OMPTaskyieldDirective: |
| 4847 | return cxstring::createRef("OMPTaskyieldDirective"); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 4848 | case CXCursor_OMPBarrierDirective: |
| 4849 | return cxstring::createRef("OMPBarrierDirective"); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 4850 | case CXCursor_OMPTaskwaitDirective: |
| 4851 | return cxstring::createRef("OMPTaskwaitDirective"); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 4852 | case CXCursor_OMPTaskgroupDirective: |
| 4853 | return cxstring::createRef("OMPTaskgroupDirective"); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 4854 | case CXCursor_OMPFlushDirective: |
| 4855 | return cxstring::createRef("OMPFlushDirective"); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 4856 | case CXCursor_OMPOrderedDirective: |
| 4857 | return cxstring::createRef("OMPOrderedDirective"); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 4858 | case CXCursor_OMPAtomicDirective: |
| 4859 | return cxstring::createRef("OMPAtomicDirective"); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 4860 | case CXCursor_OMPTargetDirective: |
| 4861 | return cxstring::createRef("OMPTargetDirective"); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 4862 | case CXCursor_OMPTargetDataDirective: |
| 4863 | return cxstring::createRef("OMPTargetDataDirective"); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 4864 | case CXCursor_OMPTargetEnterDataDirective: |
| 4865 | return cxstring::createRef("OMPTargetEnterDataDirective"); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 4866 | case CXCursor_OMPTargetExitDataDirective: |
| 4867 | return cxstring::createRef("OMPTargetExitDataDirective"); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 4868 | case CXCursor_OMPTargetParallelDirective: |
| 4869 | return cxstring::createRef("OMPTargetParallelDirective"); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 4870 | case CXCursor_OMPTargetParallelForDirective: |
| 4871 | return cxstring::createRef("OMPTargetParallelForDirective"); |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 4872 | case CXCursor_OMPTargetUpdateDirective: |
| 4873 | return cxstring::createRef("OMPTargetUpdateDirective"); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 4874 | case CXCursor_OMPTeamsDirective: |
| 4875 | return cxstring::createRef("OMPTeamsDirective"); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 4876 | case CXCursor_OMPCancellationPointDirective: |
| 4877 | return cxstring::createRef("OMPCancellationPointDirective"); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 4878 | case CXCursor_OMPCancelDirective: |
| 4879 | return cxstring::createRef("OMPCancelDirective"); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 4880 | case CXCursor_OMPTaskLoopDirective: |
| 4881 | return cxstring::createRef("OMPTaskLoopDirective"); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 4882 | case CXCursor_OMPTaskLoopSimdDirective: |
| 4883 | return cxstring::createRef("OMPTaskLoopSimdDirective"); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 4884 | case CXCursor_OMPDistributeDirective: |
| 4885 | return cxstring::createRef("OMPDistributeDirective"); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 4886 | case CXCursor_OMPDistributeParallelForDirective: |
| 4887 | return cxstring::createRef("OMPDistributeParallelForDirective"); |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 4888 | case CXCursor_OMPDistributeParallelForSimdDirective: |
| 4889 | return cxstring::createRef("OMPDistributeParallelForSimdDirective"); |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 4890 | case CXCursor_OMPDistributeSimdDirective: |
| 4891 | return cxstring::createRef("OMPDistributeSimdDirective"); |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 4892 | case CXCursor_OMPTargetParallelForSimdDirective: |
| 4893 | return cxstring::createRef("OMPTargetParallelForSimdDirective"); |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 4894 | case CXCursor_OMPTargetSimdDirective: |
| 4895 | return cxstring::createRef("OMPTargetSimdDirective"); |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 4896 | case CXCursor_OMPTeamsDistributeDirective: |
| 4897 | return cxstring::createRef("OMPTeamsDistributeDirective"); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4898 | case CXCursor_OverloadCandidate: |
| 4899 | return cxstring::createRef("OverloadCandidate"); |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 4900 | case CXCursor_TypeAliasTemplateDecl: |
| 4901 | return cxstring::createRef("TypeAliasTemplateDecl"); |
Olivier Goffart | 8197801 | 2016-06-09 16:15:55 +0000 | [diff] [blame] | 4902 | case CXCursor_StaticAssert: |
| 4903 | return cxstring::createRef("StaticAssert"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
| 4906 | llvm_unreachable("Unhandled CXCursorKind"); |
| 4907 | } |
| 4908 | |
| 4909 | struct GetCursorData { |
| 4910 | SourceLocation TokenBeginLoc; |
| 4911 | bool PointsAtMacroArgExpansion; |
| 4912 | bool VisitedObjCPropertyImplDecl; |
| 4913 | SourceLocation VisitedDeclaratorDeclStartLoc; |
| 4914 | CXCursor &BestCursor; |
| 4915 | |
| 4916 | GetCursorData(SourceManager &SM, |
| 4917 | SourceLocation tokenBegin, CXCursor &outputCursor) |
| 4918 | : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) { |
| 4919 | PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin); |
| 4920 | VisitedObjCPropertyImplDecl = false; |
| 4921 | } |
| 4922 | }; |
| 4923 | |
| 4924 | static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, |
| 4925 | CXCursor parent, |
| 4926 | CXClientData client_data) { |
| 4927 | GetCursorData *Data = static_cast<GetCursorData *>(client_data); |
| 4928 | CXCursor *BestCursor = &Data->BestCursor; |
| 4929 | |
| 4930 | // If we point inside a macro argument we should provide info of what the |
| 4931 | // token is so use the actual cursor, don't replace it with a macro expansion |
| 4932 | // cursor. |
| 4933 | if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion) |
| 4934 | return CXChildVisit_Recurse; |
| 4935 | |
| 4936 | if (clang_isDeclaration(cursor.kind)) { |
| 4937 | // Avoid having the implicit methods override the property decls. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4938 | if (const ObjCMethodDecl *MD |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4939 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 4940 | if (MD->isImplicit()) |
| 4941 | return CXChildVisit_Break; |
| 4942 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4943 | } else if (const ObjCInterfaceDecl *ID |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4944 | = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(cursor))) { |
| 4945 | // Check that when we have multiple @class references in the same line, |
| 4946 | // that later ones do not override the previous ones. |
| 4947 | // If we have: |
| 4948 | // @class Foo, Bar; |
| 4949 | // source ranges for both start at '@', so 'Bar' will end up overriding |
| 4950 | // 'Foo' even though the cursor location was at 'Foo'. |
| 4951 | if (BestCursor->kind == CXCursor_ObjCInterfaceDecl || |
| 4952 | BestCursor->kind == CXCursor_ObjCClassRef) |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4953 | if (const ObjCInterfaceDecl *PrevID |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4954 | = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(*BestCursor))){ |
| 4955 | if (PrevID != ID && |
| 4956 | !PrevID->isThisDeclarationADefinition() && |
| 4957 | !ID->isThisDeclarationADefinition()) |
| 4958 | return CXChildVisit_Break; |
| 4959 | } |
| 4960 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4961 | } else if (const DeclaratorDecl *DD |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4962 | = dyn_cast_or_null<DeclaratorDecl>(getCursorDecl(cursor))) { |
| 4963 | SourceLocation StartLoc = DD->getSourceRange().getBegin(); |
| 4964 | // Check that when we have multiple declarators in the same line, |
| 4965 | // that later ones do not override the previous ones. |
| 4966 | // If we have: |
| 4967 | // int Foo, Bar; |
| 4968 | // source ranges for both start at 'int', so 'Bar' will end up overriding |
| 4969 | // 'Foo' even though the cursor location was at 'Foo'. |
| 4970 | if (Data->VisitedDeclaratorDeclStartLoc == StartLoc) |
| 4971 | return CXChildVisit_Break; |
| 4972 | Data->VisitedDeclaratorDeclStartLoc = StartLoc; |
| 4973 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4974 | } else if (const ObjCPropertyImplDecl *PropImp |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4975 | = dyn_cast_or_null<ObjCPropertyImplDecl>(getCursorDecl(cursor))) { |
| 4976 | (void)PropImp; |
| 4977 | // Check that when we have multiple @synthesize in the same line, |
| 4978 | // that later ones do not override the previous ones. |
| 4979 | // If we have: |
| 4980 | // @synthesize Foo, Bar; |
| 4981 | // source ranges for both start at '@', so 'Bar' will end up overriding |
| 4982 | // 'Foo' even though the cursor location was at 'Foo'. |
| 4983 | if (Data->VisitedObjCPropertyImplDecl) |
| 4984 | return CXChildVisit_Break; |
| 4985 | Data->VisitedObjCPropertyImplDecl = true; |
| 4986 | } |
| 4987 | } |
| 4988 | |
| 4989 | if (clang_isExpression(cursor.kind) && |
| 4990 | clang_isDeclaration(BestCursor->kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4991 | if (const Decl *D = getCursorDecl(*BestCursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4992 | // Avoid having the cursor of an expression replace the declaration cursor |
| 4993 | // when the expression source range overlaps the declaration range. |
| 4994 | // This can happen for C++ constructor expressions whose range generally |
| 4995 | // include the variable declaration, e.g.: |
| 4996 | // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor. |
| 4997 | if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() && |
| 4998 | D->getLocation() == Data->TokenBeginLoc) |
| 4999 | return CXChildVisit_Break; |
| 5000 | } |
| 5001 | } |
| 5002 | |
| 5003 | // If our current best cursor is the construction of a temporary object, |
| 5004 | // don't replace that cursor with a type reference, because we want |
| 5005 | // clang_getCursor() to point at the constructor. |
| 5006 | if (clang_isExpression(BestCursor->kind) && |
| 5007 | isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && |
| 5008 | cursor.kind == CXCursor_TypeRef) { |
| 5009 | // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it |
| 5010 | // as having the actual point on the type reference. |
| 5011 | *BestCursor = getTypeRefedCallExprCursor(*BestCursor); |
| 5012 | return CXChildVisit_Recurse; |
| 5013 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 5014 | |
| 5015 | // If we already have an Objective-C superclass reference, don't |
| 5016 | // update it further. |
| 5017 | if (BestCursor->kind == CXCursor_ObjCSuperClassRef) |
| 5018 | return CXChildVisit_Break; |
| 5019 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5020 | *BestCursor = cursor; |
| 5021 | return CXChildVisit_Recurse; |
| 5022 | } |
| 5023 | |
| 5024 | CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 5025 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 5026 | LOG_BAD_TU(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5027 | return clang_getNullCursor(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 5028 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5029 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 5030 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5031 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 5032 | |
| 5033 | SourceLocation SLoc = cxloc::translateSourceLocation(Loc); |
| 5034 | CXCursor Result = cxcursor::getCursor(TU, SLoc); |
| 5035 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5036 | LOG_FUNC_SECTION { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5037 | CXFile SearchFile; |
| 5038 | unsigned SearchLine, SearchColumn; |
| 5039 | CXFile ResultFile; |
| 5040 | unsigned ResultLine, ResultColumn; |
| 5041 | CXString SearchFileName, ResultFileName, KindSpelling, USR; |
| 5042 | const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : ""; |
| 5043 | CXSourceLocation ResultLoc = clang_getCursorLocation(Result); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5044 | |
| 5045 | clang_getFileLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, |
| 5046 | nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5047 | clang_getFileLocation(ResultLoc, &ResultFile, &ResultLine, |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5048 | &ResultColumn, nullptr); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5049 | SearchFileName = clang_getFileName(SearchFile); |
| 5050 | ResultFileName = clang_getFileName(ResultFile); |
| 5051 | KindSpelling = clang_getCursorKindSpelling(Result.kind); |
| 5052 | USR = clang_getCursorUSR(Result); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5053 | *Log << llvm::format("(%s:%d:%d) = %s", |
| 5054 | clang_getCString(SearchFileName), SearchLine, SearchColumn, |
| 5055 | clang_getCString(KindSpelling)) |
| 5056 | << llvm::format("(%s:%d:%d):%s%s", |
| 5057 | clang_getCString(ResultFileName), ResultLine, ResultColumn, |
| 5058 | clang_getCString(USR), IsDef); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5059 | clang_disposeString(SearchFileName); |
| 5060 | clang_disposeString(ResultFileName); |
| 5061 | clang_disposeString(KindSpelling); |
| 5062 | clang_disposeString(USR); |
| 5063 | |
| 5064 | CXCursor Definition = clang_getCursorDefinition(Result); |
| 5065 | if (!clang_equalCursors(Definition, clang_getNullCursor())) { |
| 5066 | CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition); |
| 5067 | CXString DefinitionKindSpelling |
| 5068 | = clang_getCursorKindSpelling(Definition.kind); |
| 5069 | CXFile DefinitionFile; |
| 5070 | unsigned DefinitionLine, DefinitionColumn; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5071 | clang_getFileLocation(DefinitionLoc, &DefinitionFile, |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5072 | &DefinitionLine, &DefinitionColumn, nullptr); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5073 | CXString DefinitionFileName = clang_getFileName(DefinitionFile); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 5074 | *Log << llvm::format(" -> %s(%s:%d:%d)", |
| 5075 | clang_getCString(DefinitionKindSpelling), |
| 5076 | clang_getCString(DefinitionFileName), |
| 5077 | DefinitionLine, DefinitionColumn); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5078 | clang_disposeString(DefinitionFileName); |
| 5079 | clang_disposeString(DefinitionKindSpelling); |
| 5080 | } |
| 5081 | } |
| 5082 | |
| 5083 | return Result; |
| 5084 | } |
| 5085 | |
| 5086 | CXCursor clang_getNullCursor(void) { |
| 5087 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
| 5088 | } |
| 5089 | |
| 5090 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5091 | // Clear out the "FirstInDeclGroup" part in a declaration cursor, since we |
| 5092 | // can't set consistently. For example, when visiting a DeclStmt we will set |
| 5093 | // it but we don't set it on the result of clang_getCursorDefinition for |
| 5094 | // a reference of the same declaration. |
| 5095 | // FIXME: Setting "FirstInDeclGroup" in CXCursors is a hack that only works |
| 5096 | // when visiting a DeclStmt currently, the AST should be enhanced to be able |
| 5097 | // to provide that kind of info. |
| 5098 | if (clang_isDeclaration(X.kind)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5099 | X.data[1] = nullptr; |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5100 | if (clang_isDeclaration(Y.kind)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5101 | Y.data[1] = nullptr; |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5102 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5103 | return X == Y; |
| 5104 | } |
| 5105 | |
| 5106 | unsigned clang_hashCursor(CXCursor C) { |
| 5107 | unsigned Index = 0; |
| 5108 | if (clang_isExpression(C.kind) || clang_isStatement(C.kind)) |
| 5109 | Index = 1; |
| 5110 | |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5111 | return llvm::DenseMapInfo<std::pair<unsigned, const void*> >::getHashValue( |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5112 | std::make_pair(C.kind, C.data[Index])); |
| 5113 | } |
| 5114 | |
| 5115 | unsigned clang_isInvalid(enum CXCursorKind K) { |
| 5116 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 5117 | } |
| 5118 | |
| 5119 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
| 5120 | return (K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl) || |
| 5121 | (K >= CXCursor_FirstExtraDecl && K <= CXCursor_LastExtraDecl); |
| 5122 | } |
| 5123 | |
| 5124 | unsigned clang_isReference(enum CXCursorKind K) { |
| 5125 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 5126 | } |
| 5127 | |
| 5128 | unsigned clang_isExpression(enum CXCursorKind K) { |
| 5129 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; |
| 5130 | } |
| 5131 | |
| 5132 | unsigned clang_isStatement(enum CXCursorKind K) { |
| 5133 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; |
| 5134 | } |
| 5135 | |
| 5136 | unsigned clang_isAttribute(enum CXCursorKind K) { |
| 5137 | return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr; |
| 5138 | } |
| 5139 | |
| 5140 | unsigned clang_isTranslationUnit(enum CXCursorKind K) { |
| 5141 | return K == CXCursor_TranslationUnit; |
| 5142 | } |
| 5143 | |
| 5144 | unsigned clang_isPreprocessing(enum CXCursorKind K) { |
| 5145 | return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; |
| 5146 | } |
| 5147 | |
| 5148 | unsigned clang_isUnexposed(enum CXCursorKind K) { |
| 5149 | switch (K) { |
| 5150 | case CXCursor_UnexposedDecl: |
| 5151 | case CXCursor_UnexposedExpr: |
| 5152 | case CXCursor_UnexposedStmt: |
| 5153 | case CXCursor_UnexposedAttr: |
| 5154 | return true; |
| 5155 | default: |
| 5156 | return false; |
| 5157 | } |
| 5158 | } |
| 5159 | |
| 5160 | CXCursorKind clang_getCursorKind(CXCursor C) { |
| 5161 | return C.kind; |
| 5162 | } |
| 5163 | |
| 5164 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 5165 | if (clang_isReference(C.kind)) { |
| 5166 | switch (C.kind) { |
| 5167 | case CXCursor_ObjCSuperClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5168 | std::pair<const ObjCInterfaceDecl *, SourceLocation> P |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5169 | = getCursorObjCSuperClassRef(C); |
| 5170 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5171 | } |
| 5172 | |
| 5173 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5174 | std::pair<const ObjCProtocolDecl *, SourceLocation> P |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5175 | = getCursorObjCProtocolRef(C); |
| 5176 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5177 | } |
| 5178 | |
| 5179 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5180 | std::pair<const ObjCInterfaceDecl *, SourceLocation> P |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5181 | = getCursorObjCClassRef(C); |
| 5182 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5183 | } |
| 5184 | |
| 5185 | case CXCursor_TypeRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5186 | std::pair<const TypeDecl *, SourceLocation> P = getCursorTypeRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5187 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5188 | } |
| 5189 | |
| 5190 | case CXCursor_TemplateRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5191 | std::pair<const TemplateDecl *, SourceLocation> P = |
| 5192 | getCursorTemplateRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5193 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5194 | } |
| 5195 | |
| 5196 | case CXCursor_NamespaceRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5197 | std::pair<const NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5198 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5199 | } |
| 5200 | |
| 5201 | case CXCursor_MemberRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5202 | std::pair<const FieldDecl *, SourceLocation> P = getCursorMemberRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5203 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5204 | } |
| 5205 | |
| 5206 | case CXCursor_VariableRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5207 | std::pair<const VarDecl *, SourceLocation> P = getCursorVariableRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5208 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5209 | } |
| 5210 | |
| 5211 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5212 | const CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5213 | if (!BaseSpec) |
| 5214 | return clang_getNullLocation(); |
| 5215 | |
| 5216 | if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) |
| 5217 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5218 | TSInfo->getTypeLoc().getBeginLoc()); |
| 5219 | |
| 5220 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5221 | BaseSpec->getLocStart()); |
| 5222 | } |
| 5223 | |
| 5224 | case CXCursor_LabelRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5225 | std::pair<const LabelStmt *, SourceLocation> P = getCursorLabelRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5226 | return cxloc::translateSourceLocation(getCursorContext(C), P.second); |
| 5227 | } |
| 5228 | |
| 5229 | case CXCursor_OverloadedDeclRef: |
| 5230 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5231 | getCursorOverloadedDeclRef(C).second); |
| 5232 | |
| 5233 | default: |
| 5234 | // FIXME: Need a way to enumerate all non-reference cases. |
| 5235 | llvm_unreachable("Missed a reference kind"); |
| 5236 | } |
| 5237 | } |
| 5238 | |
| 5239 | if (clang_isExpression(C.kind)) |
| 5240 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5241 | getLocationFromExpr(getCursorExpr(C))); |
| 5242 | |
| 5243 | if (clang_isStatement(C.kind)) |
| 5244 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5245 | getCursorStmt(C)->getLocStart()); |
| 5246 | |
| 5247 | if (C.kind == CXCursor_PreprocessingDirective) { |
| 5248 | SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); |
| 5249 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5250 | } |
| 5251 | |
| 5252 | if (C.kind == CXCursor_MacroExpansion) { |
| 5253 | SourceLocation L |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 5254 | = cxcursor::getCursorMacroExpansion(C).getSourceRange().getBegin(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5255 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5256 | } |
| 5257 | |
| 5258 | if (C.kind == CXCursor_MacroDefinition) { |
| 5259 | SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); |
| 5260 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5261 | } |
| 5262 | |
| 5263 | if (C.kind == CXCursor_InclusionDirective) { |
| 5264 | SourceLocation L |
| 5265 | = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); |
| 5266 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5267 | } |
| 5268 | |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 5269 | if (clang_isAttribute(C.kind)) { |
| 5270 | SourceLocation L |
| 5271 | = cxcursor::getCursorAttr(C)->getLocation(); |
| 5272 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5273 | } |
| 5274 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5275 | if (!clang_isDeclaration(C.kind)) |
| 5276 | return clang_getNullLocation(); |
| 5277 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5278 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5279 | if (!D) |
| 5280 | return clang_getNullLocation(); |
| 5281 | |
| 5282 | SourceLocation Loc = D->getLocation(); |
| 5283 | // FIXME: Multiple variables declared in a single declaration |
| 5284 | // currently lack the information needed to correctly determine their |
| 5285 | // ranges when accounting for the type-specifier. We use context |
| 5286 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 5287 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5288 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5289 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 5290 | Loc = VD->getLocation(); |
| 5291 | } |
| 5292 | |
| 5293 | // For ObjC methods, give the start location of the method name. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5294 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5295 | Loc = MD->getSelectorStartLoc(); |
| 5296 | |
| 5297 | return cxloc::translateSourceLocation(getCursorContext(C), Loc); |
| 5298 | } |
| 5299 | |
| 5300 | } // end extern "C" |
| 5301 | |
| 5302 | CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) { |
| 5303 | assert(TU); |
| 5304 | |
| 5305 | // Guard against an invalid SourceLocation, or we may assert in one |
| 5306 | // of the following calls. |
| 5307 | if (SLoc.isInvalid()) |
| 5308 | return clang_getNullCursor(); |
| 5309 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 5310 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5311 | |
| 5312 | // Translate the given source location to make it point at the beginning of |
| 5313 | // the token under the cursor. |
| 5314 | SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), |
| 5315 | CXXUnit->getASTContext().getLangOpts()); |
| 5316 | |
| 5317 | CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 5318 | if (SLoc.isValid()) { |
| 5319 | GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result); |
| 5320 | CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData, |
| 5321 | /*VisitPreprocessorLast=*/true, |
| 5322 | /*VisitIncludedEntities=*/false, |
| 5323 | SourceLocation(SLoc)); |
| 5324 | CursorVis.visitFileRegion(); |
| 5325 | } |
| 5326 | |
| 5327 | return Result; |
| 5328 | } |
| 5329 | |
| 5330 | static SourceRange getRawCursorExtent(CXCursor C) { |
| 5331 | if (clang_isReference(C.kind)) { |
| 5332 | switch (C.kind) { |
| 5333 | case CXCursor_ObjCSuperClassRef: |
| 5334 | return getCursorObjCSuperClassRef(C).second; |
| 5335 | |
| 5336 | case CXCursor_ObjCProtocolRef: |
| 5337 | return getCursorObjCProtocolRef(C).second; |
| 5338 | |
| 5339 | case CXCursor_ObjCClassRef: |
| 5340 | return getCursorObjCClassRef(C).second; |
| 5341 | |
| 5342 | case CXCursor_TypeRef: |
| 5343 | return getCursorTypeRef(C).second; |
| 5344 | |
| 5345 | case CXCursor_TemplateRef: |
| 5346 | return getCursorTemplateRef(C).second; |
| 5347 | |
| 5348 | case CXCursor_NamespaceRef: |
| 5349 | return getCursorNamespaceRef(C).second; |
| 5350 | |
| 5351 | case CXCursor_MemberRef: |
| 5352 | return getCursorMemberRef(C).second; |
| 5353 | |
| 5354 | case CXCursor_CXXBaseSpecifier: |
| 5355 | return getCursorCXXBaseSpecifier(C)->getSourceRange(); |
| 5356 | |
| 5357 | case CXCursor_LabelRef: |
| 5358 | return getCursorLabelRef(C).second; |
| 5359 | |
| 5360 | case CXCursor_OverloadedDeclRef: |
| 5361 | return getCursorOverloadedDeclRef(C).second; |
| 5362 | |
| 5363 | case CXCursor_VariableRef: |
| 5364 | return getCursorVariableRef(C).second; |
| 5365 | |
| 5366 | default: |
| 5367 | // FIXME: Need a way to enumerate all non-reference cases. |
| 5368 | llvm_unreachable("Missed a reference kind"); |
| 5369 | } |
| 5370 | } |
| 5371 | |
| 5372 | if (clang_isExpression(C.kind)) |
| 5373 | return getCursorExpr(C)->getSourceRange(); |
| 5374 | |
| 5375 | if (clang_isStatement(C.kind)) |
| 5376 | return getCursorStmt(C)->getSourceRange(); |
| 5377 | |
| 5378 | if (clang_isAttribute(C.kind)) |
| 5379 | return getCursorAttr(C)->getRange(); |
| 5380 | |
| 5381 | if (C.kind == CXCursor_PreprocessingDirective) |
| 5382 | return cxcursor::getCursorPreprocessingDirective(C); |
| 5383 | |
| 5384 | if (C.kind == CXCursor_MacroExpansion) { |
| 5385 | ASTUnit *TU = getCursorASTUnit(C); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 5386 | SourceRange Range = cxcursor::getCursorMacroExpansion(C).getSourceRange(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5387 | return TU->mapRangeFromPreamble(Range); |
| 5388 | } |
| 5389 | |
| 5390 | if (C.kind == CXCursor_MacroDefinition) { |
| 5391 | ASTUnit *TU = getCursorASTUnit(C); |
| 5392 | SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange(); |
| 5393 | return TU->mapRangeFromPreamble(Range); |
| 5394 | } |
| 5395 | |
| 5396 | if (C.kind == CXCursor_InclusionDirective) { |
| 5397 | ASTUnit *TU = getCursorASTUnit(C); |
| 5398 | SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange(); |
| 5399 | return TU->mapRangeFromPreamble(Range); |
| 5400 | } |
| 5401 | |
| 5402 | if (C.kind == CXCursor_TranslationUnit) { |
| 5403 | ASTUnit *TU = getCursorASTUnit(C); |
| 5404 | FileID MainID = TU->getSourceManager().getMainFileID(); |
| 5405 | SourceLocation Start = TU->getSourceManager().getLocForStartOfFile(MainID); |
| 5406 | SourceLocation End = TU->getSourceManager().getLocForEndOfFile(MainID); |
| 5407 | return SourceRange(Start, End); |
| 5408 | } |
| 5409 | |
| 5410 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5411 | const Decl *D = cxcursor::getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5412 | if (!D) |
| 5413 | return SourceRange(); |
| 5414 | |
| 5415 | SourceRange R = D->getSourceRange(); |
| 5416 | // FIXME: Multiple variables declared in a single declaration |
| 5417 | // currently lack the information needed to correctly determine their |
| 5418 | // ranges when accounting for the type-specifier. We use context |
| 5419 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 5420 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5421 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5422 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 5423 | R.setBegin(VD->getLocation()); |
| 5424 | } |
| 5425 | return R; |
| 5426 | } |
| 5427 | return SourceRange(); |
| 5428 | } |
| 5429 | |
| 5430 | /// \brief Retrieves the "raw" cursor extent, which is then extended to include |
| 5431 | /// the decl-specifier-seq for declarations. |
| 5432 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) { |
| 5433 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5434 | const Decl *D = cxcursor::getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5435 | if (!D) |
| 5436 | return SourceRange(); |
| 5437 | |
| 5438 | SourceRange R = D->getSourceRange(); |
| 5439 | |
| 5440 | // Adjust the start of the location for declarations preceded by |
| 5441 | // declaration specifiers. |
| 5442 | SourceLocation StartLoc; |
| 5443 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
| 5444 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
| 5445 | StartLoc = TI->getTypeLoc().getLocStart(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5446 | } else if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5447 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
| 5448 | StartLoc = TI->getTypeLoc().getLocStart(); |
| 5449 | } |
| 5450 | |
| 5451 | if (StartLoc.isValid() && R.getBegin().isValid() && |
| 5452 | SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin())) |
| 5453 | R.setBegin(StartLoc); |
| 5454 | |
| 5455 | // FIXME: Multiple variables declared in a single declaration |
| 5456 | // currently lack the information needed to correctly determine their |
| 5457 | // ranges when accounting for the type-specifier. We use context |
| 5458 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 5459 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5460 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5461 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 5462 | R.setBegin(VD->getLocation()); |
| 5463 | } |
| 5464 | |
| 5465 | return R; |
| 5466 | } |
| 5467 | |
| 5468 | return getRawCursorExtent(C); |
| 5469 | } |
| 5470 | |
| 5471 | extern "C" { |
| 5472 | |
| 5473 | CXSourceRange clang_getCursorExtent(CXCursor C) { |
| 5474 | SourceRange R = getRawCursorExtent(C); |
| 5475 | if (R.isInvalid()) |
| 5476 | return clang_getNullRange(); |
| 5477 | |
| 5478 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 5479 | } |
| 5480 | |
| 5481 | CXCursor clang_getCursorReferenced(CXCursor C) { |
| 5482 | if (clang_isInvalid(C.kind)) |
| 5483 | return clang_getNullCursor(); |
| 5484 | |
| 5485 | CXTranslationUnit tu = getCursorTU(C); |
| 5486 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5487 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5488 | if (!D) |
| 5489 | return clang_getNullCursor(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5490 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5491 | return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5492 | if (const ObjCPropertyImplDecl *PropImpl = |
| 5493 | dyn_cast<ObjCPropertyImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5494 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 5495 | return MakeCXCursor(Property, tu); |
| 5496 | |
| 5497 | return C; |
| 5498 | } |
| 5499 | |
| 5500 | if (clang_isExpression(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5501 | const Expr *E = getCursorExpr(C); |
| 5502 | const Decl *D = getDeclFromExpr(E); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5503 | if (D) { |
| 5504 | CXCursor declCursor = MakeCXCursor(D, tu); |
| 5505 | declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C), |
| 5506 | declCursor); |
| 5507 | return declCursor; |
| 5508 | } |
| 5509 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5510 | if (const OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5511 | return MakeCursorOverloadedDeclRef(Ovl, tu); |
| 5512 | |
| 5513 | return clang_getNullCursor(); |
| 5514 | } |
| 5515 | |
| 5516 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 5517 | const Stmt *S = getCursorStmt(C); |
| 5518 | if (const GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5519 | if (LabelDecl *label = Goto->getLabel()) |
| 5520 | if (LabelStmt *labelS = label->getStmt()) |
| 5521 | return MakeCXCursor(labelS, getCursorDecl(C), tu); |
| 5522 | |
| 5523 | return clang_getNullCursor(); |
| 5524 | } |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 5525 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5526 | if (C.kind == CXCursor_MacroExpansion) { |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 5527 | if (const MacroDefinitionRecord *Def = |
| 5528 | getCursorMacroExpansion(C).getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5529 | return MakeMacroDefinitionCursor(Def, tu); |
| 5530 | } |
| 5531 | |
| 5532 | if (!clang_isReference(C.kind)) |
| 5533 | return clang_getNullCursor(); |
| 5534 | |
| 5535 | switch (C.kind) { |
| 5536 | case CXCursor_ObjCSuperClassRef: |
| 5537 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu); |
| 5538 | |
| 5539 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5540 | const ObjCProtocolDecl *Prot = getCursorObjCProtocolRef(C).first; |
| 5541 | if (const ObjCProtocolDecl *Def = Prot->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5542 | return MakeCXCursor(Def, tu); |
| 5543 | |
| 5544 | return MakeCXCursor(Prot, tu); |
| 5545 | } |
| 5546 | |
| 5547 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5548 | const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
| 5549 | if (const ObjCInterfaceDecl *Def = Class->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5550 | return MakeCXCursor(Def, tu); |
| 5551 | |
| 5552 | return MakeCXCursor(Class, tu); |
| 5553 | } |
| 5554 | |
| 5555 | case CXCursor_TypeRef: |
| 5556 | return MakeCXCursor(getCursorTypeRef(C).first, tu ); |
| 5557 | |
| 5558 | case CXCursor_TemplateRef: |
| 5559 | return MakeCXCursor(getCursorTemplateRef(C).first, tu ); |
| 5560 | |
| 5561 | case CXCursor_NamespaceRef: |
| 5562 | return MakeCXCursor(getCursorNamespaceRef(C).first, tu ); |
| 5563 | |
| 5564 | case CXCursor_MemberRef: |
| 5565 | return MakeCXCursor(getCursorMemberRef(C).first, tu ); |
| 5566 | |
| 5567 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5568 | const CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5569 | return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), |
| 5570 | tu )); |
| 5571 | } |
| 5572 | |
| 5573 | case CXCursor_LabelRef: |
| 5574 | // FIXME: We end up faking the "parent" declaration here because we |
| 5575 | // don't want to make CXCursor larger. |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 5576 | return MakeCXCursor(getCursorLabelRef(C).first, |
| 5577 | cxtu::getASTUnit(tu)->getASTContext() |
| 5578 | .getTranslationUnitDecl(), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5579 | tu); |
| 5580 | |
| 5581 | case CXCursor_OverloadedDeclRef: |
| 5582 | return C; |
| 5583 | |
| 5584 | case CXCursor_VariableRef: |
| 5585 | return MakeCXCursor(getCursorVariableRef(C).first, tu); |
| 5586 | |
| 5587 | default: |
| 5588 | // We would prefer to enumerate all non-reference cursor kinds here. |
| 5589 | llvm_unreachable("Unhandled reference cursor kind"); |
| 5590 | } |
| 5591 | } |
| 5592 | |
| 5593 | CXCursor clang_getCursorDefinition(CXCursor C) { |
| 5594 | if (clang_isInvalid(C.kind)) |
| 5595 | return clang_getNullCursor(); |
| 5596 | |
| 5597 | CXTranslationUnit TU = getCursorTU(C); |
| 5598 | |
| 5599 | bool WasReference = false; |
| 5600 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { |
| 5601 | C = clang_getCursorReferenced(C); |
| 5602 | WasReference = true; |
| 5603 | } |
| 5604 | |
| 5605 | if (C.kind == CXCursor_MacroExpansion) |
| 5606 | return clang_getCursorReferenced(C); |
| 5607 | |
| 5608 | if (!clang_isDeclaration(C.kind)) |
| 5609 | return clang_getNullCursor(); |
| 5610 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5611 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5612 | if (!D) |
| 5613 | return clang_getNullCursor(); |
| 5614 | |
| 5615 | switch (D->getKind()) { |
| 5616 | // Declaration kinds that don't really separate the notions of |
| 5617 | // declaration and definition. |
| 5618 | case Decl::Namespace: |
| 5619 | case Decl::Typedef: |
| 5620 | case Decl::TypeAlias: |
| 5621 | case Decl::TypeAliasTemplate: |
| 5622 | case Decl::TemplateTypeParm: |
| 5623 | case Decl::EnumConstant: |
| 5624 | case Decl::Field: |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 5625 | case Decl::Binding: |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5626 | case Decl::MSProperty: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5627 | case Decl::IndirectField: |
| 5628 | case Decl::ObjCIvar: |
| 5629 | case Decl::ObjCAtDefsField: |
| 5630 | case Decl::ImplicitParam: |
| 5631 | case Decl::ParmVar: |
| 5632 | case Decl::NonTypeTemplateParm: |
| 5633 | case Decl::TemplateTemplateParm: |
| 5634 | case Decl::ObjCCategoryImpl: |
| 5635 | case Decl::ObjCImplementation: |
| 5636 | case Decl::AccessSpec: |
| 5637 | case Decl::LinkageSpec: |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame^] | 5638 | case Decl::Export: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5639 | case Decl::ObjCPropertyImpl: |
| 5640 | case Decl::FileScopeAsm: |
| 5641 | case Decl::StaticAssert: |
| 5642 | case Decl::Block: |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 5643 | case Decl::Captured: |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 5644 | case Decl::OMPCapturedExpr: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5645 | case Decl::Label: // FIXME: Is this right?? |
| 5646 | case Decl::ClassScopeFunctionSpecialization: |
| 5647 | case Decl::Import: |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 5648 | case Decl::OMPThreadPrivate: |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 5649 | case Decl::OMPDeclareReduction: |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 5650 | case Decl::ObjCTypeParam: |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 5651 | case Decl::BuiltinTemplate: |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 5652 | case Decl::PragmaComment: |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 5653 | case Decl::PragmaDetectMismatch: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5654 | return C; |
| 5655 | |
| 5656 | // Declaration kinds that don't make any sense here, but are |
| 5657 | // nonetheless harmless. |
David Blaikie | f005d3c | 2013-02-22 17:44:58 +0000 | [diff] [blame] | 5658 | case Decl::Empty: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5659 | case Decl::TranslationUnit: |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 5660 | case Decl::ExternCContext: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5661 | break; |
| 5662 | |
| 5663 | // Declaration kinds for which the definition is not resolvable. |
| 5664 | case Decl::UnresolvedUsingTypename: |
| 5665 | case Decl::UnresolvedUsingValue: |
| 5666 | break; |
| 5667 | |
| 5668 | case Decl::UsingDirective: |
| 5669 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), |
| 5670 | TU); |
| 5671 | |
| 5672 | case Decl::NamespaceAlias: |
| 5673 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU); |
| 5674 | |
| 5675 | case Decl::Enum: |
| 5676 | case Decl::Record: |
| 5677 | case Decl::CXXRecord: |
| 5678 | case Decl::ClassTemplateSpecialization: |
| 5679 | case Decl::ClassTemplatePartialSpecialization: |
| 5680 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) |
| 5681 | return MakeCXCursor(Def, TU); |
| 5682 | return clang_getNullCursor(); |
| 5683 | |
| 5684 | case Decl::Function: |
| 5685 | case Decl::CXXMethod: |
| 5686 | case Decl::CXXConstructor: |
| 5687 | case Decl::CXXDestructor: |
| 5688 | case Decl::CXXConversion: { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5689 | const FunctionDecl *Def = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5690 | if (cast<FunctionDecl>(D)->getBody(Def)) |
Dmitri Gribenko | 9c256e3 | 2013-01-14 00:46:27 +0000 | [diff] [blame] | 5691 | return MakeCXCursor(Def, TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5692 | return clang_getNullCursor(); |
| 5693 | } |
| 5694 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5695 | case Decl::Var: |
| 5696 | case Decl::VarTemplateSpecialization: |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 5697 | case Decl::VarTemplatePartialSpecialization: |
| 5698 | case Decl::Decomposition: { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5699 | // Ask the variable if it has a definition. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5700 | if (const VarDecl *Def = cast<VarDecl>(D)->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5701 | return MakeCXCursor(Def, TU); |
| 5702 | return clang_getNullCursor(); |
| 5703 | } |
| 5704 | |
| 5705 | case Decl::FunctionTemplate: { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5706 | const FunctionDecl *Def = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5707 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) |
| 5708 | return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU); |
| 5709 | return clang_getNullCursor(); |
| 5710 | } |
| 5711 | |
| 5712 | case Decl::ClassTemplate: { |
| 5713 | if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() |
| 5714 | ->getDefinition()) |
| 5715 | return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), |
| 5716 | TU); |
| 5717 | return clang_getNullCursor(); |
| 5718 | } |
| 5719 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5720 | case Decl::VarTemplate: { |
| 5721 | if (VarDecl *Def = |
| 5722 | cast<VarTemplateDecl>(D)->getTemplatedDecl()->getDefinition()) |
| 5723 | return MakeCXCursor(cast<VarDecl>(Def)->getDescribedVarTemplate(), TU); |
| 5724 | return clang_getNullCursor(); |
| 5725 | } |
| 5726 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5727 | case Decl::Using: |
| 5728 | return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), |
| 5729 | D->getLocation(), TU); |
| 5730 | |
| 5731 | case Decl::UsingShadow: |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 5732 | case Decl::ConstructorUsingShadow: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5733 | return clang_getCursorDefinition( |
| 5734 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), |
| 5735 | TU)); |
| 5736 | |
| 5737 | case Decl::ObjCMethod: { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5738 | const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5739 | if (Method->isThisDeclarationADefinition()) |
| 5740 | return C; |
| 5741 | |
| 5742 | // Dig out the method definition in the associated |
| 5743 | // @implementation, if we have it. |
| 5744 | // FIXME: The ASTs should make finding the definition easier. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5745 | if (const ObjCInterfaceDecl *Class |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5746 | = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) |
| 5747 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) |
| 5748 | if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), |
| 5749 | Method->isInstanceMethod())) |
| 5750 | if (Def->isThisDeclarationADefinition()) |
| 5751 | return MakeCXCursor(Def, TU); |
| 5752 | |
| 5753 | return clang_getNullCursor(); |
| 5754 | } |
| 5755 | |
| 5756 | case Decl::ObjCCategory: |
| 5757 | if (ObjCCategoryImplDecl *Impl |
| 5758 | = cast<ObjCCategoryDecl>(D)->getImplementation()) |
| 5759 | return MakeCXCursor(Impl, TU); |
| 5760 | return clang_getNullCursor(); |
| 5761 | |
| 5762 | case Decl::ObjCProtocol: |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5763 | if (const ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(D)->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5764 | return MakeCXCursor(Def, TU); |
| 5765 | return clang_getNullCursor(); |
| 5766 | |
| 5767 | case Decl::ObjCInterface: { |
| 5768 | // There are two notions of a "definition" for an Objective-C |
| 5769 | // class: the interface and its implementation. When we resolved a |
| 5770 | // reference to an Objective-C class, produce the @interface as |
| 5771 | // the definition; when we were provided with the interface, |
| 5772 | // produce the @implementation as the definition. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5773 | const ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5774 | if (WasReference) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5775 | if (const ObjCInterfaceDecl *Def = IFace->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5776 | return MakeCXCursor(Def, TU); |
| 5777 | } else if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
| 5778 | return MakeCXCursor(Impl, TU); |
| 5779 | return clang_getNullCursor(); |
| 5780 | } |
| 5781 | |
| 5782 | case Decl::ObjCProperty: |
| 5783 | // FIXME: We don't really know where to find the |
| 5784 | // ObjCPropertyImplDecls that implement this property. |
| 5785 | return clang_getNullCursor(); |
| 5786 | |
| 5787 | case Decl::ObjCCompatibleAlias: |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5788 | if (const ObjCInterfaceDecl *Class |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5789 | = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5790 | if (const ObjCInterfaceDecl *Def = Class->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5791 | return MakeCXCursor(Def, TU); |
| 5792 | |
| 5793 | return clang_getNullCursor(); |
| 5794 | |
| 5795 | case Decl::Friend: |
| 5796 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) |
| 5797 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
| 5798 | return clang_getNullCursor(); |
| 5799 | |
| 5800 | case Decl::FriendTemplate: |
| 5801 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) |
| 5802 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
| 5803 | return clang_getNullCursor(); |
| 5804 | } |
| 5805 | |
| 5806 | return clang_getNullCursor(); |
| 5807 | } |
| 5808 | |
| 5809 | unsigned clang_isCursorDefinition(CXCursor C) { |
| 5810 | if (!clang_isDeclaration(C.kind)) |
| 5811 | return 0; |
| 5812 | |
| 5813 | return clang_getCursorDefinition(C) == C; |
| 5814 | } |
| 5815 | |
| 5816 | CXCursor clang_getCanonicalCursor(CXCursor C) { |
| 5817 | if (!clang_isDeclaration(C.kind)) |
| 5818 | return C; |
| 5819 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5820 | if (const Decl *D = getCursorDecl(C)) { |
| 5821 | if (const ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5822 | if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl()) |
| 5823 | return MakeCXCursor(CatD, getCursorTU(C)); |
| 5824 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5825 | if (const ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) |
| 5826 | if (const ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5827 | return MakeCXCursor(IFD, getCursorTU(C)); |
| 5828 | |
| 5829 | return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C)); |
| 5830 | } |
| 5831 | |
| 5832 | return C; |
| 5833 | } |
| 5834 | |
| 5835 | int clang_Cursor_getObjCSelectorIndex(CXCursor cursor) { |
| 5836 | return cxcursor::getSelectorIdentifierIndexAndLoc(cursor).first; |
| 5837 | } |
| 5838 | |
| 5839 | unsigned clang_getNumOverloadedDecls(CXCursor C) { |
| 5840 | if (C.kind != CXCursor_OverloadedDeclRef) |
| 5841 | return 0; |
| 5842 | |
| 5843 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5844 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5845 | return E->getNumDecls(); |
| 5846 | |
| 5847 | if (OverloadedTemplateStorage *S |
| 5848 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 5849 | return S->size(); |
| 5850 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5851 | const Decl *D = Storage.get<const Decl *>(); |
| 5852 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5853 | return Using->shadow_size(); |
| 5854 | |
| 5855 | return 0; |
| 5856 | } |
| 5857 | |
| 5858 | CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { |
| 5859 | if (cursor.kind != CXCursor_OverloadedDeclRef) |
| 5860 | return clang_getNullCursor(); |
| 5861 | |
| 5862 | if (index >= clang_getNumOverloadedDecls(cursor)) |
| 5863 | return clang_getNullCursor(); |
| 5864 | |
| 5865 | CXTranslationUnit TU = getCursorTU(cursor); |
| 5866 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5867 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5868 | return MakeCXCursor(E->decls_begin()[index], TU); |
| 5869 | |
| 5870 | if (OverloadedTemplateStorage *S |
| 5871 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 5872 | return MakeCXCursor(S->begin()[index], TU); |
| 5873 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5874 | const Decl *D = Storage.get<const Decl *>(); |
| 5875 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5876 | // FIXME: This is, unfortunately, linear time. |
| 5877 | UsingDecl::shadow_iterator Pos = Using->shadow_begin(); |
| 5878 | std::advance(Pos, index); |
| 5879 | return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU); |
| 5880 | } |
| 5881 | |
| 5882 | return clang_getNullCursor(); |
| 5883 | } |
| 5884 | |
| 5885 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
| 5886 | const char **startBuf, |
| 5887 | const char **endBuf, |
| 5888 | unsigned *startLine, |
| 5889 | unsigned *startColumn, |
| 5890 | unsigned *endLine, |
| 5891 | unsigned *endColumn) { |
| 5892 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5893 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(getCursorDecl(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5894 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
| 5895 | |
| 5896 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 5897 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 5898 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 5899 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 5900 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 5901 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 5902 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 5903 | } |
| 5904 | |
| 5905 | |
| 5906 | CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags, |
| 5907 | unsigned PieceIndex) { |
| 5908 | RefNamePieces Pieces; |
| 5909 | |
| 5910 | switch (C.kind) { |
| 5911 | case CXCursor_MemberRefExpr: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 5912 | if (const MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5913 | Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(), |
| 5914 | E->getQualifierLoc().getSourceRange()); |
| 5915 | break; |
| 5916 | |
| 5917 | case CXCursor_DeclRefExpr: |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 5918 | if (const DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C))) { |
| 5919 | SourceRange TemplateArgLoc(E->getLAngleLoc(), E->getRAngleLoc()); |
| 5920 | Pieces = |
| 5921 | buildPieces(NameFlags, false, E->getNameInfo(), |
| 5922 | E->getQualifierLoc().getSourceRange(), &TemplateArgLoc); |
| 5923 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5924 | break; |
| 5925 | |
| 5926 | case CXCursor_CallExpr: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 5927 | if (const CXXOperatorCallExpr *OCE = |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5928 | dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 5929 | const Expr *Callee = OCE->getCallee(); |
| 5930 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5931 | Callee = ICE->getSubExpr(); |
| 5932 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 5933 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5934 | Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(), |
| 5935 | DRE->getQualifierLoc().getSourceRange()); |
| 5936 | } |
| 5937 | break; |
| 5938 | |
| 5939 | default: |
| 5940 | break; |
| 5941 | } |
| 5942 | |
| 5943 | if (Pieces.empty()) { |
| 5944 | if (PieceIndex == 0) |
| 5945 | return clang_getCursorExtent(C); |
| 5946 | } else if (PieceIndex < Pieces.size()) { |
| 5947 | SourceRange R = Pieces[PieceIndex]; |
| 5948 | if (R.isValid()) |
| 5949 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 5950 | } |
| 5951 | |
| 5952 | return clang_getNullRange(); |
| 5953 | } |
| 5954 | |
| 5955 | void clang_enableStackTraces(void) { |
Richard Smith | dfed58a | 2016-06-09 00:53:41 +0000 | [diff] [blame] | 5956 | // FIXME: Provide an argv0 here so we can find llvm-symbolizer. |
| 5957 | llvm::sys::PrintStackTraceOnErrorSignal(StringRef()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5958 | } |
| 5959 | |
| 5960 | void clang_executeOnThread(void (*fn)(void*), void *user_data, |
| 5961 | unsigned stack_size) { |
| 5962 | llvm::llvm_execute_on_thread(fn, user_data, stack_size); |
| 5963 | } |
| 5964 | |
| 5965 | } // end: extern "C" |
| 5966 | |
| 5967 | //===----------------------------------------------------------------------===// |
| 5968 | // Token-based Operations. |
| 5969 | //===----------------------------------------------------------------------===// |
| 5970 | |
| 5971 | /* CXToken layout: |
| 5972 | * int_data[0]: a CXTokenKind |
| 5973 | * int_data[1]: starting token location |
| 5974 | * int_data[2]: token length |
| 5975 | * int_data[3]: reserved |
| 5976 | * ptr_data: for identifiers and keywords, an IdentifierInfo*. |
| 5977 | * otherwise unused. |
| 5978 | */ |
| 5979 | extern "C" { |
| 5980 | |
| 5981 | CXTokenKind clang_getTokenKind(CXToken CXTok) { |
| 5982 | return static_cast<CXTokenKind>(CXTok.int_data[0]); |
| 5983 | } |
| 5984 | |
| 5985 | CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { |
| 5986 | switch (clang_getTokenKind(CXTok)) { |
| 5987 | case CXToken_Identifier: |
| 5988 | case CXToken_Keyword: |
| 5989 | // We know we have an IdentifierInfo*, so use that. |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5990 | return cxstring::createRef(static_cast<IdentifierInfo *>(CXTok.ptr_data) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5991 | ->getNameStart()); |
| 5992 | |
| 5993 | case CXToken_Literal: { |
| 5994 | // We have stashed the starting pointer in the ptr_data field. Use it. |
| 5995 | const char *Text = static_cast<const char *>(CXTok.ptr_data); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 5996 | return cxstring::createDup(StringRef(Text, CXTok.int_data[2])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5997 | } |
| 5998 | |
| 5999 | case CXToken_Punctuation: |
| 6000 | case CXToken_Comment: |
| 6001 | break; |
| 6002 | } |
| 6003 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6004 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6005 | LOG_BAD_TU(TU); |
| 6006 | return cxstring::createEmpty(); |
| 6007 | } |
| 6008 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6009 | // We have to find the starting buffer pointer the hard way, by |
| 6010 | // deconstructing the source location. |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6011 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6012 | if (!CXXUnit) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 6013 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6014 | |
| 6015 | SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); |
| 6016 | std::pair<FileID, unsigned> LocInfo |
| 6017 | = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc); |
| 6018 | bool Invalid = false; |
| 6019 | StringRef Buffer |
| 6020 | = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); |
| 6021 | if (Invalid) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 6022 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6023 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 6024 | return cxstring::createDup(Buffer.substr(LocInfo.second, CXTok.int_data[2])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6025 | } |
| 6026 | |
| 6027 | CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6028 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6029 | LOG_BAD_TU(TU); |
| 6030 | return clang_getNullLocation(); |
| 6031 | } |
| 6032 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6033 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6034 | if (!CXXUnit) |
| 6035 | return clang_getNullLocation(); |
| 6036 | |
| 6037 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), |
| 6038 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 6039 | } |
| 6040 | |
| 6041 | CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6042 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6043 | LOG_BAD_TU(TU); |
| 6044 | return clang_getNullRange(); |
| 6045 | } |
| 6046 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6047 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6048 | if (!CXXUnit) |
| 6049 | return clang_getNullRange(); |
| 6050 | |
| 6051 | return cxloc::translateSourceRange(CXXUnit->getASTContext(), |
| 6052 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 6053 | } |
| 6054 | |
| 6055 | static void getTokens(ASTUnit *CXXUnit, SourceRange Range, |
| 6056 | SmallVectorImpl<CXToken> &CXTokens) { |
| 6057 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 6058 | std::pair<FileID, unsigned> BeginLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6059 | = SourceMgr.getDecomposedSpellingLoc(Range.getBegin()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6060 | std::pair<FileID, unsigned> EndLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6061 | = SourceMgr.getDecomposedSpellingLoc(Range.getEnd()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6062 | |
| 6063 | // Cannot tokenize across files. |
| 6064 | if (BeginLocInfo.first != EndLocInfo.first) |
| 6065 | return; |
| 6066 | |
| 6067 | // Create a lexer |
| 6068 | bool Invalid = false; |
| 6069 | StringRef Buffer |
| 6070 | = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 6071 | if (Invalid) |
| 6072 | return; |
| 6073 | |
| 6074 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 6075 | CXXUnit->getASTContext().getLangOpts(), |
| 6076 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); |
| 6077 | Lex.SetCommentRetentionState(true); |
| 6078 | |
| 6079 | // Lex tokens until we hit the end of the range. |
| 6080 | const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; |
| 6081 | Token Tok; |
| 6082 | bool previousWasAt = false; |
| 6083 | do { |
| 6084 | // Lex the next token |
| 6085 | Lex.LexFromRawLexer(Tok); |
| 6086 | if (Tok.is(tok::eof)) |
| 6087 | break; |
| 6088 | |
| 6089 | // Initialize the CXToken. |
| 6090 | CXToken CXTok; |
| 6091 | |
| 6092 | // - Common fields |
| 6093 | CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); |
| 6094 | CXTok.int_data[2] = Tok.getLength(); |
| 6095 | CXTok.int_data[3] = 0; |
| 6096 | |
| 6097 | // - Kind-specific fields |
| 6098 | if (Tok.isLiteral()) { |
| 6099 | CXTok.int_data[0] = CXToken_Literal; |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 6100 | CXTok.ptr_data = const_cast<char *>(Tok.getLiteralData()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6101 | } else if (Tok.is(tok::raw_identifier)) { |
| 6102 | // Lookup the identifier to determine whether we have a keyword. |
| 6103 | IdentifierInfo *II |
| 6104 | = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok); |
| 6105 | |
| 6106 | if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { |
| 6107 | CXTok.int_data[0] = CXToken_Keyword; |
| 6108 | } |
| 6109 | else { |
| 6110 | CXTok.int_data[0] = Tok.is(tok::identifier) |
| 6111 | ? CXToken_Identifier |
| 6112 | : CXToken_Keyword; |
| 6113 | } |
| 6114 | CXTok.ptr_data = II; |
| 6115 | } else if (Tok.is(tok::comment)) { |
| 6116 | CXTok.int_data[0] = CXToken_Comment; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6117 | CXTok.ptr_data = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6118 | } else { |
| 6119 | CXTok.int_data[0] = CXToken_Punctuation; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6120 | CXTok.ptr_data = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6121 | } |
| 6122 | CXTokens.push_back(CXTok); |
| 6123 | previousWasAt = Tok.is(tok::at); |
| 6124 | } while (Lex.getBufferLocation() <= EffectiveBufferEnd); |
| 6125 | } |
| 6126 | |
| 6127 | void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, |
| 6128 | CXToken **Tokens, unsigned *NumTokens) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 6129 | LOG_FUNC_SECTION { |
| 6130 | *Log << TU << ' ' << Range; |
| 6131 | } |
| 6132 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6133 | if (Tokens) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6134 | *Tokens = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6135 | if (NumTokens) |
| 6136 | *NumTokens = 0; |
| 6137 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6138 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6139 | LOG_BAD_TU(TU); |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 6140 | return; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6141 | } |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 6142 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6143 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6144 | if (!CXXUnit || !Tokens || !NumTokens) |
| 6145 | return; |
| 6146 | |
| 6147 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 6148 | |
| 6149 | SourceRange R = cxloc::translateCXSourceRange(Range); |
| 6150 | if (R.isInvalid()) |
| 6151 | return; |
| 6152 | |
| 6153 | SmallVector<CXToken, 32> CXTokens; |
| 6154 | getTokens(CXXUnit, R, CXTokens); |
| 6155 | |
| 6156 | if (CXTokens.empty()) |
| 6157 | return; |
| 6158 | |
| 6159 | *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size()); |
| 6160 | memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); |
| 6161 | *NumTokens = CXTokens.size(); |
| 6162 | } |
| 6163 | |
| 6164 | void clang_disposeTokens(CXTranslationUnit TU, |
| 6165 | CXToken *Tokens, unsigned NumTokens) { |
| 6166 | free(Tokens); |
| 6167 | } |
| 6168 | |
| 6169 | } // end: extern "C" |
| 6170 | |
| 6171 | //===----------------------------------------------------------------------===// |
| 6172 | // Token annotation APIs. |
| 6173 | //===----------------------------------------------------------------------===// |
| 6174 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6175 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 6176 | CXCursor parent, |
| 6177 | CXClientData client_data); |
| 6178 | static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor, |
| 6179 | CXClientData client_data); |
| 6180 | |
| 6181 | namespace { |
| 6182 | class AnnotateTokensWorker { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6183 | CXToken *Tokens; |
| 6184 | CXCursor *Cursors; |
| 6185 | unsigned NumTokens; |
| 6186 | unsigned TokIdx; |
| 6187 | unsigned PreprocessingTokIdx; |
| 6188 | CursorVisitor AnnotateVis; |
| 6189 | SourceManager &SrcMgr; |
| 6190 | bool HasContextSensitiveKeywords; |
| 6191 | |
| 6192 | struct PostChildrenInfo { |
| 6193 | CXCursor Cursor; |
| 6194 | SourceRange CursorRange; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6195 | unsigned BeforeReachingCursorIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6196 | unsigned BeforeChildrenTokenIdx; |
| 6197 | }; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6198 | SmallVector<PostChildrenInfo, 8> PostChildrenInfos; |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6199 | |
| 6200 | CXToken &getTok(unsigned Idx) { |
| 6201 | assert(Idx < NumTokens); |
| 6202 | return Tokens[Idx]; |
| 6203 | } |
| 6204 | const CXToken &getTok(unsigned Idx) const { |
| 6205 | assert(Idx < NumTokens); |
| 6206 | return Tokens[Idx]; |
| 6207 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6208 | bool MoreTokens() const { return TokIdx < NumTokens; } |
| 6209 | unsigned NextToken() const { return TokIdx; } |
| 6210 | void AdvanceToken() { ++TokIdx; } |
| 6211 | SourceLocation GetTokenLoc(unsigned tokI) { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6212 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6213 | } |
| 6214 | bool isFunctionMacroToken(unsigned tokI) const { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6215 | return getTok(tokI).int_data[3] != 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6216 | } |
| 6217 | SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6218 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[3]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6219 | } |
| 6220 | |
| 6221 | void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange); |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6222 | bool annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6223 | SourceRange); |
| 6224 | |
| 6225 | public: |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6226 | AnnotateTokensWorker(CXToken *tokens, CXCursor *cursors, unsigned numTokens, |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6227 | CXTranslationUnit TU, SourceRange RegionOfInterest) |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6228 | : Tokens(tokens), Cursors(cursors), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6229 | NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0), |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6230 | AnnotateVis(TU, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6231 | AnnotateTokensVisitor, this, |
| 6232 | /*VisitPreprocessorLast=*/true, |
| 6233 | /*VisitIncludedEntities=*/false, |
| 6234 | RegionOfInterest, |
| 6235 | /*VisitDeclsOnly=*/false, |
| 6236 | AnnotateTokensPostChildrenVisitor), |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6237 | SrcMgr(cxtu::getASTUnit(TU)->getSourceManager()), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6238 | HasContextSensitiveKeywords(false) { } |
| 6239 | |
| 6240 | void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } |
| 6241 | enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); |
| 6242 | bool postVisitChildren(CXCursor cursor); |
| 6243 | void AnnotateTokens(); |
| 6244 | |
| 6245 | /// \brief Determine whether the annotator saw any cursors that have |
| 6246 | /// context-sensitive keywords. |
| 6247 | bool hasContextSensitiveKeywords() const { |
| 6248 | return HasContextSensitiveKeywords; |
| 6249 | } |
| 6250 | |
| 6251 | ~AnnotateTokensWorker() { |
| 6252 | assert(PostChildrenInfos.empty()); |
| 6253 | } |
| 6254 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6255 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6256 | |
| 6257 | void AnnotateTokensWorker::AnnotateTokens() { |
| 6258 | // Walk the AST within the region of interest, annotating tokens |
| 6259 | // along the way. |
| 6260 | AnnotateVis.visitFileRegion(); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6261 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6262 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6263 | static inline void updateCursorAnnotation(CXCursor &Cursor, |
| 6264 | const CXCursor &updateC) { |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6265 | if (clang_isInvalid(updateC.kind) || !clang_isInvalid(Cursor.kind)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6266 | return; |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6267 | Cursor = updateC; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6268 | } |
| 6269 | |
| 6270 | /// \brief It annotates and advances tokens with a cursor until the comparison |
| 6271 | //// between the cursor location and the source range is the same as |
| 6272 | /// \arg compResult. |
| 6273 | /// |
| 6274 | /// Pass RangeBefore to annotate tokens with a cursor until a range is reached. |
| 6275 | /// Pass RangeOverlap to annotate tokens inside a range. |
| 6276 | void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC, |
| 6277 | RangeComparisonResult compResult, |
| 6278 | SourceRange range) { |
| 6279 | while (MoreTokens()) { |
| 6280 | const unsigned I = NextToken(); |
| 6281 | if (isFunctionMacroToken(I)) |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6282 | if (!annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range)) |
| 6283 | return; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6284 | |
| 6285 | SourceLocation TokLoc = GetTokenLoc(I); |
| 6286 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6287 | updateCursorAnnotation(Cursors[I], updateC); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6288 | AdvanceToken(); |
| 6289 | continue; |
| 6290 | } |
| 6291 | break; |
| 6292 | } |
| 6293 | } |
| 6294 | |
| 6295 | /// \brief Special annotation handling for macro argument tokens. |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6296 | /// \returns true if it advanced beyond all macro tokens, false otherwise. |
| 6297 | bool AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens( |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6298 | CXCursor updateC, |
| 6299 | RangeComparisonResult compResult, |
| 6300 | SourceRange range) { |
| 6301 | assert(MoreTokens()); |
| 6302 | assert(isFunctionMacroToken(NextToken()) && |
| 6303 | "Should be called only for macro arg tokens"); |
| 6304 | |
| 6305 | // This works differently than annotateAndAdvanceTokens; because expanded |
| 6306 | // macro arguments can have arbitrary translation-unit source order, we do not |
| 6307 | // advance the token index one by one until a token fails the range test. |
| 6308 | // We only advance once past all of the macro arg tokens if all of them |
| 6309 | // pass the range test. If one of them fails we keep the token index pointing |
| 6310 | // at the start of the macro arg tokens so that the failing token will be |
| 6311 | // annotated by a subsequent annotation try. |
| 6312 | |
| 6313 | bool atLeastOneCompFail = false; |
| 6314 | |
| 6315 | unsigned I = NextToken(); |
| 6316 | for (; I < NumTokens && isFunctionMacroToken(I); ++I) { |
| 6317 | SourceLocation TokLoc = getFunctionMacroTokenLoc(I); |
| 6318 | if (TokLoc.isFileID()) |
| 6319 | continue; // not macro arg token, it's parens or comma. |
| 6320 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 6321 | if (clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 6322 | Cursors[I] = updateC; |
| 6323 | } else |
| 6324 | atLeastOneCompFail = true; |
| 6325 | } |
| 6326 | |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6327 | if (atLeastOneCompFail) |
| 6328 | return false; |
| 6329 | |
| 6330 | TokIdx = I; // All of the tokens were handled, advance beyond all of them. |
| 6331 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6332 | } |
| 6333 | |
| 6334 | enum CXChildVisitResult |
| 6335 | AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6336 | SourceRange cursorRange = getRawCursorExtent(cursor); |
| 6337 | if (cursorRange.isInvalid()) |
| 6338 | return CXChildVisit_Recurse; |
| 6339 | |
| 6340 | if (!HasContextSensitiveKeywords) { |
| 6341 | // Objective-C properties can have context-sensitive keywords. |
| 6342 | if (cursor.kind == CXCursor_ObjCPropertyDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6343 | if (const ObjCPropertyDecl *Property |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6344 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor))) |
| 6345 | HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0; |
| 6346 | } |
| 6347 | // Objective-C methods can have context-sensitive keywords. |
| 6348 | else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl || |
| 6349 | cursor.kind == CXCursor_ObjCClassMethodDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6350 | if (const ObjCMethodDecl *Method |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6351 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 6352 | if (Method->getObjCDeclQualifier()) |
| 6353 | HasContextSensitiveKeywords = true; |
| 6354 | else { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 6355 | for (const auto *P : Method->parameters()) { |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 6356 | if (P->getObjCDeclQualifier()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6357 | HasContextSensitiveKeywords = true; |
| 6358 | break; |
| 6359 | } |
| 6360 | } |
| 6361 | } |
| 6362 | } |
| 6363 | } |
| 6364 | // C++ methods can have context-sensitive keywords. |
| 6365 | else if (cursor.kind == CXCursor_CXXMethod) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6366 | if (const CXXMethodDecl *Method |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6367 | = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) { |
| 6368 | if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>()) |
| 6369 | HasContextSensitiveKeywords = true; |
| 6370 | } |
| 6371 | } |
| 6372 | // C++ classes can have context-sensitive keywords. |
| 6373 | else if (cursor.kind == CXCursor_StructDecl || |
| 6374 | cursor.kind == CXCursor_ClassDecl || |
| 6375 | cursor.kind == CXCursor_ClassTemplate || |
| 6376 | cursor.kind == CXCursor_ClassTemplatePartialSpecialization) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6377 | if (const Decl *D = getCursorDecl(cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6378 | if (D->hasAttr<FinalAttr>()) |
| 6379 | HasContextSensitiveKeywords = true; |
| 6380 | } |
| 6381 | } |
Argyrios Kyrtzidis | 990b386 | 2013-06-04 18:24:30 +0000 | [diff] [blame] | 6382 | |
| 6383 | // Don't override a property annotation with its getter/setter method. |
| 6384 | if (cursor.kind == CXCursor_ObjCInstanceMethodDecl && |
| 6385 | parent.kind == CXCursor_ObjCPropertyDecl) |
| 6386 | return CXChildVisit_Continue; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6387 | |
| 6388 | if (clang_isPreprocessing(cursor.kind)) { |
| 6389 | // Items in the preprocessing record are kept separate from items in |
| 6390 | // declarations, so we keep a separate token index. |
| 6391 | unsigned SavedTokIdx = TokIdx; |
| 6392 | TokIdx = PreprocessingTokIdx; |
| 6393 | |
| 6394 | // Skip tokens up until we catch up to the beginning of the preprocessing |
| 6395 | // entry. |
| 6396 | while (MoreTokens()) { |
| 6397 | const unsigned I = NextToken(); |
| 6398 | SourceLocation TokLoc = GetTokenLoc(I); |
| 6399 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 6400 | case RangeBefore: |
| 6401 | AdvanceToken(); |
| 6402 | continue; |
| 6403 | case RangeAfter: |
| 6404 | case RangeOverlap: |
| 6405 | break; |
| 6406 | } |
| 6407 | break; |
| 6408 | } |
| 6409 | |
| 6410 | // Look at all of the tokens within this range. |
| 6411 | while (MoreTokens()) { |
| 6412 | const unsigned I = NextToken(); |
| 6413 | SourceLocation TokLoc = GetTokenLoc(I); |
| 6414 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 6415 | case RangeBefore: |
| 6416 | llvm_unreachable("Infeasible"); |
| 6417 | case RangeAfter: |
| 6418 | break; |
| 6419 | case RangeOverlap: |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6420 | // For macro expansions, just note where the beginning of the macro |
| 6421 | // expansion occurs. |
| 6422 | if (cursor.kind == CXCursor_MacroExpansion) { |
| 6423 | if (TokLoc == cursorRange.getBegin()) |
| 6424 | Cursors[I] = cursor; |
| 6425 | AdvanceToken(); |
| 6426 | break; |
| 6427 | } |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6428 | // We may have already annotated macro names inside macro definitions. |
| 6429 | if (Cursors[I].kind != CXCursor_MacroExpansion) |
| 6430 | Cursors[I] = cursor; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6431 | AdvanceToken(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6432 | continue; |
| 6433 | } |
| 6434 | break; |
| 6435 | } |
| 6436 | |
| 6437 | // Save the preprocessing token index; restore the non-preprocessing |
| 6438 | // token index. |
| 6439 | PreprocessingTokIdx = TokIdx; |
| 6440 | TokIdx = SavedTokIdx; |
| 6441 | return CXChildVisit_Recurse; |
| 6442 | } |
| 6443 | |
| 6444 | if (cursorRange.isInvalid()) |
| 6445 | return CXChildVisit_Continue; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6446 | |
| 6447 | unsigned BeforeReachingCursorIdx = NextToken(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6448 | const enum CXCursorKind cursorK = clang_getCursorKind(cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6449 | const enum CXCursorKind K = clang_getCursorKind(parent); |
| 6450 | const CXCursor updateC = |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6451 | (clang_isInvalid(K) || K == CXCursor_TranslationUnit || |
| 6452 | // Attributes are annotated out-of-order, skip tokens until we reach it. |
| 6453 | clang_isAttribute(cursor.kind)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6454 | ? clang_getNullCursor() : parent; |
| 6455 | |
| 6456 | annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange); |
| 6457 | |
| 6458 | // Avoid having the cursor of an expression "overwrite" the annotation of the |
| 6459 | // variable declaration that it belongs to. |
| 6460 | // This can happen for C++ constructor expressions whose range generally |
| 6461 | // include the variable declaration, e.g.: |
| 6462 | // 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] | 6463 | if (clang_isExpression(cursorK) && MoreTokens()) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6464 | const Expr *E = getCursorExpr(cursor); |
Dmitri Gribenko | a169118 | 2013-01-26 18:12:08 +0000 | [diff] [blame] | 6465 | if (const Decl *D = getCursorParentDecl(cursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6466 | const unsigned I = NextToken(); |
| 6467 | if (E->getLocStart().isValid() && D->getLocation().isValid() && |
| 6468 | E->getLocStart() == D->getLocation() && |
| 6469 | E->getLocStart() == GetTokenLoc(I)) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6470 | updateCursorAnnotation(Cursors[I], updateC); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6471 | AdvanceToken(); |
| 6472 | } |
| 6473 | } |
| 6474 | } |
| 6475 | |
| 6476 | // Before recursing into the children keep some state that we are going |
| 6477 | // to use in the AnnotateTokensWorker::postVisitChildren callback to do some |
| 6478 | // extra work after the child nodes are visited. |
| 6479 | // Note that we don't call VisitChildren here to avoid traversing statements |
| 6480 | // code-recursively which can blow the stack. |
| 6481 | |
| 6482 | PostChildrenInfo Info; |
| 6483 | Info.Cursor = cursor; |
| 6484 | Info.CursorRange = cursorRange; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6485 | Info.BeforeReachingCursorIdx = BeforeReachingCursorIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6486 | Info.BeforeChildrenTokenIdx = NextToken(); |
| 6487 | PostChildrenInfos.push_back(Info); |
| 6488 | |
| 6489 | return CXChildVisit_Recurse; |
| 6490 | } |
| 6491 | |
| 6492 | bool AnnotateTokensWorker::postVisitChildren(CXCursor cursor) { |
| 6493 | if (PostChildrenInfos.empty()) |
| 6494 | return false; |
| 6495 | const PostChildrenInfo &Info = PostChildrenInfos.back(); |
| 6496 | if (!clang_equalCursors(Info.Cursor, cursor)) |
| 6497 | return false; |
| 6498 | |
| 6499 | const unsigned BeforeChildren = Info.BeforeChildrenTokenIdx; |
| 6500 | const unsigned AfterChildren = NextToken(); |
| 6501 | SourceRange cursorRange = Info.CursorRange; |
| 6502 | |
| 6503 | // Scan the tokens that are at the end of the cursor, but are not captured |
| 6504 | // but the child cursors. |
| 6505 | annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange); |
| 6506 | |
| 6507 | // Scan the tokens that are at the beginning of the cursor, but are not |
| 6508 | // capture by the child cursors. |
| 6509 | for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { |
| 6510 | if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 6511 | break; |
| 6512 | |
| 6513 | Cursors[I] = cursor; |
| 6514 | } |
| 6515 | |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6516 | // Attributes are annotated out-of-order, rewind TokIdx to when we first |
| 6517 | // encountered the attribute cursor. |
| 6518 | if (clang_isAttribute(cursor.kind)) |
| 6519 | TokIdx = Info.BeforeReachingCursorIdx; |
| 6520 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6521 | PostChildrenInfos.pop_back(); |
| 6522 | return false; |
| 6523 | } |
| 6524 | |
| 6525 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 6526 | CXCursor parent, |
| 6527 | CXClientData client_data) { |
| 6528 | return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); |
| 6529 | } |
| 6530 | |
| 6531 | static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor, |
| 6532 | CXClientData client_data) { |
| 6533 | return static_cast<AnnotateTokensWorker*>(client_data)-> |
| 6534 | postVisitChildren(cursor); |
| 6535 | } |
| 6536 | |
| 6537 | namespace { |
| 6538 | |
| 6539 | /// \brief Uses the macro expansions in the preprocessing record to find |
| 6540 | /// and mark tokens that are macro arguments. This info is used by the |
| 6541 | /// AnnotateTokensWorker. |
| 6542 | class MarkMacroArgTokensVisitor { |
| 6543 | SourceManager &SM; |
| 6544 | CXToken *Tokens; |
| 6545 | unsigned NumTokens; |
| 6546 | unsigned CurIdx; |
| 6547 | |
| 6548 | public: |
| 6549 | MarkMacroArgTokensVisitor(SourceManager &SM, |
| 6550 | CXToken *tokens, unsigned numTokens) |
| 6551 | : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { } |
| 6552 | |
| 6553 | CXChildVisitResult visit(CXCursor cursor, CXCursor parent) { |
| 6554 | if (cursor.kind != CXCursor_MacroExpansion) |
| 6555 | return CXChildVisit_Continue; |
| 6556 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 6557 | SourceRange macroRange = getCursorMacroExpansion(cursor).getSourceRange(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6558 | if (macroRange.getBegin() == macroRange.getEnd()) |
| 6559 | return CXChildVisit_Continue; // it's not a function macro. |
| 6560 | |
| 6561 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 6562 | if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx), |
| 6563 | macroRange.getBegin())) |
| 6564 | break; |
| 6565 | } |
| 6566 | |
| 6567 | if (CurIdx == NumTokens) |
| 6568 | return CXChildVisit_Break; |
| 6569 | |
| 6570 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 6571 | SourceLocation tokLoc = getTokenLoc(CurIdx); |
| 6572 | if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd())) |
| 6573 | break; |
| 6574 | |
| 6575 | setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc)); |
| 6576 | } |
| 6577 | |
| 6578 | if (CurIdx == NumTokens) |
| 6579 | return CXChildVisit_Break; |
| 6580 | |
| 6581 | return CXChildVisit_Continue; |
| 6582 | } |
| 6583 | |
| 6584 | private: |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6585 | CXToken &getTok(unsigned Idx) { |
| 6586 | assert(Idx < NumTokens); |
| 6587 | return Tokens[Idx]; |
| 6588 | } |
| 6589 | const CXToken &getTok(unsigned Idx) const { |
| 6590 | assert(Idx < NumTokens); |
| 6591 | return Tokens[Idx]; |
| 6592 | } |
| 6593 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6594 | SourceLocation getTokenLoc(unsigned tokI) { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6595 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6596 | } |
| 6597 | |
| 6598 | void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) { |
| 6599 | // The third field is reserved and currently not used. Use it here |
| 6600 | // to mark macro arg expanded tokens with their expanded locations. |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6601 | getTok(tokI).int_data[3] = loc.getRawEncoding(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6602 | } |
| 6603 | }; |
| 6604 | |
| 6605 | } // end anonymous namespace |
| 6606 | |
| 6607 | static CXChildVisitResult |
| 6608 | MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent, |
| 6609 | CXClientData client_data) { |
| 6610 | return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor, |
| 6611 | parent); |
| 6612 | } |
| 6613 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6614 | /// \brief Used by \c annotatePreprocessorTokens. |
| 6615 | /// \returns true if lexing was finished, false otherwise. |
| 6616 | static bool lexNext(Lexer &Lex, Token &Tok, |
| 6617 | unsigned &NextIdx, unsigned NumTokens) { |
| 6618 | if (NextIdx >= NumTokens) |
| 6619 | return true; |
| 6620 | |
| 6621 | ++NextIdx; |
| 6622 | Lex.LexFromRawLexer(Tok); |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 6623 | return Tok.is(tok::eof); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6624 | } |
| 6625 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6626 | static void annotatePreprocessorTokens(CXTranslationUnit TU, |
| 6627 | SourceRange RegionOfInterest, |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6628 | CXCursor *Cursors, |
| 6629 | CXToken *Tokens, |
| 6630 | unsigned NumTokens) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6631 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6632 | |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6633 | Preprocessor &PP = CXXUnit->getPreprocessor(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6634 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 6635 | std::pair<FileID, unsigned> BeginLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6636 | = SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getBegin()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6637 | std::pair<FileID, unsigned> EndLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6638 | = SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getEnd()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6639 | |
| 6640 | if (BeginLocInfo.first != EndLocInfo.first) |
| 6641 | return; |
| 6642 | |
| 6643 | StringRef Buffer; |
| 6644 | bool Invalid = false; |
| 6645 | Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 6646 | if (Buffer.empty() || Invalid) |
| 6647 | return; |
| 6648 | |
| 6649 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 6650 | CXXUnit->getASTContext().getLangOpts(), |
| 6651 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, |
| 6652 | Buffer.end()); |
| 6653 | Lex.SetCommentRetentionState(true); |
| 6654 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6655 | unsigned NextIdx = 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6656 | // Lex tokens in raw mode until we hit the end of the range, to avoid |
| 6657 | // entering #includes or expanding macros. |
| 6658 | while (true) { |
| 6659 | Token Tok; |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6660 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 6661 | break; |
| 6662 | unsigned TokIdx = NextIdx-1; |
| 6663 | assert(Tok.getLocation() == |
| 6664 | SourceLocation::getFromRawEncoding(Tokens[TokIdx].int_data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6665 | |
| 6666 | reprocess: |
| 6667 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6668 | // We have found a preprocessing directive. Annotate the tokens |
| 6669 | // appropriately. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6670 | // |
| 6671 | // FIXME: Some simple tests here could identify macro definitions and |
| 6672 | // #undefs, to provide specific cursor kinds for those. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6673 | |
| 6674 | SourceLocation BeginLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6675 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 6676 | break; |
| 6677 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6678 | MacroInfo *MI = nullptr; |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 6679 | if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "define") { |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6680 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 6681 | break; |
| 6682 | |
| 6683 | if (Tok.is(tok::raw_identifier)) { |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 6684 | IdentifierInfo &II = |
| 6685 | PP.getIdentifierTable().get(Tok.getRawIdentifier()); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6686 | SourceLocation MappedTokLoc = |
| 6687 | CXXUnit->mapLocationToPreamble(Tok.getLocation()); |
| 6688 | MI = getMacroInfo(II, MappedTokLoc, TU); |
| 6689 | } |
| 6690 | } |
| 6691 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6692 | bool finished = false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6693 | do { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6694 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) { |
| 6695 | finished = true; |
| 6696 | break; |
| 6697 | } |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6698 | // If we are in a macro definition, check if the token was ever a |
| 6699 | // macro name and annotate it if that's the case. |
| 6700 | if (MI) { |
| 6701 | SourceLocation SaveLoc = Tok.getLocation(); |
| 6702 | Tok.setLocation(CXXUnit->mapLocationToPreamble(SaveLoc)); |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 6703 | MacroDefinitionRecord *MacroDef = |
| 6704 | checkForMacroInMacroDefinition(MI, Tok, TU); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6705 | Tok.setLocation(SaveLoc); |
| 6706 | if (MacroDef) |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 6707 | Cursors[NextIdx - 1] = |
| 6708 | MakeMacroExpansionCursor(MacroDef, Tok.getLocation(), TU); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6709 | } |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6710 | } while (!Tok.isAtStartOfLine()); |
| 6711 | |
| 6712 | unsigned LastIdx = finished ? NextIdx-1 : NextIdx-2; |
| 6713 | assert(TokIdx <= LastIdx); |
| 6714 | SourceLocation EndLoc = |
| 6715 | SourceLocation::getFromRawEncoding(Tokens[LastIdx].int_data[1]); |
| 6716 | CXCursor Cursor = |
| 6717 | MakePreprocessingDirectiveCursor(SourceRange(BeginLoc, EndLoc), TU); |
| 6718 | |
| 6719 | for (; TokIdx <= LastIdx; ++TokIdx) |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6720 | updateCursorAnnotation(Cursors[TokIdx], Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6721 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6722 | if (finished) |
| 6723 | break; |
| 6724 | goto reprocess; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6725 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6726 | } |
| 6727 | } |
| 6728 | |
| 6729 | // This gets run a separate thread to avoid stack blowout. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 6730 | static void clang_annotateTokensImpl(CXTranslationUnit TU, ASTUnit *CXXUnit, |
| 6731 | CXToken *Tokens, unsigned NumTokens, |
| 6732 | CXCursor *Cursors) { |
Dmitri Gribenko | 183436e | 2013-01-26 21:49:50 +0000 | [diff] [blame] | 6733 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6734 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) |
| 6735 | setThreadBackgroundPriority(); |
| 6736 | |
| 6737 | // Determine the region of interest, which contains all of the tokens. |
| 6738 | SourceRange RegionOfInterest; |
| 6739 | RegionOfInterest.setBegin( |
| 6740 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0]))); |
| 6741 | RegionOfInterest.setEnd( |
| 6742 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, |
| 6743 | Tokens[NumTokens-1]))); |
| 6744 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6745 | // Relex the tokens within the source range to look for preprocessing |
| 6746 | // directives. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6747 | annotatePreprocessorTokens(TU, RegionOfInterest, Cursors, Tokens, NumTokens); |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6748 | |
| 6749 | // If begin location points inside a macro argument, set it to the expansion |
| 6750 | // location so we can have the full context when annotating semantically. |
| 6751 | { |
| 6752 | SourceManager &SM = CXXUnit->getSourceManager(); |
| 6753 | SourceLocation Loc = |
| 6754 | SM.getMacroArgExpandedLocation(RegionOfInterest.getBegin()); |
| 6755 | if (Loc.isMacroID()) |
| 6756 | RegionOfInterest.setBegin(SM.getExpansionLoc(Loc)); |
| 6757 | } |
| 6758 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6759 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) { |
| 6760 | // Search and mark tokens that are macro argument expansions. |
| 6761 | MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(), |
| 6762 | Tokens, NumTokens); |
| 6763 | CursorVisitor MacroArgMarker(TU, |
| 6764 | MarkMacroArgTokensVisitorDelegate, &Visitor, |
| 6765 | /*VisitPreprocessorLast=*/true, |
| 6766 | /*VisitIncludedEntities=*/false, |
| 6767 | RegionOfInterest); |
| 6768 | MacroArgMarker.visitPreprocessedEntitiesInRegion(); |
| 6769 | } |
| 6770 | |
| 6771 | // Annotate all of the source locations in the region of interest that map to |
| 6772 | // a specific cursor. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6773 | AnnotateTokensWorker W(Tokens, Cursors, NumTokens, TU, RegionOfInterest); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6774 | |
| 6775 | // FIXME: We use a ridiculous stack size here because the data-recursion |
| 6776 | // algorithm uses a large stack frame than the non-data recursive version, |
| 6777 | // and AnnotationTokensWorker currently transforms the data-recursion |
| 6778 | // algorithm back into a traditional recursion by explicitly calling |
| 6779 | // VisitChildren(). We will need to remove this explicit recursive call. |
| 6780 | W.AnnotateTokens(); |
| 6781 | |
| 6782 | // If we ran into any entities that involve context-sensitive keywords, |
| 6783 | // take another pass through the tokens to mark them as such. |
| 6784 | if (W.hasContextSensitiveKeywords()) { |
| 6785 | for (unsigned I = 0; I != NumTokens; ++I) { |
| 6786 | if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier) |
| 6787 | continue; |
| 6788 | |
| 6789 | if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) { |
| 6790 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6791 | if (const ObjCPropertyDecl *Property |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6792 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) { |
| 6793 | if (Property->getPropertyAttributesAsWritten() != 0 && |
| 6794 | llvm::StringSwitch<bool>(II->getName()) |
| 6795 | .Case("readonly", true) |
| 6796 | .Case("assign", true) |
| 6797 | .Case("unsafe_unretained", true) |
| 6798 | .Case("readwrite", true) |
| 6799 | .Case("retain", true) |
| 6800 | .Case("copy", true) |
| 6801 | .Case("nonatomic", true) |
| 6802 | .Case("atomic", true) |
| 6803 | .Case("getter", true) |
| 6804 | .Case("setter", true) |
| 6805 | .Case("strong", true) |
| 6806 | .Case("weak", true) |
Manman Ren | 04fd4d8 | 2016-05-31 23:22:04 +0000 | [diff] [blame] | 6807 | .Case("class", true) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6808 | .Default(false)) |
| 6809 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 6810 | } |
| 6811 | continue; |
| 6812 | } |
| 6813 | |
| 6814 | if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl || |
| 6815 | Cursors[I].kind == CXCursor_ObjCClassMethodDecl) { |
| 6816 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 6817 | if (llvm::StringSwitch<bool>(II->getName()) |
| 6818 | .Case("in", true) |
| 6819 | .Case("out", true) |
| 6820 | .Case("inout", true) |
| 6821 | .Case("oneway", true) |
| 6822 | .Case("bycopy", true) |
| 6823 | .Case("byref", true) |
| 6824 | .Default(false)) |
| 6825 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 6826 | continue; |
| 6827 | } |
| 6828 | |
| 6829 | if (Cursors[I].kind == CXCursor_CXXFinalAttr || |
| 6830 | Cursors[I].kind == CXCursor_CXXOverrideAttr) { |
| 6831 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 6832 | continue; |
| 6833 | } |
| 6834 | } |
| 6835 | } |
| 6836 | } |
| 6837 | |
| 6838 | extern "C" { |
| 6839 | |
| 6840 | void clang_annotateTokens(CXTranslationUnit TU, |
| 6841 | CXToken *Tokens, unsigned NumTokens, |
| 6842 | CXCursor *Cursors) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6843 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6844 | LOG_BAD_TU(TU); |
| 6845 | return; |
| 6846 | } |
| 6847 | if (NumTokens == 0 || !Tokens || !Cursors) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 6848 | LOG_FUNC_SECTION { *Log << "<null input>"; } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6849 | return; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 6850 | } |
| 6851 | |
| 6852 | LOG_FUNC_SECTION { |
| 6853 | *Log << TU << ' '; |
| 6854 | CXSourceLocation bloc = clang_getTokenLocation(TU, Tokens[0]); |
| 6855 | CXSourceLocation eloc = clang_getTokenLocation(TU, Tokens[NumTokens-1]); |
| 6856 | *Log << clang_getRange(bloc, eloc); |
| 6857 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6858 | |
| 6859 | // Any token we don't specifically annotate will have a NULL cursor. |
| 6860 | CXCursor C = clang_getNullCursor(); |
| 6861 | for (unsigned I = 0; I != NumTokens; ++I) |
| 6862 | Cursors[I] = C; |
| 6863 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6864 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6865 | if (!CXXUnit) |
| 6866 | return; |
| 6867 | |
| 6868 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 6869 | |
| 6870 | auto AnnotateTokensImpl = [=]() { |
| 6871 | clang_annotateTokensImpl(TU, CXXUnit, Tokens, NumTokens, Cursors); |
| 6872 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6873 | llvm::CrashRecoveryContext CRC; |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 6874 | if (!RunSafely(CRC, AnnotateTokensImpl, GetSafetyThreadStackSize() * 2)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6875 | fprintf(stderr, "libclang: crash detected while annotating tokens\n"); |
| 6876 | } |
| 6877 | } |
| 6878 | |
| 6879 | } // end: extern "C" |
| 6880 | |
| 6881 | //===----------------------------------------------------------------------===// |
| 6882 | // Operations for querying linkage of a cursor. |
| 6883 | //===----------------------------------------------------------------------===// |
| 6884 | |
| 6885 | extern "C" { |
| 6886 | CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { |
| 6887 | if (!clang_isDeclaration(cursor.kind)) |
| 6888 | return CXLinkage_Invalid; |
| 6889 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6890 | const Decl *D = cxcursor::getCursorDecl(cursor); |
| 6891 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 6892 | switch (ND->getLinkageInternal()) { |
Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 6893 | case NoLinkage: |
| 6894 | case VisibleNoLinkage: return CXLinkage_NoLinkage; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6895 | case InternalLinkage: return CXLinkage_Internal; |
| 6896 | case UniqueExternalLinkage: return CXLinkage_UniqueExternal; |
| 6897 | case ExternalLinkage: return CXLinkage_External; |
| 6898 | }; |
| 6899 | |
| 6900 | return CXLinkage_Invalid; |
| 6901 | } |
| 6902 | } // end: extern "C" |
| 6903 | |
| 6904 | //===----------------------------------------------------------------------===// |
Ehsan Akhgari | b743de7 | 2016-05-31 15:55:51 +0000 | [diff] [blame] | 6905 | // Operations for querying visibility of a cursor. |
| 6906 | //===----------------------------------------------------------------------===// |
| 6907 | |
| 6908 | extern "C" { |
| 6909 | CXVisibilityKind clang_getCursorVisibility(CXCursor cursor) { |
| 6910 | if (!clang_isDeclaration(cursor.kind)) |
| 6911 | return CXVisibility_Invalid; |
| 6912 | |
| 6913 | const Decl *D = cxcursor::getCursorDecl(cursor); |
| 6914 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
| 6915 | switch (ND->getVisibility()) { |
| 6916 | case HiddenVisibility: return CXVisibility_Hidden; |
| 6917 | case ProtectedVisibility: return CXVisibility_Protected; |
| 6918 | case DefaultVisibility: return CXVisibility_Default; |
| 6919 | }; |
| 6920 | |
| 6921 | return CXVisibility_Invalid; |
| 6922 | } |
| 6923 | } // end: extern "C" |
| 6924 | |
| 6925 | //===----------------------------------------------------------------------===// |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6926 | // Operations for querying language of a cursor. |
| 6927 | //===----------------------------------------------------------------------===// |
| 6928 | |
| 6929 | static CXLanguageKind getDeclLanguage(const Decl *D) { |
| 6930 | if (!D) |
| 6931 | return CXLanguage_C; |
| 6932 | |
| 6933 | switch (D->getKind()) { |
| 6934 | default: |
| 6935 | break; |
| 6936 | case Decl::ImplicitParam: |
| 6937 | case Decl::ObjCAtDefsField: |
| 6938 | case Decl::ObjCCategory: |
| 6939 | case Decl::ObjCCategoryImpl: |
| 6940 | case Decl::ObjCCompatibleAlias: |
| 6941 | case Decl::ObjCImplementation: |
| 6942 | case Decl::ObjCInterface: |
| 6943 | case Decl::ObjCIvar: |
| 6944 | case Decl::ObjCMethod: |
| 6945 | case Decl::ObjCProperty: |
| 6946 | case Decl::ObjCPropertyImpl: |
| 6947 | case Decl::ObjCProtocol: |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 6948 | case Decl::ObjCTypeParam: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6949 | return CXLanguage_ObjC; |
| 6950 | case Decl::CXXConstructor: |
| 6951 | case Decl::CXXConversion: |
| 6952 | case Decl::CXXDestructor: |
| 6953 | case Decl::CXXMethod: |
| 6954 | case Decl::CXXRecord: |
| 6955 | case Decl::ClassTemplate: |
| 6956 | case Decl::ClassTemplatePartialSpecialization: |
| 6957 | case Decl::ClassTemplateSpecialization: |
| 6958 | case Decl::Friend: |
| 6959 | case Decl::FriendTemplate: |
| 6960 | case Decl::FunctionTemplate: |
| 6961 | case Decl::LinkageSpec: |
| 6962 | case Decl::Namespace: |
| 6963 | case Decl::NamespaceAlias: |
| 6964 | case Decl::NonTypeTemplateParm: |
| 6965 | case Decl::StaticAssert: |
| 6966 | case Decl::TemplateTemplateParm: |
| 6967 | case Decl::TemplateTypeParm: |
| 6968 | case Decl::UnresolvedUsingTypename: |
| 6969 | case Decl::UnresolvedUsingValue: |
| 6970 | case Decl::Using: |
| 6971 | case Decl::UsingDirective: |
| 6972 | case Decl::UsingShadow: |
| 6973 | return CXLanguage_CPlusPlus; |
| 6974 | } |
| 6975 | |
| 6976 | return CXLanguage_C; |
| 6977 | } |
| 6978 | |
| 6979 | extern "C" { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 6980 | |
| 6981 | static CXAvailabilityKind getCursorAvailabilityForDecl(const Decl *D) { |
| 6982 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()) |
Manuel Klimek | 8e3a7ed | 2015-09-25 17:53:16 +0000 | [diff] [blame] | 6983 | return CXAvailability_NotAvailable; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6984 | |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 6985 | switch (D->getAvailability()) { |
| 6986 | case AR_Available: |
| 6987 | case AR_NotYetIntroduced: |
| 6988 | if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(D)) |
Benjamin Kramer | 656363d | 2013-10-15 18:53:18 +0000 | [diff] [blame] | 6989 | return getCursorAvailabilityForDecl( |
| 6990 | cast<Decl>(EnumConst->getDeclContext())); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 6991 | return CXAvailability_Available; |
| 6992 | |
| 6993 | case AR_Deprecated: |
| 6994 | return CXAvailability_Deprecated; |
| 6995 | |
| 6996 | case AR_Unavailable: |
| 6997 | return CXAvailability_NotAvailable; |
| 6998 | } |
Benjamin Kramer | 656363d | 2013-10-15 18:53:18 +0000 | [diff] [blame] | 6999 | |
| 7000 | llvm_unreachable("Unknown availability kind!"); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7001 | } |
| 7002 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7003 | enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { |
| 7004 | if (clang_isDeclaration(cursor.kind)) |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7005 | if (const Decl *D = cxcursor::getCursorDecl(cursor)) |
| 7006 | return getCursorAvailabilityForDecl(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7007 | |
| 7008 | return CXAvailability_Available; |
| 7009 | } |
| 7010 | |
| 7011 | static CXVersion convertVersion(VersionTuple In) { |
| 7012 | CXVersion Out = { -1, -1, -1 }; |
| 7013 | if (In.empty()) |
| 7014 | return Out; |
| 7015 | |
| 7016 | Out.Major = In.getMajor(); |
| 7017 | |
NAKAMURA Takumi | c2b5d1f | 2013-02-21 02:32:34 +0000 | [diff] [blame] | 7018 | Optional<unsigned> Minor = In.getMinor(); |
| 7019 | if (Minor.hasValue()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7020 | Out.Minor = *Minor; |
| 7021 | else |
| 7022 | return Out; |
| 7023 | |
NAKAMURA Takumi | c2b5d1f | 2013-02-21 02:32:34 +0000 | [diff] [blame] | 7024 | Optional<unsigned> Subminor = In.getSubminor(); |
| 7025 | if (Subminor.hasValue()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7026 | Out.Subminor = *Subminor; |
| 7027 | |
| 7028 | return Out; |
| 7029 | } |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7030 | |
| 7031 | static int getCursorPlatformAvailabilityForDecl(const Decl *D, |
| 7032 | int *always_deprecated, |
| 7033 | CXString *deprecated_message, |
| 7034 | int *always_unavailable, |
| 7035 | CXString *unavailable_message, |
| 7036 | CXPlatformAvailability *availability, |
| 7037 | int availability_size) { |
| 7038 | bool HadAvailAttr = false; |
| 7039 | int N = 0; |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 7040 | for (auto A : D->attrs()) { |
| 7041 | if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(A)) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7042 | HadAvailAttr = true; |
| 7043 | if (always_deprecated) |
| 7044 | *always_deprecated = 1; |
Nico Weber | aacf031 | 2014-04-24 05:16:45 +0000 | [diff] [blame] | 7045 | if (deprecated_message) { |
Argyrios Kyrtzidis | edfe07f | 2014-04-24 06:05:40 +0000 | [diff] [blame] | 7046 | clang_disposeString(*deprecated_message); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7047 | *deprecated_message = cxstring::createDup(Deprecated->getMessage()); |
Nico Weber | aacf031 | 2014-04-24 05:16:45 +0000 | [diff] [blame] | 7048 | } |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7049 | continue; |
| 7050 | } |
| 7051 | |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 7052 | if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(A)) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7053 | HadAvailAttr = true; |
| 7054 | if (always_unavailable) |
| 7055 | *always_unavailable = 1; |
| 7056 | if (unavailable_message) { |
Argyrios Kyrtzidis | edfe07f | 2014-04-24 06:05:40 +0000 | [diff] [blame] | 7057 | clang_disposeString(*unavailable_message); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7058 | *unavailable_message = cxstring::createDup(Unavailable->getMessage()); |
| 7059 | } |
| 7060 | continue; |
| 7061 | } |
| 7062 | |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 7063 | if (AvailabilityAttr *Avail = dyn_cast<AvailabilityAttr>(A)) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7064 | HadAvailAttr = true; |
| 7065 | if (N < availability_size) { |
| 7066 | availability[N].Platform |
| 7067 | = cxstring::createDup(Avail->getPlatform()->getName()); |
| 7068 | availability[N].Introduced = convertVersion(Avail->getIntroduced()); |
| 7069 | availability[N].Deprecated = convertVersion(Avail->getDeprecated()); |
| 7070 | availability[N].Obsoleted = convertVersion(Avail->getObsoleted()); |
| 7071 | availability[N].Unavailable = Avail->getUnavailable(); |
| 7072 | availability[N].Message = cxstring::createDup(Avail->getMessage()); |
| 7073 | } |
| 7074 | ++N; |
| 7075 | } |
| 7076 | } |
| 7077 | |
| 7078 | if (!HadAvailAttr) |
| 7079 | if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(D)) |
| 7080 | return getCursorPlatformAvailabilityForDecl( |
| 7081 | cast<Decl>(EnumConst->getDeclContext()), |
| 7082 | always_deprecated, |
| 7083 | deprecated_message, |
| 7084 | always_unavailable, |
| 7085 | unavailable_message, |
| 7086 | availability, |
| 7087 | availability_size); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7088 | |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7089 | return N; |
| 7090 | } |
| 7091 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7092 | int clang_getCursorPlatformAvailability(CXCursor cursor, |
| 7093 | int *always_deprecated, |
| 7094 | CXString *deprecated_message, |
| 7095 | int *always_unavailable, |
| 7096 | CXString *unavailable_message, |
| 7097 | CXPlatformAvailability *availability, |
| 7098 | int availability_size) { |
| 7099 | if (always_deprecated) |
| 7100 | *always_deprecated = 0; |
| 7101 | if (deprecated_message) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 7102 | *deprecated_message = cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7103 | if (always_unavailable) |
| 7104 | *always_unavailable = 0; |
| 7105 | if (unavailable_message) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 7106 | *unavailable_message = cxstring::createEmpty(); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7107 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7108 | if (!clang_isDeclaration(cursor.kind)) |
| 7109 | return 0; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7110 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7111 | const Decl *D = cxcursor::getCursorDecl(cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7112 | if (!D) |
| 7113 | return 0; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7114 | |
| 7115 | return getCursorPlatformAvailabilityForDecl(D, always_deprecated, |
| 7116 | deprecated_message, |
| 7117 | always_unavailable, |
| 7118 | unavailable_message, |
| 7119 | availability, |
| 7120 | availability_size); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7121 | } |
| 7122 | |
| 7123 | void clang_disposeCXPlatformAvailability(CXPlatformAvailability *availability) { |
| 7124 | clang_disposeString(availability->Platform); |
| 7125 | clang_disposeString(availability->Message); |
| 7126 | } |
| 7127 | |
| 7128 | CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { |
| 7129 | if (clang_isDeclaration(cursor.kind)) |
| 7130 | return getDeclLanguage(cxcursor::getCursorDecl(cursor)); |
| 7131 | |
| 7132 | return CXLanguage_Invalid; |
| 7133 | } |
| 7134 | |
| 7135 | /// \brief If the given cursor is the "templated" declaration |
| 7136 | /// descibing a class or function template, return the class or |
| 7137 | /// function template. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7138 | static const Decl *maybeGetTemplateCursor(const Decl *D) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7139 | if (!D) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7140 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7141 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7142 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7143 | if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate()) |
| 7144 | return FunTmpl; |
| 7145 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7146 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7147 | if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate()) |
| 7148 | return ClassTmpl; |
| 7149 | |
| 7150 | return D; |
| 7151 | } |
| 7152 | |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 7153 | |
| 7154 | enum CX_StorageClass clang_Cursor_getStorageClass(CXCursor C) { |
| 7155 | StorageClass sc = SC_None; |
| 7156 | const Decl *D = getCursorDecl(C); |
| 7157 | if (D) { |
| 7158 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7159 | sc = FD->getStorageClass(); |
| 7160 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7161 | sc = VD->getStorageClass(); |
| 7162 | } else { |
| 7163 | return CX_SC_Invalid; |
| 7164 | } |
| 7165 | } else { |
| 7166 | return CX_SC_Invalid; |
| 7167 | } |
| 7168 | switch (sc) { |
| 7169 | case SC_None: |
| 7170 | return CX_SC_None; |
| 7171 | case SC_Extern: |
| 7172 | return CX_SC_Extern; |
| 7173 | case SC_Static: |
| 7174 | return CX_SC_Static; |
| 7175 | case SC_PrivateExtern: |
| 7176 | return CX_SC_PrivateExtern; |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 7177 | case SC_Auto: |
| 7178 | return CX_SC_Auto; |
| 7179 | case SC_Register: |
| 7180 | return CX_SC_Register; |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 7181 | } |
Kaelyn Takata | ab61e70 | 2014-10-15 18:03:26 +0000 | [diff] [blame] | 7182 | llvm_unreachable("Unhandled storage class!"); |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 7183 | } |
| 7184 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7185 | CXCursor clang_getCursorSemanticParent(CXCursor cursor) { |
| 7186 | if (clang_isDeclaration(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7187 | if (const Decl *D = getCursorDecl(cursor)) { |
| 7188 | const DeclContext *DC = D->getDeclContext(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7189 | if (!DC) |
| 7190 | return clang_getNullCursor(); |
| 7191 | |
| 7192 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 7193 | getCursorTU(cursor)); |
| 7194 | } |
| 7195 | } |
| 7196 | |
| 7197 | if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7198 | if (const Decl *D = getCursorDecl(cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7199 | return MakeCXCursor(D, getCursorTU(cursor)); |
| 7200 | } |
| 7201 | |
| 7202 | return clang_getNullCursor(); |
| 7203 | } |
| 7204 | |
| 7205 | CXCursor clang_getCursorLexicalParent(CXCursor cursor) { |
| 7206 | if (clang_isDeclaration(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7207 | if (const Decl *D = getCursorDecl(cursor)) { |
| 7208 | const DeclContext *DC = D->getLexicalDeclContext(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7209 | if (!DC) |
| 7210 | return clang_getNullCursor(); |
| 7211 | |
| 7212 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 7213 | getCursorTU(cursor)); |
| 7214 | } |
| 7215 | } |
| 7216 | |
| 7217 | // FIXME: Note that we can't easily compute the lexical context of a |
| 7218 | // statement or expression, so we return nothing. |
| 7219 | return clang_getNullCursor(); |
| 7220 | } |
| 7221 | |
| 7222 | CXFile clang_getIncludedFile(CXCursor cursor) { |
| 7223 | if (cursor.kind != CXCursor_InclusionDirective) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7224 | return nullptr; |
| 7225 | |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 7226 | const InclusionDirective *ID = getCursorInclusionDirective(cursor); |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 7227 | return const_cast<FileEntry *>(ID->getFile()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7228 | } |
| 7229 | |
Argyrios Kyrtzidis | 9adfd8a | 2013-04-18 22:15:49 +0000 | [diff] [blame] | 7230 | unsigned clang_Cursor_getObjCPropertyAttributes(CXCursor C, unsigned reserved) { |
| 7231 | if (C.kind != CXCursor_ObjCPropertyDecl) |
| 7232 | return CXObjCPropertyAttr_noattr; |
| 7233 | |
| 7234 | unsigned Result = CXObjCPropertyAttr_noattr; |
| 7235 | const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(getCursorDecl(C)); |
| 7236 | ObjCPropertyDecl::PropertyAttributeKind Attr = |
| 7237 | PD->getPropertyAttributesAsWritten(); |
| 7238 | |
| 7239 | #define SET_CXOBJCPROP_ATTR(A) \ |
| 7240 | if (Attr & ObjCPropertyDecl::OBJC_PR_##A) \ |
| 7241 | Result |= CXObjCPropertyAttr_##A |
| 7242 | SET_CXOBJCPROP_ATTR(readonly); |
| 7243 | SET_CXOBJCPROP_ATTR(getter); |
| 7244 | SET_CXOBJCPROP_ATTR(assign); |
| 7245 | SET_CXOBJCPROP_ATTR(readwrite); |
| 7246 | SET_CXOBJCPROP_ATTR(retain); |
| 7247 | SET_CXOBJCPROP_ATTR(copy); |
| 7248 | SET_CXOBJCPROP_ATTR(nonatomic); |
| 7249 | SET_CXOBJCPROP_ATTR(setter); |
| 7250 | SET_CXOBJCPROP_ATTR(atomic); |
| 7251 | SET_CXOBJCPROP_ATTR(weak); |
| 7252 | SET_CXOBJCPROP_ATTR(strong); |
| 7253 | SET_CXOBJCPROP_ATTR(unsafe_unretained); |
Manman Ren | 04fd4d8 | 2016-05-31 23:22:04 +0000 | [diff] [blame] | 7254 | SET_CXOBJCPROP_ATTR(class); |
Argyrios Kyrtzidis | 9adfd8a | 2013-04-18 22:15:49 +0000 | [diff] [blame] | 7255 | #undef SET_CXOBJCPROP_ATTR |
| 7256 | |
| 7257 | return Result; |
| 7258 | } |
| 7259 | |
Argyrios Kyrtzidis | 9d9bc01 | 2013-04-18 23:29:12 +0000 | [diff] [blame] | 7260 | unsigned clang_Cursor_getObjCDeclQualifiers(CXCursor C) { |
| 7261 | if (!clang_isDeclaration(C.kind)) |
| 7262 | return CXObjCDeclQualifier_None; |
| 7263 | |
| 7264 | Decl::ObjCDeclQualifier QT = Decl::OBJC_TQ_None; |
| 7265 | const Decl *D = getCursorDecl(C); |
| 7266 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 7267 | QT = MD->getObjCDeclQualifier(); |
| 7268 | else if (const ParmVarDecl *PD = dyn_cast<ParmVarDecl>(D)) |
| 7269 | QT = PD->getObjCDeclQualifier(); |
| 7270 | if (QT == Decl::OBJC_TQ_None) |
| 7271 | return CXObjCDeclQualifier_None; |
| 7272 | |
| 7273 | unsigned Result = CXObjCDeclQualifier_None; |
| 7274 | if (QT & Decl::OBJC_TQ_In) Result |= CXObjCDeclQualifier_In; |
| 7275 | if (QT & Decl::OBJC_TQ_Inout) Result |= CXObjCDeclQualifier_Inout; |
| 7276 | if (QT & Decl::OBJC_TQ_Out) Result |= CXObjCDeclQualifier_Out; |
| 7277 | if (QT & Decl::OBJC_TQ_Bycopy) Result |= CXObjCDeclQualifier_Bycopy; |
| 7278 | if (QT & Decl::OBJC_TQ_Byref) Result |= CXObjCDeclQualifier_Byref; |
| 7279 | if (QT & Decl::OBJC_TQ_Oneway) Result |= CXObjCDeclQualifier_Oneway; |
| 7280 | |
| 7281 | return Result; |
| 7282 | } |
| 7283 | |
Argyrios Kyrtzidis | 7b50fc5 | 2013-07-05 20:44:37 +0000 | [diff] [blame] | 7284 | unsigned clang_Cursor_isObjCOptional(CXCursor C) { |
| 7285 | if (!clang_isDeclaration(C.kind)) |
| 7286 | return 0; |
| 7287 | |
| 7288 | const Decl *D = getCursorDecl(C); |
| 7289 | if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 7290 | return PD->getPropertyImplementation() == ObjCPropertyDecl::Optional; |
| 7291 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 7292 | return MD->getImplementationControl() == ObjCMethodDecl::Optional; |
| 7293 | |
| 7294 | return 0; |
| 7295 | } |
| 7296 | |
Argyrios Kyrtzidis | 23814e4 | 2013-04-18 23:53:05 +0000 | [diff] [blame] | 7297 | unsigned clang_Cursor_isVariadic(CXCursor C) { |
| 7298 | if (!clang_isDeclaration(C.kind)) |
| 7299 | return 0; |
| 7300 | |
| 7301 | const Decl *D = getCursorDecl(C); |
| 7302 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 7303 | return FD->isVariadic(); |
| 7304 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 7305 | return MD->isVariadic(); |
| 7306 | |
| 7307 | return 0; |
| 7308 | } |
| 7309 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7310 | CXSourceRange clang_Cursor_getCommentRange(CXCursor C) { |
| 7311 | if (!clang_isDeclaration(C.kind)) |
| 7312 | return clang_getNullRange(); |
| 7313 | |
| 7314 | const Decl *D = getCursorDecl(C); |
| 7315 | ASTContext &Context = getCursorContext(C); |
| 7316 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 7317 | if (!RC) |
| 7318 | return clang_getNullRange(); |
| 7319 | |
| 7320 | return cxloc::translateSourceRange(Context, RC->getSourceRange()); |
| 7321 | } |
| 7322 | |
| 7323 | CXString clang_Cursor_getRawCommentText(CXCursor C) { |
| 7324 | if (!clang_isDeclaration(C.kind)) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 7325 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7326 | |
| 7327 | const Decl *D = getCursorDecl(C); |
| 7328 | ASTContext &Context = getCursorContext(C); |
| 7329 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 7330 | StringRef RawText = RC ? RC->getRawText(Context.getSourceManager()) : |
| 7331 | StringRef(); |
| 7332 | |
| 7333 | // Don't duplicate the string because RawText points directly into source |
| 7334 | // code. |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 7335 | return cxstring::createRef(RawText); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7336 | } |
| 7337 | |
| 7338 | CXString clang_Cursor_getBriefCommentText(CXCursor C) { |
| 7339 | if (!clang_isDeclaration(C.kind)) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 7340 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7341 | |
| 7342 | const Decl *D = getCursorDecl(C); |
| 7343 | const ASTContext &Context = getCursorContext(C); |
| 7344 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 7345 | |
| 7346 | if (RC) { |
| 7347 | StringRef BriefText = RC->getBriefText(Context); |
| 7348 | |
| 7349 | // Don't duplicate the string because RawComment ensures that this memory |
| 7350 | // will not go away. |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 7351 | return cxstring::createRef(BriefText); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7352 | } |
| 7353 | |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 7354 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7355 | } |
| 7356 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7357 | CXModule clang_Cursor_getModule(CXCursor C) { |
| 7358 | if (C.kind == CXCursor_ModuleImportDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7359 | if (const ImportDecl *ImportD = |
| 7360 | dyn_cast_or_null<ImportDecl>(getCursorDecl(C))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7361 | return ImportD->getImportedModule(); |
| 7362 | } |
| 7363 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7364 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7365 | } |
| 7366 | |
Argyrios Kyrtzidis | f6d49c3 | 2014-05-14 23:14:37 +0000 | [diff] [blame] | 7367 | CXModule clang_getModuleForFile(CXTranslationUnit TU, CXFile File) { |
| 7368 | if (isNotUsableTU(TU)) { |
| 7369 | LOG_BAD_TU(TU); |
| 7370 | return nullptr; |
| 7371 | } |
| 7372 | if (!File) |
| 7373 | return nullptr; |
| 7374 | FileEntry *FE = static_cast<FileEntry *>(File); |
| 7375 | |
| 7376 | ASTUnit &Unit = *cxtu::getASTUnit(TU); |
| 7377 | HeaderSearch &HS = Unit.getPreprocessor().getHeaderSearchInfo(); |
| 7378 | ModuleMap::KnownHeader Header = HS.findModuleForHeader(FE); |
| 7379 | |
Richard Smith | feb54b6 | 2014-10-23 02:01:19 +0000 | [diff] [blame] | 7380 | return Header.getModule(); |
Argyrios Kyrtzidis | f6d49c3 | 2014-05-14 23:14:37 +0000 | [diff] [blame] | 7381 | } |
| 7382 | |
Argyrios Kyrtzidis | 12fdb9e | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 7383 | CXFile clang_Module_getASTFile(CXModule CXMod) { |
| 7384 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7385 | return nullptr; |
Argyrios Kyrtzidis | 12fdb9e | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 7386 | Module *Mod = static_cast<Module*>(CXMod); |
| 7387 | return const_cast<FileEntry *>(Mod->getASTFile()); |
| 7388 | } |
| 7389 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7390 | CXModule clang_Module_getParent(CXModule CXMod) { |
| 7391 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7392 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7393 | Module *Mod = static_cast<Module*>(CXMod); |
| 7394 | return Mod->Parent; |
| 7395 | } |
| 7396 | |
| 7397 | CXString clang_Module_getName(CXModule CXMod) { |
| 7398 | if (!CXMod) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 7399 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7400 | Module *Mod = static_cast<Module*>(CXMod); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 7401 | return cxstring::createDup(Mod->Name); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7402 | } |
| 7403 | |
| 7404 | CXString clang_Module_getFullName(CXModule CXMod) { |
| 7405 | if (!CXMod) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 7406 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7407 | Module *Mod = static_cast<Module*>(CXMod); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 7408 | return cxstring::createDup(Mod->getFullModuleName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7409 | } |
| 7410 | |
Argyrios Kyrtzidis | 884337f | 2014-05-15 04:44:25 +0000 | [diff] [blame] | 7411 | int clang_Module_isSystem(CXModule CXMod) { |
| 7412 | if (!CXMod) |
| 7413 | return 0; |
| 7414 | Module *Mod = static_cast<Module*>(CXMod); |
| 7415 | return Mod->IsSystem; |
| 7416 | } |
| 7417 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7418 | unsigned clang_Module_getNumTopLevelHeaders(CXTranslationUnit TU, |
| 7419 | CXModule CXMod) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7420 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7421 | LOG_BAD_TU(TU); |
| 7422 | return 0; |
| 7423 | } |
| 7424 | if (!CXMod) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7425 | return 0; |
| 7426 | Module *Mod = static_cast<Module*>(CXMod); |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7427 | FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager(); |
| 7428 | ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr); |
| 7429 | return TopHeaders.size(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7430 | } |
| 7431 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7432 | CXFile clang_Module_getTopLevelHeader(CXTranslationUnit TU, |
| 7433 | CXModule CXMod, unsigned Index) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7434 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7435 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7436 | return nullptr; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7437 | } |
| 7438 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7439 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7440 | Module *Mod = static_cast<Module*>(CXMod); |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7441 | FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7442 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7443 | ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr); |
| 7444 | if (Index < TopHeaders.size()) |
| 7445 | return const_cast<FileEntry *>(TopHeaders[Index]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7446 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7447 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7448 | } |
| 7449 | |
| 7450 | } // end: extern "C" |
| 7451 | |
| 7452 | //===----------------------------------------------------------------------===// |
| 7453 | // C++ AST instrospection. |
| 7454 | //===----------------------------------------------------------------------===// |
| 7455 | |
| 7456 | extern "C" { |
Jonathan Coe | 2956535 | 2016-04-27 12:48:25 +0000 | [diff] [blame] | 7457 | |
| 7458 | unsigned clang_CXXConstructor_isDefaultConstructor(CXCursor C) { |
| 7459 | if (!clang_isDeclaration(C.kind)) |
| 7460 | return 0; |
| 7461 | |
| 7462 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7463 | const CXXConstructorDecl *Constructor = |
| 7464 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 7465 | return (Constructor && Constructor->isDefaultConstructor()) ? 1 : 0; |
| 7466 | } |
| 7467 | |
| 7468 | unsigned clang_CXXConstructor_isCopyConstructor(CXCursor C) { |
| 7469 | if (!clang_isDeclaration(C.kind)) |
| 7470 | return 0; |
| 7471 | |
| 7472 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7473 | const CXXConstructorDecl *Constructor = |
| 7474 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 7475 | return (Constructor && Constructor->isCopyConstructor()) ? 1 : 0; |
| 7476 | } |
| 7477 | |
| 7478 | unsigned clang_CXXConstructor_isMoveConstructor(CXCursor C) { |
| 7479 | if (!clang_isDeclaration(C.kind)) |
| 7480 | return 0; |
| 7481 | |
| 7482 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7483 | const CXXConstructorDecl *Constructor = |
| 7484 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 7485 | return (Constructor && Constructor->isMoveConstructor()) ? 1 : 0; |
| 7486 | } |
| 7487 | |
| 7488 | unsigned clang_CXXConstructor_isConvertingConstructor(CXCursor C) { |
| 7489 | if (!clang_isDeclaration(C.kind)) |
| 7490 | return 0; |
| 7491 | |
| 7492 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7493 | const CXXConstructorDecl *Constructor = |
| 7494 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 7495 | // Passing 'false' excludes constructors marked 'explicit'. |
| 7496 | return (Constructor && Constructor->isConvertingConstructor(false)) ? 1 : 0; |
| 7497 | } |
| 7498 | |
Saleem Abdulrasool | 6ea75db | 2015-10-27 15:50:22 +0000 | [diff] [blame] | 7499 | unsigned clang_CXXField_isMutable(CXCursor C) { |
| 7500 | if (!clang_isDeclaration(C.kind)) |
| 7501 | return 0; |
| 7502 | |
| 7503 | if (const auto D = cxcursor::getCursorDecl(C)) |
| 7504 | if (const auto FD = dyn_cast_or_null<FieldDecl>(D)) |
| 7505 | return FD->isMutable() ? 1 : 0; |
| 7506 | return 0; |
| 7507 | } |
| 7508 | |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 7509 | unsigned clang_CXXMethod_isPureVirtual(CXCursor C) { |
| 7510 | if (!clang_isDeclaration(C.kind)) |
| 7511 | return 0; |
| 7512 | |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 7513 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 7514 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7515 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 7516 | return (Method && Method->isVirtual() && Method->isPure()) ? 1 : 0; |
| 7517 | } |
| 7518 | |
Dmitri Gribenko | e570ede | 2014-04-07 14:59:13 +0000 | [diff] [blame] | 7519 | unsigned clang_CXXMethod_isConst(CXCursor C) { |
| 7520 | if (!clang_isDeclaration(C.kind)) |
| 7521 | return 0; |
| 7522 | |
| 7523 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7524 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7525 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Dmitri Gribenko | e570ede | 2014-04-07 14:59:13 +0000 | [diff] [blame] | 7526 | return (Method && (Method->getTypeQualifiers() & Qualifiers::Const)) ? 1 : 0; |
| 7527 | } |
| 7528 | |
Jonathan Coe | 2956535 | 2016-04-27 12:48:25 +0000 | [diff] [blame] | 7529 | unsigned clang_CXXMethod_isDefaulted(CXCursor C) { |
| 7530 | if (!clang_isDeclaration(C.kind)) |
| 7531 | return 0; |
| 7532 | |
| 7533 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7534 | const CXXMethodDecl *Method = |
| 7535 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
| 7536 | return (Method && Method->isDefaulted()) ? 1 : 0; |
| 7537 | } |
| 7538 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7539 | unsigned clang_CXXMethod_isStatic(CXCursor C) { |
| 7540 | if (!clang_isDeclaration(C.kind)) |
| 7541 | return 0; |
| 7542 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7543 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 7544 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7545 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7546 | return (Method && Method->isStatic()) ? 1 : 0; |
| 7547 | } |
| 7548 | |
| 7549 | unsigned clang_CXXMethod_isVirtual(CXCursor C) { |
| 7550 | if (!clang_isDeclaration(C.kind)) |
| 7551 | return 0; |
| 7552 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7553 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 7554 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7555 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7556 | return (Method && Method->isVirtual()) ? 1 : 0; |
| 7557 | } |
| 7558 | } // end: extern "C" |
| 7559 | |
| 7560 | //===----------------------------------------------------------------------===// |
| 7561 | // Attribute introspection. |
| 7562 | //===----------------------------------------------------------------------===// |
| 7563 | |
| 7564 | extern "C" { |
| 7565 | CXType clang_getIBOutletCollectionType(CXCursor C) { |
| 7566 | if (C.kind != CXCursor_IBOutletCollectionAttr) |
| 7567 | return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C)); |
| 7568 | |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 7569 | const IBOutletCollectionAttr *A = |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7570 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); |
| 7571 | |
| 7572 | return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C)); |
| 7573 | } |
| 7574 | } // end: extern "C" |
| 7575 | |
| 7576 | //===----------------------------------------------------------------------===// |
| 7577 | // Inspecting memory usage. |
| 7578 | //===----------------------------------------------------------------------===// |
| 7579 | |
| 7580 | typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries; |
| 7581 | |
| 7582 | static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries, |
| 7583 | enum CXTUResourceUsageKind k, |
| 7584 | unsigned long amount) { |
| 7585 | CXTUResourceUsageEntry entry = { k, amount }; |
| 7586 | entries.push_back(entry); |
| 7587 | } |
| 7588 | |
| 7589 | extern "C" { |
| 7590 | |
| 7591 | const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) { |
| 7592 | const char *str = ""; |
| 7593 | switch (kind) { |
| 7594 | case CXTUResourceUsage_AST: |
| 7595 | str = "ASTContext: expressions, declarations, and types"; |
| 7596 | break; |
| 7597 | case CXTUResourceUsage_Identifiers: |
| 7598 | str = "ASTContext: identifiers"; |
| 7599 | break; |
| 7600 | case CXTUResourceUsage_Selectors: |
| 7601 | str = "ASTContext: selectors"; |
| 7602 | break; |
| 7603 | case CXTUResourceUsage_GlobalCompletionResults: |
| 7604 | str = "Code completion: cached global results"; |
| 7605 | break; |
| 7606 | case CXTUResourceUsage_SourceManagerContentCache: |
| 7607 | str = "SourceManager: content cache allocator"; |
| 7608 | break; |
| 7609 | case CXTUResourceUsage_AST_SideTables: |
| 7610 | str = "ASTContext: side tables"; |
| 7611 | break; |
| 7612 | case CXTUResourceUsage_SourceManager_Membuffer_Malloc: |
| 7613 | str = "SourceManager: malloc'ed memory buffers"; |
| 7614 | break; |
| 7615 | case CXTUResourceUsage_SourceManager_Membuffer_MMap: |
| 7616 | str = "SourceManager: mmap'ed memory buffers"; |
| 7617 | break; |
| 7618 | case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc: |
| 7619 | str = "ExternalASTSource: malloc'ed memory buffers"; |
| 7620 | break; |
| 7621 | case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap: |
| 7622 | str = "ExternalASTSource: mmap'ed memory buffers"; |
| 7623 | break; |
| 7624 | case CXTUResourceUsage_Preprocessor: |
| 7625 | str = "Preprocessor: malloc'ed memory"; |
| 7626 | break; |
| 7627 | case CXTUResourceUsage_PreprocessingRecord: |
| 7628 | str = "Preprocessor: PreprocessingRecord"; |
| 7629 | break; |
| 7630 | case CXTUResourceUsage_SourceManager_DataStructures: |
| 7631 | str = "SourceManager: data structures and tables"; |
| 7632 | break; |
| 7633 | case CXTUResourceUsage_Preprocessor_HeaderSearch: |
| 7634 | str = "Preprocessor: header search tables"; |
| 7635 | break; |
| 7636 | } |
| 7637 | return str; |
| 7638 | } |
| 7639 | |
| 7640 | CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7641 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7642 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7643 | CXTUResourceUsage usage = { (void*) nullptr, 0, nullptr }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7644 | return usage; |
| 7645 | } |
| 7646 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7647 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 7648 | std::unique_ptr<MemUsageEntries> entries(new MemUsageEntries()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7649 | ASTContext &astContext = astUnit->getASTContext(); |
| 7650 | |
| 7651 | // How much memory is used by AST nodes and types? |
| 7652 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST, |
| 7653 | (unsigned long) astContext.getASTAllocatedMemory()); |
| 7654 | |
| 7655 | // How much memory is used by identifiers? |
| 7656 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers, |
| 7657 | (unsigned long) astContext.Idents.getAllocator().getTotalMemory()); |
| 7658 | |
| 7659 | // How much memory is used for selectors? |
| 7660 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors, |
| 7661 | (unsigned long) astContext.Selectors.getTotalMemory()); |
| 7662 | |
| 7663 | // How much memory is used by ASTContext's side tables? |
| 7664 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables, |
| 7665 | (unsigned long) astContext.getSideTableAllocatedMemory()); |
| 7666 | |
| 7667 | // How much memory is used for caching global code completion results? |
| 7668 | unsigned long completionBytes = 0; |
| 7669 | if (GlobalCodeCompletionAllocator *completionAllocator = |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 7670 | astUnit->getCachedCompletionAllocator().get()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7671 | completionBytes = completionAllocator->getTotalMemory(); |
| 7672 | } |
| 7673 | createCXTUResourceUsageEntry(*entries, |
| 7674 | CXTUResourceUsage_GlobalCompletionResults, |
| 7675 | completionBytes); |
| 7676 | |
| 7677 | // How much memory is being used by SourceManager's content cache? |
| 7678 | createCXTUResourceUsageEntry(*entries, |
| 7679 | CXTUResourceUsage_SourceManagerContentCache, |
| 7680 | (unsigned long) astContext.getSourceManager().getContentCacheSize()); |
| 7681 | |
| 7682 | // How much memory is being used by the MemoryBuffer's in SourceManager? |
| 7683 | const SourceManager::MemoryBufferSizes &srcBufs = |
| 7684 | astUnit->getSourceManager().getMemoryBufferSizes(); |
| 7685 | |
| 7686 | createCXTUResourceUsageEntry(*entries, |
| 7687 | CXTUResourceUsage_SourceManager_Membuffer_Malloc, |
| 7688 | (unsigned long) srcBufs.malloc_bytes); |
| 7689 | createCXTUResourceUsageEntry(*entries, |
| 7690 | CXTUResourceUsage_SourceManager_Membuffer_MMap, |
| 7691 | (unsigned long) srcBufs.mmap_bytes); |
| 7692 | createCXTUResourceUsageEntry(*entries, |
| 7693 | CXTUResourceUsage_SourceManager_DataStructures, |
| 7694 | (unsigned long) astContext.getSourceManager() |
| 7695 | .getDataStructureSizes()); |
| 7696 | |
| 7697 | // How much memory is being used by the ExternalASTSource? |
| 7698 | if (ExternalASTSource *esrc = astContext.getExternalSource()) { |
| 7699 | const ExternalASTSource::MemoryBufferSizes &sizes = |
| 7700 | esrc->getMemoryBufferSizes(); |
| 7701 | |
| 7702 | createCXTUResourceUsageEntry(*entries, |
| 7703 | CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc, |
| 7704 | (unsigned long) sizes.malloc_bytes); |
| 7705 | createCXTUResourceUsageEntry(*entries, |
| 7706 | CXTUResourceUsage_ExternalASTSource_Membuffer_MMap, |
| 7707 | (unsigned long) sizes.mmap_bytes); |
| 7708 | } |
| 7709 | |
| 7710 | // How much memory is being used by the Preprocessor? |
| 7711 | Preprocessor &pp = astUnit->getPreprocessor(); |
| 7712 | createCXTUResourceUsageEntry(*entries, |
| 7713 | CXTUResourceUsage_Preprocessor, |
| 7714 | pp.getTotalMemory()); |
| 7715 | |
| 7716 | if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) { |
| 7717 | createCXTUResourceUsageEntry(*entries, |
| 7718 | CXTUResourceUsage_PreprocessingRecord, |
| 7719 | pRec->getTotalMemory()); |
| 7720 | } |
| 7721 | |
| 7722 | createCXTUResourceUsageEntry(*entries, |
| 7723 | CXTUResourceUsage_Preprocessor_HeaderSearch, |
| 7724 | pp.getHeaderSearchInfo().getTotalMemory()); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7725 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7726 | CXTUResourceUsage usage = { (void*) entries.get(), |
| 7727 | (unsigned) entries->size(), |
Alexander Kornienko | 6ee521c | 2015-01-23 15:36:10 +0000 | [diff] [blame] | 7728 | !entries->empty() ? &(*entries)[0] : nullptr }; |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 7729 | entries.release(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7730 | return usage; |
| 7731 | } |
| 7732 | |
| 7733 | void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) { |
| 7734 | if (usage.data) |
| 7735 | delete (MemUsageEntries*) usage.data; |
| 7736 | } |
| 7737 | |
Argyrios Kyrtzidis | 0e282ef | 2013-12-06 18:55:45 +0000 | [diff] [blame] | 7738 | CXSourceRangeList *clang_getSkippedRanges(CXTranslationUnit TU, CXFile file) { |
| 7739 | CXSourceRangeList *skipped = new CXSourceRangeList; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 7740 | skipped->count = 0; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7741 | skipped->ranges = nullptr; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 7742 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7743 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7744 | LOG_BAD_TU(TU); |
| 7745 | return skipped; |
| 7746 | } |
| 7747 | |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 7748 | if (!file) |
| 7749 | return skipped; |
| 7750 | |
| 7751 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
| 7752 | PreprocessingRecord *ppRec = astUnit->getPreprocessor().getPreprocessingRecord(); |
| 7753 | if (!ppRec) |
| 7754 | return skipped; |
| 7755 | |
| 7756 | ASTContext &Ctx = astUnit->getASTContext(); |
| 7757 | SourceManager &sm = Ctx.getSourceManager(); |
| 7758 | FileEntry *fileEntry = static_cast<FileEntry *>(file); |
| 7759 | FileID wantedFileID = sm.translateFile(fileEntry); |
| 7760 | |
| 7761 | const std::vector<SourceRange> &SkippedRanges = ppRec->getSkippedRanges(); |
| 7762 | std::vector<SourceRange> wantedRanges; |
| 7763 | for (std::vector<SourceRange>::const_iterator i = SkippedRanges.begin(), ei = SkippedRanges.end(); |
| 7764 | i != ei; ++i) { |
| 7765 | if (sm.getFileID(i->getBegin()) == wantedFileID || sm.getFileID(i->getEnd()) == wantedFileID) |
| 7766 | wantedRanges.push_back(*i); |
| 7767 | } |
| 7768 | |
| 7769 | skipped->count = wantedRanges.size(); |
| 7770 | skipped->ranges = new CXSourceRange[skipped->count]; |
| 7771 | for (unsigned i = 0, ei = skipped->count; i != ei; ++i) |
| 7772 | skipped->ranges[i] = cxloc::translateSourceRange(Ctx, wantedRanges[i]); |
| 7773 | |
| 7774 | return skipped; |
| 7775 | } |
| 7776 | |
Cameron Desrochers | d809128 | 2016-08-18 15:43:55 +0000 | [diff] [blame] | 7777 | CXSourceRangeList *clang_getAllSkippedRanges(CXTranslationUnit TU) { |
| 7778 | CXSourceRangeList *skipped = new CXSourceRangeList; |
| 7779 | skipped->count = 0; |
| 7780 | skipped->ranges = nullptr; |
| 7781 | |
| 7782 | if (isNotUsableTU(TU)) { |
| 7783 | LOG_BAD_TU(TU); |
| 7784 | return skipped; |
| 7785 | } |
| 7786 | |
| 7787 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
| 7788 | PreprocessingRecord *ppRec = astUnit->getPreprocessor().getPreprocessingRecord(); |
| 7789 | if (!ppRec) |
| 7790 | return skipped; |
| 7791 | |
| 7792 | ASTContext &Ctx = astUnit->getASTContext(); |
| 7793 | |
| 7794 | const std::vector<SourceRange> &SkippedRanges = ppRec->getSkippedRanges(); |
| 7795 | |
| 7796 | skipped->count = SkippedRanges.size(); |
| 7797 | skipped->ranges = new CXSourceRange[skipped->count]; |
| 7798 | for (unsigned i = 0, ei = skipped->count; i != ei; ++i) |
| 7799 | skipped->ranges[i] = cxloc::translateSourceRange(Ctx, SkippedRanges[i]); |
| 7800 | |
| 7801 | return skipped; |
| 7802 | } |
| 7803 | |
Argyrios Kyrtzidis | 0e282ef | 2013-12-06 18:55:45 +0000 | [diff] [blame] | 7804 | void clang_disposeSourceRangeList(CXSourceRangeList *ranges) { |
| 7805 | if (ranges) { |
| 7806 | delete[] ranges->ranges; |
| 7807 | delete ranges; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 7808 | } |
| 7809 | } |
| 7810 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7811 | } // end extern "C" |
| 7812 | |
| 7813 | void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) { |
| 7814 | CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU); |
| 7815 | for (unsigned I = 0; I != Usage.numEntries; ++I) |
| 7816 | fprintf(stderr, " %s: %lu\n", |
| 7817 | clang_getTUResourceUsageName(Usage.entries[I].kind), |
| 7818 | Usage.entries[I].amount); |
| 7819 | |
| 7820 | clang_disposeCXTUResourceUsage(Usage); |
| 7821 | } |
| 7822 | |
| 7823 | //===----------------------------------------------------------------------===// |
| 7824 | // Misc. utility functions. |
| 7825 | //===----------------------------------------------------------------------===// |
| 7826 | |
| 7827 | /// Default to using an 8 MB stack size on "safety" threads. |
| 7828 | static unsigned SafetyStackThreadSize = 8 << 20; |
| 7829 | |
| 7830 | namespace clang { |
| 7831 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 7832 | bool RunSafely(llvm::CrashRecoveryContext &CRC, llvm::function_ref<void()> Fn, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7833 | unsigned Size) { |
| 7834 | if (!Size) |
| 7835 | Size = GetSafetyThreadStackSize(); |
| 7836 | if (Size) |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 7837 | return CRC.RunSafelyOnThread(Fn, Size); |
| 7838 | return CRC.RunSafely(Fn); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7839 | } |
| 7840 | |
| 7841 | unsigned GetSafetyThreadStackSize() { |
| 7842 | return SafetyStackThreadSize; |
| 7843 | } |
| 7844 | |
| 7845 | void SetSafetyThreadStackSize(unsigned Value) { |
| 7846 | SafetyStackThreadSize = Value; |
| 7847 | } |
| 7848 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7849 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7850 | |
| 7851 | void clang::setThreadBackgroundPriority() { |
| 7852 | if (getenv("LIBCLANG_BGPRIO_DISABLE")) |
| 7853 | return; |
| 7854 | |
Alp Toker | 1a86ad2 | 2014-07-06 06:24:00 +0000 | [diff] [blame] | 7855 | #ifdef USE_DARWIN_THREADS |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7856 | setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG); |
| 7857 | #endif |
| 7858 | } |
| 7859 | |
| 7860 | void cxindex::printDiagsToStderr(ASTUnit *Unit) { |
| 7861 | if (!Unit) |
| 7862 | return; |
| 7863 | |
| 7864 | for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), |
| 7865 | DEnd = Unit->stored_diag_end(); |
| 7866 | D != DEnd; ++D) { |
Ben Langmuir | 749323f | 2014-04-22 17:40:12 +0000 | [diff] [blame] | 7867 | CXStoredDiagnostic Diag(*D, Unit->getLangOpts()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7868 | CXString Msg = clang_formatDiagnostic(&Diag, |
| 7869 | clang_defaultDiagnosticDisplayOptions()); |
| 7870 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 7871 | clang_disposeString(Msg); |
| 7872 | } |
| 7873 | #ifdef LLVM_ON_WIN32 |
| 7874 | // On Windows, force a flush, since there may be multiple copies of |
| 7875 | // stderr and stdout in the file system, all with different buffers |
| 7876 | // but writing to the same device. |
| 7877 | fflush(stderr); |
| 7878 | #endif |
| 7879 | } |
| 7880 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7881 | MacroInfo *cxindex::getMacroInfo(const IdentifierInfo &II, |
| 7882 | SourceLocation MacroDefLoc, |
| 7883 | CXTranslationUnit TU){ |
| 7884 | if (MacroDefLoc.isInvalid() || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7885 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7886 | if (!II.hadMacroDefinition()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7887 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7888 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7889 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7890 | Preprocessor &PP = Unit->getPreprocessor(); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 7891 | MacroDirective *MD = PP.getLocalMacroDirectiveHistory(&II); |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 7892 | if (MD) { |
| 7893 | for (MacroDirective::DefInfo |
| 7894 | Def = MD->getDefinition(); Def; Def = Def.getPreviousDefinition()) { |
| 7895 | if (MacroDefLoc == Def.getMacroInfo()->getDefinitionLoc()) |
| 7896 | return Def.getMacroInfo(); |
| 7897 | } |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7898 | } |
| 7899 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7900 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7901 | } |
| 7902 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 7903 | const MacroInfo *cxindex::getMacroInfo(const MacroDefinitionRecord *MacroDef, |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 7904 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7905 | if (!MacroDef || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7906 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7907 | const IdentifierInfo *II = MacroDef->getName(); |
| 7908 | if (!II) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7909 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7910 | |
| 7911 | return getMacroInfo(*II, MacroDef->getLocation(), TU); |
| 7912 | } |
| 7913 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 7914 | MacroDefinitionRecord * |
| 7915 | cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, const Token &Tok, |
| 7916 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7917 | if (!MI || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7918 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7919 | if (Tok.isNot(tok::raw_identifier)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7920 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7921 | |
| 7922 | if (MI->getNumTokens() == 0) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7923 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7924 | SourceRange DefRange(MI->getReplacementToken(0).getLocation(), |
| 7925 | MI->getDefinitionEndLoc()); |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7926 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7927 | |
| 7928 | // Check that the token is inside the definition and not its argument list. |
| 7929 | SourceManager &SM = Unit->getSourceManager(); |
| 7930 | if (SM.isBeforeInTranslationUnit(Tok.getLocation(), DefRange.getBegin())) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7931 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7932 | if (SM.isBeforeInTranslationUnit(DefRange.getEnd(), Tok.getLocation())) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7933 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7934 | |
| 7935 | Preprocessor &PP = Unit->getPreprocessor(); |
| 7936 | PreprocessingRecord *PPRec = PP.getPreprocessingRecord(); |
| 7937 | if (!PPRec) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7938 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7939 | |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 7940 | IdentifierInfo &II = PP.getIdentifierTable().get(Tok.getRawIdentifier()); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7941 | if (!II.hadMacroDefinition()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7942 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7943 | |
| 7944 | // Check that the identifier is not one of the macro arguments. |
| 7945 | if (std::find(MI->arg_begin(), MI->arg_end(), &II) != MI->arg_end()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7946 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7947 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 7948 | MacroDirective *InnerMD = PP.getLocalMacroDirectiveHistory(&II); |
Argyrios Kyrtzidis | 09c9e81 | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 7949 | if (!InnerMD) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7950 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7951 | |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 7952 | return PPRec->findMacroDefinition(InnerMD->getMacroInfo()); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7953 | } |
| 7954 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 7955 | MacroDefinitionRecord * |
| 7956 | cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, SourceLocation Loc, |
| 7957 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7958 | if (Loc.isInvalid() || !MI || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7959 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7960 | |
| 7961 | if (MI->getNumTokens() == 0) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7962 | return nullptr; |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7963 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7964 | Preprocessor &PP = Unit->getPreprocessor(); |
| 7965 | if (!PP.getPreprocessingRecord()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7966 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7967 | Loc = Unit->getSourceManager().getSpellingLoc(Loc); |
| 7968 | Token Tok; |
| 7969 | if (PP.getRawToken(Loc, Tok)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7970 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7971 | |
| 7972 | return checkForMacroInMacroDefinition(MI, Tok, TU); |
| 7973 | } |
| 7974 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7975 | extern "C" { |
| 7976 | |
| 7977 | CXString clang_getClangVersion() { |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 7978 | return cxstring::createDup(getClangFullVersion()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7979 | } |
| 7980 | |
| 7981 | } // end: extern "C" |
| 7982 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7983 | Logger &cxindex::Logger::operator<<(CXTranslationUnit TU) { |
| 7984 | if (TU) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7985 | if (ASTUnit *Unit = cxtu::getASTUnit(TU)) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7986 | LogOS << '<' << Unit->getMainFileName() << '>'; |
Argyrios Kyrtzidis | 37f2ab4 | 2013-03-05 20:21:14 +0000 | [diff] [blame] | 7987 | if (Unit->isMainFileAST()) |
| 7988 | LogOS << " (" << Unit->getASTFileName() << ')'; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7989 | return *this; |
| 7990 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 7991 | } else { |
| 7992 | LogOS << "<NULL TU>"; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7993 | } |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7994 | return *this; |
| 7995 | } |
| 7996 | |
Argyrios Kyrtzidis | ba4b5f8 | 2013-03-08 02:32:26 +0000 | [diff] [blame] | 7997 | Logger &cxindex::Logger::operator<<(const FileEntry *FE) { |
| 7998 | *this << FE->getName(); |
| 7999 | return *this; |
| 8000 | } |
| 8001 | |
| 8002 | Logger &cxindex::Logger::operator<<(CXCursor cursor) { |
| 8003 | CXString cursorName = clang_getCursorDisplayName(cursor); |
| 8004 | *this << cursorName << "@" << clang_getCursorLocation(cursor); |
| 8005 | clang_disposeString(cursorName); |
| 8006 | return *this; |
| 8007 | } |
| 8008 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8009 | Logger &cxindex::Logger::operator<<(CXSourceLocation Loc) { |
| 8010 | CXFile File; |
| 8011 | unsigned Line, Column; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8012 | clang_getFileLocation(Loc, &File, &Line, &Column, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8013 | CXString FileName = clang_getFileName(File); |
| 8014 | *this << llvm::format("(%s:%d:%d)", clang_getCString(FileName), Line, Column); |
| 8015 | clang_disposeString(FileName); |
| 8016 | return *this; |
| 8017 | } |
| 8018 | |
| 8019 | Logger &cxindex::Logger::operator<<(CXSourceRange range) { |
| 8020 | CXSourceLocation BLoc = clang_getRangeStart(range); |
| 8021 | CXSourceLocation ELoc = clang_getRangeEnd(range); |
| 8022 | |
| 8023 | CXFile BFile; |
| 8024 | unsigned BLine, BColumn; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8025 | clang_getFileLocation(BLoc, &BFile, &BLine, &BColumn, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8026 | |
| 8027 | CXFile EFile; |
| 8028 | unsigned ELine, EColumn; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 8029 | clang_getFileLocation(ELoc, &EFile, &ELine, &EColumn, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8030 | |
| 8031 | CXString BFileName = clang_getFileName(BFile); |
| 8032 | if (BFile == EFile) { |
| 8033 | *this << llvm::format("[%s %d:%d-%d:%d]", clang_getCString(BFileName), |
| 8034 | BLine, BColumn, ELine, EColumn); |
| 8035 | } else { |
| 8036 | CXString EFileName = clang_getFileName(EFile); |
| 8037 | *this << llvm::format("[%s:%d:%d - ", clang_getCString(BFileName), |
| 8038 | BLine, BColumn) |
| 8039 | << llvm::format("%s:%d:%d]", clang_getCString(EFileName), |
| 8040 | ELine, EColumn); |
| 8041 | clang_disposeString(EFileName); |
| 8042 | } |
| 8043 | clang_disposeString(BFileName); |
| 8044 | return *this; |
| 8045 | } |
| 8046 | |
| 8047 | Logger &cxindex::Logger::operator<<(CXString Str) { |
| 8048 | *this << clang_getCString(Str); |
| 8049 | return *this; |
| 8050 | } |
| 8051 | |
| 8052 | Logger &cxindex::Logger::operator<<(const llvm::format_object_base &Fmt) { |
| 8053 | LogOS << Fmt; |
| 8054 | return *this; |
| 8055 | } |
| 8056 | |
Chandler Carruth | 37ad258 | 2014-06-27 15:14:39 +0000 | [diff] [blame] | 8057 | static llvm::ManagedStatic<llvm::sys::Mutex> LoggingMutex; |
| 8058 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8059 | cxindex::Logger::~Logger() { |
Chandler Carruth | 37ad258 | 2014-06-27 15:14:39 +0000 | [diff] [blame] | 8060 | llvm::sys::ScopedLock L(*LoggingMutex); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8061 | |
| 8062 | static llvm::TimeRecord sBeginTR = llvm::TimeRecord::getCurrentTime(); |
| 8063 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8064 | raw_ostream &OS = llvm::errs(); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8065 | OS << "[libclang:" << Name << ':'; |
| 8066 | |
Alp Toker | 1a86ad2 | 2014-07-06 06:24:00 +0000 | [diff] [blame] | 8067 | #ifdef USE_DARWIN_THREADS |
| 8068 | // TODO: Portability. |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8069 | mach_port_t tid = pthread_mach_thread_np(pthread_self()); |
| 8070 | OS << tid << ':'; |
| 8071 | #endif |
| 8072 | |
| 8073 | llvm::TimeRecord TR = llvm::TimeRecord::getCurrentTime(); |
| 8074 | OS << llvm::format("%7.4f] ", TR.getWallTime() - sBeginTR.getWallTime()); |
Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 8075 | OS << Msg << '\n'; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8076 | |
| 8077 | if (Trace) { |
Zachary Turner | 1fe2a8d | 2015-03-05 19:15:09 +0000 | [diff] [blame] | 8078 | llvm::sys::PrintStackTrace(OS); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 8079 | OS << "--------------------------------------------------\n"; |
| 8080 | } |
| 8081 | } |
Benjamin Kramer | c1ffdab | 2016-03-03 08:58:18 +0000 | [diff] [blame] | 8082 | |
| 8083 | #ifdef CLANG_TOOL_EXTRA_BUILD |
| 8084 | // This anchor is used to force the linker to link the clang-tidy plugin. |
| 8085 | extern volatile int ClangTidyPluginAnchorSource; |
| 8086 | static int LLVM_ATTRIBUTE_UNUSED ClangTidyPluginAnchorDestination = |
| 8087 | ClangTidyPluginAnchorSource; |
| 8088 | #endif |