blob: 317693323f999fd2305acca2578a7574d30246d5 [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/ADT/OwningPtr.h"
20#include "llvm/Support/Streams.h"
21#include "llvm/Support/raw_ostream.h"
22#include "llvm/System/Path.h"
Douglas Gregor558cb562009-04-02 01:08:08 +000023using namespace clang;
24
Chris Lattner2c78b872009-04-14 23:22:57 +000025FixItRewriter::FixItRewriter(Diagnostic &Diags, SourceManager &SourceMgr,
26 const LangOptions &LangOpts)
27 : Diags(Diags), Rewrite(SourceMgr, LangOpts), NumFailures(0) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000028 Client = Diags.getClient();
29 Diags.setClient(this);
Douglas Gregor558cb562009-04-02 01:08:08 +000030}
31
32FixItRewriter::~FixItRewriter() {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000033 Diags.setClient(Client);
Douglas Gregor558cb562009-04-02 01:08:08 +000034}
35
36bool FixItRewriter::WriteFixedFile(const std::string &InFileName,
37 const std::string &OutFileName) {
38 if (NumFailures > 0) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000039 Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
Douglas Gregor558cb562009-04-02 01:08:08 +000040 return true;
41 }
42
43 llvm::OwningPtr<llvm::raw_ostream> OwnedStream;
44 llvm::raw_ostream *OutFile;
Douglas Gregor837a4062009-04-02 16:34:42 +000045 if (!OutFileName.empty()) {
Douglas Gregor558cb562009-04-02 01:08:08 +000046 std::string Err;
Chris Lattner92bcc272009-08-23 02:59:41 +000047 OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(), Err,
48 llvm::raw_fd_ostream::F_Binary |
49 llvm::raw_fd_ostream::F_Force);
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 Lattner92bcc272009-08-23 02:59:41 +000059 OutFile = new llvm::raw_fd_ostream(Path.toString().c_str(), Err,
60 llvm::raw_fd_ostream::F_Binary |
61 llvm::raw_fd_ostream::F_Force);
Douglas Gregor558cb562009-04-02 01:08:08 +000062 OwnedStream.reset(OutFile);
63 }
64
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000065 FileID MainFileID = Rewrite.getSourceMgr().getMainFileID();
Douglas Gregor558cb562009-04-02 01:08:08 +000066 if (const RewriteBuffer *RewriteBuf =
Douglas Gregorde4bf6a2009-04-02 17:13:00 +000067 Rewrite.getRewriteBufferFor(MainFileID)) {
Douglas Gregor558cb562009-04-02 01:08:08 +000068 *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
69 } else {
70 std::fprintf(stderr, "Main file is unchanged\n");
71 }
72 OutFile->flush();
73
74 return false;
75}
76
77bool FixItRewriter::IncludeInDiagnosticCounts() const {
Douglas Gregor837a4062009-04-02 16:34:42 +000078 return Client? Client->IncludeInDiagnosticCounts() : true;
Douglas Gregor558cb562009-04-02 01:08:08 +000079}
80
81void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel,
82 const DiagnosticInfo &Info) {
Douglas Gregor26df2f02009-04-02 19:05:20 +000083 Client->HandleDiagnostic(DiagLevel, Info);
84
85 // Skip over any diagnostics that are ignored.
86 if (DiagLevel == Diagnostic::Ignored)
87 return;
88
89 if (!FixItLocations.empty()) {
90 // The user has specified the locations where we should perform
91 // the various fix-it modifications.
92
93 // If this diagnostic does not have any code modifications,
94 // completely ignore it, even if it's an error: fix-it locations
95 // are meant to perform specific fix-ups even in the presence of
96 // other errors.
97 if (Info.getNumCodeModificationHints() == 0)
98 return;
99
100 // See if the location of the error is one that matches what the
101 // user requested.
102 bool AcceptableLocation = false;
103 const FileEntry *File
104 = Rewrite.getSourceMgr().getFileEntryForID(
105 Info.getLocation().getFileID());
106 unsigned Line = Info.getLocation().getSpellingLineNumber();
107 unsigned Column = Info.getLocation().getSpellingColumnNumber();
108 for (llvm::SmallVector<RequestedSourceLocation, 4>::iterator
109 Loc = FixItLocations.begin(), LocEnd = FixItLocations.end();
110 Loc != LocEnd; ++Loc) {
111 if (Loc->File == File && Loc->Line == Line && Loc->Column == Column) {
112 AcceptableLocation = true;
113 break;
114 }
115 }
116
117 if (!AcceptableLocation)
118 return;
119 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000120
121 // Make sure that we can perform all of the modifications we
122 // in this diagnostic.
Douglas Gregor837a4062009-04-02 16:34:42 +0000123 bool CanRewrite = Info.getNumCodeModificationHints() > 0;
124 for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints();
125 Idx < Last; ++Idx) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000126 const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
127 if (Hint.RemoveRange.isValid() &&
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000128 Rewrite.getRangeSize(Hint.RemoveRange) == -1) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000129 CanRewrite = false;
130 break;
131 }
132
133 if (Hint.InsertionLoc.isValid() &&
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000134 !Rewrite.isRewritable(Hint.InsertionLoc)) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000135 CanRewrite = false;
136 break;
137 }
138 }
139
Douglas Gregor837a4062009-04-02 16:34:42 +0000140 if (!CanRewrite) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000141 if (Info.getNumCodeModificationHints() > 0)
142 Diag(Info.getLocation(), diag::note_fixit_in_macro);
Douglas Gregor837a4062009-04-02 16:34:42 +0000143
144 // If this was an error, refuse to perform any rewriting.
145 if (DiagLevel == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) {
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000146 if (++NumFailures == 1)
147 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
Douglas Gregor837a4062009-04-02 16:34:42 +0000148 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000149 return;
Douglas Gregor837a4062009-04-02 16:34:42 +0000150 }
Douglas Gregor558cb562009-04-02 01:08:08 +0000151
152 bool Failed = false;
Douglas Gregor837a4062009-04-02 16:34:42 +0000153 for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints();
154 Idx < Last; ++Idx) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000155 const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
Douglas Gregor837a4062009-04-02 16:34:42 +0000156 if (!Hint.RemoveRange.isValid()) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000157 // We're adding code.
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000158 if (Rewrite.InsertTextBefore(Hint.InsertionLoc, Hint.CodeToInsert))
Douglas Gregor558cb562009-04-02 01:08:08 +0000159 Failed = true;
Douglas Gregor837a4062009-04-02 16:34:42 +0000160 continue;
Douglas Gregor558cb562009-04-02 01:08:08 +0000161 }
Douglas Gregor837a4062009-04-02 16:34:42 +0000162
163 if (Hint.CodeToInsert.empty()) {
164 // We're removing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000165 if (Rewrite.RemoveText(Hint.RemoveRange.getBegin(),
166 Rewrite.getRangeSize(Hint.RemoveRange)))
Douglas Gregor837a4062009-04-02 16:34:42 +0000167 Failed = true;
168 continue;
169 }
170
171 // We're replacing code.
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000172 if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(),
173 Rewrite.getRangeSize(Hint.RemoveRange),
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000174 Hint.CodeToInsert))
Douglas Gregor837a4062009-04-02 16:34:42 +0000175 Failed = true;
Douglas Gregor558cb562009-04-02 01:08:08 +0000176 }
177
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000178 if (Failed) {
Douglas Gregor558cb562009-04-02 01:08:08 +0000179 ++NumFailures;
Douglas Gregorde4bf6a2009-04-02 17:13:00 +0000180 Diag(Info.getLocation(), diag::note_fixit_failed);
181 return;
182 }
183
184 Diag(Info.getLocation(), diag::note_fixit_applied);
185}
186
187/// \brief Emit a diagnostic via the adapted diagnostic client.
188void FixItRewriter::Diag(FullSourceLoc Loc, unsigned DiagID) {
189 // When producing this diagnostic, we temporarily bypass ourselves,
190 // clear out any current diagnostic, and let the downstream client
191 // format the diagnostic.
192 Diags.setClient(Client);
193 Diags.Clear();
194 Diags.Report(Loc, DiagID);
195 Diags.setClient(this);
Douglas Gregor558cb562009-04-02 01:08:08 +0000196}