Skip to content

Commit

Permalink
Avoid mutating variables that require const initialization
Browse files Browse the repository at this point in the history
We cannot mutate variable declarations that require constant
initialization as Dredd's mutations occur dynamically.

Fixes: #235.
  • Loading branch information
JamesLee-Jones committed May 25, 2024
1 parent 5f421ec commit fe7f52d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/libdredd/src/mutate_visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ bool MutateVisitor::TraverseDecl(clang::Decl* decl) {
return true;
}
if (const auto* var_decl = llvm::dyn_cast<clang::VarDecl>(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;
Expand Down
3 changes: 3 additions & 0 deletions test/single_file/const_init.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int main() {
[[clang::require_constant_initialization]] static int x = 0;
}
3 changes: 3 additions & 0 deletions test/single_file/const_init.cc.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int main() {
[[clang::require_constant_initialization]] static int x = 0;
}
3 changes: 3 additions & 0 deletions test/single_file/const_init.cc.noopt.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int main() {
[[clang::require_constant_initialization]] static int x = 0;
}

0 comments on commit fe7f52d

Please sign in to comment.