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