blob: 20cefb01655757253b4554b8c48b447f519dc47d [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"
22#include "llvm/System/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
Chris Lattner2c78b872009-04-14 23:22:57 +000028FixItRewriter::FixItRewriter(Diagnostic &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),
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000034 NumFailures(0) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000035 Client = Diags.getClient();
36 Diags.setClient(this);
Douglas Gregor558cb562009-04-02 01:08:08 +000037}
38
39FixItRewriter::~FixItRewriter() {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000040 Diags.setClient(Client);
Douglas Gregor558cb562009-04-02 01:08:08 +000041}
42
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000043bool FixItRewriter::WriteFixedFile(FileID ID, llvm::raw_ostream &OS) {
44 const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID);
45 if (!RewriteBuf) return true;
Nick Lewycky0ade8082010-04-16 18:49:45 +000046 RewriteBuf->write(OS);
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000047 OS.flush();
48 return false;
49}
50
51bool FixItRewriter::WriteFixedFiles() {
Nick Lewycky48d64fb2010-08-14 23:03:19 +000052 if (NumFailures > 0 && (!FixItOpts || !FixItOpts->FixWhatYouCan)) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000053 Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
Douglas Gregor558cb562009-04-02 01:08:08 +000054 return true;
55 }
56
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000057 for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
58 const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first);
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000059 std::string Filename = Entry->getName();
Nick Lewycky1450f262010-08-13 17:31:00 +000060 if (FixItOpts)
61 Filename = FixItOpts->RewriteFilename(Filename);
Douglas Gregor558cb562009-04-02 01:08:08 +000062 std::string Err;
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000063 llvm::raw_fd_ostream OS(Filename.c_str(), Err,
64 llvm::raw_fd_ostream::F_Binary);
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000065 if (!Err.empty()) {
66 Diags.Report(clang::diag::err_fe_unable_to_open_output)
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000067 << Filename << Err;
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000068 continue;
69 }
70 RewriteBuffer &RewriteBuf = I->second;
Nick Lewycky89fe0192010-04-24 22:31:36 +000071 RewriteBuf.write(OS);
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000072 OS.flush();
Mike Stump1eb44332009-09-09 15:08:12 +000073 }
Douglas Gregor558cb562009-04-02 01:08:08 +000074
Douglas Gregor558cb562009-04-02 01:08:08 +000075 return false;
76}
77
78bool FixItRewriter::IncludeInDiagnosticCounts() const {
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000079 return Client ? Client->IncludeInDiagnosticCounts() : true;
Douglas Gregor558cb562009-04-02 01:08:08 +000080}
81
82void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel,
83 const DiagnosticInfo &Info) {
Douglas Gregor26df2f02009-04-02 19:05:20 +000084 Client->HandleDiagnostic(DiagLevel, Info);
85
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000086 // Skip over any diagnostics that are ignored or notes.
87 if (DiagLevel <= Diagnostic::Note)
Douglas Gregor26df2f02009-04-02 19:05:20 +000088 return;
89
Douglas Gregor558cb562009-04-02 01:08:08 +000090 // Make sure that we can perform all of the modifications we
91 // in this diagnostic.
Douglas Gregor849b2432010-03-31 17:46:05 +000092 bool CanRewrite = Info.getNumFixItHints() > 0;
93 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
Douglas Gregor837a4062009-04-02 16:34:42 +000094 Idx < Last; ++Idx) {
Douglas Gregor849b2432010-03-31 17:46:05 +000095 const FixItHint &Hint = Info.getFixItHint(Idx);
Douglas Gregor558cb562009-04-02 01:08:08 +000096 if (Hint.RemoveRange.isValid() &&
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000097 Rewrite.getRangeSize(Hint.RemoveRange) == -1) {
Douglas Gregor558cb562009-04-02 01:08:08 +000098 CanRewrite = false;
99 break;
100 }
101
Mike Stump1eb44332009-09-09 15:08:12 +0000102 if (Hint.InsertionLoc.isValid() &&
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000103 !Rewrite.isRewritable(Hint.InsertionLoc)) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000104 CanRewrite = false;
105 break;
106 }
107 }
108
Mike Stump1eb44332009-09-09 15:08:12 +0000109 if (!CanRewrite) {
Douglas Gregor849b2432010-03-31 17:46:05 +0000110 if (Info.getNumFixItHints() > 0)
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000111 Diag(Info.getLocation(), diag::note_fixit_in_macro);
Douglas Gregor837a4062009-04-02 16:34:42 +0000112
113 // If this was an error, refuse to perform any rewriting.
114 if (DiagLevel == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000115 if (++NumFailures == 1)
116 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
Douglas Gregor837a4062009-04-02 16:34:42 +0000117 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000118 return;
Douglas Gregor837a4062009-04-02 16:34:42 +0000119 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000120
121 bool Failed = false;
Douglas Gregor849b2432010-03-31 17:46:05 +0000122 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
Douglas Gregor837a4062009-04-02 16:34:42 +0000123 Idx < Last; ++Idx) {
Douglas Gregor849b2432010-03-31 17:46:05 +0000124 const FixItHint &Hint = Info.getFixItHint(Idx);
Douglas Gregor837a4062009-04-02 16:34:42 +0000125 if (!Hint.RemoveRange.isValid()) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000126 // We're adding code.
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000127 if (Rewrite.InsertTextBefore(Hint.InsertionLoc, Hint.CodeToInsert))
Douglas Gregor558cb562009-04-02 01:08:08 +0000128 Failed = true;
Douglas Gregor837a4062009-04-02 16:34:42 +0000129 continue;
Douglas Gregor558cb562009-04-02 01:08:08 +0000130 }
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Douglas Gregor837a4062009-04-02 16:34:42 +0000132 if (Hint.CodeToInsert.empty()) {
133 // We're removing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000134 if (Rewrite.RemoveText(Hint.RemoveRange.getBegin(),
135 Rewrite.getRangeSize(Hint.RemoveRange)))
Douglas Gregor837a4062009-04-02 16:34:42 +0000136 Failed = true;
137 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000138 }
139
Douglas Gregor837a4062009-04-02 16:34:42 +0000140 // We're replacing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000141 if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(),
142 Rewrite.getRangeSize(Hint.RemoveRange),
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000143 Hint.CodeToInsert))
Douglas Gregor837a4062009-04-02 16:34:42 +0000144 Failed = true;
Douglas Gregor558cb562009-04-02 01:08:08 +0000145 }
146
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000147 if (Failed) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000148 ++NumFailures;
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000149 Diag(Info.getLocation(), diag::note_fixit_failed);
150 return;
151 }
152
153 Diag(Info.getLocation(), diag::note_fixit_applied);
154}
155
156/// \brief Emit a diagnostic via the adapted diagnostic client.
157void FixItRewriter::Diag(FullSourceLoc Loc, unsigned DiagID) {
158 // When producing this diagnostic, we temporarily bypass ourselves,
159 // clear out any current diagnostic, and let the downstream client
160 // format the diagnostic.
161 Diags.setClient(Client);
162 Diags.Clear();
163 Diags.Report(Loc, DiagID);
Mike Stump1eb44332009-09-09 15:08:12 +0000164 Diags.setClient(this);
Douglas Gregor558cb562009-04-02 01:08:08 +0000165}
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +0000166
Nick Lewycky1450f262010-08-13 17:31:00 +0000167FixItOptions::~FixItOptions() {}