blob: 66affb080c5c142ef1cd3feb61623544df653dfa [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"
Douglas Gregor24466d82012-01-26 03:20:14 +000026#include <unistd.h>
27
Daniel Dunbar9b414d32010-06-15 17:48:49 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// AST Consumer Actions
32//===----------------------------------------------------------------------===//
33
34ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000035 StringRef InFile) {
36 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
Daniel Dunbar9b414d32010-06-15 17:48:49 +000037 return CreateHTMLPrinter(OS, CI.getPreprocessor());
38 return 0;
39}
40
41FixItAction::FixItAction() {}
42FixItAction::~FixItAction() {}
43
44ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000045 StringRef InFile) {
Daniel Dunbar9b414d32010-06-15 17:48:49 +000046 return new ASTConsumer();
47}
48
Benjamin Kramer79ba2a62010-10-22 16:48:22 +000049namespace {
Nick Lewycky96872c42010-08-15 16:47:39 +000050class FixItRewriteInPlace : public FixItOptions {
51public:
52 std::string RewriteFilename(const std::string &Filename) { return Filename; }
53};
54
Nick Lewycky1450f262010-08-13 17:31:00 +000055class FixItActionSuffixInserter : public FixItOptions {
Daniel Dunbar9b414d32010-06-15 17:48:49 +000056 std::string NewSuffix;
57
58public:
Nick Lewycky1450f262010-08-13 17:31:00 +000059 FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
60 : NewSuffix(NewSuffix) {
61 this->FixWhatYouCan = FixWhatYouCan;
62 }
Daniel Dunbar9b414d32010-06-15 17:48:49 +000063
64 std::string RewriteFilename(const std::string &Filename) {
Michael J. Spencerd5b08be2010-12-18 04:13:32 +000065 llvm::SmallString<128> Path(Filename);
66 llvm::sys::path::replace_extension(Path,
67 NewSuffix + llvm::sys::path::extension(Path));
68 return Path.str();
Daniel Dunbar9b414d32010-06-15 17:48:49 +000069 }
70};
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +000071
72class FixItRewriteToTemp : public FixItOptions {
73public:
74 std::string RewriteFilename(const std::string &Filename) {
75 llvm::SmallString<128> Path;
76 Path = llvm::sys::path::filename(Filename);
77 Path += "-%%%%%%%%";
78 Path += llvm::sys::path::extension(Filename);
79 int fd;
80 llvm::SmallString<128> NewPath;
81 if (llvm::sys::fs::unique_file(Path.str(), fd, NewPath)
82 == llvm::errc::success)
83 ::close(fd);
84 return NewPath.str();
85 }
86};
Benjamin Kramer79ba2a62010-10-22 16:48:22 +000087} // end anonymous namespace
Daniel Dunbar9b414d32010-06-15 17:48:49 +000088
89bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000090 StringRef Filename) {
Daniel Dunbar9b414d32010-06-15 17:48:49 +000091 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
92 if (!FEOpts.FixItSuffix.empty()) {
Nick Lewycky1450f262010-08-13 17:31:00 +000093 FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
94 FEOpts.FixWhatYouCan));
Daniel Dunbar9b414d32010-06-15 17:48:49 +000095 } else {
Nick Lewycky96872c42010-08-15 16:47:39 +000096 FixItOpts.reset(new FixItRewriteInPlace);
97 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
Daniel Dunbar9b414d32010-06-15 17:48:49 +000098 }
99 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
Nick Lewycky1450f262010-08-13 17:31:00 +0000100 CI.getLangOpts(), FixItOpts.get()));
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000101 return true;
102}
103
104void FixItAction::EndSourceFileAction() {
105 // Otherwise rewrite all files.
106 Rewriter->WriteFixedFiles();
107}
108
Argyrios Kyrtzidis61d679a2012-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();
115 llvm::OwningPtr<FrontendAction> FixAction(new SyntaxOnlyAction());
116 FixAction->BeginSourceFile(CI, FEOpts.Inputs[0]);
117
118 llvm::OwningPtr<FixItOptions> FixItOpts;
119 if (FEOpts.FixToTemporaries)
120 FixItOpts.reset(new FixItRewriteToTemp());
121 else
122 FixItOpts.reset(new FixItRewriteInPlace());
123 FixItOpts->Silent = true;
124 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
125 FixItOpts->FixOnlyWarnings = FEOpts.FixOnlyWarnings;
126 FixItRewriter Rewriter(CI.getDiagnostics(), CI.getSourceManager(),
127 CI.getLangOpts(), FixItOpts.get());
128 FixAction->Execute();
129
130 err = Rewriter.WriteFixedFiles(&RewrittenFiles);
131
132 FixAction->EndSourceFile();
133 CI.setSourceManager(0);
134 CI.setFileManager(0);
135 }
136 if (err)
137 return false;
138 CI.getDiagnosticClient().clear();
139
140 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
141 PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(),
142 RewrittenFiles.begin(), RewrittenFiles.end());
143 PPOpts.RemappedFilesKeepOriginalName = false;
144
145 return true;
146}
147
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000148//===----------------------------------------------------------------------===//
149// Preprocessor Actions
150//===----------------------------------------------------------------------===//
151
152ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000153 StringRef InFile) {
154 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp"))
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000155 return CreateObjCRewriter(InFile, OS,
156 CI.getDiagnostics(), CI.getLangOpts(),
157 CI.getDiagnosticOpts().NoRewriteMacros);
158 return 0;
159}
160
161void RewriteMacrosAction::ExecuteAction() {
162 CompilerInstance &CI = getCompilerInstance();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000163 raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000164 if (!OS) return;
165
166 RewriteMacrosInInput(CI.getPreprocessor(), OS);
167}
168
169void RewriteTestAction::ExecuteAction() {
170 CompilerInstance &CI = getCompilerInstance();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000171 raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000172 if (!OS) return;
173
174 DoRewriteTest(CI.getPreprocessor(), OS);
175}