https://github.com/aeft updated https://github.com/llvm/llvm-project/pull/181113
>From 443bcc74263343aa2105d14b851f128356171621 Mon Sep 17 00:00:00 2001 From: Alex Wang <[email protected]> Date: Thu, 12 Feb 2026 02:13:05 -0800 Subject: [PATCH 1/2] [clang][CFG] Sequence RHS before LHS for overloaded assignment operators in CFG to match C++17 rules --- clang/lib/Analysis/CFG.cpp | 18 ++++++++-- .../Analysis/cfg-assignment-eval-order.cpp | 29 ++++++++++++++++ .../test/Analysis/missing-bind-temporary.cpp | 34 +++++++++---------- 3 files changed, 62 insertions(+), 19 deletions(-) create mode 100644 clang/test/Analysis/cfg-assignment-eval-order.cpp diff --git a/clang/lib/Analysis/CFG.cpp b/clang/lib/Analysis/CFG.cpp index 8001a67a5e158..1d2085ca62791 100644 --- a/clang/lib/Analysis/CFG.cpp +++ b/clang/lib/Analysis/CFG.cpp @@ -656,6 +656,7 @@ class CFGBuilder { bool ExternallyDestructed = false); CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc); CFGBlock *VisitChildren(Stmt *S); + CFGBlock *VisitCallExprChildren(CallExpr *C); CFGBlock *VisitNoRecurse(Expr *E, AddStmtChoice asc); CFGBlock *VisitOMPExecutableDirective(OMPExecutableDirective *D, AddStmtChoice asc); @@ -2542,6 +2543,19 @@ CFGBlock *CFGBuilder::VisitChildren(Stmt *S) { return B; } +CFGBlock *CFGBuilder::VisitCallExprChildren(CallExpr *C) { + // C++17 onwards require that the right operand is sequenced before the left + // operand. + if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(C)) { + if (OCE->isAssignmentOp()) { + Visit(OCE->getArg(0)); + Visit(OCE->getArg(1)); + return Visit(OCE->getCallee()); + } + } + return VisitChildren(C); +} + CFGBlock *CFGBuilder::VisitInitListExpr(InitListExpr *ILE, AddStmtChoice asc) { if (asc.alwaysAdd(*this, ILE)) { autoCreateBlock(); @@ -2879,7 +2893,7 @@ CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) { autoCreateBlock(); appendCall(Block, C); - return VisitChildren(C); + return VisitCallExprChildren(C); } if (Block) { @@ -2903,7 +2917,7 @@ CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) { addSuccessor(Block, &cfg->getExit()); } - return VisitChildren(C); + return VisitCallExprChildren(C); } CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C, diff --git a/clang/test/Analysis/cfg-assignment-eval-order.cpp b/clang/test/Analysis/cfg-assignment-eval-order.cpp new file mode 100644 index 0000000000000..a792b7f7b33dd --- /dev/null +++ b/clang/test/Analysis/cfg-assignment-eval-order.cpp @@ -0,0 +1,29 @@ +// RUN: %clang_analyze_cc1 -analyzer-checker=debug.DumpCFG -std=c++17 %s > %t 2>&1 +// RUN: FileCheck --input-file=%t %s + +// CHECK-LABEL: void test(Map &m, int a, int b) +// CHECK: 1: operator= +// CHECK-NEXT: 2: [B1.1] (ImplicitCastExpr, FunctionToPointerDecay, Map &(*)(const Map &)) +// CHECK-NEXT: 3: operator[] +// CHECK-NEXT: 4: [B1.3] (ImplicitCastExpr, FunctionToPointerDecay, Map &(*)(int)) +// CHECK-NEXT: 5: m +// CHECK-NEXT: 6: a +// CHECK-NEXT: 7: [B1.6] (ImplicitCastExpr, LValueToRValue, int) +// CHECK-NEXT: 8: [B1.5][[B1.7]] (OperatorCall) +// CHECK-NEXT: 9: [B1.8] (ImplicitCastExpr, NoOp, const Map) +// CHECK-NEXT: 10: operator[] +// CHECK-NEXT: 11: [B1.10] (ImplicitCastExpr, FunctionToPointerDecay, Map &(*)(int)) +// CHECK-NEXT: 12: m +// CHECK-NEXT: 13: b +// CHECK-NEXT: 14: [B1.13] (ImplicitCastExpr, LValueToRValue, int) +// CHECK-NEXT: 15: [B1.12][[B1.14]] (OperatorCall) +// CHECK-NEXT: 16: [B1.15] = [B1.9] (OperatorCall) + +struct Map { + Map &operator[](int); + Map &operator=(const Map &); +}; + +void test(Map &m, int a, int b) { + m[b] = m[a]; +} \ No newline at end of file diff --git a/clang/test/Analysis/missing-bind-temporary.cpp b/clang/test/Analysis/missing-bind-temporary.cpp index 3d1af469dc01c..2ce733b0986b0 100644 --- a/clang/test/Analysis/missing-bind-temporary.cpp +++ b/clang/test/Analysis/missing-bind-temporary.cpp @@ -23,11 +23,11 @@ class B { // CHECK-NEXT: 2: B i; // CHECK-NEXT: 3: operator= // CHECK-NEXT: 4: [B1.3] (ImplicitCastExpr, FunctionToPointerDecay, B &(*)(B &&) noexcept) -// CHECK-NEXT: 5: i -// CHECK-NEXT: 6: {} (CXXConstructExpr, [B1.7], [B1.8], B) -// CHECK-NEXT: 7: [B1.6] (BindTemporary) -// CHECK-NEXT: 8: [B1.7] -// CHECK-NEXT: 9: [B1.5] = [B1.8] (OperatorCall) +// CHECK-NEXT: 5: {} (CXXConstructExpr, [B1.6], [B1.7], B) +// CHECK-NEXT: 6: [B1.5] (BindTemporary) +// CHECK-NEXT: 7: [B1.6] +// CHECK-NEXT: 8: i +// CHECK-NEXT: 9: [B1.8] = [B1.7] (OperatorCall) // CHECK-NEXT: 10: ~B() (Temporary object destructor) // CHECK-NEXT: 11: [B1.2].~B() (Implicit destructor) void foo(int) { @@ -60,13 +60,13 @@ class B { // CHECK-NEXT: 2: B i; // CHECK-NEXT: 3: operator= // CHECK-NEXT: 4: [B1.3] (ImplicitCastExpr, FunctionToPointerDecay, B &(*)(B &&) noexcept) -// CHECK-NEXT: 5: i -// CHECK-NEXT: 6: {} (CXXConstructExpr, [B1.7], [B1.8], B) -// CHECK-NEXT: 7: [B1.6] (BindTemporary) -// CHECK-NEXT: 8: [B1.7] -// CHECK-NEXT: 9: [B1.5] = [B1.8] (OperatorCall) -// CHECK-NEXT: 10: ~B() (Temporary object destructor) -// CHECK-NEXT: 11: [B1.2].~B() (Implicit destructor) +// CHECK-NEXT: 5: {} (CXXConstructExpr, [B1.6], [B1.7], B) +// CHECK-NEXT: 6: [B1.5] (BindTemporary) +// CHECK-NEXT: 7: [B1.6] +// CHECK-NEXT: 8: i +// CHECK-NEXT: 9: [B1.8] = [B1.7] (OperatorCall) +// CHECK-NEXT: 10: ~B() (Temporary object destructor) +// CHECK-NEXT: 11: [B1.2].~B() (Implicit destructor) template <typename T> void foo(T) { B i; i = {}; @@ -101,12 +101,12 @@ class B { // CHECK-NEXT: 2: B i; // CHECK-NEXT: 3: operator= // CHECK-NEXT: 4: [B1.3] (ImplicitCastExpr, FunctionToPointerDecay, B &(*)(B &&) noexcept) -// CHECK-NEXT: 5: i +// CHECK-NEXT: 5: {} // CHECK-NEXT: 6: {} -// CHECK-NEXT: 7: {} -// CHECK-NEXT: 8: [B1.7] (BindTemporary) -// CHECK-NEXT: 9: [B1.8] -// CHECK-NEXT: 10: [B1.5] = [B1.9] (OperatorCall) +// CHECK-NEXT: 7: [B1.6] (BindTemporary) +// CHECK-NEXT: 8: [B1.7] +// CHECK-NEXT: 9: i +// CHECK-NEXT: 10: [B1.9] = [B1.8] (OperatorCall) // CHECK-NEXT: 11: ~B() (Temporary object destructor) // CHECK-NEXT: 12: [B1.2].~B() (Implicit destructor) template <typename T> void foo(T) { >From 3cfe7c092591636571d8658170234f33ce5dd1e6 Mon Sep 17 00:00:00 2001 From: Alex Wang <[email protected]> Date: Thu, 12 Feb 2026 02:44:12 -0800 Subject: [PATCH 2/2] fixup! [clang][CFG] Sequence RHS before LHS for overloaded assignment operators in CFG to match C++17 rules --- clang/test/Analysis/cfg-assignment-eval-order.cpp | 4 ++-- clang/test/Analysis/scopes-cfg-output.cpp | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/clang/test/Analysis/cfg-assignment-eval-order.cpp b/clang/test/Analysis/cfg-assignment-eval-order.cpp index a792b7f7b33dd..5315eb3fcda3a 100644 --- a/clang/test/Analysis/cfg-assignment-eval-order.cpp +++ b/clang/test/Analysis/cfg-assignment-eval-order.cpp @@ -9,14 +9,14 @@ // CHECK-NEXT: 5: m // CHECK-NEXT: 6: a // CHECK-NEXT: 7: [B1.6] (ImplicitCastExpr, LValueToRValue, int) -// CHECK-NEXT: 8: [B1.5][[B1.7]] (OperatorCall) +// CHECK-NEXT: 8: [B1.5]{{\[\[}}B1.7]] (OperatorCall) // CHECK-NEXT: 9: [B1.8] (ImplicitCastExpr, NoOp, const Map) // CHECK-NEXT: 10: operator[] // CHECK-NEXT: 11: [B1.10] (ImplicitCastExpr, FunctionToPointerDecay, Map &(*)(int)) // CHECK-NEXT: 12: m // CHECK-NEXT: 13: b // CHECK-NEXT: 14: [B1.13] (ImplicitCastExpr, LValueToRValue, int) -// CHECK-NEXT: 15: [B1.12][[B1.14]] (OperatorCall) +// CHECK-NEXT: 15: [B1.12]{{\[\[}}B1.14]] (OperatorCall) // CHECK-NEXT: 16: [B1.15] = [B1.9] (OperatorCall) struct Map { diff --git a/clang/test/Analysis/scopes-cfg-output.cpp b/clang/test/Analysis/scopes-cfg-output.cpp index 6ed6f3638f75b..ac8ff1179a994 100644 --- a/clang/test/Analysis/scopes-cfg-output.cpp +++ b/clang/test/Analysis/scopes-cfg-output.cpp @@ -907,10 +907,10 @@ void test_for_compound_and_break() { // CHECK-NEXT: 5: auto &i // CHECK-NEXT: 6: operator= // CHECK-NEXT: 7: [B4.6] (ImplicitCastExpr, FunctionToPointerDecay, A &(*)(const A &) -// CHECK-NEXT: 8: i -// CHECK-NEXT: 9: b -// CHECK-NEXT: 10: [B4.9] (ImplicitCastExpr, NoOp, const A) -// CHECK-NEXT: 11: [B4.8] = [B4.10] (OperatorCall) +// CHECK-NEXT: 8: b +// CHECK-NEXT: 9: [B4.8] (ImplicitCastExpr, NoOp, const A) +// CHECK-NEXT: 10: i +// CHECK-NEXT: 11: [B4.10] = [B4.9] (OperatorCall) // CHECK-NEXT: 12: CFGScopeEnd(i) // CHECK-NEXT: Preds (1): B2 // CHECK-NEXT: Succs (1): B3 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
