clang  8.0.0svn
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 
17 #include "clang/Basic/Diagnostic.h"
19 #include "clang/Basic/LLVM.h"
22 #include "clang/Edit/Commit.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 
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 
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 
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 
124  return Client ? Client->IncludeInDiagnosticCounts() : true;
125 }
126 
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,
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;
bool remove(CharSourceRange range)
Definition: Commit.cpp:92
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:507
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.
Definition: Diagnostic.h:78
bool insertFromRange(SourceLocation loc, CharSourceRange range, bool afterToken=false, bool beforePreviousInsertions=false)
Definition: Commit.cpp:65
std::unique_ptr< DiagnosticConsumer > takeClient()
Return the current diagnostic client along with ownership of that client.
Definition: Diagnostic.h:515
RewriteBuffer - As code is rewritten, SourceBuffer&#39;s from the original input with modifications get a...
Definition: RewriteBuffer.h:26
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)
Definition: Commit.cpp:49
void setClient(DiagnosticConsumer *client, bool ShouldOwnClient=true)
Set the diagnostic client associated with this diagnostic object.
Definition: Diagnostic.cpp:100
SourceLocation getBegin() const
const FixItHint & getFixItHint(unsigned Idx) const
Definition: Diagnostic.h:1409
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
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:50
bool replace(CharSourceRange range, StringRef text)
Definition: Commit.cpp:117
const SourceLocation & getLocation() const
Definition: Diagnostic.h:1324
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.
Definition: Diagnostic.h:149
~FixItRewriter() override
Destroy the fix-it rewriter.
Defines the Diagnostic-related interfaces.
StringRef Filename
Definition: Format.cpp:1629
Represents a character-granular source range.
bool BeforePreviousInsertions
Definition: Diagnostic.h:80
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
CharSourceRange InsertFromRange
Code in the specific range that should be inserted in the insertion location.
Definition: Diagnostic.h:74
bool WriteFixedFile(FileID ID, raw_ostream &OS)
Write a single modified source file.
CharSourceRange RemoveRange
Code that should be replaced to correct the error.
Definition: Diagnostic.h:70
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.
Definition: Rewriter.cpp:38
StringRef getName() const
Definition: FileManager.h:86
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:60
const RewriteBuffer * getRewriteBufferFor(FileID FID) const
getRewriteBufferFor - Return the rewrite buffer for the specified FileID.
Definition: Rewriter.h:172
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...
Definition: Rewriter.cpp:242
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
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
Definition: Commit.h:69
void Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic via the adapted diagnostic client.
unsigned getNumFixItHints() const
Definition: Diagnostic.h:1405
Rewriter::buffer_iterator iterator
SourceLocation getEnd() const
virtual ~FixItOptions()
Rewriter - This is the main interface to the rewrite buffers.
Definition: Rewriter.h:33
Defines the clang::SourceLocation class and associated facilities.
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:152
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1313
A SourceLocation and its associated SourceManager.
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition: Diagnostic.h:66
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...