mc-imperial / dredd

Framework for evaluating C/C++ compiler testing tools
Apache License 2.0
11 stars 3 forks source link

Dredd mutates `const` variable used in `static_assert` #286

Closed JonathanFoo0523 closed 1 month ago

JonathanFoo0523 commented 1 month ago

Dredd mutate the C++ program

int main() {
  const int a = 1;
  static_assert(a);
}

to

int main() {
  const int a = __dredd_replace_expr_int(1, 0);
  static_assert(a);
}

which lead to compilation error:

error: static assertion expression is not an integral constant expression
afd commented 1 month ago

The obvious thing to do here would be to remove static assertions, by replacing them with nothing, or by replacing their expressions with 1. There is no point rewriting them (like for constant-sized arrays), because if their expressions evaluated to 0 then the front-end used by Dredd would have already failed.

A concern I have is that static assertions are rather likely to appear in header files or macros (when used in header-file libraries). Because Dredd deliberately does not mutate header files, this could be a problem if constants used in such assertions have their initial values mutated.

afd commented 1 month ago

On balance I think it may be cleanest to rewrite static asserts, since this will be being done for __builtin_frame_address in #289. @JonathanFoo0523 are you happy to take care of this along with #289?

JonathanFoo0523 commented 1 month ago

Seems like it is not possible to rewrite expression under static_assert to the actual expression anyway. For example,

int main() {
  const int a = 42;
  static_assert(a);
}

The a always evaluate to 1 instead of 42.

I will just rewrite to 1 without actually evaluating, as mentioned in above comment.