clang  13.0.0git
FixItRewriter.h
Go to the documentation of this file.
1 //===- FixItRewriter.h - Fix-It Rewriter Diagnostic Client ------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_REWRITE_FRONTEND_FIXITREWRITER_H
16 #define LLVM_CLANG_REWRITE_FRONTEND_FIXITREWRITER_H
17 
18 #include "clang/Basic/Diagnostic.h"
19 #include "clang/Basic/LLVM.h"
23 #include <memory>
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 namespace clang {
29 
30 class LangOptions;
31 class SourceManager;
32 
33 class FixItOptions {
34 public:
35  FixItOptions() = default;
36  virtual ~FixItOptions();
37 
38  /// This file is about to be rewritten. Return the name of the file
39  /// that is okay to write to.
40  ///
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
43  /// otherwise.
44  virtual std::string RewriteFilename(const std::string &Filename, int &fd) = 0;
45 
46  /// True if files should be updated in place. RewriteFilename is only called
47  /// if this is false.
48  bool InPlace = false;
49 
50  /// Whether to abort fixing a file when not all errors could be fixed.
51  bool FixWhatYouCan = false;
52 
53  /// Whether to only fix warnings and not errors.
54  bool FixOnlyWarnings = false;
55 
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.
59  bool Silent = false;
60 };
61 
63  /// The diagnostics machinery.
64  DiagnosticsEngine &Diags;
65 
66  edit::EditedSource Editor;
67 
68  /// The rewriter used to perform the various code
69  /// modifications.
70  Rewriter Rewrite;
71 
72  /// The diagnostic client that performs the actual formatting
73  /// of error messages.
74  DiagnosticConsumer *Client;
75  std::unique_ptr<DiagnosticConsumer> Owner;
76 
77  /// Turn an input path into an output path. NULL implies overwriting
78  /// the original.
79  FixItOptions *FixItOpts;
80 
81  /// The number of rewriter failures.
82  unsigned NumFailures = 0;
83 
84  /// Whether the previous diagnostic was not passed to the consumer.
85  bool PrevDiagSilenced = false;
86 
87 public:
88  /// Initialize a new fix-it rewriter.
90  const LangOptions &LangOpts, FixItOptions *FixItOpts);
91 
92  /// Destroy the fix-it rewriter.
93  ~FixItRewriter() override;
94 
95  /// Check whether there are modifications for a given file.
96  bool IsModified(FileID ID) const {
97  return Rewrite.getRewriteBufferFor(ID) != nullptr;
98  }
99 
101 
102  // Iteration over files with changes.
103  iterator buffer_begin() { return Rewrite.buffer_begin(); }
104  iterator buffer_end() { return Rewrite.buffer_end(); }
105 
106  /// Write a single modified source file.
107  ///
108  /// \returns true if there was an error, false otherwise.
109  bool WriteFixedFile(FileID ID, raw_ostream &OS);
110 
111  /// Write the modified source files.
112  ///
113  /// \returns true if there was an error, false otherwise.
114  bool WriteFixedFiles(
115  std::vector<std::pair<std::string, std::string>> *RewrittenFiles = nullptr);
116 
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;
122 
123  /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or
124  /// capturing it to a log as needed.
126  const Diagnostic &Info) override;
127 
128  /// Emit a diagnostic via the adapted diagnostic client.
129  void Diag(SourceLocation Loc, unsigned DiagID);
130 };
131 
132 } // namespace clang
133 
134 #endif // LLVM_CLANG_REWRITE_FRONTEND_FIXITREWRITER_H
clang::FixItRewriter::buffer_end
iterator buffer_end()
Definition: FixItRewriter.h:104
clang::DiagnosticsEngine::Level
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:194
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::FixItRewriter::~FixItRewriter
~FixItRewriter() override
Destroy the fix-it rewriter.
Definition: FixItRewriter.cpp:47
clang::FixItOptions::InPlace
bool InPlace
True if files should be updated in place.
Definition: FixItRewriter.h:48
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:89
clang::FixItRewriter::WriteFixedFile
bool WriteFixedFile(FileID ID, raw_ostream &OS)
Write a single modified source file.
Definition: FixItRewriter.cpp:51
clang::FixItOptions::FixWhatYouCan
bool FixWhatYouCan
Whether to abort fixing a file when not all errors could be fixed.
Definition: FixItRewriter.h:51
clang::DiagnosticConsumer
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Definition: Diagnostic.h:1707
clang::TemplateSubstitutionKind::Rewrite
@ Rewrite
We are substituting template parameters for (typically) other template parameters in order to rewrite...
clang::FixItRewriter::IncludeInDiagnosticCounts
bool IncludeInDiagnosticCounts() const override
IncludeInDiagnosticCounts - This method (whose default implementation returns true) indicates whether...
Definition: FixItRewriter.cpp:124
Filename
StringRef Filename
Definition: Format.cpp:2149
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:191
clang::FixItRewriter::buffer_begin
iterator buffer_begin()
Definition: FixItRewriter.h:103
clang::FixItOptions::FixItOptions
FixItOptions()=default
clang::FixItOptions::FixOnlyWarnings
bool FixOnlyWarnings
Whether to only fix warnings and not errors.
Definition: FixItRewriter.h:54
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:624
clang::edit::EditedSource
Definition: EditedSource.h:35
clang::Diagnostic
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1532
Diagnostic.h
clang::FixItRewriter::IsModified
bool IsModified(FileID ID) const
Check whether there are modifications for a given file.
Definition: FixItRewriter.h:96
clang::FixItOptions::Silent
bool Silent
If true, only pass the diagnostic to the actual diagnostic consumer if it is an error or a fixit was ...
Definition: FixItRewriter.h:59
SourceLocation.h
clang::FixItOptions::~FixItOptions
virtual ~FixItOptions()
LLVM.h
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:58
clang::FixItRewriter
Definition: FixItRewriter.h:62
clang::FixItOptions::RewriteFilename
virtual std::string RewriteFilename(const std::string &Filename, int &fd)=0
This file is about to be rewritten.
clang::FixItRewriter::iterator
Rewriter::buffer_iterator iterator
Definition: FixItRewriter.h:100
clang::Builtin::ID
ID
Definition: Builtins.h:47
clang
Dataflow Directional Tag Classes.
Definition: CalledOnceCheck.h:17
clang::Rewriter::buffer_iterator
std::map< FileID, RewriteBuffer >::iterator buffer_iterator
Definition: Rewriter.h:65
clang::FixItRewriter::Diag
void Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic via the adapted diagnostic client.
Definition: FixItRewriter.cpp:199
clang::FileID
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Definition: SourceLocation.h:41
clang::Rewriter
Rewriter - This is the main interface to the rewrite buffers.
Definition: Rewriter.h:32
clang::FixItRewriter::FixItRewriter
FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr, const LangOptions &LangOpts, FixItOptions *FixItOpts)
Initialize a new fix-it rewriter.
Definition: FixItRewriter.cpp:37
clang::FixItOptions
Definition: FixItRewriter.h:33
Rewriter.h
clang::FixItRewriter::WriteFixedFiles
bool WriteFixedFiles(std::vector< std::pair< std::string, std::string >> *RewrittenFiles=nullptr)
Write the modified source files.
Definition: FixItRewriter.cpp:78
EditedSource.h
clang::FixItRewriter::HandleDiagnostic
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) override
HandleDiagnostic - Handle this diagnostic, reporting it to the user or capturing it to a log as neede...
Definition: FixItRewriter.cpp:128