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