https://github.com/rniwa updated https://github.com/llvm/llvm-project/pull/90414
>From 416dec586ed566e6c29ca8f5fa66be488ac09e78 Mon Sep 17 00:00:00 2001 From: Ryosuke Niwa <rn...@webkit.org> Date: Sun, 28 Apr 2024 13:17:48 -0700 Subject: [PATCH 1/2] [alpha.webkit.UncountedCallArgsChecker] Support more trivial expressions. Treat a bitwise not ~, compound operator such as |=, array subscription, sizeof, and non-type template parameter as trivial so long as subexpressions are also trivial. Also treat true/false boolean literal as trivial. --- .../Checkers/WebKit/PtrTypesSemantics.cpp | 23 ++++++++++++++++++- .../Checkers/WebKit/uncounted-obj-arg.cpp | 23 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp index 287f6a52870056..f6dd35c9e59868 100644 --- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp @@ -311,7 +311,7 @@ class TrivialFunctionAnalysisVisitor bool VisitUnaryOperator(const UnaryOperator *UO) { // Operator '*' and '!' are allowed as long as the operand is trivial. auto op = UO->getOpcode(); - if (op == UO_Deref || op == UO_AddrOf || op == UO_LNot) + if (op == UO_Deref || op == UO_AddrOf || op == UO_LNot || op == UO_Not) return Visit(UO->getSubExpr()); if (UO->isIncrementOp() || UO->isDecrementOp()) { @@ -331,6 +331,16 @@ class TrivialFunctionAnalysisVisitor return Visit(BO->getLHS()) && Visit(BO->getRHS()); } + bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO) { + // Compound assignment operator such as |= is trivial if its + // subexpresssions are trivial. + return VisitChildren(CAO); + } + + bool VisitArraySubscriptExpr(const ArraySubscriptExpr *ASE) { + return VisitChildren(ASE); + } + bool VisitConditionalOperator(const ConditionalOperator *CO) { // Ternary operators are trivial if their conditions & values are trivial. return VisitChildren(CO); @@ -360,6 +370,16 @@ class TrivialFunctionAnalysisVisitor return TrivialFunctionAnalysis::isTrivialImpl(Callee, Cache); } + bool + VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) { + // Non-type template paramter is trivial if the replacement is trivial. + return Visit(E->getReplacement()); + } + + bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E) { + return VisitChildren(E); + } + bool VisitPredefinedExpr(const PredefinedExpr *E) { // A predefined identifier such as "func" is considered trivial. return true; @@ -463,6 +483,7 @@ class TrivialFunctionAnalysisVisitor bool VisitFixedPointLiteral(const FixedPointLiteral *E) { return true; } bool VisitCharacterLiteral(const CharacterLiteral *E) { return true; } bool VisitStringLiteral(const StringLiteral *E) { return true; } + bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { return true; } bool VisitConstantExpr(const ConstantExpr *CE) { // Constant expressions are trivial. diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp index 80a9a263dab140..63a68a994a5c64 100644 --- a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp +++ b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp @@ -201,6 +201,13 @@ class RefCounted { unsigned trivial25() const { return __c11_atomic_load((volatile _Atomic(unsigned) *)&v, __ATOMIC_RELAXED); } bool trivial26() { bool hasValue = v; return !hasValue; } bool trivial27(int v) { bool value; value = v ? 1 : 0; return value; } + bool trivial28() { return true; } + bool trivial29() { return false; } + unsigned trivial30(unsigned v) { unsigned r = 0xff; r |= v; return r; } + int trivial31(int* v) { return v[0]; } + unsigned trivial32() { return sizeof(int); } + unsigned trivial33() { return ~0xff; } + template <unsigned v> unsigned trivial34() { return v; } static RefCounted& singleton() { static RefCounted s_RefCounted; @@ -273,6 +280,9 @@ class RefCounted { return val; } + int nonTrivial13() { return ~otherFunction(); } + int nonTrivial14() { int r = 0xff; r |= otherFunction(); return r; } + unsigned v { 0 }; Number* number { nullptr }; Enum enumValue { Enum::Value1 }; @@ -322,6 +332,15 @@ class UnrelatedClass { getFieldTrivial().trivial25(); // no-warning getFieldTrivial().trivial26(); // no-warning getFieldTrivial().trivial27(5); // no-warning + getFieldTrivial().trivial28(); // no-warning + getFieldTrivial().trivial29(); // no-warning + getFieldTrivial().trivial30(7); // no-warning + int a[] = {1, 2}; + getFieldTrivial().trivial31(a); // no-warning + getFieldTrivial().trivial32(); // no-warning + getFieldTrivial().trivial33(); // no-warning + getFieldTrivial().trivial34<7>(); // no-warning + RefCounted::singleton().trivial18(); // no-warning RefCounted::singleton().someFunction(); // no-warning @@ -351,6 +370,10 @@ class UnrelatedClass { // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}} getFieldTrivial().nonTrivial12(); // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}} + getFieldTrivial().nonTrivial13(); + // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}} + getFieldTrivial().nonTrivial14(); + // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}} } }; >From 647493b5c550c281744efe78c3fcdddaf4039390 Mon Sep 17 00:00:00 2001 From: Ryosuke Niwa <rn...@webkit.org> Date: Tue, 30 Apr 2024 23:08:32 -0700 Subject: [PATCH 2/2] Make VisitSubstNonTypeTemplateParmExpr always return true. --- .../lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp index f6dd35c9e59868..6901dbb415bf76 100644 --- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp @@ -372,8 +372,8 @@ class TrivialFunctionAnalysisVisitor bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) { - // Non-type template paramter is trivial if the replacement is trivial. - return Visit(E->getReplacement()); + // Non-type template paramter is compile time constant and trivial. + return true; } bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits