Home | History | Annotate | Download | only in Rewrite
      1 //===--- FixItRewriter.cpp - Fix-It Rewriter Diagnostic Client --*- C++ -*-===//
      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/FileManager.h"
     18 #include "clang/Basic/SourceLocation.h"
     19 #include "clang/Basic/SourceManager.h"
     20 #include "clang/Edit/Commit.h"
     21 #include "clang/Edit/EditsReceiver.h"
     22 #include "clang/Frontend/FrontendDiagnostic.h"
     23 #include "llvm/Support/Path.h"
     24 #include "llvm/Support/raw_ostream.h"
     25 #include <cstdio>
     26 #include <memory>
     27 
     28 using namespace clang;
     29 
     30 FixItRewriter::FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
     31                              const LangOptions &LangOpts,
     32                              FixItOptions *FixItOpts)
     33   : Diags(Diags),
     34     Editor(SourceMgr, LangOpts),
     35     Rewrite(SourceMgr, LangOpts),
     36     FixItOpts(FixItOpts),
     37     NumFailures(0),
     38     PrevDiagSilenced(false) {
     39   Owner = Diags.takeClient();
     40   Client = Diags.getClient();
     41   Diags.setClient(this, false);
     42 }
     43 
     44 FixItRewriter::~FixItRewriter() {
     45   Diags.setClient(Client, Owner.release() != nullptr);
     46 }
     47 
     48 bool FixItRewriter::WriteFixedFile(FileID ID, raw_ostream &OS) {
     49   const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID);
     50   if (!RewriteBuf) return true;
     51   RewriteBuf->write(OS);
     52   OS.flush();
     53   return false;
     54 }
     55 
     56 namespace {
     57 
     58 class RewritesReceiver : public edit::EditsReceiver {
     59   Rewriter &Rewrite;
     60 
     61 public:
     62   RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) { }
     63 
     64   void insert(SourceLocation loc, StringRef text) override {
     65     Rewrite.InsertText(loc, text);
     66   }
     67   void replace(CharSourceRange range, StringRef text) override {
     68     Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text);
     69   }
     70 };
     71 
     72 }
     73 
     74 bool FixItRewriter::WriteFixedFiles(
     75             std::vector<std::pair<std::string, std::string> > *RewrittenFiles) {
     76   if (NumFailures > 0 && !FixItOpts->FixWhatYouCan) {
     77     Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
     78     return true;
     79   }
     80 
     81   RewritesReceiver Rec(Rewrite);
     82   Editor.applyRewrites(Rec);
     83 
     84   if (FixItOpts->InPlace) {
     85     // Overwriting open files on Windows is tricky, but the rewriter can do it
     86     // for us.
     87     Rewrite.overwriteChangedFiles();
     88     return false;
     89   }
     90 
     91   for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
     92     const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first);
     93     int fd;
     94     std::string Filename = FixItOpts->RewriteFilename(Entry->getName(), fd);
     95     std::error_code EC;
     96     std::unique_ptr<llvm::raw_fd_ostream> OS;
     97     if (fd != -1) {
     98       OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
     99     } else {
    100       OS.reset(new llvm::raw_fd_ostream(Filename, EC, llvm::sys::fs::F_None));
    101     }
    102     if (EC) {
    103       Diags.Report(clang::diag::err_fe_unable_to_open_output) << Filename
    104                                                               << EC.message();
    105       continue;
    106     }
    107     RewriteBuffer &RewriteBuf = I->second;
    108     RewriteBuf.write(*OS);
    109     OS->flush();
    110 
    111     if (RewrittenFiles)
    112       RewrittenFiles->push_back(std::make_pair(Entry->getName(), Filename));
    113   }
    114 
    115   return false;
    116 }
    117 
    118 bool FixItRewriter::IncludeInDiagnosticCounts() const {
    119   return Client ? Client->IncludeInDiagnosticCounts() : true;
    120 }
    121 
    122 void FixItRewriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
    123                                      const Diagnostic &Info) {
    124   // Default implementation (Warnings/errors count).
    125   DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
    126 
    127   if (!FixItOpts->Silent ||
    128       DiagLevel >= DiagnosticsEngine::Error ||
    129       (DiagLevel == DiagnosticsEngine::Note && !PrevDiagSilenced) ||
    130       (DiagLevel > DiagnosticsEngine::Note && Info.getNumFixItHints())) {
    131     Client->HandleDiagnostic(DiagLevel, Info);
    132     PrevDiagSilenced = false;
    133   } else {
    134     PrevDiagSilenced = true;
    135   }
    136 
    137   // Skip over any diagnostics that are ignored or notes.
    138   if (DiagLevel <= DiagnosticsEngine::Note)
    139     return;
    140   // Skip over errors if we are only fixing warnings.
    141   if (DiagLevel >= DiagnosticsEngine::Error && FixItOpts->FixOnlyWarnings) {
    142     ++NumFailures;
    143     return;
    144   }
    145 
    146   // Make sure that we can perform all of the modifications we
    147   // in this diagnostic.
    148   edit::Commit commit(Editor);
    149   for (unsigned Idx = 0, Last = Info.getNumFixItHints();
    150        Idx < Last; ++Idx) {
    151     const FixItHint &Hint = Info.getFixItHint(Idx);
    152 
    153     if (Hint.CodeToInsert.empty()) {
    154       if (Hint.InsertFromRange.isValid())
    155         commit.insertFromRange(Hint.RemoveRange.getBegin(),
    156                            Hint.InsertFromRange, /*afterToken=*/false,
    157                            Hint.BeforePreviousInsertions);
    158       else
    159         commit.remove(Hint.RemoveRange);
    160     } else {
    161       if (Hint.RemoveRange.isTokenRange() ||
    162           Hint.RemoveRange.getBegin() != Hint.RemoveRange.getEnd())
    163         commit.replace(Hint.RemoveRange, Hint.CodeToInsert);
    164       else
    165         commit.insert(Hint.RemoveRange.getBegin(), Hint.CodeToInsert,
    166                     /*afterToken=*/false, Hint.BeforePreviousInsertions);
    167     }
    168   }
    169   bool CanRewrite = Info.getNumFixItHints() > 0 && commit.isCommitable();
    170 
    171   if (!CanRewrite) {
    172     if (Info.getNumFixItHints() > 0)
    173       Diag(Info.getLocation(), diag::note_fixit_in_macro);
    174 
    175     // If this was an error, refuse to perform any rewriting.
    176     if (DiagLevel >= DiagnosticsEngine::Error) {
    177       if (++NumFailures == 1)
    178         Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
    179     }
    180     return;
    181   }
    182 
    183   if (!Editor.commit(commit)) {
    184     ++NumFailures;
    185     Diag(Info.getLocation(), diag::note_fixit_failed);
    186     return;
    187   }
    188 
    189   Diag(Info.getLocation(), diag::note_fixit_applied);
    190 }
    191 
    192 /// \brief Emit a diagnostic via the adapted diagnostic client.
    193 void FixItRewriter::Diag(SourceLocation Loc, unsigned DiagID) {
    194   // When producing this diagnostic, we temporarily bypass ourselves,
    195   // clear out any current diagnostic, and let the downstream client
    196   // format the diagnostic.
    197   Diags.setClient(Client, false);
    198   Diags.Clear();
    199   Diags.Report(Loc, DiagID);
    200   Diags.setClient(this, false);
    201 }
    202 
    203 FixItOptions::~FixItOptions() {}
    204