clang 8.0.0

FixItRewriter.cpp
Go to the documentation of this file.
1 //===- FixItRewriter.cpp - Fix-It Rewriter Diagnostic Client --------------===//
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/Rewrite/Frontend/FixItRewriter.h"
17 #include "clang/Basic/Diagnostic.h"
18 #include "clang/Basic/FileManager.h"
19 #include "clang/Basic/LLVM.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "clang/Edit/Commit.h"
23 #include "clang/Edit/EditsReceiver.h"
24 #include "clang/Frontend/FrontendDiagnostic.h"
25 #include "clang/Rewrite/Core/RewriteBuffer.h"
26 #include "clang/Rewrite/Core/Rewriter.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include <cstdio>
31 #include <memory>
32 #include <string>
33 #include <system_error>
34 #include <utility>
35
36 using namespace clang;
37
38 FixItRewriter::FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
39 const LangOptions &LangOpts,
40 FixItOptions *FixItOpts)
41 : Diags(Diags), Editor(SourceMgr, LangOpts), Rewrite(SourceMgr, LangOpts),
42 FixItOpts(FixItOpts) {
43 Owner = Diags.takeClient();
44 Client = Diags.getClient();
45 Diags.setClient(this, false);
46 }
47
48 FixItRewriter::~FixItRewriter() {
49 Diags.setClient(Client, Owner.release() != nullptr);
50 }
51
52 bool FixItRewriter::WriteFixedFile(FileID ID, raw_ostream &OS) {
53 const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID);
54 if (!RewriteBuf) return true;
55 RewriteBuf->write(OS);
56 OS.flush();
57 return false;
58 }
59
60 namespace {
61
62 class RewritesReceiver : public edit::EditsReceiver {
63 Rewriter &Rewrite;
64
65 public:
66 RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) {}
67
68 void insert(SourceLocation loc, StringRef text) override {
69 Rewrite.InsertText(loc, text);
70 }
71
72 void replace(CharSourceRange range, StringRef text) override {
73 Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text);
74 }
75 };
76
77 } // namespace
78
79 bool FixItRewriter::WriteFixedFiles(
80 std::vector<std::pair<std::string, std::string>> *RewrittenFiles) {
81 if (NumFailures > 0 && !FixItOpts->FixWhatYouCan) {
82 Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
83 return true;
84 }
85
86 RewritesReceiver Rec(Rewrite);
87 Editor.applyRewrites(Rec);
88
89 if (FixItOpts->InPlace) {
90 // Overwriting open files on Windows is tricky, but the rewriter can do it
91 // for us.
92 Rewrite.overwriteChangedFiles();
93 return false;
94 }
95
96 for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
97 const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first);
98 int fd;
99 std::string Filename = FixItOpts->RewriteFilename(Entry->getName(), fd);
100 std::error_code EC;
101 std::unique_ptr<llvm::raw_fd_ostream> OS;
102 if (fd != -1) {
103 OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
104 } else {
105 OS.reset(new llvm::raw_fd_ostream(Filename, EC, llvm::sys::fs::F_None));
106 }
107 if (EC) {
108 Diags.Report(clang::diag::err_fe_unable_to_open_output) << Filename
109 << EC.message();
110 continue;
111 }
112 RewriteBuffer &RewriteBuf = I->second;
113 RewriteBuf.write(*OS);
114 OS->flush();
115
116 if (RewrittenFiles)
117 RewrittenFiles->push_back(std::make_pair(Entry->getName(), Filename));
118 }
119
120 return false;
121 }
122
123 bool FixItRewriter::IncludeInDiagnosticCounts() const {
124 return Client ? Client->IncludeInDiagnosticCounts() : true;
125 }
126
127 void FixItRewriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
128 const Diagnostic &Info) {
129 // Default implementation (Warnings/errors count).
130 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
131
132 if (!FixItOpts->Silent ||
133 DiagLevel >= DiagnosticsEngine::Error ||
134 (DiagLevel == DiagnosticsEngine::Note && !PrevDiagSilenced) ||
135 (DiagLevel > DiagnosticsEngine::Note && Info.getNumFixItHints())) {
136 Client->HandleDiagnostic(DiagLevel, Info);
137 PrevDiagSilenced = false;
138 } else {
139 PrevDiagSilenced = true;
140 }
141
142 // Skip over any diagnostics that are ignored or notes.
143 if (DiagLevel <= DiagnosticsEngine::Note)
144 return;
145 // Skip over errors if we are only fixing warnings.
146 if (DiagLevel >= DiagnosticsEngine::Error && FixItOpts->FixOnlyWarnings) {
147 ++NumFailures;
148 return;
149 }
150
151 // Make sure that we can perform all of the modifications we
152 // in this diagnostic.
153 edit::Commit commit(Editor);
154 for (unsigned Idx = 0, Last = Info.getNumFixItHints();
155 Idx < Last; ++Idx) {
156 const FixItHint &Hint = Info.getFixItHint(Idx);
157
158 if (Hint.CodeToInsert.empty()) {
159 if (Hint.InsertFromRange.isValid())
160 commit.insertFromRange(Hint.RemoveRange.getBegin(),
161 Hint.InsertFromRange, /*afterToken=*/false,
162 Hint.BeforePreviousInsertions);
163 else
164 commit.remove(Hint.RemoveRange);
165 } else {
166 if (Hint.RemoveRange.isTokenRange() ||
167 Hint.RemoveRange.getBegin() != Hint.RemoveRange.getEnd())
168 commit.replace(Hint.RemoveRange, Hint.CodeToInsert);
169 else
170 commit.insert(Hint.RemoveRange.getBegin(), Hint.CodeToInsert,
171 /*afterToken=*/false, Hint.BeforePreviousInsertions);
172 }
173 }
174 bool CanRewrite = Info.getNumFixItHints() > 0 && commit.isCommitable();
175
176 if (!CanRewrite) {
177 if (Info.getNumFixItHints() > 0)
178 Diag(Info.getLocation(), diag::note_fixit_in_macro);
179
180 // If this was an error, refuse to perform any rewriting.
181 if (DiagLevel >= DiagnosticsEngine::Error) {
182 if (++NumFailures == 1)
183 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
184 }
185 return;
186 }
187
188 if (!Editor.commit(commit)) {
189 ++NumFailures;
190 Diag(Info.getLocation(), diag::note_fixit_failed);
191 return;
192 }
193
194 Diag(Info.getLocation(), diag::note_fixit_applied);
195 }
196
197 /// Emit a diagnostic via the adapted diagnostic client.
198 void FixItRewriter::Diag(SourceLocation Loc, unsigned DiagID) {
199 // When producing this diagnostic, we temporarily bypass ourselves,
200 // clear out any current diagnostic, and let the downstream client
201 // format the diagnostic.
202 Diags.setClient(Client, false);
203 Diags.Clear();
204 Diags.Report(Loc, DiagID);
205 Diags.setClient(this, false);
206 }
207
208 FixItOptions::~FixItOptions() = default;
clang::FixItRewriter::buffer_begin
iterator buffer_begin()
Definition: FixItRewriter.h:104
clang::Builtin::ID
ID
Definition: Builtins.h:49
Rewriter.h
clang::edit::Commit::remove
bool remove(CharSourceRange range)
Definition: Commit.cpp:92
clang::DiagnosticsEngine::getClient
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:510
FileManager.h
Defines the clang::FileManager interface and associated types.
Commit.h
SourceManager.h
Defines the SourceManager interface.
clang::FixItHint::CodeToInsert
std::string CodeToInsert
The actual code to insert at the insertion location, as a string.
Definition: Diagnostic.h:78
clang::edit::Commit::insertFromRange
bool insertFromRange(SourceLocation loc, CharSourceRange range, bool afterToken=false, bool beforePreviousInsertions=false)
Definition: Commit.cpp:65
clang::DiagnosticsEngine::takeClient
std::unique_ptr< DiagnosticConsumer > takeClient()
Return the current diagnostic client along with ownership of that client.
Definition: Diagnostic.h:518
clang::RewriteBuffer
RewriteBuffer - As code is rewritten, SourceBuffer&#39;s from the original input with modifications get a...
Definition: RewriteBuffer.h:26
clang::FixItRewriter::WriteFixedFiles
bool WriteFixedFiles(std::vector< std::pair< std::string, std::string >> *RewrittenFiles=nullptr)
Write the modified source files.
Definition: FixItRewriter.cpp:79
FixItRewriter.h
clang::edit::Commit::insert
bool insert(SourceLocation loc, StringRef text, bool afterToken=false, bool beforePreviousInsertions=false)
Definition: Commit.cpp:49
clang::DiagnosticsEngine::setClient
void setClient(DiagnosticConsumer *client, bool ShouldOwnClient=true)
Set the diagnostic client associated with this diagnostic object.
Definition: Diagnostic.cpp:100
clang::CharSourceRange::getBegin
SourceLocation getBegin() const
Definition: SourceLocation.h:266
clang::DiagnosticsEngine::Note
Definition: Diagnostic.h:154
clang::edit::EditsReceiver
Definition: EditsReceiver.h:20
clang::Diagnostic::getFixItHint
const FixItHint & getFixItHint(unsigned Idx) const
Definition: Diagnostic.h:1412
clang::Rewriter::ReplaceText
bool ReplaceText(SourceLocation Start, unsigned OrigLength, StringRef NewStr)
ReplaceText - This method replaces a range of characters in the input buffer with a new string...
Definition: Rewriter.cpp:307
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:50
clang::CharSourceRange::isValid
bool isValid() const
Definition: SourceLocation.h:274
clang::edit::Commit::replace
bool replace(CharSourceRange range, StringRef text)
Definition: Commit.cpp:117
RewriteBuffer.h
clang::Diagnostic::getLocation
const SourceLocation & getLocation() const
Definition: Diagnostic.h:1327
LLVM.h
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:149
clang::FixItRewriter::~FixItRewriter
~FixItRewriter() override
Destroy the fix-it rewriter.
Definition: FixItRewriter.cpp:48
Diagnostic.h
Defines the Diagnostic-related interfaces.
EditsReceiver.h
FrontendDiagnostic.h
Filename
StringRef Filename
Definition: Format.cpp:1629
clang::CharSourceRange
Represents a character-granular source range.
Definition: SourceLocation.h:236
clang::FixItHint::BeforePreviousInsertions
bool BeforePreviousInsertions
Definition: Diagnostic.h:80
clang::Rewriter::getRangeSize
int getRangeSize(SourceRange Range, RewriteOptions opts=RewriteOptions()) const
getRangeSize - Return the size in bytes of the specified range if they are in the same file...
Definition: Rewriter.cpp:165
clang::ComparisonCategoryType::Last
clang::FixItRewriter::buffer_end
iterator buffer_end()
Definition: FixItRewriter.h:105
clang::FixItHint::InsertFromRange
CharSourceRange InsertFromRange
Code in the specific range that should be inserted in the insertion location.
Definition: Diagnostic.h:74
clang::FixItRewriter::WriteFixedFile
bool WriteFixedFile(FileID ID, raw_ostream &OS)
Write a single modified source file.
Definition: FixItRewriter.cpp:52
clang::FixItHint::RemoveRange
CharSourceRange RemoveRange
Code that should be replaced to correct the error.
Definition: Diagnostic.h:70
clang::FixItRewriter::FixItRewriter
FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr, const LangOptions &LangOpts, FixItOptions *FixItOpts)
Initialize a new fix-it rewriter.
Definition: FixItRewriter.cpp:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::RewriteBuffer::write
raw_ostream & write(raw_ostream &Stream) const
Write to Stream the result of applying all changes to the original buffer.
Definition: Rewriter.cpp:38
clang::FileEntry::getName
StringRef getName() const
Definition: FileManager.h:85
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:60
clang::Rewriter::getRewriteBufferFor
const RewriteBuffer * getRewriteBufferFor(FileID FID) const
getRewriteBufferFor - Return the rewrite buffer for the specified FileID.
Definition: Rewriter.h:172
clang::FixItRewriter::HandleDiagnostic
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) override
HandleDiagnostic - Handle this diagnostic, reporting it to the user or capturing it to a log as neede...
Definition: FixItRewriter.cpp:127
clang::Rewriter::InsertText
bool InsertText(SourceLocation Loc, StringRef Str, bool InsertAfter=true, bool indentNewLines=false)
InsertText - Insert the specified string at the specified location in the original buffer...
Definition: Rewriter.cpp:242
clang::DiagnosticConsumer::HandleDiagnostic
virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info)
Handle this diagnostic, reporting it to the user or capturing it to a log as needed.
Definition: Diagnostic.cpp:533
clang::CharSourceRange::isTokenRange
bool isTokenRange() const
Return true if the end of this range specifies the start of the last token.
Definition: SourceLocation.h:263
clang::FileID
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Definition: SourceLocation.h:40
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::edit::Commit::isCommitable
bool isCommitable() const
Definition: Commit.h:69
clang::comments::tok::text
Definition: CommentLexer.h:36
clang::edit::Commit
Definition: Commit.h:30
clang::FixItOptions
Definition: FixItRewriter.h:34
clang::FixItRewriter::Diag
void Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic via the adapted diagnostic client.
Definition: FixItRewriter.cpp:198
clang::Diagnostic::getNumFixItHints
unsigned getNumFixItHints() const
Definition: Diagnostic.h:1408
clang::DiagnosticsEngine::Error
Definition: Diagnostic.h:157
clang::FixItRewriter::iterator
Rewriter::buffer_iterator iterator
Definition: FixItRewriter.h:101
clang::CharSourceRange::getEnd
SourceLocation getEnd() const
Definition: SourceLocation.h:267
clang::FixItOptions::~FixItOptions
virtual ~FixItOptions()
clang::Rewriter
Rewriter - This is the main interface to the rewrite buffers.
Definition: Rewriter.h:33
SourceLocation.h
Defines the clang::SourceLocation class and associated facilities.
clang::DiagnosticsEngine::Level
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:152
clang::Diagnostic
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1316
clang::FullSourceLoc
A SourceLocation and its associated SourceManager.
Definition: SourceLocation.h:340
clang::FixItHint
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition: Diagnostic.h:66
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:582
clang::FixItRewriter::IncludeInDiagnosticCounts
bool IncludeInDiagnosticCounts() const override
IncludeInDiagnosticCounts - This method (whose default implementation returns true) indicates whether...
Definition: FixItRewriter.cpp:123