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

Richard Smith via cfe-users cfe-users at lists.llvm.org
Thu Feb 20 18:09:01 PST 2020


On Thu, 20 Feb 2020 at 09:33, Weston Carvalho <westoncarvalho at google.com>
wrote:

> Richard,
>
> Thanks for your response!
>
> I'm currently getting all the CXXRecordDecls using the ASTMatchFinfer.
> AFAICT, I can't access Sema since the MatchFinder is an ASTConsumer instead
> of a SemaConsumer. I guess I can make my own MatchFinderWithSema; do you
> know of a better way?
>

I don't know if there's any prebuilt way to do this.


> Also, is_copy_assignable is only one of the pieces of info I'm looking
> for. The full list if what I'm trying to get for each special member is:
>
>    1. Does it exist?
>    2. Is it trivial?
>    3. Is it implicitly defined?
>    4. If it's not implicitly defined, is it user defined or is it
>    explicitly defaulted?
>    5. What's the Access Specifier?
>    6. If it's a copy ctor or assign, is its argument const T& or T&?
>    7. If it's an assignment operation, does it return T&?
>
> I think I can get all of these by constructing type traits or expressions
> with Sema, at least modulo the weird stuff you mentioned. (It would be nice
> if I could detect/exclude those cases, but I don't think it's essential.)
>

Do you actually want information about special members, or do you want to
know what (eg) a construction or assignment expression would do? These are
different questions in C++, because overload resolution might pick
something other than a special member function in order to answer them.
is_copy_assignable answers the question of whether an expression of the
form "a = b;" would be valid, which might or might not involve special
member functions.

If you actually want information about special members, then the various
methods on CXXRecordDecl may be the best way to go. Note that some of the
questions above are then trivial, and don't have dedicated accessor
methods. For example, the answer for "Does it exist?" for a copy assignment
operator is simply "yes", for every class. (If you don't declare one, one
is always declared for you.) Alternatively, you can call
Sema::ForceDeclarationOfImplicitMembers and then look for the methods you
care about.

I don't understand how this could trigger template instantiations or
> errors; could you elaborate? Would that only be possible if the class I'm
> checking is an instantiation of a template class itself or if its copy
> constructor was templated?
>

It depends. If what you want is to ask questions about the special member
functions, then most of those can be answered without triggering
instantiations (or at least, we will have already triggered those
instantiations when the answers weren't easy). If you instead want to know
"would `a = b;` invoke a trivial assignment operator?" then determining
that can involve evaluating arbitrary metaprogramming constructs, which can
result in errors.

Thanks again for your help,
> Weston
>
> On Wed, Feb 19, 2020 at 2:49 PM Richard Smith <richard at metafoo.co.uk>
> wrote:
>
>> 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/20200220/c34711c6/attachment-0001.html>


More information about the cfe-users mailing list