clang  8.0.0svn
FixItRewriter.h
Go to the documentation of this file.
1 //===- FixItRewriter.h - Fix-It Rewriter Diagnostic Client ------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_REWRITE_FRONTEND_FIXITREWRITER_H
17 #define LLVM_CLANG_REWRITE_FRONTEND_FIXITREWRITER_H
18 
19 #include "clang/Basic/Diagnostic.h"
20 #include "clang/Basic/LLVM.h"
24 #include <memory>
25 #include <string>
26 #include <utility>
27 #include <vector>
28 
29 namespace clang {
30 
31 class LangOptions;
32 class SourceManager;
33 
34 class FixItOptions {
35 public:
36  FixItOptions() = default;
37  virtual ~FixItOptions();
38 
39  /// This file is about to be rewritten. Return the name of the file
40  /// that is okay to write to.
41  ///
42  /// \param fd out parameter for file descriptor. After the call it may be set
43  /// to an open file descriptor for the returned filename, or it will be -1
44  /// otherwise.
45  virtual std::string RewriteFilename(const std::string &Filename, int &fd) = 0;
46 
47  /// True if files should be updated in place. RewriteFilename is only called
48  /// if this is false.
49  bool InPlace = false;
50 
51  /// Whether to abort fixing a file when not all errors could be fixed.
52  bool FixWhatYouCan = false;
53 
54  /// Whether to only fix warnings and not errors.
55  bool FixOnlyWarnings = false;
56 
57  /// If true, only pass the diagnostic to the actual diagnostic consumer
58  /// if it is an error or a fixit was applied as part of the diagnostic.
59  /// It basically silences warnings without accompanying fixits.
60  bool Silent = false;
61 };
62 
64  /// The diagnostics machinery.
65  DiagnosticsEngine &Diags;
66 
67  edit::EditedSource Editor;
68 
69  /// The rewriter used to perform the various code
70  /// modifications.
71  Rewriter Rewrite;
72 
73  /// The diagnostic client that performs the actual formatting
74  /// of error messages.
75  DiagnosticConsumer *Client;
76  std::unique_ptr<DiagnosticConsumer> Owner;
77 
78  /// Turn an input path into an output path. NULL implies overwriting
79  /// the original.
80  FixItOptions *FixItOpts;
81 
82  /// The number of rewriter failures.
83  unsigned NumFailures = 0;
84 
85  /// Whether the previous diagnostic was not passed to the consumer.
86  bool PrevDiagSilenced = false;
87 
88 public:
89  /// Initialize a new fix-it rewriter.
91  const LangOptions &LangOpts, FixItOptions *FixItOpts);
92 
93  /// Destroy the fix-it rewriter.
94  ~FixItRewriter() override;
95 
96  /// Check whether there are modifications for a given file.
97  bool IsModified(FileID ID) const {
98  return Rewrite.getRewriteBufferFor(ID) != nullptr;
99  }
100 
102 
103  // Iteration over files with changes.
104  iterator buffer_begin() { return Rewrite.buffer_begin(); }
105  iterator buffer_end() { return Rewrite.buffer_end(); }
106 
107  /// Write a single modified source file.
108  ///
109  /// \returns true if there was an error, false otherwise.
110  bool WriteFixedFile(FileID ID, raw_ostream &OS);
111 
112  /// Write the modified source files.
113  ///
114  /// \returns true if there was an error, false otherwise.
115  bool WriteFixedFiles(
116  std::vector<std::pair<std::string, std::string>> *RewrittenFiles = nullptr);
117 
118  /// IncludeInDiagnosticCounts - This method (whose default implementation
119  /// returns true) indicates whether the diagnostics handled by this
120  /// DiagnosticConsumer should be included in the number of diagnostics
121  /// reported by DiagnosticsEngine.
122  bool IncludeInDiagnosticCounts() const override;
123 
124  /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or
125  /// capturing it to a log as needed.
126  void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
127  const Diagnostic &Info) override;
128 
129  /// Emit a diagnostic via the adapted diagnostic client.
130  void Diag(SourceLocation Loc, unsigned DiagID);
131 };
132 
133 } // namespace clang
134 
135 #endif // LLVM_CLANG_REWRITE_FRONTEND_FIXITREWRITER_H
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
bool FixOnlyWarnings
Whether to only fix warnings and not errors.
Definition: FixItRewriter.h:55
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Definition: Diagnostic.h:1487
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:50
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:60
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:149
Defines the Diagnostic-related interfaces.
buffer_iterator buffer_end()
Definition: Rewriter.h:180
StringRef Filename
Definition: Format.cpp:1620
FixItOptions()=default
Encodes a location in the source.
bool FixWhatYouCan
Whether to abort fixing a file when not all errors could be fixed.
Definition: FixItRewriter.h:52
const RewriteBuffer * getRewriteBufferFor(FileID FID) const
getRewriteBufferFor - Return the rewrite buffer for the specified FileID.
Definition: Rewriter.h:172
std::map< FileID, RewriteBuffer >::iterator buffer_iterator
Definition: Rewriter.h:55
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Dataflow Directional Tag Classes.
Rewriter::buffer_iterator iterator
virtual ~FixItOptions()
buffer_iterator buffer_begin()
Definition: Rewriter.h:179
Rewriter - This is the main interface to the rewrite buffers.
Definition: Rewriter.h:33
Defines the clang::SourceLocation class and associated facilities.
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:152
bool IsModified(FileID ID) const
Check whether there are modifications for a given file.
Definition: FixItRewriter.h:97
virtual std::string RewriteFilename(const std::string &Filename, int &fd)=0
This file is about to be rewritten.
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1315
bool InPlace
True if files should be updated in place.
Definition: FixItRewriter.h:49
This class handles loading and caching of source files into memory.