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