How can I ensure whether some specific code is not emitted?
Say, I tried to write something like a static switch (or static if) and I wish to detect whether second lambda operator ()'s body is emitted:
#include <type_traits>
namespace details
{
template< typename visitor, typename ...visitors >
struct composite_visitor
: std::decay_t< visitor >
, composite_visitor< visitors... >
{
using std::decay_t< visitor >::operator ();
using composite_visitor< visitors... >::operator ();
composite_visitor(visitor & _visitor, visitors &... _visitors)
: std::decay_t< visitor >(std::forward< visitor >(_visitor))
, composite_visitor< visitors... >{_visitors...}
{ ; }
};
template< typename visitor >
struct composite_visitor< visitor >
: std::decay_t< visitor >
{
using std::decay_t< visitor >::operator ();
composite_visitor(visitor & _visitor)
: std::decay_t< visitor >(std::forward< visitor >(_visitor))
{ ; }
};
}
template< typename visitor, typename ...visitors >
details::composite_visitor< visitor, visitors... >
compose_visitors(visitor && _visitor, visitors &&... _visitors)
{
return {_visitor, _visitors...};
}
#include <iostream>
#include <cstdlib>
template< int condition >
void
test()
{
compose_visitors(
[] (std::integral_constant< int, 0 >) { std::cout << 0 << std::endl; },
[&] (std::integral_constant< int, 1 >) { std::cout << 1 << std::endl; },
[] (std::integral_constant< int, 2 >) { std::cout << 2 << std::endl; }
)(std::integral_constant< int, condition >{});
}
int
main()
{
test< 0 >();
//test< 1 >();
test< 2 >();
return EXIT_SUCCESS;
}
What is the tricks that allows me to detect a presence of specific code block into the resulting binary/.o/.a?
Previously one could do the following (for GCC):
- add
-Wa,-adhlns="$@.lst"toCXXFLAGS; - add
asm volatile ("nop");statement to code block you want to test; - build, then run
awk "/\#APP/, /\#NO_APP/" build/*.lst(instead of hash symbol there may be a slash) command to see inline assembly into*.lstfiles inbuilddirectory; - if
awkprints something, then particular code block is emitted.
But I use clang and therefore can't use -Wa compiler parameter. What is a way for clang? I know I can emit assembly files (.S) using -S compiler option, but in big project it is hard to do in such a way if there is a dependency chain during building. How to emit assembly in addition to .o files?