Author: Zequan Wu Date: 2023-04-13T10:53:02-04:00 New Revision: 0529da5b948cf168f65bec65b0559139f4f5a426
URL: https://github.com/llvm/llvm-project/commit/0529da5b948cf168f65bec65b0559139f4f5a426 DIFF: https://github.com/llvm/llvm-project/commit/0529da5b948cf168f65bec65b0559139f4f5a426.diff LOG: [Coverage] Handle invalid end location of an expression/statement. Fix a crash when an expression/statement can have valid start location but invalid end location in some situations. For example: https://github.com/llvm/llvm-project/blob/llvmorg-16.0.1/clang/lib/Sema/SemaExprCXX.cpp#L1536 This confuses `CounterCoverageMappingBuilder` when popping a region from region stack as if the end location is a macro or include location. Reviewed By: hans, aaron.ballman Differential Revision: https://reviews.llvm.org/D147073 Added: Modified: clang/docs/ReleaseNotes.rst clang/lib/CodeGen/CoverageMappingGen.cpp Removed: ################################################################################ diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 9abfaf5b2322a..b838858c09179 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -297,6 +297,10 @@ Bug Fixes in This Version not a type concept. - Fix crash when a doc comment contains a line splicing. (`#62054 <https://github.com/llvm/llvm-project/issues/62054>`_) +- Work around with a clang coverage crash which happens when visiting + expressions/statements with invalid source locations in non-assert builds. + Assert builds may still see assertions triggered from this. + (`#62105 <https://github.com/llvm/llvm-project/issues/62105>`_) Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp index 426ac39b8767e..bac01c7ff67f2 100644 --- a/clang/lib/CodeGen/CoverageMappingGen.cpp +++ b/clang/lib/CodeGen/CoverageMappingGen.cpp @@ -602,6 +602,19 @@ struct CounterCoverageMappingBuilder MostRecentLocation = *StartLoc; } + // If either of these locations is invalid, something elsewhere in the + // compiler has broken. + assert((!StartLoc || StartLoc->isValid()) && "Start location is not valid"); + assert((!EndLoc || EndLoc->isValid()) && "End location is not valid"); + + // However, we can still recover without crashing. + // If either location is invalid, set it to std::nullopt to avoid + // letting users of RegionStack think that region has a valid start/end + // location. + if (StartLoc && StartLoc->isInvalid()) + StartLoc = std::nullopt; + if (EndLoc && EndLoc->isInvalid()) + EndLoc = std::nullopt; RegionStack.emplace_back(Count, FalseCount, StartLoc, EndLoc); return RegionStack.size() - 1; @@ -624,7 +637,8 @@ struct CounterCoverageMappingBuilder assert(RegionStack.size() >= ParentIndex && "parent not in stack"); while (RegionStack.size() > ParentIndex) { SourceMappingRegion &Region = RegionStack.back(); - if (Region.hasStartLoc()) { + if (Region.hasStartLoc() && + (Region.hasEndLoc() || RegionStack[ParentIndex].hasEndLoc())) { SourceLocation StartLoc = Region.getBeginLoc(); SourceLocation EndLoc = Region.hasEndLoc() ? Region.getEndLoc() @@ -691,7 +705,7 @@ struct CounterCoverageMappingBuilder assert(SM.isWrittenInSameFile(Region.getBeginLoc(), EndLoc)); assert(SpellingRegion(SM, Region).isInSourceOrder()); SourceRegions.push_back(Region); - } + } RegionStack.pop_back(); } } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits