0

I would like to use libtooling to test whether the defined by a CXXRecordDecl is copy constructable.

I have already tried :

  • hasCopyConstructorWithConstParam()
  • hasTrivialCopyConstructor() || hasNonTrivialCopyConstructor()

Unfortunately, both of those expressions return true if the class's copy constructor is implicitly deleted. This can occur if the class inherits from a non-copyable class or has a member variable that is non-copyable.

The logic to test whether a class is copy constructable is non-trivial and must be exist somewhere in clang. How can I test if a class is copy constructable with libtooling?

Michael Koval
  • 8,207
  • 5
  • 42
  • 53
  • You can retrieve the constructor with `CXXRecordDecl::ctor_begin` and check `CXXConstructorDecl::isDeleted()`... – Jarod42 Oct 23 '15 at 13:12
  • @Jarod42 Thanks! That worked - I'll accept this as an answer if you post it as one. – Michael Koval Oct 23 '15 at 19:25
  • Also, do you have any explanation of why `hasCopyConstructorWithConstParam` returns `true` even though the class **does not** have a copy constructor? – Michael Koval Oct 23 '15 at 19:26

1 Answers1

1

Turn comment into answer:

You can retrieve the constructor with CXXRecordDecl::ctor_begin

and check CXXConstructorDecl::isDeleted().

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Thanks! Are the default, copy, and move constructors *always* present in the `ctor_begin`/`ctor_end` range? What is the significance of the constructor being marked as (implicitly) deleted versus being entirely absent from the range? – Michael Koval Oct 24 '15 at 21:55
  • I tried this, but it did not work. For instance, in code such as `class A{ B b;}`, there will be no copy constructor at all. And you won't know if it's because the class cannot be copied, or because up to now, in the source code, nobody copies the class (is someone copies in instance of the class, then constructors will be generated). – Gathar Sep 25 '17 at 17:43