Index: llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h =================================================================== --- llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h +++ llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h @@ -15,6 +15,7 @@ #define LLVM_PROFILEDATA_COVERAGE_COVERAGEMAPPING_H #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/BitVector.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/Hashing.h" @@ -33,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -226,7 +228,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. - BranchRegion + BranchRegion, + + /// A DecisionRegion represents a top-level boolean expression and is + /// associated with a variable length bitmap index and condition number. + MCDCDecisionRegion, + + /// A Branch Region can be extended to include IDs to facilitate MC/DC. + MCDCBranchRegion }; /// Primary Counter that is also used for Branch Regions (TrueCount). @@ -235,6 +244,16 @@ /// Secondary Counter used for Branch Regions (FalseCount). Counter FalseCount; + /// Byte Index of Bitmap Coverage Object for a Decision Region (MC/DC only). + unsigned BitmapIdx; + + /// Number of Conditions used for a Decision Region (MC/DC only). + unsigned NumConditions; + + /// IDs used to represent a branch region and other branch regions evaluated + /// based on True and False branches (MC/DC only). + unsigned ID, TrueID, FalseID; + unsigned FileID, ExpandedFileID; unsigned LineStart, ColumnStart, LineEnd, ColumnEnd; RegionKind Kind; @@ -246,11 +265,23 @@ LineStart(LineStart), ColumnStart(ColumnStart), LineEnd(LineEnd), ColumnEnd(ColumnEnd), Kind(Kind) {} - CounterMappingRegion(Counter Count, Counter FalseCount, unsigned FileID, + CounterMappingRegion(Counter Count, Counter FalseCount, unsigned BitmapIdx, + unsigned NumConditions, unsigned ID, unsigned TrueID, + unsigned FalseID, unsigned FileID, unsigned ExpandedFileID, unsigned LineStart, unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd, RegionKind Kind) - : Count(Count), FalseCount(FalseCount), FileID(FileID), + : Count(Count), FalseCount(FalseCount), BitmapIdx(BitmapIdx), + NumConditions(NumConditions), ID(ID), TrueID(TrueID), FalseID(FalseID), + FileID(FileID), ExpandedFileID(ExpandedFileID), LineStart(LineStart), + ColumnStart(ColumnStart), LineEnd(LineEnd), ColumnEnd(ColumnEnd), + Kind(Kind) {} + + CounterMappingRegion(unsigned BitmapIdx, unsigned NumConditions, + unsigned FileID, unsigned ExpandedFileID, + unsigned LineStart, unsigned ColumnStart, + unsigned LineEnd, unsigned ColumnEnd, RegionKind Kind) + : BitmapIdx(BitmapIdx), NumConditions(NumConditions), FileID(FileID), ExpandedFileID(ExpandedFileID), LineStart(LineStart), ColumnStart(ColumnStart), LineEnd(LineEnd), ColumnEnd(ColumnEnd), Kind(Kind) {} @@ -288,8 +319,29 @@ makeBranchRegion(Counter Count, Counter FalseCount, unsigned FileID, unsigned LineStart, unsigned ColumnStart, unsigned LineEnd, unsigned ColumnEnd) { - return CounterMappingRegion(Count, FalseCount, FileID, 0, LineStart, - ColumnStart, LineEnd, ColumnEnd, BranchRegion); + return CounterMappingRegion(Count, FalseCount, 0, 0, 0, 0, 0, + FileID, 0, LineStart, ColumnStart, LineEnd, + ColumnEnd, BranchRegion); + } + + static CounterMappingRegion + makeBranchRegion(Counter Count, Counter FalseCount, unsigned ID, + unsigned TrueID, unsigned FalseID, unsigned FileID, + unsigned LineStart, unsigned ColumnStart, unsigned LineEnd, + unsigned ColumnEnd) { + return CounterMappingRegion(Count, FalseCount, 0, 0, ID, TrueID, FalseID, + FileID, 0, LineStart, ColumnStart, LineEnd, + ColumnEnd, + ID == 0 ? BranchRegion : MCDCBranchRegion); + } + + static CounterMappingRegion + makeDecisionRegion(unsigned BitmapIdx, unsigned NumConditions, + unsigned FileID, unsigned LineStart, unsigned ColumnStart, + unsigned LineEnd, unsigned ColumnEnd) { + return CounterMappingRegion(BitmapIdx, NumConditions, FileID, 0, LineStart, + ColumnStart, LineEnd, ColumnEnd, + MCDCDecisionRegion); } inline LineColPair startLoc() const { @@ -315,11 +367,163 @@ FalseExecutionCount(FalseExecutionCount), Folded(false) {} }; +/// MCDC Record grouping all information together. +struct MCDCRecord { + typedef enum { MCDC_DontCare = -1, MCDC_False = 0, MCDC_True = 1 } CondState; + + using TestVector = std::vector; + using TestVectors = std::vector; + using BoolVector = std::vector; + using TVRowPair = std::pair; + using TVPairMap = llvm::DenseMap; + using CondIDMap = llvm::DenseMap; + using LineColPairMap = llvm::DenseMap; + +private: + CounterMappingRegion Region; + TestVectors TV; + TVPairMap IndepPairs; + BoolVector Folded; + CondIDMap PosToID; + LineColPairMap CondLoc; + +public: + MCDCRecord(CounterMappingRegion Region, TestVectors TV, TVPairMap IndepPairs, + BoolVector Folded, CondIDMap PosToID, LineColPairMap CondLoc) + : Region(Region), TV(TV), IndepPairs(IndepPairs), Folded(Folded), + PosToID(PosToID), CondLoc(CondLoc){}; + + CounterMappingRegion getDecisionRegion() const { return Region; } + unsigned getNumConditions() const { return Region.NumConditions; } + unsigned getNumTestVectors() const { return TV.size(); } + bool isCondFolded(unsigned Condition) const { return Folded[Condition]; } + + CondState getTVCondition(unsigned TestVector, unsigned Condition) { + // Accessing conditions in the TestVectors requires a translation from a + // ordinal position to actual condition ID. This is done via PosToID[]. + return TV[TestVector][PosToID[Condition]]; + } + + CondState getTVResult(unsigned TestVector) { + // The last value for a Test Vector, after its constituent conditions, is + // always the Result. See MCDCRecordProcessor::RecordTestVector(). + return TV[TestVector][getNumConditions()]; + } + + bool isCondIndepPairCovered(unsigned Condition) const { + // Accessing conditions in the TestVector Row Pairs requires a translation + // from a ordinal position to actual condition ID. This is done via + // PosToID[]. + auto it = PosToID.find(Condition); + if (it != PosToID.end()) + return (IndepPairs.find(it->second) != IndepPairs.end()); + llvm_unreachable("Condition ID without an Ordinal mapping"); + } + + TVRowPair getCondIndepPair(unsigned Condition) { + // Accessing conditions in the TestVector Row Pairs requires a translation + // from a ordinal position to actual condition ID. This is done via + // PosToID[]. + assert(isCondIndepPairCovered(Condition)); + return IndepPairs[PosToID[Condition]]; + } + + float getPercentCovered() const { + unsigned folded = 0; + unsigned covered = 0; + for (unsigned c = 0; c < getNumConditions(); c++) { + if (isCondFolded(c)) + folded++; + else if (isCondIndepPairCovered(c)) + covered++; + } + + return ((double)(covered) / (double)(getNumConditions() - folded)) * 100.0; + } + + std::string getConditionHdrStr(unsigned Condition) { + std::ostringstream ss; + ss << "Condition C" << Condition + 1 << " --> ("; + ss << CondLoc[Condition].first << ":" << CondLoc[Condition].second; + ss << ")\n"; + return ss.str(); + } + + std::string getTestVectorHdrStr() { + std::ostringstream ss; + if (getNumTestVectors() == 0) { + ss << "None.\n"; + return ss.str(); + } + for (unsigned i = 0; i < getNumConditions(); i++) { + ss << "C" << i + 1; + if (i != getNumConditions() - 1) + ss << ", "; + } + ss << " Result\n"; + return ss.str(); + } + + std::string getTestVectorStr(unsigned TestVector) { + assert(TestVector < getNumTestVectors()); + std::ostringstream ss; + // Print Individual Conditions + ss << " " << TestVector + 1 << " { "; + for (unsigned Condition = 0; Condition < getNumConditions(); Condition++) { + if (isCondFolded(Condition)) + ss << "C"; + else { + switch (getTVCondition(TestVector, Condition)) { + case MCDCRecord::MCDC_DontCare: + ss << "-"; + break; + case MCDCRecord::MCDC_True: + ss << "T"; + break; + case MCDCRecord::MCDC_False: + ss << "F"; + break; + } + } + if (Condition != getNumConditions() - 1) + ss << ", "; + } + + // Print Result + ss << " = "; + if (getTVResult(TestVector) == MCDC_True) + ss << "T"; + else + ss << "F"; + ss << " }\n"; + + return ss.str(); + } + + std::string getCondCoverageStr(unsigned Condition) { + assert(Condition < getNumConditions()); + std::ostringstream ss; + + ss << " C" << Condition + 1 << "-Pair: "; + if (isCondFolded(Condition)) { + ss << "constant folded\n"; + } else if (isCondIndepPairCovered(Condition)) { + TVRowPair rows = getCondIndepPair(Condition); + ss << "covered: (" << rows.first << ","; + ss << rows.second << ")\n"; + } else + ss << "not covered\n"; + + return ss.str(); + } +}; + /// A Counter mapping context is used to connect the counters, expressions /// and the obtained counter values. class CounterMappingContext { ArrayRef Expressions; ArrayRef CounterValues; + ArrayRef BitmapBytes; public: CounterMappingContext(ArrayRef Expressions, @@ -327,6 +531,7 @@ : Expressions(Expressions), CounterValues(CounterValues) {} void setCounts(ArrayRef Counts) { CounterValues = Counts; } + void setBitmapBytes(ArrayRef Bytes) { BitmapBytes = Bytes; } void dump(const Counter &C, raw_ostream &OS) const; void dump(const Counter &C) const { dump(C, dbgs()); } @@ -335,6 +540,15 @@ /// counter was executed. Expected evaluate(const Counter &C) const; + /// Return the number of times that a region of code associated with this + /// counter was executed. + Expected evaluateBitmap(unsigned Bitmap, unsigned NumCond) const; + + /// Return an MCDC record that indicates executed test vectors and condition + /// pairs. + Expected evaluateMCDCRegion(CounterMappingRegion Region, + BitVector Bitmap, ArrayRef Branches); + unsigned getMaxCounterID(const Counter &C) const; }; @@ -353,6 +567,8 @@ std::vector CountedRegions; /// Branch Regions in the function along with their counts. std::vector CountedBranchRegions; + /// MCDC Records record a DecisionRegion and associated BranchRegions. + std::vector MCDCRecords; /// The number of times this function was executed. uint64_t ExecutionCount = 0; @@ -362,9 +578,12 @@ FunctionRecord(FunctionRecord &&FR) = default; FunctionRecord &operator=(FunctionRecord &&) = default; + void pushMCDCRecord(MCDCRecord Record) { MCDCRecords.push_back(Record); } + void pushRegion(CounterMappingRegion Region, uint64_t Count, uint64_t FalseCount) { - if (Region.Kind == CounterMappingRegion::BranchRegion) { + if (Region.Kind == CounterMappingRegion::BranchRegion || + Region.Kind == CounterMappingRegion::MCDCBranchRegion) { CountedBranchRegions.emplace_back(Region, Count, FalseCount); // If both counters are hard-coded to zero, then this region represents a // constant-folded branch. @@ -535,6 +754,7 @@ std::vector Segments; std::vector Expansions; std::vector BranchRegions; + std::vector MCDCRecords; public: CoverageData() = default; @@ -561,6 +781,9 @@ /// Branches that can be further processed. ArrayRef getBranches() const { return BranchRegions; } + + /// MCDC Records that can be further processed. + ArrayRef getMCDCRecords() const { return MCDCRecords; } }; /// The mapping of profile information to coverage data. Index: llvm/lib/ProfileData/Coverage/CoverageMapping.cpp =================================================================== --- llvm/lib/ProfileData/Coverage/CoverageMapping.cpp +++ llvm/lib/ProfileData/Coverage/CoverageMapping.cpp @@ -38,6 +38,7 @@ #include #include #include +#include using namespace llvm; using namespace coverage; @@ -188,6 +189,196 @@ llvm_unreachable("Unhandled CounterKind"); } +Expected CounterMappingContext::evaluateBitmap(unsigned ID, + unsigned NC) const { + unsigned SizeInBits = llvm::alignTo(1L << NC, CHAR_BIT); + unsigned SizeInBytes = SizeInBits / CHAR_BIT; + + ArrayRef Bytes(&BitmapBytes[ID], SizeInBytes); + + // Mask each bitmap byte into the BitVector. Go in reverse so that the + // bitvector can just be shifted over by one byte on each iteration. + BitVector Result(SizeInBits, false); + for (auto byte = std::rbegin(Bytes); byte != std::rend(Bytes); ++byte) { + uint32_t data = *byte; + Result <<= CHAR_BIT; + Result.setBitsInMask(&data, 1); + } + return Result; +} + +class MCDCRecordProcessor { + BitVector &Bitmap; + CounterMappingRegion &Region; + ArrayRef Branches; + unsigned NumConditions; + llvm::DenseMap Map; + + MCDCRecord::BoolVector Folded; + MCDCRecord::TVPairMap IndepPairs; + MCDCRecord::TestVectors TestVectors; + MCDCRecord::TestVectors ExecVectors; + +public: + MCDCRecordProcessor(BitVector &Bitmap, CounterMappingRegion &Region, + ArrayRef Branches) + : Bitmap(Bitmap), Region(Region), Branches(Branches), + NumConditions(Region.NumConditions), Folded(NumConditions, false), + IndepPairs(NumConditions), TestVectors(pow(2, NumConditions)) {} + +private: + void recordTestVector(MCDCRecord::TestVector &TV, + MCDCRecord::CondState Result) { + // Calculate an index that is used to identify the test vector in a vector + // of test vectors. This index also corresponds to the index values of an + // MCDC Region's bitmap (see findExecutedTestVectors()). + unsigned idx = 0; + for (auto Cond = std::rbegin(TV); Cond != std::rend(TV); ++Cond) { + idx <<= 1; + idx |= (*Cond == MCDCRecord::MCDC_True) ? 0x1 : 0x0; + } + + // Copy the completed test vector to the vector of testvectors. + TestVectors[idx] = TV; + + // The final value (T,F) is equal to the last non-dontcare state on the + // path (in a short-circuiting system). + TestVectors[idx].push_back(Result); + } + + void buildTestVector(MCDCRecord::TestVector &TV, unsigned ID = 1) { + // Branch regions are hashed based on an ID. + const CounterMappingRegion *Branch = Map[ID]; + + // An ID of '0' indicates the end of a test vector, at which point, the + // test vector can be copied off, and the algorithm can keep going. + + // Follow "True" Path in Test Vector. + TV[ID - 1] = MCDCRecord::MCDC_True; + if (Branch->TrueID > 0) + buildTestVector(TV, Branch->TrueID); + else + recordTestVector(TV, MCDCRecord::MCDC_True); + + // Follow "False" Path in Test Vector. + TV[ID - 1] = MCDCRecord::MCDC_False; + if (Branch->FalseID > 0) + buildTestVector(TV, Branch->FalseID); + else + recordTestVector(TV, MCDCRecord::MCDC_False); + + // Reset back to DontCare + TV[ID - 1] = MCDCRecord::MCDC_DontCare; + } + + void findExecutedTestVectors(BitVector &Bitmap) { + // Walk the bits in the bitmap. A bit set to '1' indicates that the test + // vector at the corresponding index was executed during a test run. + for (unsigned i = 0; i < Bitmap.size(); i++) + if (Bitmap[i] == 1) { + assert(!TestVectors[i].empty() && "Test Vector doesn't exist."); + ExecVectors.push_back(TestVectors[i]); + } + } + + bool matchTestVectors(unsigned Aidx, unsigned Bidx, unsigned C) { + const MCDCRecord::TestVector &A = ExecVectors[Aidx]; + const MCDCRecord::TestVector &B = ExecVectors[Bidx]; + + // If condition values in both A and B aren't opposites, no match. + if (!((A[C] ^ B[C]) == 1)) + return false; + + // If the results of both A and B aren't opposites, no match. + if (!((A[NumConditions] ^ B[NumConditions]) == 1)) + return false; + + // If just one of the other conditions don't match, no match. + for (unsigned i = 0; i < NumConditions; i++) { + if (i == C) + continue; + if (A[i] != MCDCRecord::MCDC_DontCare && + B[i] != MCDCRecord::MCDC_DontCare && A[i] != B[i]) + return false; + } + + // Otherwise, match. + return true; + } + + void findIndependencePairs() { + unsigned NumTVs = ExecVectors.size(); + + // For each condition. + for (unsigned c = 0; c < NumConditions; c++) { + bool pair_found = false; + + // For each executed test vector. + for (unsigned i = 0; !pair_found && i < NumTVs; i++) { + + // Compared to every other executed test vector. + for (unsigned j = 0; !pair_found && j < NumTVs; j++) { + if (i == j) + continue; + + // If a matching pair of vectors is found, record them. + if ((pair_found = matchTestVectors(i, j, c))) + IndepPairs[c] = std::make_pair(i + 1, j + 1); + } + } + } + } + +public: + MCDCRecord processMCDCRecord() { + unsigned i = 0; + MCDCRecord::CondIDMap PosToID; + MCDCRecord::LineColPairMap CondLoc; + + // Walk the Record's BranchRegions (representing Conditions) in order to: + // - Hash the condition based on its corresponding ID. This will be used to + // calculate the test vectors. + // - Keep a map of the condition's ordinal position (1, 2, 3, 4) to its + // actual ID. This will be used to visualize the conditions in the + // correct order. + // - Keep track of the condition source location. This will be used to + // visualize where the condition is. + // - Record whether the condition is constant folded so that we exclude it + // from being measured. + for (const auto &B : Branches) { + Map[B.ID] = &B; + PosToID[i] = B.ID - 1; + CondLoc[i] = B.startLoc(); + Folded[i++] = (B.Count.isZero() && B.FalseCount.isZero()); + } + + // Initialize a base test vector as 'DontCare'. + MCDCRecord::TestVector TV(NumConditions, MCDCRecord::MCDC_DontCare); + + // Use the base test vector to build the list of all possible test vectors. + buildTestVector(TV); + + // Using Profile Bitmap from runtime, mark the executed test vectors. + findExecutedTestVectors(Bitmap); + + // Compare executed test vectors against each other to find an independence + // pairs for each condition. This processing takes the most time. + findIndependencePairs(); + + // Record Test vectors, executed vectors, and independence pairs. + MCDCRecord Res(Region, ExecVectors, IndepPairs, Folded, PosToID, CondLoc); + return Res; + } +}; + +Expected CounterMappingContext::evaluateMCDCRegion( + CounterMappingRegion Region, BitVector Bitmap, + ArrayRef Branches) { + + MCDCRecordProcessor MCDCProcessor(Bitmap, Region, Branches); + return MCDCProcessor.processMCDCRecord(); +} + unsigned CounterMappingContext::getMaxCounterID(const Counter &C) const { switch (C.getKind()) { case Counter::Zero: @@ -230,6 +421,24 @@ return MaxCounterID; } +static unsigned getMaxBitmapSize(const CounterMappingContext &Ctx, + const CoverageMappingRecord &Record) { + unsigned MaxBitmapID = 0; + unsigned NumConditions = 0; + // The last DecisionRegion has the highest bitmap byte index used in the + // function, which when combined with its number of conditions, yields the + // full bitmap size. + for (const auto &Region : reverse(Record.MappingRegions)) { + if (Region.Kind == CounterMappingRegion::MCDCDecisionRegion) { + MaxBitmapID = Region.BitmapIdx; + NumConditions = Region.NumConditions; + break; + } + } + unsigned SizeInBits = llvm::alignTo(1L << NumConditions, CHAR_BIT); + return MaxBitmapID + (SizeInBits / CHAR_BIT); +} + Error CoverageMapping::loadFunctionRecord( const CoverageMappingRecord &Record, IndexedInstrProfReader &ProfileReader) { @@ -258,6 +467,20 @@ } Ctx.setCounts(Counts); + std::vector BitmapBytes; + if (Error E = ProfileReader.getFunctionBitmapBytes( + Record.FunctionName, Record.FunctionHash, BitmapBytes)) { + instrprof_error IPE = InstrProfError::take(std::move(E)); + if (IPE == instrprof_error::hash_mismatch) { + FuncHashMismatches.emplace_back(std::string(Record.FunctionName), + Record.FunctionHash); + return Error::success(); + } else if (IPE != instrprof_error::unknown_function) + return make_error(IPE); + BitmapBytes.assign(getMaxBitmapSize(Ctx, Record) + 1, 0); + } + Ctx.setBitmapBytes(BitmapBytes); + assert(!Record.MappingRegions.empty() && "Function has no regions"); // This coverage record is a zero region for a function that's unused in @@ -269,8 +492,20 @@ Record.MappingRegions[0].Count.isZero() && Counts[0] > 0) return Error::success(); + unsigned NumConds = 0; + const CounterMappingRegion *MCDCDecision; + std::vector MCDCBranches; + FunctionRecord Function(OrigFuncName, Record.Filenames); for (const auto &Region : Record.MappingRegions) { + // If an MCDC DecisionRegion is seen, track the BranchRegions that follow + // it according to Region.NumConditions. + if (Region.Kind == CounterMappingRegion::MCDCDecisionRegion) { + assert(NumConds == 0); + MCDCDecision = &Region; + NumConds = Region.NumConditions; + continue; + } Expected ExecutionCount = Ctx.evaluate(Region.Count); if (auto E = ExecutionCount.takeError()) { consumeError(std::move(E)); @@ -282,6 +517,40 @@ return Error::success(); } Function.pushRegion(Region, *ExecutionCount, *AltExecutionCount); + + // If a MCDCDecisionRegion was seen as well as the BranchRegions that + // correspond to it stored in a vector, according to the number of + // conditions recorded for the region. + if (NumConds > 0 && Region.Kind == CounterMappingRegion::MCDCBranchRegion) { + MCDCBranches.push_back(Region); + if (--NumConds == 0) { + // Evaluating the test vector bitmap for the decision region entails + // calculating precisely what bits are pertinent to this region alone. + // This is calculated based on the recorded offset into the global + // profile bitmap; the length is calculated based on the recorded + // number of conditions. + Expected Bitmap = Ctx.evaluateBitmap( + MCDCDecision->BitmapIdx, MCDCDecision->NumConditions); + if (auto E = Bitmap.takeError()) { + consumeError(std::move(E)); + return Error::success(); + } + + // Since the bitmap identifies the executed test vectors for an MC/DC + // DecisionRegion, all of the information is now available to process. + // This is where the bulk of the MC/DC progressing takes place. + Expected Record = + Ctx.evaluateMCDCRegion(*MCDCDecision, *Bitmap, MCDCBranches); + if (auto E = Record.takeError()) { + consumeError(std::move(E)); + return Error::success(); + } + + // Save the MC/DC Record so that it can be visualized later. + Function.pushMCDCRecord(*Record); + MCDCBranches.clear(); + } + } } // Don't create records for (filenames, function) pairs we've already seen. @@ -724,6 +993,10 @@ for (const auto &CR : Function.CountedBranchRegions) if (FileIDs.test(CR.FileID) && (CR.FileID == CR.ExpandedFileID)) FileCoverage.BranchRegions.push_back(CR); + // Capture MCDC records specific to the function. + for (const auto &MR : Function.MCDCRecords) + if (FileIDs.test(MR.getDecisionRegion().FileID)) + FileCoverage.MCDCRecords.push_back(MR); } LLVM_DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n"); @@ -776,6 +1049,11 @@ if (CR.FileID == *MainFileID) FunctionCoverage.BranchRegions.push_back(CR); + // Capture MCDC records specific to the function. + for (const auto &MR : Function.MCDCRecords) + if (MR.getDecisionRegion().FileID == *MainFileID) + FunctionCoverage.MCDCRecords.push_back(MR); + LLVM_DEBUG(dbgs() << "Emitting segments for function: " << Function.Name << "\n"); FunctionCoverage.Segments = SegmentBuilder::buildSegments(Regions); Index: llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp =================================================================== --- llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp +++ llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp @@ -237,6 +237,7 @@ unsigned LineStart = 0; for (size_t I = 0; I < NumRegions; ++I) { Counter C, C2; + uint64_t BitmapIdx = 0, Conditions = 0, ID = 0, TID = 0, FID = 0; CounterMappingRegion::RegionKind Kind = CounterMappingRegion::CodeRegion; // Read the combined counter + region kind. @@ -286,6 +287,29 @@ if (auto Err = readCounter(C2)) return Err; break; + case CounterMappingRegion::MCDCBranchRegion: + // For a MCDC Branch Region, read two successive counters and 3 IDs. + Kind = CounterMappingRegion::MCDCBranchRegion; + if (auto Err = readCounter(C)) + return Err; + if (auto Err = readCounter(C2)) + return Err; + if (auto Err = readIntMax(ID, std::numeric_limits::max())) + return Err; + if (auto Err = readIntMax(TID, std::numeric_limits::max())) + return Err; + if (auto Err = readIntMax(FID, std::numeric_limits::max())) + return Err; + break; + case CounterMappingRegion::MCDCDecisionRegion: + Kind = CounterMappingRegion::MCDCDecisionRegion; + if (auto Err = + readIntMax(BitmapIdx, std::numeric_limits::max())) + return Err; + if (auto Err = + readIntMax(Conditions, std::numeric_limits::max())) + return Err; + break; default: return make_error(coveragemap_error::malformed); } @@ -337,9 +361,10 @@ dbgs() << "\n"; }); - auto CMR = CounterMappingRegion(C, C2, InferredFileID, ExpandedFileID, - LineStart, ColumnStart, - LineStart + NumLines, ColumnEnd, Kind); + auto CMR = CounterMappingRegion( + C, C2, BitmapIdx, Conditions, ID, TID, FID, InferredFileID, + ExpandedFileID, LineStart, ColumnStart, LineStart + NumLines, ColumnEnd, + Kind); if (CMR.startLoc() > CMR.endLoc()) return make_error(coveragemap_error::malformed); MappingRegions.push_back(CMR); Index: llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp =================================================================== --- llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp +++ llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp @@ -236,6 +236,23 @@ writeCounter(MinExpressions, Count, OS); writeCounter(MinExpressions, FalseCount, OS); break; + case CounterMappingRegion::MCDCBranchRegion: + encodeULEB128(unsigned(I->Kind) + << Counter::EncodingCounterTagAndExpansionRegionTagBits, + OS); + writeCounter(MinExpressions, Count, OS); + writeCounter(MinExpressions, FalseCount, OS); + encodeULEB128(unsigned(I->ID), OS); + encodeULEB128(unsigned(I->TrueID), OS); + encodeULEB128(unsigned(I->FalseID), OS); + break; + case CounterMappingRegion::MCDCDecisionRegion: + encodeULEB128(unsigned(I->Kind) + << Counter::EncodingCounterTagAndExpansionRegionTagBits, + OS); + encodeULEB128(unsigned(I->BitmapIdx), OS); + encodeULEB128(unsigned(I->NumConditions), OS); + break; } assert(I->LineStart >= PrevLineStart); encodeULEB128(I->LineStart - PrevLineStart, OS); Index: llvm/test/tools/llvm-cov/Inputs/binary-formats.canonical.json =================================================================== --- llvm/test/tools/llvm-cov/Inputs/binary-formats.canonical.json +++ llvm/test/tools/llvm-cov/Inputs/binary-formats.canonical.json @@ -4,6 +4,7 @@ CHECK-SAME: {"branches":[], CHECK-SAME: "expansions":[], CHECK-SAME: "filename":"/tmp/binary-formats.c", +CHECK-SAME: "mcdc_records":[], CHECK-SAME: "segments": CHECK-SAME: 4,40,100,true,true,false CHECK-SAME: 4,42,0,false,false,false @@ -11,11 +12,13 @@ CHECK-SAME: "functions":{"count":1,"covered":1,"percent":100}, CHECK-SAME: "instantiations":{"count":1,"covered":1,"percent":100}, CHECK-SAME: "lines":{"count":1,"covered":1,"percent":100}, +CHECK-SAME: "mcdc":{"count":0,"covered":0,"notcovered":0,"percent":0}, CHECK-SAME: "regions":{"count":1,"covered":1,"notcovered":0,"percent":100}}} CHECK-SAME: ], CHECK-SAME: "functions":[ CHECK-SAME: {"branches":[], -CHECK-SAME: "count":100,"filenames":["/tmp/binary-formats.c"],"name":"main", +CHECK-SAME: "count":100,"filenames":["/tmp/binary-formats.c"], +CHECK-SAME: "mcdc_records":[],"name":"main", CHECK-SAME: "regions": CHECK-SAME: 4,40,4,42,100,0,0,0 CHECK-SAME: } @@ -25,6 +28,7 @@ CHECK-SAME: "functions":{"count":1,"covered":1,"percent":100}, CHECK-SAME: "instantiations":{"count":1,"covered":1,"percent":100}, CHECK-SAME: "lines":{"count":1,"covered":1,"percent":100}, +CHECk-SAME: "mcdc":{"count":0,"covered":0,"notcovered":0,"percent":0}, CHECK-SAME: "regions":{"count":1,"covered":1,"notcovered":0,"percent":100}}} CHECK-SAME: ], CHECK-SAME: "type":"llvm.coverage.json.export" Index: llvm/test/tools/llvm-cov/Inputs/mcdc-const-folding.cpp =================================================================== --- /dev/null +++ llvm/test/tools/llvm-cov/Inputs/mcdc-const-folding.cpp @@ -0,0 +1,117 @@ +#include + +bool case0(bool a) { + return 0 && a; +} +bool case1(bool a) { + return a && 0; +} +bool case2(bool a) { + return 1 && a; +} +bool case3(bool a) { + return a && 1; +} +bool case4(bool a) { + return 1 || a; +} +bool case5(bool a) { + return a || 1; +} +bool case6(bool a) { + return 0 || a; +} +bool case7(bool a) { + return a || 0; +} + +bool case8(bool a, bool b) { + return 0 && a && b; +} +bool case9(bool a, bool b) { + return a && 0 && b; +} +bool casea(bool a, bool b) { + return 1 && a && b; +} +bool caseb(bool a, bool b) { + return a && 1 && b; +} +bool casec(bool a, bool b) { + return 1 || a || b; +} +bool cased(bool a, bool b) { + return a || 1 || b; +} +bool casee(bool a, bool b) { + return 0 || a || b; +} +bool casef(bool a, bool b) { + return a || 0 || b; +} + +bool caseg(bool a, bool b) { + return b && a && 0; +} +bool caseh(bool a, bool b) { + return b && 0 && a; +} +bool casei(bool a, bool b) { + return b && a && 1; +} +bool casej(bool a, bool b) { + return b && 1 && a; +} +bool casek(bool a, bool b) { + return b || a || 1; +} +bool casel(bool a, bool b) { + return b || 1 || a; +} +bool casem(bool a, bool b) { + return b || a || 0; +} +bool casen(bool a, bool b) { + return b || 0 || a; +} + +extern "C" { + extern void __llvm_profile_write_file(void); +} + +int main(int argc, char *argv[]) +{ + bool a = atoi(argv[1]); + bool b = atoi(argv[2]); + volatile bool c; + + c = case0(a); + c = case1(a); + c = case2(a); + c = case3(a); + c = case4(a); + c = case5(a); + c = case6(a); + c = case7(a); + + c = case8(a, b); + c = case9(a, b); + c = casea(a, b); + c = caseb(a, b); + c = casec(a, b); + c = cased(a, b); + c = casee(a, b); + c = casef(a, b); + + c = caseg(a, b); + c = caseh(a, b); + c = casei(a, b); + c = casej(a, b); + c = casek(a, b); + c = casel(a, b); + c = casem(a, b); + c = casen(a, b); + + __llvm_profile_write_file(); + return 0; +} Index: llvm/test/tools/llvm-cov/Inputs/mcdc-const-folding.proftext =================================================================== --- /dev/null +++ llvm/test/tools/llvm-cov/Inputs/mcdc-const-folding.proftext @@ -0,0 +1,400 @@ +_Z5case8bb +# Func Hash: +99214 +# Num Counters: +5 +# Counter Values: +4 +0 +0 +0 +0 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +1 + + +_Z5case5b +# Func Hash: +1551 +# Num Counters: +3 +# Counter Values: +4 +1 +0 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +6 + + +_Z5caseabb +# Func Hash: +99214 +# Num Counters: +5 +# Counter Values: +4 +3 +2 +4 +3 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +a2 + + +_Z5case6b +# Func Hash: +1551 +# Num Counters: +3 +# Counter Values: +4 +4 +1 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +5 + + +_Z5casegbb +# Func Hash: +99214 +# Num Counters: +5 +# Counter Values: +4 +2 +0 +3 +2 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +23 + + +_Z5case1b +# Func Hash: +1550 +# Num Counters: +3 +# Counter Values: +4 +3 +0 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +3 + + +_Z5case7b +# Func Hash: +1551 +# Num Counters: +3 +# Counter Values: +4 +1 +1 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +3 + + +_Z5casedbb +# Func Hash: +99279 +# Num Counters: +5 +# Counter Values: +4 +0 +0 +1 +0 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +12 + + +_Z5casekbb +# Func Hash: +99279 +# Num Counters: +5 +# Counter Values: +4 +0 +0 +1 +0 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +12 + + +_Z5casehbb +# Func Hash: +99214 +# Num Counters: +5 +# Counter Values: +4 +0 +0 +3 +0 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +3 + + +_Z5case4b +# Func Hash: +1551 +# Num Counters: +3 +# Counter Values: +4 +0 +0 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +2 + + +_Z5caseibb +# Func Hash: +99214 +# Num Counters: +5 +# Counter Values: +4 +2 +2 +3 +2 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +83 + + +_Z5case2b +# Func Hash: +1550 +# Num Counters: +3 +# Counter Values: +4 +4 +3 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +a + + +_Z5casefbb +# Func Hash: +99279 +# Num Counters: +5 +# Counter Values: +4 +1 +0 +1 +1 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +6 + + +_Z5caselbb +# Func Hash: +99279 +# Num Counters: +5 +# Counter Values: +4 +0 +0 +1 +0 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +12 + + +_Z5casenbb +# Func Hash: +99279 +# Num Counters: +5 +# Counter Values: +4 +1 +0 +1 +1 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +6 + + +_Z5case9bb +# Func Hash: +99214 +# Num Counters: +5 +# Counter Values: +4 +0 +0 +3 +0 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +3 + + +_Z5casecbb +# Func Hash: +99279 +# Num Counters: +5 +# Counter Values: +4 +0 +0 +0 +0 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +2 + + +_Z5casebbb +# Func Hash: +99214 +# Num Counters: +5 +# Counter Values: +4 +3 +2 +3 +3 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +a1 + + +_Z5case0b +# Func Hash: +1550 +# Num Counters: +3 +# Counter Values: +4 +0 +0 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +1 + + +_Z5casejbb +# Func Hash: +99214 +# Num Counters: +5 +# Counter Values: +4 +3 +2 +3 +3 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +a1 + + +_Z5caseebb +# Func Hash: +99279 +# Num Counters: +5 +# Counter Values: +4 +1 +0 +4 +1 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +14 + + +main +# Func Hash: +24 +# Num Counters: +1 +# Counter Values: +4 + +_Z5case3b +# Func Hash: +1550 +# Num Counters: +3 +# Counter Values: +4 +3 +3 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +9 + + +_Z5casembb +# Func Hash: +99279 +# Num Counters: +5 +# Counter Values: +4 +0 +0 +1 +0 +# Num Bitmask Bytes: +$1 +# Bitmask Byte Values: +12 + + Index: llvm/test/tools/llvm-cov/Inputs/mcdc-const.cpp =================================================================== --- /dev/null +++ llvm/test/tools/llvm-cov/Inputs/mcdc-const.cpp @@ -0,0 +1,27 @@ + +#include + +extern "C" { +extern void __llvm_profile_write_file(void); +} + +extern int foo(); + +void test(bool a, bool b, bool c, bool d) { + + if ((a && 1) || (0 && d) || 0) + printf("test1 decision true\n"); +} + +int main() +{ + test(true,false,true,false); + test(true,false,true,true); + test(true,true,false,false); + test(false,true,true,false); + + test(true,false,false,false); + + __llvm_profile_write_file(); + return 0; +} Index: llvm/test/tools/llvm-cov/Inputs/mcdc-const.proftext =================================================================== --- /dev/null +++ llvm/test/tools/llvm-cov/Inputs/mcdc-const.proftext @@ -0,0 +1,32 @@ +main +# Func Hash: +24 +# Num Counters: +1 +# Counter Values: +1 + +_Z4testbbbb +# Func Hash: +703556281489 +# Num Counters: +9 +# Counter Values: +5 +4 +1 +1 +1 +4 +4 +0 +0 +# Num Bitmask Bytes: +$4 +# Bitmask Byte Values: +1 +2 +0 +0 + + Index: llvm/test/tools/llvm-cov/Inputs/mcdc-general-none.proftext =================================================================== --- /dev/null +++ llvm/test/tools/llvm-cov/Inputs/mcdc-general-none.proftext @@ -0,0 +1,44 @@ +main +# Func Hash: +24 +# Num Counters: +1 +# Counter Values: +1 + +_Z4testbbbb +# Func Hash: +9819241276358969079 +# Num Counters: +19 +# Counter Values: +7 +3 +5 +5 +2 +3 +1 +2 +3 +2 +1 +1 +1 +1 +2 +4 +2 +2 +1 +# Num Bitmask Bytes: +$6 +# Bitmask Byte Values: +0 +0 +0 +0 +0 +0 + + Index: llvm/test/tools/llvm-cov/Inputs/mcdc-general.cpp =================================================================== --- /dev/null +++ llvm/test/tools/llvm-cov/Inputs/mcdc-general.cpp @@ -0,0 +1,36 @@ + +#include + +extern "C" { +extern void __llvm_profile_write_file(void); +} + +extern int foo(); + +void test(bool a, bool b, bool c, bool d) { + + if ((a && b) || (c && d)) + printf("test1 decision true\n"); + + if (b && c) if (a && d) + printf("test2 decision true\n"); + + if ((c && d) && + (a && b)) + printf("test3 decision true\n"); +} + +int main() +{ + test(false,false,false,false); + test(true,false,true,false); + test(true,false,true,true); + test(true,true,false,false); + + test(true,false,false,false); + test(true,true,true,true); + test(false,true,true,false); + + __llvm_profile_write_file(); + return 0; +} Index: llvm/test/tools/llvm-cov/Inputs/mcdc-general.proftext =================================================================== --- /dev/null +++ llvm/test/tools/llvm-cov/Inputs/mcdc-general.proftext @@ -0,0 +1,44 @@ +main +# Func Hash: +24 +# Num Counters: +1 +# Counter Values: +1 + +_Z4testbbbb +# Func Hash: +9819241276358969079 +# Num Counters: +19 +# Counter Values: +7 +3 +5 +5 +2 +3 +1 +2 +3 +2 +1 +1 +1 +1 +2 +4 +2 +2 +1 +# Num Bitmask Bytes: +$6 +# Bitmask Byte Values: +2f +8 +b +9 +83 +80 + + Index: llvm/test/tools/llvm-cov/mcdc-const.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-cov/mcdc-const.test @@ -0,0 +1,203 @@ +// Test visualization of MC/DC constructs for constant-folded condition masking. + +// RUN: llvm-profdata merge %S/Inputs/mcdc-const.proftext -o %t.profdata +// RUN: llvm-cov show --show-branches=count --show-mcdc %S/Inputs/mcdc-const.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs %S/Inputs/mcdc-const.cpp | FileCheck %s -check-prefix=CHECKGENERALCASE + +// CHECKGENERALCASE: ------------------ +// CHECKGENERALCASE-NEXT: | Branch (12:8): [True: 4, False: 1] +// CHECKGENERALCASE-NEXT: | Branch (12:13): [Folded - Ignored] +// CHECKGENERALCASE-NEXT: | Branch (12:20): [Folded - Ignored] +// CHECKGENERALCASE-NEXT: | Branch (12:25): [True: 0, False: 0] +// CHECKGENERALCASE-NEXT: | Branch (12:31): [Folded - Ignored] +// CHECKGENERALCASE-NEXT: ------------------ +// CHECKGENERALCASE-NEXT: |---> MC/DC Decision Region (12:7) to (12:32) +// CHECKGENERALCASE-NEXT: | +// CHECKGENERALCASE-NEXT: | Number of Conditions: 5 +// CHECKGENERALCASE-NEXT: | Condition C1 --> (12:8) +// CHECKGENERALCASE-NEXT: | Condition C2 --> (12:13) +// CHECKGENERALCASE-NEXT: | Condition C3 --> (12:20) +// CHECKGENERALCASE-NEXT: | Condition C4 --> (12:25) +// CHECKGENERALCASE-NEXT: | Condition C5 --> (12:31) +// CHECKGENERALCASE-NEXT: | +// CHECKGENERALCASE-NEXT: | Executed MC/DC Test Vectors: +// CHECKGENERALCASE-NEXT: | +// CHECKGENERALCASE-NEXT: | C1, C2, C3, C4, C5 Result +// CHECKGENERALCASE-NEXT: | 1 { F, C, C, -, C = F } +// CHECKGENERALCASE-NEXT: | 2 { T, C, C, -, C = T } +// CHECKGENERALCASE-NEXT: | +// CHECKGENERALCASE-NEXT: | C1-Pair: covered: (1,2) +// CHECKGENERALCASE-NEXT: | C2-Pair: constant folded +// CHECKGENERALCASE-NEXT: | C3-Pair: constant folded +// CHECKGENERALCASE-NEXT: | C4-Pair: not covered +// CHECKGENERALCASE-NEXT: | C5-Pair: constant folded +// CHECKGENERALCASE-NEXT: | MC/DC Coverage for Decision: 50.00% +// CHECKGENERALCASE-NEXT: | +// CHECKGENERALCASE-NEXT: ------------------ + +// RUN: llvm-profdata merge %S/Inputs/mcdc-const-folding.proftext -o %t.profdata +// RUN: llvm-cov show --show-mcdc %S/Inputs/mcdc-const-folding.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs %S/Inputs/mcdc-const-folding.cpp | FileCheck %s -check-prefix=CHECKFULLCASE +// RUN: llvm-cov report --show-mcdc-summary %S/Inputs/mcdc-const-folding.o32l -instr-profile %t.profdata -show-functions -path-equivalence=/tmp,%S/Inputs %S/Inputs/mcdc-const-folding.cpp | FileCheck %s -check-prefix=REPORT + +// CHECKFULLCASE: | 1 { C, - = F } +// CHECKFULLCASE: | C1-Pair: constant folded +// CHECKFULLCASE: | C2-Pair: not covered +// CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% +// CHECKFULLCASE: | 1 { F, C = F } +// CHECKFULLCASE: | 2 { T, C = F } +// CHECKFULLCASE: | C1-Pair: not covered +// CHECKFULLCASE: | C2-Pair: constant folded +// CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% +// CHECKFULLCASE: | 1 { C, F = F } +// CHECKFULLCASE: | 2 { C, T = T } +// CHECKFULLCASE: | C1-Pair: constant folded +// CHECKFULLCASE: | C2-Pair: covered: (1,2) +// CHECKFULLCASE: | MC/DC Coverage for Decision: 100.00% +// CHECKFULLCASE: | 1 { F, C = F } +// CHECKFULLCASE: | 2 { T, C = T } +// CHECKFULLCASE: | C1-Pair: covered: (1,2) +// CHECKFULLCASE: | C2-Pair: constant folded +// CHECKFULLCASE: | MC/DC Coverage for Decision: 100.00% +// CHECKFULLCASE: | 1 { C, - = T } +// CHECKFULLCASE: | C1-Pair: constant folded +// CHECKFULLCASE: | C2-Pair: not covered +// CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% +// CHECKFULLCASE: | 1 { T, C = T } +// CHECKFULLCASE: | 2 { F, C = T } +// CHECKFULLCASE: | C1-Pair: not covered +// CHECKFULLCASE: | C2-Pair: constant folded +// CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% +// CHECKFULLCASE: | 1 { C, F = F } +// CHECKFULLCASE: | 2 { C, T = T } +// CHECKFULLCASE: | C1-Pair: constant folded +// CHECKFULLCASE: | C2-Pair: covered: (1,2) +// CHECKFULLCASE: | MC/DC Coverage for Decision: 100.00% +// CHECKFULLCASE: | 1 { F, C = F } +// CHECKFULLCASE: | 2 { T, C = T } +// CHECKFULLCASE: | C1-Pair: covered: (1,2) +// CHECKFULLCASE: | C2-Pair: constant folded +// CHECKFULLCASE: | MC/DC Coverage for Decision: 100.00% +// CHECKFULLCASE: | 1 { C, -, - = F } +// CHECKFULLCASE: | C1-Pair: constant folded +// CHECKFULLCASE: | C2-Pair: not covered +// CHECKFULLCASE: | C3-Pair: not covered +// CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% +// CHECKFULLCASE: | 1 { F, C, - = F } +// CHECKFULLCASE: | 2 { T, C, - = F } +// CHECKFULLCASE: | C1-Pair: not covered +// CHECKFULLCASE: | C2-Pair: constant folded +// CHECKFULLCASE: | C3-Pair: not covered +// CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% +// CHECKFULLCASE: | 1 { C, F, - = F } +// CHECKFULLCASE: | 2 { C, T, F = F } +// CHECKFULLCASE: | 3 { C, T, T = T } +// CHECKFULLCASE: | C1-Pair: constant folded +// CHECKFULLCASE: | C2-Pair: covered: (1,3) +// CHECKFULLCASE: | C3-Pair: covered: (2,3) +// CHECKFULLCASE: | MC/DC Coverage for Decision: 100.00% +// CHECKFULLCASE: | 1 { F, C, - = F } +// CHECKFULLCASE: | 2 { T, C, F = F } +// CHECKFULLCASE: | 3 { T, C, T = T } +// CHECKFULLCASE: | C1-Pair: covered: (1,3) +// CHECKFULLCASE: | C2-Pair: constant folded +// CHECKFULLCASE: | C3-Pair: covered: (2,3) +// CHECKFULLCASE: | MC/DC Coverage for Decision: 100.00% +// CHECKFULLCASE: | 1 { C, -, - = T } +// CHECKFULLCASE: | C1-Pair: constant folded +// CHECKFULLCASE: | C2-Pair: not covered +// CHECKFULLCASE: | C3-Pair: not covered +// CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% +// CHECKFULLCASE: | 1 { T, C, - = T } +// CHECKFULLCASE: | 2 { F, C, - = T } +// CHECKFULLCASE: | C1-Pair: not covered +// CHECKFULLCASE: | C2-Pair: constant folded +// CHECKFULLCASE: | C3-Pair: not covered +// CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% +// CHECKFULLCASE: | 1 { C, F, T = T } +// CHECKFULLCASE: | 2 { C, T, - = T } +// CHECKFULLCASE: | C1-Pair: constant folded +// CHECKFULLCASE: | C2-Pair: not covered +// CHECKFULLCASE: | C3-Pair: not covered +// CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% +// CHECKFULLCASE: | 1 { T, C, - = T } +// CHECKFULLCASE: | 2 { F, C, T = T } +// CHECKFULLCASE: | C1-Pair: not covered +// CHECKFULLCASE: | C2-Pair: constant folded +// CHECKFULLCASE: | C3-Pair: not covered +// CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% +// CHECKFULLCASE: | 1 { F, -, C = F } +// CHECKFULLCASE: | 2 { T, F, C = F } +// CHECKFULLCASE: | 3 { T, T, C = F } +// CHECKFULLCASE: | C1-Pair: not covered +// CHECKFULLCASE: | C2-Pair: not covered +// CHECKFULLCASE: | C3-Pair: constant folded +// CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% +// CHECKFULLCASE: | 1 { F, C, - = F } +// CHECKFULLCASE: | 2 { T, C, - = F } +// CHECKFULLCASE: | C1-Pair: not covered +// CHECKFULLCASE: | C2-Pair: constant folded +// CHECKFULLCASE: | C3-Pair: not covered +// CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% +// CHECKFULLCASE: | 1 { F, -, C = F } +// CHECKFULLCASE: | 2 { T, F, C = F } +// CHECKFULLCASE: | 3 { T, T, C = T } +// CHECKFULLCASE: | C1-Pair: covered: (1,3) +// CHECKFULLCASE: | C2-Pair: covered: (2,3) +// CHECKFULLCASE: | C3-Pair: constant folded +// CHECKFULLCASE: | MC/DC Coverage for Decision: 100.00% +// CHECKFULLCASE: | 1 { F, C, - = F } +// CHECKFULLCASE: | 2 { T, C, F = F } +// CHECKFULLCASE: | 3 { T, C, T = T } +// CHECKFULLCASE: | C1-Pair: covered: (1,3) +// CHECKFULLCASE: | C2-Pair: constant folded +// CHECKFULLCASE: | C3-Pair: covered: (2,3) +// CHECKFULLCASE: | MC/DC Coverage for Decision: 100.00% +// CHECKFULLCASE: | 1 { T, -, C = T } +// CHECKFULLCASE: | 2 { F, T, C = T } +// CHECKFULLCASE: | C1-Pair: not covered +// CHECKFULLCASE: | C2-Pair: not covered +// CHECKFULLCASE: | C3-Pair: constant folded +// CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% +// CHECKFULLCASE: | 1 { T, C, - = T } +// CHECKFULLCASE: | 2 { F, C, - = T } +// CHECKFULLCASE: | C1-Pair: not covered +// CHECKFULLCASE: | C2-Pair: constant folded +// CHECKFULLCASE: | C3-Pair: not covered +// CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% +// CHECKFULLCASE: | 1 { T, -, C = T } +// CHECKFULLCASE: | 2 { F, T, C = T } +// CHECKFULLCASE: | C1-Pair: not covered +// CHECKFULLCASE: | C2-Pair: not covered +// CHECKFULLCASE: | C3-Pair: constant folded +// CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% +// CHECKFULLCASE: | 1 { T, C, - = T } +// CHECKFULLCASE: | 2 { F, C, T = T } +// CHECKFULLCASE: | C1-Pair: not covered +// CHECKFULLCASE: | C2-Pair: constant folded +// CHECKFULLCASE: | C3-Pair: not covered +// CHECKFULLCASE: | MC/DC Coverage for Decision: 0.00% + +// REPORT: _Z5case0b {{.*}} 1 1 0.00% +// REPORT: _Z5case1b {{.*}} 1 1 0.00% +// REPORT: _Z5case2b {{.*}} 1 0 100.00% +// REPORT: _Z5case3b {{.*}} 1 0 100.00% +// REPORT: _Z5case4b {{.*}} 1 1 0.00% +// REPORT: _Z5case5b {{.*}} 1 1 0.00% +// REPORT: _Z5case6b {{.*}} 1 0 100.00% +// REPORT: _Z5case7b {{.*}} 1 0 100.00% +// REPORT: _Z5case8bb {{.*}} 2 2 0.00% +// REPORT: _Z5case9bb {{.*}} 2 2 0.00% +// REPORT: _Z5caseabb {{.*}} 2 0 100.00% +// REPORT: _Z5casebbb {{.*}} 2 0 100.00% +// REPORT: _Z5casecbb {{.*}} 2 2 0.00% +// REPORT: _Z5casedbb {{.*}} 2 2 0.00% +// REPORT: _Z5caseebb {{.*}} 2 2 0.00% +// REPORT: _Z5casefbb {{.*}} 2 2 0.00% +// REPORT: _Z5casegbb {{.*}} 2 2 0.00% +// REPORT: _Z5casehbb {{.*}} 2 2 0.00% +// REPORT: _Z5caseibb {{.*}} 2 0 100.00% +// REPORT: _Z5casejbb {{.*}} 2 0 100.00% +// REPORT: _Z5casekbb {{.*}} 2 2 0.00% +// REPORT: _Z5caselbb {{.*}} 2 2 0.00% +// REPORT: _Z5casembb {{.*}} 2 2 0.00% +// REPORT: _Z5casenbb {{.*}} 2 2 0.00% +// REPORT: TOTAL {{.*}} 40 28 30.00% Index: llvm/test/tools/llvm-cov/mcdc-export-json.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-cov/mcdc-export-json.test @@ -0,0 +1,8 @@ +// RUN: llvm-profdata merge %S/Inputs/mcdc-general.proftext -o %t.profdata +// RUN: llvm-cov export --format=text %S/Inputs/mcdc-general.o32l -instr-profile %t.profdata | FileCheck %s + +// CHECK: 12,7,12,27,0,5,[true,true,true,true] +// CHECK: 15,7,15,13,0,5,[true,true] +// CHECK: 15,19,15,25,0,5,[true,false] +// CHECK: 18,7,19,15,0,5,[true,true,false,true] +// CHECK: "mcdc":{"count":12,"covered":10,"notcovered":2,"percent":83.333333333333343} Index: llvm/test/tools/llvm-cov/mcdc-general-none.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-cov/mcdc-general-none.test @@ -0,0 +1,72 @@ +// Test visualization of general MC/DC constructs with 0 executed test vectors. + +// RUN: llvm-profdata merge %S/Inputs/mcdc-general-none.proftext -o %t.profdata +// RUN: llvm-cov show --show-mcdc %S/Inputs/mcdc-general.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs %S/Inputs/mcdc-general.cpp | FileCheck %s +// RUN: llvm-cov report --show-mcdc-summary %S/Inputs/mcdc-general.o32l -instr-profile %t.profdata -show-functions -path-equivalence=/tmp,%S/Inputs %S/Inputs/mcdc-general.cpp | FileCheck %s -check-prefix=REPORT + +// CHECK: test(bool + +// CHECK: ------------------ +// CHECK-NEXT: |---> MC/DC Decision Region (12:7) to (12:27) +// CHECK-NEXT: | +// CHECK-NEXT: | Number of Conditions: 4 +// CHECK-NEXT: | Condition C1 --> (12:8) +// CHECK-NEXT: | Condition C2 --> (12:13) +// CHECK-NEXT: | Condition C3 --> (12:20) +// CHECK-NEXT: | Condition C4 --> (12:25) +// CHECK-NEXT: | +// CHECK-NEXT: | Executed MC/DC Test Vectors: +// CHECK-NEXT: | +// CHECK-NEXT: | None. +// CHECK-NEXT: | +// CHECK-NEXT: | C1-Pair: not covered +// CHECK-NEXT: | C2-Pair: not covered +// CHECK-NEXT: | C3-Pair: not covered +// CHECK-NEXT: | C4-Pair: not covered +// CHECK-NEXT: | MC/DC Coverage for Decision: 0.00% +// CHECK-NEXT: | +// CHECK-NEXT: ------------------ + + +// Turn off MC/DC visualization. +// RUN: llvm-cov show %S/Inputs/mcdc-general.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs %S/Inputs/mcdc-general.cpp | FileCheck %s -check-prefix=NOMCDC +// NOMCDC-NOT: MC/DC Decision Region + +// REPORT: Name Regions Miss Cover Lines Miss Cover Branches Miss Cover MC/DC Conditions Miss Cover +// REPORT-NEXT: ------------------------------------------------------------------------------------------------------------------------------------------- +// REPORT-NEXT: _Z4testbbbb 25 0 100.00% 9 0 100.00% 24 2 91.67% 12 12 0.00% +// REPORT-NEXT: main 1 0 100.00% 11 0 100.00% 0 0 0.00% 0 0 0.00% +// REPORT-NEXT: --- +// REPORT-NEXT: TOTAL 26 0 100.00% 20 0 100.00% 24 2 91.67% 12 12 0.00% + +// Turn off MC/DC summary. +// RUN: llvm-cov report %S/Inputs/mcdc-general.o32l -instr-profile %t.profdata -show-functions -path-equivalence=/tmp,%S/Inputs %S/Inputs/mcdc-general.cpp | FileCheck %s -check-prefix=REPORT_NOMCDC +// REPORT_NOMCDC-NOT: TOTAL{{.*}}12 12 0.00% + + +// Test file-level report. +// RUN: llvm-cov report --show-mcdc-summary %S/Inputs/mcdc-general.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs %S/Inputs/mcdc-general.cpp | FileCheck %s -check-prefix=FILEREPORT +// FILEREPORT: TOTAL{{.*}}12 12 0.00% + + +// Test html output. +// RUN: llvm-cov show --show-mcdc-summary --show-mcdc %S/Inputs/mcdc-general.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs %S/Inputs/mcdc-general.cpp -format html -o %t.html.dir +// RUN: FileCheck -check-prefix=HTML -input-file=%t.html.dir/coverage/tmp/mcdc-general.cpp.html %s +// HTML-COUNT-4: MC/DC Decision Region ( + +// RUN: FileCheck -check-prefix HTML-INDEX -input-file %t.html.dir/index.html %s +// HTML-INDEX-LABEL: +// HTML-INDEX: +// HTML-INDEX: +// HTML-INDEX: +// HTML-INDEX: +// HTML-INDEX: +// HTML-INDEX: +// HTML-INDEX: +// HTML-INDEX:
FilenameFunction CoverageLine CoverageRegion CoverageBranch CoverageMC/DC +// HTML-INDEX: 100.00% (2/2) +// HTML-INDEX: 100.00% (20/20) +// HTML-INDEX: 100.00% (26/26) +// HTML-INDEX: 91.67% (22/24) +// HTML-INDEX: 0.00% (0/12) +// HTML-INDEX: Totals Index: llvm/test/tools/llvm-cov/mcdc-general.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-cov/mcdc-general.test @@ -0,0 +1,138 @@ +// Test visualization of general MC/DC constructs. + +// RUN: llvm-profdata merge %S/Inputs/mcdc-general.proftext -o %t.profdata +// RUN: llvm-cov show --show-mcdc %S/Inputs/mcdc-general.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs %S/Inputs/mcdc-general.cpp | FileCheck %s +// RUN: llvm-cov report --show-mcdc-summary %S/Inputs/mcdc-general.o32l -instr-profile %t.profdata -show-functions -path-equivalence=/tmp,%S/Inputs %S/Inputs/mcdc-general.cpp | FileCheck %s -check-prefix=REPORT + +// CHECK: test(bool + +// CHECK: ------------------ +// CHECK-NEXT: |---> MC/DC Decision Region (12:7) to (12:27) +// CHECK-NEXT: | +// CHECK-NEXT: | Number of Conditions: 4 +// CHECK-NEXT: | Condition C1 --> (12:8) +// CHECK-NEXT: | Condition C2 --> (12:13) +// CHECK-NEXT: | Condition C3 --> (12:20) +// CHECK-NEXT: | Condition C4 --> (12:25) +// CHECK-NEXT: | +// CHECK-NEXT: | Executed MC/DC Test Vectors: +// CHECK-NEXT: | +// CHECK-NEXT: | C1, C2, C3, C4 Result +// CHECK-NEXT: | 1 { F, -, F, - = F } +// CHECK-NEXT: | 2 { T, F, F, - = F } +// CHECK-NEXT: | 3 { F, -, T, F = F } +// CHECK-NEXT: | 4 { T, F, T, F = F } +// CHECK-NEXT: | 5 { T, T, -, - = T } +// CHECK-NEXT: | 6 { T, F, T, T = T } +// CHECK-NEXT: | +// CHECK-NEXT: | C1-Pair: covered: (1,5) +// CHECK-NEXT: | C2-Pair: covered: (2,5) +// CHECK-NEXT: | C3-Pair: covered: (2,6) +// CHECK-NEXT: | C4-Pair: covered: (4,6) +// CHECK-NEXT: | MC/DC Coverage for Decision: 100.00% +// CHECK-NEXT: | +// CHECK-NEXT: ------------------ + +// CHECK: ------------------ +// CHECK-NEXT: |---> MC/DC Decision Region (15:7) to (15:13) +// CHECK-NEXT: | +// CHECK-NEXT: | Number of Conditions: 2 +// CHECK-NEXT: | Condition C1 --> (15:7) +// CHECK-NEXT: | Condition C2 --> (15:12) +// CHECK-NEXT: | +// CHECK-NEXT: | Executed MC/DC Test Vectors: +// CHECK-NEXT: | +// CHECK-NEXT: | C1, C2 Result +// CHECK-NEXT: | 1 { F, - = F } +// CHECK-NEXT: | 2 { T, F = F } +// CHECK-NEXT: | 3 { T, T = T } +// CHECK-NEXT: | +// CHECK-NEXT: | C1-Pair: covered: (1,3) +// CHECK-NEXT: | C2-Pair: covered: (2,3) +// CHECK-NEXT: | MC/DC Coverage for Decision: 100.00% +// CHECK-NEXT: | +// CHECK-NEXT: |---> MC/DC Decision Region (15:19) to (15:25) +// CHECK-NEXT: | +// CHECK-NEXT: | Number of Conditions: 2 +// CHECK-NEXT: | Condition C1 --> (15:19) +// CHECK-NEXT: | Condition C2 --> (15:24) +// CHECK-NEXT: | +// CHECK-NEXT: | Executed MC/DC Test Vectors: +// CHECK-NEXT: | +// CHECK-NEXT: | C1, C2 Result +// CHECK-NEXT: | 1 { F, - = F } +// CHECK-NEXT: | 2 { T, T = T } +// CHECK-NEXT: | +// CHECK-NEXT: | C1-Pair: covered: (1,2) +// CHECK-NEXT: | C2-Pair: not covered +// CHECK-NEXT: | MC/DC Coverage for Decision: 50.00% +// CHECK-NEXT: | +// CHECK-NEXT: ------------------ + +// CHECK: ------------------ +// CHECK-NEXT: |---> MC/DC Decision Region (18:7) to (19:15) +// CHECK-NEXT: | +// CHECK-NEXT: | Number of Conditions: 4 +// CHECK-NEXT: | Condition C1 --> (18:8) +// CHECK-NEXT: | Condition C2 --> (18:13) +// CHECK-NEXT: | Condition C3 --> (19:8) +// CHECK-NEXT: | Condition C4 --> (19:13) +// CHECK-NEXT: | +// CHECK-NEXT: | Executed MC/DC Test Vectors: +// CHECK-NEXT: | +// CHECK-NEXT: | C1, C2, C3, C4 Result +// CHECK-NEXT: | 1 { F, -, -, - = F } +// CHECK-NEXT: | 2 { T, F, -, - = F } +// CHECK-NEXT: | 3 { T, T, T, F = F } +// CHECK-NEXT: | 4 { T, T, T, T = T } +// CHECK-NEXT: | +// CHECK-NEXT: | C1-Pair: covered: (1,4) +// CHECK-NEXT: | C2-Pair: covered: (2,4) +// CHECK-NEXT: | C3-Pair: not covered +// CHECK-NEXT: | C4-Pair: covered: (3,4) +// CHECK-NEXT: | MC/DC Coverage for Decision: 75.00% +// CHECK-NEXT: | +// CHECK-NEXT: ------------------ + +// Turn off MC/DC visualization. +// RUN: llvm-cov show %S/Inputs/mcdc-general.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs %S/Inputs/mcdc-general.cpp | FileCheck %s -check-prefix=NOMCDC +// NOMCDC-NOT: MC/DC Decision Region + +// REPORT: Name Regions Miss Cover Lines Miss Cover Branches Miss Cover MC/DC Conditions Miss Cover +// REPORT-NEXT: ------------------------------------------------------------------------------------------------------------------------------------------- +// REPORT-NEXT: _Z4testbbbb 25 0 100.00% 9 0 100.00% 24 2 91.67% 12 2 83.33% +// REPORT-NEXT: main 1 0 100.00% 11 0 100.00% 0 0 0.00% 0 0 0.00% +// REPORT-NEXT: --- +// REPORT-NEXT: TOTAL 26 0 100.00% 20 0 100.00% 24 2 91.67% 12 2 83.33% + +// Turn off MC/DC summary. +// RUN: llvm-cov report %S/Inputs/mcdc-general.o32l -instr-profile %t.profdata -show-functions -path-equivalence=/tmp,%S/Inputs %S/Inputs/mcdc-general.cpp | FileCheck %s -check-prefix=REPORT_NOMCDC +// REPORT_NOMCDC-NOT: TOTAL{{.*}}12 2 83.33% + + +// Test file-level report. +// RUN: llvm-cov report --show-mcdc-summary %S/Inputs/mcdc-general.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs %S/Inputs/mcdc-general.cpp | FileCheck %s -check-prefix=FILEREPORT +// FILEREPORT: TOTAL{{.*}}12 2 83.33% + + +// Test html output. +// RUN: llvm-cov show --show-mcdc-summary --show-mcdc %S/Inputs/mcdc-general.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs %S/Inputs/mcdc-general.cpp -format html -o %t.html.dir +// RUN: FileCheck -check-prefix=HTML -input-file=%t.html.dir/coverage/tmp/mcdc-general.cpp.html %s +// HTML-COUNT-4: MC/DC Decision Region ( + +// RUN: FileCheck -check-prefix HTML-INDEX -input-file %t.html.dir/index.html %s +// HTML-INDEX-LABEL: +// HTML-INDEX: +// HTML-INDEX: +// HTML-INDEX: +// HTML-INDEX: +// HTML-INDEX: +// HTML-INDEX: +// HTML-INDEX: +// HTML-INDEX:
FilenameFunction CoverageLine CoverageRegion CoverageBranch CoverageMC/DC +// HTML-INDEX: 100.00% (2/2) +// HTML-INDEX: 100.00% (20/20) +// HTML-INDEX: 100.00% (26/26) +// HTML-INDEX: 91.67% (22/24) +// HTML-INDEX: 83.33% (10/12) +// HTML-INDEX: Totals Index: llvm/tools/llvm-cov/CodeCoverage.cpp =================================================================== --- llvm/tools/llvm-cov/CodeCoverage.cpp +++ llvm/tools/llvm-cov/CodeCoverage.cpp @@ -101,6 +101,11 @@ const MemoryBuffer &File, CoverageData &CoverageInfo); + /// Create source views for the MCDC records. + void attachMCDCSubViews(SourceCoverageView &View, StringRef SourceName, + ArrayRef MCDCRecords, + const MemoryBuffer &File, CoverageData &CoverageInfo); + /// Create the source view of a particular function. std::unique_ptr createFunctionView(const FunctionRecord &Function, @@ -343,6 +348,36 @@ } } +void CodeCoverageTool::attachMCDCSubViews(SourceCoverageView &View, + StringRef SourceName, + ArrayRef MCDCRecords, + const MemoryBuffer &File, + CoverageData &CoverageInfo) { + if (!ViewOpts.ShowMCDC) + return; + + const auto *NextRecord = MCDCRecords.begin(); + const auto *EndRecord = MCDCRecords.end(); + + // Group and process MCDC records that have the same line number into the + // same subview. + while (NextRecord != EndRecord) { + std::vector ViewMCDCRecords; + unsigned CurrentLine = NextRecord->getDecisionRegion().LineEnd; + + while (NextRecord != EndRecord && + CurrentLine == NextRecord->getDecisionRegion().LineEnd) { + ViewMCDCRecords.push_back(*NextRecord++); + } + + if (!ViewMCDCRecords.empty()) { + auto SubView = SourceCoverageView::create(SourceName, File, ViewOpts, + std::move(CoverageInfo)); + View.addMCDCRecord(CurrentLine, ViewMCDCRecords, std::move(SubView)); + } + } +} + std::unique_ptr CodeCoverageTool::createFunctionView(const FunctionRecord &Function, const CoverageMapping &Coverage) { @@ -355,12 +390,15 @@ auto Branches = FunctionCoverage.getBranches(); auto Expansions = FunctionCoverage.getExpansions(); + auto MCDCRecords = FunctionCoverage.getMCDCRecords(); auto View = SourceCoverageView::create(DC.demangle(Function.Name), SourceBuffer.get(), ViewOpts, std::move(FunctionCoverage)); attachExpansionSubViews(*View, Expansions, Coverage); attachBranchSubViews(*View, DC.demangle(Function.Name), Branches, SourceBuffer.get(), FunctionCoverage); + attachMCDCSubViews(*View, DC.demangle(Function.Name), MCDCRecords, + SourceBuffer.get(), FunctionCoverage); return View; } @@ -377,11 +415,14 @@ auto Branches = FileCoverage.getBranches(); auto Expansions = FileCoverage.getExpansions(); + auto MCDCRecords = FileCoverage.getMCDCRecords(); auto View = SourceCoverageView::create(SourceFile, SourceBuffer.get(), ViewOpts, std::move(FileCoverage)); attachExpansionSubViews(*View, Expansions, Coverage); attachBranchSubViews(*View, SourceFile, Branches, SourceBuffer.get(), FileCoverage); + attachMCDCSubViews(*View, SourceFile, MCDCRecords, SourceBuffer.get(), + FileCoverage); if (!ViewOpts.ShowFunctionInstantiations) return View; @@ -399,11 +440,14 @@ auto SubViewCoverage = Coverage.getCoverageForFunction(*Function); auto SubViewExpansions = SubViewCoverage.getExpansions(); auto SubViewBranches = SubViewCoverage.getBranches(); + auto SubViewMCDCRecords = SubViewCoverage.getMCDCRecords(); SubView = SourceCoverageView::create( Funcname, SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage)); attachExpansionSubViews(*SubView, SubViewExpansions, Coverage); attachBranchSubViews(*SubView, SourceFile, SubViewBranches, SourceBuffer.get(), SubViewCoverage); + attachMCDCSubViews(*SubView, SourceFile, SubViewMCDCRecords, + SourceBuffer.get(), SubViewCoverage); } unsigned FileID = Function->CountedRegions.front().FileID; @@ -725,6 +769,11 @@ cl::desc("Show branch condition statistics in summary table"), cl::init(true)); + cl::opt MCDCSummary( + "show-mcdc-summary", cl::Optional, + cl::desc("Show MCDC statistics in summary table"), + cl::init(false)); + cl::opt InstantiationSummary( "show-instantiation-summary", cl::Optional, cl::desc("Show instantiation statistics in summary table")); @@ -883,6 +932,7 @@ ::exit(0); } + ViewOpts.ShowMCDCSummary = MCDCSummary; ViewOpts.ShowBranchSummary = BranchSummary; ViewOpts.ShowRegionSummary = RegionSummary; ViewOpts.ShowInstantiationSummary = InstantiationSummary; @@ -928,6 +978,11 @@ "percent", "Show True/False percent")), cl::init(CoverageViewOptions::BranchOutputType::Off)); + cl::opt ShowMCDC( + "show-mcdc", cl::Optional, + cl::desc("Show the MCDC Coverage for each applicable boolean expression"), + cl::cat(ViewCategory)); + cl::opt ShowBestLineRegionsCounts( "show-line-counts-or-regions", cl::Optional, cl::desc("Show the execution counts for each line, or the execution " @@ -1019,6 +1074,7 @@ ViewOpts.ShowExpandedRegions = ShowExpansions; ViewOpts.ShowBranchCounts = ShowBranches == CoverageViewOptions::BranchOutputType::Count; + ViewOpts.ShowMCDC = ShowMCDC; ViewOpts.ShowBranchPercents = ShowBranches == CoverageViewOptions::BranchOutputType::Percent; ViewOpts.ShowFunctionInstantiations = ShowInstantiations; Index: llvm/tools/llvm-cov/CoverageExporterJson.cpp =================================================================== --- llvm/tools/llvm-cov/CoverageExporterJson.cpp +++ llvm/tools/llvm-cov/CoverageExporterJson.cpp @@ -20,6 +20,8 @@ // -- File: dict => Coverage for a single file // -- Branches: array => List of Branches in the file // -- Branch: dict => Describes a branch of the file with counters +// -- MCDC Records: array => List of MCDC records in the file +// -- MCDC Values: array => List of T/F covered condition values // -- Segments: array => List of Segments contained in the file // -- Segment: dict => Describes a segment of the file with a counter // -- Expansions: array => List of expansion records @@ -34,6 +36,7 @@ // -- FunctionCoverage: dict => Object summarizing function coverage // -- RegionCoverage: dict => Object summarizing region coverage // -- BranchCoverage: dict => Object summarizing branch coverage +// -- MCDCCoverage: dict => Object summarizing MC/DC coverage // -- Functions: array => List of objects describing coverage for functions // -- Function: dict => Coverage info for a single function // -- Filenames: array => List of filenames that the function relates to @@ -43,6 +46,7 @@ // -- InstantiationCoverage: dict => Object summarizing inst. coverage // -- RegionCoverage: dict => Object summarizing region coverage // -- BranchCoverage: dict => Object summarizing branch coverage +// -- MCDCCoverage: dict => Object summarizing MC/DC coverage // //===----------------------------------------------------------------------===// @@ -98,6 +102,20 @@ Region.ExpandedFileID, int64_t(Region.Kind)}); } +json::Array gatherConditions(const coverage::MCDCRecord &Record) { + json::Array Conditions; + for (unsigned c = 0; c < Record.getNumConditions(); c++) + Conditions.push_back(Record.isCondIndepPairCovered(c)); + return Conditions; +} + +json::Array renderMCDCRecord(const coverage::MCDCRecord &Record) { + const llvm::coverage::CounterMappingRegion &CMR = Record.getDecisionRegion(); + return json::Array({CMR.LineStart, CMR.ColumnStart, CMR.LineEnd, + CMR.ColumnEnd, CMR.ExpandedFileID, int64_t(CMR.Kind), + gatherConditions(Record)}); +} + json::Array renderRegions(ArrayRef Regions) { json::Array RegionArray; for (const auto &Region : Regions) @@ -113,6 +131,13 @@ return RegionArray; } +json::Array renderMCDCRecords(ArrayRef Records) { + json::Array RecordArray; + for (auto &Record : Records) + RecordArray.push_back(renderMCDCRecord(Record)); + return RecordArray; +} + std::vector collectNestedBranches(const coverage::CoverageMapping &Coverage, ArrayRef Expansions) { @@ -179,7 +204,14 @@ {"covered", int64_t(Summary.BranchCoverage.getCovered())}, {"notcovered", int64_t(Summary.BranchCoverage.getNumBranches() - Summary.BranchCoverage.getCovered())}, - {"percent", Summary.BranchCoverage.getPercentCovered()}})}}); + {"percent", Summary.BranchCoverage.getPercentCovered()}})}, + {"mcdc", + json::Object( + {{"count", int64_t(Summary.MCDCCoverage.getNumPairs())}, + {"covered", int64_t(Summary.MCDCCoverage.getCoveredPairs())}, + {"notcovered", int64_t(Summary.MCDCCoverage.getNumPairs() - + Summary.MCDCCoverage.getCoveredPairs())}, + {"percent", Summary.MCDCCoverage.getPercentCovered()}})}}); } json::Array renderFileExpansions(const coverage::CoverageMapping &Coverage, @@ -207,6 +239,14 @@ return BranchArray; } +json::Array renderFileMCDC(const coverage::CoverageData &FileCoverage, + const FileCoverageSummary &FileReport) { + json::Array MCDCRecordArray; + for (const auto &Record : FileCoverage.getMCDCRecords()) + MCDCRecordArray.push_back(renderMCDCRecord(Record)); + return MCDCRecordArray; +} + json::Object renderFile(const coverage::CoverageMapping &Coverage, const std::string &Filename, const FileCoverageSummary &FileReport, @@ -217,6 +257,7 @@ auto FileCoverage = Coverage.getCoverageForFile(Filename); File["segments"] = renderFileSegments(FileCoverage, FileReport); File["branches"] = renderFileBranches(FileCoverage, FileReport); + File["mcdc_records"] = renderFileMCDC(FileCoverage, FileReport); if (!Options.SkipExpansions) { File["expansions"] = renderFileExpansions(Coverage, FileCoverage, FileReport); @@ -265,6 +306,7 @@ {"count", clamp_uint64_to_int64(F.ExecutionCount)}, {"regions", renderRegions(F.CountedRegions)}, {"branches", renderBranchRegions(F.CountedBranchRegions)}, + {"mcdc_records", renderMCDCRecords(F.MCDCRecords)}, {"filenames", json::Array(F.Filenames)}})); return FunctionArray; } Index: llvm/tools/llvm-cov/CoverageReport.cpp =================================================================== --- llvm/tools/llvm-cov/CoverageReport.cpp +++ llvm/tools/llvm-cov/CoverageReport.cpp @@ -86,9 +86,9 @@ } // Specify the default column widths. -size_t FileReportColumns[] = {25, 12, 18, 10, 12, 18, 10, 16, - 16, 10, 12, 18, 10, 12, 18, 10}; -size_t FunctionReportColumns[] = {25, 10, 8, 8, 10, 8, 8, 10, 8, 8}; +size_t FileReportColumns[] = {25, 12, 18, 10, 12, 18, 10, 16, 16, 10, + 12, 18, 10, 12, 18, 10, 20, 21, 10}; +size_t FunctionReportColumns[] = {25, 10, 8, 8, 10, 8, 8, 10, 8, 8, 20, 8, 8}; /// Adjust column widths to fit long file paths and function names. void adjustColumnWidths(ArrayRef Files, @@ -256,6 +256,22 @@ OS << column("-", FileReportColumns[15], Column::RightAlignment); } + if (Options.ShowMCDCSummary) { + OS << format("%*u", FileReportColumns[16], + (unsigned)File.MCDCCoverage.getNumPairs()); + Options.colored_ostream(OS, LineCoverageColor) + << format("%*u", FileReportColumns[17], + (unsigned)(File.MCDCCoverage.getNumPairs() - + File.MCDCCoverage.getCoveredPairs())); + if (File.MCDCCoverage.getNumPairs()) + Options.colored_ostream(OS, LineCoverageColor) + << format("%*.2f", FileReportColumns[18] - 1, + File.MCDCCoverage.getPercentCovered()) + << '%'; + else + OS << column("-", FileReportColumns[18], Column::RightAlignment); + } + OS << "\n"; } @@ -303,6 +319,19 @@ Function.BranchCoverage.getPercentCovered()) << '%'; } + if (Options.ShowMCDCSummary) { + OS << format("%*u", FunctionReportColumns[10], + (unsigned)Function.MCDCCoverage.getNumPairs()); + Options.colored_ostream(OS, LineCoverageColor) + << format("%*u", FunctionReportColumns[11], + (unsigned)(Function.MCDCCoverage.getNumPairs() - + Function.MCDCCoverage.getCoveredPairs())); + Options.colored_ostream( + OS, determineCoveragePercentageColor(Function.MCDCCoverage)) + << format("%*.2f", FunctionReportColumns[12] - 1, + Function.MCDCCoverage.getPercentCovered()) + << '%'; + } OS << "\n"; } @@ -335,6 +364,11 @@ OS << column("Branches", FunctionReportColumns[7], Column::RightAlignment) << column("Miss", FunctionReportColumns[8], Column::RightAlignment) << column("Cover", FunctionReportColumns[9], Column::RightAlignment); + if (Options.ShowMCDCSummary) + OS << column("MC/DC Conditions", FunctionReportColumns[10], + Column::RightAlignment) + << column("Miss", FunctionReportColumns[11], Column::RightAlignment) + << column("Cover", FunctionReportColumns[12], Column::RightAlignment); OS << "\n"; renderDivider(FunctionReportColumns, OS); OS << "\n"; @@ -345,6 +379,7 @@ Totals.RegionCoverage += Function.RegionCoverage; Totals.LineCoverage += Function.LineCoverage; Totals.BranchCoverage += Function.BranchCoverage; + Totals.MCDCCoverage += Function.MCDCCoverage; render(Function, DC, OS); } if (Totals.ExecutionCount) { @@ -461,6 +496,12 @@ << column("Missed Branches", FileReportColumns[14], Column::RightAlignment) << column("Cover", FileReportColumns[15], Column::RightAlignment); + if (Options.ShowMCDCSummary) + OS << column("MC/DC Conditions", FileReportColumns[16], + Column::RightAlignment) + << column("Missed Conditions", FileReportColumns[17], + Column::RightAlignment) + << column("Cover", FileReportColumns[18], Column::RightAlignment); OS << "\n"; renderDivider(FileReportColumns, OS); OS << "\n"; Index: llvm/tools/llvm-cov/CoverageSummaryInfo.h =================================================================== --- llvm/tools/llvm-cov/CoverageSummaryInfo.h +++ llvm/tools/llvm-cov/CoverageSummaryInfo.h @@ -142,6 +142,47 @@ } }; +/// Provides information about branches coverage for a function/file. +class MCDCCoverageInfo { + /// The number of Independence Pairs that were covered + size_t CoveredPairs; + + /// The total number of Independence Pairs in a function/file. + size_t NumPairs; + +public: + MCDCCoverageInfo() : CoveredPairs(0), NumPairs(0) {} + + MCDCCoverageInfo(size_t CoveredPairs, size_t NumPairs) + : CoveredPairs(CoveredPairs), NumPairs(NumPairs) { + assert(CoveredPairs <= NumPairs && "Covered Pairs over-counted"); + } + + MCDCCoverageInfo &operator+=(const MCDCCoverageInfo &RHS) { + CoveredPairs += RHS.CoveredPairs; + NumPairs += RHS.NumPairs; + return *this; + } + + void merge(const MCDCCoverageInfo &RHS) { + CoveredPairs = std::max(CoveredPairs, RHS.CoveredPairs); + NumPairs = std::max(NumPairs, RHS.NumPairs); + } + + size_t getCoveredPairs() const { return CoveredPairs; } + + size_t getNumPairs() const { return NumPairs; } + + bool isFullyCovered() const { return CoveredPairs == NumPairs; } + + double getPercentCovered() const { + assert(CoveredPairs <= NumPairs && "Covered Pairs over-counted"); + if (NumPairs == 0) + return 0.0; + return double(CoveredPairs) / double(NumPairs) * 100.0; + } +}; + /// Provides information about function coverage for a file. class FunctionCoverageInfo { /// The number of functions that were executed. @@ -189,6 +230,7 @@ RegionCoverageInfo RegionCoverage; LineCoverageInfo LineCoverage; BranchCoverageInfo BranchCoverage; + MCDCCoverageInfo MCDCCoverage; FunctionCoverageSummary(const std::string &Name) : Name(Name), ExecutionCount(0) {} @@ -196,10 +238,11 @@ FunctionCoverageSummary(const std::string &Name, uint64_t ExecutionCount, const RegionCoverageInfo &RegionCoverage, const LineCoverageInfo &LineCoverage, - const BranchCoverageInfo &BranchCoverage) + const BranchCoverageInfo &BranchCoverage, + const MCDCCoverageInfo &MCDCCoverage) : Name(Name), ExecutionCount(ExecutionCount), RegionCoverage(RegionCoverage), LineCoverage(LineCoverage), - BranchCoverage(BranchCoverage) {} + BranchCoverage(BranchCoverage), MCDCCoverage(MCDCCoverage) {} /// Compute the code coverage summary for the given function coverage /// mapping record. @@ -219,6 +262,7 @@ RegionCoverageInfo RegionCoverage; LineCoverageInfo LineCoverage; BranchCoverageInfo BranchCoverage; + MCDCCoverageInfo MCDCCoverage; FunctionCoverageInfo FunctionCoverage; FunctionCoverageInfo InstantiationCoverage; @@ -229,6 +273,7 @@ LineCoverage += RHS.LineCoverage; FunctionCoverage += RHS.FunctionCoverage; BranchCoverage += RHS.BranchCoverage; + MCDCCoverage += RHS.MCDCCoverage; InstantiationCoverage += RHS.InstantiationCoverage; return *this; } @@ -237,6 +282,7 @@ RegionCoverage += Function.RegionCoverage; LineCoverage += Function.LineCoverage; BranchCoverage += Function.BranchCoverage; + MCDCCoverage += Function.MCDCCoverage; FunctionCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0); } Index: llvm/tools/llvm-cov/CoverageSummaryInfo.cpp =================================================================== --- llvm/tools/llvm-cov/CoverageSummaryInfo.cpp +++ llvm/tools/llvm-cov/CoverageSummaryInfo.cpp @@ -44,6 +44,17 @@ } } +static void sumMCDCPairs(size_t &NumPairs, size_t &CoveredPairs, + const ArrayRef &Records) { + for (const auto &Record : Records) + for (unsigned c = 0; c < Record.getNumConditions(); c++) { + if (!Record.isCondFolded(c)) + ++NumPairs; + if (Record.isCondIndepPairCovered(c)) + ++CoveredPairs; + } +} + FunctionCoverageSummary FunctionCoverageSummary::get(const CoverageMapping &CM, const coverage::FunctionRecord &Function) { @@ -73,11 +84,15 @@ sumBranches(NumBranches, CoveredBranches, CD.getBranches()); sumBranchExpansions(NumBranches, CoveredBranches, CM, CD.getExpansions()); + size_t NumPairs = 0, CoveredPairs = 0; + sumMCDCPairs(NumPairs, CoveredPairs, CD.getMCDCRecords()); + return FunctionCoverageSummary( Function.Name, Function.ExecutionCount, RegionCoverageInfo(CoveredRegions, NumCodeRegions), LineCoverageInfo(CoveredLines, NumLines), - BranchCoverageInfo(CoveredBranches, NumBranches)); + BranchCoverageInfo(CoveredBranches, NumBranches), + MCDCCoverageInfo(CoveredPairs, NumPairs)); } FunctionCoverageSummary @@ -97,10 +112,12 @@ Summary.RegionCoverage = Summaries[0].RegionCoverage; Summary.LineCoverage = Summaries[0].LineCoverage; Summary.BranchCoverage = Summaries[0].BranchCoverage; + Summary.MCDCCoverage = Summaries[0].MCDCCoverage; for (const auto &FCS : Summaries.drop_front()) { Summary.RegionCoverage.merge(FCS.RegionCoverage); Summary.LineCoverage.merge(FCS.LineCoverage); Summary.BranchCoverage.merge(FCS.BranchCoverage); + Summary.MCDCCoverage.merge(FCS.MCDCCoverage); } return Summary; } Index: llvm/tools/llvm-cov/CoverageViewOptions.h =================================================================== --- llvm/tools/llvm-cov/CoverageViewOptions.h +++ llvm/tools/llvm-cov/CoverageViewOptions.h @@ -30,12 +30,14 @@ bool ShowLineNumbers; bool ShowLineStats; bool ShowRegionMarkers; + bool ShowMCDC; bool ShowBranchCounts; bool ShowBranchPercents; bool ShowExpandedRegions; bool ShowFunctionInstantiations; bool ShowFullFilenames; bool ShowBranchSummary; + bool ShowMCDCSummary; bool ShowRegionSummary; bool ShowInstantiationSummary; bool ExportSummaryOnly; Index: llvm/tools/llvm-cov/SourceCoverageView.h =================================================================== --- llvm/tools/llvm-cov/SourceCoverageView.h +++ llvm/tools/llvm-cov/SourceCoverageView.h @@ -84,6 +84,23 @@ } }; +/// A view that represents one or more MCDC regions on a given source line. +struct MCDCView { + std::vector Records; + std::unique_ptr View; + unsigned Line; + + MCDCView(unsigned Line, ArrayRef Records, + std::unique_ptr View) + : Records(Records), View(std::move(View)), Line(Line) {} + + unsigned getLine() const { return Line; } + + friend bool operator<(const MCDCView &LHS, const MCDCView &RHS) { + return LHS.Line < RHS.Line; + } +}; + /// A file manager that handles format-aware file creation. class CoveragePrinter { public: @@ -160,6 +177,9 @@ /// A container for all branches in the source on display. std::vector BranchSubViews; + /// A container for all MCDC records in the source on display. + std::vector MCDCSubViews; + /// A container for all instantiations (e.g template functions) in the source /// on display. std::vector InstantiationSubViews; @@ -233,6 +253,10 @@ virtual void renderBranchView(raw_ostream &OS, BranchView &BRV, unsigned ViewDepth) = 0; + /// Render an MCDC view. + virtual void renderMCDCView(raw_ostream &OS, MCDCView &BRV, + unsigned ViewDepth) = 0; + /// Render \p Title, a project title if one is available, and the /// created time. virtual void renderTitle(raw_ostream &OS, StringRef CellText) = 0; @@ -283,6 +307,10 @@ void addBranch(unsigned Line, ArrayRef Regions, std::unique_ptr View); + /// Add an MCDC subview to this view. + void addMCDCRecord(unsigned Line, ArrayRef Records, + std::unique_ptr View); + /// Print the code coverage information for a specific portion of a /// source file to the output stream. void print(raw_ostream &OS, bool WholeFile, bool ShowSourceName, Index: llvm/tools/llvm-cov/SourceCoverageView.cpp =================================================================== --- llvm/tools/llvm-cov/SourceCoverageView.cpp +++ llvm/tools/llvm-cov/SourceCoverageView.cpp @@ -174,6 +174,12 @@ BranchSubViews.emplace_back(Line, Regions, std::move(View)); } +void SourceCoverageView::addMCDCRecord( + unsigned Line, ArrayRef Records, + std::unique_ptr View) { + MCDCSubViews.emplace_back(Line, Records, std::move(View)); +} + void SourceCoverageView::addInstantiation( StringRef FunctionName, unsigned Line, std::unique_ptr View) { @@ -199,12 +205,15 @@ llvm::stable_sort(ExpansionSubViews); llvm::stable_sort(InstantiationSubViews); llvm::stable_sort(BranchSubViews); + llvm::stable_sort(MCDCSubViews); auto NextESV = ExpansionSubViews.begin(); auto EndESV = ExpansionSubViews.end(); auto NextISV = InstantiationSubViews.begin(); auto EndISV = InstantiationSubViews.end(); auto NextBRV = BranchSubViews.begin(); auto EndBRV = BranchSubViews.end(); + auto NextMSV = MCDCSubViews.begin(); + auto EndMSV = MCDCSubViews.end(); // Get the coverage information for the file. auto StartSegment = CoverageInfo.begin(); @@ -272,6 +281,11 @@ renderBranchView(OS, *NextBRV, ViewDepth + 1); RenderedSubView = true; } + for (; NextMSV != EndMSV && NextMSV->Line == LI.line_number(); ++NextMSV) { + renderViewDivider(OS, ViewDepth + 1); + renderMCDCView(OS, *NextMSV, ViewDepth + 1); + RenderedSubView = true; + } if (RenderedSubView) renderViewDivider(OS, ViewDepth + 1); renderLineSuffix(OS, ViewDepth); Index: llvm/tools/llvm-cov/SourceCoverageViewHTML.h =================================================================== --- llvm/tools/llvm-cov/SourceCoverageViewHTML.h +++ llvm/tools/llvm-cov/SourceCoverageViewHTML.h @@ -71,6 +71,9 @@ void renderBranchView(raw_ostream &OS, BranchView &BRV, unsigned ViewDepth) override; + void renderMCDCView(raw_ostream &OS, MCDCView &BRV, + unsigned ViewDepth) override; + void renderInstantiationView(raw_ostream &OS, InstantiationView &ISV, unsigned ViewDepth) override; Index: llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp =================================================================== --- llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp +++ llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp @@ -316,6 +316,8 @@ Columns.emplace_back(tag("td", "Region Coverage", "column-entry-bold")); if (Opts.ShowBranchSummary) Columns.emplace_back(tag("td", "Branch Coverage", "column-entry-bold")); + if (Opts.ShowMCDCSummary) + Columns.emplace_back(tag("td", "MC/DC", "column-entry-bold")); OS << tag("tr", join(Columns.begin(), Columns.end(), "")); } @@ -385,6 +387,10 @@ AddCoverageTripleToColumn(FCS.BranchCoverage.getCovered(), FCS.BranchCoverage.getNumBranches(), FCS.BranchCoverage.getPercentCovered()); + if (Opts.ShowMCDCSummary) + AddCoverageTripleToColumn(FCS.MCDCCoverage.getCoveredPairs(), + FCS.MCDCCoverage.getNumPairs(), + FCS.MCDCCoverage.getPercentCovered()); if (IsTotals) OS << tag("tr", join(Columns.begin(), Columns.end(), ""), "light-row-bold"); @@ -723,6 +729,52 @@ OS << EndExpansionDiv; } +void SourceCoverageViewHTML::renderMCDCView(raw_ostream &OS, MCDCView &MRV, + unsigned ViewDepth) { + for (auto &Record : MRV.Records) { + OS << BeginExpansionDiv; + OS << BeginPre; + OS << " MC/DC Decision Region ("; + + // Display Line + Column information. + const CounterMappingRegion &DecisionRegion = Record.getDecisionRegion(); + std::string LineNoStr = utostr(uint64_t(DecisionRegion.LineStart)); + std::string ColNoStr = utostr(uint64_t(DecisionRegion.ColumnStart)); + std::string TargetName = "L" + LineNoStr; + OS << tag("span", + a("#" + TargetName, tag("span", LineNoStr + ":" + ColNoStr), + TargetName), + "line-number") + + ") to ("; + LineNoStr = utostr(uint64_t(DecisionRegion.LineEnd)); + ColNoStr = utostr(uint64_t(DecisionRegion.ColumnEnd)); + OS << tag("span", + a("#" + TargetName, tag("span", LineNoStr + ":" + ColNoStr), + TargetName), + "line-number") + + ")\n\n"; + + // Display MC/DC Information. + OS << " Number of Conditions: " << Record.getNumConditions() << "\n"; + for (unsigned i = 0; i < Record.getNumConditions(); i++) { + OS << " " << Record.getConditionHdrStr(i); + } + OS << "\n"; + OS << " Executed MC/DC Test Vectors:\n\n "; + OS << Record.getTestVectorHdrStr(); + for (unsigned i = 0; i < Record.getNumTestVectors(); i++) + OS << Record.getTestVectorStr(i); + OS << "\n"; + for (unsigned i = 0; i < Record.getNumConditions(); i++) + OS << Record.getCondCoverageStr(i); + OS << " MC/DC Coverage for Expression: "; + OS << format("%0.2f", Record.getPercentCovered()) << "%\n"; + OS << EndPre; + OS << EndExpansionDiv; + } + return; +} + void SourceCoverageViewHTML::renderInstantiationView(raw_ostream &OS, InstantiationView &ISV, unsigned ViewDepth) { Index: llvm/tools/llvm-cov/SourceCoverageViewText.h =================================================================== --- llvm/tools/llvm-cov/SourceCoverageViewText.h +++ llvm/tools/llvm-cov/SourceCoverageViewText.h @@ -62,6 +62,9 @@ void renderBranchView(raw_ostream &OS, BranchView &BRV, unsigned ViewDepth) override; + void renderMCDCView(raw_ostream &OS, MCDCView &BRV, + unsigned ViewDepth) override; + void renderInstantiationView(raw_ostream &OS, InstantiationView &ISV, unsigned ViewDepth) override; Index: llvm/tools/llvm-cov/SourceCoverageViewText.cpp =================================================================== --- llvm/tools/llvm-cov/SourceCoverageViewText.cpp +++ llvm/tools/llvm-cov/SourceCoverageViewText.cpp @@ -271,6 +271,57 @@ } } +void SourceCoverageViewText::renderMCDCView(raw_ostream &OS, MCDCView &MRV, + unsigned ViewDepth) { + for (auto &Record : MRV.Records) { + renderLinePrefix(OS, ViewDepth); + OS << "---> MC/DC Decision Region ("; + // Display Line + Column information. + const CounterMappingRegion &DecisionRegion = Record.getDecisionRegion(); + OS << DecisionRegion.LineStart << ":"; + OS << DecisionRegion.ColumnStart << ") to ("; + OS << DecisionRegion.LineEnd << ":"; + OS << DecisionRegion.ColumnEnd << ")\n"; + renderLinePrefix(OS, ViewDepth); + OS << "\n"; + + // Display MC/DC Information. + renderLinePrefix(OS, ViewDepth); + OS << " Number of Conditions: " << Record.getNumConditions() << "\n"; + for (unsigned i = 0; i < Record.getNumConditions(); i++) { + renderLinePrefix(OS, ViewDepth); + OS << " " << Record.getConditionHdrStr(i); + } + renderLinePrefix(OS, ViewDepth); + OS << "\n"; + renderLinePrefix(OS, ViewDepth); + OS << " Executed MC/DC Test Vectors:\n"; + renderLinePrefix(OS, ViewDepth); + OS << "\n"; + renderLinePrefix(OS, ViewDepth); + OS << " "; + OS << Record.getTestVectorHdrStr(); + for (unsigned i = 0; i < Record.getNumTestVectors(); i++) { + renderLinePrefix(OS, ViewDepth); + OS << Record.getTestVectorStr(i); + } + renderLinePrefix(OS, ViewDepth); + OS << "\n"; + for (unsigned i = 0; i < Record.getNumConditions(); i++) { + renderLinePrefix(OS, ViewDepth); + OS << Record.getCondCoverageStr(i); + } + renderLinePrefix(OS, ViewDepth); + OS << " MC/DC Coverage for Decision: "; + colored_ostream(OS, raw_ostream::RED, + getOptions().Colors && Record.getPercentCovered() < 100.0, + /*Bold=*/false, /*BG=*/true) + << format("%0.2f", Record.getPercentCovered()) << "%\n"; + renderLinePrefix(OS, ViewDepth); + OS << "\n"; + } +} + void SourceCoverageViewText::renderInstantiationView(raw_ostream &OS, InstantiationView &ISV, unsigned ViewDepth) { Index: llvm/unittests/ProfileData/CoverageMappingTest.cpp =================================================================== --- llvm/unittests/ProfileData/CoverageMappingTest.cpp +++ llvm/unittests/ProfileData/CoverageMappingTest.cpp @@ -184,6 +184,23 @@ : CounterMappingRegion::makeRegion(C, FileID, LS, CS, LE, CE)); } + void addMCDCDecisionCMR(unsigned Mask, unsigned NC, StringRef File, + unsigned LS, unsigned CS, unsigned LE, unsigned CE) { + auto &Regions = InputFunctions.back().Regions; + unsigned FileID = getFileIndexForFunction(File); + Regions.push_back(CounterMappingRegion::makeDecisionRegion(Mask, NC, + FileID, LS, CS, LE, CE)); + } + + void addMCDCBranchCMR(Counter C1, Counter C2, unsigned ID, unsigned TrueID, + unsigned FalseID, StringRef File, unsigned LS, + unsigned CS, unsigned LE, unsigned CE) { + auto &Regions = InputFunctions.back().Regions; + unsigned FileID = getFileIndexForFunction(File); + Regions.push_back(CounterMappingRegion::makeBranchRegion(C1, C2, ID, + TrueID, FalseID, FileID, LS, CS, LE, CE)); + } + void addExpansionCMR(StringRef File, StringRef ExpandedFile, unsigned LS, unsigned CS, unsigned LE, unsigned CE) { InputFunctions.back().Regions.push_back(CounterMappingRegion::makeExpansion( @@ -824,6 +841,33 @@ ASSERT_EQ(1U, Names.size()); } +// Test that MCDC bitmasks not associated with any code regions are allowed. +TEST_P(CoverageMappingTest, non_code_region_bitmask) { + // No records in profdata + + startFunction("func", 0x1234); + addCMR(Counter::getCounter(0), "file", 1, 1, 5, 5); + addCMR(Counter::getCounter(1), "file", 1, 1, 5, 5); + addCMR(Counter::getCounter(2), "file", 1, 1, 5, 5); + addCMR(Counter::getCounter(3), "file", 1, 1, 5, 5); + + addMCDCDecisionCMR(0, 2, "file", 7, 1, 7, 6); + addMCDCBranchCMR(Counter::getCounter(0), Counter::getCounter(1), 1, 2, 0, + "file", 7, 2, 7, 3); + addMCDCBranchCMR(Counter::getCounter(2), Counter::getCounter(3), 2, 0, 0, + "file", 7, 4, 7, 5); + + EXPECT_THAT_ERROR(loadCoverageMapping(), Succeeded()); + + std::vector Names; + for (const auto &Func : LoadedCoverage->getCoveredFunctions()) { + Names.push_back(Func.Name); + ASSERT_EQ(2U, Func.CountedBranchRegions.size()); + ASSERT_EQ(1U, Func.MCDCRecords.size()); + } + ASSERT_EQ(1U, Names.size()); +} + TEST_P(CoverageMappingTest, strip_filename_prefix) { ProfileWriter.addRecord({"file1:func", 0x1234, {0}}, Err);