[clang] 8670cf9 - [LifetimeSafety] Drop the object argument of any static member operator (#240396)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 10 00:27:31 PST 2026
Author: Gábor Horváth
Date: 2026-11-10T08:27:25Z
New Revision: 8670cf9d52465cfccf4e5cc65dcd59abfeecf1b8
URL: https://github.com/llvm/llvm-project/commit/8670cf9d52465cfccf4e5cc65dcd59abfeecf1b8
DIFF: https://github.com/llvm/llvm-project/commit/8670cf9d52465cfccf4e5cc65dcd59abfeecf1b8.diff
LOG: [LifetimeSafety] Drop the object argument of any static member operator (#240396)
Added:
Modified:
clang/include/clang/Analysis/AnyCall.h
clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp
clang/test/Sema/LifetimeSafety/safety.cpp
clang/unittests/Analysis/AnyCallTest.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Analysis/AnyCall.h b/clang/include/clang/Analysis/AnyCall.h
index 0b114dd301797f..d0775588047763 100644
--- a/clang/include/clang/Analysis/AnyCall.h
+++ b/clang/include/clang/Analysis/AnyCall.h
@@ -166,7 +166,8 @@ class AnyCall {
/// \returns actual arguments for expression-backed calls, or an empty list
/// for declaration-backed calls and call kinds with implicit or synthesized
/// argument lists, such as allocators and destructors. For instance member
- /// calls, the implicit object argument is included as argument 0.
+ /// calls, the implicit object argument is included as argument 0. For static
+ /// member operator calls, the object expression is omitted.
llvm::SmallVector<const Expr *, 4> arguments() const {
if (!E)
return {};
@@ -179,12 +180,12 @@ class AnyCall {
Args.push_back(MCE->getImplicitObjectArgument());
Args.append(CE->arg_begin(), CE->arg_end());
- if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(CE))
- // For `static operator()`, the first argument is the object argument,
- // remove it from the argument list to avoid off-by-one errors.
- if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
- FD && FD->isStatic() && OCE->getOperator() == OO_Call)
- Args.erase(Args.begin());
+ // A static member operator (`operator()` or `operator[]`) is called with
+ // the object expression as argument 0; drop it so arguments match
+ // parameters.
+ if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(D);
+ isa<CXXOperatorCallExpr>(CE) && MD && MD->isStatic())
+ Args.erase(Args.begin());
return Args;
}
case Block: {
diff --git a/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp b/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp
index 0c0d7b5403a28d..f0811d1c00026e 100644
--- a/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp
+++ b/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp
@@ -920,3 +920,18 @@ struct Derived : Mid, virtual Base {
};
} // namespace field_origin_escape
+
+namespace static_subscript_operator {
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wc++23-extensions"
+struct Table {
+ static const int &operator[](const int &key [[clang::lifetimebound]]);
+};
+#pragma clang diagnostic pop
+
+// The suggestion goes to `key`, not to the object argument `table`.
+const int &lookup(const Table &table,
+ const int &key) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}}
+ return table[key]; // expected-note {{param returned here}}
+}
+} // namespace static_subscript_operator
diff --git a/clang/test/Sema/LifetimeSafety/safety.cpp b/clang/test/Sema/LifetimeSafety/safety.cpp
index 1b456de5125ecb..a287bfb57ecce9 100644
--- a/clang/test/Sema/LifetimeSafety/safety.cpp
+++ b/clang/test/Sema/LifetimeSafety/safety.cpp
@@ -2963,6 +2963,26 @@ void indexing_with_static_operator() {
use(x); // expected-note 2 {{later used here}}
}
+
+struct Table {
+ static const int &operator[](const int &key [[clang::lifetimebound]]);
+};
+
+const int &returned_from_static_subscript(const Table &table) {
+ int key = 1;
+ return table[key]; // expected-warning {{stack memory associated with local variable 'key' is returned}} \
+ // expected-note {{returned here}}
+}
+
+void dangling_from_static_subscript(const Table &table) {
+ const int *p;
+ {
+ int key = 1;
+ p = &table[key]; // expected-warning {{local variable 'key' does not live long enough}} \
+ // expected-note {{result of call to 'operator[]' aliases the storage of local variable 'key' because parameter 'key' is marked as lifetimebound}}
+ } // expected-note {{local variable 'key' is destroyed here}}
+ use(*p); // expected-note {{later used here}}
+}
} // namespace static_call_operator
namespace track_origins_for_lifetimebound_record_type {
diff --git a/clang/unittests/Analysis/AnyCallTest.cpp b/clang/unittests/Analysis/AnyCallTest.cpp
index da351e284d8c5b..b6a4dd574c549d 100644
--- a/clang/unittests/Analysis/AnyCallTest.cpp
+++ b/clang/unittests/Analysis/AnyCallTest.cpp
@@ -162,6 +162,50 @@ TEST(AnyCallTest, StaticOperatorCallExcludesObjectArgument) {
expectIntegerArguments(Call, {5});
}
+TEST(AnyCallTest, StaticSubscriptOperatorCallExcludesObjectArgument) {
+ auto AST = buildAST(R"cpp(
+ struct Table {
+ static int operator[](int row, int column);
+ };
+ int target(Table &table) { return table[6, 7]; }
+ )cpp",
+ {"-fsyntax-only", "-std=c++23"});
+ ASTContext &Ctx = AST->getASTContext();
+ ASSERT_EQ(Ctx.getDiagnostics().getClient()->getNumErrors(), 0U);
+
+ const auto *CE = selectFirst<CXXOperatorCallExpr>(
+ "call",
+ match(cxxOperatorCallExpr(hasOverloadedOperatorName("[]")).bind("call"),
+ Ctx));
+ ASSERT_NE(CE, nullptr);
+
+ AnyCall Call(CE);
+ expectIntegerArguments(Call, {6, 7});
+}
+
+TEST(AnyCallTest, StaticFreeOperatorCallKeepsFirstArgument) {
+ auto AST = buildAST(R"cpp(
+ struct Value {};
+ static bool operator==(const Value &lhs, int rhs);
+ bool target(Value value) { return value == 8; }
+ )cpp");
+ ASTContext &Ctx = AST->getASTContext();
+ ASSERT_EQ(Ctx.getDiagnostics().getClient()->getNumErrors(), 0U);
+
+ const auto *CE = selectFirst<CXXOperatorCallExpr>(
+ "call",
+ match(cxxOperatorCallExpr(hasOverloadedOperatorName("==")).bind("call"),
+ Ctx));
+ ASSERT_NE(CE, nullptr);
+
+ AnyCall Call(CE);
+ ASSERT_EQ(Call.arg_size(), 2U);
+ EXPECT_TRUE(isa<DeclRefExpr>(Call.getArg(0)->IgnoreImplicit()));
+ const auto *RHS = asIntegerLiteral(Call.getArg(1));
+ ASSERT_NE(RHS, nullptr);
+ EXPECT_EQ(RHS->getValue(), 8);
+}
+
TEST(AnyCallTest, ExposesBlockCallArguments) {
auto AST = buildAST(R"cpp(
void target() {
More information about the cfe-commits
mailing list