[libcxx] r337883 - Ensure path::iterator and PathParser share the same enumeration values.

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 24 20:31:48 PDT 2018


Author: ericwf
Date: Tue Jul 24 20:31:48 2018
New Revision: 337883

URL: http://llvm.org/viewvc/llvm-project?rev=337883&view=rev
Log:
Ensure path::iterator and PathParser share the same enumeration values.

To avoid exposing implementation details, path::iterator and PathParser
both implicitly used the same set of values to represent the state,
but they were defined twice. This could have lead to a mismatch
occuring.

This patch moves all of the parser state values into the filesystem
header and changes PathParser to use those value to avoid this.

Modified:
    libcxx/trunk/include/experimental/filesystem
    libcxx/trunk/src/experimental/filesystem/operations.cpp

Modified: libcxx/trunk/include/experimental/filesystem
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/experimental/filesystem?rev=337883&r1=337882&r2=337883&view=diff
==============================================================================
--- libcxx/trunk/include/experimental/filesystem (original)
+++ libcxx/trunk/include/experimental/filesystem Tue Jul 24 20:31:48 2018
@@ -1037,7 +1037,7 @@ public:
     _LIBCPP_INLINE_VISIBILITY path extension()      const { return string_type(__extension()); }
 
     // query
-    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY 
+    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
     bool empty() const _NOEXCEPT { return __pn_.empty(); }
 
     _LIBCPP_INLINE_VISIBILITY bool has_root_name()      const { return !__root_name().empty(); }
@@ -1170,6 +1170,17 @@ u8path(_InputIt __f, _InputIt __l) {
 class _LIBCPP_TYPE_VIS path::iterator
 {
 public:
+    enum _ParserState : unsigned char {
+      _Singular,
+      _BeforeBegin,
+      _InRootName,
+      _InRootDir,
+      _InFilenames,
+      _InTrailingSep,
+      _AtEnd
+    };
+
+public:
     typedef bidirectional_iterator_tag iterator_category;
 
     typedef path                       value_type;
@@ -1178,10 +1189,11 @@ public:
     typedef const path&                reference;
 
     typedef void __stashing_iterator_tag; // See reverse_iterator and __is_stashing_iterator
+
 public:
     _LIBCPP_INLINE_VISIBILITY
     iterator() : __stashed_elem_(), __path_ptr_(nullptr),
-                 __entry_(), __state_(__singular) {}
+                 __entry_(), __state_(_Singular) {}
 
     iterator(const iterator&) = default;
     ~iterator() = default;
@@ -1200,9 +1212,9 @@ public:
 
     _LIBCPP_INLINE_VISIBILITY
     iterator& operator++() {
-        _LIBCPP_ASSERT(__state_ != __singular,
+        _LIBCPP_ASSERT(__state_ != _Singular,
                        "attempting to increment a singular iterator");
-        _LIBCPP_ASSERT(__state_ != __at_end,
+        _LIBCPP_ASSERT(__state_ != _AtEnd,
                       "attempting to increment the end iterator");
         return __increment();
     }
@@ -1216,7 +1228,7 @@ public:
 
     _LIBCPP_INLINE_VISIBILITY
     iterator& operator--() {
-        _LIBCPP_ASSERT(__state_ != __singular,
+        _LIBCPP_ASSERT(__state_ != _Singular,
                        "attempting to decrement a singular iterator");
         _LIBCPP_ASSERT(__entry_.data() != __path_ptr_->native().data(),
                        "attempting to decrement the begin iterator");
@@ -1233,9 +1245,6 @@ public:
 private:
     friend class path;
 
-    static constexpr unsigned char __singular = 0;
-    static constexpr unsigned char __at_end = 6;
-
     inline _LIBCPP_INLINE_VISIBILITY
     friend bool operator==(const iterator&, const iterator&);
 
@@ -1245,7 +1254,7 @@ private:
     path __stashed_elem_;
     const path* __path_ptr_;
     path::__string_view __entry_;
-    unsigned char __state_;
+    _ParserState __state_;
 };
 
 inline _LIBCPP_INLINE_VISIBILITY

Modified: libcxx/trunk/src/experimental/filesystem/operations.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/experimental/filesystem/operations.cpp?rev=337883&r1=337882&r2=337883&view=diff
==============================================================================
--- libcxx/trunk/src/experimental/filesystem/operations.cpp (original)
+++ libcxx/trunk/src/experimental/filesystem/operations.cpp Tue Jul 24 20:31:48 2018
@@ -57,12 +57,12 @@ using PosPtr = path::value_type const*;
 struct PathParser {
   enum ParserState : unsigned char {
     // Zero is a special sentinel value used by default constructed iterators.
-    PS_BeforeBegin = 1,
-    PS_InRootName,
-    PS_InRootDir,
-    PS_InFilenames,
-    PS_InTrailingSep,
-    PS_AtEnd
+    PS_BeforeBegin = path::iterator::_BeforeBegin,
+    PS_InRootName = path::iterator::_InRootName,
+    PS_InRootDir = path::iterator::_InRootDir,
+    PS_InFilenames = path::iterator::_InFilenames,
+    PS_InTrailingSep = path::iterator::_InTrailingSep,
+    PS_AtEnd = path::iterator::_AtEnd
   };
 
   const string_view_t Path;
@@ -1548,7 +1548,7 @@ path::iterator path::begin() const
     auto PP = PathParser::CreateBegin(__pn_);
     iterator it;
     it.__path_ptr_ = this;
-    it.__state_ = PP.State;
+    it.__state_ = static_cast<path::iterator::_ParserState>(PP.State);
     it.__entry_ = PP.RawEntry;
     it.__stashed_elem_.__assign_view(*PP);
     return it;
@@ -1557,16 +1557,15 @@ path::iterator path::begin() const
 path::iterator path::end() const
 {
     iterator it{};
-    it.__state_ = PathParser::PS_AtEnd;
+    it.__state_ = path::iterator::_AtEnd;
     it.__path_ptr_ = this;
     return it;
 }
 
 path::iterator& path::iterator::__increment() {
-  static_assert(__at_end == PathParser::PS_AtEnd, "");
   PathParser PP(__path_ptr_->native(), __entry_, __state_);
   ++PP;
-  __state_ = PP.State;
+  __state_ = static_cast<_ParserState>(PP.State);
   __entry_ = PP.RawEntry;
   __stashed_elem_.__assign_view(*PP);
   return *this;
@@ -1575,7 +1574,7 @@ path::iterator& path::iterator::__increm
 path::iterator& path::iterator::__decrement() {
   PathParser PP(__path_ptr_->native(), __entry_, __state_);
   --PP;
-  __state_ = PP.State;
+  __state_ = static_cast<_ParserState>(PP.State);
   __entry_ = PP.RawEntry;
   __stashed_elem_.__assign_view(*PP);
   return *this;




More information about the cfe-commits mailing list