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