blob: 074f3e19ad11f6edf2f9ac7661795bca2e9cdb8d [file] [log] [blame]
Daniel Jasperd07c8402013-07-29 08:19:24 +00001//===--- ClangTidyDiagnosticConsumer.h - clang-tidy -------------*- 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#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H
11#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H
12
13#include "clang/Basic/Diagnostic.h"
14#include "clang/Basic/SourceManager.h"
15#include "clang/Tooling/Refactoring.h"
16#include "llvm/ADT/SmallString.h"
17
18namespace clang {
19
20class CompilerInstance;
21namespace ast_matchers {
22class MatchFinder;
23}
24namespace tooling {
25class CompilationDatabase;
26}
27
28namespace tidy {
29
Manuel Klimek814f9bd2013-11-14 15:49:44 +000030/// \brief A message from a clang-tidy check.
31///
32/// Note that this is independent of a \c SourceManager.
33struct ClangTidyMessage {
34 ClangTidyMessage(StringRef Message = "");
35 ClangTidyMessage(StringRef Message, const SourceManager &Sources,
36 SourceLocation Loc);
37 std::string Message;
38 std::string FilePath;
39 unsigned FileOffset;
40};
41
42/// \brief A detected error complete with information to display diagnostic and
43/// automatic fix.
44///
45/// This is used as an intermediate format to transport Diagnostics without a
46/// dependency on a SourceManager.
47///
48/// FIXME: Make Diagnostics flexible enough to support this directly.
49struct ClangTidyError {
50 ClangTidyError(const ClangTidyMessage &Message);
51
52 ClangTidyMessage Message;
53 tooling::Replacements Fix;
54 SmallVector<ClangTidyMessage, 1> Notes;
55};
56
57/// \brief Every \c ClangTidyCheck reports errors through a \c DiagnosticEngine
58/// provided by this context.
59///
60/// A \c ClangTidyCheck always has access to the active context to report
61/// warnings like:
62/// \code
63/// Context->Diag(Loc, "Single-argument constructors must be explicit")
64/// << FixItHint::CreateInsertion(Loc, "explicit ");
65/// \endcode
66class ClangTidyContext {
67public:
68 ClangTidyContext(SmallVectorImpl<ClangTidyError> *Errors) : Errors(Errors) {}
69
70 /// \brief Report any errors detected using this method.
71 ///
72 /// This is still under heavy development and will likely change towards using
73 /// tablegen'd diagnostic IDs.
74 /// FIXME: Figure out a way to manage ID spaces.
75 DiagnosticBuilder Diag(SourceLocation Loc, StringRef Message);
76
77 /// \brief Sets the \c DiagnosticsEngine so that Diagnostics can be generated
78 /// correctly.
79 ///
80 /// This is called from the \c ClangTidyCheck base class.
81 void setDiagnosticsEngine(DiagnosticsEngine *Engine);
82
83 /// \brief Sets the \c SourceManager of the used \c DiagnosticsEngine.
84 ///
85 /// This is called from the \c ClangTidyCheck base class.
86 void setSourceManager(SourceManager *SourceMgr);
87
88private:
89 friend class ClangTidyDiagnosticConsumer; // Calls storeError().
90
91 /// \brief Store a \c ClangTidyError.
92 void storeError(const ClangTidyError &Error);
93
94 SmallVectorImpl<ClangTidyError> *Errors;
95 DiagnosticsEngine *DiagEngine;
96};
97
Daniel Jasperd07c8402013-07-29 08:19:24 +000098/// \brief A diagnostic consumer that turns each \c Diagnostic into a
99/// \c SourceManager-independent \c ClangTidyError.
100//
101// FIXME: If we move away from unit-tests, this can be moved to a private
102// implementation file.
103class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
104public:
105 ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx) : Context(Ctx) {
106 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
107 Diags.reset(new DiagnosticsEngine(
108 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts, this,
109 /*ShouldOwnClient=*/false));
110 Context.setDiagnosticsEngine(Diags.get());
111 }
112
113 // FIXME: The concept of converting between FixItHints and Replacements is
114 // more generic and should be pulled out into a more useful Diagnostics
115 // library.
116 virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
117 const Diagnostic &Info) {
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000118 if (DiagLevel != DiagnosticsEngine::Note) {
119 Errors.push_back(ClangTidyError(getMessage(Info)));
120 } else {
121 Errors.back().Notes.push_back(getMessage(Info));
Daniel Jasperd07c8402013-07-29 08:19:24 +0000122 }
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000123 addFixes(Info, Errors.back());
124 }
125
126 virtual void finish() {
127 for (unsigned i = 0, e = Errors.size(); i != e; ++i) {
128 Context.storeError(Errors[i]);
129 }
Daniel Jasperd07c8402013-07-29 08:19:24 +0000130 }
131
132private:
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000133 void addFixes(const Diagnostic &Info, ClangTidyError &Error) {
134 if (!Info.hasSourceManager())
135 return;
136 SourceManager &SourceMgr = Info.getSourceManager();
137 tooling::Replacements Replacements;
138 for (unsigned i = 0, e = Info.getNumFixItHints(); i != e; ++i) {
139 Error.Fix.insert(tooling::Replacement(
140 SourceMgr, Info.getFixItHint(i).RemoveRange.getBegin(), 0,
141 Info.getFixItHint(i).CodeToInsert));
142 }
143 }
144
145 ClangTidyMessage getMessage(const Diagnostic &Info) const {
146 SmallString<100> Buf;
147 Info.FormatDiagnostic(Buf);
148 if (!Info.hasSourceManager()) {
149 return ClangTidyMessage(Buf.str());
150 }
151 return ClangTidyMessage(Buf.str(), Info.getSourceManager(),
152 Info.getLocation());
153 }
154
Daniel Jasperd07c8402013-07-29 08:19:24 +0000155 ClangTidyContext &Context;
156 OwningPtr<DiagnosticsEngine> Diags;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000157 SmallVector<ClangTidyError, 8> Errors;
Daniel Jasperd07c8402013-07-29 08:19:24 +0000158};
159
160} // end namespace tidy
161} // end namespace clang
162
163#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H