1 //===- unittests/StaticAnalyzer/RegisterCustomCheckersTest.cpp ------------===//
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/Frontend/CompilerInstance.h"
10 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
11 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
12 #include "clang/StaticAnalyzer/Core/Checker.h"
13 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
14 #include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
15 #include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h"
16 #include "clang/Tooling/Tooling.h"
17 #include "gtest/gtest.h"
18 
19 namespace clang {
20 namespace ento {
21 namespace {
22 
23 template <typename CheckerT>
24 class TestAction : public ASTFrontendAction {
25   class DiagConsumer : public PathDiagnosticConsumer {
26     llvm::raw_ostream &Output;
27 
28   public:
29     DiagConsumer(llvm::raw_ostream &Output) : Output(Output) {}
30     void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
31                               FilesMade *filesMade) override {
32       for (const auto *PD : Diags)
33         Output << PD->getCheckerName() << ":" << PD->getShortDescription();
34     }
35 
36     StringRef getName() const override { return "Test"; }
37   };
38 
39   llvm::raw_ostream &DiagsOutput;
40 
41 public:
42   TestAction(llvm::raw_ostream &DiagsOutput) : DiagsOutput(DiagsOutput) {}
43 
44   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
45                                                  StringRef File) override {
46     std::unique_ptr<AnalysisASTConsumer> AnalysisConsumer =
47         CreateAnalysisConsumer(Compiler);
48     AnalysisConsumer->AddDiagnosticConsumer(new DiagConsumer(DiagsOutput));
49     Compiler.getAnalyzerOpts()->CheckersAndPackages = {
50         {"custom.CustomChecker", true}};
51     AnalysisConsumer->AddCheckerRegistrationFn([](CheckerRegistry &Registry) {
52       Registry.addChecker<CheckerT>("custom.CustomChecker", "Description", "");
53     });
54     return std::move(AnalysisConsumer);
55   }
56 };
57 
58 template <typename CheckerT>
59 bool runCheckerOnCode(const std::string &Code, std::string &Diags) {
60   llvm::raw_string_ostream OS(Diags);
61   return tooling::runToolOnCode(std::make_unique<TestAction<CheckerT>>(OS),
62                                 Code);
63 }
64 template <typename CheckerT>
65 bool runCheckerOnCode(const std::string &Code) {
66   std::string Diags;
67   return runCheckerOnCode<CheckerT>(Code, Diags);
68 }
69 
70 
71 class CustomChecker : public Checker<check::ASTCodeBody> {
72 public:
73   void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
74                         BugReporter &BR) const {
75     BR.EmitBasicReport(D, this, "Custom diagnostic", categories::LogicError,
76                        "Custom diagnostic description",
77                        PathDiagnosticLocation(D, Mgr.getSourceManager()), {});
78   }
79 };
80 
81 TEST(RegisterCustomCheckers, RegisterChecker) {
82   std::string Diags;
83   EXPECT_TRUE(runCheckerOnCode<CustomChecker>("void f() {;}", Diags));
84   EXPECT_EQ(Diags, "custom.CustomChecker:Custom diagnostic description");
85 }
86 
87 class LocIncDecChecker : public Checker<check::Location> {
88 public:
89   void checkLocation(SVal Loc, bool IsLoad, const Stmt *S,
90                      CheckerContext &C) const {
91     auto UnaryOp = dyn_cast<UnaryOperator>(S);
92     if (UnaryOp && !IsLoad) {
93       EXPECT_FALSE(UnaryOp->isIncrementOp());
94     }
95   }
96 };
97 
98 TEST(RegisterCustomCheckers, CheckLocationIncDec) {
99   EXPECT_TRUE(
100       runCheckerOnCode<LocIncDecChecker>("void f() { int *p; (*p)++; }"));
101 }
102 
103 }
104 }
105 }
106