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 FileID FID;
393 unsigned Offset;
394 std::tie(args&: FID, args&: Offset) = SM.getDecomposedLoc(Loc);
395 // Don't skip bodies from main files; this may be revisited.
396 if (SM.getMainFileID() == FID)
397 return false;
398 OptionalFileEntryRef FE = SM.getFileEntryRefForID(FID);
399 if (!FE)
400 return false;
401
402 return ParsedLocsTracker->hasAlredyBeenParsed(Loc, FID, FE: *FE);
403 }
404
405 TranslationUnitKind getTranslationUnitKind() override {
406 if (DataConsumer->shouldIndexImplicitTemplateInsts())
407 return TU_Complete;
408 else
409 return TU_Prefix;
410 }
411 bool hasCodeCompletionSupport() const override { return false; }
412
413 void EndSourceFileAction() override {
414 if (ParsedLocsTracker)
415 ParsedLocsTracker->syncWithStorage();
416 }
417};
418
419//===----------------------------------------------------------------------===//
420// clang_indexSourceFileUnit Implementation
421//===----------------------------------------------------------------------===//
422
423static IndexingOptions getIndexingOptionsFromCXOptions(unsigned index_options) {
424 IndexingOptions IdxOpts;
425 if (index_options & CXIndexOpt_IndexFunctionLocalSymbols)
426 IdxOpts.IndexFunctionLocals = true;
427 if (index_options & CXIndexOpt_IndexImplicitTemplateInstantiations)
428 IdxOpts.IndexImplicitInstantiation = true;
429 return IdxOpts;
430}
431
432struct IndexSessionData {
433 CXIndex CIdx;
434 std::unique_ptr<ThreadSafeParsedRegions> SkipBodyData =
435 std::make_unique<ThreadSafeParsedRegions>();
436
437 explicit IndexSessionData(CXIndex cIdx) : CIdx(cIdx) {}
438};
439
440} // anonymous namespace
441
442static CXErrorCode clang_indexSourceFile_Impl(
443 CXIndexAction cxIdxAction, CXClientData client_data,
444 IndexerCallbacks *client_index_callbacks, unsigned index_callbacks_size,
445 unsigned index_options, const char *source_filename,
446 const char *const *command_line_args, int num_command_line_args,
447 ArrayRef<CXUnsavedFile> unsaved_files, CXTranslationUnit *out_TU,
448 unsigned TU_options) {
449 if (out_TU)
450 *out_TU = nullptr;
451 bool requestedToGetTU = (out_TU != nullptr);
452
453 if (!cxIdxAction) {
454 return CXError_InvalidArguments;
455 }
456 if (!client_index_callbacks || index_callbacks_size == 0) {
457 return CXError_InvalidArguments;
458 }
459
460 IndexerCallbacks CB;
461 memset(s: &CB, c: 0, n: sizeof(CB));
462 unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
463 ? index_callbacks_size : sizeof(CB);
464 memcpy(dest: &CB, src: client_index_callbacks, n: ClientCBSize);
465
466 IndexSessionData *IdxSession = static_cast<IndexSessionData *>(cxIdxAction);
467 CIndexer *CXXIdx = static_cast<CIndexer *>(IdxSession->CIdx);
468
469 if (CXXIdx->isOptEnabled(opt: CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
470 setThreadBackgroundPriority();
471
472 CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::All;
473 if (TU_options & CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles)
474 CaptureDiagnostics = CaptureDiagsKind::AllWithoutNonErrorsFromIncludes;
475 if (Logger::isLoggingEnabled())
476 CaptureDiagnostics = CaptureDiagsKind::None;
477
478 CaptureDiagnosticConsumer *CaptureDiag = nullptr;
479 if (CaptureDiagnostics != CaptureDiagsKind::None)
480 CaptureDiag = new CaptureDiagnosticConsumer();
481
482 // Configure the diagnostics.
483 auto DiagOpts = std::make_shared<DiagnosticOptions>();
484 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
485 CompilerInstance::createDiagnostics(VFS&: *llvm::vfs::getRealFileSystem(),
486 Opts&: *DiagOpts, Client: CaptureDiag,
487 /*ShouldOwnClient=*/true));
488
489 // Recover resources if we crash before exiting this function.
490 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine,
491 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> >
492 DiagCleanup(Diags.get());
493
494 std::unique_ptr<std::vector<const char *>> Args(
495 new std::vector<const char *>());
496
497 // Recover resources if we crash before exiting this method.
498 llvm::CrashRecoveryContextCleanupRegistrar<std::vector<const char*> >
499 ArgsCleanup(Args.get());
500
501 Args->insert(position: Args->end(), first: command_line_args,
502 last: command_line_args + num_command_line_args);
503
504 // The 'source_filename' argument is optional. If the caller does not
505 // specify it then it is assumed that the source file is specified
506 // in the actual argument list.
507 // Put the source file after command_line_args otherwise if '-x' flag is
508 // present it will be unused.
509 if (source_filename)
510 Args->push_back(x: source_filename);
511
512 CreateInvocationOptions CIOpts;
513 CIOpts.Diags = Diags;
514 CIOpts.ProbePrecompiled = true; // FIXME: historical default. Needed?
515 std::shared_ptr<CompilerInvocation> CInvok =
516 createInvocation(Args: *Args, Opts: std::move(CIOpts));
517
518 if (!CInvok)
519 return CXError_Failure;
520
521 // Recover resources if we crash before exiting this function.
522 llvm::CrashRecoveryContextCleanupRegistrar<
523 std::shared_ptr<CompilerInvocation>,
524 llvm::CrashRecoveryContextDestructorCleanup<
525 std::shared_ptr<CompilerInvocation>>>
526 CInvokCleanup(&CInvok);
527
528 if (CInvok->getFrontendOpts().Inputs.empty())
529 return CXError_Failure;
530
531 typedef SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 8> MemBufferOwner;
532 std::unique_ptr<MemBufferOwner> BufOwner(new MemBufferOwner);
533
534 // Recover resources if we crash before exiting this method.
535 llvm::CrashRecoveryContextCleanupRegistrar<MemBufferOwner> BufOwnerCleanup(
536 BufOwner.get());
537
538 for (auto &UF : unsaved_files) {
539 std::unique_ptr<llvm::MemoryBuffer> MB =
540 llvm::MemoryBuffer::getMemBufferCopy(InputData: getContents(UF), BufferName: UF.Filename);
541 CInvok->getPreprocessorOpts().addRemappedFile(From: UF.Filename, To: MB.get());
542 BufOwner->push_back(Elt: std::move(MB));
543 }
544
545 // Since libclang is primarily used by batch tools dealing with
546 // (often very broken) source code, where spell-checking can have a
547 // significant negative impact on performance (particularly when
548 // precompiled headers are involved), we disable it.
549 CInvok->getLangOpts().SpellChecking = false;
550
551 if (index_options & CXIndexOpt_SuppressWarnings)
552 CInvok->getDiagnosticOpts().IgnoreWarnings = true;
553
554 // Make sure to use the raw module format.
555 CInvok->getHeaderSearchOpts().ModuleFormat = std::string(
556 CXXIdx->getPCHContainerOperations()->getRawReader().getFormats().front());
557
558 auto Unit = ASTUnit::create(CI: CInvok, DiagOpts, Diags, CaptureDiagnostics,
559 /*UserFilesAreVolatile=*/true);
560 if (!Unit)
561 return CXError_InvalidArguments;
562
563 auto *UPtr = Unit.get();
564 std::unique_ptr<CXTUOwner> CXTU(
565 new CXTUOwner(MakeCXTranslationUnit(CIdx: CXXIdx, AU: std::move(Unit))));
566
567 // Recover resources if we crash before exiting this method.
568 llvm::CrashRecoveryContextCleanupRegistrar<CXTUOwner>
569 CXTUCleanup(CXTU.get());
570
571 // Enable the skip-parsed-bodies optimization only for C++; this may be
572 // revisited.
573 bool SkipBodies = (index_options & CXIndexOpt_SkipParsedBodiesInSession) &&
574 CInvok->getLangOpts().CPlusPlus;
575 if (SkipBodies)
576 CInvok->getFrontendOpts().SkipFunctionBodies = true;
577
578 auto DataConsumer =
579 std::make_shared<CXIndexDataConsumer>(args&: client_data, args&: CB, args&: index_options,
580 args: CXTU->getTU());
581 auto IndexAction = std::make_unique<IndexingFrontendAction>(
582 args&: DataConsumer, args: getIndexingOptionsFromCXOptions(index_options),
583 args: SkipBodies ? IdxSession->SkipBodyData.get() : nullptr);
584
585 // Recover resources if we crash before exiting this method.
586 llvm::CrashRecoveryContextCleanupRegistrar<FrontendAction>
587 IndexActionCleanup(IndexAction.get());
588
589 bool Persistent = requestedToGetTU;
590 bool OnlyLocalDecls = false;
591 bool PrecompilePreamble = false;
592 bool CreatePreambleOnFirstParse = false;
593 bool CacheCodeCompletionResults = false;
594 PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts();
595 PPOpts.AllowPCHWithCompilerErrors = true;
596
597 if (requestedToGetTU) {
598 OnlyLocalDecls = CXXIdx->getOnlyLocalDecls();
599 PrecompilePreamble = TU_options & CXTranslationUnit_PrecompiledPreamble;
600 CreatePreambleOnFirstParse =
601 TU_options & CXTranslationUnit_CreatePreambleOnFirstParse;
602 // FIXME: Add a flag for modules.
603 CacheCodeCompletionResults
604 = TU_options & CXTranslationUnit_CacheCompletionResults;
605 }
606
607 if (TU_options & CXTranslationUnit_DetailedPreprocessingRecord) {
608 PPOpts.DetailedRecord = true;
609 }
610
611 if (!requestedToGetTU && !CInvok->getLangOpts().Modules)
612 PPOpts.DetailedRecord = false;
613
614 // Unless the user specified that they want the preamble on the first parse
615 // set it up to be created on the first reparse. This makes the first parse
616 // faster, trading for a slower (first) reparse.
617 unsigned PrecompilePreambleAfterNParses =
618 !PrecompilePreamble ? 0 : 2 - CreatePreambleOnFirstParse;
619 DiagnosticErrorTrap DiagTrap(*Diags);
620 bool Success = ASTUnit::LoadFromCompilerInvocationAction(
621 CI: std::move(CInvok), PCHContainerOps: CXXIdx->getPCHContainerOperations(), DiagOpts, Diags,
622 Action: IndexAction.get(), Unit: UPtr, Persistent, ResourceFilesPath: CXXIdx->getClangResourcesPath(),
623 OnlyLocalDecls, CaptureDiagnostics, PrecompilePreambleAfterNParses,
624 CacheCodeCompletionResults, /*UserFilesAreVolatile=*/true);
625 if (DiagTrap.hasErrorOccurred() && CXXIdx->getDisplayDiagnostics())
626 printDiagsToStderr(Unit: UPtr);
627
628 if (isASTReadError(AU: UPtr))
629 return CXError_ASTReadError;
630
631 if (!Success)
632 return CXError_Failure;
633
634 if (out_TU)
635 *out_TU = CXTU->takeTU();
636
637 return CXError_Success;
638}
639
640//===----------------------------------------------------------------------===//
641// clang_indexTranslationUnit Implementation
642//===----------------------------------------------------------------------===//
643
644static void indexPreprocessingRecord(ASTUnit &Unit, CXIndexDataConsumer &IdxCtx) {
645 Preprocessor &PP = Unit.getPreprocessor();
646 if (!PP.getPreprocessingRecord())
647 return;
648
649 // FIXME: Only deserialize inclusion directives.
650
651 bool isModuleFile = Unit.isModuleFile();
652 for (PreprocessedEntity *PPE : Unit.getLocalPreprocessingEntities()) {
653 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(Val: PPE)) {
654 SourceLocation Loc = ID->getSourceRange().getBegin();
655 // Modules have synthetic main files as input, give an invalid location
656 // if the location points to such a file.
657 if (isModuleFile && Unit.isInMainFileID(Loc))
658 Loc = SourceLocation();
659 IdxCtx.ppIncludedFile(hashLoc: Loc, filename: ID->getFileName(),
660 File: ID->getFile(),
661 isImport: ID->getKind() == InclusionDirective::Import,
662 isAngled: !ID->wasInQuotes(), isModuleImport: ID->importedModule());
663 }
664 }
665}
666
667static CXErrorCode clang_indexTranslationUnit_Impl(
668 CXIndexAction idxAction, CXClientData client_data,
669 IndexerCallbacks *client_index_callbacks, unsigned index_callbacks_size,
670 unsigned index_options, CXTranslationUnit TU) {
671 // Check arguments.
672 if (isNotUsableTU(TU)) {
673 LOG_BAD_TU(TU);
674 return CXError_InvalidArguments;
675 }
676 if (!client_index_callbacks || index_callbacks_size == 0) {
677 return CXError_InvalidArguments;
678 }
679
680 CIndexer *CXXIdx = TU->CIdx;
681 if (CXXIdx->isOptEnabled(opt: CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
682 setThreadBackgroundPriority();
683
684 IndexerCallbacks CB;
685 memset(s: &CB, c: 0, n: sizeof(CB));
686 unsigned ClientCBSize = index_callbacks_size < sizeof(CB)
687 ? index_callbacks_size : sizeof(CB);
688 memcpy(dest: &CB, src: client_index_callbacks, n: ClientCBSize);
689
690 CXIndexDataConsumer DataConsumer(client_data, CB, index_options, TU);
691
692 ASTUnit *Unit = cxtu::getASTUnit(TU);
693 if (!Unit)
694 return CXError_Failure;
695
696 ASTUnit::ConcurrencyCheck Check(*Unit);
697
698 if (OptionalFileEntryRef PCHFile = Unit->getPCHFile())
699 DataConsumer.importedPCH(File: *PCHFile);
700
701 FileManager &FileMgr = Unit->getFileManager();
702
703 if (Unit->getOriginalSourceFileName().empty())
704 DataConsumer.enteredMainFile(File: std::nullopt);
705 else if (auto MainFile =
706 FileMgr.getFileRef(Filename: Unit->getOriginalSourceFileName()))
707 DataConsumer.enteredMainFile(File: *MainFile);
708 else
709 DataConsumer.enteredMainFile(File: std::nullopt);
710
711 DataConsumer.setASTContext(Unit->getASTContext());
712 DataConsumer.startedTranslationUnit();
713
714 indexPreprocessingRecord(Unit&: *Unit, IdxCtx&: DataConsumer);
715 indexASTUnit(Unit&: *Unit, DataConsumer, Opts: getIndexingOptionsFromCXOptions(index_options));
716 DataConsumer.indexDiagnostics();
717
718 return CXError_Success;
719}
720
721//===----------------------------------------------------------------------===//
722// libclang public APIs.
723//===----------------------------------------------------------------------===//
724
725int clang_index_isEntityObjCContainerKind(CXIdxEntityKind K) {
726 return CXIdxEntity_ObjCClass <= K && K <= CXIdxEntity_ObjCCategory;
727}
728
729const CXIdxObjCContainerDeclInfo *
730clang_index_getObjCContainerDeclInfo(const CXIdxDeclInfo *DInfo) {
731 if (!DInfo)
732 return nullptr;
733
734 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
735 if (const ObjCContainerDeclInfo *
736 ContInfo = dyn_cast<ObjCContainerDeclInfo>(Val: DI))
737 return &ContInfo->ObjCContDeclInfo;
738
739 return nullptr;
740}
741
742const CXIdxObjCInterfaceDeclInfo *
743clang_index_getObjCInterfaceDeclInfo(const CXIdxDeclInfo *DInfo) {
744 if (!DInfo)
745 return nullptr;
746
747 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
748 if (const ObjCInterfaceDeclInfo *
749 InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(Val: DI))
750 return &InterInfo->ObjCInterDeclInfo;
751
752 return nullptr;
753}
754
755const CXIdxObjCCategoryDeclInfo *
756clang_index_getObjCCategoryDeclInfo(const CXIdxDeclInfo *DInfo){
757 if (!DInfo)
758 return nullptr;
759
760 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
761 if (const ObjCCategoryDeclInfo *
762 CatInfo = dyn_cast<ObjCCategoryDeclInfo>(Val: DI))
763 return &CatInfo->ObjCCatDeclInfo;
764
765 return nullptr;
766}
767
768const CXIdxObjCProtocolRefListInfo *
769clang_index_getObjCProtocolRefListInfo(const CXIdxDeclInfo *DInfo) {
770 if (!DInfo)
771 return nullptr;
772
773 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
774
775 if (const ObjCInterfaceDeclInfo *
776 InterInfo = dyn_cast<ObjCInterfaceDeclInfo>(Val: DI))
777 return InterInfo->ObjCInterDeclInfo.protocols;
778
779 if (const ObjCProtocolDeclInfo *
780 ProtInfo = dyn_cast<ObjCProtocolDeclInfo>(Val: DI))
781 return &ProtInfo->ObjCProtoRefListInfo;
782
783 if (const ObjCCategoryDeclInfo *CatInfo = dyn_cast<ObjCCategoryDeclInfo>(Val: DI))
784 return CatInfo->ObjCCatDeclInfo.protocols;
785
786 return nullptr;
787}
788
789const CXIdxObjCPropertyDeclInfo *
790clang_index_getObjCPropertyDeclInfo(const CXIdxDeclInfo *DInfo) {
791 if (!DInfo)
792 return nullptr;
793
794 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
795 if (const ObjCPropertyDeclInfo *PropInfo = dyn_cast<ObjCPropertyDeclInfo>(Val: DI))
796 return &PropInfo->ObjCPropDeclInfo;
797
798 return nullptr;
799}
800
801const CXIdxIBOutletCollectionAttrInfo *
802clang_index_getIBOutletCollectionAttrInfo(const CXIdxAttrInfo *AInfo) {
803 if (!AInfo)
804 return nullptr;
805
806 const AttrInfo *DI = static_cast<const AttrInfo *>(AInfo);
807 if (const IBOutletCollectionInfo *
808 IBInfo = dyn_cast<IBOutletCollectionInfo>(Val: DI))
809 return &IBInfo->IBCollInfo;
810
811 return nullptr;
812}
813
814const CXIdxCXXClassDeclInfo *
815clang_index_getCXXClassDeclInfo(const CXIdxDeclInfo *DInfo) {
816 if (!DInfo)
817 return nullptr;
818
819 const DeclInfo *DI = static_cast<const DeclInfo *>(DInfo);
820 if (const CXXClassDeclInfo *ClassInfo = dyn_cast<CXXClassDeclInfo>(Val: DI))
821 return &ClassInfo->CXXClassInfo;
822
823 return nullptr;
824}
825
826CXIdxClientContainer
827clang_index_getClientContainer(const CXIdxContainerInfo *info) {
828 if (!info)
829 return nullptr;
830 const ContainerInfo *Container = static_cast<const ContainerInfo *>(info);
831 return Container->IndexCtx->getClientContainerForDC(DC: Container->DC);
832}
833
834void clang_index_setClientContainer(const CXIdxContainerInfo *info,
835 CXIdxClientContainer client) {
836 if (!info)
837 return;
838 const ContainerInfo *Container = static_cast<const ContainerInfo *>(info);
839 Container->IndexCtx->addContainerInMap(DC: Container->DC, container: client);
840}
841
842CXIdxClientEntity clang_index_getClientEntity(const CXIdxEntityInfo *info) {
843 if (!info)
844 return nullptr;
845 const EntityInfo *Entity = static_cast<const EntityInfo *>(info);
846 return Entity->IndexCtx->getClientEntity(Entity->Dcl);
847}
848
849void clang_index_setClientEntity(const CXIdxEntityInfo *info,
850 CXIdxClientEntity client) {
851 if (!info)
852 return;
853 const EntityInfo *Entity = static_cast<const EntityInfo *>(info);
854 Entity->IndexCtx->setClientEntity(Entity->Dcl, client);
855}
856
857CXIndexAction clang_IndexAction_create(CXIndex CIdx) {
858 return new IndexSessionData(CIdx);
859}
860
861void clang_IndexAction_dispose(CXIndexAction idxAction) {
862 if (idxAction)
863 delete static_cast<IndexSessionData *>(idxAction);
864}
865
866int clang_indexSourceFile(CXIndexAction idxAction,
867 CXClientData client_data,
868 IndexerCallbacks *index_callbacks,
869 unsigned index_callbacks_size,
870 unsigned index_options,
871 const char *source_filename,
872 const char * const *command_line_args,
873 int num_command_line_args,
874 struct CXUnsavedFile *unsaved_files,
875 unsigned num_unsaved_files,
876 CXTranslationUnit *out_TU,
877 unsigned TU_options) {
878 SmallVector<const char *, 4> Args;
879 Args.push_back(Elt: "clang");
880 Args.append(in_start: command_line_args, in_end: command_line_args + num_command_line_args);
881 return clang_indexSourceFileFullArgv(
882 idxAction, client_data, index_callbacks, index_callbacks_size,
883 index_options, source_filename, command_line_args: Args.data(), num_command_line_args: Args.size(), unsaved_files,
884 num_unsaved_files, out_TU, TU_options);
885}
886
887int clang_indexSourceFileFullArgv(
888 CXIndexAction idxAction, CXClientData client_data,
889 IndexerCallbacks *index_callbacks, unsigned index_callbacks_size,
890 unsigned index_options, const char *source_filename,
891 const char *const *command_line_args, int num_command_line_args,
892 struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files,
893 CXTranslationUnit *out_TU, unsigned TU_options) {
894 LOG_FUNC_SECTION {
895 *Log << source_filename << ": ";
896 for (int i = 0; i != num_command_line_args; ++i)
897 *Log << command_line_args[i] << " ";
898 }
899
900 if (num_unsaved_files && !unsaved_files)
901 return CXError_InvalidArguments;
902
903 CXErrorCode result = CXError_Failure;
904 auto IndexSourceFileImpl = [=, &result]() {
905 result = clang_indexSourceFile_Impl(
906 cxIdxAction: idxAction, client_data, client_index_callbacks: index_callbacks, index_callbacks_size,
907 index_options, source_filename, command_line_args,
908 num_command_line_args, unsaved_files: llvm::ArrayRef(unsaved_files, num_unsaved_files),
909 out_TU, TU_options);
910 };
911
912 llvm::CrashRecoveryContext CRC;
913
914 if (!RunSafely(CRC, Fn: IndexSourceFileImpl)) {
915 fprintf(stderr, format: "libclang: crash detected during indexing source file: {\n");
916 fprintf(stderr, format: " 'source_filename' : '%s'\n", source_filename);
917 fprintf(stderr, format: " 'command_line_args' : [");
918 for (int i = 0; i != num_command_line_args; ++i) {
919 if (i)
920 fprintf(stderr, format: ", ");
921 fprintf(stderr, format: "'%s'", command_line_args[i]);
922 }
923 fprintf(stderr, format: "],\n");
924 fprintf(stderr, format: " 'unsaved_files' : [");
925 for (unsigned i = 0; i != num_unsaved_files; ++i) {
926 if (i)
927 fprintf(stderr, format: ", ");
928 fprintf(stderr, format: "('%s', '...', %ld)", unsaved_files[i].Filename,
929 unsaved_files[i].Length);
930 }
931 fprintf(stderr, format: "],\n");
932 fprintf(stderr, format: " 'options' : %d,\n", TU_options);
933 fprintf(stderr, format: "}\n");
934
935 return 1;
936 } else if (getenv(name: "LIBCLANG_RESOURCE_USAGE")) {
937 if (out_TU)
938 PrintLibclangResourceUsage(TU: *out_TU);
939 }
940
941 return result;
942}
943
944int clang_indexTranslationUnit(CXIndexAction idxAction,
945 CXClientData client_data,
946 IndexerCallbacks *index_callbacks,
947 unsigned index_callbacks_size,
948 unsigned index_options,
949 CXTranslationUnit TU) {
950 LOG_FUNC_SECTION {
951 *Log << TU;
952 }
953
954 CXErrorCode result;
955 auto IndexTranslationUnitImpl = [=, &result]() {
956 result = clang_indexTranslationUnit_Impl(
957 idxAction, client_data, client_index_callbacks: index_callbacks, index_callbacks_size,
958 index_options, TU);
959 };
960
961 llvm::CrashRecoveryContext CRC;
962
963 if (!RunSafely(CRC, Fn: IndexTranslationUnitImpl)) {
964 fprintf(stderr, format: "libclang: crash detected during indexing TU\n");
965
966 return 1;
967 }
968
969 return result;
970}
971
972void clang_indexLoc_getFileLocation(CXIdxLoc location,
973 CXIdxClientFile *indexFile,
974 CXFile *file,
975 unsigned *line,
976 unsigned *column,
977 unsigned *offset) {
978 if (indexFile) *indexFile = nullptr;
979 if (file) *file = nullptr;
980 if (line) *line = 0;
981 if (column) *column = 0;
982 if (offset) *offset = 0;
983
984 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: location.int_data);
985 if (!location.ptr_data[0] || Loc.isInvalid())
986 return;
987
988 CXIndexDataConsumer &DataConsumer =
989 *static_cast<CXIndexDataConsumer*>(location.ptr_data[0]);
990 DataConsumer.translateLoc(Loc, indexFile, file, line, column, offset);
991}
992
993CXSourceLocation clang_indexLoc_getCXSourceLocation(CXIdxLoc location) {
994 SourceLocation Loc = SourceLocation::getFromRawEncoding(Encoding: location.int_data);
995 if (!location.ptr_data[0] || Loc.isInvalid())
996 return clang_getNullLocation();
997
998 CXIndexDataConsumer &DataConsumer =
999 *static_cast<CXIndexDataConsumer*>(location.ptr_data[0]);
1000 return cxloc::translateSourceLocation(Context&: DataConsumer.getASTContext(), Loc);
1001}
1002

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

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