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