From 213dba6f6d845393d657f7c472ef4375e2ea311f Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Tue, 24 Jun 2025 13:23:43 -0500 Subject: [PATCH] scripts: test.py/bench.py: Added ifndef attribute for tests/benches As you might expect, this is the inverse of ifdef, and is useful for supporting opt-out flags. I don't think ifdef + ifndef is powerful enough to handle _all_ compile-time corner cases, but they at least provide convenient handling for the most common flags. Worst case, tests/benches can always include explicit #if/#ifdef/#ifndef statements in the code itself. --- scripts/bench.py | 38 ++++++++++++++++++++++++++++++++------ scripts/test.py | 38 ++++++++++++++++++++++++++++++++------ 2 files changed, 64 insertions(+), 12 deletions(-) diff --git a/scripts/bench.py b/scripts/bench.py index 8c5fd356..99bf2376 100755 --- a/scripts/bench.py +++ b/scripts/bench.py @@ -87,6 +87,9 @@ class BenchCase: self.ifdef = config.pop('ifdef', []) if not isinstance(self.ifdef, list): self.ifdef = [self.ifdef] + self.ifndef = config.pop('ifndef', []) + if not isinstance(self.ifndef, list): + self.ifndef = [self.ifndef] self.code = config.pop('code') self.code_lineno = config.pop('code_lineno', None) self.in_ = config.pop('in', @@ -224,6 +227,9 @@ class BenchSuite: self.ifdef = config.pop('ifdef', []) if not isinstance(self.ifdef, list): self.ifdef = [self.ifdef] + self.ifndef = config.pop('ifndef', []) + if not isinstance(self.ifndef, list): + self.ifndef = [self.ifndef] self.code = config.pop('code', None) self.code_lineno = min( @@ -385,9 +391,11 @@ def compile(bench_paths, **args): # the bench defines def write_case_functions(f, suite, case): # write any ifdef prologues - if case.ifdef: + if case.ifdef or case.ifndef: for ifdef in case.ifdef: f.writeln('#ifdef %s' % ifdef) + for ifndef in case.ifndef: + f.writeln('#ifndef %s' % ifndef) f.writeln() # create case define functions @@ -446,16 +454,20 @@ def compile(bench_paths, **args): f.writeln() # write any ifdef epilogues - if case.ifdef: + if case.ifdef or case.ifndef: for ifdef in case.ifdef: f.writeln('#endif') + for ifndef in case.ifndef: + f.writeln('#endif') f.writeln() if not args.get('source'): # write any ifdef prologues - if suite.ifdef: + if suite.ifdef or suite.ifndef: for ifdef in suite.ifdef: f.writeln('#ifdef %s' % ifdef) + for ifndef in suite.ifndef: + f.writeln('#ifndef %s' % ifndef) f.writeln() # write any suite defines @@ -496,9 +508,11 @@ def compile(bench_paths, **args): f.writeln() # write any ifdef epilogues - if suite.ifdef: + if suite.ifdef or suite.ifndef: for ifdef in suite.ifdef: f.writeln('#endif') + for ifndef in suite.ifndef: + f.writeln('#endif') f.writeln() # create suite struct @@ -512,6 +526,8 @@ def compile(bench_paths, **args): or 0)) for ifdef in suite.ifdef: f.writeln(4*' '+'#ifdef %s' % ifdef) + for ifndef in suite.ifndef: + f.writeln(4*' '+'#ifndef %s' % ifndef) # create suite defines if suite.defines: f.writeln(4*' '+'.defines = (const bench_define_t[]){') @@ -522,6 +538,8 @@ def compile(bench_paths, **args): f.writeln(4*' '+'.define_count = %d,' % len(suite.defines)) for ifdef in suite.ifdef: f.writeln(4*' '+'#endif') + for ifndef in suite.ifndef: + f.writeln(4*' '+'#endif') if suite.cases: f.writeln(4*' '+'.cases = (const struct bench_case[]){') for case in suite.cases: @@ -536,6 +554,8 @@ def compile(bench_paths, **args): or 0)) for ifdef in it.chain(suite.ifdef, case.ifdef): f.writeln(12*' '+'#ifdef %s' % ifdef) + for ifndef in it.chain(suite.ifndef, case.ifndef): + f.writeln(12*' '+'#ifndef %s' % ifndef) # create case defines if case.defines: f.writeln(12*' '+'.defines' @@ -567,6 +587,8 @@ def compile(bench_paths, **args): case.name)) for ifdef in it.chain(suite.ifdef, case.ifdef): f.writeln(12*' '+'#endif') + for ifndef in it.chain(suite.ifndef, case.ifndef): + f.writeln(12*' '+'#endif') f.writeln(8*' '+'},') f.writeln(4*' '+'},') f.writeln(4*' '+'.case_count = %d,' % len(suite.cases)) @@ -600,9 +622,11 @@ def compile(bench_paths, **args): # write any internal benches for suite in suites: # any ifdef prologues - if suite.ifdef: + if suite.ifdef or suite.ifndef: for ifdef in suite.ifdef: f.writeln('#ifdef %s' % ifdef) + for ifndef in suite.ifndef: + f.writeln('#ifndef %s' % ifndef) f.writeln() # any suite code @@ -622,9 +646,11 @@ def compile(bench_paths, **args): write_case_functions(f, suite, case) # any ifdef epilogues - if suite.ifdef: + if suite.ifdef or suite.ifndef: for ifdef in suite.ifdef: f.writeln('#endif') + for ifndef in suite.ifndef: + f.writeln('#endif') f.writeln() # declare our bench suites diff --git a/scripts/test.py b/scripts/test.py index 6238ddfd..948401ac 100755 --- a/scripts/test.py +++ b/scripts/test.py @@ -88,6 +88,9 @@ class TestCase: self.ifdef = config.pop('ifdef', []) if not isinstance(self.ifdef, list): self.ifdef = [self.ifdef] + self.ifndef = config.pop('ifndef', []) + if not isinstance(self.ifndef, list): + self.ifndef = [self.ifndef] self.code = config.pop('code') self.code_lineno = config.pop('code_lineno', None) self.in_ = config.pop('in', @@ -230,6 +233,9 @@ class TestSuite: self.ifdef = config.pop('ifdef', []) if not isinstance(self.ifdef, list): self.ifdef = [self.ifdef] + self.ifndef = config.pop('ifndef', []) + if not isinstance(self.ifndef, list): + self.ifndef = [self.ifndef] self.code = config.pop('code', None) self.code_lineno = min( @@ -397,9 +403,11 @@ def compile(test_paths, **args): # the test defines def write_case_functions(f, suite, case): # write any ifdef prologues - if case.ifdef: + if case.ifdef or case.ifndef: for ifdef in case.ifdef: f.writeln('#ifdef %s' % ifdef) + for ifndef in case.ifndef: + f.writeln('#ifndef %s' % ifndef) f.writeln() # create case define functions @@ -458,16 +466,20 @@ def compile(test_paths, **args): f.writeln() # write any ifdef epilogues - if case.ifdef: + if case.ifdef or case.ifndef: for ifdef in case.ifdef: f.writeln('#endif') + for ifndef in case.ifndef: + f.writeln('#endif') f.writeln() if not args.get('source'): # write any ifdef prologues - if suite.ifdef: + if suite.ifdef or suite.ifndef: for ifdef in suite.ifdef: f.writeln('#ifdef %s' % ifdef) + for ifndef in suite.ifndef: + f.writeln('#ifndef %s' % ifndef) f.writeln() # write any suite defines @@ -508,9 +520,11 @@ def compile(test_paths, **args): f.writeln() # write any ifdef epilogues - if suite.ifdef: + if suite.ifdef or suite.ifndef: for ifdef in suite.ifdef: f.writeln('#endif') + for ifndef in suite.ifndef: + f.writeln('#endif') f.writeln() # create suite struct @@ -526,6 +540,8 @@ def compile(test_paths, **args): or 0)) for ifdef in suite.ifdef: f.writeln(4*' '+'#ifdef %s' % ifdef) + for ifndef in suite.ifndef: + f.writeln(4*' '+'#ifndef %s' % ifndef) # create suite defines if suite.defines: f.writeln(4*' '+'.defines = (const test_define_t[]){') @@ -536,6 +552,8 @@ def compile(test_paths, **args): f.writeln(4*' '+'.define_count = %d,' % len(suite.defines)) for ifdef in suite.ifdef: f.writeln(4*' '+'#endif') + for ifndef in suite.ifndef: + f.writeln(4*' '+'#endif') if suite.cases: f.writeln(4*' '+'.cases = (const struct test_case[]){') for case in suite.cases: @@ -554,6 +572,8 @@ def compile(test_paths, **args): or 0)) for ifdef in it.chain(suite.ifdef, case.ifdef): f.writeln(12*' '+'#ifdef %s' % ifdef) + for ifndef in it.chain(suite.ifndef, case.ifndef): + f.writeln(12*' '+'#ifndef %s' % ifndef) # create case defines if case.defines: f.writeln(12*' '+'.defines' @@ -585,6 +605,8 @@ def compile(test_paths, **args): case.name)) for ifdef in it.chain(suite.ifdef, case.ifdef): f.writeln(12*' '+'#endif') + for ifndef in it.chain(suite.ifndef, case.ifndef): + f.writeln(12*' '+'#endif') f.writeln(8*' '+'},') f.writeln(4*' '+'},') f.writeln(4*' '+'.case_count = %d,' % len(suite.cases)) @@ -618,9 +640,11 @@ def compile(test_paths, **args): # write any internal tests for suite in suites: # any ifdef prologues - if suite.ifdef: + if suite.ifdef or suite.ifndef: for ifdef in suite.ifdef: f.writeln('#ifdef %s' % ifdef) + for ifndef in suite.ifndef: + f.writeln('#ifndef %s' % ifndef) f.writeln() # any suite code @@ -640,9 +664,11 @@ def compile(test_paths, **args): write_case_functions(f, suite, case) # any ifdef epilogues - if suite.ifdef: + if suite.ifdef or suite.ifndef: for ifdef in suite.ifdef: f.writeln('#endif') + for ifndef in suite.ifndef: + f.writeln('#endif') f.writeln() # declare our test suites