ta0kira / zeolite

Zeolite is a statically-typed, general-purpose programming language.
Apache License 2.0
18 stars 0 forks source link

Add visibility protection for `$TestsOnly$` in C++ extensions. #90

Closed ta0kira closed 4 years ago

ta0kira commented 4 years ago

For example, guard the headers with a macro (e.g., ZEOLITE_TESTS_ONLY) so that the category is only available if that macro is defined.

It would also be helpful if there was a consequence of defining that macro so that it can't just be used arbitrarily.

ta0kira commented 4 years ago

In order to prevent a non-$TestsOnly$ category from being defined in a $TestsOnly$ C++ extension, we can probably use some macro trickery:

// In non-$TestsOnly$ Streamlined_Foo.hpp.

#ifdef ZEOLITE_TESTS_ONLY
#error Category Foo cannot be defined as $TestsOnly$
#endif

#ifndef ZEOLITE_NO_TESTS_ONLY
#define ZEOLITE_NO_TESTS_ONLY
#endif
// In $TestsOnly$ Category_Bar.hpp.

#if !defined(ZEOLITE_TESTS_ONLY) || defined(ZEOLITE_NO_TESTS_ONLY)
#error Category Bar can only be used in $TestsOnly$ sources
#endif 

The key here is that both the streamlined header (#included to define the category) and category header (#included to use the category) are generated.

These two macros should be avoidable in all hand-written code (once streamlined extensions are implemented); therefore, they could be given ugly names that would look bad in hand-written code, e.g., ZEOLITE_TESTS_ONLY_GUARD_FOR_USE_IN_CODEGEN. Or, the macro names could include the compiler hash at the end of the name, e.g., ZEOLITE_TESTS_ONLY_7bc4535736d2796f.

ta0kira commented 4 years ago

A more concise plan:

The key points are that: