[llvm] Pass std::initializer_list by value to ArrayRef constructor. (PR #113590)

Haojian Wu via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 24 08:54:20 PDT 2024


https://github.com/hokein created https://github.com/llvm/llvm-project/pull/113590

The std::initializer_list is a lightweight object, it is passed by value in general.

This would also avoid a false positive when adding the lifetimebound annotation (#113547)

```
ArrayRef<int> foo(std::initializer_list<int> list) {
  return ArrayRef<int>(list);
}
```

>From ebcb61e2eee85d2ed73a6704d6899a896de960ca Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Thu, 24 Oct 2024 17:49:49 +0200
Subject: [PATCH] Pass std::initializer_list by value to ArrayRef constructor.

The std::initializer_list is a light-weight object, in general, it is
passed by value.

This would also avoid a false positive when adding the lifetimebound
annotation.

```
ArrayRef<int> foo(std::initializer_list<int> list) {
  return ArrayRef<int>(list);
}
```
---
 llvm/include/llvm/ADT/ArrayRef.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ADT/ArrayRef.h b/llvm/include/llvm/ADT/ArrayRef.h
index d9897320ce091a..bf6b55923b84ba 100644
--- a/llvm/include/llvm/ADT/ArrayRef.h
+++ b/llvm/include/llvm/ADT/ArrayRef.h
@@ -113,7 +113,7 @@ namespace llvm {
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Winit-list-lifetime"
 #endif
-    constexpr /*implicit*/ ArrayRef(const std::initializer_list<T> &Vec)
+    constexpr /*implicit*/ ArrayRef(std::initializer_list<T> Vec)
         : Data(Vec.begin() == Vec.end() ? (T *)nullptr : Vec.begin()),
           Length(Vec.size()) {}
 #if LLVM_GNUC_PREREQ(9, 0, 0)



More information about the llvm-commits mailing list