Author: Ryosuke Niwa Date: 2024-05-09T15:34:27-07:00 New Revision: a99cb96dfa97c04c3313cb3770b876fee20eb131
URL: https://github.com/llvm/llvm-project/commit/a99cb96dfa97c04c3313cb3770b876fee20eb131 DIFF: https://github.com/llvm/llvm-project/commit/a99cb96dfa97c04c3313cb3770b876fee20eb131.diff LOG: [alpha.webkit.UncountedCallArgsChecker] Allow trivial operator++ (#91102) This PR adds the support for trivial operator++ implementations. T& operator++() and T operator++(int) are trivial if the callee is trivial. Also allow incrementing and decrementing of a POD member variable. Also treat any __builtin_ functions as trivial. Added: Modified: clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp Removed: ################################################################################ diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp index 6901dbb415bf7..3abfa4cbb295d 100644 --- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp @@ -309,21 +309,8 @@ class TrivialFunctionAnalysisVisitor bool VisitDefaultStmt(const DefaultStmt *DS) { return VisitChildren(DS); } 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 || op == UO_Not) - return Visit(UO->getSubExpr()); - - if (UO->isIncrementOp() || UO->isDecrementOp()) { - // Allow increment or decrement of a POD type. - if (auto *RefExpr = dyn_cast<DeclRefExpr>(UO->getSubExpr())) { - if (auto *Decl = dyn_cast<VarDecl>(RefExpr->getDecl())) - return Decl->isLocalVarDeclOrParm() && - Decl->getType().isPODType(Decl->getASTContext()); - } - } - // Other operators are non-trivial. - return false; + // Unary operators are trivial if its operand is trivial except co_await. + return UO->getOpcode() != UO_Coawait && Visit(UO->getSubExpr()); } bool VisitBinaryOperator(const BinaryOperator *BO) { @@ -364,7 +351,7 @@ class TrivialFunctionAnalysisVisitor if (Name == "WTFCrashWithInfo" || Name == "WTFBreakpointTrap" || Name == "WTFReportAssertionFailure" || - Name == "compilerFenceForCrash" || Name == "__builtin_unreachable") + Name == "compilerFenceForCrash" || Name.find("__builtin") == 0) return true; return TrivialFunctionAnalysis::isTrivialImpl(Callee, Cache); @@ -405,6 +392,16 @@ class TrivialFunctionAnalysisVisitor return TrivialFunctionAnalysis::isTrivialImpl(Callee, Cache); } + bool VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *OCE) { + if (!checkArguments(OCE)) + return false; + auto *Callee = OCE->getCalleeDecl(); + if (!Callee) + return false; + // Recursively descend into the callee to confirm that it's trivial as well. + return TrivialFunctionAnalysis::isTrivialImpl(Callee, Cache); + } + bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { if (auto *Expr = E->getExpr()) { if (!Visit(Expr)) diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp index e75d42b9f1491..6ca7677511d73 100644 --- a/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp +++ b/clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp @@ -138,11 +138,29 @@ class Number { Number(int v) : v(v) { } Number(double); Number operator+(const Number&); + Number& operator++() { ++v; return *this; } + Number operator++(int) { Number returnValue(v); ++v; return returnValue; } const int& value() const { return v; } + void someMethod(); + private: int v; }; +class ComplexNumber { +public: + ComplexNumber() : real(0), complex(0) { } + ComplexNumber(const ComplexNumber&); + ComplexNumber& operator++() { real.someMethod(); return *this; } + ComplexNumber operator++(int); + ComplexNumber& operator<<(int); + ComplexNumber& operator+(); + +private: + Number real; + Number complex; +}; + class RefCounted { public: void ref() const; @@ -210,6 +228,12 @@ class RefCounted { unsigned trivial32() { return sizeof(int); } unsigned trivial33() { return ~0xff; } template <unsigned v> unsigned trivial34() { return v; } + void trivial35() { v++; } + void trivial36() { ++(*number); } + void trivial37() { (*number)++; } + void trivial38() { v++; if (__builtin_expect(!!(number), 1)) (*number)++; } + int trivial39() { return -v; } + int trivial40() { return v << 2; } static RefCounted& singleton() { static RefCounted s_RefCounted; @@ -284,9 +308,14 @@ class RefCounted { int nonTrivial13() { return ~otherFunction(); } int nonTrivial14() { int r = 0xff; r |= otherFunction(); return r; } + void nonTrivial15() { ++complex; } + void nonTrivial16() { complex++; } + ComplexNumber nonTrivial17() { return complex << 2; } + ComplexNumber nonTrivial18() { return +complex; } unsigned v { 0 }; Number* number { nullptr }; + ComplexNumber complex; Enum enumValue { Enum::Value1 }; }; @@ -342,6 +371,12 @@ class UnrelatedClass { getFieldTrivial().trivial32(); // no-warning getFieldTrivial().trivial33(); // no-warning getFieldTrivial().trivial34<7>(); // no-warning + getFieldTrivial().trivial35(); // no-warning + getFieldTrivial().trivial36(); // no-warning + getFieldTrivial().trivial37(); // no-warning + getFieldTrivial().trivial38(); // no-warning + getFieldTrivial().trivial39(); // no-warning + getFieldTrivial().trivial40(); // no-warning RefCounted::singleton().trivial18(); // no-warning RefCounted::singleton().someFunction(); // no-warning @@ -376,6 +411,14 @@ class UnrelatedClass { // 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}} + getFieldTrivial().nonTrivial15(); + // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}} + getFieldTrivial().nonTrivial16(); + // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}} + getFieldTrivial().nonTrivial17(); + // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}} + getFieldTrivial().nonTrivial18(); + // expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}} } }; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits