[clang] Skip over std namespace in WebKit checkers. (PR #90552)
Ryosuke Niwa via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 29 20:25:24 PDT 2024
https://github.com/rniwa created https://github.com/llvm/llvm-project/pull/90552
None
>From fa7a6e376b07ae6262dd06920c87dc69705a646d Mon Sep 17 00:00:00 2001
From: Ryosuke Niwa <rniwa at webkit.org>
Date: Mon, 29 Apr 2024 20:24:24 -0700
Subject: [PATCH] Skip over std namespace in WebKit checkers.
---
.../WebKit/UncountedCallArgsChecker.cpp | 6 +++++
.../WebKit/UncountedLocalVarsChecker.cpp | 6 +++++
.../Analysis/Checkers/WebKit/call-args.cpp | 27 +++++++++++++++++++
.../Checkers/WebKit/uncounted-local-vars.cpp | 26 ++++++++++++++++++
4 files changed, 65 insertions(+)
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp
index ae494de58da3da..0ff1a27ff412b9 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedCallArgsChecker.cpp
@@ -53,6 +53,12 @@ class UncountedCallArgsChecker
bool shouldVisitTemplateInstantiations() const { return true; }
bool shouldVisitImplicitCode() const { return false; }
+ bool TraverseNamespaceDecl(NamespaceDecl *Decl) {
+ if (safeGetName(Decl) == "std")
+ return true;
+ return RecursiveASTVisitor<LocalVisitor>::TraverseNamespaceDecl(Decl);
+ }
+
bool TraverseClassTemplateDecl(ClassTemplateDecl *Decl) {
if (isRefType(safeGetName(Decl)))
return true;
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLocalVarsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLocalVarsChecker.cpp
index 6036ad58cf253c..b27400678a5790 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLocalVarsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLocalVarsChecker.cpp
@@ -134,6 +134,12 @@ class UncountedLocalVarsChecker
bool shouldVisitTemplateInstantiations() const { return true; }
bool shouldVisitImplicitCode() const { return false; }
+ bool TraverseNamespaceDecl(NamespaceDecl *Decl) {
+ if (safeGetName(Decl) == "std")
+ return true;
+ return RecursiveASTVisitor<LocalVisitor>::TraverseNamespaceDecl(Decl);
+ }
+
bool VisitVarDecl(VarDecl *V) {
Checker->visitVarDecl(V);
return true;
diff --git a/clang/test/Analysis/Checkers/WebKit/call-args.cpp b/clang/test/Analysis/Checkers/WebKit/call-args.cpp
index 2a4b6bb1f1063a..3a1a2f9e92289e 100644
--- a/clang/test/Analysis/Checkers/WebKit/call-args.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/call-args.cpp
@@ -333,3 +333,30 @@ namespace cxx_member_operator_call {
// expected-warning at -1{{Call argument for parameter 'bad' is uncounted and unsafe}}
}
}
+
+namespace std {
+
+template <typename T>
+T* other_function();
+
+template <typename T>
+void another_function(T*, T*);
+
+template <typename T>
+void some_function(T* a)
+{
+ another_function(other_function<T>(), a);
+}
+
+} // std
+
+namespace ignore_std_namespace {
+
+RefCountable *ref_counted();
+
+void foo() {
+ std::some_function(ref_counted());
+ // expected-warning at -1{{Call argument for parameter 'a' is uncounted and unsafe}}
+}
+
+} // ignore_std_namespace
\ No newline at end of file
diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-local-vars.cpp b/clang/test/Analysis/Checkers/WebKit/uncounted-local-vars.cpp
index 00673e91f471ea..c8550b7e2e77f1 100644
--- a/clang/test/Analysis/Checkers/WebKit/uncounted-local-vars.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/uncounted-local-vars.cpp
@@ -187,3 +187,29 @@ void bar() {
}
} // namespace ignore_for_if
+
+namespace std {
+
+void memcpy(void*, void*, unsigned long);
+
+template <typename T>
+void some_function(T* a, T* b)
+{
+ T* temp = new T;
+ memcpy(temp, a, sizeof(T));
+ memcpy(a, b, sizeof(T));
+ memcpy(b, temp, sizeof(T));
+ delete temp;
+}
+
+} // std
+
+namespace ignore_std_namespace {
+
+RefCountable *ref_counted();
+
+void foo() {
+ std::some_function(ref_counted(), ref_counted());
+}
+
+} // ignore_std_namespace
More information about the cfe-commits
mailing list