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