1 //===- FixItRewriter.h - Fix-It Rewriter Diagnostic Client ------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This is a diagnostic client adaptor that performs rewrites as
10 // suggested by code modification hints attached to diagnostics. It
11 // then forwards any diagnostics to the adapted diagnostic client.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_REWRITE_FRONTEND_FIXITREWRITER_H
16 #define LLVM_CLANG_REWRITE_FRONTEND_FIXITREWRITER_H
18 #include "clang/Basic/Diagnostic.h"
19 #include "clang/Basic/LLVM.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "clang/Edit/EditedSource.h"
22 #include "clang/Rewrite/Core/Rewriter.h"
35 FixItOptions() = default;
36 virtual ~FixItOptions();
38 /// This file is about to be rewritten. Return the name of the file
39 /// that is okay to write to.
41 /// \param fd out parameter for file descriptor. After the call it may be set
42 /// to an open file descriptor for the returned filename, or it will be -1
44 virtual std::string RewriteFilename(const std::string &Filename, int &fd) = 0;
46 /// True if files should be updated in place. RewriteFilename is only called
50 /// Whether to abort fixing a file when not all errors could be fixed.
51 bool FixWhatYouCan = false;
53 /// Whether to only fix warnings and not errors.
54 bool FixOnlyWarnings = false;
56 /// If true, only pass the diagnostic to the actual diagnostic consumer
57 /// if it is an error or a fixit was applied as part of the diagnostic.
58 /// It basically silences warnings without accompanying fixits.
62 class FixItRewriter : public DiagnosticConsumer {
63 /// The diagnostics machinery.
64 DiagnosticsEngine &Diags;
66 edit::EditedSource Editor;
68 /// The rewriter used to perform the various code
72 /// The diagnostic client that performs the actual formatting
73 /// of error messages.
74 DiagnosticConsumer *Client;
75 std::unique_ptr<DiagnosticConsumer> Owner;
77 /// Turn an input path into an output path. NULL implies overwriting
79 FixItOptions *FixItOpts;
81 /// The number of rewriter failures.
82 unsigned NumFailures = 0;
84 /// Whether the previous diagnostic was not passed to the consumer.
85 bool PrevDiagSilenced = false;
88 /// Initialize a new fix-it rewriter.
89 FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
90 const LangOptions &LangOpts, FixItOptions *FixItOpts);
92 /// Destroy the fix-it rewriter.
93 ~FixItRewriter() override;
95 /// Check whether there are modifications for a given file.
96 bool IsModified(FileID ID) const {
97 return Rewrite.getRewriteBufferFor(ID) != nullptr;
100 using iterator = Rewriter::buffer_iterator;
102 // Iteration over files with changes.
103 iterator buffer_begin() { return Rewrite.buffer_begin(); }
104 iterator buffer_end() { return Rewrite.buffer_end(); }
106 /// Write a single modified source file.
108 /// \returns true if there was an error, false otherwise.
109 bool WriteFixedFile(FileID ID, raw_ostream &OS);
111 /// Write the modified source files.
113 /// \returns true if there was an error, false otherwise.
114 bool WriteFixedFiles(
115 std::vector<std::pair<std::string, std::string>> *RewrittenFiles = nullptr);
117 /// IncludeInDiagnosticCounts - This method (whose default implementation
118 /// returns true) indicates whether the diagnostics handled by this
119 /// DiagnosticConsumer should be included in the number of diagnostics
120 /// reported by DiagnosticsEngine.
121 bool IncludeInDiagnosticCounts() const override;
123 /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or
124 /// capturing it to a log as needed.
125 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
126 const Diagnostic &Info) override;
128 /// Emit a diagnostic via the adapted diagnostic client.
129 void Diag(SourceLocation Loc, unsigned DiagID);
134 #endif // LLVM_CLANG_REWRITE_FRONTEND_FIXITREWRITER_H