1 //===- Indexing.cpp - Higher level API functions --------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "CIndexDiagnostic.h"
13 #include "CXIndexDataConsumer.h"
14 #include "CXSourceLocation.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"
36 using namespace clang
;
37 using namespace clang::index
;
39 using namespace cxindex
;
43 //===----------------------------------------------------------------------===//
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.
51 /// As an example of different regions separated by preprocessor directives:
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"
71 llvm::sys::fs::UniqueID UniqueID
;
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
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
) {
122 /// Keeps track of function bodies that have already been parsed.
125 class ThreadSafeParsedRegions
{
126 mutable std::mutex Mutex
;
127 llvm::DenseSet
<PPRegion
> ParsedRegions
;
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
;
152 /// Snapshot of the shared state at the point when this instance was
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.
163 /// Creates snapshot of \p ParsedRegionsStorage.
164 ParsedSrcLocationsTracker(ThreadSafeParsedRegions
&ParsedRegionsStorage
,
165 PPConditionalDirectiveRecord
&ppRec
,
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())
179 // Check common case, consecutive functions in the same region.
180 if (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
188 LastIsParsed
= ParsedRegionsSnapshot
.count(region
);
190 NewParsedRegions
.emplace_back(std::move(region
));
194 /// Updates ParsedRegionsStorage with newly parsed regions.
195 void syncWithStorage() {
196 ParsedRegionsStorage
.addParsedRegions(NewParsedRegions
);
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());
209 SourceLocation RegionLoc
= PPRec
.findConditionalDirectiveRegionLoc(Loc
);
210 assert(RegionLoc
.isFileID());
211 if (RegionLoc
.isInvalid())
215 unsigned RegionOffset
;
216 std::tie(RegionFID
, RegionOffset
) =
217 PPRec
.getSourceManager().getDecomposedLoc(RegionLoc
);
219 if (RegionFID
!= FID
)
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 //===----------------------------------------------------------------------===//
234 //===----------------------------------------------------------------------===//
236 class IndexPPCallbacks
: public PPCallbacks
{
238 CXIndexDataConsumer
&DataConsumer
;
239 bool IsMainFileEntered
;
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
)
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
*SuggestedModule
,
266 SrcMgr::CharacteristicKind FileType
) override
{
267 bool isImport
= (IncludeTok
.is(tok::identifier
) &&
268 IncludeTok
.getIdentifierInfo()->getPPKeywordID() == tok::pp_import
);
269 DataConsumer
.ppIncludedFile(HashLoc
, FileName
, File
, isImport
, IsAngled
,
273 /// MacroDefined - This hook is called whenever a macro definition is seen.
274 void MacroDefined(const Token
&Id
, const MacroDirective
*MD
) override
{}
276 /// MacroUndefined - This hook is called whenever a macro #undef is seen.
277 /// MI is released immediately following this callback.
278 void MacroUndefined(const Token
&MacroNameTok
,
279 const MacroDefinition
&MD
,
280 const MacroDirective
*UD
) override
{}
282 /// MacroExpands - This is called by when a macro invocation is found.
283 void MacroExpands(const Token
&MacroNameTok
, const MacroDefinition
&MD
,
284 SourceRange Range
, const MacroArgs
*Args
) override
{}
286 /// SourceRangeSkipped - This hook is called when a source range is skipped.
287 /// \param Range The SourceRange that was skipped. The range begins at the
288 /// #if/#else directive and ends after the #endif/#else directive.
289 void SourceRangeSkipped(SourceRange Range
, SourceLocation EndifLoc
) override
{
293 //===----------------------------------------------------------------------===//
295 //===----------------------------------------------------------------------===//
297 class IndexingConsumer
: public ASTConsumer
{
298 CXIndexDataConsumer
&DataConsumer
;
301 IndexingConsumer(CXIndexDataConsumer
&dataConsumer
,
302 ParsedSrcLocationsTracker
*parsedLocsTracker
)
303 : DataConsumer(dataConsumer
) {}
305 void Initialize(ASTContext
&Context
) override
{
306 DataConsumer
.setASTContext(Context
);
307 DataConsumer
.startedTranslationUnit();
310 bool HandleTopLevelDecl(DeclGroupRef DG
) override
{
311 return !DataConsumer
.shouldAbort();
315 //===----------------------------------------------------------------------===//
316 // CaptureDiagnosticConsumer
317 //===----------------------------------------------------------------------===//
319 class CaptureDiagnosticConsumer
: public DiagnosticConsumer
{
320 SmallVector
<StoredDiagnostic
, 4> Errors
;
323 void HandleDiagnostic(DiagnosticsEngine::Level level
,
324 const Diagnostic
&Info
) override
{
325 if (level
>= DiagnosticsEngine::Error
)
326 Errors
.push_back(StoredDiagnostic(level
, Info
));
330 //===----------------------------------------------------------------------===//
331 // IndexingFrontendAction
332 //===----------------------------------------------------------------------===//
334 class IndexingFrontendAction
: public ASTFrontendAction
{
335 std::shared_ptr
<CXIndexDataConsumer
> DataConsumer
;
336 IndexingOptions Opts
;
338 ThreadSafeParsedRegions
*SKData
;
339 std::unique_ptr
<ParsedSrcLocationsTracker
> ParsedLocsTracker
;
342 IndexingFrontendAction(std::shared_ptr
<CXIndexDataConsumer
> dataConsumer
,
343 const IndexingOptions
&Opts
,
344 ThreadSafeParsedRegions
*skData
)
345 : DataConsumer(std::move(dataConsumer
)), Opts(Opts
), SKData(skData
) {}
347 std::unique_ptr
<ASTConsumer
> CreateASTConsumer(CompilerInstance
&CI
,
348 StringRef InFile
) override
{
349 PreprocessorOptions
&PPOpts
= CI
.getPreprocessorOpts();
351 if (!PPOpts
.ImplicitPCHInclude
.empty()) {
353 CI
.getFileManager().getOptionalFileRef(PPOpts
.ImplicitPCHInclude
))
354 DataConsumer
->importedPCH(*File
);
357 DataConsumer
->setASTContext(CI
.getASTContext());
358 Preprocessor
&PP
= CI
.getPreprocessor();
359 PP
.addPPCallbacks(std::make_unique
<IndexPPCallbacks
>(PP
, *DataConsumer
));
360 DataConsumer
->setPreprocessor(CI
.getPreprocessorPtr());
363 auto *PPRec
= new PPConditionalDirectiveRecord(PP
.getSourceManager());
364 PP
.addPPCallbacks(std::unique_ptr
<PPCallbacks
>(PPRec
));
366 std::make_unique
<ParsedSrcLocationsTracker
>(*SKData
, *PPRec
, PP
);
369 std::vector
<std::unique_ptr
<ASTConsumer
>> Consumers
;
370 Consumers
.push_back(std::make_unique
<IndexingConsumer
>(
371 *DataConsumer
, ParsedLocsTracker
.get()));
372 Consumers
.push_back(createIndexingASTConsumer(
373 DataConsumer
, Opts
, CI
.getPreprocessorPtr(),
374 [this](const Decl
*D
) { return this->shouldSkipFunctionBody(D
); }));
375 return std::make_unique
<MultiplexConsumer
>(std::move(Consumers
));
378 bool shouldSkipFunctionBody(const Decl
*D
) {
379 if (!ParsedLocsTracker
) {
380 // Always skip bodies.
384 const SourceManager
&SM
= D
->getASTContext().getSourceManager();
385 SourceLocation Loc
= D
->getLocation();
388 if (SM
.isInSystemHeader(Loc
))
389 return true; // always skip bodies from system headers.
393 std::tie(FID
, Offset
) = SM
.getDecomposedLoc(Loc
);
394 // Don't skip bodies from main files; this may be revisited.
395 if (SM
.getMainFileID() == FID
)
397 OptionalFileEntryRef FE
= SM
.getFileEntryRefForID(FID
);
401 return ParsedLocsTracker
->hasAlredyBeenParsed(Loc
, FID
, *FE
);
404 TranslationUnitKind
getTranslationUnitKind() override
{
405 if (DataConsumer
->shouldIndexImplicitTemplateInsts())
410 bool hasCodeCompletionSupport() const override
{ return false; }
412 void EndSourceFileAction() override
{
413 if (ParsedLocsTracker
)
414 ParsedLocsTracker
->syncWithStorage();
418 //===----------------------------------------------------------------------===//
419 // clang_indexSourceFileUnit Implementation
420 //===----------------------------------------------------------------------===//
422 static IndexingOptions
getIndexingOptionsFromCXOptions(unsigned index_options
) {
423 IndexingOptions IdxOpts
;
424 if (index_options
& CXIndexOpt_IndexFunctionLocalSymbols
)
425 IdxOpts
.IndexFunctionLocals
= true;
426 if (index_options
& CXIndexOpt_IndexImplicitTemplateInstantiations
)
427 IdxOpts
.IndexImplicitInstantiation
= true;
431 struct IndexSessionData
{
433 std::unique_ptr
<ThreadSafeParsedRegions
> SkipBodyData
=
434 std::make_unique
<ThreadSafeParsedRegions
>();
436 explicit IndexSessionData(CXIndex cIdx
) : CIdx(cIdx
) {}
439 } // anonymous namespace
441 static CXErrorCode
clang_indexSourceFile_Impl(
442 CXIndexAction cxIdxAction
, CXClientData client_data
,
443 IndexerCallbacks
*client_index_callbacks
, unsigned index_callbacks_size
,
444 unsigned index_options
, const char *source_filename
,
445 const char *const *command_line_args
, int num_command_line_args
,
446 ArrayRef
<CXUnsavedFile
> unsaved_files
, CXTranslationUnit
*out_TU
,
447 unsigned TU_options
) {
450 bool requestedToGetTU
= (out_TU
!= nullptr);
453 return CXError_InvalidArguments
;
455 if (!client_index_callbacks
|| index_callbacks_size
== 0) {
456 return CXError_InvalidArguments
;
460 memset(&CB
, 0, sizeof(CB
));
461 unsigned ClientCBSize
= index_callbacks_size
< sizeof(CB
)
462 ? index_callbacks_size
: sizeof(CB
);
463 memcpy(&CB
, client_index_callbacks
, ClientCBSize
);
465 IndexSessionData
*IdxSession
= static_cast<IndexSessionData
*>(cxIdxAction
);
466 CIndexer
*CXXIdx
= static_cast<CIndexer
*>(IdxSession
->CIdx
);
468 if (CXXIdx
->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing
))
469 setThreadBackgroundPriority();
471 CaptureDiagsKind CaptureDiagnostics
= CaptureDiagsKind::All
;
472 if (TU_options
& CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles
)
473 CaptureDiagnostics
= CaptureDiagsKind::AllWithoutNonErrorsFromIncludes
;
474 if (Logger::isLoggingEnabled())
475 CaptureDiagnostics
= CaptureDiagsKind::None
;
477 CaptureDiagnosticConsumer
*CaptureDiag
= nullptr;
478 if (CaptureDiagnostics
!= CaptureDiagsKind::None
)
479 CaptureDiag
= new CaptureDiagnosticConsumer();
481 // Configure the diagnostics.
482 IntrusiveRefCntPtr
<DiagnosticsEngine
>
483 Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions
,
485 /*ShouldOwnClient=*/true));
487 // Recover resources if we crash before exiting this function.
488 llvm::CrashRecoveryContextCleanupRegistrar
<DiagnosticsEngine
,
489 llvm::CrashRecoveryContextReleaseRefCleanup
<DiagnosticsEngine
> >
490 DiagCleanup(Diags
.get());
492 std::unique_ptr
<std::vector
<const char *>> Args(
493 new std::vector
<const char *>());
495 // Recover resources if we crash before exiting this method.
496 llvm::CrashRecoveryContextCleanupRegistrar
<std::vector
<const char*> >
497 ArgsCleanup(Args
.get());
499 Args
->insert(Args
->end(), command_line_args
,
500 command_line_args
+ num_command_line_args
);
502 // The 'source_filename' argument is optional. If the caller does not
503 // specify it then it is assumed that the source file is specified
504 // in the actual argument list.
505 // Put the source file after command_line_args otherwise if '-x' flag is
506 // present it will be unused.
508 Args
->push_back(source_filename
);
510 CreateInvocationOptions CIOpts
;
511 CIOpts
.Diags
= Diags
;
512 CIOpts
.ProbePrecompiled
= true; // FIXME: historical default. Needed?
513 std::shared_ptr
<CompilerInvocation
> CInvok
=
514 createInvocation(*Args
, std::move(CIOpts
));
517 return CXError_Failure
;
519 // Recover resources if we crash before exiting this function.
520 llvm::CrashRecoveryContextCleanupRegistrar
<
521 std::shared_ptr
<CompilerInvocation
>,
522 llvm::CrashRecoveryContextDestructorCleanup
<
523 std::shared_ptr
<CompilerInvocation
>>>
524 CInvokCleanup(&CInvok
);
526 if (CInvok
->getFrontendOpts().Inputs
.empty())
527 return CXError_Failure
;
529 typedef SmallVector
<std::unique_ptr
<llvm::MemoryBuffer
>, 8> MemBufferOwner
;
530 std::unique_ptr
<MemBufferOwner
> BufOwner(new MemBufferOwner
);
532 // Recover resources if we crash before exiting this method.
533 llvm::CrashRecoveryContextCleanupRegistrar
<MemBufferOwner
> BufOwnerCleanup(
536 for (auto &UF
: unsaved_files
) {
537 std::unique_ptr
<llvm::MemoryBuffer
> MB
=
538 llvm::MemoryBuffer::getMemBufferCopy(getContents(UF
), UF
.Filename
);
539 CInvok
->getPreprocessorOpts().addRemappedFile(UF
.Filename
, MB
.get());
540 BufOwner
->push_back(std::move(MB
));
543 // Since libclang is primarily used by batch tools dealing with
544 // (often very broken) source code, where spell-checking can have a
545 // significant negative impact on performance (particularly when
546 // precompiled headers are involved), we disable it.
547 CInvok
->getLangOpts().SpellChecking
= false;
549 if (index_options
& CXIndexOpt_SuppressWarnings
)
550 CInvok
->getDiagnosticOpts().IgnoreWarnings
= true;
552 // Make sure to use the raw module format.
553 CInvok
->getHeaderSearchOpts().ModuleFormat
= std::string(
554 CXXIdx
->getPCHContainerOperations()->getRawReader().getFormats().front());
556 auto Unit
= ASTUnit::create(CInvok
, Diags
, CaptureDiagnostics
,
557 /*UserFilesAreVolatile=*/true);
559 return CXError_InvalidArguments
;
561 auto *UPtr
= Unit
.get();
562 std::unique_ptr
<CXTUOwner
> CXTU(
563 new CXTUOwner(MakeCXTranslationUnit(CXXIdx
, std::move(Unit
))));
565 // Recover resources if we crash before exiting this method.
566 llvm::CrashRecoveryContextCleanupRegistrar
<CXTUOwner
>
567 CXTUCleanup(CXTU
.get());
569 // Enable the skip-parsed-bodies optimization only for C++; this may be
571 bool SkipBodies
= (index_options
& CXIndexOpt_SkipParsedBodiesInSession
) &&
572 CInvok
->getLangOpts().CPlusPlus
;
574 CInvok
->getFrontendOpts().SkipFunctionBodies
= true;
577 std::make_shared
<CXIndexDataConsumer
>(client_data
, CB
, index_options
,
579 auto IndexAction
= std::make_unique
<IndexingFrontendAction
>(
580 DataConsumer
, getIndexingOptionsFromCXOptions(index_options
),
581 SkipBodies
? IdxSession
->SkipBodyData
.get() : nullptr);
583 // Recover resources if we crash before exiting this method.
584 llvm::CrashRecoveryContextCleanupRegistrar
<FrontendAction
>
585 IndexActionCleanup(IndexAction
.get());
587 bool Persistent
= requestedToGetTU
;
588 bool OnlyLocalDecls
= false;
589 bool PrecompilePreamble
= false;
590 bool CreatePreambleOnFirstParse
= false;
591 bool CacheCodeCompletionResults
= false;
592 PreprocessorOptions
&PPOpts
= CInvok
->getPreprocessorOpts();
593 PPOpts
.AllowPCHWithCompilerErrors
= true;
595 if (requestedToGetTU
) {
596 OnlyLocalDecls
= CXXIdx
->getOnlyLocalDecls();
597 PrecompilePreamble
= TU_options
& CXTranslationUnit_PrecompiledPreamble
;
598 CreatePreambleOnFirstParse
=
599 TU_options
& CXTranslationUnit_CreatePreambleOnFirstParse
;
600 // FIXME: Add a flag for modules.
601 CacheCodeCompletionResults
602 = TU_options
& CXTranslationUnit_CacheCompletionResults
;
605 if (TU_options
& CXTranslationUnit_DetailedPreprocessingRecord
) {
606 PPOpts
.DetailedRecord
= true;
609 if (!requestedToGetTU
&& !CInvok
->getLangOpts().Modules
)
610 PPOpts
.DetailedRecord
= false;
612 // Unless the user specified that they want the preamble on the first parse
613 // set it up to be created on the first reparse. This makes the first parse
614 // faster, trading for a slower (first) reparse.
615 unsigned PrecompilePreambleAfterNParses
=
616 !PrecompilePreamble
? 0 : 2 - CreatePreambleOnFirstParse
;
617 DiagnosticErrorTrap
DiagTrap(*Diags
);
618 bool Success
= ASTUnit::LoadFromCompilerInvocationAction(
619 std::move(CInvok
), CXXIdx
->getPCHContainerOperations(), Diags
,
620 IndexAction
.get(), UPtr
, Persistent
, CXXIdx
->getClangResourcesPath(),
621 OnlyLocalDecls
, CaptureDiagnostics
, PrecompilePreambleAfterNParses
,
622 CacheCodeCompletionResults
, /*UserFilesAreVolatile=*/true);
623 if (DiagTrap
.hasErrorOccurred() && CXXIdx
->getDisplayDiagnostics())
624 printDiagsToStderr(UPtr
);
626 if (isASTReadError(UPtr
))
627 return CXError_ASTReadError
;
630 return CXError_Failure
;
633 *out_TU
= CXTU
->takeTU();
635 return CXError_Success
;
638 //===----------------------------------------------------------------------===//
639 // clang_indexTranslationUnit Implementation
640 //===----------------------------------------------------------------------===//
642 static void indexPreprocessingRecord(ASTUnit
&Unit
, CXIndexDataConsumer
&IdxCtx
) {
643 Preprocessor
&PP
= Unit
.getPreprocessor();
644 if (!PP
.getPreprocessingRecord())
647 // FIXME: Only deserialize inclusion directives.
649 bool isModuleFile
= Unit
.isModuleFile();
650 for (PreprocessedEntity
*PPE
: Unit
.getLocalPreprocessingEntities()) {
651 if (InclusionDirective
*ID
= dyn_cast
<InclusionDirective
>(PPE
)) {
652 SourceLocation Loc
= ID
->getSourceRange().getBegin();
653 // Modules have synthetic main files as input, give an invalid location
654 // if the location points to such a file.
655 if (isModuleFile
&& Unit
.isInMainFileID(Loc
))
656 Loc
= SourceLocation();
657 IdxCtx
.ppIncludedFile(Loc
, ID
->getFileName(),
659 ID
->getKind() == InclusionDirective::Import
,
660 !ID
->wasInQuotes(), ID
->importedModule());
665 static CXErrorCode
clang_indexTranslationUnit_Impl(
666 CXIndexAction idxAction
, CXClientData client_data
,
667 IndexerCallbacks
*client_index_callbacks
, unsigned index_callbacks_size
,
668 unsigned index_options
, CXTranslationUnit TU
) {
670 if (isNotUsableTU(TU
)) {
672 return CXError_InvalidArguments
;
674 if (!client_index_callbacks
|| index_callbacks_size
== 0) {
675 return CXError_InvalidArguments
;
678 CIndexer
*CXXIdx
= TU
->CIdx
;
679 if (CXXIdx
->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing
))
680 setThreadBackgroundPriority();
683 memset(&CB
, 0, sizeof(CB
));
684 unsigned ClientCBSize
= index_callbacks_size
< sizeof(CB
)
685 ? index_callbacks_size
: sizeof(CB
);
686 memcpy(&CB
, client_index_callbacks
, ClientCBSize
);
688 CXIndexDataConsumer
DataConsumer(client_data
, CB
, index_options
, TU
);
690 ASTUnit
*Unit
= cxtu::getASTUnit(TU
);
692 return CXError_Failure
;
694 ASTUnit::ConcurrencyCheck
Check(*Unit
);
696 if (OptionalFileEntryRef PCHFile
= Unit
->getPCHFile())
697 DataConsumer
.importedPCH(*PCHFile
);
699 FileManager
&FileMgr
= Unit
->getFileManager();
701 if (Unit
->getOriginalSourceFileName().empty())
702 DataConsumer
.enteredMainFile(std::nullopt
);
703 else if (auto MainFile
=
704 FileMgr
.getFileRef(Unit
->getOriginalSourceFileName()))
705 DataConsumer
.enteredMainFile(*MainFile
);
707 DataConsumer
.enteredMainFile(std::nullopt
);
709 DataConsumer
.setASTContext(Unit
->getASTContext());
710 DataConsumer
.startedTranslationUnit();
712 indexPreprocessingRecord(*Unit
, DataConsumer
);
713 indexASTUnit(*Unit
, DataConsumer
, getIndexingOptionsFromCXOptions(index_options
));
714 DataConsumer
.indexDiagnostics();
716 return CXError_Success
;
719 //===----------------------------------------------------------------------===//
720 // libclang public APIs.
721 //===----------------------------------------------------------------------===//
723 int clang_index_isEntityObjCContainerKind(CXIdxEntityKind K
) {
724 return CXIdxEntity_ObjCClass
<= K
&& K
<= CXIdxEntity_ObjCCategory
;
727 const CXIdxObjCContainerDeclInfo
*
728 clang_index_getObjCContainerDeclInfo(const CXIdxDeclInfo
*DInfo
) {
732 const DeclInfo
*DI
= static_cast<const DeclInfo
*>(DInfo
);
733 if (const ObjCContainerDeclInfo
*
734 ContInfo
= dyn_cast
<ObjCContainerDeclInfo
>(DI
))
735 return &ContInfo
->ObjCContDeclInfo
;
740 const CXIdxObjCInterfaceDeclInfo
*
741 clang_index_getObjCInterfaceDeclInfo(const CXIdxDeclInfo
*DInfo
) {
745 const DeclInfo
*DI
= static_cast<const DeclInfo
*>(DInfo
);
746 if (const ObjCInterfaceDeclInfo
*
747 InterInfo
= dyn_cast
<ObjCInterfaceDeclInfo
>(DI
))
748 return &InterInfo
->ObjCInterDeclInfo
;
753 const CXIdxObjCCategoryDeclInfo
*
754 clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo
*DInfo
){
758 const DeclInfo
*DI
= static_cast<const DeclInfo
*>(DInfo
);
759 if (const ObjCCategoryDeclInfo
*
760 CatInfo
= dyn_cast
<ObjCCategoryDeclInfo
>(DI
))
761 return &CatInfo
->ObjCCatDeclInfo
;
766 const CXIdxObjCProtocolRefListInfo
*
767 clang_index_getObjCProtocolRefListInfo(const CXIdxDeclInfo
*DInfo
) {
771 const DeclInfo
*DI
= static_cast<const DeclInfo
*>(DInfo
);
773 if (const ObjCInterfaceDeclInfo
*
774 InterInfo
= dyn_cast
<ObjCInterfaceDeclInfo
>(DI
))
775 return InterInfo
->ObjCInterDeclInfo
.protocols
;
777 if (const ObjCProtocolDeclInfo
*
778 ProtInfo
= dyn_cast
<ObjCProtocolDeclInfo
>(DI
))
779 return &ProtInfo
->ObjCProtoRefListInfo
;
781 if (const ObjCCategoryDeclInfo
*CatInfo
= dyn_cast
<ObjCCategoryDeclInfo
>(DI
))
782 return CatInfo
->ObjCCatDeclInfo
.protocols
;
787 const CXIdxObjCPropertyDeclInfo
*
788 clang_index_getObjCPropertyDeclInfo(const CXIdxDeclInfo
*DInfo
) {
792 const DeclInfo
*DI
= static_cast<const DeclInfo
*>(DInfo
);
793 if (const ObjCPropertyDeclInfo
*PropInfo
= dyn_cast
<ObjCPropertyDeclInfo
>(DI
))
794 return &PropInfo
->ObjCPropDeclInfo
;
799 const CXIdxIBOutletCollectionAttrInfo
*
800 clang_index_getIBOutletCollectionAttrInfo(const CXIdxAttrInfo
*AInfo
) {
804 const AttrInfo
*DI
= static_cast<const AttrInfo
*>(AInfo
);
805 if (const IBOutletCollectionInfo
*
806 IBInfo
= dyn_cast
<IBOutletCollectionInfo
>(DI
))
807 return &IBInfo
->IBCollInfo
;
812 const CXIdxCXXClassDeclInfo
*
813 clang_index_getCXXClassDeclInfo(const CXIdxDeclInfo
*DInfo
) {
817 const DeclInfo
*DI
= static_cast<const DeclInfo
*>(DInfo
);
818 if (const CXXClassDeclInfo
*ClassInfo
= dyn_cast
<CXXClassDeclInfo
>(DI
))
819 return &ClassInfo
->CXXClassInfo
;
825 clang_index_getClientContainer(const CXIdxContainerInfo
*info
) {
828 const ContainerInfo
*Container
= static_cast<const ContainerInfo
*>(info
);
829 return Container
->IndexCtx
->getClientContainerForDC(Container
->DC
);
832 void clang_index_setClientContainer(const CXIdxContainerInfo
*info
,
833 CXIdxClientContainer client
) {
836 const ContainerInfo
*Container
= static_cast<const ContainerInfo
*>(info
);
837 Container
->IndexCtx
->addContainerInMap(Container
->DC
, client
);
840 CXIdxClientEntity
clang_index_getClientEntity(const CXIdxEntityInfo
*info
) {
843 const EntityInfo
*Entity
= static_cast<const EntityInfo
*>(info
);
844 return Entity
->IndexCtx
->getClientEntity(Entity
->Dcl
);
847 void clang_index_setClientEntity(const CXIdxEntityInfo
*info
,
848 CXIdxClientEntity client
) {
851 const EntityInfo
*Entity
= static_cast<const EntityInfo
*>(info
);
852 Entity
->IndexCtx
->setClientEntity(Entity
->Dcl
, client
);
855 CXIndexAction
clang_IndexAction_create(CXIndex CIdx
) {
856 return new IndexSessionData(CIdx
);
859 void clang_IndexAction_dispose(CXIndexAction idxAction
) {
861 delete static_cast<IndexSessionData
*>(idxAction
);
864 int clang_indexSourceFile(CXIndexAction idxAction
,
865 CXClientData client_data
,
866 IndexerCallbacks
*index_callbacks
,
867 unsigned index_callbacks_size
,
868 unsigned index_options
,
869 const char *source_filename
,
870 const char * const *command_line_args
,
871 int num_command_line_args
,
872 struct CXUnsavedFile
*unsaved_files
,
873 unsigned num_unsaved_files
,
874 CXTranslationUnit
*out_TU
,
875 unsigned TU_options
) {
876 SmallVector
<const char *, 4> Args
;
877 Args
.push_back("clang");
878 Args
.append(command_line_args
, command_line_args
+ num_command_line_args
);
879 return clang_indexSourceFileFullArgv(
880 idxAction
, client_data
, index_callbacks
, index_callbacks_size
,
881 index_options
, source_filename
, Args
.data(), Args
.size(), unsaved_files
,
882 num_unsaved_files
, out_TU
, TU_options
);
885 int clang_indexSourceFileFullArgv(
886 CXIndexAction idxAction
, CXClientData client_data
,
887 IndexerCallbacks
*index_callbacks
, unsigned index_callbacks_size
,
888 unsigned index_options
, const char *source_filename
,
889 const char *const *command_line_args
, int num_command_line_args
,
890 struct CXUnsavedFile
*unsaved_files
, unsigned num_unsaved_files
,
891 CXTranslationUnit
*out_TU
, unsigned TU_options
) {
893 *Log
<< source_filename
<< ": ";
894 for (int i
= 0; i
!= num_command_line_args
; ++i
)
895 *Log
<< command_line_args
[i
] << " ";
898 if (num_unsaved_files
&& !unsaved_files
)
899 return CXError_InvalidArguments
;
901 CXErrorCode result
= CXError_Failure
;
902 auto IndexSourceFileImpl
= [=, &result
]() {
903 result
= clang_indexSourceFile_Impl(
904 idxAction
, client_data
, index_callbacks
, index_callbacks_size
,
905 index_options
, source_filename
, command_line_args
,
906 num_command_line_args
, llvm::ArrayRef(unsaved_files
, num_unsaved_files
),
910 llvm::CrashRecoveryContext CRC
;
912 if (!RunSafely(CRC
, IndexSourceFileImpl
)) {
913 fprintf(stderr
, "libclang: crash detected during indexing source file: {\n");
914 fprintf(stderr
, " 'source_filename' : '%s'\n", source_filename
);
915 fprintf(stderr
, " 'command_line_args' : [");
916 for (int i
= 0; i
!= num_command_line_args
; ++i
) {
918 fprintf(stderr
, ", ");
919 fprintf(stderr
, "'%s'", command_line_args
[i
]);
921 fprintf(stderr
, "],\n");
922 fprintf(stderr
, " 'unsaved_files' : [");
923 for (unsigned i
= 0; i
!= num_unsaved_files
; ++i
) {
925 fprintf(stderr
, ", ");
926 fprintf(stderr
, "('%s', '...', %ld)", unsaved_files
[i
].Filename
,
927 unsaved_files
[i
].Length
);
929 fprintf(stderr
, "],\n");
930 fprintf(stderr
, " 'options' : %d,\n", TU_options
);
931 fprintf(stderr
, "}\n");
934 } else if (getenv("LIBCLANG_RESOURCE_USAGE")) {
936 PrintLibclangResourceUsage(*out_TU
);
942 int clang_indexTranslationUnit(CXIndexAction idxAction
,
943 CXClientData client_data
,
944 IndexerCallbacks
*index_callbacks
,
945 unsigned index_callbacks_size
,
946 unsigned index_options
,
947 CXTranslationUnit TU
) {
953 auto IndexTranslationUnitImpl
= [=, &result
]() {
954 result
= clang_indexTranslationUnit_Impl(
955 idxAction
, client_data
, index_callbacks
, index_callbacks_size
,
959 llvm::CrashRecoveryContext CRC
;
961 if (!RunSafely(CRC
, IndexTranslationUnitImpl
)) {
962 fprintf(stderr
, "libclang: crash detected during indexing TU\n");
970 void clang_indexLoc_getFileLocation(CXIdxLoc location
,
971 CXIdxClientFile
*indexFile
,
976 if (indexFile
) *indexFile
= nullptr;
977 if (file
) *file
= nullptr;
979 if (column
) *column
= 0;
980 if (offset
) *offset
= 0;
982 SourceLocation Loc
= SourceLocation::getFromRawEncoding(location
.int_data
);
983 if (!location
.ptr_data
[0] || Loc
.isInvalid())
986 CXIndexDataConsumer
&DataConsumer
=
987 *static_cast<CXIndexDataConsumer
*>(location
.ptr_data
[0]);
988 DataConsumer
.translateLoc(Loc
, indexFile
, file
, line
, column
, offset
);
991 CXSourceLocation
clang_indexLoc_getCXSourceLocation(CXIdxLoc location
) {
992 SourceLocation Loc
= SourceLocation::getFromRawEncoding(location
.int_data
);
993 if (!location
.ptr_data
[0] || Loc
.isInvalid())
994 return clang_getNullLocation();
996 CXIndexDataConsumer
&DataConsumer
=
997 *static_cast<CXIndexDataConsumer
*>(location
.ptr_data
[0]);
998 return cxloc::translateSourceLocation(DataConsumer
.getASTContext(), Loc
);