blob: b726d7604138fce4b92b2e0eb06275859ff79b68 [file] [log] [blame]
Daniel Dunbar9b414d32010-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
10#include "clang/Rewrite/FrontendActions.h"
11#include "clang/AST/ASTConsumer.h"
12#include "clang/Lex/Preprocessor.h"
13#include "clang/Parse/Parser.h"
14#include "clang/Basic/FileManager.h"
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +000015#include "clang/Frontend/FrontendActions.h"
Daniel Dunbar9b414d32010-06-15 17:48:49 +000016#include "clang/Frontend/CompilerInstance.h"
17#include "clang/Frontend/FrontendDiagnostic.h"
18#include "clang/Frontend/Utils.h"
19#include "clang/Rewrite/ASTConsumers.h"
20#include "clang/Rewrite/FixItRewriter.h"
21#include "clang/Rewrite/Rewriters.h"
22#include "llvm/ADT/OwningPtr.h"
23#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000024#include "llvm/Support/Path.h"
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +000025#include "llvm/Support/FileSystem.h"
NAKAMURA Takumi02770392012-01-26 03:47:18 +000026
27// FIXME: This is terrible, we need this for ::close.
28#if !defined(_MSC_VER)
Douglas Gregor24466d82012-01-26 03:20:14 +000029#include <unistd.h>
NAKAMURA Takumi02770392012-01-26 03:47:18 +000030#else
31#include <io.h>
32#endif
Douglas Gregor24466d82012-01-26 03:20:14 +000033
Daniel Dunbar9b414d32010-06-15 17:48:49 +000034using namespace clang;
35
36//===----------------------------------------------------------------------===//
37// AST Consumer Actions
38//===----------------------------------------------------------------------===//
39
40ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000041 StringRef InFile) {
42 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
Daniel Dunbar9b414d32010-06-15 17:48:49 +000043 return CreateHTMLPrinter(OS, CI.getPreprocessor());
44 return 0;
45}
46
47FixItAction::FixItAction() {}
48FixItAction::~FixItAction() {}
49
50ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000051 StringRef InFile) {
Daniel Dunbar9b414d32010-06-15 17:48:49 +000052 return new ASTConsumer();
53}
54
Benjamin Kramer79ba2a62010-10-22 16:48:22 +000055namespace {
Nick Lewycky96872c42010-08-15 16:47:39 +000056class FixItRewriteInPlace : public FixItOptions {
57public:
58 std::string RewriteFilename(const std::string &Filename) { return Filename; }
59};
60
Nick Lewycky1450f262010-08-13 17:31:00 +000061class FixItActionSuffixInserter : public FixItOptions {
Daniel Dunbar9b414d32010-06-15 17:48:49 +000062 std::string NewSuffix;
63
64public:
Nick Lewycky1450f262010-08-13 17:31:00 +000065 FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
66 : NewSuffix(NewSuffix) {
67 this->FixWhatYouCan = FixWhatYouCan;
68 }
Daniel Dunbar9b414d32010-06-15 17:48:49 +000069
70 std::string RewriteFilename(const std::string &Filename) {
Michael J. Spencerd5b08be2010-12-18 04:13:32 +000071 llvm::SmallString<128> Path(Filename);
72 llvm::sys::path::replace_extension(Path,
73 NewSuffix + llvm::sys::path::extension(Path));
74 return Path.str();
Daniel Dunbar9b414d32010-06-15 17:48:49 +000075 }
76};
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +000077
78class FixItRewriteToTemp : public FixItOptions {
79public:
80 std::string RewriteFilename(const std::string &Filename) {
81 llvm::SmallString<128> Path;
82 Path = llvm::sys::path::filename(Filename);
83 Path += "-%%%%%%%%";
84 Path += llvm::sys::path::extension(Filename);
85 int fd;
86 llvm::SmallString<128> NewPath;
87 if (llvm::sys::fs::unique_file(Path.str(), fd, NewPath)
88 == llvm::errc::success)
89 ::close(fd);
90 return NewPath.str();
91 }
92};
Benjamin Kramer79ba2a62010-10-22 16:48:22 +000093} // end anonymous namespace
Daniel Dunbar9b414d32010-06-15 17:48:49 +000094
95bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000096 StringRef Filename) {
Daniel Dunbar9b414d32010-06-15 17:48:49 +000097 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
98 if (!FEOpts.FixItSuffix.empty()) {
Nick Lewycky1450f262010-08-13 17:31:00 +000099 FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
100 FEOpts.FixWhatYouCan));
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000101 } else {
Nick Lewycky96872c42010-08-15 16:47:39 +0000102 FixItOpts.reset(new FixItRewriteInPlace);
103 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000104 }
105 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
Nick Lewycky1450f262010-08-13 17:31:00 +0000106 CI.getLangOpts(), FixItOpts.get()));
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000107 return true;
108}
109
110void FixItAction::EndSourceFileAction() {
111 // Otherwise rewrite all files.
112 Rewriter->WriteFixedFiles();
113}
114
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +0000115bool FixItRecompile::BeginInvocation(CompilerInstance &CI) {
116
117 std::vector<std::pair<std::string, std::string> > RewrittenFiles;
118 bool err = false;
119 {
120 const FrontendOptions &FEOpts = CI.getFrontendOpts();
121 llvm::OwningPtr<FrontendAction> FixAction(new SyntaxOnlyAction());
122 FixAction->BeginSourceFile(CI, FEOpts.Inputs[0]);
123
124 llvm::OwningPtr<FixItOptions> FixItOpts;
125 if (FEOpts.FixToTemporaries)
126 FixItOpts.reset(new FixItRewriteToTemp());
127 else
128 FixItOpts.reset(new FixItRewriteInPlace());
129 FixItOpts->Silent = true;
130 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
131 FixItOpts->FixOnlyWarnings = FEOpts.FixOnlyWarnings;
132 FixItRewriter Rewriter(CI.getDiagnostics(), CI.getSourceManager(),
133 CI.getLangOpts(), FixItOpts.get());
134 FixAction->Execute();
135
136 err = Rewriter.WriteFixedFiles(&RewrittenFiles);
137
138 FixAction->EndSourceFile();
139 CI.setSourceManager(0);
140 CI.setFileManager(0);
141 }
142 if (err)
143 return false;
144 CI.getDiagnosticClient().clear();
145
146 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
147 PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(),
148 RewrittenFiles.begin(), RewrittenFiles.end());
149 PPOpts.RemappedFilesKeepOriginalName = false;
150
151 return true;
152}
153
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000154//===----------------------------------------------------------------------===//
155// Preprocessor Actions
156//===----------------------------------------------------------------------===//
157
158ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000159 StringRef InFile) {
160 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp"))
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000161 return CreateObjCRewriter(InFile, OS,
162 CI.getDiagnostics(), CI.getLangOpts(),
163 CI.getDiagnosticOpts().NoRewriteMacros);
164 return 0;
165}
166
167void RewriteMacrosAction::ExecuteAction() {
168 CompilerInstance &CI = getCompilerInstance();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000169 raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000170 if (!OS) return;
171
172 RewriteMacrosInInput(CI.getPreprocessor(), OS);
173}
174
175void RewriteTestAction::ExecuteAction() {
176 CompilerInstance &CI = getCompilerInstance();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000177 raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000178 if (!OS) return;
179
180 DoRewriteTest(CI.getPreprocessor(), OS);
181}