Index: clang/lib/CodeGen/CodeGenFunction.h =================================================================== --- clang/lib/CodeGen/CodeGenFunction.h +++ clang/lib/CodeGen/CodeGenFunction.h @@ -4353,7 +4353,7 @@ /// constant folds return true and set the folded value. bool ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &Result, bool AllowLabels = false); - + /// isLeafCondition - Determine whether the given condition is a leaf-level /// condition (i.e. no "&&" or "||"). static bool isLeafCondition(const Expr *C); @@ -4362,8 +4362,7 @@ /// increments a profile counter based on the semantics of the given logical /// operator opcode. This is used to instrument branch condition coverage /// for logical operators. - void EmitBranchToCounterBlock(const Expr *Cond, - BinaryOperator::Opcode LOp, + void EmitBranchToCounterBlock(const Expr *Cond, BinaryOperator::Opcode LOp, llvm::BasicBlock *TrueBlock, llvm::BasicBlock *FalseBlock, uint64_t TrueCount = 0, Index: clang/lib/CodeGen/CodeGenFunction.cpp =================================================================== --- clang/lib/CodeGen/CodeGenFunction.cpp +++ clang/lib/CodeGen/CodeGenFunction.cpp @@ -1465,6 +1465,12 @@ /// Determine whether the given condition is a leaf-level condition /// (i.e. no "&&" or "||"). bool CodeGenFunction::isLeafCondition(const Expr *C) { + // Bypass simplistic logical-NOT operator before determining whether the + // condition contains any other logical operator. + if (const UnaryOperator *UnOp = dyn_cast(C->IgnoreParens())) + if (UnOp->getOpcode() == UO_LNot) + C = UnOp->getSubExpr(); + const BinaryOperator *BOp = dyn_cast(C->IgnoreParens()); return (!BOp || !BOp->isLogicalOp()); } @@ -1477,10 +1483,9 @@ BinaryOperator::Opcode LOp, llvm::BasicBlock *TrueBlock, llvm::BasicBlock *FalseBlock, - uint64_t TrueCount /* = 0 */, - const Expr *CntrIdx /*=NULL*/) -{ - // If not instrumenting or this isn't a leaf condition, just emit a branch + uint64_t TrueCount /* = 0 */, + const Expr *CntrIdx /*=NULL*/) { + // If not instrumenting or this isn't a leaf condition, just emit a branch. bool InstrumentRegions = CGM.getCodeGenOpts().hasProfileClangInstr(); if (!InstrumentRegions || !isLeafCondition(Cond)) return EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount); @@ -1489,12 +1494,12 @@ llvm::BasicBlock *ElseBlock = NULL; llvm::BasicBlock *NextBlock = NULL; - // Create the block we'll use to increment the appropriate counter + // Create the block we'll use to increment the appropriate counter. llvm::BasicBlock *CounterIncrBlock = createBasicBlock("lop.rhscnt"); // Set block pointers according to Logical-AND (BO_LAnd) semantics. This // means we need to evaluate the condition and increment the counter on TRUE: - // + // // if (Cond) // goto CounterIncrBlock; // else @@ -1512,7 +1517,7 @@ // Set block pointers according to Logical-OR (BO_LOr) semantics. This means // we need to evaluate the condition and increment the counter on FALSE: - // + // // if (Cond) // goto TrueBlock; // else @@ -1526,21 +1531,20 @@ ThenBlock = TrueBlock; ElseBlock = CounterIncrBlock; NextBlock = FalseBlock; - } - else { - assert("Expected Opcode must be that of a Logical Operator"); + } else { + llvm_unreachable("Expected Opcode must be that of a Logical Operator"); } - // Emit Branch based on condition + // Emit Branch based on condition. EmitBranchOnBoolExpr(Cond, ThenBlock, ElseBlock, TrueCount); - // Emit the block containing the counter increment(s) + // Emit the block containing the counter increment(s). EmitBlock(CounterIncrBlock); - // Increment corresponding counter; if index not provided, use Cond as index + // Increment corresponding counter; if index not provided, use Cond as index. incrementProfileCounter(CntrIdx ? CntrIdx : Cond); - // Go to the next block + // Go to the next block. EmitBranch(NextBlock); } @@ -1564,8 +1568,8 @@ ConstantBool) { // br(1 && X) -> br(X). incrementProfileCounter(CondBOp); - return EmitBranchToCounterBlock(CondBOp->getRHS(), BO_LAnd, - TrueBlock, FalseBlock, TrueCount); + return EmitBranchToCounterBlock(CondBOp->getRHS(), BO_LAnd, TrueBlock, + FalseBlock, TrueCount); } // If we have "X && 1", simplify the code to use an uncond branch. @@ -1611,8 +1615,8 @@ !ConstantBool) { // br(0 || X) -> br(X). incrementProfileCounter(CondBOp); - return EmitBranchToCounterBlock(CondBOp->getRHS(), BO_LOr, - TrueBlock, FalseBlock, TrueCount); + return EmitBranchToCounterBlock(CondBOp->getRHS(), BO_LOr, TrueBlock, + FalseBlock, TrueCount); } // If we have "X || 0", simplify the code to use an uncond branch. Index: clang/lib/CodeGen/CodeGenPGO.cpp =================================================================== --- clang/lib/CodeGen/CodeGenPGO.cpp +++ clang/lib/CodeGen/CodeGenPGO.cpp @@ -160,7 +160,7 @@ PGOHash Hash; /// The map of statements to counters. llvm::DenseMap &CounterMap; - /// The profile version + /// The profile version. uint64_t ProfileVersion; MapRegionCounters(PGOHashVersion HashVersion, uint64_t ProfileVersion, Index: clang/lib/CodeGen/CoverageMappingGen.h =================================================================== --- clang/lib/CodeGen/CoverageMappingGen.h +++ clang/lib/CodeGen/CoverageMappingGen.h @@ -116,7 +116,7 @@ /// for the given file. unsigned getFileID(const FileEntry *File); - /// Return an interface into CodeGenModule + /// Return an interface into CodeGenModule. CodeGenModule &getCodeGenModule() { return CGM; } }; Index: clang/lib/CodeGen/CoverageMappingGen.cpp =================================================================== --- clang/lib/CodeGen/CoverageMappingGen.cpp +++ clang/lib/CodeGen/CoverageMappingGen.cpp @@ -71,10 +71,10 @@ /// A region of source code that can be mapped to a counter. class SourceMappingRegion { - /// Primary Counter that is also used for Branch Regions for "True" branches + /// Primary Counter that is also used for Branch Regions for "True" branches. Counter Count; - /// Secondary Counter used for Branch Regions for "False" branches + /// Secondary Counter used for Branch Regions for "False" branches. Optional FalseCount; /// The region's starting location. @@ -92,26 +92,23 @@ public: SourceMappingRegion(Counter Count, Optional LocStart, - Optional LocEnd, - bool DeferRegion = false, + Optional LocEnd, bool DeferRegion = false, bool GapRegion = false) : Count(Count), LocStart(LocStart), LocEnd(LocEnd), DeferRegion(DeferRegion), GapRegion(GapRegion) {} SourceMappingRegion(Counter Count, Optional FalseCount, Optional LocStart, - Optional LocEnd, - bool DeferRegion = false, + Optional LocEnd, bool DeferRegion = false, bool GapRegion = false) - : Count(Count), FalseCount(FalseCount), - LocStart(LocStart), LocEnd(LocEnd), - DeferRegion(DeferRegion), GapRegion(GapRegion) {} + : Count(Count), FalseCount(FalseCount), LocStart(LocStart), + LocEnd(LocEnd), DeferRegion(DeferRegion), GapRegion(GapRegion) {} const Counter &getCounter() const { return Count; } const Counter &getFalseCounter() const { - assert(FalseCount && "Region has no alternate counter"); - return *FalseCount; + assert(FalseCount && "Region has no alternate counter"); + return *FalseCount; } void setCounter(Counter C) { Count = C; } @@ -573,8 +570,7 @@ /// /// Returns the index on the stack where the region was pushed. This can be /// used with popRegions to exit a "scope", ending the region that was pushed. - size_t pushRegion(Counter Count, - Optional StartLoc = None, + size_t pushRegion(Counter Count, Optional StartLoc = None, Optional EndLoc = None, Optional FalseCount = None) { @@ -681,37 +677,43 @@ bool UnnestStart = StartDepth >= EndDepth; bool UnnestEnd = EndDepth >= StartDepth; if (UnnestEnd) { - // The region ends in a nested file or macro expansion. Create a - // separate region for each expansion. - // Don't do this for branch regions + // The region ends in a nested file or macro expansion. If the + // region is not a branch region, create a separate region for each + // expansion, and for all regions, update the EndLoc. Branch + // regions should not be split in order to keep a straightforward + // correspondance between the region and its associated branch + // condition, even if the condition spans multiple depths. SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc); assert(SM.isWrittenInSameFile(NestedLoc, EndLoc)); - if (!isBranch && - !isRegionAlreadyAdded(NestedLoc, EndLoc)) - SourceRegions.emplace_back(Region.getCounter(), - NestedLoc, EndLoc); + if (!isBranch && !isRegionAlreadyAdded(NestedLoc, EndLoc)) + SourceRegions.emplace_back(Region.getCounter(), NestedLoc, + EndLoc); EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc)); if (EndLoc.isInvalid()) - llvm::report_fatal_error("File exit not handled before popRegions"); + llvm::report_fatal_error( + "File exit not handled before popRegions"); EndDepth--; } if (UnnestStart) { - // The region begins in a nested file or macro expansion. Create a - // separate region for each expansion. - // Don't do this for branch regions + // The region ends in a nested file or macro expansion. If the + // region is not a branch region, create a separate region for each + // expansion, and for all regions, update the StartLoc. Branch + // regions should not be split in order to keep a straightforward + // correspondance between the region and its associated branch + // condition, even if the condition spans multiple depths. SourceLocation NestedLoc = getEndOfFileOrMacro(StartLoc); assert(SM.isWrittenInSameFile(StartLoc, NestedLoc)); - if (!isBranch && - !isRegionAlreadyAdded(StartLoc, NestedLoc)) - SourceRegions.emplace_back(Region.getCounter(), - StartLoc, NestedLoc); + if (!isBranch && !isRegionAlreadyAdded(StartLoc, NestedLoc)) + SourceRegions.emplace_back(Region.getCounter(), StartLoc, + NestedLoc); StartLoc = getIncludeOrExpansionLoc(StartLoc); if (StartLoc.isInvalid()) - llvm::report_fatal_error("File exit not handled before popRegions"); + llvm::report_fatal_error( + "File exit not handled before popRegions"); StartDepth--; } } @@ -719,12 +721,12 @@ Region.setEndLoc(EndLoc); if (!isBranch) { - MostRecentLocation = EndLoc; - // If this region happens to span an entire expansion, we need to - // make sure we don't overlap the parent region with it. - if (StartLoc == getStartOfFileOrMacro(StartLoc) && - EndLoc == getEndOfFileOrMacro(EndLoc)) - MostRecentLocation = getIncludeOrExpansionLoc(EndLoc); + MostRecentLocation = EndLoc; + // If this region happens to span an entire expansion, we need to + // make sure we don't overlap the parent region with it. + if (StartLoc == getStartOfFileOrMacro(StartLoc) && + EndLoc == getEndOfFileOrMacro(EndLoc)) + MostRecentLocation = getIncludeOrExpansionLoc(EndLoc); } assert(SM.isWrittenInSameFile(Region.getBeginLoc(), EndLoc)); @@ -784,7 +786,7 @@ return ExitCount; } - /// Determine whether the given condition folds to true or false + /// Determine whether the given condition can be constant folded. bool ConditionFoldsToBool(const Expr *Cond) { Expr::EvalResult Result; return (Cond->EvaluateAsInt(Result, CVM.getCodeGenModule().getContext())); @@ -795,7 +797,7 @@ /// "True" counter and a "False" counter for all leaf-level boolean /// expressions that result in the generation of a branch. void createBranchRegion(const Expr *C, Counter TrueCnt, Counter FalseCnt) { - // Check for NULL conditions + // Check for NULL conditions. if (!C) return; @@ -814,16 +816,15 @@ popRegions(pushRegion(Counter::getZero(), getStart(C), getEnd(C), Counter::getZero())); else - // Otherwise, create a region with the True counter and False counter + // Otherwise, create a region with the True counter and False counter. popRegions(pushRegion(TrueCnt, getStart(C), getEnd(C), FalseCnt)); } } /// Create a Branch Region around a SwitchCase for code coverage - /// and add it to the function's SourceRegions + /// and add it to the function's SourceRegions. void createSwitchCaseRegion(const SwitchCase *SC, Counter TrueCnt, - Counter FalseCnt) - { + Counter FalseCnt) { // Push region onto RegionStack but immediately pop it (which adds it to // the function's SourceRegions) because it doesn't apply to any other // source other than the SwitchCase. @@ -901,12 +902,12 @@ // correct count. We avoid creating redundant regions by stopping once // we've seen this region. if (StartLocs.insert(Loc).second) { - if (I.isBranch()) - SourceRegions.emplace_back(I.getCounter(), I.getFalseCounter(), Loc, - getEndOfFileOrMacro(Loc), I.isBranch()); - else - SourceRegions.emplace_back(I.getCounter(), Loc, - getEndOfFileOrMacro(Loc)); + if (I.isBranch()) + SourceRegions.emplace_back(I.getCounter(), I.getFalseCounter(), Loc, + getEndOfFileOrMacro(Loc), I.isBranch()); + else + SourceRegions.emplace_back(I.getCounter(), Loc, + getEndOfFileOrMacro(Loc)); } Loc = getIncludeOrExpansionLoc(Loc); } @@ -1141,9 +1142,9 @@ if (OutCount != ParentCount) pushRegion(OutCount); - // Create Branch Region around condition + // Create Branch Region around condition. createBranchRegion(S->getCond(), BodyCount, - subtractCounters(CondCount, BodyCount)); + subtractCounters(CondCount, BodyCount)); } void VisitDoStmt(const DoStmt *S) { @@ -1166,9 +1167,9 @@ if (OutCount != ParentCount) pushRegion(OutCount); - // Create Branch Region around condition + // Create Branch Region around condition. createBranchRegion(S->getCond(), BodyCount, - subtractCounters(CondCount, BodyCount)); + subtractCounters(CondCount, BodyCount)); } void VisitForStmt(const ForStmt *S) { @@ -1217,9 +1218,9 @@ if (OutCount != ParentCount) pushRegion(OutCount); - // Create Branch Region around condition + // Create Branch Region around condition. createBranchRegion(S->getCond(), BodyCount, - subtractCounters(CondCount, BodyCount)); + subtractCounters(CondCount, BodyCount)); } void VisitCXXForRangeStmt(const CXXForRangeStmt *S) { @@ -1331,8 +1332,9 @@ for (; Case; Case = Case->getNextSwitchCase()) { HasDefaultCase = HasDefaultCase || isa(Case); CaseCountSum = addCounters(CaseCountSum, getRegionCounter(Case)); - createSwitchCaseRegion(Case, getRegionCounter(Case), - subtractCounters(ParentCount, getRegionCounter(Case))); + createSwitchCaseRegion( + Case, getRegionCounter(Case), + subtractCounters(ParentCount, getRegionCounter(Case))); } // If no explicit default case exists, create a branch region to represent @@ -1406,9 +1408,9 @@ if (OutCount != ParentCount) pushRegion(OutCount); - // Create Branch Region around condition + // Create Branch Region around condition. createBranchRegion(S->getCond(), ThenCount, - subtractCounters(ParentCount, ThenCount)); + subtractCounters(ParentCount, ThenCount)); } void VisitCXXTryStmt(const CXXTryStmt *S) { @@ -1453,9 +1455,9 @@ propagateCounts(subtractCounters(ParentCount, TrueCount), E->getFalseExpr()); - // Create Branch Region around condition + // Create Branch Region around condition. createBranchRegion(E->getCond(), TrueCount, - subtractCounters(ParentCount, TrueCount)); + subtractCounters(ParentCount, TrueCount)); } void VisitBinLAnd(const BinaryOperator *E) { @@ -1467,22 +1469,22 @@ extendRegion(E->getRHS()); propagateCounts(getRegionCounter(E), E->getRHS()); - // Extract the RHS's Execution Counter + // Extract the RHS's Execution Counter. Counter RHSExecCnt = getRegionCounter(E); - // Extract the RHS's "True" Instance Counter + // Extract the RHS's "True" Instance Counter. Counter RHSTrueCnt = getRegionCounter(E->getRHS()); - // Extract the Parent Region Counter + // Extract the Parent Region Counter. Counter ParentCnt = getRegion().getCounter(); - // Create Branch Region around LHS condition + // Create Branch Region around LHS condition. createBranchRegion(E->getLHS(), RHSExecCnt, - subtractCounters(ParentCnt, RHSExecCnt)); + subtractCounters(ParentCnt, RHSExecCnt)); - // Create Branch Region around RHS condition + // Create Branch Region around RHS condition. createBranchRegion(E->getRHS(), RHSTrueCnt, - subtractCounters(RHSExecCnt, RHSTrueCnt)); + subtractCounters(RHSExecCnt, RHSTrueCnt)); } void VisitBinLOr(const BinaryOperator *E) { @@ -1494,22 +1496,22 @@ extendRegion(E->getRHS()); propagateCounts(getRegionCounter(E), E->getRHS()); - // Extract the RHS's Execution Counter + // Extract the RHS's Execution Counter. Counter RHSExecCnt = getRegionCounter(E); - // Extract the RHS's "False" Instance Counter + // Extract the RHS's "False" Instance Counter. Counter RHSFalseCnt = getRegionCounter(E->getRHS()); - // Extract the Parent Region Counter + // Extract the Parent Region Counter. Counter ParentCnt = getRegion().getCounter(); - // Create Branch Region around LHS condition + // Create Branch Region around LHS condition. createBranchRegion(E->getLHS(), subtractCounters(ParentCnt, RHSExecCnt), - RHSExecCnt); + RHSExecCnt); - // Create Branch Region around RHS condition + // Create Branch Region around RHS condition. createBranchRegion(E->getRHS(), subtractCounters(RHSExecCnt, RHSFalseCnt), - RHSFalseCnt); + RHSFalseCnt); } void VisitLambdaExpr(const LambdaExpr *LE) { @@ -1556,7 +1558,8 @@ Ctx.dump(R.Count, OS); if (R.Kind == CounterMappingRegion::BranchRegion) { - OS << ", "; Ctx.dump(R.FalseCount, OS); + OS << ", "; + Ctx.dump(R.FalseCount, OS); } if (R.Kind == CounterMappingRegion::ExpansionRegion) Index: clang/test/CoverageMapping/branch-constfolded.cpp =================================================================== --- clang/test/CoverageMapping/branch-constfolded.cpp +++ clang/test/CoverageMapping/branch-constfolded.cpp @@ -1,4 +1,4 @@ -// Test that branch regions are not generated for constant-folded conditions +// Test that branch regions are not generated for constant-folded conditions. // RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -emit-llvm-only -main-file-name branch-constfolded.cpp %s | FileCheck %s Index: clang/test/CoverageMapping/branch-logical-mixed.cpp =================================================================== --- clang/test/CoverageMapping/branch-logical-mixed.cpp +++ clang/test/CoverageMapping/branch-logical-mixed.cpp @@ -1,5 +1,5 @@ // Test to ensure that each branch condition has an associated branch region -// with expected True/False counters +// with expected True/False counters. // RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -emit-llvm-only -main-file-name branch-logical-mixed.cpp %s | FileCheck %s Index: clang/test/CoverageMapping/branch-macros.cpp =================================================================== --- clang/test/CoverageMapping/branch-macros.cpp +++ clang/test/CoverageMapping/branch-macros.cpp @@ -1,5 +1,5 @@ // Test that branch regions are generated for conditions in nested macro -// expansions +// expansions. // RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -emit-llvm-only -main-file-name branch-macros.cpp %s | FileCheck %s Index: clang/test/CoverageMapping/branch-mincounters.cpp =================================================================== --- clang/test/CoverageMapping/branch-mincounters.cpp +++ clang/test/CoverageMapping/branch-mincounters.cpp @@ -10,7 +10,7 @@ bool b1 = a == b; bool b2 = a >= b; - // This should allocate a RHS branch counter on b2 (counter #3) + // This should allocate a RHS branch counter on b2 (counter #3). bool c = b0 && (b1 || b2); // CHECK: Branch,File 0, [[@LINE-1]]:12 -> [[@LINE-1]]:14 = #1, (#0 - #1) // CHECK: Branch,File 0, [[@LINE-2]]:19 -> [[@LINE-2]]:21 = (#1 - #2), #2 @@ -26,7 +26,7 @@ bool b2 = a >= b; // This should allocate a RHS branch counter on b1 and b2 (counters #2, #4) - // This could possibly be further optimized through counter reuse (future) + // This could possibly be further optimized through counter reuse (future). bool c = (b0 && b1) || b2; // CHECK: Branch,File 0, [[@LINE-1]]:13 -> [[@LINE-1]]:15 = #3, (#0 - #3) // CHECK: Branch,File 0, [[@LINE-2]]:19 -> [[@LINE-2]]:21 = #4, (#3 - #4) @@ -43,7 +43,7 @@ bool b3 = a < b; // This should allocate a RHS branch counter on b1 and b3 (counters #3, #5) - // This could possibly be further optimized through counter reuse (future) + // This could possibly be further optimized through counter reuse (future). bool c = (b0 || b1) && (b2 || b3); // CHECK: Branch,File 0, [[@LINE-1]]:13 -> [[@LINE-1]]:15 = (#0 - #2), #2 // CHECK: Branch,File 0, [[@LINE-2]]:19 -> [[@LINE-2]]:21 = (#2 - #3), #3 Index: clang/test/CoverageMapping/branch-templates.cpp =================================================================== --- clang/test/CoverageMapping/branch-templates.cpp +++ clang/test/CoverageMapping/branch-templates.cpp @@ -1,5 +1,5 @@ // Test that branch regions are generated for conditions in function template -// instantiations +// instantiations. // RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -emit-llvm-only -main-file-name branch-templates.cpp %s | FileCheck %s Index: llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h =================================================================== --- llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h +++ llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h @@ -206,7 +206,7 @@ /// A Counter mapping region associates a source range with a specific counter. struct CounterMappingRegion { enum RegionKind { - /// A CodeRegion associates some code with a counter + /// A CodeRegion associates some code with a counter. CodeRegion, /// An ExpansionRegion represents a file expansion region that associates @@ -224,14 +224,14 @@ /// A BranchRegion represents leaf-level boolean expressions and is /// associated with two counters, each representing the number of times the - /// expression evaluates to true or false + /// expression evaluates to true or false. BranchRegion }; - /// Primary Counter that is also used for Branch Regions (TrueCount) + /// Primary Counter that is also used for Branch Regions (TrueCount). Counter Count; - /// Secondary Counter used for Branch Regions (FalseCount) + /// Secondary Counter used for Branch Regions (FalseCount). Counter FalseCount; unsigned FileID, ExpandedFileID; @@ -245,10 +245,10 @@ LineStart(LineStart), ColumnStart(ColumnStart), LineEnd(LineEnd), ColumnEnd(ColumnEnd), Kind(Kind) {} - CounterMappingRegion(Counter Count, Counter FalseCount, - unsigned FileID, unsigned ExpandedFileID, - unsigned LineStart, unsigned ColumnStart, - unsigned LineEnd, unsigned ColumnEnd, RegionKind Kind) + CounterMappingRegion(Counter Count, Counter FalseCount, unsigned FileID, + unsigned ExpandedFileID, unsigned LineStart, + unsigned ColumnStart, unsigned LineEnd, + unsigned ColumnEnd, RegionKind Kind) : Count(Count), FalseCount(FalseCount), FileID(FileID), ExpandedFileID(ExpandedFileID), LineStart(LineStart), ColumnStart(ColumnStart), LineEnd(LineEnd), ColumnEnd(ColumnEnd), @@ -302,17 +302,16 @@ struct CountedRegion : public CounterMappingRegion { uint64_t ExecutionCount; uint64_t FalseExecutionCount; - bool Folded; + bool Folded; CountedRegion(const CounterMappingRegion &R, uint64_t ExecutionCount) : CounterMappingRegion(R), ExecutionCount(ExecutionCount), - FalseExecutionCount(0), Folded(false) {} + FalseExecutionCount(0), Folded(false) {} CountedRegion(const CounterMappingRegion &R, uint64_t ExecutionCount, - uint64_t FalseExecutionCount) + uint64_t FalseExecutionCount) : CounterMappingRegion(R), ExecutionCount(ExecutionCount), - FalseExecutionCount(FalseExecutionCount), - Folded(false) {} + FalseExecutionCount(FalseExecutionCount), Folded(false) {} }; /// A Counter mapping context is used to connect the counters, expressions @@ -361,11 +360,11 @@ FunctionRecord &operator=(FunctionRecord &&) = default; void pushRegion(CounterMappingRegion Region, uint64_t Count, - uint64_t FalseCount) { + uint64_t FalseCount) { if (Region.Kind == CounterMappingRegion::BranchRegion) { CountedBranchRegions.emplace_back(Region, Count, FalseCount); // If both counters are hard-coded to zero, then this region represents a - // constant-folded branch + // constant-folded branch. if (Region.Count.isZero() && Region.FalseCount.isZero()) CountedBranchRegions.back().Folded = true; return; @@ -457,10 +456,10 @@ IsRegionEntry(IsRegionEntry), IsGapRegion(IsGapRegion) {} friend bool operator==(const CoverageSegment &L, const CoverageSegment &R) { - return std::tie(L.Line, L.Col, L.Count, L.HasCount, - L.IsRegionEntry, L.IsGapRegion) == - std::tie(R.Line, R.Col, R.Count, R.HasCount, - R.IsRegionEntry, R.IsGapRegion); + return std::tie(L.Line, L.Col, L.Count, L.HasCount, L.IsRegionEntry, + L.IsGapRegion) == std::tie(R.Line, R.Col, R.Count, + R.HasCount, R.IsRegionEntry, + R.IsGapRegion); } }; Index: llvm/include/llvm/ProfileData/InstrProf.h =================================================================== --- llvm/include/llvm/ProfileData/InstrProf.h +++ llvm/include/llvm/ProfileData/InstrProf.h @@ -990,7 +990,7 @@ // In this version, the frontend PGO stable hash algorithm got fixed and // may produce hashes different from Version5. Version6 = 6, - // An additional counter is added around logical operators + // An additional counter is added around logical operators. Version7 = 7, // The current version is 7. CurrentVersion = INSTR_PROF_INDEX_VERSION Index: llvm/lib/ProfileData/Coverage/CoverageMapping.cpp =================================================================== --- llvm/lib/ProfileData/Coverage/CoverageMapping.cpp +++ llvm/lib/ProfileData/Coverage/CoverageMapping.cpp @@ -669,7 +669,7 @@ if (MainFileID && isExpansion(CR, *MainFileID)) FileCoverage.Expansions.emplace_back(CR, Function); } - // Capture branch regions specific to the function (excluding expansions) + // Capture branch regions specific to the function (excluding expansions). for (const auto &CR : Function.CountedBranchRegions) if (FileIDs.test(CR.FileID) && (CR.FileID == CR.ExpandedFileID)) FileCoverage.BranchRegions.push_back(CR); @@ -720,7 +720,7 @@ if (isExpansion(CR, *MainFileID)) FunctionCoverage.Expansions.emplace_back(CR, Function); } - // Capture branch regions specific to the function (excluding expansions) + // Capture branch regions specific to the function (excluding expansions). for (const auto &CR : Function.CountedBranchRegions) if (CR.FileID == *MainFileID) FunctionCoverage.BranchRegions.push_back(CR); @@ -744,7 +744,7 @@ ExpansionCoverage.Expansions.emplace_back(CR, Expansion.Function); } for (const auto &CR : Expansion.Function.CountedBranchRegions) - // Capture branch regions that only pertain to the corresponding expansion + // Capture branch regions that only pertain to the corresponding expansion. if (CR.FileID == Expansion.FileID) ExpansionCoverage.BranchRegions.push_back(CR); Index: llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp =================================================================== --- llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp +++ llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp @@ -258,12 +258,12 @@ Kind = CounterMappingRegion::SkippedRegion; break; case CounterMappingRegion::BranchRegion: - // For a Branch Region, read two successive counters + // For a Branch Region, read two successive counters. Kind = CounterMappingRegion::BranchRegion; if (auto Err = readCounter(C)) - return Err; + return Err; if (auto Err = readCounter(C2)) - return Err; + return Err; break; default: return make_error(coveragemap_error::malformed); Index: llvm/test/tools/llvm-cov/branch-c-general.c =================================================================== --- llvm/test/tools/llvm-cov/branch-c-general.c +++ llvm/test/tools/llvm-cov/branch-c-general.c @@ -156,7 +156,7 @@ case (1 << 2) ... (1 << 12):// CHECK: Branch ([[@LINE]]:5): [True: 11, False: 21] if (i) {} // CHECK: Branch ([[@LINE]]:11): [True: 11, False: 0] break; - // The branch for the large case range above appears after the case body + // The branch for the large case range above appears after the case body. case (1 << 13): // CHECK: Branch ([[@LINE]]:5): [True: 1, False: 31] if (i) {} // CHECK: Branch ([[@LINE]]:11): [True: 1, False: 0] @@ -164,8 +164,8 @@ case (1 << 14) ... (1 << 28):// CHECK: Branch ([[@LINE]]:5): [True: 15, False: 17] if (i) {} // CHECK: Branch ([[@LINE]]:11): [True: 15, False: 0] break; - // The branch for the large case range above appears after the case body - // CHECK: Branch ([[@LINE+1]]:5): [True: 1, False: 31] + // The branch for the large case range above appears after the case body. + // CHECK: Branch ([[@LINE+1]]:5): [True: 1, False: 31] case (1 << 29) ... ((1 << 29) + 1): if (i) {} // CHECK: Branch ([[@LINE]]:11): [True: 1, False: 0] break; @@ -263,18 +263,18 @@ // REPORT-NEXT: --- // REPORT-NEXT: simple_loops 8 0 100.00% 9 0 100.00% 6 0 100.00% // REPORT-NEXT: conditionals 24 0 100.00% 15 0 100.00% 16 2 87.50% -// REPORT-NEXT: early_exits 20 4 80.00% 25 2 92.00% 16 6 62.50% -// REPORT-NEXT: jumps 39 12 69.23% 48 2 95.83% 26 9 65.38% -// REPORT-NEXT: switches 28 5 82.14% 38 4 89.47% 30 9 70.00% +// REPORT-NEXT: early_exits 20 4 80.00% 25 3 88.00% 16 6 62.50% +// REPORT-NEXT: jumps 39 12 69.23% 48 4 91.67% 26 9 65.38% +// REPORT-NEXT: switches 28 5 82.14% 38 5 86.84% 30 9 70.00% // REPORT-NEXT: big_switch 25 1 96.00% 32 0 100.00% 30 6 80.00% // REPORT-NEXT: boolean_operators 16 0 100.00% 13 0 100.00% 22 2 90.91% // REPORT-NEXT: boolop_loops 19 0 100.00% 14 0 100.00% 16 2 87.50% -// REPORT-NEXT: conditional_operator 4 2 50.00% 8 0 100.00% 4 2 50.00% +// REPORT-NEXT: conditional_operator 4 2 50.00% 8 1 87.50% 4 2 50.00% // REPORT-NEXT: do_fallthrough 9 0 100.00% 12 0 100.00% 6 0 100.00% // REPORT-NEXT: main 1 0 100.00% 16 0 100.00% 0 0 0.00% // REPORT-NEXT: c-general.c:static_func 4 0 100.00% 4 0 100.00% 2 0 100.00% // REPORT-NEXT: --- -// REPORT-NEXT: TOTAL 197 24 87.82% 234 8 96.58% 174 38 78.16% +// REPORT-NEXT: TOTAL 197 24 87.82% 234 13 94.44% 174 38 78.16% // Test file-level report. // RUN: llvm-profdata merge %S/Inputs/branch-c-general.proftext -o %t.profdata @@ -302,7 +302,7 @@ // HTML-INDEX: // HTML-INDEX: 100.00% (12/12) // HTML-INDEX: -// HTML-INDEX: 96.58% (226/234) +// HTML-INDEX: 94.44% (221/234) // HTML-INDEX: // HTML-INDEX: 87.82% (173/197) // HTML-INDEX: Index: llvm/test/tools/llvm-cov/branch-export-json.test =================================================================== --- llvm/test/tools/llvm-cov/branch-export-json.test +++ llvm/test/tools/llvm-cov/branch-export-json.test @@ -20,7 +20,7 @@ // CHECK: 53,12,53,20,50,5,0,0,4 // CHECK: {"count":30,"covered":26,"notcovered":4,"percent":86.666666666666671} -// Check recursive macro-expansions +// Check recursive macro-expansions. // RUN: llvm-profdata merge %S/Inputs/branch-macros.proftext -o %t.profdata // RUN: llvm-cov export --format=text %S/Inputs/branch-macros.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S %S/branch-macros.c | FileCheck %s -check-prefix=MACROS Index: llvm/test/tools/llvm-cov/branch-export-lcov.test =================================================================== --- llvm/test/tools/llvm-cov/branch-export-lcov.test +++ llvm/test/tools/llvm-cov/branch-export-lcov.test @@ -36,7 +36,7 @@ // CHECK: BRF:30 // CHECK: BFH:26 -// Check recursive macro-expansions +// Check recursive macro-expansions. // RUN: llvm-profdata merge %S/Inputs/branch-macros.proftext -o %t.profdata // RUN: llvm-cov export --format=lcov %S/Inputs/branch-macros.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S %S/branch-macros.cpp | FileCheck %s -check-prefix=MACROS Index: llvm/test/tools/llvm-cov/branch-logical-mixed.cpp =================================================================== --- llvm/test/tools/llvm-cov/branch-logical-mixed.cpp +++ llvm/test/tools/llvm-cov/branch-logical-mixed.cpp @@ -84,7 +84,7 @@ // REPORT: Name Regions Miss Cover Lines Miss Cover Branches Miss Cover // REPORT-NEXT: --- -// REPORT-NEXT: _Z4funcii 77 9 88.31% 68 3 95.59% 80 32 60.00% +// REPORT-NEXT: _Z4funcii 77 9 88.31% 68 10 85.29% 80 32 60.00% // REPORT-NEXT: main 1 0 100.00% 5 0 100.00% 0 0 0.00% // REPORT-NEXT: --- -// REPORT-NEXT: TOTAL 78 9 88.46% 73 3 95.89% 80 32 60.00% +// REPORT-NEXT: TOTAL 78 9 88.46% 73 10 86.30% 80 32 60.00% Index: llvm/test/tools/llvm-cov/branch-noShowBranch.test =================================================================== --- llvm/test/tools/llvm-cov/branch-noShowBranch.test +++ llvm/test/tools/llvm-cov/branch-noShowBranch.test @@ -20,6 +20,6 @@ // REPORT-NOT: do_fallthrough 9 0 100.00% 12 0 100.00% 6 0 100.00% // REPORT-NOT: main 1 0 100.00% 16 0 100.00% 0 0 0.00% // REPORT-NOT: c-general.c:static_func 4 0 100.00% 4 0 100.00% 2 0 100.00% -// REPORT: TOTAL 197 24 87.82% 234 8 96.58% -// REPORT-NOT: TOTAL 197 24 87.82% 234 8 96.58% 174 38 78.16% +// REPORT: TOTAL 197 24 87.82% 234 13 94.44% +// REPORT-NOT: TOTAL 197 24 87.82% 234 13 94.44% 174 38 78.16% Index: llvm/tools/llvm-cov/CodeCoverage.cpp =================================================================== --- llvm/tools/llvm-cov/CodeCoverage.cpp +++ llvm/tools/llvm-cov/CodeCoverage.cpp @@ -89,8 +89,7 @@ const CoverageMapping &Coverage); /// Create source views for the branches of the view. - void attachBranchSubViews(SourceCoverageView &View, - StringRef SourceName, + void attachBranchSubViews(SourceCoverageView &View, StringRef SourceName, ArrayRef Branches, const MemoryBuffer &File, CoverageData &CoverageInfo); @@ -283,17 +282,18 @@ } } -void CodeCoverageTool::attachBranchSubViews( - SourceCoverageView &View, StringRef SourceName, - ArrayRef Branches, - const MemoryBuffer &File, CoverageData &CoverageInfo) { +void CodeCoverageTool::attachBranchSubViews(SourceCoverageView &View, + StringRef SourceName, + ArrayRef Branches, + const MemoryBuffer &File, + CoverageData &CoverageInfo) { if (!ViewOpts.ShowBranchCounts && !ViewOpts.ShowBranchPercents) return; - auto NextBranch = Branches.begin(); - auto EndBranch = Branches.end(); + const auto *NextBranch = Branches.begin(); + const auto *EndBranch = Branches.end(); - // Group branches that have the same line number into the same subview + // Group branches that have the same line number into the same subview. while (NextBranch != EndBranch) { std::vector ViewBranches; unsigned CurrentLine = NextBranch->LineStart; @@ -303,7 +303,7 @@ if (!ViewBranches.empty()) { auto SubView = SourceCoverageView::create(SourceName, File, ViewOpts, - std::move(CoverageInfo)); + std::move(CoverageInfo)); View.addBranch(CurrentLine, ViewBranches, std::move(SubView)); } } @@ -865,12 +865,11 @@ cl::opt ShowBranches( "show-branches", cl::Optional, - cl::desc("Show coverage for branch conditions"), - cl::cat(ViewCategory), + cl::desc("Show coverage for branch conditions"), cl::cat(ViewCategory), cl::values(clEnumValN(CoverageViewOptions::BranchOutputType::Count, - "count", "Show True/False counts"), + "count", "Show True/False counts"), clEnumValN(CoverageViewOptions::BranchOutputType::Percent, - "percent", "Show True/False percent")), + "percent", "Show True/False percent")), cl::init(CoverageViewOptions::BranchOutputType::Off)); cl::opt ShowBestLineRegionsCounts( @@ -916,10 +915,10 @@ !ShowRegions || ShowBestLineRegionsCounts; ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts; ViewOpts.ShowExpandedRegions = ShowExpansions; - ViewOpts.ShowBranchCounts = ShowBranches == - CoverageViewOptions::BranchOutputType::Count; - ViewOpts.ShowBranchPercents = ShowBranches == - CoverageViewOptions::BranchOutputType::Percent; + ViewOpts.ShowBranchCounts = + ShowBranches == CoverageViewOptions::BranchOutputType::Count; + ViewOpts.ShowBranchPercents = + ShowBranches == CoverageViewOptions::BranchOutputType::Percent; ViewOpts.ShowFunctionInstantiations = ShowInstantiations; ViewOpts.ShowOutputDirectory = ShowOutputDirectory; ViewOpts.TabSize = TabSize; Index: llvm/tools/llvm-cov/CoverageExporterJson.cpp =================================================================== --- llvm/tools/llvm-cov/CoverageExporterJson.cpp +++ llvm/tools/llvm-cov/CoverageExporterJson.cpp @@ -91,11 +91,11 @@ } json::Array renderBranch(const coverage::CountedRegion &Region) { - return json::Array({Region.LineStart, Region.ColumnStart, Region.LineEnd, - Region.ColumnEnd, clamp_uint64_to_int64(Region.ExecutionCount), - clamp_uint64_to_int64(Region.FalseExecutionCount), - Region.FileID, Region.ExpandedFileID, - int64_t(Region.Kind)}); + return json::Array( + {Region.LineStart, Region.ColumnStart, Region.LineEnd, Region.ColumnEnd, + clamp_uint64_to_int64(Region.ExecutionCount), + clamp_uint64_to_int64(Region.FalseExecutionCount), Region.FileID, + Region.ExpandedFileID, int64_t(Region.Kind)}); } json::Array renderRegions(ArrayRef Regions) { @@ -114,20 +114,19 @@ } std::vector -CollectNestedBranches(const coverage::CoverageMapping &Coverage, - ArrayRef Expansions) -{ +collectNestedBranches(const coverage::CoverageMapping &Coverage, + ArrayRef Expansions) { std::vector Branches; for (const auto &Expansion : Expansions) { auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion); - // Recursively collect branches from nested expansions + // Recursively collect branches from nested expansions. auto NestedExpansions = ExpansionCoverage.getExpansions(); - auto NestedExBranches = CollectNestedBranches(Coverage, NestedExpansions); + auto NestedExBranches = collectNestedBranches(Coverage, NestedExpansions); Branches.insert(Branches.end(), NestedExBranches.begin(), - NestedExBranches.end()); + NestedExBranches.end()); - // Add branches from this level of expansion + // Add branches from this level of expansion. auto ExBranches = ExpansionCoverage.getBranches(); for (auto B : ExBranches) if (B.FileID == Expansion.FileID) @@ -139,7 +138,7 @@ json::Object renderExpansion(const coverage::CoverageMapping &Coverage, const coverage::ExpansionRecord &Expansion) { - std::vector Expansions = { Expansion }; + std::vector Expansions = {Expansion}; return json::Object( {{"filenames", json::Array(Expansion.Function.Filenames)}, // Mark the beginning and end of this expansion in the source file. @@ -147,8 +146,8 @@ // Enumerate the coverage information for the expansion. {"target_regions", renderRegions(Expansion.Function.CountedRegions)}, // Enumerate the branch coverage information for the expansion. - {"branches", renderBranchRegions(CollectNestedBranches(Coverage, - Expansions))}}); + {"branches", + renderBranchRegions(collectNestedBranches(Coverage, Expansions))}}); } json::Object renderSummary(const FileCoverageSummary &Summary) { @@ -220,7 +219,8 @@ File["segments"] = renderFileSegments(FileCoverage, FileReport); File["branches"] = renderFileBranches(FileCoverage, FileReport); if (!Options.SkipExpansions) { - File["expansions"] = renderFileExpansions(Coverage, FileCoverage, FileReport); + File["expansions"] = + renderFileExpansions(Coverage, FileCoverage, FileReport); } } File["summary"] = renderSummary(FileReport); Index: llvm/tools/llvm-cov/CoverageExporterLcov.cpp =================================================================== --- llvm/tools/llvm-cov/CoverageExporterLcov.cpp +++ llvm/tools/llvm-cov/CoverageExporterLcov.cpp @@ -76,26 +76,25 @@ } std::vector -CollectNestedBranches(const coverage::CoverageMapping &Coverage, +collectNestedBranches(const coverage::CoverageMapping &Coverage, ArrayRef Expansions, - int ViewDepth = 0, int SrcLine = 0) -{ + int ViewDepth = 0, int SrcLine = 0) { std::vector Branches; for (const auto &Expansion : Expansions) { auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion); - // If we're at the top level, set the corresponding source line + // If we're at the top level, set the corresponding source line. if (ViewDepth == 0) SrcLine = Expansion.Region.LineStart; - // Recursively collect branches from nested expansions + // Recursively collect branches from nested expansions. auto NestedExpansions = ExpansionCoverage.getExpansions(); - auto NestedExBranches = CollectNestedBranches(Coverage, NestedExpansions, - ViewDepth+1, SrcLine); + auto NestedExBranches = collectNestedBranches(Coverage, NestedExpansions, + ViewDepth + 1, SrcLine); Branches.insert(Branches.end(), NestedExBranches.begin(), - NestedExBranches.end()); + NestedExBranches.end()); - // Add branches from this level of expansion + // Add branches from this level of expansion. auto ExBranches = ExpansionCoverage.getBranches(); for (auto B : ExBranches) if (B.FileID == Expansion.FileID) { @@ -107,33 +106,34 @@ return Branches; } -bool SortLine(llvm::coverage::CountedRegion i, - llvm::coverage::CountedRegion j) { - return (i.LineStart < j.LineStart) || - ((i.LineStart == j.LineStart) && (i.ColumnStart < j.ColumnStart)); +bool sortLine(llvm::coverage::CountedRegion I, + llvm::coverage::CountedRegion J) { + return (I.LineStart < J.LineStart) || + ((I.LineStart == J.LineStart) && (I.ColumnStart < J.ColumnStart)); } void renderBranchExecutionCounts(raw_ostream &OS, const coverage::CoverageMapping &Coverage, const coverage::CoverageData &FileCoverage) { - std::vector Branches = FileCoverage.getBranches(); + std::vector Branches = + FileCoverage.getBranches(); - // Recursively collect branches for all file expansions + // Recursively collect branches for all file expansions. std::vector ExBranches = - CollectNestedBranches(Coverage, FileCoverage.getExpansions()); - - // Append Expansion Branches to Source Branches + collectNestedBranches(Coverage, FileCoverage.getExpansions()); + + // Append Expansion Branches to Source Branches. Branches.insert(Branches.end(), ExBranches.begin(), ExBranches.end()); // Sort branches based on line number to ensure branches corresponding to the // same source line are counted together. - std::sort(Branches.begin(), Branches.end(), SortLine); + std::sort(Branches.begin(), Branches.end(), sortLine); auto NextBranch = Branches.begin(); auto EndBranch = Branches.end(); // Branches with the same source line are enumerated individually - // (BranchIndex) as well as based on True/False pairs (PairIndex) + // (BranchIndex) as well as based on True/False pairs (PairIndex). while (NextBranch != EndBranch) { unsigned CurrentLine = NextBranch->LineStart; unsigned PairIndex = 0; @@ -144,15 +144,16 @@ unsigned BC1 = NextBranch->ExecutionCount; unsigned BC2 = NextBranch->FalseExecutionCount; bool BranchNotExecuted = (BC1 == 0 && BC2 == 0); - - for (int i = 0; i < 2; i++, BranchIndex++) { - OS << "BRDA:" << CurrentLine << ',' << PairIndex << ',' < &Branches) { - for (auto &BR : Branches) { - // Skip folded branches + for (const auto &BR : Branches) { + // Skip folded branches. if (BR.Folded) continue; - // "True" Condition Branches + // "True" Condition Branches. ++NumBranches; if (BR.ExecutionCount > 0) - ++CoveredBranches; - // "False" Condition Branches + ++CoveredBranches; + // "False" Condition Branches. ++NumBranches; if (BR.FalseExecutionCount > 0) - ++CoveredBranches; + ++CoveredBranches; } } -static void SumBranchExpansions(size_t &NumBranches, size_t &CoveredBranches, +static void sumBranchExpansions(size_t &NumBranches, size_t &CoveredBranches, const CoverageMapping &CM, ArrayRef Expansions) { - for (auto &Expansion : Expansions) { + for (const auto &Expansion : Expansions) { auto CE = CM.getCoverageForExpansion(Expansion); - SumBranches(NumBranches, CoveredBranches, CE.getBranches()); - SumBranchExpansions(NumBranches, CoveredBranches, CM, CE.getExpansions()); + sumBranches(NumBranches, CoveredBranches, CE.getBranches()); + sumBranchExpansions(NumBranches, CoveredBranches, CM, CE.getExpansions()); } } @@ -57,7 +57,7 @@ ++CoveredRegions; } - // Compute the line coverage + // Compute the line coverage. size_t NumLines = 0, CoveredLines = 0; CoverageData CD = CM.getCoverageForFunction(Function); for (const auto &LCS : getLineCoverageStats(CD)) { @@ -68,10 +68,10 @@ ++CoveredLines; } - // Compute the branch coverage, including branches from expansions + // Compute the branch coverage, including branches from expansions. size_t NumBranches = 0, CoveredBranches = 0; - SumBranches(NumBranches, CoveredBranches, CD.getBranches()); - SumBranchExpansions(NumBranches, CoveredBranches, CM, CD.getExpansions()); + sumBranches(NumBranches, CoveredBranches, CD.getBranches()); + sumBranchExpansions(NumBranches, CoveredBranches, CM, CD.getExpansions()); return FunctionCoverageSummary( Function.Name, Function.ExecutionCount, Index: llvm/tools/llvm-cov/CoverageViewOptions.h =================================================================== --- llvm/tools/llvm-cov/CoverageViewOptions.h +++ llvm/tools/llvm-cov/CoverageViewOptions.h @@ -23,11 +23,7 @@ Lcov }; - enum class BranchOutputType { - Count, - Percent, - Off - }; + enum class BranchOutputType { Count, Percent, Off }; bool Debug; bool Colors; Index: llvm/tools/llvm-cov/SourceCoverageView.h =================================================================== --- llvm/tools/llvm-cov/SourceCoverageView.h +++ llvm/tools/llvm-cov/SourceCoverageView.h @@ -67,14 +67,13 @@ } }; -/// A view that represents one or more branch regions on a given source line +/// A view that represents one or more branch regions on a given source line. struct BranchView { std::vector Regions; std::unique_ptr View; unsigned Line; - BranchView(unsigned Line, - ArrayRef Regions, + BranchView(unsigned Line, ArrayRef Regions, std::unique_ptr View) : Regions(Regions), View(std::move(View)), Line(Line) {} @@ -230,7 +229,7 @@ virtual void renderInstantiationView(raw_ostream &OS, InstantiationView &ISV, unsigned ViewDepth) = 0; - /// Render an branch view and any nested views. + /// Render a branch view and any nested views. virtual void renderBranchView(raw_ostream &OS, BranchView &BRV, unsigned ViewDepth) = 0; @@ -280,9 +279,8 @@ void addInstantiation(StringRef FunctionName, unsigned Line, std::unique_ptr View); - /// Add an branch subview to this view. - void addBranch(unsigned Line, - ArrayRef Regions, + /// Add a branch subview to this view. + void addBranch(unsigned Line, ArrayRef Regions, std::unique_ptr View); /// Print the code coverage information for a specific portion of a Index: llvm/tools/llvm-cov/SourceCoverageView.cpp =================================================================== --- llvm/tools/llvm-cov/SourceCoverageView.cpp +++ llvm/tools/llvm-cov/SourceCoverageView.cpp @@ -168,10 +168,9 @@ ExpansionSubViews.emplace_back(Region, std::move(View)); } -void SourceCoverageView::addBranch( - unsigned Line, - ArrayRef Regions, - std::unique_ptr View) { +void SourceCoverageView::addBranch(unsigned Line, + ArrayRef Regions, + std::unique_ptr View) { BranchSubViews.emplace_back(Line, Regions, std::move(View)); } Index: llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp =================================================================== --- llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp +++ llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp @@ -656,8 +656,7 @@ OS << EndExpansionDiv; } -void SourceCoverageViewHTML::renderBranchView(raw_ostream &OS, - BranchView &BRV, +void SourceCoverageViewHTML::renderBranchView(raw_ostream &OS, BranchView &BRV, unsigned ViewDepth) { // Render the child subview. if (getOptions().Debug) @@ -665,9 +664,8 @@ OS << BeginExpansionDiv; OS << BeginPre; - for (const auto R : BRV.Regions) - { - // Calculate TruePercent and False Percent + for (const auto &R : BRV.Regions) { + // Calculate TruePercent and False Percent. double TruePercent = 0.0; double FalsePercent = 0.0; unsigned Total = R.ExecutionCount + R.FalseExecutionCount; @@ -677,21 +675,24 @@ FalsePercent = ((double)(R.FalseExecutionCount) / (double)Total) * 100.0; } - // Display Line + Column + // Display Line + Column. std::string LineNoStr = utostr(uint64_t(R.LineStart)); std::string ColNoStr = utostr(uint64_t(R.ColumnStart)); std::string TargetName = "L" + LineNoStr; OS << " Branch ("; - OS << tag("span", a("#" + TargetName, tag("span", - LineNoStr + ":" + ColNoStr), TargetName), "line-number") + "): ["; + OS << tag("span", + a("#" + TargetName, tag("span", LineNoStr + ":" + ColNoStr), + TargetName), + "line-number") + + "): ["; if (R.Folded) { OS << "Folded - Ignored]\n"; continue; } - // Display TrueCount or TruePercent + // Display TrueCount or TruePercent. std::string TrueColor = R.ExecutionCount ? "None" : "red"; std::string TrueCovClass = (R.ExecutionCount > 0) ? "covered-line" : "uncovered-line"; @@ -703,7 +704,7 @@ else OS << format("%0.2f", TruePercent) << "%, "; - // Display FalseCount or FalsePercent + // Display FalseCount or FalsePercent. std::string FalseColor = R.FalseExecutionCount ? "None" : "red"; std::string FalseCovClass = (R.FalseExecutionCount > 0) ? "covered-line" : "uncovered-line"; Index: llvm/tools/llvm-cov/SourceCoverageViewText.cpp =================================================================== --- llvm/tools/llvm-cov/SourceCoverageViewText.cpp +++ llvm/tools/llvm-cov/SourceCoverageViewText.cpp @@ -223,14 +223,13 @@ /*ShowTitle=*/false, ViewDepth + 1); } -void SourceCoverageViewText::renderBranchView(raw_ostream &OS, - BranchView &BRV, +void SourceCoverageViewText::renderBranchView(raw_ostream &OS, BranchView &BRV, unsigned ViewDepth) { // Render the child subview. if (getOptions().Debug) errs() << "Branch at line " << BRV.getLine() << '\n'; - for (const auto R : BRV.Regions) { + for (const auto &R : BRV.Regions) { double TruePercent = 0.0; double FalsePercent = 0.0; unsigned Total = R.ExecutionCount + R.FalseExecutionCount; @@ -249,8 +248,9 @@ } colored_ostream(OS, raw_ostream::RED, - getOptions().Colors && !R.ExecutionCount, - /*Bold=*/false, /*BG=*/true) << "True"; + getOptions().Colors && !R.ExecutionCount, + /*Bold=*/false, /*BG=*/true) + << "True"; if (getOptions().ShowBranchCounts) OS << ": " << formatCount(R.ExecutionCount) << ", "; @@ -258,8 +258,9 @@ OS << ": " << format("%0.2f", TruePercent) << "%, "; colored_ostream(OS, raw_ostream::RED, - getOptions().Colors && !R.FalseExecutionCount, - /*Bold=*/false, /*BG=*/true) << "False"; + getOptions().Colors && !R.FalseExecutionCount, + /*Bold=*/false, /*BG=*/true) + << "False"; if (getOptions().ShowBranchCounts) OS << ": " << formatCount(R.FalseExecutionCount);