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/Edit/EditedSource.h"
21 #include "clang/Rewrite/Core/Rewriter.h"
30 FixItOptions() : FixWhatYouCan(false),
31 FixOnlyWarnings(false), Silent(false) { }
33 virtual ~FixItOptions();
35 /// \brief This file is about to be rewritten. Return the name of the file
36 /// that is okay to write to.
38 /// \param fd out parameter for file descriptor. After the call it may be set
39 /// to an open file descriptor for the returned filename, or it will be -1
42 virtual std::string RewriteFilename(const std::string &Filename, int &fd) = 0;
44 /// \brief Whether to abort fixing a file when not all errors could be fixed.
47 /// \brief Whether to only fix warnings and not errors.
50 /// \brief If true, only pass the diagnostic to the actual diagnostic consumer
51 /// if it is an error or a fixit was applied as part of the diagnostic.
52 /// It basically silences warnings without accompanying fixits.
56 class FixItRewriter : public DiagnosticConsumer {
57 /// \brief The diagnostics machinery.
58 DiagnosticsEngine &Diags;
60 edit::EditedSource Editor;
62 /// \brief The rewriter used to perform the various code
66 /// \brief The diagnostic client that performs the actual formatting
67 /// of error messages.
68 DiagnosticConsumer *Client;
71 /// \brief Turn an input path into an output path. NULL implies overwriting
73 FixItOptions *FixItOpts;
75 /// \brief The number of rewriter failures.
78 /// \brief Whether the previous diagnostic was not passed to the consumer.
79 bool PrevDiagSilenced;
82 typedef Rewriter::buffer_iterator iterator;
84 /// \brief Initialize a new fix-it rewriter.
85 FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
86 const LangOptions &LangOpts, FixItOptions *FixItOpts);
88 /// \brief Destroy the fix-it rewriter.
91 /// \brief Check whether there are modifications for a given file.
92 bool IsModified(FileID ID) const {
93 return Rewrite.getRewriteBufferFor(ID) != NULL;
96 // Iteration over files with changes.
97 iterator buffer_begin() { return Rewrite.buffer_begin(); }
98 iterator buffer_end() { return Rewrite.buffer_end(); }
100 /// \brief Write a single modified source file.
102 /// \returns true if there was an error, false otherwise.
103 bool WriteFixedFile(FileID ID, raw_ostream &OS);
105 /// \brief Write the modified source files.
107 /// \returns true if there was an error, false otherwise.
108 bool WriteFixedFiles(
109 std::vector<std::pair<std::string, std::string> > *RewrittenFiles = 0);
111 /// IncludeInDiagnosticCounts - This method (whose default implementation
112 /// returns true) indicates whether the diagnostics handled by this
113 /// DiagnosticConsumer should be included in the number of diagnostics
114 /// reported by DiagnosticsEngine.
115 virtual bool IncludeInDiagnosticCounts() const;
117 /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or
118 /// capturing it to a log as needed.
119 virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
120 const Diagnostic &Info);
122 /// \brief Emit a diagnostic via the adapted diagnostic client.
123 void Diag(SourceLocation Loc, unsigned DiagID);
125 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const;
130 #endif // LLVM_CLANG_REWRITE_FIX_IT_REWRITER_H