Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1 | //===- CIndexHigh.cpp - Higher level API functions ------------------------===// |
| 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 | #include "IndexingContext.h" |
Argyrios Kyrtzidis | f2d99b0 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 11 | #include "CIndexDiagnostic.h" |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 12 | #include "CIndexer.h" |
Chandler Carruth | 4b41745 | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 13 | #include "CLog.h" |
Chandler Carruth | cc0694c | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 14 | #include "CXCursor.h" |
| 15 | #include "CXSourceLocation.h" |
| 16 | #include "CXString.h" |
| 17 | #include "CXTranslationUnit.h" |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTConsumer.h" |
| 19 | #include "clang/AST/DeclVisitor.h" |
Chandler Carruth | cc0694c | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/ASTUnit.h" |
| 21 | #include "clang/Frontend/CompilerInstance.h" |
| 22 | #include "clang/Frontend/CompilerInvocation.h" |
| 23 | #include "clang/Frontend/FrontendAction.h" |
| 24 | #include "clang/Frontend/Utils.h" |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 25 | #include "clang/Lex/HeaderSearch.h" |
Chandler Carruth | 44eb4f6 | 2013-01-02 10:28:36 +0000 | [diff] [blame] | 26 | #include "clang/Lex/PPCallbacks.h" |
| 27 | #include "clang/Lex/PPConditionalDirectiveRecord.h" |
| 28 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | cc0694c | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 29 | #include "clang/Sema/SemaConsumer.h" |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CrashRecoveryContext.h" |
Chandler Carruth | cc0694c | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MemoryBuffer.h" |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Mutex.h" |
| 33 | #include "llvm/Support/MutexGuard.h" |
Dmitri Gribenko | ab458a1 | 2013-08-19 16:14:33 +0000 | [diff] [blame] | 34 | #include <cstdio> |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace clang; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 37 | using namespace cxtu; |
| 38 | using namespace cxindex; |
| 39 | |
Argyrios Kyrtzidis | f2d99b0 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 40 | static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx); |
| 41 | |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 42 | namespace { |
| 43 | |
| 44 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 45 | // Skip Parsed Bodies |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | |
| 48 | #ifdef LLVM_ON_WIN32 |
| 49 | |
| 50 | // FIXME: On windows it is disabled since current implementation depends on |
| 51 | // file inodes. |
| 52 | |
| 53 | class SessionSkipBodyData { }; |
| 54 | |
| 55 | class TUSkipBodyControl { |
| 56 | public: |
| 57 | TUSkipBodyControl(SessionSkipBodyData &sessionData, |
Evgeniy Stepanov | 8660623 | 2013-01-31 14:27:38 +0000 | [diff] [blame] | 58 | PPConditionalDirectiveRecord &ppRec, |
| 59 | Preprocessor &pp) { } |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 60 | bool isParsed(SourceLocation Loc, FileID FID, const FileEntry *FE) { |
| 61 | return false; |
| 62 | } |
| 63 | void finished() { } |
| 64 | }; |
| 65 | |
| 66 | #else |
| 67 | |
| 68 | /// \brief A "region" in source code identified by the file/offset of the |
| 69 | /// preprocessor conditional directive that it belongs to. |
| 70 | /// Multiple, non-consecutive ranges can be parts of the same region. |
| 71 | /// |
| 72 | /// As an example of different regions separated by preprocessor directives: |
| 73 | /// |
| 74 | /// \code |
| 75 | /// #1 |
| 76 | /// #ifdef BLAH |
| 77 | /// #2 |
| 78 | /// #ifdef CAKE |
| 79 | /// #3 |
| 80 | /// #endif |
| 81 | /// #2 |
| 82 | /// #endif |
| 83 | /// #1 |
| 84 | /// \endcode |
| 85 | /// |
| 86 | /// There are 3 regions, with non-consecutive parts: |
| 87 | /// #1 is identified as the beginning of the file |
| 88 | /// #2 is identified as the location of "#ifdef BLAH" |
| 89 | /// #3 is identified as the location of "#ifdef CAKE" |
| 90 | /// |
| 91 | class PPRegion { |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 92 | llvm::sys::fs::UniqueID UniqueID; |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 93 | time_t ModTime; |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 94 | unsigned Offset; |
| 95 | public: |
Alexey Samsonov | c04b5cd | 2013-08-02 09:06:39 +0000 | [diff] [blame] | 96 | PPRegion() : UniqueID(0, 0), ModTime(), Offset() {} |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 97 | PPRegion(llvm::sys::fs::UniqueID UniqueID, unsigned offset, time_t modTime) |
| 98 | : UniqueID(UniqueID), ModTime(modTime), Offset(offset) {} |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 99 | |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 100 | const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; } |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 101 | unsigned getOffset() const { return Offset; } |
| 102 | time_t getModTime() const { return ModTime; } |
| 103 | |
| 104 | bool isInvalid() const { return *this == PPRegion(); } |
| 105 | |
| 106 | friend bool operator==(const PPRegion &lhs, const PPRegion &rhs) { |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 107 | return lhs.UniqueID == rhs.UniqueID && lhs.Offset == rhs.Offset && |
| 108 | lhs.ModTime == rhs.ModTime; |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 109 | } |
| 110 | }; |
| 111 | |
| 112 | typedef llvm::DenseSet<PPRegion> PPRegionSetTy; |
| 113 | |
| 114 | } // end anonymous namespace |
| 115 | |
| 116 | namespace llvm { |
| 117 | template <> struct isPodLike<PPRegion> { |
| 118 | static const bool value = true; |
| 119 | }; |
| 120 | |
| 121 | template <> |
| 122 | struct DenseMapInfo<PPRegion> { |
| 123 | static inline PPRegion getEmptyKey() { |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 124 | return PPRegion(llvm::sys::fs::UniqueID(0, 0), unsigned(-1), 0); |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 125 | } |
| 126 | static inline PPRegion getTombstoneKey() { |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 127 | return PPRegion(llvm::sys::fs::UniqueID(0, 0), unsigned(-2), 0); |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static unsigned getHashValue(const PPRegion &S) { |
| 131 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 132 | const llvm::sys::fs::UniqueID &UniqueID = S.getUniqueID(); |
| 133 | ID.AddInteger(UniqueID.getFile()); |
| 134 | ID.AddInteger(UniqueID.getDevice()); |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 135 | ID.AddInteger(S.getOffset()); |
| 136 | ID.AddInteger(S.getModTime()); |
| 137 | return ID.ComputeHash(); |
| 138 | } |
| 139 | |
| 140 | static bool isEqual(const PPRegion &LHS, const PPRegion &RHS) { |
| 141 | return LHS == RHS; |
| 142 | } |
| 143 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 144 | } |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 145 | |
| 146 | namespace { |
| 147 | |
| 148 | class SessionSkipBodyData { |
| 149 | llvm::sys::Mutex Mux; |
| 150 | PPRegionSetTy ParsedRegions; |
| 151 | |
| 152 | public: |
| 153 | SessionSkipBodyData() : Mux(/*recursive=*/false) {} |
| 154 | ~SessionSkipBodyData() { |
| 155 | //llvm::errs() << "RegionData: " << Skipped.size() << " - " << Skipped.getMemorySize() << "\n"; |
| 156 | } |
| 157 | |
| 158 | void copyTo(PPRegionSetTy &Set) { |
| 159 | llvm::MutexGuard MG(Mux); |
| 160 | Set = ParsedRegions; |
| 161 | } |
| 162 | |
| 163 | void update(ArrayRef<PPRegion> Regions) { |
| 164 | llvm::MutexGuard MG(Mux); |
| 165 | ParsedRegions.insert(Regions.begin(), Regions.end()); |
| 166 | } |
| 167 | }; |
| 168 | |
| 169 | class TUSkipBodyControl { |
| 170 | SessionSkipBodyData &SessionData; |
| 171 | PPConditionalDirectiveRecord &PPRec; |
| 172 | Preprocessor &PP; |
| 173 | |
| 174 | PPRegionSetTy ParsedRegions; |
| 175 | SmallVector<PPRegion, 32> NewParsedRegions; |
| 176 | PPRegion LastRegion; |
| 177 | bool LastIsParsed; |
| 178 | |
| 179 | public: |
| 180 | TUSkipBodyControl(SessionSkipBodyData &sessionData, |
| 181 | PPConditionalDirectiveRecord &ppRec, |
| 182 | Preprocessor &pp) |
| 183 | : SessionData(sessionData), PPRec(ppRec), PP(pp) { |
| 184 | SessionData.copyTo(ParsedRegions); |
| 185 | } |
| 186 | |
| 187 | bool isParsed(SourceLocation Loc, FileID FID, const FileEntry *FE) { |
| 188 | PPRegion region = getRegion(Loc, FID, FE); |
| 189 | if (region.isInvalid()) |
| 190 | return false; |
| 191 | |
| 192 | // Check common case, consecutive functions in the same region. |
| 193 | if (LastRegion == region) |
| 194 | return LastIsParsed; |
| 195 | |
| 196 | LastRegion = region; |
| 197 | LastIsParsed = ParsedRegions.count(region); |
| 198 | if (!LastIsParsed) |
| 199 | NewParsedRegions.push_back(region); |
| 200 | return LastIsParsed; |
| 201 | } |
| 202 | |
| 203 | void finished() { |
| 204 | SessionData.update(NewParsedRegions); |
| 205 | } |
| 206 | |
| 207 | private: |
| 208 | PPRegion getRegion(SourceLocation Loc, FileID FID, const FileEntry *FE) { |
| 209 | SourceLocation RegionLoc = PPRec.findConditionalDirectiveRegionLoc(Loc); |
| 210 | if (RegionLoc.isInvalid()) { |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 211 | if (isParsedOnceInclude(FE)) { |
| 212 | const llvm::sys::fs::UniqueID &ID = FE->getUniqueID(); |
| 213 | return PPRegion(ID, 0, FE->getModificationTime()); |
| 214 | } |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 215 | return PPRegion(); |
| 216 | } |
| 217 | |
| 218 | const SourceManager &SM = PPRec.getSourceManager(); |
| 219 | assert(RegionLoc.isFileID()); |
| 220 | FileID RegionFID; |
| 221 | unsigned RegionOffset; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 222 | std::tie(RegionFID, RegionOffset) = SM.getDecomposedLoc(RegionLoc); |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 223 | |
| 224 | if (RegionFID != FID) { |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 225 | if (isParsedOnceInclude(FE)) { |
| 226 | const llvm::sys::fs::UniqueID &ID = FE->getUniqueID(); |
| 227 | return PPRegion(ID, 0, FE->getModificationTime()); |
| 228 | } |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 229 | return PPRegion(); |
| 230 | } |
| 231 | |
Rafael Espindola | f8f91b8 | 2013-08-01 21:42:11 +0000 | [diff] [blame] | 232 | const llvm::sys::fs::UniqueID &ID = FE->getUniqueID(); |
| 233 | return PPRegion(ID, RegionOffset, FE->getModificationTime()); |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | bool isParsedOnceInclude(const FileEntry *FE) { |
| 237 | return PP.getHeaderSearchInfo().isFileMultipleIncludeGuarded(FE); |
| 238 | } |
| 239 | }; |
| 240 | |
| 241 | #endif |
| 242 | |
| 243 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 244 | // IndexPPCallbacks |
| 245 | //===----------------------------------------------------------------------===// |
| 246 | |
| 247 | class IndexPPCallbacks : public PPCallbacks { |
| 248 | Preprocessor &PP; |
| 249 | IndexingContext &IndexCtx; |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 250 | bool IsMainFileEntered; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 251 | |
| 252 | public: |
| 253 | IndexPPCallbacks(Preprocessor &PP, IndexingContext &indexCtx) |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 254 | : PP(PP), IndexCtx(indexCtx), IsMainFileEntered(false) { } |
| 255 | |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 256 | void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 257 | SrcMgr::CharacteristicKind FileType, FileID PrevFID) override { |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 258 | if (IsMainFileEntered) |
| 259 | return; |
| 260 | |
| 261 | SourceManager &SM = PP.getSourceManager(); |
| 262 | SourceLocation MainFileLoc = SM.getLocForStartOfFile(SM.getMainFileID()); |
| 263 | |
| 264 | if (Loc == MainFileLoc && Reason == PPCallbacks::EnterFile) { |
| 265 | IsMainFileEntered = true; |
| 266 | IndexCtx.enteredMainFile(SM.getFileEntryForID(SM.getMainFileID())); |
| 267 | } |
| 268 | } |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 269 | |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 270 | void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, |
| 271 | StringRef FileName, bool IsAngled, |
| 272 | CharSourceRange FilenameRange, const FileEntry *File, |
| 273 | StringRef SearchPath, StringRef RelativePath, |
| 274 | const Module *Imported) override { |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 275 | bool isImport = (IncludeTok.is(tok::identifier) && |
| 276 | IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import); |
Argyrios Kyrtzidis | 5e2ec48 | 2012-10-18 00:17:05 +0000 | [diff] [blame] | 277 | IndexCtx.ppIncludedFile(HashLoc, FileName, File, isImport, IsAngled, |
| 278 | Imported); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /// MacroDefined - This hook is called whenever a macro definition is seen. |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 282 | void MacroDefined(const Token &Id, const MacroDirective *MD) override {} |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 283 | |
| 284 | /// MacroUndefined - This hook is called whenever a macro #undef is seen. |
| 285 | /// MI is released immediately following this callback. |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 286 | void MacroUndefined(const Token &MacroNameTok, |
Richard Smith | 36bd40d | 2015-05-04 03:15:40 +0000 | [diff] [blame] | 287 | const MacroDefinition &MD) override {} |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 288 | |
| 289 | /// MacroExpands - This is called by when a macro invocation is found. |
Richard Smith | 36bd40d | 2015-05-04 03:15:40 +0000 | [diff] [blame] | 290 | void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 291 | SourceRange Range, const MacroArgs *Args) override {} |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 292 | |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 293 | /// SourceRangeSkipped - This hook is called when a source range is skipped. |
| 294 | /// \param Range The SourceRange that was skipped. The range begins at the |
| 295 | /// #if/#else directive and ends after the #endif/#else directive. |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 296 | void SourceRangeSkipped(SourceRange Range) override {} |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | //===----------------------------------------------------------------------===// |
| 300 | // IndexingConsumer |
| 301 | //===----------------------------------------------------------------------===// |
| 302 | |
| 303 | class IndexingConsumer : public ASTConsumer { |
| 304 | IndexingContext &IndexCtx; |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 305 | TUSkipBodyControl *SKCtrl; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 306 | |
| 307 | public: |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 308 | IndexingConsumer(IndexingContext &indexCtx, TUSkipBodyControl *skCtrl) |
| 309 | : IndexCtx(indexCtx), SKCtrl(skCtrl) { } |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 310 | |
| 311 | // ASTConsumer Implementation |
| 312 | |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 313 | void Initialize(ASTContext &Context) override { |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 314 | IndexCtx.setASTContext(Context); |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 315 | IndexCtx.startedTranslationUnit(); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 318 | void HandleTranslationUnit(ASTContext &Ctx) override { |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 319 | if (SKCtrl) |
| 320 | SKCtrl->finished(); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 323 | bool HandleTopLevelDecl(DeclGroupRef DG) override { |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 324 | IndexCtx.indexDeclGroupRef(DG); |
Argyrios Kyrtzidis | 841dd88 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 325 | return !IndexCtx.shouldAbort(); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /// \brief Handle the specified top-level declaration that occurred inside |
| 329 | /// and ObjC container. |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 330 | void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override { |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 331 | // They will be handled after the interface is seen first. |
| 332 | IndexCtx.addTUDeclInObjCContainer(D); |
| 333 | } |
| 334 | |
| 335 | /// \brief This is called by the AST reader when deserializing things. |
| 336 | /// The default implementation forwards to HandleTopLevelDecl but we don't |
| 337 | /// care about them when indexing, so have an empty definition. |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 338 | void HandleInterestingDecl(DeclGroupRef D) override {} |
Argyrios Kyrtzidis | e5dc5b3 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 339 | |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 340 | void HandleTagDeclDefinition(TagDecl *D) override { |
Argyrios Kyrtzidis | 7e74795 | 2012-02-14 22:23:11 +0000 | [diff] [blame] | 341 | if (!IndexCtx.shouldIndexImplicitTemplateInsts()) |
| 342 | return; |
| 343 | |
Argyrios Kyrtzidis | e5dc5b3 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 344 | if (IndexCtx.isTemplateImplicitInstantiation(D)) |
| 345 | IndexCtx.indexDecl(D); |
| 346 | } |
| 347 | |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 348 | void HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) override { |
Argyrios Kyrtzidis | 7e74795 | 2012-02-14 22:23:11 +0000 | [diff] [blame] | 349 | if (!IndexCtx.shouldIndexImplicitTemplateInsts()) |
| 350 | return; |
| 351 | |
Argyrios Kyrtzidis | e5dc5b3 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 352 | IndexCtx.indexDecl(D); |
| 353 | } |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 354 | |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 355 | bool shouldSkipFunctionBody(Decl *D) override { |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 356 | if (!SKCtrl) { |
| 357 | // Always skip bodies. |
| 358 | return true; |
| 359 | } |
| 360 | |
| 361 | const SourceManager &SM = IndexCtx.getASTContext().getSourceManager(); |
| 362 | SourceLocation Loc = D->getLocation(); |
| 363 | if (Loc.isMacroID()) |
| 364 | return false; |
| 365 | if (SM.isInSystemHeader(Loc)) |
| 366 | return true; // always skip bodies from system headers. |
| 367 | |
| 368 | FileID FID; |
| 369 | unsigned Offset; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 370 | std::tie(FID, Offset) = SM.getDecomposedLoc(Loc); |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 371 | // Don't skip bodies from main files; this may be revisited. |
| 372 | if (SM.getMainFileID() == FID) |
| 373 | return false; |
| 374 | const FileEntry *FE = SM.getFileEntryForID(FID); |
| 375 | if (!FE) |
| 376 | return false; |
| 377 | |
| 378 | return SKCtrl->isParsed(Loc, FID, FE); |
| 379 | } |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | effdbf5 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 383 | // CaptureDiagnosticConsumer |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 384 | //===----------------------------------------------------------------------===// |
| 385 | |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 386 | class CaptureDiagnosticConsumer : public DiagnosticConsumer { |
| 387 | SmallVector<StoredDiagnostic, 4> Errors; |
| 388 | public: |
| 389 | |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 390 | void HandleDiagnostic(DiagnosticsEngine::Level level, |
| 391 | const Diagnostic &Info) override { |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 392 | if (level >= DiagnosticsEngine::Error) |
| 393 | Errors.push_back(StoredDiagnostic(level, Info)); |
| 394 | } |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 395 | }; |
| 396 | |
| 397 | //===----------------------------------------------------------------------===// |
| 398 | // IndexingFrontendAction |
| 399 | //===----------------------------------------------------------------------===// |
| 400 | |
| 401 | class IndexingFrontendAction : public ASTFrontendAction { |
| 402 | IndexingContext IndexCtx; |
Argyrios Kyrtzidis | f2d99b0 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 403 | CXTranslationUnit CXTU; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 404 | |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 405 | SessionSkipBodyData *SKData; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 406 | std::unique_ptr<TUSkipBodyControl> SKCtrl; |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 407 | |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 408 | public: |
| 409 | IndexingFrontendAction(CXClientData clientData, |
| 410 | IndexerCallbacks &indexCallbacks, |
| 411 | unsigned indexOptions, |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 412 | CXTranslationUnit cxTU, |
| 413 | SessionSkipBodyData *skData) |
Argyrios Kyrtzidis | f2d99b0 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 414 | : IndexCtx(clientData, indexCallbacks, indexOptions, cxTU), |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 415 | CXTU(cxTU), SKData(skData) { } |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 416 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 417 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
| 418 | StringRef InFile) override { |
Argyrios Kyrtzidis | f484b13 | 2012-10-03 21:05:51 +0000 | [diff] [blame] | 419 | PreprocessorOptions &PPOpts = CI.getPreprocessorOpts(); |
| 420 | |
Argyrios Kyrtzidis | f484b13 | 2012-10-03 21:05:51 +0000 | [diff] [blame] | 421 | if (!PPOpts.ImplicitPCHInclude.empty()) { |
| 422 | IndexCtx.importedPCH( |
| 423 | CI.getFileManager().getFile(PPOpts.ImplicitPCHInclude)); |
| 424 | } |
Argyrios Kyrtzidis | b36ee5c | 2012-10-02 16:10:41 +0000 | [diff] [blame] | 425 | |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 426 | IndexCtx.setASTContext(CI.getASTContext()); |
| 427 | Preprocessor &PP = CI.getPreprocessor(); |
Craig Topper | b8a7053 | 2014-09-10 04:53:53 +0000 | [diff] [blame] | 428 | PP.addPPCallbacks(llvm::make_unique<IndexPPCallbacks>(PP, IndexCtx)); |
Argyrios Kyrtzidis | da6e054 | 2012-01-17 18:48:07 +0000 | [diff] [blame] | 429 | IndexCtx.setPreprocessor(PP); |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 430 | |
| 431 | if (SKData) { |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 432 | auto *PPRec = new PPConditionalDirectiveRecord(PP.getSourceManager()); |
Craig Topper | b8a7053 | 2014-09-10 04:53:53 +0000 | [diff] [blame] | 433 | PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(PPRec)); |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 434 | SKCtrl = llvm::make_unique<TUSkipBodyControl>(*SKData, *PPRec, PP); |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 435 | } |
| 436 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 437 | return llvm::make_unique<IndexingConsumer>(IndexCtx, SKCtrl.get()); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 440 | void EndSourceFileAction() override { |
Argyrios Kyrtzidis | f2d99b0 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 441 | indexDiagnostics(CXTU, IndexCtx); |
| 442 | } |
| 443 | |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 444 | TranslationUnitKind getTranslationUnitKind() override { |
Argyrios Kyrtzidis | 7e74795 | 2012-02-14 22:23:11 +0000 | [diff] [blame] | 445 | if (IndexCtx.shouldIndexImplicitTemplateInsts()) |
| 446 | return TU_Complete; |
| 447 | else |
| 448 | return TU_Prefix; |
| 449 | } |
Craig Topper | 3683556 | 2014-03-15 07:47:46 +0000 | [diff] [blame] | 450 | bool hasCodeCompletionSupport() const override { return false; } |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 451 | }; |
| 452 | |
| 453 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 454 | // clang_indexSourceFileUnit Implementation |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 455 | //===----------------------------------------------------------------------===// |
| 456 | |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 457 | struct IndexSessionData { |
| 458 | CXIndex CIdx; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 459 | std::unique_ptr<SessionSkipBodyData> SkipBodyData; |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 460 | |
| 461 | explicit IndexSessionData(CXIndex cIdx) |
| 462 | : CIdx(cIdx), SkipBodyData(new SessionSkipBodyData) {} |
| 463 | }; |
| 464 | |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 465 | } // anonymous namespace |
| 466 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 467 | static CXErrorCode clang_indexSourceFile_Impl( |
| 468 | CXIndexAction cxIdxAction, CXClientData client_data, |
| 469 | IndexerCallbacks *client_index_callbacks, unsigned index_callbacks_size, |
| 470 | unsigned index_options, const char *source_filename, |
| 471 | const char *const *command_line_args, int num_command_line_args, |
| 472 | ArrayRef<CXUnsavedFile> unsaved_files, CXTranslationUnit *out_TU, |
| 473 | unsigned TU_options) { |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 474 | if (out_TU) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 475 | *out_TU = nullptr; |
| 476 | bool requestedToGetTU = (out_TU != nullptr); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 477 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 478 | if (!cxIdxAction) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 479 | return CXError_InvalidArguments; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 480 | } |
| 481 | if (!client_index_callbacks || index_callbacks_size == 0) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 482 | return CXError_InvalidArguments; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 483 | } |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 484 | |
| 485 | IndexerCallbacks CB; |
| 486 | memset(&CB, 0, sizeof(CB)); |
| 487 | unsigned ClientCBSize = index_callbacks_size < sizeof(CB) |
| 488 | ? index_callbacks_size : sizeof(CB); |
| 489 | memcpy(&CB, client_index_callbacks, ClientCBSize); |
| 490 | |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 491 | IndexSessionData *IdxSession = static_cast<IndexSessionData *>(cxIdxAction); |
| 492 | CIndexer *CXXIdx = static_cast<CIndexer *>(IdxSession->CIdx); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 493 | |
Argyrios Kyrtzidis | 7317a5c | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 494 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
Argyrios Kyrtzidis | 58d5f3f | 2012-03-28 02:49:54 +0000 | [diff] [blame] | 495 | setThreadBackgroundPriority(); |
Argyrios Kyrtzidis | 7317a5c | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 496 | |
Argyrios Kyrtzidis | 828f4d4 | 2013-03-29 21:51:44 +0000 | [diff] [blame] | 497 | bool CaptureDiagnostics = !Logger::isLoggingEnabled(); |
| 498 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 499 | CaptureDiagnosticConsumer *CaptureDiag = nullptr; |
Argyrios Kyrtzidis | 828f4d4 | 2013-03-29 21:51:44 +0000 | [diff] [blame] | 500 | if (CaptureDiagnostics) |
| 501 | CaptureDiag = new CaptureDiagnosticConsumer(); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 502 | |
| 503 | // Configure the diagnostics. |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 504 | IntrusiveRefCntPtr<DiagnosticsEngine> |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 505 | Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions, |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 506 | CaptureDiag, |
Douglas Gregor | 30071cea | 2013-05-03 23:07:45 +0000 | [diff] [blame] | 507 | /*ShouldOwnClient=*/true)); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 508 | |
| 509 | // Recover resources if we crash before exiting this function. |
| 510 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 511 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 512 | DiagCleanup(Diags.get()); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 513 | |
| 514 | std::unique_ptr<std::vector<const char *>> Args( |
| 515 | new std::vector<const char *>()); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 516 | |
| 517 | // Recover resources if we crash before exiting this method. |
| 518 | llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> > |
| 519 | ArgsCleanup(Args.get()); |
| 520 | |
| 521 | Args->insert(Args->end(), command_line_args, |
| 522 | command_line_args + num_command_line_args); |
| 523 | |
| 524 | // The 'source_filename' argument is optional. If the caller does not |
| 525 | // specify it then it is assumed that the source file is specified |
| 526 | // in the actual argument list. |
| 527 | // Put the source file after command_line_args otherwise if '-x' flag is |
| 528 | // present it will be unused. |
| 529 | if (source_filename) |
| 530 | Args->push_back(source_filename); |
| 531 | |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 532 | IntrusiveRefCntPtr<CompilerInvocation> |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 533 | CInvok(createInvocationFromCommandLine(*Args, Diags)); |
| 534 | |
| 535 | if (!CInvok) |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 536 | return CXError_Failure; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 537 | |
| 538 | // Recover resources if we crash before exiting this function. |
| 539 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation, |
| 540 | llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> > |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 541 | CInvokCleanup(CInvok.get()); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 542 | |
| 543 | if (CInvok->getFrontendOpts().Inputs.empty()) |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 544 | return CXError_Failure; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 545 | |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 546 | typedef SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 8> MemBufferOwner; |
| 547 | std::unique_ptr<MemBufferOwner> BufOwner(new MemBufferOwner); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 548 | |
| 549 | // Recover resources if we crash before exiting this method. |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 550 | llvm::CrashRecoveryContextCleanupRegistrar<MemBufferOwner> BufOwnerCleanup( |
| 551 | BufOwner.get()); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 552 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 553 | for (auto &UF : unsaved_files) { |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 554 | std::unique_ptr<llvm::MemoryBuffer> MB = |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 555 | llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 556 | CInvok->getPreprocessorOpts().addRemappedFile(UF.Filename, MB.get()); |
| 557 | BufOwner->push_back(std::move(MB)); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | // Since libclang is primarily used by batch tools dealing with |
| 561 | // (often very broken) source code, where spell-checking can have a |
| 562 | // significant negative impact on performance (particularly when |
| 563 | // precompiled headers are involved), we disable it. |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 564 | CInvok->getLangOpts()->SpellChecking = false; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 565 | |
Argyrios Kyrtzidis | 6c9ed7d | 2012-03-27 21:38:03 +0000 | [diff] [blame] | 566 | if (index_options & CXIndexOpt_SuppressWarnings) |
| 567 | CInvok->getDiagnosticOpts().IgnoreWarnings = true; |
| 568 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 569 | ASTUnit *Unit = ASTUnit::create(CInvok.get(), Diags, CaptureDiagnostics, |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 570 | /*UserFilesAreVolatile=*/true); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 571 | if (!Unit) |
| 572 | return CXError_InvalidArguments; |
Ben Langmuir | 8832c06 | 2014-04-15 18:16:25 +0000 | [diff] [blame] | 573 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 574 | std::unique_ptr<CXTUOwner> CXTU( |
| 575 | new CXTUOwner(MakeCXTranslationUnit(CXXIdx, Unit))); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 576 | |
| 577 | // Recover resources if we crash before exiting this method. |
| 578 | llvm::CrashRecoveryContextCleanupRegistrar<CXTUOwner> |
| 579 | CXTUCleanup(CXTU.get()); |
| 580 | |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 581 | // Enable the skip-parsed-bodies optimization only for C++; this may be |
| 582 | // revisited. |
| 583 | bool SkipBodies = (index_options & CXIndexOpt_SkipParsedBodiesInSession) && |
| 584 | CInvok->getLangOpts()->CPlusPlus; |
| 585 | if (SkipBodies) |
| 586 | CInvok->getFrontendOpts().SkipFunctionBodies = true; |
| 587 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 588 | std::unique_ptr<IndexingFrontendAction> IndexAction; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 589 | IndexAction.reset(new IndexingFrontendAction(client_data, CB, |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 590 | index_options, CXTU->getTU(), |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 591 | SkipBodies ? IdxSession->SkipBodyData.get() : nullptr)); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 592 | |
| 593 | // Recover resources if we crash before exiting this method. |
| 594 | llvm::CrashRecoveryContextCleanupRegistrar<IndexingFrontendAction> |
| 595 | IndexActionCleanup(IndexAction.get()); |
| 596 | |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 597 | bool Persistent = requestedToGetTU; |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 598 | bool OnlyLocalDecls = false; |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 599 | bool PrecompilePreamble = false; |
| 600 | bool CacheCodeCompletionResults = false; |
| 601 | PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts(); |
Argyrios Kyrtzidis | 4a280ff | 2012-03-07 01:51:17 +0000 | [diff] [blame] | 602 | PPOpts.AllowPCHWithCompilerErrors = true; |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 603 | |
| 604 | if (requestedToGetTU) { |
| 605 | OnlyLocalDecls = CXXIdx->getOnlyLocalDecls(); |
| 606 | PrecompilePreamble = TU_options & CXTranslationUnit_PrecompiledPreamble; |
| 607 | // FIXME: Add a flag for modules. |
| 608 | CacheCodeCompletionResults |
| 609 | = TU_options & CXTranslationUnit_CacheCompletionResults; |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Argyrios Kyrtzidis | 6a80e75 | 2012-10-06 01:18:35 +0000 | [diff] [blame] | 612 | if (TU_options & CXTranslationUnit_DetailedPreprocessingRecord) { |
| 613 | PPOpts.DetailedRecord = true; |
| 614 | } |
Argyrios Kyrtzidis | b36ee5c | 2012-10-02 16:10:41 +0000 | [diff] [blame] | 615 | |
Argyrios Kyrtzidis | 15f6b8e | 2012-10-10 02:12:50 +0000 | [diff] [blame] | 616 | if (!requestedToGetTU && !CInvok->getLangOpts()->Modules) |
| 617 | PPOpts.DetailedRecord = false; |
| 618 | |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 619 | DiagnosticErrorTrap DiagTrap(*Diags); |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 620 | bool Success = ASTUnit::LoadFromCompilerInvocationAction( |
| 621 | CInvok.get(), CXXIdx->getPCHContainerOperations(), Diags, |
| 622 | IndexAction.get(), Unit, Persistent, CXXIdx->getClangResourcesPath(), |
| 623 | OnlyLocalDecls, CaptureDiagnostics, PrecompilePreamble, |
| 624 | CacheCodeCompletionResults, |
| 625 | /*IncludeBriefCommentsInCodeCompletion=*/false, |
| 626 | /*UserFilesAreVolatile=*/true); |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 627 | if (DiagTrap.hasErrorOccurred() && CXXIdx->getDisplayDiagnostics()) |
| 628 | printDiagsToStderr(Unit); |
| 629 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 630 | if (isASTReadError(Unit)) |
| 631 | return CXError_ASTReadError; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 632 | |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 633 | if (!Success) |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 634 | return CXError_Failure; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 635 | |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 636 | if (out_TU) |
| 637 | *out_TU = CXTU->takeTU(); |
| 638 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 639 | return CXError_Success; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 643 | // clang_indexTranslationUnit Implementation |
| 644 | //===----------------------------------------------------------------------===// |
| 645 | |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 646 | static void indexPreprocessingRecord(ASTUnit &Unit, IndexingContext &IdxCtx) { |
| 647 | Preprocessor &PP = Unit.getPreprocessor(); |
| 648 | if (!PP.getPreprocessingRecord()) |
| 649 | return; |
| 650 | |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 651 | // FIXME: Only deserialize inclusion directives. |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 652 | |
Argyrios Kyrtzidis | e445c72 | 2012-10-10 02:12:47 +0000 | [diff] [blame] | 653 | bool isModuleFile = Unit.isModuleFile(); |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 654 | for (PreprocessedEntity *PPE : Unit.getLocalPreprocessingEntities()) { |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 655 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) { |
Argyrios Kyrtzidis | 5e2ec48 | 2012-10-18 00:17:05 +0000 | [diff] [blame] | 656 | SourceLocation Loc = ID->getSourceRange().getBegin(); |
| 657 | // Modules have synthetic main files as input, give an invalid location |
| 658 | // if the location points to such a file. |
| 659 | if (isModuleFile && Unit.isInMainFileID(Loc)) |
| 660 | Loc = SourceLocation(); |
| 661 | IdxCtx.ppIncludedFile(Loc, ID->getFileName(), |
| 662 | ID->getFile(), |
| 663 | ID->getKind() == InclusionDirective::Import, |
| 664 | !ID->wasInQuotes(), ID->importedModule()); |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
Argyrios Kyrtzidis | e514b20 | 2012-10-03 01:58:28 +0000 | [diff] [blame] | 669 | static bool topLevelDeclVisitor(void *context, const Decl *D) { |
Argyrios Kyrtzidis | 10e7846 | 2012-10-02 21:09:13 +0000 | [diff] [blame] | 670 | IndexingContext &IdxCtx = *static_cast<IndexingContext*>(context); |
| 671 | IdxCtx.indexTopLevelDecl(D); |
| 672 | if (IdxCtx.shouldAbort()) |
| 673 | return false; |
| 674 | return true; |
| 675 | } |
| 676 | |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 677 | static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IdxCtx) { |
Argyrios Kyrtzidis | e514b20 | 2012-10-03 01:58:28 +0000 | [diff] [blame] | 678 | Unit.visitLocalTopLevelDecls(&IdxCtx, topLevelDeclVisitor); |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx) { |
Argyrios Kyrtzidis | f2d99b0 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 682 | if (!IdxCtx.hasDiagnosticCallback()) |
| 683 | return; |
| 684 | |
| 685 | CXDiagnosticSetImpl *DiagSet = cxdiag::lazyCreateDiags(TU); |
| 686 | IdxCtx.handleDiagnosticSet(DiagSet); |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 689 | static CXErrorCode clang_indexTranslationUnit_Impl( |
| 690 | CXIndexAction idxAction, CXClientData client_data, |
| 691 | IndexerCallbacks *client_index_callbacks, unsigned index_callbacks_size, |
| 692 | unsigned index_options, CXTranslationUnit TU) { |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 693 | // Check arguments. |
Dmitri Gribenko | 852d622 | 2014-02-11 15:02:48 +0000 | [diff] [blame] | 694 | if (isNotUsableTU(TU)) { |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 695 | LOG_BAD_TU(TU); |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 696 | return CXError_InvalidArguments; |
Dmitri Gribenko | 256454f | 2014-02-11 14:34:14 +0000 | [diff] [blame] | 697 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 698 | if (!client_index_callbacks || index_callbacks_size == 0) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 699 | return CXError_InvalidArguments; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 700 | } |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 701 | |
Dmitri Gribenko | 183436e | 2013-01-26 21:49:50 +0000 | [diff] [blame] | 702 | CIndexer *CXXIdx = TU->CIdx; |
Argyrios Kyrtzidis | 7317a5c | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 703 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) |
Argyrios Kyrtzidis | 58d5f3f | 2012-03-28 02:49:54 +0000 | [diff] [blame] | 704 | setThreadBackgroundPriority(); |
Argyrios Kyrtzidis | 7317a5c | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 705 | |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 706 | IndexerCallbacks CB; |
| 707 | memset(&CB, 0, sizeof(CB)); |
| 708 | unsigned ClientCBSize = index_callbacks_size < sizeof(CB) |
| 709 | ? index_callbacks_size : sizeof(CB); |
| 710 | memcpy(&CB, client_index_callbacks, ClientCBSize); |
| 711 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 712 | std::unique_ptr<IndexingContext> IndexCtx; |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 713 | IndexCtx.reset(new IndexingContext(client_data, CB, index_options, TU)); |
| 714 | |
| 715 | // Recover resources if we crash before exiting this method. |
| 716 | llvm::CrashRecoveryContextCleanupRegistrar<IndexingContext> |
| 717 | IndexCtxCleanup(IndexCtx.get()); |
| 718 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 719 | std::unique_ptr<IndexingConsumer> IndexConsumer; |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 720 | IndexConsumer.reset(new IndexingConsumer(*IndexCtx, nullptr)); |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 721 | |
| 722 | // Recover resources if we crash before exiting this method. |
| 723 | llvm::CrashRecoveryContextCleanupRegistrar<IndexingConsumer> |
| 724 | IndexConsumerCleanup(IndexConsumer.get()); |
| 725 | |
Dmitri Gribenko | c22ea1c | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 726 | ASTUnit *Unit = cxtu::getASTUnit(TU); |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 727 | if (!Unit) |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 728 | return CXError_Failure; |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 729 | |
Argyrios Kyrtzidis | 341de09 | 2012-09-25 19:29:50 +0000 | [diff] [blame] | 730 | ASTUnit::ConcurrencyCheck Check(*Unit); |
| 731 | |
Argyrios Kyrtzidis | f484b13 | 2012-10-03 21:05:51 +0000 | [diff] [blame] | 732 | if (const FileEntry *PCHFile = Unit->getPCHFile()) |
| 733 | IndexCtx->importedPCH(PCHFile); |
| 734 | |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 735 | FileManager &FileMgr = Unit->getFileManager(); |
| 736 | |
| 737 | if (Unit->getOriginalSourceFileName().empty()) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 738 | IndexCtx->enteredMainFile(nullptr); |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 739 | else |
| 740 | IndexCtx->enteredMainFile(FileMgr.getFile(Unit->getOriginalSourceFileName())); |
| 741 | |
| 742 | IndexConsumer->Initialize(Unit->getASTContext()); |
| 743 | |
| 744 | indexPreprocessingRecord(*Unit, *IndexCtx); |
| 745 | indexTranslationUnit(*Unit, *IndexCtx); |
| 746 | indexDiagnostics(TU, *IndexCtx); |
| 747 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 748 | return CXError_Success; |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 752 | // libclang public APIs. |
| 753 | //===----------------------------------------------------------------------===// |
| 754 | |
| 755 | extern "C" { |
| 756 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 757 | int clang_index_isEntityObjCContainerKind(CXIdxEntityKind K) { |
| 758 | return CXIdxEntity_ObjCClass <= K && K <= CXIdxEntity_ObjCCategory; |
| 759 | } |
| 760 | |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 761 | const CXIdxObjCContainerDeclInfo * |
| 762 | clang_index_getObjCContainerDeclInfo(const CXIdxDeclInfo *DInfo) { |
| 763 | if (!DInfo) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 764 | return nullptr; |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 765 | |
Argyrios Kyrtzidis | 86acd72 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 766 | const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); |
| 767 | if (const ObjCContainerDeclInfo * |
| 768 | ContInfo = dyn_cast<ObjCContainerDeclInfo>(DI)) |
| 769 | return &ContInfo->ObjCContDeclInfo; |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 770 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 771 | return nullptr; |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 774 | const CXIdxObjCInterfaceDeclInfo * |
| 775 | clang_index_getObjCInterfaceDeclInfo(const CXIdxDeclInfo *DInfo) { |
Argyrios Kyrtzidis | 86acd72 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 776 | if (!DInfo) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 777 | return nullptr; |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 778 | |
Argyrios Kyrtzidis | 86acd72 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 779 | const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); |
| 780 | if (const ObjCInterfaceDeclInfo * |
| 781 | InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI)) |
| 782 | return &InterInfo->ObjCInterDeclInfo; |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 783 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 784 | return nullptr; |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | const CXIdxObjCCategoryDeclInfo * |
| 788 | clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo *DInfo){ |
Argyrios Kyrtzidis | 86acd72 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 789 | if (!DInfo) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 790 | return nullptr; |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 791 | |
Argyrios Kyrtzidis | 86acd72 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 792 | const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); |
| 793 | if (const ObjCCategoryDeclInfo * |
| 794 | CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI)) |
| 795 | return &CatInfo->ObjCCatDeclInfo; |
| 796 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 797 | return nullptr; |
Argyrios Kyrtzidis | 86acd72 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | const CXIdxObjCProtocolRefListInfo * |
| 801 | clang_index_getObjCProtocolRefListInfo(const CXIdxDeclInfo *DInfo) { |
| 802 | if (!DInfo) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 803 | return nullptr; |
Argyrios Kyrtzidis | 86acd72 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 804 | |
| 805 | const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); |
| 806 | |
| 807 | if (const ObjCInterfaceDeclInfo * |
| 808 | InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI)) |
| 809 | return InterInfo->ObjCInterDeclInfo.protocols; |
| 810 | |
| 811 | if (const ObjCProtocolDeclInfo * |
| 812 | ProtInfo = dyn_cast<ObjCProtocolDeclInfo>(DI)) |
| 813 | return &ProtInfo->ObjCProtoRefListInfo; |
| 814 | |
Argyrios Kyrtzidis | 9b9f7a9 | 2011-12-13 18:47:45 +0000 | [diff] [blame] | 815 | if (const ObjCCategoryDeclInfo *CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI)) |
| 816 | return CatInfo->ObjCCatDeclInfo.protocols; |
| 817 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 818 | return nullptr; |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Argyrios Kyrtzidis | 93db292 | 2012-02-28 17:50:33 +0000 | [diff] [blame] | 821 | const CXIdxObjCPropertyDeclInfo * |
| 822 | clang_index_getObjCPropertyDeclInfo(const CXIdxDeclInfo *DInfo) { |
| 823 | if (!DInfo) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 824 | return nullptr; |
Argyrios Kyrtzidis | 93db292 | 2012-02-28 17:50:33 +0000 | [diff] [blame] | 825 | |
| 826 | const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); |
| 827 | if (const ObjCPropertyDeclInfo *PropInfo = dyn_cast<ObjCPropertyDeclInfo>(DI)) |
| 828 | return &PropInfo->ObjCPropDeclInfo; |
| 829 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 830 | return nullptr; |
Argyrios Kyrtzidis | 93db292 | 2012-02-28 17:50:33 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Argyrios Kyrtzidis | effdbf5 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 833 | const CXIdxIBOutletCollectionAttrInfo * |
| 834 | clang_index_getIBOutletCollectionAttrInfo(const CXIdxAttrInfo *AInfo) { |
| 835 | if (!AInfo) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 836 | return nullptr; |
Argyrios Kyrtzidis | effdbf5 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 837 | |
| 838 | const AttrInfo *DI = static_cast<const AttrInfo *>(AInfo); |
| 839 | if (const IBOutletCollectionInfo * |
| 840 | IBInfo = dyn_cast<IBOutletCollectionInfo>(DI)) |
| 841 | return &IBInfo->IBCollInfo; |
| 842 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 843 | return nullptr; |
Argyrios Kyrtzidis | effdbf5 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 846 | const CXIdxCXXClassDeclInfo * |
| 847 | clang_index_getCXXClassDeclInfo(const CXIdxDeclInfo *DInfo) { |
| 848 | if (!DInfo) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 849 | return nullptr; |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 850 | |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 851 | const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo); |
| 852 | if (const CXXClassDeclInfo *ClassInfo = dyn_cast<CXXClassDeclInfo>(DI)) |
| 853 | return &ClassInfo->CXXClassInfo; |
| 854 | |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 855 | return nullptr; |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | CXIdxClientContainer |
| 859 | clang_index_getClientContainer(const CXIdxContainerInfo *info) { |
| 860 | if (!info) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 861 | return nullptr; |
Benjamin Kramer | 7f107ee | 2011-11-29 12:31:20 +0000 | [diff] [blame] | 862 | const ContainerInfo *Container = static_cast<const ContainerInfo *>(info); |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 863 | return Container->IndexCtx->getClientContainerForDC(Container->DC); |
| 864 | } |
| 865 | |
| 866 | void clang_index_setClientContainer(const CXIdxContainerInfo *info, |
| 867 | CXIdxClientContainer client) { |
| 868 | if (!info) |
| 869 | return; |
Benjamin Kramer | 7f107ee | 2011-11-29 12:31:20 +0000 | [diff] [blame] | 870 | const ContainerInfo *Container = static_cast<const ContainerInfo *>(info); |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 871 | Container->IndexCtx->addContainerInMap(Container->DC, client); |
| 872 | } |
| 873 | |
| 874 | CXIdxClientEntity clang_index_getClientEntity(const CXIdxEntityInfo *info) { |
| 875 | if (!info) |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 876 | return nullptr; |
Benjamin Kramer | 7f107ee | 2011-11-29 12:31:20 +0000 | [diff] [blame] | 877 | const EntityInfo *Entity = static_cast<const EntityInfo *>(info); |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 878 | return Entity->IndexCtx->getClientEntity(Entity->Dcl); |
| 879 | } |
| 880 | |
| 881 | void clang_index_setClientEntity(const CXIdxEntityInfo *info, |
| 882 | CXIdxClientEntity client) { |
| 883 | if (!info) |
| 884 | return; |
Benjamin Kramer | 7f107ee | 2011-11-29 12:31:20 +0000 | [diff] [blame] | 885 | const EntityInfo *Entity = static_cast<const EntityInfo *>(info); |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 886 | Entity->IndexCtx->setClientEntity(Entity->Dcl, client); |
| 887 | } |
| 888 | |
| 889 | CXIndexAction clang_IndexAction_create(CXIndex CIdx) { |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 890 | return new IndexSessionData(CIdx); |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | void clang_IndexAction_dispose(CXIndexAction idxAction) { |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 894 | if (idxAction) |
| 895 | delete static_cast<IndexSessionData *>(idxAction); |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | int clang_indexSourceFile(CXIndexAction idxAction, |
| 899 | CXClientData client_data, |
| 900 | IndexerCallbacks *index_callbacks, |
| 901 | unsigned index_callbacks_size, |
| 902 | unsigned index_options, |
| 903 | const char *source_filename, |
| 904 | const char * const *command_line_args, |
| 905 | int num_command_line_args, |
| 906 | struct CXUnsavedFile *unsaved_files, |
| 907 | unsigned num_unsaved_files, |
| 908 | CXTranslationUnit *out_TU, |
| 909 | unsigned TU_options) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 910 | LOG_FUNC_SECTION { |
| 911 | *Log << source_filename << ": "; |
| 912 | for (int i = 0; i != num_command_line_args; ++i) |
| 913 | *Log << command_line_args[i] << " "; |
| 914 | } |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 915 | |
Alp Toker | 9d85b18 | 2014-07-07 01:23:14 +0000 | [diff] [blame] | 916 | if (num_unsaved_files && !unsaved_files) |
| 917 | return CXError_InvalidArguments; |
| 918 | |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 919 | CXErrorCode result = CXError_Failure; |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 920 | auto IndexSourceFileImpl = [=, &result]() { |
| 921 | result = clang_indexSourceFile_Impl( |
| 922 | idxAction, client_data, index_callbacks, index_callbacks_size, |
| 923 | index_options, source_filename, command_line_args, |
| 924 | num_command_line_args, |
| 925 | llvm::makeArrayRef(unsaved_files, num_unsaved_files), out_TU, |
| 926 | TU_options); |
| 927 | }; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 928 | |
Argyrios Kyrtzidis | 9780553 | 2011-10-29 19:32:39 +0000 | [diff] [blame] | 929 | if (getenv("LIBCLANG_NOTHREADS")) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 930 | IndexSourceFileImpl(); |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 931 | return result; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | llvm::CrashRecoveryContext CRC; |
| 935 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 936 | if (!RunSafely(CRC, IndexSourceFileImpl)) { |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 937 | fprintf(stderr, "libclang: crash detected during indexing source file: {\n"); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 938 | fprintf(stderr, " 'source_filename' : '%s'\n", source_filename); |
| 939 | fprintf(stderr, " 'command_line_args' : ["); |
| 940 | for (int i = 0; i != num_command_line_args; ++i) { |
| 941 | if (i) |
| 942 | fprintf(stderr, ", "); |
| 943 | fprintf(stderr, "'%s'", command_line_args[i]); |
| 944 | } |
| 945 | fprintf(stderr, "],\n"); |
| 946 | fprintf(stderr, " 'unsaved_files' : ["); |
| 947 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
| 948 | if (i) |
| 949 | fprintf(stderr, ", "); |
| 950 | fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename, |
| 951 | unsaved_files[i].Length); |
| 952 | } |
| 953 | fprintf(stderr, "],\n"); |
| 954 | fprintf(stderr, " 'options' : %d,\n", TU_options); |
| 955 | fprintf(stderr, "}\n"); |
| 956 | |
| 957 | return 1; |
| 958 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) { |
| 959 | if (out_TU) |
| 960 | PrintLibclangResourceUsage(*out_TU); |
| 961 | } |
Alp Toker | 5c53298 | 2014-07-07 22:42:03 +0000 | [diff] [blame] | 962 | |
| 963 | return result; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 966 | int clang_indexTranslationUnit(CXIndexAction idxAction, |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 967 | CXClientData client_data, |
| 968 | IndexerCallbacks *index_callbacks, |
| 969 | unsigned index_callbacks_size, |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 970 | unsigned index_options, |
| 971 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | ea47435 | 2013-01-10 18:54:52 +0000 | [diff] [blame] | 972 | LOG_FUNC_SECTION { |
| 973 | *Log << TU; |
| 974 | } |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 975 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 976 | CXErrorCode result; |
| 977 | auto IndexTranslationUnitImpl = [=, &result]() { |
| 978 | result = clang_indexTranslationUnit_Impl( |
| 979 | idxAction, client_data, index_callbacks, index_callbacks_size, |
| 980 | index_options, TU); |
| 981 | }; |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 982 | |
| 983 | if (getenv("LIBCLANG_NOTHREADS")) { |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 984 | IndexTranslationUnitImpl(); |
| 985 | return result; |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | llvm::CrashRecoveryContext CRC; |
| 989 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 990 | if (!RunSafely(CRC, IndexTranslationUnitImpl)) { |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 991 | fprintf(stderr, "libclang: crash detected during indexing TU\n"); |
| 992 | |
| 993 | return 1; |
| 994 | } |
| 995 | |
Benjamin Kramer | 11a9cd9 | 2015-07-25 20:55:44 +0000 | [diff] [blame^] | 996 | return result; |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 999 | void clang_indexLoc_getFileLocation(CXIdxLoc location, |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1000 | CXIdxClientFile *indexFile, |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1001 | CXFile *file, |
| 1002 | unsigned *line, |
| 1003 | unsigned *column, |
| 1004 | unsigned *offset) { |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 1005 | if (indexFile) *indexFile = nullptr; |
| 1006 | if (file) *file = nullptr; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1007 | if (line) *line = 0; |
| 1008 | if (column) *column = 0; |
| 1009 | if (offset) *offset = 0; |
| 1010 | |
| 1011 | SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); |
| 1012 | if (!location.ptr_data[0] || Loc.isInvalid()) |
| 1013 | return; |
| 1014 | |
| 1015 | IndexingContext &IndexCtx = |
| 1016 | *static_cast<IndexingContext*>(location.ptr_data[0]); |
| 1017 | IndexCtx.translateLoc(Loc, indexFile, file, line, column, offset); |
| 1018 | } |
| 1019 | |
| 1020 | CXSourceLocation clang_indexLoc_getCXSourceLocation(CXIdxLoc location) { |
| 1021 | SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data); |
| 1022 | if (!location.ptr_data[0] || Loc.isInvalid()) |
| 1023 | return clang_getNullLocation(); |
| 1024 | |
| 1025 | IndexingContext &IndexCtx = |
| 1026 | *static_cast<IndexingContext*>(location.ptr_data[0]); |
| 1027 | return cxloc::translateSourceLocation(IndexCtx.getASTContext(), Loc); |
| 1028 | } |
| 1029 | |
| 1030 | } // end: extern "C" |
| 1031 | |