-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[clang][coverage] fixing "if constexpr" and "if consteval" coverage report #77214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-clang-codegen @llvm/pr-subscribers-clang Author: Hana Dusíková (hanickadot) ChangesIt was a while since I noticed coverage report is broken for "if constexpr" and "if consteval" (as shown on first picture). Report after this change: Main problem was replacement of non-taken "if constexpr" branch with With "if consteval" I'm no longer introducing new branch counter for non-taken "branch". But in future it would be useful to mark whole gap there as skipped instead. If there is interest I would do it in another PR. Full diff: https://github.com/llvm/llvm-project/pull/77214.diff 3 Files Affected:
diff --git a/clang/include/clang/AST/Stmt.h b/clang/include/clang/AST/Stmt.h
index da7b37ce0e1211..fb50212083316e 100644
--- a/clang/include/clang/AST/Stmt.h
+++ b/clang/include/clang/AST/Stmt.h
@@ -1631,8 +1631,10 @@ class CompoundStmt final
SourceLocation RB);
// Build an empty compound statement with a location.
- explicit CompoundStmt(SourceLocation Loc)
- : Stmt(CompoundStmtClass), LBraceLoc(Loc), RBraceLoc(Loc) {
+ explicit CompoundStmt(SourceLocation Loc) : CompoundStmt(Loc, Loc) {}
+
+ explicit CompoundStmt(SourceLocation Loc, SourceLocation EndLoc)
+ : Stmt(CompoundStmtClass), LBraceLoc(Loc), RBraceLoc(EndLoc) {
CompoundStmtBits.NumStmts = 0;
CompoundStmtBits.HasFPFeatures = 0;
}
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp
index bf227386a71b78..b245abd16c3f4a 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -1712,7 +1712,11 @@ struct CounterCoverageMappingBuilder
extendRegion(S->getCond());
Counter ParentCount = getRegion().getCounter();
- Counter ThenCount = getRegionCounter(S);
+
+ // If this is "if !consteval" the then-branch will never be taken, we don't
+ // need to change counter
+ Counter ThenCount =
+ S->isNegatedConsteval() ? ParentCount : getRegionCounter(S);
if (!S->isConsteval()) {
// Emitting a counter for the condition makes it easier to interpret the
@@ -1729,7 +1733,12 @@ struct CounterCoverageMappingBuilder
extendRegion(S->getThen());
Counter OutCount = propagateCounts(ThenCount, S->getThen());
- Counter ElseCount = subtractCounters(ParentCount, ThenCount);
+ // If this is "if consteval" the else-branch will never be taken, we don't
+ // need to change counter
+ Counter ElseCount = S->isNonNegatedConsteval()
+ ? ParentCount
+ : subtractCounters(ParentCount, ThenCount);
+
if (const Stmt *Else = S->getElse()) {
bool ThenHasTerminateStmt = HasTerminateStmt;
HasTerminateStmt = false;
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index c8c5a51bf9f94e..0033c851b618a1 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -7732,7 +7732,8 @@ TreeTransform<Derived>::TransformIfStmt(IfStmt *S) {
if (Then.isInvalid())
return StmtError();
} else {
- Then = new (getSema().Context) NullStmt(S->getThen()->getBeginLoc());
+ Then = new (getSema().Context)
+ CompoundStmt(S->getThen()->getBeginLoc(), S->getThen()->getEndLoc());
}
// Transform the "else" branch.
@@ -7741,6 +7742,10 @@ TreeTransform<Derived>::TransformIfStmt(IfStmt *S) {
Else = getDerived().TransformStmt(S->getElse());
if (Else.isInvalid())
return StmtError();
+ } else if (S->getElse() && ConstexprConditionValue &&
+ *ConstexprConditionValue) {
+ Else = new (getSema().Context)
+ CompoundStmt(S->getElse()->getBeginLoc(), S->getElse()->getEndLoc());
}
if (!getDerived().AlwaysRebuild() &&
|
5c6d6b3
to
8f1370a
Compare
c227189
to
bde3362
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the patch!
I think this might need a release note (referencing the couple open issues about this bug), and probably some tests.
Fixes #54419. |
bde3362
to
1891847
Compare
Adding a few folks to the review (We do not seem to update the coverage mapping very often) |
1891847
to
fb2efde
Compare
This would be very useful not to treat compile-time unreachable code as uncovered. Can the skipped code be marked separately in the reports? |
Yes, and it will be a bit bigger change. This is currently my biggest change yet :) But I want to do it as next. This fix's intention is to make the source location of regions properly done. |
c227189
to
413517b
Compare
Added some tests. And also pinging @ornata for review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some comments, mostly nits on the test.
LGTM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@hanickadot do you need me to merge that for you?
yes please, I don't have merge rights |
Oups, can you rebase? |
f46e9d6
to
766df92
Compare
…rt (llvm#77214) Replace the discarded statement by an empty compound statement so we can keep track of the whole source range we need to skip in coverage Fixes llvm#54419
It was a while since I noticed coverage report is broken for "if constexpr" and "if consteval" (as shown on first picture).
Main problem was replacement of non-taken "if constexpr" branch with
NullStmt
but such object doesn't have begin/end for source location properly. So I introduced a new constructor for emptyCompoundStmt
and used it.With "if consteval" I'm no longer introducing new branch counter for non-taken "branch". But in future it would be useful to mark whole gap there as skipped instead. If there is interest I would do it in another PR.