[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
Sun Jul 7 03:35:52 PDT 2024


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

>From edf0d10b41099068ef49a2d2fe0ce60356d2f2fd 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 1/2] [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.

Issue: #92495
---
 .../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 00000000000000..b9b591ebc79d7e
--- /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"}};
+}

>From a29869ad88f546563404f7f1d60ca9ca2c3a255b Mon Sep 17 00:00:00 2001
From: Pavel Samolysov <samolisov at gmail.com>
Date: Sun, 7 Jul 2024 13:29:06 +0300
Subject: [PATCH 2/2] Regenerate the test using the
 llvm-project/clang/test/AST/gen_ast_dump_json_test.py tool

---
 .../AST/explicit-base-class-move-cntr.cpp     | 154 ++++++++++++++++--
 1 file changed, 141 insertions(+), 13 deletions(-)

diff --git a/clang/test/AST/explicit-base-class-move-cntr.cpp b/clang/test/AST/explicit-base-class-move-cntr.cpp
index b9b591ebc79d7e..808af2fc94336f 100644
--- a/clang/test/AST/explicit-base-class-move-cntr.cpp
+++ b/clang/test/AST/explicit-base-class-move-cntr.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -ast-dump -fblocks %s | FileCheck -strict-whitespace %s
+// RUN: %clang_cc1 -ast-dump=json %s | FileCheck -strict-whitespace %s
 
 struct ExplicitBase {
   explicit ExplicitBase(const char *) { }
@@ -12,14 +12,85 @@ struct 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"
+// CHECK:  "kind": "FunctionDecl",
+// CHECK:  "name": "makeDerived1",
+
+// CHECK:    "kind": "CompoundStmt",
+
+// CHECK:      "kind": "ReturnStmt",
+// CHECK:        "kind": "ExprWithCleanups",
+// CHECK:        "type": {
+// CHECK-NEXT:     "qualType": "Derived1"
+// CHECK-NEXT:   },
+
+// CHECK:          "kind": "CXXFunctionalCastExpr",
+// CHECK:          "type": {
+// CHECK-NEXT:       "qualType": "Derived1"
+// CHECK-NEXT:     },
+// CHECK-NEXT:     "valueCategory": "prvalue",
+// CHECK-NEXT:     "castKind": "NoOp",
+
+// CHECK:            "kind": "CXXBindTemporaryExpr",
+// CHECK:            "type": {
+// CHECK-NEXT:         "qualType": "Derived1"
+// CHECK-NEXT:       },
+// CHECK-NEXT:       "valueCategory": "prvalue",
+
+// CHECK:              "kind": "InitListExpr",
+// CHECK:              "type": {
+// CHECK-NEXT:           "qualType": "Derived1"
+// CHECK-NEXT:         },
+// CHECK-NEXT:         "valueCategory": "prvalue",
+
+// CHECK:                "kind": "CXXConstructExpr",
+// CHECK:                "type": {
+// CHECK-NEXT:             "qualType": "ExplicitBase"
+// CHECK-NEXT:           },
+// CHECK-NEXT:           "valueCategory": "prvalue",
+// CHECK-NEXT:           "ctorType": {
+// CHECK-NEXT:             "qualType": "void (ExplicitBase &&)"
+// CHECK-NEXT:           },
+// CHECK-NEXT:           "hadMultipleCandidates": true,
+// CHECK-NEXT:           "constructionKind": "non-virtual base",
+
+// CHECK:                  "kind": "MaterializeTemporaryExpr",
+// CHECK:                  "type": {
+// CHECK-NEXT:               "qualType": "ExplicitBase"
+// CHECK-NEXT:             },
+// CHECK-NEXT:             "valueCategory": "xvalue",
+// CHECK-NEXT:             "storageDuration": "full expression",
+
+// CHECK:                    "kind": "CXXBindTemporaryExpr",
+// CHECK:                    "type": {
+// CHECK-NEXT:                 "qualType": "ExplicitBase"
+// CHECK-NEXT:               },
+// CHECK-NEXT:               "valueCategory": "prvalue",
+
+// CHECK:                      "kind": "CXXTemporaryObjectExpr",
+// CHECK:                      "type": {
+// CHECK-NEXT:                   "qualType": "ExplicitBase"
+// CHECK-NEXT:                 },
+// CHECK-NEXT:                 "valueCategory": "prvalue",
+// CHECK-NEXT:                 "ctorType": {
+// CHECK-NEXT:                   "qualType": "void (const char *)"
+// CHECK-NEXT:                 },
+// CHECK-NEXT:                 "list": true,
+// CHECK-NEXT:                 "hadMultipleCandidates": true,
+// CHECK-NEXT:                 "constructionKind": "complete",
+
+// CHECK:                        "kind": "ImplicitCastExpr",
+// CHECK:                        "type": {
+// CHECK-NEXT:                     "qualType": "const char *"
+// CHECK-NEXT:                   },
+// CHECK-NEXT:                   "valueCategory": "prvalue",
+// CHECK-NEXT:                   "castKind": "ArrayToPointerDecay",
+
+// CHECK:                          "kind": "StringLiteral",
+// CHECK:                          "type": {
+// CHECK-NEXT:                       "qualType": "const char[10]"
+// CHECK-NEXT:                     },
+// CHECK-NEXT:                     "valueCategory": "lvalue",
+// CHECK-NEXT:                     "value": "\"Move Ctor\""
   return Derived1{ExplicitBase{"Move Ctor"}};
 }
 
@@ -35,9 +106,66 @@ struct 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
+// CHECK:  "kind": "FunctionDecl",
+// CHECK:  "name": "makeDerived2",
+
+// CHECK:    "kind": "CompoundStmt",
+
+// CHECK:      "kind": "ReturnStmt",
+
+// CHECK:        "kind": "ExprWithCleanups",
+// CHECK:        "type": {
+// CHECK-NEXT:     "qualType": "Derived2"
+// CHECK-NEXT:   },
+// CHECK-NEXT:   "valueCategory": "prvalue",
+// CHECK-NEXT:   "cleanupsHaveSideEffects": true,
+
+// CHECK:          "kind": "CXXFunctionalCastExpr",
+// CHECK:          "type": {
+// CHECK-NEXT:       "qualType": "Derived2"
+// CHECK-NEXT:     },
+// CHECK-NEXT:     "valueCategory": "prvalue",
+// CHECK-NEXT:     "castKind": "NoOp",
+
+// CHECK:            "kind": "CXXBindTemporaryExpr",
+// CHECK:            "type": {
+// CHECK-NEXT:         "qualType": "Derived2"
+// CHECK-NEXT:       },
+// CHECK-NEXT:       "valueCategory": "prvalue",
+
+// CHECK:              "kind": "InitListExpr",
+// CHECK:              "type": {
+// CHECK-NEXT:           "qualType": "Derived2"
+// CHECK-NEXT:         },
+// CHECK-NEXT:         "valueCategory": "prvalue",
+
+// CHECK:                "kind": "CXXConstructExpr",
+// CHECK:                "type": {
+// CHECK-NEXT:             "qualType": "ImplicitBase"
+// CHECK-NEXT:           },
+// CHECK-NEXT:           "valueCategory": "prvalue",
+// CHECK-NEXT:           "ctorType": {
+// CHECK-NEXT:             "qualType": "void (const char *)"
+// CHECK-NEXT:           },
+// CHECK-NEXT:           "list": true,
+// CHECK-NEXT:           "hadMultipleCandidates": true,
+// CHECK-NEXT:           "constructionKind": "non-virtual base",
+
+// CHECK:                  "kind": "ImplicitCastExpr",
+// CHECK:                  "type": {
+// CHECK-NEXT:               "qualType": "const char *"
+// CHECK-NEXT:             },
+// CHECK-NEXT:             "valueCategory": "prvalue",
+// CHECK-NEXT:             "castKind": "ArrayToPointerDecay",
+
+// CHECK:                    "kind": "StringLiteral",
+// CHECK:                    "type": {
+// CHECK-NEXT:                 "qualType": "const char[8]"
+// CHECK-NEXT:               },
+// CHECK-NEXT:               "valueCategory": "lvalue",
+// CHECK-NEXT:               "value": "\"No Ctor\""
   return Derived2{{"No Ctor"}};
 }
+
+// NOTE: CHECK lines have been autogenerated by gen_ast_dump_json_test.py
+// using --filters=FunctionDecl,CompoundStmt,ReturnStmt,MaterializeTemporaryExpr,CXXBindTemporaryExpr,CXXTemporaryObjectExpr,ImplicitCastExpr,StringLiteralStringLiteral



More information about the cfe-commits mailing list