blob: 24aa98c2110006a5c72a54324dffc32b7922d036 [file] [log] [blame]
Daniel Dunbarc1b17292010-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
Ted Kremenekcdf81492012-09-01 05:09:24 +000010#include "clang/Rewrite/Frontend/FrontendActions.h"
Daniel Dunbarc1b17292010-06-15 17:48:49 +000011#include "clang/AST/ASTConsumer.h"
Daniel Dunbarc1b17292010-06-15 17:48:49 +000012#include "clang/Basic/FileManager.h"
13#include "clang/Frontend/CompilerInstance.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "clang/Frontend/FrontendActions.h"
Daniel Dunbarc1b17292010-06-15 17:48:49 +000015#include "clang/Frontend/FrontendDiagnostic.h"
16#include "clang/Frontend/Utils.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/Lex/Preprocessor.h"
18#include "clang/Parse/Parser.h"
Ted Kremenekcdf81492012-09-01 05:09:24 +000019#include "clang/Rewrite/Frontend/ASTConsumers.h"
20#include "clang/Rewrite/Frontend/FixItRewriter.h"
21#include "clang/Rewrite/Frontend/Rewriters.h"
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000022#include "llvm/Support/FileSystem.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "llvm/Support/Path.h"
24#include "llvm/Support/raw_ostream.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000025#include <memory>
Benjamin Kramercfeacf52016-05-27 14:27:13 +000026#include <utility>
NAKAMURA Takumi69ee7d52012-01-26 03:47:18 +000027
Daniel Dunbarc1b17292010-06-15 17:48:49 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// AST Consumer Actions
32//===----------------------------------------------------------------------===//
33
David Blaikie6beb6aa2014-08-10 19:56:51 +000034std::unique_ptr<ASTConsumer>
35HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000036 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
Daniel Dunbarc1b17292010-06-15 17:48:49 +000037 return CreateHTMLPrinter(OS, CI.getPreprocessor());
Craig Topper8ae12032014-05-07 06:21:57 +000038 return nullptr;
Daniel Dunbarc1b17292010-06-15 17:48:49 +000039}
40
41FixItAction::FixItAction() {}
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000042FixItAction::~FixItAction() {}
Daniel Dunbarc1b17292010-06-15 17:48:49 +000043
David Blaikie6beb6aa2014-08-10 19:56:51 +000044std::unique_ptr<ASTConsumer>
45FixItAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
46 return llvm::make_unique<ASTConsumer>();
Daniel Dunbarc1b17292010-06-15 17:48:49 +000047}
48
Benjamin Kramerfb5e5842010-10-22 16:48:22 +000049namespace {
Nick Lewycky53f10422010-08-15 16:47:39 +000050class FixItRewriteInPlace : public FixItOptions {
51public:
Reid Kleckner3df5dd42015-06-17 17:47:30 +000052 FixItRewriteInPlace() { InPlace = true; }
53
Craig Topperfb6b25b2014-03-15 04:29:04 +000054 std::string RewriteFilename(const std::string &Filename, int &fd) override {
Reid Kleckner3df5dd42015-06-17 17:47:30 +000055 llvm_unreachable("don't call RewriteFilename for inplace rewrites");
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +000056 }
Nick Lewycky53f10422010-08-15 16:47:39 +000057};
58
Nick Lewycky078a5e22010-08-13 17:31:00 +000059class FixItActionSuffixInserter : public FixItOptions {
Daniel Dunbarc1b17292010-06-15 17:48:49 +000060 std::string NewSuffix;
61
62public:
Nick Lewycky078a5e22010-08-13 17:31:00 +000063 FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
Benjamin Kramercfeacf52016-05-27 14:27:13 +000064 : NewSuffix(std::move(NewSuffix)) {
65 this->FixWhatYouCan = FixWhatYouCan;
Nick Lewycky078a5e22010-08-13 17:31:00 +000066 }
Daniel Dunbarc1b17292010-06-15 17:48:49 +000067
Craig Topperfb6b25b2014-03-15 04:29:04 +000068 std::string RewriteFilename(const std::string &Filename, int &fd) override {
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +000069 fd = -1;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000070 SmallString<128> Path(Filename);
Michael J. Spencere47230f2010-12-18 04:13:32 +000071 llvm::sys::path::replace_extension(Path,
72 NewSuffix + llvm::sys::path::extension(Path));
73 return Path.str();
Daniel Dunbarc1b17292010-06-15 17:48:49 +000074 }
75};
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000076
77class FixItRewriteToTemp : public FixItOptions {
78public:
Craig Topperfb6b25b2014-03-15 04:29:04 +000079 std::string RewriteFilename(const std::string &Filename, int &fd) override {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000080 SmallString<128> Path;
Rafael Espindolaa36e78e2013-07-05 20:00:06 +000081 llvm::sys::fs::createTemporaryFile(llvm::sys::path::filename(Filename),
Argyrios Kyrtzidis203e9232015-09-09 16:48:47 +000082 llvm::sys::path::extension(Filename).drop_front(), fd,
Rafael Espindolaa36e78e2013-07-05 20:00:06 +000083 Path);
84 return Path.str();
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000085 }
86};
Benjamin Kramerfb5e5842010-10-22 16:48:22 +000087} // end anonymous namespace
Daniel Dunbarc1b17292010-06-15 17:48:49 +000088
89bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000090 StringRef Filename) {
Daniel Dunbarc1b17292010-06-15 17:48:49 +000091 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
92 if (!FEOpts.FixItSuffix.empty()) {
Nick Lewycky078a5e22010-08-13 17:31:00 +000093 FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
94 FEOpts.FixWhatYouCan));
Daniel Dunbarc1b17292010-06-15 17:48:49 +000095 } else {
Nick Lewycky53f10422010-08-15 16:47:39 +000096 FixItOpts.reset(new FixItRewriteInPlace);
97 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
Daniel Dunbarc1b17292010-06-15 17:48:49 +000098 }
99 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
Nick Lewycky078a5e22010-08-13 17:31:00 +0000100 CI.getLangOpts(), FixItOpts.get()));
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000101 return true;
102}
103
104void FixItAction::EndSourceFileAction() {
105 // Otherwise rewrite all files.
106 Rewriter->WriteFixedFiles();
107}
108
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000109bool FixItRecompile::BeginInvocation(CompilerInstance &CI) {
110
111 std::vector<std::pair<std::string, std::string> > RewrittenFiles;
112 bool err = false;
113 {
114 const FrontendOptions &FEOpts = CI.getFrontendOpts();
Ahmed Charlesb8984322014-03-07 20:03:18 +0000115 std::unique_ptr<FrontendAction> FixAction(new SyntaxOnlyAction());
Argyrios Kyrtzidisaf0bdfc2012-01-27 01:00:47 +0000116 if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000117 std::unique_ptr<FixItOptions> FixItOpts;
Argyrios Kyrtzidisaf0bdfc2012-01-27 01:00:47 +0000118 if (FEOpts.FixToTemporaries)
119 FixItOpts.reset(new FixItRewriteToTemp());
120 else
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();
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000128
Argyrios Kyrtzidisaf0bdfc2012-01-27 01:00:47 +0000129 err = Rewriter.WriteFixedFiles(&RewrittenFiles);
130
131 FixAction->EndSourceFile();
Craig Topper8ae12032014-05-07 06:21:57 +0000132 CI.setSourceManager(nullptr);
133 CI.setFileManager(nullptr);
Argyrios Kyrtzidisaf0bdfc2012-01-27 01:00:47 +0000134 } else {
135 err = true;
136 }
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000137 }
138 if (err)
139 return false;
140 CI.getDiagnosticClient().clear();
Argyrios Kyrtzidis0b5ec2d2012-01-27 06:15:37 +0000141 CI.getDiagnostics().Reset();
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000142
143 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
144 PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(),
145 RewrittenFiles.begin(), RewrittenFiles.end());
146 PPOpts.RemappedFilesKeepOriginalName = false;
147
148 return true;
149}
150
Alp Toker0621cb22014-07-16 16:48:33 +0000151#ifdef CLANG_ENABLE_OBJC_REWRITER
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000152
David Blaikie6beb6aa2014-08-10 19:56:51 +0000153std::unique_ptr<ASTConsumer>
154RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Fariborz Jahanian11671902012-02-07 17:11:38 +0000155 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp")) {
John McCall5fb5df92012-06-20 06:18:46 +0000156 if (CI.getLangOpts().ObjCRuntime.isNonFragile())
Benjamin Kramer8c305922016-02-02 11:06:51 +0000157 return CreateModernObjCRewriter(
158 InFile, OS, CI.getDiagnostics(), CI.getLangOpts(),
159 CI.getDiagnosticOpts().NoRewriteMacros,
160 (CI.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo));
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000161 return CreateObjCRewriter(InFile, OS,
162 CI.getDiagnostics(), CI.getLangOpts(),
163 CI.getDiagnosticOpts().NoRewriteMacros);
Fariborz Jahanian11671902012-02-07 17:11:38 +0000164 }
Craig Topper8ae12032014-05-07 06:21:57 +0000165 return nullptr;
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000166}
167
Alp Toker0621cb22014-07-16 16:48:33 +0000168#endif
169
170//===----------------------------------------------------------------------===//
171// Preprocessor Actions
172//===----------------------------------------------------------------------===//
173
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000174void RewriteMacrosAction::ExecuteAction() {
175 CompilerInstance &CI = getCompilerInstance();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000176 raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000177 if (!OS) return;
178
179 RewriteMacrosInInput(CI.getPreprocessor(), OS);
180}
181
182void RewriteTestAction::ExecuteAction() {
183 CompilerInstance &CI = getCompilerInstance();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000184 raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000185 if (!OS) return;
186
187 DoRewriteTest(CI.getPreprocessor(), OS);
188}
David Blaikied5321242012-06-06 18:52:13 +0000189
190void RewriteIncludesAction::ExecuteAction() {
191 CompilerInstance &CI = getCompilerInstance();
192 raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
193 if (!OS) return;
194
195 RewriteIncludesInInput(CI.getPreprocessor(), OS,
196 CI.getPreprocessorOutputOpts());
197}