blob: df7b60547a4ebbecf4bef4056ef8de0fa3b613d1 [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 Lewycky96872c42010-08-15 16:47:39 +000052 if (NumFailures > 0 && !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 Lewycky96872c42010-08-15 16:47:39 +000059 std::string Filename = FixItOpts->RewriteFilename(Entry->getName());
Douglas Gregor558cb562009-04-02 01:08:08 +000060 std::string Err;
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000061 llvm::raw_fd_ostream OS(Filename.c_str(), Err,
62 llvm::raw_fd_ostream::F_Binary);
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000063 if (!Err.empty()) {
64 Diags.Report(clang::diag::err_fe_unable_to_open_output)
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000065 << Filename << Err;
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000066 continue;
67 }
68 RewriteBuffer &RewriteBuf = I->second;
Nick Lewycky89fe0192010-04-24 22:31:36 +000069 RewriteBuf.write(OS);
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000070 OS.flush();
Mike Stump1eb44332009-09-09 15:08:12 +000071 }
Douglas Gregor558cb562009-04-02 01:08:08 +000072
Douglas Gregor558cb562009-04-02 01:08:08 +000073 return false;
74}
75
76bool FixItRewriter::IncludeInDiagnosticCounts() const {
Nick Lewyckyd4a97a12010-04-15 06:46:58 +000077 return Client ? Client->IncludeInDiagnosticCounts() : true;
Douglas Gregor558cb562009-04-02 01:08:08 +000078}
79
80void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel,
81 const DiagnosticInfo &Info) {
Douglas Gregor26df2f02009-04-02 19:05:20 +000082 Client->HandleDiagnostic(DiagLevel, Info);
83
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000084 // Skip over any diagnostics that are ignored or notes.
85 if (DiagLevel <= Diagnostic::Note)
Douglas Gregor26df2f02009-04-02 19:05:20 +000086 return;
87
Douglas Gregor558cb562009-04-02 01:08:08 +000088 // Make sure that we can perform all of the modifications we
89 // in this diagnostic.
Douglas Gregor849b2432010-03-31 17:46:05 +000090 bool CanRewrite = Info.getNumFixItHints() > 0;
91 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
Douglas Gregor837a4062009-04-02 16:34:42 +000092 Idx < Last; ++Idx) {
Douglas Gregor849b2432010-03-31 17:46:05 +000093 const FixItHint &Hint = Info.getFixItHint(Idx);
Douglas Gregor558cb562009-04-02 01:08:08 +000094 if (Hint.RemoveRange.isValid() &&
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000095 Rewrite.getRangeSize(Hint.RemoveRange) == -1) {
Douglas Gregor558cb562009-04-02 01:08:08 +000096 CanRewrite = false;
97 break;
98 }
99
Mike Stump1eb44332009-09-09 15:08:12 +0000100 if (Hint.InsertionLoc.isValid() &&
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000101 !Rewrite.isRewritable(Hint.InsertionLoc)) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000102 CanRewrite = false;
103 break;
104 }
105 }
106
Mike Stump1eb44332009-09-09 15:08:12 +0000107 if (!CanRewrite) {
Douglas Gregor849b2432010-03-31 17:46:05 +0000108 if (Info.getNumFixItHints() > 0)
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000109 Diag(Info.getLocation(), diag::note_fixit_in_macro);
Douglas Gregor837a4062009-04-02 16:34:42 +0000110
111 // If this was an error, refuse to perform any rewriting.
112 if (DiagLevel == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000113 if (++NumFailures == 1)
114 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
Douglas Gregor837a4062009-04-02 16:34:42 +0000115 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000116 return;
Douglas Gregor837a4062009-04-02 16:34:42 +0000117 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000118
119 bool Failed = false;
Douglas Gregor849b2432010-03-31 17:46:05 +0000120 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
Douglas Gregor837a4062009-04-02 16:34:42 +0000121 Idx < Last; ++Idx) {
Douglas Gregor849b2432010-03-31 17:46:05 +0000122 const FixItHint &Hint = Info.getFixItHint(Idx);
Douglas Gregor837a4062009-04-02 16:34:42 +0000123 if (!Hint.RemoveRange.isValid()) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000124 // We're adding code.
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000125 if (Rewrite.InsertTextBefore(Hint.InsertionLoc, Hint.CodeToInsert))
Douglas Gregor558cb562009-04-02 01:08:08 +0000126 Failed = true;
Douglas Gregor837a4062009-04-02 16:34:42 +0000127 continue;
Douglas Gregor558cb562009-04-02 01:08:08 +0000128 }
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Douglas Gregor837a4062009-04-02 16:34:42 +0000130 if (Hint.CodeToInsert.empty()) {
131 // We're removing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000132 if (Rewrite.RemoveText(Hint.RemoveRange.getBegin(),
133 Rewrite.getRangeSize(Hint.RemoveRange)))
Douglas Gregor837a4062009-04-02 16:34:42 +0000134 Failed = true;
135 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000136 }
137
Douglas Gregor837a4062009-04-02 16:34:42 +0000138 // We're replacing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000139 if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(),
140 Rewrite.getRangeSize(Hint.RemoveRange),
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000141 Hint.CodeToInsert))
Douglas Gregor837a4062009-04-02 16:34:42 +0000142 Failed = true;
Douglas Gregor558cb562009-04-02 01:08:08 +0000143 }
144
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000145 if (Failed) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000146 ++NumFailures;
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000147 Diag(Info.getLocation(), diag::note_fixit_failed);
148 return;
149 }
150
151 Diag(Info.getLocation(), diag::note_fixit_applied);
152}
153
154/// \brief Emit a diagnostic via the adapted diagnostic client.
155void FixItRewriter::Diag(FullSourceLoc Loc, unsigned DiagID) {
156 // When producing this diagnostic, we temporarily bypass ourselves,
157 // clear out any current diagnostic, and let the downstream client
158 // format the diagnostic.
159 Diags.setClient(Client);
160 Diags.Clear();
161 Diags.Report(Loc, DiagID);
Mike Stump1eb44332009-09-09 15:08:12 +0000162 Diags.setClient(this);
Douglas Gregor558cb562009-04-02 01:08:08 +0000163}
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +0000164
Nick Lewycky1450f262010-08-13 17:31:00 +0000165FixItOptions::~FixItOptions() {}