26 #include "llvm/ADT/StringRef.h" 27 #include "llvm/Support/FileSystem.h" 28 #include "llvm/Support/raw_ostream.h" 32 #include <system_error> 35 using namespace clang;
40 : Diags(Diags), Editor(SourceMgr, LangOpts), Rewrite(SourceMgr, LangOpts),
41 FixItOpts(FixItOpts) {
48 Diags.
setClient(Client, Owner.release() !=
nullptr);
53 if (!RewriteBuf)
return true;
54 RewriteBuf->
write(OS);
65 RewritesReceiver(
Rewriter &Rewrite) : Rewrite(Rewrite) {}
79 std::vector<std::pair<std::string, std::string>> *RewrittenFiles) {
80 if (NumFailures > 0 && !FixItOpts->FixWhatYouCan) {
85 RewritesReceiver Rec(Rewrite);
86 Editor.applyRewrites(Rec);
88 if (FixItOpts->InPlace) {
91 Rewrite.overwriteChangedFiles();
96 const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first);
98 std::string
Filename = FixItOpts->RewriteFilename(Entry->
getName(), fd);
100 std::unique_ptr<llvm::raw_fd_ostream> OS;
102 OS.reset(
new llvm::raw_fd_ostream(fd,
true));
104 OS.reset(
new llvm::raw_fd_ostream(Filename, EC, llvm::sys::fs::F_None));
107 Diags.Report(clang::diag::err_fe_unable_to_open_output) << Filename
112 RewriteBuf.
write(*OS);
123 return Client ? Client->IncludeInDiagnosticCounts() :
true;
131 if (!FixItOpts->Silent ||
135 Client->HandleDiagnostic(DiagLevel, Info);
136 PrevDiagSilenced =
false;
138 PrevDiagSilenced =
true;
181 if (++NumFailures == 1)
187 if (!Editor.commit(commit)) {
201 Diags.setClient(Client,
false);
203 Diags.Report(Loc, DiagID);
204 Diags.setClient(
this,
false);
bool remove(CharSourceRange range)
DiagnosticConsumer * getClient()
Defines the clang::FileManager interface and associated types.
Defines the SourceManager interface.
std::string CodeToInsert
The actual code to insert at the insertion location, as a string.
bool insertFromRange(SourceLocation loc, CharSourceRange range, bool afterToken=false, bool beforePreviousInsertions=false)
std::unique_ptr< DiagnosticConsumer > takeClient()
Return the current diagnostic client along with ownership of that client.
RewriteBuffer - As code is rewritten, SourceBuffer's from the original input with modifications get a...
bool WriteFixedFiles(std::vector< std::pair< std::string, std::string >> *RewrittenFiles=nullptr)
Write the modified source files.
bool insert(SourceLocation loc, StringRef text, bool afterToken=false, bool beforePreviousInsertions=false)
void setClient(DiagnosticConsumer *client, bool ShouldOwnClient=true)
Set the diagnostic client associated with this diagnostic object.
SourceLocation getBegin() const
const FixItHint & getFixItHint(unsigned Idx) const
bool ReplaceText(SourceLocation Start, unsigned OrigLength, StringRef NewStr)
ReplaceText - This method replaces a range of characters in the input buffer with a new string...
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
bool replace(CharSourceRange range, StringRef text)
const SourceLocation & getLocation() const
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
Concrete class used by the front-end to report problems and issues.
~FixItRewriter() override
Destroy the fix-it rewriter.
Defines the Diagnostic-related interfaces.
Represents a character-granular source range.
bool BeforePreviousInsertions
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...
CharSourceRange InsertFromRange
Code in the specific range that should be inserted in the insertion location.
bool WriteFixedFile(FileID ID, raw_ostream &OS)
Write a single modified source file.
CharSourceRange RemoveRange
Code that should be replaced to correct the error.
FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr, const LangOptions &LangOpts, FixItOptions *FixItOpts)
Initialize a new fix-it rewriter.
Encodes a location in the source.
raw_ostream & write(raw_ostream &Stream) const
Write to Stream the result of applying all changes to the original buffer.
StringRef getName() const
Cached information about one file (either on disk or in the virtual file system). ...
const RewriteBuffer * getRewriteBufferFor(FileID FID) const
getRewriteBufferFor - Return the rewrite buffer for the specified FileID.
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...
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...
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.
bool isTokenRange() const
Return true if the end of this range specifies the start of the last token.
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Dataflow Directional Tag Classes.
bool isCommitable() const
void Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic via the adapted diagnostic client.
unsigned getNumFixItHints() const
Rewriter::buffer_iterator iterator
SourceLocation getEnd() const
Rewriter - This is the main interface to the rewrite buffers.
Defines the clang::SourceLocation class and associated facilities.
Level
The level of the diagnostic, after it has been through mapping.
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
A SourceLocation and its associated SourceManager.
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
This class handles loading and caching of source files into memory.
bool IncludeInDiagnosticCounts() const override
IncludeInDiagnosticCounts - This method (whose default implementation returns true) indicates whether...