Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Token pasting as a vector for variation #44

Open
eternaleye opened this issue Aug 21, 2020 · 1 comment
Open

Token pasting as a vector for variation #44

eternaleye opened this issue Aug 21, 2020 · 1 comment

Comments

@eternaleye
Copy link

Token pasting in the preprocessor is sufficient to introduce a switch/case-like construct; this can act as a vector for variation to propagate through the codebase. This example switches on whether LONG_MAX is 2^31-1, 2^63-1, or undefined:

#include <assert.h>
#include <limits.h>
#include <stdio.h>

#define SELECT(cases, selector) _SELECT(cases, selector)
#define _SELECT(cases, selector) cases ## _ ## selector

// switch(val)
//     case 0x7fffffffL: return 4;
#define EVIL_0x7fffffffL 4
//     case 0x7fffffffffffffffL: return 8;
#define EVIL_0x7fffffffffffffffL 8
//     default: return 0;
#define EVIL_LONG_MAX 0
// }

#define LONG_BYTES SELECT(EVIL, LONG_MAX)

int main() {
    assert(LONG_BYTES > 0);
    printf("A long is %d bytes\n", LONG_BYTES);
}

I originally came up with it as an example case of how the preprocessor is hostile to "partial evaluation", as https://github.com/tendra/tendra attempts to perform, as in their approach LONG_MAX would act as undefined in the initial platform-agnostic preprocessing, resulting in an unconditional assert.

@ckaestne
Copy link
Owner

Conceptually this should be handled in the SuperC lexer that we are using by default (I know that the original TypeChef lexer doesn't handle this correctly).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants