blob: ec99ace18a92a06d54c923ddd20888c2f752e6dd [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,
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
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) {
61 llvm::sys::Path Path(Filename);
62 std::string Suffix = Path.getSuffix();
63 Path.eraseSuffix();
64 Path.appendSuffix(NewSuffix + "." + Suffix);
65 return Path.c_str();
66 }
67};
Benjamin Kramer79ba2a62010-10-22 16:48:22 +000068} // end anonymous namespace
Daniel Dunbar9b414d32010-06-15 17:48:49 +000069
70bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
71 llvm::StringRef Filename) {
72 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
73 if (!FEOpts.FixItSuffix.empty()) {
Nick Lewycky1450f262010-08-13 17:31:00 +000074 FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
75 FEOpts.FixWhatYouCan));
Daniel Dunbar9b414d32010-06-15 17:48:49 +000076 } else {
Nick Lewycky96872c42010-08-15 16:47:39 +000077 FixItOpts.reset(new FixItRewriteInPlace);
78 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
Daniel Dunbar9b414d32010-06-15 17:48:49 +000079 }
80 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
Nick Lewycky1450f262010-08-13 17:31:00 +000081 CI.getLangOpts(), FixItOpts.get()));
Daniel Dunbar9b414d32010-06-15 17:48:49 +000082 return true;
83}
84
85void FixItAction::EndSourceFileAction() {
86 // Otherwise rewrite all files.
87 Rewriter->WriteFixedFiles();
88}
89
90//===----------------------------------------------------------------------===//
91// Preprocessor Actions
92//===----------------------------------------------------------------------===//
93
94ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI,
95 llvm::StringRef InFile) {
96 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp"))
97 return CreateObjCRewriter(InFile, OS,
98 CI.getDiagnostics(), CI.getLangOpts(),
99 CI.getDiagnosticOpts().NoRewriteMacros);
100 return 0;
101}
102
103void RewriteMacrosAction::ExecuteAction() {
104 CompilerInstance &CI = getCompilerInstance();
105 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
106 if (!OS) return;
107
108 RewriteMacrosInInput(CI.getPreprocessor(), OS);
109}
110
111void RewriteTestAction::ExecuteAction() {
112 CompilerInstance &CI = getCompilerInstance();
113 llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
114 if (!OS) return;
115
116 DoRewriteTest(CI.getPreprocessor(), OS);
117}