Visiting implicit copy assignment

Hi all,

I'm experimenting with a Clang tool to generate export tables from
source code (MSVC's .DEF files)

I've noticed that MSVC always exports the copy assignment operator if
the class is marked __declspec(dllexport), so I'm trying to mimic that
behavior.

So, given:

  class Implicits { };

Can I somehow visit the implicit methods (default ctor, copy ctor,
copy assignment operator, dtor) in a Clang tool?

I've smuggled Sema into the tool and tried to use the DefineImplicit
methods to define them before visitation, but haven't had much luck.

Is there a documented way to handle this? It seems Clang only defines
these methods if they are used, but for tooling it would be useful to
be able to work with them whether they're actually defined or not.

Thanks,
- Kim

This is something that would also be useful for my clang tool (and I was planning to send an email to the list on it next week!).

Something like std::vector<Decl*> getImplicitDecls() would be very handy, without polluting the AST in a way that would confuse things that only care about what was in the source?

Hi all,

I'm experimenting with a Clang tool to generate export tables from
source code (MSVC's .DEF files)

I've noticed that MSVC always exports the copy assignment operator if
the class is marked __declspec(dllexport), so I'm trying to mimic that
behavior.

Clang already implements that behavior if you use __declspec(dllexport).
:slight_smile: But I assume you have your reasons for wanting to use .def files.

So, given:

  class Implicits { };

Can I somehow visit the implicit methods (default ctor, copy ctor,
copy assignment operator, dtor) in a Clang tool?

I've smuggled Sema into the tool and tried to use the DefineImplicit
methods to define them before visitation, but haven't had much luck.

Is there a documented way to handle this? It seems Clang only defines
these methods if they are used, but for tooling it would be useful to
be able to work with them whether they're actually defined or not.

See where Clang implements this in SemaDeclCXX.cpp. It calls
Sema::ForceDeclarationOfImplicitMembers(), and then iterates over them and
marks them referenced to ensure they are emitted.

Hi Reid,

I've noticed that MSVC always exports the copy assignment operator if
the class is marked __declspec(dllexport), so I'm trying to mimic that
behavior.

Clang already implements that behavior if you use __declspec(dllexport). :slight_smile:
But I assume you have your reasons for wanting to use .def files.

Right, yes. The FOO_EXPORT macros have always bugged me, so I wanted
to see if there was a way to generate the export tables out-of-line
for portable code bases.

Is there a documented way to handle this? It seems Clang only defines
these methods if they are used, but for tooling it would be useful to
be able to work with them whether they're actually defined or not.

See where Clang implements this in SemaDeclCXX.cpp. It calls
Sema::ForceDeclarationOfImplicitMembers(), and then iterates over them and
marks them referenced to ensure they are emitted.

Thanks, that looks straightforward enough!

I guess my next question is tooling-related -- how do I best get a
hold of Sema and call
ForceDeclarationOfImplicitMembers/MarkFunctionReferenced before
traversing the AST (either with a visitor or preferably AST matchers)?
It seems dangerous to modify the AST (which I assume this does) as
part of traversal, and I haven't found a way to run matchers on a
Decl.

I managed to smuggle Sema into a tool by passing a SourceFileCallbacks
implementation to stash the CompilerInstance into newFrontEndFactory,
but it felt decidedly dirty :slight_smile:

Thanks,
- Kim