aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm/tools/clang/include/clang/Rewrite/FixItRewriter.h
blob: bf7e7911c97719582e370634e4988b37d4bbdb70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//===--- FixItRewriter.h - Fix-It Rewriter Diagnostic Client ----*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This is a diagnostic client adaptor that performs rewrites as
// suggested by code modification hints attached to diagnostics. It
// then forwards any diagnostics to the adapted diagnostic client.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_REWRITE_FIX_IT_REWRITER_H
#define LLVM_CLANG_REWRITE_FIX_IT_REWRITER_H

#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Rewrite/Rewriter.h"

namespace clang {

class SourceManager;
class FileEntry;

class FixItOptions {
public:
  virtual ~FixItOptions();

  /// \brief This file is about to be rewritten. Return the name of the file
  /// that is okay to write to.
  virtual std::string RewriteFilename(const std::string &Filename) = 0;

  /// \brief Whether to abort fixing a file when not all errors could be fixed.
  bool FixWhatYouCan;
};

class FixItRewriter : public DiagnosticConsumer {
  /// \brief The diagnostics machinery.
  DiagnosticsEngine &Diags;

  /// \brief The rewriter used to perform the various code
  /// modifications.
  Rewriter Rewrite;

  /// \brief The diagnostic client that performs the actual formatting
  /// of error messages.
  DiagnosticConsumer *Client;

  /// \brief Turn an input path into an output path. NULL implies overwriting
  /// the original.
  FixItOptions *FixItOpts;

  /// \brief The number of rewriter failures.
  unsigned NumFailures;

public:
  typedef Rewriter::buffer_iterator iterator;

  /// \brief Initialize a new fix-it rewriter.
  FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
                const LangOptions &LangOpts, FixItOptions *FixItOpts);

  /// \brief Destroy the fix-it rewriter.
  ~FixItRewriter();

  /// \brief Check whether there are modifications for a given file.
  bool IsModified(FileID ID) const {
    return Rewrite.getRewriteBufferFor(ID) != NULL;
  }

  // Iteration over files with changes.
  iterator buffer_begin() { return Rewrite.buffer_begin(); }
  iterator buffer_end() { return Rewrite.buffer_end(); }

  /// \brief Write a single modified source file.
  ///
  /// \returns true if there was an error, false otherwise.
  bool WriteFixedFile(FileID ID, raw_ostream &OS);

  /// \brief Write the modified source files.
  ///
  /// \returns true if there was an error, false otherwise.
  bool WriteFixedFiles();

  /// IncludeInDiagnosticCounts - This method (whose default implementation
  /// returns true) indicates whether the diagnostics handled by this
  /// DiagnosticConsumer should be included in the number of diagnostics
  /// reported by DiagnosticsEngine.
  virtual bool IncludeInDiagnosticCounts() const;

  /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or
  /// capturing it to a log as needed.
  virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
                                const Diagnostic &Info);

  /// \brief Emit a diagnostic via the adapted diagnostic client.
  void Diag(SourceLocation Loc, unsigned DiagID);
  
  DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const;
};

}

#endif // LLVM_CLANG_REWRITE_FIX_IT_REWRITER_H