blob: 98e2503d59d046cbe4fe9e6185455e76226fecd3 [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
37bool FixItRewriter::WriteFixedFile(const std::string &InFileName,
38 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,
49 llvm::raw_fd_ostream::F_Binary |
50 llvm::raw_fd_ostream::F_Force);
Douglas Gregor558cb562009-04-02 01:08:08 +000051 OwnedStream.reset(OutFile);
52 } else if (InFileName == "-") {
53 OutFile = &llvm::outs();
54 } else {
55 llvm::sys::Path Path(InFileName);
Douglas Gregor26103482009-04-02 03:14:12 +000056 std::string Suffix = Path.getSuffix();
Douglas Gregor558cb562009-04-02 01:08:08 +000057 Path.eraseSuffix();
Douglas Gregor26103482009-04-02 03:14:12 +000058 Path.appendSuffix("fixit." + Suffix);
Douglas Gregor558cb562009-04-02 01:08:08 +000059 std::string Err;
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000060 OutFile = new llvm::raw_fd_ostream(Path.c_str(), Err,
Chris Lattner92bcc272009-08-23 02:59:41 +000061 llvm::raw_fd_ostream::F_Binary |
62 llvm::raw_fd_ostream::F_Force);
Douglas Gregor558cb562009-04-02 01:08:08 +000063 OwnedStream.reset(OutFile);
64 }
65
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000066 FileID MainFileID = Rewrite.getSourceMgr().getMainFileID();
Douglas Gregor558cb562009-04-02 01:08:08 +000067 if (const RewriteBuffer *RewriteBuf =
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000068 Rewrite.getRewriteBufferFor(MainFileID)) {
Douglas Gregor558cb562009-04-02 01:08:08 +000069 *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
70 } else {
71 std::fprintf(stderr, "Main file is unchanged\n");
72 }
73 OutFile->flush();
74
75 return false;
76}
77
78bool FixItRewriter::IncludeInDiagnosticCounts() const {
Douglas Gregor837a4062009-04-02 16:34:42 +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
86 // Skip over any diagnostics that are ignored.
87 if (DiagLevel == Diagnostic::Ignored)
88 return;
89
90 if (!FixItLocations.empty()) {
91 // The user has specified the locations where we should perform
92 // the various fix-it modifications.
93
94 // If this diagnostic does not have any code modifications,
95 // completely ignore it, even if it's an error: fix-it locations
96 // are meant to perform specific fix-ups even in the presence of
97 // other errors.
98 if (Info.getNumCodeModificationHints() == 0)
99 return;
100
101 // See if the location of the error is one that matches what the
102 // user requested.
103 bool AcceptableLocation = false;
104 const FileEntry *File
105 = Rewrite.getSourceMgr().getFileEntryForID(
106 Info.getLocation().getFileID());
107 unsigned Line = Info.getLocation().getSpellingLineNumber();
108 unsigned Column = Info.getLocation().getSpellingColumnNumber();
109 for (llvm::SmallVector<RequestedSourceLocation, 4>::iterator
110 Loc = FixItLocations.begin(), LocEnd = FixItLocations.end();
111 Loc != LocEnd; ++Loc) {
112 if (Loc->File == File && Loc->Line == Line && Loc->Column == Column) {
113 AcceptableLocation = true;
114 break;
115 }
116 }
117
118 if (!AcceptableLocation)
119 return;
120 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000121
122 // Make sure that we can perform all of the modifications we
123 // in this diagnostic.
Douglas Gregor837a4062009-04-02 16:34:42 +0000124 bool CanRewrite = Info.getNumCodeModificationHints() > 0;
125 for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints();
126 Idx < Last; ++Idx) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000127 const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
128 if (Hint.RemoveRange.isValid() &&
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000129 Rewrite.getRangeSize(Hint.RemoveRange) == -1) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000130 CanRewrite = false;
131 break;
132 }
133
134 if (Hint.InsertionLoc.isValid() &&
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000135 !Rewrite.isRewritable(Hint.InsertionLoc)) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000136 CanRewrite = false;
137 break;
138 }
139 }
140
Douglas Gregor837a4062009-04-02 16:34:42 +0000141 if (!CanRewrite) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000142 if (Info.getNumCodeModificationHints() > 0)
143 Diag(Info.getLocation(), diag::note_fixit_in_macro);
Douglas Gregor837a4062009-04-02 16:34:42 +0000144
145 // If this was an error, refuse to perform any rewriting.
146 if (DiagLevel == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000147 if (++NumFailures == 1)
148 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
Douglas Gregor837a4062009-04-02 16:34:42 +0000149 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000150 return;
Douglas Gregor837a4062009-04-02 16:34:42 +0000151 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000152
153 bool Failed = false;
Douglas Gregor837a4062009-04-02 16:34:42 +0000154 for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints();
155 Idx < Last; ++Idx) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000156 const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
Douglas Gregor837a4062009-04-02 16:34:42 +0000157 if (!Hint.RemoveRange.isValid()) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000158 // We're adding code.
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000159 if (Rewrite.InsertTextBefore(Hint.InsertionLoc, Hint.CodeToInsert))
Douglas Gregor558cb562009-04-02 01:08:08 +0000160 Failed = true;
Douglas Gregor837a4062009-04-02 16:34:42 +0000161 continue;
Douglas Gregor558cb562009-04-02 01:08:08 +0000162 }
Douglas Gregor837a4062009-04-02 16:34:42 +0000163
164 if (Hint.CodeToInsert.empty()) {
165 // We're removing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000166 if (Rewrite.RemoveText(Hint.RemoveRange.getBegin(),
167 Rewrite.getRangeSize(Hint.RemoveRange)))
Douglas Gregor837a4062009-04-02 16:34:42 +0000168 Failed = true;
169 continue;
170 }
171
172 // We're replacing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000173 if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(),
174 Rewrite.getRangeSize(Hint.RemoveRange),
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000175 Hint.CodeToInsert))
Douglas Gregor837a4062009-04-02 16:34:42 +0000176 Failed = true;
Douglas Gregor558cb562009-04-02 01:08:08 +0000177 }
178
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000179 if (Failed) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000180 ++NumFailures;
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000181 Diag(Info.getLocation(), diag::note_fixit_failed);
182 return;
183 }
184
185 Diag(Info.getLocation(), diag::note_fixit_applied);
186}
187
188/// \brief Emit a diagnostic via the adapted diagnostic client.
189void FixItRewriter::Diag(FullSourceLoc Loc, unsigned DiagID) {
190 // When producing this diagnostic, we temporarily bypass ourselves,
191 // clear out any current diagnostic, and let the downstream client
192 // format the diagnostic.
193 Diags.setClient(Client);
194 Diags.Clear();
195 Diags.Report(Loc, DiagID);
196 Diags.setClient(this);
Douglas Gregor558cb562009-04-02 01:08:08 +0000197}