Home | History | Annotate | Download | only in Rewrite
      1 //===--- FixItRewriter.h - 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 #ifndef LLVM_CLANG_REWRITE_FIX_IT_REWRITER_H
     16 #define LLVM_CLANG_REWRITE_FIX_IT_REWRITER_H
     17 
     18 #include "clang/Basic/Diagnostic.h"
     19 #include "clang/Basic/SourceLocation.h"
     20 #include "clang/Rewrite/Rewriter.h"
     21 
     22 namespace llvm { class raw_ostream; }
     23 
     24 namespace clang {
     25 
     26 class SourceManager;
     27 class FileEntry;
     28 
     29 class FixItOptions {
     30 public:
     31   virtual ~FixItOptions();
     32 
     33   /// \brief This file is about to be rewritten. Return the name of the file
     34   /// that is okay to write to.
     35   virtual std::string RewriteFilename(const std::string &Filename) = 0;
     36 
     37   /// \brief Whether to abort fixing a file when not all errors could be fixed.
     38   bool FixWhatYouCan;
     39 };
     40 
     41 class FixItRewriter : public DiagnosticClient {
     42   /// \brief The diagnostics machinery.
     43   Diagnostic &Diags;
     44 
     45   /// \brief The rewriter used to perform the various code
     46   /// modifications.
     47   Rewriter Rewrite;
     48 
     49   /// \brief The diagnostic client that performs the actual formatting
     50   /// of error messages.
     51   DiagnosticClient *Client;
     52 
     53   /// \brief Turn an input path into an output path. NULL implies overwriting
     54   /// the original.
     55   FixItOptions *FixItOpts;
     56 
     57   /// \brief The number of rewriter failures.
     58   unsigned NumFailures;
     59 
     60 public:
     61   typedef Rewriter::buffer_iterator iterator;
     62 
     63   /// \brief Initialize a new fix-it rewriter.
     64   FixItRewriter(Diagnostic &Diags, SourceManager &SourceMgr,
     65                 const LangOptions &LangOpts, FixItOptions *FixItOpts);
     66 
     67   /// \brief Destroy the fix-it rewriter.
     68   ~FixItRewriter();
     69 
     70   /// \brief Check whether there are modifications for a given file.
     71   bool IsModified(FileID ID) const {
     72     return Rewrite.getRewriteBufferFor(ID) != NULL;
     73   }
     74 
     75   // Iteration over files with changes.
     76   iterator buffer_begin() { return Rewrite.buffer_begin(); }
     77   iterator buffer_end() { return Rewrite.buffer_end(); }
     78 
     79   /// \brief Write a single modified source file.
     80   ///
     81   /// \returns true if there was an error, false otherwise.
     82   bool WriteFixedFile(FileID ID, llvm::raw_ostream &OS);
     83 
     84   /// \brief Write the modified source files.
     85   ///
     86   /// \returns true if there was an error, false otherwise.
     87   bool WriteFixedFiles();
     88 
     89   /// IncludeInDiagnosticCounts - This method (whose default implementation
     90   /// returns true) indicates whether the diagnostics handled by this
     91   /// DiagnosticClient should be included in the number of diagnostics
     92   /// reported by Diagnostic.
     93   virtual bool IncludeInDiagnosticCounts() const;
     94 
     95   /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or
     96   /// capturing it to a log as needed.
     97   virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
     98                                 const DiagnosticInfo &Info);
     99 
    100   /// \brief Emit a diagnostic via the adapted diagnostic client.
    101   void Diag(SourceLocation Loc, unsigned DiagID);
    102 };
    103 
    104 }
    105 
    106 #endif // LLVM_CLANG_REWRITE_FIX_IT_REWRITER_H
    107