1 //===--- FixItRewriter.h - Fix-It Rewriter Diagnostic Client ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_REWRITE_FIX_IT_REWRITER_H
16 #define LLVM_CLANG_REWRITE_FIX_IT_REWRITER_H
18 #include "clang/Basic/Diagnostic.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "clang/Rewrite/Rewriter.h"
21 #include "llvm/ADT/SmallVector.h"
23 namespace llvm { class raw_ostream; }
32 virtual ~FixItOptions();
34 /// \brief This file is about to be rewritten. Return the name of the file
35 /// that is okay to write to.
36 virtual std::string RewriteFilename(const std::string &Filename) = 0;
38 /// \brief Whether to abort fixing a file when not all errors could be fixed.
42 class FixItRewriter : public DiagnosticClient {
43 /// \brief The diagnostics machinery.
46 /// \brief The rewriter used to perform the various code
50 /// \brief The diagnostic client that performs the actual formatting
51 /// of error messages.
52 DiagnosticClient *Client;
54 /// \brief Turn an input path into an output path. NULL implies overwriting
56 FixItOptions *FixItOpts;
58 /// \brief The number of rewriter failures.
62 typedef Rewriter::buffer_iterator iterator;
64 /// \brief Initialize a new fix-it rewriter.
65 FixItRewriter(Diagnostic &Diags, SourceManager &SourceMgr,
66 const LangOptions &LangOpts, FixItOptions *FixItOpts);
68 /// \brief Destroy the fix-it rewriter.
71 /// \brief Check whether there are modifications for a given file.
72 bool IsModified(FileID ID) const {
73 return Rewrite.getRewriteBufferFor(ID) != NULL;
76 // Iteration over files with changes.
77 iterator buffer_begin() { return Rewrite.buffer_begin(); }
78 iterator buffer_end() { return Rewrite.buffer_end(); }
80 /// \brief Write a single modified source file.
82 /// \returns true if there was an error, false otherwise.
83 bool WriteFixedFile(FileID ID, llvm::raw_ostream &OS);
85 /// \brief Write the modified source files.
87 /// \returns true if there was an error, false otherwise.
88 bool WriteFixedFiles();
90 /// IncludeInDiagnosticCounts - This method (whose default implementation
91 /// returns true) indicates whether the diagnostics handled by this
92 /// DiagnosticClient should be included in the number of diagnostics
93 /// reported by Diagnostic.
94 virtual bool IncludeInDiagnosticCounts() const;
96 /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or
97 /// capturing it to a log as needed.
98 virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
99 const DiagnosticInfo &Info);
101 /// \brief Emit a diagnostic via the adapted diagnostic client.
102 void Diag(SourceLocation Loc, unsigned DiagID);
107 #endif // LLVM_CLANG_REWRITE_FIX_IT_REWRITER_H