Implement filesystem NB comments, relative paths, and related issues.
This is a fairly large patch that implements all of the filesystem NB comments
and the relative paths changes (ex. adding weakly_canonical). These issues
and papers are all interrelated so their implementation couldn't be split up
nicely.
This patch upgrades <experimental/filesystem> to match the C++17 spec and not
the published experimental TS spec. Some of the changes in this patch are both
API and ABI breaking, however libc++ makes no guarantee about stability for
experimental implementations.
The major changes in this patch are:
* Implement NB comments for filesystem (P0492R2), including:
* Implement `perm_options` enum as part of NB comments, and update the
`permissions` function to match.
* Implement changes to `remove_filename` and `replace_filename`
* Implement changes to `path::stem()` and `path::extension()` which support
splitting examples like `.profile`.
* Change path iteration to return an empty path instead of '.' for trailing
separators.
* Change `operator/=` to handle absolute paths on the RHS.
* Change `absolute` to no longer accept a current path argument.
* Implement relative paths according to NB comments (P0219r1)
* Combine `path.cpp` and `operations.cpp` since some path functions require
access to the operations internals, and some fs operations require access
to the path parser.
llvm-svn: 329028
diff --git a/libcxx/test/support/verbose_assert.h b/libcxx/test/support/verbose_assert.h
new file mode 100644
index 0000000..353e71c
--- /dev/null
+++ b/libcxx/test/support/verbose_assert.h
@@ -0,0 +1,222 @@
+#ifndef TEST_SUPPORT_VERBOSE_ASSERT
+#define TEST_SUPPORT_VERBOSE_ASSERT
+
+#include <iostream>
+#include <cstdio>
+#include <sstream>
+#include <string>
+#include "test_macros.h"
+
+namespace verbose_assert {
+
+typedef std::basic_ostream<char>&(EndLType)(std::basic_ostream<char>&);
+
+template <class Stream, class Tp,
+ class = decltype(std::declval<Stream&>() << std::declval<Tp const&>())>
+std::true_type IsStreamableImp(int);
+template <class Stream, class Tp> std::false_type IsStreamableImp(long);
+
+template <class Stream, class Tp>
+struct IsStreamable : decltype(IsStreamableImp<Stream, Tp>(0)) {};
+
+template <class Tp, int ST = (IsStreamable<decltype(std::cerr), Tp>::value ? 1
+ : (IsStreamable<decltype(std::wcerr), Tp>::value ? 2 : -1))>
+struct SelectStream {
+ static_assert(ST == -1, "specialization required for ST != -1");
+ static void Print(Tp const&) { std::clog << "Value Not Streamable!\n"; }
+};
+
+template <class Tp>
+struct SelectStream<Tp, 1> {
+ static void Print(Tp const& val) { std::cerr << val; }
+};
+
+template <class Tp>
+struct SelectStream<Tp, 2> {
+ static void Print(Tp const& val) { std::wcerr << val; }
+};
+
+struct AssertData {
+ AssertData(const char* xcheck, const char* xfile, const char* xfunc,
+ unsigned long xline, bool xpassed = true)
+ : passed(xpassed), check(xcheck), file(xfile), func(xfunc), line(xline),
+ msg() {}
+
+ AssertData& SetFailed(std::string xmsg = std::string()) {
+ msg = xmsg;
+ passed = false;
+ return *this;
+ }
+
+ void PrintFailed() const {
+ std::fprintf(stderr, "%s:%lu %s: Assertion '%s' failed.\n", file, line,
+ func, check);
+ if (!msg.empty())
+ std::fprintf(stderr, "%s\n", msg.data());
+ }
+
+ bool passed;
+ const char* check;
+ const char* file;
+ const char* func;
+ unsigned long line;
+ std::string msg;
+};
+
+// AssertHandler is the class constructed by failing CHECK macros. AssertHandler
+// will log information about the failures and abort when it is destructed.
+class AssertHandler {
+public:
+ AssertHandler(AssertData const& Data)
+ : passed(Data.passed) {
+ if (!passed)
+ Data.PrintFailed();
+ }
+
+ ~AssertHandler() TEST_NOEXCEPT_FALSE {
+ if (!passed) {
+ error_log << std::endl;
+ std::abort();
+ }
+ }
+
+ class LogType {
+ friend class AssertHandler;
+
+ template <class Tp>
+ friend LogType& operator<<(LogType& log, Tp const& value) {
+ if (!log.is_disabled) {
+ SelectStream<Tp>::Print(value);
+ }
+ return log;
+ }
+
+ friend LogType& operator<<(LogType& log, EndLType* m) {
+ if (!log.is_disabled) {
+ SelectStream<EndLType*>::Print(m);
+ }
+ return log;
+ }
+
+ private:
+ LogType(bool disable) : is_disabled(disable) {}
+ bool is_disabled;
+
+ LogType(LogType const&);
+ LogType& operator=(LogType const&);
+ };
+
+ LogType& GetLog() {
+ if (passed)
+ return null_log;
+ return error_log;
+ }
+
+private:
+ static LogType null_log;
+ static LogType error_log;
+
+ AssertHandler& operator=(const AssertHandler&) = delete;
+ AssertHandler(const AssertHandler&) = delete;
+ AssertHandler() = delete;
+
+private:
+ bool passed;
+};
+
+AssertHandler::LogType AssertHandler::null_log(true);
+AssertHandler::LogType AssertHandler::error_log(false);
+
+template <class It1>
+std::string PrintRange(const char* Name, It1 F, It1 E) {
+ std::stringstream ss;
+ ss << " " << Name << " = [";
+ while (F != E) {
+ ss << *F;
+ ++F;
+ if (F != E)
+ ss << ", ";
+ }
+ ss << "]\n";
+ return ss.str();
+}
+
+template <class Tp, class Up>
+std::string PrintMismatch(Tp const& LHS, Up const& RHS, int Elem) {
+ std::stringstream ss;
+ ss << " Element " << Elem << " mismatched: `" << LHS << "` != `" << RHS
+ << "`!\n";
+ return ss.str();
+};
+
+struct EqualToComp {
+ template <class Tp, class Up>
+ bool operator()(Tp const& LHS, Up const& RHS) const {
+ return LHS == RHS;
+ }
+};
+
+template <class It1, class It2, class Comp>
+AssertData CheckCollectionsEqual(It1 F1, It1 E1, It2 F2, It2 E2,
+ AssertData Data, Comp C = EqualToComp()) {
+ const It1 F1Orig = F1;
+ const It2 F2Orig = F2;
+ bool Failed = false;
+ std::string ErrorMsg;
+ int Idx = 0;
+ while (F1 != E1 && F2 != E2) {
+ if (!(C(*F1, *F2))) {
+ ErrorMsg += PrintMismatch(*F1, *F2, Idx);
+ Failed = true;
+ break;
+ }
+ ++Idx;
+ ++F1;
+ ++F2;
+ }
+ if (!Failed && (F1 != E1 || F2 != E2)) {
+ ErrorMsg += " Ranges have different sizes!\n";
+ Failed = true;
+ }
+ if (Failed) {
+ ErrorMsg += PrintRange("LHS", F1Orig, E1);
+ ErrorMsg += PrintRange("RHS", F2Orig, E2);
+ Data.SetFailed(ErrorMsg);
+ }
+ return Data;
+}
+} // namespace verbose_assert
+
+#ifdef __GNUC__
+#define ASSERT_FN_NAME() __PRETTY_FUNCTION__
+#else
+#define ASSERT_FN_NAME() __func__
+#endif
+
+#define DISPLAY(...) " " #__VA_ARGS__ " = " << (__VA_ARGS__) << "\n"
+
+#define ASSERT(...) \
+ ::verbose_assert::AssertHandler(::verbose_assert::AssertData( \
+ #__VA_ARGS__, __FILE__, ASSERT_FN_NAME(), __LINE__,(__VA_ARGS__))).GetLog()
+
+#define ASSERT_EQ(LHS, RHS) \
+ ASSERT(LHS == RHS) << DISPLAY(LHS) << DISPLAY(RHS)
+#define ASSERT_NEQ(LHS, RHS) \
+ ASSERT(LHS != RHS) << DISPLAY(LHS) << DISPLAY(RHS)
+#define ASSERT_PRED(PRED, LHS, RHS) \
+ ASSERT(PRED(LHS, RHS)) << DISPLAY(LHS) << DISPLAY(RHS)
+
+#define ASSERT_COLLECTION_EQ_COMP(F1, E1, F2, E2, Comp) \
+ (::verbose_assert::AssertHandler( \
+ ::verbose_assert::CheckCollectionsEqual( \
+ F1, E1, F2, E2, \
+ ::verbose_assert::AssertData("CheckCollectionsEqual(" #F1 ", " #E1 \
+ ", " #F2 ", " #E2 ")", \
+ __FILE__, ASSERT_FN_NAME(), __LINE__), \
+ Comp)) \
+ .GetLog())
+
+#define ASSERT_COLLECTION_EQ(F1, E1, F2, E2) \
+ ASSERT_COLLECTION_EQ_COMP(F1, E1, F2, E2, ::verbose_assert::EqualToComp())
+
+#endif