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 | d2536a6 | 2011-11-15 22:58:25 +0000 | [diff] [blame] | 89 | return new PCHGenerator(CI.getPreprocessor(), OutputFile, MakeModule, |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 90 | 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 | |
| 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 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 209 | ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 210 | StringRef InFile) { |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 211 | return new ASTConsumer(); |
| 212 | } |
| 213 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 214 | //===----------------------------------------------------------------------===// |
| 215 | // Preprocessor Actions |
| 216 | //===----------------------------------------------------------------------===// |
| 217 | |
| 218 | void DumpRawTokensAction::ExecuteAction() { |
| 219 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 220 | SourceManager &SM = PP.getSourceManager(); |
| 221 | |
| 222 | // Start lexing the specified input file. |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 223 | const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID()); |
| 224 | Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions()); |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 225 | RawLex.SetKeepWhitespaceMode(true); |
| 226 | |
| 227 | Token RawTok; |
| 228 | RawLex.LexFromRawLexer(RawTok); |
| 229 | while (RawTok.isNot(tok::eof)) { |
| 230 | PP.DumpToken(RawTok, true); |
Daniel Dunbar | 33f57f8 | 2009-11-25 10:27:48 +0000 | [diff] [blame] | 231 | llvm::errs() << "\n"; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 232 | RawLex.LexFromRawLexer(RawTok); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | void DumpTokensAction::ExecuteAction() { |
| 237 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 238 | // Start preprocessing the specified input file. |
| 239 | Token Tok; |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 240 | PP.EnterMainSourceFile(); |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 241 | do { |
| 242 | PP.Lex(Tok); |
| 243 | PP.DumpToken(Tok, true); |
Daniel Dunbar | 33f57f8 | 2009-11-25 10:27:48 +0000 | [diff] [blame] | 244 | llvm::errs() << "\n"; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 245 | } while (Tok.isNot(tok::eof)); |
| 246 | } |
| 247 | |
| 248 | void GeneratePTHAction::ExecuteAction() { |
| 249 | CompilerInstance &CI = getCompilerInstance(); |
| 250 | if (CI.getFrontendOpts().OutputFile.empty() || |
| 251 | CI.getFrontendOpts().OutputFile == "-") { |
| 252 | // FIXME: Don't fail this way. |
| 253 | // FIXME: Verify that we can actually seek in the given file. |
Chris Lattner | 83e7a78 | 2010-04-07 22:58:06 +0000 | [diff] [blame] | 254 | llvm::report_fatal_error("PTH requires a seekable file for output!"); |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 255 | } |
| 256 | llvm::raw_fd_ostream *OS = |
| 257 | CI.createDefaultOutputFile(true, getCurrentFile()); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 258 | if (!OS) return; |
| 259 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 260 | CacheTokens(CI.getPreprocessor(), OS); |
| 261 | } |
| 262 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 263 | void PreprocessOnlyAction::ExecuteAction() { |
| 264 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 265 | |
Daniel Dunbar | c72cc50 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 266 | // Ignore unknown pragmas. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 267 | PP.AddPragmaHandler(new EmptyPragmaHandler()); |
Daniel Dunbar | c72cc50 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 268 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 269 | Token Tok; |
| 270 | // Start parsing the specified input file. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 271 | PP.EnterMainSourceFile(); |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 272 | do { |
| 273 | PP.Lex(Tok); |
| 274 | } while (Tok.isNot(tok::eof)); |
| 275 | } |
| 276 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 277 | void PrintPreprocessedAction::ExecuteAction() { |
| 278 | CompilerInstance &CI = getCompilerInstance(); |
Douglas Gregor | 10efbaf | 2011-09-23 23:43:36 +0000 | [diff] [blame] | 279 | // 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] | 280 | // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>). |
Douglas Gregor | 10efbaf | 2011-09-23 23:43:36 +0000 | [diff] [blame] | 281 | // |
| 282 | // Look to see what type of line endings the file uses. If there's a |
| 283 | // CRLF, then we won't open the file up in binary mode. If there is |
| 284 | // just an LF or CR, then we will open the file up in binary mode. |
| 285 | // In this fashion, the output format should match the input format, unless |
| 286 | // the input format has inconsistent line endings. |
| 287 | // |
| 288 | // This should be a relatively fast operation since most files won't have |
| 289 | // all of their source code on a single line. However, that is still a |
| 290 | // concern, so if we scan for too long, we'll just assume the file should |
| 291 | // be opened in binary mode. |
| 292 | bool BinaryMode = true; |
| 293 | bool InvalidFile = false; |
| 294 | const SourceManager& SM = CI.getSourceManager(); |
| 295 | const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(), |
| 296 | &InvalidFile); |
| 297 | if (!InvalidFile) { |
| 298 | const char *cur = Buffer->getBufferStart(); |
| 299 | const char *end = Buffer->getBufferEnd(); |
| 300 | const char *next = (cur != end) ? cur + 1 : end; |
| 301 | |
| 302 | // Limit ourselves to only scanning 256 characters into the source |
| 303 | // file. This is mostly a sanity check in case the file has no |
| 304 | // newlines whatsoever. |
| 305 | if (end - cur > 256) end = cur + 256; |
| 306 | |
| 307 | while (next < end) { |
| 308 | if (*cur == 0x0D) { // CR |
| 309 | if (*next == 0x0A) // CRLF |
| 310 | BinaryMode = false; |
| 311 | |
| 312 | break; |
| 313 | } else if (*cur == 0x0A) // LF |
| 314 | break; |
| 315 | |
| 316 | ++cur, ++next; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile()); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 321 | if (!OS) return; |
| 322 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 323 | DoPrintPreprocessedInput(CI.getPreprocessor(), OS, |
| 324 | CI.getPreprocessorOutputOpts()); |
| 325 | } |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 326 | |
| 327 | void PrintPreambleAction::ExecuteAction() { |
| 328 | switch (getCurrentFileKind()) { |
| 329 | case IK_C: |
| 330 | case IK_CXX: |
| 331 | case IK_ObjC: |
| 332 | case IK_ObjCXX: |
| 333 | case IK_OpenCL: |
Peter Collingbourne | 895fcca | 2010-12-01 03:15:20 +0000 | [diff] [blame] | 334 | case IK_CUDA: |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 335 | break; |
| 336 | |
| 337 | case IK_None: |
| 338 | case IK_Asm: |
| 339 | case IK_PreprocessedC: |
| 340 | case IK_PreprocessedCXX: |
| 341 | case IK_PreprocessedObjC: |
| 342 | case IK_PreprocessedObjCXX: |
| 343 | case IK_AST: |
| 344 | case IK_LLVM_IR: |
| 345 | // We can't do anything with these. |
| 346 | return; |
| 347 | } |
| 348 | |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 349 | CompilerInstance &CI = getCompilerInstance(); |
| 350 | llvm::MemoryBuffer *Buffer |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 351 | = CI.getFileManager().getBufferForFile(getCurrentFile()); |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 352 | if (Buffer) { |
Argyrios Kyrtzidis | 03c107a | 2011-08-25 20:39:19 +0000 | [diff] [blame] | 353 | unsigned Preamble = Lexer::ComputePreamble(Buffer, CI.getLangOpts()).first; |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 354 | llvm::outs().write(Buffer->getBufferStart(), Preamble); |
| 355 | delete Buffer; |
| 356 | } |
| 357 | } |