cpp11-migrate: Fixed path problem with include/exclude paths

This fixes a problem when the path separator in the include/exclude
directory is different (e.g. "\" vs. "/") from the path separator in
the file path we are modifying.

Differential Revision: http://llvm-reviews.chandlerc.com/D1326

llvm-svn: 188094
This commit is contained in:
Tareq A. Siraj 2013-08-09 19:24:05 +00:00
parent bc7bfb1bee
commit 9111258ad7
3 changed files with 34 additions and 8 deletions

View File

@ -22,17 +22,17 @@
using namespace llvm;
/// A string type to represent paths.
typedef SmallString<64> PathString;
namespace {
/// \brief Helper function to determine whether a file has the same path
/// prefix as \a Path.
///
/// \a File shouldn't contain relative operators i.e. ".." or "." since Path
/// comes from the include/exclude list of paths in which relative operators
/// were removed.
/// \a Path must be an absolute path.
bool fileHasPathPrefix(StringRef File, StringRef Path) {
// Converts File to its absolute path.
SmallString<64> AbsoluteFile = File;
PathString AbsoluteFile = File;
sys::fs::make_absolute(AbsoluteFile);
// Convert path strings to sys::path to iterate over each of its directories.
@ -43,7 +43,10 @@ bool fileHasPathPrefix(StringRef File, StringRef Path) {
while (FileI != FileE && PathI != PathE) {
// If the strings aren't equal then the two paths aren't contained within
// each other.
if (!FileI->equals(*PathI))
bool IsSeparator = ((FileI->size() == 1) && (PathI->size() == 1) &&
sys::path::is_separator((*FileI)[0]) &&
sys::path::is_separator((*PathI)[0]));
if (!FileI->equals(*PathI) && !IsSeparator)
return false;
++FileI;
++PathI;
@ -53,6 +56,7 @@ bool fileHasPathPrefix(StringRef File, StringRef Path) {
/// \brief Helper function for removing relative operators from a given
/// path i.e. "..", ".".
/// \a Path must be a absolute path.
std::string removeRelativeOperators(StringRef Path) {
sys::path::const_iterator PathI = sys::path::begin(Path);
sys::path::const_iterator PathE = sys::path::end(Path);
@ -68,7 +72,7 @@ std::string removeRelativeOperators(StringRef Path) {
++PathI;
}
// Rebuild the new path.
SmallString<64> NewPath;
PathString NewPath;
for (SmallVectorImpl<StringRef>::iterator I = PathT.begin(), E = PathT.end();
I != E; ++I) {
llvm::sys::path::append(NewPath, *I);
@ -86,7 +90,7 @@ error_code parseCLInput(StringRef Line, std::vector<std::string> &List,
E = Tokens.end();
I != E; ++I) {
// Convert each path to its absolute path.
SmallString<64> Path = I->rtrim();
PathString Path = I->rtrim();
if (error_code Err = sys::fs::make_absolute(Path))
return Err;
// Remove relative operators from the path.

View File

@ -42,6 +42,10 @@ public:
/// \brief Determine if the given path is in the list of include paths but
/// not in the list of exclude paths.
///
/// \a FilePath shouldn't contain relative operators i.e. ".." or "." since
/// Path comes from the include/exclude list of paths in which relative
/// operators were removed.
bool isFileIncluded(llvm::StringRef FilePath) const;
private:

View File

@ -28,7 +28,7 @@
TEST(IncludeExcludeTest, ParseString) {
IncludeExcludeInfo IEManager;
llvm::error_code Err = IEManager.readListFromString(
/*include=*/ "a,b/b2,c/c2",
/*include=*/ "a,b/b2,c/c2,d/../d2/../d3",
/*exclude=*/ "a/af.cpp,a/a2,b/b2/b2f.cpp,c/c2");
ASSERT_EQ(Err, llvm::error_code::success());
@ -37,11 +37,14 @@ TEST(IncludeExcludeTest, ParseString) {
// transform. Files are not safe to transform by default.
EXPECT_FALSE(IEManager.isFileIncluded("f.cpp"));
EXPECT_FALSE(IEManager.isFileIncluded("b/dir/f.cpp"));
EXPECT_FALSE(IEManager.isFileIncluded("d/f.cpp"));
EXPECT_FALSE(IEManager.isFileIncluded("d2/f.cpp"));
// If the file appears on only the include list then it is safe to transform.
EXPECT_TRUE(IEManager.isFileIncluded("a/f.cpp"));
EXPECT_TRUE(IEManager.isFileIncluded("a/dir/f.cpp"));
EXPECT_TRUE(IEManager.isFileIncluded("b/b2/f.cpp"));
EXPECT_TRUE(IEManager.isFileIncluded("d3/f.cpp"));
// If the file appears on both the include or exclude list then it is not
// safe to transform.
@ -50,6 +53,21 @@ TEST(IncludeExcludeTest, ParseString) {
EXPECT_FALSE(IEManager.isFileIncluded("a/a2/dir/f.cpp"));
EXPECT_FALSE(IEManager.isFileIncluded("b/b2/b2f.cpp"));
EXPECT_FALSE(IEManager.isFileIncluded("c/c2/c3/f.cpp"));
#ifdef LLVM_ON_WIN32
// Check for cases when the path separators are different between the path
// that was read and the path that we are checking for. This can happen on
// windows where lit provides "\" and the test has "/".
ASSERT_NO_ERROR(IEManager.readListFromString(
/*include=*/ "C:\\foo,a\\b/c,a/../b\\c/..\\d",
/*exclude=*/ "C:\\bar"
));
EXPECT_TRUE(IEManager.isFileIncluded("C:/foo/code.h"));
EXPECT_FALSE(IEManager.isFileIncluded("C:/bar/code.h"));
EXPECT_TRUE(IEManager.isFileIncluded("a/b\\c/code.h"));
EXPECT_FALSE(IEManager.isFileIncluded("b\\c/code.h"));
EXPECT_TRUE(IEManager.isFileIncluded("b/d\\code.h"));
#endif
}
TEST(IncludeExcludeTest, ParseStringCases) {