Relatedly, with all the clang-based tooling like clang-tidy or clang-format, it's kind of disappointing that there isn't a tool to expand arbitrary macros in a source. That would have been extremely useful when I was recently refactoring some old C code. I ended up writing a small script that can expand one macro at a time, but I wish I hadn't had to. I'm sure that script doesn't work in the general case.
https://github.com/goldsborough/clang-expand (not perfect because of name conflicts that may occur, but usable) Can be used in atom: https://atom.io/packages/atom-clang-expand
Auditable Macros in C Code
11–13 of 13 posts
Re: Auditable Macros in C Code
#12Earlier quoted context omitted.
If I have a file like this: #include #include #include "mydefs-control.h" #include "mydefs-value.h" int main(void) { assert(1 == 1); printf("%d\n", MYDEF_VALUE_MACRO); MYDEF_MACRO_WITH_CONTROL_FLOW(); } My goal is to produce output which doesn't include thousands of lines from stdio.h and assert.h, doesn't expand assert() or MYDEF_VALUE_MACRO, and only expands MYDEF_MACRO_WITH_CONTROL_FLOW: int main(void) { assert(1…
Which just raises the question for me, why have the macros at all? I did C++ development for years (on low level and performance sensitive applications) but 95% of the macros I saw were there to reduce lines of code or "eliminate boilerplate." Almost all of those could be eliminated by refactoring within C++, without resorting to the preprocessor, but developers love using macros...
Re: Auditable Macros in C Code
#130. Anything involving file and line number.
1. Generating a string version of something alongside the value of something, without repeating myself.
2. Creating printing macros to keep me from typing out the entire asinine syntax for something like “std::cerr << foo << bar << baz << std::endl” so I only need PRINT(foo << bar << baz). Substituting code fragments is dead simple with a macro and absurdly complex or impossible with other C++ mechanisms. Ironically I’m only doing it because of the poor design of the entire “iostream” stack.