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//===----------------------------------------------------------------------===//
8
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 "llvm/Support/VirtualFileSystem.h"
33#include <cstdio>
34#include <mutex>
35#include <utility>
36
37using namespace clang;
38using namespace clang::index;
39using namespace cxtu;
40using namespace cxindex;
41
42namespace {
43
44//===----------------------------------------------------------------------===//
45// Skip Parsed Bodies
46//===----------------------------------------------------------------------===//
47
48/// A "region" in source code identified by the file/offset of the
49/// preprocessor conditional directive that it belongs to.
50/// Multiple, non-consecutive ranges can be parts of the same region.
51///
52/// As an example of different regions separated by preprocessor directives:
53///
54/// \code
55/// #1
56/// #ifdef BLAH
57/// #2
58/// #ifdef CAKE
59/// #3
60/// #endif
61/// #2
62/// #endif
63/// #1
64/// \endcode
65///
66/// There are 3 regions, with non-consecutive parts:
67/// #1 is identified as the beginning of the file
68/// #2 is identified as the location of "#ifdef BLAH"
69/// #3 is identified as the location of "#ifdef CAKE"
70///
71class PPRegion {
72 llvm::sys::fs::UniqueID UniqueID;
73 time_t ModTime;
74 unsigned Offset;
75public:
76 PPRegion() : UniqueID(0, 0), ModTime(), Offset() {}
77 PPRegion(llvm::sys::fs::UniqueID UniqueID, unsigned offset, time_t modTime)
78 : UniqueID(UniqueID), ModTime(modTime), Offset(offset) {}
79
80 const llvm::sys::fs::UniqueID &getUniqueID() const { return UniqueID; }
81 unsigned getOffset() const { return Offset; }
82 time_t getModTime() const { return ModTime; }
83
84 bool isInvalid() const { return *this == PPRegion(); }
85
86 friend bool operator==(const PPRegion &lhs, const PPRegion &rhs) {
87 return lhs.UniqueID == rhs.UniqueID && lhs.Offset == rhs.Offset &&
88 lhs.ModTime == rhs.ModTime;
89 }
90};
91
92} // end anonymous namespace
93
94namespace llvm {
95
96 template <>
97 struct DenseMapInfo<PPRegion> {
98 static inline PPRegion getEmptyKey() {
99 return PPRegion(llvm::sys::fs::UniqueID(0, 0), unsigned(-1), 0);
100 }
101 static inline PPRegion getTombstoneKey() {
102 return PPRegion(llvm::sys::fs::UniqueID(0, 0), unsigned(-2), 0);
103 }
104
105 static unsigned getHashValue(const PPRegion &S) {
106 llvm::FoldingSetNodeID ID;
107 const llvm::sys::fs::UniqueID &UniqueID = S.getUniqueID();
108 ID.AddInteger(I: UniqueID.getFile());
109 ID.AddInteger(I: UniqueID.getDevice());
110 ID.AddInteger(I: S.getOffset());
111 ID.AddInteger(I: S.getModTime());
112 return ID.ComputeHash();
113 }
114
115 static bool isEqual(const PPRegion &LHS, const PPRegion &RHS) {
116 return LHS == RHS;
117 }
118 };
119}
120
121namespace {
122
123/// Keeps track of function bodies that have already been parsed.
124///
125/// Is thread-safe.
126class ThreadSafeParsedRegions {
127 mutable std::mutex Mutex;
128 llvm::DenseSet<PPRegion> ParsedRegions;
129
130public:
131 ~ThreadSafeParsedRegions() = default;
132
133 llvm::DenseSet<PPRegion> getParsedRegions() const {
134 std::lock_guard<std::mutex> MG(Mutex);
135 return ParsedRegions;
136 }
137
138 void addParsedRegions(ArrayRef<PPRegion> Regions) {
139 std::lock_guard<std::mutex> MG(Mutex);
140 ParsedRegions.insert_range(R&: Regions);
141 }
142};
143
144/// Provides information whether source locations have already been parsed in
145/// another FrontendAction.
146///
147/// Is NOT thread-safe.
148class ParsedSrcLocationsTracker {
149 ThreadSafeParsedRegions &ParsedRegionsStorage;
150 PPConditionalDirectiveRecord &PPRec;
151 Preprocessor &PP;
152
153 /// Snapshot of the shared state at the point when this instance was
154 /// constructed.
155 llvm::DenseSet<PPRegion> ParsedRegionsSnapshot;
156 /// Regions that were queried during this instance lifetime.
157 SmallVector<PPRegion, 32> NewParsedRegions;
158
159 /// Caching the last queried region.
160 PPRegion LastRegion;
161 bool LastIsParsed;
162
163public:
164 /// Creates snapshot of \p ParsedRegionsStorage.
165 ParsedSrcLocationsTracker(ThreadSafeParsedRegions &ParsedRegionsStorage,
166 PPConditionalDirectiveRecord &ppRec,
167 Preprocessor &pp)
168 : ParsedRegionsStorage(ParsedRegionsStorage), PPRec(ppRec), PP(pp),
169 ParsedRegionsSnapshot(ParsedRegionsStorage.getParsedRegions()) {}
170
171 /// \returns true iff \p Loc has already been parsed.
172 ///
173 /// Can provide false-negative in case the location was parsed after this
174 /// instance had been constructed.
175 bool hasAlredyBeenParsed(SourceLocation Loc, FileID FID, FileEntryRef FE) {
176 PPRegion region = getRegion(Loc, FID, FE);
177 if (region.isInvalid())
178 return false;
179
180 // Check common case, consecutive functions in the same region.
181 if (LastRegion == region)
182 return LastIsParsed;
183
184 LastRegion = region;
185 // Source locations can't be revisited during single TU parsing.
186 // That means if we hit the same region again, it's a different location in
187 // the same region and so the "is parsed" value from the snapshot is still
188 // correct.
189 LastIsParsed = ParsedRegionsSnapshot.count(V: region);
190 if (!LastIsParsed)
191 NewParsedRegions.emplace_back(Args: std::move(region));
192 return LastIsParsed;
193 }
194
195 /// Updates ParsedRegionsStorage with newly parsed regions.
196 void syncWithStorage() {
197 ParsedRegionsStorage.addParsedRegions(Regions: NewParsedRegions);
198 }
199
200private:
201 PPRegion getRegion(SourceLocation Loc, FileID FID, FileEntryRef FE) {
202 auto Bail = [this, FE]() {
203 if (isParsedOnceInclude(FE)) {
204 const llvm::sys::fs::UniqueID &ID = FE.getUniqueID();
205 return PPRegion(ID, 0, FE.getModificationTime());
206 }
207 return PPRegion();
208 };
209
210 SourceLocation RegionLoc = PPRec.findConditionalDirectiveRegionLoc(Loc);
211 assert(RegionLoc.isFileID());
212 if (RegionLoc.isInvalid())
213 return Bail();
214
215 FileID RegionFID;
216 unsigned RegionOffset;
217 std::tie(args&: RegionFID, args&: RegionOffset) =
218 PPRec.getSourceManager().getDecomposedLoc(Loc: RegionLoc);
219
220 if (RegionFID != FID)
221 return Bail();
222
223 const llvm::sys::fs::UniqueID &ID = FE.getUniqueID();
224 return PPRegion(ID, RegionOffset, FE.getModificationTime());
225 }
226
227 bool isParsedOnceInclude(FileEntryRef FE) {
228 return PP.getHeaderSearchInfo().isFileMultipleIncludeGuarded(File: FE) ||
229 PP.getHeaderSearchInfo().hasFileBeenImported(File: FE);
230 }
231};
232
233//===----------------------------------------------------------------------===//
234// IndexPPCallbacks
235//===----------------------------------------------------------------------===//
236
237class IndexPPCallbacks : public PPCallbacks {
238 Preprocessor &PP;
239 CXIndexDataConsumer &DataConsumer;
240 bool IsMainFileEntered;
241
242public:
243 IndexPPCallbacks(Preprocessor &PP, CXIndexDataConsumer &dataConsumer)
244 : PP(PP), DataConsumer(dataConsumer), IsMainFileEntered(false) { }
245
246 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
247 SrcMgr::CharacteristicKind FileType, FileID PrevFID) override {
248 if (IsMainFileEntered)
249 return;
250
251 SourceManager &SM = PP.getSourceManager();
252 SourceLocation MainFileLoc = SM.getLocForStartOfFile(FID: SM.getMainFileID());
253
254 if (Loc == MainFileLoc && Reason == PPCallbacks::EnterFile) {
255 IsMainFileEntered = true;
256 DataConsumer.enteredMainFile(
257 File: *SM.getFileEntryRefForID(FID: SM.getMainFileID()));
258 }
259 }
260
261 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
262 StringRef FileName, bool IsAngled,
263 CharSourceRange FilenameRange,
264 OptionalFileEntryRef File, StringRef SearchPath,
265 StringRef RelativePath, const Module *SuggestedModule,
266 bool ModuleImported,
267 SrcMgr::CharacteristicKind FileType) override {
268 bool isImport = (IncludeTok.is(K: tok::identifier) &&
269 IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import);
270 DataConsumer.ppIncludedFile(hashLoc: HashLoc, filename: FileName, File, isImport, isAngled: IsAngled,
271 isModuleImport: ModuleImported);
272 }
273
274 /// MacroDefined - This hook is called whenever a macro definition is seen.
275 void MacroDefined(const Token &Id, const MacroDirective *MD) override {}
276
277 /// MacroUndefined - This hook is called whenever a macro #undef is seen.
278 /// MI is released immediately following this callback.
279 void MacroUndefined(const Token &MacroNameTok,
280 const MacroDefinition &MD,
281 const MacroDirective *UD) override {}
282
283 /// MacroExpands - This is called by when a macro invocation is found.
284 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
285 SourceRange Range, const MacroArgs *Args) override {}
286
287 /// SourceRangeSkipped - This hook is called when a source range is skipped.
288 /// \param Range The SourceRange that was skipped. The range begins at the
289 /// #if/#else directive and ends after the #endif/#else directive.
290 void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override {
291 }
292};
293
294//===----------------------------------------------------------------------===//
295// IndexingConsumer
296//===----------------------------------------------------------------------===//
297
298class IndexingConsumer : public ASTConsumer {
299 CXIndexDataConsumer &DataConsumer;
300
301public:
302 IndexingConsumer(CXIndexDataConsumer &dataConsumer,
303 ParsedSrcLocationsTracker *parsedLocsTracker)
304 : DataConsumer(dataConsumer) {}
305
306 void Initialize(ASTContext &Context) override {
307 DataConsumer.setASTContext(Context);
308 DataConsumer.startedTranslationUnit();
309 }
310
311 bool HandleTopLevelDecl(DeclGroupRef DG) override {
312 return !DataConsumer.shouldAbort();
313 }
314};
315
316//===----------------------------------------------------------------------===//
317// CaptureDiagnosticConsumer
318//===----------------------------------------------------------------------===//
319
320class CaptureDiagnosticConsumer : public DiagnosticConsumer {
321 SmallVector<StoredDiagnostic, 4> Errors;
322public:
323
324 void HandleDiagnostic(DiagnosticsEngine::Level level,
325 const Diagnostic &Info) override {
326 if (level >= DiagnosticsEngine::Error)
327 Errors.push_back(Elt: StoredDiagnostic(level, Info));
328 }
329};
330
331//===----------------------------------------------------------------------===//
332// IndexingFrontendAction
333//===----------------------------------------------------------------------===//
334
335class IndexingFrontendAction : public ASTFrontendAction {
336 std::shared_ptr<CXIndexDataConsumer> DataConsumer;
337 IndexingOptions Opts;
338
339 ThreadSafeParsedRegions *SKData;
340 std::unique_ptr<ParsedSrcLocationsTracker> ParsedLocsTracker;
341
342public:
343 IndexingFrontendAction(std::shared_ptr<CXIndexDataConsumer> dataConsumer,
344 const IndexingOptions &Opts,
345 ThreadSafeParsedRegions *skData)
346 : DataConsumer(std::move(dataConsumer)), Opts(Opts), SKData(skData) {}
347
348 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
349 StringRef InFile) override {
350 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
351
352 if (!PPOpts.ImplicitPCHInclude.empty()) {
353 if (auto File =
354 CI.getFileManager().getOptionalFileRef(Filename: PPOpts.ImplicitPCHInclude))
355 DataConsumer->importedPCH(File: *File);
356 }
357
358 DataConsumer->setASTContext(CI.getASTContext());
359 Preprocessor &PP = CI.getPreprocessor();
360 PP.addPPCallbacks(C: std::make_unique<IndexPPCallbacks>(args&: PP, args&: *DataConsumer));
361 DataConsumer->setPreprocessor(CI.getPreprocessorPtr());
362
363 if (SKData) {
364 auto *PPRec = new PPConditionalDirectiveRecord(PP.getSourceManager());
365 PP.addPPCallbacks(C: std::unique_ptr<PPCallbacks>(PPRec));
366 ParsedLocsTracker =
367 std::make_unique<ParsedSrcLocationsTracker>(args&: *SKData, args&: *PPRec, args&: PP);
368 }
369
370 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
371 Consumers.push_back(x: std::make_unique<IndexingConsumer>(
372 args&: *DataConsumer, args: ParsedLocsTracker.get()));
373 Consumers.push_back(x: createIndexingASTConsumer(
374 DataConsumer, Opts, PP: CI.getPreprocessorPtr(),
375 ShouldSkipFunctionBody: [this](const Decl *D) { return this->shouldSkipFunctionBody(D); }));
376 return std::make_unique<MultiplexConsumer>(args: std::move(Consumers));
377 }
378
379 bool shouldSkipFunctionBody(const Decl *D) {
380 if (!ParsedLocsTracker) {
381 // Always skip bodies.
382 return true;
383 }
384
385 const SourceManager &SM = D->getASTContext().getSourceManager();
386 SourceLocation Loc = D->getLocation();
387 if (Loc.isMacroID())
388 return false;
389 if (SM.isInSystemHeader(Loc))
390 return true; // always skip bodies from system headers.
391
392 auto [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;
399
400 return ParsedLocsTracker->hasAlredyBeenParsed(Loc, FID, FE: *FE);
401 }
402
403 TranslationUnitKind getTranslationUnitKind() override {
404 if (DataConsumer->shouldIndexImplicitTemplateInsts())
405 return TU_Complete;
406 else
407 return TU_Prefix;
408 }
409 bool hasCodeCompletionSupport() const override { return false; }
410
411 void EndSourceFileAction() override {
412 if (ParsedLocsTracker)
413 ParsedLocsTracker->syncWithStorage();
414 }
415};
416
417//===----------------------------------------------------------------------===//
418// clang_indexSourceFileUnit Implementation
419//===----------------------------------------------------------------------===//
420
421static 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;
428}
429
430struct IndexSessionData {
431 CXIndex CIdx;
432 std::unique_ptr<ThreadSafeParsedRegions> SkipBodyData =
433 std::make_unique<ThreadSafeParsedRegions>();
434
435 explicit IndexSessionData(CXIndex cIdx) : CIdx(cIdx) {}
436};
437
438} // anonymous namespace
439
440static 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);
450
451 if (!cxIdxAction) {
452 return CXError_InvalidArguments;
453 }
454 if (!client_index_callbacks || index_callbacks_size == 0) {
455 return CXError_InvalidArguments;
456 }
457
458 IndexerCallbacks CB;
459 memset(s: &CB, c: 0, n: sizeof(CB));
460 unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
461 ? index_callbacks_size : sizeof(CB);
462 memcpy(dest: &CB, src: client_index_callbacks, n: ClientCBSize);
463
464 IndexSessionData *IdxSession = static_cast<IndexSessionData *>(cxIdxAction);
465 CIndexer *CXXIdx = static_cast<CIndexer *>(IdxSession->CIdx);
466
467 if (CXXIdx->isOptEnabled(opt: CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
468 setThreadBackgroundPriority();
469
470 CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::All;
471 if (TU_options & CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles)
472 CaptureDiagnostics = CaptureDiagsKind::AllWithoutNonErrorsFromIncludes;
473 if (Logger::isLoggingEnabled())
474 CaptureDiagnostics = CaptureDiagsKind::None;
475
476 CaptureDiagnosticConsumer *CaptureDiag = nullptr;
477 if (CaptureDiagnostics != CaptureDiagsKind::None)
478 CaptureDiag = new CaptureDiagnosticConsumer();
479
480 // Configure the diagnostics.
481 auto DiagOpts = std::make_shared<DiagnosticOptions>();
482 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
483 CompilerInstance::createDiagnostics(VFS&: *llvm::vfs::getRealFileSystem(),
484 Opts&: *DiagOpts, Client: CaptureDiag,
485 /*ShouldOwnClient=*/true));
486
487 // Recover resources if we crash before exiting this function.
488 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
489 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
490 DiagCleanup(Diags.get());
491
492 std::unique_ptr<std::vector<const char *>> Args(
493 new std::vector<const char *>());
494
495 // Recover resources if we crash before exiting this method.
496 llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> >
497 ArgsCleanup(Args.get());
498
499 Args->insert(position: Args->end(), first: command_line_args,
500 last: command_line_args + num_command_line_args);
501
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.
507 if (source_filename)
508 Args->push_back(x: source_filename);
509
510 CreateInvocationOptions CIOpts;
511 CIOpts.Diags = Diags;
512 CIOpts.ProbePrecompiled = true; // FIXME: historical default. Needed?
513 std::shared_ptr<CompilerInvocation> CInvok =
514 createInvocation(Args: *Args, Opts: std::move(CIOpts));
515
516 if (!CInvok)
517 return CXError_Failure;
518
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);
525
526 if (CInvok->getFrontendOpts().Inputs.empty())
527 return CXError_Failure;
528
529 typedef SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 8> MemBufferOwner;
530 std::unique_ptr<MemBufferOwner> BufOwner(new MemBufferOwner);
531
532 // Recover resources if we crash before exiting this method.
533 llvm::CrashRecoveryContextCleanupRegistrar<MemBufferOwner> BufOwnerCleanup(
534 BufOwner.get());
535
536 for (auto &UF : unsaved_files) {
537 std::unique_ptr<llvm::MemoryBuffer> MB =
538 llvm::MemoryBuffer::getMemBufferCopy(InputData: getContents(UF), BufferName: UF.Filename);
539 CInvok->getPreprocessorOpts().addRemappedFile(From: UF.Filename, To: MB.get());
540 BufOwner->push_back(Elt: std::move(MB));
541 }
542
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;
548
549 if (index_options & CXIndexOpt_SuppressWarnings)
550 CInvok->getDiagnosticOpts().IgnoreWarnings = true;
551
552 // Make sure to use the raw module format.
553 CInvok->getHeaderSearchOpts().ModuleFormat = std::string(
554 CXXIdx->getPCHContainerOperations()->getRawReader().getFormats().front());
555
556 auto Unit = ASTUnit::create(CI: CInvok, DiagOpts, Diags, CaptureDiagnostics,
557 /*UserFilesAreVolatile=*/true);
558 if (!Unit)
559 return CXError_InvalidArguments;
560
561 auto *UPtr = Unit.get();
562 std::unique_ptr<CXTUOwner> CXTU(
563 new CXTUOwner(MakeCXTranslationUnit(CIdx: CXXIdx, AU: std::move(Unit))));
564
565 // Recover resources if we crash before exiting this method.
566 llvm::CrashRecoveryContextCleanupRegistrar<CXTUOwner>
567 CXTUCleanup(CXTU.get());
568
569 // Enable the skip-parsed-bodies optimization only for C++; this may be
570 // revisited.
571 bool SkipBodies = (index_options & CXIndexOpt_SkipParsedBodiesInSession) &&
572 CInvok->getLangOpts().CPlusPlus;
573 if (SkipBodies)
574 CInvok->getFrontendOpts().SkipFunctionBodies = true;
575
576 auto DataConsumer =
577 std::make_shared<CXIndexDataConsumer>(args&: client_data, args&: CB, args&: index_options,
578 args: CXTU->getTU());
579 auto IndexAction = std::make_unique<IndexingFrontendAction>(
580 args&: DataConsumer, args: getIndexingOptionsFromCXOptions(index_options),
581 args: SkipBodies ? IdxSession->SkipBodyData.get() : nullptr);
582
583 // Recover resources if we crash before exiting this method.
584 llvm::CrashRecoveryContextCleanupRegistrar<FrontendAction>
585 IndexActionCleanup(IndexAction.get());
586
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;
594
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;
603 }
604
605 if (TU_options & CXTranslationUnit_DetailedPreprocessingRecord) {
606 PPOpts.DetailedRecord = true;
607 }
608
609 if (!requestedToGetTU && !CInvok->getLangOpts().Modules)
610 PPOpts.DetailedRecord = false;
611
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 CI: std::move(CInvok), PCHContainerOps: CXXIdx->getPCHContainerOperations(), DiagOpts, Diags,
620 Action: IndexAction.get(), Unit: UPtr, Persistent, ResourceFilesPath: CXXIdx->getClangResourcesPath(),
621 OnlyLocalDecls, CaptureDiagnostics, PrecompilePreambleAfterNParses,
622 CacheCodeCompletionResults, /*UserFilesAreVolatile=*/true);
623 if (DiagTrap.hasErrorOccurred() && CXXIdx->getDisplayDiagnostics())
624 printDiagsToStderr(Unit: UPtr);
625
626 if (isASTReadError(AU: UPtr))
627 return CXError_ASTReadError;
628
629 if (!Success)
630 return CXError_Failure;
631
632 if (out_TU)
633 *out_TU = CXTU->takeTU();
634
635 return CXError_Success;
636}
637
638//===----------------------------------------------------------------------===//
639// clang_indexTranslationUnit Implementation
640//===----------------------------------------------------------------------===//
641
642static void indexPreprocessingRecord(ASTUnit &Unit, CXIndexDataConsumer &IdxCtx) {
643 Preprocessor &PP = Unit.getPreprocessor();
644 if (!PP.getPreprocessingRecord())
645 return;
646
647 // FIXME: Only deserialize inclusion directives.
648
649 bool isModuleFile = Unit.isModuleFile();
650 for (PreprocessedEntity *PPE : Unit.getLocalPreprocessingEntities()) {
651 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(Val: 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(hashLoc: Loc, filename: ID->getFileName(),
658 File: ID->getFile(),
659 isImport: ID->getKind() == InclusionDirective::Import,
660 isAngled: !ID->wasInQuotes(), isModuleImport: ID->importedModule());
661 }
662 }
663}
664
665static 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) {
669 // Check arguments.
670 if (isNotUsableTU(TU)) {
671 LOG_BAD_TU(TU);
672 return CXError_InvalidArguments;
673 }
674 if (!client_index_callbacks || index_callbacks_size == 0) {
675 return CXError_InvalidArguments;
676 }
677
678 CIndexer *CXXIdx = TU->CIdx;
679 if (CXXIdx->isOptEnabled(opt: CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
680 setThreadBackgroundPriority();
681
682 IndexerCallbacks CB;
683 memset(s: &CB, c: 0, n: sizeof(CB));
684 unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
685 ? index_callbacks_size : sizeof(CB);
686 memcpy(dest: &CB, src: client_index_callbacks, n: ClientCBSize);
687
688 CXIndexDataConsumer DataConsumer(client_data, CB, index_options, TU);
689
690 ASTUnit *Unit = cxtu::getASTUnit(TU);
691 if (!Unit)
692 return CXError_Failure;
693
694 ASTUnit::ConcurrencyCheck Check(*Unit);
695
696 if (OptionalFileEntryRef PCHFile = Unit->getPCHFile())
697 DataConsumer.importedPCH(File: *PCHFile);
698
699 FileManager &FileMgr = Unit->getFileManager();
700
701 if (Unit->getOriginalSourceFileName().empty())
702 DataConsumer.enteredMainFile(File: std::nullopt);
703 else if (auto MainFile =
704 FileMgr.getFileRef(Filename: Unit->getOriginalSourceFileName()))
705 DataConsumer.enteredMainFile(File: *MainFile);
706 else
707 DataConsumer.enteredMainFile(File: std::nullopt);
708
709 DataConsumer.setASTContext(Unit->getASTContext());
710 DataConsumer.startedTranslationUnit();
711
712 indexPreprocessingRecord(Unit&: *Unit, IdxCtx&: DataConsumer);
713 indexASTUnit(Unit&: *Unit, DataConsumer, Opts: getIndexingOptionsFromCXOptions(index_options));
714 DataConsumer.indexDiagnostics();
715
716 return CXError_Success;
717}
718
719//===----------------------------------------------------------------------===//
720// libclang public APIs.
721//===----------------------------------------------------------------------===//
722
723int clang_index_isEntityObjCContainerKind(CXIdxEntityKind K) {
724 return CXIdxEntity_ObjCClass <= K && K <= CXIdxEntity_ObjCCategory;
725}
726
727const CXIdxObjCContainerDeclInfo *
728clang_index_getObjCContainerDeclInfo(const CXIdxDeclInfo *DInfo) {
729 if (!DInfo)
730 return nullptr;
731
732 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
733 if (const ObjCContainerDeclInfo *
734 ContInfo = dyn_cast<ObjCContainerDeclInfo>(Val: DI))
735 return &ContInfo->ObjCContDeclInfo;
736
737 return nullptr;
738}
739
740const CXIdxObjCInterfaceDeclInfo *
741clang_index_getObjCInterfaceDeclInfo(const CXIdxDeclInfo *DInfo) {
742 if (!DInfo)
743 return nullptr;
744
745 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
746 if (const ObjCInterfaceDeclInfo *
747 InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(Val: DI))
748 return &InterInfo->ObjCInterDeclInfo;
749
750 return nullptr;
751}
752
753const CXIdxObjCCategoryDeclInfo *
754clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo *DInfo){
755 if (!DInfo)
756 return nullptr;
757
758 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
759 if (const ObjCCategoryDeclInfo *
760 CatInfo = dyn_cast<ObjCCategoryDeclInfo>(Val: DI))
761 return &CatInfo->ObjCCatDeclInfo;
762
763 return nullptr;
764}
765
766const CXIdxObjCProtocolRefListInfo *
767clang_index_getObjCProtocolRefListInfo(const CXIdxDeclInfo *DInfo) {
768 if (!DInfo)
769 return nullptr;
770
771 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
772
773 if (const ObjCInterfaceDeclInfo *
774 InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(Val: DI))
775 return InterInfo->ObjCInterDeclInfo.protocols;
776
777 if (const ObjCProtocolDeclInfo *
778 ProtInfo = dyn_cast<ObjCProtocolDeclInfo>(Val: DI))
779 return &ProtInfo->ObjCProtoRefListInfo;
780
781 if (const ObjCCategoryDeclInfo *CatInfo = dyn_cast<ObjCCategoryDeclInfo>(Val: DI))
782 return CatInfo->ObjCCatDeclInfo.protocols;
783
784 return nullptr;
785}
786
787const CXIdxObjCPropertyDeclInfo *
788clang_index_getObjCPropertyDeclInfo(const CXIdxDeclInfo *DInfo) {
789 if (!DInfo)
790 return nullptr;
791
792 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
793 if (const ObjCPropertyDeclInfo *PropInfo = dyn_cast<ObjCPropertyDeclInfo>(Val: DI))
794 return &PropInfo->ObjCPropDeclInfo;
795
796 return nullptr;
797}
798
799const CXIdxIBOutletCollectionAttrInfo *
800clang_index_getIBOutletCollectionAttrInfo(const CXIdxAttrInfo *AInfo) {
801 if (!AInfo)
802 return nullptr;
803
804 const AttrInfo *DI = static_cast<const AttrInfo *>(AInfo);
805 if (const IBOutletCollectionInfo *
806 IBInfo = dyn_cast<IBOutletCollectionInfo>(Val: DI))
807 return &IBInfo->IBCollInfo;
808
809 return nullptr;
810}
811
812const CXIdxCXXClassDeclInfo *
813clang_index_getCXXClassDeclInfo(const CXIdxDeclInfo *DInfo) {
814 if (!DInfo)
815 return nullptr;
816
817 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
818 if (const CXXClassDeclInfo *ClassInfo = dyn_cast<CXXClassDeclInfo>(Val: DI))
819 return &ClassInfo->CXXClassInfo;
820
821 return nullptr;
822}
823
824CXIdxClientContainer
825clang_index_getClientContainer(const CXIdxContainerInfo *info) {
826 if (!info)
827 return nullptr;
828 const ContainerInfo *Container = static_cast<const ContainerInfo *>(info);
829 return Container->IndexCtx->getClientContainerForDC(DC: Container->DC);
830}
831
832void clang_index_setClientContainer(const CXIdxContainerInfo *info,
833 CXIdxClientContainer client) {
834 if (!info)
835 return;
836 const ContainerInfo *Container = static_cast<const ContainerInfo *>(info);
837 Container->IndexCtx->addContainerInMap(DC: Container->DC, container: client);
838}
839
840CXIdxClientEntity clang_index_getClientEntity(const CXIdxEntityInfo *info) {
841 if (!info)
842 return nullptr;
843 const EntityInfo *Entity = static_cast<const EntityInfo *>(info);
844 return Entity->IndexCtx->getClientEntity(D: Entity->Dcl);
845}
846
847void clang_index_setClientEntity(const CXIdxEntityInfo *info,
848 CXIdxClientEntity client) {
849 if (!info)
850 return;
851 const EntityInfo *Entity = static_cast<const EntityInfo *>(info);
852 Entity->IndexCtx->setClientEntity(D: Entity->Dcl, client);
853}
854
855CXIndexAction clang_IndexAction_create(CXIndex CIdx) {
856 return new IndexSessionData(CIdx);
857}
858
859void clang_IndexAction_dispose(CXIndexAction idxAction) {
860 if (idxAction)
861 delete static_cast<IndexSessionData *>(idxAction);
862}
863
864int 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(Elt: "clang");
878 Args.append(in_start: command_line_args, in_end: 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, command_line_args: Args.data(), num_command_line_args: Args.size(), unsaved_files,
882 num_unsaved_files, out_TU, TU_options);
883}
884
885int 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) {
892 LOG_FUNC_SECTION {
893 *Log << source_filename << ": ";
894 for (int i = 0; i != num_command_line_args; ++i)
895 *Log << command_line_args[i] << " ";
896 }
897
898 if (num_unsaved_files && !unsaved_files)
899 return CXError_InvalidArguments;
900
901 CXErrorCode result = CXError_Failure;
902 auto IndexSourceFileImpl = [=, &result]() {
903 result = clang_indexSourceFile_Impl(
904 cxIdxAction: idxAction, client_data, client_index_callbacks: index_callbacks, index_callbacks_size,
905 index_options, source_filename, command_line_args,
906 num_command_line_args, unsaved_files: llvm::ArrayRef(unsaved_files, num_unsaved_files),
907 out_TU, TU_options);
908 };
909
910 llvm::CrashRecoveryContext CRC;
911
912 if (!RunSafely(CRC, Fn: IndexSourceFileImpl)) {
913 fprintf(stderr, format: "libclang: crash detected during indexing source file: {\n");
914 fprintf(stderr, format: " 'source_filename' : '%s'\n", source_filename);
915 fprintf(stderr, format: " 'command_line_args' : [");
916 for (int i = 0; i != num_command_line_args; ++i) {
917 if (i)
918 fprintf(stderr, format: ", ");
919 fprintf(stderr, format: "'%s'", command_line_args[i]);
920 }
921 fprintf(stderr, format: "],\n");
922 fprintf(stderr, format: " 'unsaved_files' : [");
923 for (unsigned i = 0; i != num_unsaved_files; ++i) {
924 if (i)
925 fprintf(stderr, format: ", ");
926 fprintf(stderr, format: "('%s', '...', %ld)", unsaved_files[i].Filename,
927 unsaved_files[i].Length);
928 }
929 fprintf(stderr, format: "],\n");
930 fprintf(stderr, format: " 'options' : %d,\n", TU_options);
931 fprintf(stderr, format: "}\n");
932
933 return 1;
934 } else if (getenv(name: "LIBCLANG_RESOURCE_USAGE")) {
935 if (out_TU)
936 PrintLibclangResourceUsage(TU: *out_TU);
937 }
938
939 return result;
940}
941
942int clang_indexTranslationUnit(CXIndexAction idxAction,
943 CXClientData client_data,
944 IndexerCallbacks *index_callbacks,
945 unsigned index_callbacks_size,
946 unsigned index_options,
947 CXTranslationUnit TU) {
948 LOG_FUNC_SECTION {
949 *Log << TU;
950 }
951
952 CXErrorCode result;
953 auto IndexTranslationUnitImpl = [=, &result]() {
954 result = clang_indexTranslationUnit_Impl(
955 idxAction, client_data, client_index_callbacks: index_callbacks, index_callbacks_size,
956 index_options, TU);
957 };
958
959 llvm::CrashRecoveryContext CRC;
960
961 if (!RunSafely(CRC, Fn: IndexTranslationUnitImpl)) {
962 fprintf(stderr, format: "libclang: crash detected during indexing TU\n");
963
964 return 1;
965 }
966
967 return result;
968}
969
970void clang_indexLoc_getFileLocation(CXIdxLoc location,
971 CXIdxClientFile *indexFile,
972 CXFile *file,
973 unsigned *line,
974 unsigned *column,
975 unsigned *offset) {
976 if (indexFile) *indexFile = nullptr;
977 if (file) *file = nullptr;
978 if (line) *line = 0;
979 if (column) *column = 0;
980 if (offset) *offset = 0;
981
982 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: location.int_data);
983 if (!location.ptr_data[0] || Loc.isInvalid())
984 return;
985
986 CXIndexDataConsumer &DataConsumer =
987 *static_cast<CXIndexDataConsumer*>(location.ptr_data[0]);
988 DataConsumer.translateLoc(Loc, indexFile, file, line, column, offset);
989}
990
991CXSourceLocation clang_indexLoc_getCXSourceLocation(CXIdxLoc location) {
992 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: location.int_data);
993 if (!location.ptr_data[0] || Loc.isInvalid())
994 return clang_getNullLocation();
995
996 CXIndexDataConsumer &DataConsumer =
997 *static_cast<CXIndexDataConsumer*>(location.ptr_data[0]);
998 return cxloc::translateSourceLocation(Context&: DataConsumer.getASTContext(), Loc);
999}
1000

source code of clang/tools/libclang/Indexing.cpp