[cfe-users] How to check whether a type is_copy_assignable?

Richard Smith via cfe-users cfe-users at lists.llvm.org
Wed Feb 19 14:49:28 PST 2020


On Thu, 6 Feb 2020 at 12:13, Weston Carvalho via cfe-users <
cfe-users at lists.llvm.org> wrote:

> HI,
>
> I'm trying to write a tool that uses the AST to look at all the class
> declarations in our codebase and get some metrics on our use of special
> member functions. (How many classes are copyable, how many have
> ctors without the corresponding assignment operator, etc.) I'm running into
> some problems with implicitly declared special members. For example:
>
> class UserDefCopy {
>  public:
>   UserDefCopy(const UserDefCopy&) {}
>   UserDefCopy& operator=(const UserDefCopy&) { return *this; }
> };
> class ContainsUserDefCopy {
>  private:
>   UserDefCopy x_;
> };
>
> UserDefCopy has a CXXConstructorDecl for its copy constructor and a
> CXXMethodDecl for the copy-assign operator, so I can use the methods on the
> decls to get the info I need (isDeleted, isExplicitlyDefaulted, getAccess,
> etc.) However, ContainsUserDefCopy's copy-assign operator is implicit, so
> there's no decl for it. Since there's no decl, I can't differentiate
> between a class with an implicitly defaulted copy-assign and one with an
> implicitly deleted copy-assign. ContainsUserDefCopy does have a
> declaration for its copy constructor, but AFAICT from looking at
> clang::Sema::AddImplicitlyDeclaredMembersToClass
> <https://clang.llvm.org/doxygen/classclang_1_1Sema.html#a2ddba48d645ed86f2f4d11b6e7a9010a>,
> that's because special members with the needs_overload_resolution tag are
> eagerly generated while others are deferred. I don't understand why the
> ctor has the tag while the assign operator doesn't, though.
>
> The questions I have are:
>
>    1. What makes a method needs_overload_resolution? The docs in
>    CXXRecordDecl just say that it "[determines] whether we need to
>    eagerly declare a defaulted [member] for this class."
>
> Once the class is completely defined, the definition data accessors on
CXXRecordDecl (eg, hasTrivialCopyConstructor) are required to return
accurate information about the class. The "needs overload resolution" flag
is set by CXXRecordDecl if it couldn't work those properties out by itself
and needs Sema to help. For example, if it's not trivially obvious what
constructor would be used to copy a subobject of a class (see
CXXRecordDecl::hasSimpleCopyConstructor), CXXRecordDecl sets the "need
overload resolution" flag so that Sema can properly work out whether that
class's copy constructor is trivial.

This flag is just an implementation detail, and doesn't have any
interesting meaning outside of the implementation of CXXRecordDecl and Sema.

>
>    1. Is there a different way I should query the AST so that I see any
>    decls that were deferred during the Sema step? Or some other way to get
>    this information? Trying to use an implicitly deleted copy assignment
>    operator is a compiler error, so that information is obviously available
>    *somewhere*.
>
> It depends on what question you want the answer to. I'd guess (based on
the subject of your email) that you want the same answer that
is_copy_assignable would give, which means you want to know whether a
certain assignment expression would be valid (and note that the assignment
expression might be implemented by something other than a copy-assignment
operator in general, C++ being what it is). Probably the easiest way to
query that would be to call Sema::BuildTypeTrait and call
TypeTraitExpr::getValue() on the result.

You should be aware that making this query may have side-effects (in
particular, it may trigger template instantiations and result in errors
outside of the immediate context). But that's fundamental to the question
you're asking, I'm afraid.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20200219/88e4148e/attachment.html>


More information about the cfe-users mailing list