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
13namespace clang {
14namespace ento {
15
16TEST(StaticAnalyzerOptions, getRegisteredCheckers) {
17 auto IsDebugChecker = [](StringRef CheckerName) {
18 return CheckerName.starts_with(Prefix: "debug");
19 };
20 auto IsAlphaChecker = [](StringRef CheckerName) {
21 return CheckerName.starts_with(Prefix: "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
33TEST(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 struct CheckerOneMock : CheckerBase {
41 StringRef getTagDescription() const override {
42 return "Outer.Inner.CheckerOne";
43 }
44 };
45 struct CheckerTwoMock : CheckerBase {
46 StringRef getTagDescription() const override {
47 return "Outer.Inner.CheckerTwo";
48 }
49 };
50
51 // CheckerTwo one has Option specified as true. It should read true regardless
52 // of search mode.
53 CheckerOneMock CheckerOne;
54 EXPECT_TRUE(Opts.getCheckerBooleanOption(&CheckerOne, "Option"));
55 // The package option is overridden with a checker option.
56 EXPECT_TRUE(Opts.getCheckerBooleanOption(&CheckerOne, "Option", true));
57 // The Outer package option is overridden by the Inner package option. No
58 // package option is specified.
59 EXPECT_TRUE(Opts.getCheckerBooleanOption(&CheckerOne, "Option2", true));
60 // No package option is specified and search in packages is turned off. We
61 // should assert here, but we can't test that.
62 //Opts.getCheckerBooleanOption(&CheckerOne, "Option2");
63 //Opts.getCheckerBooleanOption(&CheckerOne, "Option2");
64
65 // Checker true has no option specified. It should get false when search in
66 // parents turned on.
67 CheckerTwoMock CheckerTwo;
68 EXPECT_FALSE(Opts.getCheckerBooleanOption(&CheckerTwo, "Option", true));
69 // In any other case, we should assert, that we cannot test unfortunately.
70 //Opts.getCheckerBooleanOption(&CheckerTwo, "Option");
71 //Opts.getCheckerBooleanOption(&CheckerTwo, "Option");
72}
73
74TEST(StaticAnalyzerOptions, StringOptions) {
75 AnalyzerOptions Opts;
76 Opts.Config["Outer.Inner.CheckerOne:Option"] = "StringValue";
77
78 struct CheckerOneMock : CheckerBase {
79 StringRef getTagDescription() const override {
80 return "Outer.Inner.CheckerOne";
81 }
82 };
83
84 CheckerOneMock CheckerOne;
85 EXPECT_TRUE("StringValue" ==
86 Opts.getCheckerStringOption(&CheckerOne, "Option"));
87}
88
89TEST(StaticAnalyzerOptions, SubCheckerOptions) {
90 AnalyzerOptions Opts;
91 Opts.Config["Outer.Inner.CheckerOne:Option"] = "StringValue";
92 EXPECT_TRUE("StringValue" == Opts.getCheckerStringOption(
93 "Outer.Inner.CheckerOne", "Option"));
94}
95
96} // end namespace ento
97} // end namespace clang
98

source code of clang/unittests/StaticAnalyzer/AnalyzerOptionsTest.cpp