summaryrefslogtreecommitdiffstats
path: root/Source/Checks/cm_cxx_filesystem.cxx
blob: bdc7c6d743101ff1ad1f981ebe366dd33a2d336d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

#include <filesystem>
#if defined(__GLIBCXX__)
#  include <string_view>
#endif

int main()
{
  std::filesystem::path p0(L"/a/b/c");

  std::filesystem::path p1("/a/b/c");
  std::filesystem::path p2("/a/b//c");
  if (p1 != p2.lexically_normal()) {
    return 1;
  }

#if defined(_WIN32)
  // "//host/" is not preserved in some environments like GNU under MinGW.
  std::filesystem::path p3("//host/a/b/../c");
  if (p3.lexically_normal().generic_string() != "//host/a/c") {
    return 1;
  }

  std::filesystem::path p4("c://a/.///b/../");
  if (p4.lexically_normal().generic_string() != "c:/a/") {
    return 1;
  }

  std::filesystem::path b1("C:\\path\\y\\..\\");
  if (std::filesystem::weakly_canonical("\\\\?\\C:\\path\\x\\..") !=
      b1.lexically_normal()) {
    return 1;
  }
#endif

  // If std::string is copy-on-write, the std::filesystem::path
  // implementation may accidentally trigger a reallocation and compute
  // an offset between two allocations, leading to undefined behavior.
#if defined(__GLIBCXX__) &&                                                   \
  (!defined(_GLIBCXX_USE_CXX11_ABI) || !_GLIBCXX_USE_CXX11_ABI)
  std::string p5s1 = "/path";
  std::string p5s2 = std::move(p5s1);
  std::filesystem::path p5 = std::string(p5s2);
  p5.remove_filename();
#endif

#if defined(__GLIBCXX__)
  // RH gcc-toolset-10 has a strange bug: it selects, in some circumstances,
  // the wrong constructor which generate error in template instantiation.
  class my_string_view : std::string_view
  {
  public:
    my_string_view(const char* p)
      : std::string_view(p)
    {
    }
  };
  class my_path
  {
  public:
    my_path(std::filesystem::path path) {}

    my_path(my_string_view path) {}
  };

  my_path p{ my_string_view{ "abc" } };
  // here is the bug: the constructor taking std::filesystem::path as argument
  // is selected, so the compiler try to build a std::filesystem::path instance
  // from the my_string_view argument and fails to do so.
#endif

  return 0;
}