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" |
Daniel Dunbar | c72cc50 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 12 | #include "clang/Lex/Pragma.h" |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 13 | #include "clang/Lex/Preprocessor.h" |
| 14 | #include "clang/Parse/Parser.h" |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 15 | #include "clang/Basic/FileManager.h" |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/ASTConsumers.h" |
| 17 | #include "clang/Frontend/ASTUnit.h" |
| 18 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 20 | #include "clang/Frontend/Utils.h" |
Sebastian Redl | 7faa2ec | 2010-08-18 23:56:37 +0000 | [diff] [blame] | 21 | #include "clang/Serialization/ASTWriter.h" |
Nick Lewycky | ba5f6ec | 2010-04-24 01:30:46 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/OwningPtr.h" |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | using namespace clang; |
| 26 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 2758595 | 2010-03-19 19:44:04 +0000 | [diff] [blame] | 28 | // Custom Actions |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | ASTConsumer *InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 32 | StringRef InFile) { |
Daniel Dunbar | 2758595 | 2010-03-19 19:44:04 +0000 | [diff] [blame] | 33 | return new ASTConsumer(); |
| 34 | } |
| 35 | |
| 36 | void InitOnlyAction::ExecuteAction() { |
| 37 | } |
| 38 | |
| 39 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 40 | // AST Consumer Actions |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 43 | ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 44 | StringRef InFile) { |
| 45 | if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile)) |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 46 | return CreateASTPrinter(OS); |
| 47 | return 0; |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 50 | ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 51 | StringRef InFile) { |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 52 | return CreateASTDumper(); |
| 53 | } |
| 54 | |
John McCall | f351424 | 2010-11-24 11:21:45 +0000 | [diff] [blame] | 55 | ASTConsumer *ASTDumpXMLAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 56 | StringRef InFile) { |
| 57 | raw_ostream *OS; |
John McCall | f351424 | 2010-11-24 11:21:45 +0000 | [diff] [blame] | 58 | if (CI.getFrontendOpts().OutputFile.empty()) |
| 59 | OS = &llvm::outs(); |
| 60 | else |
| 61 | OS = CI.createDefaultOutputFile(false, InFile); |
| 62 | if (!OS) return 0; |
| 63 | return CreateASTDumperXML(*OS); |
| 64 | } |
| 65 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 66 | ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 67 | StringRef InFile) { |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 68 | return CreateASTViewer(); |
| 69 | } |
| 70 | |
| 71 | ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 72 | StringRef InFile) { |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 73 | return CreateDeclContextPrinter(); |
| 74 | } |
| 75 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 76 | ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 77 | StringRef InFile) { |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 78 | std::string Sysroot; |
Argyrios Kyrtzidis | 8e3df4d | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 79 | std::string OutputFile; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 80 | raw_ostream *OS = 0; |
Douglas Gregor | 9293ba8 | 2011-08-25 22:35:51 +0000 | [diff] [blame] | 81 | if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS)) |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 82 | return 0; |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 83 | |
Douglas Gregor | 832d620 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 84 | if (!CI.getFrontendOpts().RelocatablePCH) |
| 85 | Sysroot.clear(); |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 86 | return new PCHGenerator(CI.getPreprocessor(), OutputFile, MakeModule, |
| 87 | Sysroot, OS); |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 91 | StringRef InFile, |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 92 | std::string &Sysroot, |
Argyrios Kyrtzidis | 8e3df4d | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 93 | std::string &OutputFile, |
Douglas Gregor | 9293ba8 | 2011-08-25 22:35:51 +0000 | [diff] [blame] | 94 | raw_ostream *&OS) { |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 95 | Sysroot = CI.getHeaderSearchOpts().Sysroot; |
| 96 | if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) { |
Sebastian Redl | 11c3dc4 | 2010-08-17 17:55:38 +0000 | [diff] [blame] | 97 | CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot); |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 98 | return true; |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Daniel Dunbar | c7a9cda | 2011-01-31 22:00:44 +0000 | [diff] [blame] | 101 | // We use createOutputFile here because this is exposed via libclang, and we |
| 102 | // must disable the RemoveFileOnSignal behavior. |
Argyrios Kyrtzidis | 7e90985 | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 103 | // We use a temporary to avoid race conditions. |
Daniel Dunbar | c7a9cda | 2011-01-31 22:00:44 +0000 | [diff] [blame] | 104 | OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true, |
Argyrios Kyrtzidis | 7e90985 | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 105 | /*RemoveFileOnSignal=*/false, InFile, |
| 106 | /*Extension=*/"", /*useTemporary=*/true); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 107 | if (!OS) |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 108 | return true; |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 109 | |
Argyrios Kyrtzidis | 8e3df4d | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 110 | OutputFile = CI.getFrontendOpts().OutputFile; |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 111 | return false; |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 114 | ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 115 | StringRef InFile) { |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 116 | return new ASTConsumer(); |
| 117 | } |
| 118 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 119 | //===----------------------------------------------------------------------===// |
| 120 | // Preprocessor Actions |
| 121 | //===----------------------------------------------------------------------===// |
| 122 | |
| 123 | void DumpRawTokensAction::ExecuteAction() { |
| 124 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 125 | SourceManager &SM = PP.getSourceManager(); |
| 126 | |
| 127 | // Start lexing the specified input file. |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 128 | const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID()); |
| 129 | Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions()); |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 130 | RawLex.SetKeepWhitespaceMode(true); |
| 131 | |
| 132 | Token RawTok; |
| 133 | RawLex.LexFromRawLexer(RawTok); |
| 134 | while (RawTok.isNot(tok::eof)) { |
| 135 | PP.DumpToken(RawTok, true); |
Daniel Dunbar | 33f57f8 | 2009-11-25 10:27:48 +0000 | [diff] [blame] | 136 | llvm::errs() << "\n"; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 137 | RawLex.LexFromRawLexer(RawTok); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void DumpTokensAction::ExecuteAction() { |
| 142 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 143 | // Start preprocessing the specified input file. |
| 144 | Token Tok; |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 145 | PP.EnterMainSourceFile(); |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 146 | do { |
| 147 | PP.Lex(Tok); |
| 148 | PP.DumpToken(Tok, true); |
Daniel Dunbar | 33f57f8 | 2009-11-25 10:27:48 +0000 | [diff] [blame] | 149 | llvm::errs() << "\n"; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 150 | } while (Tok.isNot(tok::eof)); |
| 151 | } |
| 152 | |
| 153 | void GeneratePTHAction::ExecuteAction() { |
| 154 | CompilerInstance &CI = getCompilerInstance(); |
| 155 | if (CI.getFrontendOpts().OutputFile.empty() || |
| 156 | CI.getFrontendOpts().OutputFile == "-") { |
| 157 | // FIXME: Don't fail this way. |
| 158 | // FIXME: Verify that we can actually seek in the given file. |
Chris Lattner | 83e7a78 | 2010-04-07 22:58:06 +0000 | [diff] [blame] | 159 | llvm::report_fatal_error("PTH requires a seekable file for output!"); |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 160 | } |
| 161 | llvm::raw_fd_ostream *OS = |
| 162 | CI.createDefaultOutputFile(true, getCurrentFile()); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 163 | if (!OS) return; |
| 164 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 165 | CacheTokens(CI.getPreprocessor(), OS); |
| 166 | } |
| 167 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 168 | void PreprocessOnlyAction::ExecuteAction() { |
| 169 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 170 | |
Daniel Dunbar | c72cc50 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 171 | // Ignore unknown pragmas. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 172 | PP.AddPragmaHandler(new EmptyPragmaHandler()); |
Daniel Dunbar | c72cc50 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 173 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 174 | Token Tok; |
| 175 | // Start parsing the specified input file. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 176 | PP.EnterMainSourceFile(); |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 177 | do { |
| 178 | PP.Lex(Tok); |
| 179 | } while (Tok.isNot(tok::eof)); |
| 180 | } |
| 181 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 182 | void PrintPreprocessedAction::ExecuteAction() { |
| 183 | CompilerInstance &CI = getCompilerInstance(); |
Steve Naroff | d57d7c0 | 2010-01-05 17:33:23 +0000 | [diff] [blame] | 184 | // Output file needs to be set to 'Binary', to avoid converting Unix style |
| 185 | // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 186 | raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile()); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 187 | if (!OS) return; |
| 188 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 189 | DoPrintPreprocessedInput(CI.getPreprocessor(), OS, |
| 190 | CI.getPreprocessorOutputOpts()); |
| 191 | } |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 192 | |
| 193 | void PrintPreambleAction::ExecuteAction() { |
| 194 | switch (getCurrentFileKind()) { |
| 195 | case IK_C: |
| 196 | case IK_CXX: |
| 197 | case IK_ObjC: |
| 198 | case IK_ObjCXX: |
| 199 | case IK_OpenCL: |
Peter Collingbourne | 895fcca | 2010-12-01 03:15:20 +0000 | [diff] [blame] | 200 | case IK_CUDA: |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 201 | break; |
| 202 | |
| 203 | case IK_None: |
| 204 | case IK_Asm: |
| 205 | case IK_PreprocessedC: |
| 206 | case IK_PreprocessedCXX: |
| 207 | case IK_PreprocessedObjC: |
| 208 | case IK_PreprocessedObjCXX: |
| 209 | case IK_AST: |
| 210 | case IK_LLVM_IR: |
| 211 | // We can't do anything with these. |
| 212 | return; |
| 213 | } |
| 214 | |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 215 | CompilerInstance &CI = getCompilerInstance(); |
| 216 | llvm::MemoryBuffer *Buffer |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 217 | = CI.getFileManager().getBufferForFile(getCurrentFile()); |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 218 | if (Buffer) { |
Argyrios Kyrtzidis | 03c107a | 2011-08-25 20:39:19 +0000 | [diff] [blame] | 219 | unsigned Preamble = Lexer::ComputePreamble(Buffer, CI.getLangOpts()).first; |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 220 | llvm::outs().write(Buffer->getBufferStart(), Preamble); |
| 221 | delete Buffer; |
| 222 | } |
| 223 | } |