blob: 3fdec07459d6bde792c5fc181e6592e66ceade94 [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 }
Douglas Gregor558cb562009-04-02 01:08:08 +000099 }
100
Mike Stump1eb44332009-09-09 15:08:12 +0000101 if (!CanRewrite) {
Douglas Gregor849b2432010-03-31 17:46:05 +0000102 if (Info.getNumFixItHints() > 0)
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000103 Diag(Info.getLocation(), diag::note_fixit_in_macro);
Douglas Gregor837a4062009-04-02 16:34:42 +0000104
105 // If this was an error, refuse to perform any rewriting.
106 if (DiagLevel == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000107 if (++NumFailures == 1)
108 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
Douglas Gregor837a4062009-04-02 16:34:42 +0000109 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000110 return;
Douglas Gregor837a4062009-04-02 16:34:42 +0000111 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000112
113 bool Failed = false;
Douglas Gregor849b2432010-03-31 17:46:05 +0000114 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);
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Douglas Gregor837a4062009-04-02 16:34:42 +0000118 if (Hint.CodeToInsert.empty()) {
119 // We're removing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000120 if (Rewrite.RemoveText(Hint.RemoveRange.getBegin(),
121 Rewrite.getRangeSize(Hint.RemoveRange)))
Douglas Gregor837a4062009-04-02 16:34:42 +0000122 Failed = true;
123 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000124 }
125
Douglas Gregor837a4062009-04-02 16:34:42 +0000126 // We're replacing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000127 if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(),
128 Rewrite.getRangeSize(Hint.RemoveRange),
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000129 Hint.CodeToInsert))
Douglas Gregor837a4062009-04-02 16:34:42 +0000130 Failed = true;
Douglas Gregor558cb562009-04-02 01:08:08 +0000131 }
132
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000133 if (Failed) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000134 ++NumFailures;
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000135 Diag(Info.getLocation(), diag::note_fixit_failed);
136 return;
137 }
138
139 Diag(Info.getLocation(), diag::note_fixit_applied);
140}
141
142/// \brief Emit a diagnostic via the adapted diagnostic client.
143void FixItRewriter::Diag(FullSourceLoc Loc, unsigned DiagID) {
144 // When producing this diagnostic, we temporarily bypass ourselves,
145 // clear out any current diagnostic, and let the downstream client
146 // format the diagnostic.
147 Diags.setClient(Client);
148 Diags.Clear();
149 Diags.Report(Loc, DiagID);
Mike Stump1eb44332009-09-09 15:08:12 +0000150 Diags.setClient(this);
Douglas Gregor558cb562009-04-02 01:08:08 +0000151}
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +0000152
Nick Lewycky1450f262010-08-13 17:31:00 +0000153FixItOptions::~FixItOptions() {}