1 //===- unittest/Analysis/AnalyzerOptionsTest.cpp - SA Options test --------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
11 #include "clang/StaticAnalyzer/Core/Checker.h"
12 #include "gtest/gtest.h"
13
14 namespace clang {
15 namespace ento {
16
TEST(StaticAnalyzerOptions,SearchInParentPackageTests)17 TEST(StaticAnalyzerOptions, SearchInParentPackageTests) {
18 AnalyzerOptions Opts;
19 Opts.Config["Outer.Inner.CheckerOne:Option"] = "true";
20 Opts.Config["Outer.Inner:Option"] = "false";
21 Opts.Config["Outer.Inner:Option2"] = "true";
22 Opts.Config["Outer:Option2"] = "false";
23
24 struct CheckerOneMock : CheckerBase {
25 StringRef getTagDescription() const override {
26 return "Outer.Inner.CheckerOne";
27 }
28 };
29 struct CheckerTwoMock : CheckerBase {
30 StringRef getTagDescription() const override {
31 return "Outer.Inner.CheckerTwo";
32 }
33 };
34
35 // Checker one has Option specified as true. It should read true regardless of
36 // search mode.
37 CheckerOneMock CheckerOne;
38 EXPECT_TRUE(Opts.getBooleanOption("Option", false, &CheckerOne));
39 // The package option is overriden with a checker option.
40 EXPECT_TRUE(Opts.getBooleanOption("Option", false, &CheckerOne, true));
41 // The Outer package option is overriden by the Inner package option. No
42 // package option is specified.
43 EXPECT_TRUE(Opts.getBooleanOption("Option2", false, &CheckerOne, true));
44 // No package option is specified and search in packages is turned off. The
45 // default value should be returned.
46 EXPECT_FALSE(Opts.getBooleanOption("Option2", false, &CheckerOne));
47 EXPECT_TRUE(Opts.getBooleanOption("Option2", true, &CheckerOne));
48
49 // Checker true has no option specified. It should get the default value when
50 // search in parents turned off and false when search in parents turned on.
51 CheckerTwoMock CheckerTwo;
52 EXPECT_FALSE(Opts.getBooleanOption("Option", false, &CheckerTwo));
53 EXPECT_TRUE(Opts.getBooleanOption("Option", true, &CheckerTwo));
54 EXPECT_FALSE(Opts.getBooleanOption("Option", true, &CheckerTwo, true));
55 }
56
TEST(StaticAnalyzerOptions,StringOptions)57 TEST(StaticAnalyzerOptions, StringOptions) {
58 AnalyzerOptions Opts;
59 Opts.Config["Outer.Inner.CheckerOne:Option"] = "StringValue";
60
61 struct CheckerOneMock : CheckerBase {
62 StringRef getTagDescription() const override {
63 return "Outer.Inner.CheckerOne";
64 }
65 };
66
67 CheckerOneMock CheckerOne;
68 EXPECT_TRUE("StringValue" ==
69 Opts.getOptionAsString("Option", "DefaultValue", &CheckerOne));
70 EXPECT_TRUE("DefaultValue" ==
71 Opts.getOptionAsString("Option2", "DefaultValue", &CheckerOne));
72 }
73 } // end namespace ento
74 } // end namespace clang
75