From fe7f52d689825e01614faa8640d5af6687ed0b6e Mon Sep 17 00:00:00 2001 From: James Lee-Jones Date: Sat, 25 May 2024 22:52:01 +0100 Subject: [PATCH] Avoid mutating variables that require const initialization We cannot mutate variable declarations that require constant initialization as Dredd's mutations occur dynamically. Fixes: #235. --- src/libdredd/src/mutate_visitor.cc | 2 +- test/single_file/const_init.cc | 3 +++ test/single_file/const_init.cc.expected | 3 +++ test/single_file/const_init.cc.noopt.expected | 3 +++ 4 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 test/single_file/const_init.cc create mode 100644 test/single_file/const_init.cc.expected create mode 100644 test/single_file/const_init.cc.noopt.expected diff --git a/src/libdredd/src/mutate_visitor.cc b/src/libdredd/src/mutate_visitor.cc index 284ec16f..80ebdd89 100644 --- a/src/libdredd/src/mutate_visitor.cc +++ b/src/libdredd/src/mutate_visitor.cc @@ -118,7 +118,7 @@ bool MutateVisitor::TraverseDecl(clang::Decl* decl) { return true; } if (const auto* var_decl = llvm::dyn_cast(decl)) { - if (var_decl->isConstexpr()) { + if (var_decl->isConstexpr() || var_decl->hasConstantInitialization()) { // Because Dredd's mutations occur dynamically, they cannot be applied to // C++ constexprs, which require compile-time evaluation. return true; diff --git a/test/single_file/const_init.cc b/test/single_file/const_init.cc new file mode 100644 index 00000000..6934836b --- /dev/null +++ b/test/single_file/const_init.cc @@ -0,0 +1,3 @@ +int main() { + [[clang::require_constant_initialization]] static int x = 0; +} diff --git a/test/single_file/const_init.cc.expected b/test/single_file/const_init.cc.expected new file mode 100644 index 00000000..6934836b --- /dev/null +++ b/test/single_file/const_init.cc.expected @@ -0,0 +1,3 @@ +int main() { + [[clang::require_constant_initialization]] static int x = 0; +} diff --git a/test/single_file/const_init.cc.noopt.expected b/test/single_file/const_init.cc.noopt.expected new file mode 100644 index 00000000..6934836b --- /dev/null +++ b/test/single_file/const_init.cc.noopt.expected @@ -0,0 +1,3 @@ +int main() { + [[clang::require_constant_initialization]] static int x = 0; +}