58 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 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"15 16using namespace clang;17using namespace ento;18 19namespace {20class EvalCallBase : public Checker<eval::Call> {21 const CallDescription Foo = {CDM::SimpleFunc, {"foo"}, 0};22 23public:24 bool evalCall(const CallEvent &Call, CheckerContext &C) const {25 return Foo.matches(Call);26 }27};28 29class EvalCallFoo1 : public EvalCallBase {};30class EvalCallFoo2 : public EvalCallBase {};31void 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", "MockDescription");37 Registry.addChecker<EvalCallFoo2>("test.EvalFoo2", "MockDescription");38 });39}40} // namespace41 42TEST(EvalCall, DetectConflictingEvalCalls) {43#ifdef NDEBUG44 GTEST_SKIP() << "This test is only available for debug builds.";45#endif46 const std::string Code = R"code(47 void foo();48 void top() {49 foo(); // crash50 }51 )code";52 constexpr auto Regex =53 "The 'foo\\(\\)' call has been already evaluated by the test\\.EvalFoo1 "54 "checker, while the test\\.EvalFoo2 checker also tried to evaluate the "55 "same call\\. At most one checker supposed to evaluate a call\\.";56 ASSERT_DEATH(runCheckerOnCode<addEvalFooCheckers>(Code), Regex);57}58