Skip to content

Commit ba5628f

Browse files
committed
ADT: Use 'using' to inherit assign and append in SmallString
Rather than reimplement, use a `using` declaration to bring in `SmallVectorImpl<char>`'s assign and append implementations in `SmallString`. The `SmallString` versions were missing reference invalidation assertions from `SmallVector`. This patch also fixes a bug in `llvm::FileCollector::addFileImpl`, which was a copy/paste from `clang::ModuleDependencyCollector::copyToRoot`, both caught by the no-longer-skipped assertions. As a drive-by, this also sinks the `const SmallVectorImpl&` versions of these methods down into `SmallVectorImpl`, since I imagine they'd be useful elsewhere. Differential Revision: https://reviews.llvm.org/D95202
1 parent 47e95e8 commit ba5628f

File tree

5 files changed

+32
-42
lines changed

5 files changed

+32
-42
lines changed

clang/lib/Frontend/ModuleDependencyCollector.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,17 @@ std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src,
190190
// Canonicalize src to a native path to avoid mixed separator styles.
191191
path::native(AbsoluteSrc);
192192
// Remove redundant leading "./" pieces and consecutive separators.
193-
AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
193+
StringRef TrimmedAbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
194194

195195
// Canonicalize the source path by removing "..", "." components.
196-
SmallString<256> VirtualPath = AbsoluteSrc;
196+
SmallString<256> VirtualPath = TrimmedAbsoluteSrc;
197197
path::remove_dots(VirtualPath, /*remove_dot_dot=*/true);
198198

199199
// If a ".." component is present after a symlink component, remove_dots may
200200
// lead to the wrong real destination path. Let the source be canonicalized
201201
// like that but make sure we always use the real path for the destination.
202202
SmallString<256> CopyFrom;
203-
if (!getRealPath(AbsoluteSrc, CopyFrom))
203+
if (!getRealPath(TrimmedAbsoluteSrc, CopyFrom))
204204
CopyFrom = VirtualPath;
205205
SmallString<256> CacheDst = getDest();
206206

llvm/include/llvm/ADT/SmallString.h

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,15 @@ class SmallString : public SmallVector<char, InternalLen> {
4040
template<typename ItTy>
4141
SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {}
4242

43-
// Note that in order to add new overloads for append & assign, we have to
44-
// duplicate the inherited versions so as not to inadvertently hide them.
45-
4643
/// @}
4744
/// @name String Assignment
4845
/// @{
4946

50-
/// Assign from a repeated element.
51-
void assign(size_t NumElts, char Elt) {
52-
this->SmallVectorImpl<char>::assign(NumElts, Elt);
53-
}
54-
55-
/// Assign from an iterator pair.
56-
template<typename in_iter>
57-
void assign(in_iter S, in_iter E) {
58-
this->clear();
59-
SmallVectorImpl<char>::append(S, E);
60-
}
47+
using SmallVector<char, InternalLen>::assign;
6148

6249
/// Assign from a StringRef.
6350
void assign(StringRef RHS) {
64-
this->clear();
65-
SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
66-
}
67-
68-
/// Assign from a SmallVector.
69-
void assign(const SmallVectorImpl<char> &RHS) {
70-
this->clear();
71-
SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
51+
SmallVectorImpl<char>::assign(RHS.begin(), RHS.end());
7252
}
7353

7454
/// Assign from a list of StringRefs.
@@ -81,26 +61,13 @@ class SmallString : public SmallVector<char, InternalLen> {
8161
/// @name String Concatenation
8262
/// @{
8363

84-
/// Append from an iterator pair.
85-
template<typename in_iter>
86-
void append(in_iter S, in_iter E) {
87-
SmallVectorImpl<char>::append(S, E);
88-
}
89-
90-
void append(size_t NumInputs, char Elt) {
91-
SmallVectorImpl<char>::append(NumInputs, Elt);
92-
}
64+
using SmallVector<char, InternalLen>::append;
9365

9466
/// Append from a StringRef.
9567
void append(StringRef RHS) {
9668
SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
9769
}
9870

99-
/// Append from a SmallVector.
100-
void append(const SmallVectorImpl<char> &RHS) {
101-
SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
102-
}
103-
10471
/// Append from a list of StringRefs.
10572
void append(std::initializer_list<StringRef> Refs) {
10673
size_t SizeNeeded = this->size();

llvm/include/llvm/ADT/SmallVector.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,8 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> {
664664
append(IL.begin(), IL.end());
665665
}
666666

667+
void append(const SmallVectorImpl &RHS) { append(RHS.begin(), RHS.end()); }
668+
667669
void assign(size_type NumElts, ValueParamT Elt) {
668670
// Note that Elt could be an internal reference.
669671
if (NumElts > this->capacity()) {
@@ -698,6 +700,8 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> {
698700
append(IL);
699701
}
700702

703+
void assign(const SmallVectorImpl &RHS) { assign(RHS.begin(), RHS.end()); }
704+
701705
iterator erase(const_iterator CI) {
702706
// Just cast away constness because this is a non-const member function.
703707
iterator I = const_cast<iterator>(CI);

llvm/lib/Support/FileCollector.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,18 @@ void FileCollector::addFileImpl(StringRef SrcPath) {
8686
sys::path::native(AbsoluteSrc);
8787

8888
// Remove redundant leading "./" pieces and consecutive separators.
89-
AbsoluteSrc = sys::path::remove_leading_dotslash(AbsoluteSrc);
89+
StringRef TrimmedAbsoluteSrc =
90+
sys::path::remove_leading_dotslash(AbsoluteSrc);
9091

9192
// Canonicalize the source path by removing "..", "." components.
92-
SmallString<256> VirtualPath = AbsoluteSrc;
93+
SmallString<256> VirtualPath = TrimmedAbsoluteSrc;
9394
sys::path::remove_dots(VirtualPath, /*remove_dot_dot=*/true);
9495

9596
// If a ".." component is present after a symlink component, remove_dots may
9697
// lead to the wrong real destination path. Let the source be canonicalized
9798
// like that but make sure we always use the real path for the destination.
9899
SmallString<256> CopyFrom;
99-
if (!getRealPath(AbsoluteSrc, CopyFrom))
100+
if (!getRealPath(TrimmedAbsoluteSrc, CopyFrom))
100101
CopyFrom = VirtualPath;
101102

102103
SmallString<256> DstPath = StringRef(Root);

llvm/unittests/ADT/SmallVectorTest.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,15 @@ TYPED_TEST(SmallVectorTest, AppendRepeatedNonForwardIterator) {
485485
this->assertValuesInOrder(this->theVector, 3u, 1, 7, 7);
486486
}
487487

488+
TYPED_TEST(SmallVectorTest, AppendSmallVector) {
489+
SCOPED_TRACE("AppendSmallVector");
490+
491+
SmallVector<Constructable, 3> otherVector = {7, 7};
492+
this->theVector.push_back(Constructable(1));
493+
this->theVector.append(otherVector);
494+
this->assertValuesInOrder(this->theVector, 3u, 1, 7, 7);
495+
}
496+
488497
// Assign test
489498
TYPED_TEST(SmallVectorTest, AssignTest) {
490499
SCOPED_TRACE("AssignTest");
@@ -513,6 +522,15 @@ TYPED_TEST(SmallVectorTest, AssignNonIterTest) {
513522
this->assertValuesInOrder(this->theVector, 2u, 7, 7);
514523
}
515524

525+
TYPED_TEST(SmallVectorTest, AssignSmallVector) {
526+
SCOPED_TRACE("AssignSmallVector");
527+
528+
SmallVector<Constructable, 3> otherVector = {7, 7};
529+
this->theVector.push_back(Constructable(1));
530+
this->theVector.assign(otherVector);
531+
this->assertValuesInOrder(this->theVector, 2u, 7, 7);
532+
}
533+
516534
// Move-assign test
517535
TYPED_TEST(SmallVectorTest, MoveAssignTest) {
518536
SCOPED_TRACE("MoveAssignTest");

0 commit comments

Comments
 (0)