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