Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / tools / libclang / Indexing.cpp
blob17d393ef80842583d3156410846b79bd75f9be22
1 //===- Indexing.cpp - Higher level API functions --------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "CIndexDiagnostic.h"
10 #include "CIndexer.h"
11 #include "CLog.h"
12 #include "CXCursor.h"
13 #include "CXIndexDataConsumer.h"
14 #include "CXSourceLocation.h"
15 #include "CXString.h"
16 #include "CXTranslationUnit.h"
17 #include "clang/AST/ASTConsumer.h"
18 #include "clang/Frontend/ASTUnit.h"
19 #include "clang/Frontend/CompilerInstance.h"
20 #include "clang/Frontend/CompilerInvocation.h"
21 #include "clang/Frontend/FrontendAction.h"
22 #include "clang/Frontend/MultiplexConsumer.h"
23 #include "clang/Frontend/Utils.h"
24 #include "clang/Index/IndexingAction.h"
25 #include "clang/Lex/HeaderSearch.h"
26 #include "clang/Lex/PPCallbacks.h"
27 #include "clang/Lex/PPConditionalDirectiveRecord.h"
28 #include "clang/Lex/Preprocessor.h"
29 #include "clang/Lex/PreprocessorOptions.h"
30 #include "llvm/Support/CrashRecoveryContext.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include <cstdio>
33 #include <mutex>
34 #include <utility>
36 using namespace clang;
37 using namespace clang::index;
38 using namespace cxtu;
39 using namespace cxindex;
41 namespace {
43 //===----------------------------------------------------------------------===//
44 // Skip Parsed Bodies
45 //===----------------------------------------------------------------------===//
47 /// A "region" in source code identified by the file/offset of the
48 /// preprocessor conditional directive that it belongs to.
49 /// Multiple, non-consecutive ranges can be parts of the same region.
50 ///
51 /// As an example of different regions separated by preprocessor directives:
52 ///
53 /// \code
54 /// #1
55 /// #ifdef BLAH
56 /// #2
57 /// #ifdef CAKE
58 /// #3
59 /// #endif
60 /// #2
61 /// #endif
62 /// #1
63 /// \endcode
64 ///
65 /// There are 3 regions, with non-consecutive parts:
66 /// #1 is identified as the beginning of the file
67 /// #2 is identified as the location of "#ifdef BLAH"
68 /// #3 is identified as the location of "#ifdef CAKE"
69 ///
70 class PPRegion {
71 llvm::sys::fs::UniqueID UniqueID;
72 time_t ModTime;
73 unsigned Offset;
74 public:
75 PPRegion() : UniqueID(0, 0), ModTime(), Offset() {}
76 PPRegion(llvm::sys::fs::UniqueID UniqueID, unsigned offset, time_t modTime)
77 : UniqueID(UniqueID), ModTime(modTime), Offset(offset) {}
79 const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; }
80 unsigned getOffset() const { return Offset; }
81 time_t getModTime() const { return ModTime; }
83 bool isInvalid() const { return *this == PPRegion(); }
85 friend bool operator==(const PPRegion &lhs, const PPRegion &rhs) {
86 return lhs.UniqueID == rhs.UniqueID && lhs.Offset == rhs.Offset &&
87 lhs.ModTime == rhs.ModTime;
91 } // end anonymous namespace
93 namespace llvm {
95 template <>
96 struct DenseMapInfo<PPRegion> {
97 static inline PPRegion getEmptyKey() {
98 return PPRegion(llvm::sys::fs::UniqueID(0, 0), unsigned(-1), 0);
100 static inline PPRegion getTombstoneKey() {
101 return PPRegion(llvm::sys::fs::UniqueID(0, 0), unsigned(-2), 0);
104 static unsigned getHashValue(const PPRegion &S) {
105 llvm::FoldingSetNodeID ID;
106 const llvm::sys::fs::UniqueID &UniqueID = S.getUniqueID();
107 ID.AddInteger(UniqueID.getFile());
108 ID.AddInteger(UniqueID.getDevice());
109 ID.AddInteger(S.getOffset());
110 ID.AddInteger(S.getModTime());
111 return ID.ComputeHash();
114 static bool isEqual(const PPRegion &LHS, const PPRegion &RHS) {
115 return LHS == RHS;
120 namespace {
122 /// Keeps track of function bodies that have already been parsed.
124 /// Is thread-safe.
125 class ThreadSafeParsedRegions {
126 mutable std::mutex Mutex;
127 llvm::DenseSet<PPRegion> ParsedRegions;
129 public:
130 ~ThreadSafeParsedRegions() = default;
132 llvm::DenseSet<PPRegion> getParsedRegions() const {
133 std::lock_guard<std::mutex> MG(Mutex);
134 return ParsedRegions;
137 void addParsedRegions(ArrayRef<PPRegion> Regions) {
138 std::lock_guard<std::mutex> MG(Mutex);
139 ParsedRegions.insert(Regions.begin(), Regions.end());
143 /// Provides information whether source locations have already been parsed in
144 /// another FrontendAction.
146 /// Is NOT thread-safe.
147 class ParsedSrcLocationsTracker {
148 ThreadSafeParsedRegions &ParsedRegionsStorage;
149 PPConditionalDirectiveRecord &PPRec;
150 Preprocessor &PP;
152 /// Snapshot of the shared state at the point when this instance was
153 /// constructed.
154 llvm::DenseSet<PPRegion> ParsedRegionsSnapshot;
155 /// Regions that were queried during this instance lifetime.
156 SmallVector<PPRegion, 32> NewParsedRegions;
158 /// Caching the last queried region.
159 PPRegion LastRegion;
160 bool LastIsParsed;
162 public:
163 /// Creates snapshot of \p ParsedRegionsStorage.
164 ParsedSrcLocationsTracker(ThreadSafeParsedRegions &ParsedRegionsStorage,
165 PPConditionalDirectiveRecord &ppRec,
166 Preprocessor &pp)
167 : ParsedRegionsStorage(ParsedRegionsStorage), PPRec(ppRec), PP(pp),
168 ParsedRegionsSnapshot(ParsedRegionsStorage.getParsedRegions()) {}
170 /// \returns true iff \p Loc has already been parsed.
172 /// Can provide false-negative in case the location was parsed after this
173 /// instance had been constructed.
174 bool hasAlredyBeenParsed(SourceLocation Loc, FileID FID, FileEntryRef FE) {
175 PPRegion region = getRegion(Loc, FID, FE);
176 if (region.isInvalid())
177 return false;
179 // Check common case, consecutive functions in the same region.
180 if (LastRegion == region)
181 return LastIsParsed;
183 LastRegion = region;
184 // Source locations can't be revisited during single TU parsing.
185 // That means if we hit the same region again, it's a different location in
186 // the same region and so the "is parsed" value from the snapshot is still
187 // correct.
188 LastIsParsed = ParsedRegionsSnapshot.count(region);
189 if (!LastIsParsed)
190 NewParsedRegions.emplace_back(std::move(region));
191 return LastIsParsed;
194 /// Updates ParsedRegionsStorage with newly parsed regions.
195 void syncWithStorage() {
196 ParsedRegionsStorage.addParsedRegions(NewParsedRegions);
199 private:
200 PPRegion getRegion(SourceLocation Loc, FileID FID, FileEntryRef FE) {
201 auto Bail = [this, FE]() {
202 if (isParsedOnceInclude(FE)) {
203 const llvm::sys::fs::UniqueID &ID = FE.getUniqueID();
204 return PPRegion(ID, 0, FE.getModificationTime());
206 return PPRegion();
209 SourceLocation RegionLoc = PPRec.findConditionalDirectiveRegionLoc(Loc);
210 assert(RegionLoc.isFileID());
211 if (RegionLoc.isInvalid())
212 return Bail();
214 FileID RegionFID;
215 unsigned RegionOffset;
216 std::tie(RegionFID, RegionOffset) =
217 PPRec.getSourceManager().getDecomposedLoc(RegionLoc);
219 if (RegionFID != FID)
220 return Bail();
222 const llvm::sys::fs::UniqueID &ID = FE.getUniqueID();
223 return PPRegion(ID, RegionOffset, FE.getModificationTime());
226 bool isParsedOnceInclude(FileEntryRef FE) {
227 return PP.getHeaderSearchInfo().isFileMultipleIncludeGuarded(FE) ||
228 PP.getHeaderSearchInfo().hasFileBeenImported(FE);
232 //===----------------------------------------------------------------------===//
233 // IndexPPCallbacks
234 //===----------------------------------------------------------------------===//
236 class IndexPPCallbacks : public PPCallbacks {
237 Preprocessor &PP;
238 CXIndexDataConsumer &DataConsumer;
239 bool IsMainFileEntered;
241 public:
242 IndexPPCallbacks(Preprocessor &PP, CXIndexDataConsumer &dataConsumer)
243 : PP(PP), DataConsumer(dataConsumer), IsMainFileEntered(false) { }
245 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
246 SrcMgr::CharacteristicKind FileType, FileID PrevFID) override {
247 if (IsMainFileEntered)
248 return;
250 SourceManager &SM = PP.getSourceManager();
251 SourceLocation MainFileLoc = SM.getLocForStartOfFile(SM.getMainFileID());
253 if (Loc == MainFileLoc && Reason == PPCallbacks::EnterFile) {
254 IsMainFileEntered = true;
255 DataConsumer.enteredMainFile(
256 *SM.getFileEntryRefForID(SM.getMainFileID()));
260 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
261 StringRef FileName, bool IsAngled,
262 CharSourceRange FilenameRange,
263 OptionalFileEntryRef File, StringRef SearchPath,
264 StringRef RelativePath, const Module *Imported,
265 SrcMgr::CharacteristicKind FileType) override {
266 bool isImport = (IncludeTok.is(tok::identifier) &&
267 IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import);
268 DataConsumer.ppIncludedFile(HashLoc, FileName, File, isImport, IsAngled,
269 Imported);
272 /// MacroDefined - This hook is called whenever a macro definition is seen.
273 void MacroDefined(const Token &Id, const MacroDirective *MD) override {}
275 /// MacroUndefined - This hook is called whenever a macro #undef is seen.
276 /// MI is released immediately following this callback.
277 void MacroUndefined(const Token &MacroNameTok,
278 const MacroDefinition &MD,
279 const MacroDirective *UD) override {}
281 /// MacroExpands - This is called by when a macro invocation is found.
282 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
283 SourceRange Range, const MacroArgs *Args) override {}
285 /// SourceRangeSkipped - This hook is called when a source range is skipped.
286 /// \param Range The SourceRange that was skipped. The range begins at the
287 /// #if/#else directive and ends after the #endif/#else directive.
288 void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override {
292 //===----------------------------------------------------------------------===//
293 // IndexingConsumer
294 //===----------------------------------------------------------------------===//
296 class IndexingConsumer : public ASTConsumer {
297 CXIndexDataConsumer &DataConsumer;
299 public:
300 IndexingConsumer(CXIndexDataConsumer &dataConsumer,
301 ParsedSrcLocationsTracker *parsedLocsTracker)
302 : DataConsumer(dataConsumer) {}
304 void Initialize(ASTContext &Context) override {
305 DataConsumer.setASTContext(Context);
306 DataConsumer.startedTranslationUnit();
309 bool HandleTopLevelDecl(DeclGroupRef DG) override {
310 return !DataConsumer.shouldAbort();
314 //===----------------------------------------------------------------------===//
315 // CaptureDiagnosticConsumer
316 //===----------------------------------------------------------------------===//
318 class CaptureDiagnosticConsumer : public DiagnosticConsumer {
319 SmallVector<StoredDiagnostic, 4> Errors;
320 public:
322 void HandleDiagnostic(DiagnosticsEngine::Level level,
323 const Diagnostic &Info) override {
324 if (level >= DiagnosticsEngine::Error)
325 Errors.push_back(StoredDiagnostic(level, Info));
329 //===----------------------------------------------------------------------===//
330 // IndexingFrontendAction
331 //===----------------------------------------------------------------------===//
333 class IndexingFrontendAction : public ASTFrontendAction {
334 std::shared_ptr<CXIndexDataConsumer> DataConsumer;
335 IndexingOptions Opts;
337 ThreadSafeParsedRegions *SKData;
338 std::unique_ptr<ParsedSrcLocationsTracker> ParsedLocsTracker;
340 public:
341 IndexingFrontendAction(std::shared_ptr<CXIndexDataConsumer> dataConsumer,
342 const IndexingOptions &Opts,
343 ThreadSafeParsedRegions *skData)
344 : DataConsumer(std::move(dataConsumer)), Opts(Opts), SKData(skData) {}
346 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
347 StringRef InFile) override {
348 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
350 if (!PPOpts.ImplicitPCHInclude.empty()) {
351 if (auto File =
352 CI.getFileManager().getOptionalFileRef(PPOpts.ImplicitPCHInclude))
353 DataConsumer->importedPCH(*File);
356 DataConsumer->setASTContext(CI.getASTContext());
357 Preprocessor &PP = CI.getPreprocessor();
358 PP.addPPCallbacks(std::make_unique<IndexPPCallbacks>(PP, *DataConsumer));
359 DataConsumer->setPreprocessor(CI.getPreprocessorPtr());
361 if (SKData) {
362 auto *PPRec = new PPConditionalDirectiveRecord(PP.getSourceManager());
363 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(PPRec));
364 ParsedLocsTracker =
365 std::make_unique<ParsedSrcLocationsTracker>(*SKData, *PPRec, PP);
368 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
369 Consumers.push_back(std::make_unique<IndexingConsumer>(
370 *DataConsumer, ParsedLocsTracker.get()));
371 Consumers.push_back(createIndexingASTConsumer(
372 DataConsumer, Opts, CI.getPreprocessorPtr(),
373 [this](const Decl *D) { return this->shouldSkipFunctionBody(D); }));
374 return std::make_unique<MultiplexConsumer>(std::move(Consumers));
377 bool shouldSkipFunctionBody(const Decl *D) {
378 if (!ParsedLocsTracker) {
379 // Always skip bodies.
380 return true;
383 const SourceManager &SM = D->getASTContext().getSourceManager();
384 SourceLocation Loc = D->getLocation();
385 if (Loc.isMacroID())
386 return false;
387 if (SM.isInSystemHeader(Loc))
388 return true; // always skip bodies from system headers.
390 FileID FID;
391 unsigned Offset;
392 std::tie(FID, Offset) = SM.getDecomposedLoc(Loc);
393 // Don't skip bodies from main files; this may be revisited.
394 if (SM.getMainFileID() == FID)
395 return false;
396 OptionalFileEntryRef FE = SM.getFileEntryRefForID(FID);
397 if (!FE)
398 return false;
400 return ParsedLocsTracker->hasAlredyBeenParsed(Loc, FID, *FE);
403 TranslationUnitKind getTranslationUnitKind() override {
404 if (DataConsumer->shouldIndexImplicitTemplateInsts())
405 return TU_Complete;
406 else
407 return TU_Prefix;
409 bool hasCodeCompletionSupport() const override { return false; }
411 void EndSourceFileAction() override {
412 if (ParsedLocsTracker)
413 ParsedLocsTracker->syncWithStorage();
417 //===----------------------------------------------------------------------===//
418 // clang_indexSourceFileUnit Implementation
419 //===----------------------------------------------------------------------===//
421 static IndexingOptions getIndexingOptionsFromCXOptions(unsigned index_options) {
422 IndexingOptions IdxOpts;
423 if (index_options & CXIndexOpt_IndexFunctionLocalSymbols)
424 IdxOpts.IndexFunctionLocals = true;
425 if (index_options & CXIndexOpt_IndexImplicitTemplateInstantiations)
426 IdxOpts.IndexImplicitInstantiation = true;
427 return IdxOpts;
430 struct IndexSessionData {
431 CXIndex CIdx;
432 std::unique_ptr<ThreadSafeParsedRegions> SkipBodyData =
433 std::make_unique<ThreadSafeParsedRegions>();
435 explicit IndexSessionData(CXIndex cIdx) : CIdx(cIdx) {}
438 } // anonymous namespace
440 static CXErrorCode clang_indexSourceFile_Impl(
441 CXIndexAction cxIdxAction, CXClientData client_data,
442 IndexerCallbacks *client_index_callbacks, unsigned index_callbacks_size,
443 unsigned index_options, const char *source_filename,
444 const char *const *command_line_args, int num_command_line_args,
445 ArrayRef<CXUnsavedFile> unsaved_files, CXTranslationUnit *out_TU,
446 unsigned TU_options) {
447 if (out_TU)
448 *out_TU = nullptr;
449 bool requestedToGetTU = (out_TU != nullptr);
451 if (!cxIdxAction) {
452 return CXError_InvalidArguments;
454 if (!client_index_callbacks || index_callbacks_size == 0) {
455 return CXError_InvalidArguments;
458 IndexerCallbacks CB;
459 memset(&CB, 0, sizeof(CB));
460 unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
461 ? index_callbacks_size : sizeof(CB);
462 memcpy(&CB, client_index_callbacks, ClientCBSize);
464 IndexSessionData *IdxSession = static_cast<IndexSessionData *>(cxIdxAction);
465 CIndexer *CXXIdx = static_cast<CIndexer *>(IdxSession->CIdx);
467 if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
468 setThreadBackgroundPriority();
470 CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::All;
471 if (TU_options & CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles)
472 CaptureDiagnostics = CaptureDiagsKind::AllWithoutNonErrorsFromIncludes;
473 if (Logger::isLoggingEnabled())
474 CaptureDiagnostics = CaptureDiagsKind::None;
476 CaptureDiagnosticConsumer *CaptureDiag = nullptr;
477 if (CaptureDiagnostics != CaptureDiagsKind::None)
478 CaptureDiag = new CaptureDiagnosticConsumer();
480 // Configure the diagnostics.
481 IntrusiveRefCntPtr<DiagnosticsEngine>
482 Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions,
483 CaptureDiag,
484 /*ShouldOwnClient=*/true));
486 // Recover resources if we crash before exiting this function.
487 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
488 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
489 DiagCleanup(Diags.get());
491 std::unique_ptr<std::vector<const char *>> Args(
492 new std::vector<const char *>());
494 // Recover resources if we crash before exiting this method.
495 llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> >
496 ArgsCleanup(Args.get());
498 Args->insert(Args->end(), command_line_args,
499 command_line_args + num_command_line_args);
501 // The 'source_filename' argument is optional. If the caller does not
502 // specify it then it is assumed that the source file is specified
503 // in the actual argument list.
504 // Put the source file after command_line_args otherwise if '-x' flag is
505 // present it will be unused.
506 if (source_filename)
507 Args->push_back(source_filename);
509 CreateInvocationOptions CIOpts;
510 CIOpts.Diags = Diags;
511 CIOpts.ProbePrecompiled = true; // FIXME: historical default. Needed?
512 std::shared_ptr<CompilerInvocation> CInvok =
513 createInvocation(*Args, std::move(CIOpts));
515 if (!CInvok)
516 return CXError_Failure;
518 // Recover resources if we crash before exiting this function.
519 llvm::CrashRecoveryContextCleanupRegistrar<
520 std::shared_ptr<CompilerInvocation>,
521 llvm::CrashRecoveryContextDestructorCleanup<
522 std::shared_ptr<CompilerInvocation>>>
523 CInvokCleanup(&CInvok);
525 if (CInvok->getFrontendOpts().Inputs.empty())
526 return CXError_Failure;
528 typedef SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 8> MemBufferOwner;
529 std::unique_ptr<MemBufferOwner> BufOwner(new MemBufferOwner);
531 // Recover resources if we crash before exiting this method.
532 llvm::CrashRecoveryContextCleanupRegistrar<MemBufferOwner> BufOwnerCleanup(
533 BufOwner.get());
535 for (auto &UF : unsaved_files) {
536 std::unique_ptr<llvm::MemoryBuffer> MB =
537 llvm::MemoryBuffer::getMemBufferCopy(getContents(UF), UF.Filename);
538 CInvok->getPreprocessorOpts().addRemappedFile(UF.Filename, MB.get());
539 BufOwner->push_back(std::move(MB));
542 // Since libclang is primarily used by batch tools dealing with
543 // (often very broken) source code, where spell-checking can have a
544 // significant negative impact on performance (particularly when
545 // precompiled headers are involved), we disable it.
546 CInvok->getLangOpts().SpellChecking = false;
548 if (index_options & CXIndexOpt_SuppressWarnings)
549 CInvok->getDiagnosticOpts().IgnoreWarnings = true;
551 // Make sure to use the raw module format.
552 CInvok->getHeaderSearchOpts().ModuleFormat = std::string(
553 CXXIdx->getPCHContainerOperations()->getRawReader().getFormats().front());
555 auto Unit = ASTUnit::create(CInvok, Diags, CaptureDiagnostics,
556 /*UserFilesAreVolatile=*/true);
557 if (!Unit)
558 return CXError_InvalidArguments;
560 auto *UPtr = Unit.get();
561 std::unique_ptr<CXTUOwner> CXTU(
562 new CXTUOwner(MakeCXTranslationUnit(CXXIdx, std::move(Unit))));
564 // Recover resources if we crash before exiting this method.
565 llvm::CrashRecoveryContextCleanupRegistrar<CXTUOwner>
566 CXTUCleanup(CXTU.get());
568 // Enable the skip-parsed-bodies optimization only for C++; this may be
569 // revisited.
570 bool SkipBodies = (index_options & CXIndexOpt_SkipParsedBodiesInSession) &&
571 CInvok->getLangOpts().CPlusPlus;
572 if (SkipBodies)
573 CInvok->getFrontendOpts().SkipFunctionBodies = true;
575 auto DataConsumer =
576 std::make_shared<CXIndexDataConsumer>(client_data, CB, index_options,
577 CXTU->getTU());
578 auto IndexAction = std::make_unique<IndexingFrontendAction>(
579 DataConsumer, getIndexingOptionsFromCXOptions(index_options),
580 SkipBodies ? IdxSession->SkipBodyData.get() : nullptr);
582 // Recover resources if we crash before exiting this method.
583 llvm::CrashRecoveryContextCleanupRegistrar<FrontendAction>
584 IndexActionCleanup(IndexAction.get());
586 bool Persistent = requestedToGetTU;
587 bool OnlyLocalDecls = false;
588 bool PrecompilePreamble = false;
589 bool CreatePreambleOnFirstParse = false;
590 bool CacheCodeCompletionResults = false;
591 PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts();
592 PPOpts.AllowPCHWithCompilerErrors = true;
594 if (requestedToGetTU) {
595 OnlyLocalDecls = CXXIdx->getOnlyLocalDecls();
596 PrecompilePreamble = TU_options & CXTranslationUnit_PrecompiledPreamble;
597 CreatePreambleOnFirstParse =
598 TU_options & CXTranslationUnit_CreatePreambleOnFirstParse;
599 // FIXME: Add a flag for modules.
600 CacheCodeCompletionResults
601 = TU_options & CXTranslationUnit_CacheCompletionResults;
604 if (TU_options & CXTranslationUnit_DetailedPreprocessingRecord) {
605 PPOpts.DetailedRecord = true;
608 if (!requestedToGetTU && !CInvok->getLangOpts().Modules)
609 PPOpts.DetailedRecord = false;
611 // Unless the user specified that they want the preamble on the first parse
612 // set it up to be created on the first reparse. This makes the first parse
613 // faster, trading for a slower (first) reparse.
614 unsigned PrecompilePreambleAfterNParses =
615 !PrecompilePreamble ? 0 : 2 - CreatePreambleOnFirstParse;
616 DiagnosticErrorTrap DiagTrap(*Diags);
617 bool Success = ASTUnit::LoadFromCompilerInvocationAction(
618 std::move(CInvok), CXXIdx->getPCHContainerOperations(), Diags,
619 IndexAction.get(), UPtr, Persistent, CXXIdx->getClangResourcesPath(),
620 OnlyLocalDecls, CaptureDiagnostics, PrecompilePreambleAfterNParses,
621 CacheCodeCompletionResults, /*UserFilesAreVolatile=*/true);
622 if (DiagTrap.hasErrorOccurred() && CXXIdx->getDisplayDiagnostics())
623 printDiagsToStderr(UPtr);
625 if (isASTReadError(UPtr))
626 return CXError_ASTReadError;
628 if (!Success)
629 return CXError_Failure;
631 if (out_TU)
632 *out_TU = CXTU->takeTU();
634 return CXError_Success;
637 //===----------------------------------------------------------------------===//
638 // clang_indexTranslationUnit Implementation
639 //===----------------------------------------------------------------------===//
641 static void indexPreprocessingRecord(ASTUnit &Unit, CXIndexDataConsumer &IdxCtx) {
642 Preprocessor &PP = Unit.getPreprocessor();
643 if (!PP.getPreprocessingRecord())
644 return;
646 // FIXME: Only deserialize inclusion directives.
648 bool isModuleFile = Unit.isModuleFile();
649 for (PreprocessedEntity *PPE : Unit.getLocalPreprocessingEntities()) {
650 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(PPE)) {
651 SourceLocation Loc = ID->getSourceRange().getBegin();
652 // Modules have synthetic main files as input, give an invalid location
653 // if the location points to such a file.
654 if (isModuleFile && Unit.isInMainFileID(Loc))
655 Loc = SourceLocation();
656 IdxCtx.ppIncludedFile(Loc, ID->getFileName(),
657 ID->getFile(),
658 ID->getKind() == InclusionDirective::Import,
659 !ID->wasInQuotes(), ID->importedModule());
664 static CXErrorCode clang_indexTranslationUnit_Impl(
665 CXIndexAction idxAction, CXClientData client_data,
666 IndexerCallbacks *client_index_callbacks, unsigned index_callbacks_size,
667 unsigned index_options, CXTranslationUnit TU) {
668 // Check arguments.
669 if (isNotUsableTU(TU)) {
670 LOG_BAD_TU(TU);
671 return CXError_InvalidArguments;
673 if (!client_index_callbacks || index_callbacks_size == 0) {
674 return CXError_InvalidArguments;
677 CIndexer *CXXIdx = TU->CIdx;
678 if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
679 setThreadBackgroundPriority();
681 IndexerCallbacks CB;
682 memset(&CB, 0, sizeof(CB));
683 unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
684 ? index_callbacks_size : sizeof(CB);
685 memcpy(&CB, client_index_callbacks, ClientCBSize);
687 CXIndexDataConsumer DataConsumer(client_data, CB, index_options, TU);
689 ASTUnit *Unit = cxtu::getASTUnit(TU);
690 if (!Unit)
691 return CXError_Failure;
693 ASTUnit::ConcurrencyCheck Check(*Unit);
695 if (OptionalFileEntryRef PCHFile = Unit->getPCHFile())
696 DataConsumer.importedPCH(*PCHFile);
698 FileManager &FileMgr = Unit->getFileManager();
700 if (Unit->getOriginalSourceFileName().empty())
701 DataConsumer.enteredMainFile(std::nullopt);
702 else if (auto MainFile =
703 FileMgr.getFileRef(Unit->getOriginalSourceFileName()))
704 DataConsumer.enteredMainFile(*MainFile);
705 else
706 DataConsumer.enteredMainFile(std::nullopt);
708 DataConsumer.setASTContext(Unit->getASTContext());
709 DataConsumer.startedTranslationUnit();
711 indexPreprocessingRecord(*Unit, DataConsumer);
712 indexASTUnit(*Unit, DataConsumer, getIndexingOptionsFromCXOptions(index_options));
713 DataConsumer.indexDiagnostics();
715 return CXError_Success;
718 //===----------------------------------------------------------------------===//
719 // libclang public APIs.
720 //===----------------------------------------------------------------------===//
722 int clang_index_isEntityObjCContainerKind(CXIdxEntityKind K) {
723 return CXIdxEntity_ObjCClass <= K && K <= CXIdxEntity_ObjCCategory;
726 const CXIdxObjCContainerDeclInfo *
727 clang_index_getObjCContainerDeclInfo(const CXIdxDeclInfo *DInfo) {
728 if (!DInfo)
729 return nullptr;
731 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
732 if (const ObjCContainerDeclInfo *
733 ContInfo = dyn_cast<ObjCContainerDeclInfo>(DI))
734 return &ContInfo->ObjCContDeclInfo;
736 return nullptr;
739 const CXIdxObjCInterfaceDeclInfo *
740 clang_index_getObjCInterfaceDeclInfo(const CXIdxDeclInfo *DInfo) {
741 if (!DInfo)
742 return nullptr;
744 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
745 if (const ObjCInterfaceDeclInfo *
746 InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI))
747 return &InterInfo->ObjCInterDeclInfo;
749 return nullptr;
752 const CXIdxObjCCategoryDeclInfo *
753 clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo *DInfo){
754 if (!DInfo)
755 return nullptr;
757 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
758 if (const ObjCCategoryDeclInfo *
759 CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI))
760 return &CatInfo->ObjCCatDeclInfo;
762 return nullptr;
765 const CXIdxObjCProtocolRefListInfo *
766 clang_index_getObjCProtocolRefListInfo(const CXIdxDeclInfo *DInfo) {
767 if (!DInfo)
768 return nullptr;
770 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
772 if (const ObjCInterfaceDeclInfo *
773 InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(DI))
774 return InterInfo->ObjCInterDeclInfo.protocols;
776 if (const ObjCProtocolDeclInfo *
777 ProtInfo = dyn_cast<ObjCProtocolDeclInfo>(DI))
778 return &ProtInfo->ObjCProtoRefListInfo;
780 if (const ObjCCategoryDeclInfo *CatInfo = dyn_cast<ObjCCategoryDeclInfo>(DI))
781 return CatInfo->ObjCCatDeclInfo.protocols;
783 return nullptr;
786 const CXIdxObjCPropertyDeclInfo *
787 clang_index_getObjCPropertyDeclInfo(const CXIdxDeclInfo *DInfo) {
788 if (!DInfo)
789 return nullptr;
791 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
792 if (const ObjCPropertyDeclInfo *PropInfo = dyn_cast<ObjCPropertyDeclInfo>(DI))
793 return &PropInfo->ObjCPropDeclInfo;
795 return nullptr;
798 const CXIdxIBOutletCollectionAttrInfo *
799 clang_index_getIBOutletCollectionAttrInfo(const CXIdxAttrInfo *AInfo) {
800 if (!AInfo)
801 return nullptr;
803 const AttrInfo *DI = static_cast<const AttrInfo *>(AInfo);
804 if (const IBOutletCollectionInfo *
805 IBInfo = dyn_cast<IBOutletCollectionInfo>(DI))
806 return &IBInfo->IBCollInfo;
808 return nullptr;
811 const CXIdxCXXClassDeclInfo *
812 clang_index_getCXXClassDeclInfo(const CXIdxDeclInfo *DInfo) {
813 if (!DInfo)
814 return nullptr;
816 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
817 if (const CXXClassDeclInfo *ClassInfo = dyn_cast<CXXClassDeclInfo>(DI))
818 return &ClassInfo->CXXClassInfo;
820 return nullptr;
823 CXIdxClientContainer
824 clang_index_getClientContainer(const CXIdxContainerInfo *info) {
825 if (!info)
826 return nullptr;
827 const ContainerInfo *Container = static_cast<const ContainerInfo *>(info);
828 return Container->IndexCtx->getClientContainerForDC(Container->DC);
831 void clang_index_setClientContainer(const CXIdxContainerInfo *info,
832 CXIdxClientContainer client) {
833 if (!info)
834 return;
835 const ContainerInfo *Container = static_cast<const ContainerInfo *>(info);
836 Container->IndexCtx->addContainerInMap(Container->DC, client);
839 CXIdxClientEntity clang_index_getClientEntity(const CXIdxEntityInfo *info) {
840 if (!info)
841 return nullptr;
842 const EntityInfo *Entity = static_cast<const EntityInfo *>(info);
843 return Entity->IndexCtx->getClientEntity(Entity->Dcl);
846 void clang_index_setClientEntity(const CXIdxEntityInfo *info,
847 CXIdxClientEntity client) {
848 if (!info)
849 return;
850 const EntityInfo *Entity = static_cast<const EntityInfo *>(info);
851 Entity->IndexCtx->setClientEntity(Entity->Dcl, client);
854 CXIndexAction clang_IndexAction_create(CXIndex CIdx) {
855 return new IndexSessionData(CIdx);
858 void clang_IndexAction_dispose(CXIndexAction idxAction) {
859 if (idxAction)
860 delete static_cast<IndexSessionData *>(idxAction);
863 int clang_indexSourceFile(CXIndexAction idxAction,
864 CXClientData client_data,
865 IndexerCallbacks *index_callbacks,
866 unsigned index_callbacks_size,
867 unsigned index_options,
868 const char *source_filename,
869 const char * const *command_line_args,
870 int num_command_line_args,
871 struct CXUnsavedFile *unsaved_files,
872 unsigned num_unsaved_files,
873 CXTranslationUnit *out_TU,
874 unsigned TU_options) {
875 SmallVector<const char *, 4> Args;
876 Args.push_back("clang");
877 Args.append(command_line_args, command_line_args + num_command_line_args);
878 return clang_indexSourceFileFullArgv(
879 idxAction, client_data, index_callbacks, index_callbacks_size,
880 index_options, source_filename, Args.data(), Args.size(), unsaved_files,
881 num_unsaved_files, out_TU, TU_options);
884 int clang_indexSourceFileFullArgv(
885 CXIndexAction idxAction, CXClientData client_data,
886 IndexerCallbacks *index_callbacks, unsigned index_callbacks_size,
887 unsigned index_options, const char *source_filename,
888 const char *const *command_line_args, int num_command_line_args,
889 struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files,
890 CXTranslationUnit *out_TU, unsigned TU_options) {
891 LOG_FUNC_SECTION {
892 *Log << source_filename << ": ";
893 for (int i = 0; i != num_command_line_args; ++i)
894 *Log << command_line_args[i] << " ";
897 if (num_unsaved_files && !unsaved_files)
898 return CXError_InvalidArguments;
900 CXErrorCode result = CXError_Failure;
901 auto IndexSourceFileImpl = [=, &result]() {
902 result = clang_indexSourceFile_Impl(
903 idxAction, client_data, index_callbacks, index_callbacks_size,
904 index_options, source_filename, command_line_args,
905 num_command_line_args, llvm::ArrayRef(unsaved_files, num_unsaved_files),
906 out_TU, TU_options);
909 llvm::CrashRecoveryContext CRC;
911 if (!RunSafely(CRC, IndexSourceFileImpl)) {
912 fprintf(stderr, "libclang: crash detected during indexing source file: {\n");
913 fprintf(stderr, " 'source_filename' : '%s'\n", source_filename);
914 fprintf(stderr, " 'command_line_args' : [");
915 for (int i = 0; i != num_command_line_args; ++i) {
916 if (i)
917 fprintf(stderr, ", ");
918 fprintf(stderr, "'%s'", command_line_args[i]);
920 fprintf(stderr, "],\n");
921 fprintf(stderr, " 'unsaved_files' : [");
922 for (unsigned i = 0; i != num_unsaved_files; ++i) {
923 if (i)
924 fprintf(stderr, ", ");
925 fprintf(stderr, "('%s', '...', %ld)", unsaved_files[i].Filename,
926 unsaved_files[i].Length);
928 fprintf(stderr, "],\n");
929 fprintf(stderr, " 'options' : %d,\n", TU_options);
930 fprintf(stderr, "}\n");
932 return 1;
933 } else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
934 if (out_TU)
935 PrintLibclangResourceUsage(*out_TU);
938 return result;
941 int clang_indexTranslationUnit(CXIndexAction idxAction,
942 CXClientData client_data,
943 IndexerCallbacks *index_callbacks,
944 unsigned index_callbacks_size,
945 unsigned index_options,
946 CXTranslationUnit TU) {
947 LOG_FUNC_SECTION {
948 *Log << TU;
951 CXErrorCode result;
952 auto IndexTranslationUnitImpl = [=, &result]() {
953 result = clang_indexTranslationUnit_Impl(
954 idxAction, client_data, index_callbacks, index_callbacks_size,
955 index_options, TU);
958 llvm::CrashRecoveryContext CRC;
960 if (!RunSafely(CRC, IndexTranslationUnitImpl)) {
961 fprintf(stderr, "libclang: crash detected during indexing TU\n");
963 return 1;
966 return result;
969 void clang_indexLoc_getFileLocation(CXIdxLoc location,
970 CXIdxClientFile *indexFile,
971 CXFile *file,
972 unsigned *line,
973 unsigned *column,
974 unsigned *offset) {
975 if (indexFile) *indexFile = nullptr;
976 if (file) *file = nullptr;
977 if (line) *line = 0;
978 if (column) *column = 0;
979 if (offset) *offset = 0;
981 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
982 if (!location.ptr_data[0] || Loc.isInvalid())
983 return;
985 CXIndexDataConsumer &DataConsumer =
986 *static_cast<CXIndexDataConsumer*>(location.ptr_data[0]);
987 DataConsumer.translateLoc(Loc, indexFile, file, line, column, offset);
990 CXSourceLocation clang_indexLoc_getCXSourceLocation(CXIdxLoc location) {
991 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
992 if (!location.ptr_data[0] || Loc.isInvalid())
993 return clang_getNullLocation();
995 CXIndexDataConsumer &DataConsumer =
996 *static_cast<CXIndexDataConsumer*>(location.ptr_data[0]);
997 return cxloc::translateSourceLocation(DataConsumer.getASTContext(), Loc);