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"
29 virtual ~FixItOptions();
31 /// \brief This file is about to be rewritten. Return the name of the file
32 /// that is okay to write to.
33 virtual std::string RewriteFilename(const std::string &Filename) = 0;
35 /// \brief Whether to abort fixing a file when not all errors could be fixed.
39 class FixItRewriter : public DiagnosticConsumer {
40 /// \brief The diagnostics machinery.
41 DiagnosticsEngine &Diags;
43 /// \brief The rewriter used to perform the various code
47 /// \brief The diagnostic client that performs the actual formatting
48 /// of error messages.
49 DiagnosticConsumer *Client;
51 /// \brief Turn an input path into an output path. NULL implies overwriting
53 FixItOptions *FixItOpts;
55 /// \brief The number of rewriter failures.
59 typedef Rewriter::buffer_iterator iterator;
61 /// \brief Initialize a new fix-it rewriter.
62 FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
63 const LangOptions &LangOpts, FixItOptions *FixItOpts);
65 /// \brief Destroy the fix-it rewriter.
68 /// \brief Check whether there are modifications for a given file.
69 bool IsModified(FileID ID) const {
70 return Rewrite.getRewriteBufferFor(ID) != NULL;
73 // Iteration over files with changes.
74 iterator buffer_begin() { return Rewrite.buffer_begin(); }
75 iterator buffer_end() { return Rewrite.buffer_end(); }
77 /// \brief Write a single modified source file.
79 /// \returns true if there was an error, false otherwise.
80 bool WriteFixedFile(FileID ID, raw_ostream &OS);
82 /// \brief Write the modified source files.
84 /// \returns true if there was an error, false otherwise.
85 bool WriteFixedFiles();
87 /// IncludeInDiagnosticCounts - This method (whose default implementation
88 /// returns true) indicates whether the diagnostics handled by this
89 /// DiagnosticConsumer should be included in the number of diagnostics
90 /// reported by DiagnosticsEngine.
91 virtual bool IncludeInDiagnosticCounts() const;
93 /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or
94 /// capturing it to a log as needed.
95 virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
96 const Diagnostic &Info);
98 /// \brief Emit a diagnostic via the adapted diagnostic client.
99 void Diag(SourceLocation Loc, unsigned DiagID);
101 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const;
106 #endif // LLVM_CLANG_REWRITE_FIX_IT_REWRITER_H