blob: 76eb07e981dc0fc82a8288e5a9be6d169aa0cb5c [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/Frontend/CompilerInstance.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000013#include "clang/Frontend/FrontendActions.h"
Daniel Dunbarc1b17292010-06-15 17:48:49 +000014#include "clang/Frontend/FrontendDiagnostic.h"
15#include "clang/Frontend/Utils.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/Lex/Preprocessor.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000017#include "clang/Lex/PreprocessorOptions.h"
Ted Kremenekcdf81492012-09-01 05:09:24 +000018#include "clang/Rewrite/Frontend/ASTConsumers.h"
19#include "clang/Rewrite/Frontend/FixItRewriter.h"
20#include "clang/Rewrite/Frontend/Rewriters.h"
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000021#include "llvm/Support/FileSystem.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "llvm/Support/Path.h"
23#include "llvm/Support/raw_ostream.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000024#include <memory>
Benjamin Kramercfeacf52016-05-27 14:27:13 +000025#include <utility>
NAKAMURA Takumi69ee7d52012-01-26 03:47:18 +000026
Daniel Dunbarc1b17292010-06-15 17:48:49 +000027using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// AST Consumer Actions
31//===----------------------------------------------------------------------===//
32
David Blaikie6beb6aa2014-08-10 19:56:51 +000033std::unique_ptr<ASTConsumer>
34HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Peter Collingbourne03f89072016-07-15 00:55:40 +000035 if (std::unique_ptr<raw_ostream> OS =
36 CI.createDefaultOutputFile(false, InFile))
37 return CreateHTMLPrinter(std::move(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
Richard Smithd9259c22017-06-09 01:36:10 +000089bool FixItAction::BeginSourceFileAction(CompilerInstance &CI) {
Daniel Dunbarc1b17292010-06-15 17:48:49 +000090 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
91 if (!FEOpts.FixItSuffix.empty()) {
Nick Lewycky078a5e22010-08-13 17:31:00 +000092 FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
93 FEOpts.FixWhatYouCan));
Daniel Dunbarc1b17292010-06-15 17:48:49 +000094 } else {
Nick Lewycky53f10422010-08-15 16:47:39 +000095 FixItOpts.reset(new FixItRewriteInPlace);
96 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
Daniel Dunbarc1b17292010-06-15 17:48:49 +000097 }
98 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
Nick Lewycky078a5e22010-08-13 17:31:00 +000099 CI.getLangOpts(), FixItOpts.get()));
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000100 return true;
101}
102
103void FixItAction::EndSourceFileAction() {
104 // Otherwise rewrite all files.
105 Rewriter->WriteFixedFiles();
106}
107
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000108bool FixItRecompile::BeginInvocation(CompilerInstance &CI) {
109
110 std::vector<std::pair<std::string, std::string> > RewrittenFiles;
111 bool err = false;
112 {
113 const FrontendOptions &FEOpts = CI.getFrontendOpts();
Ahmed Charlesb8984322014-03-07 20:03:18 +0000114 std::unique_ptr<FrontendAction> FixAction(new SyntaxOnlyAction());
Argyrios Kyrtzidisaf0bdfc2012-01-27 01:00:47 +0000115 if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000116 std::unique_ptr<FixItOptions> FixItOpts;
Argyrios Kyrtzidisaf0bdfc2012-01-27 01:00:47 +0000117 if (FEOpts.FixToTemporaries)
118 FixItOpts.reset(new FixItRewriteToTemp());
119 else
120 FixItOpts.reset(new FixItRewriteInPlace());
121 FixItOpts->Silent = true;
122 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
123 FixItOpts->FixOnlyWarnings = FEOpts.FixOnlyWarnings;
124 FixItRewriter Rewriter(CI.getDiagnostics(), CI.getSourceManager(),
125 CI.getLangOpts(), FixItOpts.get());
126 FixAction->Execute();
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000127
Argyrios Kyrtzidisaf0bdfc2012-01-27 01:00:47 +0000128 err = Rewriter.WriteFixedFiles(&RewrittenFiles);
129
130 FixAction->EndSourceFile();
Craig Topper8ae12032014-05-07 06:21:57 +0000131 CI.setSourceManager(nullptr);
132 CI.setFileManager(nullptr);
Argyrios Kyrtzidisaf0bdfc2012-01-27 01:00:47 +0000133 } else {
134 err = true;
135 }
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000136 }
137 if (err)
138 return false;
139 CI.getDiagnosticClient().clear();
Argyrios Kyrtzidis0b5ec2d2012-01-27 06:15:37 +0000140 CI.getDiagnostics().Reset();
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000141
142 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
143 PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(),
144 RewrittenFiles.begin(), RewrittenFiles.end());
145 PPOpts.RemappedFilesKeepOriginalName = false;
146
147 return true;
148}
149
Alp Toker0621cb22014-07-16 16:48:33 +0000150#ifdef CLANG_ENABLE_OBJC_REWRITER
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000151
David Blaikie6beb6aa2014-08-10 19:56:51 +0000152std::unique_ptr<ASTConsumer>
153RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Peter Collingbourne03f89072016-07-15 00:55:40 +0000154 if (std::unique_ptr<raw_ostream> OS =
155 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(
Peter Collingbourne03f89072016-07-15 00:55:40 +0000158 InFile, std::move(OS), CI.getDiagnostics(), CI.getLangOpts(),
Benjamin Kramer8c305922016-02-02 11:06:51 +0000159 CI.getDiagnosticOpts().NoRewriteMacros,
160 (CI.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo));
Peter Collingbourne03f89072016-07-15 00:55:40 +0000161 return CreateObjCRewriter(InFile, std::move(OS), CI.getDiagnostics(),
162 CI.getLangOpts(),
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000163 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();
Peter Collingbourne03f89072016-07-15 00:55:40 +0000176 std::unique_ptr<raw_ostream> OS =
177 CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000178 if (!OS) return;
179
Peter Collingbourne03f89072016-07-15 00:55:40 +0000180 RewriteMacrosInInput(CI.getPreprocessor(), OS.get());
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000181}
182
183void RewriteTestAction::ExecuteAction() {
184 CompilerInstance &CI = getCompilerInstance();
Peter Collingbourne03f89072016-07-15 00:55:40 +0000185 std::unique_ptr<raw_ostream> OS =
186 CI.createDefaultOutputFile(false, getCurrentFile());
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000187 if (!OS) return;
188
Peter Collingbourne03f89072016-07-15 00:55:40 +0000189 DoRewriteTest(CI.getPreprocessor(), OS.get());
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000190}
David Blaikied5321242012-06-06 18:52:13 +0000191
192void RewriteIncludesAction::ExecuteAction() {
193 CompilerInstance &CI = getCompilerInstance();
Peter Collingbourne03f89072016-07-15 00:55:40 +0000194 std::unique_ptr<raw_ostream> OS =
195 CI.createDefaultOutputFile(true, getCurrentFile());
David Blaikied5321242012-06-06 18:52:13 +0000196 if (!OS) return;
197
Richard Smith8128f332017-05-05 22:18:51 +0000198 // If we're preprocessing a module map, start by dumping the contents of the
199 // module itself before switching to the input buffer.
200 auto &Input = getCurrentInput();
201 if (Input.getKind().getFormat() == InputKind::ModuleMap) {
Richard Smithc784e962017-06-01 20:10:35 +0000202 if (Input.isFile()) {
203 (*OS) << "# 1 \"";
204 OS->write_escaped(Input.getFile());
205 (*OS) << "\"\n";
206 }
Richard Smith8128f332017-05-05 22:18:51 +0000207 // FIXME: Include additional information here so that we don't need the
208 // original source files to exist on disk.
209 getCurrentModule()->print(*OS);
210 (*OS) << "#pragma clang module contents\n";
211 }
212
Peter Collingbourne03f89072016-07-15 00:55:40 +0000213 RewriteIncludesInInput(CI.getPreprocessor(), OS.get(),
David Blaikied5321242012-06-06 18:52:13 +0000214 CI.getPreprocessorOutputOpts());
215}