[llvm] 898b961 - [ADT] Fix llvm::join on containers of char*s (#67113)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 28 05:45:08 PDT 2023
Author: Sam McCall
Date: 2023-09-28T14:45:02+02:00
New Revision: 898b961885c8589b9b75ac7daa083843d9d00f79
URL: https://github.com/llvm/llvm-project/commit/898b961885c8589b9b75ac7daa083843d9d00f79
DIFF: https://github.com/llvm/llvm-project/commit/898b961885c8589b9b75ac7daa083843d9d00f79.diff
LOG: [ADT] Fix llvm::join on containers of char*s (#67113)
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.
Added:
Modified:
llvm/include/llvm/ADT/StringExtras.h
llvm/unittests/ADT/StringExtrasTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/StringExtras.h b/llvm/include/llvm/ADT/StringExtras.h
index 30397b23ab03b37..091a40dc8afd561 100644
--- a/llvm/include/llvm/ADT/StringExtras.h
+++ b/llvm/include/llvm/ADT/StringExtras.h
@@ -418,7 +418,7 @@ inline std::string join_impl(IteratorT Begin, IteratorT End,
size_t Len = (std::distance(Begin, End) - 1) * Separator.size();
for (IteratorT I = Begin; I != End; ++I)
- Len += (*I).size();
+ Len += StringRef(*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..3f69c91b270a355 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,17 @@ 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, JoinItems) {
const char *Foo = "foo";
std::string Bar = "bar";
More information about the llvm-commits
mailing list