[PATCH] -include option and -exclude ignore arguments finishing with '/'
Ariel Bernal
ariel.j.bernal at intel.com
Mon Jul 29 10:20:32 PDT 2013
Added a new function to pre-process include/exclude paths and remove from the path any relative operation i.e. "..", '.".
Hi Sarcasm,
http://llvm-reviews.chandlerc.com/D1134
CHANGE SINCE LAST DIFF
http://llvm-reviews.chandlerc.com/D1134?vs=2783&id=3053#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
@@ -48,6 +48,34 @@
return true;
}
+/// \brief Helper function for removing relative operations from an absolute
+/// path i.e. "..", ".".
+void RemoveRelativePath(SmallString<64>& AbsPath) {
+ sys::path::const_iterator PathI = sys::path::begin(AbsPath);
+ sys::path::const_iterator PathE = sys::path::end(AbsPath);
+ std::vector<StringRef> NewPath;
+ while (PathI != PathE) {
+ if (PathI->equals("..")) {
+ NewPath.pop_back();
+ // If this codition is reached then the given path is incorrect.
+ if (NewPath.size() == 0) {
+ llvm::errs() << "Incorrect path provided: " << AbsPath << "\n";
+ AbsPath = "";
+ return;
+ }
+ } else if (!PathI->equals("."))
+ NewPath.push_back(*PathI);
+ ++PathI;
+ }
+ // Recreate new absolute path.
+ AbsPath = "";
+ for (std::vector<StringRef>::iterator I = NewPath.begin(),
+ E = NewPath.end();
+ I != E; ++I) {
+ llvm::sys::path::append(AbsPath, *I);
+ }
+}
+
/// \brief Helper function to tokenize a string of paths and populate
/// the vector.
error_code parseCLInput(StringRef Line, std::vector<std::string> &List,
@@ -61,12 +89,10 @@
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
+ // Remove from the path any relative operation.
+ RemoveRelativePath(Path);
+ // Add only non-empty paths to the list.
+ if (Path.size() > 0)
List.push_back(Path.str());
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,19 @@
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/../../c2/",
+ /*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/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.2.patch
Type: text/x-patch
Size: 2854 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130729/ccb253e7/attachment.bin>
More information about the cfe-commits
mailing list