Skip to content

Commit

Permalink
[coverage] fix crash in code coverage and if constexpr with `ExprWi…
Browse files Browse the repository at this point in the history
…thCleanups` (llvm#80292)

Fixes llvm#80285

(cherry picked from commit bfc6eaa)
  • Loading branch information
hanickadot authored and tstellar committed Feb 14, 2024
1 parent 9153cbb commit 616b9e1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
6 changes: 4 additions & 2 deletions clang/lib/CodeGen/CoverageMappingGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1812,8 +1812,10 @@ struct CounterCoverageMappingBuilder
assert(S->isConstexpr());

// evaluate constant condition...
const auto *E = cast<ConstantExpr>(S->getCond());
const bool isTrue = E->getResultAsAPSInt().getExtValue();
const bool isTrue =
S->getCond()
->EvaluateKnownConstInt(CVM.getCodeGenModule().getContext())
.getBoolValue();

extendRegion(S);

Expand Down
29 changes: 29 additions & 0 deletions clang/test/CoverageMapping/if.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,35 @@ constexpr int check_macro_consteval_if_skipped(int i) { // CHECK-NEXT: [[@LINE
return i;
}

struct false_value {
constexpr operator bool() {
return false;
}
};

template <typename> struct dependable_false_value {
constexpr operator bool() {
return false;
}
};

// GH-80285
void should_not_crash() {
if constexpr (false_value{}) { };
}

template <typename> void should_not_crash_dependable() {
if constexpr (dependable_false_value<int>{}) { };
}

void should_not_crash_with_template_instance() {
should_not_crash_dependable<int>();
}

void should_not_crash_with_requires_expr() {
if constexpr (requires {42;}) { };
}

int instantiate_consteval(int i) {
i *= check_consteval_with_else_discarded_then(i);
i *= check_notconsteval_with_else_discarded_else(i);
Expand Down

0 comments on commit 616b9e1

Please sign in to comment.