[llvm] [ADT] Fix llvm::join on containers of char*s (PR #67113)
Sam McCall via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 22 03:36:09 PDT 2023
https://github.com/sam-mccall updated https://github.com/llvm/llvm-project/pull/67113
>From a834970b321c39183def162cef81019190a614f1 Mon Sep 17 00:00:00 2001
From: Sam McCall <sam.mccall at gmail.com>
Date: Fri, 22 Sep 2023 12:32:03 +0200
Subject: [PATCH 1/2] [ADT] Fix llvm::join on containers of char*s
Currently it tries to call S.size() when preallocating the target string,
which doesn't compile.
vector<const char*> is used a bunch in e.g. clang driver.
---
llvm/include/llvm/ADT/StringExtras.h | 10 ++++++++--
llvm/unittests/ADT/StringExtrasTest.cpp | 17 +++++++++++++++--
2 files changed, 23 insertions(+), 4 deletions(-)
diff --git a/llvm/include/llvm/ADT/StringExtras.h b/llvm/include/llvm/ADT/StringExtras.h
index 30397b23ab03b37..b2c400f512695e8 100644
--- a/llvm/include/llvm/ADT/StringExtras.h
+++ b/llvm/include/llvm/ADT/StringExtras.h
@@ -26,6 +26,7 @@
#include <cstring>
#include <iterator>
#include <string>
+#include <type_traits>
#include <utility>
namespace llvm {
@@ -417,8 +418,13 @@ inline std::string join_impl(IteratorT Begin, IteratorT End,
return S;
size_t Len = (std::distance(Begin, End) - 1) * Separator.size();
- for (IteratorT I = Begin; I != End; ++I)
- Len += (*I).size();
+ for (IteratorT I = Begin; I != End; ++I) {
+ if constexpr (std::is_same_v<std::remove_reference_t<decltype(*I)>,
+ const char *>)
+ Len += strlen(*I);
+ else
+ Len += (*I).size();
+ }
S.reserve(Len);
size_t PrevCapacity = S.capacity();
(void)PrevCapacity;
diff --git a/llvm/unittests/ADT/StringExtrasTest.cpp b/llvm/unittests/ADT/StringExtrasTest.cpp
index 971560b8eb8c4ee..0fead285e3d9d8a 100644
--- a/llvm/unittests/ADT/StringExtrasTest.cpp
+++ b/llvm/unittests/ADT/StringExtrasTest.cpp
@@ -59,8 +59,8 @@ TEST(StringExtrasTest, isUpper) {
EXPECT_FALSE(isUpper('\?'));
}
-TEST(StringExtrasTest, Join) {
- std::vector<std::string> Items;
+template <class ContainerT> void testJoin() {
+ ContainerT Items;
EXPECT_EQ("", join(Items.begin(), Items.end(), " <sep> "));
Items = {"foo"};
@@ -74,6 +74,19 @@ TEST(StringExtrasTest, Join) {
join(Items.begin(), Items.end(), " <sep> "));
}
+TEST(StringExtrasTest, Join) {
+ {
+ SCOPED_TRACE("std::vector<std::string>");
+ testJoin<std::vector<std::string>>();
+ }
+ {
+ SCOPED_TRACE("std::vector<const char*>");
+ testJoin<std::vector<const char *>>();
+ }
+}
+
+TEST(StringExtrasTest, JoinCStrings) { std::vector<const char *> Items; }
+
TEST(StringExtrasTest, JoinItems) {
const char *Foo = "foo";
std::string Bar = "bar";
>From 8eec9bc7e4bd0abdc7f959af5ee1a79bce93f908 Mon Sep 17 00:00:00 2001
From: Sam McCall <sam.mccall at gmail.com>
Date: Fri, 22 Sep 2023 12:35:43 +0200
Subject: [PATCH 2/2] drop leftover test stuff
---
llvm/unittests/ADT/StringExtrasTest.cpp | 2 --
1 file changed, 2 deletions(-)
diff --git a/llvm/unittests/ADT/StringExtrasTest.cpp b/llvm/unittests/ADT/StringExtrasTest.cpp
index 0fead285e3d9d8a..3f69c91b270a355 100644
--- a/llvm/unittests/ADT/StringExtrasTest.cpp
+++ b/llvm/unittests/ADT/StringExtrasTest.cpp
@@ -85,8 +85,6 @@ TEST(StringExtrasTest, Join) {
}
}
-TEST(StringExtrasTest, JoinCStrings) { std::vector<const char *> Items; }
-
TEST(StringExtrasTest, JoinItems) {
const char *Foo = "foo";
std::string Bar = "bar";
More information about the llvm-commits
mailing list