clang: lib/Frontend/Rewrite/FixItRewriter.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
25#include "llvm/ADT/RewriteBuffer.h"
26#include "llvm/ADT/StringRef.h"
27#include "llvm/Support/FileSystem.h"
28#include "llvm/Support/raw_ostream.h"
29#include
30#include
31#include
32#include <system_error>
33#include
34
35using namespace clang;
36using llvm::RewriteBuffer;
37
41 : Diags(Diags), Editor(SourceMgr, LangOpts), Rewrite(SourceMgr, LangOpts),
42 FixItOpts(FixItOpts) {
46}
47
49 Diags.setClient(Client, Owner.release() != nullptr);
50}
51
54 if (!RewriteBuf) return true;
55 RewriteBuf->write(OS);
56 OS.flush();
57 return false;
58}
59
60namespace {
61
64
65public:
67
68 void insert(SourceLocation loc, StringRef text) override {
69 Rewrite.InsertText(loc, text);
70 }
71
72 void replace(CharSourceRange range, StringRef text) override {
74 }
75};
76
77}
78
80 std::vector<std::pair<std::string, std::string>> *RewrittenFiles) {
81 if (NumFailures > 0 && !FixItOpts->FixWhatYouCan) {
83 return true;
84 }
85
86 RewritesReceiver Rec(Rewrite);
88
90
91
92 Rewrite.overwriteChangedFiles();
93 return false;
94 }
95
98 Rewrite.getSourceMgr().getFileEntryRefForID(I->first);
99 int fd;
102 std::error_code EC;
103 std::unique_ptrllvm::raw\_fd\_ostream OS;
104 if (fd != -1) {
105 OS.reset(new llvm::raw_fd_ostream(fd, true));
106 } else {
107 OS.reset(new llvm::raw_fd_ostream(Filename, EC, llvm::sys::fs::OF_None));
108 }
109 if (EC) {
110 Diags.Report(clang::diag::err_fe_unable_to_open_output) << Filename
111 << EC.message();
112 continue;
113 }
114 RewriteBuffer &RewriteBuf = I->second;
115 RewriteBuf.write(*OS);
116 OS->flush();
117
118 if (RewrittenFiles)
119 RewrittenFiles->push_back(
120 std::make_pair(std::string(Entry->getName()), Filename));
121 }
122
123 return false;
124}
125
128}
129
132
134
135 if (!FixItOpts->Silent ||
140 PrevDiagSilenced = false;
141 } else {
142 PrevDiagSilenced = true;
143 }
144
145
147 return;
148
150 ++NumFailures;
151 return;
152 }
153
154
155
158 Idx < Last; ++Idx) {
160
166 else
168 } else {
172 else
175 }
176 }
178
179 if (!CanRewrite) {
182
183
185 if (++NumFailures == 1)
186 Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
187 }
188 return;
189 }
190
191 if (!Editor.commit(commit)) {
192 ++NumFailures;
194 return;
195 }
196
198}
199
200
202
203
207}
208
Defines the Diagnostic-related interfaces.
Defines the clang::FileManager interface and associated types.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
Represents a character-granular source range.
bool isTokenRange() const
Return true if the end of this range specifies the start of the last token.
SourceLocation getEnd() const
SourceLocation getBegin() const
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.
virtual bool IncludeInDiagnosticCounts() const
Indicates whether the diagnostics handled by this DiagnosticConsumer should be included in the number...
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine a...
const SourceLocation & getLocation() const
unsigned getNumFixItHints() const
const FixItHint & getFixItHint(unsigned Idx) const
Concrete class used by the front-end to report problems and issues.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
void setClient(DiagnosticConsumer *client, bool ShouldOwnClient=true)
Set the diagnostic client associated with this diagnostic object.
std::unique_ptr< DiagnosticConsumer > takeClient()
Return the current diagnostic client along with ownership of that client.
Level
The level of the diagnostic, after it has been through mapping.
DiagnosticConsumer * getClient()
StringRef getName() const
The name of this FileEntry.
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
bool BeforePreviousInsertions
CharSourceRange RemoveRange
Code that should be replaced to correct the error.
CharSourceRange InsertFromRange
Code in the specific range that should be inserted in the insertion location.
std::string CodeToInsert
The actual code to insert at the insertion location, as a string.
bool FixOnlyWarnings
Whether to only fix warnings and not errors.
bool FixWhatYouCan
Whether to abort fixing a file when not all errors could be fixed.
virtual std::string RewriteFilename(const std::string &Filename, int &fd)=0
This file is about to be rewritten.
bool Silent
If true, only pass the diagnostic to the actual diagnostic consumer if it is an error or a fixit was ...
bool InPlace
True if files should be updated in place.
Rewriter::buffer_iterator iterator
void Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic via the adapted diagnostic client.
bool WriteFixedFiles(std::vector< std::pair< std::string, std::string > > *RewrittenFiles=nullptr)
Write the modified source files.
bool WriteFixedFile(FileID ID, raw_ostream &OS)
Write a single modified source file.
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...
FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr, const LangOptions &LangOpts, FixItOptions *FixItOpts)
Initialize a new fix-it rewriter.
~FixItRewriter() override
Destroy the fix-it rewriter.
bool IncludeInDiagnosticCounts() const override
IncludeInDiagnosticCounts - This method (whose default implementation returns true) indicates whether...
A SourceLocation and its associated SourceManager.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Rewriter - This is the main interface to the rewrite buffers.
const llvm::RewriteBuffer * getRewriteBufferFor(FileID FID) const
getRewriteBufferFor - Return the rewrite buffer for the specified FileID.
Encodes a location in the source.
This class handles loading and caching of source files into memory.
bool insertFromRange(SourceLocation loc, CharSourceRange range, bool afterToken=false, bool beforePreviousInsertions=false)
bool insert(SourceLocation loc, StringRef text, bool afterToken=false, bool beforePreviousInsertions=false)
bool isCommitable() const
bool remove(CharSourceRange range)
bool replace(CharSourceRange range, StringRef text)
void applyRewrites(EditsReceiver &receiver, bool adjustRemovals=true)
bool commit(const Commit &commit)
RangeSelector range(RangeSelector Begin, RangeSelector End)
DEPRECATED. Use enclose.
The JSON file list parser is used to communicate input to InstallAPI.
@ Rewrite
We are substituting template parameters for (typically) other template parameters in order to rewrite...