AnalyzerOptionsTest.cpp 3.5 KB

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