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 /// \brief 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; 209