Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +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/Rewrite/FrontendActions.h" |
| 11 | #include "clang/AST/ASTConsumer.h" |
| 12 | #include "clang/Lex/Preprocessor.h" |
| 13 | #include "clang/Parse/Parser.h" |
| 14 | #include "clang/Basic/FileManager.h" |
| 15 | #include "clang/Frontend/CompilerInstance.h" |
| 16 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 17 | #include "clang/Frontend/Utils.h" |
| 18 | #include "clang/Rewrite/ASTConsumers.h" |
| 19 | #include "clang/Rewrite/FixItRewriter.h" |
| 20 | #include "clang/Rewrite/Rewriters.h" |
| 21 | #include "llvm/ADT/OwningPtr.h" |
| 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | #include "llvm/System/Path.h" |
| 24 | using namespace clang; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // AST Consumer Actions |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, |
| 31 | llvm::StringRef InFile) { |
| 32 | if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile)) |
| 33 | return CreateHTMLPrinter(OS, CI.getPreprocessor()); |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | FixItAction::FixItAction() {} |
| 38 | FixItAction::~FixItAction() {} |
| 39 | |
| 40 | ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI, |
| 41 | llvm::StringRef InFile) { |
| 42 | return new ASTConsumer(); |
| 43 | } |
| 44 | |
| 45 | class FixItActionSuffixInserter : public FixItPathRewriter { |
| 46 | std::string NewSuffix; |
| 47 | |
| 48 | public: |
| 49 | explicit FixItActionSuffixInserter(std::string NewSuffix) |
| 50 | : NewSuffix(NewSuffix) {} |
| 51 | |
| 52 | std::string RewriteFilename(const std::string &Filename) { |
| 53 | llvm::sys::Path Path(Filename); |
| 54 | std::string Suffix = Path.getSuffix(); |
| 55 | Path.eraseSuffix(); |
| 56 | Path.appendSuffix(NewSuffix + "." + Suffix); |
| 57 | return Path.c_str(); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | bool FixItAction::BeginSourceFileAction(CompilerInstance &CI, |
| 62 | llvm::StringRef Filename) { |
| 63 | const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts(); |
| 64 | if (!FEOpts.FixItSuffix.empty()) { |
| 65 | PathRewriter.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix)); |
| 66 | } else { |
| 67 | PathRewriter.reset(); |
| 68 | } |
| 69 | Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(), |
| 70 | CI.getLangOpts(), PathRewriter.get())); |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | void FixItAction::EndSourceFileAction() { |
| 75 | // Otherwise rewrite all files. |
| 76 | Rewriter->WriteFixedFiles(); |
| 77 | } |
| 78 | |
| 79 | //===----------------------------------------------------------------------===// |
| 80 | // Preprocessor Actions |
| 81 | //===----------------------------------------------------------------------===// |
| 82 | |
| 83 | ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, |
| 84 | llvm::StringRef InFile) { |
| 85 | if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp")) |
| 86 | return CreateObjCRewriter(InFile, OS, |
| 87 | CI.getDiagnostics(), CI.getLangOpts(), |
| 88 | CI.getDiagnosticOpts().NoRewriteMacros); |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | void RewriteMacrosAction::ExecuteAction() { |
| 93 | CompilerInstance &CI = getCompilerInstance(); |
| 94 | llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile()); |
| 95 | if (!OS) return; |
| 96 | |
| 97 | RewriteMacrosInInput(CI.getPreprocessor(), OS); |
| 98 | } |
| 99 | |
| 100 | void RewriteTestAction::ExecuteAction() { |
| 101 | CompilerInstance &CI = getCompilerInstance(); |
| 102 | llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile()); |
| 103 | if (!OS) return; |
| 104 | |
| 105 | DoRewriteTest(CI.getPreprocessor(), OS); |
| 106 | } |