[PATCH] -include option and -exclude ignore arguments finishing with '/'

Ariel Bernal ariel.j.bernal at intel.com
Tue Jul 30 10:40:34 PDT 2013


  FIxed test

Hi Sarcasm,

http://llvm-reviews.chandlerc.com/D1134

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D1134?vs=3070&id=3071#toc

Files:
  cpp11-migrate/Core/IncludeExcludeInfo.cpp
  unittests/cpp11-migrate/IncludeExcludeTest.cpp

Index: cpp11-migrate/Core/IncludeExcludeInfo.cpp
===================================================================
--- cpp11-migrate/Core/IncludeExcludeInfo.cpp
+++ cpp11-migrate/Core/IncludeExcludeInfo.cpp
@@ -26,6 +26,9 @@
 /// \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.
@@ -48,6 +51,33 @@
   return true;
 }
 
+/// \brief Helper function for removing relative operations from a given
+/// path i.e. "..", ".".
+std::string removeRelativeOperators(StringRef Path) {
+  sys::path::const_iterator PathI = sys::path::begin(Path);
+  sys::path::const_iterator PathE = sys::path::end(Path);
+  SmallVector<StringRef, 32> PathT;
+  while (PathI != PathE) {
+    if (PathI->equals("..")) {
+      // Test if we have reached the root.
+      if (PathT.empty()) {
+        llvm::errs() << "Unable to remove relative operators: " << Path << "\n";
+        return "";
+      }
+      PathT.pop_back();
+    } else if (!PathI->equals("."))
+      PathT.push_back(*PathI);
+    ++PathI;
+  }
+  // Recreate new absolute path.
+  SmallString<64> NewPath;
+  for (SmallVectorImpl<StringRef>::iterator I = PathT.begin(), E = PathT.end();
+       I != E; ++I) {
+    llvm::sys::path::append(NewPath, *I);
+  }
+  return NewPath.str();
+}
+
 /// \brief Helper function to tokenize a string of paths and populate
 /// the vector.
 error_code parseCLInput(StringRef Line, std::vector<std::string> &List,
@@ -61,13 +91,11 @@
     SmallString<64> Path = I->rtrim();
     if (error_code Err = sys::fs::make_absolute(Path))
       return Err;
-
-    // sys::fs::make_absolute will turn "." into "<pwd>/.". Need to strip "/."
-    // off or it interferes with prefix checking.
-    if (Path.endswith("/."))
-      List.push_back(Path.slice(0, Path.size() - 2).str());
-    else
-      List.push_back(Path.str());
+    // Remove from the path any relative operation.
+    std::string AbsPath = removeRelativeOperators(Path);
+    // Add only non-empty paths to the list.
+    if (!AbsPath.empty())
+      List.push_back(AbsPath);
 
     llvm::errs() << "Parse: " <<List.back() << "\n";
   }
Index: unittests/cpp11-migrate/IncludeExcludeTest.cpp
===================================================================
--- unittests/cpp11-migrate/IncludeExcludeTest.cpp
+++ unittests/cpp11-migrate/IncludeExcludeTest.cpp
@@ -52,6 +52,23 @@
   EXPECT_FALSE(IEManager.isFileIncluded("c/c2/c3/f.cpp"));
 }
 
+TEST(IncludeExcludeTest, ParseStringCases) {
+  IncludeExcludeInfo IEManager;
+  llvm::error_code Err = IEManager.readListFromString(
+      /*include=*/  "a/.,b/b2/,c/c2/c3/../../c4/,d/d2/./d3/,/e/e2/.",
+      /*exclude=*/ "");
+
+  ASSERT_EQ(Err, llvm::error_code::success());
+
+  EXPECT_TRUE(IEManager.isFileIncluded("a/f.cpp"));
+  EXPECT_TRUE(IEManager.isFileIncluded("b/b2/f.cpp"));
+  EXPECT_TRUE(IEManager.isFileIncluded("c/c4/f.cpp"));
+  EXPECT_TRUE(IEManager.isFileIncluded("d/d2/d3/f.cpp"));
+  EXPECT_TRUE(IEManager.isFileIncluded("/e/e2/f.cpp"));
+
+  EXPECT_FALSE(IEManager.isFileIncluded("c/c2/c3/f.cpp"));
+}
+
 // Utility for creating and filling files with data for IncludeExcludeFileTest
 // tests.
 struct InputFiles {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1134.4.patch
Type: text/x-patch
Size: 3489 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130730/7588f3ad/attachment.bin>


More information about the cfe-commits mailing list