From 225396c2b52a50fa8a781e9a7b5cc81dceae010a Mon Sep 17 00:00:00 2001 From: "Tareq A. Siraj" Date: Mon, 12 Aug 2013 17:10:49 +0000 Subject: [PATCH] Fixes a bug when iterating on paths This fixes the incorrect implementation of iterating on file/directory paths. Differential Review: http://llvm-reviews.chandlerc.com/D1277 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@188183 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/Path.cpp | 2 +- unittests/Support/Path.cpp | 69 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/lib/Support/Path.cpp b/lib/Support/Path.cpp index cfd9ed6db3d..366fb849338 100644 --- a/lib/Support/Path.cpp +++ b/lib/Support/Path.cpp @@ -77,7 +77,7 @@ namespace { return path.substr(0, 1); // * {file,directory}name - size_t end = path.find_first_of(separators, 2); + size_t end = path.find_first_of(separators); return path.substr(0, end); } diff --git a/unittests/Support/Path.cpp b/unittests/Support/Path.cpp index 6f5992b063c..cc8731b3c07 100644 --- a/unittests/Support/Path.cpp +++ b/unittests/Support/Path.cpp @@ -141,6 +141,75 @@ TEST(Support, Path) { } } +TEST(Support, RelativePathIterator) { + SmallString<64> Path(StringRef("c/d/e/foo.txt")); + typedef SmallVector PathComponents; + PathComponents ExpectedPathComponents; + PathComponents ActualPathComponents; + + StringRef(Path).split(ExpectedPathComponents, "/"); + + for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; + ++I) { + ActualPathComponents.push_back(*I); + } + + ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); + + for (size_t i = 0; i Path(StringRef("/c/d/e/foo.txt")); + typedef SmallVector PathComponents; + PathComponents ExpectedPathComponents; + PathComponents ActualPathComponents; + + StringRef(Path).split(ExpectedPathComponents, "/"); + + // The root path will also be a component when iterating + ExpectedPathComponents[0] = "/"; + + for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; + ++I) { + ActualPathComponents.push_back(*I); + } + + ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); + + for (size_t i = 0; i Path(StringRef("c:\\c\\e\\foo.txt")); + typedef SmallVector PathComponents; + PathComponents ExpectedPathComponents; + PathComponents ActualPathComponents; + + StringRef(Path).split(ExpectedPathComponents, "\\"); + + // The root path (which comes after the drive name) will also be a component + // when iterating. + ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\"); + + for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; + ++I) { + ActualPathComponents.push_back(*I); + } + + ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); + + for (size_t i = 0; i