1341825Sdim//===---  BugType.h - Bug Information Description ---------------*- C++ -*-===//
2218887Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6218887Sdim//
7218887Sdim//===----------------------------------------------------------------------===//
8218887Sdim//
9218887Sdim//  This file defines BugType, a class representing a bug type.
10218887Sdim//
11218887Sdim//===----------------------------------------------------------------------===//
12218887Sdim
13280031Sdim#ifndef LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_BUGTYPE_H
14280031Sdim#define LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_BUGTYPE_H
15218887Sdim
16276479Sdim#include "clang/Basic/LLVM.h"
17261991Sdim#include "clang/StaticAnalyzer/Core/BugReporter/CommonBugCategories.h"
18276479Sdim#include "clang/StaticAnalyzer/Core/Checker.h"
19218887Sdim#include <string>
20218887Sdim
21218887Sdimnamespace clang {
22218887Sdim
23218887Sdimnamespace ento {
24218887Sdim
25226633Sdimclass BugReporter;
26218887Sdimclass ExplodedNode;
27218887Sdimclass ExprEngine;
28218887Sdim
29218887Sdimclass BugType {
30218887Sdimprivate:
31360784Sdim  const CheckerNameRef CheckerName;
32360784Sdim  const std::string Description;
33218887Sdim  const std::string Category;
34328381Sdim  const CheckerBase *Checker;
35328381Sdim  bool SuppressOnSink;
36261991Sdim
37261991Sdim  virtual void anchor();
38328381Sdim
39218887Sdimpublic:
40360784Sdim  BugType(CheckerNameRef CheckerName, StringRef Name, StringRef Cat,
41360784Sdim          bool SuppressOnSink = false)
42360784Sdim      : CheckerName(CheckerName), Description(Name), Category(Cat),
43360784Sdim        Checker(nullptr), SuppressOnSink(SuppressOnSink) {}
44353358Sdim  BugType(const CheckerBase *Checker, StringRef Name, StringRef Cat,
45360784Sdim          bool SuppressOnSink = false)
46360784Sdim      : CheckerName(Checker->getCheckerName()), Description(Name),
47360784Sdim        Category(Cat), Checker(Checker), SuppressOnSink(SuppressOnSink) {}
48328381Sdim  virtual ~BugType() = default;
49218887Sdim
50360784Sdim  StringRef getDescription() const { return Description; }
51226633Sdim  StringRef getCategory() const { return Category; }
52360784Sdim  StringRef getCheckerName() const {
53360784Sdim    // FIXME: This is a workaround to ensure that the correct checerk name is
54360784Sdim    // used. The checker names are set after the constructors are run.
55328381Sdim    // In case the BugType object is initialized in the checker's ctor
56360784Sdim    // the CheckerName field will be empty. To circumvent this problem we use
57328381Sdim    // CheckerBase whenever it is possible.
58360784Sdim    StringRef Ret = Checker ? Checker->getCheckerName() : CheckerName;
59360784Sdim    assert(!Ret.empty() && "Checker name is not set properly.");
60360784Sdim    return Ret;
61328381Sdim  }
62276479Sdim
63218887Sdim  /// isSuppressOnSink - Returns true if bug reports associated with this bug
64218887Sdim  ///  type should be suppressed if the end node of the report is post-dominated
65218887Sdim  ///  by a sink node.
66328381Sdim  bool isSuppressOnSink() const { return SuppressOnSink; }
67218887Sdim};
68218887Sdim
69218887Sdimclass BuiltinBug : public BugType {
70261991Sdim  const std::string desc;
71276479Sdim  void anchor() override;
72218887Sdimpublic:
73360784Sdim  BuiltinBug(class CheckerNameRef checker, const char *name,
74360784Sdim             const char *description)
75360784Sdim      : BugType(checker, name, categories::LogicError), desc(description) {}
76276479Sdim
77276479Sdim  BuiltinBug(const CheckerBase *checker, const char *name,
78276479Sdim             const char *description)
79276479Sdim      : BugType(checker, name, categories::LogicError), desc(description) {}
80276479Sdim
81276479Sdim  BuiltinBug(const CheckerBase *checker, const char *name)
82276479Sdim      : BugType(checker, name, categories::LogicError), desc(name) {}
83276479Sdim
84226633Sdim  StringRef getDescription() const { return desc; }
85218887Sdim};
86218887Sdim
87328381Sdim} // end ento namespace
88218887Sdim
89218887Sdim} // end clang namespace
90218887Sdim#endif
91