llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Steven Noonan (tycho)

<details>
<summary>Changes</summary>

`SemaPseudoObject::checkAssignment` handles a type-dependent LHS or RHS by 
building a dependent `BinaryOperator`, even when the opcode is a compound 
assignment such as `+=`. `StmtVisitor` dispatches on the opcode, so such a node 
is visited as a `CompoundAssignOperator`, and the computation types are read 
past the end of the smaller `BinaryOperator` allocation.

In practice this crashes when writing a PCH that contains a compound assignment 
to a `__declspec(property)` with a dependent operand:

```c++
template &lt;typename T&gt; struct V {
  T get_x() const;
  void put_x(T v);
  __declspec(property(get = get_x, put = put_x)) T x;
  template &lt;typename U&gt; void f(U s) { this-&gt;x += 
static_cast&lt;T&gt;(s); }
};
```

It crashes in `ASTStmtWriter::VisitCompoundAssignOperator` while serializing 
`V::f`.

This patch builds a `CompoundAssignOperator` with dependent computation types 
in that case, as `BuildOverloadedBinOp` already does for dependent compound 
assignments.

## Testing

New `clang/test/PCH/ms-property-compound-assign.cpp`. It covers every compound 
assignment operator on a scalar property and an indexed property, checks the 
AST dump directly and through a PCH round trip, and also checks 
`-fpch-instantiate-templates`.

On x86_64 Windows (Release build, assertions enabled), all 2,326 tests in 
`clang/test/PCH`, `clang/test/SemaCXX` and `clang/test/AST` pass.

---
Full diff: https://github.com/llvm/llvm-project/pull/244958.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaPseudoObject.cpp (+10-1) 
- (added) clang/test/PCH/ms-property-compound-assign.cpp (+68) 


``````````diff
diff --git a/clang/lib/Sema/SemaPseudoObject.cpp 
b/clang/lib/Sema/SemaPseudoObject.cpp
index 66736670984ef..cea356fde457d 100644
--- a/clang/lib/Sema/SemaPseudoObject.cpp
+++ b/clang/lib/Sema/SemaPseudoObject.cpp
@@ -1504,10 +1504,19 @@ ExprResult SemaPseudoObject::checkAssignment(Scope *S, 
SourceLocation opcLoc,
                                              BinaryOperatorKind opcode,
                                              Expr *LHS, Expr *RHS) {
   // Do nothing if either argument is dependent.
-  if (LHS->isTypeDependent() || RHS->isTypeDependent())
+  if (LHS->isTypeDependent() || RHS->isTypeDependent()) {
+    // A compound assignment must be represented by a CompoundAssignOperator:
+    // consumers such as StmtVisitor dispatch on the opcode and would otherwise
+    // read the computation types past the end of a plain BinaryOperator.
+    if (BinaryOperator::isCompoundAssignmentOp(opcode))
+      return CompoundAssignOperator::Create(
+          SemaRef.Context, LHS, RHS, opcode, SemaRef.Context.DependentTy,
+          VK_PRValue, OK_Ordinary, opcLoc, SemaRef.CurFPFeatureOverrides(),
+          SemaRef.Context.DependentTy, SemaRef.Context.DependentTy);
     return BinaryOperator::Create(
         SemaRef.Context, LHS, RHS, opcode, SemaRef.Context.DependentTy,
         VK_PRValue, OK_Ordinary, opcLoc, SemaRef.CurFPFeatureOverrides());
+  }
 
   // Filter out non-overload placeholder types in the RHS.
   if (RHS->getType()->isNonOverloadPlaceholderType()) {
diff --git a/clang/test/PCH/ms-property-compound-assign.cpp 
b/clang/test/PCH/ms-property-compound-assign.cpp
new file mode 100644
index 0000000000000..4d27c95fa8cb3
--- /dev/null
+++ b/clang/test/PCH/ms-property-compound-assign.cpp
@@ -0,0 +1,68 @@
+// Compound assignments to a __declspec(property) with a type-dependent operand
+// must be represented by a CompoundAssignOperator; serializing them used to
+// read the computation types past the end of a plain BinaryOperator.
+
+// RUN: %clang_cc1 -std=c++20 -fdeclspec -ast-dump %s | FileCheck %s
+
+// RUN: %clang_cc1 -std=c++20 -fdeclspec -x c++-header -emit-pch -o %t %s
+// RUN: %clang_cc1 -std=c++20 -fdeclspec -include-pch %t -fsyntax-only -verify 
%s
+// RUN: %clang_cc1 -std=c++20 -fdeclspec -include-pch %t -ast-dump-all %s | 
FileCheck %s
+
+// RUN: %clang_cc1 -std=c++20 -fdeclspec -x c++-header -emit-pch 
-fpch-instantiate-templates -o %t.inst %s
+// RUN: %clang_cc1 -std=c++20 -fdeclspec -include-pch %t.inst -fsyntax-only 
-verify %s
+
+// expected-no-diagnostics
+
+#ifndef HEADER
+#define HEADER
+
+template <typename T>
+struct V {
+  T data[2];
+  T get_x() const { return data[0]; }
+  void put_x(T v) { data[0] = v; }
+  __declspec(property(get = get_x, put = put_x)) T x;
+
+  T get_at(int i) const { return data[i]; }
+  void put_at(int i, T v) { data[i] = v; }
+  __declspec(property(get = get_at, put = put_at)) T at[];
+
+  template <typename U> void f(U s) {
+    this->x += static_cast<T>(s);
+    this->x -= static_cast<T>(s);
+    this->x *= static_cast<T>(s);
+    this->x /= static_cast<T>(s);
+    this->x %= static_cast<T>(s);
+    this->x <<= static_cast<T>(s);
+    this->x >>= static_cast<T>(s);
+    this->x &= static_cast<T>(s);
+    this->x |= static_cast<T>(s);
+    this->x ^= static_cast<T>(s);
+    this->at[1] += static_cast<T>(s);
+  }
+};
+
+// CHECK-LABEL: FunctionTemplateDecl {{.*}} f
+// CHECK:       CXXMethodDecl {{.*}} f 'void (U)'
+// CHECK:       CompoundAssignOperator {{.*}} '<dependent type>' {{.*}}'+=' 
ComputeLHSTy='<dependent type>' ComputeResultTy='<dependent type>'
+// CHECK-NEXT:    MSPropertyRefExpr {{.*}} '<pseudo-object type>' lvalue
+// CHECK:       CompoundAssignOperator {{.*}} '-='
+// CHECK:       CompoundAssignOperator {{.*}} '*='
+// CHECK:       CompoundAssignOperator {{.*}} '/='
+// CHECK:       CompoundAssignOperator {{.*}} '%='
+// CHECK:       CompoundAssignOperator {{.*}} '<<='
+// CHECK:       CompoundAssignOperator {{.*}} '>>='
+// CHECK:       CompoundAssignOperator {{.*}} '&='
+// CHECK:       CompoundAssignOperator {{.*}} '|='
+// CHECK:       CompoundAssignOperator {{.*}} '^='
+// CHECK:       CompoundAssignOperator {{.*}} '<dependent type>' {{.*}}'+=' 
ComputeLHSTy='<dependent type>' ComputeResultTy='<dependent type>'
+// CHECK-NEXT:    MSPropertySubscriptExpr
+
+#else
+
+void test() {
+  V<int> v{{8, 9}};
+  v.f(1L);
+}
+
+#endif

``````````

</details>


https://github.com/llvm/llvm-project/pull/244958
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to