Skip to content

Commit 87c51e2

Browse files
authored
Run PreStmt/PostStmt checker for GCCAsmStmt (#95409)
Fixes #94940 Run PreStmt and PostStmt checker for GCCAsmStmt. Unittest to validate that corresponding callback functions are triggered.
1 parent 7333c09 commit 87c51e2

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

clang/lib/StaticAnalyzer/Core/ExprEngine.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,11 +2057,17 @@ void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred,
20572057
llvm_unreachable("Support for MatrixSubscriptExpr is not implemented.");
20582058
break;
20592059

2060-
case Stmt::GCCAsmStmtClass:
2060+
case Stmt::GCCAsmStmtClass: {
20612061
Bldr.takeNodes(Pred);
2062-
VisitGCCAsmStmt(cast<GCCAsmStmt>(S), Pred, Dst);
2062+
ExplodedNodeSet PreVisit;
2063+
getCheckerManager().runCheckersForPreStmt(PreVisit, Pred, S, *this);
2064+
ExplodedNodeSet PostVisit;
2065+
for (ExplodedNode *const N : PreVisit)
2066+
VisitGCCAsmStmt(cast<GCCAsmStmt>(S), N, PostVisit);
2067+
getCheckerManager().runCheckersForPostStmt(Dst, PostVisit, S, *this);
20632068
Bldr.addNodes(Dst);
20642069
break;
2070+
}
20652071

20662072
case Stmt::MSAsmStmtClass:
20672073
Bldr.takeNodes(Pred);

clang/unittests/StaticAnalyzer/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_clang_unittest(StaticAnalysisTests
1010
CallDescriptionTest.cpp
1111
CallEventTest.cpp
1212
ConflictingEvalCallsTest.cpp
13+
ExprEngineVisitTest.cpp
1314
FalsePositiveRefutationBRVisitorTest.cpp
1415
IsCLibraryFunctionTest.cpp
1516
MemRegionDescriptiveNameTest.cpp
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//===- ExprEngineVisitTest.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 "CheckerRegistration.h"
10+
#include "clang/AST/Stmt.h"
11+
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
12+
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
13+
#include "clang/StaticAnalyzer/Core/Checker.h"
14+
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
15+
#include "gtest/gtest.h"
16+
17+
using namespace clang;
18+
using namespace ento;
19+
20+
namespace {
21+
22+
void emitErrorReport(CheckerContext &C, const BugType &Bug,
23+
const std::string &Desc) {
24+
if (ExplodedNode *Node = C.generateNonFatalErrorNode(C.getState())) {
25+
auto Report = std::make_unique<PathSensitiveBugReport>(Bug, Desc, Node);
26+
C.emitReport(std::move(Report));
27+
}
28+
}
29+
30+
#define CREATE_EXPR_ENGINE_CHECKER(CHECKER_NAME, CALLBACK, STMT_TYPE, \
31+
BUG_NAME) \
32+
class CHECKER_NAME : public Checker<check::CALLBACK<STMT_TYPE>> { \
33+
public: \
34+
void check##CALLBACK(const STMT_TYPE *ASM, CheckerContext &C) const { \
35+
emitErrorReport(C, Bug, "check" #CALLBACK "<" #STMT_TYPE ">"); \
36+
} \
37+
\
38+
private: \
39+
const BugType Bug{this, BUG_NAME}; \
40+
};
41+
42+
CREATE_EXPR_ENGINE_CHECKER(ExprEngineVisitPreChecker, PreStmt, GCCAsmStmt,
43+
"GCCAsmStmtBug")
44+
CREATE_EXPR_ENGINE_CHECKER(ExprEngineVisitPostChecker, PostStmt, GCCAsmStmt,
45+
"GCCAsmStmtBug")
46+
47+
void addExprEngineVisitPreChecker(AnalysisASTConsumer &AnalysisConsumer,
48+
AnalyzerOptions &AnOpts) {
49+
AnOpts.CheckersAndPackages = {{"ExprEngineVisitPreChecker", true}};
50+
AnalysisConsumer.AddCheckerRegistrationFn([](CheckerRegistry &Registry) {
51+
Registry.addChecker<ExprEngineVisitPreChecker>("ExprEngineVisitPreChecker",
52+
"Desc", "DocsURI");
53+
});
54+
}
55+
56+
void addExprEngineVisitPostChecker(AnalysisASTConsumer &AnalysisConsumer,
57+
AnalyzerOptions &AnOpts) {
58+
AnOpts.CheckersAndPackages = {{"ExprEngineVisitPostChecker", true}};
59+
AnalysisConsumer.AddCheckerRegistrationFn([](CheckerRegistry &Registry) {
60+
Registry.addChecker<ExprEngineVisitPostChecker>(
61+
"ExprEngineVisitPostChecker", "Desc", "DocsURI");
62+
});
63+
}
64+
65+
TEST(ExprEngineVisitTest, checkPreStmtGCCAsmStmt) {
66+
std::string Diags;
67+
EXPECT_TRUE(runCheckerOnCode<addExprEngineVisitPreChecker>(R"(
68+
void top() {
69+
asm("");
70+
}
71+
)",
72+
Diags));
73+
EXPECT_EQ(Diags, "ExprEngineVisitPreChecker: checkPreStmt<GCCAsmStmt>\n");
74+
}
75+
76+
TEST(ExprEngineVisitTest, checkPostStmtGCCAsmStmt) {
77+
std::string Diags;
78+
EXPECT_TRUE(runCheckerOnCode<addExprEngineVisitPostChecker>(R"(
79+
void top() {
80+
asm("");
81+
}
82+
)",
83+
Diags));
84+
EXPECT_EQ(Diags, "ExprEngineVisitPostChecker: checkPostStmt<GCCAsmStmt>\n");
85+
}
86+
87+
} // namespace

0 commit comments

Comments
 (0)