1//===--- CompilerInstance.cpp ---------------------------------------------===//
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 "clang/Frontend/CompilerInstance.h"
10#include "clang/AST/ASTConsumer.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/AST/Decl.h"
13#include "clang/Basic/AtomicLineLogger.h"
14#include "clang/Basic/CharInfo.h"
15#include "clang/Basic/Diagnostic.h"
16#include "clang/Basic/DiagnosticFrontend.h"
17#include "clang/Basic/DiagnosticOptions.h"
18#include "clang/Basic/FileManager.h"
19#include "clang/Basic/LangStandard.h"
20#include "clang/Basic/SourceManager.h"
21#include "clang/Basic/Stack.h"
22#include "clang/Basic/TargetInfo.h"
23#include "clang/Basic/Version.h"
24#include "clang/Config/config.h"
25#include "clang/Frontend/ChainedDiagnosticConsumer.h"
26#include "clang/Frontend/FrontendAction.h"
27#include "clang/Frontend/FrontendActions.h"
28#include "clang/Frontend/FrontendPluginRegistry.h"
29#include "clang/Frontend/LogDiagnosticPrinter.h"
30#include "clang/Frontend/SARIFDiagnosticPrinter.h"
31#include "clang/Frontend/SerializedDiagnosticPrinter.h"
32#include "clang/Frontend/TextDiagnosticPrinter.h"
33#include "clang/Frontend/Utils.h"
34#include "clang/Frontend/VerifyDiagnosticConsumer.h"
35#include "clang/Lex/HeaderSearch.h"
36#include "clang/Lex/Preprocessor.h"
37#include "clang/Lex/PreprocessorOptions.h"
38#include "clang/Lex/TextEncoding.h"
39#include "clang/Sema/CodeCompleteConsumer.h"
40#include "clang/Sema/ParsedAttr.h"
41#include "clang/Sema/Sema.h"
42#include "clang/Serialization/ASTReader.h"
43#include "clang/Serialization/GlobalModuleIndex.h"
44#include "clang/Serialization/InMemoryModuleCache.h"
45#include "clang/Serialization/ModuleCache.h"
46#include "clang/Serialization/ModuleManager.h"
47#include "clang/Serialization/SerializationDiagnostic.h"
48#include "llvm/ADT/IntrusiveRefCntPtr.h"
49#include "llvm/ADT/STLExtras.h"
50#include "llvm/ADT/ScopeExit.h"
51#include "llvm/ADT/Statistic.h"
52#include "llvm/Config/llvm-config.h"
53#include "llvm/Plugins/PassPlugin.h"
54#include "llvm/Support/AdvisoryLock.h"
55#include "llvm/Support/BuryPointer.h"
56#include "llvm/Support/CrashRecoveryContext.h"
57#include "llvm/Support/Errc.h"
58#include "llvm/Support/FileSystem.h"
59#include "llvm/Support/MemoryBuffer.h"
60#include "llvm/Support/Path.h"
61#include "llvm/Support/Signals.h"
62#include "llvm/Support/SmallVectorMemoryBuffer.h"
63#include "llvm/Support/Threading.h"
64#include "llvm/Support/TimeProfiler.h"
65#include "llvm/Support/Timer.h"
66#include "llvm/Support/VirtualFileSystem.h"
67#include "llvm/Support/VirtualOutputBackends.h"
68#include "llvm/Support/VirtualOutputError.h"
69#include "llvm/Support/raw_ostream.h"
70#include "llvm/TargetParser/Host.h"
71#include <optional>
72#include <time.h>
73#include <utility>
74
75using namespace clang;
76
77CompilerInstance::CompilerInstance(
78 std::shared_ptr<CompilerInvocation> Invocation,
79 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
80 std::shared_ptr<ModuleCache> ModCache)
81 : ModuleLoader(/*BuildingModule=*/ModCache != nullptr),
82 Invocation(std::move(Invocation)),
83 ModCache(ModCache ? std::move(ModCache)
84 : createCrossProcessModuleCache()),
85 ThePCHContainerOperations(std::move(PCHContainerOps)) {
86 assert(this->Invocation && "Invocation must not be null");
87}
88
89CompilerInstance::~CompilerInstance() {
90 assert(OutputFiles.empty() && "Still output files in flight?");
91}
92
93bool CompilerInstance::shouldBuildGlobalModuleIndex() const {
94 return (BuildGlobalModuleIndex ||
95 (TheASTReader && TheASTReader->isGlobalIndexUnavailable() &&
96 getFrontendOpts().GenerateGlobalModuleIndex)) &&
97 !DisableGeneratingGlobalModuleIndex;
98}
99
100void CompilerInstance::setDiagnostics(
101 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Value) {
102 Diagnostics = std::move(Value);
103}
104
105void CompilerInstance::setVerboseOutputStream(raw_ostream &Value) {
106 OwnedVerboseOutputStream.reset();
107 VerboseOutputStream = &Value;
108}
109
110void CompilerInstance::setVerboseOutputStream(std::unique_ptr<raw_ostream> Value) {
111 OwnedVerboseOutputStream.swap(u&: Value);
112 VerboseOutputStream = OwnedVerboseOutputStream.get();
113}
114
115void CompilerInstance::setTarget(TargetInfo *Value) { Target = Value; }
116void CompilerInstance::setAuxTarget(TargetInfo *Value) { AuxTarget = Value; }
117
118bool CompilerInstance::createTarget() {
119 // Create the target instance.
120 setTarget(TargetInfo::CreateTargetInfo(Diags&: getDiagnostics(),
121 Opts&: getInvocation().getTargetOpts()));
122 if (!hasTarget())
123 return false;
124
125 if (getLangOpts().SYCLIsDevice && !getTarget().getTriple().isGPU()) {
126 getDiagnostics().Report(DiagID: diag::err_sycl_device_invalid_target)
127 << getTarget().getTriple().str();
128 return false;
129 }
130
131 // Check whether AuxTarget exists, if not, then create TargetInfo for the
132 // other side of CUDA/OpenMP/SYCL compilation.
133 if (!getAuxTarget() &&
134 (getLangOpts().CUDA || getLangOpts().isTargetDevice()) &&
135 !getFrontendOpts().AuxTriple.empty()) {
136 auto &TO = AuxTargetOpts = std::make_unique<TargetOptions>();
137 TO->Triple = llvm::Triple::normalize(Str: getFrontendOpts().AuxTriple);
138 if (getFrontendOpts().AuxTargetCPU)
139 TO->CPU = *getFrontendOpts().AuxTargetCPU;
140 if (getFrontendOpts().AuxTargetFeatures)
141 TO->FeaturesAsWritten = *getFrontendOpts().AuxTargetFeatures;
142 TO->HostTriple = getTarget().getTriple().str();
143 setAuxTarget(TargetInfo::CreateTargetInfo(Diags&: getDiagnostics(), Opts&: *TO));
144 }
145
146 if (!getTarget().hasStrictFP() && !getLangOpts().ExpStrictFP) {
147 if (getLangOpts().RoundingMath) {
148 getDiagnostics().Report(DiagID: diag::warn_fe_backend_unsupported_fp_rounding);
149 getLangOpts().RoundingMath = false;
150 }
151 auto FPExc = getLangOpts().getFPExceptionMode();
152 if (FPExc != LangOptions::FPE_Default && FPExc != LangOptions::FPE_Ignore) {
153 getDiagnostics().Report(DiagID: diag::warn_fe_backend_unsupported_fp_exceptions);
154 getLangOpts().setFPExceptionMode(LangOptions::FPE_Ignore);
155 }
156 // FIXME: can we disable FEnvAccess?
157 }
158
159 // We should do it here because target knows nothing about
160 // language options when it's being created.
161 if (getLangOpts().OpenCL &&
162 !getTarget().validateOpenCLTarget(Opts: getLangOpts(), Diags&: getDiagnostics()))
163 return false;
164
165 // Inform the target of the language options.
166 // FIXME: We shouldn't need to do this, the target should be immutable once
167 // created. This complexity should be lifted elsewhere.
168 getTarget().adjust(Diags&: getDiagnostics(), Opts&: getLangOpts(), Aux: getAuxTarget());
169
170 if (auto *Aux = getAuxTarget())
171 getTarget().setAuxTarget(Aux);
172
173 return true;
174}
175
176void CompilerInstance::setFileManager(IntrusiveRefCntPtr<FileManager> Value) {
177 assert(Value == nullptr ||
178 getVirtualFileSystemPtr() == Value->getVirtualFileSystemPtr());
179 FileMgr = std::move(Value);
180}
181
182void CompilerInstance::setSourceManager(
183 llvm::IntrusiveRefCntPtr<SourceManager> Value) {
184 SourceMgr = std::move(Value);
185}
186
187void CompilerInstance::setPreprocessor(std::shared_ptr<Preprocessor> Value) {
188 PP = std::move(Value);
189}
190
191IntrusiveRefCntPtr<ASTContext> CompilerInstance::getASTContextPtr() const {
192 assert(Context && "Compiler instance has no AST context!");
193 return Context;
194}
195
196void CompilerInstance::setASTContext(
197 llvm::IntrusiveRefCntPtr<ASTContext> Value) {
198 Context = std::move(Value);
199
200 if (Context && Consumer)
201 getASTConsumer().Initialize(Context&: getASTContext());
202}
203
204void CompilerInstance::setSema(Sema *S) {
205 TheSema.reset(p: S);
206}
207
208void CompilerInstance::setASTConsumer(std::unique_ptr<ASTConsumer> Value) {
209 Consumer = std::move(Value);
210
211 if (Context && Consumer)
212 getASTConsumer().Initialize(Context&: getASTContext());
213}
214
215void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
216 CompletionConsumer.reset(p: Value);
217}
218
219std::unique_ptr<Sema> CompilerInstance::takeSema() {
220 return std::move(TheSema);
221}
222
223IntrusiveRefCntPtr<ASTReader> CompilerInstance::getASTReader() const {
224 return TheASTReader;
225}
226void CompilerInstance::setASTReader(IntrusiveRefCntPtr<ASTReader> Reader) {
227 assert(ModCache.get() == &Reader->getModuleManager().getModuleCache() &&
228 "Expected ASTReader to use the same PCM cache");
229 TheASTReader = std::move(Reader);
230}
231
232std::shared_ptr<ModuleDependencyCollector>
233CompilerInstance::getModuleDepCollector() const {
234 return ModuleDepCollector;
235}
236
237void CompilerInstance::setModuleDepCollector(
238 std::shared_ptr<ModuleDependencyCollector> Collector) {
239 ModuleDepCollector = std::move(Collector);
240}
241
242static void collectHeaderMaps(const HeaderSearch &HS,
243 std::shared_ptr<ModuleDependencyCollector> MDC) {
244 SmallVector<std::string, 4> HeaderMapFileNames;
245 HS.getHeaderMapFileNames(Names&: HeaderMapFileNames);
246 for (auto &Name : HeaderMapFileNames)
247 MDC->addFile(Filename: Name);
248}
249
250static void collectIncludePCH(CompilerInstance &CI,
251 std::shared_ptr<ModuleDependencyCollector> MDC) {
252 const PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
253 if (PPOpts.ImplicitPCHInclude.empty())
254 return;
255
256 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
257 FileManager &FileMgr = CI.getFileManager();
258 auto PCHDir = FileMgr.getOptionalDirectoryRef(DirName: PCHInclude);
259 if (!PCHDir) {
260 MDC->addFile(Filename: PCHInclude);
261 return;
262 }
263
264 std::error_code EC;
265 SmallString<128> DirNative;
266 llvm::sys::path::native(path: PCHDir->getName(), result&: DirNative);
267 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
268 SimpleASTReaderListener Validator(CI.getPreprocessor());
269 for (llvm::vfs::directory_iterator Dir = FS.dir_begin(Dir: DirNative, EC), DirEnd;
270 Dir != DirEnd && !EC; Dir.increment(EC)) {
271 // Check whether this is an AST file. ASTReader::isAcceptableASTFile is not
272 // used here since we're not interested in validating the PCH at this time,
273 // but only to check whether this is a file containing an AST.
274 if (!ASTReader::readASTFileControlBlock(
275 Filename: Dir->path(), FileMgr, ModCache: CI.getModuleCache(),
276 PCHContainerRdr: CI.getPCHContainerReader(),
277 /*FindModuleFileExtensions=*/false, Listener&: Validator,
278 /*ValidateDiagnosticOptions=*/false))
279 MDC->addFile(Filename: Dir->path());
280 }
281}
282
283static void collectVFSEntries(CompilerInstance &CI,
284 std::shared_ptr<ModuleDependencyCollector> MDC) {
285 // Collect all VFS found.
286 SmallVector<llvm::vfs::YAMLVFSEntry, 16> VFSEntries;
287 CI.getVirtualFileSystem().visit(Callback: [&](llvm::vfs::FileSystem &VFS) {
288 if (auto *RedirectingVFS = dyn_cast<llvm::vfs::RedirectingFileSystem>(Val: &VFS))
289 llvm::vfs::collectVFSEntries(VFS&: *RedirectingVFS, CollectedEntries&: VFSEntries);
290 });
291
292 for (auto &E : VFSEntries)
293 MDC->addFile(Filename: E.VPath, FileDst: E.RPath);
294}
295
296void CompilerInstance::createVirtualFileSystem(
297 IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS, DiagnosticConsumer *DC) {
298 bool ShouldOwnClient = false;
299 if (!DC) {
300 DC = new DiagnosticConsumer;
301 ShouldOwnClient = true;
302 }
303
304 DiagnosticOptions DiagOpts;
305 DiagnosticsEngine Diags(DiagnosticIDs::create(), DiagOpts, DC,
306 ShouldOwnClient);
307
308 VFS = createVFSFromCompilerInvocation(CI: getInvocation(), Diags,
309 BaseFS: std::move(BaseFS));
310 // FIXME: Should this go into createVFSFromCompilerInvocation?
311 if (getFrontendOpts().ShowStats)
312 VFS =
313 llvm::makeIntrusiveRefCnt<llvm::vfs::TracingFileSystem>(A: std::move(VFS));
314}
315
316// Diagnostics
317static void SetUpDiagnosticLog(DiagnosticOptions &DiagOpts,
318 const CodeGenOptions *CodeGenOpts,
319 DiagnosticsEngine &Diags) {
320 std::error_code EC;
321 std::unique_ptr<raw_ostream> StreamOwner;
322 raw_ostream *OS = &llvm::errs();
323 if (DiagOpts.DiagnosticLogFile != "-") {
324 // Create the output stream.
325 auto FileOS = std::make_unique<llvm::raw_fd_ostream>(
326 args&: DiagOpts.DiagnosticLogFile, args&: EC,
327 args: llvm::sys::fs::OF_Append | llvm::sys::fs::OF_TextWithCRLF);
328 if (EC) {
329 Diags.Report(DiagID: diag::warn_fe_cc_log_diagnostics_failure)
330 << DiagOpts.DiagnosticLogFile << EC.message();
331 } else {
332 FileOS->SetUnbuffered();
333 OS = FileOS.get();
334 StreamOwner = std::move(FileOS);
335 }
336 }
337
338 // Chain in the diagnostic client which will log the diagnostics.
339 auto Logger = std::make_unique<LogDiagnosticPrinter>(args&: *OS, args&: DiagOpts,
340 args: std::move(StreamOwner));
341 if (CodeGenOpts)
342 Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
343 if (Diags.ownsClient()) {
344 Diags.setClient(
345 client: new ChainedDiagnosticConsumer(Diags.takeClient(), std::move(Logger)));
346 } else {
347 Diags.setClient(
348 client: new ChainedDiagnosticConsumer(Diags.getClient(), std::move(Logger)));
349 }
350}
351
352static void SetupSerializedDiagnostics(DiagnosticOptions &DiagOpts,
353 DiagnosticsEngine &Diags,
354 StringRef OutputFile) {
355 auto SerializedConsumer =
356 clang::serialized_diags::create(OutputFile, DiagOpts);
357
358 if (Diags.ownsClient()) {
359 Diags.setClient(client: new ChainedDiagnosticConsumer(
360 Diags.takeClient(), std::move(SerializedConsumer)));
361 } else {
362 Diags.setClient(client: new ChainedDiagnosticConsumer(
363 Diags.getClient(), std::move(SerializedConsumer)));
364 }
365}
366
367void CompilerInstance::createDiagnostics(DiagnosticConsumer *Client,
368 bool ShouldOwnClient) {
369 Diagnostics = createDiagnostics(VFS&: getVirtualFileSystem(), Opts&: getDiagnosticOpts(),
370 Client, ShouldOwnClient, CodeGenOpts: &getCodeGenOpts());
371}
372
373IntrusiveRefCntPtr<DiagnosticsEngine> CompilerInstance::createDiagnostics(
374 llvm::vfs::FileSystem &VFS, DiagnosticOptions &Opts,
375 DiagnosticConsumer *Client, bool ShouldOwnClient,
376 const CodeGenOptions *CodeGenOpts) {
377 auto Diags = llvm::makeIntrusiveRefCnt<DiagnosticsEngine>(
378 A: DiagnosticIDs::create(), A&: Opts);
379
380 // Create the diagnostic client for reporting errors or for
381 // implementing -verify.
382 if (Client) {
383 Diags->setClient(client: Client, ShouldOwnClient);
384 } else if (Opts.getFormat() == DiagnosticOptions::SARIF) {
385 Diags->setClient(client: new SARIFDiagnosticPrinter(llvm::errs(), Opts));
386 } else
387 Diags->setClient(client: new TextDiagnosticPrinter(llvm::errs(), Opts));
388
389 // Chain in -verify checker, if requested.
390 if (Opts.VerifyDiagnostics)
391 Diags->setClient(client: new VerifyDiagnosticConsumer(*Diags));
392
393 // Chain in -diagnostic-log-file dumper, if requested.
394 if (!Opts.DiagnosticLogFile.empty())
395 SetUpDiagnosticLog(DiagOpts&: Opts, CodeGenOpts, Diags&: *Diags);
396
397 if (!Opts.DiagnosticSerializationFile.empty())
398 SetupSerializedDiagnostics(DiagOpts&: Opts, Diags&: *Diags, OutputFile: Opts.DiagnosticSerializationFile);
399
400 // Configure our handling of diagnostics.
401 ProcessWarningOptions(Diags&: *Diags, Opts, VFS);
402
403 return Diags;
404}
405
406// File Manager
407
408void CompilerInstance::createFileManager() {
409 assert(VFS && "CompilerInstance needs a VFS for creating FileManager");
410 FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(A&: getFileSystemOpts(), A&: VFS);
411}
412
413// Source Manager
414
415void CompilerInstance::createSourceManager() {
416 assert(Diagnostics && "DiagnosticsEngine needed for creating SourceManager");
417 assert(FileMgr && "FileManager needed for creating SourceManager");
418 SourceMgr = llvm::makeIntrusiveRefCnt<SourceManager>(A&: getDiagnostics(),
419 A&: getFileManager());
420}
421
422// Initialize the remapping of files to alternative contents, e.g.,
423// those specified through other files.
424static void InitializeFileRemapping(DiagnosticsEngine &Diags,
425 SourceManager &SourceMgr,
426 FileManager &FileMgr,
427 const PreprocessorOptions &InitOpts) {
428 // Remap files in the source manager (with buffers).
429 for (const auto &RB : InitOpts.RemappedFileBuffers) {
430 // Create the file entry for the file that we're mapping from.
431 FileEntryRef FromFile =
432 FileMgr.getVirtualFileRef(Filename: RB.first, Size: RB.second->getBufferSize(), ModificationTime: 0);
433
434 // Override the contents of the "from" file with the contents of the
435 // "to" file. If the caller owns the buffers, then pass a MemoryBufferRef;
436 // otherwise, pass as a std::unique_ptr<MemoryBuffer> to transfer ownership
437 // to the SourceManager.
438 if (InitOpts.RetainRemappedFileBuffers)
439 SourceMgr.overrideFileContents(SourceFile: FromFile, Buffer: RB.second->getMemBufferRef());
440 else
441 SourceMgr.overrideFileContents(
442 SourceFile: FromFile, Buffer: std::unique_ptr<llvm::MemoryBuffer>(RB.second));
443 }
444
445 // Remap files in the source manager (with other files).
446 for (const auto &RF : InitOpts.RemappedFiles) {
447 // Find the file that we're mapping to.
448 OptionalFileEntryRef ToFile = FileMgr.getOptionalFileRef(Filename: RF.second);
449 if (!ToFile) {
450 Diags.Report(DiagID: diag::err_fe_remap_missing_to_file) << RF.first << RF.second;
451 continue;
452 }
453
454 // Create the file entry for the file that we're mapping from.
455 FileEntryRef FromFile =
456 FileMgr.getVirtualFileRef(Filename: RF.first, Size: ToFile->getSize(), ModificationTime: 0);
457
458 // Override the contents of the "from" file with the contents of
459 // the "to" file.
460 SourceMgr.overrideFileContents(SourceFile: FromFile, NewFile: *ToFile);
461 }
462
463 SourceMgr.setOverridenFilesKeepOriginalName(
464 InitOpts.RemappedFilesKeepOriginalName);
465}
466
467// Preprocessor
468
469void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) {
470 const PreprocessorOptions &PPOpts = getPreprocessorOpts();
471
472 // The AST reader holds a reference to the old preprocessor (if any).
473 TheASTReader.reset();
474
475 // Create the Preprocessor.
476 HeaderSearch *HeaderInfo =
477 new HeaderSearch(getHeaderSearchOpts(), getSourceManager(),
478 getDiagnostics(), getLangOpts(), &getTarget());
479 PP = std::make_shared<Preprocessor>(args&: Invocation->getPreprocessorOpts(),
480 args&: getDiagnostics(), args&: getLangOpts(),
481 args&: getSourceManager(), args&: *HeaderInfo, args&: *this,
482 /*IdentifierInfoLookup=*/args: nullptr,
483 /*OwnsHeaderSearch=*/args: true, args&: TUKind);
484 getTarget().adjust(Diags&: getDiagnostics(), Opts&: getLangOpts(), Aux: getAuxTarget());
485 PP->Initialize(Target: getTarget(), AuxTarget: getAuxTarget());
486
487 if (PPOpts.DetailedRecord)
488 PP->createPreprocessingRecord();
489
490 // Apply remappings to the source manager.
491 InitializeFileRemapping(Diags&: PP->getDiagnostics(), SourceMgr&: PP->getSourceManager(),
492 FileMgr&: PP->getFileManager(), InitOpts: PPOpts);
493
494 // Predefine macros and configure the preprocessor.
495 InitializePreprocessor(PP&: *PP, PPOpts, PCHContainerRdr: getPCHContainerReader(),
496 FEOpts: getFrontendOpts(), CodeGenOpts: getCodeGenOpts());
497
498 // Initialize the header search object. In CUDA compilations, we use the aux
499 // triple (the host triple) to initialize our header search, since we need to
500 // find the host headers in order to compile the CUDA code.
501 const llvm::Triple *HeaderSearchTriple = &PP->getTargetInfo().getTriple();
502 if (PP->getTargetInfo().getTriple().getOS() == llvm::Triple::CUDA &&
503 PP->getAuxTargetInfo())
504 HeaderSearchTriple = &PP->getAuxTargetInfo()->getTriple();
505
506 ApplyHeaderSearchOptions(HS&: PP->getHeaderSearchInfo(), HSOpts: getHeaderSearchOpts(),
507 Lang: PP->getLangOpts(), triple: *HeaderSearchTriple);
508
509 PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP);
510
511 if (PP->getLangOpts().Modules && PP->getLangOpts().ImplicitModules) {
512 // FIXME: We already might've computed the context hash and the specific
513 // module cache path in `FrontendAction::BeginSourceFile()` when turning
514 // "-include-pch <DIR>" into "-include-pch <DIR>/<FILE>". Reuse those here.
515 PP->getHeaderSearchInfo().initializeModuleCachePath(
516 ContextHash: getInvocation().computeContextHash());
517 }
518
519 // Handle generating dependencies, if requested.
520 const DependencyOutputOptions &DepOpts = getDependencyOutputOpts();
521 if (!DepOpts.OutputFile.empty())
522 addDependencyCollector(Listener: std::make_shared<DependencyFileGenerator>(args: DepOpts));
523 if (!DepOpts.DOTOutputFile.empty())
524 AttachDependencyGraphGen(PP&: *PP, OutputFile: DepOpts.DOTOutputFile,
525 SysRoot: getHeaderSearchOpts().Sysroot);
526
527 // If we don't have a collector, but we are collecting module dependencies,
528 // then we're the top level compiler instance and need to create one.
529 if (!ModuleDepCollector && !DepOpts.ModuleDependencyOutputDir.empty()) {
530 ModuleDepCollector = std::make_shared<ModuleDependencyCollector>(
531 args: DepOpts.ModuleDependencyOutputDir, args: getVirtualFileSystemPtr());
532 }
533
534 // If there is a module dep collector, register with other dep collectors
535 // and also (a) collect header maps and (b) TODO: input vfs overlay files.
536 if (ModuleDepCollector) {
537 addDependencyCollector(Listener: ModuleDepCollector);
538 collectHeaderMaps(HS: PP->getHeaderSearchInfo(), MDC: ModuleDepCollector);
539 collectIncludePCH(CI&: *this, MDC: ModuleDepCollector);
540 collectVFSEntries(CI&: *this, MDC: ModuleDepCollector);
541 }
542
543 // Modules need an output manager.
544 if (!hasOutputManager())
545 createOutputManager();
546
547 for (auto &Listener : DependencyCollectors)
548 Listener->attachToPreprocessor(PP&: *PP);
549
550 // Handle generating header include information, if requested.
551 if (DepOpts.ShowHeaderIncludes)
552 AttachHeaderIncludeGen(PP&: *PP, DepOpts);
553 if (!DepOpts.HeaderIncludeOutputFile.empty()) {
554 StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
555 if (OutputPath == "-")
556 OutputPath = "";
557 AttachHeaderIncludeGen(PP&: *PP, DepOpts,
558 /*ShowAllHeaders=*/true, OutputPath,
559 /*ShowDepth=*/false);
560 }
561
562 if (DepOpts.ShowIncludesDest != ShowIncludesDestination::None) {
563 AttachHeaderIncludeGen(PP&: *PP, DepOpts,
564 /*ShowAllHeaders=*/true, /*OutputPath=*/"",
565 /*ShowDepth=*/true, /*MSStyle=*/true);
566 }
567
568 if (GetDependencyDirectives)
569 PP->setDependencyDirectivesGetter(*GetDependencyDirectives);
570
571 if (auto EC = TextEncoding::setConvertersFromOptions(
572 TE&: PP->getTextEncoding(), Opts: getLangOpts(), TInfo&: getTarget()))
573 PP->getDiagnostics().Report(DiagID: clang::diag::err_fe_text_encoding_config)
574 << PP->getTextEncoding().getLiteralEncoding();
575}
576
577// ASTContext
578
579void CompilerInstance::createASTContext() {
580 Preprocessor &PP = getPreprocessor();
581 auto Context = llvm::makeIntrusiveRefCnt<ASTContext>(
582 A&: getLangOpts(), A&: PP.getSourceManager(), A&: PP.getIdentifierTable(),
583 A&: PP.getSelectorTable(), A&: PP.getBuiltinInfo(), A: PP.TUKind);
584 Context->InitBuiltinTypes(Target: getTarget(), AuxTarget: getAuxTarget());
585 setASTContext(std::move(Context));
586}
587
588// ExternalASTSource
589
590namespace {
591// Helper to recursively read the module names for all modules we're adding.
592// We mark these as known and redirect any attempt to load that module to
593// the files we were handed.
594struct ReadModuleNames : ASTReaderListener {
595 Preprocessor &PP;
596 llvm::SmallVector<std::string, 8> LoadedModules;
597
598 ReadModuleNames(Preprocessor &PP) : PP(PP) {}
599
600 void ReadModuleName(StringRef ModuleName) override {
601 // Keep the module name as a string for now. It's not safe to create a new
602 // IdentifierInfo from an ASTReader callback.
603 LoadedModules.push_back(Elt: ModuleName.str());
604 }
605
606 void registerAll() {
607 ModuleMap &MM = PP.getHeaderSearchInfo().getModuleMap();
608 for (const std::string &LoadedModule : LoadedModules)
609 MM.cacheModuleLoad(II: *PP.getIdentifierInfo(Name: LoadedModule),
610 M: MM.findOrLoadModule(Name: LoadedModule));
611 LoadedModules.clear();
612 }
613
614 void markAllUnavailable() {
615 for (const std::string &LoadedModule : LoadedModules) {
616 if (Module *M = PP.getHeaderSearchInfo().getModuleMap().findOrLoadModule(
617 Name: LoadedModule)) {
618 M->HasIncompatibleModuleFile = true;
619
620 // Mark module as available if the only reason it was unavailable
621 // was missing headers.
622 SmallVector<Module *, 2> Stack;
623 Stack.push_back(Elt: M);
624 while (!Stack.empty()) {
625 Module *Current = Stack.pop_back_val();
626 if (Current->IsUnimportable) continue;
627 Current->IsAvailable = true;
628 auto SubmodulesRange = Current->submodules();
629 llvm::append_range(C&: Stack, R&: SubmodulesRange);
630 }
631 }
632 }
633 LoadedModules.clear();
634 }
635};
636} // namespace
637
638void CompilerInstance::createPCHExternalASTSource(
639 StringRef Path, DisableValidationForModuleKind DisableValidation,
640 bool AllowPCHWithCompilerErrors, void *DeserializationListener,
641 bool OwnDeserializationListener) {
642 bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
643 TheASTReader = createPCHExternalASTSource(
644 Path, Sysroot: getHeaderSearchOpts().Sysroot, DisableValidation,
645 AllowPCHWithCompilerErrors, PP&: getPreprocessor(), ModCache&: getModuleCache(),
646 Context&: getASTContext(), PCHContainerRdr: getPCHContainerReader(), CodeGenOpts: getCodeGenOpts(),
647 Extensions: getFrontendOpts().ModuleFileExtensions, DependencyCollectors,
648 DeserializationListener, OwnDeserializationListener, Preamble,
649 UseGlobalModuleIndex: getFrontendOpts().UseGlobalModuleIndex);
650}
651
652IntrusiveRefCntPtr<ASTReader> CompilerInstance::createPCHExternalASTSource(
653 StringRef Path, StringRef Sysroot,
654 DisableValidationForModuleKind DisableValidation,
655 bool AllowPCHWithCompilerErrors, Preprocessor &PP, ModuleCache &ModCache,
656 ASTContext &Context, const PCHContainerReader &PCHContainerRdr,
657 const CodeGenOptions &CodeGenOpts,
658 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
659 ArrayRef<std::shared_ptr<DependencyCollector>> DependencyCollectors,
660 void *DeserializationListener, bool OwnDeserializationListener,
661 bool Preamble, bool UseGlobalModuleIndex) {
662 const HeaderSearchOptions &HSOpts =
663 PP.getHeaderSearchInfo().getHeaderSearchOpts();
664
665 auto Reader = llvm::makeIntrusiveRefCnt<ASTReader>(
666 A&: PP, A&: ModCache, A: &Context, A: PCHContainerRdr, A: CodeGenOpts, A&: Extensions,
667 A: Sysroot.empty() ? "" : Sysroot.data(), A&: DisableValidation,
668 A&: AllowPCHWithCompilerErrors, /*AllowConfigurationMismatch*/ A: false,
669 A: HSOpts.ModulesValidateSystemHeaders,
670 A: HSOpts.ModulesForceValidateUserHeaders,
671 A: HSOpts.ValidateASTInputFilesContent, A&: UseGlobalModuleIndex);
672
673 // We need the external source to be set up before we read the AST, because
674 // eagerly-deserialized declarations may use it.
675 Context.setExternalSource(Reader);
676
677 Reader->setDeserializationListener(
678 Listener: static_cast<ASTDeserializationListener *>(DeserializationListener),
679 /*TakeOwnership=*/OwnDeserializationListener);
680
681 for (auto &Listener : DependencyCollectors)
682 Listener->attachToASTReader(R&: *Reader);
683
684 auto Listener = std::make_unique<ReadModuleNames>(args&: PP);
685 auto &ListenerRef = *Listener;
686 ASTReader::ListenerScope ReadModuleNamesListener(*Reader,
687 std::move(Listener));
688
689 switch (Reader->ReadAST(FileName: ModuleFileName::makeExplicit(Name: Path),
690 Type: Preamble ? serialization::MK_Preamble
691 : serialization::MK_PCH,
692 ImportLoc: SourceLocation(), ClientLoadCapabilities: ASTReader::ARR_None)) {
693 case ASTReader::Success:
694 // Set the predefines buffer as suggested by the PCH reader. Typically, the
695 // predefines buffer will be empty.
696 PP.setPredefines(Reader->getSuggestedPredefines());
697 ListenerRef.registerAll();
698 return Reader;
699
700 case ASTReader::Failure:
701 // Unrecoverable failure: don't even try to process the input file.
702 break;
703
704 case ASTReader::Missing:
705 case ASTReader::OutOfDate:
706 case ASTReader::VersionMismatch:
707 case ASTReader::ConfigurationMismatch:
708 case ASTReader::HadErrors:
709 // No suitable PCH file could be found. Return an error.
710 break;
711 }
712
713 ListenerRef.markAllUnavailable();
714 Context.setExternalSource(nullptr);
715 return nullptr;
716}
717
718// Code Completion
719
720static bool EnableCodeCompletion(Preprocessor &PP,
721 StringRef Filename,
722 unsigned Line,
723 unsigned Column) {
724 // Tell the source manager to chop off the given file at a specific
725 // line and column.
726 auto Entry = PP.getFileManager().getOptionalFileRef(Filename);
727 if (!Entry) {
728 PP.getDiagnostics().Report(DiagID: diag::err_fe_invalid_code_complete_file)
729 << Filename;
730 return true;
731 }
732
733 // Truncate the named file at the given line/column.
734 PP.SetCodeCompletionPoint(File: *Entry, Line, Column);
735 return false;
736}
737
738void CompilerInstance::createCodeCompletionConsumer() {
739 const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
740 if (!CompletionConsumer) {
741 setCodeCompletionConsumer(createCodeCompletionConsumer(
742 PP&: getPreprocessor(), Filename: Loc.FileName, Line: Loc.Line, Column: Loc.Column,
743 Opts: getFrontendOpts().CodeCompleteOpts, OS&: llvm::outs()));
744 return;
745 } else if (EnableCodeCompletion(PP&: getPreprocessor(), Filename: Loc.FileName,
746 Line: Loc.Line, Column: Loc.Column)) {
747 setCodeCompletionConsumer(nullptr);
748 return;
749 }
750}
751
752void CompilerInstance::createFrontendTimer() {
753 timerGroup.reset(p: new llvm::TimerGroup("clang", "Clang time report"));
754 FrontendTimer.reset(p: new llvm::Timer("frontend", "Front end", *timerGroup));
755}
756
757CodeCompleteConsumer *
758CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
759 StringRef Filename,
760 unsigned Line,
761 unsigned Column,
762 const CodeCompleteOptions &Opts,
763 raw_ostream &OS) {
764 if (EnableCodeCompletion(PP, Filename, Line, Column))
765 return nullptr;
766
767 // Set up the creation routine for code-completion.
768 return new PrintingCodeCompleteConsumer(Opts, OS);
769}
770
771void CompilerInstance::createSema(TranslationUnitKind TUKind,
772 CodeCompleteConsumer *CompletionConsumer) {
773 TheSema.reset(p: new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
774 TUKind, CompletionConsumer));
775
776 // Set up API notes.
777 TheSema->APINotes.setSwiftVersion(getAPINotesOpts().SwiftVersion);
778
779 // Attach the external sema source if there is any.
780 if (ExternalSemaSrc) {
781 TheSema->addExternalSource(E: ExternalSemaSrc);
782 ExternalSemaSrc->InitializeSema(S&: *TheSema);
783 }
784
785 // If we're building a module and are supposed to load API notes,
786 // notify the API notes manager.
787 if (auto *currentModule = getPreprocessor().getCurrentModule()) {
788 (void)TheSema->APINotes.loadCurrentModuleAPINotes(
789 M: currentModule, LookInModule: getLangOpts().APINotesModules,
790 SearchPaths: getAPINotesOpts().ModuleSearchPaths);
791 }
792}
793
794// Output Files
795
796void CompilerInstance::clearOutputFiles(bool EraseFiles) {
797 // The ASTConsumer can own streams that write to the output files.
798 assert(!hasASTConsumer() && "ASTConsumer should be reset");
799 if (!EraseFiles) {
800 for (auto &O : OutputFiles)
801 llvm::handleAllErrors(
802 E: O.keep(),
803 Handlers: [&](const llvm::vfs::TempFileOutputError &E) {
804 getDiagnostics().Report(DiagID: diag::err_unable_to_rename_temp)
805 << E.getTempPath() << E.getOutputPath()
806 << E.convertToErrorCode().message();
807 },
808 Handlers: [&](const llvm::vfs::OutputError &E) {
809 getDiagnostics().Report(DiagID: diag::err_fe_unable_to_open_output)
810 << E.getOutputPath() << E.convertToErrorCode().message();
811 },
812 Handlers: [&](const llvm::ErrorInfoBase &EIB) { // Handle any remaining error
813 getDiagnostics().Report(DiagID: diag::err_fe_unable_to_open_output)
814 << O.getPath() << EIB.message();
815 });
816 }
817 OutputFiles.clear();
818 if (DeleteBuiltModules) {
819 for (auto &Module : BuiltModules)
820 llvm::sys::fs::remove(path: Module.second);
821 BuiltModules.clear();
822 }
823}
824
825std::unique_ptr<raw_pwrite_stream> CompilerInstance::createDefaultOutputFile(
826 bool Binary, StringRef InFile, StringRef Extension, bool RemoveFileOnSignal,
827 bool CreateMissingDirectories, bool ForceUseTemporary,
828 bool SetOnlyIfDifferent) {
829 StringRef OutputPath = getFrontendOpts().OutputFile;
830 std::optional<SmallString<128>> PathStorage;
831 if (OutputPath.empty()) {
832 if (InFile == "-" || Extension.empty()) {
833 OutputPath = "-";
834 } else {
835 PathStorage.emplace(args&: InFile);
836 llvm::sys::path::replace_extension(path&: *PathStorage, extension: Extension);
837 OutputPath = *PathStorage;
838 }
839 }
840
841 return createOutputFile(OutputPath, Binary, RemoveFileOnSignal,
842 UseTemporary: getFrontendOpts().UseTemporary || ForceUseTemporary,
843 CreateMissingDirectories, SetOnlyIfDifferent);
844}
845
846std::unique_ptr<raw_pwrite_stream> CompilerInstance::createNullOutputFile() {
847 return std::make_unique<llvm::raw_null_ostream>();
848}
849
850// Output Manager
851
852void CompilerInstance::setOutputManager(
853 IntrusiveRefCntPtr<llvm::vfs::OutputBackend> NewOutputs) {
854 assert(!OutputMgr && "Already has an output manager");
855 OutputMgr = std::move(NewOutputs);
856}
857
858void CompilerInstance::createOutputManager() {
859 assert(!OutputMgr && "Already has an output manager");
860 OutputMgr = llvm::makeIntrusiveRefCnt<llvm::vfs::OnDiskOutputBackend>();
861}
862
863llvm::vfs::OutputBackend &CompilerInstance::getOutputManager() {
864 assert(OutputMgr);
865 return *OutputMgr;
866}
867
868llvm::vfs::OutputBackend &CompilerInstance::getOrCreateOutputManager() {
869 if (!hasOutputManager())
870 createOutputManager();
871 return getOutputManager();
872}
873
874std::unique_ptr<raw_pwrite_stream> CompilerInstance::createOutputFile(
875 StringRef OutputPath, bool Binary, bool RemoveFileOnSignal,
876 bool UseTemporary, bool CreateMissingDirectories, bool SetOnlyIfDifferent) {
877 Expected<std::unique_ptr<raw_pwrite_stream>> OS =
878 createOutputFileImpl(OutputPath, Binary, RemoveFileOnSignal, UseTemporary,
879 CreateMissingDirectories, SetOnlyIfDifferent);
880 if (OS)
881 return std::move(*OS);
882 getDiagnostics().Report(DiagID: diag::err_fe_unable_to_open_output)
883 << OutputPath << errorToErrorCode(Err: OS.takeError()).message();
884 return nullptr;
885}
886
887Expected<std::unique_ptr<llvm::raw_pwrite_stream>>
888CompilerInstance::createOutputFileImpl(StringRef OutputPath, bool Binary,
889 bool RemoveFileOnSignal,
890 bool UseTemporary,
891 bool CreateMissingDirectories,
892 bool SetOnlyIfDifferent) {
893 assert((!CreateMissingDirectories || UseTemporary) &&
894 "CreateMissingDirectories is only allowed when using temporary files");
895
896 // If '-working-directory' was passed, the output filename should be
897 // relative to that.
898 std::optional<SmallString<128>> AbsPath;
899 if (OutputPath != "-" && !llvm::sys::path::is_absolute(path: OutputPath)) {
900 assert(hasFileManager() &&
901 "File Manager is required to fix up relative path.\n");
902
903 AbsPath.emplace(args&: OutputPath);
904 FileManager::fixupRelativePath(FileSystemOpts: getFileSystemOpts(), Path&: *AbsPath);
905 OutputPath = *AbsPath;
906 }
907
908 using namespace llvm::vfs;
909 Expected<OutputFile> O = getOrCreateOutputManager().createFile(
910 Path: OutputPath,
911 Config: OutputConfig()
912 .setTextWithCRLF(!Binary)
913 .setDiscardOnSignal(RemoveFileOnSignal)
914 .setAtomicWrite(UseTemporary)
915 .setImplyCreateDirectories(UseTemporary && CreateMissingDirectories)
916 .setOnlyIfDifferent(SetOnlyIfDifferent));
917 if (!O)
918 return O.takeError();
919
920 O->discardOnDestroy(Handler: [](llvm::Error E) { consumeError(Err: std::move(E)); });
921 OutputFiles.push_back(x: std::move(*O));
922 return OutputFiles.back().createProxy();
923}
924
925// Initialization Utilities
926
927bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input){
928 return InitializeSourceManager(Input, Diags&: getDiagnostics(), FileMgr&: getFileManager(),
929 SourceMgr&: getSourceManager());
930}
931
932// static
933bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input,
934 DiagnosticsEngine &Diags,
935 FileManager &FileMgr,
936 SourceManager &SourceMgr) {
937 SrcMgr::CharacteristicKind Kind =
938 Input.getKind().getFormat() == InputKind::ModuleMap
939 ? Input.isSystem() ? SrcMgr::C_System_ModuleMap
940 : SrcMgr::C_User_ModuleMap
941 : Input.isSystem() ? SrcMgr::C_System : SrcMgr::C_User;
942
943 if (Input.isBuffer()) {
944 SourceMgr.setMainFileID(SourceMgr.createFileID(Buffer: Input.getBuffer(), FileCharacter: Kind));
945 assert(SourceMgr.getMainFileID().isValid() &&
946 "Couldn't establish MainFileID!");
947 return true;
948 }
949
950 StringRef InputFile = Input.getFile();
951
952 // Figure out where to get and map in the main file.
953 auto FileOrErr = InputFile == "-"
954 ? FileMgr.getSTDIN()
955 : FileMgr.getFileRef(Filename: InputFile, /*OpenFile=*/true);
956 if (!FileOrErr) {
957 auto EC = llvm::errorToErrorCode(Err: FileOrErr.takeError());
958 if (InputFile != "-")
959 Diags.Report(DiagID: diag::err_fe_error_reading) << InputFile << EC.message();
960 else
961 Diags.Report(DiagID: diag::err_fe_error_reading_stdin) << EC.message();
962 return false;
963 }
964
965 SourceMgr.setMainFileID(
966 SourceMgr.createFileID(SourceFile: *FileOrErr, IncludePos: SourceLocation(), FileCharacter: Kind));
967
968 assert(SourceMgr.getMainFileID().isValid() &&
969 "Couldn't establish MainFileID!");
970 return true;
971}
972
973// High-Level Operations
974
975void CompilerInstance::PrepareForExecution() {
976 // Set up the frontend timer for -ftime-report. BackendConsumer uses
977 // getTimerGroup() and getFrontendTimer() when TimePasses is set. In the
978 // cc1 driver path this was done in cc1_main before calling
979 // ExecuteCompilerInvocation; we consolidate it here so that all tools
980 // (cc1, clang-repl, libclang, etc.) get consistent behavior.
981 if (getCodeGenOpts().TimePasses && !FrontendTimer) {
982 createFrontendTimer();
983 getFrontendTimer().startTimer();
984 }
985
986 // FIXME: Consider consolidating additional per-instance setup here:
987 // - llvm::timeTraceProfilerInitialize) when TimeTracePath is set.
988 // - Plugin loading (LoadRequestedPlugins) and -mllvm argument processing.
989}
990
991bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
992 assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
993 assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
994 assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
995
996 llvm::TimeTraceScope TimeScope("ExecuteCompiler");
997
998 PrepareForExecution();
999
1000 // Mark this point as the bottom of the stack if we don't have somewhere
1001 // better. We generally expect frontend actions to be invoked with (nearly)
1002 // DesiredStackSpace available.
1003 noteBottomOfStack();
1004
1005 raw_ostream &OS = getVerboseOutputStream();
1006
1007 if (!Act.PrepareToExecute(CI&: *this))
1008 return false;
1009
1010 if (!createTarget())
1011 return false;
1012
1013 // rewriter project will change target built-in bool type from its default.
1014 if (getFrontendOpts().ProgramAction == frontend::RewriteObjC)
1015 getTarget().noSignedCharForObjCBool();
1016
1017 // Validate/process some options.
1018 if (getHeaderSearchOpts().Verbose)
1019 OS << "clang -cc1 version " CLANG_VERSION_STRING << " based upon LLVM "
1020 << LLVM_VERSION_STRING << " default target "
1021 << llvm::sys::getDefaultTargetTriple() << "\n";
1022
1023 if (getFrontendOpts().ShowStats || !getFrontendOpts().StatsFile.empty())
1024 llvm::EnableStatistics(DoPrintOnExit: false);
1025
1026 // Sort vectors containing toc data and no toc data variables to facilitate
1027 // binary search later.
1028 llvm::sort(C&: getCodeGenOpts().TocDataVarsUserSpecified);
1029 llvm::sort(C&: getCodeGenOpts().NoTocDataVars);
1030
1031 for (const FrontendInputFile &FIF : getFrontendOpts().Inputs) {
1032 // Reset the ID tables if we are reusing the SourceManager and parsing
1033 // regular files.
1034 if (hasSourceManager() && !Act.isModelParsingAction())
1035 getSourceManager().clearIDTables();
1036
1037 ModuleImportResults.clear();
1038
1039 if (Act.BeginSourceFile(CI&: *this, Input: FIF)) {
1040 if (llvm::Error Err = Act.Execute()) {
1041 consumeError(Err: std::move(Err)); // FIXME this drops errors on the floor.
1042 }
1043 Act.EndSourceFile();
1044 }
1045 }
1046
1047 printDiagnosticStats();
1048
1049 if (getFrontendOpts().ShowStats) {
1050 if (hasFileManager()) {
1051 getFileManager().PrintStats();
1052 OS << '\n';
1053 }
1054 llvm::PrintStatistics(OS);
1055 }
1056 StringRef StatsFile = getFrontendOpts().StatsFile;
1057 if (!StatsFile.empty()) {
1058 llvm::sys::fs::OpenFlags FileFlags = llvm::sys::fs::OF_TextWithCRLF;
1059 if (getFrontendOpts().AppendStats)
1060 FileFlags |= llvm::sys::fs::OF_Append;
1061 std::error_code EC;
1062 auto StatS =
1063 std::make_unique<llvm::raw_fd_ostream>(args&: StatsFile, args&: EC, args&: FileFlags);
1064 if (EC) {
1065 getDiagnostics().Report(DiagID: diag::warn_fe_unable_to_open_stats_file)
1066 << StatsFile << EC.message();
1067 } else {
1068 llvm::PrintStatisticsJSON(OS&: *StatS);
1069 }
1070 }
1071
1072 return !getDiagnostics().getClient()->getNumErrors();
1073}
1074
1075void CompilerInstance::printDiagnosticStats() {
1076 if (!getDiagnosticOpts().ShowCarets)
1077 return;
1078
1079 raw_ostream &OS = getVerboseOutputStream();
1080
1081 // We can have multiple diagnostics sharing one diagnostic client.
1082 // Get the total number of warnings/errors from the client.
1083 unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
1084 unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
1085
1086 if (NumWarnings)
1087 OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
1088 if (NumWarnings && NumErrors)
1089 OS << " and ";
1090 if (NumErrors)
1091 OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
1092 if (NumWarnings || NumErrors) {
1093 OS << " generated";
1094 if (getLangOpts().CUDA) {
1095 if (!getLangOpts().CUDAIsDevice) {
1096 OS << " when compiling for host";
1097 } else {
1098 OS << " when compiling for "
1099 << (!getTargetOpts().CPU.empty() ? getTargetOpts().CPU
1100 : getTarget().getTriple().str());
1101 }
1102 }
1103 OS << ".\n";
1104 }
1105}
1106
1107void CompilerInstance::LoadRequestedPlugins() {
1108 // Load any requested plugins.
1109 for (const std::string &Path : getFrontendOpts().Plugins) {
1110 std::string Error;
1111 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Filename: Path.c_str(), ErrMsg: &Error))
1112 getDiagnostics().Report(DiagID: diag::err_fe_unable_to_load_plugin)
1113 << Path << Error;
1114 }
1115
1116 // Load and store pass plugins for the back-end.
1117 for (const std::string &Path : getCodeGenOpts().PassPlugins) {
1118 if (auto PassPlugin = llvm::PassPlugin::load(Filename: Path)) {
1119 PassPlugins.emplace_back(args: std::make_unique<llvm::PassPlugin>(args&: *PassPlugin));
1120 } else {
1121 getDiagnostics().Report(DiagID: diag::err_fe_unable_to_load_plugin)
1122 << Path << toString(E: PassPlugin.takeError());
1123 }
1124 }
1125
1126 // Check if any of the loaded plugins replaces the main AST action
1127 for (const FrontendPluginRegistry::entry &Plugin :
1128 FrontendPluginRegistry::entries()) {
1129 std::unique_ptr<PluginASTAction> P(Plugin.instantiate());
1130 if (P->getActionType() == PluginASTAction::ReplaceAction) {
1131 getFrontendOpts().ProgramAction = clang::frontend::PluginAction;
1132 getFrontendOpts().ActionName = Plugin.getName().str();
1133 break;
1134 }
1135 }
1136}
1137
1138bool CompilerInstance::parseLLVMArgs(raw_ostream *Errs) {
1139 const std::vector<std::string> &LLVMArgs = getFrontendOpts().LLVMArgs;
1140 if (LLVMArgs.empty())
1141 return true;
1142 SmallVector<const char *> Args = {"clang (LLVM option parsing)"};
1143 for (const std::string &Arg : LLVMArgs)
1144 Args.push_back(Elt: Arg.c_str());
1145 return llvm::cl::parseCommandLineOptions(Context&: LibraryOptions, Args,
1146 /*Overview=*/"", Errs,
1147 VFS: &getVirtualFileSystem());
1148}
1149
1150/// Determine the appropriate source input kind based on language
1151/// options.
1152static Language getLanguageFromOptions(const LangOptions &LangOpts) {
1153 if (LangOpts.OpenCL)
1154 return Language::OpenCL;
1155 if (LangOpts.CUDA)
1156 return Language::CUDA;
1157 if (LangOpts.ObjC)
1158 return LangOpts.CPlusPlus ? Language::ObjCXX : Language::ObjC;
1159 return LangOpts.CPlusPlus ? Language::CXX : Language::C;
1160}
1161
1162std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
1163 SourceLocation ImportLoc, StringRef ModuleName, FrontendInputFile Input,
1164 StringRef OriginalModuleMapFile, StringRef ModuleFileName,
1165 ArrayRef<std::string> DirectoryDependencies,
1166 std::optional<ThreadSafeCloneConfig> ThreadSafeConfig) {
1167 // Construct a compiler invocation for creating this module.
1168 auto Invocation = std::make_shared<CompilerInvocation>(args&: getInvocation());
1169
1170 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
1171
1172 // For any options that aren't intended to affect how a module is built,
1173 // reset them to their default values.
1174 Invocation->resetNonModularOptions();
1175
1176 // Remove any macro definitions that are explicitly ignored by the module.
1177 // They aren't supposed to affect how the module is built anyway.
1178 HeaderSearchOptions &HSOpts = Invocation->getHeaderSearchOpts();
1179 llvm::erase_if(C&: PPOpts.Macros,
1180 P: [&HSOpts](const std::pair<std::string, bool> &def) {
1181 StringRef MacroDef = def.first;
1182 return HSOpts.ModulesIgnoreMacros.contains(
1183 key: llvm::CachedHashString(MacroDef.split(Separator: '=').first));
1184 });
1185 HSOpts.ModulesIgnoreMacros.clear();
1186
1187 // Remove any search paths that are explicitly ignored by the module.
1188 // They aren't supposed to affect how the module is built anyway.
1189 if (!HSOpts.ModulesIgnoreSearchPaths.empty())
1190 llvm::erase_if(C&: HSOpts.UserEntries,
1191 P: [&HSOpts](const HeaderSearchOptions::Entry &E) {
1192 return HSOpts.ModulesIgnoreSearchPaths.contains(
1193 key: llvm::CachedHashString(E.Path));
1194 });
1195 HSOpts.ModulesIgnoreSearchPaths.clear();
1196
1197 // If the original compiler invocation had -fmodule-name, pass it through.
1198 Invocation->getLangOpts().ModuleName =
1199 getInvocation().getLangOpts().ModuleName;
1200
1201 // Note the name of the module we're building.
1202 Invocation->getLangOpts().CurrentModule = std::string(ModuleName);
1203
1204 // If there is a module map file, build the module using the module map.
1205 // Set up the inputs/outputs so that we build the module from its umbrella
1206 // header.
1207 FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
1208 FrontendOpts.OutputFile = ModuleFileName.str();
1209 FrontendOpts.DisableFree = false;
1210 FrontendOpts.GenerateGlobalModuleIndex = false;
1211 FrontendOpts.BuildingImplicitModule = true;
1212 FrontendOpts.OriginalModuleMap = std::string(OriginalModuleMapFile);
1213 // Force implicitly-built modules to hash the content of the module file.
1214 HSOpts.ModulesHashContent = true;
1215 FrontendOpts.Inputs = {std::move(Input)};
1216
1217 // Don't free the remapped file buffers; they are owned by our caller.
1218 PPOpts.RetainRemappedFileBuffers = true;
1219
1220 DiagnosticOptions &DiagOpts = Invocation->getDiagnosticOpts();
1221
1222 DiagOpts.VerifyDiagnostics = 0;
1223 assert(getInvocation().computeContextHash() ==
1224 Invocation->computeContextHash() &&
1225 "Module hash mismatch!");
1226
1227 std::shared_ptr<ModuleCache> ModCache;
1228 if (ThreadSafeConfig) {
1229 ModCache = ThreadSafeConfig->getModuleCache();
1230 } else {
1231 ModCache = this->ModCache;
1232 }
1233
1234 // Construct a compiler instance that will be used to create the module.
1235 auto InstancePtr = std::make_unique<CompilerInstance>(
1236 args: std::move(Invocation), args: getPCHContainerOperations(), args: std::move(ModCache));
1237 auto &Instance = *InstancePtr;
1238
1239 auto &Inv = Instance.getInvocation();
1240
1241 if (ThreadSafeConfig) {
1242 Instance.setVirtualFileSystem(ThreadSafeConfig->getVFS());
1243 Instance.createFileManager();
1244 } else if (FrontendOpts.ModulesShareFileManager) {
1245 Instance.setVirtualFileSystem(getVirtualFileSystemPtr());
1246 Instance.setFileManager(getFileManagerPtr());
1247 } else {
1248 Instance.setVirtualFileSystem(getVirtualFileSystemPtr());
1249 Instance.createFileManager();
1250 }
1251
1252 if (ThreadSafeConfig) {
1253 Instance.createDiagnostics(Client: &ThreadSafeConfig->getDiagConsumer(),
1254 /*ShouldOwnClient=*/false);
1255 } else {
1256 Instance.createDiagnostics(
1257 Client: new ForwardingDiagnosticConsumer(getDiagnosticClient()),
1258 /*ShouldOwnClient=*/true);
1259 }
1260 if (llvm::is_contained(Range&: DiagOpts.SystemHeaderWarningsModules, Element: ModuleName))
1261 Instance.getDiagnostics().setSuppressSystemWarnings(false);
1262
1263 Instance.createSourceManager();
1264 SourceManager &SourceMgr = Instance.getSourceManager();
1265
1266 if (ThreadSafeConfig) {
1267 // Detecting cycles in the module graph is responsibility of the client.
1268 } else {
1269 // Note that this module is part of the module build stack, so that we
1270 // can detect cycles in the module graph.
1271 SourceMgr.setModuleBuildStack(getSourceManager().getModuleBuildStack());
1272 SourceMgr.pushModuleBuildStack(
1273 moduleName: ModuleName, importLoc: FullSourceLoc(ImportLoc, getSourceManager()));
1274 }
1275
1276 // Make a copy for the new instance.
1277 Instance.FailedModules = FailedModules;
1278
1279 // An inferred framework is built from module map text printed by this
1280 // instance rather than by repeating the inference, so directories the
1281 // inference enumerated (its Frameworks subdirectory) are only known here.
1282 Instance.setInheritedDirectoryDependencies(DirectoryDependencies);
1283
1284 // Pass along the GenModuleActionWrapper callback.
1285 Instance.setGenModuleActionWrapper(getGenModuleActionWrapper());
1286
1287 if (GetDependencyDirectives)
1288 Instance.GetDependencyDirectives =
1289 GetDependencyDirectives->cloneFor(FileMgr&: Instance.getFileManager());
1290
1291 if (ThreadSafeConfig) {
1292 Instance.setModuleDepCollector(ThreadSafeConfig->getModuleDepCollector());
1293 } else {
1294 // If we're collecting module dependencies, we need to share a collector
1295 // between all of the module CompilerInstances. Other than that, we don't
1296 // want to produce any dependency output from the module build.
1297 Instance.setModuleDepCollector(getModuleDepCollector());
1298 }
1299 Inv.getDependencyOutputOpts() = DependencyOutputOptions();
1300
1301 return InstancePtr;
1302}
1303
1304namespace {
1305class PrettyStackTraceBuildModule : public llvm::PrettyStackTraceEntry {
1306 StringRef ModuleName;
1307 StringRef ModuleFileName;
1308
1309public:
1310 PrettyStackTraceBuildModule(StringRef ModuleName, StringRef ModuleFileName)
1311 : ModuleName(ModuleName), ModuleFileName(ModuleFileName) {}
1312 void print(raw_ostream &OS) const override {
1313 OS << "Building module '" << ModuleName << "' as '" << ModuleFileName
1314 << "'\n";
1315 }
1316};
1317} // namespace
1318
1319std::unique_ptr<llvm::MemoryBuffer>
1320CompilerInstance::compileModule(SourceLocation ImportLoc, StringRef ModuleName,
1321 StringRef ModuleFileName,
1322 CompilerInstance &Instance) {
1323 PrettyStackTraceBuildModule CrashInfo(ModuleName, ModuleFileName);
1324 llvm::TimeTraceScope TimeScope("Module Compile", ModuleName);
1325
1326 // Never compile a module that's already finalized - this would cause the
1327 // existing module to be freed, causing crashes if it is later referenced
1328 if (getModuleCache().getInMemoryModuleCache().isPCMFinal(Filename: ModuleFileName)) {
1329 getDiagnostics().Report(Loc: ImportLoc, DiagID: diag::err_module_rebuild_finalized)
1330 << ModuleName;
1331 return nullptr;
1332 }
1333
1334 getDiagnostics().Report(Loc: ImportLoc, DiagID: diag::remark_module_build)
1335 << ModuleName << ModuleFileName;
1336
1337 SmallString<0> Buffer;
1338
1339 // Execute the action to actually build the module in-place. Use a separate
1340 // thread so that we get a stack large enough.
1341 uint64_t ParentTID = llvm::get_threadid();
1342 bool Crashed = !llvm::CrashRecoveryContext().RunSafelyOnNewStack(
1343 [&]() {
1344 getModuleCache().getLogger().log()
1345 << "module_compile_thread: parent=" << ParentTID
1346 << " pcm_compile: " << ModuleFileName;
1347
1348 auto OS = std::make_unique<llvm::raw_svector_ostream>(args&: Buffer);
1349
1350 std::unique_ptr<FrontendAction> Action =
1351 std::make_unique<GenerateModuleFromModuleMapAction>(args: std::move(OS));
1352
1353 if (auto WrapGenModuleAction = Instance.getGenModuleActionWrapper())
1354 Action = WrapGenModuleAction(Instance.getFrontendOpts(),
1355 std::move(Action));
1356
1357 Instance.ExecuteAction(Act&: *Action);
1358 },
1359 RequestedStackSize: DesiredStackSize);
1360
1361 getDiagnostics().Report(Loc: ImportLoc, DiagID: diag::remark_module_build_done)
1362 << ModuleName;
1363
1364 // Propagate the statistics to the parent FileManager.
1365 if (!getFrontendOpts().ModulesShareFileManager)
1366 getFileManager().AddStats(Other: Instance.getFileManager());
1367
1368 // Propagate the failed modules to the parent instance.
1369 FailedModules = std::move(Instance.FailedModules);
1370
1371 if (Crashed) {
1372 // Clear the ASTConsumer if it hasn't been already, in case it owns streams
1373 // that must be closed before clearing output files.
1374 Instance.setSema(nullptr);
1375 Instance.setASTConsumer(nullptr);
1376
1377 // Delete any remaining temporary files related to Instance.
1378 Instance.clearOutputFiles(/*EraseFiles=*/true);
1379 }
1380
1381 // We've rebuilt a module. If we're allowed to generate or update the global
1382 // module index, record that fact in the importing compiler instance.
1383 if (getFrontendOpts().GenerateGlobalModuleIndex) {
1384 setBuildGlobalModuleIndex(true);
1385 }
1386
1387 if (Crashed)
1388 return nullptr;
1389
1390 // Unless \p AllowPCMWithCompilerErrors is set, return 'failure' if errors
1391 // occurred.
1392 if (Instance.getDiagnostics().hasErrorOccurred() &&
1393 !Instance.getFrontendOpts().AllowPCMWithCompilerErrors)
1394 return nullptr;
1395
1396 // PCMs don't need to be null-terminated. Since the buffer typically has no
1397 // spare capacity, adding the terminator would reallocate and copy it.
1398 return std::make_unique<llvm::SmallVectorMemoryBuffer>(
1399 args: std::move(Buffer), args&: Instance.getFrontendOpts().OutputFile,
1400 /*RequiresNullTerminator=*/args: false);
1401}
1402
1403static OptionalFileEntryRef getPublicModuleMap(FileEntryRef File,
1404 FileManager &FileMgr) {
1405 StringRef Filename = llvm::sys::path::filename(path: File.getName());
1406 SmallString<128> PublicFilename(File.getDir().getName());
1407 if (Filename == "module_private.map")
1408 llvm::sys::path::append(path&: PublicFilename, a: "module.map");
1409 else if (Filename == "module.private.modulemap")
1410 llvm::sys::path::append(path&: PublicFilename, a: "module.modulemap");
1411 else
1412 return std::nullopt;
1413 return FileMgr.getOptionalFileRef(Filename: PublicFilename);
1414}
1415
1416std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompile(
1417 SourceLocation ImportLoc, const Module *Module, StringRef ModuleFileName,
1418 std::optional<ThreadSafeCloneConfig> ThreadSafeConfig) {
1419 StringRef ModuleName = Module->getTopLevelModuleName();
1420
1421 InputKind IK(getLanguageFromOptions(LangOpts: getLangOpts()), InputKind::ModuleMap);
1422
1423 // Get or create the module map that we'll use to build this module.
1424 ModuleMap &ModMap = getPreprocessor().getHeaderSearchInfo().getModuleMap();
1425 SourceManager &SourceMgr = getSourceManager();
1426
1427 if (FileID ModuleMapFID = ModMap.getContainingModuleMapFileID(Module);
1428 ModuleMapFID.isValid()) {
1429 // We want to use the top-level module map. If we don't, the compiling
1430 // instance may think the containing module map is a top-level one, while
1431 // the importing instance knows it's included from a parent module map via
1432 // the extern directive. This mismatch could bite us later.
1433 SourceLocation Loc = SourceMgr.getIncludeLoc(FID: ModuleMapFID);
1434 while (Loc.isValid() && isModuleMap(CK: SourceMgr.getFileCharacteristic(Loc))) {
1435 ModuleMapFID = SourceMgr.getFileID(SpellingLoc: Loc);
1436 Loc = SourceMgr.getIncludeLoc(FID: ModuleMapFID);
1437 }
1438
1439 OptionalFileEntryRef ModuleMapFile =
1440 SourceMgr.getFileEntryRefForID(FID: ModuleMapFID);
1441 assert(ModuleMapFile && "Top-level module map with no FileID");
1442
1443 // Canonicalize compilation to start with the public module map. This is
1444 // vital for submodules declarations in the private module maps to be
1445 // correctly parsed when depending on a top level module in the public one.
1446 if (OptionalFileEntryRef PublicMMFile =
1447 getPublicModuleMap(File: *ModuleMapFile, FileMgr&: getFileManager()))
1448 ModuleMapFile = PublicMMFile;
1449
1450 StringRef ModuleMapFilePath = ModuleMapFile->getNameAsRequested();
1451
1452 // Use the systemness of the module map as parsed instead of using the
1453 // IsSystem attribute of the module. If the module has [system] but the
1454 // module map is not in a system path, then this would incorrectly parse
1455 // any other modules in that module map as system too.
1456 const SrcMgr::SLocEntry &SLoc = SourceMgr.getSLocEntry(FID: ModuleMapFID);
1457 bool IsSystem = isSystem(CK: SLoc.getFile().getFileCharacteristic());
1458
1459 // Use the module map where this module resides.
1460 return cloneForModuleCompileImpl(
1461 ImportLoc, ModuleName,
1462 Input: FrontendInputFile(ModuleMapFilePath, IK, IsSystem),
1463 OriginalModuleMapFile: ModMap.getModuleMapFileForUniquing(M: Module)->getName(), ModuleFileName,
1464 DirectoryDependencies: Module->getDirectoryDependencies(), ThreadSafeConfig: std::move(ThreadSafeConfig));
1465 }
1466
1467 // FIXME: We only need to fake up an input file here as a way of
1468 // transporting the module's directory to the module map parser. We should
1469 // be able to do that more directly, and parse from a memory buffer without
1470 // inventing this file.
1471 SmallString<128> FakeModuleMapFile(Module->Directory->getName());
1472 llvm::sys::path::append(path&: FakeModuleMapFile, a: "__inferred_module.map");
1473
1474 std::string InferredModuleMapContent;
1475 llvm::raw_string_ostream OS(InferredModuleMapContent);
1476 Module->print(OS);
1477
1478 auto Instance = cloneForModuleCompileImpl(
1479 ImportLoc, ModuleName,
1480 Input: FrontendInputFile(FakeModuleMapFile, IK, +Module->IsSystem),
1481 OriginalModuleMapFile: ModMap.getModuleMapFileForUniquing(M: Module)->getName(), ModuleFileName,
1482 DirectoryDependencies: Module->getDirectoryDependencies(), ThreadSafeConfig: std::move(ThreadSafeConfig));
1483
1484 std::unique_ptr<llvm::MemoryBuffer> ModuleMapBuffer =
1485 llvm::MemoryBuffer::getMemBufferCopy(InputData: InferredModuleMapContent);
1486 FileEntryRef ModuleMapFile = Instance->getFileManager().getVirtualFileRef(
1487 Filename: FakeModuleMapFile, Size: InferredModuleMapContent.size(), ModificationTime: 0);
1488 Instance->getSourceManager().overrideFileContents(SourceFile: ModuleMapFile,
1489 Buffer: std::move(ModuleMapBuffer));
1490
1491 return Instance;
1492}
1493
1494/// Read the AST right after compiling the module.
1495/// Returns true on success, false on failure.
1496static bool readASTAfterCompileModule(CompilerInstance &ImportingInstance,
1497 SourceLocation ImportLoc,
1498 SourceLocation ModuleNameLoc,
1499 Module *Module,
1500 ModuleFileName ModuleFileName,
1501 bool *OutOfDate, bool *Missing) {
1502 DiagnosticsEngine &Diags = ImportingInstance.getDiagnostics();
1503
1504 unsigned ModuleLoadCapabilities = ASTReader::ARR_Missing;
1505 if (OutOfDate)
1506 ModuleLoadCapabilities |= ASTReader::ARR_OutOfDate;
1507
1508 // Try to read the module file, now that we've compiled it.
1509 ASTReader::ASTReadResult ReadResult =
1510 ImportingInstance.getASTReader()->ReadAST(
1511 FileName: ModuleFileName, Type: serialization::MK_ImplicitModule, ImportLoc,
1512 ClientLoadCapabilities: ModuleLoadCapabilities);
1513 if (ReadResult == ASTReader::Success)
1514 return true;
1515
1516 // The caller wants to handle out-of-date failures.
1517 if (OutOfDate && ReadResult == ASTReader::OutOfDate) {
1518 *OutOfDate = true;
1519 return false;
1520 }
1521
1522 // The caller wants to handle missing module files.
1523 if (Missing && ReadResult == ASTReader::Missing) {
1524 *Missing = true;
1525 return false;
1526 }
1527
1528 // The ASTReader didn't diagnose the error, so conservatively report it.
1529 if (ReadResult == ASTReader::Missing || !Diags.hasErrorOccurred())
1530 Diags.Report(Loc: ModuleNameLoc, DiagID: diag::err_module_not_built)
1531 << Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
1532
1533 return false;
1534}
1535
1536namespace {
1537/// A buffer exposing a sub-range of an owned buffer.
1538class SubMemoryBuffer : public llvm::MemoryBuffer {
1539 std::unique_ptr<llvm::MemoryBuffer> Underlying;
1540
1541public:
1542 SubMemoryBuffer(std::unique_ptr<llvm::MemoryBuffer> Underlying,
1543 StringRef Range)
1544 : Underlying(std::move(Underlying)) {
1545 init(BufStart: Range.begin(), BufEnd: Range.end(), /*RequiresNullTerminator=*/false);
1546 }
1547
1548 StringRef getBufferIdentifier() const override {
1549 return Underlying->getBufferIdentifier();
1550 }
1551
1552 BufferKind getBufferKind() const override {
1553 return Underlying->getBufferKind();
1554 }
1555};
1556} // namespace
1557
1558/// Compile a module in a separate compiler instance.
1559/// Returns true on success, false on failure.
1560static bool compileModuleImpl(CompilerInstance &ImportingInstance,
1561 SourceLocation ImportLoc,
1562 SourceLocation ModuleNameLoc, Module *Module,
1563 ModuleFileName ModuleFileName) {
1564 std::unique_ptr<llvm::MemoryBuffer> Buffer;
1565
1566 {
1567 auto Instance = ImportingInstance.cloneForModuleCompile(
1568 ImportLoc: ModuleNameLoc, Module, ModuleFileName);
1569
1570 Buffer = ImportingInstance.compileModule(ImportLoc: ModuleNameLoc,
1571 ModuleName: Module->getTopLevelModuleName(),
1572 ModuleFileName, Instance&: *Instance);
1573
1574 if (!Buffer) {
1575 ImportingInstance.getDiagnostics().Report(Loc: ModuleNameLoc,
1576 DiagID: diag::err_module_not_built)
1577 << Module->Name << SourceRange(ImportLoc, ModuleNameLoc);
1578 return false;
1579 }
1580 }
1581
1582 off_t Size;
1583 time_t ModTime;
1584 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Written =
1585 ImportingInstance.getModuleCache().write(
1586 Path: ModuleFileName, Buffer: std::move(Buffer), Size, ModTime);
1587 if (!Written) {
1588 ImportingInstance.getDiagnostics().Report(Loc: ModuleNameLoc,
1589 DiagID: diag::err_module_not_written)
1590 << Module->Name << ModuleFileName << Written.getError().message()
1591 << SourceRange(ImportLoc, ModuleNameLoc);
1592 return false;
1593 }
1594 Buffer = std::move(*Written);
1595
1596 // The module is built successfully, we can update its timestamp now.
1597 if (ImportingInstance.getPreprocessor()
1598 .getHeaderSearchInfo()
1599 .getHeaderSearchOpts()
1600 .ModulesValidateOncePerBuildSession) {
1601 ImportingInstance.getModuleCache().updateModuleTimestamp(ModuleFilename: ModuleFileName);
1602 }
1603
1604 // Readers use the module cache's view of the written PCM. Extracting the AST
1605 // file (which may be wrapped in an object file) isn't strictly necessary, but
1606 // it's more efficient to do so now rather than repeatedly in the readers.
1607 const PCHContainerReader &Rdr = ImportingInstance.getPCHContainerReader();
1608 StringRef ExtractedBuffer = Rdr.ExtractPCH(Buffer: *Buffer);
1609 Buffer =
1610 std::make_unique<SubMemoryBuffer>(args: std::move(Buffer), args&: ExtractedBuffer);
1611 ImportingInstance.getModuleCache().getInMemoryModuleCache().addBuiltPCM(
1612 Filename: ModuleFileName, Buffer: std::move(Buffer), Size, ModTime);
1613
1614 return true;
1615}
1616
1617/// The result of `compileModuleBehindLockOrRead()`.
1618enum class CompileOrReadResult : uint8_t {
1619 /// We failed to compile the module.
1620 FailedToCompile,
1621 /// We successfully compiled the module and we still need to read it.
1622 Compiled,
1623 /// We failed to read the module file compiled by another instance.
1624 FailedToRead,
1625 /// We read a module file compiled by another instance.
1626 Read,
1627};
1628
1629/// Attempt to compile the module in a separate compiler instance behind a lock
1630/// (to avoid building the same module in multiple compiler instances), or read
1631/// the AST produced by another compiler instance.
1632static CompileOrReadResult
1633compileModuleBehindLockOrRead(CompilerInstance &ImportingInstance,
1634 SourceLocation ImportLoc,
1635 SourceLocation ModuleNameLoc, Module *Module,
1636 ModuleFileName ModuleFileName) {
1637 DiagnosticsEngine &Diags = ImportingInstance.getDiagnostics();
1638
1639 Diags.Report(Loc: ModuleNameLoc, DiagID: diag::remark_module_lock)
1640 << ModuleFileName << Module->Name;
1641
1642 auto &ModuleCache = ImportingInstance.getModuleCache();
1643
1644 while (true) {
1645 auto Lock = ModuleCache.getLock(ModuleFilename: ModuleFileName);
1646 bool Owned;
1647 if (llvm::Error Err = Lock->tryLock().moveInto(Value&: Owned)) {
1648 // ModuleCache takes care of correctness and locks are only necessary for
1649 // performance. Fallback to building the module in case of any lock
1650 // related errors.
1651 Diags.Report(Loc: ModuleNameLoc, DiagID: diag::remark_module_lock_failure)
1652 << Module->Name << toString(E: std::move(Err));
1653 if (!compileModuleImpl(ImportingInstance, ImportLoc, ModuleNameLoc,
1654 Module, ModuleFileName))
1655 return CompileOrReadResult::FailedToCompile;
1656 return CompileOrReadResult::Compiled;
1657 }
1658 if (Owned) {
1659 // We're responsible for building the module ourselves.
1660 if (!compileModuleImpl(ImportingInstance, ImportLoc, ModuleNameLoc,
1661 Module, ModuleFileName))
1662 return CompileOrReadResult::FailedToCompile;
1663 return CompileOrReadResult::Compiled;
1664 }
1665
1666 // Someone else is responsible for building the module. Wait for them to
1667 // finish.
1668 unsigned Timeout =
1669 ImportingInstance.getFrontendOpts().ImplicitModulesLockTimeoutSeconds;
1670 switch (Lock->waitForUnlockFor(MaxSeconds: std::chrono::seconds(Timeout))) {
1671 case llvm::WaitForUnlockResult::Success:
1672 break; // The interesting case.
1673 case llvm::WaitForUnlockResult::OwnerDied:
1674 continue; // try again to get the lock.
1675 case llvm::WaitForUnlockResult::Timeout:
1676 // Since the InMemoryModuleCache takes care of correctness, we try waiting
1677 // for someone else to complete the build so that it does not happen
1678 // twice. In case of timeout, try to build it ourselves again.
1679 Diags.Report(Loc: ModuleNameLoc, DiagID: diag::remark_module_lock_timeout)
1680 << Module->Name;
1681 // Clear the lock file so that future invocations can make progress.
1682 Lock->unsafeUnlock();
1683 continue;
1684 }
1685
1686 // Read the module that was just written by someone else.
1687 bool OutOfDate = false;
1688 bool Missing = false;
1689 if (readASTAfterCompileModule(ImportingInstance, ImportLoc, ModuleNameLoc,
1690 Module, ModuleFileName, OutOfDate: &OutOfDate, Missing: &Missing))
1691 return CompileOrReadResult::Read;
1692 if (!OutOfDate && !Missing)
1693 return CompileOrReadResult::FailedToRead;
1694
1695 // The module may be missing or out of date in the presence of file system
1696 // races. It may also be out of date if one of its imports depends on header
1697 // search paths that are not consistent with this ImportingInstance.
1698 // Try again...
1699 }
1700}
1701
1702/// Compile a module in a separate compiler instance and read the AST,
1703/// returning true if the module compiles without errors, potentially using a
1704/// lock manager to avoid building the same module in multiple compiler
1705/// instances.
1706static bool compileModuleAndReadAST(CompilerInstance &ImportingInstance,
1707 SourceLocation ImportLoc,
1708 SourceLocation ModuleNameLoc,
1709 Module *Module,
1710 ModuleFileName ModuleFileName) {
1711 if (ImportingInstance.getInvocation()
1712 .getFrontendOpts()
1713 .BuildingImplicitModuleUsesLock) {
1714 switch (compileModuleBehindLockOrRead(
1715 ImportingInstance, ImportLoc, ModuleNameLoc, Module, ModuleFileName)) {
1716 case CompileOrReadResult::FailedToRead:
1717 case CompileOrReadResult::FailedToCompile:
1718 return false;
1719 case CompileOrReadResult::Read:
1720 return true;
1721 case CompileOrReadResult::Compiled:
1722 // We successfully compiled the module under a lock. Let's read it from
1723 // the in-memory module cache now.
1724 break;
1725 }
1726 } else {
1727 if (!compileModuleImpl(ImportingInstance, ImportLoc, ModuleNameLoc, Module,
1728 ModuleFileName))
1729 return false;
1730 }
1731
1732 return readASTAfterCompileModule(ImportingInstance, ImportLoc, ModuleNameLoc,
1733 Module, ModuleFileName,
1734 /*OutOfDate=*/nullptr, /*Missing=*/nullptr);
1735}
1736
1737/// Diagnose differences between the current definition of the given
1738/// configuration macro and the definition provided on the command line.
1739static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro,
1740 Module *Mod, SourceLocation ImportLoc) {
1741 IdentifierInfo *Id = PP.getIdentifierInfo(Name: ConfigMacro);
1742 SourceManager &SourceMgr = PP.getSourceManager();
1743
1744 // If this identifier has never had a macro definition, then it could
1745 // not have changed.
1746 if (!Id->hadMacroDefinition())
1747 return;
1748 auto *LatestLocalMD = PP.getLocalMacroDirectiveHistory(II: Id);
1749
1750 // Find the macro definition from the command line.
1751 MacroInfo *CmdLineDefinition = nullptr;
1752 for (auto *MD = LatestLocalMD; MD; MD = MD->getPrevious()) {
1753 SourceLocation MDLoc = MD->getLocation();
1754 FileID FID = SourceMgr.getFileID(SpellingLoc: MDLoc);
1755 if (FID.isInvalid())
1756 continue;
1757 // We only care about the predefines buffer, or if the macro is defined
1758 // over the command line transitively through a PCH.
1759 if (FID != PP.getPredefinesFileID() &&
1760 !SourceMgr.isWrittenInCommandLineFile(Loc: MDLoc))
1761 continue;
1762 if (auto *DMD = dyn_cast<DefMacroDirective>(Val: MD))
1763 CmdLineDefinition = DMD->getMacroInfo();
1764 break;
1765 }
1766
1767 auto *CurrentDefinition = PP.getMacroInfo(II: Id);
1768 if (CurrentDefinition == CmdLineDefinition) {
1769 // Macro matches. Nothing to do.
1770 } else if (!CurrentDefinition) {
1771 // This macro was defined on the command line, then #undef'd later.
1772 // Complain.
1773 PP.Diag(Loc: ImportLoc, DiagID: diag::warn_module_config_macro_undef)
1774 << true << ConfigMacro << Mod->getFullModuleName();
1775 auto LatestDef = LatestLocalMD->getDefinition();
1776 assert(LatestDef.isUndefined() &&
1777 "predefined macro went away with no #undef?");
1778 PP.Diag(Loc: LatestDef.getUndefLocation(), DiagID: diag::note_module_def_undef_here)
1779 << true;
1780 return;
1781 } else if (!CmdLineDefinition) {
1782 // There was no definition for this macro in the command line,
1783 // but there was a local definition. Complain.
1784 PP.Diag(Loc: ImportLoc, DiagID: diag::warn_module_config_macro_undef)
1785 << false << ConfigMacro << Mod->getFullModuleName();
1786 PP.Diag(Loc: CurrentDefinition->getDefinitionLoc(),
1787 DiagID: diag::note_module_def_undef_here)
1788 << false;
1789 } else if (!CurrentDefinition->isIdenticalTo(Other: *CmdLineDefinition, PP,
1790 /*Syntactically=*/true)) {
1791 // The macro definitions differ.
1792 PP.Diag(Loc: ImportLoc, DiagID: diag::warn_module_config_macro_undef)
1793 << false << ConfigMacro << Mod->getFullModuleName();
1794 PP.Diag(Loc: CurrentDefinition->getDefinitionLoc(),
1795 DiagID: diag::note_module_def_undef_here)
1796 << false;
1797 }
1798}
1799
1800static void checkConfigMacros(Preprocessor &PP, Module *M,
1801 SourceLocation ImportLoc) {
1802 clang::Module *TopModule = M->getTopLevelModule();
1803 for (const StringRef ConMacro : TopModule->ConfigMacros) {
1804 checkConfigMacro(PP, ConfigMacro: ConMacro, Mod: M, ImportLoc);
1805 }
1806}
1807
1808void CompilerInstance::createASTReader() {
1809 if (TheASTReader)
1810 return;
1811
1812 if (!hasASTContext())
1813 createASTContext();
1814
1815 // If we're implicitly building modules but not currently recursively
1816 // building a module, check whether we need to prune the module cache.
1817 if (getSourceManager().getModuleBuildStack().empty() &&
1818 !getPreprocessor()
1819 .getHeaderSearchInfo()
1820 .getSpecificModuleCachePath()
1821 .empty())
1822 ModCache->maybePrune(Path: getHeaderSearchOpts().ModuleCachePath,
1823 PruneInterval: getHeaderSearchOpts().ModuleCachePruneInterval,
1824 PruneAfter: getHeaderSearchOpts().ModuleCachePruneAfter);
1825
1826 HeaderSearchOptions &HSOpts = getHeaderSearchOpts();
1827 std::string Sysroot = HSOpts.Sysroot;
1828 const PreprocessorOptions &PPOpts = getPreprocessorOpts();
1829 const FrontendOptions &FEOpts = getFrontendOpts();
1830 std::unique_ptr<llvm::Timer> ReadTimer;
1831
1832 if (timerGroup)
1833 ReadTimer = std::make_unique<llvm::Timer>(args: "reading_modules",
1834 args: "Reading modules", args&: *timerGroup);
1835 TheASTReader = llvm::makeIntrusiveRefCnt<ASTReader>(
1836 A&: getPreprocessor(), A&: getModuleCache(), A: &getASTContext(),
1837 A: getPCHContainerReader(), A&: getCodeGenOpts(),
1838 A&: getFrontendOpts().ModuleFileExtensions,
1839 A: Sysroot.empty() ? "" : Sysroot.c_str(),
1840 A: PPOpts.DisablePCHOrModuleValidation,
1841 /*AllowASTWithCompilerErrors=*/A: FEOpts.AllowPCMWithCompilerErrors,
1842 /*AllowConfigurationMismatch=*/A: false,
1843 A: +HSOpts.ModulesValidateSystemHeaders,
1844 A: +HSOpts.ModulesForceValidateUserHeaders,
1845 A: +HSOpts.ValidateASTInputFilesContent,
1846 A: +getFrontendOpts().UseGlobalModuleIndex, A: std::move(ReadTimer));
1847 if (hasASTConsumer()) {
1848 TheASTReader->setDeserializationListener(
1849 Listener: getASTConsumer().GetASTDeserializationListener());
1850 getASTContext().setASTMutationListener(
1851 getASTConsumer().GetASTMutationListener());
1852 }
1853 getASTContext().setExternalSource(TheASTReader);
1854 if (hasSema())
1855 TheASTReader->InitializeSema(S&: getSema());
1856 if (hasASTConsumer())
1857 TheASTReader->StartTranslationUnit(Consumer: &getASTConsumer());
1858
1859 for (auto &Listener : DependencyCollectors)
1860 Listener->attachToASTReader(R&: *TheASTReader);
1861}
1862
1863bool CompilerInstance::loadModuleFile(
1864 ModuleFileName FileName, serialization::ModuleFile *&LoadedModuleFile) {
1865 llvm::Timer Timer;
1866 if (timerGroup)
1867 Timer.init(TimerName: "preloading." + std::string(FileName.str()),
1868 TimerDescription: "Preloading " + std::string(FileName.str()), tg&: *timerGroup);
1869 llvm::TimeRegion TimeLoading(timerGroup ? &Timer : nullptr);
1870
1871 // If we don't already have an ASTReader, create one now.
1872 if (!TheASTReader)
1873 createASTReader();
1874
1875 // If -Wmodule-file-config-mismatch is mapped as an error or worse, allow the
1876 // ASTReader to diagnose it, since it can produce better errors that we can.
1877 bool ConfigMismatchIsRecoverable =
1878 getDiagnostics().getDiagnosticLevel(DiagID: diag::warn_ast_file_config_mismatch,
1879 Loc: SourceLocation()) <=
1880 DiagnosticsEngine::Warning;
1881
1882 auto Listener = std::make_unique<ReadModuleNames>(args&: *PP);
1883 auto &ListenerRef = *Listener;
1884 ASTReader::ListenerScope ReadModuleNamesListener(*TheASTReader,
1885 std::move(Listener));
1886
1887 // Try to load the module file.
1888 switch (TheASTReader->ReadAST(
1889 FileName, Type: serialization::MK_ExplicitModule, ImportLoc: SourceLocation(),
1890 ClientLoadCapabilities: ConfigMismatchIsRecoverable ? ASTReader::ARR_ConfigurationMismatch : 0,
1891 NewLoadedModuleFile: &LoadedModuleFile)) {
1892 case ASTReader::Success:
1893 // We successfully loaded the module file; remember the set of provided
1894 // modules so that we don't try to load implicit modules for them.
1895 ListenerRef.registerAll();
1896 return true;
1897
1898 case ASTReader::ConfigurationMismatch:
1899 // Ignore unusable module files.
1900 getDiagnostics().Report(Loc: SourceLocation(),
1901 DiagID: diag::warn_ast_file_config_mismatch)
1902 << FileName;
1903 // All modules provided by any files we tried and failed to load are now
1904 // unavailable; includes of those modules should now be handled textually.
1905 ListenerRef.markAllUnavailable();
1906 return true;
1907
1908 default:
1909 return false;
1910 }
1911}
1912
1913namespace {
1914enum ModuleSource {
1915 MS_ModuleNotFound,
1916 MS_ModuleCache,
1917 MS_PrebuiltModulePath,
1918 MS_ModuleBuildPragma
1919};
1920} // end namespace
1921
1922/// Select a source for loading the named module and compute the filename to
1923/// load it from.
1924static ModuleSource selectModuleSource(
1925 Module *M, StringRef ModuleName, ModuleFileName &ModuleFilename,
1926 const std::map<std::string, std::string, std::less<>> &BuiltModules,
1927 HeaderSearch &HS) {
1928 assert(ModuleFilename.empty() && "Already has a module source?");
1929
1930 // Check to see if the module has been built as part of this compilation
1931 // via a module build pragma.
1932 auto BuiltModuleIt = BuiltModules.find(x: ModuleName);
1933 if (BuiltModuleIt != BuiltModules.end()) {
1934 ModuleFilename = ModuleFileName::makeExplicit(Name: BuiltModuleIt->second);
1935 return MS_ModuleBuildPragma;
1936 }
1937
1938 // Try to load the module from the prebuilt module path.
1939 const HeaderSearchOptions &HSOpts = HS.getHeaderSearchOpts();
1940 if (!HSOpts.PrebuiltModuleFiles.empty() ||
1941 !HSOpts.PrebuiltModulePaths.empty()) {
1942 ModuleFilename = HS.getPrebuiltModuleFileName(ModuleName);
1943 if (HSOpts.EnablePrebuiltImplicitModules && ModuleFilename.empty())
1944 ModuleFilename = HS.getPrebuiltImplicitModuleFileName(Module: M);
1945 if (!ModuleFilename.empty())
1946 return MS_PrebuiltModulePath;
1947 }
1948
1949 // Try to load the module from the module cache.
1950 if (M) {
1951 ModuleFilename = HS.getCachedModuleFileName(Module: M);
1952 return MS_ModuleCache;
1953 }
1954
1955 return MS_ModuleNotFound;
1956}
1957
1958ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
1959 StringRef ModuleName, SourceLocation ImportLoc, SourceRange ModuleNameRange,
1960 bool IsInclusionDirective) {
1961 // Search for a module with the given name.
1962 HeaderSearch &HS = PP->getHeaderSearchInfo();
1963 Module *M =
1964 HS.lookupModule(ModuleName, ImportLoc, AllowSearch: true, AllowExtraModuleMapSearch: !IsInclusionDirective);
1965
1966 // Check for any configuration macros that have changed. This is done
1967 // immediately before potentially building a module in case this module
1968 // depends on having one of its configuration macros defined to successfully
1969 // build. If this is not done the user will never see the warning.
1970 if (M)
1971 checkConfigMacros(PP&: getPreprocessor(), M, ImportLoc);
1972
1973 // Select the source and filename for loading the named module.
1974 ModuleFileName ModuleFilename;
1975 ModuleSource Source =
1976 selectModuleSource(M, ModuleName, ModuleFilename, BuiltModules, HS);
1977 SourceLocation ModuleNameLoc = ModuleNameRange.getBegin();
1978 if (Source == MS_ModuleNotFound) {
1979 // We can't find a module, error out here.
1980 getDiagnostics().Report(Loc: ModuleNameLoc, DiagID: diag::err_module_not_found)
1981 << ModuleName << ModuleNameRange;
1982 return nullptr;
1983 }
1984 if (ModuleFilename.empty()) {
1985 if (M && M->HasIncompatibleModuleFile) {
1986 // We tried and failed to load a module file for this module. Fall
1987 // back to textual inclusion for its headers.
1988 return ModuleLoadResult::ConfigMismatch;
1989 }
1990
1991 getDiagnostics().Report(Loc: ModuleNameLoc, DiagID: diag::err_module_build_disabled)
1992 << ModuleName;
1993 return nullptr;
1994 }
1995
1996 // Create an ASTReader on demand.
1997 if (!getASTReader())
1998 createASTReader();
1999
2000 // Time how long it takes to load the module.
2001 llvm::Timer Timer;
2002 if (timerGroup)
2003 Timer.init(TimerName: "loading." + std::string(ModuleFilename.str()),
2004 TimerDescription: "Loading " + std::string(ModuleFilename.str()), tg&: *timerGroup);
2005 llvm::TimeRegion TimeLoading(timerGroup ? &Timer : nullptr);
2006 llvm::TimeTraceScope TimeScope("Module Load", ModuleName);
2007
2008 // Try to load the module file. If we are not trying to load from the
2009 // module cache, we don't know how to rebuild modules.
2010 unsigned ARRFlags = Source == MS_ModuleCache
2011 ? ASTReader::ARR_OutOfDate | ASTReader::ARR_Missing |
2012 ASTReader::ARR_TreatModuleWithErrorsAsOutOfDate
2013 : Source == MS_PrebuiltModulePath
2014 ? 0
2015 : ASTReader::ARR_ConfigurationMismatch;
2016 switch (getASTReader()->ReadAST(FileName: ModuleFilename,
2017 Type: Source == MS_PrebuiltModulePath
2018 ? serialization::MK_PrebuiltModule
2019 : Source == MS_ModuleBuildPragma
2020 ? serialization::MK_ExplicitModule
2021 : serialization::MK_ImplicitModule,
2022 ImportLoc, ClientLoadCapabilities: ARRFlags)) {
2023 case ASTReader::Success: {
2024 if (M)
2025 return M;
2026 assert(Source != MS_ModuleCache &&
2027 "missing module, but file loaded from cache");
2028
2029 // A prebuilt module is indexed as a ModuleFile; the Module does not exist
2030 // until the first call to ReadAST. Look it up now.
2031 M = HS.lookupModule(ModuleName, ImportLoc, AllowSearch: true, AllowExtraModuleMapSearch: !IsInclusionDirective);
2032
2033 // Check whether M refers to the file in the prebuilt module path.
2034 if (M && M->getASTFileKey() &&
2035 *M->getASTFileKey() ==
2036 getASTReader()->getModuleManager().makeKey(Name: ModuleFilename))
2037 return M;
2038
2039 getDiagnostics().Report(Loc: ModuleNameLoc, DiagID: diag::err_module_prebuilt)
2040 << ModuleName;
2041 return ModuleLoadResult();
2042 }
2043
2044 case ASTReader::OutOfDate:
2045 case ASTReader::Missing:
2046 // The most interesting case.
2047 break;
2048
2049 case ASTReader::ConfigurationMismatch:
2050 if (Source == MS_PrebuiltModulePath)
2051 // FIXME: We shouldn't be setting HadFatalFailure below if we only
2052 // produce a warning here!
2053 getDiagnostics().Report(Loc: SourceLocation(),
2054 DiagID: diag::warn_ast_file_config_mismatch)
2055 << ModuleFilename;
2056 // Fall through to error out.
2057 [[fallthrough]];
2058 case ASTReader::VersionMismatch:
2059 case ASTReader::HadErrors:
2060 ModuleLoader::HadFatalFailure = true;
2061 // FIXME: The ASTReader will already have complained, but can we shoehorn
2062 // that diagnostic information into a more useful form?
2063 return ModuleLoadResult();
2064
2065 case ASTReader::Failure:
2066 ModuleLoader::HadFatalFailure = true;
2067 return ModuleLoadResult();
2068 }
2069
2070 // ReadAST returned Missing or OutOfDate.
2071 if (Source != MS_ModuleCache) {
2072 // We don't know the desired configuration for this module and don't
2073 // necessarily even have a module map. Since ReadAST already produces
2074 // diagnostics for these two cases, we simply error out here.
2075 return ModuleLoadResult();
2076 }
2077
2078 // The module file is missing or out-of-date. Build it.
2079 assert(M && "missing module, but trying to compile for cache");
2080
2081 // Check whether there is a cycle in the module graph.
2082 ModuleBuildStack ModPath = getSourceManager().getModuleBuildStack();
2083 ModuleBuildStack::iterator Pos = ModPath.begin(), PosEnd = ModPath.end();
2084 for (; Pos != PosEnd; ++Pos) {
2085 if (Pos->first == ModuleName)
2086 break;
2087 }
2088
2089 if (Pos != PosEnd) {
2090 SmallString<256> CyclePath;
2091 for (; Pos != PosEnd; ++Pos) {
2092 CyclePath += Pos->first;
2093 CyclePath += " -> ";
2094 }
2095 CyclePath += ModuleName;
2096
2097 getDiagnostics().Report(Loc: ModuleNameLoc, DiagID: diag::err_module_cycle)
2098 << ModuleName << CyclePath;
2099 return nullptr;
2100 }
2101
2102 // Check whether we have already attempted to build this module (but failed).
2103 if (FailedModules.contains(key: ModuleName)) {
2104 getDiagnostics().Report(Loc: ModuleNameLoc, DiagID: diag::err_module_not_built)
2105 << ModuleName << SourceRange(ImportLoc, ModuleNameLoc);
2106 return nullptr;
2107 }
2108
2109 // Try to compile and then read the AST.
2110 if (!compileModuleAndReadAST(ImportingInstance&: *this, ImportLoc, ModuleNameLoc, Module: M,
2111 ModuleFileName: ModuleFilename)) {
2112 assert(getDiagnostics().hasErrorOccurred() &&
2113 "undiagnosed error in compileModuleAndReadAST");
2114 FailedModules.insert(key: ModuleName);
2115 return nullptr;
2116 }
2117
2118 // Okay, we've rebuilt and now loaded the module.
2119 return M;
2120}
2121
2122ModuleLoadResult
2123CompilerInstance::loadModule(SourceLocation ImportLoc,
2124 ModuleIdPath Path,
2125 Module::NameVisibilityKind Visibility,
2126 bool IsInclusionDirective) {
2127 // Determine what file we're searching from.
2128 StringRef ModuleName = Path[0].getIdentifierInfo()->getName();
2129 SourceLocation ModuleNameLoc = Path[0].getLoc();
2130
2131 // If we've already handled this import, just return the cached result.
2132 // This cache eliminates redundant diagnostics when both the preprocessor
2133 // and parser see the same import declaration.
2134 if (ImportLoc.isValid()) {
2135 auto CacheIt = ModuleImportResults.find(Val: ImportLoc);
2136 if (CacheIt != ModuleImportResults.end()) {
2137 if (CacheIt->second && ModuleName != getLangOpts().CurrentModule)
2138 TheASTReader->makeModuleVisible(Mod: CacheIt->second, NameVisibility: Visibility, ImportLoc);
2139 return CacheIt->second;
2140 }
2141 }
2142
2143 // If we don't already have information on this module, load the module now.
2144 Module *Module = nullptr;
2145 ModuleMap &MM = getPreprocessor().getHeaderSearchInfo().getModuleMap();
2146 if (auto MaybeModule = MM.getCachedModuleLoad(II: *Path[0].getIdentifierInfo())) {
2147 // Use the cached result, which may be nullptr.
2148 Module = *MaybeModule;
2149 // Config macros are already checked before building a module, but they need
2150 // to be checked at each import location in case any of the config macros
2151 // have a new value at the current `ImportLoc`.
2152 if (Module)
2153 checkConfigMacros(PP&: getPreprocessor(), M: Module, ImportLoc);
2154 } else if (ModuleName == getLangOpts().CurrentModule) {
2155 // This is the module we're building.
2156 Module = PP->getHeaderSearchInfo().lookupModule(
2157 ModuleName, ImportLoc, /*AllowSearch*/ true,
2158 /*AllowExtraModuleMapSearch*/ !IsInclusionDirective);
2159
2160 // Config macros do not need to be checked here for two reasons.
2161 // * This will always be textual inclusion, and thus the config macros
2162 // actually do impact the content of the header.
2163 // * `Preprocessor::HandleHeaderIncludeOrImport` will never call this
2164 // function as the `#include` or `#import` is textual.
2165
2166 MM.cacheModuleLoad(II: *Path[0].getIdentifierInfo(), M: Module);
2167 } else if (getPreprocessorOpts().SingleModuleParseMode) {
2168 // This mimics how findOrCompileModuleAndReadAST() finds the module.
2169 Module = getPreprocessor().getHeaderSearchInfo().lookupModule(
2170 ModuleName, ImportLoc, AllowSearch: true, AllowExtraModuleMapSearch: !IsInclusionDirective);
2171 if (Module) {
2172 if (PPCallbacks *PPCb = getPreprocessor().getPPCallbacks())
2173 PPCb->moduleLoadSkipped(Skipped: Module);
2174 // Mark the module and its submodules as if they were loaded from a PCM.
2175 // This prevents emission of the "missing submodule" diagnostic below.
2176 std::vector<clang::Module *> Worklist{Module};
2177 while (!Worklist.empty()) {
2178 clang::Module *M = Worklist.back();
2179 Worklist.pop_back();
2180 M->IsFromModuleFile = true;
2181 for (clang::Module *SubM : M->submodules())
2182 Worklist.push_back(x: SubM);
2183 }
2184 }
2185 MM.cacheModuleLoad(II: *Path[0].getIdentifierInfo(), M: Module);
2186 } else {
2187 SourceLocation ModuleNameEndLoc = Path.back().getLoc().getLocWithOffset(
2188 Offset: Path.back().getIdentifierInfo()->getLength());
2189 ModuleLoadResult Result = findOrCompileModuleAndReadAST(
2190 ModuleName, ImportLoc, ModuleNameRange: SourceRange{ModuleNameLoc, ModuleNameEndLoc},
2191 IsInclusionDirective);
2192 if (!Result.isNormal())
2193 return Result;
2194 if (!Result)
2195 DisableGeneratingGlobalModuleIndex = true;
2196 Module = Result;
2197 MM.cacheModuleLoad(II: *Path[0].getIdentifierInfo(), M: Module);
2198 }
2199
2200 // If we never found the module, fail. Otherwise, verify the module and link
2201 // it up.
2202 if (!Module)
2203 return ModuleLoadResult();
2204
2205 // Verify that the rest of the module path actually corresponds to
2206 // a submodule.
2207 bool MapPrivateSubModToTopLevel = false;
2208 for (unsigned I = 1, N = Path.size(); I != N; ++I) {
2209 StringRef Name = Path[I].getIdentifierInfo()->getName();
2210 clang::Module *Sub = Module->findSubmodule(Name);
2211
2212 // If the user is requesting Foo.Private and it doesn't exist, try to
2213 // match Foo_Private and emit a warning asking for the user to write
2214 // @import Foo_Private instead. FIXME: remove this when existing clients
2215 // migrate off of Foo.Private syntax.
2216 if (!Sub && Name == "Private" && Module == Module->getTopLevelModule()) {
2217 SmallString<128> PrivateModule(Module->Name);
2218 PrivateModule.append(RHS: "_Private");
2219
2220 SmallVector<IdentifierLoc, 2> PrivPath;
2221 auto &II = PP->getIdentifierTable().get(
2222 Name: PrivateModule, TokenCode: PP->getIdentifierInfo(Name: Module->Name)->getTokenID());
2223 PrivPath.emplace_back(Args: Path[0].getLoc(), Args: &II);
2224
2225 ModuleFileName FileName;
2226 // If there is a modulemap module or prebuilt module, load it.
2227 if (PP->getHeaderSearchInfo().lookupModule(ModuleName: PrivateModule, ImportLoc, AllowSearch: true,
2228 AllowExtraModuleMapSearch: !IsInclusionDirective) ||
2229 selectModuleSource(M: nullptr, ModuleName: PrivateModule, ModuleFilename&: FileName, BuiltModules,
2230 HS&: PP->getHeaderSearchInfo()) != MS_ModuleNotFound)
2231 Sub = loadModule(ImportLoc, Path: PrivPath, Visibility, IsInclusionDirective);
2232 if (Sub) {
2233 MapPrivateSubModToTopLevel = true;
2234 PP->markClangModuleAsAffecting(M: Module);
2235 if (!getDiagnostics().isIgnored(
2236 DiagID: diag::warn_no_priv_submodule_use_toplevel, Loc: ImportLoc)) {
2237 getDiagnostics().Report(Loc: Path[I].getLoc(),
2238 DiagID: diag::warn_no_priv_submodule_use_toplevel)
2239 << Path[I].getIdentifierInfo() << Module->getFullModuleName()
2240 << PrivateModule
2241 << SourceRange(Path[0].getLoc(), Path[I].getLoc())
2242 << FixItHint::CreateReplacement(RemoveRange: SourceRange(Path[0].getLoc()),
2243 Code: PrivateModule);
2244 getDiagnostics().Report(Loc: Sub->DefinitionLoc,
2245 DiagID: diag::note_private_top_level_defined);
2246 }
2247 }
2248 }
2249
2250 if (!Sub) {
2251 // Attempt to perform typo correction to find a module name that works.
2252 SmallVector<StringRef, 2> Best;
2253 unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)();
2254
2255 for (class Module *SubModule : Module->submodules()) {
2256 unsigned ED =
2257 Name.edit_distance(Other: SubModule->Name,
2258 /*AllowReplacements=*/true, MaxEditDistance: BestEditDistance);
2259 if (ED <= BestEditDistance) {
2260 if (ED < BestEditDistance) {
2261 Best.clear();
2262 BestEditDistance = ED;
2263 }
2264
2265 Best.push_back(Elt: SubModule->Name);
2266 }
2267 }
2268
2269 // If there was a clear winner, user it.
2270 if (Best.size() == 1) {
2271 getDiagnostics().Report(Loc: Path[I].getLoc(),
2272 DiagID: diag::err_no_submodule_suggest)
2273 << Path[I].getIdentifierInfo() << Module->getFullModuleName()
2274 << Best[0] << SourceRange(Path[0].getLoc(), Path[I - 1].getLoc())
2275 << FixItHint::CreateReplacement(RemoveRange: SourceRange(Path[I].getLoc()),
2276 Code: Best[0]);
2277
2278 Sub = Module->findSubmodule(Name: Best[0]);
2279 }
2280 }
2281
2282 if (!Sub) {
2283 // No submodule by this name. Complain, and don't look for further
2284 // submodules.
2285 getDiagnostics().Report(Loc: Path[I].getLoc(), DiagID: diag::err_no_submodule)
2286 << Path[I].getIdentifierInfo() << Module->getFullModuleName()
2287 << SourceRange(Path[0].getLoc(), Path[I - 1].getLoc());
2288 break;
2289 }
2290
2291 Module = Sub;
2292 }
2293
2294 // Make the named module visible, if it's not already part of the module
2295 // we are parsing.
2296 if (ModuleName != getLangOpts().CurrentModule) {
2297 if (!Module->IsFromModuleFile && !MapPrivateSubModToTopLevel) {
2298 // We have an umbrella header or directory that doesn't actually include
2299 // all of the headers within the directory it covers. Complain about
2300 // this missing submodule and recover by forgetting that we ever saw
2301 // this submodule.
2302 // FIXME: Should we detect this at module load time? It seems fairly
2303 // expensive (and rare).
2304 getDiagnostics().Report(Loc: ImportLoc, DiagID: diag::warn_missing_submodule)
2305 << Module->getFullModuleName()
2306 << SourceRange(Path.front().getLoc(), Path.back().getLoc());
2307
2308 return ModuleLoadResult(Module, ModuleLoadResult::MissingExpected);
2309 }
2310
2311 // Check whether this module is available.
2312 if (Preprocessor::checkModuleIsAvailable(LangOpts: getLangOpts(), TargetInfo: getTarget(),
2313 M: *Module, Diags&: getDiagnostics())) {
2314 getDiagnostics().Report(Loc: ImportLoc, DiagID: diag::note_module_import_here)
2315 << SourceRange(Path.front().getLoc(), Path.back().getLoc());
2316 ModuleImportResults[ImportLoc] = ModuleLoadResult();
2317 return ModuleLoadResult();
2318 }
2319
2320 TheASTReader->makeModuleVisible(Mod: Module, NameVisibility: Visibility, ImportLoc);
2321 }
2322
2323 // Resolve any remaining module using export_as for this one.
2324 getPreprocessor()
2325 .getHeaderSearchInfo()
2326 .getModuleMap()
2327 .resolveLinkAsDependencies(Mod: Module->getTopLevelModule());
2328
2329 ModuleImportResults[ImportLoc] = ModuleLoadResult(Module);
2330 return ModuleLoadResult(Module);
2331}
2332
2333void CompilerInstance::createModuleFromSource(SourceLocation ImportLoc,
2334 StringRef ModuleName,
2335 StringRef Source) {
2336 // Avoid creating filenames with special characters.
2337 SmallString<128> CleanModuleName(ModuleName);
2338 for (auto &C : CleanModuleName)
2339 if (!isAlphanumeric(c: C))
2340 C = '_';
2341
2342 // FIXME: Using a randomized filename here means that our intermediate .pcm
2343 // output is nondeterministic (as .pcm files refer to each other by name).
2344 // Can this affect the output in any way?
2345 SmallString<128> ModuleFileName;
2346 int FD;
2347 if (std::error_code EC = llvm::sys::fs::createTemporaryFile(
2348 Prefix: CleanModuleName, Suffix: "pcm", ResultFD&: FD, ResultPath&: ModuleFileName)) {
2349 getDiagnostics().Report(Loc: ImportLoc, DiagID: diag::err_fe_unable_to_open_output)
2350 << ModuleFileName << EC.message();
2351 return;
2352 }
2353 std::string ModuleMapFileName = (CleanModuleName + ".map").str();
2354
2355 FrontendInputFile Input(
2356 ModuleMapFileName,
2357 InputKind(getLanguageFromOptions(LangOpts: Invocation->getLangOpts()),
2358 InputKind::ModuleMap, /*Preprocessed*/true));
2359
2360 std::string NullTerminatedSource(Source.str());
2361
2362 auto Other = cloneForModuleCompileImpl(ImportLoc, ModuleName, Input,
2363 OriginalModuleMapFile: StringRef(), ModuleFileName);
2364
2365 // Create a virtual file containing our desired source.
2366 // FIXME: We shouldn't need to do this.
2367 FileEntryRef ModuleMapFile = Other->getFileManager().getVirtualFileRef(
2368 Filename: ModuleMapFileName, Size: NullTerminatedSource.size(), ModificationTime: 0);
2369 Other->getSourceManager().overrideFileContents(
2370 SourceFile: ModuleMapFile, Buffer: llvm::MemoryBuffer::getMemBuffer(InputData: NullTerminatedSource));
2371
2372 Other->BuiltModules = std::move(BuiltModules);
2373 Other->DeleteBuiltModules = false;
2374
2375 // Build the module, inheriting any modules that we've built locally.
2376 std::unique_ptr<llvm::MemoryBuffer> Buffer =
2377 compileModule(ImportLoc, ModuleName, ModuleFileName, Instance&: *Other);
2378 BuiltModules = std::move(Other->BuiltModules);
2379
2380 if (Buffer) {
2381 llvm::raw_fd_ostream OS(FD, /*shouldClose=*/true);
2382 BuiltModules[std::string(ModuleName)] = std::string(ModuleFileName);
2383 OS << Buffer->getBuffer();
2384 llvm::sys::RemoveFileOnSignal(Filename: ModuleFileName);
2385 }
2386}
2387
2388void CompilerInstance::makeModuleVisible(Module *Mod,
2389 Module::NameVisibilityKind Visibility,
2390 SourceLocation ImportLoc) {
2391 if (!TheASTReader)
2392 createASTReader();
2393 if (!TheASTReader)
2394 return;
2395
2396 TheASTReader->makeModuleVisible(Mod, NameVisibility: Visibility, ImportLoc);
2397}
2398
2399GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex(
2400 SourceLocation TriggerLoc) {
2401 if (getPreprocessor()
2402 .getHeaderSearchInfo()
2403 .getSpecificModuleCachePath()
2404 .empty())
2405 return nullptr;
2406 if (!TheASTReader)
2407 createASTReader();
2408 // Can't do anything if we don't have the module manager.
2409 if (!TheASTReader)
2410 return nullptr;
2411 // Get an existing global index. This loads it if not already
2412 // loaded.
2413 TheASTReader->loadGlobalIndex();
2414 GlobalModuleIndex *GlobalIndex = TheASTReader->getGlobalIndex();
2415 // If the global index doesn't exist, create it.
2416 if (!GlobalIndex && shouldBuildGlobalModuleIndex() && hasFileManager() &&
2417 hasPreprocessor()) {
2418 llvm::sys::fs::create_directories(
2419 path: getPreprocessor().getHeaderSearchInfo().getSpecificModuleCachePath());
2420 if (llvm::Error Err = GlobalModuleIndex::writeIndex(
2421 FileMgr&: getFileManager(), PCHContainerRdr: getPCHContainerReader(),
2422 Path: getPreprocessor()
2423 .getHeaderSearchInfo()
2424 .getSpecificModuleCachePath())) {
2425 // FIXME this drops the error on the floor. This code is only used for
2426 // typo correction and drops more than just this one source of errors
2427 // (such as the directory creation failure above). It should handle the
2428 // error.
2429 consumeError(Err: std::move(Err));
2430 return nullptr;
2431 }
2432 TheASTReader->resetForReload();
2433 TheASTReader->loadGlobalIndex();
2434 GlobalIndex = TheASTReader->getGlobalIndex();
2435 }
2436 // For finding modules needing to be imported for fixit messages,
2437 // we need to make the global index cover all modules, so we do that here.
2438 if (!HaveFullGlobalModuleIndex && GlobalIndex && !buildingModule()) {
2439 ModuleMap &MMap = getPreprocessor().getHeaderSearchInfo().getModuleMap();
2440
2441 // Load modules that were parsed from module maps but not loaded yet.
2442 MMap.loadAllParsedModules();
2443
2444 bool RecreateIndex = false;
2445 for (ModuleMap::module_iterator I = MMap.module_begin(),
2446 E = MMap.module_end(); I != E; ++I) {
2447 Module *TheModule = I->second;
2448 if (!TheModule->getASTFileKey()) {
2449 SmallVector<IdentifierLoc, 2> Path;
2450 Path.emplace_back(Args&: TriggerLoc,
2451 Args: getPreprocessor().getIdentifierInfo(Name: TheModule->Name));
2452 std::reverse(first: Path.begin(), last: Path.end());
2453 // Load a module as hidden. This also adds it to the global index.
2454 loadModule(ImportLoc: TheModule->DefinitionLoc, Path, Visibility: Module::Hidden, IsInclusionDirective: false);
2455 RecreateIndex = true;
2456 }
2457 }
2458 if (RecreateIndex) {
2459 if (llvm::Error Err = GlobalModuleIndex::writeIndex(
2460 FileMgr&: getFileManager(), PCHContainerRdr: getPCHContainerReader(),
2461 Path: getPreprocessor()
2462 .getHeaderSearchInfo()
2463 .getSpecificModuleCachePath())) {
2464 // FIXME As above, this drops the error on the floor.
2465 consumeError(Err: std::move(Err));
2466 return nullptr;
2467 }
2468 TheASTReader->resetForReload();
2469 TheASTReader->loadGlobalIndex();
2470 GlobalIndex = TheASTReader->getGlobalIndex();
2471 }
2472 HaveFullGlobalModuleIndex = true;
2473 }
2474 return GlobalIndex;
2475}
2476
2477// Check global module index for missing imports.
2478bool
2479CompilerInstance::lookupMissingImports(StringRef Name,
2480 SourceLocation TriggerLoc) {
2481 // Look for the symbol in non-imported modules, but only if an error
2482 // actually occurred.
2483 if (!buildingModule()) {
2484 // Load global module index, or retrieve a previously loaded one.
2485 GlobalModuleIndex *GlobalIndex = loadGlobalModuleIndex(
2486 TriggerLoc);
2487
2488 // Only if we have a global index.
2489 if (GlobalIndex) {
2490 GlobalModuleIndex::HitSet FoundModules;
2491
2492 // Find the modules that reference the identifier.
2493 // Note that this only finds top-level modules.
2494 // We'll let diagnoseTypo find the actual declaration module.
2495 if (GlobalIndex->lookupIdentifier(Name, Hits&: FoundModules))
2496 return true;
2497 }
2498 }
2499
2500 return false;
2501}
2502void CompilerInstance::resetAndLeakSema() { llvm::BuryPointer(Ptr: takeSema()); }
2503
2504void CompilerInstance::setExternalSemaSource(
2505 IntrusiveRefCntPtr<ExternalSemaSource> ESS) {
2506 ExternalSemaSrc = std::move(ESS);
2507}
2508