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/Frontend/CompilerInstance.h"
13 #include "clang/Frontend/FrontendActions.h"
14 #include "clang/Frontend/FrontendDiagnostic.h"
15 #include "clang/Frontend/Utils.h"
16 #include "clang/Lex/Preprocessor.h"
17 #include "clang/Lex/PreprocessorOptions.h"
18 #include "clang/Rewrite/Frontend/ASTConsumers.h"
19 #include "clang/Rewrite/Frontend/FixItRewriter.h"
20 #include "clang/Rewrite/Frontend/Rewriters.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/Path.h"
23 #include "llvm/Support/raw_ostream.h"
27 using namespace clang;
29 //===----------------------------------------------------------------------===//
30 // AST Consumer Actions
31 //===----------------------------------------------------------------------===//
33 std::unique_ptr<ASTConsumer>
34 HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
35 if (std::unique_ptr<raw_ostream> OS =
36 CI.createDefaultOutputFile(false, InFile))
37 return CreateHTMLPrinter(std::move(OS), CI.getPreprocessor());
41 FixItAction::FixItAction() {}
42 FixItAction::~FixItAction() {}
44 std::unique_ptr<ASTConsumer>
45 FixItAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
46 return llvm::make_unique<ASTConsumer>();
50 class FixItRewriteInPlace : public FixItOptions {
52 FixItRewriteInPlace() { InPlace = true; }
54 std::string RewriteFilename(const std::string &Filename, int &fd) override {
55 llvm_unreachable("don't call RewriteFilename for inplace rewrites");
59 class FixItActionSuffixInserter : public FixItOptions {
60 std::string NewSuffix;
63 FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
64 : NewSuffix(std::move(NewSuffix)) {
65 this->FixWhatYouCan = FixWhatYouCan;
68 std::string RewriteFilename(const std::string &Filename, int &fd) override {
70 SmallString<128> Path(Filename);
71 llvm::sys::path::replace_extension(Path,
72 NewSuffix + llvm::sys::path::extension(Path));
77 class FixItRewriteToTemp : public FixItOptions {
79 std::string RewriteFilename(const std::string &Filename, int &fd) override {
80 SmallString<128> Path;
81 llvm::sys::fs::createTemporaryFile(llvm::sys::path::filename(Filename),
82 llvm::sys::path::extension(Filename).drop_front(), fd,
87 } // end anonymous namespace
89 bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
91 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
92 if (!FEOpts.FixItSuffix.empty()) {
93 FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
94 FEOpts.FixWhatYouCan));
96 FixItOpts.reset(new FixItRewriteInPlace);
97 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
99 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
100 CI.getLangOpts(), FixItOpts.get()));
104 void FixItAction::EndSourceFileAction() {
105 // Otherwise rewrite all files.
106 Rewriter->WriteFixedFiles();
109 bool FixItRecompile::BeginInvocation(CompilerInstance &CI) {
111 std::vector<std::pair<std::string, std::string> > RewrittenFiles;
114 const FrontendOptions &FEOpts = CI.getFrontendOpts();
115 std::unique_ptr<FrontendAction> FixAction(new SyntaxOnlyAction());
116 if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) {
117 std::unique_ptr<FixItOptions> FixItOpts;
118 if (FEOpts.FixToTemporaries)
119 FixItOpts.reset(new FixItRewriteToTemp());
121 FixItOpts.reset(new FixItRewriteInPlace());
122 FixItOpts->Silent = true;
123 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
124 FixItOpts->FixOnlyWarnings = FEOpts.FixOnlyWarnings;
125 FixItRewriter Rewriter(CI.getDiagnostics(), CI.getSourceManager(),
126 CI.getLangOpts(), FixItOpts.get());
127 FixAction->Execute();
129 err = Rewriter.WriteFixedFiles(&RewrittenFiles);
131 FixAction->EndSourceFile();
132 CI.setSourceManager(nullptr);
133 CI.setFileManager(nullptr);
140 CI.getDiagnosticClient().clear();
141 CI.getDiagnostics().Reset();
143 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
144 PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(),
145 RewrittenFiles.begin(), RewrittenFiles.end());
146 PPOpts.RemappedFilesKeepOriginalName = false;
151 #ifdef CLANG_ENABLE_OBJC_REWRITER
153 std::unique_ptr<ASTConsumer>
154 RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
155 if (std::unique_ptr<raw_ostream> OS =
156 CI.createDefaultOutputFile(false, InFile, "cpp")) {
157 if (CI.getLangOpts().ObjCRuntime.isNonFragile())
158 return CreateModernObjCRewriter(
159 InFile, std::move(OS), CI.getDiagnostics(), CI.getLangOpts(),
160 CI.getDiagnosticOpts().NoRewriteMacros,
161 (CI.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo));
162 return CreateObjCRewriter(InFile, std::move(OS), CI.getDiagnostics(),
164 CI.getDiagnosticOpts().NoRewriteMacros);
171 //===----------------------------------------------------------------------===//
172 // Preprocessor Actions
173 //===----------------------------------------------------------------------===//
175 void RewriteMacrosAction::ExecuteAction() {
176 CompilerInstance &CI = getCompilerInstance();
177 std::unique_ptr<raw_ostream> OS =
178 CI.createDefaultOutputFile(true, getCurrentFile());
181 RewriteMacrosInInput(CI.getPreprocessor(), OS.get());
184 void RewriteTestAction::ExecuteAction() {
185 CompilerInstance &CI = getCompilerInstance();
186 std::unique_ptr<raw_ostream> OS =
187 CI.createDefaultOutputFile(false, getCurrentFile());
190 DoRewriteTest(CI.getPreprocessor(), OS.get());
193 void RewriteIncludesAction::ExecuteAction() {
194 CompilerInstance &CI = getCompilerInstance();
195 std::unique_ptr<raw_ostream> OS =
196 CI.createDefaultOutputFile(true, getCurrentFile());
199 RewriteIncludesInInput(CI.getPreprocessor(), OS.get(),
200 CI.getPreprocessorOutputOpts());