Mechanism to check if a C++ member is private
You can use the AST output of the Clang compiler to validate that a certain member is private or public. For example, for the following code:
class test {
public:
int pub;
private:
int prv;
};
Running this command: clang -Xclang -ast-dump -fsyntax-only t.cpp
gives the AST dump:
TranslationUnitDecl 0x55f6f550e3f8 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x55f6f550e9b0 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'
| `-BuiltinType 0x55f6f550e690 '__int128'
|-TypedefDecl 0x55f6f550ea20 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'
| `-BuiltinType 0x55f6f550e6b0 'unsigned __int128'
|-TypedefDecl 0x55f6f550ed68 <<invalid sloc>> <invalid sloc> implicit __NSConstantString '__NSConstantString_tag'
| `-RecordType 0x55f6f550eb10 '__NSConstantString_tag'
| `-CXXRecord 0x55f6f550ea78 '__NSConstantString_tag'
|-TypedefDecl 0x55f6f550ee00 <<invalid sloc>> <invalid sloc> implicit __builtin_ms_va_list 'char *'
| `-PointerType 0x55f6f550edc0 'char *'
| `-BuiltinType 0x55f6f550e490 'char'
|-TypedefDecl 0x55f6f5545bf8 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list '__va_list_tag [1]'
| `-ConstantArrayType 0x55f6f5545ba0 '__va_list_tag [1]' 1
| `-RecordType 0x55f6f550eef0 '__va_list_tag'
| `-CXXRecord 0x55f6f550ee58 '__va_list_tag'
`-CXXRecordDecl 0x55f6f5545c50 <t.cpp:1:1, line:6:1> line:1:7 class test definition
|-DefinitionData pass_in_registers trivially_copyable trivial literal
| |-DefaultConstructor exists trivial needs_implicit
| |-CopyConstructor simple trivial has_const_param needs_implicit implicit_has_const_param
| |-MoveConstructor exists simple trivial needs_implicit
| |-CopyAssignment trivial has_const_param needs_implicit implicit_has_const_param
| |-MoveAssignment exists simple trivial needs_implicit
| `-Destructor simple irrelevant trivial needs_implicit
|-CXXRecordDecl 0x55f6f5545d78 <col:1, col:7> col:7 implicit class test
|-AccessSpecDecl 0x55f6f5545e10 <line:2:1, col:7> col:1 public
|-FieldDecl 0x55f6f5545e50 <line:3:5, col:9> col:9 pub 'int'
|-AccessSpecDecl 0x55f6f5545e98 <line:4:1, col:8> col:1 private
`-FieldDecl 0x55f6f5545ed8 <line:5:5, col:9> col:9 prv 'int'
which is quite simple to parse by a script. Or you can use the Clang AST library to create LibASTMatcher
to validate using data itself as described in the documentation.
If you want to assert that a type Bar
doesn't have a public member named foo
, you can write the following test:
template<typename T>
constexpr auto has_public_foo(T const &t) -> decltype(t.foo, true)
{
return true;
}
constexpr auto has_public_foo(...)
{
return false;
}
static_assert(not has_public_foo(Bar{}), "Public members are bad practice");
Here's a demo.
To append cigien's correct answer as I am testing a non-constant object, i.e., T
is altered during construction of the object, and thus will test at runtime rather than compile time. This just involves removing the const
keywords:
// Check that color is a public member
template<typename T>
auto has_public_color(T &t) -> decltype(t.color, true)
{
// Returns true if T has a public member named color
return true;
}
auto has_public_color(...)
{
return false;
}
Then I just insert into my unit testing framework (Boost unit testing) as such:
BOOST_AUTO_TEST_CASE(test_cell_no_public_color)
{
BOOST_TEST_MESSAGE("Testing that Cell has no public member of color");
// Check that Cell has no public color
BOOST_CHECK(not has_public_color(Cell{}));
}