[cfe-dev] Detecting an out-of-class defaulted method

David Rector via cfe-dev cfe-dev at lists.llvm.org
Wed Sep 1 05:08:44 PDT 2021



> On Aug 31, 2021, at 6:16 PM, via cfe-dev <cfe-dev at lists.llvm.org> wrote:
> 
> Clang AST novice here...
> 
> I have a CXXMethodDecl*, and I'm trying to determine whether it is
> (a) marked `= default` in the class declaration,
> (b) not `= default` in the class declaration, but `= default` in
>    the outside-the-class definition, or
> (c) neither.
> 
> Currently I'm doing this:
> 
>  if (Method->isExplicitlyDefaulted()) {
>    if (Method->isUserProvided())
>      defaulted_out_of_class;
>    else
>      defaulted_in_class;
>  } else
>    not_defaulted;
> 
> and I have a test case like
> 
> class foo {
>  foo() = default;
>  ~foo();
> };
> foo::~foo() = default;
> 
> The ctor is going through the defaulted_in_class path, but
> the dtor is going through the not_defaulted path.
> 
> I've tried `Method->getCanonicalDecl()` instead of just `Method`
> but the results are the same.

The canonical decl has no meaning other than its uniqueness over its redecls; I believe it is always just the first declaration (even when that is, say, a friend declaration).

I would manually look through each redeclaration of `Method`, e.g.

```
for (auto Redecl : Method->redecls()) {
  if (Redecl->isExplicitlyDefaulted()) {
    if (Redecl->isOutOfLine())
      return defaulted_out_of_class;
    else
      return defaulted_in_class;
  }
}
return not_defaulted;
```



More information about the cfe-dev mailing list