blob: 1b5eb2855beaf0022d66c9c515d4d6e3623c5054 [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>
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) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000035 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
Daniel Dunbarc1b17292010-06-15 17:48:49 +000036 return CreateHTMLPrinter(OS, CI.getPreprocessor());
Craig Topper8ae12032014-05-07 06:21:57 +000037 return nullptr;
Daniel Dunbarc1b17292010-06-15 17:48:49 +000038}
39
40FixItAction::FixItAction() {}
41FixItAction::~FixItAction() {}
42
David Blaikie6beb6aa2014-08-10 19:56:51 +000043std::unique_ptr<ASTConsumer>
44FixItAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
45 return llvm::make_unique<ASTConsumer>();
Daniel Dunbarc1b17292010-06-15 17:48:49 +000046}
47
Benjamin Kramerfb5e5842010-10-22 16:48:22 +000048namespace {
Nick Lewycky53f10422010-08-15 16:47:39 +000049class FixItRewriteInPlace : public FixItOptions {
50public:
Craig Topperfb6b25b2014-03-15 04:29:04 +000051 std::string RewriteFilename(const std::string &Filename, int &fd) override {
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +000052 fd = -1;
53 return Filename;
54 }
Nick Lewycky53f10422010-08-15 16:47:39 +000055};
56
Nick Lewycky078a5e22010-08-13 17:31:00 +000057class FixItActionSuffixInserter : public FixItOptions {
Daniel Dunbarc1b17292010-06-15 17:48:49 +000058 std::string NewSuffix;
59
60public:
Nick Lewycky078a5e22010-08-13 17:31:00 +000061 FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
62 : NewSuffix(NewSuffix) {
63 this->FixWhatYouCan = FixWhatYouCan;
64 }
Daniel Dunbarc1b17292010-06-15 17:48:49 +000065
Craig Topperfb6b25b2014-03-15 04:29:04 +000066 std::string RewriteFilename(const std::string &Filename, int &fd) override {
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +000067 fd = -1;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000068 SmallString<128> Path(Filename);
Michael J. Spencere47230f2010-12-18 04:13:32 +000069 llvm::sys::path::replace_extension(Path,
70 NewSuffix + llvm::sys::path::extension(Path));
71 return Path.str();
Daniel Dunbarc1b17292010-06-15 17:48:49 +000072 }
73};
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000074
75class FixItRewriteToTemp : public FixItOptions {
76public:
Craig Topperfb6b25b2014-03-15 04:29:04 +000077 std::string RewriteFilename(const std::string &Filename, int &fd) override {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000078 SmallString<128> Path;
Rafael Espindolaa36e78e2013-07-05 20:00:06 +000079 llvm::sys::fs::createTemporaryFile(llvm::sys::path::filename(Filename),
80 llvm::sys::path::extension(Filename), fd,
81 Path);
82 return Path.str();
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000083 }
84};
Benjamin Kramerfb5e5842010-10-22 16:48:22 +000085} // end anonymous namespace
Daniel Dunbarc1b17292010-06-15 17:48:49 +000086
87bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000088 StringRef Filename) {
Daniel Dunbarc1b17292010-06-15 17:48:49 +000089 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
90 if (!FEOpts.FixItSuffix.empty()) {
Nick Lewycky078a5e22010-08-13 17:31:00 +000091 FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
92 FEOpts.FixWhatYouCan));
Daniel Dunbarc1b17292010-06-15 17:48:49 +000093 } else {
Nick Lewycky53f10422010-08-15 16:47:39 +000094 FixItOpts.reset(new FixItRewriteInPlace);
95 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
Daniel Dunbarc1b17292010-06-15 17:48:49 +000096 }
97 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
Nick Lewycky078a5e22010-08-13 17:31:00 +000098 CI.getLangOpts(), FixItOpts.get()));
Daniel Dunbarc1b17292010-06-15 17:48:49 +000099 return true;
100}
101
102void FixItAction::EndSourceFileAction() {
103 // Otherwise rewrite all files.
104 Rewriter->WriteFixedFiles();
105}
106
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000107bool FixItRecompile::BeginInvocation(CompilerInstance &CI) {
108
109 std::vector<std::pair<std::string, std::string> > RewrittenFiles;
110 bool err = false;
111 {
112 const FrontendOptions &FEOpts = CI.getFrontendOpts();
Ahmed Charlesb8984322014-03-07 20:03:18 +0000113 std::unique_ptr<FrontendAction> FixAction(new SyntaxOnlyAction());
Argyrios Kyrtzidisaf0bdfc2012-01-27 01:00:47 +0000114 if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000115 std::unique_ptr<FixItOptions> FixItOpts;
Argyrios Kyrtzidisaf0bdfc2012-01-27 01:00:47 +0000116 if (FEOpts.FixToTemporaries)
117 FixItOpts.reset(new FixItRewriteToTemp());
118 else
119 FixItOpts.reset(new FixItRewriteInPlace());
120 FixItOpts->Silent = true;
121 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
122 FixItOpts->FixOnlyWarnings = FEOpts.FixOnlyWarnings;
123 FixItRewriter Rewriter(CI.getDiagnostics(), CI.getSourceManager(),
124 CI.getLangOpts(), FixItOpts.get());
125 FixAction->Execute();
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000126
Argyrios Kyrtzidisaf0bdfc2012-01-27 01:00:47 +0000127 err = Rewriter.WriteFixedFiles(&RewrittenFiles);
128
129 FixAction->EndSourceFile();
Craig Topper8ae12032014-05-07 06:21:57 +0000130 CI.setSourceManager(nullptr);
131 CI.setFileManager(nullptr);
Argyrios Kyrtzidisaf0bdfc2012-01-27 01:00:47 +0000132 } else {
133 err = true;
134 }
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000135 }
136 if (err)
137 return false;
138 CI.getDiagnosticClient().clear();
Argyrios Kyrtzidis0b5ec2d2012-01-27 06:15:37 +0000139 CI.getDiagnostics().Reset();
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000140
141 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
142 PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(),
143 RewrittenFiles.begin(), RewrittenFiles.end());
144 PPOpts.RemappedFilesKeepOriginalName = false;
145
146 return true;
147}
148
Alp Toker0621cb22014-07-16 16:48:33 +0000149#ifdef CLANG_ENABLE_OBJC_REWRITER
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000150
David Blaikie6beb6aa2014-08-10 19:56:51 +0000151std::unique_ptr<ASTConsumer>
152RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Fariborz Jahanian11671902012-02-07 17:11:38 +0000153 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp")) {
John McCall5fb5df92012-06-20 06:18:46 +0000154 if (CI.getLangOpts().ObjCRuntime.isNonFragile())
Fariborz Jahanian11671902012-02-07 17:11:38 +0000155 return CreateModernObjCRewriter(InFile, OS,
156 CI.getDiagnostics(), CI.getLangOpts(),
Fariborz Jahaniane4c7e852013-02-08 00:27:34 +0000157 CI.getDiagnosticOpts().NoRewriteMacros,
158 (CI.getCodeGenOpts().getDebugInfo() !=
159 CodeGenOptions::NoDebugInfo));
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000160 return CreateObjCRewriter(InFile, OS,
161 CI.getDiagnostics(), CI.getLangOpts(),
162 CI.getDiagnosticOpts().NoRewriteMacros);
Fariborz Jahanian11671902012-02-07 17:11:38 +0000163 }
Craig Topper8ae12032014-05-07 06:21:57 +0000164 return nullptr;
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000165}
166
Alp Toker0621cb22014-07-16 16:48:33 +0000167#endif
168
169//===----------------------------------------------------------------------===//
170// Preprocessor Actions
171//===----------------------------------------------------------------------===//
172
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000173void RewriteMacrosAction::ExecuteAction() {
174 CompilerInstance &CI = getCompilerInstance();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000175 raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000176 if (!OS) return;
177
178 RewriteMacrosInInput(CI.getPreprocessor(), OS);
179}
180
181void RewriteTestAction::ExecuteAction() {
182 CompilerInstance &CI = getCompilerInstance();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000183 raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000184 if (!OS) return;
185
186 DoRewriteTest(CI.getPreprocessor(), OS);
187}
David Blaikied5321242012-06-06 18:52:13 +0000188
189void RewriteIncludesAction::ExecuteAction() {
190 CompilerInstance &CI = getCompilerInstance();
191 raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
192 if (!OS) return;
193
194 RewriteIncludesInInput(CI.getPreprocessor(), OS,
195 CI.getPreprocessorOutputOpts());
196}