[clang] [Clang][Sema] Add a test for move ctor calling for a base class. NFC (PR #97164)

Pavel Samolysov via cfe-commits cfe-commits at lists.llvm.org
Sat Jun 29 05:22:40 PDT 2024


https://github.com/samolisov created https://github.com/llvm/llvm-project/pull/97164

When clang compiles the following expression:

```c++
  return A{B{"Move Ctor"}};
```
(where `B` is a base class for `A`), it adds a call to the move constructor of `B`. When the code is changed to...

```c++
  return A{{"No Move Ctor"}};
```
... a move constructor is invoked neither for `A` nor for `B`.

The lit test demonstrates the difference in the generated AST.

>From 2cf5a4e016fb7b5cc58a0d2ca5411df65e17a8cb Mon Sep 17 00:00:00 2001
From: Pavel Samolysov <samolisov at gmail.com>
Date: Sat, 29 Jun 2024 15:18:11 +0300
Subject: [PATCH] [Clang][Sema] Add a test for move ctor calling for a base
 class. NFC

When clang compiles the following expression:

```c++
  return A{B{"Move Ctor"}};
```
(where `B` is a base class for `A`), it adds a call to the move
constructor of `B`. When the code is changed to...

```c++
  return A{{"No Move Ctor"}};
```
... a move constructor is invoked neither for `A` nor for `B`.

The lit test demonstrates the difference in the generated AST.
---
 .../AST/explicit-base-class-move-cntr.cpp     | 43 +++++++++++++++++++
 1 file changed, 43 insertions(+)
 create mode 100644 clang/test/AST/explicit-base-class-move-cntr.cpp

diff --git a/clang/test/AST/explicit-base-class-move-cntr.cpp b/clang/test/AST/explicit-base-class-move-cntr.cpp
new file mode 100644
index 0000000000000..b9b591ebc79d7
--- /dev/null
+++ b/clang/test/AST/explicit-base-class-move-cntr.cpp
@@ -0,0 +1,43 @@
+// RUN: %clang_cc1 -ast-dump -fblocks %s | FileCheck -strict-whitespace %s
+
+struct ExplicitBase {
+  explicit ExplicitBase(const char *) { }
+  ExplicitBase(const ExplicitBase &) {}
+  ExplicitBase(ExplicitBase &&) {}
+  ExplicitBase &operator=(const ExplicitBase &) { return *this; }
+  ExplicitBase &operator=(ExplicitBase &&) { return *this; }
+  ~ExplicitBase() { }
+};
+
+struct Derived1 : ExplicitBase {};
+
+Derived1 makeDerived1() {
+  // CHECK:      FunctionDecl 0x{{[^ ]*}} <line:[[@LINE-1]]:1, line:{{[^:]*}}:1> line:[[@LINE-1]]:10 makeDerived1 'Derived1 ()'
+  // CHECK-NEXT: CompoundStmt 0x{{[^ ]*}} <col:{{[^ ^,]+}}, line:{{[^:]*}}:1
+  // CHECK-NEXT: ReturnStmt 0x{{[^ ]*}} <line:[[@LINE+6]]:3, col:{{[0-9]+}}>
+  // CHECK-DAG:  MaterializeTemporaryExpr 0x{{[^ ]*}} <col:{{[0-9]+}}, col:{{[0-9]+}}> 'ExplicitBase' xvalue
+  // CHECK-NEXT: CXXBindTemporaryExpr 0x[[TEMP:[^ ]*]] <col:{{[0-9]+}}, col:{{[0-9]+}}> 'ExplicitBase' (CXXTemporary 0x[[TEMP]])
+  // CHECK-NEXT: CXXTemporaryObjectExpr 0x{{[^ ]*}} <col:{{[0-9]+}}, col:{{[0-9]+}}> 'ExplicitBase' 'void (const char *)' list
+  // CHECK-NEXT: ImplicitCastExpr 0x{{[^ ]*}} <col:{{[0-9]+}}> 'const char *' <ArrayToPointerDecay>
+  // CHECK-NEXT: StringLiteral 0x{{[^ ]*}} <col:{{[0-9]+}}> 'const char[10]' lvalue "Move Ctor"
+  return Derived1{ExplicitBase{"Move Ctor"}};
+}
+
+struct ImplicitBase {
+  ImplicitBase(const char *) { }
+  ImplicitBase(const ImplicitBase &) {}
+  ImplicitBase(ImplicitBase &&) {}
+  ImplicitBase &operator=(const ImplicitBase &) { return *this; }
+  ImplicitBase &operator=(ImplicitBase &&) { return *this; }
+  ~ImplicitBase() { }
+};
+
+struct Derived2 : ImplicitBase {};
+
+Derived2 makeDerived2() {
+  // CHECK:      FunctionDecl 0x{{[^ ]*}} <line:[[@LINE-1]]:1, line:{{[^:]*}}:1> line:[[@LINE-1]]:10 makeDerived2 'Derived2 ()'
+  // CHECK-NEXT: CompoundStmt 0x{{[^ ]*}} <col:{{[^ ^,]+}}, line:{{[^:]*}}:1
+  // CHECK-NEXT: ReturnStmt 0x{{[^ ]*}} <line:[[@LINE+2]]:3, col:{{[0-9]+}}>
+  // CHECK-NOT:  MaterializeTemporaryExpr 0x{{[^ ]*}} <col:{{[0-9]+}}, col:{{[0-9]+}}> 'ImplicitBase' xvalue
+  return Derived2{{"No Ctor"}};
+}



More information about the cfe-commits mailing list