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