| 1 |
//===- unittest/StaticAnalyzer/AnalyzerOptionsTest.cpp - SA Options test --===// |
| 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 |
#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" |
| 10 |
#include "clang/StaticAnalyzer/Core/Checker.h" |
| 11 |
#include "gtest/gtest.h" |
| 12 |
|
| 13 |
namespace clang { |
| 14 |
namespace ento { |
| 15 |
|
| 16 |
TEST(StaticAnalyzerOptions, getRegisteredCheckers) { |
| 17 |
auto IsDebugChecker = [](StringRef CheckerName) { |
| 18 |
return CheckerName.starts_with("debug"); |
| 19 |
}; |
| 20 |
auto IsAlphaChecker = [](StringRef CheckerName) { |
| 21 |
return CheckerName.starts_with("alpha"); |
| 22 |
}; |
| 23 |
const auto &AllCheckers = |
| 24 |
AnalyzerOptions::getRegisteredCheckers(/*IncludeExperimental=*/true); |
| 25 |
EXPECT_FALSE(llvm::any_of(AllCheckers, IsDebugChecker)); |
| 26 |
EXPECT_TRUE(llvm::any_of(AllCheckers, IsAlphaChecker)); |
| 27 |
|
| 28 |
const auto &StableCheckers = AnalyzerOptions::getRegisteredCheckers(); |
| 29 |
EXPECT_FALSE(llvm::any_of(StableCheckers, IsDebugChecker)); |
| 30 |
EXPECT_FALSE(llvm::any_of(StableCheckers, IsAlphaChecker)); |
| 31 |
} |
| 32 |
|
| 33 |
TEST(StaticAnalyzerOptions, SearchInParentPackageTests) { |
| 34 |
AnalyzerOptions Opts; |
| 35 |
Opts.Config["Outer.Inner.CheckerOne:Option"] = "true"; |
| 36 |
Opts.Config["Outer.Inner:Option"] = "false"; |
| 37 |
Opts.Config["Outer.Inner:Option2"] = "true"; |
| 38 |
Opts.Config["Outer:Option2"] = "false"; |
| 39 |
|
| 40 |
StringRef CheckerOneName = "Outer.Inner.CheckerOne"; |
| 41 |
StringRef CheckerTwoName = "Outer.Inner.CheckerTwo"; |
| 42 |
|
| 43 |
// CheckerTwo one has Option specified as true. It should read true regardless |
| 44 |
// of search mode. |
| 45 |
EXPECT_TRUE(Opts.getCheckerBooleanOption(CheckerOneName, "Option")); |
| 46 |
// The package option is overridden with a checker option. |
| 47 |
EXPECT_TRUE(Opts.getCheckerBooleanOption(CheckerOneName, "Option", true)); |
| 48 |
// The Outer package option is overridden by the Inner package option. No |
| 49 |
// package option is specified. |
| 50 |
EXPECT_TRUE(Opts.getCheckerBooleanOption(CheckerOneName, "Option2", true)); |
| 51 |
// No package option is specified and search in packages is turned off. We |
| 52 |
// should assert here, but we can't test that. |
| 53 |
//Opts.getCheckerBooleanOption(&CheckerOne, "Option2"); |
| 54 |
//Opts.getCheckerBooleanOption(&CheckerOne, "Option2"); |
| 55 |
|
| 56 |
// Checker true has no option specified. It should get false when search in |
| 57 |
// parents turned on. |
| 58 |
EXPECT_FALSE(Opts.getCheckerBooleanOption(CheckerTwoName, "Option", true)); |
| 59 |
// In any other case, we should assert, that we cannot test unfortunately. |
| 60 |
//Opts.getCheckerBooleanOption(&CheckerTwo, "Option"); |
| 61 |
//Opts.getCheckerBooleanOption(&CheckerTwo, "Option"); |
| 62 |
} |
| 63 |
|
| 64 |
TEST(StaticAnalyzerOptions, StringOptions) { |
| 65 |
AnalyzerOptions Opts; |
| 66 |
Opts.Config["Outer.Inner.CheckerOne:Option"] = "StringValue"; |
| 67 |
EXPECT_TRUE("StringValue" == Opts.getCheckerStringOption( |
| 68 |
"Outer.Inner.CheckerOne", "Option")); |
| 69 |
} |
| 70 |
|
| 71 |
} // end namespace ento |
| 72 |
} // end namespace clang |