blob: 0b04cf2b44d3ae7c1a1037368d84e1bb63b2bbf5 [file] [log] [blame]
Shih-wei Liaof8fd82b2010-02-10 11:10:31 -08001//===--- 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//===----------------------------------------------------------------------===//
15
16#include "clang/Frontend/FixItRewriter.h"
17#include "clang/Basic/SourceManager.h"
18#include "clang/Frontend/FrontendDiagnostic.h"
19#include "llvm/Support/raw_ostream.h"
20#include "llvm/System/Path.h"
21#include "llvm/ADT/OwningPtr.h"
22#include <cstdio>
23
24using namespace clang;
25
26FixItRewriter::FixItRewriter(Diagnostic &Diags, SourceManager &SourceMgr,
27 const LangOptions &LangOpts)
28 : Diags(Diags), Rewrite(SourceMgr, LangOpts), NumFailures(0) {
29 Client = Diags.getClient();
30 Diags.setClient(this);
31}
32
33FixItRewriter::~FixItRewriter() {
34 Diags.setClient(Client);
35}
36
37bool FixItRewriter::WriteFixedFile(const std::string &InFileName,
38 const std::string &OutFileName) {
39 if (NumFailures > 0) {
40 Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
41 return true;
42 }
43
44 llvm::OwningPtr<llvm::raw_ostream> OwnedStream;
45 llvm::raw_ostream *OutFile;
46 if (!OutFileName.empty()) {
47 std::string Err;
48 OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(), Err,
49 llvm::raw_fd_ostream::F_Binary);
50 OwnedStream.reset(OutFile);
51 } else if (InFileName == "-") {
52 OutFile = &llvm::outs();
53 } else {
54 llvm::sys::Path Path(InFileName);
55 std::string Suffix = Path.getSuffix();
56 Path.eraseSuffix();
57 Path.appendSuffix("fixit." + Suffix);
58 std::string Err;
59 OutFile = new llvm::raw_fd_ostream(Path.c_str(), Err,
60 llvm::raw_fd_ostream::F_Binary);
61 OwnedStream.reset(OutFile);
62 }
63
64 FileID MainFileID = Rewrite.getSourceMgr().getMainFileID();
65 if (const RewriteBuffer *RewriteBuf =
66 Rewrite.getRewriteBufferFor(MainFileID)) {
67 *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
68 } else {
69 Diag(FullSourceLoc(), diag::note_fixit_main_file_unchanged);
70 }
71 OutFile->flush();
72
73 return false;
74}
75
76bool FixItRewriter::IncludeInDiagnosticCounts() const {
77 return Client? Client->IncludeInDiagnosticCounts() : true;
78}
79
80void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel,
81 const DiagnosticInfo &Info) {
82 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;
102 const FileEntry *File
103 = 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;
118 } else if (DiagLevel == Diagnostic::Note) {
119 // Don't apply fix-it modifications in notes.
120 return;
121 }
122
123 // Make sure that we can perform all of the modifications we
124 // in this diagnostic.
125 bool CanRewrite = Info.getNumCodeModificationHints() > 0;
126 for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints();
127 Idx < Last; ++Idx) {
128 const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
129 if (Hint.RemoveRange.isValid() &&
130 Rewrite.getRangeSize(Hint.RemoveRange) == -1) {
131 CanRewrite = false;
132 break;
133 }
134
135 if (Hint.InsertionLoc.isValid() &&
136 !Rewrite.isRewritable(Hint.InsertionLoc)) {
137 CanRewrite = false;
138 break;
139 }
140 }
141
142 if (!CanRewrite) {
143 if (Info.getNumCodeModificationHints() > 0)
144 Diag(Info.getLocation(), diag::note_fixit_in_macro);
145
146 // If this was an error, refuse to perform any rewriting.
147 if (DiagLevel == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) {
148 if (++NumFailures == 1)
149 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
150 }
151 return;
152 }
153
154 bool Failed = false;
155 for (unsigned Idx = 0, Last = Info.getNumCodeModificationHints();
156 Idx < Last; ++Idx) {
157 const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
158 if (!Hint.RemoveRange.isValid()) {
159 // We're adding code.
160 if (Rewrite.InsertTextBefore(Hint.InsertionLoc, Hint.CodeToInsert))
161 Failed = true;
162 continue;
163 }
164
165 if (Hint.CodeToInsert.empty()) {
166 // We're removing code.
167 if (Rewrite.RemoveText(Hint.RemoveRange.getBegin(),
168 Rewrite.getRangeSize(Hint.RemoveRange)))
169 Failed = true;
170 continue;
171 }
172
173 // We're replacing code.
174 if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(),
175 Rewrite.getRangeSize(Hint.RemoveRange),
176 Hint.CodeToInsert))
177 Failed = true;
178 }
179
180 if (Failed) {
181 ++NumFailures;
182 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);
197 Diags.setClient(this);
198}