blob: f00e7fd9c91307d8eab955150358d4fa3ace4e72 [file] [log] [blame]
Daniel Dunbar9b414d32010-06-15 17:48:49 +00001//===--- 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"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000023#include "llvm/Support/Path.h"
Daniel Dunbar9b414d32010-06-15 17:48:49 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// AST Consumer Actions
28//===----------------------------------------------------------------------===//
29
30ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000031 StringRef InFile) {
32 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
Daniel Dunbar9b414d32010-06-15 17:48:49 +000033 return CreateHTMLPrinter(OS, CI.getPreprocessor());
34 return 0;
35}
36
37FixItAction::FixItAction() {}
38FixItAction::~FixItAction() {}
39
40ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000041 StringRef InFile) {
Daniel Dunbar9b414d32010-06-15 17:48:49 +000042 return new ASTConsumer();
43}
44
Benjamin Kramer79ba2a62010-10-22 16:48:22 +000045namespace {
Nick Lewycky96872c42010-08-15 16:47:39 +000046class FixItRewriteInPlace : public FixItOptions {
47public:
48 std::string RewriteFilename(const std::string &Filename) { return Filename; }
49};
50
Nick Lewycky1450f262010-08-13 17:31:00 +000051class FixItActionSuffixInserter : public FixItOptions {
Daniel Dunbar9b414d32010-06-15 17:48:49 +000052 std::string NewSuffix;
53
54public:
Nick Lewycky1450f262010-08-13 17:31:00 +000055 FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
56 : NewSuffix(NewSuffix) {
57 this->FixWhatYouCan = FixWhatYouCan;
58 }
Daniel Dunbar9b414d32010-06-15 17:48:49 +000059
60 std::string RewriteFilename(const std::string &Filename) {
Michael J. Spencerd5b08be2010-12-18 04:13:32 +000061 llvm::SmallString<128> Path(Filename);
62 llvm::sys::path::replace_extension(Path,
63 NewSuffix + llvm::sys::path::extension(Path));
64 return Path.str();
Daniel Dunbar9b414d32010-06-15 17:48:49 +000065 }
66};
Benjamin Kramer79ba2a62010-10-22 16:48:22 +000067} // end anonymous namespace
Daniel Dunbar9b414d32010-06-15 17:48:49 +000068
69bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000070 StringRef Filename) {
Daniel Dunbar9b414d32010-06-15 17:48:49 +000071 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
72 if (!FEOpts.FixItSuffix.empty()) {
Nick Lewycky1450f262010-08-13 17:31:00 +000073 FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
74 FEOpts.FixWhatYouCan));
Daniel Dunbar9b414d32010-06-15 17:48:49 +000075 } else {
Nick Lewycky96872c42010-08-15 16:47:39 +000076 FixItOpts.reset(new FixItRewriteInPlace);
77 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
Daniel Dunbar9b414d32010-06-15 17:48:49 +000078 }
79 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
Nick Lewycky1450f262010-08-13 17:31:00 +000080 CI.getLangOpts(), FixItOpts.get()));
Daniel Dunbar9b414d32010-06-15 17:48:49 +000081 return true;
82}
83
84void FixItAction::EndSourceFileAction() {
85 // Otherwise rewrite all files.
86 Rewriter->WriteFixedFiles();
87}
88
89//===----------------------------------------------------------------------===//
90// Preprocessor Actions
91//===----------------------------------------------------------------------===//
92
93ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000094 StringRef InFile) {
95 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp"))
Daniel Dunbar9b414d32010-06-15 17:48:49 +000096 return CreateObjCRewriter(InFile, OS,
97 CI.getDiagnostics(), CI.getLangOpts(),
98 CI.getDiagnosticOpts().NoRewriteMacros);
99 return 0;
100}
101
102void RewriteMacrosAction::ExecuteAction() {
103 CompilerInstance &CI = getCompilerInstance();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000104 raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000105 if (!OS) return;
106
107 RewriteMacrosInInput(CI.getPreprocessor(), OS);
108}
109
110void RewriteTestAction::ExecuteAction() {
111 CompilerInstance &CI = getCompilerInstance();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000112 raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000113 if (!OS) return;
114
115 DoRewriteTest(CI.getPreprocessor(), OS);
116}