Live data from Hacker News

Auditable Macros in C Code

cossacklabs.com

1–10 of 13 posts

Re: Auditable Macros in C Code

#2
TL;DR

Let the compiler preprocess a source file by first removing #include lines which contain definitions of macros we don't want to see expanded (expanding only the definitions from the local file or from chosen headers, achieving less cluttered and more readable output). Say we have a file like:

    #include "foo_defs.h"
    #define BAR 42
    FOO;
    BAR;
We filter out the include line and then preprocess the file, getting the following output:

    FOO;
    42;

Re: Auditable Macros in C Code

#3

TL;DR Let the compiler preprocess a source file by first removing #include lines which contain definitions of macros we don't want to see expanded (expanding only the definitions from the local file or from chosen headers, achieving less cluttered and more readable output). Say we have a file like: #include "foo_defs.h" #define BAR 42 FOO; BAR; We filter out the include line and then preprocess the file, getting the…

TLDR of why less cluttered preprocessor output matters? I’d say cpp output is usually not that interesting on a daily basis.

Re: Auditable Macros in C Code

#4
post #3

TL;DR Let the compiler preprocess a source file by first removing #include lines which contain definitions of macros we don't want to see expanded (expanding only the definitions from the local file or from chosen headers, achieving less cluttered and more readable output). Say we have a file like: #include "foo_defs.h" #define BAR 42 FOO; BAR; We filter out the include line and then preprocess the file, getting the…

TLDR of why less cluttered preprocessor output matters? I’d say cpp output is usually not that interesting on a daily basis.

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 == 1);
        printf("%d\n", MYDEF_VALUE_MACRO);

        if (1) {
            return 0;
        } else {
            return 1;
        };
    }
According to the author, having only the "interesting" stuff expanded makes it easier to reason about the control flow of the code.

Re: Auditable Macros in C Code

#5
I try to use ObjC PPC macros for things that are constant for the run of the program, and can be reasoned about globally.

For example, some good ones I use are isLandscape or isIPad, which are both variable depending on the user, but constant for the run of the app. Perfect for a PCH file I think.

Re: Auditable Macros in C Code

#6
post #3

Earlier quoted context omitted.

TLDR of why less cluttered preprocessor output matters? I’d say cpp output is usually not that interesting on a daily basis.

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

#7

TL;DR Let the compiler preprocess a source file by first removing #include lines which contain definitions of macros we don't want to see expanded (expanding only the definitions from the local file or from chosen headers, achieving less cluttered and more readable output). Say we have a file like: #include "foo_defs.h" #define BAR 42 FOO; BAR; We filter out the include line and then preprocess the file, getting the…

Their technique is specific for their set of includes. It would be great if there was a general way to do the macro expansion in a need-to-know eay that could then be a basis for interop tools such as SWIG or python cffi

Re: Auditable Macros in C Code

#8
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.

Re: Auditable Macros in C Code

#9

Earlier 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...

I guess it standardizes your boilerplate at write-time even if you have to review it expanded.

Re: Auditable Macros in C Code

#10
post #8

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

Post reply on HN