blob: f8ac6efdf31253554b0ad39f78b77818a3cdc49a [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"
Daniel Jasperd07c8402013-07-29 08:19:24 +000016
17namespace clang {
18
19class CompilerInstance;
20namespace ast_matchers {
21class MatchFinder;
22}
23namespace tooling {
24class CompilationDatabase;
25}
26
27namespace tidy {
28
Manuel Klimek814f9bd2013-11-14 15:49:44 +000029/// \brief A message from a clang-tidy check.
30///
31/// Note that this is independent of a \c SourceManager.
32struct ClangTidyMessage {
33 ClangTidyMessage(StringRef Message = "");
34 ClangTidyMessage(StringRef Message, const SourceManager &Sources,
35 SourceLocation Loc);
36 std::string Message;
37 std::string FilePath;
38 unsigned FileOffset;
39};
40
41/// \brief A detected error complete with information to display diagnostic and
42/// automatic fix.
43///
44/// This is used as an intermediate format to transport Diagnostics without a
45/// dependency on a SourceManager.
46///
47/// FIXME: Make Diagnostics flexible enough to support this directly.
48struct ClangTidyError {
49 ClangTidyError(const ClangTidyMessage &Message);
50
51 ClangTidyMessage Message;
52 tooling::Replacements Fix;
53 SmallVector<ClangTidyMessage, 1> Notes;
54};
55
56/// \brief Every \c ClangTidyCheck reports errors through a \c DiagnosticEngine
57/// provided by this context.
58///
59/// A \c ClangTidyCheck always has access to the active context to report
60/// warnings like:
61/// \code
62/// Context->Diag(Loc, "Single-argument constructors must be explicit")
63/// << FixItHint::CreateInsertion(Loc, "explicit ");
64/// \endcode
65class ClangTidyContext {
66public:
Alexander Kornienko175fefb2014-01-03 09:31:57 +000067 ClangTidyContext(SmallVectorImpl<ClangTidyError> *Errors)
68 : Errors(Errors), DiagEngine(0) {}
Manuel Klimek814f9bd2013-11-14 15:49:44 +000069
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:
Alexander Kornienko0ba86b72014-01-09 16:31:25 +0000105 ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000106
107 // FIXME: The concept of converting between FixItHints and Replacements is
108 // more generic and should be pulled out into a more useful Diagnostics
109 // library.
110 virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
Alexander Kornienko0ba86b72014-01-09 16:31:25 +0000111 const Diagnostic &Info) LLVM_OVERRIDE;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000112
Alexander Kornienkofc589872014-01-03 15:34:40 +0000113 // Flushes the internal diagnostics buffer to the ClangTidyContext.
Alexander Kornienko0ba86b72014-01-09 16:31:25 +0000114 virtual void finish() LLVM_OVERRIDE;
Daniel Jasperd07c8402013-07-29 08:19:24 +0000115
116private:
Alexander Kornienko0ba86b72014-01-09 16:31:25 +0000117 void addFixes(const Diagnostic &Info, ClangTidyError &Error);
118 ClangTidyMessage getMessage(const Diagnostic &Info) const;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000119
Daniel Jasperd07c8402013-07-29 08:19:24 +0000120 ClangTidyContext &Context;
121 OwningPtr<DiagnosticsEngine> Diags;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000122 SmallVector<ClangTidyError, 8> Errors;
Daniel Jasperd07c8402013-07-29 08:19:24 +0000123};
124
125} // end namespace tidy
126} // end namespace clang
127
128#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H