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