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"
22 namespace llvm { class raw_ostream; }
31 virtual ~FixItOptions();
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;
37 /// \brief Whether to abort fixing a file when not all errors could be fixed.
41 class FixItRewriter : public DiagnosticClient {
42 /// \brief The diagnostics machinery.
45 /// \brief The rewriter used to perform the various code
49 /// \brief The diagnostic client that performs the actual formatting
50 /// of error messages.
51 DiagnosticClient *Client;
53 /// \brief Turn an input path into an output path. NULL implies overwriting
55 FixItOptions *FixItOpts;
57 /// \brief The number of rewriter failures.
61 typedef Rewriter::buffer_iterator iterator;
63 /// \brief Initialize a new fix-it rewriter.
64 FixItRewriter(Diagnostic &Diags, SourceManager &SourceMgr,
65 const LangOptions &LangOpts, FixItOptions *FixItOpts);
67 /// \brief Destroy the fix-it rewriter.
70 /// \brief Check whether there are modifications for a given file.
71 bool IsModified(FileID ID) const {
72 return Rewrite.getRewriteBufferFor(ID) != NULL;
75 // Iteration over files with changes.
76 iterator buffer_begin() { return Rewrite.buffer_begin(); }
77 iterator buffer_end() { return Rewrite.buffer_end(); }
79 /// \brief Write a single modified source file.
81 /// \returns true if there was an error, false otherwise.
82 bool WriteFixedFile(FileID ID, llvm::raw_ostream &OS);
84 /// \brief Write the modified source files.
86 /// \returns true if there was an error, false otherwise.
87 bool WriteFixedFiles();
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;
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);
100 /// \brief Emit a diagnostic via the adapted diagnostic client.
101 void Diag(SourceLocation Loc, unsigned DiagID);
106 #endif // LLVM_CLANG_REWRITE_FIX_IT_REWRITER_H