Daniel Dunbar | 636404a | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 1 | //===--- CompilerInstance.cpp ---------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Daniel Dunbar | 636404a | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | 56d9c29 | 2009-11-14 02:47:17 +0000 | [diff] [blame] | 10 | #include "clang/AST/ASTConsumer.h" |
Daniel Dunbar | df3e30c | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | bcfc7d0 | 2011-12-02 23:42:12 +0000 | [diff] [blame] | 12 | #include "clang/AST/Decl.h" |
Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 13 | #include "clang/Basic/CharInfo.h" |
Daniel Dunbar | 636404a | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 14 | #include "clang/Basic/Diagnostic.h" |
Daniel Dunbar | 546a676 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 15 | #include "clang/Basic/FileManager.h" |
| 16 | #include "clang/Basic/SourceManager.h" |
Richard Smith | 0a7b297 | 2018-07-03 21:34:13 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Stack.h" |
Daniel Dunbar | 636404a | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Version.h" |
Alp Toker | f988d00 | 2014-06-06 10:36:22 +0000 | [diff] [blame] | 20 | #include "clang/Config/config.h" |
David Blaikie | 8b00dcb | 2011-09-26 00:21:47 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/ChainedDiagnosticConsumer.h" |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/FrontendAction.h" |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/LogDiagnosticPrinter.h" |
Ted Kremenek | 4610ea2 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/SerializedDiagnosticPrinter.h" |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/Utils.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 29 | #include "clang/Frontend/VerifyDiagnosticConsumer.h" |
| 30 | #include "clang/Lex/HeaderSearch.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Preprocessor.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 32 | #include "clang/Lex/PreprocessorOptions.h" |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 33 | #include "clang/Sema/CodeCompleteConsumer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 34 | #include "clang/Sema/Sema.h" |
| 35 | #include "clang/Serialization/ASTReader.h" |
John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 36 | #include "clang/Serialization/GlobalModuleIndex.h" |
Duncan P. N. Exon Smith | 8bef5cd | 2019-03-09 17:33:56 +0000 | [diff] [blame] | 37 | #include "clang/Serialization/InMemoryModuleCache.h" |
Douglas Gregor | 171b780 | 2010-03-30 17:33:59 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/Statistic.h" |
David Blaikie | 9941da4 | 2018-11-17 18:04:13 +0000 | [diff] [blame] | 39 | #include "llvm/Support/BuryPointer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 40 | #include "llvm/Support/CrashRecoveryContext.h" |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Errc.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 42 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Host.h" |
Douglas Gregor | e212489 | 2012-01-29 20:15:24 +0000 | [diff] [blame] | 44 | #include "llvm/Support/LockFileManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 45 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Path.h" |
| 47 | #include "llvm/Support/Program.h" |
| 48 | #include "llvm/Support/Signals.h" |
Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 49 | #include "llvm/Support/TimeProfiler.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Timer.h" |
| 51 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 527b1c9 | 2013-03-25 21:19:16 +0000 | [diff] [blame] | 52 | #include <sys/stat.h> |
Rafael Espindola | 8a8e554 | 2014-06-12 17:19:42 +0000 | [diff] [blame] | 53 | #include <system_error> |
Douglas Gregor | 37da327 | 2013-03-25 21:51:16 +0000 | [diff] [blame] | 54 | #include <time.h> |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 55 | #include <utility> |
Douglas Gregor | 54a8881 | 2011-10-05 14:53:30 +0000 | [diff] [blame] | 56 | |
Daniel Dunbar | 636404a | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 57 | using namespace clang; |
| 58 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 59 | CompilerInstance::CompilerInstance( |
| 60 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
Duncan P. N. Exon Smith | 8bef5cd | 2019-03-09 17:33:56 +0000 | [diff] [blame] | 61 | InMemoryModuleCache *SharedModuleCache) |
| 62 | : ModuleLoader(/* BuildingModule = */ SharedModuleCache), |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame] | 63 | Invocation(new CompilerInvocation()), |
Duncan P. N. Exon Smith | 8bef5cd | 2019-03-09 17:33:56 +0000 | [diff] [blame] | 64 | ModuleCache(SharedModuleCache ? SharedModuleCache |
| 65 | : new InMemoryModuleCache), |
Duncan P. N. Exon Smith | 0a2be46 | 2019-03-09 17:44:01 +0000 | [diff] [blame] | 66 | ThePCHContainerOperations(std::move(PCHContainerOps)) {} |
Daniel Dunbar | 636404a | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 67 | |
| 68 | CompilerInstance::~CompilerInstance() { |
Benjamin Kramer | 3c717b4 | 2012-10-14 19:21:21 +0000 | [diff] [blame] | 69 | assert(OutputFiles.empty() && "Still output files in flight?"); |
Daniel Dunbar | e922d9b | 2010-02-16 01:54:47 +0000 | [diff] [blame] | 70 | } |
| 71 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 72 | void CompilerInstance::setInvocation( |
| 73 | std::shared_ptr<CompilerInvocation> Value) { |
| 74 | Invocation = std::move(Value); |
Daniel Dunbar | 6824225 | 2010-01-30 21:47:07 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Douglas Gregor | c1bbec8 | 2013-01-25 00:45:27 +0000 | [diff] [blame] | 77 | bool CompilerInstance::shouldBuildGlobalModuleIndex() const { |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 78 | return (BuildGlobalModuleIndex || |
Douglas Gregor | 11ef0b7 | 2013-03-22 21:26:48 +0000 | [diff] [blame] | 79 | (ModuleManager && ModuleManager->isGlobalIndexUnavailable() && |
| 80 | getFrontendOpts().GenerateGlobalModuleIndex)) && |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 81 | !ModuleBuildFailed; |
Douglas Gregor | c1bbec8 | 2013-01-25 00:45:27 +0000 | [diff] [blame] | 82 | } |
| 83 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 84 | void CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) { |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 85 | Diagnostics = Value; |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 88 | void CompilerInstance::setTarget(TargetInfo *Value) { Target = Value; } |
| 89 | void CompilerInstance::setAuxTarget(TargetInfo *Value) { AuxTarget = Value; } |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 90 | |
| 91 | void CompilerInstance::setFileManager(FileManager *Value) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 92 | FileMgr = Value; |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 93 | } |
| 94 | |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 95 | void CompilerInstance::setSourceManager(SourceManager *Value) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 96 | SourceMgr = Value; |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 97 | } |
| 98 | |
David Blaikie | 4156546 | 2017-01-05 19:48:07 +0000 | [diff] [blame] | 99 | void CompilerInstance::setPreprocessor(std::shared_ptr<Preprocessor> Value) { |
| 100 | PP = std::move(Value); |
| 101 | } |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 102 | |
Richard Smith | 293534b | 2015-08-18 20:39:29 +0000 | [diff] [blame] | 103 | void CompilerInstance::setASTContext(ASTContext *Value) { |
| 104 | Context = Value; |
| 105 | |
| 106 | if (Context && Consumer) |
| 107 | getASTConsumer().Initialize(getASTContext()); |
| 108 | } |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 109 | |
Douglas Gregor | 0e93f01 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 110 | void CompilerInstance::setSema(Sema *S) { |
| 111 | TheSema.reset(S); |
| 112 | } |
| 113 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 114 | void CompilerInstance::setASTConsumer(std::unique_ptr<ASTConsumer> Value) { |
| 115 | Consumer = std::move(Value); |
Richard Smith | 293534b | 2015-08-18 20:39:29 +0000 | [diff] [blame] | 116 | |
| 117 | if (Context && Consumer) |
| 118 | getASTConsumer().Initialize(getASTContext()); |
Daniel Dunbar | 56d9c29 | 2009-11-14 02:47:17 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 121 | void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) { |
| 122 | CompletionConsumer.reset(Value); |
| 123 | } |
David Blaikie | 6153581 | 2014-08-10 20:12:39 +0000 | [diff] [blame] | 124 | |
| 125 | std::unique_ptr<Sema> CompilerInstance::takeSema() { |
| 126 | return std::move(TheSema); |
| 127 | } |
| 128 | |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 129 | IntrusiveRefCntPtr<ASTReader> CompilerInstance::getModuleManager() const { |
| 130 | return ModuleManager; |
| 131 | } |
| 132 | void CompilerInstance::setModuleManager(IntrusiveRefCntPtr<ASTReader> Reader) { |
Duncan P. N. Exon Smith | 8bef5cd | 2019-03-09 17:33:56 +0000 | [diff] [blame] | 133 | assert(ModuleCache.get() == &Reader->getModuleManager().getModuleCache() && |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame] | 134 | "Expected ASTReader to use the same PCM cache"); |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 135 | ModuleManager = std::move(Reader); |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 136 | } |
Daniel Dunbar | e01dc86 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 137 | |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 138 | std::shared_ptr<ModuleDependencyCollector> |
| 139 | CompilerInstance::getModuleDepCollector() const { |
| 140 | return ModuleDepCollector; |
| 141 | } |
| 142 | |
| 143 | void CompilerInstance::setModuleDepCollector( |
| 144 | std::shared_ptr<ModuleDependencyCollector> Collector) { |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 145 | ModuleDepCollector = std::move(Collector); |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Bruno Cardoso Lopes | 181225b | 2016-12-11 04:27:28 +0000 | [diff] [blame] | 148 | static void collectHeaderMaps(const HeaderSearch &HS, |
| 149 | std::shared_ptr<ModuleDependencyCollector> MDC) { |
| 150 | SmallVector<std::string, 4> HeaderMapFileNames; |
| 151 | HS.getHeaderMapFileNames(HeaderMapFileNames); |
| 152 | for (auto &Name : HeaderMapFileNames) |
| 153 | MDC->addFile(Name); |
| 154 | } |
| 155 | |
Bruno Cardoso Lopes | 7aff2bb | 2016-12-12 19:28:25 +0000 | [diff] [blame] | 156 | static void collectIncludePCH(CompilerInstance &CI, |
| 157 | std::shared_ptr<ModuleDependencyCollector> MDC) { |
| 158 | const PreprocessorOptions &PPOpts = CI.getPreprocessorOpts(); |
| 159 | if (PPOpts.ImplicitPCHInclude.empty()) |
| 160 | return; |
| 161 | |
| 162 | StringRef PCHInclude = PPOpts.ImplicitPCHInclude; |
| 163 | FileManager &FileMgr = CI.getFileManager(); |
| 164 | const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude); |
| 165 | if (!PCHDir) { |
| 166 | MDC->addFile(PCHInclude); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | std::error_code EC; |
| 171 | SmallString<128> DirNative; |
| 172 | llvm::sys::path::native(PCHDir->getName(), DirNative); |
Duncan P. N. Exon Smith | db8a742 | 2019-03-26 22:32:06 +0000 | [diff] [blame] | 173 | llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem(); |
Bruno Cardoso Lopes | 7aff2bb | 2016-12-12 19:28:25 +0000 | [diff] [blame] | 174 | SimpleASTReaderListener Validator(CI.getPreprocessor()); |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 175 | for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd; |
Bruno Cardoso Lopes | 7aff2bb | 2016-12-12 19:28:25 +0000 | [diff] [blame] | 176 | Dir != DirEnd && !EC; Dir.increment(EC)) { |
| 177 | // Check whether this is an AST file. ASTReader::isAcceptableASTFile is not |
| 178 | // used here since we're not interested in validating the PCH at this time, |
| 179 | // but only to check whether this is a file containing an AST. |
| 180 | if (!ASTReader::readASTFileControlBlock( |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 181 | Dir->path(), FileMgr, CI.getPCHContainerReader(), |
Bruno Cardoso Lopes | 7aff2bb | 2016-12-12 19:28:25 +0000 | [diff] [blame] | 182 | /*FindModuleFileExtensions=*/false, Validator, |
| 183 | /*ValidateDiagnosticOptions=*/false)) |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 184 | MDC->addFile(Dir->path()); |
Bruno Cardoso Lopes | 7aff2bb | 2016-12-12 19:28:25 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Bruno Cardoso Lopes | 82ec4fde | 2016-12-22 07:06:03 +0000 | [diff] [blame] | 188 | static void collectVFSEntries(CompilerInstance &CI, |
| 189 | std::shared_ptr<ModuleDependencyCollector> MDC) { |
| 190 | if (CI.getHeaderSearchOpts().VFSOverlayFiles.empty()) |
| 191 | return; |
| 192 | |
| 193 | // Collect all VFS found. |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 194 | SmallVector<llvm::vfs::YAMLVFSEntry, 16> VFSEntries; |
Bruno Cardoso Lopes | 82ec4fde | 2016-12-22 07:06:03 +0000 | [diff] [blame] | 195 | for (const std::string &VFSFile : CI.getHeaderSearchOpts().VFSOverlayFiles) { |
| 196 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer = |
| 197 | llvm::MemoryBuffer::getFile(VFSFile); |
| 198 | if (!Buffer) |
| 199 | return; |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 200 | llvm::vfs::collectVFSFromYAML(std::move(Buffer.get()), |
| 201 | /*DiagHandler*/ nullptr, VFSFile, VFSEntries); |
Bruno Cardoso Lopes | 82ec4fde | 2016-12-22 07:06:03 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | for (auto &E : VFSEntries) |
| 205 | MDC->addFile(E.VPath, E.RPath); |
| 206 | } |
| 207 | |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 208 | // Diagnostics |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 209 | static void SetUpDiagnosticLog(DiagnosticOptions *DiagOpts, |
Daniel Dunbar | 7b83306 | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 210 | const CodeGenOptions *CodeGenOpts, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 211 | DiagnosticsEngine &Diags) { |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 212 | std::error_code EC; |
David Blaikie | 11f8a94 | 2014-09-15 17:30:56 +0000 | [diff] [blame] | 213 | std::unique_ptr<raw_ostream> StreamOwner; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 214 | raw_ostream *OS = &llvm::errs(); |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 215 | if (DiagOpts->DiagnosticLogFile != "-") { |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 216 | // Create the output stream. |
David Blaikie | 11f8a94 | 2014-09-15 17:30:56 +0000 | [diff] [blame] | 217 | auto FileOS = llvm::make_unique<llvm::raw_fd_ostream>( |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 218 | DiagOpts->DiagnosticLogFile, EC, |
David Blaikie | 11f8a94 | 2014-09-15 17:30:56 +0000 | [diff] [blame] | 219 | llvm::sys::fs::F_Append | llvm::sys::fs::F_Text); |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 220 | if (EC) { |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 221 | Diags.Report(diag::warn_fe_cc_log_diagnostics_failure) |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 222 | << DiagOpts->DiagnosticLogFile << EC.message(); |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 223 | } else { |
| 224 | FileOS->SetUnbuffered(); |
David Blaikie | 11f8a94 | 2014-09-15 17:30:56 +0000 | [diff] [blame] | 225 | OS = FileOS.get(); |
| 226 | StreamOwner = std::move(FileOS); |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | // Chain in the diagnostic client which will log the diagnostics. |
David Blaikie | 7ee2550 | 2014-09-15 17:50:10 +0000 | [diff] [blame] | 231 | auto Logger = llvm::make_unique<LogDiagnosticPrinter>(*OS, DiagOpts, |
| 232 | std::move(StreamOwner)); |
Daniel Dunbar | 7b83306 | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 233 | if (CodeGenOpts) |
| 234 | Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags); |
Alex Lorenz | 88377d8 | 2019-06-10 23:32:42 +0000 | [diff] [blame] | 235 | if (Diags.ownsClient()) { |
| 236 | Diags.setClient( |
| 237 | new ChainedDiagnosticConsumer(Diags.takeClient(), std::move(Logger))); |
| 238 | } else { |
| 239 | Diags.setClient( |
| 240 | new ChainedDiagnosticConsumer(Diags.getClient(), std::move(Logger))); |
| 241 | } |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 244 | static void SetupSerializedDiagnostics(DiagnosticOptions *DiagOpts, |
Ted Kremenek | 4610ea2 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 245 | DiagnosticsEngine &Diags, |
| 246 | StringRef OutputFile) { |
David Blaikie | 7ee2550 | 2014-09-15 17:50:10 +0000 | [diff] [blame] | 247 | auto SerializedConsumer = |
Justin Bogner | 5a6a2fc | 2014-10-23 22:20:11 +0000 | [diff] [blame] | 248 | clang::serialized_diags::create(OutputFile, DiagOpts); |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 249 | |
Alexander Kornienko | 254b7db | 2014-11-13 13:08:27 +0000 | [diff] [blame] | 250 | if (Diags.ownsClient()) { |
| 251 | Diags.setClient(new ChainedDiagnosticConsumer( |
Alexander Kornienko | 41c247a | 2014-11-17 23:46:02 +0000 | [diff] [blame] | 252 | Diags.takeClient(), std::move(SerializedConsumer))); |
Alexander Kornienko | 254b7db | 2014-11-13 13:08:27 +0000 | [diff] [blame] | 253 | } else { |
| 254 | Diags.setClient(new ChainedDiagnosticConsumer( |
Alexander Kornienko | 4c0ef379 | 2014-11-17 14:46:28 +0000 | [diff] [blame] | 255 | Diags.getClient(), std::move(SerializedConsumer))); |
Alexander Kornienko | 254b7db | 2014-11-13 13:08:27 +0000 | [diff] [blame] | 256 | } |
Ted Kremenek | 4610ea2 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Sean Silva | f1b49e2 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 259 | void CompilerInstance::createDiagnostics(DiagnosticConsumer *Client, |
Douglas Gregor | 30071cea | 2013-05-03 23:07:45 +0000 | [diff] [blame] | 260 | bool ShouldOwnClient) { |
Sean Silva | f1b49e2 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 261 | Diagnostics = createDiagnostics(&getDiagnosticOpts(), Client, |
Douglas Gregor | 30071cea | 2013-05-03 23:07:45 +0000 | [diff] [blame] | 262 | ShouldOwnClient, &getCodeGenOpts()); |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 265 | IntrusiveRefCntPtr<DiagnosticsEngine> |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 266 | CompilerInstance::createDiagnostics(DiagnosticOptions *Opts, |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 267 | DiagnosticConsumer *Client, |
Douglas Gregor | 2b9b464 | 2011-09-13 01:26:44 +0000 | [diff] [blame] | 268 | bool ShouldOwnClient, |
Daniel Dunbar | 7b83306 | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 269 | const CodeGenOptions *CodeGenOpts) { |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 270 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 271 | IntrusiveRefCntPtr<DiagnosticsEngine> |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 272 | Diags(new DiagnosticsEngine(DiagID, Opts)); |
Daniel Dunbar | 1b39a2e | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 273 | |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 274 | // Create the diagnostic client for reporting errors or for |
| 275 | // implementing -verify. |
Douglas Gregor | d0e9e3a | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 276 | if (Client) { |
Douglas Gregor | 30071cea | 2013-05-03 23:07:45 +0000 | [diff] [blame] | 277 | Diags->setClient(Client, ShouldOwnClient); |
Douglas Gregor | d0e9e3a | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 278 | } else |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 279 | Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts)); |
Daniel Dunbar | 50ec0da | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 280 | |
| 281 | // Chain in -verify checker, if requested. |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 282 | if (Opts->VerifyDiagnostics) |
David Blaikie | 69609dc | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 283 | Diags->setClient(new VerifyDiagnosticConsumer(*Diags)); |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 284 | |
Daniel Dunbar | 2083c32 | 2011-04-07 18:31:10 +0000 | [diff] [blame] | 285 | // Chain in -diagnostic-log-file dumper, if requested. |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 286 | if (!Opts->DiagnosticLogFile.empty()) |
Daniel Dunbar | 7b83306 | 2011-04-07 18:59:02 +0000 | [diff] [blame] | 287 | SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 288 | |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 289 | if (!Opts->DiagnosticSerializationFile.empty()) |
Ted Kremenek | 4610ea2 | 2011-10-29 00:12:39 +0000 | [diff] [blame] | 290 | SetupSerializedDiagnostics(Opts, *Diags, |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 291 | Opts->DiagnosticSerializationFile); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 292 | |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 293 | // Configure our handling of diagnostics. |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 294 | ProcessWarningOptions(*Diags, *Opts); |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 295 | |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 296 | return Diags; |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // File Manager |
| 300 | |
Duncan P. N. Exon Smith | 1da7eac | 2019-03-26 22:18:52 +0000 | [diff] [blame] | 301 | FileManager *CompilerInstance::createFileManager( |
| 302 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) { |
| 303 | if (!VFS) |
Duncan P. N. Exon Smith | db8a742 | 2019-03-26 22:32:06 +0000 | [diff] [blame] | 304 | VFS = FileMgr ? &FileMgr->getVirtualFileSystem() |
Duncan P. N. Exon Smith | 1da7eac | 2019-03-26 22:18:52 +0000 | [diff] [blame] | 305 | : createVFSFromCompilerInvocation(getInvocation(), |
| 306 | getDiagnostics()); |
| 307 | assert(VFS && "FileManager has no VFS?"); |
| 308 | FileMgr = new FileManager(getFileSystemOpts(), std::move(VFS)); |
Raphael Isemann | abc3d04 | 2017-09-12 16:54:53 +0000 | [diff] [blame] | 309 | return FileMgr.get(); |
Daniel Dunbar | 546a676 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 312 | // Source Manager |
| 313 | |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 314 | void CompilerInstance::createSourceManager(FileManager &FileMgr) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 315 | SourceMgr = new SourceManager(getDiagnostics(), FileMgr); |
Daniel Dunbar | 546a676 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 316 | } |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 317 | |
Alp Toker | c358000 | 2014-07-07 06:05:00 +0000 | [diff] [blame] | 318 | // Initialize the remapping of files to alternative contents, e.g., |
| 319 | // those specified through other files. |
| 320 | static void InitializeFileRemapping(DiagnosticsEngine &Diags, |
| 321 | SourceManager &SourceMgr, |
| 322 | FileManager &FileMgr, |
| 323 | const PreprocessorOptions &InitOpts) { |
| 324 | // Remap files in the source manager (with buffers). |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 325 | for (const auto &RB : InitOpts.RemappedFileBuffers) { |
Alp Toker | c358000 | 2014-07-07 06:05:00 +0000 | [diff] [blame] | 326 | // Create the file entry for the file that we're mapping from. |
| 327 | const FileEntry *FromFile = |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 328 | FileMgr.getVirtualFile(RB.first, RB.second->getBufferSize(), 0); |
Alp Toker | c358000 | 2014-07-07 06:05:00 +0000 | [diff] [blame] | 329 | if (!FromFile) { |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 330 | Diags.Report(diag::err_fe_remap_missing_from_file) << RB.first; |
Alp Toker | c358000 | 2014-07-07 06:05:00 +0000 | [diff] [blame] | 331 | if (!InitOpts.RetainRemappedFileBuffers) |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 332 | delete RB.second; |
Alp Toker | c358000 | 2014-07-07 06:05:00 +0000 | [diff] [blame] | 333 | continue; |
| 334 | } |
| 335 | |
| 336 | // Override the contents of the "from" file with the contents of |
| 337 | // the "to" file. |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 338 | SourceMgr.overrideFileContents(FromFile, RB.second, |
Alp Toker | c358000 | 2014-07-07 06:05:00 +0000 | [diff] [blame] | 339 | InitOpts.RetainRemappedFileBuffers); |
| 340 | } |
| 341 | |
| 342 | // Remap files in the source manager (with other files). |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 343 | for (const auto &RF : InitOpts.RemappedFiles) { |
Alp Toker | c358000 | 2014-07-07 06:05:00 +0000 | [diff] [blame] | 344 | // Find the file that we're mapping to. |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 345 | const FileEntry *ToFile = FileMgr.getFile(RF.second); |
Alp Toker | c358000 | 2014-07-07 06:05:00 +0000 | [diff] [blame] | 346 | if (!ToFile) { |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 347 | Diags.Report(diag::err_fe_remap_missing_to_file) << RF.first << RF.second; |
Alp Toker | c358000 | 2014-07-07 06:05:00 +0000 | [diff] [blame] | 348 | continue; |
| 349 | } |
| 350 | |
| 351 | // Create the file entry for the file that we're mapping from. |
| 352 | const FileEntry *FromFile = |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 353 | FileMgr.getVirtualFile(RF.first, ToFile->getSize(), 0); |
Alp Toker | c358000 | 2014-07-07 06:05:00 +0000 | [diff] [blame] | 354 | if (!FromFile) { |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 355 | Diags.Report(diag::err_fe_remap_missing_from_file) << RF.first; |
Alp Toker | c358000 | 2014-07-07 06:05:00 +0000 | [diff] [blame] | 356 | continue; |
| 357 | } |
| 358 | |
| 359 | // Override the contents of the "from" file with the contents of |
| 360 | // the "to" file. |
| 361 | SourceMgr.overrideFileContents(FromFile, ToFile); |
| 362 | } |
| 363 | |
| 364 | SourceMgr.setOverridenFilesKeepOriginalName( |
| 365 | InitOpts.RemappedFilesKeepOriginalName); |
| 366 | } |
| 367 | |
Daniel Dunbar | 7d75afc | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 368 | // Preprocessor |
| 369 | |
Argyrios Kyrtzidis | e1974dc | 2014-03-07 07:47:58 +0000 | [diff] [blame] | 370 | void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) { |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 371 | const PreprocessorOptions &PPOpts = getPreprocessorOpts(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 372 | |
Benjamin Kramer | 5904c41 | 2018-11-05 12:46:02 +0000 | [diff] [blame] | 373 | // The module manager holds a reference to the old preprocessor (if any). |
| 374 | ModuleManager.reset(); |
| 375 | |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 376 | // Create the Preprocessor. |
David Blaikie | 9c28cb3 | 2017-01-06 01:04:46 +0000 | [diff] [blame] | 377 | HeaderSearch *HeaderInfo = |
| 378 | new HeaderSearch(getHeaderSearchOptsPtr(), getSourceManager(), |
| 379 | getDiagnostics(), getLangOpts(), &getTarget()); |
Duncan P. N. Exon Smith | 8bef5cd | 2019-03-09 17:33:56 +0000 | [diff] [blame] | 380 | PP = std::make_shared<Preprocessor>(Invocation->getPreprocessorOptsPtr(), |
| 381 | getDiagnostics(), getLangOpts(), |
| 382 | getSourceManager(), *HeaderInfo, *this, |
| 383 | /*IdentifierInfoLookup=*/nullptr, |
| 384 | /*OwnsHeaderSearch=*/true, TUKind); |
Saleem Abdulrasool | 729379a | 2017-10-06 23:09:55 +0000 | [diff] [blame] | 385 | getTarget().adjust(getLangOpts()); |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 386 | PP->Initialize(getTarget(), getAuxTarget()); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 387 | |
Douglas Gregor | 7f6d60d | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 388 | if (PPOpts.DetailedRecord) |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 389 | PP->createPreprocessingRecord(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 390 | |
Alp Toker | c358000 | 2014-07-07 06:05:00 +0000 | [diff] [blame] | 391 | // Apply remappings to the source manager. |
| 392 | InitializeFileRemapping(PP->getDiagnostics(), PP->getSourceManager(), |
| 393 | PP->getFileManager(), PPOpts); |
| 394 | |
| 395 | // Predefine macros and configure the preprocessor. |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 396 | InitializePreprocessor(*PP, PPOpts, getPCHContainerReader(), |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 397 | getFrontendOpts()); |
Alp Toker | c358000 | 2014-07-07 06:05:00 +0000 | [diff] [blame] | 398 | |
Justin Lebar | f91086b | 2016-11-18 00:41:27 +0000 | [diff] [blame] | 399 | // Initialize the header search object. In CUDA compilations, we use the aux |
| 400 | // triple (the host triple) to initialize our header search, since we need to |
| 401 | // find the host headers in order to compile the CUDA code. |
| 402 | const llvm::Triple *HeaderSearchTriple = &PP->getTargetInfo().getTriple(); |
| 403 | if (PP->getTargetInfo().getTriple().getOS() == llvm::Triple::CUDA && |
| 404 | PP->getAuxTargetInfo()) |
| 405 | HeaderSearchTriple = &PP->getAuxTargetInfo()->getTriple(); |
| 406 | |
Alp Toker | c358000 | 2014-07-07 06:05:00 +0000 | [diff] [blame] | 407 | ApplyHeaderSearchOptions(PP->getHeaderSearchInfo(), getHeaderSearchOpts(), |
Justin Lebar | f91086b | 2016-11-18 00:41:27 +0000 | [diff] [blame] | 408 | PP->getLangOpts(), *HeaderSearchTriple); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 409 | |
Jordan Rose | 1744158 | 2013-01-30 01:52:57 +0000 | [diff] [blame] | 410 | PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP); |
| 411 | |
Richard Smith | 3938f0c | 2015-08-15 00:34:15 +0000 | [diff] [blame] | 412 | if (PP->getLangOpts().Modules && PP->getLangOpts().ImplicitModules) |
Chandler Carruth | ff8d943 | 2015-03-28 01:10:44 +0000 | [diff] [blame] | 413 | PP->getHeaderSearchInfo().setModuleCachePath(getSpecificModuleCachePath()); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 414 | |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 415 | // Handle generating dependencies, if requested. |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 416 | const DependencyOutputOptions &DepOpts = getDependencyOutputOpts(); |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 417 | if (!DepOpts.OutputFile.empty()) |
Ben Langmuir | cb69b57 | 2014-03-07 06:40:32 +0000 | [diff] [blame] | 418 | TheDependencyFileGenerator.reset( |
| 419 | DependencyFileGenerator::CreateAndAttachToPreprocessor(*PP, DepOpts)); |
Douglas Gregor | 2e12965 | 2012-02-02 23:45:13 +0000 | [diff] [blame] | 420 | if (!DepOpts.DOTOutputFile.empty()) |
| 421 | AttachDependencyGraphGen(*PP, DepOpts.DOTOutputFile, |
Douglas Gregor | 83d46be | 2012-02-02 00:54:52 +0000 | [diff] [blame] | 422 | getHeaderSearchOpts().Sysroot); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 423 | |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 424 | // If we don't have a collector, but we are collecting module dependencies, |
| 425 | // then we're the top level compiler instance and need to create one. |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 426 | if (!ModuleDepCollector && !DepOpts.ModuleDependencyOutputDir.empty()) { |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 427 | ModuleDepCollector = std::make_shared<ModuleDependencyCollector>( |
| 428 | DepOpts.ModuleDependencyOutputDir); |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Bruno Cardoso Lopes | 181225b | 2016-12-11 04:27:28 +0000 | [diff] [blame] | 431 | // If there is a module dep collector, register with other dep collectors |
| 432 | // and also (a) collect header maps and (b) TODO: input vfs overlay files. |
| 433 | if (ModuleDepCollector) { |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 434 | addDependencyCollector(ModuleDepCollector); |
Bruno Cardoso Lopes | 181225b | 2016-12-11 04:27:28 +0000 | [diff] [blame] | 435 | collectHeaderMaps(PP->getHeaderSearchInfo(), ModuleDepCollector); |
Bruno Cardoso Lopes | 7aff2bb | 2016-12-12 19:28:25 +0000 | [diff] [blame] | 436 | collectIncludePCH(*this, ModuleDepCollector); |
Bruno Cardoso Lopes | 82ec4fde | 2016-12-22 07:06:03 +0000 | [diff] [blame] | 437 | collectVFSEntries(*this, ModuleDepCollector); |
Bruno Cardoso Lopes | 181225b | 2016-12-11 04:27:28 +0000 | [diff] [blame] | 438 | } |
Bruno Cardoso Lopes | b1631d9 | 2016-03-29 23:47:40 +0000 | [diff] [blame] | 439 | |
| 440 | for (auto &Listener : DependencyCollectors) |
| 441 | Listener->attachToPreprocessor(*PP); |
Hans Wennborg | 0fd6207 | 2013-08-09 00:32:23 +0000 | [diff] [blame] | 442 | |
Daniel Dunbar | 27734fd | 2011-02-02 15:41:17 +0000 | [diff] [blame] | 443 | // Handle generating header include information, if requested. |
| 444 | if (DepOpts.ShowHeaderIncludes) |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame] | 445 | AttachHeaderIncludeGen(*PP, DepOpts); |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 446 | if (!DepOpts.HeaderIncludeOutputFile.empty()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 447 | StringRef OutputPath = DepOpts.HeaderIncludeOutputFile; |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 448 | if (OutputPath == "-") |
| 449 | OutputPath = ""; |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame] | 450 | AttachHeaderIncludeGen(*PP, DepOpts, |
Ivan Krasin | 1193f2c | 2015-08-13 04:04:37 +0000 | [diff] [blame] | 451 | /*ShowAllHeaders=*/true, OutputPath, |
Daniel Dunbar | fe908a8 | 2011-03-21 19:37:38 +0000 | [diff] [blame] | 452 | /*ShowDepth=*/false); |
Daniel Dunbar | 1af1d2751 | 2011-02-02 21:11:31 +0000 | [diff] [blame] | 453 | } |
Hans Wennborg | 0fd6207 | 2013-08-09 00:32:23 +0000 | [diff] [blame] | 454 | |
Erich Keane | 425f48d | 2018-05-04 15:58:31 +0000 | [diff] [blame] | 455 | if (DepOpts.ShowIncludesDest != ShowIncludesDestination::None) { |
Nico Weber | f54146c | 2016-03-23 18:46:57 +0000 | [diff] [blame] | 456 | AttachHeaderIncludeGen(*PP, DepOpts, |
Nico Weber | 149d9522 | 2016-03-23 18:00:22 +0000 | [diff] [blame] | 457 | /*ShowAllHeaders=*/true, /*OutputPath=*/"", |
Hans Wennborg | 0fd6207 | 2013-08-09 00:32:23 +0000 | [diff] [blame] | 458 | /*ShowDepth=*/true, /*MSStyle=*/true); |
| 459 | } |
Daniel Dunbar | aaa148f | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 460 | } |
Daniel Dunbar | df3e30c | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 461 | |
Argyrios Kyrtzidis | bd0b651 | 2015-02-19 20:12:20 +0000 | [diff] [blame] | 462 | std::string CompilerInstance::getSpecificModuleCachePath() { |
| 463 | // Set up the module path, including the hash for the |
| 464 | // module-creation options. |
Richard Smith | d520a25 | 2015-07-21 18:07:47 +0000 | [diff] [blame] | 465 | SmallString<256> SpecificModuleCache(getHeaderSearchOpts().ModuleCachePath); |
| 466 | if (!SpecificModuleCache.empty() && !getHeaderSearchOpts().DisableModuleHash) |
Argyrios Kyrtzidis | bd0b651 | 2015-02-19 20:12:20 +0000 | [diff] [blame] | 467 | llvm::sys::path::append(SpecificModuleCache, |
| 468 | getInvocation().getModuleHash()); |
| 469 | return SpecificModuleCache.str(); |
| 470 | } |
| 471 | |
Daniel Dunbar | df3e30c | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 472 | // ASTContext |
| 473 | |
| 474 | void CompilerInstance::createASTContext() { |
| 475 | Preprocessor &PP = getPreprocessor(); |
Richard Smith | 293534b | 2015-08-18 20:39:29 +0000 | [diff] [blame] | 476 | auto *Context = new ASTContext(getLangOpts(), PP.getSourceManager(), |
| 477 | PP.getIdentifierTable(), PP.getSelectorTable(), |
| 478 | PP.getBuiltinInfo()); |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 479 | Context->InitBuiltinTypes(getTarget(), getAuxTarget()); |
Richard Smith | 293534b | 2015-08-18 20:39:29 +0000 | [diff] [blame] | 480 | setASTContext(Context); |
Daniel Dunbar | df3e30c | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 481 | } |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 482 | |
| 483 | // ExternalASTSource |
| 484 | |
Nico Weber | 824285e | 2014-05-08 04:26:47 +0000 | [diff] [blame] | 485 | void CompilerInstance::createPCHExternalASTSource( |
| 486 | StringRef Path, bool DisablePCHValidation, bool AllowPCHWithCompilerErrors, |
| 487 | void *DeserializationListener, bool OwnDeserializationListener) { |
Sebastian Redl | 009e7f2 | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 488 | bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; |
Richard Smith | 4eca9b9 | 2015-02-04 23:37:59 +0000 | [diff] [blame] | 489 | ModuleManager = createPCHExternalASTSource( |
Nico Weber | 824285e | 2014-05-08 04:26:47 +0000 | [diff] [blame] | 490 | Path, getHeaderSearchOpts().Sysroot, DisablePCHValidation, |
Duncan P. N. Exon Smith | 8bef5cd | 2019-03-09 17:33:56 +0000 | [diff] [blame] | 491 | AllowPCHWithCompilerErrors, getPreprocessor(), getModuleCache(), |
| 492 | getASTContext(), getPCHContainerReader(), |
| 493 | getFrontendOpts().ModuleFileExtensions, TheDependencyFileGenerator.get(), |
| 494 | DependencyCollectors, DeserializationListener, OwnDeserializationListener, |
| 495 | Preamble, getFrontendOpts().UseGlobalModuleIndex); |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Richard Smith | 4eca9b9 | 2015-02-04 23:37:59 +0000 | [diff] [blame] | 498 | IntrusiveRefCntPtr<ASTReader> CompilerInstance::createPCHExternalASTSource( |
Yaron Keren | 5b81606 | 2015-07-06 08:47:15 +0000 | [diff] [blame] | 499 | StringRef Path, StringRef Sysroot, bool DisablePCHValidation, |
Duncan P. N. Exon Smith | 8bef5cd | 2019-03-09 17:33:56 +0000 | [diff] [blame] | 500 | bool AllowPCHWithCompilerErrors, Preprocessor &PP, |
| 501 | InMemoryModuleCache &ModuleCache, ASTContext &Context, |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 502 | const PCHContainerReader &PCHContainerRdr, |
David Blaikie | 61137e1 | 2017-01-05 18:23:18 +0000 | [diff] [blame] | 503 | ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, |
Graydon Hoare | ce539b5 | 2017-03-29 17:33:09 +0000 | [diff] [blame] | 504 | DependencyFileGenerator *DependencyFile, |
| 505 | ArrayRef<std::shared_ptr<DependencyCollector>> DependencyCollectors, |
Nico Weber | 824285e | 2014-05-08 04:26:47 +0000 | [diff] [blame] | 506 | void *DeserializationListener, bool OwnDeserializationListener, |
| 507 | bool Preamble, bool UseGlobalModuleIndex) { |
Ben Langmuir | dcf7386 | 2014-03-12 00:06:17 +0000 | [diff] [blame] | 508 | HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts(); |
| 509 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 510 | IntrusiveRefCntPtr<ASTReader> Reader(new ASTReader( |
Duncan P. N. Exon Smith | 8bef5cd | 2019-03-09 17:33:56 +0000 | [diff] [blame] | 511 | PP, ModuleCache, &Context, PCHContainerRdr, Extensions, |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 512 | Sysroot.empty() ? "" : Sysroot.data(), DisablePCHValidation, |
| 513 | AllowPCHWithCompilerErrors, /*AllowConfigurationMismatch*/ false, |
| 514 | HSOpts.ModulesValidateSystemHeaders, UseGlobalModuleIndex)); |
Richard Smith | 4eca9b9 | 2015-02-04 23:37:59 +0000 | [diff] [blame] | 515 | |
| 516 | // We need the external source to be set up before we read the AST, because |
| 517 | // eagerly-deserialized declarations may use it. |
| 518 | Context.setExternalSource(Reader.get()); |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 519 | |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 520 | Reader->setDeserializationListener( |
Nico Weber | 824285e | 2014-05-08 04:26:47 +0000 | [diff] [blame] | 521 | static_cast<ASTDeserializationListener *>(DeserializationListener), |
| 522 | /*TakeOwnership=*/OwnDeserializationListener); |
Graydon Hoare | ce539b5 | 2017-03-29 17:33:09 +0000 | [diff] [blame] | 523 | |
| 524 | if (DependencyFile) |
| 525 | DependencyFile->AttachToASTReader(*Reader); |
| 526 | for (auto &Listener : DependencyCollectors) |
| 527 | Listener->attachToASTReader(*Reader); |
| 528 | |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 529 | switch (Reader->ReadAST(Path, |
| 530 | Preamble ? serialization::MK_Preamble |
Douglas Gregor | 4b29c16 | 2012-10-22 23:51:00 +0000 | [diff] [blame] | 531 | : serialization::MK_PCH, |
Argyrios Kyrtzidis | 2ec2936 | 2012-11-15 18:57:22 +0000 | [diff] [blame] | 532 | SourceLocation(), |
Ben Langmuir | 3d4417c | 2014-02-07 17:31:11 +0000 | [diff] [blame] | 533 | ASTReader::ARR_None)) { |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 534 | case ASTReader::Success: |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 535 | // Set the predefines buffer as suggested by the PCH reader. Typically, the |
| 536 | // predefines buffer will be empty. |
| 537 | PP.setPredefines(Reader->getSuggestedPredefines()); |
Richard Smith | 4eca9b9 | 2015-02-04 23:37:59 +0000 | [diff] [blame] | 538 | return Reader; |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 539 | |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 540 | case ASTReader::Failure: |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 541 | // Unrecoverable failure: don't even try to process the input file. |
| 542 | break; |
| 543 | |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 544 | case ASTReader::Missing: |
Douglas Gregor | c9ad5fb | 2012-10-22 22:50:17 +0000 | [diff] [blame] | 545 | case ASTReader::OutOfDate: |
| 546 | case ASTReader::VersionMismatch: |
| 547 | case ASTReader::ConfigurationMismatch: |
| 548 | case ASTReader::HadErrors: |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 549 | // No suitable PCH file could be found. Return an error. |
| 550 | break; |
| 551 | } |
| 552 | |
Richard Smith | 4eca9b9 | 2015-02-04 23:37:59 +0000 | [diff] [blame] | 553 | Context.setExternalSource(nullptr); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 554 | return nullptr; |
Daniel Dunbar | 599313e | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 555 | } |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 556 | |
| 557 | // Code Completion |
| 558 | |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 559 | static bool EnableCodeCompletion(Preprocessor &PP, |
Benjamin Kramer | 0772c42 | 2016-02-13 13:42:54 +0000 | [diff] [blame] | 560 | StringRef Filename, |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 561 | unsigned Line, |
| 562 | unsigned Column) { |
| 563 | // Tell the source manager to chop off the given file at a specific |
| 564 | // line and column. |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 565 | const FileEntry *Entry = PP.getFileManager().getFile(Filename); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 566 | if (!Entry) { |
| 567 | PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file) |
| 568 | << Filename; |
| 569 | return true; |
| 570 | } |
| 571 | |
| 572 | // Truncate the named file at the given line/column. |
| 573 | PP.SetCodeCompletionPoint(Entry, Line, Column); |
| 574 | return false; |
| 575 | } |
| 576 | |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 577 | void CompilerInstance::createCodeCompletionConsumer() { |
| 578 | const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt; |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 579 | if (!CompletionConsumer) { |
Erik Verbruggen | 2fca3c2 | 2012-04-12 10:31:12 +0000 | [diff] [blame] | 580 | setCodeCompletionConsumer( |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 581 | createCodeCompletionConsumer(getPreprocessor(), |
| 582 | Loc.FileName, Loc.Line, Loc.Column, |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 583 | getFrontendOpts().CodeCompleteOpts, |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 584 | llvm::outs())); |
| 585 | if (!CompletionConsumer) |
| 586 | return; |
| 587 | } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName, |
| 588 | Loc.Line, Loc.Column)) { |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 589 | setCodeCompletionConsumer(nullptr); |
Douglas Gregor | 00a0cf7 | 2010-03-16 06:04:47 +0000 | [diff] [blame] | 590 | return; |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 591 | } |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Kovarththanan Rajaratnam | 5505dff | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 594 | void CompilerInstance::createFrontendTimer() { |
Matthias Braun | ae032b6 | 2016-11-18 19:43:25 +0000 | [diff] [blame] | 595 | FrontendTimerGroup.reset( |
| 596 | new llvm::TimerGroup("frontend", "Clang front-end time report")); |
Richard Smith | ce18a18 | 2015-07-14 00:26:00 +0000 | [diff] [blame] | 597 | FrontendTimer.reset( |
Matthias Braun | ae032b6 | 2016-11-18 19:43:25 +0000 | [diff] [blame] | 598 | new llvm::Timer("frontend", "Clang front-end timer", |
| 599 | *FrontendTimerGroup)); |
Kovarththanan Rajaratnam | 5505dff | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 602 | CodeCompleteConsumer * |
| 603 | CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP, |
Yaron Keren | 5b81606 | 2015-07-06 08:47:15 +0000 | [diff] [blame] | 604 | StringRef Filename, |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 605 | unsigned Line, |
| 606 | unsigned Column, |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 607 | const CodeCompleteOptions &Opts, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 608 | raw_ostream &OS) { |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 609 | if (EnableCodeCompletion(PP, Filename, Line, Column)) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 610 | return nullptr; |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 611 | |
| 612 | // Set up the creation routine for code-completion. |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 613 | return new PrintingCodeCompleteConsumer(Opts, OS); |
Daniel Dunbar | f7093b5 | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 614 | } |
Daniel Dunbar | 566eeb2 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 615 | |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 616 | void CompilerInstance::createSema(TranslationUnitKind TUKind, |
Douglas Gregor | 0e93f01 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 617 | CodeCompleteConsumer *CompletionConsumer) { |
| 618 | TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(), |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 619 | TUKind, CompletionConsumer)); |
Benjamin Kramer | 7de9969 | 2016-11-16 18:15:26 +0000 | [diff] [blame] | 620 | // Attach the external sema source if there is any. |
| 621 | if (ExternalSemaSrc) { |
| 622 | TheSema->addExternalSource(ExternalSemaSrc.get()); |
| 623 | ExternalSemaSrc->InitializeSema(*TheSema); |
| 624 | } |
Douglas Gregor | 0e93f01 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Daniel Dunbar | 566eeb2 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 627 | // Output Files |
| 628 | |
Rafael Espindola | 269ec0f | 2015-04-10 14:11:52 +0000 | [diff] [blame] | 629 | void CompilerInstance::addOutputFile(OutputFile &&OutFile) { |
Rafael Espindola | 269ec0f | 2015-04-10 14:11:52 +0000 | [diff] [blame] | 630 | OutputFiles.push_back(std::move(OutFile)); |
Daniel Dunbar | 566eeb2 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Kovarththanan Rajaratnam | 1c558cd | 2010-03-06 12:07:48 +0000 | [diff] [blame] | 633 | void CompilerInstance::clearOutputFiles(bool EraseFiles) { |
Reid Kleckner | 0aa128e | 2015-04-10 17:27:58 +0000 | [diff] [blame] | 634 | for (OutputFile &OF : OutputFiles) { |
Reid Kleckner | 0aa128e | 2015-04-10 17:27:58 +0000 | [diff] [blame] | 635 | if (!OF.TempFilename.empty()) { |
Anders Carlsson | b5c356a | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 636 | if (EraseFiles) { |
Reid Kleckner | 0aa128e | 2015-04-10 17:27:58 +0000 | [diff] [blame] | 637 | llvm::sys::fs::remove(OF.TempFilename); |
Anders Carlsson | b5c356a | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 638 | } else { |
Reid Kleckner | 0aa128e | 2015-04-10 17:27:58 +0000 | [diff] [blame] | 639 | SmallString<128> NewOutFile(OF.Filename); |
Anders Carlsson | b5c356a | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 640 | |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 641 | // If '-working-directory' was passed, the output filename should be |
| 642 | // relative to that. |
Anders Carlsson | 9ba8fb1 | 2011-03-14 01:13:54 +0000 | [diff] [blame] | 643 | FileMgr->FixupRelativePath(NewOutFile); |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 644 | if (std::error_code ec = |
Reid Kleckner | 0aa128e | 2015-04-10 17:27:58 +0000 | [diff] [blame] | 645 | llvm::sys::fs::rename(OF.TempFilename, NewOutFile)) { |
Manuel Klimek | 3ef9c44 | 2012-05-16 20:55:58 +0000 | [diff] [blame] | 646 | getDiagnostics().Report(diag::err_unable_to_rename_temp) |
Reid Kleckner | 0aa128e | 2015-04-10 17:27:58 +0000 | [diff] [blame] | 647 | << OF.TempFilename << OF.Filename << ec.message(); |
Anders Carlsson | b5c356a | 2011-03-06 22:25:35 +0000 | [diff] [blame] | 648 | |
Reid Kleckner | 0aa128e | 2015-04-10 17:27:58 +0000 | [diff] [blame] | 649 | llvm::sys::fs::remove(OF.TempFilename); |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 650 | } |
| 651 | } |
Reid Kleckner | 0aa128e | 2015-04-10 17:27:58 +0000 | [diff] [blame] | 652 | } else if (!OF.Filename.empty() && EraseFiles) |
| 653 | llvm::sys::fs::remove(OF.Filename); |
Daniel Dunbar | 566eeb2 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 654 | } |
| 655 | OutputFiles.clear(); |
Richard Smith | 86a3ef5 | 2017-06-09 21:24:02 +0000 | [diff] [blame] | 656 | if (DeleteBuiltModules) { |
| 657 | for (auto &Module : BuiltModules) |
| 658 | llvm::sys::fs::remove(Module.second); |
| 659 | BuiltModules.clear(); |
| 660 | } |
Rafael Espindola | 2f16bc1 | 2015-04-14 15:15:49 +0000 | [diff] [blame] | 661 | NonSeekStream.reset(); |
Daniel Dunbar | 566eeb2 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 664 | std::unique_ptr<raw_pwrite_stream> |
Rafael Espindola | 2f16bc1 | 2015-04-14 15:15:49 +0000 | [diff] [blame] | 665 | CompilerInstance::createDefaultOutputFile(bool Binary, StringRef InFile, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 666 | StringRef Extension) { |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 667 | return createOutputFile(getFrontendOpts().OutputFile, Binary, |
Daniel Dunbar | ae77b3d | 2012-03-03 00:36:06 +0000 | [diff] [blame] | 668 | /*RemoveFileOnSignal=*/true, InFile, Extension, |
| 669 | /*UseTemporary=*/true); |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 672 | std::unique_ptr<raw_pwrite_stream> CompilerInstance::createNullOutputFile() { |
| 673 | return llvm::make_unique<llvm::raw_null_ostream>(); |
Alp Toker | ea04672 | 2014-06-03 17:23:34 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 676 | std::unique_ptr<raw_pwrite_stream> |
Rafael Espindola | 2f16bc1 | 2015-04-14 15:15:49 +0000 | [diff] [blame] | 677 | CompilerInstance::createOutputFile(StringRef OutputPath, bool Binary, |
| 678 | bool RemoveFileOnSignal, StringRef InFile, |
| 679 | StringRef Extension, bool UseTemporary, |
Daniel Dunbar | b9c62c0 | 2012-03-03 00:36:02 +0000 | [diff] [blame] | 680 | bool CreateMissingDirectories) { |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 681 | std::string OutputPathName, TempPathName; |
| 682 | std::error_code EC; |
Rafael Espindola | 2f16bc1 | 2015-04-14 15:15:49 +0000 | [diff] [blame] | 683 | std::unique_ptr<raw_pwrite_stream> OS = createOutputFile( |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 684 | OutputPath, EC, Binary, RemoveFileOnSignal, InFile, Extension, |
Rafael Espindola | c80a406 | 2015-04-10 14:30:43 +0000 | [diff] [blame] | 685 | UseTemporary, CreateMissingDirectories, &OutputPathName, &TempPathName); |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 686 | if (!OS) { |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 687 | getDiagnostics().Report(diag::err_fe_unable_to_open_output) << OutputPath |
| 688 | << EC.message(); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 689 | return nullptr; |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | // Add the output file -- but don't try to remove "-", since this means we are |
| 693 | // using stdin. |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 694 | addOutputFile( |
| 695 | OutputFile((OutputPathName != "-") ? OutputPathName : "", TempPathName)); |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 696 | |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 697 | return OS; |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Rafael Espindola | 2f16bc1 | 2015-04-14 15:15:49 +0000 | [diff] [blame] | 700 | std::unique_ptr<llvm::raw_pwrite_stream> CompilerInstance::createOutputFile( |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 701 | StringRef OutputPath, std::error_code &Error, bool Binary, |
| 702 | bool RemoveFileOnSignal, StringRef InFile, StringRef Extension, |
| 703 | bool UseTemporary, bool CreateMissingDirectories, |
| 704 | std::string *ResultPathName, std::string *TempPathName) { |
Daniel Dunbar | b9c62c0 | 2012-03-03 00:36:02 +0000 | [diff] [blame] | 705 | assert((!CreateMissingDirectories || UseTemporary) && |
| 706 | "CreateMissingDirectories is only allowed when using temporary files"); |
| 707 | |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 708 | std::string OutFile, TempFile; |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 709 | if (!OutputPath.empty()) { |
| 710 | OutFile = OutputPath; |
| 711 | } else if (InFile == "-") { |
| 712 | OutFile = "-"; |
| 713 | } else if (!Extension.empty()) { |
Rafael Espindola | 399ab33 | 2013-06-26 04:32:59 +0000 | [diff] [blame] | 714 | SmallString<128> Path(InFile); |
| 715 | llvm::sys::path::replace_extension(Path, Extension); |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 716 | OutFile = Path.str(); |
| 717 | } else { |
| 718 | OutFile = "-"; |
| 719 | } |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 720 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 721 | std::unique_ptr<llvm::raw_fd_ostream> OS; |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 722 | std::string OSFile; |
| 723 | |
Rafael Espindola | 73c23a7 | 2013-06-27 18:26:26 +0000 | [diff] [blame] | 724 | if (UseTemporary) { |
| 725 | if (OutFile == "-") |
| 726 | UseTemporary = false; |
| 727 | else { |
| 728 | llvm::sys::fs::file_status Status; |
| 729 | llvm::sys::fs::status(OutputPath, Status); |
| 730 | if (llvm::sys::fs::exists(Status)) { |
| 731 | // Fail early if we can't write to the final destination. |
Douglas Katzman | 8bfac2c | 2015-09-17 16:45:12 +0000 | [diff] [blame] | 732 | if (!llvm::sys::fs::can_write(OutputPath)) { |
Rafael Espindola | ee4e08b | 2015-10-05 11:49:35 +0000 | [diff] [blame] | 733 | Error = make_error_code(llvm::errc::operation_not_permitted); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 734 | return nullptr; |
Douglas Katzman | 8bfac2c | 2015-09-17 16:45:12 +0000 | [diff] [blame] | 735 | } |
Rafael Espindola | 73c23a7 | 2013-06-27 18:26:26 +0000 | [diff] [blame] | 736 | |
| 737 | // Don't use a temporary if the output is a special file. This handles |
| 738 | // things like '-o /dev/null' |
| 739 | if (!llvm::sys::fs::is_regular_file(Status)) |
| 740 | UseTemporary = false; |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 741 | } |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | |
Rafael Espindola | 73c23a7 | 2013-06-27 18:26:26 +0000 | [diff] [blame] | 745 | if (UseTemporary) { |
Rafael Espindola | 73c23a7 | 2013-06-27 18:26:26 +0000 | [diff] [blame] | 746 | // Create a temporary file. |
Nico Weber | 2db4719 | 2017-08-08 16:21:23 +0000 | [diff] [blame] | 747 | // Insert -%%%%%%%% before the extension (if any), and because some tools |
| 748 | // (noticeable, clang's own GlobalModuleIndex.cpp) glob for build |
| 749 | // artifacts, also append .tmp. |
| 750 | StringRef OutputExtension = llvm::sys::path::extension(OutFile); |
| 751 | SmallString<128> TempPath = |
| 752 | StringRef(OutFile).drop_back(OutputExtension.size()); |
Rafael Espindola | 73c23a7 | 2013-06-27 18:26:26 +0000 | [diff] [blame] | 753 | TempPath += "-%%%%%%%%"; |
Nico Weber | 2db4719 | 2017-08-08 16:21:23 +0000 | [diff] [blame] | 754 | TempPath += OutputExtension; |
| 755 | TempPath += ".tmp"; |
Rafael Espindola | 73c23a7 | 2013-06-27 18:26:26 +0000 | [diff] [blame] | 756 | int fd; |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 757 | std::error_code EC = |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 758 | llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath); |
Rafael Espindola | 157f34b | 2013-06-28 03:49:04 +0000 | [diff] [blame] | 759 | |
| 760 | if (CreateMissingDirectories && |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 761 | EC == llvm::errc::no_such_file_or_directory) { |
Rafael Espindola | 157f34b | 2013-06-28 03:49:04 +0000 | [diff] [blame] | 762 | StringRef Parent = llvm::sys::path::parent_path(OutputPath); |
| 763 | EC = llvm::sys::fs::create_directories(Parent); |
| 764 | if (!EC) { |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 765 | EC = llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath); |
Rafael Espindola | 157f34b | 2013-06-28 03:49:04 +0000 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | |
| 769 | if (!EC) { |
NAKAMURA Takumi | 69f3528 | 2014-08-11 06:53:11 +0000 | [diff] [blame] | 770 | OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true)); |
Rafael Espindola | 73c23a7 | 2013-06-27 18:26:26 +0000 | [diff] [blame] | 771 | OSFile = TempFile = TempPath.str(); |
| 772 | } |
| 773 | // If we failed to create the temporary, fallback to writing to the file |
| 774 | // directly. This handles the corner case where we cannot write to the |
| 775 | // directory, but can write to the file. |
| 776 | } |
| 777 | |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 778 | if (!OS) { |
| 779 | OSFile = OutFile; |
NAKAMURA Takumi | 69f3528 | 2014-08-11 06:53:11 +0000 | [diff] [blame] | 780 | OS.reset(new llvm::raw_fd_ostream( |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 781 | OSFile, Error, |
NAKAMURA Takumi | 69f3528 | 2014-08-11 06:53:11 +0000 | [diff] [blame] | 782 | (Binary ? llvm::sys::fs::F_None : llvm::sys::fs::F_Text))); |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 783 | if (Error) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 784 | return nullptr; |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 785 | } |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 786 | |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 787 | // Make sure the out stream file gets removed if we crash. |
Daniel Dunbar | e326f9b | 2011-01-31 22:00:42 +0000 | [diff] [blame] | 788 | if (RemoveFileOnSignal) |
Rafael Espindola | 18556de | 2013-06-13 21:02:40 +0000 | [diff] [blame] | 789 | llvm::sys::RemoveFileOnSignal(OSFile); |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 790 | |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 791 | if (ResultPathName) |
| 792 | *ResultPathName = OutFile; |
Argyrios Kyrtzidis | d059997 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 793 | if (TempPathName) |
| 794 | *TempPathName = TempFile; |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 795 | |
Rafael Espindola | 2f16bc1 | 2015-04-14 15:15:49 +0000 | [diff] [blame] | 796 | if (!Binary || OS->supportsSeeking()) |
| 797 | return std::move(OS); |
| 798 | |
| 799 | auto B = llvm::make_unique<llvm::buffer_ostream>(*OS); |
| 800 | assert(!NonSeekStream); |
| 801 | NonSeekStream = std::move(OS); |
| 802 | return std::move(B); |
Daniel Dunbar | 420b0f1 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 803 | } |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 804 | |
| 805 | // Initialization Utilities |
| 806 | |
Argyrios Kyrtzidis | 1b3240b | 2012-11-09 19:40:33 +0000 | [diff] [blame] | 807 | bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input){ |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 808 | return InitializeSourceManager( |
| 809 | Input, getDiagnostics(), getFileManager(), getSourceManager(), |
| 810 | hasPreprocessor() ? &getPreprocessor().getHeaderSearchInfo() : nullptr, |
Nico Weber | 4b5aede | 2016-03-13 02:44:13 +0000 | [diff] [blame] | 811 | getDependencyOutputOpts(), getFrontendOpts()); |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Nico Weber | 2ca4be9 | 2016-03-01 23:16:44 +0000 | [diff] [blame] | 814 | // static |
Nico Weber | 4b5aede | 2016-03-13 02:44:13 +0000 | [diff] [blame] | 815 | bool CompilerInstance::InitializeSourceManager( |
| 816 | const FrontendInputFile &Input, DiagnosticsEngine &Diags, |
| 817 | FileManager &FileMgr, SourceManager &SourceMgr, HeaderSearch *HS, |
| 818 | DependencyOutputOptions &DepOpts, const FrontendOptions &Opts) { |
Richard Smith | f3f8461 | 2017-06-29 02:19:42 +0000 | [diff] [blame] | 819 | SrcMgr::CharacteristicKind Kind = |
| 820 | Input.getKind().getFormat() == InputKind::ModuleMap |
| 821 | ? Input.isSystem() ? SrcMgr::C_System_ModuleMap |
| 822 | : SrcMgr::C_User_ModuleMap |
| 823 | : Input.isSystem() ? SrcMgr::C_System : SrcMgr::C_User; |
Argyrios Kyrtzidis | 1b3240b | 2012-11-09 19:40:33 +0000 | [diff] [blame] | 824 | |
Argyrios Kyrtzidis | 6566e23 | 2012-11-09 19:40:45 +0000 | [diff] [blame] | 825 | if (Input.isBuffer()) { |
Richard Smith | 6d9bc27 | 2017-09-09 01:14:04 +0000 | [diff] [blame] | 826 | SourceMgr.setMainFileID(SourceMgr.createFileID(SourceManager::Unowned, |
| 827 | Input.getBuffer(), Kind)); |
Yaron Keren | 8b56366 | 2015-10-03 10:46:20 +0000 | [diff] [blame] | 828 | assert(SourceMgr.getMainFileID().isValid() && |
Argyrios Kyrtzidis | 6566e23 | 2012-11-09 19:40:45 +0000 | [diff] [blame] | 829 | "Couldn't establish MainFileID!"); |
| 830 | return true; |
| 831 | } |
| 832 | |
| 833 | StringRef InputFile = Input.getFile(); |
| 834 | |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 835 | // Figure out where to get and map in the main file. |
| 836 | if (InputFile != "-") { |
Erich Keane | 76675de | 2018-07-05 17:22:13 +0000 | [diff] [blame] | 837 | const FileEntry *File = FileMgr.getFile(InputFile, /*OpenFile=*/true); |
Dan Gohman | 5276521 | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 838 | if (!File) { |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 839 | Diags.Report(diag::err_fe_error_reading) << InputFile; |
| 840 | return false; |
| 841 | } |
Daniel Dunbar | e2951f4 | 2012-11-05 22:53:33 +0000 | [diff] [blame] | 842 | |
| 843 | // The natural SourceManager infrastructure can't currently handle named |
| 844 | // pipes, but we would at least like to accept them for the main |
Benjamin Kramer | 3841fa3 | 2013-08-12 13:46:52 +0000 | [diff] [blame] | 845 | // file. Detect them here, read them with the volatile flag so FileMgr will |
| 846 | // pick up the correct size, and simply override their contents as we do for |
| 847 | // STDIN. |
Daniel Dunbar | e2951f4 | 2012-11-05 22:53:33 +0000 | [diff] [blame] | 848 | if (File->isNamedPipe()) { |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 849 | auto MB = FileMgr.getBufferForFile(File, /*isVolatile=*/true); |
| 850 | if (MB) { |
Benjamin Kramer | 3841fa3 | 2013-08-12 13:46:52 +0000 | [diff] [blame] | 851 | // Create a new virtual file that will have the correct size. |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 852 | File = FileMgr.getVirtualFile(InputFile, (*MB)->getBufferSize(), 0); |
| 853 | SourceMgr.overrideFileContents(File, std::move(*MB)); |
Benjamin Kramer | 3841fa3 | 2013-08-12 13:46:52 +0000 | [diff] [blame] | 854 | } else { |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 855 | Diags.Report(diag::err_cannot_open_file) << InputFile |
| 856 | << MB.getError().message(); |
Daniel Dunbar | e2951f4 | 2012-11-05 22:53:33 +0000 | [diff] [blame] | 857 | return false; |
| 858 | } |
Daniel Dunbar | e2951f4 | 2012-11-05 22:53:33 +0000 | [diff] [blame] | 859 | } |
Daniel Dunbar | db0745a | 2012-11-27 00:04:16 +0000 | [diff] [blame] | 860 | |
Alp Toker | b671e34 | 2014-05-21 01:12:41 +0000 | [diff] [blame] | 861 | SourceMgr.setMainFileID( |
| 862 | SourceMgr.createFileID(File, SourceLocation(), Kind)); |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 863 | } else { |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 864 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> SBOrErr = |
| 865 | llvm::MemoryBuffer::getSTDIN(); |
| 866 | if (std::error_code EC = SBOrErr.getError()) { |
| 867 | Diags.Report(diag::err_fe_error_reading_stdin) << EC.message(); |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 868 | return false; |
| 869 | } |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 870 | std::unique_ptr<llvm::MemoryBuffer> SB = std::move(SBOrErr.get()); |
| 871 | |
Dan Gohman | 2f76cd7 | 2010-10-26 23:21:25 +0000 | [diff] [blame] | 872 | const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(), |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 873 | SB->getBufferSize(), 0); |
Alp Toker | b671e34 | 2014-05-21 01:12:41 +0000 | [diff] [blame] | 874 | SourceMgr.setMainFileID( |
| 875 | SourceMgr.createFileID(File, SourceLocation(), Kind)); |
David Blaikie | 49cc318 | 2014-08-27 20:54:45 +0000 | [diff] [blame] | 876 | SourceMgr.overrideFileContents(File, std::move(SB)); |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Yaron Keren | 8b56366 | 2015-10-03 10:46:20 +0000 | [diff] [blame] | 879 | assert(SourceMgr.getMainFileID().isValid() && |
Dan Gohman | 5276521 | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 880 | "Couldn't establish MainFileID!"); |
Daniel Dunbar | 409e890 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 881 | return true; |
| 882 | } |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 883 | |
| 884 | // High-Level Operations |
| 885 | |
| 886 | bool CompilerInstance::ExecuteAction(FrontendAction &Act) { |
| 887 | assert(hasDiagnostics() && "Diagnostics engine is not initialized!"); |
| 888 | assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!"); |
| 889 | assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!"); |
| 890 | |
| 891 | // FIXME: Take this as an argument, once all the APIs we used have moved to |
| 892 | // taking it as an input instead of hard-coding llvm::errs. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 893 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 894 | |
Richard Smith | d6509cf | 2018-09-15 01:21:15 +0000 | [diff] [blame] | 895 | if (!Act.PrepareToExecute(*this)) |
| 896 | return false; |
| 897 | |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 898 | // Create the target instance. |
Alp Toker | 8075808 | 2014-07-06 05:26:44 +0000 | [diff] [blame] | 899 | setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 900 | getInvocation().TargetOpts)); |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 901 | if (!hasTarget()) |
| 902 | return false; |
| 903 | |
Gheorghe-Teodor Bercea | 59d7b77 | 2017-06-29 15:49:03 +0000 | [diff] [blame] | 904 | // Create TargetInfo for the other side of CUDA and OpenMP compilation. |
| 905 | if ((getLangOpts().CUDA || getLangOpts().OpenMPIsDevice) && |
| 906 | !getFrontendOpts().AuxTriple.empty()) { |
Justin Lebar | 76945b2 | 2016-04-29 23:05:19 +0000 | [diff] [blame] | 907 | auto TO = std::make_shared<TargetOptions>(); |
Petr Hosek | 7b27454 | 2018-08-08 22:23:57 +0000 | [diff] [blame] | 908 | TO->Triple = llvm::Triple::normalize(getFrontendOpts().AuxTriple); |
Justin Lebar | 76945b2 | 2016-04-29 23:05:19 +0000 | [diff] [blame] | 909 | TO->HostTriple = getTarget().getTriple().str(); |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 910 | setAuxTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), TO)); |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 913 | // Inform the target of the language options. |
| 914 | // |
| 915 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 916 | // created. This complexity should be lifted elsewhere. |
Alp Toker | 7443797 | 2014-07-06 05:14:24 +0000 | [diff] [blame] | 917 | getTarget().adjust(getLangOpts()); |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 918 | |
Yaxun Liu | 2c17e82 | 2016-08-09 19:43:38 +0000 | [diff] [blame] | 919 | // Adjust target options based on codegen options. |
| 920 | getTarget().adjustTargetOptions(getCodeGenOpts(), getTargetOpts()); |
| 921 | |
Yaxun Liu | 95f2ca5 | 2019-01-30 12:26:54 +0000 | [diff] [blame] | 922 | if (auto *Aux = getAuxTarget()) |
| 923 | getTarget().setAuxTarget(Aux); |
| 924 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 925 | // rewriter project will change target built-in bool type from its default. |
Fariborz Jahanian | 29898f4 | 2012-04-16 21:03:30 +0000 | [diff] [blame] | 926 | if (getFrontendOpts().ProgramAction == frontend::RewriteObjC) |
| 927 | getTarget().noSignedCharForObjCBool(); |
| 928 | |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 929 | // Validate/process some options. |
| 930 | if (getHeaderSearchOpts().Verbose) |
| 931 | OS << "clang -cc1 version " CLANG_VERSION_STRING |
Alp Toker | f988d00 | 2014-06-06 10:36:22 +0000 | [diff] [blame] | 932 | << " based upon " << BACKEND_PACKAGE_STRING |
Sebastian Pop | 8188c8a | 2011-11-01 21:33:06 +0000 | [diff] [blame] | 933 | << " default target " << llvm::sys::getDefaultTargetTriple() << "\n"; |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 934 | |
| 935 | if (getFrontendOpts().ShowTimers) |
| 936 | createFrontendTimer(); |
| 937 | |
Matthias Braun | abb6eea | 2016-09-26 18:53:34 +0000 | [diff] [blame] | 938 | if (getFrontendOpts().ShowStats || !getFrontendOpts().StatsFile.empty()) |
Matthias Braun | ec1c5a2 | 2016-09-27 19:38:59 +0000 | [diff] [blame] | 939 | llvm::EnableStatistics(false); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 940 | |
Vedant Kumar | 5b60ad6 | 2015-11-16 00:59:34 +0000 | [diff] [blame] | 941 | for (const FrontendInputFile &FIF : getFrontendOpts().Inputs) { |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 942 | // Reset the ID tables if we are reusing the SourceManager and parsing |
| 943 | // regular files. |
| 944 | if (hasSourceManager() && !Act.isModelParsingAction()) |
Daniel Dunbar | aed46fc | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 945 | getSourceManager().clearIDTables(); |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 946 | |
Vedant Kumar | 5b60ad6 | 2015-11-16 00:59:34 +0000 | [diff] [blame] | 947 | if (Act.BeginSourceFile(*this, FIF)) { |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 948 | Act.Execute(); |
| 949 | Act.EndSourceFile(); |
| 950 | } |
| 951 | } |
| 952 | |
Argyrios Kyrtzidis | 7910d7b | 2011-12-07 05:52:12 +0000 | [diff] [blame] | 953 | // Notify the diagnostic client that all files were processed. |
| 954 | getDiagnostics().getClient()->finish(); |
| 955 | |
Chris Lattner | 198cb4d | 2010-04-07 18:47:42 +0000 | [diff] [blame] | 956 | if (getDiagnosticOpts().ShowCarets) { |
Argyrios Kyrtzidis | c79346a | 2010-11-18 20:06:46 +0000 | [diff] [blame] | 957 | // We can have multiple diagnostics sharing one diagnostic client. |
| 958 | // Get the total number of warnings/errors from the client. |
| 959 | unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings(); |
| 960 | unsigned NumErrors = getDiagnostics().getClient()->getNumErrors(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 961 | |
Chris Lattner | 198cb4d | 2010-04-07 18:47:42 +0000 | [diff] [blame] | 962 | if (NumWarnings) |
| 963 | OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s"); |
| 964 | if (NumWarnings && NumErrors) |
| 965 | OS << " and "; |
| 966 | if (NumErrors) |
| 967 | OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s"); |
Justin Lebar | 78137ec | 2017-09-07 18:37:16 +0000 | [diff] [blame] | 968 | if (NumWarnings || NumErrors) { |
| 969 | OS << " generated"; |
| 970 | if (getLangOpts().CUDA) { |
| 971 | if (!getLangOpts().CUDAIsDevice) { |
| 972 | OS << " when compiling for host"; |
| 973 | } else { |
| 974 | OS << " when compiling for " << getTargetOpts().CPU; |
| 975 | } |
| 976 | } |
| 977 | OS << ".\n"; |
| 978 | } |
Chris Lattner | 198cb4d | 2010-04-07 18:47:42 +0000 | [diff] [blame] | 979 | } |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 980 | |
Matthias Braun | abb6eea | 2016-09-26 18:53:34 +0000 | [diff] [blame] | 981 | if (getFrontendOpts().ShowStats) { |
| 982 | if (hasFileManager()) { |
| 983 | getFileManager().PrintStats(); |
| 984 | OS << '\n'; |
| 985 | } |
| 986 | llvm::PrintStatistics(OS); |
| 987 | } |
| 988 | StringRef StatsFile = getFrontendOpts().StatsFile; |
| 989 | if (!StatsFile.empty()) { |
| 990 | std::error_code EC; |
| 991 | auto StatS = llvm::make_unique<llvm::raw_fd_ostream>(StatsFile, EC, |
| 992 | llvm::sys::fs::F_Text); |
| 993 | if (EC) { |
| 994 | getDiagnostics().Report(diag::warn_fe_unable_to_open_stats_file) |
| 995 | << StatsFile << EC.message(); |
| 996 | } else { |
| 997 | llvm::PrintStatisticsJSON(*StatS); |
| 998 | } |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Argyrios Kyrtzidis | bc46793 | 2010-11-18 21:13:57 +0000 | [diff] [blame] | 1001 | return !getDiagnostics().getClient()->getNumErrors(); |
Daniel Dunbar | 4f2bc55 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1004 | /// Determine the appropriate source input kind based on language |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 1005 | /// options. |
Richard Smith | 40c0efa | 2017-04-26 18:57:40 +0000 | [diff] [blame] | 1006 | static InputKind::Language getLanguageFromOptions(const LangOptions &LangOpts) { |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 1007 | if (LangOpts.OpenCL) |
Richard Smith | 40c0efa | 2017-04-26 18:57:40 +0000 | [diff] [blame] | 1008 | return InputKind::OpenCL; |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 1009 | if (LangOpts.CUDA) |
Richard Smith | 40c0efa | 2017-04-26 18:57:40 +0000 | [diff] [blame] | 1010 | return InputKind::CUDA; |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 1011 | if (LangOpts.ObjC) |
Richard Smith | 40c0efa | 2017-04-26 18:57:40 +0000 | [diff] [blame] | 1012 | return LangOpts.CPlusPlus ? InputKind::ObjCXX : InputKind::ObjC; |
| 1013 | return LangOpts.CPlusPlus ? InputKind::CXX : InputKind::C; |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1016 | /// Compile a module file for the given module, using the options |
Ben Langmuir | b797d59 | 2014-07-19 16:29:28 +0000 | [diff] [blame] | 1017 | /// provided by the importing compiler instance. Returns true if the module |
| 1018 | /// was built without errors. |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1019 | static bool |
| 1020 | compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc, |
| 1021 | StringRef ModuleName, FrontendInputFile Input, |
| 1022 | StringRef OriginalModuleMapFile, StringRef ModuleFileName, |
| 1023 | llvm::function_ref<void(CompilerInstance &)> PreBuildStep = |
| 1024 | [](CompilerInstance &) {}, |
| 1025 | llvm::function_ref<void(CompilerInstance &)> PostBuildStep = |
| 1026 | [](CompilerInstance &) {}) { |
Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 1027 | llvm::TimeTraceScope TimeScope("Module Compile", ModuleName); |
| 1028 | |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 1029 | // Construct a compiler invocation for creating this module. |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1030 | auto Invocation = |
| 1031 | std::make_shared<CompilerInvocation>(ImportingInstance.getInvocation()); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1032 | |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1033 | PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1034 | |
Douglas Gregor | 44bf68d | 2011-09-15 20:53:28 +0000 | [diff] [blame] | 1035 | // For any options that aren't intended to affect how a module is built, |
| 1036 | // reset them to their default values. |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 1037 | Invocation->getLangOpts()->resetNonModularOptions(); |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1038 | PPOpts.resetNonModularOptions(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1039 | |
Douglas Gregor | 5dc3899 | 2013-02-07 00:21:12 +0000 | [diff] [blame] | 1040 | // Remove any macro definitions that are explicitly ignored by the module. |
| 1041 | // They aren't supposed to affect how the module is built anyway. |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 1042 | HeaderSearchOptions &HSOpts = Invocation->getHeaderSearchOpts(); |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 1043 | PPOpts.Macros.erase( |
| 1044 | std::remove_if(PPOpts.Macros.begin(), PPOpts.Macros.end(), |
| 1045 | [&HSOpts](const std::pair<std::string, bool> &def) { |
| 1046 | StringRef MacroDef = def.first; |
Justin Lebar | 5e83dfe | 2016-10-21 21:45:01 +0000 | [diff] [blame] | 1047 | return HSOpts.ModulesIgnoreMacros.count( |
| 1048 | llvm::CachedHashString(MacroDef.split('=').first)) > 0; |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 1049 | }), |
| 1050 | PPOpts.Macros.end()); |
Douglas Gregor | 5dc3899 | 2013-02-07 00:21:12 +0000 | [diff] [blame] | 1051 | |
Bruno Cardoso Lopes | 970b281 | 2018-03-20 22:36:39 +0000 | [diff] [blame] | 1052 | // If the original compiler invocation had -fmodule-name, pass it through. |
| 1053 | Invocation->getLangOpts()->ModuleName = |
| 1054 | ImportingInstance.getInvocation().getLangOpts()->ModuleName; |
| 1055 | |
Douglas Gregor | 7d106e4 | 2011-11-15 19:35:01 +0000 | [diff] [blame] | 1056 | // Note the name of the module we're building. |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1057 | Invocation->getLangOpts()->CurrentModule = ModuleName; |
Douglas Gregor | 7d106e4 | 2011-11-15 19:35:01 +0000 | [diff] [blame] | 1058 | |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1059 | // Make sure that the failed-module structure has been allocated in |
| 1060 | // the importing instance, and propagate the pointer to the newly-created |
| 1061 | // instance. |
| 1062 | PreprocessorOptions &ImportingPPOpts |
| 1063 | = ImportingInstance.getInvocation().getPreprocessorOpts(); |
| 1064 | if (!ImportingPPOpts.FailedModules) |
David Blaikie | f95113d | 2017-01-05 19:11:31 +0000 | [diff] [blame] | 1065 | ImportingPPOpts.FailedModules = |
| 1066 | std::make_shared<PreprocessorOptions::FailedModulesSet>(); |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1067 | PPOpts.FailedModules = ImportingPPOpts.FailedModules; |
| 1068 | |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1069 | // If there is a module map file, build the module using the module map. |
Douglas Gregor | 44bf68d | 2011-09-15 20:53:28 +0000 | [diff] [blame] | 1070 | // Set up the inputs/outputs so that we build the module from its umbrella |
| 1071 | // header. |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 1072 | FrontendOptions &FrontendOpts = Invocation->getFrontendOpts(); |
Douglas Gregor | 1735f4e | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 1073 | FrontendOpts.OutputFile = ModuleFileName.str(); |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 1074 | FrontendOpts.DisableFree = false; |
Douglas Gregor | c1bbec8 | 2013-01-25 00:45:27 +0000 | [diff] [blame] | 1075 | FrontendOpts.GenerateGlobalModuleIndex = false; |
Richard Smith | e75ee0f | 2015-08-17 07:13:32 +0000 | [diff] [blame] | 1076 | FrontendOpts.BuildingImplicitModule = true; |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1077 | FrontendOpts.OriginalModuleMap = OriginalModuleMapFile; |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 1078 | // Force implicitly-built modules to hash the content of the module file. |
| 1079 | HSOpts.ModulesHashContent = true; |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1080 | FrontendOpts.Inputs = {Input}; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1081 | |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1082 | // Don't free the remapped file buffers; they are owned by our caller. |
| 1083 | PPOpts.RetainRemappedFileBuffers = true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1084 | |
Douglas Gregor | 2b9b464 | 2011-09-13 01:26:44 +0000 | [diff] [blame] | 1085 | Invocation->getDiagnosticOpts().VerifyDiagnostics = 0; |
Douglas Gregor | 3728ea7 | 2011-09-13 23:20:27 +0000 | [diff] [blame] | 1086 | assert(ImportingInstance.getInvocation().getModuleHash() == |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1087 | Invocation->getModuleHash() && "Module hash mismatch!"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1088 | |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 1089 | // Construct a compiler instance that will be used to actually create the |
Duncan P. N. Exon Smith | 8bef5cd | 2019-03-09 17:33:56 +0000 | [diff] [blame] | 1090 | // module. Since we're sharing an in-memory module cache, |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame] | 1091 | // CompilerInstance::CompilerInstance is responsible for finalizing the |
| 1092 | // buffers to prevent use-after-frees. |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1093 | CompilerInstance Instance(ImportingInstance.getPCHContainerOperations(), |
Duncan P. N. Exon Smith | 8bef5cd | 2019-03-09 17:33:56 +0000 | [diff] [blame] | 1094 | &ImportingInstance.getModuleCache()); |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1095 | auto &Inv = *Invocation; |
| 1096 | Instance.setInvocation(std::move(Invocation)); |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 1097 | |
| 1098 | Instance.createDiagnostics(new ForwardingDiagnosticConsumer( |
| 1099 | ImportingInstance.getDiagnosticClient()), |
Douglas Gregor | 30071cea | 2013-05-03 23:07:45 +0000 | [diff] [blame] | 1100 | /*ShouldOwnClient=*/true); |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 1101 | |
Douglas Gregor | 6336543 | 2012-11-30 22:11:57 +0000 | [diff] [blame] | 1102 | // Note that this module is part of the module build stack, so that we |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 1103 | // can detect cycles in the module graph. |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 1104 | Instance.setFileManager(&ImportingInstance.getFileManager()); |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 1105 | Instance.createSourceManager(Instance.getFileManager()); |
| 1106 | SourceManager &SourceMgr = Instance.getSourceManager(); |
Douglas Gregor | 6336543 | 2012-11-30 22:11:57 +0000 | [diff] [blame] | 1107 | SourceMgr.setModuleBuildStack( |
| 1108 | ImportingInstance.getSourceManager().getModuleBuildStack()); |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1109 | SourceMgr.pushModuleBuildStack(ModuleName, |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 1110 | FullSourceLoc(ImportLoc, ImportingInstance.getSourceManager())); |
| 1111 | |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 1112 | // If we're collecting module dependencies, we need to share a collector |
Richard Smith | 38c1e6d | 2015-08-09 06:03:55 +0000 | [diff] [blame] | 1113 | // between all of the module CompilerInstances. Other than that, we don't |
| 1114 | // want to produce any dependency output from the module build. |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 1115 | Instance.setModuleDepCollector(ImportingInstance.getModuleDepCollector()); |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1116 | Inv.getDependencyOutputOpts() = DependencyOutputOptions(); |
Justin Bogner | 86d1259 | 2014-06-19 19:36:03 +0000 | [diff] [blame] | 1117 | |
Richard Smith | 99891da | 2014-10-14 02:08:30 +0000 | [diff] [blame] | 1118 | ImportingInstance.getDiagnostics().Report(ImportLoc, |
| 1119 | diag::remark_module_build) |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1120 | << ModuleName << ModuleFileName; |
| 1121 | |
| 1122 | PreBuildStep(Instance); |
Richard Smith | 99891da | 2014-10-14 02:08:30 +0000 | [diff] [blame] | 1123 | |
Douglas Gregor | 51e0b54 | 2011-10-04 00:21:21 +0000 | [diff] [blame] | 1124 | // Execute the action to actually build the module in-place. Use a separate |
| 1125 | // thread so that we get a stack large enough. |
Douglas Gregor | 51e0b54 | 2011-10-04 00:21:21 +0000 | [diff] [blame] | 1126 | llvm::CrashRecoveryContext CRC; |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 1127 | CRC.RunSafelyOnThread( |
| 1128 | [&]() { |
| 1129 | GenerateModuleFromModuleMapAction Action; |
| 1130 | Instance.ExecuteAction(Action); |
| 1131 | }, |
Richard Smith | 0a7b297 | 2018-07-03 21:34:13 +0000 | [diff] [blame] | 1132 | DesiredStackSize); |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 1133 | |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1134 | PostBuildStep(Instance); |
| 1135 | |
Richard Smith | 99891da | 2014-10-14 02:08:30 +0000 | [diff] [blame] | 1136 | ImportingInstance.getDiagnostics().Report(ImportLoc, |
| 1137 | diag::remark_module_build_done) |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1138 | << ModuleName; |
Richard Smith | 99891da | 2014-10-14 02:08:30 +0000 | [diff] [blame] | 1139 | |
Douglas Gregor | f545f67 | 2011-11-29 21:59:16 +0000 | [diff] [blame] | 1140 | // Delete the temporary module map file. |
| 1141 | // FIXME: Even though we're executing under crash protection, it would still |
| 1142 | // be nice to do this with RemoveFileOnSignal when we can. However, that |
| 1143 | // doesn't make sense for all clients, so clean this up manually. |
Benjamin Kramer | 13afbf4 | 2012-10-14 19:50:53 +0000 | [diff] [blame] | 1144 | Instance.clearOutputFiles(/*EraseFiles=*/true); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 1145 | |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1146 | return !Instance.getDiagnostics().hasErrorOccurred(); |
| 1147 | } |
| 1148 | |
Bruno Cardoso Lopes | 22d9706 | 2018-05-02 02:25:03 +0000 | [diff] [blame] | 1149 | static const FileEntry *getPublicModuleMap(const FileEntry *File, |
| 1150 | FileManager &FileMgr) { |
| 1151 | StringRef Filename = llvm::sys::path::filename(File->getName()); |
| 1152 | SmallString<128> PublicFilename(File->getDir()->getName()); |
| 1153 | if (Filename == "module_private.map") |
| 1154 | llvm::sys::path::append(PublicFilename, "module.map"); |
| 1155 | else if (Filename == "module.private.modulemap") |
| 1156 | llvm::sys::path::append(PublicFilename, "module.modulemap"); |
| 1157 | else |
| 1158 | return nullptr; |
| 1159 | return FileMgr.getFile(PublicFilename); |
| 1160 | } |
| 1161 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1162 | /// Compile a module file for the given module, using the options |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1163 | /// provided by the importing compiler instance. Returns true if the module |
| 1164 | /// was built without errors. |
| 1165 | static bool compileModuleImpl(CompilerInstance &ImportingInstance, |
| 1166 | SourceLocation ImportLoc, |
| 1167 | Module *Module, |
| 1168 | StringRef ModuleFileName) { |
| 1169 | InputKind IK(getLanguageFromOptions(ImportingInstance.getLangOpts()), |
| 1170 | InputKind::ModuleMap); |
| 1171 | |
| 1172 | // Get or create the module map that we'll use to build this module. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1173 | ModuleMap &ModMap |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1174 | = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap(); |
| 1175 | bool Result; |
| 1176 | if (const FileEntry *ModuleMapFile = |
| 1177 | ModMap.getContainingModuleMapFile(Module)) { |
Bruno Cardoso Lopes | 22d9706 | 2018-05-02 02:25:03 +0000 | [diff] [blame] | 1178 | // Canonicalize compilation to start with the public module map. This is |
| 1179 | // vital for submodules declarations in the private module maps to be |
| 1180 | // correctly parsed when depending on a top level module in the public one. |
| 1181 | if (const FileEntry *PublicMMFile = getPublicModuleMap( |
| 1182 | ModuleMapFile, ImportingInstance.getFileManager())) |
| 1183 | ModuleMapFile = PublicMMFile; |
| 1184 | |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1185 | // Use the module map where this module resides. |
| 1186 | Result = compileModuleImpl( |
| 1187 | ImportingInstance, ImportLoc, Module->getTopLevelModuleName(), |
| 1188 | FrontendInputFile(ModuleMapFile->getName(), IK, +Module->IsSystem), |
| 1189 | ModMap.getModuleMapFileForUniquing(Module)->getName(), |
| 1190 | ModuleFileName); |
| 1191 | } else { |
| 1192 | // FIXME: We only need to fake up an input file here as a way of |
| 1193 | // transporting the module's directory to the module map parser. We should |
| 1194 | // be able to do that more directly, and parse from a memory buffer without |
| 1195 | // inventing this file. |
| 1196 | SmallString<128> FakeModuleMapFile(Module->Directory->getName()); |
| 1197 | llvm::sys::path::append(FakeModuleMapFile, "__inferred_module.map"); |
| 1198 | |
| 1199 | std::string InferredModuleMapContent; |
| 1200 | llvm::raw_string_ostream OS(InferredModuleMapContent); |
| 1201 | Module->print(OS); |
| 1202 | OS.flush(); |
| 1203 | |
| 1204 | Result = compileModuleImpl( |
| 1205 | ImportingInstance, ImportLoc, Module->getTopLevelModuleName(), |
| 1206 | FrontendInputFile(FakeModuleMapFile, IK, +Module->IsSystem), |
| 1207 | ModMap.getModuleMapFileForUniquing(Module)->getName(), |
| 1208 | ModuleFileName, |
| 1209 | [&](CompilerInstance &Instance) { |
| 1210 | std::unique_ptr<llvm::MemoryBuffer> ModuleMapBuffer = |
| 1211 | llvm::MemoryBuffer::getMemBuffer(InferredModuleMapContent); |
| 1212 | ModuleMapFile = Instance.getFileManager().getVirtualFile( |
| 1213 | FakeModuleMapFile, InferredModuleMapContent.size(), 0); |
| 1214 | Instance.getSourceManager().overrideFileContents( |
| 1215 | ModuleMapFile, std::move(ModuleMapBuffer)); |
| 1216 | }); |
| 1217 | } |
| 1218 | |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 1219 | // We've rebuilt a module. If we're allowed to generate or update the global |
| 1220 | // module index, record that fact in the importing compiler instance. |
Douglas Gregor | c1bbec8 | 2013-01-25 00:45:27 +0000 | [diff] [blame] | 1221 | if (ImportingInstance.getFrontendOpts().GenerateGlobalModuleIndex) { |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 1222 | ImportingInstance.setBuildGlobalModuleIndex(true); |
| 1223 | } |
Ben Langmuir | b797d59 | 2014-07-19 16:29:28 +0000 | [diff] [blame] | 1224 | |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1225 | return Result; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1226 | } |
Douglas Gregor | faeb1d4 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 1227 | |
Ben Langmuir | dbdc036 | 2014-06-17 22:35:27 +0000 | [diff] [blame] | 1228 | static bool compileAndLoadModule(CompilerInstance &ImportingInstance, |
| 1229 | SourceLocation ImportLoc, |
Ben Langmuir | b797d59 | 2014-07-19 16:29:28 +0000 | [diff] [blame] | 1230 | SourceLocation ModuleNameLoc, Module *Module, |
Ben Langmuir | dbdc036 | 2014-06-17 22:35:27 +0000 | [diff] [blame] | 1231 | StringRef ModuleFileName) { |
Ben Langmuir | d213aab | 2014-09-26 22:42:23 +0000 | [diff] [blame] | 1232 | DiagnosticsEngine &Diags = ImportingInstance.getDiagnostics(); |
| 1233 | |
Ben Langmuir | b797d59 | 2014-07-19 16:29:28 +0000 | [diff] [blame] | 1234 | auto diagnoseBuildFailure = [&] { |
Ben Langmuir | d213aab | 2014-09-26 22:42:23 +0000 | [diff] [blame] | 1235 | Diags.Report(ModuleNameLoc, diag::err_module_not_built) |
Ben Langmuir | b797d59 | 2014-07-19 16:29:28 +0000 | [diff] [blame] | 1236 | << Module->Name << SourceRange(ImportLoc, ModuleNameLoc); |
| 1237 | }; |
| 1238 | |
Argyrios Kyrtzidis | 4382fe7 | 2014-04-06 03:21:44 +0000 | [diff] [blame] | 1239 | // FIXME: have LockFileManager return an error_code so that we can |
| 1240 | // avoid the mkdir when the directory already exists. |
| 1241 | StringRef Dir = llvm::sys::path::parent_path(ModuleFileName); |
| 1242 | llvm::sys::fs::create_directories(Dir); |
| 1243 | |
| 1244 | while (1) { |
Ben Langmuir | dbdc036 | 2014-06-17 22:35:27 +0000 | [diff] [blame] | 1245 | unsigned ModuleLoadCapabilities = ASTReader::ARR_Missing; |
Argyrios Kyrtzidis | 4382fe7 | 2014-04-06 03:21:44 +0000 | [diff] [blame] | 1246 | llvm::LockFileManager Locked(ModuleFileName); |
| 1247 | switch (Locked) { |
| 1248 | case llvm::LockFileManager::LFS_Error: |
Duncan P. N. Exon Smith | 8bef5cd | 2019-03-09 17:33:56 +0000 | [diff] [blame] | 1249 | // ModuleCache takes care of correctness and locks are only necessary for |
Bruno Cardoso Lopes | 5a0af1f | 2017-03-18 00:26:18 +0000 | [diff] [blame] | 1250 | // performance. Fallback to building the module in case of any lock |
| 1251 | // related errors. |
| 1252 | Diags.Report(ModuleNameLoc, diag::remark_module_lock_failure) |
Bruno Cardoso Lopes | 4a52222 | 2016-06-04 01:13:22 +0000 | [diff] [blame] | 1253 | << Module->Name << Locked.getErrorMessage(); |
Bruno Cardoso Lopes | 5a0af1f | 2017-03-18 00:26:18 +0000 | [diff] [blame] | 1254 | // Clear out any potential leftover. |
| 1255 | Locked.unsafeRemoveLockFile(); |
Reid Kleckner | 4dc0b1a | 2018-11-01 19:54:45 +0000 | [diff] [blame] | 1256 | LLVM_FALLTHROUGH; |
Argyrios Kyrtzidis | 4382fe7 | 2014-04-06 03:21:44 +0000 | [diff] [blame] | 1257 | case llvm::LockFileManager::LFS_Owned: |
Ben Langmuir | dbdc036 | 2014-06-17 22:35:27 +0000 | [diff] [blame] | 1258 | // We're responsible for building the module ourselves. |
Ben Langmuir | b797d59 | 2014-07-19 16:29:28 +0000 | [diff] [blame] | 1259 | if (!compileModuleImpl(ImportingInstance, ModuleNameLoc, Module, |
| 1260 | ModuleFileName)) { |
| 1261 | diagnoseBuildFailure(); |
| 1262 | return false; |
| 1263 | } |
Argyrios Kyrtzidis | 4382fe7 | 2014-04-06 03:21:44 +0000 | [diff] [blame] | 1264 | break; |
| 1265 | |
| 1266 | case llvm::LockFileManager::LFS_Shared: |
| 1267 | // Someone else is responsible for building the module. Wait for them to |
| 1268 | // finish. |
Ben Langmuir | 1daf480 | 2015-02-09 20:35:13 +0000 | [diff] [blame] | 1269 | switch (Locked.waitForUnlock()) { |
| 1270 | case llvm::LockFileManager::Res_Success: |
| 1271 | ModuleLoadCapabilities |= ASTReader::ARR_OutOfDate; |
| 1272 | break; |
| 1273 | case llvm::LockFileManager::Res_OwnerDied: |
Argyrios Kyrtzidis | 4382fe7 | 2014-04-06 03:21:44 +0000 | [diff] [blame] | 1274 | continue; // try again to get the lock. |
Ben Langmuir | 1daf480 | 2015-02-09 20:35:13 +0000 | [diff] [blame] | 1275 | case llvm::LockFileManager::Res_Timeout: |
Duncan P. N. Exon Smith | 8bef5cd | 2019-03-09 17:33:56 +0000 | [diff] [blame] | 1276 | // Since ModuleCache takes care of correctness, we try waiting for |
| 1277 | // another process to complete the build so clang does not do it done |
| 1278 | // twice. If case of timeout, build it ourselves. |
Bruno Cardoso Lopes | 5a0af1f | 2017-03-18 00:26:18 +0000 | [diff] [blame] | 1279 | Diags.Report(ModuleNameLoc, diag::remark_module_lock_timeout) |
Ben Langmuir | 1daf480 | 2015-02-09 20:35:13 +0000 | [diff] [blame] | 1280 | << Module->Name; |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 1281 | // Clear the lock file so that future invocations can make progress. |
Ben Langmuir | 1daf480 | 2015-02-09 20:35:13 +0000 | [diff] [blame] | 1282 | Locked.unsafeRemoveLockFile(); |
Bruno Cardoso Lopes | 5a0af1f | 2017-03-18 00:26:18 +0000 | [diff] [blame] | 1283 | continue; |
Ben Langmuir | 1daf480 | 2015-02-09 20:35:13 +0000 | [diff] [blame] | 1284 | } |
Ben Langmuir | dbdc036 | 2014-06-17 22:35:27 +0000 | [diff] [blame] | 1285 | break; |
Argyrios Kyrtzidis | 4382fe7 | 2014-04-06 03:21:44 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Ben Langmuir | dbdc036 | 2014-06-17 22:35:27 +0000 | [diff] [blame] | 1288 | // Try to read the module file, now that we've compiled it. |
| 1289 | ASTReader::ASTReadResult ReadResult = |
| 1290 | ImportingInstance.getModuleManager()->ReadAST( |
Richard Smith | e842a47 | 2014-10-22 02:05:46 +0000 | [diff] [blame] | 1291 | ModuleFileName, serialization::MK_ImplicitModule, ImportLoc, |
Ben Langmuir | dbdc036 | 2014-06-17 22:35:27 +0000 | [diff] [blame] | 1292 | ModuleLoadCapabilities); |
| 1293 | |
| 1294 | if (ReadResult == ASTReader::OutOfDate && |
| 1295 | Locked == llvm::LockFileManager::LFS_Shared) { |
| 1296 | // The module may be out of date in the presence of file system races, |
| 1297 | // or if one of its imports depends on header search paths that are not |
| 1298 | // consistent with this ImportingInstance. Try again... |
| 1299 | continue; |
| 1300 | } else if (ReadResult == ASTReader::Missing) { |
Ben Langmuir | b797d59 | 2014-07-19 16:29:28 +0000 | [diff] [blame] | 1301 | diagnoseBuildFailure(); |
Ben Langmuir | d213aab | 2014-09-26 22:42:23 +0000 | [diff] [blame] | 1302 | } else if (ReadResult != ASTReader::Success && |
| 1303 | !Diags.hasErrorOccurred()) { |
| 1304 | // The ASTReader didn't diagnose the error, so conservatively report it. |
| 1305 | diagnoseBuildFailure(); |
Ben Langmuir | dbdc036 | 2014-06-17 22:35:27 +0000 | [diff] [blame] | 1306 | } |
| 1307 | return ReadResult == ASTReader::Success; |
Argyrios Kyrtzidis | 4382fe7 | 2014-04-06 03:21:44 +0000 | [diff] [blame] | 1308 | } |
| 1309 | } |
| 1310 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1311 | /// Diagnose differences between the current definition of the given |
Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 1312 | /// configuration macro and the definition provided on the command line. |
| 1313 | static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro, |
| 1314 | Module *Mod, SourceLocation ImportLoc) { |
| 1315 | IdentifierInfo *Id = PP.getIdentifierInfo(ConfigMacro); |
| 1316 | SourceManager &SourceMgr = PP.getSourceManager(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1317 | |
Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 1318 | // If this identifier has never had a macro definition, then it could |
| 1319 | // not have changed. |
| 1320 | if (!Id->hadMacroDefinition()) |
| 1321 | return; |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 1322 | auto *LatestLocalMD = PP.getLocalMacroDirectiveHistory(Id); |
Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 1323 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 1324 | // Find the macro definition from the command line. |
| 1325 | MacroInfo *CmdLineDefinition = nullptr; |
| 1326 | for (auto *MD = LatestLocalMD; MD; MD = MD->getPrevious()) { |
Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 1327 | // We only care about the predefines buffer. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 1328 | FileID FID = SourceMgr.getFileID(MD->getLocation()); |
| 1329 | if (FID.isInvalid() || FID != PP.getPredefinesFileID()) |
Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 1330 | continue; |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 1331 | if (auto *DMD = dyn_cast<DefMacroDirective>(MD)) |
| 1332 | CmdLineDefinition = DMD->getMacroInfo(); |
Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 1333 | break; |
| 1334 | } |
| 1335 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 1336 | auto *CurrentDefinition = PP.getMacroInfo(Id); |
| 1337 | if (CurrentDefinition == CmdLineDefinition) { |
| 1338 | // Macro matches. Nothing to do. |
| 1339 | } else if (!CurrentDefinition) { |
| 1340 | // This macro was defined on the command line, then #undef'd later. |
| 1341 | // Complain. |
| 1342 | PP.Diag(ImportLoc, diag::warn_module_config_macro_undef) |
| 1343 | << true << ConfigMacro << Mod->getFullModuleName(); |
| 1344 | auto LatestDef = LatestLocalMD->getDefinition(); |
| 1345 | assert(LatestDef.isUndefined() && |
| 1346 | "predefined macro went away with no #undef?"); |
| 1347 | PP.Diag(LatestDef.getUndefLocation(), diag::note_module_def_undef_here) |
| 1348 | << true; |
| 1349 | return; |
| 1350 | } else if (!CmdLineDefinition) { |
| 1351 | // There was no definition for this macro in the predefines buffer, |
| 1352 | // but there was a local definition. Complain. |
Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 1353 | PP.Diag(ImportLoc, diag::warn_module_config_macro_undef) |
| 1354 | << false << ConfigMacro << Mod->getFullModuleName(); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 1355 | PP.Diag(CurrentDefinition->getDefinitionLoc(), |
| 1356 | diag::note_module_def_undef_here) |
Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 1357 | << false; |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 1358 | } else if (!CurrentDefinition->isIdenticalTo(*CmdLineDefinition, PP, |
| 1359 | /*Syntactically=*/true)) { |
| 1360 | // The macro definitions differ. |
| 1361 | PP.Diag(ImportLoc, diag::warn_module_config_macro_undef) |
| 1362 | << false << ConfigMacro << Mod->getFullModuleName(); |
| 1363 | PP.Diag(CurrentDefinition->getDefinitionLoc(), |
| 1364 | diag::note_module_def_undef_here) |
| 1365 | << false; |
Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 1366 | } |
Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1369 | /// Write a new timestamp file with the given path. |
Douglas Gregor | 527b1c9 | 2013-03-25 21:19:16 +0000 | [diff] [blame] | 1370 | static void writeTimestampFile(StringRef TimestampFile) { |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 1371 | std::error_code EC; |
| 1372 | llvm::raw_fd_ostream Out(TimestampFile.str(), EC, llvm::sys::fs::F_None); |
Douglas Gregor | 527b1c9 | 2013-03-25 21:19:16 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1375 | /// Prune the module cache of modules that haven't been accessed in |
Douglas Gregor | 527b1c9 | 2013-03-25 21:19:16 +0000 | [diff] [blame] | 1376 | /// a long time. |
| 1377 | static void pruneModuleCache(const HeaderSearchOptions &HSOpts) { |
| 1378 | struct stat StatBuf; |
| 1379 | llvm::SmallString<128> TimestampFile; |
| 1380 | TimestampFile = HSOpts.ModuleCachePath; |
Richard Smith | 3938f0c | 2015-08-15 00:34:15 +0000 | [diff] [blame] | 1381 | assert(!TimestampFile.empty()); |
Douglas Gregor | 527b1c9 | 2013-03-25 21:19:16 +0000 | [diff] [blame] | 1382 | llvm::sys::path::append(TimestampFile, "modules.timestamp"); |
| 1383 | |
| 1384 | // Try to stat() the timestamp file. |
| 1385 | if (::stat(TimestampFile.c_str(), &StatBuf)) { |
| 1386 | // If the timestamp file wasn't there, create one now. |
| 1387 | if (errno == ENOENT) { |
| 1388 | writeTimestampFile(TimestampFile); |
| 1389 | } |
| 1390 | return; |
| 1391 | } |
| 1392 | |
| 1393 | // Check whether the time stamp is older than our pruning interval. |
| 1394 | // If not, do nothing. |
| 1395 | time_t TimeStampModTime = StatBuf.st_mtime; |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1396 | time_t CurrentTime = time(nullptr); |
Benjamin Kramer | dbcf503 | 2013-03-29 17:39:43 +0000 | [diff] [blame] | 1397 | if (CurrentTime - TimeStampModTime <= time_t(HSOpts.ModuleCachePruneInterval)) |
Douglas Gregor | 527b1c9 | 2013-03-25 21:19:16 +0000 | [diff] [blame] | 1398 | return; |
Douglas Gregor | 527b1c9 | 2013-03-25 21:19:16 +0000 | [diff] [blame] | 1399 | |
| 1400 | // Write a new timestamp file so that nobody else attempts to prune. |
| 1401 | // There is a benign race condition here, if two Clang instances happen to |
| 1402 | // notice at the same time that the timestamp is out-of-date. |
| 1403 | writeTimestampFile(TimestampFile); |
| 1404 | |
| 1405 | // Walk the entire module cache, looking for unused module files and module |
| 1406 | // indices. |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 1407 | std::error_code EC; |
Douglas Gregor | 527b1c9 | 2013-03-25 21:19:16 +0000 | [diff] [blame] | 1408 | SmallString<128> ModuleCachePathNative; |
| 1409 | llvm::sys::path::native(HSOpts.ModuleCachePath, ModuleCachePathNative); |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 1410 | for (llvm::sys::fs::directory_iterator Dir(ModuleCachePathNative, EC), DirEnd; |
Douglas Gregor | 527b1c9 | 2013-03-25 21:19:16 +0000 | [diff] [blame] | 1411 | Dir != DirEnd && !EC; Dir.increment(EC)) { |
| 1412 | // If we don't have a directory, there's nothing to look into. |
Rafael Espindola | a07f720 | 2013-07-17 04:23:07 +0000 | [diff] [blame] | 1413 | if (!llvm::sys::fs::is_directory(Dir->path())) |
Douglas Gregor | 527b1c9 | 2013-03-25 21:19:16 +0000 | [diff] [blame] | 1414 | continue; |
| 1415 | |
| 1416 | // Walk all of the files within this directory. |
Douglas Gregor | 527b1c9 | 2013-03-25 21:19:16 +0000 | [diff] [blame] | 1417 | for (llvm::sys::fs::directory_iterator File(Dir->path(), EC), FileEnd; |
| 1418 | File != FileEnd && !EC; File.increment(EC)) { |
| 1419 | // We only care about module and global module index files. |
Dmitri Gribenko | f430da4 | 2014-02-12 10:33:14 +0000 | [diff] [blame] | 1420 | StringRef Extension = llvm::sys::path::extension(File->path()); |
| 1421 | if (Extension != ".pcm" && Extension != ".timestamp" && |
| 1422 | llvm::sys::path::filename(File->path()) != "modules.idx") |
Douglas Gregor | 527b1c9 | 2013-03-25 21:19:16 +0000 | [diff] [blame] | 1423 | continue; |
Douglas Gregor | 527b1c9 | 2013-03-25 21:19:16 +0000 | [diff] [blame] | 1424 | |
| 1425 | // Look at this file. If we can't stat it, there's nothing interesting |
| 1426 | // there. |
Dmitri Gribenko | f430da4 | 2014-02-12 10:33:14 +0000 | [diff] [blame] | 1427 | if (::stat(File->path().c_str(), &StatBuf)) |
Douglas Gregor | 527b1c9 | 2013-03-25 21:19:16 +0000 | [diff] [blame] | 1428 | continue; |
Douglas Gregor | 527b1c9 | 2013-03-25 21:19:16 +0000 | [diff] [blame] | 1429 | |
| 1430 | // If the file has been used recently enough, leave it there. |
| 1431 | time_t FileAccessTime = StatBuf.st_atime; |
Benjamin Kramer | dbcf503 | 2013-03-29 17:39:43 +0000 | [diff] [blame] | 1432 | if (CurrentTime - FileAccessTime <= |
| 1433 | time_t(HSOpts.ModuleCachePruneAfter)) { |
Douglas Gregor | 527b1c9 | 2013-03-25 21:19:16 +0000 | [diff] [blame] | 1434 | continue; |
| 1435 | } |
| 1436 | |
| 1437 | // Remove the file. |
Dmitri Gribenko | f430da4 | 2014-02-12 10:33:14 +0000 | [diff] [blame] | 1438 | llvm::sys::fs::remove(File->path()); |
| 1439 | |
| 1440 | // Remove the timestamp file. |
| 1441 | std::string TimpestampFilename = File->path() + ".timestamp"; |
| 1442 | llvm::sys::fs::remove(TimpestampFilename); |
Douglas Gregor | 527b1c9 | 2013-03-25 21:19:16 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | // If we removed all of the files in the directory, remove the directory |
| 1446 | // itself. |
Dmitri Gribenko | f430da4 | 2014-02-12 10:33:14 +0000 | [diff] [blame] | 1447 | if (llvm::sys::fs::directory_iterator(Dir->path(), EC) == |
| 1448 | llvm::sys::fs::directory_iterator() && !EC) |
Rafael Espindola | 2a00878 | 2014-01-10 21:32:14 +0000 | [diff] [blame] | 1449 | llvm::sys::fs::remove(Dir->path()); |
Douglas Gregor | 527b1c9 | 2013-03-25 21:19:16 +0000 | [diff] [blame] | 1450 | } |
| 1451 | } |
| 1452 | |
John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 1453 | void CompilerInstance::createModuleManager() { |
| 1454 | if (!ModuleManager) { |
| 1455 | if (!hasASTContext()) |
| 1456 | createASTContext(); |
| 1457 | |
Chandler Carruth | 580dd29 | 2015-03-24 21:44:25 +0000 | [diff] [blame] | 1458 | // If we're implicitly building modules but not currently recursively |
| 1459 | // building a module, check whether we need to prune the module cache. |
Richard Smith | 3938f0c | 2015-08-15 00:34:15 +0000 | [diff] [blame] | 1460 | if (getSourceManager().getModuleBuildStack().empty() && |
| 1461 | !getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty() && |
John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 1462 | getHeaderSearchOpts().ModuleCachePruneInterval > 0 && |
| 1463 | getHeaderSearchOpts().ModuleCachePruneAfter > 0) { |
| 1464 | pruneModuleCache(getHeaderSearchOpts()); |
| 1465 | } |
| 1466 | |
| 1467 | HeaderSearchOptions &HSOpts = getHeaderSearchOpts(); |
| 1468 | std::string Sysroot = HSOpts.Sysroot; |
| 1469 | const PreprocessorOptions &PPOpts = getPreprocessorOpts(); |
Richard Smith | ce18a18 | 2015-07-14 00:26:00 +0000 | [diff] [blame] | 1470 | std::unique_ptr<llvm::Timer> ReadTimer; |
| 1471 | if (FrontendTimerGroup) |
Matthias Braun | ae032b6 | 2016-11-18 19:43:25 +0000 | [diff] [blame] | 1472 | ReadTimer = llvm::make_unique<llvm::Timer>("reading_modules", |
| 1473 | "Reading modules", |
Richard Smith | ce18a18 | 2015-07-14 00:26:00 +0000 | [diff] [blame] | 1474 | *FrontendTimerGroup); |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1475 | ModuleManager = new ASTReader( |
Duncan P. N. Exon Smith | 8bef5cd | 2019-03-09 17:33:56 +0000 | [diff] [blame] | 1476 | getPreprocessor(), getModuleCache(), &getASTContext(), |
| 1477 | getPCHContainerReader(), getFrontendOpts().ModuleFileExtensions, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1478 | Sysroot.empty() ? "" : Sysroot.c_str(), PPOpts.DisablePCHValidation, |
| 1479 | /*AllowASTWithCompilerErrors=*/false, |
| 1480 | /*AllowConfigurationMismatch=*/false, |
| 1481 | HSOpts.ModulesValidateSystemHeaders, |
Duncan P. N. Exon Smith | 8bef5cd | 2019-03-09 17:33:56 +0000 | [diff] [blame] | 1482 | getFrontendOpts().UseGlobalModuleIndex, std::move(ReadTimer)); |
John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 1483 | if (hasASTConsumer()) { |
| 1484 | ModuleManager->setDeserializationListener( |
| 1485 | getASTConsumer().GetASTDeserializationListener()); |
| 1486 | getASTContext().setASTMutationListener( |
| 1487 | getASTConsumer().GetASTMutationListener()); |
| 1488 | } |
| 1489 | getASTContext().setExternalSource(ModuleManager); |
| 1490 | if (hasSema()) |
| 1491 | ModuleManager->InitializeSema(getSema()); |
Richard Smith | 293534b | 2015-08-18 20:39:29 +0000 | [diff] [blame] | 1492 | if (hasASTConsumer()) |
John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 1493 | ModuleManager->StartTranslationUnit(&getASTConsumer()); |
Richard Smith | 03f7e61 | 2015-08-09 02:28:42 +0000 | [diff] [blame] | 1494 | |
| 1495 | if (TheDependencyFileGenerator) |
| 1496 | TheDependencyFileGenerator->AttachToASTReader(*ModuleManager); |
Richard Smith | 03f7e61 | 2015-08-09 02:28:42 +0000 | [diff] [blame] | 1497 | for (auto &Listener : DependencyCollectors) |
| 1498 | Listener->attachToASTReader(*ModuleManager); |
John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 1499 | } |
| 1500 | } |
| 1501 | |
Richard Smith | d4b230b | 2014-10-27 23:01:16 +0000 | [diff] [blame] | 1502 | bool CompilerInstance::loadModuleFile(StringRef FileName) { |
Richard Smith | ce18a18 | 2015-07-14 00:26:00 +0000 | [diff] [blame] | 1503 | llvm::Timer Timer; |
| 1504 | if (FrontendTimerGroup) |
Matthias Braun | ae032b6 | 2016-11-18 19:43:25 +0000 | [diff] [blame] | 1505 | Timer.init("preloading." + FileName.str(), "Preloading " + FileName.str(), |
| 1506 | *FrontendTimerGroup); |
Richard Smith | ce18a18 | 2015-07-14 00:26:00 +0000 | [diff] [blame] | 1507 | llvm::TimeRegion TimeLoading(FrontendTimerGroup ? &Timer : nullptr); |
| 1508 | |
Richard Smith | d4b230b | 2014-10-27 23:01:16 +0000 | [diff] [blame] | 1509 | // Helper to recursively read the module names for all modules we're adding. |
| 1510 | // We mark these as known and redirect any attempt to load that module to |
| 1511 | // the files we were handed. |
| 1512 | struct ReadModuleNames : ASTReaderListener { |
| 1513 | CompilerInstance &CI; |
Richard Smith | 0f99d6a | 2015-08-09 08:48:41 +0000 | [diff] [blame] | 1514 | llvm::SmallVector<IdentifierInfo*, 8> LoadedModules; |
Richard Smith | e842a47 | 2014-10-22 02:05:46 +0000 | [diff] [blame] | 1515 | |
Richard Smith | 0f99d6a | 2015-08-09 08:48:41 +0000 | [diff] [blame] | 1516 | ReadModuleNames(CompilerInstance &CI) : CI(CI) {} |
Richard Smith | e842a47 | 2014-10-22 02:05:46 +0000 | [diff] [blame] | 1517 | |
Richard Smith | d4b230b | 2014-10-27 23:01:16 +0000 | [diff] [blame] | 1518 | void ReadModuleName(StringRef ModuleName) override { |
Richard Smith | 0f99d6a | 2015-08-09 08:48:41 +0000 | [diff] [blame] | 1519 | LoadedModules.push_back( |
| 1520 | CI.getPreprocessor().getIdentifierInfo(ModuleName)); |
Richard Smith | d4b230b | 2014-10-27 23:01:16 +0000 | [diff] [blame] | 1521 | } |
Richard Smith | 0f99d6a | 2015-08-09 08:48:41 +0000 | [diff] [blame] | 1522 | |
| 1523 | void registerAll() { |
| 1524 | for (auto *II : LoadedModules) { |
| 1525 | CI.KnownModules[II] = CI.getPreprocessor() |
| 1526 | .getHeaderSearchInfo() |
| 1527 | .getModuleMap() |
| 1528 | .findModule(II->getName()); |
| 1529 | } |
| 1530 | LoadedModules.clear(); |
| 1531 | } |
Richard Smith | 8a308ec | 2015-11-05 00:54:55 +0000 | [diff] [blame] | 1532 | |
| 1533 | void markAllUnavailable() { |
| 1534 | for (auto *II : LoadedModules) { |
| 1535 | if (Module *M = CI.getPreprocessor() |
| 1536 | .getHeaderSearchInfo() |
| 1537 | .getModuleMap() |
Richard Smith | a114c46 | 2016-12-06 00:40:17 +0000 | [diff] [blame] | 1538 | .findModule(II->getName())) { |
Richard Smith | 8a308ec | 2015-11-05 00:54:55 +0000 | [diff] [blame] | 1539 | M->HasIncompatibleModuleFile = true; |
Richard Smith | a114c46 | 2016-12-06 00:40:17 +0000 | [diff] [blame] | 1540 | |
| 1541 | // Mark module as available if the only reason it was unavailable |
| 1542 | // was missing headers. |
| 1543 | SmallVector<Module *, 2> Stack; |
| 1544 | Stack.push_back(M); |
| 1545 | while (!Stack.empty()) { |
| 1546 | Module *Current = Stack.pop_back_val(); |
| 1547 | if (Current->IsMissingRequirement) continue; |
| 1548 | Current->IsAvailable = true; |
| 1549 | Stack.insert(Stack.end(), |
| 1550 | Current->submodule_begin(), Current->submodule_end()); |
| 1551 | } |
| 1552 | } |
Richard Smith | 8a308ec | 2015-11-05 00:54:55 +0000 | [diff] [blame] | 1553 | } |
| 1554 | LoadedModules.clear(); |
| 1555 | } |
Richard Smith | 0f99d6a | 2015-08-09 08:48:41 +0000 | [diff] [blame] | 1556 | }; |
Richard Smith | d4b230b | 2014-10-27 23:01:16 +0000 | [diff] [blame] | 1557 | |
Richard Smith | 7f330cd | 2015-03-18 01:42:29 +0000 | [diff] [blame] | 1558 | // If we don't already have an ASTReader, create one now. |
| 1559 | if (!ModuleManager) |
| 1560 | createModuleManager(); |
| 1561 | |
Richard Smith | 7a985e1 | 2018-05-24 20:03:51 +0000 | [diff] [blame] | 1562 | // If -Wmodule-file-config-mismatch is mapped as an error or worse, allow the |
| 1563 | // ASTReader to diagnose it, since it can produce better errors that we can. |
| 1564 | bool ConfigMismatchIsRecoverable = |
| 1565 | getDiagnostics().getDiagnosticLevel(diag::warn_module_config_mismatch, |
| 1566 | SourceLocation()) |
| 1567 | <= DiagnosticsEngine::Warning; |
| 1568 | |
Richard Smith | 0f99d6a | 2015-08-09 08:48:41 +0000 | [diff] [blame] | 1569 | auto Listener = llvm::make_unique<ReadModuleNames>(*this); |
| 1570 | auto &ListenerRef = *Listener; |
| 1571 | ASTReader::ListenerScope ReadModuleNamesListener(*ModuleManager, |
| 1572 | std::move(Listener)); |
Richard Smith | 7f330cd | 2015-03-18 01:42:29 +0000 | [diff] [blame] | 1573 | |
Richard Smith | 0f99d6a | 2015-08-09 08:48:41 +0000 | [diff] [blame] | 1574 | // Try to load the module file. |
Richard Smith | 7a985e1 | 2018-05-24 20:03:51 +0000 | [diff] [blame] | 1575 | switch (ModuleManager->ReadAST( |
| 1576 | FileName, serialization::MK_ExplicitModule, SourceLocation(), |
| 1577 | ConfigMismatchIsRecoverable ? ASTReader::ARR_ConfigurationMismatch : 0)) { |
Richard Smith | 95dc57a | 2015-10-16 23:20:19 +0000 | [diff] [blame] | 1578 | case ASTReader::Success: |
Richard Smith | 8a308ec | 2015-11-05 00:54:55 +0000 | [diff] [blame] | 1579 | // We successfully loaded the module file; remember the set of provided |
| 1580 | // modules so that we don't try to load implicit modules for them. |
| 1581 | ListenerRef.registerAll(); |
| 1582 | return true; |
Richard Smith | 95dc57a | 2015-10-16 23:20:19 +0000 | [diff] [blame] | 1583 | |
| 1584 | case ASTReader::ConfigurationMismatch: |
| 1585 | // Ignore unusable module files. |
| 1586 | getDiagnostics().Report(SourceLocation(), diag::warn_module_config_mismatch) |
| 1587 | << FileName; |
Richard Smith | 8a308ec | 2015-11-05 00:54:55 +0000 | [diff] [blame] | 1588 | // All modules provided by any files we tried and failed to load are now |
| 1589 | // unavailable; includes of those modules should now be handled textually. |
| 1590 | ListenerRef.markAllUnavailable(); |
Richard Smith | 95dc57a | 2015-10-16 23:20:19 +0000 | [diff] [blame] | 1591 | return true; |
| 1592 | |
| 1593 | default: |
| 1594 | return false; |
| 1595 | } |
Richard Smith | e842a47 | 2014-10-22 02:05:46 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | ModuleLoadResult |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1599 | CompilerInstance::loadModule(SourceLocation ImportLoc, |
| 1600 | ModuleIdPath Path, |
| 1601 | Module::NameVisibilityKind Visibility, |
| 1602 | bool IsInclusionDirective) { |
Richard Smith | 92304e0 | 2013-10-18 22:48:20 +0000 | [diff] [blame] | 1603 | // Determine what file we're searching from. |
Richard Smith | d6509cf | 2018-09-15 01:21:15 +0000 | [diff] [blame] | 1604 | StringRef ModuleName = Path[0].first->getName(); |
Richard Smith | 92304e0 | 2013-10-18 22:48:20 +0000 | [diff] [blame] | 1605 | SourceLocation ModuleNameLoc = Path[0].second; |
| 1606 | |
Douglas Gregor | 1805b8a | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 1607 | // If we've already handled this import, just return the cached result. |
| 1608 | // This one-element cache is important to eliminate redundant diagnostics |
| 1609 | // when both the preprocessor and parser see the same import declaration. |
Yaron Keren | 8b56366 | 2015-10-03 10:46:20 +0000 | [diff] [blame] | 1610 | if (ImportLoc.isValid() && LastModuleImportLoc == ImportLoc) { |
Douglas Gregor | ff2be53 | 2011-12-01 17:11:21 +0000 | [diff] [blame] | 1611 | // Make the named module visible. |
Richard Smith | 7e82e01 | 2016-02-19 22:25:36 +0000 | [diff] [blame] | 1612 | if (LastModuleImportResult && ModuleName != getLangOpts().CurrentModule) |
Argyrios Kyrtzidis | 125df05 | 2013-02-01 16:36:12 +0000 | [diff] [blame] | 1613 | ModuleManager->makeModuleVisible(LastModuleImportResult, Visibility, |
Richard Smith | a7e2cc6 | 2015-05-01 01:53:09 +0000 | [diff] [blame] | 1614 | ImportLoc); |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1615 | return LastModuleImportResult; |
Douglas Gregor | ff2be53 | 2011-12-01 17:11:21 +0000 | [diff] [blame] | 1616 | } |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1617 | |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1618 | clang::Module *Module = nullptr; |
Richard Smith | 92304e0 | 2013-10-18 22:48:20 +0000 | [diff] [blame] | 1619 | |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1620 | // If we don't already have information on this module, load the module now. |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 1621 | llvm::DenseMap<const IdentifierInfo *, clang::Module *>::iterator Known |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1622 | = KnownModules.find(Path[0].first); |
Douglas Gregor | 2537a36 | 2011-12-08 17:01:29 +0000 | [diff] [blame] | 1623 | if (Known != KnownModules.end()) { |
| 1624 | // Retrieve the cached top-level module. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1625 | Module = Known->second; |
Richard Smith | 7e82e01 | 2016-02-19 22:25:36 +0000 | [diff] [blame] | 1626 | } else if (ModuleName == getLangOpts().CurrentModule) { |
Bruno Cardoso Lopes | 52431f3 | 2018-07-18 23:21:19 +0000 | [diff] [blame] | 1627 | // This is the module we're building. |
| 1628 | Module = PP->getHeaderSearchInfo().lookupModule( |
| 1629 | ModuleName, /*AllowSearch*/ true, |
| 1630 | /*AllowExtraModuleMapSearch*/ !IsInclusionDirective); |
Boris Kolpackov | d30446f | 2017-08-31 06:26:43 +0000 | [diff] [blame] | 1631 | /// FIXME: perhaps we should (a) look for a module using the module name |
| 1632 | // to file map (PrebuiltModuleFiles) and (b) diagnose if still not found? |
| 1633 | //if (Module == nullptr) { |
| 1634 | // getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found) |
| 1635 | // << ModuleName; |
| 1636 | // ModuleBuildFailed = true; |
| 1637 | // return ModuleLoadResult(); |
| 1638 | //} |
Douglas Gregor | 2537a36 | 2011-12-08 17:01:29 +0000 | [diff] [blame] | 1639 | Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first; |
| 1640 | } else { |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1641 | // Search for a module with the given name. |
Bruno Cardoso Lopes | 52431f3 | 2018-07-18 23:21:19 +0000 | [diff] [blame] | 1642 | Module = PP->getHeaderSearchInfo().lookupModule(ModuleName, true, |
| 1643 | !IsInclusionDirective); |
Manman Ren | 11f2a47 | 2016-08-18 17:42:15 +0000 | [diff] [blame] | 1644 | HeaderSearchOptions &HSOpts = |
| 1645 | PP->getHeaderSearchInfo().getHeaderSearchOpts(); |
| 1646 | |
| 1647 | std::string ModuleFileName; |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1648 | enum ModuleSource { |
| 1649 | ModuleNotFound, ModuleCache, PrebuiltModulePath, ModuleBuildPragma |
| 1650 | } Source = ModuleNotFound; |
| 1651 | |
| 1652 | // Check to see if the module has been built as part of this compilation |
| 1653 | // via a module build pragma. |
| 1654 | auto BuiltModuleIt = BuiltModules.find(ModuleName); |
| 1655 | if (BuiltModuleIt != BuiltModules.end()) { |
| 1656 | ModuleFileName = BuiltModuleIt->second; |
| 1657 | Source = ModuleBuildPragma; |
| 1658 | } |
| 1659 | |
| 1660 | // Try to load the module from the prebuilt module path. |
Boris Kolpackov | d30446f | 2017-08-31 06:26:43 +0000 | [diff] [blame] | 1661 | if (Source == ModuleNotFound && (!HSOpts.PrebuiltModuleFiles.empty() || |
| 1662 | !HSOpts.PrebuiltModulePaths.empty())) { |
| 1663 | ModuleFileName = |
| 1664 | PP->getHeaderSearchInfo().getPrebuiltModuleFileName(ModuleName); |
Manman Ren | 11f2a47 | 2016-08-18 17:42:15 +0000 | [diff] [blame] | 1665 | if (!ModuleFileName.empty()) |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1666 | Source = PrebuiltModulePath; |
Manman Ren | 11f2a47 | 2016-08-18 17:42:15 +0000 | [diff] [blame] | 1667 | } |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1668 | |
| 1669 | // Try to load the module from the module cache. |
| 1670 | if (Source == ModuleNotFound && Module) { |
Boris Kolpackov | d30446f | 2017-08-31 06:26:43 +0000 | [diff] [blame] | 1671 | ModuleFileName = PP->getHeaderSearchInfo().getCachedModuleFileName(Module); |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1672 | Source = ModuleCache; |
| 1673 | } |
| 1674 | |
| 1675 | if (Source == ModuleNotFound) { |
Manman Ren | 11f2a47 | 2016-08-18 17:42:15 +0000 | [diff] [blame] | 1676 | // We can't find a module, error out here. |
Ben Langmuir | 9eb229b | 2014-01-22 23:19:39 +0000 | [diff] [blame] | 1677 | getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found) |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1678 | << ModuleName << SourceRange(ImportLoc, ModuleNameLoc); |
Ben Langmuir | 9eb229b | 2014-01-22 23:19:39 +0000 | [diff] [blame] | 1679 | ModuleBuildFailed = true; |
| 1680 | return ModuleLoadResult(); |
| 1681 | } |
| 1682 | |
Richard Smith | d520a25 | 2015-07-21 18:07:47 +0000 | [diff] [blame] | 1683 | if (ModuleFileName.empty()) { |
Manman Ren | 11f2a47 | 2016-08-18 17:42:15 +0000 | [diff] [blame] | 1684 | if (Module && Module->HasIncompatibleModuleFile) { |
Richard Smith | 8a308ec | 2015-11-05 00:54:55 +0000 | [diff] [blame] | 1685 | // We tried and failed to load a module file for this module. Fall |
| 1686 | // back to textual inclusion for its headers. |
Richard Smith | a114c46 | 2016-12-06 00:40:17 +0000 | [diff] [blame] | 1687 | return ModuleLoadResult::ConfigMismatch; |
Richard Smith | 8a308ec | 2015-11-05 00:54:55 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Manuel Klimek | d2e8b04 | 2015-02-20 11:44:41 +0000 | [diff] [blame] | 1690 | getDiagnostics().Report(ModuleNameLoc, diag::err_module_build_disabled) |
| 1691 | << ModuleName; |
| 1692 | ModuleBuildFailed = true; |
| 1693 | return ModuleLoadResult(); |
| 1694 | } |
Richard Smith | d4b230b | 2014-10-27 23:01:16 +0000 | [diff] [blame] | 1695 | |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1696 | // If we don't already have an ASTReader, create one now. |
John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 1697 | if (!ModuleManager) |
| 1698 | createModuleManager(); |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1699 | |
Richard Smith | ce18a18 | 2015-07-14 00:26:00 +0000 | [diff] [blame] | 1700 | llvm::Timer Timer; |
| 1701 | if (FrontendTimerGroup) |
Matthias Braun | ae032b6 | 2016-11-18 19:43:25 +0000 | [diff] [blame] | 1702 | Timer.init("loading." + ModuleFileName, "Loading " + ModuleFileName, |
| 1703 | *FrontendTimerGroup); |
Richard Smith | ce18a18 | 2015-07-14 00:26:00 +0000 | [diff] [blame] | 1704 | llvm::TimeRegion TimeLoading(FrontendTimerGroup ? &Timer : nullptr); |
Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 1705 | llvm::TimeTraceScope TimeScope("Module Load", ModuleName); |
Richard Smith | ce18a18 | 2015-07-14 00:26:00 +0000 | [diff] [blame] | 1706 | |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1707 | // Try to load the module file. If we are not trying to load from the |
| 1708 | // module cache, we don't know how to rebuild modules. |
| 1709 | unsigned ARRFlags = Source == ModuleCache ? |
| 1710 | ASTReader::ARR_OutOfDate | ASTReader::ARR_Missing : |
David Blaikie | 436f7b6 | 2018-11-08 20:47:30 +0000 | [diff] [blame] | 1711 | Source == PrebuiltModulePath ? |
| 1712 | 0 : |
| 1713 | ASTReader::ARR_ConfigurationMismatch; |
Richard Smith | e842a47 | 2014-10-22 02:05:46 +0000 | [diff] [blame] | 1714 | switch (ModuleManager->ReadAST(ModuleFileName, |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1715 | Source == PrebuiltModulePath |
| 1716 | ? serialization::MK_PrebuiltModule |
| 1717 | : Source == ModuleBuildPragma |
| 1718 | ? serialization::MK_ExplicitModule |
| 1719 | : serialization::MK_ImplicitModule, |
| 1720 | ImportLoc, ARRFlags)) { |
Manman Ren | 11f2a47 | 2016-08-18 17:42:15 +0000 | [diff] [blame] | 1721 | case ASTReader::Success: { |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1722 | if (Source != ModuleCache && !Module) { |
Bruno Cardoso Lopes | 52431f3 | 2018-07-18 23:21:19 +0000 | [diff] [blame] | 1723 | Module = PP->getHeaderSearchInfo().lookupModule(ModuleName, true, |
| 1724 | !IsInclusionDirective); |
Manman Ren | 11f2a47 | 2016-08-18 17:42:15 +0000 | [diff] [blame] | 1725 | if (!Module || !Module->getASTFile() || |
| 1726 | FileMgr->getFile(ModuleFileName) != Module->getASTFile()) { |
| 1727 | // Error out if Module does not refer to the file in the prebuilt |
| 1728 | // module path. |
| 1729 | getDiagnostics().Report(ModuleNameLoc, diag::err_module_prebuilt) |
| 1730 | << ModuleName; |
| 1731 | ModuleBuildFailed = true; |
| 1732 | KnownModules[Path[0].first] = nullptr; |
| 1733 | return ModuleLoadResult(); |
| 1734 | } |
| 1735 | } |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1736 | break; |
Manman Ren | 11f2a47 | 2016-08-18 17:42:15 +0000 | [diff] [blame] | 1737 | } |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1738 | |
Eli Friedman | 963ff2c | 2013-09-17 00:51:29 +0000 | [diff] [blame] | 1739 | case ASTReader::OutOfDate: |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 1740 | case ASTReader::Missing: { |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1741 | if (Source != ModuleCache) { |
| 1742 | // We don't know the desired configuration for this module and don't |
| 1743 | // necessarily even have a module map. Since ReadAST already produces |
| 1744 | // diagnostics for these two cases, we simply error out here. |
Manman Ren | 11f2a47 | 2016-08-18 17:42:15 +0000 | [diff] [blame] | 1745 | ModuleBuildFailed = true; |
| 1746 | KnownModules[Path[0].first] = nullptr; |
| 1747 | return ModuleLoadResult(); |
| 1748 | } |
| 1749 | |
Eli Friedman | 963ff2c | 2013-09-17 00:51:29 +0000 | [diff] [blame] | 1750 | // The module file is missing or out-of-date. Build it. |
Ben Langmuir | 9eb229b | 2014-01-22 23:19:39 +0000 | [diff] [blame] | 1751 | assert(Module && "missing module file"); |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 1752 | // Check whether there is a cycle in the module graph. |
| 1753 | ModuleBuildStack ModPath = getSourceManager().getModuleBuildStack(); |
| 1754 | ModuleBuildStack::iterator Pos = ModPath.begin(), PosEnd = ModPath.end(); |
| 1755 | for (; Pos != PosEnd; ++Pos) { |
| 1756 | if (Pos->first == ModuleName) |
| 1757 | break; |
| 1758 | } |
| 1759 | |
| 1760 | if (Pos != PosEnd) { |
| 1761 | SmallString<256> CyclePath; |
| 1762 | for (; Pos != PosEnd; ++Pos) { |
| 1763 | CyclePath += Pos->first; |
| 1764 | CyclePath += " -> "; |
| 1765 | } |
| 1766 | CyclePath += ModuleName; |
| 1767 | |
| 1768 | getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle) |
| 1769 | << ModuleName << CyclePath; |
| 1770 | return ModuleLoadResult(); |
| 1771 | } |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1772 | |
| 1773 | // Check whether we have already attempted to build this module (but |
| 1774 | // failed). |
| 1775 | if (getPreprocessorOpts().FailedModules && |
| 1776 | getPreprocessorOpts().FailedModules->hasAlreadyFailed(ModuleName)) { |
| 1777 | getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built) |
| 1778 | << ModuleName |
| 1779 | << SourceRange(ImportLoc, ModuleNameLoc); |
Douglas Gregor | c1bbec8 | 2013-01-25 00:45:27 +0000 | [diff] [blame] | 1780 | ModuleBuildFailed = true; |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1781 | return ModuleLoadResult(); |
| 1782 | } |
| 1783 | |
Ben Langmuir | dbdc036 | 2014-06-17 22:35:27 +0000 | [diff] [blame] | 1784 | // Try to compile and then load the module. |
| 1785 | if (!compileAndLoadModule(*this, ImportLoc, ModuleNameLoc, Module, |
| 1786 | ModuleFileName)) { |
Ben Langmuir | d213aab | 2014-09-26 22:42:23 +0000 | [diff] [blame] | 1787 | assert(getDiagnostics().hasErrorOccurred() && |
| 1788 | "undiagnosed error in compileAndLoadModule"); |
Douglas Gregor | 0f2b463 | 2013-01-10 02:04:18 +0000 | [diff] [blame] | 1789 | if (getPreprocessorOpts().FailedModules) |
| 1790 | getPreprocessorOpts().FailedModules->addFailed(ModuleName); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1791 | KnownModules[Path[0].first] = nullptr; |
Douglas Gregor | c1bbec8 | 2013-01-25 00:45:27 +0000 | [diff] [blame] | 1792 | ModuleBuildFailed = true; |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1793 | return ModuleLoadResult(); |
Douglas Gregor | 188dbef | 2012-11-07 17:46:15 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | // Okay, we've rebuilt and now loaded the module. |
| 1797 | break; |
| 1798 | } |
| 1799 | |
Douglas Gregor | c9ad5fb | 2012-10-22 22:50:17 +0000 | [diff] [blame] | 1800 | case ASTReader::ConfigurationMismatch: |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1801 | if (Source == PrebuiltModulePath) |
| 1802 | // FIXME: We shouldn't be setting HadFatalFailure below if we only |
| 1803 | // produce a warning here! |
Manman Ren | 11f2a47 | 2016-08-18 17:42:15 +0000 | [diff] [blame] | 1804 | getDiagnostics().Report(SourceLocation(), |
| 1805 | diag::warn_module_config_mismatch) |
| 1806 | << ModuleFileName; |
| 1807 | // Fall through to error out. |
Galina Kistanova | e37ad5a | 2017-06-03 06:27:16 +0000 | [diff] [blame] | 1808 | LLVM_FALLTHROUGH; |
Manman Ren | 11f2a47 | 2016-08-18 17:42:15 +0000 | [diff] [blame] | 1809 | case ASTReader::VersionMismatch: |
Douglas Gregor | c9ad5fb | 2012-10-22 22:50:17 +0000 | [diff] [blame] | 1810 | case ASTReader::HadErrors: |
Argyrios Kyrtzidis | dc9fdaf | 2013-05-24 05:44:08 +0000 | [diff] [blame] | 1811 | ModuleLoader::HadFatalFailure = true; |
Richard Smith | 0f99d6a | 2015-08-09 08:48:41 +0000 | [diff] [blame] | 1812 | // FIXME: The ASTReader will already have complained, but can we shoehorn |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1813 | // that diagnostic information into a more useful form? |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1814 | KnownModules[Path[0].first] = nullptr; |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1815 | return ModuleLoadResult(); |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1816 | |
| 1817 | case ASTReader::Failure: |
Argyrios Kyrtzidis | dc9fdaf | 2013-05-24 05:44:08 +0000 | [diff] [blame] | 1818 | ModuleLoader::HadFatalFailure = true; |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1819 | // Already complained, but note now that we failed. |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1820 | KnownModules[Path[0].first] = nullptr; |
Douglas Gregor | c1bbec8 | 2013-01-25 00:45:27 +0000 | [diff] [blame] | 1821 | ModuleBuildFailed = true; |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1822 | return ModuleLoadResult(); |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1823 | } |
Argyrios Kyrtzidis | 43af513 | 2012-09-29 01:06:04 +0000 | [diff] [blame] | 1824 | |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1825 | // Cache the result of this top-level module lookup for later. |
| 1826 | Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first; |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1827 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1828 | |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1829 | // If we never found the module, fail. |
| 1830 | if (!Module) |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1831 | return ModuleLoadResult(); |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1832 | |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1833 | // Verify that the rest of the module path actually corresponds to |
| 1834 | // a submodule. |
Bruno Cardoso Lopes | 84bc0a2 | 2017-12-22 05:04:43 +0000 | [diff] [blame] | 1835 | bool MapPrivateSubModToTopLevel = false; |
Richard Smith | d6509cf | 2018-09-15 01:21:15 +0000 | [diff] [blame] | 1836 | if (Path.size() > 1) { |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1837 | for (unsigned I = 1, N = Path.size(); I != N; ++I) { |
| 1838 | StringRef Name = Path[I].first->getName(); |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1839 | clang::Module *Sub = Module->findSubmodule(Name); |
Bruno Cardoso Lopes | 9284931 | 2018-02-12 23:43:21 +0000 | [diff] [blame] | 1840 | |
| 1841 | // If the user is requesting Foo.Private and it doesn't exist, try to |
| 1842 | // match Foo_Private and emit a warning asking for the user to write |
| 1843 | // @import Foo_Private instead. FIXME: remove this when existing clients |
| 1844 | // migrate off of Foo.Private syntax. |
| 1845 | if (!Sub && PP->getLangOpts().ImplicitModules && Name == "Private" && |
| 1846 | Module == Module->getTopLevelModule()) { |
| 1847 | SmallString<128> PrivateModule(Module->Name); |
| 1848 | PrivateModule.append("_Private"); |
| 1849 | |
| 1850 | SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> PrivPath; |
| 1851 | auto &II = PP->getIdentifierTable().get( |
| 1852 | PrivateModule, PP->getIdentifierInfo(Module->Name)->getTokenID()); |
| 1853 | PrivPath.push_back(std::make_pair(&II, Path[0].second)); |
| 1854 | |
Bruno Cardoso Lopes | 52431f3 | 2018-07-18 23:21:19 +0000 | [diff] [blame] | 1855 | if (PP->getHeaderSearchInfo().lookupModule(PrivateModule, true, |
| 1856 | !IsInclusionDirective)) |
Bruno Cardoso Lopes | 9284931 | 2018-02-12 23:43:21 +0000 | [diff] [blame] | 1857 | Sub = |
| 1858 | loadModule(ImportLoc, PrivPath, Visibility, IsInclusionDirective); |
| 1859 | if (Sub) { |
| 1860 | MapPrivateSubModToTopLevel = true; |
| 1861 | if (!getDiagnostics().isIgnored( |
| 1862 | diag::warn_no_priv_submodule_use_toplevel, ImportLoc)) { |
| 1863 | getDiagnostics().Report(Path[I].second, |
| 1864 | diag::warn_no_priv_submodule_use_toplevel) |
| 1865 | << Path[I].first << Module->getFullModuleName() << PrivateModule |
| 1866 | << SourceRange(Path[0].second, Path[I].second) |
| 1867 | << FixItHint::CreateReplacement(SourceRange(Path[0].second), |
| 1868 | PrivateModule); |
| 1869 | getDiagnostics().Report(Sub->DefinitionLoc, |
| 1870 | diag::note_private_top_level_defined); |
| 1871 | } |
| 1872 | } |
| 1873 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1874 | |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1875 | if (!Sub) { |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1876 | // Attempt to perform typo correction to find a module name that works. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1877 | SmallVector<StringRef, 2> Best; |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1878 | unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1879 | |
| 1880 | for (clang::Module::submodule_iterator J = Module->submodule_begin(), |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1881 | JEnd = Module->submodule_end(); |
Matt Beaumont-Gay | eb44eda | 2011-11-30 19:41:21 +0000 | [diff] [blame] | 1882 | J != JEnd; ++J) { |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1883 | unsigned ED = Name.edit_distance((*J)->Name, |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1884 | /*AllowReplacements=*/true, |
| 1885 | BestEditDistance); |
| 1886 | if (ED <= BestEditDistance) { |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1887 | if (ED < BestEditDistance) { |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1888 | Best.clear(); |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1889 | BestEditDistance = ED; |
| 1890 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1891 | |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1892 | Best.push_back((*J)->Name); |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1893 | } |
| 1894 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1895 | |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1896 | // If there was a clear winner, user it. |
| 1897 | if (Best.size() == 1) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1898 | getDiagnostics().Report(Path[I].second, |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1899 | diag::err_no_submodule_suggest) |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1900 | << Path[I].first << Module->getFullModuleName() << Best[0] |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1901 | << SourceRange(Path[0].second, Path[I-1].second) |
| 1902 | << FixItHint::CreateReplacement(SourceRange(Path[I].second), |
| 1903 | Best[0]); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1904 | |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1905 | Sub = Module->findSubmodule(Best[0]); |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1906 | } |
| 1907 | } |
Bruno Cardoso Lopes | 84bc0a2 | 2017-12-22 05:04:43 +0000 | [diff] [blame] | 1908 | |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1909 | if (!Sub) { |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1910 | // No submodule by this name. Complain, and don't look for further |
| 1911 | // submodules. |
| 1912 | getDiagnostics().Report(Path[I].second, diag::err_no_submodule) |
Douglas Gregor | 6902197 | 2011-11-30 17:33:56 +0000 | [diff] [blame] | 1913 | << Path[I].first << Module->getFullModuleName() |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1914 | << SourceRange(Path[0].second, Path[I-1].second); |
| 1915 | break; |
| 1916 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1917 | |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 1918 | Module = Sub; |
Douglas Gregor | 5196bc6 | 2011-11-30 04:03:44 +0000 | [diff] [blame] | 1919 | } |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1920 | } |
Ben Langmuir | b537a3a | 2014-07-23 15:30:23 +0000 | [diff] [blame] | 1921 | |
Douglas Gregor | 2537a36 | 2011-12-08 17:01:29 +0000 | [diff] [blame] | 1922 | // Make the named module visible, if it's not already part of the module |
| 1923 | // we are parsing. |
Douglas Gregor | 98a52db | 2011-12-20 00:28:52 +0000 | [diff] [blame] | 1924 | if (ModuleName != getLangOpts().CurrentModule) { |
Bruno Cardoso Lopes | 84bc0a2 | 2017-12-22 05:04:43 +0000 | [diff] [blame] | 1925 | if (!Module->IsFromModuleFile && !MapPrivateSubModToTopLevel) { |
Douglas Gregor | 98a52db | 2011-12-20 00:28:52 +0000 | [diff] [blame] | 1926 | // We have an umbrella header or directory that doesn't actually include |
| 1927 | // all of the headers within the directory it covers. Complain about |
| 1928 | // this missing submodule and recover by forgetting that we ever saw |
| 1929 | // this submodule. |
| 1930 | // FIXME: Should we detect this at module load time? It seems fairly |
| 1931 | // expensive (and rare). |
| 1932 | getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule) |
| 1933 | << Module->getFullModuleName() |
| 1934 | << SourceRange(Path.front().second, Path.back().second); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1935 | |
Richard Smith | a114c46 | 2016-12-06 00:40:17 +0000 | [diff] [blame] | 1936 | return ModuleLoadResult::MissingExpected; |
Douglas Gregor | 98a52db | 2011-12-20 00:28:52 +0000 | [diff] [blame] | 1937 | } |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1938 | |
| 1939 | // Check whether this module is available. |
Richard Smith | 27e5aa0 | 2017-06-05 18:57:56 +0000 | [diff] [blame] | 1940 | if (Preprocessor::checkModuleIsAvailable(getLangOpts(), getTarget(), |
| 1941 | getDiagnostics(), Module)) { |
| 1942 | getDiagnostics().Report(ImportLoc, diag::note_module_import_here) |
| 1943 | << SourceRange(Path.front().second, Path.back().second); |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1944 | LastModuleImportLoc = ImportLoc; |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1945 | LastModuleImportResult = ModuleLoadResult(); |
| 1946 | return ModuleLoadResult(); |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
Richard Smith | a7e2cc6 | 2015-05-01 01:53:09 +0000 | [diff] [blame] | 1949 | ModuleManager->makeModuleVisible(Module, Visibility, ImportLoc); |
Douglas Gregor | 98a52db | 2011-12-20 00:28:52 +0000 | [diff] [blame] | 1950 | } |
Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 1951 | |
| 1952 | // Check for any configuration macros that have changed. |
| 1953 | clang::Module *TopModule = Module->getTopLevelModule(); |
| 1954 | for (unsigned I = 0, N = TopModule->ConfigMacros.size(); I != N; ++I) { |
| 1955 | checkConfigMacro(getPreprocessor(), TopModule->ConfigMacros[I], |
| 1956 | Module, ImportLoc); |
| 1957 | } |
| 1958 | |
Bruno Cardoso Lopes | a3b5f71 | 2018-04-16 19:42:32 +0000 | [diff] [blame] | 1959 | // Resolve any remaining module using export_as for this one. |
| 1960 | getPreprocessor() |
| 1961 | .getHeaderSearchInfo() |
| 1962 | .getModuleMap() |
| 1963 | .resolveLinkAsDependencies(TopModule); |
| 1964 | |
Douglas Gregor | 1805b8a | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 1965 | LastModuleImportLoc = ImportLoc; |
Richard Smith | a114c46 | 2016-12-06 00:40:17 +0000 | [diff] [blame] | 1966 | LastModuleImportResult = ModuleLoadResult(Module); |
Douglas Gregor | 7a62657 | 2012-11-29 23:55:25 +0000 | [diff] [blame] | 1967 | return LastModuleImportResult; |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1968 | } |
Douglas Gregor | c147b0b | 2013-01-12 01:29:50 +0000 | [diff] [blame] | 1969 | |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1970 | void CompilerInstance::loadModuleFromSource(SourceLocation ImportLoc, |
| 1971 | StringRef ModuleName, |
| 1972 | StringRef Source) { |
Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 1973 | // Avoid creating filenames with special characters. |
| 1974 | SmallString<128> CleanModuleName(ModuleName); |
| 1975 | for (auto &C : CleanModuleName) |
| 1976 | if (!isAlphanumeric(C)) |
| 1977 | C = '_'; |
| 1978 | |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1979 | // FIXME: Using a randomized filename here means that our intermediate .pcm |
| 1980 | // output is nondeterministic (as .pcm files refer to each other by name). |
| 1981 | // Can this affect the output in any way? |
| 1982 | SmallString<128> ModuleFileName; |
| 1983 | if (std::error_code EC = llvm::sys::fs::createTemporaryFile( |
Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 1984 | CleanModuleName, "pcm", ModuleFileName)) { |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1985 | getDiagnostics().Report(ImportLoc, diag::err_fe_unable_to_open_output) |
| 1986 | << ModuleFileName << EC.message(); |
| 1987 | return; |
| 1988 | } |
Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 1989 | std::string ModuleMapFileName = (CleanModuleName + ".map").str(); |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1990 | |
| 1991 | FrontendInputFile Input( |
| 1992 | ModuleMapFileName, |
| 1993 | InputKind(getLanguageFromOptions(*Invocation->getLangOpts()), |
| 1994 | InputKind::ModuleMap, /*Preprocessed*/true)); |
| 1995 | |
| 1996 | std::string NullTerminatedSource(Source.str()); |
| 1997 | |
| 1998 | auto PreBuildStep = [&](CompilerInstance &Other) { |
| 1999 | // Create a virtual file containing our desired source. |
| 2000 | // FIXME: We shouldn't need to do this. |
| 2001 | const FileEntry *ModuleMapFile = Other.getFileManager().getVirtualFile( |
| 2002 | ModuleMapFileName, NullTerminatedSource.size(), 0); |
| 2003 | Other.getSourceManager().overrideFileContents( |
| 2004 | ModuleMapFile, |
| 2005 | llvm::MemoryBuffer::getMemBuffer(NullTerminatedSource.c_str())); |
| 2006 | |
| 2007 | Other.BuiltModules = std::move(BuiltModules); |
Richard Smith | 86a3ef5 | 2017-06-09 21:24:02 +0000 | [diff] [blame] | 2008 | Other.DeleteBuiltModules = false; |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 2009 | }; |
| 2010 | |
| 2011 | auto PostBuildStep = [this](CompilerInstance &Other) { |
| 2012 | BuiltModules = std::move(Other.BuiltModules); |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 2013 | }; |
| 2014 | |
| 2015 | // Build the module, inheriting any modules that we've built locally. |
| 2016 | if (compileModuleImpl(*this, ImportLoc, ModuleName, Input, StringRef(), |
| 2017 | ModuleFileName, PreBuildStep, PostBuildStep)) { |
| 2018 | BuiltModules[ModuleName] = ModuleFileName.str(); |
| 2019 | llvm::sys::RemoveFileOnSignal(ModuleFileName); |
| 2020 | } |
| 2021 | } |
| 2022 | |
Douglas Gregor | c147b0b | 2013-01-12 01:29:50 +0000 | [diff] [blame] | 2023 | void CompilerInstance::makeModuleVisible(Module *Mod, |
Argyrios Kyrtzidis | 125df05 | 2013-02-01 16:36:12 +0000 | [diff] [blame] | 2024 | Module::NameVisibilityKind Visibility, |
Richard Smith | a7e2cc6 | 2015-05-01 01:53:09 +0000 | [diff] [blame] | 2025 | SourceLocation ImportLoc) { |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 2026 | if (!ModuleManager) |
| 2027 | createModuleManager(); |
| 2028 | if (!ModuleManager) |
| 2029 | return; |
| 2030 | |
Richard Smith | a7e2cc6 | 2015-05-01 01:53:09 +0000 | [diff] [blame] | 2031 | ModuleManager->makeModuleVisible(Mod, Visibility, ImportLoc); |
Douglas Gregor | c147b0b | 2013-01-12 01:29:50 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 2034 | GlobalModuleIndex *CompilerInstance::loadGlobalModuleIndex( |
| 2035 | SourceLocation TriggerLoc) { |
Richard Smith | 3938f0c | 2015-08-15 00:34:15 +0000 | [diff] [blame] | 2036 | if (getPreprocessor().getHeaderSearchInfo().getModuleCachePath().empty()) |
| 2037 | return nullptr; |
John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 2038 | if (!ModuleManager) |
| 2039 | createModuleManager(); |
| 2040 | // Can't do anything if we don't have the module manager. |
| 2041 | if (!ModuleManager) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 2042 | return nullptr; |
John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 2043 | // Get an existing global index. This loads it if not already |
| 2044 | // loaded. |
| 2045 | ModuleManager->loadGlobalIndex(); |
| 2046 | GlobalModuleIndex *GlobalIndex = ModuleManager->getGlobalIndex(); |
| 2047 | // If the global index doesn't exist, create it. |
| 2048 | if (!GlobalIndex && shouldBuildGlobalModuleIndex() && hasFileManager() && |
| 2049 | hasPreprocessor()) { |
| 2050 | llvm::sys::fs::create_directories( |
| 2051 | getPreprocessor().getHeaderSearchInfo().getModuleCachePath()); |
| 2052 | GlobalModuleIndex::writeIndex( |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 2053 | getFileManager(), getPCHContainerReader(), |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 2054 | getPreprocessor().getHeaderSearchInfo().getModuleCachePath()); |
John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 2055 | ModuleManager->resetForReload(); |
| 2056 | ModuleManager->loadGlobalIndex(); |
| 2057 | GlobalIndex = ModuleManager->getGlobalIndex(); |
| 2058 | } |
| 2059 | // For finding modules needing to be imported for fixit messages, |
| 2060 | // we need to make the global index cover all modules, so we do that here. |
| 2061 | if (!HaveFullGlobalModuleIndex && GlobalIndex && !buildingModule()) { |
| 2062 | ModuleMap &MMap = getPreprocessor().getHeaderSearchInfo().getModuleMap(); |
| 2063 | bool RecreateIndex = false; |
| 2064 | for (ModuleMap::module_iterator I = MMap.module_begin(), |
| 2065 | E = MMap.module_end(); I != E; ++I) { |
| 2066 | Module *TheModule = I->second; |
| 2067 | const FileEntry *Entry = TheModule->getASTFile(); |
| 2068 | if (!Entry) { |
| 2069 | SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path; |
| 2070 | Path.push_back(std::make_pair( |
Richard Smith | 629d8e6 | 2015-08-11 00:03:28 +0000 | [diff] [blame] | 2071 | getPreprocessor().getIdentifierInfo(TheModule->Name), TriggerLoc)); |
John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 2072 | std::reverse(Path.begin(), Path.end()); |
Richard Smith | 629d8e6 | 2015-08-11 00:03:28 +0000 | [diff] [blame] | 2073 | // Load a module as hidden. This also adds it to the global index. |
| 2074 | loadModule(TheModule->DefinitionLoc, Path, Module::Hidden, false); |
John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 2075 | RecreateIndex = true; |
| 2076 | } |
| 2077 | } |
| 2078 | if (RecreateIndex) { |
| 2079 | GlobalModuleIndex::writeIndex( |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 2080 | getFileManager(), getPCHContainerReader(), |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 2081 | getPreprocessor().getHeaderSearchInfo().getModuleCachePath()); |
John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 2082 | ModuleManager->resetForReload(); |
| 2083 | ModuleManager->loadGlobalIndex(); |
| 2084 | GlobalIndex = ModuleManager->getGlobalIndex(); |
| 2085 | } |
| 2086 | HaveFullGlobalModuleIndex = true; |
| 2087 | } |
| 2088 | return GlobalIndex; |
| 2089 | } |
John Thompson | 2d94bbb | 2014-04-23 19:04:32 +0000 | [diff] [blame] | 2090 | |
| 2091 | // Check global module index for missing imports. |
| 2092 | bool |
| 2093 | CompilerInstance::lookupMissingImports(StringRef Name, |
| 2094 | SourceLocation TriggerLoc) { |
| 2095 | // Look for the symbol in non-imported modules, but only if an error |
| 2096 | // actually occurred. |
| 2097 | if (!buildingModule()) { |
| 2098 | // Load global module index, or retrieve a previously loaded one. |
| 2099 | GlobalModuleIndex *GlobalIndex = loadGlobalModuleIndex( |
| 2100 | TriggerLoc); |
| 2101 | |
| 2102 | // Only if we have a global index. |
| 2103 | if (GlobalIndex) { |
| 2104 | GlobalModuleIndex::HitSet FoundModules; |
| 2105 | |
| 2106 | // Find the modules that reference the identifier. |
| 2107 | // Note that this only finds top-level modules. |
| 2108 | // We'll let diagnoseTypo find the actual declaration module. |
| 2109 | if (GlobalIndex->lookupIdentifier(Name, FoundModules)) |
| 2110 | return true; |
| 2111 | } |
| 2112 | } |
| 2113 | |
| 2114 | return false; |
| 2115 | } |
David Blaikie | 9941da4 | 2018-11-17 18:04:13 +0000 | [diff] [blame] | 2116 | void CompilerInstance::resetAndLeakSema() { llvm::BuryPointer(takeSema()); } |
Benjamin Kramer | 7de9969 | 2016-11-16 18:15:26 +0000 | [diff] [blame] | 2117 | |
| 2118 | void CompilerInstance::setExternalSemaSource( |
| 2119 | IntrusiveRefCntPtr<ExternalSemaSource> ESS) { |
| 2120 | ExternalSemaSrc = std::move(ESS); |
| 2121 | } |