blob: caa7d06ac2e54b2aca4b62c14ae68addf41d611a [file] [log] [blame]
Douglas Gregor578dae52009-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 Gregor068913e2009-04-02 16:34:42 +000015
Daniel Dunbarc1b17292010-06-15 17:48:49 +000016#include "clang/Rewrite/FixItRewriter.h"
Nick Lewyckya1e20de2010-04-15 06:46:58 +000017#include "clang/Basic/FileManager.h"
18#include "clang/Basic/SourceLocation.h"
Douglas Gregor068913e2009-04-02 16:34:42 +000019#include "clang/Basic/SourceManager.h"
Douglas Gregora42bd842009-04-02 17:13:00 +000020#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregor578dae52009-04-02 01:08:08 +000021#include "llvm/Support/raw_ostream.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000022#include "llvm/Support/Path.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000023#include "llvm/ADT/OwningPtr.h"
Torok Edwindb714922009-08-24 13:25:12 +000024#include <cstdio>
25
Douglas Gregor578dae52009-04-02 01:08:08 +000026using namespace clang;
27
David Blaikie9c902b52011-09-25 23:23:43 +000028FixItRewriter::FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
Nick Lewycky784fad72010-04-24 01:30:46 +000029 const LangOptions &LangOpts,
Nick Lewycky078a5e22010-08-13 17:31:00 +000030 FixItOptions *FixItOpts)
Nick Lewycky784fad72010-04-24 01:30:46 +000031 : Diags(Diags),
32 Rewrite(SourceMgr, LangOpts),
Nick Lewycky078a5e22010-08-13 17:31:00 +000033 FixItOpts(FixItOpts),
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000034 NumFailures(0),
35 PrevDiagSilenced(false) {
36 OwnsClient = Diags.ownsClient();
Douglas Gregor2dd19f12010-08-18 22:29:43 +000037 Client = Diags.takeClient();
Douglas Gregora42bd842009-04-02 17:13:00 +000038 Diags.setClient(this);
Douglas Gregor578dae52009-04-02 01:08:08 +000039}
40
41FixItRewriter::~FixItRewriter() {
Douglas Gregor2dd19f12010-08-18 22:29:43 +000042 Diags.takeClient();
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000043 Diags.setClient(Client, OwnsClient);
Douglas Gregor578dae52009-04-02 01:08:08 +000044}
45
Chris Lattner0e62c1c2011-07-23 10:55:15 +000046bool FixItRewriter::WriteFixedFile(FileID ID, raw_ostream &OS) {
Nick Lewyckya1e20de2010-04-15 06:46:58 +000047 const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID);
48 if (!RewriteBuf) return true;
Nick Lewycky40884c02010-04-16 18:49:45 +000049 RewriteBuf->write(OS);
Nick Lewyckya1e20de2010-04-15 06:46:58 +000050 OS.flush();
51 return false;
52}
53
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000054bool FixItRewriter::WriteFixedFiles(
55 std::vector<std::pair<std::string, std::string> > *RewrittenFiles) {
Nick Lewycky53f10422010-08-15 16:47:39 +000056 if (NumFailures > 0 && !FixItOpts->FixWhatYouCan) {
Douglas Gregora42bd842009-04-02 17:13:00 +000057 Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
Douglas Gregor578dae52009-04-02 01:08:08 +000058 return true;
59 }
60
Nick Lewyckya1e20de2010-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);
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +000063 int fd;
64 std::string Filename = FixItOpts->RewriteFilename(Entry->getName(), fd);
Douglas Gregor578dae52009-04-02 01:08:08 +000065 std::string Err;
Dylan Noblesmithe2778992012-02-05 02:12:40 +000066 OwningPtr<llvm::raw_fd_ostream> OS;
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +000067 if (fd != -1) {
68 OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
69 } else {
70 OS.reset(new llvm::raw_fd_ostream(Filename.c_str(), Err,
71 llvm::raw_fd_ostream::F_Binary));
72 }
Nick Lewyckya1e20de2010-04-15 06:46:58 +000073 if (!Err.empty()) {
74 Diags.Report(clang::diag::err_fe_unable_to_open_output)
Nick Lewycky784fad72010-04-24 01:30:46 +000075 << Filename << Err;
Nick Lewyckya1e20de2010-04-15 06:46:58 +000076 continue;
77 }
78 RewriteBuffer &RewriteBuf = I->second;
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +000079 RewriteBuf.write(*OS);
80 OS->flush();
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000081
82 if (RewrittenFiles)
83 RewrittenFiles->push_back(std::make_pair(Entry->getName(), Filename));
Mike Stump11289f42009-09-09 15:08:12 +000084 }
Douglas Gregor578dae52009-04-02 01:08:08 +000085
Douglas Gregor578dae52009-04-02 01:08:08 +000086 return false;
87}
88
89bool FixItRewriter::IncludeInDiagnosticCounts() const {
Nick Lewyckya1e20de2010-04-15 06:46:58 +000090 return Client ? Client->IncludeInDiagnosticCounts() : true;
Douglas Gregor578dae52009-04-02 01:08:08 +000091}
92
David Blaikie9c902b52011-09-25 23:23:43 +000093void FixItRewriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikieb5784322011-09-26 01:18:08 +000094 const Diagnostic &Info) {
Argyrios Kyrtzidis6d35b5a2010-11-18 21:13:54 +000095 // Default implementation (Warnings/errors count).
David Blaikiee2eefae2011-09-25 23:39:51 +000096 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
Argyrios Kyrtzidis6d35b5a2010-11-18 21:13:54 +000097
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000098 if (!FixItOpts->Silent ||
99 DiagLevel >= DiagnosticsEngine::Error ||
100 (DiagLevel == DiagnosticsEngine::Note && !PrevDiagSilenced) ||
101 (DiagLevel > DiagnosticsEngine::Note && Info.getNumFixItHints())) {
102 Client->HandleDiagnostic(DiagLevel, Info);
103 PrevDiagSilenced = false;
104 } else {
105 PrevDiagSilenced = true;
106 }
Douglas Gregor9c0d38a2009-04-02 19:05:20 +0000107
Nick Lewycky784fad72010-04-24 01:30:46 +0000108 // Skip over any diagnostics that are ignored or notes.
David Blaikie9c902b52011-09-25 23:23:43 +0000109 if (DiagLevel <= DiagnosticsEngine::Note)
Douglas Gregor9c0d38a2009-04-02 19:05:20 +0000110 return;
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000111 // Skip over errors if we are only fixing warnings.
112 if (DiagLevel >= DiagnosticsEngine::Error && FixItOpts->FixOnlyWarnings) {
113 ++NumFailures;
114 return;
115 }
Douglas Gregor9c0d38a2009-04-02 19:05:20 +0000116
Douglas Gregor578dae52009-04-02 01:08:08 +0000117 // Make sure that we can perform all of the modifications we
118 // in this diagnostic.
Douglas Gregora771f462010-03-31 17:46:05 +0000119 bool CanRewrite = Info.getNumFixItHints() > 0;
120 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
Douglas Gregor068913e2009-04-02 16:34:42 +0000121 Idx < Last; ++Idx) {
Douglas Gregora771f462010-03-31 17:46:05 +0000122 const FixItHint &Hint = Info.getFixItHint(Idx);
Douglas Gregor578dae52009-04-02 01:08:08 +0000123 if (Hint.RemoveRange.isValid() &&
Douglas Gregora42bd842009-04-02 17:13:00 +0000124 Rewrite.getRangeSize(Hint.RemoveRange) == -1) {
Douglas Gregor578dae52009-04-02 01:08:08 +0000125 CanRewrite = false;
126 break;
127 }
Douglas Gregor578dae52009-04-02 01:08:08 +0000128 }
129
Mike Stump11289f42009-09-09 15:08:12 +0000130 if (!CanRewrite) {
Douglas Gregora771f462010-03-31 17:46:05 +0000131 if (Info.getNumFixItHints() > 0)
Douglas Gregora42bd842009-04-02 17:13:00 +0000132 Diag(Info.getLocation(), diag::note_fixit_in_macro);
Douglas Gregor068913e2009-04-02 16:34:42 +0000133
134 // If this was an error, refuse to perform any rewriting.
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000135 if (DiagLevel >= DiagnosticsEngine::Error) {
Douglas Gregora42bd842009-04-02 17:13:00 +0000136 if (++NumFailures == 1)
137 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
Douglas Gregor068913e2009-04-02 16:34:42 +0000138 }
Douglas Gregor578dae52009-04-02 01:08:08 +0000139 return;
Douglas Gregor068913e2009-04-02 16:34:42 +0000140 }
Douglas Gregor578dae52009-04-02 01:08:08 +0000141
142 bool Failed = false;
Douglas Gregora771f462010-03-31 17:46:05 +0000143 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
Douglas Gregor068913e2009-04-02 16:34:42 +0000144 Idx < Last; ++Idx) {
Douglas Gregora771f462010-03-31 17:46:05 +0000145 const FixItHint &Hint = Info.getFixItHint(Idx);
Mike Stump11289f42009-09-09 15:08:12 +0000146
Douglas Gregor068913e2009-04-02 16:34:42 +0000147 if (Hint.CodeToInsert.empty()) {
148 // We're removing code.
John McCall31168b02011-06-15 23:02:42 +0000149 if (Rewrite.RemoveText(Hint.RemoveRange))
Douglas Gregor068913e2009-04-02 16:34:42 +0000150 Failed = true;
151 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000152 }
153
Douglas Gregor068913e2009-04-02 16:34:42 +0000154 // We're replacing code.
Douglas Gregora42bd842009-04-02 17:13:00 +0000155 if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(),
156 Rewrite.getRangeSize(Hint.RemoveRange),
Daniel Dunbardec484a2009-08-19 19:10:30 +0000157 Hint.CodeToInsert))
Douglas Gregor068913e2009-04-02 16:34:42 +0000158 Failed = true;
Douglas Gregor578dae52009-04-02 01:08:08 +0000159 }
160
Douglas Gregora42bd842009-04-02 17:13:00 +0000161 if (Failed) {
Douglas Gregor578dae52009-04-02 01:08:08 +0000162 ++NumFailures;
Douglas Gregora42bd842009-04-02 17:13:00 +0000163 Diag(Info.getLocation(), diag::note_fixit_failed);
164 return;
165 }
166
167 Diag(Info.getLocation(), diag::note_fixit_applied);
168}
169
170/// \brief Emit a diagnostic via the adapted diagnostic client.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000171void FixItRewriter::Diag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregora42bd842009-04-02 17:13:00 +0000172 // When producing this diagnostic, we temporarily bypass ourselves,
173 // clear out any current diagnostic, and let the downstream client
174 // format the diagnostic.
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000175 Diags.takeClient();
Douglas Gregora42bd842009-04-02 17:13:00 +0000176 Diags.setClient(Client);
177 Diags.Clear();
178 Diags.Report(Loc, DiagID);
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000179 Diags.takeClient();
Mike Stump11289f42009-09-09 15:08:12 +0000180 Diags.setClient(this);
Douglas Gregor578dae52009-04-02 01:08:08 +0000181}
Nick Lewycky784fad72010-04-24 01:30:46 +0000182
Douglas Gregord0e9e3a2011-09-29 00:38:00 +0000183DiagnosticConsumer *FixItRewriter::clone(DiagnosticsEngine &Diags) const {
184 return new FixItRewriter(Diags, Diags.getSourceManager(),
185 Rewrite.getLangOpts(), FixItOpts);
186}
187
Nick Lewycky078a5e22010-08-13 17:31:00 +0000188FixItOptions::~FixItOptions() {}