clang 22.0.0git
BugType.h
Go to the documentation of this file.
1//===--- BugType.h - Bug Information Description ---------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines BugType, a class representing a bug type.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_BUGTYPE_H
14#define LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_BUGTYPE_H
15
16#include "clang/Basic/LLVM.h"
19#include <string>
20#include <variant>
21
22namespace clang {
23
24namespace ento {
25
26class BugReporter;
27
28class BugType {
29private:
30 using CheckerNameInfo = std::variant<CheckerNameRef, const CheckerFrontend *>;
31
32 const CheckerNameInfo CheckerName;
33 const std::string Description;
34 const std::string Category;
35 bool SuppressOnSink;
36
37 virtual void anchor();
38
39public:
40 // Straightforward constructor where the checker name is specified directly.
41 // TODO: As far as I know all applications of this constructor involve ugly
42 // hacks that could be avoided by switching to the other constructor.
43 // When those are all eliminated, this constructor should be removed to
44 // eliminate the `variant` and simplify this class.
45 BugType(CheckerNameRef CheckerName, StringRef Desc,
46 StringRef Cat = categories::LogicError, bool SuppressOnSink = false)
47 : CheckerName(CheckerName), Description(Desc), Category(Cat),
48 SuppressOnSink(SuppressOnSink) {}
49 // Constructor that can be called from the constructor of a checker object.
50 // At that point the checker name is not yet available, but we can save a
51 // pointer to the checker and use that to query the name.
52 BugType(const CheckerFrontend *Checker, StringRef Desc,
53 StringRef Cat = categories::LogicError, bool SuppressOnSink = false)
54 : CheckerName(Checker), Description(Desc), Category(Cat),
55 SuppressOnSink(SuppressOnSink) {}
56 virtual ~BugType() = default;
57
58 StringRef getDescription() const { return Description; }
59 StringRef getCategory() const { return Category; }
60 StringRef getCheckerName() const {
61 if (const auto *CNR = std::get_if<CheckerNameRef>(&CheckerName))
62 return *CNR;
63
64 return std::get<const CheckerFrontend *>(CheckerName)->getName();
65 }
66
67 /// isSuppressOnSink - Returns true if bug reports associated with this bug
68 /// type should be suppressed if the end node of the report is post-dominated
69 /// by a sink node.
70 bool isSuppressOnSink() const { return SuppressOnSink; }
71};
72
73/// Trivial convenience class for the common case when a certain checker
74/// frontend always uses the same bug type. This way instead of writing
75/// ```
76/// CheckerFrontend LongCheckerFrontendName;
77/// BugType LongCheckerFrontendNameBug{LongCheckerFrontendName, "..."};
78/// ```
79/// we can use `CheckerFrontendWithBugType LongCheckerFrontendName{"..."}`.
81public:
83 StringRef Cat = categories::LogicError,
84 bool SuppressOnSink = false)
85 : BugType(this, Desc, Cat, SuppressOnSink) {}
86};
87
88} // namespace ento
89
90} // end clang namespace
91#endif
int Category
Definition: Format.cpp:3180
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
bool isSuppressOnSink() const
isSuppressOnSink - Returns true if bug reports associated with this bug type should be suppressed if ...
Definition: BugType.h:70
BugType(CheckerNameRef CheckerName, StringRef Desc, StringRef Cat=categories::LogicError, bool SuppressOnSink=false)
Definition: BugType.h:45
StringRef getCategory() const
Definition: BugType.h:59
BugType(const CheckerFrontend *Checker, StringRef Desc, StringRef Cat=categories::LogicError, bool SuppressOnSink=false)
Definition: BugType.h:52
StringRef getDescription() const
Definition: BugType.h:58
virtual ~BugType()=default
StringRef getCheckerName() const
Definition: BugType.h:60
Trivial convenience class for the common case when a certain checker frontend always uses the same bu...
Definition: BugType.h:80
CheckerFrontendWithBugType(StringRef Desc, StringRef Cat=categories::LogicError, bool SuppressOnSink=false)
Definition: BugType.h:82
A CheckerFrontend instance is what the user recognizes as "one checker": it has a public canonical na...
Definition: Checker.h:514
This wrapper is used to ensure that only StringRefs originating from the CheckerRegistry are used as ...
Simple checker classes that implement one frontend (i.e.
Definition: Checker.h:553
The JSON file list parser is used to communicate input to InstallAPI.