blob: 8c5eb161b5ab1f4691df6bffb4a04ab9961510b6 [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
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) {
Peter Collingbourne03f89072016-07-15 00:55:40 +0000155 if (std::unique_ptr<raw_ostream> OS =
156 CI.createDefaultOutputFile(false, InFile, "cpp")) {
John McCall5fb5df92012-06-20 06:18:46 +0000157 if (CI.getLangOpts().ObjCRuntime.isNonFragile())
Benjamin Kramer8c305922016-02-02 11:06:51 +0000158 return CreateModernObjCRewriter(
Peter Collingbourne03f89072016-07-15 00:55:40 +0000159 InFile, std::move(OS), CI.getDiagnostics(), CI.getLangOpts(),
Benjamin Kramer8c305922016-02-02 11:06:51 +0000160 CI.getDiagnosticOpts().NoRewriteMacros,
161 (CI.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo));
Peter Collingbourne03f89072016-07-15 00:55:40 +0000162 return CreateObjCRewriter(InFile, std::move(OS), CI.getDiagnostics(),
163 CI.getLangOpts(),
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000164 CI.getDiagnosticOpts().NoRewriteMacros);
Fariborz Jahanian11671902012-02-07 17:11:38 +0000165 }
Craig Topper8ae12032014-05-07 06:21:57 +0000166 return nullptr;
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000167}
168
Alp Toker0621cb22014-07-16 16:48:33 +0000169#endif
170
171//===----------------------------------------------------------------------===//
172// Preprocessor Actions
173//===----------------------------------------------------------------------===//
174
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000175void RewriteMacrosAction::ExecuteAction() {
176 CompilerInstance &CI = getCompilerInstance();
Peter Collingbourne03f89072016-07-15 00:55:40 +0000177 std::unique_ptr<raw_ostream> OS =
178 CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000179 if (!OS) return;
180
Peter Collingbourne03f89072016-07-15 00:55:40 +0000181 RewriteMacrosInInput(CI.getPreprocessor(), OS.get());
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000182}
183
184void RewriteTestAction::ExecuteAction() {
185 CompilerInstance &CI = getCompilerInstance();
Peter Collingbourne03f89072016-07-15 00:55:40 +0000186 std::unique_ptr<raw_ostream> OS =
187 CI.createDefaultOutputFile(false, getCurrentFile());
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000188 if (!OS) return;
189
Peter Collingbourne03f89072016-07-15 00:55:40 +0000190 DoRewriteTest(CI.getPreprocessor(), OS.get());
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000191}
David Blaikied5321242012-06-06 18:52:13 +0000192
193void RewriteIncludesAction::ExecuteAction() {
194 CompilerInstance &CI = getCompilerInstance();
Peter Collingbourne03f89072016-07-15 00:55:40 +0000195 std::unique_ptr<raw_ostream> OS =
196 CI.createDefaultOutputFile(true, getCurrentFile());
David Blaikied5321242012-06-06 18:52:13 +0000197 if (!OS) return;
198
Richard Smith8128f332017-05-05 22:18:51 +0000199 // If we're preprocessing a module map, start by dumping the contents of the
200 // module itself before switching to the input buffer.
201 auto &Input = getCurrentInput();
202 if (Input.getKind().getFormat() == InputKind::ModuleMap) {
203 if (Input.isFile())
204 (*OS) << "# 1 \"" << Input.getFile() << "\"\n";
205 // FIXME: Include additional information here so that we don't need the
206 // original source files to exist on disk.
207 getCurrentModule()->print(*OS);
208 (*OS) << "#pragma clang module contents\n";
209 }
210
Peter Collingbourne03f89072016-07-15 00:55:40 +0000211 RewriteIncludesInInput(CI.getPreprocessor(), OS.get(),
David Blaikied5321242012-06-06 18:52:13 +0000212 CI.getPreprocessorOutputOpts());
213}