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 | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 12 | #include "clang/Lex/Preprocessor.h" |
| 13 | #include "clang/Parse/Parser.h" |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 14 | #include "clang/Basic/FileManager.h" |
| 15 | #include "clang/Frontend/AnalysisConsumer.h" |
| 16 | #include "clang/Frontend/ASTConsumers.h" |
| 17 | #include "clang/Frontend/ASTUnit.h" |
| 18 | #include "clang/Frontend/CompilerInstance.h" |
| 19 | #include "clang/Frontend/FixItRewriter.h" |
| 20 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 21 | #include "clang/Frontend/Utils.h" |
| 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 | //===----------------------------------------------------------------------===// |
| 26 | // AST Consumer Actions |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 29 | ASTConsumer *AnalysisAction::CreateASTConsumer(CompilerInstance &CI, |
| 30 | llvm::StringRef InFile) { |
| 31 | return CreateAnalysisConsumer(CI.getPreprocessor(), |
| 32 | CI.getFrontendOpts().OutputFile, |
| 33 | CI.getAnalyzerOpts()); |
| 34 | } |
| 35 | |
| 36 | ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, |
| 37 | llvm::StringRef InFile) { |
| 38 | return CreateASTPrinter(CI.createDefaultOutputFile(false, InFile)); |
| 39 | } |
| 40 | |
| 41 | ASTConsumer *ASTPrintXMLAction::CreateASTConsumer(CompilerInstance &CI, |
| 42 | llvm::StringRef InFile) { |
| 43 | return CreateASTPrinterXML(CI.createDefaultOutputFile(false, InFile, |
| 44 | "xml")); |
| 45 | } |
| 46 | |
| 47 | ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, |
| 48 | llvm::StringRef InFile) { |
| 49 | return CreateASTDumper(); |
| 50 | } |
| 51 | |
| 52 | ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI, |
| 53 | llvm::StringRef InFile) { |
| 54 | return CreateASTViewer(); |
| 55 | } |
| 56 | |
| 57 | ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI, |
| 58 | llvm::StringRef InFile) { |
| 59 | return CreateDeclContextPrinter(); |
| 60 | } |
| 61 | |
| 62 | ASTConsumer *DumpRecordAction::CreateASTConsumer(CompilerInstance &CI, |
| 63 | llvm::StringRef InFile) { |
| 64 | return CreateRecordLayoutDumper(); |
| 65 | } |
| 66 | |
| 67 | ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, |
| 68 | llvm::StringRef InFile) { |
| 69 | const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot; |
| 70 | if (CI.getFrontendOpts().RelocatablePCH && |
| 71 | Sysroot.empty()) { |
| 72 | CI.getDiagnostics().Report(diag::err_relocatable_without_without_isysroot); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, InFile); |
| 77 | if (CI.getFrontendOpts().RelocatablePCH) |
| 78 | return CreatePCHGenerator(CI.getPreprocessor(), OS, Sysroot.c_str()); |
| 79 | |
| 80 | return CreatePCHGenerator(CI.getPreprocessor(), OS); |
| 81 | } |
| 82 | |
| 83 | ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, |
| 84 | llvm::StringRef InFile) { |
| 85 | return CreateHTMLPrinter(CI.createDefaultOutputFile(false, InFile), |
| 86 | CI.getPreprocessor()); |
| 87 | } |
| 88 | |
| 89 | ASTConsumer *InheritanceViewAction::CreateASTConsumer(CompilerInstance &CI, |
| 90 | llvm::StringRef InFile) { |
| 91 | return CreateInheritanceViewer(CI.getFrontendOpts().ViewClassInheritance); |
| 92 | } |
| 93 | |
| 94 | FixItAction::FixItAction() {} |
| 95 | FixItAction::~FixItAction() {} |
| 96 | |
| 97 | ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI, |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 98 | llvm::StringRef InFile) { |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 99 | return new ASTConsumer(); |
| 100 | } |
| 101 | |
| 102 | /// AddFixItLocations - Add any individual user specified "fix-it" locations, |
| 103 | /// and return true on success. |
| 104 | static bool AddFixItLocations(CompilerInstance &CI, |
| 105 | FixItRewriter &FixItRewrite) { |
| 106 | const std::vector<ParsedSourceLocation> &Locs = |
| 107 | CI.getFrontendOpts().FixItLocations; |
| 108 | for (unsigned i = 0, e = Locs.size(); i != e; ++i) { |
| 109 | const FileEntry *File = CI.getFileManager().getFile(Locs[i].FileName); |
| 110 | if (!File) { |
| 111 | CI.getDiagnostics().Report(diag::err_fe_unable_to_find_fixit_file) |
| 112 | << Locs[i].FileName; |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | RequestedSourceLocation Requested; |
| 117 | Requested.File = File; |
| 118 | Requested.Line = Locs[i].Line; |
| 119 | Requested.Column = Locs[i].Column; |
| 120 | FixItRewrite.addFixItLocation(Requested); |
| 121 | } |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | bool FixItAction::BeginSourceFileAction(CompilerInstance &CI, |
| 127 | llvm::StringRef Filename) { |
| 128 | Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(), |
| 129 | CI.getLangOpts())); |
| 130 | if (!AddFixItLocations(CI, *Rewriter)) |
| 131 | return false; |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | void FixItAction::EndSourceFileAction() { |
| 137 | const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts(); |
| 138 | Rewriter->WriteFixedFile(getCurrentFile(), FEOpts.OutputFile); |
| 139 | } |
| 140 | |
| 141 | ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, |
| 142 | llvm::StringRef InFile) { |
| 143 | return CreateObjCRewriter(InFile, |
| 144 | CI.createDefaultOutputFile(true, InFile, "cpp"), |
| 145 | CI.getDiagnostics(), CI.getLangOpts(), |
| 146 | CI.getDiagnosticOpts().NoRewriteMacros); |
| 147 | } |
| 148 | |
| 149 | ASTConsumer *RewriteBlocksAction::CreateASTConsumer(CompilerInstance &CI, |
| 150 | llvm::StringRef InFile) { |
| 151 | return CreateBlockRewriter(InFile, CI.getDiagnostics(), CI.getLangOpts()); |
| 152 | } |
| 153 | |
| 154 | ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, |
| 155 | llvm::StringRef InFile) { |
| 156 | return new ASTConsumer(); |
| 157 | } |
| 158 | |
| 159 | CodeGenAction::CodeGenAction(unsigned _Act) : Act(_Act) {} |
| 160 | |
| 161 | ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI, |
| 162 | llvm::StringRef InFile) { |
| 163 | BackendAction BA = static_cast<BackendAction>(Act); |
| 164 | llvm::OwningPtr<llvm::raw_ostream> OS; |
| 165 | if (BA == Backend_EmitAssembly) |
| 166 | OS.reset(CI.createDefaultOutputFile(false, InFile, "s")); |
| 167 | else if (BA == Backend_EmitLL) |
| 168 | OS.reset(CI.createDefaultOutputFile(false, InFile, "ll")); |
| 169 | else if (BA == Backend_EmitBC) |
| 170 | OS.reset(CI.createDefaultOutputFile(true, InFile, "bc")); |
| 171 | |
| 172 | return CreateBackendConsumer(BA, CI.getDiagnostics(), CI.getLangOpts(), |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 173 | CI.getCodeGenOpts(), CI.getTargetOpts(), InFile, |
| 174 | OS.take(), CI.getLLVMContext()); |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | EmitAssemblyAction::EmitAssemblyAction() |
| 178 | : CodeGenAction(Backend_EmitAssembly) {} |
| 179 | |
| 180 | EmitBCAction::EmitBCAction() : CodeGenAction(Backend_EmitBC) {} |
| 181 | |
| 182 | EmitLLVMAction::EmitLLVMAction() : CodeGenAction(Backend_EmitLL) {} |
| 183 | |
| 184 | EmitLLVMOnlyAction::EmitLLVMOnlyAction() : CodeGenAction(Backend_EmitNothing) {} |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 185 | |
| 186 | //===----------------------------------------------------------------------===// |
| 187 | // Preprocessor Actions |
| 188 | //===----------------------------------------------------------------------===// |
| 189 | |
| 190 | void DumpRawTokensAction::ExecuteAction() { |
| 191 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 192 | SourceManager &SM = PP.getSourceManager(); |
| 193 | |
| 194 | // Start lexing the specified input file. |
| 195 | Lexer RawLex(SM.getMainFileID(), SM, PP.getLangOptions()); |
| 196 | RawLex.SetKeepWhitespaceMode(true); |
| 197 | |
| 198 | Token RawTok; |
| 199 | RawLex.LexFromRawLexer(RawTok); |
| 200 | while (RawTok.isNot(tok::eof)) { |
| 201 | PP.DumpToken(RawTok, true); |
Daniel Dunbar | 33f57f8 | 2009-11-25 10:27:48 +0000 | [diff] [blame] | 202 | llvm::errs() << "\n"; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 203 | RawLex.LexFromRawLexer(RawTok); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void DumpTokensAction::ExecuteAction() { |
| 208 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 209 | // Start preprocessing the specified input file. |
| 210 | Token Tok; |
| 211 | PP.EnterMainSourceFile(); |
| 212 | do { |
| 213 | PP.Lex(Tok); |
| 214 | PP.DumpToken(Tok, true); |
Daniel Dunbar | 33f57f8 | 2009-11-25 10:27:48 +0000 | [diff] [blame] | 215 | llvm::errs() << "\n"; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 216 | } while (Tok.isNot(tok::eof)); |
| 217 | } |
| 218 | |
| 219 | void GeneratePTHAction::ExecuteAction() { |
| 220 | CompilerInstance &CI = getCompilerInstance(); |
| 221 | if (CI.getFrontendOpts().OutputFile.empty() || |
| 222 | CI.getFrontendOpts().OutputFile == "-") { |
| 223 | // FIXME: Don't fail this way. |
| 224 | // FIXME: Verify that we can actually seek in the given file. |
Gabor Greif | 56e4713 | 2009-11-26 15:18:50 +0000 | [diff] [blame] | 225 | llvm::llvm_report_error("PTH requires a seekable file for output!"); |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 226 | } |
| 227 | llvm::raw_fd_ostream *OS = |
| 228 | CI.createDefaultOutputFile(true, getCurrentFile()); |
| 229 | CacheTokens(CI.getPreprocessor(), OS); |
| 230 | } |
| 231 | |
| 232 | void ParseOnlyAction::ExecuteAction() { |
| 233 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 234 | llvm::OwningPtr<Action> PA(new MinimalAction(PP)); |
| 235 | |
| 236 | Parser P(PP, *PA); |
| 237 | PP.EnterMainSourceFile(); |
| 238 | P.ParseTranslationUnit(); |
| 239 | } |
| 240 | |
| 241 | void PreprocessOnlyAction::ExecuteAction() { |
| 242 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 243 | |
| 244 | Token Tok; |
| 245 | // Start parsing the specified input file. |
| 246 | PP.EnterMainSourceFile(); |
| 247 | do { |
| 248 | PP.Lex(Tok); |
| 249 | } while (Tok.isNot(tok::eof)); |
| 250 | } |
| 251 | |
| 252 | void PrintParseAction::ExecuteAction() { |
| 253 | CompilerInstance &CI = getCompilerInstance(); |
| 254 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 255 | llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile()); |
| 256 | llvm::OwningPtr<Action> PA(CreatePrintParserActionsAction(PP, OS)); |
| 257 | |
| 258 | Parser P(PP, *PA); |
| 259 | PP.EnterMainSourceFile(); |
| 260 | P.ParseTranslationUnit(); |
| 261 | } |
| 262 | |
| 263 | void PrintPreprocessedAction::ExecuteAction() { |
| 264 | CompilerInstance &CI = getCompilerInstance(); |
| 265 | llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile()); |
| 266 | DoPrintPreprocessedInput(CI.getPreprocessor(), OS, |
| 267 | CI.getPreprocessorOutputOpts()); |
| 268 | } |
| 269 | |
| 270 | void RewriteMacrosAction::ExecuteAction() { |
| 271 | CompilerInstance &CI = getCompilerInstance(); |
| 272 | llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile()); |
| 273 | RewriteMacrosInInput(CI.getPreprocessor(), OS); |
| 274 | } |
| 275 | |
| 276 | void RewriteTestAction::ExecuteAction() { |
| 277 | CompilerInstance &CI = getCompilerInstance(); |
| 278 | llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile()); |
| 279 | DoRewriteTest(CI.getPreprocessor(), OS); |
| 280 | } |