summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h
blob: 613e8fb8c579e8aeeb56c820953786dfa225d15c (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//===--- ClangTidyDiagnosticConsumer.h - clang-tidy -------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H

#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Tooling/Refactoring.h"
#include "llvm/ADT/SmallString.h"

namespace clang {

class CompilerInstance;
namespace ast_matchers {
class MatchFinder;
}
namespace tooling {
class CompilationDatabase;
}

namespace tidy {

/// \brief A message from a clang-tidy check.
///
/// Note that this is independent of a \c SourceManager.
struct ClangTidyMessage {
  ClangTidyMessage(StringRef Message = "");
  ClangTidyMessage(StringRef Message, const SourceManager &Sources,
                   SourceLocation Loc);
  std::string Message;
  std::string FilePath;
  unsigned FileOffset;
};

/// \brief A detected error complete with information to display diagnostic and
/// automatic fix.
///
/// This is used as an intermediate format to transport Diagnostics without a
/// dependency on a SourceManager.
///
/// FIXME: Make Diagnostics flexible enough to support this directly.
struct ClangTidyError {
  ClangTidyError(const ClangTidyMessage &Message);

  ClangTidyMessage Message;
  tooling::Replacements Fix;
  SmallVector<ClangTidyMessage, 1> Notes;
};

/// \brief Every \c ClangTidyCheck reports errors through a \c DiagnosticEngine
/// provided by this context.
///
/// A \c ClangTidyCheck always has access to the active context to report
/// warnings like:
/// \code
/// Context->Diag(Loc, "Single-argument constructors must be explicit")
///     << FixItHint::CreateInsertion(Loc, "explicit ");
/// \endcode
class ClangTidyContext {
public:
  ClangTidyContext(SmallVectorImpl<ClangTidyError> *Errors)
      : Errors(Errors), DiagEngine(0) {}

  /// \brief Report any errors detected using this method.
  ///
  /// This is still under heavy development and will likely change towards using
  /// tablegen'd diagnostic IDs.
  /// FIXME: Figure out a way to manage ID spaces.
  DiagnosticBuilder Diag(SourceLocation Loc, StringRef Message);

  /// \brief Sets the \c DiagnosticsEngine so that Diagnostics can be generated
  /// correctly.
  ///
  /// This is called from the \c ClangTidyCheck base class.
  void setDiagnosticsEngine(DiagnosticsEngine *Engine);

  /// \brief Sets the \c SourceManager of the used \c DiagnosticsEngine.
  ///
  /// This is called from the \c ClangTidyCheck base class.
  void setSourceManager(SourceManager *SourceMgr);

private:
  friend class ClangTidyDiagnosticConsumer; // Calls storeError().

  /// \brief Store a \c ClangTidyError.
  void storeError(const ClangTidyError &Error);

  SmallVectorImpl<ClangTidyError> *Errors;
  DiagnosticsEngine *DiagEngine;
};

/// \brief A diagnostic consumer that turns each \c Diagnostic into a
/// \c SourceManager-independent \c ClangTidyError.
//
// FIXME: If we move away from unit-tests, this can be moved to a private
// implementation file.
class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
public:
  ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx) : Context(Ctx) {
    IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
    Diags.reset(new DiagnosticsEngine(
        IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts, this,
        /*ShouldOwnClient=*/false));
    Context.setDiagnosticsEngine(Diags.get());
  }

  // FIXME: The concept of converting between FixItHints and Replacements is
  // more generic and should be pulled out into a more useful Diagnostics
  // library.
  virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
                                const Diagnostic &Info) LLVM_OVERRIDE {
    // FIXME: Ensure that we don't get notes from user code related to errors
    // from non-user code.
    if (Diags->getSourceManager().isInSystemHeader(Info.getLocation()))
      return;
    if (DiagLevel != DiagnosticsEngine::Note) {
      Errors.push_back(ClangTidyError(getMessage(Info)));
    } else {
      assert(!Errors.empty() &&
             "A diagnostic note can only be appended to a message.");
      Errors.back().Notes.push_back(getMessage(Info));
    }
    addFixes(Info, Errors.back());
  }

  // Flushes the internal diagnostics buffer to the ClangTidyContext.
  virtual void finish() LLVM_OVERRIDE {
    for (unsigned i = 0, e = Errors.size(); i != e; ++i) {
      Context.storeError(Errors[i]);
    }
    Errors.clear();
  }

private:
  void addFixes(const Diagnostic &Info, ClangTidyError &Error) {
    if (!Info.hasSourceManager())
      return;
    SourceManager &SourceMgr = Info.getSourceManager();
    tooling::Replacements Replacements;
    for (unsigned i = 0, e = Info.getNumFixItHints(); i != e; ++i) {
      Error.Fix.insert(tooling::Replacement(
          SourceMgr, Info.getFixItHint(i).RemoveRange.getBegin(), 0,
          Info.getFixItHint(i).CodeToInsert));
    }
  }

  ClangTidyMessage getMessage(const Diagnostic &Info) const {
    SmallString<100> Buf;
    Info.FormatDiagnostic(Buf);
    if (!Info.hasSourceManager()) {
      return ClangTidyMessage(Buf.str());
    }
    return ClangTidyMessage(Buf.str(), Info.getSourceManager(),
                            Info.getLocation());
  }

  ClangTidyContext &Context;
  OwningPtr<DiagnosticsEngine> Diags;
  SmallVector<ClangTidyError, 8> Errors;
};

} // end namespace tidy
} // end namespace clang

#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H
OpenPOWER on IntegriCloud