1 //===--- FrontendActions.cpp ----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Rewrite/Frontend/FrontendActions.h"
11 #include "clang/AST/ASTConsumer.h"
12 #include "clang/Basic/FileManager.h"
13 #include "clang/Frontend/CompilerInstance.h"
14 #include "clang/Frontend/FrontendActions.h"
15 #include "clang/Frontend/FrontendDiagnostic.h"
16 #include "clang/Frontend/Utils.h"
17 #include "clang/Lex/Preprocessor.h"
18 #include "clang/Parse/Parser.h"
19 #include "clang/Rewrite/Frontend/ASTConsumers.h"
20 #include "clang/Rewrite/Frontend/FixItRewriter.h"
21 #include "clang/Rewrite/Frontend/Rewriters.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Support/raw_ostream.h"
28 using namespace clang;
30 //===----------------------------------------------------------------------===//
31 // AST Consumer Actions
32 //===----------------------------------------------------------------------===//
34 std::unique_ptr<ASTConsumer>
35 HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
36 if (std::unique_ptr<raw_ostream> OS =
37 CI.createDefaultOutputFile(false, InFile))
38 return CreateHTMLPrinter(std::move(OS), CI.getPreprocessor());
42 FixItAction::FixItAction() {}
43 FixItAction::~FixItAction() {}
45 std::unique_ptr<ASTConsumer>
46 FixItAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
47 return llvm::make_unique<ASTConsumer>();
51 class FixItRewriteInPlace : public FixItOptions {
53 FixItRewriteInPlace() { InPlace = true; }
55 std::string RewriteFilename(const std::string &Filename, int &fd) override {
56 llvm_unreachable("don't call RewriteFilename for inplace rewrites");
60 class FixItActionSuffixInserter : public FixItOptions {
61 std::string NewSuffix;
64 FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
65 : NewSuffix(std::move(NewSuffix)) {
66 this->FixWhatYouCan = FixWhatYouCan;
69 std::string RewriteFilename(const std::string &Filename, int &fd) override {
71 SmallString<128> Path(Filename);
72 llvm::sys::path::replace_extension(Path,
73 NewSuffix + llvm::sys::path::extension(Path));
78 class FixItRewriteToTemp : public FixItOptions {
80 std::string RewriteFilename(const std::string &Filename, int &fd) override {
81 SmallString<128> Path;
82 llvm::sys::fs::createTemporaryFile(llvm::sys::path::filename(Filename),
83 llvm::sys::path::extension(Filename).drop_front(), fd,
88 } // end anonymous namespace
90 bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
92 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
93 if (!FEOpts.FixItSuffix.empty()) {
94 FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
95 FEOpts.FixWhatYouCan));
97 FixItOpts.reset(new FixItRewriteInPlace);
98 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
100 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
101 CI.getLangOpts(), FixItOpts.get()));
105 void FixItAction::EndSourceFileAction() {
106 // Otherwise rewrite all files.
107 Rewriter->WriteFixedFiles();
110 bool FixItRecompile::BeginInvocation(CompilerInstance &CI) {
112 std::vector<std::pair<std::string, std::string> > RewrittenFiles;
115 const FrontendOptions &FEOpts = CI.getFrontendOpts();
116 std::unique_ptr<FrontendAction> FixAction(new SyntaxOnlyAction());
117 if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) {
118 std::unique_ptr<FixItOptions> FixItOpts;
119 if (FEOpts.FixToTemporaries)
120 FixItOpts.reset(new FixItRewriteToTemp());
122 FixItOpts.reset(new FixItRewriteInPlace());
123 FixItOpts->Silent = true;
124 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
125 FixItOpts->FixOnlyWarnings = FEOpts.FixOnlyWarnings;
126 FixItRewriter Rewriter(CI.getDiagnostics(), CI.getSourceManager(),
127 CI.getLangOpts(), FixItOpts.get());
128 FixAction->Execute();
130 err = Rewriter.WriteFixedFiles(&RewrittenFiles);
132 FixAction->EndSourceFile();
133 CI.setSourceManager(nullptr);
134 CI.setFileManager(nullptr);
141 CI.getDiagnosticClient().clear();
142 CI.getDiagnostics().Reset();
144 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
145 PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(),
146 RewrittenFiles.begin(), RewrittenFiles.end());
147 PPOpts.RemappedFilesKeepOriginalName = false;
152 #ifdef CLANG_ENABLE_OBJC_REWRITER
154 std::unique_ptr<ASTConsumer>
155 RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
156 if (std::unique_ptr<raw_ostream> OS =
157 CI.createDefaultOutputFile(false, InFile, "cpp")) {
158 if (CI.getLangOpts().ObjCRuntime.isNonFragile())
159 return CreateModernObjCRewriter(
160 InFile, std::move(OS), CI.getDiagnostics(), CI.getLangOpts(),
161 CI.getDiagnosticOpts().NoRewriteMacros,
162 (CI.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo));
163 return CreateObjCRewriter(InFile, std::move(OS), CI.getDiagnostics(),
165 CI.getDiagnosticOpts().NoRewriteMacros);
172 //===----------------------------------------------------------------------===//
173 // Preprocessor Actions
174 //===----------------------------------------------------------------------===//
176 void RewriteMacrosAction::ExecuteAction() {
177 CompilerInstance &CI = getCompilerInstance();
178 std::unique_ptr<raw_ostream> OS =
179 CI.createDefaultOutputFile(true, getCurrentFile());
182 RewriteMacrosInInput(CI.getPreprocessor(), OS.get());
185 void RewriteTestAction::ExecuteAction() {
186 CompilerInstance &CI = getCompilerInstance();
187 std::unique_ptr<raw_ostream> OS =
188 CI.createDefaultOutputFile(false, getCurrentFile());
191 DoRewriteTest(CI.getPreprocessor(), OS.get());
194 void RewriteIncludesAction::ExecuteAction() {
195 CompilerInstance &CI = getCompilerInstance();
196 std::unique_ptr<raw_ostream> OS =
197 CI.createDefaultOutputFile(true, getCurrentFile());
200 RewriteIncludesInInput(CI.getPreprocessor(), OS.get(),
201 CI.getPreprocessorOutputOpts());