blob: 0b04cf2b44d3ae7c1a1037368d84e1bb63b2bbf5 [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
Douglas Gregor558cb562009-04-02 01:08:08 +000016#include "clang/Frontend/FixItRewriter.h"
Douglas Gregor837a4062009-04-02 16:34:42 +000017#include "clang/Basic/SourceManager.h"
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000018#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregor558cb562009-04-02 01:08:08 +000019#include "llvm/Support/raw_ostream.h"
20#include "llvm/System/Path.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000021#include "llvm/ADT/OwningPtr.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000022#include <cstdio>
23
Douglas Gregor558cb562009-04-02 01:08:08 +000024using namespace clang;
25
Chris Lattner2c78b872009-04-14 23:22:57 +000026FixItRewriter::FixItRewriter(Diagnostic &Diags, SourceManager &SourceMgr,
27 const LangOptions &LangOpts)
28 : Diags(Diags), Rewrite(SourceMgr, LangOpts), NumFailures(0) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000029 Client = Diags.getClient();
30 Diags.setClient(this);
Douglas Gregor558cb562009-04-02 01:08:08 +000031}
32
33FixItRewriter::~FixItRewriter() {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000034 Diags.setClient(Client);
Douglas Gregor558cb562009-04-02 01:08:08 +000035}
36
Mike Stump1eb44332009-09-09 15:08:12 +000037bool FixItRewriter::WriteFixedFile(const std::string &InFileName,
Douglas Gregor558cb562009-04-02 01:08:08 +000038 const std::string &OutFileName) {
39 if (NumFailures > 0) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000040 Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
Douglas Gregor558cb562009-04-02 01:08:08 +000041 return true;
42 }
43
44 llvm::OwningPtr<llvm::raw_ostream> OwnedStream;
45 llvm::raw_ostream *OutFile;
Douglas Gregor837a4062009-04-02 16:34:42 +000046 if (!OutFileName.empty()) {
Douglas Gregor558cb562009-04-02 01:08:08 +000047 std::string Err;
Chris Lattner92bcc272009-08-23 02:59:41 +000048 OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(), Err,
Dan Gohmanb044c472009-08-25 15:36:09 +000049 llvm::raw_fd_ostream::F_Binary);
Douglas Gregor558cb562009-04-02 01:08:08 +000050 OwnedStream.reset(OutFile);
51 } else if (InFileName == "-") {
52 OutFile = &llvm::outs();
53 } else {
54 llvm::sys::Path Path(InFileName);
Douglas Gregor26103482009-04-02 03:14:12 +000055 std::string Suffix = Path.getSuffix();
Douglas Gregor558cb562009-04-02 01:08:08 +000056 Path.eraseSuffix();
Douglas Gregor26103482009-04-02 03:14:12 +000057 Path.appendSuffix("fixit." + Suffix);
Douglas Gregor558cb562009-04-02 01:08:08 +000058 std::string Err;
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000059 OutFile = new llvm::raw_fd_ostream(Path.c_str(), Err,
Dan Gohmanb044c472009-08-25 15:36:09 +000060 llvm::raw_fd_ostream::F_Binary);
Douglas Gregor558cb562009-04-02 01:08:08 +000061 OwnedStream.reset(OutFile);
Mike Stump1eb44332009-09-09 15:08:12 +000062 }
Douglas Gregor558cb562009-04-02 01:08:08 +000063
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000064 FileID MainFileID = Rewrite.getSourceMgr().getMainFileID();
Mike Stump1eb44332009-09-09 15:08:12 +000065 if (const RewriteBuffer *RewriteBuf =
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000066 Rewrite.getRewriteBufferFor(MainFileID)) {
Douglas Gregor558cb562009-04-02 01:08:08 +000067 *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
68 } else {
Daniel Dunbare7cb7e42009-12-03 09:14:02 +000069 Diag(FullSourceLoc(), diag::note_fixit_main_file_unchanged);
Douglas Gregor558cb562009-04-02 01:08:08 +000070 }
71 OutFile->flush();
72
73 return false;
74}
75
76bool FixItRewriter::IncludeInDiagnosticCounts() const {
Douglas Gregor837a4062009-04-02 16:34:42 +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
84 // Skip over any diagnostics that are ignored.
85 if (DiagLevel == Diagnostic::Ignored)
86 return;
87
88 if (!FixItLocations.empty()) {
89 // The user has specified the locations where we should perform
90 // the various fix-it modifications.
91
92 // If this diagnostic does not have any code modifications,
93 // completely ignore it, even if it's an error: fix-it locations
94 // are meant to perform specific fix-ups even in the presence of
95 // other errors.
96 if (Info.getNumCodeModificationHints() == 0)
97 return;
98
99 // See if the location of the error is one that matches what the
100 // user requested.
101 bool AcceptableLocation = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000102 const FileEntry *File
Douglas Gregor26df2f02009-04-02 19:05:20 +0000103 = Rewrite.getSourceMgr().getFileEntryForID(
104 Info.getLocation().getFileID());
105 unsigned Line = Info.getLocation().getSpellingLineNumber();
106 unsigned Column = Info.getLocation().getSpellingColumnNumber();
107 for (llvm::SmallVector<RequestedSourceLocation, 4>::iterator
108 Loc = FixItLocations.begin(), LocEnd = FixItLocations.end();
109 Loc != LocEnd; ++Loc) {
110 if (Loc->File == File && Loc->Line == Line && Loc->Column == Column) {
111 AcceptableLocation = true;
112 break;
113 }
114 }
115
116 if (!AcceptableLocation)
117 return;
Douglas Gregora6f26382010-01-06 23:44:25 +0000118 } else if (DiagLevel == Diagnostic::Note) {
119 // Don't apply fix-it modifications in notes.
120 return;
Douglas Gregor26df2f02009-04-02 19:05:20 +0000121 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000122
123 // Make sure that we can perform all of the modifications we
124 // in this diagnostic.
Douglas Gregor837a4062009-04-02 16:34:42 +0000125 bool CanRewrite = Info.getNumCodeModificationHints() > 0;
126 for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints();
127 Idx < Last; ++Idx) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000128 const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
129 if (Hint.RemoveRange.isValid() &&
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000130 Rewrite.getRangeSize(Hint.RemoveRange) == -1) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000131 CanRewrite = false;
132 break;
133 }
134
Mike Stump1eb44332009-09-09 15:08:12 +0000135 if (Hint.InsertionLoc.isValid() &&
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000136 !Rewrite.isRewritable(Hint.InsertionLoc)) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000137 CanRewrite = false;
138 break;
139 }
140 }
141
Mike Stump1eb44332009-09-09 15:08:12 +0000142 if (!CanRewrite) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000143 if (Info.getNumCodeModificationHints() > 0)
144 Diag(Info.getLocation(), diag::note_fixit_in_macro);
Douglas Gregor837a4062009-04-02 16:34:42 +0000145
146 // If this was an error, refuse to perform any rewriting.
147 if (DiagLevel == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000148 if (++NumFailures == 1)
149 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
Douglas Gregor837a4062009-04-02 16:34:42 +0000150 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000151 return;
Douglas Gregor837a4062009-04-02 16:34:42 +0000152 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000153
154 bool Failed = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000155 for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints();
Douglas Gregor837a4062009-04-02 16:34:42 +0000156 Idx < Last; ++Idx) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000157 const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
Douglas Gregor837a4062009-04-02 16:34:42 +0000158 if (!Hint.RemoveRange.isValid()) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000159 // We're adding code.
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000160 if (Rewrite.InsertTextBefore(Hint.InsertionLoc, Hint.CodeToInsert))
Douglas Gregor558cb562009-04-02 01:08:08 +0000161 Failed = true;
Douglas Gregor837a4062009-04-02 16:34:42 +0000162 continue;
Douglas Gregor558cb562009-04-02 01:08:08 +0000163 }
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Douglas Gregor837a4062009-04-02 16:34:42 +0000165 if (Hint.CodeToInsert.empty()) {
166 // We're removing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000167 if (Rewrite.RemoveText(Hint.RemoveRange.getBegin(),
168 Rewrite.getRangeSize(Hint.RemoveRange)))
Douglas Gregor837a4062009-04-02 16:34:42 +0000169 Failed = true;
170 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000171 }
172
Douglas Gregor837a4062009-04-02 16:34:42 +0000173 // We're replacing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000174 if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(),
175 Rewrite.getRangeSize(Hint.RemoveRange),
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000176 Hint.CodeToInsert))
Douglas Gregor837a4062009-04-02 16:34:42 +0000177 Failed = true;
Douglas Gregor558cb562009-04-02 01:08:08 +0000178 }
179
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000180 if (Failed) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000181 ++NumFailures;
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000182 Diag(Info.getLocation(), diag::note_fixit_failed);
183 return;
184 }
185
186 Diag(Info.getLocation(), diag::note_fixit_applied);
187}
188
189/// \brief Emit a diagnostic via the adapted diagnostic client.
190void FixItRewriter::Diag(FullSourceLoc Loc, unsigned DiagID) {
191 // When producing this diagnostic, we temporarily bypass ourselves,
192 // clear out any current diagnostic, and let the downstream client
193 // format the diagnostic.
194 Diags.setClient(Client);
195 Diags.Clear();
196 Diags.Report(Loc, DiagID);
Mike Stump1eb44332009-09-09 15:08:12 +0000197 Diags.setClient(this);
Douglas Gregor558cb562009-04-02 01:08:08 +0000198}