blob: 6d98987a1470c471bc712bb9efe3a78d43d18ea0 [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 Lewycky1450f262010-08-13 17:31:00 +000045class FixItActionSuffixInserter : public FixItOptions {
Daniel Dunbar9b414d32010-06-15 17:48:49 +000046 std::string NewSuffix;
47
48public:
Nick Lewycky1450f262010-08-13 17:31:00 +000049 FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
50 : NewSuffix(NewSuffix) {
51 this->FixWhatYouCan = FixWhatYouCan;
52 }
Daniel Dunbar9b414d32010-06-15 17:48:49 +000053
54 std::string RewriteFilename(const std::string &Filename) {
55 llvm::sys::Path Path(Filename);
56 std::string Suffix = Path.getSuffix();
57 Path.eraseSuffix();
58 Path.appendSuffix(NewSuffix + "." + Suffix);
59 return Path.c_str();
60 }
61};
62
63bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
64 llvm::StringRef Filename) {
65 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
66 if (!FEOpts.FixItSuffix.empty()) {
Nick Lewycky1450f262010-08-13 17:31:00 +000067 FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
68 FEOpts.FixWhatYouCan));
Daniel Dunbar9b414d32010-06-15 17:48:49 +000069 } else {
Nick Lewycky1450f262010-08-13 17:31:00 +000070 FixItOpts.reset();
Daniel Dunbar9b414d32010-06-15 17:48:49 +000071 }
72 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
Nick Lewycky1450f262010-08-13 17:31:00 +000073 CI.getLangOpts(), FixItOpts.get()));
Daniel Dunbar9b414d32010-06-15 17:48:49 +000074 return true;
75}
76
77void FixItAction::EndSourceFileAction() {
78 // Otherwise rewrite all files.
79 Rewriter->WriteFixedFiles();
80}
81
82//===----------------------------------------------------------------------===//
83// Preprocessor Actions
84//===----------------------------------------------------------------------===//
85
86ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI,
87 llvm::StringRef InFile) {
88 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp"))
89 return CreateObjCRewriter(InFile, OS,
90 CI.getDiagnostics(), CI.getLangOpts(),
91 CI.getDiagnosticOpts().NoRewriteMacros);
92 return 0;
93}
94
95void RewriteMacrosAction::ExecuteAction() {
96 CompilerInstance &CI = getCompilerInstance();
97 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
98 if (!OS) return;
99
100 RewriteMacrosInInput(CI.getPreprocessor(), OS);
101}
102
103void RewriteTestAction::ExecuteAction() {
104 CompilerInstance &CI = getCompilerInstance();
105 llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
106 if (!OS) return;
107
108 DoRewriteTest(CI.getPreprocessor(), OS);
109}