Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / unittests / StaticAnalyzer / ConflictingEvalCallsTest.cpp
blobd3eb4b7a94d3290e0f1598ea6e46e100990b183b
1 //===----------------------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "CheckerRegistration.h"
10 #include "clang/StaticAnalyzer/Core/Checker.h"
11 #include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
12 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
13 #include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h"
14 #include "gtest/gtest.h"
16 using namespace clang;
17 using namespace ento;
19 namespace {
20 class EvalCallBase : public Checker<eval::Call> {
21 const CallDescription Foo = {{"foo"}, 0};
23 public:
24 bool evalCall(const CallEvent &Call, CheckerContext &C) const {
25 return Foo.matches(Call);
29 class EvalCallFoo1 : public EvalCallBase {};
30 class EvalCallFoo2 : public EvalCallBase {};
31 void addEvalFooCheckers(AnalysisASTConsumer &AnalysisConsumer,
32 AnalyzerOptions &AnOpts) {
33 AnOpts.CheckersAndPackages = {{"test.EvalFoo1", true},
34 {"test.EvalFoo2", true}};
35 AnalysisConsumer.AddCheckerRegistrationFn([](CheckerRegistry &Registry) {
36 Registry.addChecker<EvalCallFoo1>("test.EvalFoo1", "EmptyDescription",
37 "EmptyDocsUri");
38 Registry.addChecker<EvalCallFoo2>("test.EvalFoo2", "EmptyDescription",
39 "EmptyDocsUri");
40 });
42 } // namespace
44 TEST(EvalCall, DetectConflictingEvalCalls) {
45 #ifdef NDEBUG
46 GTEST_SKIP() << "This test is only available for debug builds.";
47 #endif
48 const std::string Code = R"code(
49 void foo();
50 void top() {
51 foo(); // crash
53 )code";
54 constexpr auto Regex =
55 "The 'foo\\(\\)' call has been already evaluated by the test\\.EvalFoo1 "
56 "checker, while the test\\.EvalFoo2 checker also tried to evaluate the "
57 "same call\\. At most one checker supposed to evaluate a call\\.";
58 ASSERT_DEATH(runCheckerOnCode<addEvalFooCheckers>(Code), Regex);