Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 1 | //===--- FrontendActions.cpp ----------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "clang/Frontend/FrontendActions.h" |
| 11 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | db1cde7 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 12 | #include "clang/Lex/HeaderSearch.h" |
Daniel Dunbar | c72cc50 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 13 | #include "clang/Lex/Pragma.h" |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 14 | #include "clang/Lex/Preprocessor.h" |
| 15 | #include "clang/Parse/Parser.h" |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 16 | #include "clang/Basic/FileManager.h" |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/ASTConsumers.h" |
| 18 | #include "clang/Frontend/ASTUnit.h" |
| 19 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 21 | #include "clang/Frontend/Utils.h" |
Sebastian Redl | 7faa2ec | 2010-08-18 23:56:37 +0000 | [diff] [blame] | 22 | #include "clang/Serialization/ASTWriter.h" |
Nick Lewycky | ba5f6ec | 2010-04-24 01:30:46 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/OwningPtr.h" |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 10efbaf | 2011-09-23 23:43:36 +0000 | [diff] [blame] | 27 | #include "llvm/Support/system_error.h" |
| 28 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 2758595 | 2010-03-19 19:44:04 +0000 | [diff] [blame] | 32 | // Custom Actions |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
| 35 | ASTConsumer *InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 36 | StringRef InFile) { |
Daniel Dunbar | 2758595 | 2010-03-19 19:44:04 +0000 | [diff] [blame] | 37 | return new ASTConsumer(); |
| 38 | } |
| 39 | |
| 40 | void InitOnlyAction::ExecuteAction() { |
| 41 | } |
| 42 | |
| 43 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 44 | // AST Consumer Actions |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 47 | ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 48 | StringRef InFile) { |
| 49 | if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile)) |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 50 | return CreateASTPrinter(OS); |
| 51 | return 0; |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 54 | ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 55 | StringRef InFile) { |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 56 | return CreateASTDumper(); |
| 57 | } |
| 58 | |
John McCall | f351424 | 2010-11-24 11:21:45 +0000 | [diff] [blame] | 59 | ASTConsumer *ASTDumpXMLAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 60 | StringRef InFile) { |
| 61 | raw_ostream *OS; |
John McCall | f351424 | 2010-11-24 11:21:45 +0000 | [diff] [blame] | 62 | if (CI.getFrontendOpts().OutputFile.empty()) |
| 63 | OS = &llvm::outs(); |
| 64 | else |
| 65 | OS = CI.createDefaultOutputFile(false, InFile); |
| 66 | if (!OS) return 0; |
| 67 | return CreateASTDumperXML(*OS); |
| 68 | } |
| 69 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 70 | ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 71 | StringRef InFile) { |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 72 | return CreateASTViewer(); |
| 73 | } |
| 74 | |
| 75 | ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 76 | StringRef InFile) { |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 77 | return CreateDeclContextPrinter(); |
| 78 | } |
| 79 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 80 | ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 81 | StringRef InFile) { |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 82 | std::string Sysroot; |
Argyrios Kyrtzidis | 8e3df4d | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 83 | std::string OutputFile; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 84 | raw_ostream *OS = 0; |
Douglas Gregor | 9293ba8 | 2011-08-25 22:35:51 +0000 | [diff] [blame] | 85 | if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS)) |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 86 | return 0; |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 87 | |
Douglas Gregor | 832d620 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 88 | if (!CI.getFrontendOpts().RelocatablePCH) |
| 89 | Sysroot.clear(); |
Douglas Gregor | a8cc6ce | 2011-11-30 04:39:39 +0000 | [diff] [blame] | 90 | return new PCHGenerator(CI.getPreprocessor(), OutputFile, 0, Sysroot, OS); |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 94 | StringRef InFile, |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 95 | std::string &Sysroot, |
Argyrios Kyrtzidis | 8e3df4d | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 96 | std::string &OutputFile, |
Douglas Gregor | 9293ba8 | 2011-08-25 22:35:51 +0000 | [diff] [blame] | 97 | raw_ostream *&OS) { |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 98 | Sysroot = CI.getHeaderSearchOpts().Sysroot; |
| 99 | if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) { |
Sebastian Redl | 11c3dc4 | 2010-08-17 17:55:38 +0000 | [diff] [blame] | 100 | CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot); |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 101 | return true; |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Daniel Dunbar | c7a9cda | 2011-01-31 22:00:44 +0000 | [diff] [blame] | 104 | // We use createOutputFile here because this is exposed via libclang, and we |
| 105 | // must disable the RemoveFileOnSignal behavior. |
Argyrios Kyrtzidis | 7e90985 | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 106 | // We use a temporary to avoid race conditions. |
Daniel Dunbar | c7a9cda | 2011-01-31 22:00:44 +0000 | [diff] [blame] | 107 | OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true, |
Argyrios Kyrtzidis | 7e90985 | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 108 | /*RemoveFileOnSignal=*/false, InFile, |
| 109 | /*Extension=*/"", /*useTemporary=*/true); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 110 | if (!OS) |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 111 | return true; |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 112 | |
Argyrios Kyrtzidis | 8e3df4d | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 113 | OutputFile = CI.getFrontendOpts().OutputFile; |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 114 | return false; |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Douglas Gregor | db1cde7 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | a8cc6ce | 2011-11-30 04:39:39 +0000 | [diff] [blame] | 125 | return new PCHGenerator(CI.getPreprocessor(), OutputFile, Module, |
Douglas Gregor | db1cde7 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 126 | Sysroot, OS); |
| 127 | } |
| 128 | |
Douglas Gregor | 261e75b | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 129 | /// \brief Collect the set of header includes needed to construct the given |
| 130 | /// module. |
| 131 | /// |
| 132 | /// \param Module The module we're collecting includes from. |
Douglas Gregor | 8075ce6 | 2011-12-06 17:15:11 +0000 | [diff] [blame] | 133 | /// |
| 134 | /// \param Includes Will be augmented with the set of #includes or #imports |
| 135 | /// needed to load all of the named headers. |
Douglas Gregor | 261e75b | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 136 | static void collectModuleHeaderIncludes(const LangOptions &LangOpts, |
Douglas Gregor | 752769f | 2012-01-05 00:04:05 +0000 | [diff] [blame] | 137 | FileManager &FileMgr, |
| 138 | ModuleMap &ModMap, |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 139 | clang::Module *Module, |
Douglas Gregor | 261e75b | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 140 | llvm::SmallString<256> &Includes) { |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 141 | // Don't collect any headers for unavailable modules. |
| 142 | if (!Module->isAvailable()) |
| 143 | return; |
| 144 | |
Douglas Gregor | 8075ce6 | 2011-12-06 17:15:11 +0000 | [diff] [blame] | 145 | // Add includes for each of these headers. |
| 146 | for (unsigned I = 0, N = Module->Headers.size(); I != N; ++I) { |
| 147 | if (LangOpts.ObjC1) |
| 148 | Includes += "#import \""; |
| 149 | else |
| 150 | Includes += "#include \""; |
| 151 | Includes += Module->Headers[I]->getName(); |
| 152 | Includes += "\"\n"; |
| 153 | } |
| 154 | |
Douglas Gregor | 10694ce | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 155 | if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader()) { |
| 156 | if (Module->Parent) { |
| 157 | // Include the umbrella header for submodules. |
| 158 | if (LangOpts.ObjC1) |
| 159 | Includes += "#import \""; |
| 160 | else |
| 161 | Includes += "#include \""; |
| 162 | Includes += UmbrellaHeader->getName(); |
| 163 | Includes += "\"\n"; |
| 164 | } |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 165 | } else if (const DirectoryEntry *UmbrellaDir = Module->getUmbrellaDir()) { |
Douglas Gregor | 752769f | 2012-01-05 00:04:05 +0000 | [diff] [blame] | 166 | // Add all of the headers we find in this subdirectory. |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 167 | llvm::error_code EC; |
| 168 | llvm::SmallString<128> DirNative; |
| 169 | llvm::sys::path::native(UmbrellaDir->getName(), DirNative); |
Douglas Gregor | 3b29bb9 | 2011-12-12 19:13:53 +0000 | [diff] [blame] | 170 | for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative.str(), EC), |
| 171 | DirEnd; |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 172 | Dir != DirEnd && !EC; Dir.increment(EC)) { |
| 173 | // Check whether this entry has an extension typically associated with |
| 174 | // headers. |
| 175 | if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path())) |
| 176 | .Cases(".h", ".H", ".hh", ".hpp", true) |
| 177 | .Default(false)) |
| 178 | continue; |
| 179 | |
Douglas Gregor | 752769f | 2012-01-05 00:04:05 +0000 | [diff] [blame] | 180 | // If this header is marked 'unavailable' in this module, don't include |
| 181 | // it. |
| 182 | if (const FileEntry *Header = FileMgr.getFile(Dir->path())) |
| 183 | if (ModMap.isHeaderInUnavailableModule(Header)) |
| 184 | continue; |
| 185 | |
Douglas Gregor | 77d029f | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 186 | // Include this header umbrella header for submodules. |
| 187 | if (LangOpts.ObjC1) |
| 188 | Includes += "#import \""; |
| 189 | else |
| 190 | Includes += "#include \""; |
| 191 | Includes += Dir->path(); |
| 192 | Includes += "\"\n"; |
| 193 | } |
Douglas Gregor | 261e75b | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // Recurse into submodules. |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 197 | for (clang::Module::submodule_iterator Sub = Module->submodule_begin(), |
| 198 | SubEnd = Module->submodule_end(); |
Douglas Gregor | 8075ce6 | 2011-12-06 17:15:11 +0000 | [diff] [blame] | 199 | Sub != SubEnd; ++Sub) |
Douglas Gregor | 752769f | 2012-01-05 00:04:05 +0000 | [diff] [blame] | 200 | collectModuleHeaderIncludes(LangOpts, FileMgr, ModMap, *Sub, Includes); |
Douglas Gregor | 261e75b | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Douglas Gregor | db1cde7 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 203 | bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI, |
| 204 | StringRef Filename) { |
| 205 | // Find the module map file. |
| 206 | const FileEntry *ModuleMap = CI.getFileManager().getFile(Filename); |
| 207 | if (!ModuleMap) { |
| 208 | CI.getDiagnostics().Report(diag::err_module_map_not_found) |
| 209 | << Filename; |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | // Parse the module map file. |
| 214 | HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); |
| 215 | if (HS.loadModuleMapFile(ModuleMap)) |
| 216 | return false; |
| 217 | |
| 218 | if (CI.getLangOpts().CurrentModule.empty()) { |
| 219 | CI.getDiagnostics().Report(diag::err_missing_module_name); |
| 220 | |
| 221 | // FIXME: Eventually, we could consider asking whether there was just |
| 222 | // a single module described in the module map, and use that as a |
| 223 | // default. Then it would be fairly trivial to just "compile" a module |
| 224 | // map with a single module (the common case). |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | // Dig out the module definition. |
Douglas Gregor | a8cc6ce | 2011-11-30 04:39:39 +0000 | [diff] [blame] | 229 | Module = HS.getModule(CI.getLangOpts().CurrentModule, /*AllowSearch=*/false); |
Douglas Gregor | db1cde7 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 230 | if (!Module) { |
| 231 | CI.getDiagnostics().Report(diag::err_missing_module) |
| 232 | << CI.getLangOpts().CurrentModule << Filename; |
| 233 | |
| 234 | return false; |
| 235 | } |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 236 | |
| 237 | // Check whether we can build this module at all. |
| 238 | StringRef Feature; |
| 239 | if (!Module->isAvailable(CI.getLangOpts(), Feature)) { |
| 240 | CI.getDiagnostics().Report(diag::err_module_unavailable) |
| 241 | << Module->getFullModuleName() |
| 242 | << Feature; |
| 243 | |
| 244 | return false; |
| 245 | } |
| 246 | |
Douglas Gregor | 10694ce | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 247 | // Do we have an umbrella header for this module? |
| 248 | const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader(); |
| 249 | |
Douglas Gregor | 261e75b | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 250 | // Collect the set of #includes we need to build the module. |
| 251 | llvm::SmallString<256> HeaderContents; |
Douglas Gregor | 752769f | 2012-01-05 00:04:05 +0000 | [diff] [blame] | 252 | collectModuleHeaderIncludes(CI.getLangOpts(), CI.getFileManager(), |
| 253 | CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), |
| 254 | Module, HeaderContents); |
Douglas Gregor | 10694ce | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 255 | if (UmbrellaHeader && HeaderContents.empty()) { |
Douglas Gregor | 261e75b | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 256 | // Simple case: we have an umbrella header and there are no additional |
| 257 | // includes, we can just parse the umbrella header directly. |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 258 | setCurrentInput(FrontendInputFile(UmbrellaHeader->getName(), |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 259 | getCurrentFileKind(), |
| 260 | Module->IsSystem)); |
Douglas Gregor | 261e75b | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 261 | return true; |
Douglas Gregor | db1cde7 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Douglas Gregor | 261e75b | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 264 | FileManager &FileMgr = CI.getFileManager(); |
| 265 | llvm::SmallString<128> HeaderName; |
| 266 | time_t ModTime; |
Douglas Gregor | 10694ce | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 267 | if (UmbrellaHeader) { |
Douglas Gregor | 261e75b | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 268 | // Read in the umbrella header. |
| 269 | // FIXME: Go through the source manager; the umbrella header may have |
| 270 | // been overridden. |
| 271 | std::string ErrorStr; |
| 272 | llvm::MemoryBuffer *UmbrellaContents |
Douglas Gregor | 10694ce | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 273 | = FileMgr.getBufferForFile(UmbrellaHeader, &ErrorStr); |
Douglas Gregor | 261e75b | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 274 | if (!UmbrellaContents) { |
| 275 | CI.getDiagnostics().Report(diag::err_missing_umbrella_header) |
Douglas Gregor | 10694ce | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 276 | << UmbrellaHeader->getName() << ErrorStr; |
Douglas Gregor | 261e75b | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | |
| 280 | // Combine the contents of the umbrella header with the automatically- |
| 281 | // generated includes. |
| 282 | llvm::SmallString<256> OldContents = HeaderContents; |
| 283 | HeaderContents = UmbrellaContents->getBuffer(); |
| 284 | HeaderContents += "\n\n"; |
| 285 | HeaderContents += "/* Module includes */\n"; |
| 286 | HeaderContents += OldContents; |
| 287 | |
| 288 | // Pretend that we're parsing the umbrella header. |
Douglas Gregor | 10694ce | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 289 | HeaderName = UmbrellaHeader->getName(); |
| 290 | ModTime = UmbrellaHeader->getModificationTime(); |
Douglas Gregor | 261e75b | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 291 | |
| 292 | delete UmbrellaContents; |
| 293 | } else { |
| 294 | // Pick an innocuous-sounding name for the umbrella header. |
| 295 | HeaderName = Module->Name + ".h"; |
| 296 | if (FileMgr.getFile(HeaderName, /*OpenFile=*/false, |
| 297 | /*CacheFailure=*/false)) { |
| 298 | // Try again! |
| 299 | HeaderName = Module->Name + "-module.h"; |
| 300 | if (FileMgr.getFile(HeaderName, /*OpenFile=*/false, |
| 301 | /*CacheFailure=*/false)) { |
| 302 | // Pick something ridiculous and go with it. |
| 303 | HeaderName = Module->Name + "-module.hmod"; |
| 304 | } |
| 305 | } |
| 306 | ModTime = time(0); |
| 307 | } |
| 308 | |
| 309 | // Remap the contents of the header name we're using to our synthesized |
| 310 | // buffer. |
| 311 | const FileEntry *HeaderFile = FileMgr.getVirtualFile(HeaderName, |
| 312 | HeaderContents.size(), |
| 313 | ModTime); |
| 314 | llvm::MemoryBuffer *HeaderContentsBuf |
| 315 | = llvm::MemoryBuffer::getMemBufferCopy(HeaderContents); |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 316 | CI.getSourceManager().overrideFileContents(HeaderFile, HeaderContentsBuf); |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 317 | setCurrentInput(FrontendInputFile(HeaderName, getCurrentFileKind(), |
| 318 | Module->IsSystem)); |
Douglas Gregor | db1cde7 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 319 | return true; |
| 320 | } |
| 321 | |
| 322 | bool GenerateModuleAction::ComputeASTConsumerArguments(CompilerInstance &CI, |
| 323 | StringRef InFile, |
| 324 | std::string &Sysroot, |
| 325 | std::string &OutputFile, |
| 326 | raw_ostream *&OS) { |
| 327 | // If no output file was provided, figure out where this module would go |
| 328 | // in the module cache. |
| 329 | if (CI.getFrontendOpts().OutputFile.empty()) { |
| 330 | HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); |
| 331 | llvm::SmallString<256> ModuleFileName(HS.getModuleCachePath()); |
| 332 | llvm::sys::path::append(ModuleFileName, |
| 333 | CI.getLangOpts().CurrentModule + ".pcm"); |
| 334 | CI.getFrontendOpts().OutputFile = ModuleFileName.str(); |
| 335 | } |
| 336 | |
| 337 | // We use createOutputFile here because this is exposed via libclang, and we |
| 338 | // must disable the RemoveFileOnSignal behavior. |
| 339 | // We use a temporary to avoid race conditions. |
| 340 | OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true, |
| 341 | /*RemoveFileOnSignal=*/false, InFile, |
| 342 | /*Extension=*/"", /*useTemporary=*/true); |
| 343 | if (!OS) |
| 344 | return true; |
| 345 | |
| 346 | OutputFile = CI.getFrontendOpts().OutputFile; |
| 347 | return false; |
| 348 | } |
| 349 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 350 | ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 351 | StringRef InFile) { |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 352 | return new ASTConsumer(); |
| 353 | } |
| 354 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 355 | //===----------------------------------------------------------------------===// |
| 356 | // Preprocessor Actions |
| 357 | //===----------------------------------------------------------------------===// |
| 358 | |
| 359 | void DumpRawTokensAction::ExecuteAction() { |
| 360 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 361 | SourceManager &SM = PP.getSourceManager(); |
| 362 | |
| 363 | // Start lexing the specified input file. |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 364 | const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID()); |
| 365 | Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions()); |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 366 | RawLex.SetKeepWhitespaceMode(true); |
| 367 | |
| 368 | Token RawTok; |
| 369 | RawLex.LexFromRawLexer(RawTok); |
| 370 | while (RawTok.isNot(tok::eof)) { |
| 371 | PP.DumpToken(RawTok, true); |
Daniel Dunbar | 33f57f8 | 2009-11-25 10:27:48 +0000 | [diff] [blame] | 372 | llvm::errs() << "\n"; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 373 | RawLex.LexFromRawLexer(RawTok); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | void DumpTokensAction::ExecuteAction() { |
| 378 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 379 | // Start preprocessing the specified input file. |
| 380 | Token Tok; |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 381 | PP.EnterMainSourceFile(); |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 382 | do { |
| 383 | PP.Lex(Tok); |
| 384 | PP.DumpToken(Tok, true); |
Daniel Dunbar | 33f57f8 | 2009-11-25 10:27:48 +0000 | [diff] [blame] | 385 | llvm::errs() << "\n"; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 386 | } while (Tok.isNot(tok::eof)); |
| 387 | } |
| 388 | |
| 389 | void GeneratePTHAction::ExecuteAction() { |
| 390 | CompilerInstance &CI = getCompilerInstance(); |
| 391 | if (CI.getFrontendOpts().OutputFile.empty() || |
| 392 | CI.getFrontendOpts().OutputFile == "-") { |
| 393 | // FIXME: Don't fail this way. |
| 394 | // FIXME: Verify that we can actually seek in the given file. |
Chris Lattner | 83e7a78 | 2010-04-07 22:58:06 +0000 | [diff] [blame] | 395 | llvm::report_fatal_error("PTH requires a seekable file for output!"); |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 396 | } |
| 397 | llvm::raw_fd_ostream *OS = |
| 398 | CI.createDefaultOutputFile(true, getCurrentFile()); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 399 | if (!OS) return; |
| 400 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 401 | CacheTokens(CI.getPreprocessor(), OS); |
| 402 | } |
| 403 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 404 | void PreprocessOnlyAction::ExecuteAction() { |
| 405 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 406 | |
Daniel Dunbar | c72cc50 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 407 | // Ignore unknown pragmas. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 408 | PP.AddPragmaHandler(new EmptyPragmaHandler()); |
Daniel Dunbar | c72cc50 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 409 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 410 | Token Tok; |
| 411 | // Start parsing the specified input file. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 412 | PP.EnterMainSourceFile(); |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 413 | do { |
| 414 | PP.Lex(Tok); |
| 415 | } while (Tok.isNot(tok::eof)); |
| 416 | } |
| 417 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 418 | void PrintPreprocessedAction::ExecuteAction() { |
| 419 | CompilerInstance &CI = getCompilerInstance(); |
Douglas Gregor | 10efbaf | 2011-09-23 23:43:36 +0000 | [diff] [blame] | 420 | // Output file may need to be set to 'Binary', to avoid converting Unix style |
Steve Naroff | d57d7c0 | 2010-01-05 17:33:23 +0000 | [diff] [blame] | 421 | // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>). |
Douglas Gregor | 10efbaf | 2011-09-23 23:43:36 +0000 | [diff] [blame] | 422 | // |
| 423 | // Look to see what type of line endings the file uses. If there's a |
| 424 | // CRLF, then we won't open the file up in binary mode. If there is |
| 425 | // just an LF or CR, then we will open the file up in binary mode. |
| 426 | // In this fashion, the output format should match the input format, unless |
| 427 | // the input format has inconsistent line endings. |
| 428 | // |
| 429 | // This should be a relatively fast operation since most files won't have |
| 430 | // all of their source code on a single line. However, that is still a |
| 431 | // concern, so if we scan for too long, we'll just assume the file should |
| 432 | // be opened in binary mode. |
| 433 | bool BinaryMode = true; |
| 434 | bool InvalidFile = false; |
| 435 | const SourceManager& SM = CI.getSourceManager(); |
| 436 | const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(), |
| 437 | &InvalidFile); |
| 438 | if (!InvalidFile) { |
| 439 | const char *cur = Buffer->getBufferStart(); |
| 440 | const char *end = Buffer->getBufferEnd(); |
| 441 | const char *next = (cur != end) ? cur + 1 : end; |
| 442 | |
| 443 | // Limit ourselves to only scanning 256 characters into the source |
| 444 | // file. This is mostly a sanity check in case the file has no |
| 445 | // newlines whatsoever. |
| 446 | if (end - cur > 256) end = cur + 256; |
| 447 | |
| 448 | while (next < end) { |
| 449 | if (*cur == 0x0D) { // CR |
| 450 | if (*next == 0x0A) // CRLF |
| 451 | BinaryMode = false; |
| 452 | |
| 453 | break; |
| 454 | } else if (*cur == 0x0A) // LF |
| 455 | break; |
| 456 | |
| 457 | ++cur, ++next; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile()); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 462 | if (!OS) return; |
| 463 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 464 | DoPrintPreprocessedInput(CI.getPreprocessor(), OS, |
| 465 | CI.getPreprocessorOutputOpts()); |
| 466 | } |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 467 | |
| 468 | void PrintPreambleAction::ExecuteAction() { |
| 469 | switch (getCurrentFileKind()) { |
| 470 | case IK_C: |
| 471 | case IK_CXX: |
| 472 | case IK_ObjC: |
| 473 | case IK_ObjCXX: |
| 474 | case IK_OpenCL: |
Peter Collingbourne | 895fcca | 2010-12-01 03:15:20 +0000 | [diff] [blame] | 475 | case IK_CUDA: |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 476 | break; |
| 477 | |
| 478 | case IK_None: |
| 479 | case IK_Asm: |
| 480 | case IK_PreprocessedC: |
| 481 | case IK_PreprocessedCXX: |
| 482 | case IK_PreprocessedObjC: |
| 483 | case IK_PreprocessedObjCXX: |
| 484 | case IK_AST: |
| 485 | case IK_LLVM_IR: |
| 486 | // We can't do anything with these. |
| 487 | return; |
| 488 | } |
| 489 | |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 490 | CompilerInstance &CI = getCompilerInstance(); |
| 491 | llvm::MemoryBuffer *Buffer |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 492 | = CI.getFileManager().getBufferForFile(getCurrentFile()); |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 493 | if (Buffer) { |
Argyrios Kyrtzidis | 03c107a | 2011-08-25 20:39:19 +0000 | [diff] [blame] | 494 | unsigned Preamble = Lexer::ComputePreamble(Buffer, CI.getLangOpts()).first; |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 495 | llvm::outs().write(Buffer->getBufferStart(), Preamble); |
| 496 | delete Buffer; |
| 497 | } |
| 498 | } |