blob: 51029665e47e3276982b146a5fa21858a1f88259 [file] [log] [blame]
Douglas Gregor558cb562009-04-02 01:08:08 +00001//===--- FixItRewriter.cpp - Fix-It Rewriter Diagnostic Client --*- C++ -*-===//
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// This is a diagnostic client adaptor that performs rewrites as
11// suggested by code modification hints attached to diagnostics. It
12// then forwards any diagnostics to the adapted diagnostic client.
13//
14//===----------------------------------------------------------------------===//
Douglas Gregor837a4062009-04-02 16:34:42 +000015
Daniel Dunbar9b414d32010-06-15 17:48:49 +000016#include "clang/Rewrite/FixItRewriter.h"
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000017#include "clang/Basic/FileManager.h"
18#include "clang/Basic/SourceLocation.h"
Douglas Gregor837a4062009-04-02 16:34:42 +000019#include "clang/Basic/SourceManager.h"
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000020#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregor558cb562009-04-02 01:08:08 +000021#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000022#include "llvm/Support/Path.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000023#include "llvm/ADT/OwningPtr.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000024#include <cstdio>
25
Douglas Gregor558cb562009-04-02 01:08:08 +000026using namespace clang;
27
David Blaikied6471f72011-09-25 23:23:43 +000028FixItRewriter::FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000029 const LangOptions &LangOpts,
Nick Lewycky1450f262010-08-13 17:31:00 +000030 FixItOptions *FixItOpts)
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000031 : Diags(Diags),
32 Rewrite(SourceMgr, LangOpts),
Nick Lewycky1450f262010-08-13 17:31:00 +000033 FixItOpts(FixItOpts),
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +000034 NumFailures(0),
35 PrevDiagSilenced(false) {
36 OwnsClient = Diags.ownsClient();
Douglas Gregorbdbb0042010-08-18 22:29:43 +000037 Client = Diags.takeClient();
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000038 Diags.setClient(this);
Douglas Gregor558cb562009-04-02 01:08:08 +000039}
40
41FixItRewriter::~FixItRewriter() {
Douglas Gregorbdbb0042010-08-18 22:29:43 +000042 Diags.takeClient();
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +000043 Diags.setClient(Client, OwnsClient);
Douglas Gregor558cb562009-04-02 01:08:08 +000044}
45
Chris Lattner5f9e2722011-07-23 10:55:15 +000046bool FixItRewriter::WriteFixedFile(FileID ID, raw_ostream &OS) {
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000047 const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID);
48 if (!RewriteBuf) return true;
Nick Lewycky0ade8082010-04-16 18:49:45 +000049 RewriteBuf->write(OS);
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000050 OS.flush();
51 return false;
52}
53
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +000054bool FixItRewriter::WriteFixedFiles(
55 std::vector<std::pair<std::string, std::string> > *RewrittenFiles) {
Nick Lewycky96872c42010-08-15 16:47:39 +000056 if (NumFailures > 0 && !FixItOpts->FixWhatYouCan) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000057 Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
Douglas Gregor558cb562009-04-02 01:08:08 +000058 return true;
59 }
60
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000061 for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
62 const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first);
Nick Lewycky96872c42010-08-15 16:47:39 +000063 std::string Filename = FixItOpts->RewriteFilename(Entry->getName());
Douglas Gregor558cb562009-04-02 01:08:08 +000064 std::string Err;
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000065 llvm::raw_fd_ostream OS(Filename.c_str(), Err,
66 llvm::raw_fd_ostream::F_Binary);
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000067 if (!Err.empty()) {
68 Diags.Report(clang::diag::err_fe_unable_to_open_output)
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000069 << Filename << Err;
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000070 continue;
71 }
72 RewriteBuffer &RewriteBuf = I->second;
Nick Lewycky89fe0192010-04-24 22:31:36 +000073 RewriteBuf.write(OS);
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000074 OS.flush();
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +000075
76 if (RewrittenFiles)
77 RewrittenFiles->push_back(std::make_pair(Entry->getName(), Filename));
Mike Stump1eb44332009-09-09 15:08:12 +000078 }
Douglas Gregor558cb562009-04-02 01:08:08 +000079
Douglas Gregor558cb562009-04-02 01:08:08 +000080 return false;
81}
82
83bool FixItRewriter::IncludeInDiagnosticCounts() const {
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000084 return Client ? Client->IncludeInDiagnosticCounts() : true;
Douglas Gregor558cb562009-04-02 01:08:08 +000085}
86
David Blaikied6471f72011-09-25 23:23:43 +000087void FixItRewriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikie40847cf2011-09-26 01:18:08 +000088 const Diagnostic &Info) {
Argyrios Kyrtzidisdea22a32010-11-18 21:13:54 +000089 // Default implementation (Warnings/errors count).
David Blaikie78ad0b92011-09-25 23:39:51 +000090 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
Argyrios Kyrtzidisdea22a32010-11-18 21:13:54 +000091
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +000092 if (!FixItOpts->Silent ||
93 DiagLevel >= DiagnosticsEngine::Error ||
94 (DiagLevel == DiagnosticsEngine::Note && !PrevDiagSilenced) ||
95 (DiagLevel > DiagnosticsEngine::Note && Info.getNumFixItHints())) {
96 Client->HandleDiagnostic(DiagLevel, Info);
97 PrevDiagSilenced = false;
98 } else {
99 PrevDiagSilenced = true;
100 }
Douglas Gregor26df2f02009-04-02 19:05:20 +0000101
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +0000102 // Skip over any diagnostics that are ignored or notes.
David Blaikied6471f72011-09-25 23:23:43 +0000103 if (DiagLevel <= DiagnosticsEngine::Note)
Douglas Gregor26df2f02009-04-02 19:05:20 +0000104 return;
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +0000105 // Skip over errors if we are only fixing warnings.
106 if (DiagLevel >= DiagnosticsEngine::Error && FixItOpts->FixOnlyWarnings) {
107 ++NumFailures;
108 return;
109 }
Douglas Gregor26df2f02009-04-02 19:05:20 +0000110
Douglas Gregor558cb562009-04-02 01:08:08 +0000111 // Make sure that we can perform all of the modifications we
112 // in this diagnostic.
Douglas Gregor849b2432010-03-31 17:46:05 +0000113 bool CanRewrite = Info.getNumFixItHints() > 0;
114 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
Douglas Gregor837a4062009-04-02 16:34:42 +0000115 Idx < Last; ++Idx) {
Douglas Gregor849b2432010-03-31 17:46:05 +0000116 const FixItHint &Hint = Info.getFixItHint(Idx);
Douglas Gregor558cb562009-04-02 01:08:08 +0000117 if (Hint.RemoveRange.isValid() &&
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000118 Rewrite.getRangeSize(Hint.RemoveRange) == -1) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000119 CanRewrite = false;
120 break;
121 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000122 }
123
Mike Stump1eb44332009-09-09 15:08:12 +0000124 if (!CanRewrite) {
Douglas Gregor849b2432010-03-31 17:46:05 +0000125 if (Info.getNumFixItHints() > 0)
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000126 Diag(Info.getLocation(), diag::note_fixit_in_macro);
Douglas Gregor837a4062009-04-02 16:34:42 +0000127
128 // If this was an error, refuse to perform any rewriting.
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +0000129 if (DiagLevel >= DiagnosticsEngine::Error) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000130 if (++NumFailures == 1)
131 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
Douglas Gregor837a4062009-04-02 16:34:42 +0000132 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000133 return;
Douglas Gregor837a4062009-04-02 16:34:42 +0000134 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000135
136 bool Failed = false;
Douglas Gregor849b2432010-03-31 17:46:05 +0000137 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
Douglas Gregor837a4062009-04-02 16:34:42 +0000138 Idx < Last; ++Idx) {
Douglas Gregor849b2432010-03-31 17:46:05 +0000139 const FixItHint &Hint = Info.getFixItHint(Idx);
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Douglas Gregor837a4062009-04-02 16:34:42 +0000141 if (Hint.CodeToInsert.empty()) {
142 // We're removing code.
John McCallf85e1932011-06-15 23:02:42 +0000143 if (Rewrite.RemoveText(Hint.RemoveRange))
Douglas Gregor837a4062009-04-02 16:34:42 +0000144 Failed = true;
145 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000146 }
147
Douglas Gregor837a4062009-04-02 16:34:42 +0000148 // We're replacing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000149 if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(),
150 Rewrite.getRangeSize(Hint.RemoveRange),
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000151 Hint.CodeToInsert))
Douglas Gregor837a4062009-04-02 16:34:42 +0000152 Failed = true;
Douglas Gregor558cb562009-04-02 01:08:08 +0000153 }
154
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000155 if (Failed) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000156 ++NumFailures;
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000157 Diag(Info.getLocation(), diag::note_fixit_failed);
158 return;
159 }
160
161 Diag(Info.getLocation(), diag::note_fixit_applied);
162}
163
164/// \brief Emit a diagnostic via the adapted diagnostic client.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000165void FixItRewriter::Diag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000166 // When producing this diagnostic, we temporarily bypass ourselves,
167 // clear out any current diagnostic, and let the downstream client
168 // format the diagnostic.
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000169 Diags.takeClient();
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000170 Diags.setClient(Client);
171 Diags.Clear();
172 Diags.Report(Loc, DiagID);
Douglas Gregorbdbb0042010-08-18 22:29:43 +0000173 Diags.takeClient();
Mike Stump1eb44332009-09-09 15:08:12 +0000174 Diags.setClient(this);
Douglas Gregor558cb562009-04-02 01:08:08 +0000175}
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +0000176
Douglas Gregoraee526e2011-09-29 00:38:00 +0000177DiagnosticConsumer *FixItRewriter::clone(DiagnosticsEngine &Diags) const {
178 return new FixItRewriter(Diags, Diags.getSourceManager(),
179 Rewrite.getLangOpts(), FixItOpts);
180}
181
Nick Lewycky1450f262010-08-13 17:31:00 +0000182FixItOptions::~FixItOptions() {}