]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/clang/Rewrite/FixItRewriter.h
Vendor import of clang trunk r126079:
[FreeBSD/FreeBSD.git] / include / clang / Rewrite / FixItRewriter.h
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 #ifndef LLVM_CLANG_REWRITE_FIX_IT_REWRITER_H
16 #define LLVM_CLANG_REWRITE_FIX_IT_REWRITER_H
17
18 #include "clang/Basic/Diagnostic.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "clang/Rewrite/Rewriter.h"
21 #include "llvm/ADT/SmallVector.h"
22
23 namespace llvm { class raw_ostream; }
24
25 namespace clang {
26
27 class SourceManager;
28 class FileEntry;
29
30 class FixItOptions {
31 public:
32   virtual ~FixItOptions();
33
34   /// \brief This file is about to be rewritten. Return the name of the file
35   /// that is okay to write to.
36   virtual std::string RewriteFilename(const std::string &Filename) = 0;
37
38   /// \brief Whether to abort fixing a file when not all errors could be fixed.
39   bool FixWhatYouCan;
40 };
41
42 class FixItRewriter : public DiagnosticClient {
43   /// \brief The diagnostics machinery.
44   Diagnostic &Diags;
45
46   /// \brief The rewriter used to perform the various code
47   /// modifications.
48   Rewriter Rewrite;
49
50   /// \brief The diagnostic client that performs the actual formatting
51   /// of error messages.
52   DiagnosticClient *Client;
53
54   /// \brief Turn an input path into an output path. NULL implies overwriting
55   /// the original.
56   FixItOptions *FixItOpts;
57
58   /// \brief The number of rewriter failures.
59   unsigned NumFailures;
60
61 public:
62   typedef Rewriter::buffer_iterator iterator;
63
64   /// \brief Initialize a new fix-it rewriter.
65   FixItRewriter(Diagnostic &Diags, SourceManager &SourceMgr,
66                 const LangOptions &LangOpts, FixItOptions *FixItOpts);
67
68   /// \brief Destroy the fix-it rewriter.
69   ~FixItRewriter();
70
71   /// \brief Check whether there are modifications for a given file.
72   bool IsModified(FileID ID) const {
73     return Rewrite.getRewriteBufferFor(ID) != NULL;
74   }
75
76   // Iteration over files with changes.
77   iterator buffer_begin() { return Rewrite.buffer_begin(); }
78   iterator buffer_end() { return Rewrite.buffer_end(); }
79
80   /// \brief Write a single modified source file.
81   ///
82   /// \returns true if there was an error, false otherwise.
83   bool WriteFixedFile(FileID ID, llvm::raw_ostream &OS);
84
85   /// \brief Write the modified source files.
86   ///
87   /// \returns true if there was an error, false otherwise.
88   bool WriteFixedFiles();
89
90   /// IncludeInDiagnosticCounts - This method (whose default implementation
91   /// returns true) indicates whether the diagnostics handled by this
92   /// DiagnosticClient should be included in the number of diagnostics
93   /// reported by Diagnostic.
94   virtual bool IncludeInDiagnosticCounts() const;
95
96   /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or
97   /// capturing it to a log as needed.
98   virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
99                                 const DiagnosticInfo &Info);
100
101   /// \brief Emit a diagnostic via the adapted diagnostic client.
102   void Diag(SourceLocation Loc, unsigned DiagID);
103 };
104
105 }
106
107 #endif // LLVM_CLANG_REWRITE_FIX_IT_REWRITER_H