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 | |
| 15 | #include "CIndexer.h" |
| 16 | #include "CIndexDiagnostic.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) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 526 | if (Visit(MakeCXCursor(*TL, TU, RegionOfInterest), true)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 527 | return true; |
| 528 | } |
| 529 | } else if (VisitDeclContext( |
| 530 | CXXUnit->getASTContext().getTranslationUnitDecl())) |
| 531 | return true; |
| 532 | continue; |
| 533 | } |
| 534 | |
| 535 | // Walk the preprocessing record. |
| 536 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) |
| 537 | visitPreprocessedEntitiesInRegion(); |
| 538 | } |
| 539 | |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | if (Cursor.kind == CXCursor_CXXBaseSpecifier) { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 544 | if (const CXXBaseSpecifier *Base = getCursorCXXBaseSpecifier(Cursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 545 | if (TypeSourceInfo *BaseTSInfo = Base->getTypeSourceInfo()) { |
| 546 | return Visit(BaseTSInfo->getTypeLoc()); |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 552 | const IBOutletCollectionAttr *A = |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 553 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(Cursor)); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 554 | if (const ObjCObjectType *ObjT = A->getInterface()->getAs<ObjCObjectType>()) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 555 | return Visit(cxcursor::MakeCursorObjCClassRef( |
| 556 | ObjT->getInterface(), |
| 557 | A->getInterfaceLoc()->getTypeLoc().getLocStart(), TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 560 | // If pointing inside a macro definition, check if the token is an identifier |
| 561 | // that was ever defined as a macro. In such a case, create a "pseudo" macro |
| 562 | // expansion cursor for that token. |
| 563 | SourceLocation BeginLoc = RegionOfInterest.getBegin(); |
| 564 | if (Cursor.kind == CXCursor_MacroDefinition && |
| 565 | BeginLoc == RegionOfInterest.getEnd()) { |
| 566 | SourceLocation Loc = AU->mapLocationToPreamble(BeginLoc); |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 567 | const MacroInfo *MI = |
| 568 | getMacroInfo(cxcursor::getCursorMacroDefinition(Cursor), TU); |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 569 | if (MacroDefinitionRecord *MacroDef = |
| 570 | checkForMacroInMacroDefinition(MI, Loc, TU)) |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 571 | return Visit(cxcursor::MakeMacroExpansionCursor(MacroDef, BeginLoc, TU)); |
| 572 | } |
| 573 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 574 | // Nothing to visit at the moment. |
| 575 | return false; |
| 576 | } |
| 577 | |
| 578 | bool CursorVisitor::VisitBlockDecl(BlockDecl *B) { |
| 579 | if (TypeSourceInfo *TSInfo = B->getSignatureAsWritten()) |
| 580 | if (Visit(TSInfo->getTypeLoc())) |
| 581 | return true; |
| 582 | |
| 583 | if (Stmt *Body = B->getBody()) |
| 584 | return Visit(MakeCXCursor(Body, StmtParent, TU, RegionOfInterest)); |
| 585 | |
| 586 | return false; |
| 587 | } |
| 588 | |
Ted Kremenek | 0332558 | 2013-02-21 01:29:01 +0000 | [diff] [blame] | 589 | Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 590 | if (RegionOfInterest.isValid()) { |
| 591 | SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager()); |
| 592 | if (Range.isInvalid()) |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 593 | return None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 594 | |
| 595 | switch (CompareRegionOfInterest(Range)) { |
| 596 | case RangeBefore: |
| 597 | // This declaration comes before the region of interest; skip it. |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 598 | return None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 599 | |
| 600 | case RangeAfter: |
| 601 | // This declaration comes after the region of interest; we're done. |
| 602 | return false; |
| 603 | |
| 604 | case RangeOverlap: |
| 605 | // This declaration overlaps the region of interest; visit it. |
| 606 | break; |
| 607 | } |
| 608 | } |
| 609 | return true; |
| 610 | } |
| 611 | |
| 612 | bool CursorVisitor::VisitDeclContext(DeclContext *DC) { |
| 613 | DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 614 | |
| 615 | // FIXME: Eventually remove. This part of a hack to support proper |
| 616 | // iteration over all Decls contained lexically within an ObjC container. |
| 617 | SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I); |
| 618 | SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E); |
| 619 | |
| 620 | for ( ; I != E; ++I) { |
| 621 | Decl *D = *I; |
| 622 | if (D->getLexicalDeclContext() != DC) |
| 623 | continue; |
| 624 | CXCursor Cursor = MakeCXCursor(D, TU, RegionOfInterest); |
| 625 | |
| 626 | // Ignore synthesized ivars here, otherwise if we have something like: |
| 627 | // @synthesize prop = _prop; |
| 628 | // and '_prop' is not declared, we will encounter a '_prop' ivar before |
| 629 | // encountering the 'prop' synthesize declaration and we will think that |
| 630 | // we passed the region-of-interest. |
| 631 | if (ObjCIvarDecl *ivarD = dyn_cast<ObjCIvarDecl>(D)) { |
| 632 | if (ivarD->getSynthesize()) |
| 633 | continue; |
| 634 | } |
| 635 | |
| 636 | // FIXME: ObjCClassRef/ObjCProtocolRef for forward class/protocol |
| 637 | // declarations is a mismatch with the compiler semantics. |
| 638 | if (Cursor.kind == CXCursor_ObjCInterfaceDecl) { |
| 639 | ObjCInterfaceDecl *ID = cast<ObjCInterfaceDecl>(D); |
| 640 | if (!ID->isThisDeclarationADefinition()) |
| 641 | Cursor = MakeCursorObjCClassRef(ID, ID->getLocation(), TU); |
| 642 | |
| 643 | } else if (Cursor.kind == CXCursor_ObjCProtocolDecl) { |
| 644 | ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D); |
| 645 | if (!PD->isThisDeclarationADefinition()) |
| 646 | Cursor = MakeCursorObjCProtocolRef(PD, PD->getLocation(), TU); |
| 647 | } |
| 648 | |
Ted Kremenek | 0332558 | 2013-02-21 01:29:01 +0000 | [diff] [blame] | 649 | const Optional<bool> &V = shouldVisitCursor(Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 650 | if (!V.hasValue()) |
| 651 | continue; |
| 652 | if (!V.getValue()) |
| 653 | return false; |
| 654 | if (Visit(Cursor, true)) |
| 655 | return true; |
| 656 | } |
| 657 | return false; |
| 658 | } |
| 659 | |
| 660 | bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 661 | llvm_unreachable("Translation units are visited directly by Visit()"); |
| 662 | } |
| 663 | |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 664 | bool CursorVisitor::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| 665 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 666 | return true; |
| 667 | |
| 668 | return Visit(MakeCXCursor(D->getTemplatedDecl(), TU, RegionOfInterest)); |
| 669 | } |
| 670 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 671 | bool CursorVisitor::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 672 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 673 | return Visit(TSInfo->getTypeLoc()); |
| 674 | |
| 675 | return false; |
| 676 | } |
| 677 | |
| 678 | bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) { |
| 679 | if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo()) |
| 680 | return Visit(TSInfo->getTypeLoc()); |
| 681 | |
| 682 | return false; |
| 683 | } |
| 684 | |
| 685 | bool CursorVisitor::VisitTagDecl(TagDecl *D) { |
| 686 | return VisitDeclContext(D); |
| 687 | } |
| 688 | |
| 689 | bool CursorVisitor::VisitClassTemplateSpecializationDecl( |
| 690 | ClassTemplateSpecializationDecl *D) { |
| 691 | bool ShouldVisitBody = false; |
| 692 | switch (D->getSpecializationKind()) { |
| 693 | case TSK_Undeclared: |
| 694 | case TSK_ImplicitInstantiation: |
| 695 | // Nothing to visit |
| 696 | return false; |
| 697 | |
| 698 | case TSK_ExplicitInstantiationDeclaration: |
| 699 | case TSK_ExplicitInstantiationDefinition: |
| 700 | break; |
| 701 | |
| 702 | case TSK_ExplicitSpecialization: |
| 703 | ShouldVisitBody = true; |
| 704 | break; |
| 705 | } |
| 706 | |
| 707 | // Visit the template arguments used in the specialization. |
| 708 | if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { |
| 709 | TypeLoc TL = SpecType->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 710 | if (TemplateSpecializationTypeLoc TSTLoc = |
| 711 | TL.getAs<TemplateSpecializationTypeLoc>()) { |
| 712 | for (unsigned I = 0, N = TSTLoc.getNumArgs(); I != N; ++I) |
| 713 | if (VisitTemplateArgumentLoc(TSTLoc.getArgLoc(I))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 714 | return true; |
| 715 | } |
| 716 | } |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 717 | |
| 718 | return ShouldVisitBody && VisitCXXRecordDecl(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | bool CursorVisitor::VisitClassTemplatePartialSpecializationDecl( |
| 722 | ClassTemplatePartialSpecializationDecl *D) { |
| 723 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 724 | // before visiting these template parameters. |
| 725 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 726 | return true; |
| 727 | |
| 728 | // Visit the partial specialization arguments. |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 729 | const ASTTemplateArgumentListInfo *Info = D->getTemplateArgsAsWritten(); |
| 730 | const TemplateArgumentLoc *TemplateArgs = Info->getTemplateArgs(); |
| 731 | for (unsigned I = 0, N = Info->NumTemplateArgs; I != N; ++I) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 732 | if (VisitTemplateArgumentLoc(TemplateArgs[I])) |
| 733 | return true; |
| 734 | |
| 735 | return VisitCXXRecordDecl(D); |
| 736 | } |
| 737 | |
| 738 | bool CursorVisitor::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
| 739 | // Visit the default argument. |
| 740 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 741 | if (TypeSourceInfo *DefArg = D->getDefaultArgumentInfo()) |
| 742 | if (Visit(DefArg->getTypeLoc())) |
| 743 | return true; |
| 744 | |
| 745 | return false; |
| 746 | } |
| 747 | |
| 748 | bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 749 | if (Expr *Init = D->getInitExpr()) |
| 750 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
| 751 | return false; |
| 752 | } |
| 753 | |
| 754 | bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) { |
Argyrios Kyrtzidis | 2ec7674 | 2013-04-05 21:04:10 +0000 | [diff] [blame] | 755 | unsigned NumParamList = DD->getNumTemplateParameterLists(); |
| 756 | for (unsigned i = 0; i < NumParamList; i++) { |
| 757 | TemplateParameterList* Params = DD->getTemplateParameterList(i); |
| 758 | if (VisitTemplateParameters(Params)) |
| 759 | return true; |
| 760 | } |
| 761 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 762 | if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo()) |
| 763 | if (Visit(TSInfo->getTypeLoc())) |
| 764 | return true; |
| 765 | |
| 766 | // Visit the nested-name-specifier, if present. |
| 767 | if (NestedNameSpecifierLoc QualifierLoc = DD->getQualifierLoc()) |
| 768 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 769 | return true; |
| 770 | |
| 771 | return false; |
| 772 | } |
| 773 | |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 774 | /// \brief Compare two base or member initializers based on their source order. |
| 775 | static int CompareCXXCtorInitializers(CXXCtorInitializer *const *X, |
| 776 | CXXCtorInitializer *const *Y) { |
| 777 | return (*X)->getSourceOrder() - (*Y)->getSourceOrder(); |
| 778 | } |
| 779 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 780 | bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { |
Argyrios Kyrtzidis | 2ec7674 | 2013-04-05 21:04:10 +0000 | [diff] [blame] | 781 | unsigned NumParamList = ND->getNumTemplateParameterLists(); |
| 782 | for (unsigned i = 0; i < NumParamList; i++) { |
| 783 | TemplateParameterList* Params = ND->getTemplateParameterList(i); |
| 784 | if (VisitTemplateParameters(Params)) |
| 785 | return true; |
| 786 | } |
| 787 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 788 | if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) { |
| 789 | // Visit the function declaration's syntactic components in the order |
| 790 | // written. This requires a bit of work. |
| 791 | TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 792 | FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 793 | |
| 794 | // If we have a function declared directly (without the use of a typedef), |
| 795 | // visit just the return type. Otherwise, just visit the function's type |
| 796 | // now. |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 797 | if ((FTL && !isa<CXXConversionDecl>(ND) && Visit(FTL.getReturnLoc())) || |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 798 | (!FTL && Visit(TL))) |
| 799 | return true; |
| 800 | |
| 801 | // Visit the nested-name-specifier, if present. |
| 802 | if (NestedNameSpecifierLoc QualifierLoc = ND->getQualifierLoc()) |
| 803 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 804 | return true; |
| 805 | |
| 806 | // Visit the declaration name. |
Argyrios Kyrtzidis | 4a4d2b4 | 2014-02-09 08:13:47 +0000 | [diff] [blame] | 807 | if (!isa<CXXDestructorDecl>(ND)) |
| 808 | if (VisitDeclarationNameInfo(ND->getNameInfo())) |
| 809 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 810 | |
| 811 | // FIXME: Visit explicitly-specified template arguments! |
| 812 | |
| 813 | // Visit the function parameters, if we have a function type. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 814 | if (FTL && VisitFunctionTypeLoc(FTL, true)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 815 | return true; |
| 816 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 817 | // FIXME: Attributes? |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) { |
| 821 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) { |
| 822 | // Find the initializers that were written in the source. |
| 823 | SmallVector<CXXCtorInitializer *, 4> WrittenInits; |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 824 | for (auto *I : Constructor->inits()) { |
| 825 | if (!I->isWritten()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 826 | continue; |
| 827 | |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 828 | WrittenInits.push_back(I); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | // Sort the initializers in source order |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 832 | llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(), |
| 833 | &CompareCXXCtorInitializers); |
| 834 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 835 | // Visit the initializers in source order |
| 836 | for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) { |
| 837 | CXXCtorInitializer *Init = WrittenInits[I]; |
| 838 | if (Init->isAnyMemberInitializer()) { |
| 839 | if (Visit(MakeCursorMemberRef(Init->getAnyMember(), |
| 840 | Init->getMemberLocation(), TU))) |
| 841 | return true; |
| 842 | } else if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) { |
| 843 | if (Visit(TInfo->getTypeLoc())) |
| 844 | return true; |
| 845 | } |
| 846 | |
| 847 | // Visit the initializer value. |
| 848 | if (Expr *Initializer = Init->getInit()) |
| 849 | if (Visit(MakeCXCursor(Initializer, ND, TU, RegionOfInterest))) |
| 850 | return true; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest))) |
| 855 | return true; |
| 856 | } |
| 857 | |
| 858 | return false; |
| 859 | } |
| 860 | |
| 861 | bool CursorVisitor::VisitFieldDecl(FieldDecl *D) { |
| 862 | if (VisitDeclaratorDecl(D)) |
| 863 | return true; |
| 864 | |
| 865 | if (Expr *BitWidth = D->getBitWidth()) |
| 866 | return Visit(MakeCXCursor(BitWidth, StmtParent, TU, RegionOfInterest)); |
| 867 | |
| 868 | return false; |
| 869 | } |
| 870 | |
| 871 | bool CursorVisitor::VisitVarDecl(VarDecl *D) { |
| 872 | if (VisitDeclaratorDecl(D)) |
| 873 | return true; |
| 874 | |
| 875 | if (Expr *Init = D->getInit()) |
| 876 | return Visit(MakeCXCursor(Init, StmtParent, TU, RegionOfInterest)); |
| 877 | |
| 878 | return false; |
| 879 | } |
| 880 | |
| 881 | bool CursorVisitor::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 882 | if (VisitDeclaratorDecl(D)) |
| 883 | return true; |
| 884 | |
| 885 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) |
| 886 | if (Expr *DefArg = D->getDefaultArgument()) |
| 887 | return Visit(MakeCXCursor(DefArg, StmtParent, TU, RegionOfInterest)); |
| 888 | |
| 889 | return false; |
| 890 | } |
| 891 | |
| 892 | bool CursorVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 893 | // FIXME: Visit the "outer" template parameter lists on the FunctionDecl |
| 894 | // before visiting these template parameters. |
| 895 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 896 | return true; |
| 897 | |
| 898 | return VisitFunctionDecl(D->getTemplatedDecl()); |
| 899 | } |
| 900 | |
| 901 | bool CursorVisitor::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 902 | // FIXME: Visit the "outer" template parameter lists on the TagDecl |
| 903 | // before visiting these template parameters. |
| 904 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 905 | return true; |
| 906 | |
| 907 | return VisitCXXRecordDecl(D->getTemplatedDecl()); |
| 908 | } |
| 909 | |
| 910 | bool CursorVisitor::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 911 | if (VisitTemplateParameters(D->getTemplateParameters())) |
| 912 | return true; |
| 913 | |
| 914 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited() && |
| 915 | VisitTemplateArgumentLoc(D->getDefaultArgument())) |
| 916 | return true; |
| 917 | |
| 918 | return false; |
| 919 | } |
| 920 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 921 | bool CursorVisitor::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { |
| 922 | // Visit the bound, if it's explicit. |
| 923 | if (D->hasExplicitBound()) { |
| 924 | if (auto TInfo = D->getTypeSourceInfo()) { |
| 925 | if (Visit(TInfo->getTypeLoc())) |
| 926 | return true; |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | return false; |
| 931 | } |
| 932 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 933 | bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 934 | if (TypeSourceInfo *TSInfo = ND->getReturnTypeSourceInfo()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 935 | if (Visit(TSInfo->getTypeLoc())) |
| 936 | return true; |
| 937 | |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 938 | for (const auto *P : ND->params()) { |
| 939 | if (Visit(MakeCXCursor(P, TU, RegionOfInterest))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 940 | return true; |
| 941 | } |
| 942 | |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 943 | return ND->isThisDeclarationADefinition() && |
| 944 | Visit(MakeCXCursor(ND->getBody(), StmtParent, TU, RegionOfInterest)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | template <typename DeclIt> |
| 948 | static void addRangedDeclsInContainer(DeclIt *DI_current, DeclIt DE_current, |
| 949 | SourceManager &SM, SourceLocation EndLoc, |
| 950 | SmallVectorImpl<Decl *> &Decls) { |
| 951 | DeclIt next = *DI_current; |
| 952 | while (++next != DE_current) { |
| 953 | Decl *D_next = *next; |
| 954 | if (!D_next) |
| 955 | break; |
| 956 | SourceLocation L = D_next->getLocStart(); |
| 957 | if (!L.isValid()) |
| 958 | break; |
| 959 | if (SM.isBeforeInTranslationUnit(L, EndLoc)) { |
| 960 | *DI_current = next; |
| 961 | Decls.push_back(D_next); |
| 962 | continue; |
| 963 | } |
| 964 | break; |
| 965 | } |
| 966 | } |
| 967 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 968 | bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) { |
| 969 | // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially |
| 970 | // an @implementation can lexically contain Decls that are not properly |
| 971 | // nested in the AST. When we identify such cases, we need to retrofit |
| 972 | // this nesting here. |
| 973 | if (!DI_current && !FileDI_current) |
| 974 | return VisitDeclContext(D); |
| 975 | |
| 976 | // Scan the Decls that immediately come after the container |
| 977 | // in the current DeclContext. If any fall within the |
| 978 | // container's lexical region, stash them into a vector |
| 979 | // for later processing. |
| 980 | SmallVector<Decl *, 24> DeclsInContainer; |
| 981 | SourceLocation EndLoc = D->getSourceRange().getEnd(); |
| 982 | SourceManager &SM = AU->getSourceManager(); |
| 983 | if (EndLoc.isValid()) { |
| 984 | if (DI_current) { |
| 985 | addRangedDeclsInContainer(DI_current, DE_current, SM, EndLoc, |
| 986 | DeclsInContainer); |
| 987 | } else { |
| 988 | addRangedDeclsInContainer(FileDI_current, FileDE_current, SM, EndLoc, |
| 989 | DeclsInContainer); |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | // The common case. |
| 994 | if (DeclsInContainer.empty()) |
| 995 | return VisitDeclContext(D); |
| 996 | |
| 997 | // Get all the Decls in the DeclContext, and sort them with the |
| 998 | // additional ones we've collected. Then visit them. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 999 | for (auto *SubDecl : D->decls()) { |
| 1000 | if (!SubDecl || SubDecl->getLexicalDeclContext() != D || |
| 1001 | SubDecl->getLocStart().isInvalid()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1002 | continue; |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1003 | DeclsInContainer.push_back(SubDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | // Now sort the Decls so that they appear in lexical order. |
| 1007 | std::sort(DeclsInContainer.begin(), DeclsInContainer.end(), |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 1008 | [&SM](Decl *A, Decl *B) { |
| 1009 | SourceLocation L_A = A->getLocStart(); |
| 1010 | SourceLocation L_B = B->getLocStart(); |
| 1011 | assert(L_A.isValid() && L_B.isValid()); |
| 1012 | return SM.isBeforeInTranslationUnit(L_A, L_B); |
| 1013 | }); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1014 | |
| 1015 | // Now visit the decls. |
| 1016 | for (SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(), |
| 1017 | E = DeclsInContainer.end(); I != E; ++I) { |
| 1018 | CXCursor Cursor = MakeCXCursor(*I, TU, RegionOfInterest); |
Ted Kremenek | 0332558 | 2013-02-21 01:29:01 +0000 | [diff] [blame] | 1019 | const Optional<bool> &V = shouldVisitCursor(Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1020 | if (!V.hasValue()) |
| 1021 | continue; |
| 1022 | if (!V.getValue()) |
| 1023 | return false; |
| 1024 | if (Visit(Cursor, true)) |
| 1025 | return true; |
| 1026 | } |
| 1027 | return false; |
| 1028 | } |
| 1029 | |
| 1030 | bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { |
| 1031 | if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(), |
| 1032 | TU))) |
| 1033 | return true; |
| 1034 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1035 | if (VisitObjCTypeParamList(ND->getTypeParamList())) |
| 1036 | return true; |
| 1037 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1038 | ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin(); |
| 1039 | for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(), |
| 1040 | E = ND->protocol_end(); I != E; ++I, ++PL) |
| 1041 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1042 | return true; |
| 1043 | |
| 1044 | return VisitObjCContainerDecl(ND); |
| 1045 | } |
| 1046 | |
| 1047 | bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
| 1048 | if (!PID->isThisDeclarationADefinition()) |
| 1049 | return Visit(MakeCursorObjCProtocolRef(PID, PID->getLocation(), TU)); |
| 1050 | |
| 1051 | ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin(); |
| 1052 | for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(), |
| 1053 | E = PID->protocol_end(); I != E; ++I, ++PL) |
| 1054 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1055 | return true; |
| 1056 | |
| 1057 | return VisitObjCContainerDecl(PID); |
| 1058 | } |
| 1059 | |
| 1060 | bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) { |
| 1061 | if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc())) |
| 1062 | return true; |
| 1063 | |
| 1064 | // FIXME: This implements a workaround with @property declarations also being |
| 1065 | // installed in the DeclContext for the @interface. Eventually this code |
| 1066 | // should be removed. |
| 1067 | ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(PD->getDeclContext()); |
| 1068 | if (!CDecl || !CDecl->IsClassExtension()) |
| 1069 | return false; |
| 1070 | |
| 1071 | ObjCInterfaceDecl *ID = CDecl->getClassInterface(); |
| 1072 | if (!ID) |
| 1073 | return false; |
| 1074 | |
| 1075 | IdentifierInfo *PropertyId = PD->getIdentifier(); |
| 1076 | ObjCPropertyDecl *prevDecl = |
Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 1077 | ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(ID), PropertyId, |
| 1078 | PD->getQueryKind()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1079 | |
| 1080 | if (!prevDecl) |
| 1081 | return false; |
| 1082 | |
| 1083 | // Visit synthesized methods since they will be skipped when visiting |
| 1084 | // the @interface. |
| 1085 | if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl()) |
| 1086 | if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl) |
| 1087 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
| 1088 | return true; |
| 1089 | |
| 1090 | if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl()) |
| 1091 | if (MD->isPropertyAccessor() && MD->getLexicalDeclContext() == CDecl) |
| 1092 | if (Visit(MakeCXCursor(MD, TU, RegionOfInterest))) |
| 1093 | return true; |
| 1094 | |
| 1095 | return false; |
| 1096 | } |
| 1097 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1098 | bool CursorVisitor::VisitObjCTypeParamList(ObjCTypeParamList *typeParamList) { |
| 1099 | if (!typeParamList) |
| 1100 | return false; |
| 1101 | |
| 1102 | for (auto *typeParam : *typeParamList) { |
| 1103 | // Visit the type parameter. |
| 1104 | if (Visit(MakeCXCursor(typeParam, TU, RegionOfInterest))) |
| 1105 | return true; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | return false; |
| 1109 | } |
| 1110 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1111 | bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 1112 | if (!D->isThisDeclarationADefinition()) { |
| 1113 | // Forward declaration is treated like a reference. |
| 1114 | return Visit(MakeCursorObjCClassRef(D, D->getLocation(), TU)); |
| 1115 | } |
| 1116 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1117 | // Objective-C type parameters. |
| 1118 | if (VisitObjCTypeParamList(D->getTypeParamListAsWritten())) |
| 1119 | return true; |
| 1120 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1121 | // Issue callbacks for super class. |
| 1122 | if (D->getSuperClass() && |
| 1123 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
| 1124 | D->getSuperClassLoc(), |
| 1125 | TU))) |
| 1126 | return true; |
| 1127 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1128 | if (TypeSourceInfo *SuperClassTInfo = D->getSuperClassTInfo()) |
| 1129 | if (Visit(SuperClassTInfo->getTypeLoc())) |
| 1130 | return true; |
| 1131 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1132 | ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(); |
| 1133 | for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(), |
| 1134 | E = D->protocol_end(); I != E; ++I, ++PL) |
| 1135 | if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU))) |
| 1136 | return true; |
| 1137 | |
| 1138 | return VisitObjCContainerDecl(D); |
| 1139 | } |
| 1140 | |
| 1141 | bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) { |
| 1142 | return VisitObjCContainerDecl(D); |
| 1143 | } |
| 1144 | |
| 1145 | bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 1146 | // 'ID' could be null when dealing with invalid code. |
| 1147 | if (ObjCInterfaceDecl *ID = D->getClassInterface()) |
| 1148 | if (Visit(MakeCursorObjCClassRef(ID, D->getLocation(), TU))) |
| 1149 | return true; |
| 1150 | |
| 1151 | return VisitObjCImplDecl(D); |
| 1152 | } |
| 1153 | |
| 1154 | bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 1155 | #if 0 |
| 1156 | // Issue callbacks for super class. |
| 1157 | // FIXME: No source location information! |
| 1158 | if (D->getSuperClass() && |
| 1159 | Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(), |
| 1160 | D->getSuperClassLoc(), |
| 1161 | TU))) |
| 1162 | return true; |
| 1163 | #endif |
| 1164 | |
| 1165 | return VisitObjCImplDecl(D); |
| 1166 | } |
| 1167 | |
| 1168 | bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) { |
| 1169 | if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl()) |
| 1170 | if (PD->isIvarNameSpecified()) |
| 1171 | return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU)); |
| 1172 | |
| 1173 | return false; |
| 1174 | } |
| 1175 | |
| 1176 | bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { |
| 1177 | return VisitDeclContext(D); |
| 1178 | } |
| 1179 | |
| 1180 | bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 1181 | // Visit nested-name-specifier. |
| 1182 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1183 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1184 | return true; |
| 1185 | |
| 1186 | return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), |
| 1187 | D->getTargetNameLoc(), TU)); |
| 1188 | } |
| 1189 | |
| 1190 | bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { |
| 1191 | // Visit nested-name-specifier. |
| 1192 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1193 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1194 | return true; |
| 1195 | } |
| 1196 | |
| 1197 | if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU))) |
| 1198 | return true; |
| 1199 | |
| 1200 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1201 | } |
| 1202 | |
| 1203 | bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 1204 | // Visit nested-name-specifier. |
| 1205 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1206 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1207 | return true; |
| 1208 | |
| 1209 | return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), |
| 1210 | D->getIdentLocation(), TU)); |
| 1211 | } |
| 1212 | |
| 1213 | bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
| 1214 | // Visit nested-name-specifier. |
| 1215 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) { |
| 1216 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1217 | return true; |
| 1218 | } |
| 1219 | |
| 1220 | return VisitDeclarationNameInfo(D->getNameInfo()); |
| 1221 | } |
| 1222 | |
| 1223 | bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( |
| 1224 | UnresolvedUsingTypenameDecl *D) { |
| 1225 | // Visit nested-name-specifier. |
| 1226 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1227 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1228 | return true; |
| 1229 | |
| 1230 | return false; |
| 1231 | } |
| 1232 | |
| 1233 | bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { |
| 1234 | switch (Name.getName().getNameKind()) { |
| 1235 | case clang::DeclarationName::Identifier: |
| 1236 | case clang::DeclarationName::CXXLiteralOperatorName: |
| 1237 | case clang::DeclarationName::CXXOperatorName: |
| 1238 | case clang::DeclarationName::CXXUsingDirective: |
| 1239 | return false; |
| 1240 | |
| 1241 | case clang::DeclarationName::CXXConstructorName: |
| 1242 | case clang::DeclarationName::CXXDestructorName: |
| 1243 | case clang::DeclarationName::CXXConversionFunctionName: |
| 1244 | if (TypeSourceInfo *TSInfo = Name.getNamedTypeInfo()) |
| 1245 | return Visit(TSInfo->getTypeLoc()); |
| 1246 | return false; |
| 1247 | |
| 1248 | case clang::DeclarationName::ObjCZeroArgSelector: |
| 1249 | case clang::DeclarationName::ObjCOneArgSelector: |
| 1250 | case clang::DeclarationName::ObjCMultiArgSelector: |
| 1251 | // FIXME: Per-identifier location info? |
| 1252 | return false; |
| 1253 | } |
| 1254 | |
| 1255 | llvm_unreachable("Invalid DeclarationName::Kind!"); |
| 1256 | } |
| 1257 | |
| 1258 | bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, |
| 1259 | SourceRange Range) { |
| 1260 | // FIXME: This whole routine is a hack to work around the lack of proper |
| 1261 | // source information in nested-name-specifiers (PR5791). Since we do have |
| 1262 | // a beginning source location, we can visit the first component of the |
| 1263 | // nested-name-specifier, if it's a single-token component. |
| 1264 | if (!NNS) |
| 1265 | return false; |
| 1266 | |
| 1267 | // Get the first component in the nested-name-specifier. |
| 1268 | while (NestedNameSpecifier *Prefix = NNS->getPrefix()) |
| 1269 | NNS = Prefix; |
| 1270 | |
| 1271 | switch (NNS->getKind()) { |
| 1272 | case NestedNameSpecifier::Namespace: |
| 1273 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), |
| 1274 | TU)); |
| 1275 | |
| 1276 | case NestedNameSpecifier::NamespaceAlias: |
| 1277 | return Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1278 | Range.getBegin(), TU)); |
| 1279 | |
| 1280 | case NestedNameSpecifier::TypeSpec: { |
| 1281 | // If the type has a form where we know that the beginning of the source |
| 1282 | // range matches up with a reference cursor. Visit the appropriate reference |
| 1283 | // cursor. |
| 1284 | const Type *T = NNS->getAsType(); |
| 1285 | if (const TypedefType *Typedef = dyn_cast<TypedefType>(T)) |
| 1286 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); |
| 1287 | if (const TagType *Tag = dyn_cast<TagType>(T)) |
| 1288 | return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); |
| 1289 | if (const TemplateSpecializationType *TST |
| 1290 | = dyn_cast<TemplateSpecializationType>(T)) |
| 1291 | return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); |
| 1292 | break; |
| 1293 | } |
| 1294 | |
| 1295 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1296 | case NestedNameSpecifier::Global: |
| 1297 | case NestedNameSpecifier::Identifier: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 1298 | case NestedNameSpecifier::Super: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1299 | break; |
| 1300 | } |
| 1301 | |
| 1302 | return false; |
| 1303 | } |
| 1304 | |
| 1305 | bool |
| 1306 | CursorVisitor::VisitNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 1307 | SmallVector<NestedNameSpecifierLoc, 4> Qualifiers; |
| 1308 | for (; Qualifier; Qualifier = Qualifier.getPrefix()) |
| 1309 | Qualifiers.push_back(Qualifier); |
| 1310 | |
| 1311 | while (!Qualifiers.empty()) { |
| 1312 | NestedNameSpecifierLoc Q = Qualifiers.pop_back_val(); |
| 1313 | NestedNameSpecifier *NNS = Q.getNestedNameSpecifier(); |
| 1314 | switch (NNS->getKind()) { |
| 1315 | case NestedNameSpecifier::Namespace: |
| 1316 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), |
| 1317 | Q.getLocalBeginLoc(), |
| 1318 | TU))) |
| 1319 | return true; |
| 1320 | |
| 1321 | break; |
| 1322 | |
| 1323 | case NestedNameSpecifier::NamespaceAlias: |
| 1324 | if (Visit(MakeCursorNamespaceRef(NNS->getAsNamespaceAlias(), |
| 1325 | Q.getLocalBeginLoc(), |
| 1326 | TU))) |
| 1327 | return true; |
| 1328 | |
| 1329 | break; |
| 1330 | |
| 1331 | case NestedNameSpecifier::TypeSpec: |
| 1332 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1333 | if (Visit(Q.getTypeLoc())) |
| 1334 | return true; |
| 1335 | |
| 1336 | break; |
| 1337 | |
| 1338 | case NestedNameSpecifier::Global: |
| 1339 | case NestedNameSpecifier::Identifier: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 1340 | case NestedNameSpecifier::Super: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1341 | break; |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | return false; |
| 1346 | } |
| 1347 | |
| 1348 | bool CursorVisitor::VisitTemplateParameters( |
| 1349 | const TemplateParameterList *Params) { |
| 1350 | if (!Params) |
| 1351 | return false; |
| 1352 | |
| 1353 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 1354 | PEnd = Params->end(); |
| 1355 | P != PEnd; ++P) { |
| 1356 | if (Visit(MakeCXCursor(*P, TU, RegionOfInterest))) |
| 1357 | return true; |
| 1358 | } |
| 1359 | |
| 1360 | return false; |
| 1361 | } |
| 1362 | |
| 1363 | bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) { |
| 1364 | switch (Name.getKind()) { |
| 1365 | case TemplateName::Template: |
| 1366 | return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU)); |
| 1367 | |
| 1368 | case TemplateName::OverloadedTemplate: |
| 1369 | // Visit the overloaded template set. |
| 1370 | if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU))) |
| 1371 | return true; |
| 1372 | |
| 1373 | return false; |
| 1374 | |
| 1375 | case TemplateName::DependentTemplate: |
| 1376 | // FIXME: Visit nested-name-specifier. |
| 1377 | return false; |
| 1378 | |
| 1379 | case TemplateName::QualifiedTemplate: |
| 1380 | // FIXME: Visit nested-name-specifier. |
| 1381 | return Visit(MakeCursorTemplateRef( |
| 1382 | Name.getAsQualifiedTemplateName()->getDecl(), |
| 1383 | Loc, TU)); |
| 1384 | |
| 1385 | case TemplateName::SubstTemplateTemplateParm: |
| 1386 | return Visit(MakeCursorTemplateRef( |
| 1387 | Name.getAsSubstTemplateTemplateParm()->getParameter(), |
| 1388 | Loc, TU)); |
| 1389 | |
| 1390 | case TemplateName::SubstTemplateTemplateParmPack: |
| 1391 | return Visit(MakeCursorTemplateRef( |
| 1392 | Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(), |
| 1393 | Loc, TU)); |
| 1394 | } |
| 1395 | |
| 1396 | llvm_unreachable("Invalid TemplateName::Kind!"); |
| 1397 | } |
| 1398 | |
| 1399 | bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) { |
| 1400 | switch (TAL.getArgument().getKind()) { |
| 1401 | case TemplateArgument::Null: |
| 1402 | case TemplateArgument::Integral: |
| 1403 | case TemplateArgument::Pack: |
| 1404 | return false; |
| 1405 | |
| 1406 | case TemplateArgument::Type: |
| 1407 | if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo()) |
| 1408 | return Visit(TSInfo->getTypeLoc()); |
| 1409 | return false; |
| 1410 | |
| 1411 | case TemplateArgument::Declaration: |
| 1412 | if (Expr *E = TAL.getSourceDeclExpression()) |
| 1413 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
| 1414 | return false; |
| 1415 | |
| 1416 | case TemplateArgument::NullPtr: |
| 1417 | if (Expr *E = TAL.getSourceNullPtrExpression()) |
| 1418 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
| 1419 | return false; |
| 1420 | |
| 1421 | case TemplateArgument::Expression: |
| 1422 | if (Expr *E = TAL.getSourceExpression()) |
| 1423 | return Visit(MakeCXCursor(E, StmtParent, TU, RegionOfInterest)); |
| 1424 | return false; |
| 1425 | |
| 1426 | case TemplateArgument::Template: |
| 1427 | case TemplateArgument::TemplateExpansion: |
| 1428 | if (VisitNestedNameSpecifierLoc(TAL.getTemplateQualifierLoc())) |
| 1429 | return true; |
| 1430 | |
| 1431 | return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(), |
| 1432 | TAL.getTemplateNameLoc()); |
| 1433 | } |
| 1434 | |
| 1435 | llvm_unreachable("Invalid TemplateArgument::Kind!"); |
| 1436 | } |
| 1437 | |
| 1438 | bool CursorVisitor::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 1439 | return VisitDeclContext(D); |
| 1440 | } |
| 1441 | |
| 1442 | bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1443 | return Visit(TL.getUnqualifiedLoc()); |
| 1444 | } |
| 1445 | |
| 1446 | bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
| 1447 | ASTContext &Context = AU->getASTContext(); |
| 1448 | |
| 1449 | // Some builtin types (such as Objective-C's "id", "sel", and |
| 1450 | // "Class") have associated declarations. Create cursors for those. |
| 1451 | QualType VisitType; |
| 1452 | switch (TL.getTypePtr()->getKind()) { |
| 1453 | |
| 1454 | case BuiltinType::Void: |
| 1455 | case BuiltinType::NullPtr: |
| 1456 | case BuiltinType::Dependent: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 1457 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 1458 | case BuiltinType::Id: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 1459 | #include "clang/Basic/OpenCLImageTypes.def" |
NAKAMURA Takumi | 288c42e | 2013-02-07 12:47:42 +0000 | [diff] [blame] | 1460 | case BuiltinType::OCLSampler: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1461 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1462 | case BuiltinType::OCLClkEvent: |
| 1463 | case BuiltinType::OCLQueue: |
| 1464 | case BuiltinType::OCLNDRange: |
| 1465 | case BuiltinType::OCLReserveID: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1466 | #define BUILTIN_TYPE(Id, SingletonId) |
| 1467 | #define SIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1468 | #define UNSIGNED_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1469 | #define FLOATING_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1470 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 1471 | #include "clang/AST/BuiltinTypes.def" |
| 1472 | break; |
| 1473 | |
| 1474 | case BuiltinType::ObjCId: |
| 1475 | VisitType = Context.getObjCIdType(); |
| 1476 | break; |
| 1477 | |
| 1478 | case BuiltinType::ObjCClass: |
| 1479 | VisitType = Context.getObjCClassType(); |
| 1480 | break; |
| 1481 | |
| 1482 | case BuiltinType::ObjCSel: |
| 1483 | VisitType = Context.getObjCSelType(); |
| 1484 | break; |
| 1485 | } |
| 1486 | |
| 1487 | if (!VisitType.isNull()) { |
| 1488 | if (const TypedefType *Typedef = VisitType->getAs<TypedefType>()) |
| 1489 | return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(), |
| 1490 | TU)); |
| 1491 | } |
| 1492 | |
| 1493 | return false; |
| 1494 | } |
| 1495 | |
| 1496 | bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 1497 | return Visit(MakeCursorTypeRef(TL.getTypedefNameDecl(), TL.getNameLoc(), TU)); |
| 1498 | } |
| 1499 | |
| 1500 | bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 1501 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1502 | } |
| 1503 | |
| 1504 | bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { |
| 1505 | if (TL.isDefinition()) |
| 1506 | return Visit(MakeCXCursor(TL.getDecl(), TU, RegionOfInterest)); |
| 1507 | |
| 1508 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1509 | } |
| 1510 | |
| 1511 | bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 1512 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1513 | } |
| 1514 | |
| 1515 | bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 1516 | return Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | bool CursorVisitor::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 1520 | if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseLoc())) |
| 1521 | return true; |
| 1522 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 1523 | for (unsigned I = 0, N = TL.getNumTypeArgs(); I != N; ++I) { |
| 1524 | if (Visit(TL.getTypeArgTInfo(I)->getTypeLoc())) |
| 1525 | return true; |
| 1526 | } |
| 1527 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1528 | for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) { |
| 1529 | if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I), |
| 1530 | TU))) |
| 1531 | return true; |
| 1532 | } |
| 1533 | |
| 1534 | return false; |
| 1535 | } |
| 1536 | |
| 1537 | bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 1538 | return Visit(TL.getPointeeLoc()); |
| 1539 | } |
| 1540 | |
| 1541 | bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) { |
| 1542 | return Visit(TL.getInnerLoc()); |
| 1543 | } |
| 1544 | |
| 1545 | bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1546 | return Visit(TL.getPointeeLoc()); |
| 1547 | } |
| 1548 | |
| 1549 | bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1550 | return Visit(TL.getPointeeLoc()); |
| 1551 | } |
| 1552 | |
| 1553 | bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1554 | return Visit(TL.getPointeeLoc()); |
| 1555 | } |
| 1556 | |
| 1557 | bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 1558 | return Visit(TL.getPointeeLoc()); |
| 1559 | } |
| 1560 | |
| 1561 | bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 1562 | return Visit(TL.getPointeeLoc()); |
| 1563 | } |
| 1564 | |
| 1565 | bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 1566 | return Visit(TL.getModifiedLoc()); |
| 1567 | } |
| 1568 | |
| 1569 | bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL, |
| 1570 | bool SkipResultType) { |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 1571 | if (!SkipResultType && Visit(TL.getReturnLoc())) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1572 | return true; |
| 1573 | |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 1574 | for (unsigned I = 0, N = TL.getNumParams(); I != N; ++I) |
| 1575 | if (Decl *D = TL.getParam(I)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1576 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest))) |
| 1577 | return true; |
| 1578 | |
| 1579 | return false; |
| 1580 | } |
| 1581 | |
| 1582 | bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1583 | if (Visit(TL.getElementLoc())) |
| 1584 | return true; |
| 1585 | |
| 1586 | if (Expr *Size = TL.getSizeExpr()) |
| 1587 | return Visit(MakeCXCursor(Size, StmtParent, TU, RegionOfInterest)); |
| 1588 | |
| 1589 | return false; |
| 1590 | } |
| 1591 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 1592 | bool CursorVisitor::VisitDecayedTypeLoc(DecayedTypeLoc TL) { |
| 1593 | return Visit(TL.getOriginalLoc()); |
| 1594 | } |
| 1595 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 1596 | bool CursorVisitor::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { |
| 1597 | return Visit(TL.getOriginalLoc()); |
| 1598 | } |
| 1599 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1600 | bool CursorVisitor::VisitTemplateSpecializationTypeLoc( |
| 1601 | TemplateSpecializationTypeLoc TL) { |
| 1602 | // Visit the template name. |
| 1603 | if (VisitTemplateName(TL.getTypePtr()->getTemplateName(), |
| 1604 | TL.getTemplateNameLoc())) |
| 1605 | return true; |
| 1606 | |
| 1607 | // Visit the template arguments. |
| 1608 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1609 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1610 | return true; |
| 1611 | |
| 1612 | return false; |
| 1613 | } |
| 1614 | |
| 1615 | bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 1616 | return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU)); |
| 1617 | } |
| 1618 | |
| 1619 | bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 1620 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1621 | return Visit(TSInfo->getTypeLoc()); |
| 1622 | |
| 1623 | return false; |
| 1624 | } |
| 1625 | |
| 1626 | bool CursorVisitor::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 1627 | if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo()) |
| 1628 | return Visit(TSInfo->getTypeLoc()); |
| 1629 | |
| 1630 | return false; |
| 1631 | } |
| 1632 | |
| 1633 | bool CursorVisitor::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 1634 | return VisitNestedNameSpecifierLoc(TL.getQualifierLoc()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | bool CursorVisitor::VisitDependentTemplateSpecializationTypeLoc( |
| 1638 | DependentTemplateSpecializationTypeLoc TL) { |
| 1639 | // Visit the nested-name-specifier, if there is one. |
| 1640 | if (TL.getQualifierLoc() && |
| 1641 | VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1642 | return true; |
| 1643 | |
| 1644 | // Visit the template arguments. |
| 1645 | for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I) |
| 1646 | if (VisitTemplateArgumentLoc(TL.getArgLoc(I))) |
| 1647 | return true; |
| 1648 | |
| 1649 | return false; |
| 1650 | } |
| 1651 | |
| 1652 | bool CursorVisitor::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 1653 | if (VisitNestedNameSpecifierLoc(TL.getQualifierLoc())) |
| 1654 | return true; |
| 1655 | |
| 1656 | return Visit(TL.getNamedTypeLoc()); |
| 1657 | } |
| 1658 | |
| 1659 | bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { |
| 1660 | return Visit(TL.getPatternLoc()); |
| 1661 | } |
| 1662 | |
| 1663 | bool CursorVisitor::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 1664 | if (Expr *E = TL.getUnderlyingExpr()) |
| 1665 | return Visit(MakeCXCursor(E, StmtParent, TU)); |
| 1666 | |
| 1667 | return false; |
| 1668 | } |
| 1669 | |
| 1670 | bool CursorVisitor::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 1671 | return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); |
| 1672 | } |
| 1673 | |
| 1674 | bool CursorVisitor::VisitAtomicTypeLoc(AtomicTypeLoc TL) { |
| 1675 | return Visit(TL.getValueLoc()); |
| 1676 | } |
| 1677 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 1678 | bool CursorVisitor::VisitPipeTypeLoc(PipeTypeLoc TL) { |
| 1679 | return Visit(TL.getValueLoc()); |
| 1680 | } |
| 1681 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1682 | #define DEFAULT_TYPELOC_IMPL(CLASS, PARENT) \ |
| 1683 | bool CursorVisitor::Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { \ |
| 1684 | return Visit##PARENT##Loc(TL); \ |
| 1685 | } |
| 1686 | |
| 1687 | DEFAULT_TYPELOC_IMPL(Complex, Type) |
| 1688 | DEFAULT_TYPELOC_IMPL(ConstantArray, ArrayType) |
| 1689 | DEFAULT_TYPELOC_IMPL(IncompleteArray, ArrayType) |
| 1690 | DEFAULT_TYPELOC_IMPL(VariableArray, ArrayType) |
| 1691 | DEFAULT_TYPELOC_IMPL(DependentSizedArray, ArrayType) |
| 1692 | DEFAULT_TYPELOC_IMPL(DependentSizedExtVector, Type) |
| 1693 | DEFAULT_TYPELOC_IMPL(Vector, Type) |
| 1694 | DEFAULT_TYPELOC_IMPL(ExtVector, VectorType) |
| 1695 | DEFAULT_TYPELOC_IMPL(FunctionProto, FunctionType) |
| 1696 | DEFAULT_TYPELOC_IMPL(FunctionNoProto, FunctionType) |
| 1697 | DEFAULT_TYPELOC_IMPL(Record, TagType) |
| 1698 | DEFAULT_TYPELOC_IMPL(Enum, TagType) |
| 1699 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParm, Type) |
| 1700 | DEFAULT_TYPELOC_IMPL(SubstTemplateTypeParmPack, Type) |
| 1701 | DEFAULT_TYPELOC_IMPL(Auto, Type) |
| 1702 | |
| 1703 | bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 1704 | // Visit the nested-name-specifier, if present. |
| 1705 | if (NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc()) |
| 1706 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 1707 | return true; |
| 1708 | |
| 1709 | if (D->isCompleteDefinition()) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1710 | for (const auto &I : D->bases()) { |
| 1711 | if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(&I, TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1712 | return true; |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | return VisitTagDecl(D); |
| 1717 | } |
| 1718 | |
| 1719 | bool CursorVisitor::VisitAttributes(Decl *D) { |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 1720 | for (const auto *I : D->attrs()) |
| 1721 | if (Visit(MakeCXCursor(I, D, TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1722 | return true; |
| 1723 | |
| 1724 | return false; |
| 1725 | } |
| 1726 | |
| 1727 | //===----------------------------------------------------------------------===// |
| 1728 | // Data-recursive visitor methods. |
| 1729 | //===----------------------------------------------------------------------===// |
| 1730 | |
| 1731 | namespace { |
| 1732 | #define DEF_JOB(NAME, DATA, KIND)\ |
| 1733 | class NAME : public VisitorJob {\ |
| 1734 | public:\ |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1735 | NAME(const DATA *d, CXCursor parent) : \ |
| 1736 | VisitorJob(parent, VisitorJob::KIND, d) {} \ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1737 | static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\ |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1738 | const DATA *get() const { return static_cast<const DATA*>(data[0]); }\ |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1739 | }; |
| 1740 | |
| 1741 | DEF_JOB(StmtVisit, Stmt, StmtVisitKind) |
| 1742 | DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind) |
| 1743 | DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind) |
| 1744 | DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1745 | DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind) |
| 1746 | DEF_JOB(LambdaExprParts, LambdaExpr, LambdaExprPartsKind) |
| 1747 | DEF_JOB(PostChildrenVisit, void, PostChildrenVisitKind) |
| 1748 | #undef DEF_JOB |
| 1749 | |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 1750 | class ExplicitTemplateArgsVisit : public VisitorJob { |
| 1751 | public: |
| 1752 | ExplicitTemplateArgsVisit(const TemplateArgumentLoc *Begin, |
| 1753 | const TemplateArgumentLoc *End, CXCursor parent) |
| 1754 | : VisitorJob(parent, VisitorJob::ExplicitTemplateArgsVisitKind, Begin, |
| 1755 | End) {} |
| 1756 | static bool classof(const VisitorJob *VJ) { |
| 1757 | return VJ->getKind() == ExplicitTemplateArgsVisitKind; |
| 1758 | } |
| 1759 | const TemplateArgumentLoc *begin() const { |
| 1760 | return static_cast<const TemplateArgumentLoc *>(data[0]); |
| 1761 | } |
| 1762 | const TemplateArgumentLoc *end() { |
| 1763 | return static_cast<const TemplateArgumentLoc *>(data[1]); |
| 1764 | } |
| 1765 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1766 | class DeclVisit : public VisitorJob { |
| 1767 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1768 | DeclVisit(const Decl *D, CXCursor parent, bool isFirst) : |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1769 | VisitorJob(parent, VisitorJob::DeclVisitKind, |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 1770 | D, isFirst ? (void*) 1 : (void*) nullptr) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1771 | static bool classof(const VisitorJob *VJ) { |
| 1772 | return VJ->getKind() == DeclVisitKind; |
| 1773 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1774 | const Decl *get() const { return static_cast<const Decl *>(data[0]); } |
Dmitri Gribenko | e5423a7 | 2015-03-23 19:23:50 +0000 | [diff] [blame] | 1775 | bool isFirst() const { return data[1] != nullptr; } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1776 | }; |
| 1777 | class TypeLocVisit : public VisitorJob { |
| 1778 | public: |
| 1779 | TypeLocVisit(TypeLoc tl, CXCursor parent) : |
| 1780 | VisitorJob(parent, VisitorJob::TypeLocVisitKind, |
| 1781 | tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {} |
| 1782 | |
| 1783 | static bool classof(const VisitorJob *VJ) { |
| 1784 | return VJ->getKind() == TypeLocVisitKind; |
| 1785 | } |
| 1786 | |
| 1787 | TypeLoc get() const { |
| 1788 | QualType T = QualType::getFromOpaquePtr(data[0]); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1789 | return TypeLoc(T, const_cast<void *>(data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1790 | } |
| 1791 | }; |
| 1792 | |
| 1793 | class LabelRefVisit : public VisitorJob { |
| 1794 | public: |
| 1795 | LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent) |
| 1796 | : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD, |
| 1797 | labelLoc.getPtrEncoding()) {} |
| 1798 | |
| 1799 | static bool classof(const VisitorJob *VJ) { |
| 1800 | return VJ->getKind() == VisitorJob::LabelRefVisitKind; |
| 1801 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1802 | const LabelDecl *get() const { |
| 1803 | return static_cast<const LabelDecl *>(data[0]); |
| 1804 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1805 | SourceLocation getLoc() const { |
| 1806 | return SourceLocation::getFromPtrEncoding(data[1]); } |
| 1807 | }; |
| 1808 | |
| 1809 | class NestedNameSpecifierLocVisit : public VisitorJob { |
| 1810 | public: |
| 1811 | NestedNameSpecifierLocVisit(NestedNameSpecifierLoc Qualifier, CXCursor parent) |
| 1812 | : VisitorJob(parent, VisitorJob::NestedNameSpecifierLocVisitKind, |
| 1813 | Qualifier.getNestedNameSpecifier(), |
| 1814 | Qualifier.getOpaqueData()) { } |
| 1815 | |
| 1816 | static bool classof(const VisitorJob *VJ) { |
| 1817 | return VJ->getKind() == VisitorJob::NestedNameSpecifierLocVisitKind; |
| 1818 | } |
| 1819 | |
| 1820 | NestedNameSpecifierLoc get() const { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1821 | return NestedNameSpecifierLoc( |
| 1822 | const_cast<NestedNameSpecifier *>( |
| 1823 | static_cast<const NestedNameSpecifier *>(data[0])), |
| 1824 | const_cast<void *>(data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1825 | } |
| 1826 | }; |
| 1827 | |
| 1828 | class DeclarationNameInfoVisit : public VisitorJob { |
| 1829 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1830 | DeclarationNameInfoVisit(const Stmt *S, CXCursor parent) |
Dmitri Gribenko | dd7dacf | 2013-02-03 13:19:54 +0000 | [diff] [blame] | 1831 | : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1832 | static bool classof(const VisitorJob *VJ) { |
| 1833 | return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind; |
| 1834 | } |
| 1835 | DeclarationNameInfo get() const { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1836 | const Stmt *S = static_cast<const Stmt *>(data[0]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1837 | switch (S->getStmtClass()) { |
| 1838 | default: |
| 1839 | llvm_unreachable("Unhandled Stmt"); |
| 1840 | case clang::Stmt::MSDependentExistsStmtClass: |
| 1841 | return cast<MSDependentExistsStmt>(S)->getNameInfo(); |
| 1842 | case Stmt::CXXDependentScopeMemberExprClass: |
| 1843 | return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo(); |
| 1844 | case Stmt::DependentScopeDeclRefExprClass: |
| 1845 | return cast<DependentScopeDeclRefExpr>(S)->getNameInfo(); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1846 | case Stmt::OMPCriticalDirectiveClass: |
| 1847 | return cast<OMPCriticalDirective>(S)->getDirectiveName(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1848 | } |
| 1849 | } |
| 1850 | }; |
| 1851 | class MemberRefVisit : public VisitorJob { |
| 1852 | public: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1853 | MemberRefVisit(const FieldDecl *D, SourceLocation L, CXCursor parent) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1854 | : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D, |
| 1855 | L.getPtrEncoding()) {} |
| 1856 | static bool classof(const VisitorJob *VJ) { |
| 1857 | return VJ->getKind() == VisitorJob::MemberRefVisitKind; |
| 1858 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1859 | const FieldDecl *get() const { |
| 1860 | return static_cast<const FieldDecl *>(data[0]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1861 | } |
| 1862 | SourceLocation getLoc() const { |
| 1863 | return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]); |
| 1864 | } |
| 1865 | }; |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1866 | class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void> { |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1867 | friend class OMPClauseEnqueue; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1868 | VisitorWorkList &WL; |
| 1869 | CXCursor Parent; |
| 1870 | public: |
| 1871 | EnqueueVisitor(VisitorWorkList &wl, CXCursor parent) |
| 1872 | : WL(wl), Parent(parent) {} |
| 1873 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1874 | void VisitAddrLabelExpr(const AddrLabelExpr *E); |
| 1875 | void VisitBlockExpr(const BlockExpr *B); |
| 1876 | void VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 1877 | void VisitCompoundStmt(const CompoundStmt *S); |
| 1878 | void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { /* Do nothing. */ } |
| 1879 | void VisitMSDependentExistsStmt(const MSDependentExistsStmt *S); |
| 1880 | void VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E); |
| 1881 | void VisitCXXNewExpr(const CXXNewExpr *E); |
| 1882 | void VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E); |
| 1883 | void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *E); |
| 1884 | void VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E); |
| 1885 | void VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *E); |
| 1886 | void VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
| 1887 | void VisitCXXUnresolvedConstructExpr(const CXXUnresolvedConstructExpr *E); |
| 1888 | void VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
| 1889 | void VisitCXXCatchStmt(const CXXCatchStmt *S); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 1890 | void VisitCXXForRangeStmt(const CXXForRangeStmt *S); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1891 | void VisitDeclRefExpr(const DeclRefExpr *D); |
| 1892 | void VisitDeclStmt(const DeclStmt *S); |
| 1893 | void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E); |
| 1894 | void VisitDesignatedInitExpr(const DesignatedInitExpr *E); |
| 1895 | void VisitExplicitCastExpr(const ExplicitCastExpr *E); |
| 1896 | void VisitForStmt(const ForStmt *FS); |
| 1897 | void VisitGotoStmt(const GotoStmt *GS); |
| 1898 | void VisitIfStmt(const IfStmt *If); |
| 1899 | void VisitInitListExpr(const InitListExpr *IE); |
| 1900 | void VisitMemberExpr(const MemberExpr *M); |
| 1901 | void VisitOffsetOfExpr(const OffsetOfExpr *E); |
| 1902 | void VisitObjCEncodeExpr(const ObjCEncodeExpr *E); |
| 1903 | void VisitObjCMessageExpr(const ObjCMessageExpr *M); |
| 1904 | void VisitOverloadExpr(const OverloadExpr *E); |
| 1905 | void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
| 1906 | void VisitStmt(const Stmt *S); |
| 1907 | void VisitSwitchStmt(const SwitchStmt *S); |
| 1908 | void VisitWhileStmt(const WhileStmt *W); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1909 | void VisitTypeTraitExpr(const TypeTraitExpr *E); |
| 1910 | void VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E); |
| 1911 | void VisitExpressionTraitExpr(const ExpressionTraitExpr *E); |
| 1912 | void VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U); |
| 1913 | void VisitVAArgExpr(const VAArgExpr *E); |
| 1914 | void VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
| 1915 | void VisitPseudoObjectExpr(const PseudoObjectExpr *E); |
| 1916 | void VisitOpaqueValueExpr(const OpaqueValueExpr *E); |
| 1917 | void VisitLambdaExpr(const LambdaExpr *E); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1918 | void VisitOMPExecutableDirective(const OMPExecutableDirective *D); |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 1919 | void VisitOMPLoopDirective(const OMPLoopDirective *D); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1920 | void VisitOMPParallelDirective(const OMPParallelDirective *D); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 1921 | void VisitOMPSimdDirective(const OMPSimdDirective *D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1922 | void VisitOMPForDirective(const OMPForDirective *D); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1923 | void VisitOMPForSimdDirective(const OMPForSimdDirective *D); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 1924 | void VisitOMPSectionsDirective(const OMPSectionsDirective *D); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1925 | void VisitOMPSectionDirective(const OMPSectionDirective *D); |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 1926 | void VisitOMPSingleDirective(const OMPSingleDirective *D); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1927 | void VisitOMPMasterDirective(const OMPMasterDirective *D); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1928 | void VisitOMPCriticalDirective(const OMPCriticalDirective *D); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1929 | void VisitOMPParallelForDirective(const OMPParallelForDirective *D); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1930 | void VisitOMPParallelForSimdDirective(const OMPParallelForSimdDirective *D); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1931 | void VisitOMPParallelSectionsDirective(const OMPParallelSectionsDirective *D); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1932 | void VisitOMPTaskDirective(const OMPTaskDirective *D); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1933 | void VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 1934 | void VisitOMPBarrierDirective(const OMPBarrierDirective *D); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 1935 | void VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1936 | void VisitOMPTaskgroupDirective(const OMPTaskgroupDirective *D); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 1937 | void |
| 1938 | VisitOMPCancellationPointDirective(const OMPCancellationPointDirective *D); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 1939 | void VisitOMPCancelDirective(const OMPCancelDirective *D); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 1940 | void VisitOMPFlushDirective(const OMPFlushDirective *D); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1941 | void VisitOMPOrderedDirective(const OMPOrderedDirective *D); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 1942 | void VisitOMPAtomicDirective(const OMPAtomicDirective *D); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 1943 | void VisitOMPTargetDirective(const OMPTargetDirective *D); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 1944 | void VisitOMPTargetDataDirective(const OMPTargetDataDirective *D); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 1945 | void VisitOMPTargetEnterDataDirective(const OMPTargetEnterDataDirective *D); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 1946 | void VisitOMPTargetExitDataDirective(const OMPTargetExitDataDirective *D); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 1947 | void VisitOMPTargetParallelDirective(const OMPTargetParallelDirective *D); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 1948 | void |
| 1949 | VisitOMPTargetParallelForDirective(const OMPTargetParallelForDirective *D); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 1950 | void VisitOMPTeamsDirective(const OMPTeamsDirective *D); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 1951 | void VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 1952 | void VisitOMPTaskLoopSimdDirective(const OMPTaskLoopSimdDirective *D); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 1953 | void VisitOMPDistributeDirective(const OMPDistributeDirective *D); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1954 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1955 | private: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1956 | void AddDeclarationNameInfo(const Stmt *S); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1957 | void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier); |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 1958 | void AddExplicitTemplateArgs(const TemplateArgumentLoc *A, |
| 1959 | unsigned NumTemplateArgs); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1960 | void AddMemberRef(const FieldDecl *D, SourceLocation L); |
| 1961 | void AddStmt(const Stmt *S); |
| 1962 | void AddDecl(const Decl *D, bool isFirst = true); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1963 | void AddTypeLoc(TypeSourceInfo *TI); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1964 | void EnqueueChildren(const Stmt *S); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1965 | void EnqueueChildren(const OMPClause *S); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1966 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1967 | } // end anonyous namespace |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1968 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1969 | void EnqueueVisitor::AddDeclarationNameInfo(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1970 | // 'S' should always be non-null, since it comes from the |
| 1971 | // statement we are visiting. |
| 1972 | WL.push_back(DeclarationNameInfoVisit(S, Parent)); |
| 1973 | } |
| 1974 | |
| 1975 | void |
| 1976 | EnqueueVisitor::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc Qualifier) { |
| 1977 | if (Qualifier) |
| 1978 | WL.push_back(NestedNameSpecifierLocVisit(Qualifier, Parent)); |
| 1979 | } |
| 1980 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1981 | void EnqueueVisitor::AddStmt(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1982 | if (S) |
| 1983 | WL.push_back(StmtVisit(S, Parent)); |
| 1984 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1985 | void EnqueueVisitor::AddDecl(const Decl *D, bool isFirst) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1986 | if (D) |
| 1987 | WL.push_back(DeclVisit(D, Parent, isFirst)); |
| 1988 | } |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 1989 | void EnqueueVisitor::AddExplicitTemplateArgs(const TemplateArgumentLoc *A, |
| 1990 | unsigned NumTemplateArgs) { |
| 1991 | WL.push_back(ExplicitTemplateArgsVisit(A, A + NumTemplateArgs, Parent)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1992 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 1993 | void EnqueueVisitor::AddMemberRef(const FieldDecl *D, SourceLocation L) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1994 | if (D) |
| 1995 | WL.push_back(MemberRefVisit(D, L, Parent)); |
| 1996 | } |
| 1997 | void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) { |
| 1998 | if (TI) |
| 1999 | WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent)); |
| 2000 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2001 | void EnqueueVisitor::EnqueueChildren(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2002 | unsigned size = WL.size(); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2003 | for (const Stmt *SubStmt : S->children()) { |
| 2004 | AddStmt(SubStmt); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2005 | } |
| 2006 | if (size == WL.size()) |
| 2007 | return; |
| 2008 | // Now reverse the entries we just added. This will match the DFS |
| 2009 | // ordering performed by the worklist. |
| 2010 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2011 | std::reverse(I, E); |
| 2012 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2013 | namespace { |
| 2014 | class OMPClauseEnqueue : public ConstOMPClauseVisitor<OMPClauseEnqueue> { |
| 2015 | EnqueueVisitor *Visitor; |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2016 | /// \brief Process clauses with list of variables. |
| 2017 | template <typename T> |
| 2018 | void VisitOMPClauseList(T *Node); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2019 | public: |
| 2020 | OMPClauseEnqueue(EnqueueVisitor *Visitor) : Visitor(Visitor) { } |
| 2021 | #define OPENMP_CLAUSE(Name, Class) \ |
| 2022 | void Visit##Class(const Class *C); |
| 2023 | #include "clang/Basic/OpenMPKinds.def" |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2024 | void VisitOMPClauseWithPreInit(const OMPClauseWithPreInit *C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2025 | void VisitOMPClauseWithPostUpdate(const OMPClauseWithPostUpdate *C); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2026 | }; |
| 2027 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2028 | void OMPClauseEnqueue::VisitOMPClauseWithPreInit( |
| 2029 | const OMPClauseWithPreInit *C) { |
| 2030 | Visitor->AddStmt(C->getPreInitStmt()); |
| 2031 | } |
| 2032 | |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2033 | void OMPClauseEnqueue::VisitOMPClauseWithPostUpdate( |
| 2034 | const OMPClauseWithPostUpdate *C) { |
Alexey Bataev | 37e594c | 2016-03-04 07:21:16 +0000 | [diff] [blame] | 2035 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2036 | Visitor->AddStmt(C->getPostUpdateExpr()); |
| 2037 | } |
| 2038 | |
Alexey Bataev | aadd52e | 2014-02-13 05:29:23 +0000 | [diff] [blame] | 2039 | void OMPClauseEnqueue::VisitOMPIfClause(const OMPIfClause *C) { |
| 2040 | Visitor->AddStmt(C->getCondition()); |
| 2041 | } |
| 2042 | |
Alexey Bataev | 3778b60 | 2014-07-17 07:32:53 +0000 | [diff] [blame] | 2043 | void OMPClauseEnqueue::VisitOMPFinalClause(const OMPFinalClause *C) { |
| 2044 | Visitor->AddStmt(C->getCondition()); |
| 2045 | } |
| 2046 | |
Alexey Bataev | 568a833 | 2014-03-06 06:15:19 +0000 | [diff] [blame] | 2047 | void OMPClauseEnqueue::VisitOMPNumThreadsClause(const OMPNumThreadsClause *C) { |
| 2048 | Visitor->AddStmt(C->getNumThreads()); |
| 2049 | } |
| 2050 | |
Alexey Bataev | 62c87d2 | 2014-03-21 04:51:18 +0000 | [diff] [blame] | 2051 | void OMPClauseEnqueue::VisitOMPSafelenClause(const OMPSafelenClause *C) { |
| 2052 | Visitor->AddStmt(C->getSafelen()); |
| 2053 | } |
| 2054 | |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 2055 | void OMPClauseEnqueue::VisitOMPSimdlenClause(const OMPSimdlenClause *C) { |
| 2056 | Visitor->AddStmt(C->getSimdlen()); |
| 2057 | } |
| 2058 | |
Alexander Musman | 8bd31e6 | 2014-05-27 15:12:19 +0000 | [diff] [blame] | 2059 | void OMPClauseEnqueue::VisitOMPCollapseClause(const OMPCollapseClause *C) { |
| 2060 | Visitor->AddStmt(C->getNumForLoops()); |
| 2061 | } |
| 2062 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2063 | void OMPClauseEnqueue::VisitOMPDefaultClause(const OMPDefaultClause *C) { } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2064 | |
Alexey Bataev | bcbadb6 | 2014-05-06 06:04:14 +0000 | [diff] [blame] | 2065 | void OMPClauseEnqueue::VisitOMPProcBindClause(const OMPProcBindClause *C) { } |
| 2066 | |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2067 | void OMPClauseEnqueue::VisitOMPScheduleClause(const OMPScheduleClause *C) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2068 | VisitOMPClauseWithPreInit(C); |
Alexey Bataev | 56dafe8 | 2014-06-20 07:16:17 +0000 | [diff] [blame] | 2069 | Visitor->AddStmt(C->getChunkSize()); |
| 2070 | } |
| 2071 | |
Alexey Bataev | 10e775f | 2015-07-30 11:36:16 +0000 | [diff] [blame] | 2072 | void OMPClauseEnqueue::VisitOMPOrderedClause(const OMPOrderedClause *C) { |
| 2073 | Visitor->AddStmt(C->getNumForLoops()); |
| 2074 | } |
Alexey Bataev | 142e1fc | 2014-06-20 09:44:06 +0000 | [diff] [blame] | 2075 | |
Alexey Bataev | 236070f | 2014-06-20 11:19:47 +0000 | [diff] [blame] | 2076 | void OMPClauseEnqueue::VisitOMPNowaitClause(const OMPNowaitClause *) {} |
| 2077 | |
Alexey Bataev | 7aea99a | 2014-07-17 12:19:31 +0000 | [diff] [blame] | 2078 | void OMPClauseEnqueue::VisitOMPUntiedClause(const OMPUntiedClause *) {} |
| 2079 | |
Alexey Bataev | 74ba3a5 | 2014-07-17 12:47:03 +0000 | [diff] [blame] | 2080 | void OMPClauseEnqueue::VisitOMPMergeableClause(const OMPMergeableClause *) {} |
| 2081 | |
Alexey Bataev | f98b00c | 2014-07-23 02:27:21 +0000 | [diff] [blame] | 2082 | void OMPClauseEnqueue::VisitOMPReadClause(const OMPReadClause *) {} |
| 2083 | |
Alexey Bataev | dea4761 | 2014-07-23 07:46:59 +0000 | [diff] [blame] | 2084 | void OMPClauseEnqueue::VisitOMPWriteClause(const OMPWriteClause *) {} |
| 2085 | |
Alexey Bataev | 67a4f22 | 2014-07-23 10:25:33 +0000 | [diff] [blame] | 2086 | void OMPClauseEnqueue::VisitOMPUpdateClause(const OMPUpdateClause *) {} |
| 2087 | |
Alexey Bataev | 459dec0 | 2014-07-24 06:46:57 +0000 | [diff] [blame] | 2088 | void OMPClauseEnqueue::VisitOMPCaptureClause(const OMPCaptureClause *) {} |
| 2089 | |
Alexey Bataev | 82bad8b | 2014-07-24 08:55:34 +0000 | [diff] [blame] | 2090 | void OMPClauseEnqueue::VisitOMPSeqCstClause(const OMPSeqCstClause *) {} |
| 2091 | |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 2092 | void OMPClauseEnqueue::VisitOMPThreadsClause(const OMPThreadsClause *) {} |
| 2093 | |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 2094 | void OMPClauseEnqueue::VisitOMPSIMDClause(const OMPSIMDClause *) {} |
| 2095 | |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 2096 | void OMPClauseEnqueue::VisitOMPNogroupClause(const OMPNogroupClause *) {} |
| 2097 | |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 2098 | void OMPClauseEnqueue::VisitOMPDeviceClause(const OMPDeviceClause *C) { |
| 2099 | Visitor->AddStmt(C->getDevice()); |
| 2100 | } |
| 2101 | |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 2102 | void OMPClauseEnqueue::VisitOMPNumTeamsClause(const OMPNumTeamsClause *C) { |
| 2103 | Visitor->AddStmt(C->getNumTeams()); |
| 2104 | } |
| 2105 | |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 2106 | void OMPClauseEnqueue::VisitOMPThreadLimitClause(const OMPThreadLimitClause *C) { |
| 2107 | Visitor->AddStmt(C->getThreadLimit()); |
| 2108 | } |
| 2109 | |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 2110 | void OMPClauseEnqueue::VisitOMPPriorityClause(const OMPPriorityClause *C) { |
| 2111 | Visitor->AddStmt(C->getPriority()); |
| 2112 | } |
| 2113 | |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 2114 | void OMPClauseEnqueue::VisitOMPGrainsizeClause(const OMPGrainsizeClause *C) { |
| 2115 | Visitor->AddStmt(C->getGrainsize()); |
| 2116 | } |
| 2117 | |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 2118 | void OMPClauseEnqueue::VisitOMPNumTasksClause(const OMPNumTasksClause *C) { |
| 2119 | Visitor->AddStmt(C->getNumTasks()); |
| 2120 | } |
| 2121 | |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 2122 | void OMPClauseEnqueue::VisitOMPHintClause(const OMPHintClause *C) { |
| 2123 | Visitor->AddStmt(C->getHint()); |
| 2124 | } |
| 2125 | |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2126 | template<typename T> |
| 2127 | void OMPClauseEnqueue::VisitOMPClauseList(T *Node) { |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2128 | for (const auto *I : Node->varlists()) { |
Aaron Ballman | 2205d2a | 2014-03-14 15:55:35 +0000 | [diff] [blame] | 2129 | Visitor->AddStmt(I); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2130 | } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2131 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2132 | |
| 2133 | void OMPClauseEnqueue::VisitOMPPrivateClause(const OMPPrivateClause *C) { |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2134 | VisitOMPClauseList(C); |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 2135 | for (const auto *E : C->private_copies()) { |
| 2136 | Visitor->AddStmt(E); |
| 2137 | } |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2138 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2139 | void OMPClauseEnqueue::VisitOMPFirstprivateClause( |
| 2140 | const OMPFirstprivateClause *C) { |
| 2141 | VisitOMPClauseList(C); |
Alexey Bataev | 417089f | 2016-02-17 13:19:37 +0000 | [diff] [blame] | 2142 | VisitOMPClauseWithPreInit(C); |
| 2143 | for (const auto *E : C->private_copies()) { |
| 2144 | Visitor->AddStmt(E); |
| 2145 | } |
| 2146 | for (const auto *E : C->inits()) { |
| 2147 | Visitor->AddStmt(E); |
| 2148 | } |
Alexey Bataev | d5af8e4 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 2149 | } |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2150 | void OMPClauseEnqueue::VisitOMPLastprivateClause( |
| 2151 | const OMPLastprivateClause *C) { |
| 2152 | VisitOMPClauseList(C); |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 2153 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2154 | for (auto *E : C->private_copies()) { |
| 2155 | Visitor->AddStmt(E); |
| 2156 | } |
| 2157 | for (auto *E : C->source_exprs()) { |
| 2158 | Visitor->AddStmt(E); |
| 2159 | } |
| 2160 | for (auto *E : C->destination_exprs()) { |
| 2161 | Visitor->AddStmt(E); |
| 2162 | } |
| 2163 | for (auto *E : C->assignment_ops()) { |
| 2164 | Visitor->AddStmt(E); |
| 2165 | } |
Alexander Musman | 1bb328c | 2014-06-04 13:06:39 +0000 | [diff] [blame] | 2166 | } |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2167 | void OMPClauseEnqueue::VisitOMPSharedClause(const OMPSharedClause *C) { |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2168 | VisitOMPClauseList(C); |
Alexey Bataev | 758e55e | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 2169 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2170 | void OMPClauseEnqueue::VisitOMPReductionClause(const OMPReductionClause *C) { |
| 2171 | VisitOMPClauseList(C); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2172 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 2173 | for (auto *E : C->privates()) { |
| 2174 | Visitor->AddStmt(E); |
| 2175 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2176 | for (auto *E : C->lhs_exprs()) { |
| 2177 | Visitor->AddStmt(E); |
| 2178 | } |
| 2179 | for (auto *E : C->rhs_exprs()) { |
| 2180 | Visitor->AddStmt(E); |
| 2181 | } |
| 2182 | for (auto *E : C->reduction_ops()) { |
| 2183 | Visitor->AddStmt(E); |
| 2184 | } |
Alexey Bataev | c5e0258 | 2014-06-16 07:08:35 +0000 | [diff] [blame] | 2185 | } |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2186 | void OMPClauseEnqueue::VisitOMPLinearClause(const OMPLinearClause *C) { |
| 2187 | VisitOMPClauseList(C); |
Alexey Bataev | 78849fb | 2016-03-09 09:49:00 +0000 | [diff] [blame] | 2188 | VisitOMPClauseWithPostUpdate(C); |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 2189 | for (const auto *E : C->privates()) { |
| 2190 | Visitor->AddStmt(E); |
| 2191 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 2192 | for (const auto *E : C->inits()) { |
| 2193 | Visitor->AddStmt(E); |
| 2194 | } |
| 2195 | for (const auto *E : C->updates()) { |
| 2196 | Visitor->AddStmt(E); |
| 2197 | } |
| 2198 | for (const auto *E : C->finals()) { |
| 2199 | Visitor->AddStmt(E); |
| 2200 | } |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2201 | Visitor->AddStmt(C->getStep()); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 2202 | Visitor->AddStmt(C->getCalcStep()); |
Alexander Musman | 8dba664 | 2014-04-22 13:09:42 +0000 | [diff] [blame] | 2203 | } |
Alexander Musman | f0d76e7 | 2014-05-29 14:36:25 +0000 | [diff] [blame] | 2204 | void OMPClauseEnqueue::VisitOMPAlignedClause(const OMPAlignedClause *C) { |
| 2205 | VisitOMPClauseList(C); |
| 2206 | Visitor->AddStmt(C->getAlignment()); |
| 2207 | } |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 2208 | void OMPClauseEnqueue::VisitOMPCopyinClause(const OMPCopyinClause *C) { |
| 2209 | VisitOMPClauseList(C); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 2210 | for (auto *E : C->source_exprs()) { |
| 2211 | Visitor->AddStmt(E); |
| 2212 | } |
| 2213 | for (auto *E : C->destination_exprs()) { |
| 2214 | Visitor->AddStmt(E); |
| 2215 | } |
| 2216 | for (auto *E : C->assignment_ops()) { |
| 2217 | Visitor->AddStmt(E); |
| 2218 | } |
Alexey Bataev | d48bcd8 | 2014-03-31 03:36:38 +0000 | [diff] [blame] | 2219 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2220 | void |
| 2221 | OMPClauseEnqueue::VisitOMPCopyprivateClause(const OMPCopyprivateClause *C) { |
| 2222 | VisitOMPClauseList(C); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2223 | for (auto *E : C->source_exprs()) { |
| 2224 | Visitor->AddStmt(E); |
| 2225 | } |
| 2226 | for (auto *E : C->destination_exprs()) { |
| 2227 | Visitor->AddStmt(E); |
| 2228 | } |
| 2229 | for (auto *E : C->assignment_ops()) { |
| 2230 | Visitor->AddStmt(E); |
| 2231 | } |
Alexey Bataev | bae9a79 | 2014-06-27 10:37:06 +0000 | [diff] [blame] | 2232 | } |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2233 | void OMPClauseEnqueue::VisitOMPFlushClause(const OMPFlushClause *C) { |
| 2234 | VisitOMPClauseList(C); |
| 2235 | } |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 2236 | void OMPClauseEnqueue::VisitOMPDependClause(const OMPDependClause *C) { |
| 2237 | VisitOMPClauseList(C); |
| 2238 | } |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 2239 | void OMPClauseEnqueue::VisitOMPMapClause(const OMPMapClause *C) { |
| 2240 | VisitOMPClauseList(C); |
| 2241 | } |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2242 | void OMPClauseEnqueue::VisitOMPDistScheduleClause( |
| 2243 | const OMPDistScheduleClause *C) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2244 | VisitOMPClauseWithPreInit(C); |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2245 | Visitor->AddStmt(C->getChunkSize()); |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2246 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2247 | void OMPClauseEnqueue::VisitOMPDefaultmapClause( |
| 2248 | const OMPDefaultmapClause * /*C*/) {} |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame^] | 2249 | void OMPClauseEnqueue::VisitOMPToClause(const OMPToClause *C) { |
| 2250 | VisitOMPClauseList(C); |
| 2251 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2252 | } |
Alexey Bataev | 756c196 | 2013-09-24 03:17:45 +0000 | [diff] [blame] | 2253 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2254 | void EnqueueVisitor::EnqueueChildren(const OMPClause *S) { |
| 2255 | unsigned size = WL.size(); |
| 2256 | OMPClauseEnqueue Visitor(this); |
| 2257 | Visitor.Visit(S); |
| 2258 | if (size == WL.size()) |
| 2259 | return; |
| 2260 | // Now reverse the entries we just added. This will match the DFS |
| 2261 | // ordering performed by the worklist. |
| 2262 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2263 | std::reverse(I, E); |
| 2264 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2265 | void EnqueueVisitor::VisitAddrLabelExpr(const AddrLabelExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2266 | WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent)); |
| 2267 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2268 | void EnqueueVisitor::VisitBlockExpr(const BlockExpr *B) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2269 | AddDecl(B->getBlockDecl()); |
| 2270 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2271 | void EnqueueVisitor::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2272 | EnqueueChildren(E); |
| 2273 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2274 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2275 | void EnqueueVisitor::VisitCompoundStmt(const CompoundStmt *S) { |
Pete Cooper | 57d3f14 | 2015-07-30 17:22:52 +0000 | [diff] [blame] | 2276 | for (auto &I : llvm::reverse(S->body())) |
| 2277 | AddStmt(I); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2278 | } |
| 2279 | void EnqueueVisitor:: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2280 | VisitMSDependentExistsStmt(const MSDependentExistsStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2281 | AddStmt(S->getSubStmt()); |
| 2282 | AddDeclarationNameInfo(S); |
| 2283 | if (NestedNameSpecifierLoc QualifierLoc = S->getQualifierLoc()) |
| 2284 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2285 | } |
| 2286 | |
| 2287 | void EnqueueVisitor:: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2288 | VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2289 | if (E->hasExplicitTemplateArgs()) |
| 2290 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2291 | AddDeclarationNameInfo(E); |
| 2292 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 2293 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2294 | if (!E->isImplicitAccess()) |
| 2295 | AddStmt(E->getBase()); |
| 2296 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2297 | void EnqueueVisitor::VisitCXXNewExpr(const CXXNewExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2298 | // Enqueue the initializer , if any. |
| 2299 | AddStmt(E->getInitializer()); |
| 2300 | // Enqueue the array size, if any. |
| 2301 | AddStmt(E->getArraySize()); |
| 2302 | // Enqueue the allocated type. |
| 2303 | AddTypeLoc(E->getAllocatedTypeSourceInfo()); |
| 2304 | // Enqueue the placement arguments. |
| 2305 | for (unsigned I = E->getNumPlacementArgs(); I > 0; --I) |
| 2306 | AddStmt(E->getPlacementArg(I-1)); |
| 2307 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2308 | void EnqueueVisitor::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *CE) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2309 | for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I) |
| 2310 | AddStmt(CE->getArg(I-1)); |
| 2311 | AddStmt(CE->getCallee()); |
| 2312 | AddStmt(CE->getArg(0)); |
| 2313 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2314 | void EnqueueVisitor::VisitCXXPseudoDestructorExpr( |
| 2315 | const CXXPseudoDestructorExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2316 | // Visit the name of the type being destroyed. |
| 2317 | AddTypeLoc(E->getDestroyedTypeInfo()); |
| 2318 | // Visit the scope type that looks disturbingly like the nested-name-specifier |
| 2319 | // but isn't. |
| 2320 | AddTypeLoc(E->getScopeTypeInfo()); |
| 2321 | // Visit the nested-name-specifier. |
| 2322 | if (NestedNameSpecifierLoc QualifierLoc = E->getQualifierLoc()) |
| 2323 | AddNestedNameSpecifierLoc(QualifierLoc); |
| 2324 | // Visit base expression. |
| 2325 | AddStmt(E->getBase()); |
| 2326 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2327 | void EnqueueVisitor::VisitCXXScalarValueInitExpr( |
| 2328 | const CXXScalarValueInitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2329 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2330 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2331 | void EnqueueVisitor::VisitCXXTemporaryObjectExpr( |
| 2332 | const CXXTemporaryObjectExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2333 | EnqueueChildren(E); |
| 2334 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2335 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2336 | void EnqueueVisitor::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2337 | EnqueueChildren(E); |
| 2338 | if (E->isTypeOperand()) |
| 2339 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 2340 | } |
| 2341 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2342 | void EnqueueVisitor::VisitCXXUnresolvedConstructExpr( |
| 2343 | const CXXUnresolvedConstructExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2344 | EnqueueChildren(E); |
| 2345 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2346 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2347 | void EnqueueVisitor::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2348 | EnqueueChildren(E); |
| 2349 | if (E->isTypeOperand()) |
| 2350 | AddTypeLoc(E->getTypeOperandSourceInfo()); |
| 2351 | } |
| 2352 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2353 | void EnqueueVisitor::VisitCXXCatchStmt(const CXXCatchStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2354 | EnqueueChildren(S); |
| 2355 | AddDecl(S->getExceptionDecl()); |
| 2356 | } |
| 2357 | |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2358 | void EnqueueVisitor::VisitCXXForRangeStmt(const CXXForRangeStmt *S) { |
Argyrios Kyrtzidis | cde7069 | 2014-11-13 09:50:19 +0000 | [diff] [blame] | 2359 | AddStmt(S->getBody()); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2360 | AddStmt(S->getRangeInit()); |
Argyrios Kyrtzidis | cde7069 | 2014-11-13 09:50:19 +0000 | [diff] [blame] | 2361 | AddDecl(S->getLoopVariable()); |
Argyrios Kyrtzidis | 9989124 | 2014-11-13 09:03:21 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2364 | void EnqueueVisitor::VisitDeclRefExpr(const DeclRefExpr *DR) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2365 | if (DR->hasExplicitTemplateArgs()) |
| 2366 | AddExplicitTemplateArgs(DR->getTemplateArgs(), DR->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2367 | WL.push_back(DeclRefExprParts(DR, Parent)); |
| 2368 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2369 | void EnqueueVisitor::VisitDependentScopeDeclRefExpr( |
| 2370 | const DependentScopeDeclRefExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2371 | if (E->hasExplicitTemplateArgs()) |
| 2372 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2373 | AddDeclarationNameInfo(E); |
| 2374 | AddNestedNameSpecifierLoc(E->getQualifierLoc()); |
| 2375 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2376 | void EnqueueVisitor::VisitDeclStmt(const DeclStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2377 | unsigned size = WL.size(); |
| 2378 | bool isFirst = true; |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 2379 | for (const auto *D : S->decls()) { |
| 2380 | AddDecl(D, isFirst); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2381 | isFirst = false; |
| 2382 | } |
| 2383 | if (size == WL.size()) |
| 2384 | return; |
| 2385 | // Now reverse the entries we just added. This will match the DFS |
| 2386 | // ordering performed by the worklist. |
| 2387 | VisitorWorkList::iterator I = WL.begin() + size, E = WL.end(); |
| 2388 | std::reverse(I, E); |
| 2389 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2390 | void EnqueueVisitor::VisitDesignatedInitExpr(const DesignatedInitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2391 | AddStmt(E->getInit()); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2392 | for (DesignatedInitExpr::const_reverse_designators_iterator |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2393 | D = E->designators_rbegin(), DEnd = E->designators_rend(); |
| 2394 | D != DEnd; ++D) { |
| 2395 | if (D->isFieldDesignator()) { |
| 2396 | if (FieldDecl *Field = D->getField()) |
| 2397 | AddMemberRef(Field, D->getFieldLoc()); |
| 2398 | continue; |
| 2399 | } |
| 2400 | if (D->isArrayDesignator()) { |
| 2401 | AddStmt(E->getArrayIndex(*D)); |
| 2402 | continue; |
| 2403 | } |
| 2404 | assert(D->isArrayRangeDesignator() && "Unknown designator kind"); |
| 2405 | AddStmt(E->getArrayRangeEnd(*D)); |
| 2406 | AddStmt(E->getArrayRangeStart(*D)); |
| 2407 | } |
| 2408 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2409 | void EnqueueVisitor::VisitExplicitCastExpr(const ExplicitCastExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2410 | EnqueueChildren(E); |
| 2411 | AddTypeLoc(E->getTypeInfoAsWritten()); |
| 2412 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2413 | void EnqueueVisitor::VisitForStmt(const ForStmt *FS) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2414 | AddStmt(FS->getBody()); |
| 2415 | AddStmt(FS->getInc()); |
| 2416 | AddStmt(FS->getCond()); |
| 2417 | AddDecl(FS->getConditionVariable()); |
| 2418 | AddStmt(FS->getInit()); |
| 2419 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2420 | void EnqueueVisitor::VisitGotoStmt(const GotoStmt *GS) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2421 | WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent)); |
| 2422 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2423 | void EnqueueVisitor::VisitIfStmt(const IfStmt *If) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2424 | AddStmt(If->getElse()); |
| 2425 | AddStmt(If->getThen()); |
| 2426 | AddStmt(If->getCond()); |
| 2427 | AddDecl(If->getConditionVariable()); |
| 2428 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2429 | void EnqueueVisitor::VisitInitListExpr(const InitListExpr *IE) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2430 | // We care about the syntactic form of the initializer list, only. |
| 2431 | if (InitListExpr *Syntactic = IE->getSyntacticForm()) |
| 2432 | IE = Syntactic; |
| 2433 | EnqueueChildren(IE); |
| 2434 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2435 | void EnqueueVisitor::VisitMemberExpr(const MemberExpr *M) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2436 | WL.push_back(MemberExprParts(M, Parent)); |
| 2437 | |
| 2438 | // If the base of the member access expression is an implicit 'this', don't |
| 2439 | // visit it. |
| 2440 | // FIXME: If we ever want to show these implicit accesses, this will be |
| 2441 | // unfortunate. However, clang_getCursor() relies on this behavior. |
Argyrios Kyrtzidis | 58d0e7a | 2015-03-13 04:40:07 +0000 | [diff] [blame] | 2442 | if (M->isImplicitAccess()) |
| 2443 | return; |
| 2444 | |
| 2445 | // Ignore base anonymous struct/union fields, otherwise they will shadow the |
| 2446 | // real field that that we are interested in. |
| 2447 | if (auto *SubME = dyn_cast<MemberExpr>(M->getBase())) { |
| 2448 | if (auto *FD = dyn_cast_or_null<FieldDecl>(SubME->getMemberDecl())) { |
| 2449 | if (FD->isAnonymousStructOrUnion()) { |
| 2450 | AddStmt(SubME->getBase()); |
| 2451 | return; |
| 2452 | } |
| 2453 | } |
| 2454 | } |
| 2455 | |
| 2456 | AddStmt(M->getBase()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2457 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2458 | void EnqueueVisitor::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2459 | AddTypeLoc(E->getEncodedTypeSourceInfo()); |
| 2460 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2461 | void EnqueueVisitor::VisitObjCMessageExpr(const ObjCMessageExpr *M) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2462 | EnqueueChildren(M); |
| 2463 | AddTypeLoc(M->getClassReceiverTypeInfo()); |
| 2464 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2465 | void EnqueueVisitor::VisitOffsetOfExpr(const OffsetOfExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2466 | // Visit the components of the offsetof expression. |
| 2467 | for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2468 | const OffsetOfNode &Node = E->getComponent(I-1); |
| 2469 | switch (Node.getKind()) { |
| 2470 | case OffsetOfNode::Array: |
| 2471 | AddStmt(E->getIndexExpr(Node.getArrayExprIndex())); |
| 2472 | break; |
| 2473 | case OffsetOfNode::Field: |
| 2474 | AddMemberRef(Node.getField(), Node.getSourceRange().getEnd()); |
| 2475 | break; |
| 2476 | case OffsetOfNode::Identifier: |
| 2477 | case OffsetOfNode::Base: |
| 2478 | continue; |
| 2479 | } |
| 2480 | } |
| 2481 | // Visit the type into which we're computing the offset. |
| 2482 | AddTypeLoc(E->getTypeSourceInfo()); |
| 2483 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2484 | void EnqueueVisitor::VisitOverloadExpr(const OverloadExpr *E) { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2485 | if (E->hasExplicitTemplateArgs()) |
| 2486 | AddExplicitTemplateArgs(E->getTemplateArgs(), E->getNumTemplateArgs()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2487 | WL.push_back(OverloadExprParts(E, Parent)); |
| 2488 | } |
| 2489 | void EnqueueVisitor::VisitUnaryExprOrTypeTraitExpr( |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2490 | const UnaryExprOrTypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2491 | EnqueueChildren(E); |
| 2492 | if (E->isArgumentType()) |
| 2493 | AddTypeLoc(E->getArgumentTypeInfo()); |
| 2494 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2495 | void EnqueueVisitor::VisitStmt(const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2496 | EnqueueChildren(S); |
| 2497 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2498 | void EnqueueVisitor::VisitSwitchStmt(const SwitchStmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2499 | AddStmt(S->getBody()); |
| 2500 | AddStmt(S->getCond()); |
| 2501 | AddDecl(S->getConditionVariable()); |
| 2502 | } |
| 2503 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2504 | void EnqueueVisitor::VisitWhileStmt(const WhileStmt *W) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2505 | AddStmt(W->getBody()); |
| 2506 | AddStmt(W->getCond()); |
| 2507 | AddDecl(W->getConditionVariable()); |
| 2508 | } |
| 2509 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2510 | void EnqueueVisitor::VisitTypeTraitExpr(const TypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2511 | for (unsigned I = E->getNumArgs(); I > 0; --I) |
| 2512 | AddTypeLoc(E->getArg(I-1)); |
| 2513 | } |
| 2514 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2515 | void EnqueueVisitor::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2516 | AddTypeLoc(E->getQueriedTypeSourceInfo()); |
| 2517 | } |
| 2518 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2519 | void EnqueueVisitor::VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2520 | EnqueueChildren(E); |
| 2521 | } |
| 2522 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2523 | void EnqueueVisitor::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *U) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2524 | VisitOverloadExpr(U); |
| 2525 | if (!U->isImplicitAccess()) |
| 2526 | AddStmt(U->getBase()); |
| 2527 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2528 | void EnqueueVisitor::VisitVAArgExpr(const VAArgExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2529 | AddStmt(E->getSubExpr()); |
| 2530 | AddTypeLoc(E->getWrittenTypeInfo()); |
| 2531 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2532 | void EnqueueVisitor::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2533 | WL.push_back(SizeOfPackExprParts(E, Parent)); |
| 2534 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2535 | void EnqueueVisitor::VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2536 | // If the opaque value has a source expression, just transparently |
| 2537 | // visit that. This is useful for (e.g.) pseudo-object expressions. |
| 2538 | if (Expr *SourceExpr = E->getSourceExpr()) |
| 2539 | return Visit(SourceExpr); |
| 2540 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2541 | void EnqueueVisitor::VisitLambdaExpr(const LambdaExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2542 | AddStmt(E->getBody()); |
| 2543 | WL.push_back(LambdaExprParts(E, Parent)); |
| 2544 | } |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2545 | void EnqueueVisitor::VisitPseudoObjectExpr(const PseudoObjectExpr *E) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2546 | // Treat the expression like its syntactic form. |
| 2547 | Visit(E->getSyntacticForm()); |
| 2548 | } |
| 2549 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2550 | void EnqueueVisitor::VisitOMPExecutableDirective( |
| 2551 | const OMPExecutableDirective *D) { |
| 2552 | EnqueueChildren(D); |
| 2553 | for (ArrayRef<OMPClause *>::iterator I = D->clauses().begin(), |
| 2554 | E = D->clauses().end(); |
| 2555 | I != E; ++I) |
| 2556 | EnqueueChildren(*I); |
| 2557 | } |
| 2558 | |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2559 | void EnqueueVisitor::VisitOMPLoopDirective(const OMPLoopDirective *D) { |
| 2560 | VisitOMPExecutableDirective(D); |
| 2561 | } |
| 2562 | |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 2563 | void EnqueueVisitor::VisitOMPParallelDirective(const OMPParallelDirective *D) { |
| 2564 | VisitOMPExecutableDirective(D); |
| 2565 | } |
| 2566 | |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2567 | void EnqueueVisitor::VisitOMPSimdDirective(const OMPSimdDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2568 | VisitOMPLoopDirective(D); |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2571 | void EnqueueVisitor::VisitOMPForDirective(const OMPForDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2572 | VisitOMPLoopDirective(D); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2573 | } |
| 2574 | |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2575 | void EnqueueVisitor::VisitOMPForSimdDirective(const OMPForSimdDirective *D) { |
| 2576 | VisitOMPLoopDirective(D); |
| 2577 | } |
| 2578 | |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2579 | void EnqueueVisitor::VisitOMPSectionsDirective(const OMPSectionsDirective *D) { |
| 2580 | VisitOMPExecutableDirective(D); |
| 2581 | } |
| 2582 | |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2583 | void EnqueueVisitor::VisitOMPSectionDirective(const OMPSectionDirective *D) { |
| 2584 | VisitOMPExecutableDirective(D); |
| 2585 | } |
| 2586 | |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 2587 | void EnqueueVisitor::VisitOMPSingleDirective(const OMPSingleDirective *D) { |
| 2588 | VisitOMPExecutableDirective(D); |
| 2589 | } |
| 2590 | |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2591 | void EnqueueVisitor::VisitOMPMasterDirective(const OMPMasterDirective *D) { |
| 2592 | VisitOMPExecutableDirective(D); |
| 2593 | } |
| 2594 | |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2595 | void EnqueueVisitor::VisitOMPCriticalDirective(const OMPCriticalDirective *D) { |
| 2596 | VisitOMPExecutableDirective(D); |
| 2597 | AddDeclarationNameInfo(D); |
| 2598 | } |
| 2599 | |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2600 | void |
| 2601 | EnqueueVisitor::VisitOMPParallelForDirective(const OMPParallelForDirective *D) { |
Alexander Musman | 3aaab66 | 2014-08-19 11:27:13 +0000 | [diff] [blame] | 2602 | VisitOMPLoopDirective(D); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2605 | void EnqueueVisitor::VisitOMPParallelForSimdDirective( |
| 2606 | const OMPParallelForSimdDirective *D) { |
| 2607 | VisitOMPLoopDirective(D); |
| 2608 | } |
| 2609 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2610 | void EnqueueVisitor::VisitOMPParallelSectionsDirective( |
| 2611 | const OMPParallelSectionsDirective *D) { |
| 2612 | VisitOMPExecutableDirective(D); |
| 2613 | } |
| 2614 | |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2615 | void EnqueueVisitor::VisitOMPTaskDirective(const OMPTaskDirective *D) { |
| 2616 | VisitOMPExecutableDirective(D); |
| 2617 | } |
| 2618 | |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2619 | void |
| 2620 | EnqueueVisitor::VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *D) { |
| 2621 | VisitOMPExecutableDirective(D); |
| 2622 | } |
| 2623 | |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 2624 | void EnqueueVisitor::VisitOMPBarrierDirective(const OMPBarrierDirective *D) { |
| 2625 | VisitOMPExecutableDirective(D); |
| 2626 | } |
| 2627 | |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 2628 | void EnqueueVisitor::VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D) { |
| 2629 | VisitOMPExecutableDirective(D); |
| 2630 | } |
| 2631 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2632 | void EnqueueVisitor::VisitOMPTaskgroupDirective( |
| 2633 | const OMPTaskgroupDirective *D) { |
| 2634 | VisitOMPExecutableDirective(D); |
| 2635 | } |
| 2636 | |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2637 | void EnqueueVisitor::VisitOMPFlushDirective(const OMPFlushDirective *D) { |
| 2638 | VisitOMPExecutableDirective(D); |
| 2639 | } |
| 2640 | |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2641 | void EnqueueVisitor::VisitOMPOrderedDirective(const OMPOrderedDirective *D) { |
| 2642 | VisitOMPExecutableDirective(D); |
| 2643 | } |
| 2644 | |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2645 | void EnqueueVisitor::VisitOMPAtomicDirective(const OMPAtomicDirective *D) { |
| 2646 | VisitOMPExecutableDirective(D); |
| 2647 | } |
| 2648 | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 2649 | void EnqueueVisitor::VisitOMPTargetDirective(const OMPTargetDirective *D) { |
| 2650 | VisitOMPExecutableDirective(D); |
| 2651 | } |
| 2652 | |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2653 | void EnqueueVisitor::VisitOMPTargetDataDirective(const |
| 2654 | OMPTargetDataDirective *D) { |
| 2655 | VisitOMPExecutableDirective(D); |
| 2656 | } |
| 2657 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 2658 | void EnqueueVisitor::VisitOMPTargetEnterDataDirective( |
| 2659 | const OMPTargetEnterDataDirective *D) { |
| 2660 | VisitOMPExecutableDirective(D); |
| 2661 | } |
| 2662 | |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 2663 | void EnqueueVisitor::VisitOMPTargetExitDataDirective( |
| 2664 | const OMPTargetExitDataDirective *D) { |
| 2665 | VisitOMPExecutableDirective(D); |
| 2666 | } |
| 2667 | |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 2668 | void EnqueueVisitor::VisitOMPTargetParallelDirective( |
| 2669 | const OMPTargetParallelDirective *D) { |
| 2670 | VisitOMPExecutableDirective(D); |
| 2671 | } |
| 2672 | |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 2673 | void EnqueueVisitor::VisitOMPTargetParallelForDirective( |
| 2674 | const OMPTargetParallelForDirective *D) { |
| 2675 | VisitOMPLoopDirective(D); |
| 2676 | } |
| 2677 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2678 | void EnqueueVisitor::VisitOMPTeamsDirective(const OMPTeamsDirective *D) { |
| 2679 | VisitOMPExecutableDirective(D); |
| 2680 | } |
| 2681 | |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2682 | void EnqueueVisitor::VisitOMPCancellationPointDirective( |
| 2683 | const OMPCancellationPointDirective *D) { |
| 2684 | VisitOMPExecutableDirective(D); |
| 2685 | } |
| 2686 | |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2687 | void EnqueueVisitor::VisitOMPCancelDirective(const OMPCancelDirective *D) { |
| 2688 | VisitOMPExecutableDirective(D); |
| 2689 | } |
| 2690 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 2691 | void EnqueueVisitor::VisitOMPTaskLoopDirective(const OMPTaskLoopDirective *D) { |
| 2692 | VisitOMPLoopDirective(D); |
| 2693 | } |
| 2694 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 2695 | void EnqueueVisitor::VisitOMPTaskLoopSimdDirective( |
| 2696 | const OMPTaskLoopSimdDirective *D) { |
| 2697 | VisitOMPLoopDirective(D); |
| 2698 | } |
| 2699 | |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2700 | void EnqueueVisitor::VisitOMPDistributeDirective( |
| 2701 | const OMPDistributeDirective *D) { |
| 2702 | VisitOMPLoopDirective(D); |
| 2703 | } |
| 2704 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2705 | void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, const Stmt *S) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2706 | EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S); |
| 2707 | } |
| 2708 | |
| 2709 | bool CursorVisitor::IsInRegionOfInterest(CXCursor C) { |
| 2710 | if (RegionOfInterest.isValid()) { |
| 2711 | SourceRange Range = getRawCursorExtent(C); |
| 2712 | if (Range.isInvalid() || CompareRegionOfInterest(Range)) |
| 2713 | return false; |
| 2714 | } |
| 2715 | return true; |
| 2716 | } |
| 2717 | |
| 2718 | bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { |
| 2719 | while (!WL.empty()) { |
| 2720 | // Dequeue the worklist item. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2721 | VisitorJob LI = WL.pop_back_val(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2722 | |
| 2723 | // Set the Parent field, then back to its old value once we're done. |
| 2724 | SetParentRAII SetParent(Parent, StmtParent, LI.getParent()); |
| 2725 | |
| 2726 | switch (LI.getKind()) { |
| 2727 | case VisitorJob::DeclVisitKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2728 | const Decl *D = cast<DeclVisit>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2729 | if (!D) |
| 2730 | continue; |
| 2731 | |
| 2732 | // For now, perform default visitation for Decls. |
| 2733 | if (Visit(MakeCXCursor(D, TU, RegionOfInterest, |
| 2734 | cast<DeclVisit>(&LI)->isFirst()))) |
| 2735 | return true; |
| 2736 | |
| 2737 | continue; |
| 2738 | } |
| 2739 | case VisitorJob::ExplicitTemplateArgsVisitKind: { |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2740 | for (const TemplateArgumentLoc &Arg : |
| 2741 | *cast<ExplicitTemplateArgsVisit>(&LI)) { |
| 2742 | if (VisitTemplateArgumentLoc(Arg)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2743 | return true; |
| 2744 | } |
| 2745 | continue; |
| 2746 | } |
| 2747 | case VisitorJob::TypeLocVisitKind: { |
| 2748 | // Perform default visitation for TypeLocs. |
| 2749 | if (Visit(cast<TypeLocVisit>(&LI)->get())) |
| 2750 | return true; |
| 2751 | continue; |
| 2752 | } |
| 2753 | case VisitorJob::LabelRefVisitKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2754 | const LabelDecl *LS = cast<LabelRefVisit>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2755 | if (LabelStmt *stmt = LS->getStmt()) { |
| 2756 | if (Visit(MakeCursorLabelRef(stmt, cast<LabelRefVisit>(&LI)->getLoc(), |
| 2757 | TU))) { |
| 2758 | return true; |
| 2759 | } |
| 2760 | } |
| 2761 | continue; |
| 2762 | } |
| 2763 | |
| 2764 | case VisitorJob::NestedNameSpecifierLocVisitKind: { |
| 2765 | NestedNameSpecifierLocVisit *V = cast<NestedNameSpecifierLocVisit>(&LI); |
| 2766 | if (VisitNestedNameSpecifierLoc(V->get())) |
| 2767 | return true; |
| 2768 | continue; |
| 2769 | } |
| 2770 | |
| 2771 | case VisitorJob::DeclarationNameInfoVisitKind: { |
| 2772 | if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI) |
| 2773 | ->get())) |
| 2774 | return true; |
| 2775 | continue; |
| 2776 | } |
| 2777 | case VisitorJob::MemberRefVisitKind: { |
| 2778 | MemberRefVisit *V = cast<MemberRefVisit>(&LI); |
| 2779 | if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU))) |
| 2780 | return true; |
| 2781 | continue; |
| 2782 | } |
| 2783 | case VisitorJob::StmtVisitKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2784 | const Stmt *S = cast<StmtVisit>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2785 | if (!S) |
| 2786 | continue; |
| 2787 | |
| 2788 | // Update the current cursor. |
| 2789 | CXCursor Cursor = MakeCXCursor(S, StmtParent, TU, RegionOfInterest); |
| 2790 | if (!IsInRegionOfInterest(Cursor)) |
| 2791 | continue; |
| 2792 | switch (Visitor(Cursor, Parent, ClientData)) { |
| 2793 | case CXChildVisit_Break: return true; |
| 2794 | case CXChildVisit_Continue: break; |
| 2795 | case CXChildVisit_Recurse: |
| 2796 | if (PostChildrenVisitor) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 2797 | WL.push_back(PostChildrenVisit(nullptr, Cursor)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2798 | EnqueueWorkList(WL, S); |
| 2799 | break; |
| 2800 | } |
| 2801 | continue; |
| 2802 | } |
| 2803 | case VisitorJob::MemberExprPartsKind: { |
| 2804 | // Handle the other pieces in the MemberExpr besides the base. |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2805 | const MemberExpr *M = cast<MemberExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2806 | |
| 2807 | // Visit the nested-name-specifier |
| 2808 | if (NestedNameSpecifierLoc QualifierLoc = M->getQualifierLoc()) |
| 2809 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 2810 | return true; |
| 2811 | |
| 2812 | // Visit the declaration name. |
| 2813 | if (VisitDeclarationNameInfo(M->getMemberNameInfo())) |
| 2814 | return true; |
| 2815 | |
| 2816 | // Visit the explicitly-specified template arguments, if any. |
| 2817 | if (M->hasExplicitTemplateArgs()) { |
| 2818 | for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(), |
| 2819 | *ArgEnd = Arg + M->getNumTemplateArgs(); |
| 2820 | Arg != ArgEnd; ++Arg) { |
| 2821 | if (VisitTemplateArgumentLoc(*Arg)) |
| 2822 | return true; |
| 2823 | } |
| 2824 | } |
| 2825 | continue; |
| 2826 | } |
| 2827 | case VisitorJob::DeclRefExprPartsKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2828 | const DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2829 | // Visit nested-name-specifier, if present. |
| 2830 | if (NestedNameSpecifierLoc QualifierLoc = DR->getQualifierLoc()) |
| 2831 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 2832 | return true; |
| 2833 | // Visit declaration name. |
| 2834 | if (VisitDeclarationNameInfo(DR->getNameInfo())) |
| 2835 | return true; |
| 2836 | continue; |
| 2837 | } |
| 2838 | case VisitorJob::OverloadExprPartsKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2839 | const OverloadExpr *O = cast<OverloadExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2840 | // Visit the nested-name-specifier. |
| 2841 | if (NestedNameSpecifierLoc QualifierLoc = O->getQualifierLoc()) |
| 2842 | if (VisitNestedNameSpecifierLoc(QualifierLoc)) |
| 2843 | return true; |
| 2844 | // Visit the declaration name. |
| 2845 | if (VisitDeclarationNameInfo(O->getNameInfo())) |
| 2846 | return true; |
| 2847 | // Visit the overloaded declaration reference. |
| 2848 | if (Visit(MakeCursorOverloadedDeclRef(O, TU))) |
| 2849 | return true; |
| 2850 | continue; |
| 2851 | } |
| 2852 | case VisitorJob::SizeOfPackExprPartsKind: { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2853 | const SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2854 | NamedDecl *Pack = E->getPack(); |
| 2855 | if (isa<TemplateTypeParmDecl>(Pack)) { |
| 2856 | if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack), |
| 2857 | E->getPackLoc(), TU))) |
| 2858 | return true; |
| 2859 | |
| 2860 | continue; |
| 2861 | } |
| 2862 | |
| 2863 | if (isa<TemplateTemplateParmDecl>(Pack)) { |
| 2864 | if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack), |
| 2865 | E->getPackLoc(), TU))) |
| 2866 | return true; |
| 2867 | |
| 2868 | continue; |
| 2869 | } |
| 2870 | |
| 2871 | // Non-type template parameter packs and function parameter packs are |
| 2872 | // treated like DeclRefExpr cursors. |
| 2873 | continue; |
| 2874 | } |
| 2875 | |
| 2876 | case VisitorJob::LambdaExprPartsKind: { |
| 2877 | // Visit captures. |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2878 | const LambdaExpr *E = cast<LambdaExprParts>(&LI)->get(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2879 | for (LambdaExpr::capture_iterator C = E->explicit_capture_begin(), |
| 2880 | CEnd = E->explicit_capture_end(); |
| 2881 | C != CEnd; ++C) { |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 2882 | // FIXME: Lambda init-captures. |
| 2883 | if (!C->capturesVariable()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2884 | continue; |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 2885 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2886 | if (Visit(MakeCursorVariableRef(C->getCapturedVar(), |
| 2887 | C->getLocation(), |
| 2888 | TU))) |
| 2889 | return true; |
| 2890 | } |
| 2891 | |
| 2892 | // Visit parameters and return type, if present. |
| 2893 | if (E->hasExplicitParameters() || E->hasExplicitResultType()) { |
| 2894 | TypeLoc TL = E->getCallOperator()->getTypeSourceInfo()->getTypeLoc(); |
| 2895 | if (E->hasExplicitParameters() && E->hasExplicitResultType()) { |
| 2896 | // Visit the whole type. |
| 2897 | if (Visit(TL)) |
| 2898 | return true; |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2899 | } else if (FunctionProtoTypeLoc Proto = |
| 2900 | TL.getAs<FunctionProtoTypeLoc>()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2901 | if (E->hasExplicitParameters()) { |
| 2902 | // Visit parameters. |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2903 | for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) |
| 2904 | if (Visit(MakeCXCursor(Proto.getParam(I), TU))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2905 | return true; |
| 2906 | } else { |
| 2907 | // Visit result type. |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 2908 | if (Visit(Proto.getReturnLoc())) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2909 | return true; |
| 2910 | } |
| 2911 | } |
| 2912 | } |
| 2913 | break; |
| 2914 | } |
| 2915 | |
| 2916 | case VisitorJob::PostChildrenVisitKind: |
| 2917 | if (PostChildrenVisitor(Parent, ClientData)) |
| 2918 | return true; |
| 2919 | break; |
| 2920 | } |
| 2921 | } |
| 2922 | return false; |
| 2923 | } |
| 2924 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 2925 | bool CursorVisitor::Visit(const Stmt *S) { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 2926 | VisitorWorkList *WL = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2927 | if (!WorkListFreeList.empty()) { |
| 2928 | WL = WorkListFreeList.back(); |
| 2929 | WL->clear(); |
| 2930 | WorkListFreeList.pop_back(); |
| 2931 | } |
| 2932 | else { |
| 2933 | WL = new VisitorWorkList(); |
| 2934 | WorkListCache.push_back(WL); |
| 2935 | } |
| 2936 | EnqueueWorkList(*WL, S); |
| 2937 | bool result = RunVisitorWorkList(*WL); |
| 2938 | WorkListFreeList.push_back(WL); |
| 2939 | return result; |
| 2940 | } |
| 2941 | |
| 2942 | namespace { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2943 | typedef SmallVector<SourceRange, 4> RefNamePieces; |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2944 | RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr, |
| 2945 | const DeclarationNameInfo &NI, SourceRange QLoc, |
| 2946 | const SourceRange *TemplateArgsLoc = nullptr) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2947 | const bool WantQualifier = NameFlags & CXNameRange_WantQualifier; |
| 2948 | const bool WantTemplateArgs = NameFlags & CXNameRange_WantTemplateArgs; |
| 2949 | const bool WantSinglePiece = NameFlags & CXNameRange_WantSinglePiece; |
| 2950 | |
| 2951 | const DeclarationName::NameKind Kind = NI.getName().getNameKind(); |
| 2952 | |
| 2953 | RefNamePieces Pieces; |
| 2954 | |
| 2955 | if (WantQualifier && QLoc.isValid()) |
| 2956 | Pieces.push_back(QLoc); |
| 2957 | |
| 2958 | if (Kind != DeclarationName::CXXOperatorName || IsMemberRefExpr) |
| 2959 | Pieces.push_back(NI.getLoc()); |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2960 | |
| 2961 | if (WantTemplateArgs && TemplateArgsLoc && TemplateArgsLoc->isValid()) |
| 2962 | Pieces.push_back(*TemplateArgsLoc); |
| 2963 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2964 | if (Kind == DeclarationName::CXXOperatorName) { |
| 2965 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 2966 | NI.getInfo().CXXOperatorName.BeginOpNameLoc)); |
| 2967 | Pieces.push_back(SourceLocation::getFromRawEncoding( |
| 2968 | NI.getInfo().CXXOperatorName.EndOpNameLoc)); |
| 2969 | } |
| 2970 | |
| 2971 | if (WantSinglePiece) { |
| 2972 | SourceRange R(Pieces.front().getBegin(), Pieces.back().getEnd()); |
| 2973 | Pieces.clear(); |
| 2974 | Pieces.push_back(R); |
| 2975 | } |
| 2976 | |
| 2977 | return Pieces; |
| 2978 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2979 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2980 | |
| 2981 | //===----------------------------------------------------------------------===// |
| 2982 | // Misc. API hooks. |
| 2983 | //===----------------------------------------------------------------------===// |
| 2984 | |
Chad Rosier | 05c71aa | 2013-03-27 18:28:23 +0000 | [diff] [blame] | 2985 | static void fatal_error_handler(void *user_data, const std::string& reason, |
| 2986 | bool gen_crash_diag) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2987 | // Write the result out to stderr avoiding errs() because raw_ostreams can |
| 2988 | // call report_fatal_error. |
| 2989 | fprintf(stderr, "LIBCLANG FATAL ERROR: %s\n", reason.c_str()); |
| 2990 | ::abort(); |
| 2991 | } |
| 2992 | |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 2993 | namespace { |
| 2994 | struct RegisterFatalErrorHandler { |
| 2995 | RegisterFatalErrorHandler() { |
| 2996 | llvm::install_fatal_error_handler(fatal_error_handler, nullptr); |
| 2997 | } |
| 2998 | }; |
| 2999 | } |
| 3000 | |
| 3001 | static llvm::ManagedStatic<RegisterFatalErrorHandler> RegisterFatalErrorHandlerOnce; |
| 3002 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3003 | extern "C" { |
| 3004 | CXIndex clang_createIndex(int excludeDeclarationsFromPCH, |
| 3005 | int displayDiagnostics) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3006 | // We use crash recovery to make some of our APIs more reliable, implicitly |
| 3007 | // enable it. |
Argyrios Kyrtzidis | 3701f54 | 2013-11-27 08:58:09 +0000 | [diff] [blame] | 3008 | if (!getenv("LIBCLANG_DISABLE_CRASH_RECOVERY")) |
| 3009 | llvm::CrashRecoveryContext::Enable(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3010 | |
Chandler Carruth | 6666074 | 2014-06-27 16:37:27 +0000 | [diff] [blame] | 3011 | // Look through the managed static to trigger construction of the managed |
| 3012 | // static which registers our fatal error handler. This ensures it is only |
| 3013 | // registered once. |
| 3014 | (void)*RegisterFatalErrorHandlerOnce; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3015 | |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 3016 | // Initialize targets for clang module support. |
| 3017 | llvm::InitializeAllTargets(); |
| 3018 | llvm::InitializeAllTargetMCs(); |
| 3019 | llvm::InitializeAllAsmPrinters(); |
| 3020 | llvm::InitializeAllAsmParsers(); |
| 3021 | |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 3022 | CIndexer *CIdxr = new CIndexer(); |
| 3023 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3024 | if (excludeDeclarationsFromPCH) |
| 3025 | CIdxr->setOnlyLocalDecls(); |
| 3026 | if (displayDiagnostics) |
| 3027 | CIdxr->setDisplayDiagnostics(); |
| 3028 | |
| 3029 | if (getenv("LIBCLANG_BGPRIO_INDEX")) |
| 3030 | CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() | |
| 3031 | CXGlobalOpt_ThreadBackgroundPriorityForIndexing); |
| 3032 | if (getenv("LIBCLANG_BGPRIO_EDIT")) |
| 3033 | CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() | |
| 3034 | CXGlobalOpt_ThreadBackgroundPriorityForEditing); |
| 3035 | |
| 3036 | return CIdxr; |
| 3037 | } |
| 3038 | |
| 3039 | void clang_disposeIndex(CXIndex CIdx) { |
| 3040 | if (CIdx) |
| 3041 | delete static_cast<CIndexer *>(CIdx); |
| 3042 | } |
| 3043 | |
| 3044 | void clang_CXIndex_setGlobalOptions(CXIndex CIdx, unsigned options) { |
| 3045 | if (CIdx) |
| 3046 | static_cast<CIndexer *>(CIdx)->setCXGlobalOptFlags(options); |
| 3047 | } |
| 3048 | |
| 3049 | unsigned clang_CXIndex_getGlobalOptions(CXIndex CIdx) { |
| 3050 | if (CIdx) |
| 3051 | return static_cast<CIndexer *>(CIdx)->getCXGlobalOptFlags(); |
| 3052 | return 0; |
| 3053 | } |
| 3054 | |
| 3055 | void clang_toggleCrashRecovery(unsigned isEnabled) { |
| 3056 | if (isEnabled) |
| 3057 | llvm::CrashRecoveryContext::Enable(); |
| 3058 | else |
| 3059 | llvm::CrashRecoveryContext::Disable(); |
| 3060 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3061 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3062 | CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx, |
| 3063 | const char *ast_filename) { |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3064 | CXTranslationUnit TU; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3065 | enum CXErrorCode Result = |
| 3066 | clang_createTranslationUnit2(CIdx, ast_filename, &TU); |
Reid Kleckner | fd48fc6 | 2014-02-12 23:56:20 +0000 | [diff] [blame] | 3067 | (void)Result; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3068 | assert((TU && Result == CXError_Success) || |
| 3069 | (!TU && Result != CXError_Success)); |
| 3070 | return TU; |
| 3071 | } |
| 3072 | |
| 3073 | enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx, |
| 3074 | const char *ast_filename, |
| 3075 | CXTranslationUnit *out_TU) { |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3076 | if (out_TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3077 | *out_TU = nullptr; |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 3078 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3079 | if (!CIdx || !ast_filename || !out_TU) |
| 3080 | return CXError_InvalidArguments; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3081 | |
Argyrios Kyrtzidis | 2702101 | 2013-05-24 22:24:07 +0000 | [diff] [blame] | 3082 | LOG_FUNC_SECTION { |
| 3083 | *Log << ast_filename; |
| 3084 | } |
| 3085 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3086 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 3087 | FileSystemOptions FileSystemOpts; |
| 3088 | |
Justin Bogner | d512c1e | 2014-10-15 00:33:06 +0000 | [diff] [blame] | 3089 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags = |
| 3090 | CompilerInstance::createDiagnostics(new DiagnosticOptions()); |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 3091 | std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile( |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 3092 | ast_filename, CXXIdx->getPCHContainerOperations()->getRawReader(), Diags, |
Adrian Prantl | 6b21ab2 | 2015-08-27 19:46:20 +0000 | [diff] [blame] | 3093 | FileSystemOpts, /*UseDebugInfo=*/false, |
| 3094 | CXXIdx->getOnlyLocalDecls(), None, |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 3095 | /*CaptureDiagnostics=*/true, |
| 3096 | /*AllowPCHWithCompilerErrors=*/true, |
| 3097 | /*UserFilesAreVolatile=*/true); |
| 3098 | *out_TU = MakeCXTranslationUnit(CXXIdx, AU.release()); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3099 | return *out_TU ? CXError_Success : CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | unsigned clang_defaultEditingTranslationUnitOptions() { |
| 3103 | return CXTranslationUnit_PrecompiledPreamble | |
| 3104 | CXTranslationUnit_CacheCompletionResults; |
| 3105 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3106 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3107 | CXTranslationUnit |
| 3108 | clang_createTranslationUnitFromSourceFile(CXIndex CIdx, |
| 3109 | const char *source_filename, |
| 3110 | int num_command_line_args, |
| 3111 | const char * const *command_line_args, |
| 3112 | unsigned num_unsaved_files, |
| 3113 | struct CXUnsavedFile *unsaved_files) { |
| 3114 | unsigned Options = CXTranslationUnit_DetailedPreprocessingRecord; |
| 3115 | return clang_parseTranslationUnit(CIdx, source_filename, |
| 3116 | command_line_args, num_command_line_args, |
| 3117 | unsaved_files, num_unsaved_files, |
| 3118 | Options); |
| 3119 | } |
| 3120 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3121 | static CXErrorCode |
| 3122 | clang_parseTranslationUnit_Impl(CXIndex CIdx, const char *source_filename, |
| 3123 | const char *const *command_line_args, |
| 3124 | int num_command_line_args, |
| 3125 | ArrayRef<CXUnsavedFile> unsaved_files, |
| 3126 | unsigned options, CXTranslationUnit *out_TU) { |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3127 | // Set up the initial return values. |
| 3128 | if (out_TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3129 | *out_TU = nullptr; |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3130 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3131 | // Check arguments. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3132 | if (!CIdx || !out_TU) |
| 3133 | return CXError_InvalidArguments; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3134 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3135 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 3136 | |
| 3137 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
| 3138 | setThreadBackgroundPriority(); |
| 3139 | |
| 3140 | bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble; |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3141 | bool CreatePreambleOnFirstParse = |
| 3142 | options & CXTranslationUnit_CreatePreambleOnFirstParse; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3143 | // FIXME: Add a flag for modules. |
| 3144 | TranslationUnitKind TUKind |
| 3145 | = (options & CXTranslationUnit_Incomplete)? TU_Prefix : TU_Complete; |
Alp Toker | 8c8a875 | 2013-12-03 06:53:35 +0000 | [diff] [blame] | 3146 | bool CacheCodeCompletionResults |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3147 | = options & CXTranslationUnit_CacheCompletionResults; |
| 3148 | bool IncludeBriefCommentsInCodeCompletion |
| 3149 | = options & CXTranslationUnit_IncludeBriefCommentsInCodeCompletion; |
| 3150 | bool SkipFunctionBodies = options & CXTranslationUnit_SkipFunctionBodies; |
| 3151 | bool ForSerialization = options & CXTranslationUnit_ForSerialization; |
| 3152 | |
| 3153 | // Configure the diagnostics. |
| 3154 | IntrusiveRefCntPtr<DiagnosticsEngine> |
Sean Silva | f1b49e2 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 3155 | Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3156 | |
Manuel Klimek | 016c024 | 2016-03-01 10:56:19 +0000 | [diff] [blame] | 3157 | if (options & CXTranslationUnit_KeepGoing) |
| 3158 | Diags->setFatalsAsError(true); |
| 3159 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3160 | // Recover resources if we crash before exiting this function. |
| 3161 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 3162 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 3163 | DiagCleanup(Diags.get()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3164 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3165 | std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles( |
| 3166 | new std::vector<ASTUnit::RemappedFile>()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3167 | |
| 3168 | // Recover resources if we crash before exiting this function. |
| 3169 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 3170 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
| 3171 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3172 | for (auto &UF : unsaved_files) { |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3173 | std::unique_ptr<llvm::MemoryBuffer> MB = |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3174 | llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3175 | RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3178 | std::unique_ptr<std::vector<const char *>> Args( |
| 3179 | new std::vector<const char *>()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3180 | |
| 3181 | // Recover resources if we crash before exiting this method. |
| 3182 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > |
| 3183 | ArgsCleanup(Args.get()); |
| 3184 | |
| 3185 | // Since the Clang C library is primarily used by batch tools dealing with |
| 3186 | // (often very broken) source code, where spell-checking can have a |
| 3187 | // significant negative impact on performance (particularly when |
| 3188 | // precompiled headers are involved), we disable it by default. |
| 3189 | // Only do this if we haven't found a spell-checking-related argument. |
| 3190 | bool FoundSpellCheckingArgument = false; |
| 3191 | for (int I = 0; I != num_command_line_args; ++I) { |
| 3192 | if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 || |
| 3193 | strcmp(command_line_args[I], "-fspell-checking") == 0) { |
| 3194 | FoundSpellCheckingArgument = true; |
| 3195 | break; |
| 3196 | } |
| 3197 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3198 | Args->insert(Args->end(), command_line_args, |
| 3199 | command_line_args + num_command_line_args); |
| 3200 | |
Benjamin Kramer | c02670e | 2015-11-18 16:14:27 +0000 | [diff] [blame] | 3201 | if (!FoundSpellCheckingArgument) |
| 3202 | Args->insert(Args->begin() + 1, "-fno-spell-checking"); |
| 3203 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3204 | // The 'source_filename' argument is optional. If the caller does not |
| 3205 | // specify it then it is assumed that the source file is specified |
| 3206 | // in the actual argument list. |
| 3207 | // Put the source file after command_line_args otherwise if '-x' flag is |
| 3208 | // present it will be unused. |
| 3209 | if (source_filename) |
| 3210 | Args->push_back(source_filename); |
| 3211 | |
| 3212 | // Do we need the detailed preprocessing record? |
| 3213 | if (options & CXTranslationUnit_DetailedPreprocessingRecord) { |
| 3214 | Args->push_back("-Xclang"); |
| 3215 | Args->push_back("-detailed-preprocessing-record"); |
| 3216 | } |
| 3217 | |
| 3218 | unsigned NumErrors = Diags->getClient()->getNumErrors(); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3219 | std::unique_ptr<ASTUnit> ErrUnit; |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3220 | // Unless the user specified that they want the preamble on the first parse |
| 3221 | // set it up to be created on the first reparse. This makes the first parse |
| 3222 | // faster, trading for a slower (first) reparse. |
| 3223 | unsigned PrecompilePreambleAfterNParses = |
| 3224 | !PrecompilePreamble ? 0 : 2 - CreatePreambleOnFirstParse; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3225 | std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCommandLine( |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 3226 | Args->data(), Args->data() + Args->size(), |
| 3227 | CXXIdx->getPCHContainerOperations(), Diags, |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3228 | CXXIdx->getClangResourcesPath(), CXXIdx->getOnlyLocalDecls(), |
| 3229 | /*CaptureDiagnostics=*/true, *RemappedFiles.get(), |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 3230 | /*RemappedFilesKeepOriginalName=*/true, PrecompilePreambleAfterNParses, |
| 3231 | TUKind, CacheCodeCompletionResults, IncludeBriefCommentsInCodeCompletion, |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3232 | /*AllowPCHWithCompilerErrors=*/true, SkipFunctionBodies, |
Argyrios Kyrtzidis | a3e2ff1 | 2015-11-20 03:36:21 +0000 | [diff] [blame] | 3233 | /*UserFilesAreVolatile=*/true, ForSerialization, |
| 3234 | CXXIdx->getPCHContainerOperations()->getRawReader().getFormat(), |
| 3235 | &ErrUnit)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3236 | |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3237 | // Early failures in LoadFromCommandLine may return with ErrUnit unset. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3238 | if (!Unit && !ErrUnit) |
| 3239 | return CXError_ASTReadError; |
Artem Belevich | 0ff05cd | 2015-07-13 23:27:56 +0000 | [diff] [blame] | 3240 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3241 | if (NumErrors != Diags->getClient()->getNumErrors()) { |
| 3242 | // Make sure to check that 'Unit' is non-NULL. |
| 3243 | if (CXXIdx->getDisplayDiagnostics()) |
| 3244 | printDiagsToStderr(Unit ? Unit.get() : ErrUnit.get()); |
| 3245 | } |
| 3246 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3247 | if (isASTReadError(Unit ? Unit.get() : ErrUnit.get())) |
| 3248 | return CXError_ASTReadError; |
| 3249 | |
| 3250 | *out_TU = MakeCXTranslationUnit(CXXIdx, Unit.release()); |
| 3251 | return *out_TU ? CXError_Success : CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3252 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3253 | |
| 3254 | CXTranslationUnit |
| 3255 | clang_parseTranslationUnit(CXIndex CIdx, |
| 3256 | const char *source_filename, |
| 3257 | const char *const *command_line_args, |
| 3258 | int num_command_line_args, |
| 3259 | struct CXUnsavedFile *unsaved_files, |
| 3260 | unsigned num_unsaved_files, |
| 3261 | unsigned options) { |
| 3262 | CXTranslationUnit TU; |
| 3263 | enum CXErrorCode Result = clang_parseTranslationUnit2( |
| 3264 | CIdx, source_filename, command_line_args, num_command_line_args, |
| 3265 | unsaved_files, num_unsaved_files, options, &TU); |
Reid Kleckner | 6eaf05a | 2014-02-13 01:19:59 +0000 | [diff] [blame] | 3266 | (void)Result; |
Dmitri Gribenko | 1bf8d91 | 2014-02-18 15:20:02 +0000 | [diff] [blame] | 3267 | assert((TU && Result == CXError_Success) || |
| 3268 | (!TU && Result != CXError_Success)); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3269 | return TU; |
| 3270 | } |
| 3271 | |
| 3272 | enum CXErrorCode clang_parseTranslationUnit2( |
Benjamin Kramer | c02670e | 2015-11-18 16:14:27 +0000 | [diff] [blame] | 3273 | CXIndex CIdx, const char *source_filename, |
| 3274 | const char *const *command_line_args, int num_command_line_args, |
| 3275 | struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files, |
| 3276 | unsigned options, CXTranslationUnit *out_TU) { |
| 3277 | SmallVector<const char *, 4> Args; |
| 3278 | Args.push_back("clang"); |
| 3279 | Args.append(command_line_args, command_line_args + num_command_line_args); |
| 3280 | return clang_parseTranslationUnit2FullArgv( |
| 3281 | CIdx, source_filename, Args.data(), Args.size(), unsaved_files, |
| 3282 | num_unsaved_files, options, out_TU); |
| 3283 | } |
| 3284 | |
| 3285 | enum CXErrorCode clang_parseTranslationUnit2FullArgv( |
| 3286 | CXIndex CIdx, const char *source_filename, |
| 3287 | const char *const *command_line_args, int num_command_line_args, |
| 3288 | struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files, |
| 3289 | unsigned options, CXTranslationUnit *out_TU) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 3290 | LOG_FUNC_SECTION { |
| 3291 | *Log << source_filename << ": "; |
| 3292 | for (int i = 0; i != num_command_line_args; ++i) |
| 3293 | *Log << command_line_args[i] << " "; |
| 3294 | } |
| 3295 | |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3296 | if (num_unsaved_files && !unsaved_files) |
| 3297 | return CXError_InvalidArguments; |
| 3298 | |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 3299 | CXErrorCode result = CXError_Failure; |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3300 | auto ParseTranslationUnitImpl = [=, &result] { |
| 3301 | result = clang_parseTranslationUnit_Impl( |
| 3302 | CIdx, source_filename, command_line_args, num_command_line_args, |
| 3303 | llvm::makeArrayRef(unsaved_files, num_unsaved_files), options, out_TU); |
| 3304 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3305 | llvm::CrashRecoveryContext CRC; |
| 3306 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3307 | if (!RunSafely(CRC, ParseTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3308 | fprintf(stderr, "libclang: crash detected during parsing: {\n"); |
| 3309 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 3310 | fprintf(stderr, " 'command_line_args' : ["); |
| 3311 | for (int i = 0; i != num_command_line_args; ++i) { |
| 3312 | if (i) |
| 3313 | fprintf(stderr, ", "); |
| 3314 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 3315 | } |
| 3316 | fprintf(stderr, "],\n"); |
| 3317 | fprintf(stderr, " 'unsaved_files' : ["); |
| 3318 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 3319 | if (i) |
| 3320 | fprintf(stderr, ", "); |
| 3321 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 3322 | unsaved_files[i].Length); |
| 3323 | } |
| 3324 | fprintf(stderr, "],\n"); |
| 3325 | fprintf(stderr, " 'options' : %d,\n", options); |
| 3326 | fprintf(stderr, "}\n"); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3327 | |
| 3328 | return CXError_Crashed; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3329 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3330 | if (CXTranslationUnit *TU = out_TU) |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3331 | PrintLibclangResourceUsage(*TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3332 | } |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 3333 | |
| 3334 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3337 | CXString clang_Type_getObjCEncoding(CXType CT) { |
| 3338 | CXTranslationUnit tu = static_cast<CXTranslationUnit>(CT.data[1]); |
| 3339 | ASTContext &Ctx = getASTUnit(tu)->getASTContext(); |
| 3340 | std::string encoding; |
| 3341 | Ctx.getObjCEncodingForType(QualType::getFromOpaquePtr(CT.data[0]), |
| 3342 | encoding); |
| 3343 | |
| 3344 | return cxstring::createDup(encoding); |
| 3345 | } |
| 3346 | |
| 3347 | static const IdentifierInfo *getMacroIdentifier(CXCursor C) { |
| 3348 | if (C.kind == CXCursor_MacroDefinition) { |
| 3349 | if (const MacroDefinitionRecord *MDR = getCursorMacroDefinition(C)) |
| 3350 | return MDR->getName(); |
| 3351 | } else if (C.kind == CXCursor_MacroExpansion) { |
| 3352 | MacroExpansionCursor ME = getCursorMacroExpansion(C); |
| 3353 | return ME.getName(); |
| 3354 | } |
| 3355 | return nullptr; |
| 3356 | } |
| 3357 | |
| 3358 | unsigned clang_Cursor_isMacroFunctionLike(CXCursor C) { |
| 3359 | const IdentifierInfo *II = getMacroIdentifier(C); |
| 3360 | if (!II) { |
| 3361 | return false; |
| 3362 | } |
| 3363 | ASTUnit *ASTU = getCursorASTUnit(C); |
| 3364 | Preprocessor &PP = ASTU->getPreprocessor(); |
| 3365 | if (const MacroInfo *MI = PP.getMacroInfo(II)) |
| 3366 | return MI->isFunctionLike(); |
| 3367 | return false; |
| 3368 | } |
| 3369 | |
| 3370 | unsigned clang_Cursor_isMacroBuiltin(CXCursor C) { |
| 3371 | const IdentifierInfo *II = getMacroIdentifier(C); |
| 3372 | if (!II) { |
| 3373 | return false; |
| 3374 | } |
| 3375 | ASTUnit *ASTU = getCursorASTUnit(C); |
| 3376 | Preprocessor &PP = ASTU->getPreprocessor(); |
| 3377 | if (const MacroInfo *MI = PP.getMacroInfo(II)) |
| 3378 | return MI->isBuiltinMacro(); |
| 3379 | return false; |
| 3380 | } |
| 3381 | |
| 3382 | unsigned clang_Cursor_isFunctionInlined(CXCursor C) { |
| 3383 | const Decl *D = getCursorDecl(C); |
| 3384 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 3385 | if (!FD) { |
| 3386 | return false; |
| 3387 | } |
| 3388 | return FD->isInlined(); |
| 3389 | } |
| 3390 | |
| 3391 | static StringLiteral* getCFSTR_value(CallExpr *callExpr) { |
| 3392 | if (callExpr->getNumArgs() != 1) { |
| 3393 | return nullptr; |
| 3394 | } |
| 3395 | |
| 3396 | StringLiteral *S = nullptr; |
| 3397 | auto *arg = callExpr->getArg(0); |
| 3398 | if (arg->getStmtClass() == Stmt::ImplicitCastExprClass) { |
| 3399 | ImplicitCastExpr *I = static_cast<ImplicitCastExpr *>(arg); |
| 3400 | auto *subExpr = I->getSubExprAsWritten(); |
| 3401 | |
| 3402 | if(subExpr->getStmtClass() != Stmt::StringLiteralClass){ |
| 3403 | return nullptr; |
| 3404 | } |
| 3405 | |
| 3406 | S = static_cast<StringLiteral *>(I->getSubExprAsWritten()); |
| 3407 | } else if (arg->getStmtClass() == Stmt::StringLiteralClass) { |
| 3408 | S = static_cast<StringLiteral *>(callExpr->getArg(0)); |
| 3409 | } else { |
| 3410 | return nullptr; |
| 3411 | } |
| 3412 | return S; |
| 3413 | } |
| 3414 | |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3415 | struct ExprEvalResult { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3416 | CXEvalResultKind EvalType; |
| 3417 | union { |
| 3418 | int intVal; |
| 3419 | double floatVal; |
| 3420 | char *stringVal; |
| 3421 | } EvalData; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3422 | ~ExprEvalResult() { |
| 3423 | if (EvalType != CXEval_UnExposed && EvalType != CXEval_Float && |
| 3424 | EvalType != CXEval_Int) { |
| 3425 | delete EvalData.stringVal; |
| 3426 | } |
| 3427 | } |
| 3428 | }; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3429 | |
| 3430 | void clang_EvalResult_dispose(CXEvalResult E) { |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3431 | delete static_cast<ExprEvalResult *>(E); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3432 | } |
| 3433 | |
| 3434 | CXEvalResultKind clang_EvalResult_getKind(CXEvalResult E) { |
| 3435 | if (!E) { |
| 3436 | return CXEval_UnExposed; |
| 3437 | } |
| 3438 | return ((ExprEvalResult *)E)->EvalType; |
| 3439 | } |
| 3440 | |
| 3441 | int clang_EvalResult_getAsInt(CXEvalResult E) { |
| 3442 | if (!E) { |
| 3443 | return 0; |
| 3444 | } |
| 3445 | return ((ExprEvalResult *)E)->EvalData.intVal; |
| 3446 | } |
| 3447 | |
| 3448 | double clang_EvalResult_getAsDouble(CXEvalResult E) { |
| 3449 | if (!E) { |
| 3450 | return 0; |
| 3451 | } |
| 3452 | return ((ExprEvalResult *)E)->EvalData.floatVal; |
| 3453 | } |
| 3454 | |
| 3455 | const char* clang_EvalResult_getAsStr(CXEvalResult E) { |
| 3456 | if (!E) { |
| 3457 | return nullptr; |
| 3458 | } |
| 3459 | return ((ExprEvalResult *)E)->EvalData.stringVal; |
| 3460 | } |
| 3461 | |
| 3462 | static const ExprEvalResult* evaluateExpr(Expr *expr, CXCursor C) { |
| 3463 | Expr::EvalResult ER; |
| 3464 | ASTContext &ctx = getCursorContext(C); |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3465 | if (!expr) |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3466 | return nullptr; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3467 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3468 | expr = expr->IgnoreParens(); |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3469 | if (!expr->EvaluateAsRValue(ER, ctx)) |
| 3470 | return nullptr; |
| 3471 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3472 | QualType rettype; |
| 3473 | CallExpr *callExpr; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3474 | auto result = llvm::make_unique<ExprEvalResult>(); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3475 | result->EvalType = CXEval_UnExposed; |
| 3476 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3477 | if (ER.Val.isInt()) { |
| 3478 | result->EvalType = CXEval_Int; |
| 3479 | result->EvalData.intVal = ER.Val.getInt().getExtValue(); |
| 3480 | return result.release(); |
| 3481 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3482 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3483 | if (ER.Val.isFloat()) { |
| 3484 | llvm::SmallVector<char, 100> Buffer; |
| 3485 | ER.Val.getFloat().toString(Buffer); |
| 3486 | std::string floatStr(Buffer.data(), Buffer.size()); |
| 3487 | result->EvalType = CXEval_Float; |
| 3488 | bool ignored; |
| 3489 | llvm::APFloat apFloat = ER.Val.getFloat(); |
| 3490 | apFloat.convert(llvm::APFloat::IEEEdouble, |
| 3491 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 3492 | result->EvalData.floatVal = apFloat.convertToDouble(); |
| 3493 | return result.release(); |
| 3494 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3495 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3496 | if (expr->getStmtClass() == Stmt::ImplicitCastExprClass) { |
| 3497 | const ImplicitCastExpr *I = dyn_cast<ImplicitCastExpr>(expr); |
| 3498 | auto *subExpr = I->getSubExprAsWritten(); |
| 3499 | if (subExpr->getStmtClass() == Stmt::StringLiteralClass || |
| 3500 | subExpr->getStmtClass() == Stmt::ObjCStringLiteralClass) { |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3501 | const StringLiteral *StrE = nullptr; |
| 3502 | const ObjCStringLiteral *ObjCExpr; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3503 | ObjCExpr = dyn_cast<ObjCStringLiteral>(subExpr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3504 | |
| 3505 | if (ObjCExpr) { |
| 3506 | StrE = ObjCExpr->getString(); |
| 3507 | result->EvalType = CXEval_ObjCStrLiteral; |
| 3508 | } else { |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3509 | StrE = cast<StringLiteral>(I->getSubExprAsWritten()); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3510 | result->EvalType = CXEval_StrLiteral; |
| 3511 | } |
| 3512 | |
| 3513 | std::string strRef(StrE->getString().str()); |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3514 | result->EvalData.stringVal = new char[strRef.size() + 1]; |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3515 | strncpy((char *)result->EvalData.stringVal, strRef.c_str(), |
| 3516 | strRef.size()); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3517 | result->EvalData.stringVal[strRef.size()] = '\0'; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3518 | return result.release(); |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3519 | } |
| 3520 | } else if (expr->getStmtClass() == Stmt::ObjCStringLiteralClass || |
| 3521 | expr->getStmtClass() == Stmt::StringLiteralClass) { |
| 3522 | const StringLiteral *StrE = nullptr; |
| 3523 | const ObjCStringLiteral *ObjCExpr; |
| 3524 | ObjCExpr = dyn_cast<ObjCStringLiteral>(expr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3525 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3526 | if (ObjCExpr) { |
| 3527 | StrE = ObjCExpr->getString(); |
| 3528 | result->EvalType = CXEval_ObjCStrLiteral; |
| 3529 | } else { |
| 3530 | StrE = cast<StringLiteral>(expr); |
| 3531 | result->EvalType = CXEval_StrLiteral; |
| 3532 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3533 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3534 | std::string strRef(StrE->getString().str()); |
| 3535 | result->EvalData.stringVal = new char[strRef.size() + 1]; |
| 3536 | strncpy((char *)result->EvalData.stringVal, strRef.c_str(), strRef.size()); |
| 3537 | result->EvalData.stringVal[strRef.size()] = '\0'; |
| 3538 | return result.release(); |
| 3539 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3540 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3541 | if (expr->getStmtClass() == Stmt::CStyleCastExprClass) { |
| 3542 | CStyleCastExpr *CC = static_cast<CStyleCastExpr *>(expr); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3543 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3544 | rettype = CC->getType(); |
| 3545 | if (rettype.getAsString() == "CFStringRef" && |
| 3546 | CC->getSubExpr()->getStmtClass() == Stmt::CallExprClass) { |
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 | callExpr = static_cast<CallExpr *>(CC->getSubExpr()); |
| 3549 | StringLiteral *S = getCFSTR_value(callExpr); |
| 3550 | if (S) { |
| 3551 | std::string strLiteral(S->getString().str()); |
| 3552 | result->EvalType = CXEval_CFStr; |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3553 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3554 | result->EvalData.stringVal = new char[strLiteral.size() + 1]; |
| 3555 | strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(), |
| 3556 | strLiteral.size()); |
| 3557 | result->EvalData.stringVal[strLiteral.size()] = '\0'; |
David Blaikie | 5927257 | 2016-04-13 18:23:33 +0000 | [diff] [blame] | 3558 | return result.release(); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3559 | } |
| 3560 | } |
| 3561 | |
David Blaikie | bbc0088 | 2016-04-13 18:36:19 +0000 | [diff] [blame] | 3562 | } else if (expr->getStmtClass() == Stmt::CallExprClass) { |
| 3563 | callExpr = static_cast<CallExpr *>(expr); |
| 3564 | rettype = callExpr->getCallReturnType(ctx); |
| 3565 | |
| 3566 | if (rettype->isVectorType() || callExpr->getNumArgs() > 1) |
| 3567 | return nullptr; |
| 3568 | |
| 3569 | if (rettype->isIntegralType(ctx) || rettype->isRealFloatingType()) { |
| 3570 | if (callExpr->getNumArgs() == 1 && |
| 3571 | !callExpr->getArg(0)->getType()->isIntegralType(ctx)) |
| 3572 | return nullptr; |
| 3573 | } else if (rettype.getAsString() == "CFStringRef") { |
| 3574 | |
| 3575 | StringLiteral *S = getCFSTR_value(callExpr); |
| 3576 | if (S) { |
| 3577 | std::string strLiteral(S->getString().str()); |
| 3578 | result->EvalType = CXEval_CFStr; |
| 3579 | result->EvalData.stringVal = new char[strLiteral.size() + 1]; |
| 3580 | strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(), |
| 3581 | strLiteral.size()); |
| 3582 | result->EvalData.stringVal[strLiteral.size()] = '\0'; |
| 3583 | return result.release(); |
| 3584 | } |
| 3585 | } |
| 3586 | } else if (expr->getStmtClass() == Stmt::DeclRefExprClass) { |
| 3587 | DeclRefExpr *D = static_cast<DeclRefExpr *>(expr); |
| 3588 | ValueDecl *V = D->getDecl(); |
| 3589 | if (V->getKind() == Decl::Function) { |
| 3590 | std::string strName = V->getNameAsString(); |
| 3591 | result->EvalType = CXEval_Other; |
| 3592 | result->EvalData.stringVal = new char[strName.size() + 1]; |
| 3593 | strncpy(result->EvalData.stringVal, strName.c_str(), strName.size()); |
| 3594 | result->EvalData.stringVal[strName.size()] = '\0'; |
| 3595 | return result.release(); |
| 3596 | } |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3597 | } |
| 3598 | |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3599 | return nullptr; |
| 3600 | } |
| 3601 | |
| 3602 | CXEvalResult clang_Cursor_Evaluate(CXCursor C) { |
| 3603 | const Decl *D = getCursorDecl(C); |
| 3604 | if (D) { |
| 3605 | const Expr *expr = nullptr; |
| 3606 | if (auto *Var = dyn_cast<VarDecl>(D)) { |
| 3607 | expr = Var->getInit(); |
| 3608 | } else if (auto *Field = dyn_cast<FieldDecl>(D)) { |
| 3609 | expr = Field->getInClassInitializer(); |
| 3610 | } |
| 3611 | if (expr) |
Aaron Ballman | 01dc157 | 2016-01-20 15:25:30 +0000 | [diff] [blame] | 3612 | return const_cast<CXEvalResult>(reinterpret_cast<const void *>( |
| 3613 | evaluateExpr(const_cast<Expr *>(expr), C))); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3614 | return nullptr; |
| 3615 | } |
| 3616 | |
| 3617 | const CompoundStmt *compoundStmt = dyn_cast_or_null<CompoundStmt>(getCursorStmt(C)); |
| 3618 | if (compoundStmt) { |
| 3619 | Expr *expr = nullptr; |
| 3620 | for (auto *bodyIterator : compoundStmt->body()) { |
| 3621 | if ((expr = dyn_cast<Expr>(bodyIterator))) { |
| 3622 | break; |
| 3623 | } |
| 3624 | } |
| 3625 | if (expr) |
Aaron Ballman | 01dc157 | 2016-01-20 15:25:30 +0000 | [diff] [blame] | 3626 | return const_cast<CXEvalResult>( |
| 3627 | reinterpret_cast<const void *>(evaluateExpr(expr, C))); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 3628 | } |
| 3629 | return nullptr; |
| 3630 | } |
| 3631 | |
| 3632 | unsigned clang_Cursor_hasAttrs(CXCursor C) { |
| 3633 | const Decl *D = getCursorDecl(C); |
| 3634 | if (!D) { |
| 3635 | return 0; |
| 3636 | } |
| 3637 | |
| 3638 | if (D->hasAttrs()) { |
| 3639 | return 1; |
| 3640 | } |
| 3641 | |
| 3642 | return 0; |
| 3643 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3644 | unsigned clang_defaultSaveOptions(CXTranslationUnit TU) { |
| 3645 | return CXSaveTranslationUnit_None; |
| 3646 | } |
| 3647 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3648 | static CXSaveError clang_saveTranslationUnit_Impl(CXTranslationUnit TU, |
| 3649 | const char *FileName, |
| 3650 | unsigned options) { |
| 3651 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3652 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
| 3653 | setThreadBackgroundPriority(); |
| 3654 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3655 | bool hadError = cxtu::getASTUnit(TU)->Save(FileName); |
| 3656 | return hadError ? CXSaveError_Unknown : CXSaveError_None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3657 | } |
| 3658 | |
| 3659 | int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName, |
| 3660 | unsigned options) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 3661 | LOG_FUNC_SECTION { |
| 3662 | *Log << TU << ' ' << FileName; |
| 3663 | } |
| 3664 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 3665 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3666 | LOG_BAD_TU(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3667 | return CXSaveError_InvalidTU; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3668 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3669 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3670 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3671 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 3672 | if (!CXXUnit->hasSema()) |
| 3673 | return CXSaveError_InvalidTU; |
| 3674 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3675 | CXSaveError result; |
| 3676 | auto SaveTranslationUnitImpl = [=, &result]() { |
| 3677 | result = clang_saveTranslationUnit_Impl(TU, FileName, options); |
| 3678 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3679 | |
| 3680 | if (!CXXUnit->getDiagnostics().hasUnrecoverableErrorOccurred() || |
| 3681 | getenv("LIBCLANG_NOTHREADS")) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3682 | SaveTranslationUnitImpl(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3683 | |
| 3684 | if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 3685 | PrintLibclangResourceUsage(TU); |
| 3686 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3687 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3688 | } |
| 3689 | |
| 3690 | // We have an AST that has invalid nodes due to compiler errors. |
| 3691 | // Use a crash recovery thread for protection. |
| 3692 | |
| 3693 | llvm::CrashRecoveryContext CRC; |
| 3694 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3695 | if (!RunSafely(CRC, SaveTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3696 | fprintf(stderr, "libclang: crash detected during AST saving: {\n"); |
| 3697 | fprintf(stderr, " 'filename' : '%s'\n", FileName); |
| 3698 | fprintf(stderr, " 'options' : %d,\n", options); |
| 3699 | fprintf(stderr, "}\n"); |
| 3700 | |
| 3701 | return CXSaveError_Unknown; |
| 3702 | |
| 3703 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
| 3704 | PrintLibclangResourceUsage(TU); |
| 3705 | } |
| 3706 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3707 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) { |
| 3711 | if (CTUnit) { |
| 3712 | // If the translation unit has been marked as unsafe to free, just discard |
| 3713 | // it. |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3714 | ASTUnit *Unit = cxtu::getASTUnit(CTUnit); |
| 3715 | if (Unit && Unit->isUnsafeToFree()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3716 | return; |
| 3717 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3718 | delete cxtu::getASTUnit(CTUnit); |
Dmitri Gribenko | b95b3f1 | 2013-01-26 22:44:19 +0000 | [diff] [blame] | 3719 | delete CTUnit->StringPool; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3720 | delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics); |
| 3721 | disposeOverridenCXCursorsPool(CTUnit->OverridenCursorsPool); |
Dmitri Gribenko | 9e60511 | 2013-11-13 22:16:51 +0000 | [diff] [blame] | 3722 | delete CTUnit->CommentToXML; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3723 | delete CTUnit; |
| 3724 | } |
| 3725 | } |
| 3726 | |
| 3727 | unsigned clang_defaultReparseOptions(CXTranslationUnit TU) { |
| 3728 | return CXReparse_None; |
| 3729 | } |
| 3730 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3731 | static CXErrorCode |
| 3732 | clang_reparseTranslationUnit_Impl(CXTranslationUnit TU, |
| 3733 | ArrayRef<CXUnsavedFile> unsaved_files, |
| 3734 | unsigned options) { |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3735 | // Check arguments. |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 3736 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3737 | LOG_BAD_TU(TU); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3738 | return CXError_InvalidArguments; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3739 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3740 | |
| 3741 | // Reset the associated diagnostics. |
| 3742 | delete static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3743 | TU->Diagnostics = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3744 | |
Dmitri Gribenko | 183436e | 2013-01-26 21:49:50 +0000 | [diff] [blame] | 3745 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3746 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) |
| 3747 | setThreadBackgroundPriority(); |
| 3748 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3749 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3750 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 3751 | |
| 3752 | std::unique_ptr<std::vector<ASTUnit::RemappedFile>> RemappedFiles( |
| 3753 | new std::vector<ASTUnit::RemappedFile>()); |
| 3754 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3755 | // Recover resources if we crash before exiting this function. |
| 3756 | llvm::CrashRecoveryContextCleanupRegistrar< |
| 3757 | std::vector<ASTUnit::RemappedFile> > RemappedCleanup(RemappedFiles.get()); |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3758 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3759 | for (auto &UF : unsaved_files) { |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3760 | std::unique_ptr<llvm::MemoryBuffer> MB = |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3761 | llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 3762 | RemappedFiles->push_back(std::make_pair(UF.Filename, MB.release())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3763 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3764 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 3765 | if (!CXXUnit->Reparse(CXXIdx->getPCHContainerOperations(), |
| 3766 | *RemappedFiles.get())) |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3767 | return CXError_Success; |
| 3768 | if (isASTReadError(CXXUnit)) |
| 3769 | return CXError_ASTReadError; |
| 3770 | return CXError_Failure; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3771 | } |
| 3772 | |
| 3773 | int clang_reparseTranslationUnit(CXTranslationUnit TU, |
| 3774 | unsigned num_unsaved_files, |
| 3775 | struct CXUnsavedFile *unsaved_files, |
| 3776 | unsigned options) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 3777 | LOG_FUNC_SECTION { |
| 3778 | *Log << TU; |
| 3779 | } |
| 3780 | |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 3781 | if (num_unsaved_files && !unsaved_files) |
| 3782 | return CXError_InvalidArguments; |
| 3783 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3784 | CXErrorCode result; |
| 3785 | auto ReparseTranslationUnitImpl = [=, &result]() { |
| 3786 | result = clang_reparseTranslationUnit_Impl( |
| 3787 | TU, llvm::makeArrayRef(unsaved_files, num_unsaved_files), options); |
| 3788 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3789 | |
| 3790 | if (getenv("LIBCLANG_NOTHREADS")) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3791 | ReparseTranslationUnitImpl(); |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 3792 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3793 | } |
| 3794 | |
| 3795 | llvm::CrashRecoveryContext CRC; |
| 3796 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 3797 | if (!RunSafely(CRC, ReparseTranslationUnitImpl)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3798 | fprintf(stderr, "libclang: crash detected during reparsing\n"); |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3799 | cxtu::getASTUnit(TU)->setUnsafeToFree(true); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3800 | return CXError_Crashed; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3801 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 3802 | PrintLibclangResourceUsage(TU); |
| 3803 | |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 3804 | return result; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3805 | } |
| 3806 | |
| 3807 | |
| 3808 | CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 3809 | if (isNotUsableTU(CTUnit)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3810 | LOG_BAD_TU(CTUnit); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 3811 | return cxstring::createEmpty(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3812 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3813 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3814 | ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 3815 | return cxstring::createDup(CXXUnit->getOriginalSourceFileName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
| 3818 | CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 3819 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3820 | LOG_BAD_TU(TU); |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 3821 | return clang_getNullCursor(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3822 | } |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 3823 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3824 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3825 | return MakeCXCursor(CXXUnit->getASTContext().getTranslationUnitDecl(), TU); |
| 3826 | } |
| 3827 | |
| 3828 | } // end: extern "C" |
| 3829 | |
| 3830 | //===----------------------------------------------------------------------===// |
| 3831 | // CXFile Operations. |
| 3832 | //===----------------------------------------------------------------------===// |
| 3833 | |
| 3834 | extern "C" { |
| 3835 | CXString clang_getFileName(CXFile SFile) { |
| 3836 | if (!SFile) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 3837 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3838 | |
| 3839 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 3840 | return cxstring::createRef(FEnt->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
| 3843 | time_t clang_getFileTime(CXFile SFile) { |
| 3844 | if (!SFile) |
| 3845 | return 0; |
| 3846 | |
| 3847 | FileEntry *FEnt = static_cast<FileEntry *>(SFile); |
| 3848 | return FEnt->getModificationTime(); |
| 3849 | } |
| 3850 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3851 | CXFile clang_getFile(CXTranslationUnit TU, const char *file_name) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 3852 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3853 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3854 | return nullptr; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3855 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3856 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3857 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3858 | |
| 3859 | FileManager &FMgr = CXXUnit->getFileManager(); |
| 3860 | return const_cast<FileEntry *>(FMgr.getFile(file_name)); |
| 3861 | } |
| 3862 | |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3863 | unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit TU, |
| 3864 | CXFile file) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 3865 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 3866 | LOG_BAD_TU(TU); |
| 3867 | return 0; |
| 3868 | } |
| 3869 | |
| 3870 | if (!file) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3871 | return 0; |
| 3872 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 3873 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3874 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
| 3875 | return CXXUnit->getPreprocessor().getHeaderSearchInfo() |
| 3876 | .isFileMultipleIncludeGuarded(FEnt); |
| 3877 | } |
| 3878 | |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 3879 | int clang_getFileUniqueID(CXFile file, CXFileUniqueID *outID) { |
| 3880 | if (!file || !outID) |
| 3881 | return 1; |
| 3882 | |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 3883 | FileEntry *FEnt = static_cast<FileEntry *>(file); |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 3884 | const llvm::sys::fs::UniqueID &ID = FEnt->getUniqueID(); |
| 3885 | outID->data[0] = ID.getDevice(); |
| 3886 | outID->data[1] = ID.getFile(); |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 3887 | outID->data[2] = FEnt->getModificationTime(); |
| 3888 | return 0; |
Argyrios Kyrtzidis | ac08b26 | 2013-01-26 04:52:52 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
Argyrios Kyrtzidis | ac3997e | 2014-08-16 00:26:19 +0000 | [diff] [blame] | 3891 | int clang_File_isEqual(CXFile file1, CXFile file2) { |
| 3892 | if (file1 == file2) |
| 3893 | return true; |
| 3894 | |
| 3895 | if (!file1 || !file2) |
| 3896 | return false; |
| 3897 | |
| 3898 | FileEntry *FEnt1 = static_cast<FileEntry *>(file1); |
| 3899 | FileEntry *FEnt2 = static_cast<FileEntry *>(file2); |
| 3900 | return FEnt1->getUniqueID() == FEnt2->getUniqueID(); |
| 3901 | } |
| 3902 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3903 | } // end: extern "C" |
| 3904 | |
| 3905 | //===----------------------------------------------------------------------===// |
| 3906 | // CXCursor Operations. |
| 3907 | //===----------------------------------------------------------------------===// |
| 3908 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3909 | static const Decl *getDeclFromExpr(const Stmt *E) { |
| 3910 | if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3911 | return getDeclFromExpr(CE->getSubExpr()); |
| 3912 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3913 | if (const DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3914 | return RefExpr->getDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3915 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3916 | return ME->getMemberDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3917 | if (const ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3918 | return RE->getDecl(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3919 | if (const ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3920 | if (PRE->isExplicitProperty()) |
| 3921 | return PRE->getExplicitProperty(); |
| 3922 | // It could be messaging both getter and setter as in: |
| 3923 | // ++myobj.myprop; |
| 3924 | // in which case prefer to associate the setter since it is less obvious |
| 3925 | // from inspecting the source that the setter is going to get called. |
| 3926 | if (PRE->isMessagingSetter()) |
| 3927 | return PRE->getImplicitPropertySetter(); |
| 3928 | return PRE->getImplicitPropertyGetter(); |
| 3929 | } |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3930 | if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3931 | return getDeclFromExpr(POE->getSyntacticForm()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3932 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3933 | if (Expr *Src = OVE->getSourceExpr()) |
| 3934 | return getDeclFromExpr(Src); |
| 3935 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3936 | if (const CallExpr *CE = dyn_cast<CallExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3937 | return getDeclFromExpr(CE->getCallee()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3938 | if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3939 | if (!CE->isElidable()) |
| 3940 | return CE->getConstructor(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3941 | if (const ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3942 | return OME->getMethodDecl(); |
| 3943 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3944 | if (const ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3945 | return PE->getProtocol(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3946 | if (const SubstNonTypeTemplateParmPackExpr *NTTP |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3947 | = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E)) |
| 3948 | return NTTP->getParameterPack(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 3949 | if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3950 | if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) || |
| 3951 | isa<ParmVarDecl>(SizeOfPack->getPack())) |
| 3952 | return SizeOfPack->getPack(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 3953 | |
| 3954 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3955 | } |
| 3956 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3957 | static SourceLocation getLocationFromExpr(const Expr *E) { |
| 3958 | if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3959 | return getLocationFromExpr(CE->getSubExpr()); |
| 3960 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3961 | if (const ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3962 | return /*FIXME:*/Msg->getLeftLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3963 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3964 | return DRE->getLocation(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3965 | if (const MemberExpr *Member = dyn_cast<MemberExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3966 | return Member->getMemberLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3967 | if (const ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3968 | return Ivar->getLocation(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3969 | if (const SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3970 | return SizeOfPack->getPackLoc(); |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 3971 | if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3972 | return PropRef->getLocation(); |
| 3973 | |
| 3974 | return E->getLocStart(); |
| 3975 | } |
| 3976 | |
| 3977 | extern "C" { |
| 3978 | |
| 3979 | unsigned clang_visitChildren(CXCursor parent, |
| 3980 | CXCursorVisitor visitor, |
| 3981 | CXClientData client_data) { |
| 3982 | CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, |
| 3983 | /*VisitPreprocessorLast=*/false); |
| 3984 | return CursorVis.VisitChildren(parent); |
| 3985 | } |
| 3986 | |
| 3987 | #ifndef __has_feature |
| 3988 | #define __has_feature(x) 0 |
| 3989 | #endif |
| 3990 | #if __has_feature(blocks) |
| 3991 | typedef enum CXChildVisitResult |
| 3992 | (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent); |
| 3993 | |
| 3994 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 3995 | CXClientData client_data) { |
| 3996 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 3997 | return block(cursor, parent); |
| 3998 | } |
| 3999 | #else |
| 4000 | // If we are compiled with a compiler that doesn't have native blocks support, |
| 4001 | // define and call the block manually, so the |
| 4002 | typedef struct _CXChildVisitResult |
| 4003 | { |
| 4004 | void *isa; |
| 4005 | int flags; |
| 4006 | int reserved; |
| 4007 | enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor, |
| 4008 | CXCursor); |
| 4009 | } *CXCursorVisitorBlock; |
| 4010 | |
| 4011 | static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent, |
| 4012 | CXClientData client_data) { |
| 4013 | CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data; |
| 4014 | return block->invoke(block, cursor, parent); |
| 4015 | } |
| 4016 | #endif |
| 4017 | |
| 4018 | |
| 4019 | unsigned clang_visitChildrenWithBlock(CXCursor parent, |
| 4020 | CXCursorVisitorBlock block) { |
| 4021 | return clang_visitChildren(parent, visitWithBlock, block); |
| 4022 | } |
| 4023 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4024 | static CXString getDeclSpelling(const Decl *D) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4025 | if (!D) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4026 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4027 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4028 | const NamedDecl *ND = dyn_cast<NamedDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4029 | if (!ND) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4030 | if (const ObjCPropertyImplDecl *PropImpl = |
| 4031 | dyn_cast<ObjCPropertyImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4032 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4033 | return cxstring::createDup(Property->getIdentifier()->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4034 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4035 | if (const ImportDecl *ImportD = dyn_cast<ImportDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4036 | if (Module *Mod = ImportD->getImportedModule()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4037 | return cxstring::createDup(Mod->getFullModuleName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4038 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4039 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4040 | } |
| 4041 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4042 | if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4043 | return cxstring::createDup(OMD->getSelector().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4044 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4045 | if (const ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4046 | // No, this isn't the same as the code below. getIdentifier() is non-virtual |
| 4047 | // and returns different names. NamedDecl returns the class name and |
| 4048 | // ObjCCategoryImplDecl returns the category name. |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4049 | return cxstring::createRef(CIMP->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4050 | |
| 4051 | if (isa<UsingDirectiveDecl>(D)) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4052 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4053 | |
| 4054 | SmallString<1024> S; |
| 4055 | llvm::raw_svector_ostream os(S); |
| 4056 | ND->printName(os); |
| 4057 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4058 | return cxstring::createDup(os.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4059 | } |
| 4060 | |
| 4061 | CXString clang_getCursorSpelling(CXCursor C) { |
| 4062 | if (clang_isTranslationUnit(C.kind)) |
Dmitri Gribenko | 2c173b4 | 2013-01-11 19:28:44 +0000 | [diff] [blame] | 4063 | return clang_getTranslationUnitSpelling(getCursorTU(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4064 | |
| 4065 | if (clang_isReference(C.kind)) { |
| 4066 | switch (C.kind) { |
| 4067 | case CXCursor_ObjCSuperClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4068 | const ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first; |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4069 | return cxstring::createRef(Super->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4070 | } |
| 4071 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4072 | const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4073 | return cxstring::createRef(Class->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4074 | } |
| 4075 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4076 | const ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4077 | assert(OID && "getCursorSpelling(): Missing protocol decl"); |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4078 | return cxstring::createRef(OID->getIdentifier()->getNameStart()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4079 | } |
| 4080 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4081 | const CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4082 | return cxstring::createDup(B->getType().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4083 | } |
| 4084 | case CXCursor_TypeRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4085 | const TypeDecl *Type = getCursorTypeRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4086 | assert(Type && "Missing type decl"); |
| 4087 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4088 | return cxstring::createDup(getCursorContext(C).getTypeDeclType(Type). |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4089 | getAsString()); |
| 4090 | } |
| 4091 | case CXCursor_TemplateRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4092 | const TemplateDecl *Template = getCursorTemplateRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4093 | assert(Template && "Missing template decl"); |
| 4094 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4095 | return cxstring::createDup(Template->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4096 | } |
| 4097 | |
| 4098 | case CXCursor_NamespaceRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4099 | const NamedDecl *NS = getCursorNamespaceRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4100 | assert(NS && "Missing namespace decl"); |
| 4101 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4102 | return cxstring::createDup(NS->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4103 | } |
| 4104 | |
| 4105 | case CXCursor_MemberRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4106 | const FieldDecl *Field = getCursorMemberRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4107 | assert(Field && "Missing member decl"); |
| 4108 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4109 | return cxstring::createDup(Field->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | case CXCursor_LabelRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4113 | const LabelStmt *Label = getCursorLabelRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4114 | assert(Label && "Missing label"); |
| 4115 | |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4116 | return cxstring::createRef(Label->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4117 | } |
| 4118 | |
| 4119 | case CXCursor_OverloadedDeclRef: { |
| 4120 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4121 | if (const Decl *D = Storage.dyn_cast<const Decl *>()) { |
| 4122 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4123 | return cxstring::createDup(ND->getNameAsString()); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4124 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4125 | } |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4126 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4127 | return cxstring::createDup(E->getName().getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4128 | OverloadedTemplateStorage *Ovl |
| 4129 | = Storage.get<OverloadedTemplateStorage*>(); |
| 4130 | if (Ovl->size() == 0) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4131 | return cxstring::createEmpty(); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4132 | return cxstring::createDup((*Ovl->begin())->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4133 | } |
| 4134 | |
| 4135 | case CXCursor_VariableRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 4136 | const VarDecl *Var = getCursorVariableRef(C).first; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4137 | assert(Var && "Missing variable decl"); |
| 4138 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4139 | return cxstring::createDup(Var->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
| 4142 | default: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4143 | return cxstring::createRef("<not implemented>"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4144 | } |
| 4145 | } |
| 4146 | |
| 4147 | if (clang_isExpression(C.kind)) { |
Argyrios Kyrtzidis | 3227d86 | 2014-03-03 19:40:52 +0000 | [diff] [blame] | 4148 | const Expr *E = getCursorExpr(C); |
| 4149 | |
| 4150 | if (C.kind == CXCursor_ObjCStringLiteral || |
| 4151 | C.kind == CXCursor_StringLiteral) { |
| 4152 | const StringLiteral *SLit; |
| 4153 | if (const ObjCStringLiteral *OSL = dyn_cast<ObjCStringLiteral>(E)) { |
| 4154 | SLit = OSL->getString(); |
| 4155 | } else { |
| 4156 | SLit = cast<StringLiteral>(E); |
| 4157 | } |
| 4158 | SmallString<256> Buf; |
| 4159 | llvm::raw_svector_ostream OS(Buf); |
| 4160 | SLit->outputString(OS); |
| 4161 | return cxstring::createDup(OS.str()); |
| 4162 | } |
| 4163 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4164 | const Decl *D = getDeclFromExpr(getCursorExpr(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4165 | if (D) |
| 4166 | return getDeclSpelling(D); |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4167 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4168 | } |
| 4169 | |
| 4170 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4171 | const Stmt *S = getCursorStmt(C); |
| 4172 | if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4173 | return cxstring::createRef(Label->getName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4174 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4175 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4176 | } |
| 4177 | |
| 4178 | if (C.kind == CXCursor_MacroExpansion) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4179 | return cxstring::createRef(getCursorMacroExpansion(C).getName() |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4180 | ->getNameStart()); |
| 4181 | |
| 4182 | if (C.kind == CXCursor_MacroDefinition) |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4183 | return cxstring::createRef(getCursorMacroDefinition(C)->getName() |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4184 | ->getNameStart()); |
| 4185 | |
| 4186 | if (C.kind == CXCursor_InclusionDirective) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4187 | return cxstring::createDup(getCursorInclusionDirective(C)->getFileName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4188 | |
| 4189 | if (clang_isDeclaration(C.kind)) |
| 4190 | return getDeclSpelling(getCursorDecl(C)); |
| 4191 | |
| 4192 | if (C.kind == CXCursor_AnnotateAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 4193 | const AnnotateAttr *AA = cast<AnnotateAttr>(cxcursor::getCursorAttr(C)); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4194 | return cxstring::createDup(AA->getAnnotation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
| 4197 | if (C.kind == CXCursor_AsmLabelAttr) { |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 4198 | const AsmLabelAttr *AA = cast<AsmLabelAttr>(cxcursor::getCursorAttr(C)); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4199 | return cxstring::createDup(AA->getLabel()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4200 | } |
| 4201 | |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 4202 | if (C.kind == CXCursor_PackedAttr) { |
| 4203 | return cxstring::createRef("packed"); |
| 4204 | } |
| 4205 | |
Saleem Abdulrasool | 79c6971 | 2015-09-05 18:53:43 +0000 | [diff] [blame] | 4206 | if (C.kind == CXCursor_VisibilityAttr) { |
| 4207 | const VisibilityAttr *AA = cast<VisibilityAttr>(cxcursor::getCursorAttr(C)); |
| 4208 | switch (AA->getVisibility()) { |
| 4209 | case VisibilityAttr::VisibilityType::Default: |
| 4210 | return cxstring::createRef("default"); |
| 4211 | case VisibilityAttr::VisibilityType::Hidden: |
| 4212 | return cxstring::createRef("hidden"); |
| 4213 | case VisibilityAttr::VisibilityType::Protected: |
| 4214 | return cxstring::createRef("protected"); |
| 4215 | } |
| 4216 | llvm_unreachable("unknown visibility type"); |
| 4217 | } |
| 4218 | |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4219 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4220 | } |
| 4221 | |
| 4222 | CXSourceRange clang_Cursor_getSpellingNameRange(CXCursor C, |
| 4223 | unsigned pieceIndex, |
| 4224 | unsigned options) { |
| 4225 | if (clang_Cursor_isNull(C)) |
| 4226 | return clang_getNullRange(); |
| 4227 | |
| 4228 | ASTContext &Ctx = getCursorContext(C); |
| 4229 | |
| 4230 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4231 | const Stmt *S = getCursorStmt(C); |
| 4232 | if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4233 | if (pieceIndex > 0) |
| 4234 | return clang_getNullRange(); |
| 4235 | return cxloc::translateSourceRange(Ctx, Label->getIdentLoc()); |
| 4236 | } |
| 4237 | |
| 4238 | return clang_getNullRange(); |
| 4239 | } |
| 4240 | |
| 4241 | if (C.kind == CXCursor_ObjCMessageExpr) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 4242 | if (const ObjCMessageExpr * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4243 | ME = dyn_cast_or_null<ObjCMessageExpr>(getCursorExpr(C))) { |
| 4244 | if (pieceIndex >= ME->getNumSelectorLocs()) |
| 4245 | return clang_getNullRange(); |
| 4246 | return cxloc::translateSourceRange(Ctx, ME->getSelectorLoc(pieceIndex)); |
| 4247 | } |
| 4248 | } |
| 4249 | |
| 4250 | if (C.kind == CXCursor_ObjCInstanceMethodDecl || |
| 4251 | C.kind == CXCursor_ObjCClassMethodDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4252 | if (const ObjCMethodDecl * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4253 | MD = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(C))) { |
| 4254 | if (pieceIndex >= MD->getNumSelectorLocs()) |
| 4255 | return clang_getNullRange(); |
| 4256 | return cxloc::translateSourceRange(Ctx, MD->getSelectorLoc(pieceIndex)); |
| 4257 | } |
| 4258 | } |
| 4259 | |
| 4260 | if (C.kind == CXCursor_ObjCCategoryDecl || |
| 4261 | C.kind == CXCursor_ObjCCategoryImplDecl) { |
| 4262 | if (pieceIndex > 0) |
| 4263 | return clang_getNullRange(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4264 | if (const ObjCCategoryDecl * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4265 | CD = dyn_cast_or_null<ObjCCategoryDecl>(getCursorDecl(C))) |
| 4266 | return cxloc::translateSourceRange(Ctx, CD->getCategoryNameLoc()); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4267 | if (const ObjCCategoryImplDecl * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4268 | CID = dyn_cast_or_null<ObjCCategoryImplDecl>(getCursorDecl(C))) |
| 4269 | return cxloc::translateSourceRange(Ctx, CID->getCategoryNameLoc()); |
| 4270 | } |
| 4271 | |
| 4272 | if (C.kind == CXCursor_ModuleImportDecl) { |
| 4273 | if (pieceIndex > 0) |
| 4274 | return clang_getNullRange(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4275 | if (const ImportDecl *ImportD = |
| 4276 | dyn_cast_or_null<ImportDecl>(getCursorDecl(C))) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4277 | ArrayRef<SourceLocation> Locs = ImportD->getIdentifierLocs(); |
| 4278 | if (!Locs.empty()) |
| 4279 | return cxloc::translateSourceRange(Ctx, |
| 4280 | SourceRange(Locs.front(), Locs.back())); |
| 4281 | } |
| 4282 | return clang_getNullRange(); |
| 4283 | } |
| 4284 | |
Argyrios Kyrtzidis | a2a1e53 | 2014-08-26 20:23:26 +0000 | [diff] [blame] | 4285 | if (C.kind == CXCursor_CXXMethod || C.kind == CXCursor_Destructor || |
| 4286 | C.kind == CXCursor_ConversionFunction) { |
| 4287 | if (pieceIndex > 0) |
| 4288 | return clang_getNullRange(); |
| 4289 | if (const FunctionDecl *FD = |
| 4290 | dyn_cast_or_null<FunctionDecl>(getCursorDecl(C))) { |
| 4291 | DeclarationNameInfo FunctionName = FD->getNameInfo(); |
| 4292 | return cxloc::translateSourceRange(Ctx, FunctionName.getSourceRange()); |
| 4293 | } |
| 4294 | return clang_getNullRange(); |
| 4295 | } |
| 4296 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4297 | // FIXME: A CXCursor_InclusionDirective should give the location of the |
| 4298 | // filename, but we don't keep track of this. |
| 4299 | |
| 4300 | // FIXME: A CXCursor_AnnotateAttr should give the location of the annotation |
| 4301 | // but we don't keep track of this. |
| 4302 | |
| 4303 | // FIXME: A CXCursor_AsmLabelAttr should give the location of the label |
| 4304 | // but we don't keep track of this. |
| 4305 | |
| 4306 | // Default handling, give the location of the cursor. |
| 4307 | |
| 4308 | if (pieceIndex > 0) |
| 4309 | return clang_getNullRange(); |
| 4310 | |
| 4311 | CXSourceLocation CXLoc = clang_getCursorLocation(C); |
| 4312 | SourceLocation Loc = cxloc::translateSourceLocation(CXLoc); |
| 4313 | return cxloc::translateSourceRange(Ctx, Loc); |
| 4314 | } |
| 4315 | |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4316 | CXString clang_Cursor_getMangling(CXCursor C) { |
| 4317 | if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind)) |
| 4318 | return cxstring::createEmpty(); |
| 4319 | |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4320 | // Mangling only works for functions and variables. |
Eli Bendersky | 7975959 | 2014-08-01 15:01:10 +0000 | [diff] [blame] | 4321 | const Decl *D = getCursorDecl(C); |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4322 | if (!D || !(isa<FunctionDecl>(D) || isa<VarDecl>(D))) |
| 4323 | return cxstring::createEmpty(); |
| 4324 | |
Argyrios Kyrtzidis | ca741ce | 2016-02-14 22:30:14 +0000 | [diff] [blame] | 4325 | ASTContext &Ctx = D->getASTContext(); |
| 4326 | index::CodegenNameGenerator CGNameGen(Ctx); |
| 4327 | return cxstring::createDup(CGNameGen.getName(D)); |
Eli Bendersky | 44a206f | 2014-07-31 18:04:56 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
Saleem Abdulrasool | 6003443 | 2015-11-12 03:57:22 +0000 | [diff] [blame] | 4330 | CXStringSet *clang_Cursor_getCXXManglings(CXCursor C) { |
| 4331 | if (clang_isInvalid(C.kind) || !clang_isDeclaration(C.kind)) |
| 4332 | return nullptr; |
| 4333 | |
| 4334 | const Decl *D = getCursorDecl(C); |
| 4335 | if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D))) |
| 4336 | return nullptr; |
| 4337 | |
Argyrios Kyrtzidis | ca741ce | 2016-02-14 22:30:14 +0000 | [diff] [blame] | 4338 | ASTContext &Ctx = D->getASTContext(); |
| 4339 | index::CodegenNameGenerator CGNameGen(Ctx); |
| 4340 | std::vector<std::string> Manglings = CGNameGen.getAllManglings(D); |
Saleem Abdulrasool | 6003443 | 2015-11-12 03:57:22 +0000 | [diff] [blame] | 4341 | return cxstring::createSet(Manglings); |
| 4342 | } |
| 4343 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4344 | CXString clang_getCursorDisplayName(CXCursor C) { |
| 4345 | if (!clang_isDeclaration(C.kind)) |
| 4346 | return clang_getCursorSpelling(C); |
| 4347 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4348 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4349 | if (!D) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 4350 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4351 | |
| 4352 | PrintingPolicy Policy = getCursorContext(C).getPrintingPolicy(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4353 | if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4354 | D = FunTmpl->getTemplatedDecl(); |
| 4355 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4356 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4357 | SmallString<64> Str; |
| 4358 | llvm::raw_svector_ostream OS(Str); |
| 4359 | OS << *Function; |
| 4360 | if (Function->getPrimaryTemplate()) |
| 4361 | OS << "<>"; |
| 4362 | OS << "("; |
| 4363 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) { |
| 4364 | if (I) |
| 4365 | OS << ", "; |
| 4366 | OS << Function->getParamDecl(I)->getType().getAsString(Policy); |
| 4367 | } |
| 4368 | |
| 4369 | if (Function->isVariadic()) { |
| 4370 | if (Function->getNumParams()) |
| 4371 | OS << ", "; |
| 4372 | OS << "..."; |
| 4373 | } |
| 4374 | OS << ")"; |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4375 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4376 | } |
| 4377 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4378 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4379 | SmallString<64> Str; |
| 4380 | llvm::raw_svector_ostream OS(Str); |
| 4381 | OS << *ClassTemplate; |
| 4382 | OS << "<"; |
| 4383 | TemplateParameterList *Params = ClassTemplate->getTemplateParameters(); |
| 4384 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 4385 | if (I) |
| 4386 | OS << ", "; |
| 4387 | |
| 4388 | NamedDecl *Param = Params->getParam(I); |
| 4389 | if (Param->getIdentifier()) { |
| 4390 | OS << Param->getIdentifier()->getName(); |
| 4391 | continue; |
| 4392 | } |
| 4393 | |
| 4394 | // There is no parameter name, which makes this tricky. Try to come up |
| 4395 | // with something useful that isn't too long. |
| 4396 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 4397 | OS << (TTP->wasDeclaredWithTypename()? "typename" : "class"); |
| 4398 | else if (NonTypeTemplateParmDecl *NTTP |
| 4399 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 4400 | OS << NTTP->getType().getAsString(Policy); |
| 4401 | else |
| 4402 | OS << "template<...> class"; |
| 4403 | } |
| 4404 | |
| 4405 | OS << ">"; |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4406 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4407 | } |
| 4408 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4409 | if (const ClassTemplateSpecializationDecl *ClassSpec |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4410 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 4411 | // If the type was explicitly written, use that. |
| 4412 | if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten()) |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4413 | return cxstring::createDup(TSInfo->getType().getAsString(Policy)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4414 | |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 4415 | SmallString<128> Str; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4416 | llvm::raw_svector_ostream OS(Str); |
| 4417 | OS << *ClassSpec; |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 4418 | TemplateSpecializationType::PrintTemplateArgumentList(OS, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4419 | ClassSpec->getTemplateArgs().data(), |
| 4420 | ClassSpec->getTemplateArgs().size(), |
| 4421 | Policy); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 4422 | return cxstring::createDup(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4423 | } |
| 4424 | |
| 4425 | return clang_getCursorSpelling(C); |
| 4426 | } |
| 4427 | |
| 4428 | CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { |
| 4429 | switch (Kind) { |
| 4430 | case CXCursor_FunctionDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4431 | return cxstring::createRef("FunctionDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4432 | case CXCursor_TypedefDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4433 | return cxstring::createRef("TypedefDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4434 | case CXCursor_EnumDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4435 | return cxstring::createRef("EnumDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4436 | case CXCursor_EnumConstantDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4437 | return cxstring::createRef("EnumConstantDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4438 | case CXCursor_StructDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4439 | return cxstring::createRef("StructDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4440 | case CXCursor_UnionDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4441 | return cxstring::createRef("UnionDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4442 | case CXCursor_ClassDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4443 | return cxstring::createRef("ClassDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4444 | case CXCursor_FieldDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4445 | return cxstring::createRef("FieldDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4446 | case CXCursor_VarDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4447 | return cxstring::createRef("VarDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4448 | case CXCursor_ParmDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4449 | return cxstring::createRef("ParmDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4450 | case CXCursor_ObjCInterfaceDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4451 | return cxstring::createRef("ObjCInterfaceDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4452 | case CXCursor_ObjCCategoryDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4453 | return cxstring::createRef("ObjCCategoryDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4454 | case CXCursor_ObjCProtocolDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4455 | return cxstring::createRef("ObjCProtocolDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4456 | case CXCursor_ObjCPropertyDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4457 | return cxstring::createRef("ObjCPropertyDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4458 | case CXCursor_ObjCIvarDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4459 | return cxstring::createRef("ObjCIvarDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4460 | case CXCursor_ObjCInstanceMethodDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4461 | return cxstring::createRef("ObjCInstanceMethodDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4462 | case CXCursor_ObjCClassMethodDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4463 | return cxstring::createRef("ObjCClassMethodDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4464 | case CXCursor_ObjCImplementationDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4465 | return cxstring::createRef("ObjCImplementationDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4466 | case CXCursor_ObjCCategoryImplDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4467 | return cxstring::createRef("ObjCCategoryImplDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4468 | case CXCursor_CXXMethod: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4469 | return cxstring::createRef("CXXMethod"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4470 | case CXCursor_UnexposedDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4471 | return cxstring::createRef("UnexposedDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4472 | case CXCursor_ObjCSuperClassRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4473 | return cxstring::createRef("ObjCSuperClassRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4474 | case CXCursor_ObjCProtocolRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4475 | return cxstring::createRef("ObjCProtocolRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4476 | case CXCursor_ObjCClassRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4477 | return cxstring::createRef("ObjCClassRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4478 | case CXCursor_TypeRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4479 | return cxstring::createRef("TypeRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4480 | case CXCursor_TemplateRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4481 | return cxstring::createRef("TemplateRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4482 | case CXCursor_NamespaceRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4483 | return cxstring::createRef("NamespaceRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4484 | case CXCursor_MemberRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4485 | return cxstring::createRef("MemberRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4486 | case CXCursor_LabelRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4487 | return cxstring::createRef("LabelRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4488 | case CXCursor_OverloadedDeclRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4489 | return cxstring::createRef("OverloadedDeclRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4490 | case CXCursor_VariableRef: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4491 | return cxstring::createRef("VariableRef"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4492 | case CXCursor_IntegerLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4493 | return cxstring::createRef("IntegerLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4494 | case CXCursor_FloatingLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4495 | return cxstring::createRef("FloatingLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4496 | case CXCursor_ImaginaryLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4497 | return cxstring::createRef("ImaginaryLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4498 | case CXCursor_StringLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4499 | return cxstring::createRef("StringLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4500 | case CXCursor_CharacterLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4501 | return cxstring::createRef("CharacterLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4502 | case CXCursor_ParenExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4503 | return cxstring::createRef("ParenExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4504 | case CXCursor_UnaryOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4505 | return cxstring::createRef("UnaryOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4506 | case CXCursor_ArraySubscriptExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4507 | return cxstring::createRef("ArraySubscriptExpr"); |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 4508 | case CXCursor_OMPArraySectionExpr: |
| 4509 | return cxstring::createRef("OMPArraySectionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4510 | case CXCursor_BinaryOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4511 | return cxstring::createRef("BinaryOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4512 | case CXCursor_CompoundAssignOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4513 | return cxstring::createRef("CompoundAssignOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4514 | case CXCursor_ConditionalOperator: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4515 | return cxstring::createRef("ConditionalOperator"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4516 | case CXCursor_CStyleCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4517 | return cxstring::createRef("CStyleCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4518 | case CXCursor_CompoundLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4519 | return cxstring::createRef("CompoundLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4520 | case CXCursor_InitListExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4521 | return cxstring::createRef("InitListExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4522 | case CXCursor_AddrLabelExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4523 | return cxstring::createRef("AddrLabelExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4524 | case CXCursor_StmtExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4525 | return cxstring::createRef("StmtExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4526 | case CXCursor_GenericSelectionExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4527 | return cxstring::createRef("GenericSelectionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4528 | case CXCursor_GNUNullExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4529 | return cxstring::createRef("GNUNullExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4530 | case CXCursor_CXXStaticCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4531 | return cxstring::createRef("CXXStaticCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4532 | case CXCursor_CXXDynamicCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4533 | return cxstring::createRef("CXXDynamicCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4534 | case CXCursor_CXXReinterpretCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4535 | return cxstring::createRef("CXXReinterpretCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4536 | case CXCursor_CXXConstCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4537 | return cxstring::createRef("CXXConstCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4538 | case CXCursor_CXXFunctionalCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4539 | return cxstring::createRef("CXXFunctionalCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4540 | case CXCursor_CXXTypeidExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4541 | return cxstring::createRef("CXXTypeidExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4542 | case CXCursor_CXXBoolLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4543 | return cxstring::createRef("CXXBoolLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4544 | case CXCursor_CXXNullPtrLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4545 | return cxstring::createRef("CXXNullPtrLiteralExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4546 | case CXCursor_CXXThisExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4547 | return cxstring::createRef("CXXThisExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4548 | case CXCursor_CXXThrowExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4549 | return cxstring::createRef("CXXThrowExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4550 | case CXCursor_CXXNewExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4551 | return cxstring::createRef("CXXNewExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4552 | case CXCursor_CXXDeleteExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4553 | return cxstring::createRef("CXXDeleteExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4554 | case CXCursor_UnaryExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4555 | return cxstring::createRef("UnaryExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4556 | case CXCursor_ObjCStringLiteral: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4557 | return cxstring::createRef("ObjCStringLiteral"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4558 | case CXCursor_ObjCBoolLiteralExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4559 | return cxstring::createRef("ObjCBoolLiteralExpr"); |
Argyrios Kyrtzidis | c2233be | 2013-04-23 17:57:17 +0000 | [diff] [blame] | 4560 | case CXCursor_ObjCSelfExpr: |
| 4561 | return cxstring::createRef("ObjCSelfExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4562 | case CXCursor_ObjCEncodeExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4563 | return cxstring::createRef("ObjCEncodeExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4564 | case CXCursor_ObjCSelectorExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4565 | return cxstring::createRef("ObjCSelectorExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4566 | case CXCursor_ObjCProtocolExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4567 | return cxstring::createRef("ObjCProtocolExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4568 | case CXCursor_ObjCBridgedCastExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4569 | return cxstring::createRef("ObjCBridgedCastExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4570 | case CXCursor_BlockExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4571 | return cxstring::createRef("BlockExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4572 | case CXCursor_PackExpansionExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4573 | return cxstring::createRef("PackExpansionExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4574 | case CXCursor_SizeOfPackExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4575 | return cxstring::createRef("SizeOfPackExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4576 | case CXCursor_LambdaExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4577 | return cxstring::createRef("LambdaExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4578 | case CXCursor_UnexposedExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4579 | return cxstring::createRef("UnexposedExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4580 | case CXCursor_DeclRefExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4581 | return cxstring::createRef("DeclRefExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4582 | case CXCursor_MemberRefExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4583 | return cxstring::createRef("MemberRefExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4584 | case CXCursor_CallExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4585 | return cxstring::createRef("CallExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4586 | case CXCursor_ObjCMessageExpr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4587 | return cxstring::createRef("ObjCMessageExpr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4588 | case CXCursor_UnexposedStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4589 | return cxstring::createRef("UnexposedStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4590 | case CXCursor_DeclStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4591 | return cxstring::createRef("DeclStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4592 | case CXCursor_LabelStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4593 | return cxstring::createRef("LabelStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4594 | case CXCursor_CompoundStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4595 | return cxstring::createRef("CompoundStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4596 | case CXCursor_CaseStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4597 | return cxstring::createRef("CaseStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4598 | case CXCursor_DefaultStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4599 | return cxstring::createRef("DefaultStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4600 | case CXCursor_IfStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4601 | return cxstring::createRef("IfStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4602 | case CXCursor_SwitchStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4603 | return cxstring::createRef("SwitchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4604 | case CXCursor_WhileStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4605 | return cxstring::createRef("WhileStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4606 | case CXCursor_DoStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4607 | return cxstring::createRef("DoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4608 | case CXCursor_ForStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4609 | return cxstring::createRef("ForStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4610 | case CXCursor_GotoStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4611 | return cxstring::createRef("GotoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4612 | case CXCursor_IndirectGotoStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4613 | return cxstring::createRef("IndirectGotoStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4614 | case CXCursor_ContinueStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4615 | return cxstring::createRef("ContinueStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4616 | case CXCursor_BreakStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4617 | return cxstring::createRef("BreakStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4618 | case CXCursor_ReturnStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4619 | return cxstring::createRef("ReturnStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4620 | case CXCursor_GCCAsmStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4621 | return cxstring::createRef("GCCAsmStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4622 | case CXCursor_MSAsmStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4623 | return cxstring::createRef("MSAsmStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4624 | case CXCursor_ObjCAtTryStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4625 | return cxstring::createRef("ObjCAtTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4626 | case CXCursor_ObjCAtCatchStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4627 | return cxstring::createRef("ObjCAtCatchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4628 | case CXCursor_ObjCAtFinallyStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4629 | return cxstring::createRef("ObjCAtFinallyStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4630 | case CXCursor_ObjCAtThrowStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4631 | return cxstring::createRef("ObjCAtThrowStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4632 | case CXCursor_ObjCAtSynchronizedStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4633 | return cxstring::createRef("ObjCAtSynchronizedStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4634 | case CXCursor_ObjCAutoreleasePoolStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4635 | return cxstring::createRef("ObjCAutoreleasePoolStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4636 | case CXCursor_ObjCForCollectionStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4637 | return cxstring::createRef("ObjCForCollectionStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4638 | case CXCursor_CXXCatchStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4639 | return cxstring::createRef("CXXCatchStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4640 | case CXCursor_CXXTryStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4641 | return cxstring::createRef("CXXTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4642 | case CXCursor_CXXForRangeStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4643 | return cxstring::createRef("CXXForRangeStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4644 | case CXCursor_SEHTryStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4645 | return cxstring::createRef("SEHTryStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4646 | case CXCursor_SEHExceptStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4647 | return cxstring::createRef("SEHExceptStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4648 | case CXCursor_SEHFinallyStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4649 | return cxstring::createRef("SEHFinallyStmt"); |
Nico Weber | 9b98207 | 2014-07-07 00:12:30 +0000 | [diff] [blame] | 4650 | case CXCursor_SEHLeaveStmt: |
| 4651 | return cxstring::createRef("SEHLeaveStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4652 | case CXCursor_NullStmt: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4653 | return cxstring::createRef("NullStmt"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4654 | case CXCursor_InvalidFile: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4655 | return cxstring::createRef("InvalidFile"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4656 | case CXCursor_InvalidCode: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4657 | return cxstring::createRef("InvalidCode"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4658 | case CXCursor_NoDeclFound: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4659 | return cxstring::createRef("NoDeclFound"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4660 | case CXCursor_NotImplemented: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4661 | return cxstring::createRef("NotImplemented"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4662 | case CXCursor_TranslationUnit: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4663 | return cxstring::createRef("TranslationUnit"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4664 | case CXCursor_UnexposedAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4665 | return cxstring::createRef("UnexposedAttr"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4666 | case CXCursor_IBActionAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4667 | return cxstring::createRef("attribute(ibaction)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4668 | case CXCursor_IBOutletAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4669 | return cxstring::createRef("attribute(iboutlet)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4670 | case CXCursor_IBOutletCollectionAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4671 | return cxstring::createRef("attribute(iboutletcollection)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4672 | case CXCursor_CXXFinalAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4673 | return cxstring::createRef("attribute(final)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4674 | case CXCursor_CXXOverrideAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4675 | return cxstring::createRef("attribute(override)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4676 | case CXCursor_AnnotateAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4677 | return cxstring::createRef("attribute(annotate)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4678 | case CXCursor_AsmLabelAttr: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4679 | return cxstring::createRef("asm label"); |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 4680 | case CXCursor_PackedAttr: |
| 4681 | return cxstring::createRef("attribute(packed)"); |
Joey Gouly | 8122838 | 2014-05-01 15:41:58 +0000 | [diff] [blame] | 4682 | case CXCursor_PureAttr: |
| 4683 | return cxstring::createRef("attribute(pure)"); |
| 4684 | case CXCursor_ConstAttr: |
| 4685 | return cxstring::createRef("attribute(const)"); |
| 4686 | case CXCursor_NoDuplicateAttr: |
| 4687 | return cxstring::createRef("attribute(noduplicate)"); |
Eli Bendersky | 2581e66 | 2014-05-28 19:29:58 +0000 | [diff] [blame] | 4688 | case CXCursor_CUDAConstantAttr: |
| 4689 | return cxstring::createRef("attribute(constant)"); |
| 4690 | case CXCursor_CUDADeviceAttr: |
| 4691 | return cxstring::createRef("attribute(device)"); |
| 4692 | case CXCursor_CUDAGlobalAttr: |
| 4693 | return cxstring::createRef("attribute(global)"); |
| 4694 | case CXCursor_CUDAHostAttr: |
| 4695 | return cxstring::createRef("attribute(host)"); |
Eli Bendersky | 9b07147 | 2014-08-08 14:59:00 +0000 | [diff] [blame] | 4696 | case CXCursor_CUDASharedAttr: |
| 4697 | return cxstring::createRef("attribute(shared)"); |
Saleem Abdulrasool | 79c6971 | 2015-09-05 18:53:43 +0000 | [diff] [blame] | 4698 | case CXCursor_VisibilityAttr: |
| 4699 | return cxstring::createRef("attribute(visibility)"); |
Saleem Abdulrasool | 8aa0b80 | 2015-12-10 18:45:18 +0000 | [diff] [blame] | 4700 | case CXCursor_DLLExport: |
| 4701 | return cxstring::createRef("attribute(dllexport)"); |
| 4702 | case CXCursor_DLLImport: |
| 4703 | return cxstring::createRef("attribute(dllimport)"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4704 | case CXCursor_PreprocessingDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4705 | return cxstring::createRef("preprocessing directive"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4706 | case CXCursor_MacroDefinition: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4707 | return cxstring::createRef("macro definition"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4708 | case CXCursor_MacroExpansion: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4709 | return cxstring::createRef("macro expansion"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4710 | case CXCursor_InclusionDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4711 | return cxstring::createRef("inclusion directive"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4712 | case CXCursor_Namespace: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4713 | return cxstring::createRef("Namespace"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4714 | case CXCursor_LinkageSpec: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4715 | return cxstring::createRef("LinkageSpec"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4716 | case CXCursor_CXXBaseSpecifier: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4717 | return cxstring::createRef("C++ base class specifier"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4718 | case CXCursor_Constructor: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4719 | return cxstring::createRef("CXXConstructor"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4720 | case CXCursor_Destructor: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4721 | return cxstring::createRef("CXXDestructor"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4722 | case CXCursor_ConversionFunction: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4723 | return cxstring::createRef("CXXConversion"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4724 | case CXCursor_TemplateTypeParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4725 | return cxstring::createRef("TemplateTypeParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4726 | case CXCursor_NonTypeTemplateParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4727 | return cxstring::createRef("NonTypeTemplateParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4728 | case CXCursor_TemplateTemplateParameter: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4729 | return cxstring::createRef("TemplateTemplateParameter"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4730 | case CXCursor_FunctionTemplate: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4731 | return cxstring::createRef("FunctionTemplate"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4732 | case CXCursor_ClassTemplate: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4733 | return cxstring::createRef("ClassTemplate"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4734 | case CXCursor_ClassTemplatePartialSpecialization: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4735 | return cxstring::createRef("ClassTemplatePartialSpecialization"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4736 | case CXCursor_NamespaceAlias: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4737 | return cxstring::createRef("NamespaceAlias"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4738 | case CXCursor_UsingDirective: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4739 | return cxstring::createRef("UsingDirective"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4740 | case CXCursor_UsingDeclaration: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4741 | return cxstring::createRef("UsingDeclaration"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4742 | case CXCursor_TypeAliasDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4743 | return cxstring::createRef("TypeAliasDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4744 | case CXCursor_ObjCSynthesizeDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4745 | return cxstring::createRef("ObjCSynthesizeDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4746 | case CXCursor_ObjCDynamicDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4747 | return cxstring::createRef("ObjCDynamicDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4748 | case CXCursor_CXXAccessSpecifier: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4749 | return cxstring::createRef("CXXAccessSpecifier"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4750 | case CXCursor_ModuleImportDecl: |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 4751 | return cxstring::createRef("ModuleImport"); |
Alexey Bataev | 5ec3eb1 | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 4752 | case CXCursor_OMPParallelDirective: |
Alexey Bataev | 1b59ab5 | 2014-02-27 08:29:12 +0000 | [diff] [blame] | 4753 | return cxstring::createRef("OMPParallelDirective"); |
| 4754 | case CXCursor_OMPSimdDirective: |
| 4755 | return cxstring::createRef("OMPSimdDirective"); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 4756 | case CXCursor_OMPForDirective: |
| 4757 | return cxstring::createRef("OMPForDirective"); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 4758 | case CXCursor_OMPForSimdDirective: |
| 4759 | return cxstring::createRef("OMPForSimdDirective"); |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 4760 | case CXCursor_OMPSectionsDirective: |
| 4761 | return cxstring::createRef("OMPSectionsDirective"); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 4762 | case CXCursor_OMPSectionDirective: |
| 4763 | return cxstring::createRef("OMPSectionDirective"); |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 4764 | case CXCursor_OMPSingleDirective: |
| 4765 | return cxstring::createRef("OMPSingleDirective"); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 4766 | case CXCursor_OMPMasterDirective: |
| 4767 | return cxstring::createRef("OMPMasterDirective"); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 4768 | case CXCursor_OMPCriticalDirective: |
| 4769 | return cxstring::createRef("OMPCriticalDirective"); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 4770 | case CXCursor_OMPParallelForDirective: |
| 4771 | return cxstring::createRef("OMPParallelForDirective"); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 4772 | case CXCursor_OMPParallelForSimdDirective: |
| 4773 | return cxstring::createRef("OMPParallelForSimdDirective"); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 4774 | case CXCursor_OMPParallelSectionsDirective: |
| 4775 | return cxstring::createRef("OMPParallelSectionsDirective"); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 4776 | case CXCursor_OMPTaskDirective: |
| 4777 | return cxstring::createRef("OMPTaskDirective"); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 4778 | case CXCursor_OMPTaskyieldDirective: |
| 4779 | return cxstring::createRef("OMPTaskyieldDirective"); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 4780 | case CXCursor_OMPBarrierDirective: |
| 4781 | return cxstring::createRef("OMPBarrierDirective"); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 4782 | case CXCursor_OMPTaskwaitDirective: |
| 4783 | return cxstring::createRef("OMPTaskwaitDirective"); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 4784 | case CXCursor_OMPTaskgroupDirective: |
| 4785 | return cxstring::createRef("OMPTaskgroupDirective"); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 4786 | case CXCursor_OMPFlushDirective: |
| 4787 | return cxstring::createRef("OMPFlushDirective"); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 4788 | case CXCursor_OMPOrderedDirective: |
| 4789 | return cxstring::createRef("OMPOrderedDirective"); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 4790 | case CXCursor_OMPAtomicDirective: |
| 4791 | return cxstring::createRef("OMPAtomicDirective"); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 4792 | case CXCursor_OMPTargetDirective: |
| 4793 | return cxstring::createRef("OMPTargetDirective"); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 4794 | case CXCursor_OMPTargetDataDirective: |
| 4795 | return cxstring::createRef("OMPTargetDataDirective"); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 4796 | case CXCursor_OMPTargetEnterDataDirective: |
| 4797 | return cxstring::createRef("OMPTargetEnterDataDirective"); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 4798 | case CXCursor_OMPTargetExitDataDirective: |
| 4799 | return cxstring::createRef("OMPTargetExitDataDirective"); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 4800 | case CXCursor_OMPTargetParallelDirective: |
| 4801 | return cxstring::createRef("OMPTargetParallelDirective"); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 4802 | case CXCursor_OMPTargetParallelForDirective: |
| 4803 | return cxstring::createRef("OMPTargetParallelForDirective"); |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 4804 | case CXCursor_OMPTargetUpdateDirective: |
| 4805 | return cxstring::createRef("OMPTargetUpdateDirective"); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 4806 | case CXCursor_OMPTeamsDirective: |
| 4807 | return cxstring::createRef("OMPTeamsDirective"); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 4808 | case CXCursor_OMPCancellationPointDirective: |
| 4809 | return cxstring::createRef("OMPCancellationPointDirective"); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 4810 | case CXCursor_OMPCancelDirective: |
| 4811 | return cxstring::createRef("OMPCancelDirective"); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 4812 | case CXCursor_OMPTaskLoopDirective: |
| 4813 | return cxstring::createRef("OMPTaskLoopDirective"); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 4814 | case CXCursor_OMPTaskLoopSimdDirective: |
| 4815 | return cxstring::createRef("OMPTaskLoopSimdDirective"); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 4816 | case CXCursor_OMPDistributeDirective: |
| 4817 | return cxstring::createRef("OMPDistributeDirective"); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 4818 | case CXCursor_OverloadCandidate: |
| 4819 | return cxstring::createRef("OverloadCandidate"); |
Sergey Kalinichev | 8f3b187 | 2015-11-15 13:48:32 +0000 | [diff] [blame] | 4820 | case CXCursor_TypeAliasTemplateDecl: |
| 4821 | return cxstring::createRef("TypeAliasTemplateDecl"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4822 | } |
| 4823 | |
| 4824 | llvm_unreachable("Unhandled CXCursorKind"); |
| 4825 | } |
| 4826 | |
| 4827 | struct GetCursorData { |
| 4828 | SourceLocation TokenBeginLoc; |
| 4829 | bool PointsAtMacroArgExpansion; |
| 4830 | bool VisitedObjCPropertyImplDecl; |
| 4831 | SourceLocation VisitedDeclaratorDeclStartLoc; |
| 4832 | CXCursor &BestCursor; |
| 4833 | |
| 4834 | GetCursorData(SourceManager &SM, |
| 4835 | SourceLocation tokenBegin, CXCursor &outputCursor) |
| 4836 | : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) { |
| 4837 | PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin); |
| 4838 | VisitedObjCPropertyImplDecl = false; |
| 4839 | } |
| 4840 | }; |
| 4841 | |
| 4842 | static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, |
| 4843 | CXCursor parent, |
| 4844 | CXClientData client_data) { |
| 4845 | GetCursorData *Data = static_cast<GetCursorData *>(client_data); |
| 4846 | CXCursor *BestCursor = &Data->BestCursor; |
| 4847 | |
| 4848 | // If we point inside a macro argument we should provide info of what the |
| 4849 | // token is so use the actual cursor, don't replace it with a macro expansion |
| 4850 | // cursor. |
| 4851 | if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion) |
| 4852 | return CXChildVisit_Recurse; |
| 4853 | |
| 4854 | if (clang_isDeclaration(cursor.kind)) { |
| 4855 | // Avoid having the implicit methods override the property decls. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4856 | if (const ObjCMethodDecl *MD |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4857 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 4858 | if (MD->isImplicit()) |
| 4859 | return CXChildVisit_Break; |
| 4860 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4861 | } else if (const ObjCInterfaceDecl *ID |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4862 | = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(cursor))) { |
| 4863 | // Check that when we have multiple @class references in the same line, |
| 4864 | // that later ones do not override the previous ones. |
| 4865 | // If we have: |
| 4866 | // @class Foo, Bar; |
| 4867 | // source ranges for both start at '@', so 'Bar' will end up overriding |
| 4868 | // 'Foo' even though the cursor location was at 'Foo'. |
| 4869 | if (BestCursor->kind == CXCursor_ObjCInterfaceDecl || |
| 4870 | BestCursor->kind == CXCursor_ObjCClassRef) |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4871 | if (const ObjCInterfaceDecl *PrevID |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4872 | = dyn_cast_or_null<ObjCInterfaceDecl>(getCursorDecl(*BestCursor))){ |
| 4873 | if (PrevID != ID && |
| 4874 | !PrevID->isThisDeclarationADefinition() && |
| 4875 | !ID->isThisDeclarationADefinition()) |
| 4876 | return CXChildVisit_Break; |
| 4877 | } |
| 4878 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4879 | } else if (const DeclaratorDecl *DD |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4880 | = dyn_cast_or_null<DeclaratorDecl>(getCursorDecl(cursor))) { |
| 4881 | SourceLocation StartLoc = DD->getSourceRange().getBegin(); |
| 4882 | // Check that when we have multiple declarators in the same line, |
| 4883 | // that later ones do not override the previous ones. |
| 4884 | // If we have: |
| 4885 | // int Foo, Bar; |
| 4886 | // source ranges for both start at 'int', so 'Bar' will end up overriding |
| 4887 | // 'Foo' even though the cursor location was at 'Foo'. |
| 4888 | if (Data->VisitedDeclaratorDeclStartLoc == StartLoc) |
| 4889 | return CXChildVisit_Break; |
| 4890 | Data->VisitedDeclaratorDeclStartLoc = StartLoc; |
| 4891 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4892 | } else if (const ObjCPropertyImplDecl *PropImp |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4893 | = dyn_cast_or_null<ObjCPropertyImplDecl>(getCursorDecl(cursor))) { |
| 4894 | (void)PropImp; |
| 4895 | // Check that when we have multiple @synthesize in the same line, |
| 4896 | // that later ones do not override the previous ones. |
| 4897 | // If we have: |
| 4898 | // @synthesize Foo, Bar; |
| 4899 | // source ranges for both start at '@', so 'Bar' will end up overriding |
| 4900 | // 'Foo' even though the cursor location was at 'Foo'. |
| 4901 | if (Data->VisitedObjCPropertyImplDecl) |
| 4902 | return CXChildVisit_Break; |
| 4903 | Data->VisitedObjCPropertyImplDecl = true; |
| 4904 | } |
| 4905 | } |
| 4906 | |
| 4907 | if (clang_isExpression(cursor.kind) && |
| 4908 | clang_isDeclaration(BestCursor->kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 4909 | if (const Decl *D = getCursorDecl(*BestCursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4910 | // Avoid having the cursor of an expression replace the declaration cursor |
| 4911 | // when the expression source range overlaps the declaration range. |
| 4912 | // This can happen for C++ constructor expressions whose range generally |
| 4913 | // include the variable declaration, e.g.: |
| 4914 | // MyCXXClass foo; // Make sure pointing at 'foo' returns a VarDecl cursor. |
| 4915 | if (D->getLocation().isValid() && Data->TokenBeginLoc.isValid() && |
| 4916 | D->getLocation() == Data->TokenBeginLoc) |
| 4917 | return CXChildVisit_Break; |
| 4918 | } |
| 4919 | } |
| 4920 | |
| 4921 | // If our current best cursor is the construction of a temporary object, |
| 4922 | // don't replace that cursor with a type reference, because we want |
| 4923 | // clang_getCursor() to point at the constructor. |
| 4924 | if (clang_isExpression(BestCursor->kind) && |
| 4925 | isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) && |
| 4926 | cursor.kind == CXCursor_TypeRef) { |
| 4927 | // Keep the cursor pointing at CXXTemporaryObjectExpr but also mark it |
| 4928 | // as having the actual point on the type reference. |
| 4929 | *BestCursor = getTypeRefedCallExprCursor(*BestCursor); |
| 4930 | return CXChildVisit_Recurse; |
| 4931 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4932 | |
| 4933 | // If we already have an Objective-C superclass reference, don't |
| 4934 | // update it further. |
| 4935 | if (BestCursor->kind == CXCursor_ObjCSuperClassRef) |
| 4936 | return CXChildVisit_Break; |
| 4937 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4938 | *BestCursor = cursor; |
| 4939 | return CXChildVisit_Recurse; |
| 4940 | } |
| 4941 | |
| 4942 | CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 4943 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4944 | LOG_BAD_TU(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4945 | return clang_getNullCursor(); |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 4946 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4947 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 4948 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4949 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 4950 | |
| 4951 | SourceLocation SLoc = cxloc::translateSourceLocation(Loc); |
| 4952 | CXCursor Result = cxcursor::getCursor(TU, SLoc); |
| 4953 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 4954 | LOG_FUNC_SECTION { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4955 | CXFile SearchFile; |
| 4956 | unsigned SearchLine, SearchColumn; |
| 4957 | CXFile ResultFile; |
| 4958 | unsigned ResultLine, ResultColumn; |
| 4959 | CXString SearchFileName, ResultFileName, KindSpelling, USR; |
| 4960 | const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : ""; |
| 4961 | CXSourceLocation ResultLoc = clang_getCursorLocation(Result); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 4962 | |
| 4963 | clang_getFileLocation(Loc, &SearchFile, &SearchLine, &SearchColumn, |
| 4964 | nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 4965 | clang_getFileLocation(ResultLoc, &ResultFile, &ResultLine, |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 4966 | &ResultColumn, nullptr); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4967 | SearchFileName = clang_getFileName(SearchFile); |
| 4968 | ResultFileName = clang_getFileName(ResultFile); |
| 4969 | KindSpelling = clang_getCursorKindSpelling(Result.kind); |
| 4970 | USR = clang_getCursorUSR(Result); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 4971 | *Log << llvm::format("(%s:%d:%d) = %s", |
| 4972 | clang_getCString(SearchFileName), SearchLine, SearchColumn, |
| 4973 | clang_getCString(KindSpelling)) |
| 4974 | << llvm::format("(%s:%d:%d):%s%s", |
| 4975 | clang_getCString(ResultFileName), ResultLine, ResultColumn, |
| 4976 | clang_getCString(USR), IsDef); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4977 | clang_disposeString(SearchFileName); |
| 4978 | clang_disposeString(ResultFileName); |
| 4979 | clang_disposeString(KindSpelling); |
| 4980 | clang_disposeString(USR); |
| 4981 | |
| 4982 | CXCursor Definition = clang_getCursorDefinition(Result); |
| 4983 | if (!clang_equalCursors(Definition, clang_getNullCursor())) { |
| 4984 | CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition); |
| 4985 | CXString DefinitionKindSpelling |
| 4986 | = clang_getCursorKindSpelling(Definition.kind); |
| 4987 | CXFile DefinitionFile; |
| 4988 | unsigned DefinitionLine, DefinitionColumn; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 4989 | clang_getFileLocation(DefinitionLoc, &DefinitionFile, |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 4990 | &DefinitionLine, &DefinitionColumn, nullptr); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4991 | CXString DefinitionFileName = clang_getFileName(DefinitionFile); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 4992 | *Log << llvm::format(" -> %s(%s:%d:%d)", |
| 4993 | clang_getCString(DefinitionKindSpelling), |
| 4994 | clang_getCString(DefinitionFileName), |
| 4995 | DefinitionLine, DefinitionColumn); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4996 | clang_disposeString(DefinitionFileName); |
| 4997 | clang_disposeString(DefinitionKindSpelling); |
| 4998 | } |
| 4999 | } |
| 5000 | |
| 5001 | return Result; |
| 5002 | } |
| 5003 | |
| 5004 | CXCursor clang_getNullCursor(void) { |
| 5005 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
| 5006 | } |
| 5007 | |
| 5008 | unsigned clang_equalCursors(CXCursor X, CXCursor Y) { |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5009 | // Clear out the "FirstInDeclGroup" part in a declaration cursor, since we |
| 5010 | // can't set consistently. For example, when visiting a DeclStmt we will set |
| 5011 | // it but we don't set it on the result of clang_getCursorDefinition for |
| 5012 | // a reference of the same declaration. |
| 5013 | // FIXME: Setting "FirstInDeclGroup" in CXCursors is a hack that only works |
| 5014 | // when visiting a DeclStmt currently, the AST should be enhanced to be able |
| 5015 | // to provide that kind of info. |
| 5016 | if (clang_isDeclaration(X.kind)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5017 | X.data[1] = nullptr; |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5018 | if (clang_isDeclaration(Y.kind)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5019 | Y.data[1] = nullptr; |
Argyrios Kyrtzidis | bf1be59 | 2013-01-08 18:23:28 +0000 | [diff] [blame] | 5020 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5021 | return X == Y; |
| 5022 | } |
| 5023 | |
| 5024 | unsigned clang_hashCursor(CXCursor C) { |
| 5025 | unsigned Index = 0; |
| 5026 | if (clang_isExpression(C.kind) || clang_isStatement(C.kind)) |
| 5027 | Index = 1; |
| 5028 | |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5029 | return llvm::DenseMapInfo<std::pair<unsigned, const void*> >::getHashValue( |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5030 | std::make_pair(C.kind, C.data[Index])); |
| 5031 | } |
| 5032 | |
| 5033 | unsigned clang_isInvalid(enum CXCursorKind K) { |
| 5034 | return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid; |
| 5035 | } |
| 5036 | |
| 5037 | unsigned clang_isDeclaration(enum CXCursorKind K) { |
| 5038 | return (K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl) || |
| 5039 | (K >= CXCursor_FirstExtraDecl && K <= CXCursor_LastExtraDecl); |
| 5040 | } |
| 5041 | |
| 5042 | unsigned clang_isReference(enum CXCursorKind K) { |
| 5043 | return K >= CXCursor_FirstRef && K <= CXCursor_LastRef; |
| 5044 | } |
| 5045 | |
| 5046 | unsigned clang_isExpression(enum CXCursorKind K) { |
| 5047 | return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr; |
| 5048 | } |
| 5049 | |
| 5050 | unsigned clang_isStatement(enum CXCursorKind K) { |
| 5051 | return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt; |
| 5052 | } |
| 5053 | |
| 5054 | unsigned clang_isAttribute(enum CXCursorKind K) { |
| 5055 | return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr; |
| 5056 | } |
| 5057 | |
| 5058 | unsigned clang_isTranslationUnit(enum CXCursorKind K) { |
| 5059 | return K == CXCursor_TranslationUnit; |
| 5060 | } |
| 5061 | |
| 5062 | unsigned clang_isPreprocessing(enum CXCursorKind K) { |
| 5063 | return K >= CXCursor_FirstPreprocessing && K <= CXCursor_LastPreprocessing; |
| 5064 | } |
| 5065 | |
| 5066 | unsigned clang_isUnexposed(enum CXCursorKind K) { |
| 5067 | switch (K) { |
| 5068 | case CXCursor_UnexposedDecl: |
| 5069 | case CXCursor_UnexposedExpr: |
| 5070 | case CXCursor_UnexposedStmt: |
| 5071 | case CXCursor_UnexposedAttr: |
| 5072 | return true; |
| 5073 | default: |
| 5074 | return false; |
| 5075 | } |
| 5076 | } |
| 5077 | |
| 5078 | CXCursorKind clang_getCursorKind(CXCursor C) { |
| 5079 | return C.kind; |
| 5080 | } |
| 5081 | |
| 5082 | CXSourceLocation clang_getCursorLocation(CXCursor C) { |
| 5083 | if (clang_isReference(C.kind)) { |
| 5084 | switch (C.kind) { |
| 5085 | case CXCursor_ObjCSuperClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5086 | std::pair<const ObjCInterfaceDecl *, SourceLocation> P |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5087 | = getCursorObjCSuperClassRef(C); |
| 5088 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5089 | } |
| 5090 | |
| 5091 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5092 | std::pair<const ObjCProtocolDecl *, SourceLocation> P |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5093 | = getCursorObjCProtocolRef(C); |
| 5094 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5095 | } |
| 5096 | |
| 5097 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5098 | std::pair<const ObjCInterfaceDecl *, SourceLocation> P |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5099 | = getCursorObjCClassRef(C); |
| 5100 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5101 | } |
| 5102 | |
| 5103 | case CXCursor_TypeRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5104 | std::pair<const TypeDecl *, SourceLocation> P = getCursorTypeRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5105 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5106 | } |
| 5107 | |
| 5108 | case CXCursor_TemplateRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5109 | std::pair<const TemplateDecl *, SourceLocation> P = |
| 5110 | getCursorTemplateRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5111 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5112 | } |
| 5113 | |
| 5114 | case CXCursor_NamespaceRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5115 | std::pair<const NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5116 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5117 | } |
| 5118 | |
| 5119 | case CXCursor_MemberRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5120 | std::pair<const FieldDecl *, SourceLocation> P = getCursorMemberRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5121 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5122 | } |
| 5123 | |
| 5124 | case CXCursor_VariableRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5125 | std::pair<const VarDecl *, SourceLocation> P = getCursorVariableRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5126 | return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); |
| 5127 | } |
| 5128 | |
| 5129 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5130 | const CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5131 | if (!BaseSpec) |
| 5132 | return clang_getNullLocation(); |
| 5133 | |
| 5134 | if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo()) |
| 5135 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5136 | TSInfo->getTypeLoc().getBeginLoc()); |
| 5137 | |
| 5138 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5139 | BaseSpec->getLocStart()); |
| 5140 | } |
| 5141 | |
| 5142 | case CXCursor_LabelRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5143 | std::pair<const LabelStmt *, SourceLocation> P = getCursorLabelRef(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5144 | return cxloc::translateSourceLocation(getCursorContext(C), P.second); |
| 5145 | } |
| 5146 | |
| 5147 | case CXCursor_OverloadedDeclRef: |
| 5148 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5149 | getCursorOverloadedDeclRef(C).second); |
| 5150 | |
| 5151 | default: |
| 5152 | // FIXME: Need a way to enumerate all non-reference cases. |
| 5153 | llvm_unreachable("Missed a reference kind"); |
| 5154 | } |
| 5155 | } |
| 5156 | |
| 5157 | if (clang_isExpression(C.kind)) |
| 5158 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5159 | getLocationFromExpr(getCursorExpr(C))); |
| 5160 | |
| 5161 | if (clang_isStatement(C.kind)) |
| 5162 | return cxloc::translateSourceLocation(getCursorContext(C), |
| 5163 | getCursorStmt(C)->getLocStart()); |
| 5164 | |
| 5165 | if (C.kind == CXCursor_PreprocessingDirective) { |
| 5166 | SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin(); |
| 5167 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5168 | } |
| 5169 | |
| 5170 | if (C.kind == CXCursor_MacroExpansion) { |
| 5171 | SourceLocation L |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 5172 | = cxcursor::getCursorMacroExpansion(C).getSourceRange().getBegin(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5173 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5174 | } |
| 5175 | |
| 5176 | if (C.kind == CXCursor_MacroDefinition) { |
| 5177 | SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation(); |
| 5178 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5179 | } |
| 5180 | |
| 5181 | if (C.kind == CXCursor_InclusionDirective) { |
| 5182 | SourceLocation L |
| 5183 | = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin(); |
| 5184 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5185 | } |
| 5186 | |
Argyrios Kyrtzidis | 16834f1 | 2013-09-25 00:14:38 +0000 | [diff] [blame] | 5187 | if (clang_isAttribute(C.kind)) { |
| 5188 | SourceLocation L |
| 5189 | = cxcursor::getCursorAttr(C)->getLocation(); |
| 5190 | return cxloc::translateSourceLocation(getCursorContext(C), L); |
| 5191 | } |
| 5192 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5193 | if (!clang_isDeclaration(C.kind)) |
| 5194 | return clang_getNullLocation(); |
| 5195 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5196 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5197 | if (!D) |
| 5198 | return clang_getNullLocation(); |
| 5199 | |
| 5200 | SourceLocation Loc = D->getLocation(); |
| 5201 | // FIXME: Multiple variables declared in a single declaration |
| 5202 | // currently lack the information needed to correctly determine their |
| 5203 | // ranges when accounting for the type-specifier. We use context |
| 5204 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 5205 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5206 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5207 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 5208 | Loc = VD->getLocation(); |
| 5209 | } |
| 5210 | |
| 5211 | // For ObjC methods, give the start location of the method name. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5212 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5213 | Loc = MD->getSelectorStartLoc(); |
| 5214 | |
| 5215 | return cxloc::translateSourceLocation(getCursorContext(C), Loc); |
| 5216 | } |
| 5217 | |
| 5218 | } // end extern "C" |
| 5219 | |
| 5220 | CXCursor cxcursor::getCursor(CXTranslationUnit TU, SourceLocation SLoc) { |
| 5221 | assert(TU); |
| 5222 | |
| 5223 | // Guard against an invalid SourceLocation, or we may assert in one |
| 5224 | // of the following calls. |
| 5225 | if (SLoc.isInvalid()) |
| 5226 | return clang_getNullCursor(); |
| 5227 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 5228 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5229 | |
| 5230 | // Translate the given source location to make it point at the beginning of |
| 5231 | // the token under the cursor. |
| 5232 | SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(), |
| 5233 | CXXUnit->getASTContext().getLangOpts()); |
| 5234 | |
| 5235 | CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 5236 | if (SLoc.isValid()) { |
| 5237 | GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result); |
| 5238 | CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData, |
| 5239 | /*VisitPreprocessorLast=*/true, |
| 5240 | /*VisitIncludedEntities=*/false, |
| 5241 | SourceLocation(SLoc)); |
| 5242 | CursorVis.visitFileRegion(); |
| 5243 | } |
| 5244 | |
| 5245 | return Result; |
| 5246 | } |
| 5247 | |
| 5248 | static SourceRange getRawCursorExtent(CXCursor C) { |
| 5249 | if (clang_isReference(C.kind)) { |
| 5250 | switch (C.kind) { |
| 5251 | case CXCursor_ObjCSuperClassRef: |
| 5252 | return getCursorObjCSuperClassRef(C).second; |
| 5253 | |
| 5254 | case CXCursor_ObjCProtocolRef: |
| 5255 | return getCursorObjCProtocolRef(C).second; |
| 5256 | |
| 5257 | case CXCursor_ObjCClassRef: |
| 5258 | return getCursorObjCClassRef(C).second; |
| 5259 | |
| 5260 | case CXCursor_TypeRef: |
| 5261 | return getCursorTypeRef(C).second; |
| 5262 | |
| 5263 | case CXCursor_TemplateRef: |
| 5264 | return getCursorTemplateRef(C).second; |
| 5265 | |
| 5266 | case CXCursor_NamespaceRef: |
| 5267 | return getCursorNamespaceRef(C).second; |
| 5268 | |
| 5269 | case CXCursor_MemberRef: |
| 5270 | return getCursorMemberRef(C).second; |
| 5271 | |
| 5272 | case CXCursor_CXXBaseSpecifier: |
| 5273 | return getCursorCXXBaseSpecifier(C)->getSourceRange(); |
| 5274 | |
| 5275 | case CXCursor_LabelRef: |
| 5276 | return getCursorLabelRef(C).second; |
| 5277 | |
| 5278 | case CXCursor_OverloadedDeclRef: |
| 5279 | return getCursorOverloadedDeclRef(C).second; |
| 5280 | |
| 5281 | case CXCursor_VariableRef: |
| 5282 | return getCursorVariableRef(C).second; |
| 5283 | |
| 5284 | default: |
| 5285 | // FIXME: Need a way to enumerate all non-reference cases. |
| 5286 | llvm_unreachable("Missed a reference kind"); |
| 5287 | } |
| 5288 | } |
| 5289 | |
| 5290 | if (clang_isExpression(C.kind)) |
| 5291 | return getCursorExpr(C)->getSourceRange(); |
| 5292 | |
| 5293 | if (clang_isStatement(C.kind)) |
| 5294 | return getCursorStmt(C)->getSourceRange(); |
| 5295 | |
| 5296 | if (clang_isAttribute(C.kind)) |
| 5297 | return getCursorAttr(C)->getRange(); |
| 5298 | |
| 5299 | if (C.kind == CXCursor_PreprocessingDirective) |
| 5300 | return cxcursor::getCursorPreprocessingDirective(C); |
| 5301 | |
| 5302 | if (C.kind == CXCursor_MacroExpansion) { |
| 5303 | ASTUnit *TU = getCursorASTUnit(C); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 5304 | SourceRange Range = cxcursor::getCursorMacroExpansion(C).getSourceRange(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5305 | return TU->mapRangeFromPreamble(Range); |
| 5306 | } |
| 5307 | |
| 5308 | if (C.kind == CXCursor_MacroDefinition) { |
| 5309 | ASTUnit *TU = getCursorASTUnit(C); |
| 5310 | SourceRange Range = cxcursor::getCursorMacroDefinition(C)->getSourceRange(); |
| 5311 | return TU->mapRangeFromPreamble(Range); |
| 5312 | } |
| 5313 | |
| 5314 | if (C.kind == CXCursor_InclusionDirective) { |
| 5315 | ASTUnit *TU = getCursorASTUnit(C); |
| 5316 | SourceRange Range = cxcursor::getCursorInclusionDirective(C)->getSourceRange(); |
| 5317 | return TU->mapRangeFromPreamble(Range); |
| 5318 | } |
| 5319 | |
| 5320 | if (C.kind == CXCursor_TranslationUnit) { |
| 5321 | ASTUnit *TU = getCursorASTUnit(C); |
| 5322 | FileID MainID = TU->getSourceManager().getMainFileID(); |
| 5323 | SourceLocation Start = TU->getSourceManager().getLocForStartOfFile(MainID); |
| 5324 | SourceLocation End = TU->getSourceManager().getLocForEndOfFile(MainID); |
| 5325 | return SourceRange(Start, End); |
| 5326 | } |
| 5327 | |
| 5328 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5329 | const Decl *D = cxcursor::getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5330 | if (!D) |
| 5331 | return SourceRange(); |
| 5332 | |
| 5333 | SourceRange R = D->getSourceRange(); |
| 5334 | // FIXME: Multiple variables declared in a single declaration |
| 5335 | // currently lack the information needed to correctly determine their |
| 5336 | // ranges when accounting for the type-specifier. We use context |
| 5337 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 5338 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5339 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5340 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 5341 | R.setBegin(VD->getLocation()); |
| 5342 | } |
| 5343 | return R; |
| 5344 | } |
| 5345 | return SourceRange(); |
| 5346 | } |
| 5347 | |
| 5348 | /// \brief Retrieves the "raw" cursor extent, which is then extended to include |
| 5349 | /// the decl-specifier-seq for declarations. |
| 5350 | static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) { |
| 5351 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5352 | const Decl *D = cxcursor::getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5353 | if (!D) |
| 5354 | return SourceRange(); |
| 5355 | |
| 5356 | SourceRange R = D->getSourceRange(); |
| 5357 | |
| 5358 | // Adjust the start of the location for declarations preceded by |
| 5359 | // declaration specifiers. |
| 5360 | SourceLocation StartLoc; |
| 5361 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
| 5362 | if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) |
| 5363 | StartLoc = TI->getTypeLoc().getLocStart(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5364 | } else if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5365 | if (TypeSourceInfo *TI = Typedef->getTypeSourceInfo()) |
| 5366 | StartLoc = TI->getTypeLoc().getLocStart(); |
| 5367 | } |
| 5368 | |
| 5369 | if (StartLoc.isValid() && R.getBegin().isValid() && |
| 5370 | SrcMgr.isBeforeInTranslationUnit(StartLoc, R.getBegin())) |
| 5371 | R.setBegin(StartLoc); |
| 5372 | |
| 5373 | // FIXME: Multiple variables declared in a single declaration |
| 5374 | // currently lack the information needed to correctly determine their |
| 5375 | // ranges when accounting for the type-specifier. We use context |
| 5376 | // stored in the CXCursor to determine if the VarDecl is in a DeclGroup, |
| 5377 | // and if so, whether it is the first decl. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5378 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5379 | if (!cxcursor::isFirstInDeclGroup(C)) |
| 5380 | R.setBegin(VD->getLocation()); |
| 5381 | } |
| 5382 | |
| 5383 | return R; |
| 5384 | } |
| 5385 | |
| 5386 | return getRawCursorExtent(C); |
| 5387 | } |
| 5388 | |
| 5389 | extern "C" { |
| 5390 | |
| 5391 | CXSourceRange clang_getCursorExtent(CXCursor C) { |
| 5392 | SourceRange R = getRawCursorExtent(C); |
| 5393 | if (R.isInvalid()) |
| 5394 | return clang_getNullRange(); |
| 5395 | |
| 5396 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 5397 | } |
| 5398 | |
| 5399 | CXCursor clang_getCursorReferenced(CXCursor C) { |
| 5400 | if (clang_isInvalid(C.kind)) |
| 5401 | return clang_getNullCursor(); |
| 5402 | |
| 5403 | CXTranslationUnit tu = getCursorTU(C); |
| 5404 | if (clang_isDeclaration(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5405 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5406 | if (!D) |
| 5407 | return clang_getNullCursor(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5408 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5409 | return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5410 | if (const ObjCPropertyImplDecl *PropImpl = |
| 5411 | dyn_cast<ObjCPropertyImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5412 | if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl()) |
| 5413 | return MakeCXCursor(Property, tu); |
| 5414 | |
| 5415 | return C; |
| 5416 | } |
| 5417 | |
| 5418 | if (clang_isExpression(C.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5419 | const Expr *E = getCursorExpr(C); |
| 5420 | const Decl *D = getDeclFromExpr(E); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5421 | if (D) { |
| 5422 | CXCursor declCursor = MakeCXCursor(D, tu); |
| 5423 | declCursor = getSelectorIdentifierCursor(getSelectorIdentifierIndex(C), |
| 5424 | declCursor); |
| 5425 | return declCursor; |
| 5426 | } |
| 5427 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5428 | if (const OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5429 | return MakeCursorOverloadedDeclRef(Ovl, tu); |
| 5430 | |
| 5431 | return clang_getNullCursor(); |
| 5432 | } |
| 5433 | |
| 5434 | if (clang_isStatement(C.kind)) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 5435 | const Stmt *S = getCursorStmt(C); |
| 5436 | if (const GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5437 | if (LabelDecl *label = Goto->getLabel()) |
| 5438 | if (LabelStmt *labelS = label->getStmt()) |
| 5439 | return MakeCXCursor(labelS, getCursorDecl(C), tu); |
| 5440 | |
| 5441 | return clang_getNullCursor(); |
| 5442 | } |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 5443 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5444 | if (C.kind == CXCursor_MacroExpansion) { |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 5445 | if (const MacroDefinitionRecord *Def = |
| 5446 | getCursorMacroExpansion(C).getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5447 | return MakeMacroDefinitionCursor(Def, tu); |
| 5448 | } |
| 5449 | |
| 5450 | if (!clang_isReference(C.kind)) |
| 5451 | return clang_getNullCursor(); |
| 5452 | |
| 5453 | switch (C.kind) { |
| 5454 | case CXCursor_ObjCSuperClassRef: |
| 5455 | return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu); |
| 5456 | |
| 5457 | case CXCursor_ObjCProtocolRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5458 | const ObjCProtocolDecl *Prot = getCursorObjCProtocolRef(C).first; |
| 5459 | if (const ObjCProtocolDecl *Def = Prot->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5460 | return MakeCXCursor(Def, tu); |
| 5461 | |
| 5462 | return MakeCXCursor(Prot, tu); |
| 5463 | } |
| 5464 | |
| 5465 | case CXCursor_ObjCClassRef: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5466 | const ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first; |
| 5467 | if (const ObjCInterfaceDecl *Def = Class->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5468 | return MakeCXCursor(Def, tu); |
| 5469 | |
| 5470 | return MakeCXCursor(Class, tu); |
| 5471 | } |
| 5472 | |
| 5473 | case CXCursor_TypeRef: |
| 5474 | return MakeCXCursor(getCursorTypeRef(C).first, tu ); |
| 5475 | |
| 5476 | case CXCursor_TemplateRef: |
| 5477 | return MakeCXCursor(getCursorTemplateRef(C).first, tu ); |
| 5478 | |
| 5479 | case CXCursor_NamespaceRef: |
| 5480 | return MakeCXCursor(getCursorNamespaceRef(C).first, tu ); |
| 5481 | |
| 5482 | case CXCursor_MemberRef: |
| 5483 | return MakeCXCursor(getCursorMemberRef(C).first, tu ); |
| 5484 | |
| 5485 | case CXCursor_CXXBaseSpecifier: { |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 5486 | const CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5487 | return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), |
| 5488 | tu )); |
| 5489 | } |
| 5490 | |
| 5491 | case CXCursor_LabelRef: |
| 5492 | // FIXME: We end up faking the "parent" declaration here because we |
| 5493 | // don't want to make CXCursor larger. |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 5494 | return MakeCXCursor(getCursorLabelRef(C).first, |
| 5495 | cxtu::getASTUnit(tu)->getASTContext() |
| 5496 | .getTranslationUnitDecl(), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5497 | tu); |
| 5498 | |
| 5499 | case CXCursor_OverloadedDeclRef: |
| 5500 | return C; |
| 5501 | |
| 5502 | case CXCursor_VariableRef: |
| 5503 | return MakeCXCursor(getCursorVariableRef(C).first, tu); |
| 5504 | |
| 5505 | default: |
| 5506 | // We would prefer to enumerate all non-reference cursor kinds here. |
| 5507 | llvm_unreachable("Unhandled reference cursor kind"); |
| 5508 | } |
| 5509 | } |
| 5510 | |
| 5511 | CXCursor clang_getCursorDefinition(CXCursor C) { |
| 5512 | if (clang_isInvalid(C.kind)) |
| 5513 | return clang_getNullCursor(); |
| 5514 | |
| 5515 | CXTranslationUnit TU = getCursorTU(C); |
| 5516 | |
| 5517 | bool WasReference = false; |
| 5518 | if (clang_isReference(C.kind) || clang_isExpression(C.kind)) { |
| 5519 | C = clang_getCursorReferenced(C); |
| 5520 | WasReference = true; |
| 5521 | } |
| 5522 | |
| 5523 | if (C.kind == CXCursor_MacroExpansion) |
| 5524 | return clang_getCursorReferenced(C); |
| 5525 | |
| 5526 | if (!clang_isDeclaration(C.kind)) |
| 5527 | return clang_getNullCursor(); |
| 5528 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5529 | const Decl *D = getCursorDecl(C); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5530 | if (!D) |
| 5531 | return clang_getNullCursor(); |
| 5532 | |
| 5533 | switch (D->getKind()) { |
| 5534 | // Declaration kinds that don't really separate the notions of |
| 5535 | // declaration and definition. |
| 5536 | case Decl::Namespace: |
| 5537 | case Decl::Typedef: |
| 5538 | case Decl::TypeAlias: |
| 5539 | case Decl::TypeAliasTemplate: |
| 5540 | case Decl::TemplateTypeParm: |
| 5541 | case Decl::EnumConstant: |
| 5542 | case Decl::Field: |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 5543 | case Decl::MSProperty: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5544 | case Decl::IndirectField: |
| 5545 | case Decl::ObjCIvar: |
| 5546 | case Decl::ObjCAtDefsField: |
| 5547 | case Decl::ImplicitParam: |
| 5548 | case Decl::ParmVar: |
| 5549 | case Decl::NonTypeTemplateParm: |
| 5550 | case Decl::TemplateTemplateParm: |
| 5551 | case Decl::ObjCCategoryImpl: |
| 5552 | case Decl::ObjCImplementation: |
| 5553 | case Decl::AccessSpec: |
| 5554 | case Decl::LinkageSpec: |
| 5555 | case Decl::ObjCPropertyImpl: |
| 5556 | case Decl::FileScopeAsm: |
| 5557 | case Decl::StaticAssert: |
| 5558 | case Decl::Block: |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 5559 | case Decl::Captured: |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 5560 | case Decl::OMPCapturedExpr: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5561 | case Decl::Label: // FIXME: Is this right?? |
| 5562 | case Decl::ClassScopeFunctionSpecialization: |
| 5563 | case Decl::Import: |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 5564 | case Decl::OMPThreadPrivate: |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 5565 | case Decl::OMPDeclareReduction: |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 5566 | case Decl::ObjCTypeParam: |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 5567 | case Decl::BuiltinTemplate: |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 5568 | case Decl::PragmaComment: |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 5569 | case Decl::PragmaDetectMismatch: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5570 | return C; |
| 5571 | |
| 5572 | // Declaration kinds that don't make any sense here, but are |
| 5573 | // nonetheless harmless. |
David Blaikie | f005d3c | 2013-02-22 17:44:58 +0000 | [diff] [blame] | 5574 | case Decl::Empty: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5575 | case Decl::TranslationUnit: |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 5576 | case Decl::ExternCContext: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5577 | break; |
| 5578 | |
| 5579 | // Declaration kinds for which the definition is not resolvable. |
| 5580 | case Decl::UnresolvedUsingTypename: |
| 5581 | case Decl::UnresolvedUsingValue: |
| 5582 | break; |
| 5583 | |
| 5584 | case Decl::UsingDirective: |
| 5585 | return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(), |
| 5586 | TU); |
| 5587 | |
| 5588 | case Decl::NamespaceAlias: |
| 5589 | return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU); |
| 5590 | |
| 5591 | case Decl::Enum: |
| 5592 | case Decl::Record: |
| 5593 | case Decl::CXXRecord: |
| 5594 | case Decl::ClassTemplateSpecialization: |
| 5595 | case Decl::ClassTemplatePartialSpecialization: |
| 5596 | if (TagDecl *Def = cast<TagDecl>(D)->getDefinition()) |
| 5597 | return MakeCXCursor(Def, TU); |
| 5598 | return clang_getNullCursor(); |
| 5599 | |
| 5600 | case Decl::Function: |
| 5601 | case Decl::CXXMethod: |
| 5602 | case Decl::CXXConstructor: |
| 5603 | case Decl::CXXDestructor: |
| 5604 | case Decl::CXXConversion: { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5605 | const FunctionDecl *Def = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5606 | if (cast<FunctionDecl>(D)->getBody(Def)) |
Dmitri Gribenko | 9c256e3 | 2013-01-14 00:46:27 +0000 | [diff] [blame] | 5607 | return MakeCXCursor(Def, TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5608 | return clang_getNullCursor(); |
| 5609 | } |
| 5610 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5611 | case Decl::Var: |
| 5612 | case Decl::VarTemplateSpecialization: |
| 5613 | case Decl::VarTemplatePartialSpecialization: { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5614 | // Ask the variable if it has a definition. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5615 | if (const VarDecl *Def = cast<VarDecl>(D)->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5616 | return MakeCXCursor(Def, TU); |
| 5617 | return clang_getNullCursor(); |
| 5618 | } |
| 5619 | |
| 5620 | case Decl::FunctionTemplate: { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 5621 | const FunctionDecl *Def = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5622 | if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def)) |
| 5623 | return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU); |
| 5624 | return clang_getNullCursor(); |
| 5625 | } |
| 5626 | |
| 5627 | case Decl::ClassTemplate: { |
| 5628 | if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl() |
| 5629 | ->getDefinition()) |
| 5630 | return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(), |
| 5631 | TU); |
| 5632 | return clang_getNullCursor(); |
| 5633 | } |
| 5634 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5635 | case Decl::VarTemplate: { |
| 5636 | if (VarDecl *Def = |
| 5637 | cast<VarTemplateDecl>(D)->getTemplatedDecl()->getDefinition()) |
| 5638 | return MakeCXCursor(cast<VarDecl>(Def)->getDescribedVarTemplate(), TU); |
| 5639 | return clang_getNullCursor(); |
| 5640 | } |
| 5641 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5642 | case Decl::Using: |
| 5643 | return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D), |
| 5644 | D->getLocation(), TU); |
| 5645 | |
| 5646 | case Decl::UsingShadow: |
| 5647 | return clang_getCursorDefinition( |
| 5648 | MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(), |
| 5649 | TU)); |
| 5650 | |
| 5651 | case Decl::ObjCMethod: { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5652 | const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5653 | if (Method->isThisDeclarationADefinition()) |
| 5654 | return C; |
| 5655 | |
| 5656 | // Dig out the method definition in the associated |
| 5657 | // @implementation, if we have it. |
| 5658 | // FIXME: The ASTs should make finding the definition easier. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5659 | if (const ObjCInterfaceDecl *Class |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5660 | = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) |
| 5661 | if (ObjCImplementationDecl *ClassImpl = Class->getImplementation()) |
| 5662 | if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(), |
| 5663 | Method->isInstanceMethod())) |
| 5664 | if (Def->isThisDeclarationADefinition()) |
| 5665 | return MakeCXCursor(Def, TU); |
| 5666 | |
| 5667 | return clang_getNullCursor(); |
| 5668 | } |
| 5669 | |
| 5670 | case Decl::ObjCCategory: |
| 5671 | if (ObjCCategoryImplDecl *Impl |
| 5672 | = cast<ObjCCategoryDecl>(D)->getImplementation()) |
| 5673 | return MakeCXCursor(Impl, TU); |
| 5674 | return clang_getNullCursor(); |
| 5675 | |
| 5676 | case Decl::ObjCProtocol: |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5677 | if (const ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(D)->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5678 | return MakeCXCursor(Def, TU); |
| 5679 | return clang_getNullCursor(); |
| 5680 | |
| 5681 | case Decl::ObjCInterface: { |
| 5682 | // There are two notions of a "definition" for an Objective-C |
| 5683 | // class: the interface and its implementation. When we resolved a |
| 5684 | // reference to an Objective-C class, produce the @interface as |
| 5685 | // the definition; when we were provided with the interface, |
| 5686 | // produce the @implementation as the definition. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5687 | const ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5688 | if (WasReference) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5689 | if (const ObjCInterfaceDecl *Def = IFace->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5690 | return MakeCXCursor(Def, TU); |
| 5691 | } else if (ObjCImplementationDecl *Impl = IFace->getImplementation()) |
| 5692 | return MakeCXCursor(Impl, TU); |
| 5693 | return clang_getNullCursor(); |
| 5694 | } |
| 5695 | |
| 5696 | case Decl::ObjCProperty: |
| 5697 | // FIXME: We don't really know where to find the |
| 5698 | // ObjCPropertyImplDecls that implement this property. |
| 5699 | return clang_getNullCursor(); |
| 5700 | |
| 5701 | case Decl::ObjCCompatibleAlias: |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5702 | if (const ObjCInterfaceDecl *Class |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5703 | = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface()) |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5704 | if (const ObjCInterfaceDecl *Def = Class->getDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5705 | return MakeCXCursor(Def, TU); |
| 5706 | |
| 5707 | return clang_getNullCursor(); |
| 5708 | |
| 5709 | case Decl::Friend: |
| 5710 | if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl()) |
| 5711 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
| 5712 | return clang_getNullCursor(); |
| 5713 | |
| 5714 | case Decl::FriendTemplate: |
| 5715 | if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl()) |
| 5716 | return clang_getCursorDefinition(MakeCXCursor(Friend, TU)); |
| 5717 | return clang_getNullCursor(); |
| 5718 | } |
| 5719 | |
| 5720 | return clang_getNullCursor(); |
| 5721 | } |
| 5722 | |
| 5723 | unsigned clang_isCursorDefinition(CXCursor C) { |
| 5724 | if (!clang_isDeclaration(C.kind)) |
| 5725 | return 0; |
| 5726 | |
| 5727 | return clang_getCursorDefinition(C) == C; |
| 5728 | } |
| 5729 | |
| 5730 | CXCursor clang_getCanonicalCursor(CXCursor C) { |
| 5731 | if (!clang_isDeclaration(C.kind)) |
| 5732 | return C; |
| 5733 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5734 | if (const Decl *D = getCursorDecl(C)) { |
| 5735 | if (const ObjCCategoryImplDecl *CatImplD = dyn_cast<ObjCCategoryImplDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5736 | if (ObjCCategoryDecl *CatD = CatImplD->getCategoryDecl()) |
| 5737 | return MakeCXCursor(CatD, getCursorTU(C)); |
| 5738 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5739 | if (const ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) |
| 5740 | if (const ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5741 | return MakeCXCursor(IFD, getCursorTU(C)); |
| 5742 | |
| 5743 | return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C)); |
| 5744 | } |
| 5745 | |
| 5746 | return C; |
| 5747 | } |
| 5748 | |
| 5749 | int clang_Cursor_getObjCSelectorIndex(CXCursor cursor) { |
| 5750 | return cxcursor::getSelectorIdentifierIndexAndLoc(cursor).first; |
| 5751 | } |
| 5752 | |
| 5753 | unsigned clang_getNumOverloadedDecls(CXCursor C) { |
| 5754 | if (C.kind != CXCursor_OverloadedDeclRef) |
| 5755 | return 0; |
| 5756 | |
| 5757 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5758 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5759 | return E->getNumDecls(); |
| 5760 | |
| 5761 | if (OverloadedTemplateStorage *S |
| 5762 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 5763 | return S->size(); |
| 5764 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5765 | const Decl *D = Storage.get<const Decl *>(); |
| 5766 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5767 | return Using->shadow_size(); |
| 5768 | |
| 5769 | return 0; |
| 5770 | } |
| 5771 | |
| 5772 | CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) { |
| 5773 | if (cursor.kind != CXCursor_OverloadedDeclRef) |
| 5774 | return clang_getNullCursor(); |
| 5775 | |
| 5776 | if (index >= clang_getNumOverloadedDecls(cursor)) |
| 5777 | return clang_getNullCursor(); |
| 5778 | |
| 5779 | CXTranslationUnit TU = getCursorTU(cursor); |
| 5780 | OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5781 | if (const OverloadExpr *E = Storage.dyn_cast<const OverloadExpr *>()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5782 | return MakeCXCursor(E->decls_begin()[index], TU); |
| 5783 | |
| 5784 | if (OverloadedTemplateStorage *S |
| 5785 | = Storage.dyn_cast<OverloadedTemplateStorage*>()) |
| 5786 | return MakeCXCursor(S->begin()[index], TU); |
| 5787 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5788 | const Decl *D = Storage.get<const Decl *>(); |
| 5789 | if (const UsingDecl *Using = dyn_cast<UsingDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5790 | // FIXME: This is, unfortunately, linear time. |
| 5791 | UsingDecl::shadow_iterator Pos = Using->shadow_begin(); |
| 5792 | std::advance(Pos, index); |
| 5793 | return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU); |
| 5794 | } |
| 5795 | |
| 5796 | return clang_getNullCursor(); |
| 5797 | } |
| 5798 | |
| 5799 | void clang_getDefinitionSpellingAndExtent(CXCursor C, |
| 5800 | const char **startBuf, |
| 5801 | const char **endBuf, |
| 5802 | unsigned *startLine, |
| 5803 | unsigned *startColumn, |
| 5804 | unsigned *endLine, |
| 5805 | unsigned *endColumn) { |
| 5806 | assert(getCursorDecl(C) && "CXCursor has null decl"); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 5807 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(getCursorDecl(C)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5808 | CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody()); |
| 5809 | |
| 5810 | SourceManager &SM = FD->getASTContext().getSourceManager(); |
| 5811 | *startBuf = SM.getCharacterData(Body->getLBracLoc()); |
| 5812 | *endBuf = SM.getCharacterData(Body->getRBracLoc()); |
| 5813 | *startLine = SM.getSpellingLineNumber(Body->getLBracLoc()); |
| 5814 | *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc()); |
| 5815 | *endLine = SM.getSpellingLineNumber(Body->getRBracLoc()); |
| 5816 | *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc()); |
| 5817 | } |
| 5818 | |
| 5819 | |
| 5820 | CXSourceRange clang_getCursorReferenceNameRange(CXCursor C, unsigned NameFlags, |
| 5821 | unsigned PieceIndex) { |
| 5822 | RefNamePieces Pieces; |
| 5823 | |
| 5824 | switch (C.kind) { |
| 5825 | case CXCursor_MemberRefExpr: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 5826 | if (const MemberExpr *E = dyn_cast<MemberExpr>(getCursorExpr(C))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5827 | Pieces = buildPieces(NameFlags, true, E->getMemberNameInfo(), |
| 5828 | E->getQualifierLoc().getSourceRange()); |
| 5829 | break; |
| 5830 | |
| 5831 | case CXCursor_DeclRefExpr: |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 5832 | if (const DeclRefExpr *E = dyn_cast<DeclRefExpr>(getCursorExpr(C))) { |
| 5833 | SourceRange TemplateArgLoc(E->getLAngleLoc(), E->getRAngleLoc()); |
| 5834 | Pieces = |
| 5835 | buildPieces(NameFlags, false, E->getNameInfo(), |
| 5836 | E->getQualifierLoc().getSourceRange(), &TemplateArgLoc); |
| 5837 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5838 | break; |
| 5839 | |
| 5840 | case CXCursor_CallExpr: |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 5841 | if (const CXXOperatorCallExpr *OCE = |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5842 | dyn_cast<CXXOperatorCallExpr>(getCursorExpr(C))) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 5843 | const Expr *Callee = OCE->getCallee(); |
| 5844 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Callee)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5845 | Callee = ICE->getSubExpr(); |
| 5846 | |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 5847 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5848 | Pieces = buildPieces(NameFlags, false, DRE->getNameInfo(), |
| 5849 | DRE->getQualifierLoc().getSourceRange()); |
| 5850 | } |
| 5851 | break; |
| 5852 | |
| 5853 | default: |
| 5854 | break; |
| 5855 | } |
| 5856 | |
| 5857 | if (Pieces.empty()) { |
| 5858 | if (PieceIndex == 0) |
| 5859 | return clang_getCursorExtent(C); |
| 5860 | } else if (PieceIndex < Pieces.size()) { |
| 5861 | SourceRange R = Pieces[PieceIndex]; |
| 5862 | if (R.isValid()) |
| 5863 | return cxloc::translateSourceRange(getCursorContext(C), R); |
| 5864 | } |
| 5865 | |
| 5866 | return clang_getNullRange(); |
| 5867 | } |
| 5868 | |
| 5869 | void clang_enableStackTraces(void) { |
| 5870 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 5871 | } |
| 5872 | |
| 5873 | void clang_executeOnThread(void (*fn)(void*), void *user_data, |
| 5874 | unsigned stack_size) { |
| 5875 | llvm::llvm_execute_on_thread(fn, user_data, stack_size); |
| 5876 | } |
| 5877 | |
| 5878 | } // end: extern "C" |
| 5879 | |
| 5880 | //===----------------------------------------------------------------------===// |
| 5881 | // Token-based Operations. |
| 5882 | //===----------------------------------------------------------------------===// |
| 5883 | |
| 5884 | /* CXToken layout: |
| 5885 | * int_data[0]: a CXTokenKind |
| 5886 | * int_data[1]: starting token location |
| 5887 | * int_data[2]: token length |
| 5888 | * int_data[3]: reserved |
| 5889 | * ptr_data: for identifiers and keywords, an IdentifierInfo*. |
| 5890 | * otherwise unused. |
| 5891 | */ |
| 5892 | extern "C" { |
| 5893 | |
| 5894 | CXTokenKind clang_getTokenKind(CXToken CXTok) { |
| 5895 | return static_cast<CXTokenKind>(CXTok.int_data[0]); |
| 5896 | } |
| 5897 | |
| 5898 | CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) { |
| 5899 | switch (clang_getTokenKind(CXTok)) { |
| 5900 | case CXToken_Identifier: |
| 5901 | case CXToken_Keyword: |
| 5902 | // We know we have an IdentifierInfo*, so use that. |
Dmitri Gribenko | 3c66b0b | 2013-02-02 00:02:12 +0000 | [diff] [blame] | 5903 | return cxstring::createRef(static_cast<IdentifierInfo *>(CXTok.ptr_data) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5904 | ->getNameStart()); |
| 5905 | |
| 5906 | case CXToken_Literal: { |
| 5907 | // We have stashed the starting pointer in the ptr_data field. Use it. |
| 5908 | const char *Text = static_cast<const char *>(CXTok.ptr_data); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 5909 | return cxstring::createDup(StringRef(Text, CXTok.int_data[2])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5910 | } |
| 5911 | |
| 5912 | case CXToken_Punctuation: |
| 5913 | case CXToken_Comment: |
| 5914 | break; |
| 5915 | } |
| 5916 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 5917 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 5918 | LOG_BAD_TU(TU); |
| 5919 | return cxstring::createEmpty(); |
| 5920 | } |
| 5921 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5922 | // We have to find the starting buffer pointer the hard way, by |
| 5923 | // deconstructing the source location. |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 5924 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5925 | if (!CXXUnit) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 5926 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5927 | |
| 5928 | SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]); |
| 5929 | std::pair<FileID, unsigned> LocInfo |
| 5930 | = CXXUnit->getSourceManager().getDecomposedSpellingLoc(Loc); |
| 5931 | bool Invalid = false; |
| 5932 | StringRef Buffer |
| 5933 | = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid); |
| 5934 | if (Invalid) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 5935 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5936 | |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 5937 | return cxstring::createDup(Buffer.substr(LocInfo.second, CXTok.int_data[2])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5938 | } |
| 5939 | |
| 5940 | CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 5941 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 5942 | LOG_BAD_TU(TU); |
| 5943 | return clang_getNullLocation(); |
| 5944 | } |
| 5945 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 5946 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5947 | if (!CXXUnit) |
| 5948 | return clang_getNullLocation(); |
| 5949 | |
| 5950 | return cxloc::translateSourceLocation(CXXUnit->getASTContext(), |
| 5951 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 5952 | } |
| 5953 | |
| 5954 | CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 5955 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 5956 | LOG_BAD_TU(TU); |
| 5957 | return clang_getNullRange(); |
| 5958 | } |
| 5959 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 5960 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5961 | if (!CXXUnit) |
| 5962 | return clang_getNullRange(); |
| 5963 | |
| 5964 | return cxloc::translateSourceRange(CXXUnit->getASTContext(), |
| 5965 | SourceLocation::getFromRawEncoding(CXTok.int_data[1])); |
| 5966 | } |
| 5967 | |
| 5968 | static void getTokens(ASTUnit *CXXUnit, SourceRange Range, |
| 5969 | SmallVectorImpl<CXToken> &CXTokens) { |
| 5970 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 5971 | std::pair<FileID, unsigned> BeginLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 5972 | = SourceMgr.getDecomposedSpellingLoc(Range.getBegin()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5973 | std::pair<FileID, unsigned> EndLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 5974 | = SourceMgr.getDecomposedSpellingLoc(Range.getEnd()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 5975 | |
| 5976 | // Cannot tokenize across files. |
| 5977 | if (BeginLocInfo.first != EndLocInfo.first) |
| 5978 | return; |
| 5979 | |
| 5980 | // Create a lexer |
| 5981 | bool Invalid = false; |
| 5982 | StringRef Buffer |
| 5983 | = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 5984 | if (Invalid) |
| 5985 | return; |
| 5986 | |
| 5987 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 5988 | CXXUnit->getASTContext().getLangOpts(), |
| 5989 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, Buffer.end()); |
| 5990 | Lex.SetCommentRetentionState(true); |
| 5991 | |
| 5992 | // Lex tokens until we hit the end of the range. |
| 5993 | const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second; |
| 5994 | Token Tok; |
| 5995 | bool previousWasAt = false; |
| 5996 | do { |
| 5997 | // Lex the next token |
| 5998 | Lex.LexFromRawLexer(Tok); |
| 5999 | if (Tok.is(tok::eof)) |
| 6000 | break; |
| 6001 | |
| 6002 | // Initialize the CXToken. |
| 6003 | CXToken CXTok; |
| 6004 | |
| 6005 | // - Common fields |
| 6006 | CXTok.int_data[1] = Tok.getLocation().getRawEncoding(); |
| 6007 | CXTok.int_data[2] = Tok.getLength(); |
| 6008 | CXTok.int_data[3] = 0; |
| 6009 | |
| 6010 | // - Kind-specific fields |
| 6011 | if (Tok.isLiteral()) { |
| 6012 | CXTok.int_data[0] = CXToken_Literal; |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 6013 | CXTok.ptr_data = const_cast<char *>(Tok.getLiteralData()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6014 | } else if (Tok.is(tok::raw_identifier)) { |
| 6015 | // Lookup the identifier to determine whether we have a keyword. |
| 6016 | IdentifierInfo *II |
| 6017 | = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok); |
| 6018 | |
| 6019 | if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) { |
| 6020 | CXTok.int_data[0] = CXToken_Keyword; |
| 6021 | } |
| 6022 | else { |
| 6023 | CXTok.int_data[0] = Tok.is(tok::identifier) |
| 6024 | ? CXToken_Identifier |
| 6025 | : CXToken_Keyword; |
| 6026 | } |
| 6027 | CXTok.ptr_data = II; |
| 6028 | } else if (Tok.is(tok::comment)) { |
| 6029 | CXTok.int_data[0] = CXToken_Comment; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6030 | CXTok.ptr_data = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6031 | } else { |
| 6032 | CXTok.int_data[0] = CXToken_Punctuation; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6033 | CXTok.ptr_data = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6034 | } |
| 6035 | CXTokens.push_back(CXTok); |
| 6036 | previousWasAt = Tok.is(tok::at); |
| 6037 | } while (Lex.getBufferLocation() <= EffectiveBufferEnd); |
| 6038 | } |
| 6039 | |
| 6040 | void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range, |
| 6041 | CXToken **Tokens, unsigned *NumTokens) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 6042 | LOG_FUNC_SECTION { |
| 6043 | *Log << TU << ' ' << Range; |
| 6044 | } |
| 6045 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6046 | if (Tokens) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6047 | *Tokens = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6048 | if (NumTokens) |
| 6049 | *NumTokens = 0; |
| 6050 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6051 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6052 | LOG_BAD_TU(TU); |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 6053 | return; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6054 | } |
Argyrios Kyrtzidis | 0e95fca | 2013-04-04 22:40:59 +0000 | [diff] [blame] | 6055 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6056 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6057 | if (!CXXUnit || !Tokens || !NumTokens) |
| 6058 | return; |
| 6059 | |
| 6060 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
| 6061 | |
| 6062 | SourceRange R = cxloc::translateCXSourceRange(Range); |
| 6063 | if (R.isInvalid()) |
| 6064 | return; |
| 6065 | |
| 6066 | SmallVector<CXToken, 32> CXTokens; |
| 6067 | getTokens(CXXUnit, R, CXTokens); |
| 6068 | |
| 6069 | if (CXTokens.empty()) |
| 6070 | return; |
| 6071 | |
| 6072 | *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size()); |
| 6073 | memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size()); |
| 6074 | *NumTokens = CXTokens.size(); |
| 6075 | } |
| 6076 | |
| 6077 | void clang_disposeTokens(CXTranslationUnit TU, |
| 6078 | CXToken *Tokens, unsigned NumTokens) { |
| 6079 | free(Tokens); |
| 6080 | } |
| 6081 | |
| 6082 | } // end: extern "C" |
| 6083 | |
| 6084 | //===----------------------------------------------------------------------===// |
| 6085 | // Token annotation APIs. |
| 6086 | //===----------------------------------------------------------------------===// |
| 6087 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6088 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 6089 | CXCursor parent, |
| 6090 | CXClientData client_data); |
| 6091 | static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor, |
| 6092 | CXClientData client_data); |
| 6093 | |
| 6094 | namespace { |
| 6095 | class AnnotateTokensWorker { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6096 | CXToken *Tokens; |
| 6097 | CXCursor *Cursors; |
| 6098 | unsigned NumTokens; |
| 6099 | unsigned TokIdx; |
| 6100 | unsigned PreprocessingTokIdx; |
| 6101 | CursorVisitor AnnotateVis; |
| 6102 | SourceManager &SrcMgr; |
| 6103 | bool HasContextSensitiveKeywords; |
| 6104 | |
| 6105 | struct PostChildrenInfo { |
| 6106 | CXCursor Cursor; |
| 6107 | SourceRange CursorRange; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6108 | unsigned BeforeReachingCursorIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6109 | unsigned BeforeChildrenTokenIdx; |
| 6110 | }; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6111 | SmallVector<PostChildrenInfo, 8> PostChildrenInfos; |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6112 | |
| 6113 | CXToken &getTok(unsigned Idx) { |
| 6114 | assert(Idx < NumTokens); |
| 6115 | return Tokens[Idx]; |
| 6116 | } |
| 6117 | const CXToken &getTok(unsigned Idx) const { |
| 6118 | assert(Idx < NumTokens); |
| 6119 | return Tokens[Idx]; |
| 6120 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6121 | bool MoreTokens() const { return TokIdx < NumTokens; } |
| 6122 | unsigned NextToken() const { return TokIdx; } |
| 6123 | void AdvanceToken() { ++TokIdx; } |
| 6124 | SourceLocation GetTokenLoc(unsigned tokI) { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6125 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6126 | } |
| 6127 | bool isFunctionMacroToken(unsigned tokI) const { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6128 | return getTok(tokI).int_data[3] != 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6129 | } |
| 6130 | SourceLocation getFunctionMacroTokenLoc(unsigned tokI) const { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6131 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[3]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6132 | } |
| 6133 | |
| 6134 | void annotateAndAdvanceTokens(CXCursor, RangeComparisonResult, SourceRange); |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6135 | bool annotateAndAdvanceFunctionMacroTokens(CXCursor, RangeComparisonResult, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6136 | SourceRange); |
| 6137 | |
| 6138 | public: |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6139 | AnnotateTokensWorker(CXToken *tokens, CXCursor *cursors, unsigned numTokens, |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6140 | CXTranslationUnit TU, SourceRange RegionOfInterest) |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6141 | : Tokens(tokens), Cursors(cursors), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6142 | NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0), |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6143 | AnnotateVis(TU, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6144 | AnnotateTokensVisitor, this, |
| 6145 | /*VisitPreprocessorLast=*/true, |
| 6146 | /*VisitIncludedEntities=*/false, |
| 6147 | RegionOfInterest, |
| 6148 | /*VisitDeclsOnly=*/false, |
| 6149 | AnnotateTokensPostChildrenVisitor), |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6150 | SrcMgr(cxtu::getASTUnit(TU)->getSourceManager()), |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6151 | HasContextSensitiveKeywords(false) { } |
| 6152 | |
| 6153 | void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); } |
| 6154 | enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent); |
| 6155 | bool postVisitChildren(CXCursor cursor); |
| 6156 | void AnnotateTokens(); |
| 6157 | |
| 6158 | /// \brief Determine whether the annotator saw any cursors that have |
| 6159 | /// context-sensitive keywords. |
| 6160 | bool hasContextSensitiveKeywords() const { |
| 6161 | return HasContextSensitiveKeywords; |
| 6162 | } |
| 6163 | |
| 6164 | ~AnnotateTokensWorker() { |
| 6165 | assert(PostChildrenInfos.empty()); |
| 6166 | } |
| 6167 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6168 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6169 | |
| 6170 | void AnnotateTokensWorker::AnnotateTokens() { |
| 6171 | // Walk the AST within the region of interest, annotating tokens |
| 6172 | // along the way. |
| 6173 | AnnotateVis.visitFileRegion(); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6174 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6175 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6176 | static inline void updateCursorAnnotation(CXCursor &Cursor, |
| 6177 | const CXCursor &updateC) { |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6178 | if (clang_isInvalid(updateC.kind) || !clang_isInvalid(Cursor.kind)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6179 | return; |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6180 | Cursor = updateC; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6181 | } |
| 6182 | |
| 6183 | /// \brief It annotates and advances tokens with a cursor until the comparison |
| 6184 | //// between the cursor location and the source range is the same as |
| 6185 | /// \arg compResult. |
| 6186 | /// |
| 6187 | /// Pass RangeBefore to annotate tokens with a cursor until a range is reached. |
| 6188 | /// Pass RangeOverlap to annotate tokens inside a range. |
| 6189 | void AnnotateTokensWorker::annotateAndAdvanceTokens(CXCursor updateC, |
| 6190 | RangeComparisonResult compResult, |
| 6191 | SourceRange range) { |
| 6192 | while (MoreTokens()) { |
| 6193 | const unsigned I = NextToken(); |
| 6194 | if (isFunctionMacroToken(I)) |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6195 | if (!annotateAndAdvanceFunctionMacroTokens(updateC, compResult, range)) |
| 6196 | return; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6197 | |
| 6198 | SourceLocation TokLoc = GetTokenLoc(I); |
| 6199 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6200 | updateCursorAnnotation(Cursors[I], updateC); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6201 | AdvanceToken(); |
| 6202 | continue; |
| 6203 | } |
| 6204 | break; |
| 6205 | } |
| 6206 | } |
| 6207 | |
| 6208 | /// \brief Special annotation handling for macro argument tokens. |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6209 | /// \returns true if it advanced beyond all macro tokens, false otherwise. |
| 6210 | bool AnnotateTokensWorker::annotateAndAdvanceFunctionMacroTokens( |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6211 | CXCursor updateC, |
| 6212 | RangeComparisonResult compResult, |
| 6213 | SourceRange range) { |
| 6214 | assert(MoreTokens()); |
| 6215 | assert(isFunctionMacroToken(NextToken()) && |
| 6216 | "Should be called only for macro arg tokens"); |
| 6217 | |
| 6218 | // This works differently than annotateAndAdvanceTokens; because expanded |
| 6219 | // macro arguments can have arbitrary translation-unit source order, we do not |
| 6220 | // advance the token index one by one until a token fails the range test. |
| 6221 | // We only advance once past all of the macro arg tokens if all of them |
| 6222 | // pass the range test. If one of them fails we keep the token index pointing |
| 6223 | // at the start of the macro arg tokens so that the failing token will be |
| 6224 | // annotated by a subsequent annotation try. |
| 6225 | |
| 6226 | bool atLeastOneCompFail = false; |
| 6227 | |
| 6228 | unsigned I = NextToken(); |
| 6229 | for (; I < NumTokens && isFunctionMacroToken(I); ++I) { |
| 6230 | SourceLocation TokLoc = getFunctionMacroTokenLoc(I); |
| 6231 | if (TokLoc.isFileID()) |
| 6232 | continue; // not macro arg token, it's parens or comma. |
| 6233 | if (LocationCompare(SrcMgr, TokLoc, range) == compResult) { |
| 6234 | if (clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 6235 | Cursors[I] = updateC; |
| 6236 | } else |
| 6237 | atLeastOneCompFail = true; |
| 6238 | } |
| 6239 | |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6240 | if (atLeastOneCompFail) |
| 6241 | return false; |
| 6242 | |
| 6243 | TokIdx = I; // All of the tokens were handled, advance beyond all of them. |
| 6244 | return true; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6245 | } |
| 6246 | |
| 6247 | enum CXChildVisitResult |
| 6248 | AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6249 | SourceRange cursorRange = getRawCursorExtent(cursor); |
| 6250 | if (cursorRange.isInvalid()) |
| 6251 | return CXChildVisit_Recurse; |
| 6252 | |
| 6253 | if (!HasContextSensitiveKeywords) { |
| 6254 | // Objective-C properties can have context-sensitive keywords. |
| 6255 | if (cursor.kind == CXCursor_ObjCPropertyDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6256 | if (const ObjCPropertyDecl *Property |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6257 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(cursor))) |
| 6258 | HasContextSensitiveKeywords = Property->getPropertyAttributesAsWritten() != 0; |
| 6259 | } |
| 6260 | // Objective-C methods can have context-sensitive keywords. |
| 6261 | else if (cursor.kind == CXCursor_ObjCInstanceMethodDecl || |
| 6262 | cursor.kind == CXCursor_ObjCClassMethodDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6263 | if (const ObjCMethodDecl *Method |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6264 | = dyn_cast_or_null<ObjCMethodDecl>(getCursorDecl(cursor))) { |
| 6265 | if (Method->getObjCDeclQualifier()) |
| 6266 | HasContextSensitiveKeywords = true; |
| 6267 | else { |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 6268 | for (const auto *P : Method->params()) { |
| 6269 | if (P->getObjCDeclQualifier()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6270 | HasContextSensitiveKeywords = true; |
| 6271 | break; |
| 6272 | } |
| 6273 | } |
| 6274 | } |
| 6275 | } |
| 6276 | } |
| 6277 | // C++ methods can have context-sensitive keywords. |
| 6278 | else if (cursor.kind == CXCursor_CXXMethod) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6279 | if (const CXXMethodDecl *Method |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6280 | = dyn_cast_or_null<CXXMethodDecl>(getCursorDecl(cursor))) { |
| 6281 | if (Method->hasAttr<FinalAttr>() || Method->hasAttr<OverrideAttr>()) |
| 6282 | HasContextSensitiveKeywords = true; |
| 6283 | } |
| 6284 | } |
| 6285 | // C++ classes can have context-sensitive keywords. |
| 6286 | else if (cursor.kind == CXCursor_StructDecl || |
| 6287 | cursor.kind == CXCursor_ClassDecl || |
| 6288 | cursor.kind == CXCursor_ClassTemplate || |
| 6289 | cursor.kind == CXCursor_ClassTemplatePartialSpecialization) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6290 | if (const Decl *D = getCursorDecl(cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6291 | if (D->hasAttr<FinalAttr>()) |
| 6292 | HasContextSensitiveKeywords = true; |
| 6293 | } |
| 6294 | } |
Argyrios Kyrtzidis | 990b386 | 2013-06-04 18:24:30 +0000 | [diff] [blame] | 6295 | |
| 6296 | // Don't override a property annotation with its getter/setter method. |
| 6297 | if (cursor.kind == CXCursor_ObjCInstanceMethodDecl && |
| 6298 | parent.kind == CXCursor_ObjCPropertyDecl) |
| 6299 | return CXChildVisit_Continue; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6300 | |
| 6301 | if (clang_isPreprocessing(cursor.kind)) { |
| 6302 | // Items in the preprocessing record are kept separate from items in |
| 6303 | // declarations, so we keep a separate token index. |
| 6304 | unsigned SavedTokIdx = TokIdx; |
| 6305 | TokIdx = PreprocessingTokIdx; |
| 6306 | |
| 6307 | // Skip tokens up until we catch up to the beginning of the preprocessing |
| 6308 | // entry. |
| 6309 | while (MoreTokens()) { |
| 6310 | const unsigned I = NextToken(); |
| 6311 | SourceLocation TokLoc = GetTokenLoc(I); |
| 6312 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 6313 | case RangeBefore: |
| 6314 | AdvanceToken(); |
| 6315 | continue; |
| 6316 | case RangeAfter: |
| 6317 | case RangeOverlap: |
| 6318 | break; |
| 6319 | } |
| 6320 | break; |
| 6321 | } |
| 6322 | |
| 6323 | // Look at all of the tokens within this range. |
| 6324 | while (MoreTokens()) { |
| 6325 | const unsigned I = NextToken(); |
| 6326 | SourceLocation TokLoc = GetTokenLoc(I); |
| 6327 | switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) { |
| 6328 | case RangeBefore: |
| 6329 | llvm_unreachable("Infeasible"); |
| 6330 | case RangeAfter: |
| 6331 | break; |
| 6332 | case RangeOverlap: |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6333 | // For macro expansions, just note where the beginning of the macro |
| 6334 | // expansion occurs. |
| 6335 | if (cursor.kind == CXCursor_MacroExpansion) { |
| 6336 | if (TokLoc == cursorRange.getBegin()) |
| 6337 | Cursors[I] = cursor; |
| 6338 | AdvanceToken(); |
| 6339 | break; |
| 6340 | } |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6341 | // We may have already annotated macro names inside macro definitions. |
| 6342 | if (Cursors[I].kind != CXCursor_MacroExpansion) |
| 6343 | Cursors[I] = cursor; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6344 | AdvanceToken(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6345 | continue; |
| 6346 | } |
| 6347 | break; |
| 6348 | } |
| 6349 | |
| 6350 | // Save the preprocessing token index; restore the non-preprocessing |
| 6351 | // token index. |
| 6352 | PreprocessingTokIdx = TokIdx; |
| 6353 | TokIdx = SavedTokIdx; |
| 6354 | return CXChildVisit_Recurse; |
| 6355 | } |
| 6356 | |
| 6357 | if (cursorRange.isInvalid()) |
| 6358 | return CXChildVisit_Continue; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6359 | |
| 6360 | unsigned BeforeReachingCursorIdx = NextToken(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6361 | const enum CXCursorKind cursorK = clang_getCursorKind(cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6362 | const enum CXCursorKind K = clang_getCursorKind(parent); |
| 6363 | const CXCursor updateC = |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6364 | (clang_isInvalid(K) || K == CXCursor_TranslationUnit || |
| 6365 | // Attributes are annotated out-of-order, skip tokens until we reach it. |
| 6366 | clang_isAttribute(cursor.kind)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6367 | ? clang_getNullCursor() : parent; |
| 6368 | |
| 6369 | annotateAndAdvanceTokens(updateC, RangeBefore, cursorRange); |
| 6370 | |
| 6371 | // Avoid having the cursor of an expression "overwrite" the annotation of the |
| 6372 | // variable declaration that it belongs to. |
| 6373 | // This can happen for C++ constructor expressions whose range generally |
| 6374 | // include the variable declaration, e.g.: |
| 6375 | // 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] | 6376 | if (clang_isExpression(cursorK) && MoreTokens()) { |
Dmitri Gribenko | e835406 | 2013-01-26 15:29:08 +0000 | [diff] [blame] | 6377 | const Expr *E = getCursorExpr(cursor); |
Dmitri Gribenko | a169118 | 2013-01-26 18:12:08 +0000 | [diff] [blame] | 6378 | if (const Decl *D = getCursorParentDecl(cursor)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6379 | const unsigned I = NextToken(); |
| 6380 | if (E->getLocStart().isValid() && D->getLocation().isValid() && |
| 6381 | E->getLocStart() == D->getLocation() && |
| 6382 | E->getLocStart() == GetTokenLoc(I)) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6383 | updateCursorAnnotation(Cursors[I], updateC); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6384 | AdvanceToken(); |
| 6385 | } |
| 6386 | } |
| 6387 | } |
| 6388 | |
| 6389 | // Before recursing into the children keep some state that we are going |
| 6390 | // to use in the AnnotateTokensWorker::postVisitChildren callback to do some |
| 6391 | // extra work after the child nodes are visited. |
| 6392 | // Note that we don't call VisitChildren here to avoid traversing statements |
| 6393 | // code-recursively which can blow the stack. |
| 6394 | |
| 6395 | PostChildrenInfo Info; |
| 6396 | Info.Cursor = cursor; |
| 6397 | Info.CursorRange = cursorRange; |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6398 | Info.BeforeReachingCursorIdx = BeforeReachingCursorIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6399 | Info.BeforeChildrenTokenIdx = NextToken(); |
| 6400 | PostChildrenInfos.push_back(Info); |
| 6401 | |
| 6402 | return CXChildVisit_Recurse; |
| 6403 | } |
| 6404 | |
| 6405 | bool AnnotateTokensWorker::postVisitChildren(CXCursor cursor) { |
| 6406 | if (PostChildrenInfos.empty()) |
| 6407 | return false; |
| 6408 | const PostChildrenInfo &Info = PostChildrenInfos.back(); |
| 6409 | if (!clang_equalCursors(Info.Cursor, cursor)) |
| 6410 | return false; |
| 6411 | |
| 6412 | const unsigned BeforeChildren = Info.BeforeChildrenTokenIdx; |
| 6413 | const unsigned AfterChildren = NextToken(); |
| 6414 | SourceRange cursorRange = Info.CursorRange; |
| 6415 | |
| 6416 | // Scan the tokens that are at the end of the cursor, but are not captured |
| 6417 | // but the child cursors. |
| 6418 | annotateAndAdvanceTokens(cursor, RangeOverlap, cursorRange); |
| 6419 | |
| 6420 | // Scan the tokens that are at the beginning of the cursor, but are not |
| 6421 | // capture by the child cursors. |
| 6422 | for (unsigned I = BeforeChildren; I != AfterChildren; ++I) { |
| 6423 | if (!clang_isInvalid(clang_getCursorKind(Cursors[I]))) |
| 6424 | break; |
| 6425 | |
| 6426 | Cursors[I] = cursor; |
| 6427 | } |
| 6428 | |
Argyrios Kyrtzidis | a2ed813 | 2013-02-08 01:12:25 +0000 | [diff] [blame] | 6429 | // Attributes are annotated out-of-order, rewind TokIdx to when we first |
| 6430 | // encountered the attribute cursor. |
| 6431 | if (clang_isAttribute(cursor.kind)) |
| 6432 | TokIdx = Info.BeforeReachingCursorIdx; |
| 6433 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6434 | PostChildrenInfos.pop_back(); |
| 6435 | return false; |
| 6436 | } |
| 6437 | |
| 6438 | static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor, |
| 6439 | CXCursor parent, |
| 6440 | CXClientData client_data) { |
| 6441 | return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent); |
| 6442 | } |
| 6443 | |
| 6444 | static bool AnnotateTokensPostChildrenVisitor(CXCursor cursor, |
| 6445 | CXClientData client_data) { |
| 6446 | return static_cast<AnnotateTokensWorker*>(client_data)-> |
| 6447 | postVisitChildren(cursor); |
| 6448 | } |
| 6449 | |
| 6450 | namespace { |
| 6451 | |
| 6452 | /// \brief Uses the macro expansions in the preprocessing record to find |
| 6453 | /// and mark tokens that are macro arguments. This info is used by the |
| 6454 | /// AnnotateTokensWorker. |
| 6455 | class MarkMacroArgTokensVisitor { |
| 6456 | SourceManager &SM; |
| 6457 | CXToken *Tokens; |
| 6458 | unsigned NumTokens; |
| 6459 | unsigned CurIdx; |
| 6460 | |
| 6461 | public: |
| 6462 | MarkMacroArgTokensVisitor(SourceManager &SM, |
| 6463 | CXToken *tokens, unsigned numTokens) |
| 6464 | : SM(SM), Tokens(tokens), NumTokens(numTokens), CurIdx(0) { } |
| 6465 | |
| 6466 | CXChildVisitResult visit(CXCursor cursor, CXCursor parent) { |
| 6467 | if (cursor.kind != CXCursor_MacroExpansion) |
| 6468 | return CXChildVisit_Continue; |
| 6469 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 6470 | SourceRange macroRange = getCursorMacroExpansion(cursor).getSourceRange(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6471 | if (macroRange.getBegin() == macroRange.getEnd()) |
| 6472 | return CXChildVisit_Continue; // it's not a function macro. |
| 6473 | |
| 6474 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 6475 | if (!SM.isBeforeInTranslationUnit(getTokenLoc(CurIdx), |
| 6476 | macroRange.getBegin())) |
| 6477 | break; |
| 6478 | } |
| 6479 | |
| 6480 | if (CurIdx == NumTokens) |
| 6481 | return CXChildVisit_Break; |
| 6482 | |
| 6483 | for (; CurIdx < NumTokens; ++CurIdx) { |
| 6484 | SourceLocation tokLoc = getTokenLoc(CurIdx); |
| 6485 | if (!SM.isBeforeInTranslationUnit(tokLoc, macroRange.getEnd())) |
| 6486 | break; |
| 6487 | |
| 6488 | setFunctionMacroTokenLoc(CurIdx, SM.getMacroArgExpandedLocation(tokLoc)); |
| 6489 | } |
| 6490 | |
| 6491 | if (CurIdx == NumTokens) |
| 6492 | return CXChildVisit_Break; |
| 6493 | |
| 6494 | return CXChildVisit_Continue; |
| 6495 | } |
| 6496 | |
| 6497 | private: |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6498 | CXToken &getTok(unsigned Idx) { |
| 6499 | assert(Idx < NumTokens); |
| 6500 | return Tokens[Idx]; |
| 6501 | } |
| 6502 | const CXToken &getTok(unsigned Idx) const { |
| 6503 | assert(Idx < NumTokens); |
| 6504 | return Tokens[Idx]; |
| 6505 | } |
| 6506 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6507 | SourceLocation getTokenLoc(unsigned tokI) { |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6508 | return SourceLocation::getFromRawEncoding(getTok(tokI).int_data[1]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6509 | } |
| 6510 | |
| 6511 | void setFunctionMacroTokenLoc(unsigned tokI, SourceLocation loc) { |
| 6512 | // The third field is reserved and currently not used. Use it here |
| 6513 | // to mark macro arg expanded tokens with their expanded locations. |
Argyrios Kyrtzidis | 50126f1 | 2013-11-27 05:50:55 +0000 | [diff] [blame] | 6514 | getTok(tokI).int_data[3] = loc.getRawEncoding(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6515 | } |
| 6516 | }; |
| 6517 | |
| 6518 | } // end anonymous namespace |
| 6519 | |
| 6520 | static CXChildVisitResult |
| 6521 | MarkMacroArgTokensVisitorDelegate(CXCursor cursor, CXCursor parent, |
| 6522 | CXClientData client_data) { |
| 6523 | return static_cast<MarkMacroArgTokensVisitor*>(client_data)->visit(cursor, |
| 6524 | parent); |
| 6525 | } |
| 6526 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6527 | /// \brief Used by \c annotatePreprocessorTokens. |
| 6528 | /// \returns true if lexing was finished, false otherwise. |
| 6529 | static bool lexNext(Lexer &Lex, Token &Tok, |
| 6530 | unsigned &NextIdx, unsigned NumTokens) { |
| 6531 | if (NextIdx >= NumTokens) |
| 6532 | return true; |
| 6533 | |
| 6534 | ++NextIdx; |
| 6535 | Lex.LexFromRawLexer(Tok); |
Alexander Kornienko | 1a9f184 | 2015-12-28 15:24:08 +0000 | [diff] [blame] | 6536 | return Tok.is(tok::eof); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6537 | } |
| 6538 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6539 | static void annotatePreprocessorTokens(CXTranslationUnit TU, |
| 6540 | SourceRange RegionOfInterest, |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6541 | CXCursor *Cursors, |
| 6542 | CXToken *Tokens, |
| 6543 | unsigned NumTokens) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6544 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6545 | |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6546 | Preprocessor &PP = CXXUnit->getPreprocessor(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6547 | SourceManager &SourceMgr = CXXUnit->getSourceManager(); |
| 6548 | std::pair<FileID, unsigned> BeginLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6549 | = SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getBegin()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6550 | std::pair<FileID, unsigned> EndLocInfo |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6551 | = SourceMgr.getDecomposedSpellingLoc(RegionOfInterest.getEnd()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6552 | |
| 6553 | if (BeginLocInfo.first != EndLocInfo.first) |
| 6554 | return; |
| 6555 | |
| 6556 | StringRef Buffer; |
| 6557 | bool Invalid = false; |
| 6558 | Buffer = SourceMgr.getBufferData(BeginLocInfo.first, &Invalid); |
| 6559 | if (Buffer.empty() || Invalid) |
| 6560 | return; |
| 6561 | |
| 6562 | Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first), |
| 6563 | CXXUnit->getASTContext().getLangOpts(), |
| 6564 | Buffer.begin(), Buffer.data() + BeginLocInfo.second, |
| 6565 | Buffer.end()); |
| 6566 | Lex.SetCommentRetentionState(true); |
| 6567 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6568 | unsigned NextIdx = 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6569 | // Lex tokens in raw mode until we hit the end of the range, to avoid |
| 6570 | // entering #includes or expanding macros. |
| 6571 | while (true) { |
| 6572 | Token Tok; |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6573 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 6574 | break; |
| 6575 | unsigned TokIdx = NextIdx-1; |
| 6576 | assert(Tok.getLocation() == |
| 6577 | SourceLocation::getFromRawEncoding(Tokens[TokIdx].int_data[1])); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6578 | |
| 6579 | reprocess: |
| 6580 | if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6581 | // We have found a preprocessing directive. Annotate the tokens |
| 6582 | // appropriately. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6583 | // |
| 6584 | // FIXME: Some simple tests here could identify macro definitions and |
| 6585 | // #undefs, to provide specific cursor kinds for those. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6586 | |
| 6587 | SourceLocation BeginLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6588 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 6589 | break; |
| 6590 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 6591 | MacroInfo *MI = nullptr; |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 6592 | if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "define") { |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6593 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) |
| 6594 | break; |
| 6595 | |
| 6596 | if (Tok.is(tok::raw_identifier)) { |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 6597 | IdentifierInfo &II = |
| 6598 | PP.getIdentifierTable().get(Tok.getRawIdentifier()); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6599 | SourceLocation MappedTokLoc = |
| 6600 | CXXUnit->mapLocationToPreamble(Tok.getLocation()); |
| 6601 | MI = getMacroInfo(II, MappedTokLoc, TU); |
| 6602 | } |
| 6603 | } |
| 6604 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6605 | bool finished = false; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6606 | do { |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6607 | if (lexNext(Lex, Tok, NextIdx, NumTokens)) { |
| 6608 | finished = true; |
| 6609 | break; |
| 6610 | } |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6611 | // If we are in a macro definition, check if the token was ever a |
| 6612 | // macro name and annotate it if that's the case. |
| 6613 | if (MI) { |
| 6614 | SourceLocation SaveLoc = Tok.getLocation(); |
| 6615 | Tok.setLocation(CXXUnit->mapLocationToPreamble(SaveLoc)); |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 6616 | MacroDefinitionRecord *MacroDef = |
| 6617 | checkForMacroInMacroDefinition(MI, Tok, TU); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6618 | Tok.setLocation(SaveLoc); |
| 6619 | if (MacroDef) |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 6620 | Cursors[NextIdx - 1] = |
| 6621 | MakeMacroExpansionCursor(MacroDef, Tok.getLocation(), TU); |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6622 | } |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6623 | } while (!Tok.isAtStartOfLine()); |
| 6624 | |
| 6625 | unsigned LastIdx = finished ? NextIdx-1 : NextIdx-2; |
| 6626 | assert(TokIdx <= LastIdx); |
| 6627 | SourceLocation EndLoc = |
| 6628 | SourceLocation::getFromRawEncoding(Tokens[LastIdx].int_data[1]); |
| 6629 | CXCursor Cursor = |
| 6630 | MakePreprocessingDirectiveCursor(SourceRange(BeginLoc, EndLoc), TU); |
| 6631 | |
| 6632 | for (; TokIdx <= LastIdx; ++TokIdx) |
Argyrios Kyrtzidis | 68d31ce | 2013-01-07 19:16:32 +0000 | [diff] [blame] | 6633 | updateCursorAnnotation(Cursors[TokIdx], Cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6634 | |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6635 | if (finished) |
| 6636 | break; |
| 6637 | goto reprocess; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6638 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6639 | } |
| 6640 | } |
| 6641 | |
| 6642 | // This gets run a separate thread to avoid stack blowout. |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 6643 | static void clang_annotateTokensImpl(CXTranslationUnit TU, ASTUnit *CXXUnit, |
| 6644 | CXToken *Tokens, unsigned NumTokens, |
| 6645 | CXCursor *Cursors) { |
Dmitri Gribenko | 183436e | 2013-01-26 21:49:50 +0000 | [diff] [blame] | 6646 | CIndexer *CXXIdx = TU->CIdx; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6647 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) |
| 6648 | setThreadBackgroundPriority(); |
| 6649 | |
| 6650 | // Determine the region of interest, which contains all of the tokens. |
| 6651 | SourceRange RegionOfInterest; |
| 6652 | RegionOfInterest.setBegin( |
| 6653 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0]))); |
| 6654 | RegionOfInterest.setEnd( |
| 6655 | cxloc::translateSourceLocation(clang_getTokenLocation(TU, |
| 6656 | Tokens[NumTokens-1]))); |
| 6657 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6658 | // Relex the tokens within the source range to look for preprocessing |
| 6659 | // directives. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6660 | annotatePreprocessorTokens(TU, RegionOfInterest, Cursors, Tokens, NumTokens); |
Argyrios Kyrtzidis | 5d47a9b | 2013-02-13 18:33:28 +0000 | [diff] [blame] | 6661 | |
| 6662 | // If begin location points inside a macro argument, set it to the expansion |
| 6663 | // location so we can have the full context when annotating semantically. |
| 6664 | { |
| 6665 | SourceManager &SM = CXXUnit->getSourceManager(); |
| 6666 | SourceLocation Loc = |
| 6667 | SM.getMacroArgExpandedLocation(RegionOfInterest.getBegin()); |
| 6668 | if (Loc.isMacroID()) |
| 6669 | RegionOfInterest.setBegin(SM.getExpansionLoc(Loc)); |
| 6670 | } |
| 6671 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6672 | if (CXXUnit->getPreprocessor().getPreprocessingRecord()) { |
| 6673 | // Search and mark tokens that are macro argument expansions. |
| 6674 | MarkMacroArgTokensVisitor Visitor(CXXUnit->getSourceManager(), |
| 6675 | Tokens, NumTokens); |
| 6676 | CursorVisitor MacroArgMarker(TU, |
| 6677 | MarkMacroArgTokensVisitorDelegate, &Visitor, |
| 6678 | /*VisitPreprocessorLast=*/true, |
| 6679 | /*VisitIncludedEntities=*/false, |
| 6680 | RegionOfInterest); |
| 6681 | MacroArgMarker.visitPreprocessedEntitiesInRegion(); |
| 6682 | } |
| 6683 | |
| 6684 | // Annotate all of the source locations in the region of interest that map to |
| 6685 | // a specific cursor. |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 6686 | AnnotateTokensWorker W(Tokens, Cursors, NumTokens, TU, RegionOfInterest); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6687 | |
| 6688 | // FIXME: We use a ridiculous stack size here because the data-recursion |
| 6689 | // algorithm uses a large stack frame than the non-data recursive version, |
| 6690 | // and AnnotationTokensWorker currently transforms the data-recursion |
| 6691 | // algorithm back into a traditional recursion by explicitly calling |
| 6692 | // VisitChildren(). We will need to remove this explicit recursive call. |
| 6693 | W.AnnotateTokens(); |
| 6694 | |
| 6695 | // If we ran into any entities that involve context-sensitive keywords, |
| 6696 | // take another pass through the tokens to mark them as such. |
| 6697 | if (W.hasContextSensitiveKeywords()) { |
| 6698 | for (unsigned I = 0; I != NumTokens; ++I) { |
| 6699 | if (clang_getTokenKind(Tokens[I]) != CXToken_Identifier) |
| 6700 | continue; |
| 6701 | |
| 6702 | if (Cursors[I].kind == CXCursor_ObjCPropertyDecl) { |
| 6703 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6704 | if (const ObjCPropertyDecl *Property |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6705 | = dyn_cast_or_null<ObjCPropertyDecl>(getCursorDecl(Cursors[I]))) { |
| 6706 | if (Property->getPropertyAttributesAsWritten() != 0 && |
| 6707 | llvm::StringSwitch<bool>(II->getName()) |
| 6708 | .Case("readonly", true) |
| 6709 | .Case("assign", true) |
| 6710 | .Case("unsafe_unretained", true) |
| 6711 | .Case("readwrite", true) |
| 6712 | .Case("retain", true) |
| 6713 | .Case("copy", true) |
| 6714 | .Case("nonatomic", true) |
| 6715 | .Case("atomic", true) |
| 6716 | .Case("getter", true) |
| 6717 | .Case("setter", true) |
| 6718 | .Case("strong", true) |
| 6719 | .Case("weak", true) |
| 6720 | .Default(false)) |
| 6721 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 6722 | } |
| 6723 | continue; |
| 6724 | } |
| 6725 | |
| 6726 | if (Cursors[I].kind == CXCursor_ObjCInstanceMethodDecl || |
| 6727 | Cursors[I].kind == CXCursor_ObjCClassMethodDecl) { |
| 6728 | IdentifierInfo *II = static_cast<IdentifierInfo *>(Tokens[I].ptr_data); |
| 6729 | if (llvm::StringSwitch<bool>(II->getName()) |
| 6730 | .Case("in", true) |
| 6731 | .Case("out", true) |
| 6732 | .Case("inout", true) |
| 6733 | .Case("oneway", true) |
| 6734 | .Case("bycopy", true) |
| 6735 | .Case("byref", true) |
| 6736 | .Default(false)) |
| 6737 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 6738 | continue; |
| 6739 | } |
| 6740 | |
| 6741 | if (Cursors[I].kind == CXCursor_CXXFinalAttr || |
| 6742 | Cursors[I].kind == CXCursor_CXXOverrideAttr) { |
| 6743 | Tokens[I].int_data[0] = CXToken_Keyword; |
| 6744 | continue; |
| 6745 | } |
| 6746 | } |
| 6747 | } |
| 6748 | } |
| 6749 | |
| 6750 | extern "C" { |
| 6751 | |
| 6752 | void clang_annotateTokens(CXTranslationUnit TU, |
| 6753 | CXToken *Tokens, unsigned NumTokens, |
| 6754 | CXCursor *Cursors) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 6755 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 6756 | LOG_BAD_TU(TU); |
| 6757 | return; |
| 6758 | } |
| 6759 | if (NumTokens == 0 || !Tokens || !Cursors) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 6760 | LOG_FUNC_SECTION { *Log << "<null input>"; } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6761 | return; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 6762 | } |
| 6763 | |
| 6764 | LOG_FUNC_SECTION { |
| 6765 | *Log << TU << ' '; |
| 6766 | CXSourceLocation bloc = clang_getTokenLocation(TU, Tokens[0]); |
| 6767 | CXSourceLocation eloc = clang_getTokenLocation(TU, Tokens[NumTokens-1]); |
| 6768 | *Log << clang_getRange(bloc, eloc); |
| 6769 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6770 | |
| 6771 | // Any token we don't specifically annotate will have a NULL cursor. |
| 6772 | CXCursor C = clang_getNullCursor(); |
| 6773 | for (unsigned I = 0; I != NumTokens; ++I) |
| 6774 | Cursors[I] = C; |
| 6775 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 6776 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6777 | if (!CXXUnit) |
| 6778 | return; |
| 6779 | |
| 6780 | ASTUnit::ConcurrencyCheck Check(*CXXUnit); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 6781 | |
| 6782 | auto AnnotateTokensImpl = [=]() { |
| 6783 | clang_annotateTokensImpl(TU, CXXUnit, Tokens, NumTokens, Cursors); |
| 6784 | }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6785 | llvm::CrashRecoveryContext CRC; |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 6786 | if (!RunSafely(CRC, AnnotateTokensImpl, GetSafetyThreadStackSize() * 2)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6787 | fprintf(stderr, "libclang: crash detected while annotating tokens\n"); |
| 6788 | } |
| 6789 | } |
| 6790 | |
| 6791 | } // end: extern "C" |
| 6792 | |
| 6793 | //===----------------------------------------------------------------------===// |
| 6794 | // Operations for querying linkage of a cursor. |
| 6795 | //===----------------------------------------------------------------------===// |
| 6796 | |
| 6797 | extern "C" { |
| 6798 | CXLinkageKind clang_getCursorLinkage(CXCursor cursor) { |
| 6799 | if (!clang_isDeclaration(cursor.kind)) |
| 6800 | return CXLinkage_Invalid; |
| 6801 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 6802 | const Decl *D = cxcursor::getCursorDecl(cursor); |
| 6803 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 6804 | switch (ND->getLinkageInternal()) { |
Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 6805 | case NoLinkage: |
| 6806 | case VisibleNoLinkage: return CXLinkage_NoLinkage; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6807 | case InternalLinkage: return CXLinkage_Internal; |
| 6808 | case UniqueExternalLinkage: return CXLinkage_UniqueExternal; |
| 6809 | case ExternalLinkage: return CXLinkage_External; |
| 6810 | }; |
| 6811 | |
| 6812 | return CXLinkage_Invalid; |
| 6813 | } |
| 6814 | } // end: extern "C" |
| 6815 | |
| 6816 | //===----------------------------------------------------------------------===// |
Ehsan Akhgari | 93697fa | 2015-11-23 19:56:46 +0000 | [diff] [blame] | 6817 | // Operations for querying visibility of a cursor. |
| 6818 | //===----------------------------------------------------------------------===// |
| 6819 | |
| 6820 | extern "C" { |
| 6821 | CXVisibilityKind clang_getCursorVisibility(CXCursor cursor) { |
| 6822 | if (!clang_isDeclaration(cursor.kind)) |
| 6823 | return CXVisibility_Invalid; |
| 6824 | |
| 6825 | const Decl *D = cxcursor::getCursorDecl(cursor); |
| 6826 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D)) |
| 6827 | switch (ND->getVisibility()) { |
| 6828 | case HiddenVisibility: return CXVisibility_Hidden; |
| 6829 | case ProtectedVisibility: return CXVisibility_Protected; |
| 6830 | case DefaultVisibility: return CXVisibility_Default; |
| 6831 | }; |
| 6832 | |
| 6833 | return CXVisibility_Invalid; |
| 6834 | } |
| 6835 | } // end: extern "C" |
| 6836 | |
| 6837 | //===----------------------------------------------------------------------===// |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6838 | // Operations for querying language of a cursor. |
| 6839 | //===----------------------------------------------------------------------===// |
| 6840 | |
| 6841 | static CXLanguageKind getDeclLanguage(const Decl *D) { |
| 6842 | if (!D) |
| 6843 | return CXLanguage_C; |
| 6844 | |
| 6845 | switch (D->getKind()) { |
| 6846 | default: |
| 6847 | break; |
| 6848 | case Decl::ImplicitParam: |
| 6849 | case Decl::ObjCAtDefsField: |
| 6850 | case Decl::ObjCCategory: |
| 6851 | case Decl::ObjCCategoryImpl: |
| 6852 | case Decl::ObjCCompatibleAlias: |
| 6853 | case Decl::ObjCImplementation: |
| 6854 | case Decl::ObjCInterface: |
| 6855 | case Decl::ObjCIvar: |
| 6856 | case Decl::ObjCMethod: |
| 6857 | case Decl::ObjCProperty: |
| 6858 | case Decl::ObjCPropertyImpl: |
| 6859 | case Decl::ObjCProtocol: |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 6860 | case Decl::ObjCTypeParam: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6861 | return CXLanguage_ObjC; |
| 6862 | case Decl::CXXConstructor: |
| 6863 | case Decl::CXXConversion: |
| 6864 | case Decl::CXXDestructor: |
| 6865 | case Decl::CXXMethod: |
| 6866 | case Decl::CXXRecord: |
| 6867 | case Decl::ClassTemplate: |
| 6868 | case Decl::ClassTemplatePartialSpecialization: |
| 6869 | case Decl::ClassTemplateSpecialization: |
| 6870 | case Decl::Friend: |
| 6871 | case Decl::FriendTemplate: |
| 6872 | case Decl::FunctionTemplate: |
| 6873 | case Decl::LinkageSpec: |
| 6874 | case Decl::Namespace: |
| 6875 | case Decl::NamespaceAlias: |
| 6876 | case Decl::NonTypeTemplateParm: |
| 6877 | case Decl::StaticAssert: |
| 6878 | case Decl::TemplateTemplateParm: |
| 6879 | case Decl::TemplateTypeParm: |
| 6880 | case Decl::UnresolvedUsingTypename: |
| 6881 | case Decl::UnresolvedUsingValue: |
| 6882 | case Decl::Using: |
| 6883 | case Decl::UsingDirective: |
| 6884 | case Decl::UsingShadow: |
| 6885 | return CXLanguage_CPlusPlus; |
| 6886 | } |
| 6887 | |
| 6888 | return CXLanguage_C; |
| 6889 | } |
| 6890 | |
| 6891 | extern "C" { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 6892 | |
| 6893 | static CXAvailabilityKind getCursorAvailabilityForDecl(const Decl *D) { |
| 6894 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isDeleted()) |
Manuel Klimek | 8e3a7ed | 2015-09-25 17:53:16 +0000 | [diff] [blame] | 6895 | return CXAvailability_NotAvailable; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6896 | |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 6897 | switch (D->getAvailability()) { |
| 6898 | case AR_Available: |
| 6899 | case AR_NotYetIntroduced: |
| 6900 | if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(D)) |
Benjamin Kramer | 656363d | 2013-10-15 18:53:18 +0000 | [diff] [blame] | 6901 | return getCursorAvailabilityForDecl( |
| 6902 | cast<Decl>(EnumConst->getDeclContext())); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 6903 | return CXAvailability_Available; |
| 6904 | |
| 6905 | case AR_Deprecated: |
| 6906 | return CXAvailability_Deprecated; |
| 6907 | |
| 6908 | case AR_Unavailable: |
| 6909 | return CXAvailability_NotAvailable; |
| 6910 | } |
Benjamin Kramer | 656363d | 2013-10-15 18:53:18 +0000 | [diff] [blame] | 6911 | |
| 6912 | llvm_unreachable("Unknown availability kind!"); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 6913 | } |
| 6914 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6915 | enum CXAvailabilityKind clang_getCursorAvailability(CXCursor cursor) { |
| 6916 | if (clang_isDeclaration(cursor.kind)) |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 6917 | if (const Decl *D = cxcursor::getCursorDecl(cursor)) |
| 6918 | return getCursorAvailabilityForDecl(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6919 | |
| 6920 | return CXAvailability_Available; |
| 6921 | } |
| 6922 | |
| 6923 | static CXVersion convertVersion(VersionTuple In) { |
| 6924 | CXVersion Out = { -1, -1, -1 }; |
| 6925 | if (In.empty()) |
| 6926 | return Out; |
| 6927 | |
| 6928 | Out.Major = In.getMajor(); |
| 6929 | |
NAKAMURA Takumi | c2b5d1f | 2013-02-21 02:32:34 +0000 | [diff] [blame] | 6930 | Optional<unsigned> Minor = In.getMinor(); |
| 6931 | if (Minor.hasValue()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6932 | Out.Minor = *Minor; |
| 6933 | else |
| 6934 | return Out; |
| 6935 | |
NAKAMURA Takumi | c2b5d1f | 2013-02-21 02:32:34 +0000 | [diff] [blame] | 6936 | Optional<unsigned> Subminor = In.getSubminor(); |
| 6937 | if (Subminor.hasValue()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 6938 | Out.Subminor = *Subminor; |
| 6939 | |
| 6940 | return Out; |
| 6941 | } |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 6942 | |
| 6943 | static int getCursorPlatformAvailabilityForDecl(const Decl *D, |
| 6944 | int *always_deprecated, |
| 6945 | CXString *deprecated_message, |
| 6946 | int *always_unavailable, |
| 6947 | CXString *unavailable_message, |
| 6948 | CXPlatformAvailability *availability, |
| 6949 | int availability_size) { |
| 6950 | bool HadAvailAttr = false; |
| 6951 | int N = 0; |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 6952 | for (auto A : D->attrs()) { |
| 6953 | if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(A)) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 6954 | HadAvailAttr = true; |
| 6955 | if (always_deprecated) |
| 6956 | *always_deprecated = 1; |
Nico Weber | aacf031 | 2014-04-24 05:16:45 +0000 | [diff] [blame] | 6957 | if (deprecated_message) { |
Argyrios Kyrtzidis | edfe07f | 2014-04-24 06:05:40 +0000 | [diff] [blame] | 6958 | clang_disposeString(*deprecated_message); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 6959 | *deprecated_message = cxstring::createDup(Deprecated->getMessage()); |
Nico Weber | aacf031 | 2014-04-24 05:16:45 +0000 | [diff] [blame] | 6960 | } |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 6961 | continue; |
| 6962 | } |
| 6963 | |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 6964 | if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(A)) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 6965 | HadAvailAttr = true; |
| 6966 | if (always_unavailable) |
| 6967 | *always_unavailable = 1; |
| 6968 | if (unavailable_message) { |
Argyrios Kyrtzidis | edfe07f | 2014-04-24 06:05:40 +0000 | [diff] [blame] | 6969 | clang_disposeString(*unavailable_message); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 6970 | *unavailable_message = cxstring::createDup(Unavailable->getMessage()); |
| 6971 | } |
| 6972 | continue; |
| 6973 | } |
| 6974 | |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 6975 | if (AvailabilityAttr *Avail = dyn_cast<AvailabilityAttr>(A)) { |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 6976 | HadAvailAttr = true; |
| 6977 | if (N < availability_size) { |
| 6978 | availability[N].Platform |
| 6979 | = cxstring::createDup(Avail->getPlatform()->getName()); |
| 6980 | availability[N].Introduced = convertVersion(Avail->getIntroduced()); |
| 6981 | availability[N].Deprecated = convertVersion(Avail->getDeprecated()); |
| 6982 | availability[N].Obsoleted = convertVersion(Avail->getObsoleted()); |
| 6983 | availability[N].Unavailable = Avail->getUnavailable(); |
| 6984 | availability[N].Message = cxstring::createDup(Avail->getMessage()); |
| 6985 | } |
| 6986 | ++N; |
| 6987 | } |
| 6988 | } |
| 6989 | |
| 6990 | if (!HadAvailAttr) |
| 6991 | if (const EnumConstantDecl *EnumConst = dyn_cast<EnumConstantDecl>(D)) |
| 6992 | return getCursorPlatformAvailabilityForDecl( |
| 6993 | cast<Decl>(EnumConst->getDeclContext()), |
| 6994 | always_deprecated, |
| 6995 | deprecated_message, |
| 6996 | always_unavailable, |
| 6997 | unavailable_message, |
| 6998 | availability, |
| 6999 | availability_size); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7000 | |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7001 | return N; |
| 7002 | } |
| 7003 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7004 | int clang_getCursorPlatformAvailability(CXCursor cursor, |
| 7005 | int *always_deprecated, |
| 7006 | CXString *deprecated_message, |
| 7007 | int *always_unavailable, |
| 7008 | CXString *unavailable_message, |
| 7009 | CXPlatformAvailability *availability, |
| 7010 | int availability_size) { |
| 7011 | if (always_deprecated) |
| 7012 | *always_deprecated = 0; |
| 7013 | if (deprecated_message) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 7014 | *deprecated_message = cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7015 | if (always_unavailable) |
| 7016 | *always_unavailable = 0; |
| 7017 | if (unavailable_message) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 7018 | *unavailable_message = cxstring::createEmpty(); |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7019 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7020 | if (!clang_isDeclaration(cursor.kind)) |
| 7021 | return 0; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7022 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7023 | const Decl *D = cxcursor::getCursorDecl(cursor); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7024 | if (!D) |
| 7025 | return 0; |
Argyrios Kyrtzidis | dc2973f | 2013-10-15 17:00:53 +0000 | [diff] [blame] | 7026 | |
| 7027 | return getCursorPlatformAvailabilityForDecl(D, always_deprecated, |
| 7028 | deprecated_message, |
| 7029 | always_unavailable, |
| 7030 | unavailable_message, |
| 7031 | availability, |
| 7032 | availability_size); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7033 | } |
| 7034 | |
| 7035 | void clang_disposeCXPlatformAvailability(CXPlatformAvailability *availability) { |
| 7036 | clang_disposeString(availability->Platform); |
| 7037 | clang_disposeString(availability->Message); |
| 7038 | } |
| 7039 | |
| 7040 | CXLanguageKind clang_getCursorLanguage(CXCursor cursor) { |
| 7041 | if (clang_isDeclaration(cursor.kind)) |
| 7042 | return getDeclLanguage(cxcursor::getCursorDecl(cursor)); |
| 7043 | |
| 7044 | return CXLanguage_Invalid; |
| 7045 | } |
| 7046 | |
| 7047 | /// \brief If the given cursor is the "templated" declaration |
| 7048 | /// descibing a class or function template, return the class or |
| 7049 | /// function template. |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7050 | static const Decl *maybeGetTemplateCursor(const Decl *D) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7051 | if (!D) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7052 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7053 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7054 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7055 | if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate()) |
| 7056 | return FunTmpl; |
| 7057 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7058 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7059 | if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate()) |
| 7060 | return ClassTmpl; |
| 7061 | |
| 7062 | return D; |
| 7063 | } |
| 7064 | |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 7065 | |
| 7066 | enum CX_StorageClass clang_Cursor_getStorageClass(CXCursor C) { |
| 7067 | StorageClass sc = SC_None; |
| 7068 | const Decl *D = getCursorDecl(C); |
| 7069 | if (D) { |
| 7070 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7071 | sc = FD->getStorageClass(); |
| 7072 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7073 | sc = VD->getStorageClass(); |
| 7074 | } else { |
| 7075 | return CX_SC_Invalid; |
| 7076 | } |
| 7077 | } else { |
| 7078 | return CX_SC_Invalid; |
| 7079 | } |
| 7080 | switch (sc) { |
| 7081 | case SC_None: |
| 7082 | return CX_SC_None; |
| 7083 | case SC_Extern: |
| 7084 | return CX_SC_Extern; |
| 7085 | case SC_Static: |
| 7086 | return CX_SC_Static; |
| 7087 | case SC_PrivateExtern: |
| 7088 | return CX_SC_PrivateExtern; |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 7089 | case SC_Auto: |
| 7090 | return CX_SC_Auto; |
| 7091 | case SC_Register: |
| 7092 | return CX_SC_Register; |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 7093 | } |
Kaelyn Takata | ab61e70 | 2014-10-15 18:03:26 +0000 | [diff] [blame] | 7094 | llvm_unreachable("Unhandled storage class!"); |
Argyrios Kyrtzidis | 4e0854f | 2014-10-15 17:05:31 +0000 | [diff] [blame] | 7095 | } |
| 7096 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7097 | CXCursor clang_getCursorSemanticParent(CXCursor cursor) { |
| 7098 | if (clang_isDeclaration(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7099 | if (const Decl *D = getCursorDecl(cursor)) { |
| 7100 | const DeclContext *DC = D->getDeclContext(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7101 | if (!DC) |
| 7102 | return clang_getNullCursor(); |
| 7103 | |
| 7104 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 7105 | getCursorTU(cursor)); |
| 7106 | } |
| 7107 | } |
| 7108 | |
| 7109 | if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7110 | if (const Decl *D = getCursorDecl(cursor)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7111 | return MakeCXCursor(D, getCursorTU(cursor)); |
| 7112 | } |
| 7113 | |
| 7114 | return clang_getNullCursor(); |
| 7115 | } |
| 7116 | |
| 7117 | CXCursor clang_getCursorLexicalParent(CXCursor cursor) { |
| 7118 | if (clang_isDeclaration(cursor.kind)) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7119 | if (const Decl *D = getCursorDecl(cursor)) { |
| 7120 | const DeclContext *DC = D->getLexicalDeclContext(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7121 | if (!DC) |
| 7122 | return clang_getNullCursor(); |
| 7123 | |
| 7124 | return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)), |
| 7125 | getCursorTU(cursor)); |
| 7126 | } |
| 7127 | } |
| 7128 | |
| 7129 | // FIXME: Note that we can't easily compute the lexical context of a |
| 7130 | // statement or expression, so we return nothing. |
| 7131 | return clang_getNullCursor(); |
| 7132 | } |
| 7133 | |
| 7134 | CXFile clang_getIncludedFile(CXCursor cursor) { |
| 7135 | if (cursor.kind != CXCursor_InclusionDirective) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7136 | return nullptr; |
| 7137 | |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 7138 | const InclusionDirective *ID = getCursorInclusionDirective(cursor); |
Dmitri Gribenko | f930448 | 2013-01-23 15:56:07 +0000 | [diff] [blame] | 7139 | return const_cast<FileEntry *>(ID->getFile()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7140 | } |
| 7141 | |
Argyrios Kyrtzidis | 9adfd8a | 2013-04-18 22:15:49 +0000 | [diff] [blame] | 7142 | unsigned clang_Cursor_getObjCPropertyAttributes(CXCursor C, unsigned reserved) { |
| 7143 | if (C.kind != CXCursor_ObjCPropertyDecl) |
| 7144 | return CXObjCPropertyAttr_noattr; |
| 7145 | |
| 7146 | unsigned Result = CXObjCPropertyAttr_noattr; |
| 7147 | const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(getCursorDecl(C)); |
| 7148 | ObjCPropertyDecl::PropertyAttributeKind Attr = |
| 7149 | PD->getPropertyAttributesAsWritten(); |
| 7150 | |
| 7151 | #define SET_CXOBJCPROP_ATTR(A) \ |
| 7152 | if (Attr & ObjCPropertyDecl::OBJC_PR_##A) \ |
| 7153 | Result |= CXObjCPropertyAttr_##A |
| 7154 | SET_CXOBJCPROP_ATTR(readonly); |
| 7155 | SET_CXOBJCPROP_ATTR(getter); |
| 7156 | SET_CXOBJCPROP_ATTR(assign); |
| 7157 | SET_CXOBJCPROP_ATTR(readwrite); |
| 7158 | SET_CXOBJCPROP_ATTR(retain); |
| 7159 | SET_CXOBJCPROP_ATTR(copy); |
| 7160 | SET_CXOBJCPROP_ATTR(nonatomic); |
| 7161 | SET_CXOBJCPROP_ATTR(setter); |
| 7162 | SET_CXOBJCPROP_ATTR(atomic); |
| 7163 | SET_CXOBJCPROP_ATTR(weak); |
| 7164 | SET_CXOBJCPROP_ATTR(strong); |
| 7165 | SET_CXOBJCPROP_ATTR(unsafe_unretained); |
| 7166 | #undef SET_CXOBJCPROP_ATTR |
| 7167 | |
| 7168 | return Result; |
| 7169 | } |
| 7170 | |
Argyrios Kyrtzidis | 9d9bc01 | 2013-04-18 23:29:12 +0000 | [diff] [blame] | 7171 | unsigned clang_Cursor_getObjCDeclQualifiers(CXCursor C) { |
| 7172 | if (!clang_isDeclaration(C.kind)) |
| 7173 | return CXObjCDeclQualifier_None; |
| 7174 | |
| 7175 | Decl::ObjCDeclQualifier QT = Decl::OBJC_TQ_None; |
| 7176 | const Decl *D = getCursorDecl(C); |
| 7177 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 7178 | QT = MD->getObjCDeclQualifier(); |
| 7179 | else if (const ParmVarDecl *PD = dyn_cast<ParmVarDecl>(D)) |
| 7180 | QT = PD->getObjCDeclQualifier(); |
| 7181 | if (QT == Decl::OBJC_TQ_None) |
| 7182 | return CXObjCDeclQualifier_None; |
| 7183 | |
| 7184 | unsigned Result = CXObjCDeclQualifier_None; |
| 7185 | if (QT & Decl::OBJC_TQ_In) Result |= CXObjCDeclQualifier_In; |
| 7186 | if (QT & Decl::OBJC_TQ_Inout) Result |= CXObjCDeclQualifier_Inout; |
| 7187 | if (QT & Decl::OBJC_TQ_Out) Result |= CXObjCDeclQualifier_Out; |
| 7188 | if (QT & Decl::OBJC_TQ_Bycopy) Result |= CXObjCDeclQualifier_Bycopy; |
| 7189 | if (QT & Decl::OBJC_TQ_Byref) Result |= CXObjCDeclQualifier_Byref; |
| 7190 | if (QT & Decl::OBJC_TQ_Oneway) Result |= CXObjCDeclQualifier_Oneway; |
| 7191 | |
| 7192 | return Result; |
| 7193 | } |
| 7194 | |
Argyrios Kyrtzidis | 7b50fc5 | 2013-07-05 20:44:37 +0000 | [diff] [blame] | 7195 | unsigned clang_Cursor_isObjCOptional(CXCursor C) { |
| 7196 | if (!clang_isDeclaration(C.kind)) |
| 7197 | return 0; |
| 7198 | |
| 7199 | const Decl *D = getCursorDecl(C); |
| 7200 | if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 7201 | return PD->getPropertyImplementation() == ObjCPropertyDecl::Optional; |
| 7202 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 7203 | return MD->getImplementationControl() == ObjCMethodDecl::Optional; |
| 7204 | |
| 7205 | return 0; |
| 7206 | } |
| 7207 | |
Argyrios Kyrtzidis | 23814e4 | 2013-04-18 23:53:05 +0000 | [diff] [blame] | 7208 | unsigned clang_Cursor_isVariadic(CXCursor C) { |
| 7209 | if (!clang_isDeclaration(C.kind)) |
| 7210 | return 0; |
| 7211 | |
| 7212 | const Decl *D = getCursorDecl(C); |
| 7213 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 7214 | return FD->isVariadic(); |
| 7215 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 7216 | return MD->isVariadic(); |
| 7217 | |
| 7218 | return 0; |
| 7219 | } |
| 7220 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7221 | CXSourceRange clang_Cursor_getCommentRange(CXCursor C) { |
| 7222 | if (!clang_isDeclaration(C.kind)) |
| 7223 | return clang_getNullRange(); |
| 7224 | |
| 7225 | const Decl *D = getCursorDecl(C); |
| 7226 | ASTContext &Context = getCursorContext(C); |
| 7227 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 7228 | if (!RC) |
| 7229 | return clang_getNullRange(); |
| 7230 | |
| 7231 | return cxloc::translateSourceRange(Context, RC->getSourceRange()); |
| 7232 | } |
| 7233 | |
| 7234 | CXString clang_Cursor_getRawCommentText(CXCursor C) { |
| 7235 | if (!clang_isDeclaration(C.kind)) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 7236 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7237 | |
| 7238 | const Decl *D = getCursorDecl(C); |
| 7239 | ASTContext &Context = getCursorContext(C); |
| 7240 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 7241 | StringRef RawText = RC ? RC->getRawText(Context.getSourceManager()) : |
| 7242 | StringRef(); |
| 7243 | |
| 7244 | // Don't duplicate the string because RawText points directly into source |
| 7245 | // code. |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 7246 | return cxstring::createRef(RawText); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7247 | } |
| 7248 | |
| 7249 | CXString clang_Cursor_getBriefCommentText(CXCursor C) { |
| 7250 | if (!clang_isDeclaration(C.kind)) |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 7251 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7252 | |
| 7253 | const Decl *D = getCursorDecl(C); |
| 7254 | const ASTContext &Context = getCursorContext(C); |
| 7255 | const RawComment *RC = Context.getRawCommentForAnyRedecl(D); |
| 7256 | |
| 7257 | if (RC) { |
| 7258 | StringRef BriefText = RC->getBriefText(Context); |
| 7259 | |
| 7260 | // Don't duplicate the string because RawComment ensures that this memory |
| 7261 | // will not go away. |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 7262 | return cxstring::createRef(BriefText); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7263 | } |
| 7264 | |
Dmitri Gribenko | f98dfba | 2013-02-01 14:13:32 +0000 | [diff] [blame] | 7265 | return cxstring::createNull(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7266 | } |
| 7267 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7268 | CXModule clang_Cursor_getModule(CXCursor C) { |
| 7269 | if (C.kind == CXCursor_ModuleImportDecl) { |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7270 | if (const ImportDecl *ImportD = |
| 7271 | dyn_cast_or_null<ImportDecl>(getCursorDecl(C))) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7272 | return ImportD->getImportedModule(); |
| 7273 | } |
| 7274 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7275 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7276 | } |
| 7277 | |
Argyrios Kyrtzidis | f6d49c3 | 2014-05-14 23:14:37 +0000 | [diff] [blame] | 7278 | CXModule clang_getModuleForFile(CXTranslationUnit TU, CXFile File) { |
| 7279 | if (isNotUsableTU(TU)) { |
| 7280 | LOG_BAD_TU(TU); |
| 7281 | return nullptr; |
| 7282 | } |
| 7283 | if (!File) |
| 7284 | return nullptr; |
| 7285 | FileEntry *FE = static_cast<FileEntry *>(File); |
| 7286 | |
| 7287 | ASTUnit &Unit = *cxtu::getASTUnit(TU); |
| 7288 | HeaderSearch &HS = Unit.getPreprocessor().getHeaderSearchInfo(); |
| 7289 | ModuleMap::KnownHeader Header = HS.findModuleForHeader(FE); |
| 7290 | |
Richard Smith | feb54b6 | 2014-10-23 02:01:19 +0000 | [diff] [blame] | 7291 | return Header.getModule(); |
Argyrios Kyrtzidis | f6d49c3 | 2014-05-14 23:14:37 +0000 | [diff] [blame] | 7292 | } |
| 7293 | |
Argyrios Kyrtzidis | 12fdb9e | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 7294 | CXFile clang_Module_getASTFile(CXModule CXMod) { |
| 7295 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7296 | return nullptr; |
Argyrios Kyrtzidis | 12fdb9e | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 7297 | Module *Mod = static_cast<Module*>(CXMod); |
| 7298 | return const_cast<FileEntry *>(Mod->getASTFile()); |
| 7299 | } |
| 7300 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7301 | CXModule clang_Module_getParent(CXModule CXMod) { |
| 7302 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7303 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7304 | Module *Mod = static_cast<Module*>(CXMod); |
| 7305 | return Mod->Parent; |
| 7306 | } |
| 7307 | |
| 7308 | CXString clang_Module_getName(CXModule CXMod) { |
| 7309 | if (!CXMod) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 7310 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7311 | Module *Mod = static_cast<Module*>(CXMod); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 7312 | return cxstring::createDup(Mod->Name); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7313 | } |
| 7314 | |
| 7315 | CXString clang_Module_getFullName(CXModule CXMod) { |
| 7316 | if (!CXMod) |
Dmitri Gribenko | 36a6dd0 | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 7317 | return cxstring::createEmpty(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7318 | Module *Mod = static_cast<Module*>(CXMod); |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 7319 | return cxstring::createDup(Mod->getFullModuleName()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7320 | } |
| 7321 | |
Argyrios Kyrtzidis | 884337f | 2014-05-15 04:44:25 +0000 | [diff] [blame] | 7322 | int clang_Module_isSystem(CXModule CXMod) { |
| 7323 | if (!CXMod) |
| 7324 | return 0; |
| 7325 | Module *Mod = static_cast<Module*>(CXMod); |
| 7326 | return Mod->IsSystem; |
| 7327 | } |
| 7328 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7329 | unsigned clang_Module_getNumTopLevelHeaders(CXTranslationUnit TU, |
| 7330 | CXModule CXMod) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7331 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7332 | LOG_BAD_TU(TU); |
| 7333 | return 0; |
| 7334 | } |
| 7335 | if (!CXMod) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7336 | return 0; |
| 7337 | Module *Mod = static_cast<Module*>(CXMod); |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7338 | FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager(); |
| 7339 | ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr); |
| 7340 | return TopHeaders.size(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7341 | } |
| 7342 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7343 | CXFile clang_Module_getTopLevelHeader(CXTranslationUnit TU, |
| 7344 | CXModule CXMod, unsigned Index) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7345 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7346 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7347 | return nullptr; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7348 | } |
| 7349 | if (!CXMod) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7350 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7351 | Module *Mod = static_cast<Module*>(CXMod); |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7352 | FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7353 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 7354 | ArrayRef<const FileEntry *> TopHeaders = Mod->getTopHeaders(FileMgr); |
| 7355 | if (Index < TopHeaders.size()) |
| 7356 | return const_cast<FileEntry *>(TopHeaders[Index]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7357 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7358 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7359 | } |
| 7360 | |
| 7361 | } // end: extern "C" |
| 7362 | |
| 7363 | //===----------------------------------------------------------------------===// |
| 7364 | // C++ AST instrospection. |
| 7365 | //===----------------------------------------------------------------------===// |
| 7366 | |
| 7367 | extern "C" { |
Jonathan Coe | 2956535 | 2016-04-27 12:48:25 +0000 | [diff] [blame] | 7368 | |
| 7369 | unsigned clang_CXXConstructor_isDefaultConstructor(CXCursor C) { |
| 7370 | if (!clang_isDeclaration(C.kind)) |
| 7371 | return 0; |
| 7372 | |
| 7373 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7374 | const CXXConstructorDecl *Constructor = |
| 7375 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 7376 | return (Constructor && Constructor->isDefaultConstructor()) ? 1 : 0; |
| 7377 | } |
| 7378 | |
| 7379 | unsigned clang_CXXConstructor_isCopyConstructor(CXCursor C) { |
| 7380 | if (!clang_isDeclaration(C.kind)) |
| 7381 | return 0; |
| 7382 | |
| 7383 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7384 | const CXXConstructorDecl *Constructor = |
| 7385 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 7386 | return (Constructor && Constructor->isCopyConstructor()) ? 1 : 0; |
| 7387 | } |
| 7388 | |
| 7389 | unsigned clang_CXXConstructor_isMoveConstructor(CXCursor C) { |
| 7390 | if (!clang_isDeclaration(C.kind)) |
| 7391 | return 0; |
| 7392 | |
| 7393 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7394 | const CXXConstructorDecl *Constructor = |
| 7395 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 7396 | return (Constructor && Constructor->isMoveConstructor()) ? 1 : 0; |
| 7397 | } |
| 7398 | |
| 7399 | unsigned clang_CXXConstructor_isConvertingConstructor(CXCursor C) { |
| 7400 | if (!clang_isDeclaration(C.kind)) |
| 7401 | return 0; |
| 7402 | |
| 7403 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7404 | const CXXConstructorDecl *Constructor = |
| 7405 | D ? dyn_cast_or_null<CXXConstructorDecl>(D->getAsFunction()) : nullptr; |
| 7406 | // Passing 'false' excludes constructors marked 'explicit'. |
| 7407 | return (Constructor && Constructor->isConvertingConstructor(false)) ? 1 : 0; |
| 7408 | } |
| 7409 | |
Saleem Abdulrasool | 6ea75db | 2015-10-27 15:50:22 +0000 | [diff] [blame] | 7410 | unsigned clang_CXXField_isMutable(CXCursor C) { |
| 7411 | if (!clang_isDeclaration(C.kind)) |
| 7412 | return 0; |
| 7413 | |
| 7414 | if (const auto D = cxcursor::getCursorDecl(C)) |
| 7415 | if (const auto FD = dyn_cast_or_null<FieldDecl>(D)) |
| 7416 | return FD->isMutable() ? 1 : 0; |
| 7417 | return 0; |
| 7418 | } |
| 7419 | |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 7420 | unsigned clang_CXXMethod_isPureVirtual(CXCursor C) { |
| 7421 | if (!clang_isDeclaration(C.kind)) |
| 7422 | return 0; |
| 7423 | |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 7424 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 7425 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7426 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 7427 | return (Method && Method->isVirtual() && Method->isPure()) ? 1 : 0; |
| 7428 | } |
| 7429 | |
Dmitri Gribenko | e570ede | 2014-04-07 14:59:13 +0000 | [diff] [blame] | 7430 | unsigned clang_CXXMethod_isConst(CXCursor C) { |
| 7431 | if (!clang_isDeclaration(C.kind)) |
| 7432 | return 0; |
| 7433 | |
| 7434 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7435 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7436 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Dmitri Gribenko | e570ede | 2014-04-07 14:59:13 +0000 | [diff] [blame] | 7437 | return (Method && (Method->getTypeQualifiers() & Qualifiers::Const)) ? 1 : 0; |
| 7438 | } |
| 7439 | |
Jonathan Coe | 2956535 | 2016-04-27 12:48:25 +0000 | [diff] [blame] | 7440 | unsigned clang_CXXMethod_isDefaulted(CXCursor C) { |
| 7441 | if (!clang_isDeclaration(C.kind)) |
| 7442 | return 0; |
| 7443 | |
| 7444 | const Decl *D = cxcursor::getCursorDecl(C); |
| 7445 | const CXXMethodDecl *Method = |
| 7446 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
| 7447 | return (Method && Method->isDefaulted()) ? 1 : 0; |
| 7448 | } |
| 7449 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7450 | unsigned clang_CXXMethod_isStatic(CXCursor C) { |
| 7451 | if (!clang_isDeclaration(C.kind)) |
| 7452 | return 0; |
| 7453 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7454 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 7455 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7456 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7457 | return (Method && Method->isStatic()) ? 1 : 0; |
| 7458 | } |
| 7459 | |
| 7460 | unsigned clang_CXXMethod_isVirtual(CXCursor C) { |
| 7461 | if (!clang_isDeclaration(C.kind)) |
| 7462 | return 0; |
| 7463 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 7464 | const Decl *D = cxcursor::getCursorDecl(C); |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 7465 | const CXXMethodDecl *Method = |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7466 | D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7467 | return (Method && Method->isVirtual()) ? 1 : 0; |
| 7468 | } |
| 7469 | } // end: extern "C" |
| 7470 | |
| 7471 | //===----------------------------------------------------------------------===// |
| 7472 | // Attribute introspection. |
| 7473 | //===----------------------------------------------------------------------===// |
| 7474 | |
| 7475 | extern "C" { |
| 7476 | CXType clang_getIBOutletCollectionType(CXCursor C) { |
| 7477 | if (C.kind != CXCursor_IBOutletCollectionAttr) |
| 7478 | return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C)); |
| 7479 | |
Dmitri Gribenko | e4baea6 | 2013-01-26 18:08:08 +0000 | [diff] [blame] | 7480 | const IBOutletCollectionAttr *A = |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7481 | cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C)); |
| 7482 | |
| 7483 | return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C)); |
| 7484 | } |
| 7485 | } // end: extern "C" |
| 7486 | |
| 7487 | //===----------------------------------------------------------------------===// |
| 7488 | // Inspecting memory usage. |
| 7489 | //===----------------------------------------------------------------------===// |
| 7490 | |
| 7491 | typedef std::vector<CXTUResourceUsageEntry> MemUsageEntries; |
| 7492 | |
| 7493 | static inline void createCXTUResourceUsageEntry(MemUsageEntries &entries, |
| 7494 | enum CXTUResourceUsageKind k, |
| 7495 | unsigned long amount) { |
| 7496 | CXTUResourceUsageEntry entry = { k, amount }; |
| 7497 | entries.push_back(entry); |
| 7498 | } |
| 7499 | |
| 7500 | extern "C" { |
| 7501 | |
| 7502 | const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) { |
| 7503 | const char *str = ""; |
| 7504 | switch (kind) { |
| 7505 | case CXTUResourceUsage_AST: |
| 7506 | str = "ASTContext: expressions, declarations, and types"; |
| 7507 | break; |
| 7508 | case CXTUResourceUsage_Identifiers: |
| 7509 | str = "ASTContext: identifiers"; |
| 7510 | break; |
| 7511 | case CXTUResourceUsage_Selectors: |
| 7512 | str = "ASTContext: selectors"; |
| 7513 | break; |
| 7514 | case CXTUResourceUsage_GlobalCompletionResults: |
| 7515 | str = "Code completion: cached global results"; |
| 7516 | break; |
| 7517 | case CXTUResourceUsage_SourceManagerContentCache: |
| 7518 | str = "SourceManager: content cache allocator"; |
| 7519 | break; |
| 7520 | case CXTUResourceUsage_AST_SideTables: |
| 7521 | str = "ASTContext: side tables"; |
| 7522 | break; |
| 7523 | case CXTUResourceUsage_SourceManager_Membuffer_Malloc: |
| 7524 | str = "SourceManager: malloc'ed memory buffers"; |
| 7525 | break; |
| 7526 | case CXTUResourceUsage_SourceManager_Membuffer_MMap: |
| 7527 | str = "SourceManager: mmap'ed memory buffers"; |
| 7528 | break; |
| 7529 | case CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc: |
| 7530 | str = "ExternalASTSource: malloc'ed memory buffers"; |
| 7531 | break; |
| 7532 | case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap: |
| 7533 | str = "ExternalASTSource: mmap'ed memory buffers"; |
| 7534 | break; |
| 7535 | case CXTUResourceUsage_Preprocessor: |
| 7536 | str = "Preprocessor: malloc'ed memory"; |
| 7537 | break; |
| 7538 | case CXTUResourceUsage_PreprocessingRecord: |
| 7539 | str = "Preprocessor: PreprocessingRecord"; |
| 7540 | break; |
| 7541 | case CXTUResourceUsage_SourceManager_DataStructures: |
| 7542 | str = "SourceManager: data structures and tables"; |
| 7543 | break; |
| 7544 | case CXTUResourceUsage_Preprocessor_HeaderSearch: |
| 7545 | str = "Preprocessor: header search tables"; |
| 7546 | break; |
| 7547 | } |
| 7548 | return str; |
| 7549 | } |
| 7550 | |
| 7551 | CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) { |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7552 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7553 | LOG_BAD_TU(TU); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7554 | CXTUResourceUsage usage = { (void*) nullptr, 0, nullptr }; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7555 | return usage; |
| 7556 | } |
| 7557 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7558 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 7559 | std::unique_ptr<MemUsageEntries> entries(new MemUsageEntries()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7560 | ASTContext &astContext = astUnit->getASTContext(); |
| 7561 | |
| 7562 | // How much memory is used by AST nodes and types? |
| 7563 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST, |
| 7564 | (unsigned long) astContext.getASTAllocatedMemory()); |
| 7565 | |
| 7566 | // How much memory is used by identifiers? |
| 7567 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Identifiers, |
| 7568 | (unsigned long) astContext.Idents.getAllocator().getTotalMemory()); |
| 7569 | |
| 7570 | // How much memory is used for selectors? |
| 7571 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_Selectors, |
| 7572 | (unsigned long) astContext.Selectors.getTotalMemory()); |
| 7573 | |
| 7574 | // How much memory is used by ASTContext's side tables? |
| 7575 | createCXTUResourceUsageEntry(*entries, CXTUResourceUsage_AST_SideTables, |
| 7576 | (unsigned long) astContext.getSideTableAllocatedMemory()); |
| 7577 | |
| 7578 | // How much memory is used for caching global code completion results? |
| 7579 | unsigned long completionBytes = 0; |
| 7580 | if (GlobalCodeCompletionAllocator *completionAllocator = |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 7581 | astUnit->getCachedCompletionAllocator().get()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7582 | completionBytes = completionAllocator->getTotalMemory(); |
| 7583 | } |
| 7584 | createCXTUResourceUsageEntry(*entries, |
| 7585 | CXTUResourceUsage_GlobalCompletionResults, |
| 7586 | completionBytes); |
| 7587 | |
| 7588 | // How much memory is being used by SourceManager's content cache? |
| 7589 | createCXTUResourceUsageEntry(*entries, |
| 7590 | CXTUResourceUsage_SourceManagerContentCache, |
| 7591 | (unsigned long) astContext.getSourceManager().getContentCacheSize()); |
| 7592 | |
| 7593 | // How much memory is being used by the MemoryBuffer's in SourceManager? |
| 7594 | const SourceManager::MemoryBufferSizes &srcBufs = |
| 7595 | astUnit->getSourceManager().getMemoryBufferSizes(); |
| 7596 | |
| 7597 | createCXTUResourceUsageEntry(*entries, |
| 7598 | CXTUResourceUsage_SourceManager_Membuffer_Malloc, |
| 7599 | (unsigned long) srcBufs.malloc_bytes); |
| 7600 | createCXTUResourceUsageEntry(*entries, |
| 7601 | CXTUResourceUsage_SourceManager_Membuffer_MMap, |
| 7602 | (unsigned long) srcBufs.mmap_bytes); |
| 7603 | createCXTUResourceUsageEntry(*entries, |
| 7604 | CXTUResourceUsage_SourceManager_DataStructures, |
| 7605 | (unsigned long) astContext.getSourceManager() |
| 7606 | .getDataStructureSizes()); |
| 7607 | |
| 7608 | // How much memory is being used by the ExternalASTSource? |
| 7609 | if (ExternalASTSource *esrc = astContext.getExternalSource()) { |
| 7610 | const ExternalASTSource::MemoryBufferSizes &sizes = |
| 7611 | esrc->getMemoryBufferSizes(); |
| 7612 | |
| 7613 | createCXTUResourceUsageEntry(*entries, |
| 7614 | CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc, |
| 7615 | (unsigned long) sizes.malloc_bytes); |
| 7616 | createCXTUResourceUsageEntry(*entries, |
| 7617 | CXTUResourceUsage_ExternalASTSource_Membuffer_MMap, |
| 7618 | (unsigned long) sizes.mmap_bytes); |
| 7619 | } |
| 7620 | |
| 7621 | // How much memory is being used by the Preprocessor? |
| 7622 | Preprocessor &pp = astUnit->getPreprocessor(); |
| 7623 | createCXTUResourceUsageEntry(*entries, |
| 7624 | CXTUResourceUsage_Preprocessor, |
| 7625 | pp.getTotalMemory()); |
| 7626 | |
| 7627 | if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) { |
| 7628 | createCXTUResourceUsageEntry(*entries, |
| 7629 | CXTUResourceUsage_PreprocessingRecord, |
| 7630 | pRec->getTotalMemory()); |
| 7631 | } |
| 7632 | |
| 7633 | createCXTUResourceUsageEntry(*entries, |
| 7634 | CXTUResourceUsage_Preprocessor_HeaderSearch, |
| 7635 | pp.getHeaderSearchInfo().getTotalMemory()); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7636 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7637 | CXTUResourceUsage usage = { (void*) entries.get(), |
| 7638 | (unsigned) entries->size(), |
Alexander Kornienko | 6ee521c | 2015-01-23 15:36:10 +0000 | [diff] [blame] | 7639 | !entries->empty() ? &(*entries)[0] : nullptr }; |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 7640 | entries.release(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7641 | return usage; |
| 7642 | } |
| 7643 | |
| 7644 | void clang_disposeCXTUResourceUsage(CXTUResourceUsage usage) { |
| 7645 | if (usage.data) |
| 7646 | delete (MemUsageEntries*) usage.data; |
| 7647 | } |
| 7648 | |
Argyrios Kyrtzidis | 0e282ef | 2013-12-06 18:55:45 +0000 | [diff] [blame] | 7649 | CXSourceRangeList *clang_getSkippedRanges(CXTranslationUnit TU, CXFile file) { |
| 7650 | CXSourceRangeList *skipped = new CXSourceRangeList; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 7651 | skipped->count = 0; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7652 | skipped->ranges = nullptr; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 7653 | |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 7654 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 7655 | LOG_BAD_TU(TU); |
| 7656 | return skipped; |
| 7657 | } |
| 7658 | |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 7659 | if (!file) |
| 7660 | return skipped; |
| 7661 | |
| 7662 | ASTUnit *astUnit = cxtu::getASTUnit(TU); |
| 7663 | PreprocessingRecord *ppRec = astUnit->getPreprocessor().getPreprocessingRecord(); |
| 7664 | if (!ppRec) |
| 7665 | return skipped; |
| 7666 | |
| 7667 | ASTContext &Ctx = astUnit->getASTContext(); |
| 7668 | SourceManager &sm = Ctx.getSourceManager(); |
| 7669 | FileEntry *fileEntry = static_cast<FileEntry *>(file); |
| 7670 | FileID wantedFileID = sm.translateFile(fileEntry); |
| 7671 | |
| 7672 | const std::vector<SourceRange> &SkippedRanges = ppRec->getSkippedRanges(); |
| 7673 | std::vector<SourceRange> wantedRanges; |
| 7674 | for (std::vector<SourceRange>::const_iterator i = SkippedRanges.begin(), ei = SkippedRanges.end(); |
| 7675 | i != ei; ++i) { |
| 7676 | if (sm.getFileID(i->getBegin()) == wantedFileID || sm.getFileID(i->getEnd()) == wantedFileID) |
| 7677 | wantedRanges.push_back(*i); |
| 7678 | } |
| 7679 | |
| 7680 | skipped->count = wantedRanges.size(); |
| 7681 | skipped->ranges = new CXSourceRange[skipped->count]; |
| 7682 | for (unsigned i = 0, ei = skipped->count; i != ei; ++i) |
| 7683 | skipped->ranges[i] = cxloc::translateSourceRange(Ctx, wantedRanges[i]); |
| 7684 | |
| 7685 | return skipped; |
| 7686 | } |
| 7687 | |
Argyrios Kyrtzidis | 0e282ef | 2013-12-06 18:55:45 +0000 | [diff] [blame] | 7688 | void clang_disposeSourceRangeList(CXSourceRangeList *ranges) { |
| 7689 | if (ranges) { |
| 7690 | delete[] ranges->ranges; |
| 7691 | delete ranges; |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 7692 | } |
| 7693 | } |
| 7694 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7695 | } // end extern "C" |
| 7696 | |
| 7697 | void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) { |
| 7698 | CXTUResourceUsage Usage = clang_getCXTUResourceUsage(TU); |
| 7699 | for (unsigned I = 0; I != Usage.numEntries; ++I) |
| 7700 | fprintf(stderr, " %s: %lu\n", |
| 7701 | clang_getTUResourceUsageName(Usage.entries[I].kind), |
| 7702 | Usage.entries[I].amount); |
| 7703 | |
| 7704 | clang_disposeCXTUResourceUsage(Usage); |
| 7705 | } |
| 7706 | |
| 7707 | //===----------------------------------------------------------------------===// |
| 7708 | // Misc. utility functions. |
| 7709 | //===----------------------------------------------------------------------===// |
| 7710 | |
| 7711 | /// Default to using an 8 MB stack size on "safety" threads. |
| 7712 | static unsigned SafetyStackThreadSize = 8 << 20; |
| 7713 | |
| 7714 | namespace clang { |
| 7715 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 7716 | bool RunSafely(llvm::CrashRecoveryContext &CRC, llvm::function_ref<void()> Fn, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7717 | unsigned Size) { |
| 7718 | if (!Size) |
| 7719 | Size = GetSafetyThreadStackSize(); |
| 7720 | if (Size) |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame] | 7721 | return CRC.RunSafelyOnThread(Fn, Size); |
| 7722 | return CRC.RunSafely(Fn); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7723 | } |
| 7724 | |
| 7725 | unsigned GetSafetyThreadStackSize() { |
| 7726 | return SafetyStackThreadSize; |
| 7727 | } |
| 7728 | |
| 7729 | void SetSafetyThreadStackSize(unsigned Value) { |
| 7730 | SafetyStackThreadSize = Value; |
| 7731 | } |
| 7732 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7733 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7734 | |
| 7735 | void clang::setThreadBackgroundPriority() { |
| 7736 | if (getenv("LIBCLANG_BGPRIO_DISABLE")) |
| 7737 | return; |
| 7738 | |
Alp Toker | 1a86ad2 | 2014-07-06 06:24:00 +0000 | [diff] [blame] | 7739 | #ifdef USE_DARWIN_THREADS |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7740 | setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG); |
| 7741 | #endif |
| 7742 | } |
| 7743 | |
| 7744 | void cxindex::printDiagsToStderr(ASTUnit *Unit) { |
| 7745 | if (!Unit) |
| 7746 | return; |
| 7747 | |
| 7748 | for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), |
| 7749 | DEnd = Unit->stored_diag_end(); |
| 7750 | D != DEnd; ++D) { |
Ben Langmuir | 749323f | 2014-04-22 17:40:12 +0000 | [diff] [blame] | 7751 | CXStoredDiagnostic Diag(*D, Unit->getLangOpts()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7752 | CXString Msg = clang_formatDiagnostic(&Diag, |
| 7753 | clang_defaultDiagnosticDisplayOptions()); |
| 7754 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 7755 | clang_disposeString(Msg); |
| 7756 | } |
| 7757 | #ifdef LLVM_ON_WIN32 |
| 7758 | // On Windows, force a flush, since there may be multiple copies of |
| 7759 | // stderr and stdout in the file system, all with different buffers |
| 7760 | // but writing to the same device. |
| 7761 | fflush(stderr); |
| 7762 | #endif |
| 7763 | } |
| 7764 | |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7765 | MacroInfo *cxindex::getMacroInfo(const IdentifierInfo &II, |
| 7766 | SourceLocation MacroDefLoc, |
| 7767 | CXTranslationUnit TU){ |
| 7768 | if (MacroDefLoc.isInvalid() || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7769 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7770 | if (!II.hadMacroDefinition()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7771 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7772 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7773 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 2d77aeb | 2013-01-07 19:16:30 +0000 | [diff] [blame] | 7774 | Preprocessor &PP = Unit->getPreprocessor(); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 7775 | MacroDirective *MD = PP.getLocalMacroDirectiveHistory(&II); |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 7776 | if (MD) { |
| 7777 | for (MacroDirective::DefInfo |
| 7778 | Def = MD->getDefinition(); Def; Def = Def.getPreviousDefinition()) { |
| 7779 | if (MacroDefLoc == Def.getMacroInfo()->getDefinitionLoc()) |
| 7780 | return Def.getMacroInfo(); |
| 7781 | } |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7782 | } |
| 7783 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7784 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7785 | } |
| 7786 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 7787 | const MacroInfo *cxindex::getMacroInfo(const MacroDefinitionRecord *MacroDef, |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 7788 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7789 | if (!MacroDef || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7790 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7791 | const IdentifierInfo *II = MacroDef->getName(); |
| 7792 | if (!II) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7793 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7794 | |
| 7795 | return getMacroInfo(*II, MacroDef->getLocation(), TU); |
| 7796 | } |
| 7797 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 7798 | MacroDefinitionRecord * |
| 7799 | cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, const Token &Tok, |
| 7800 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7801 | if (!MI || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7802 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7803 | if (Tok.isNot(tok::raw_identifier)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7804 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7805 | |
| 7806 | if (MI->getNumTokens() == 0) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7807 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7808 | SourceRange DefRange(MI->getReplacementToken(0).getLocation(), |
| 7809 | MI->getDefinitionEndLoc()); |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7810 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7811 | |
| 7812 | // Check that the token is inside the definition and not its argument list. |
| 7813 | SourceManager &SM = Unit->getSourceManager(); |
| 7814 | if (SM.isBeforeInTranslationUnit(Tok.getLocation(), DefRange.getBegin())) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7815 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7816 | if (SM.isBeforeInTranslationUnit(DefRange.getEnd(), Tok.getLocation())) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7817 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7818 | |
| 7819 | Preprocessor &PP = Unit->getPreprocessor(); |
| 7820 | PreprocessingRecord *PPRec = PP.getPreprocessingRecord(); |
| 7821 | if (!PPRec) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7822 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7823 | |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 7824 | IdentifierInfo &II = PP.getIdentifierTable().get(Tok.getRawIdentifier()); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7825 | if (!II.hadMacroDefinition()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7826 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7827 | |
| 7828 | // Check that the identifier is not one of the macro arguments. |
| 7829 | 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] | 7830 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7831 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 7832 | MacroDirective *InnerMD = PP.getLocalMacroDirectiveHistory(&II); |
Argyrios Kyrtzidis | 09c9e81 | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 7833 | if (!InnerMD) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7834 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7835 | |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 7836 | return PPRec->findMacroDefinition(InnerMD->getMacroInfo()); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7837 | } |
| 7838 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 7839 | MacroDefinitionRecord * |
| 7840 | cxindex::checkForMacroInMacroDefinition(const MacroInfo *MI, SourceLocation Loc, |
| 7841 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7842 | if (Loc.isInvalid() || !MI || !TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7843 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7844 | |
| 7845 | if (MI->getNumTokens() == 0) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7846 | return nullptr; |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7847 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7848 | Preprocessor &PP = Unit->getPreprocessor(); |
| 7849 | if (!PP.getPreprocessingRecord()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7850 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7851 | Loc = Unit->getSourceManager().getSpellingLoc(Loc); |
| 7852 | Token Tok; |
| 7853 | if (PP.getRawToken(Loc, Tok)) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7854 | return nullptr; |
Argyrios Kyrtzidis | 579825a | 2013-01-07 19:16:25 +0000 | [diff] [blame] | 7855 | |
| 7856 | return checkForMacroInMacroDefinition(MI, Tok, TU); |
| 7857 | } |
| 7858 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7859 | extern "C" { |
| 7860 | |
| 7861 | CXString clang_getClangVersion() { |
Dmitri Gribenko | 2f23e9c | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 7862 | return cxstring::createDup(getClangFullVersion()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 7863 | } |
| 7864 | |
| 7865 | } // end: extern "C" |
| 7866 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7867 | Logger &cxindex::Logger::operator<<(CXTranslationUnit TU) { |
| 7868 | if (TU) { |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 7869 | if (ASTUnit *Unit = cxtu::getASTUnit(TU)) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7870 | LogOS << '<' << Unit->getMainFileName() << '>'; |
Argyrios Kyrtzidis | 37f2ab4 | 2013-03-05 20:21:14 +0000 | [diff] [blame] | 7871 | if (Unit->isMainFileAST()) |
| 7872 | LogOS << " (" << Unit->getASTFileName() << ')'; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7873 | return *this; |
| 7874 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 7875 | } else { |
| 7876 | LogOS << "<NULL TU>"; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7877 | } |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7878 | return *this; |
| 7879 | } |
| 7880 | |
Argyrios Kyrtzidis | ba4b5f8 | 2013-03-08 02:32:26 +0000 | [diff] [blame] | 7881 | Logger &cxindex::Logger::operator<<(const FileEntry *FE) { |
| 7882 | *this << FE->getName(); |
| 7883 | return *this; |
| 7884 | } |
| 7885 | |
| 7886 | Logger &cxindex::Logger::operator<<(CXCursor cursor) { |
| 7887 | CXString cursorName = clang_getCursorDisplayName(cursor); |
| 7888 | *this << cursorName << "@" << clang_getCursorLocation(cursor); |
| 7889 | clang_disposeString(cursorName); |
| 7890 | return *this; |
| 7891 | } |
| 7892 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7893 | Logger &cxindex::Logger::operator<<(CXSourceLocation Loc) { |
| 7894 | CXFile File; |
| 7895 | unsigned Line, Column; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7896 | clang_getFileLocation(Loc, &File, &Line, &Column, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7897 | CXString FileName = clang_getFileName(File); |
| 7898 | *this << llvm::format("(%s:%d:%d)", clang_getCString(FileName), Line, Column); |
| 7899 | clang_disposeString(FileName); |
| 7900 | return *this; |
| 7901 | } |
| 7902 | |
| 7903 | Logger &cxindex::Logger::operator<<(CXSourceRange range) { |
| 7904 | CXSourceLocation BLoc = clang_getRangeStart(range); |
| 7905 | CXSourceLocation ELoc = clang_getRangeEnd(range); |
| 7906 | |
| 7907 | CXFile BFile; |
| 7908 | unsigned BLine, BColumn; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7909 | clang_getFileLocation(BLoc, &BFile, &BLine, &BColumn, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7910 | |
| 7911 | CXFile EFile; |
| 7912 | unsigned ELine, EColumn; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 7913 | clang_getFileLocation(ELoc, &EFile, &ELine, &EColumn, nullptr); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7914 | |
| 7915 | CXString BFileName = clang_getFileName(BFile); |
| 7916 | if (BFile == EFile) { |
| 7917 | *this << llvm::format("[%s %d:%d-%d:%d]", clang_getCString(BFileName), |
| 7918 | BLine, BColumn, ELine, EColumn); |
| 7919 | } else { |
| 7920 | CXString EFileName = clang_getFileName(EFile); |
| 7921 | *this << llvm::format("[%s:%d:%d - ", clang_getCString(BFileName), |
| 7922 | BLine, BColumn) |
| 7923 | << llvm::format("%s:%d:%d]", clang_getCString(EFileName), |
| 7924 | ELine, EColumn); |
| 7925 | clang_disposeString(EFileName); |
| 7926 | } |
| 7927 | clang_disposeString(BFileName); |
| 7928 | return *this; |
| 7929 | } |
| 7930 | |
| 7931 | Logger &cxindex::Logger::operator<<(CXString Str) { |
| 7932 | *this << clang_getCString(Str); |
| 7933 | return *this; |
| 7934 | } |
| 7935 | |
| 7936 | Logger &cxindex::Logger::operator<<(const llvm::format_object_base &Fmt) { |
| 7937 | LogOS << Fmt; |
| 7938 | return *this; |
| 7939 | } |
| 7940 | |
Chandler Carruth | 37ad258 | 2014-06-27 15:14:39 +0000 | [diff] [blame] | 7941 | static llvm::ManagedStatic<llvm::sys::Mutex> LoggingMutex; |
| 7942 | |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7943 | cxindex::Logger::~Logger() { |
Chandler Carruth | 37ad258 | 2014-06-27 15:14:39 +0000 | [diff] [blame] | 7944 | llvm::sys::ScopedLock L(*LoggingMutex); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7945 | |
| 7946 | static llvm::TimeRecord sBeginTR = llvm::TimeRecord::getCurrentTime(); |
| 7947 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 7948 | raw_ostream &OS = llvm::errs(); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7949 | OS << "[libclang:" << Name << ':'; |
| 7950 | |
Alp Toker | 1a86ad2 | 2014-07-06 06:24:00 +0000 | [diff] [blame] | 7951 | #ifdef USE_DARWIN_THREADS |
| 7952 | // TODO: Portability. |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7953 | mach_port_t tid = pthread_mach_thread_np(pthread_self()); |
| 7954 | OS << tid << ':'; |
| 7955 | #endif |
| 7956 | |
| 7957 | llvm::TimeRecord TR = llvm::TimeRecord::getCurrentTime(); |
| 7958 | OS << llvm::format("%7.4f] ", TR.getWallTime() - sBeginTR.getWallTime()); |
Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 7959 | OS << Msg << '\n'; |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7960 | |
| 7961 | if (Trace) { |
Zachary Turner | 1fe2a8d | 2015-03-05 19:15:09 +0000 | [diff] [blame] | 7962 | llvm::sys::PrintStackTrace(OS); |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 7963 | OS << "--------------------------------------------------\n"; |
| 7964 | } |
| 7965 | } |
Benjamin Kramer | c1ffdab | 2016-03-03 08:58:18 +0000 | [diff] [blame] | 7966 | |
| 7967 | #ifdef CLANG_TOOL_EXTRA_BUILD |
| 7968 | // This anchor is used to force the linker to link the clang-tidy plugin. |
| 7969 | extern volatile int ClangTidyPluginAnchorSource; |
| 7970 | static int LLVM_ATTRIBUTE_UNUSED ClangTidyPluginAnchorDestination = |
| 7971 | ClangTidyPluginAnchorSource; |
| 7972 | #endif |