|
9 | 9 |
|
10 | 10 | #include "clang/Frontend/FrontendActions.h"
|
11 | 11 | #include "clang/AST/ASTConsumer.h"
|
| 12 | +#include "clang/Lex/HeaderSearch.h" |
12 | 13 | #include "clang/Lex/Pragma.h"
|
13 | 14 | #include "clang/Lex/Preprocessor.h"
|
14 | 15 | #include "clang/Parse/Parser.h"
|
@@ -113,6 +114,98 @@ bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
|
113 | 114 | return false;
|
114 | 115 | }
|
115 | 116 |
|
| 117 | +ASTConsumer *GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI, |
| 118 | + StringRef InFile) { |
| 119 | + std::string Sysroot; |
| 120 | + std::string OutputFile; |
| 121 | + raw_ostream *OS = 0; |
| 122 | + if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS)) |
| 123 | + return 0; |
| 124 | + |
| 125 | + return new PCHGenerator(CI.getPreprocessor(), OutputFile, /*MakeModule=*/true, |
| 126 | + Sysroot, OS); |
| 127 | +} |
| 128 | + |
| 129 | +bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI, |
| 130 | + StringRef Filename) { |
| 131 | + // Find the module map file. |
| 132 | + const FileEntry *ModuleMap = CI.getFileManager().getFile(Filename); |
| 133 | + if (!ModuleMap) { |
| 134 | + CI.getDiagnostics().Report(diag::err_module_map_not_found) |
| 135 | + << Filename; |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + // Parse the module map file. |
| 140 | + HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); |
| 141 | + if (HS.loadModuleMapFile(ModuleMap)) |
| 142 | + return false; |
| 143 | + |
| 144 | + if (CI.getLangOpts().CurrentModule.empty()) { |
| 145 | + CI.getDiagnostics().Report(diag::err_missing_module_name); |
| 146 | + |
| 147 | + // FIXME: Eventually, we could consider asking whether there was just |
| 148 | + // a single module described in the module map, and use that as a |
| 149 | + // default. Then it would be fairly trivial to just "compile" a module |
| 150 | + // map with a single module (the common case). |
| 151 | + return false; |
| 152 | + } |
| 153 | + |
| 154 | + // Dig out the module definition. |
| 155 | + ModuleMap::Module *Module = HS.getModule(CI.getLangOpts().CurrentModule, |
| 156 | + /*AllowSearch=*/false); |
| 157 | + if (!Module) { |
| 158 | + CI.getDiagnostics().Report(diag::err_missing_module) |
| 159 | + << CI.getLangOpts().CurrentModule << Filename; |
| 160 | + |
| 161 | + return false; |
| 162 | + } |
| 163 | + |
| 164 | + // If there is an umbrella header, use it as our actual input file. |
| 165 | + if (Module->UmbrellaHeader) { |
| 166 | + // FIXME: Deal with explicit submodule headers, which won't be contained |
| 167 | + // within the umbrella header. |
| 168 | + fprintf(stderr, "note: using umbrella header \"%s\"\n", |
| 169 | + Module->UmbrellaHeader->getName()); |
| 170 | + setCurrentFile(Module->UmbrellaHeader->getName(), getCurrentFileKind()); |
| 171 | + } else { |
| 172 | + // FIXME: Deal with the non-umbrella case, where we have to synthesize |
| 173 | + // a header to parse. |
| 174 | + // FIXME: Diagnose, at least for now. |
| 175 | + return false; |
| 176 | + } |
| 177 | + |
| 178 | + return true; |
| 179 | +} |
| 180 | + |
| 181 | +bool GenerateModuleAction::ComputeASTConsumerArguments(CompilerInstance &CI, |
| 182 | + StringRef InFile, |
| 183 | + std::string &Sysroot, |
| 184 | + std::string &OutputFile, |
| 185 | + raw_ostream *&OS) { |
| 186 | + // If no output file was provided, figure out where this module would go |
| 187 | + // in the module cache. |
| 188 | + if (CI.getFrontendOpts().OutputFile.empty()) { |
| 189 | + HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); |
| 190 | + llvm::SmallString<256> ModuleFileName(HS.getModuleCachePath()); |
| 191 | + llvm::sys::path::append(ModuleFileName, |
| 192 | + CI.getLangOpts().CurrentModule + ".pcm"); |
| 193 | + CI.getFrontendOpts().OutputFile = ModuleFileName.str(); |
| 194 | + } |
| 195 | + |
| 196 | + // We use createOutputFile here because this is exposed via libclang, and we |
| 197 | + // must disable the RemoveFileOnSignal behavior. |
| 198 | + // We use a temporary to avoid race conditions. |
| 199 | + OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true, |
| 200 | + /*RemoveFileOnSignal=*/false, InFile, |
| 201 | + /*Extension=*/"", /*useTemporary=*/true); |
| 202 | + if (!OS) |
| 203 | + return true; |
| 204 | + |
| 205 | + OutputFile = CI.getFrontendOpts().OutputFile; |
| 206 | + return false; |
| 207 | +} |
| 208 | + |
116 | 209 | ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
|
117 | 210 | StringRef InFile) {
|
118 | 211 | return new ASTConsumer();
|
|
0 commit comments