blob: 977e0cfba6d07e203fc417428c838d2f112d536f [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"
23#include "llvm/System/Path.h"
24using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// AST Consumer Actions
28//===----------------------------------------------------------------------===//
29
30ASTConsumer *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
37FixItAction::FixItAction() {}
38FixItAction::~FixItAction() {}
39
40ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI,
41 llvm::StringRef InFile) {
42 return new ASTConsumer();
43}
44
Nick Lewycky96872c42010-08-15 16:47:39 +000045class FixItRewriteInPlace : public FixItOptions {
46public:
47 std::string RewriteFilename(const std::string &Filename) { return Filename; }
48};
49
Nick Lewycky1450f262010-08-13 17:31:00 +000050class FixItActionSuffixInserter : public FixItOptions {
Daniel Dunbar9b414d32010-06-15 17:48:49 +000051 std::string NewSuffix;
52
53public:
Nick Lewycky1450f262010-08-13 17:31:00 +000054 FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
55 : NewSuffix(NewSuffix) {
56 this->FixWhatYouCan = FixWhatYouCan;
57 }
Daniel Dunbar9b414d32010-06-15 17:48:49 +000058
59 std::string RewriteFilename(const std::string &Filename) {
60 llvm::sys::Path Path(Filename);
61 std::string Suffix = Path.getSuffix();
62 Path.eraseSuffix();
63 Path.appendSuffix(NewSuffix + "." + Suffix);
64 return Path.c_str();
65 }
66};
67
68bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
69 llvm::StringRef Filename) {
70 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
71 if (!FEOpts.FixItSuffix.empty()) {
Nick Lewycky1450f262010-08-13 17:31:00 +000072 FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
73 FEOpts.FixWhatYouCan));
Daniel Dunbar9b414d32010-06-15 17:48:49 +000074 } else {
Nick Lewycky96872c42010-08-15 16:47:39 +000075 FixItOpts.reset(new FixItRewriteInPlace);
76 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
Daniel Dunbar9b414d32010-06-15 17:48:49 +000077 }
78 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
Nick Lewycky1450f262010-08-13 17:31:00 +000079 CI.getLangOpts(), FixItOpts.get()));
Daniel Dunbar9b414d32010-06-15 17:48:49 +000080 return true;
81}
82
83void FixItAction::EndSourceFileAction() {
84 // Otherwise rewrite all files.
85 Rewriter->WriteFixedFiles();
86}
87
88//===----------------------------------------------------------------------===//
89// Preprocessor Actions
90//===----------------------------------------------------------------------===//
91
92ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI,
93 llvm::StringRef InFile) {
94 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp"))
95 return CreateObjCRewriter(InFile, OS,
96 CI.getDiagnostics(), CI.getLangOpts(),
97 CI.getDiagnosticOpts().NoRewriteMacros);
98 return 0;
99}
100
101void RewriteMacrosAction::ExecuteAction() {
102 CompilerInstance &CI = getCompilerInstance();
103 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
104 if (!OS) return;
105
106 RewriteMacrosInInput(CI.getPreprocessor(), OS);
107}
108
109void RewriteTestAction::ExecuteAction() {
110 CompilerInstance &CI = getCompilerInstance();
111 llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
112 if (!OS) return;
113
114 DoRewriteTest(CI.getPreprocessor(), OS);
115}