[LLVMbugs] [Bug 20105] New: Feature request: Spot failures to comply with regular container usable type

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Mon Jun 23 04:21:21 PDT 2014


http://llvm.org/bugs/show_bug.cgi?id=20105

            Bug ID: 20105
           Summary: Feature request: Spot failures to comply with regular
                    container usable type
           Product: clang
           Version: unspecified
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Static Analyzer
          Assignee: kremenek at apple.com
          Reporter: s_llvm at nedprod.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

Eric Niebler's C++ Now presentation suggested the following "regular" minimum
definitions for types used in STL containers. I would like if the clang static
analyser could spot use of a type in a STL container, and if so then the static
checks below ought to be applied to remind the programmer when they forgot
something like putting noexcept on the move constructors or forgot to declare a
std::hash overload (some method of marking a type as non-hashable would be
great, I am always forgetting the std::hash overload).

For C++03 Eric suggested this:

class Regular {
  Regular();
  Regular(Regular const &);
  ~Regular(); // throw()
  Regular & operator=(Regular const &);
  friend bool operator==(Regular const &, Regular const &);
  friend bool operator!=(Regular const &, Regular const &);
  friend bool operator<(Regular const &, Regular const &);
  friend void swap(Regular &, Regular &);
};

For C++11 Eric suggested this:

class Regular {
  Regular();
  Regular(Regular const &);
  // OR
  Regular(Regular &&) noexcept;
  ~Regular(); // Personally I think this needs noexcept
  Regular & operator=(Regular const &);
  // OR
  Regular & operator=(Regular &&) noexcept;
  friend bool operator==(Regular const &, Regular const &);
  friend bool operator!=(Regular const &, Regular const &);
  friend bool operator<(Regular const &, Regular const &);
};
namespace std {
  template<> struct hash<Regular>;
}

Eric also suggested the following compile-time check which might be useful for
implementing this feature request:

template<typename T> struct is_regular : std::integral_constant<bool,
  std::is_default_constructible<T>::value &&
  std::is_copy_constructible<T>::value &&
  std::is_move_constructible<T>::value &&
  std::is_copy_assignable<T>::value &&
  std::is_move_assignable<T>::value>
{};

My only issue with this check is that it doesn't test for noexcept-ness nor
that the comparison operators have been defined.

I appreciate that some may feel this is a problem better handled with Concepts
- equally, those are somewhat far away. Even better if there were an attribute
or markup with which one could mark a type as being "regular" when you were
writing it, this would let the analyser not have to watch STL container type
usage.

Niall

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20140623/b4538201/attachment.html>


More information about the llvm-bugs mailing list