Perl: the Markov chain saw | |
PerlMonks |
Re^2: [OT] Abusing the C preprocessorby afoken (Chancellor) |
on Nov 11, 2017 at 20:15 UTC ( [id://1203194]=note: print w/replies, xml ) | Need Help?? |
I don't get it. To me, this looks backwards. I don't wan't to define USING_int. I want the precompiler to "know" if the configuration file contains one or more CFG_ADD(int, ...) or not. Building on that "knowledge", the precompiler should either emit the function definition for compare_int() or not. You could define the compare functions via inline versions. [...] There should be no warnings for unused inline functions. Nice idea. I suspected that CC would not accept inline functions for function pointer, but my Linux gcc 5.x does:
And so does the gcc 4.x for my target processor. COOL!
That function does not make sense in this form. Compare functions are expected to return int, and I guess that you wanted to call _inine_cmpfunc_int(a,b). So I assume you meant to wrote something like this:
This is great - except that now, the compiler may legally generate one compare function per NAME. Assuming a configuration file with 10 ints and 10 shorts, the compiler could emit 20 compare functions, 10 for int and 10 for short, and all would be in use. That's even worse than the current situation: Two compare functions for int and short, and eight unused functions for other types. I think gcc should be able to optimize and ignore the inline. But then, gcc should also be able to optimize the eight unused functions away. Does it optimize in that way? And will it still optimize that way when we update to a gcc version three major version numbers away from now? And here we are at a point that I did not mention. Having to answer "It depends" to a yes-or-no question is not acceptable in that project. The system will run in a highly regulated environment, and we need predictable behaviour. Regarding warnings: you can suppress unused warnings on individual basis with an __attribute__((__unused__)). Right. But the warning is not just cosmetic in this case. The compiler generates code that should not be generated. I could not care less if the target system was a PC from this century. But my target system is an embedded microcontroller with limited program memory. Running some primitive tests, it seems that gcc not only warns about unused functions, but actually removes them, even when optimizing is disabled (-O0). I think this is the way to go. I'll wrap __attribute__((__unused__)) in a macro named MAYBE_UNUSED (or similar), and at a lot of documentation explaining why and how this works together with gcc. The nice thing about this solution is that it is testable. We can simply look into the map file and search for the name of the functions that should not be there. THANKS! Alexander
-- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
In Section
Seekers of Perl Wisdom
|
|