blob: f5e2857f66f53c0db4367bd98b17ac8684337e3b [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"
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +000016#include "llvm/ADT/DenseMap.h"
Daniel Jasperd07c8402013-07-29 08:19:24 +000017
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 {
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +000050 ClangTidyError(StringRef CheckName, const ClangTidyMessage &Message);
Manuel Klimek814f9bd2013-11-14 15:49:44 +000051
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +000052 std::string CheckName;
Manuel Klimek814f9bd2013-11-14 15:49:44 +000053 ClangTidyMessage Message;
54 tooling::Replacements Fix;
55 SmallVector<ClangTidyMessage, 1> Notes;
56};
57
58/// \brief Every \c ClangTidyCheck reports errors through a \c DiagnosticEngine
59/// provided by this context.
60///
61/// A \c ClangTidyCheck always has access to the active context to report
62/// warnings like:
63/// \code
64/// Context->Diag(Loc, "Single-argument constructors must be explicit")
65/// << FixItHint::CreateInsertion(Loc, "explicit ");
66/// \endcode
67class ClangTidyContext {
68public:
Alexander Kornienko175fefb2014-01-03 09:31:57 +000069 ClangTidyContext(SmallVectorImpl<ClangTidyError> *Errors)
70 : Errors(Errors), DiagEngine(0) {}
Manuel Klimek814f9bd2013-11-14 15:49:44 +000071
72 /// \brief Report any errors detected using this method.
73 ///
74 /// This is still under heavy development and will likely change towards using
75 /// tablegen'd diagnostic IDs.
76 /// FIXME: Figure out a way to manage ID spaces.
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +000077 DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc,
78 StringRef Message);
Manuel Klimek814f9bd2013-11-14 15:49:44 +000079
80 /// \brief Sets the \c DiagnosticsEngine so that Diagnostics can be generated
81 /// correctly.
82 ///
83 /// This is called from the \c ClangTidyCheck base class.
84 void setDiagnosticsEngine(DiagnosticsEngine *Engine);
85
86 /// \brief Sets the \c SourceManager of the used \c DiagnosticsEngine.
87 ///
88 /// This is called from the \c ClangTidyCheck base class.
89 void setSourceManager(SourceManager *SourceMgr);
90
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +000091 /// \brief Returns the name of the clang-tidy check which produced this
92 /// diagnostic ID.
93 StringRef getCheckName(unsigned DiagnosticID) const;
94
Manuel Klimek814f9bd2013-11-14 15:49:44 +000095private:
96 friend class ClangTidyDiagnosticConsumer; // Calls storeError().
97
98 /// \brief Store a \c ClangTidyError.
99 void storeError(const ClangTidyError &Error);
100
101 SmallVectorImpl<ClangTidyError> *Errors;
102 DiagnosticsEngine *DiagEngine;
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +0000103 llvm::DenseMap<unsigned, std::string> CheckNamesByDiagnosticID;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000104};
105
Daniel Jasperd07c8402013-07-29 08:19:24 +0000106/// \brief A diagnostic consumer that turns each \c Diagnostic into a
107/// \c SourceManager-independent \c ClangTidyError.
108//
109// FIXME: If we move away from unit-tests, this can be moved to a private
110// implementation file.
111class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
112public:
Alexander Kornienko0ba86b72014-01-09 16:31:25 +0000113 ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000114
115 // FIXME: The concept of converting between FixItHints and Replacements is
116 // more generic and should be pulled out into a more useful Diagnostics
117 // library.
118 virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
Alexander Kornienko0ba86b72014-01-09 16:31:25 +0000119 const Diagnostic &Info) LLVM_OVERRIDE;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000120
Alexander Kornienkofc589872014-01-03 15:34:40 +0000121 // Flushes the internal diagnostics buffer to the ClangTidyContext.
Alexander Kornienko0ba86b72014-01-09 16:31:25 +0000122 virtual void finish() LLVM_OVERRIDE;
Daniel Jasperd07c8402013-07-29 08:19:24 +0000123
124private:
Alexander Kornienko0ba86b72014-01-09 16:31:25 +0000125 void addFixes(const Diagnostic &Info, ClangTidyError &Error);
126 ClangTidyMessage getMessage(const Diagnostic &Info) const;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000127
Daniel Jasperd07c8402013-07-29 08:19:24 +0000128 ClangTidyContext &Context;
129 OwningPtr<DiagnosticsEngine> Diags;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000130 SmallVector<ClangTidyError, 8> Errors;
Daniel Jasperd07c8402013-07-29 08:19:24 +0000131};
132
133} // end namespace tidy
134} // end namespace clang
135
136#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H