2014-08-05 02:41:51 +08:00
|
|
|
//===--- CoverageMappingGen.cpp - Coverage mapping generation ---*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Instrumentation-based code coverage mapping generator
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CoverageMappingGen.h"
|
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
2016-01-29 01:52:18 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2016-01-22 03:25:35 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2015-02-19 05:24:51 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2016-04-30 02:53:16 +08:00
|
|
|
#include "llvm/ProfileData/Coverage/CoverageMapping.h"
|
|
|
|
#include "llvm/ProfileData/Coverage/CoverageMappingReader.h"
|
|
|
|
#include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
|
2015-01-14 19:29:14 +08:00
|
|
|
#include "llvm/ProfileData/InstrProfReader.h"
|
2014-08-05 02:41:51 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2016-07-19 05:01:27 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2014-08-05 02:41:51 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
using namespace llvm::coverage;
|
|
|
|
|
2017-09-12 04:47:42 +08:00
|
|
|
void CoverageSourceInfo::SourceRangeSkipped(SourceRange Range, SourceLocation) {
|
2014-08-05 02:41:51 +08:00
|
|
|
SkippedRanges.push_back(Range);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// \brief A region of source code that can be mapped to a counter.
|
2014-10-01 11:33:49 +08:00
|
|
|
class SourceMappingRegion {
|
2014-08-05 02:41:51 +08:00
|
|
|
Counter Count;
|
|
|
|
|
|
|
|
/// \brief The region's starting location.
|
2015-02-19 05:24:51 +08:00
|
|
|
Optional<SourceLocation> LocStart;
|
2014-08-05 02:41:51 +08:00
|
|
|
|
|
|
|
/// \brief The region's ending location.
|
2015-02-19 05:24:51 +08:00
|
|
|
Optional<SourceLocation> LocEnd;
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2017-09-09 02:44:56 +08:00
|
|
|
/// Whether this region should be emitted after its parent is emitted.
|
|
|
|
bool DeferRegion;
|
|
|
|
|
2017-09-19 07:37:30 +08:00
|
|
|
/// Whether this region is a gap region. The count from a gap region is set
|
|
|
|
/// as the line execution count if there are no other regions on the line.
|
|
|
|
bool GapRegion;
|
|
|
|
|
2014-10-01 11:33:49 +08:00
|
|
|
public:
|
2015-02-19 05:24:51 +08:00
|
|
|
SourceMappingRegion(Counter Count, Optional<SourceLocation> LocStart,
|
2017-09-19 07:37:30 +08:00
|
|
|
Optional<SourceLocation> LocEnd, bool DeferRegion = false,
|
|
|
|
bool GapRegion = false)
|
2017-09-09 02:44:56 +08:00
|
|
|
: Count(Count), LocStart(LocStart), LocEnd(LocEnd),
|
2017-09-19 07:37:30 +08:00
|
|
|
DeferRegion(DeferRegion), GapRegion(GapRegion) {}
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2014-10-01 11:33:49 +08:00
|
|
|
const Counter &getCounter() const { return Count; }
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
void setCounter(Counter C) { Count = C; }
|
2014-10-01 11:33:49 +08:00
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
bool hasStartLoc() const { return LocStart.hasValue(); }
|
2014-10-01 11:33:49 +08:00
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
void setStartLoc(SourceLocation Loc) { LocStart = Loc; }
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2015-09-26 13:10:14 +08:00
|
|
|
SourceLocation getStartLoc() const {
|
2015-02-19 05:24:51 +08:00
|
|
|
assert(LocStart && "Region has no start location");
|
|
|
|
return *LocStart;
|
|
|
|
}
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
bool hasEndLoc() const { return LocEnd.hasValue(); }
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
void setEndLoc(SourceLocation Loc) { LocEnd = Loc; }
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2015-09-26 13:10:14 +08:00
|
|
|
SourceLocation getEndLoc() const {
|
2015-02-19 05:24:51 +08:00
|
|
|
assert(LocEnd && "Region has no end location");
|
|
|
|
return *LocEnd;
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
2017-09-09 02:44:56 +08:00
|
|
|
|
|
|
|
bool isDeferred() const { return DeferRegion; }
|
|
|
|
|
|
|
|
void setDeferred(bool Deferred) { DeferRegion = Deferred; }
|
2017-09-19 07:37:30 +08:00
|
|
|
|
|
|
|
bool isGap() const { return GapRegion; }
|
|
|
|
|
|
|
|
void setGap(bool Gap) { GapRegion = Gap; }
|
2014-08-05 02:41:51 +08:00
|
|
|
};
|
|
|
|
|
2017-07-27 10:20:25 +08:00
|
|
|
/// Spelling locations for the start and end of a source region.
|
|
|
|
struct SpellingRegion {
|
|
|
|
/// The line where the region starts.
|
|
|
|
unsigned LineStart;
|
|
|
|
|
|
|
|
/// The column where the region starts.
|
|
|
|
unsigned ColumnStart;
|
|
|
|
|
|
|
|
/// The line where the region ends.
|
|
|
|
unsigned LineEnd;
|
|
|
|
|
|
|
|
/// The column where the region ends.
|
|
|
|
unsigned ColumnEnd;
|
|
|
|
|
|
|
|
SpellingRegion(SourceManager &SM, SourceLocation LocStart,
|
|
|
|
SourceLocation LocEnd) {
|
|
|
|
LineStart = SM.getSpellingLineNumber(LocStart);
|
|
|
|
ColumnStart = SM.getSpellingColumnNumber(LocStart);
|
|
|
|
LineEnd = SM.getSpellingLineNumber(LocEnd);
|
|
|
|
ColumnEnd = SM.getSpellingColumnNumber(LocEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if the start and end locations appear in source order, i.e
|
|
|
|
/// top->bottom, left->right.
|
|
|
|
bool isInSourceOrder() const {
|
|
|
|
return (LineStart < LineEnd) ||
|
|
|
|
(LineStart == LineEnd && ColumnStart <= ColumnEnd);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
/// \brief Provides the common functionality for the different
|
|
|
|
/// coverage mapping region builders.
|
|
|
|
class CoverageMappingBuilder {
|
|
|
|
public:
|
|
|
|
CoverageMappingModuleGen &CVM;
|
|
|
|
SourceManager &SM;
|
|
|
|
const LangOptions &LangOpts;
|
|
|
|
|
|
|
|
private:
|
2015-02-19 05:24:51 +08:00
|
|
|
/// \brief Map of clang's FileIDs to IDs used for coverage mapping.
|
|
|
|
llvm::SmallDenseMap<FileID, std::pair<unsigned, SourceLocation>, 8>
|
|
|
|
FileIDMapping;
|
2014-08-05 02:41:51 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// \brief The coverage mapping regions for this function
|
|
|
|
llvm::SmallVector<CounterMappingRegion, 32> MappingRegions;
|
|
|
|
/// \brief The source mapping regions for this function.
|
2014-10-01 11:33:52 +08:00
|
|
|
std::vector<SourceMappingRegion> SourceRegions;
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2016-08-31 15:04:16 +08:00
|
|
|
/// \brief A set of regions which can be used as a filter.
|
|
|
|
///
|
|
|
|
/// It is produced by emitExpansionRegions() and is used in
|
|
|
|
/// emitSourceRegions() to suppress producing code regions if
|
|
|
|
/// the same area is covered by expansion regions.
|
|
|
|
typedef llvm::SmallSet<std::pair<SourceLocation, SourceLocation>, 8>
|
|
|
|
SourceRegionFilter;
|
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
CoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
|
|
|
|
const LangOptions &LangOpts)
|
2015-02-19 05:24:51 +08:00
|
|
|
: CVM(CVM), SM(SM), LangOpts(LangOpts) {}
|
2014-08-05 02:41:51 +08:00
|
|
|
|
|
|
|
/// \brief Return the precise end location for the given token.
|
|
|
|
SourceLocation getPreciseTokenLocEnd(SourceLocation Loc) {
|
2015-02-19 05:24:51 +08:00
|
|
|
// We avoid getLocForEndOfToken here, because it doesn't do what we want for
|
|
|
|
// macro locations, which we just treat as expanded files.
|
|
|
|
unsigned TokLen =
|
|
|
|
Lexer::MeasureTokenLength(SM.getSpellingLoc(Loc), SM, LangOpts);
|
|
|
|
return Loc.getLocWithOffset(TokLen);
|
|
|
|
}
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
/// \brief Return the start location of an included file or expanded macro.
|
|
|
|
SourceLocation getStartOfFileOrMacro(SourceLocation Loc) {
|
|
|
|
if (Loc.isMacroID())
|
|
|
|
return Loc.getLocWithOffset(-SM.getFileOffset(Loc));
|
|
|
|
return SM.getLocForStartOfFile(SM.getFileID(Loc));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Return the end location of an included file or expanded macro.
|
|
|
|
SourceLocation getEndOfFileOrMacro(SourceLocation Loc) {
|
|
|
|
if (Loc.isMacroID())
|
|
|
|
return Loc.getLocWithOffset(SM.getFileIDSize(SM.getFileID(Loc)) -
|
2015-03-25 12:13:49 +08:00
|
|
|
SM.getFileOffset(Loc));
|
2015-02-19 05:24:51 +08:00
|
|
|
return SM.getLocForEndOfFile(SM.getFileID(Loc));
|
|
|
|
}
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
/// \brief Find out where the current file is included or macro is expanded.
|
|
|
|
SourceLocation getIncludeOrExpansionLoc(SourceLocation Loc) {
|
|
|
|
return Loc.isMacroID() ? SM.getImmediateExpansionRange(Loc).first
|
|
|
|
: SM.getIncludeLoc(SM.getFileID(Loc));
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
2015-05-15 06:14:10 +08:00
|
|
|
/// \brief Return true if \c Loc is a location in a built-in macro.
|
|
|
|
bool isInBuiltin(SourceLocation Loc) {
|
2016-10-02 00:38:28 +08:00
|
|
|
return SM.getBufferName(SM.getSpellingLoc(Loc)) == "<built-in>";
|
2015-05-15 06:14:10 +08:00
|
|
|
}
|
|
|
|
|
2016-06-07 18:07:51 +08:00
|
|
|
/// \brief Check whether \c Loc is included or expanded from \c Parent.
|
|
|
|
bool isNestedIn(SourceLocation Loc, FileID Parent) {
|
|
|
|
do {
|
|
|
|
Loc = getIncludeOrExpansionLoc(Loc);
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
return false;
|
|
|
|
} while (!SM.isInFileID(Loc, Parent));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-15 06:14:10 +08:00
|
|
|
/// \brief Get the start of \c S ignoring macro arguments and builtin macros.
|
2015-02-19 05:24:51 +08:00
|
|
|
SourceLocation getStart(const Stmt *S) {
|
|
|
|
SourceLocation Loc = S->getLocStart();
|
2015-05-15 06:14:10 +08:00
|
|
|
while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
|
2015-02-19 05:24:51 +08:00
|
|
|
Loc = SM.getImmediateExpansionRange(Loc).first;
|
|
|
|
return Loc;
|
|
|
|
}
|
|
|
|
|
2015-05-15 06:14:10 +08:00
|
|
|
/// \brief Get the end of \c S ignoring macro arguments and builtin macros.
|
2015-02-19 05:24:51 +08:00
|
|
|
SourceLocation getEnd(const Stmt *S) {
|
|
|
|
SourceLocation Loc = S->getLocEnd();
|
2015-05-15 06:14:10 +08:00
|
|
|
while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
|
2015-02-19 05:24:51 +08:00
|
|
|
Loc = SM.getImmediateExpansionRange(Loc).first;
|
2015-03-25 12:13:49 +08:00
|
|
|
return getPreciseTokenLocEnd(Loc);
|
2015-02-19 05:24:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Find the set of files we have regions for and assign IDs
|
|
|
|
///
|
|
|
|
/// Fills \c Mapping with the virtual file mapping needed to write out
|
|
|
|
/// coverage and collects the necessary file information to emit source and
|
|
|
|
/// expansion regions.
|
|
|
|
void gatherFileIDs(SmallVectorImpl<unsigned> &Mapping) {
|
|
|
|
FileIDMapping.clear();
|
|
|
|
|
2016-01-29 01:52:18 +08:00
|
|
|
llvm::SmallSet<FileID, 8> Visited;
|
2015-02-19 05:24:51 +08:00
|
|
|
SmallVector<std::pair<SourceLocation, unsigned>, 8> FileLocs;
|
|
|
|
for (const auto &Region : SourceRegions) {
|
|
|
|
SourceLocation Loc = Region.getStartLoc();
|
|
|
|
FileID File = SM.getFileID(Loc);
|
2016-01-29 01:52:18 +08:00
|
|
|
if (!Visited.insert(File).second)
|
2015-02-19 05:24:51 +08:00
|
|
|
continue;
|
|
|
|
|
2016-07-12 06:57:46 +08:00
|
|
|
// Do not map FileID's associated with system headers.
|
|
|
|
if (SM.isInSystemHeader(SM.getSpellingLoc(Loc)))
|
|
|
|
continue;
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
unsigned Depth = 0;
|
|
|
|
for (SourceLocation Parent = getIncludeOrExpansionLoc(Loc);
|
2015-10-03 13:15:57 +08:00
|
|
|
Parent.isValid(); Parent = getIncludeOrExpansionLoc(Parent))
|
2015-02-19 05:24:51 +08:00
|
|
|
++Depth;
|
|
|
|
FileLocs.push_back(std::make_pair(Loc, Depth));
|
|
|
|
}
|
|
|
|
std::stable_sort(FileLocs.begin(), FileLocs.end(), llvm::less_second());
|
|
|
|
|
|
|
|
for (const auto &FL : FileLocs) {
|
|
|
|
SourceLocation Loc = FL.first;
|
|
|
|
FileID SpellingFile = SM.getDecomposedSpellingLoc(Loc).first;
|
|
|
|
auto Entry = SM.getFileEntryForID(SpellingFile);
|
|
|
|
if (!Entry)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
FileIDMapping[SM.getFileID(Loc)] = std::make_pair(Mapping.size(), Loc);
|
|
|
|
Mapping.push_back(CVM.getFileID(Entry));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Get the coverage mapping file ID for \c Loc.
|
|
|
|
///
|
|
|
|
/// If such file id doesn't exist, return None.
|
|
|
|
Optional<unsigned> getCoverageFileID(SourceLocation Loc) {
|
|
|
|
auto Mapping = FileIDMapping.find(SM.getFileID(Loc));
|
|
|
|
if (Mapping != FileIDMapping.end())
|
|
|
|
return Mapping->second.first;
|
|
|
|
return None;
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Gather all the regions that were skipped by the preprocessor
|
|
|
|
/// using the constructs like #if.
|
|
|
|
void gatherSkippedRegions() {
|
|
|
|
/// An array of the minimum lineStarts and the maximum lineEnds
|
|
|
|
/// for mapping regions from the appropriate source files.
|
|
|
|
llvm::SmallVector<std::pair<unsigned, unsigned>, 8> FileLineRanges;
|
|
|
|
FileLineRanges.resize(
|
|
|
|
FileIDMapping.size(),
|
|
|
|
std::make_pair(std::numeric_limits<unsigned>::max(), 0));
|
|
|
|
for (const auto &R : MappingRegions) {
|
|
|
|
FileLineRanges[R.FileID].first =
|
|
|
|
std::min(FileLineRanges[R.FileID].first, R.LineStart);
|
|
|
|
FileLineRanges[R.FileID].second =
|
|
|
|
std::max(FileLineRanges[R.FileID].second, R.LineEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto SkippedRanges = CVM.getSourceInfo().getSkippedRanges();
|
|
|
|
for (const auto &I : SkippedRanges) {
|
|
|
|
auto LocStart = I.getBegin();
|
|
|
|
auto LocEnd = I.getEnd();
|
2015-02-19 05:24:51 +08:00
|
|
|
assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
|
|
|
|
"region spans multiple files");
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
auto CovFileID = getCoverageFileID(LocStart);
|
2015-01-25 04:22:32 +08:00
|
|
|
if (!CovFileID)
|
2014-08-05 02:41:51 +08:00
|
|
|
continue;
|
2017-07-27 10:20:25 +08:00
|
|
|
SpellingRegion SR{SM, LocStart, LocEnd};
|
2015-02-04 07:59:48 +08:00
|
|
|
auto Region = CounterMappingRegion::makeSkipped(
|
2017-07-27 10:20:25 +08:00
|
|
|
*CovFileID, SR.LineStart, SR.ColumnStart, SR.LineEnd, SR.ColumnEnd);
|
2014-08-05 02:41:51 +08:00
|
|
|
// Make sure that we only collect the regions that are inside
|
|
|
|
// the souce code of this function.
|
2015-01-25 04:22:32 +08:00
|
|
|
if (Region.LineStart >= FileLineRanges[*CovFileID].first &&
|
|
|
|
Region.LineEnd <= FileLineRanges[*CovFileID].second)
|
2014-08-05 02:41:51 +08:00
|
|
|
MappingRegions.push_back(Region);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Generate the coverage counter mapping regions from collected
|
|
|
|
/// source regions.
|
2016-08-31 15:04:16 +08:00
|
|
|
void emitSourceRegions(const SourceRegionFilter &Filter) {
|
2015-02-19 05:24:51 +08:00
|
|
|
for (const auto &Region : SourceRegions) {
|
|
|
|
assert(Region.hasEndLoc() && "incomplete region");
|
|
|
|
|
|
|
|
SourceLocation LocStart = Region.getStartLoc();
|
2015-10-03 18:46:20 +08:00
|
|
|
assert(SM.getFileID(LocStart).isValid() && "region in invalid file");
|
2015-02-19 05:24:51 +08:00
|
|
|
|
2016-07-12 06:57:46 +08:00
|
|
|
// Ignore regions from system headers.
|
|
|
|
if (SM.isInSystemHeader(SM.getSpellingLoc(LocStart)))
|
|
|
|
continue;
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
auto CovFileID = getCoverageFileID(LocStart);
|
|
|
|
// Ignore regions that don't have a file, such as builtin macros.
|
|
|
|
if (!CovFileID)
|
2014-08-05 02:41:51 +08:00
|
|
|
continue;
|
|
|
|
|
2015-03-25 12:13:49 +08:00
|
|
|
SourceLocation LocEnd = Region.getEndLoc();
|
2015-02-19 05:24:51 +08:00
|
|
|
assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
|
|
|
|
"region spans multiple files");
|
|
|
|
|
2016-08-31 15:04:16 +08:00
|
|
|
// Don't add code regions for the area covered by expansion regions.
|
|
|
|
// This not only suppresses redundant regions, but sometimes prevents
|
|
|
|
// creating regions with wrong counters if, for example, a statement's
|
|
|
|
// body ends at the end of a nested macro.
|
|
|
|
if (Filter.count(std::make_pair(LocStart, LocEnd)))
|
|
|
|
continue;
|
|
|
|
|
2017-07-27 10:20:25 +08:00
|
|
|
// Find the spelling locations for the mapping region.
|
|
|
|
SpellingRegion SR{SM, LocStart, LocEnd};
|
|
|
|
assert(SR.isInSourceOrder() && "region start and end out of order");
|
2017-09-19 07:37:30 +08:00
|
|
|
|
|
|
|
if (Region.isGap()) {
|
|
|
|
MappingRegions.push_back(CounterMappingRegion::makeGapRegion(
|
|
|
|
Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart,
|
|
|
|
SR.LineEnd, SR.ColumnEnd));
|
|
|
|
} else {
|
|
|
|
MappingRegions.push_back(CounterMappingRegion::makeRegion(
|
|
|
|
Region.getCounter(), *CovFileID, SR.LineStart, SR.ColumnStart,
|
|
|
|
SR.LineEnd, SR.ColumnEnd));
|
|
|
|
}
|
2015-02-19 05:24:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Generate expansion regions for each virtual file we've seen.
|
2016-08-31 15:04:16 +08:00
|
|
|
SourceRegionFilter emitExpansionRegions() {
|
|
|
|
SourceRegionFilter Filter;
|
2015-02-19 05:24:51 +08:00
|
|
|
for (const auto &FM : FileIDMapping) {
|
|
|
|
SourceLocation ExpandedLoc = FM.second.second;
|
|
|
|
SourceLocation ParentLoc = getIncludeOrExpansionLoc(ExpandedLoc);
|
|
|
|
if (ParentLoc.isInvalid())
|
2014-08-05 02:41:51 +08:00
|
|
|
continue;
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
auto ParentFileID = getCoverageFileID(ParentLoc);
|
|
|
|
if (!ParentFileID)
|
|
|
|
continue;
|
|
|
|
auto ExpandedFileID = getCoverageFileID(ExpandedLoc);
|
|
|
|
assert(ExpandedFileID && "expansion in uncovered file");
|
|
|
|
|
|
|
|
SourceLocation LocEnd = getPreciseTokenLocEnd(ParentLoc);
|
|
|
|
assert(SM.isWrittenInSameFile(ParentLoc, LocEnd) &&
|
|
|
|
"region spans multiple files");
|
2016-08-31 15:04:16 +08:00
|
|
|
Filter.insert(std::make_pair(ParentLoc, LocEnd));
|
2015-02-19 05:24:51 +08:00
|
|
|
|
2017-07-27 10:20:25 +08:00
|
|
|
SpellingRegion SR{SM, ParentLoc, LocEnd};
|
|
|
|
assert(SR.isInSourceOrder() && "region start and end out of order");
|
2015-02-19 05:24:51 +08:00
|
|
|
MappingRegions.push_back(CounterMappingRegion::makeExpansion(
|
2017-07-27 10:20:25 +08:00
|
|
|
*ParentFileID, *ExpandedFileID, SR.LineStart, SR.ColumnStart,
|
|
|
|
SR.LineEnd, SR.ColumnEnd));
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
2016-08-31 15:04:16 +08:00
|
|
|
return Filter;
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Creates unreachable coverage regions for the functions that
|
|
|
|
/// are not emitted.
|
|
|
|
struct EmptyCoverageMappingBuilder : public CoverageMappingBuilder {
|
|
|
|
EmptyCoverageMappingBuilder(CoverageMappingModuleGen &CVM, SourceManager &SM,
|
|
|
|
const LangOptions &LangOpts)
|
|
|
|
: CoverageMappingBuilder(CVM, SM, LangOpts) {}
|
|
|
|
|
|
|
|
void VisitDecl(const Decl *D) {
|
|
|
|
if (!D->hasBody())
|
|
|
|
return;
|
|
|
|
auto Body = D->getBody();
|
2016-06-07 18:07:51 +08:00
|
|
|
SourceLocation Start = getStart(Body);
|
|
|
|
SourceLocation End = getEnd(Body);
|
|
|
|
if (!SM.isWrittenInSameFile(Start, End)) {
|
|
|
|
// Walk up to find the common ancestor.
|
|
|
|
// Correct the locations accordingly.
|
|
|
|
FileID StartFileID = SM.getFileID(Start);
|
|
|
|
FileID EndFileID = SM.getFileID(End);
|
|
|
|
while (StartFileID != EndFileID && !isNestedIn(End, StartFileID)) {
|
|
|
|
Start = getIncludeOrExpansionLoc(Start);
|
|
|
|
assert(Start.isValid() &&
|
|
|
|
"Declaration start location not nested within a known region");
|
|
|
|
StartFileID = SM.getFileID(Start);
|
|
|
|
}
|
|
|
|
while (StartFileID != EndFileID) {
|
|
|
|
End = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(End));
|
|
|
|
assert(End.isValid() &&
|
|
|
|
"Declaration end location not nested within a known region");
|
|
|
|
EndFileID = SM.getFileID(End);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SourceRegions.emplace_back(Counter(), Start, End);
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Write the mapping data to the output stream
|
|
|
|
void write(llvm::raw_ostream &OS) {
|
|
|
|
SmallVector<unsigned, 16> FileIDMapping;
|
2015-02-19 05:24:51 +08:00
|
|
|
gatherFileIDs(FileIDMapping);
|
2016-08-31 15:04:16 +08:00
|
|
|
emitSourceRegions(SourceRegionFilter());
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2016-07-26 08:24:59 +08:00
|
|
|
if (MappingRegions.empty())
|
|
|
|
return;
|
|
|
|
|
2014-08-27 14:28:36 +08:00
|
|
|
CoverageMappingWriter Writer(FileIDMapping, None, MappingRegions);
|
2014-08-05 02:41:51 +08:00
|
|
|
Writer.write(OS);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief A StmtVisitor that creates coverage mapping regions which map
|
|
|
|
/// from the source code locations to the PGO counters.
|
|
|
|
struct CounterCoverageMappingBuilder
|
|
|
|
: public CoverageMappingBuilder,
|
|
|
|
public ConstStmtVisitor<CounterCoverageMappingBuilder> {
|
|
|
|
/// \brief The map of statements to count values.
|
|
|
|
llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
/// \brief A stack of currently live regions.
|
|
|
|
std::vector<SourceMappingRegion> RegionStack;
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2017-09-09 02:44:56 +08:00
|
|
|
/// The currently deferred region: its end location and count can be set once
|
|
|
|
/// its parent has been popped from the region stack.
|
|
|
|
Optional<SourceMappingRegion> DeferredRegion;
|
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
CounterExpressionBuilder Builder;
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
/// \brief A location in the most recently visited file or macro.
|
|
|
|
///
|
|
|
|
/// This is used to adjust the active source regions appropriately when
|
|
|
|
/// expressions cross file or macro boundaries.
|
|
|
|
SourceLocation MostRecentLocation;
|
|
|
|
|
|
|
|
/// \brief Return a counter for the subtraction of \c RHS from \c LHS
|
2014-08-05 02:41:51 +08:00
|
|
|
Counter subtractCounters(Counter LHS, Counter RHS) {
|
|
|
|
return Builder.subtract(LHS, RHS);
|
|
|
|
}
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
/// \brief Return a counter for the sum of \c LHS and \c RHS.
|
2014-08-05 02:41:51 +08:00
|
|
|
Counter addCounters(Counter LHS, Counter RHS) {
|
|
|
|
return Builder.add(LHS, RHS);
|
|
|
|
}
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
Counter addCounters(Counter C1, Counter C2, Counter C3) {
|
|
|
|
return addCounters(addCounters(C1, C2), C3);
|
|
|
|
}
|
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
/// \brief Return the region counter for the given statement.
|
2015-02-19 05:24:51 +08:00
|
|
|
///
|
2014-08-05 02:41:51 +08:00
|
|
|
/// This should only be called on statements that have a dedicated counter.
|
2015-02-19 05:24:51 +08:00
|
|
|
Counter getRegionCounter(const Stmt *S) {
|
|
|
|
return Counter::getCounter(CounterMap[S]);
|
|
|
|
}
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
/// \brief Push a region onto the stack.
|
|
|
|
///
|
|
|
|
/// 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<SourceLocation> StartLoc = None,
|
|
|
|
Optional<SourceLocation> EndLoc = None) {
|
2017-09-09 02:44:56 +08:00
|
|
|
if (StartLoc) {
|
2015-02-19 05:24:51 +08:00
|
|
|
MostRecentLocation = *StartLoc;
|
2017-09-09 02:44:56 +08:00
|
|
|
completeDeferred(Count, MostRecentLocation);
|
|
|
|
}
|
2015-02-19 05:24:51 +08:00
|
|
|
RegionStack.emplace_back(Count, StartLoc, EndLoc);
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
return RegionStack.size() - 1;
|
|
|
|
}
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2017-09-09 02:44:56 +08:00
|
|
|
/// Complete any pending deferred region by setting its end location and
|
|
|
|
/// count, and then pushing it onto the region stack.
|
|
|
|
size_t completeDeferred(Counter Count, SourceLocation DeferredEndLoc) {
|
|
|
|
size_t Index = RegionStack.size();
|
|
|
|
if (!DeferredRegion)
|
|
|
|
return Index;
|
|
|
|
|
|
|
|
// Consume the pending region.
|
|
|
|
SourceMappingRegion DR = DeferredRegion.getValue();
|
|
|
|
DeferredRegion = None;
|
|
|
|
|
|
|
|
// If the region ends in an expansion, find the expansion site.
|
|
|
|
if (SM.getFileID(DeferredEndLoc) != SM.getMainFileID()) {
|
|
|
|
FileID StartFile = SM.getFileID(DR.getStartLoc());
|
|
|
|
if (isNestedIn(DeferredEndLoc, StartFile)) {
|
|
|
|
do {
|
|
|
|
DeferredEndLoc = getIncludeOrExpansionLoc(DeferredEndLoc);
|
|
|
|
} while (StartFile != SM.getFileID(DeferredEndLoc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The parent of this deferred region ends where the containing decl ends,
|
|
|
|
// so the region isn't useful.
|
|
|
|
if (DR.getStartLoc() == DeferredEndLoc)
|
|
|
|
return Index;
|
|
|
|
|
|
|
|
// If we're visiting statements in non-source order (e.g switch cases or
|
|
|
|
// a loop condition) we can't construct a sensible deferred region.
|
|
|
|
if (!SpellingRegion(SM, DR.getStartLoc(), DeferredEndLoc).isInSourceOrder())
|
|
|
|
return Index;
|
|
|
|
|
2017-09-19 07:37:30 +08:00
|
|
|
DR.setGap(true);
|
2017-09-09 02:44:56 +08:00
|
|
|
DR.setCounter(Count);
|
|
|
|
DR.setEndLoc(DeferredEndLoc);
|
|
|
|
handleFileExit(DeferredEndLoc);
|
|
|
|
RegionStack.push_back(DR);
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
/// \brief Pop regions from the stack into the function's list of regions.
|
|
|
|
///
|
|
|
|
/// Adds all regions from \c ParentIndex to the top of the stack to the
|
|
|
|
/// function's \c SourceRegions.
|
|
|
|
void popRegions(size_t ParentIndex) {
|
|
|
|
assert(RegionStack.size() >= ParentIndex && "parent not in stack");
|
2017-09-09 02:44:56 +08:00
|
|
|
bool ParentOfDeferredRegion = false;
|
2015-02-19 05:24:51 +08:00
|
|
|
while (RegionStack.size() > ParentIndex) {
|
|
|
|
SourceMappingRegion &Region = RegionStack.back();
|
|
|
|
if (Region.hasStartLoc()) {
|
|
|
|
SourceLocation StartLoc = Region.getStartLoc();
|
|
|
|
SourceLocation EndLoc = Region.hasEndLoc()
|
|
|
|
? Region.getEndLoc()
|
|
|
|
: RegionStack[ParentIndex].getEndLoc();
|
|
|
|
while (!SM.isWrittenInSameFile(StartLoc, EndLoc)) {
|
|
|
|
// The region ends in a nested file or macro expansion. Create a
|
|
|
|
// separate region for each expansion.
|
|
|
|
SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc);
|
|
|
|
assert(SM.isWrittenInSameFile(NestedLoc, EndLoc));
|
|
|
|
|
2016-08-29 19:48:50 +08:00
|
|
|
if (!isRegionAlreadyAdded(NestedLoc, EndLoc))
|
|
|
|
SourceRegions.emplace_back(Region.getCounter(), NestedLoc, EndLoc);
|
2015-02-19 05:24:51 +08:00
|
|
|
|
2015-03-25 12:13:49 +08:00
|
|
|
EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc));
|
2015-07-18 07:31:21 +08:00
|
|
|
if (EndLoc.isInvalid())
|
|
|
|
llvm::report_fatal_error("File exit not handled before popRegions");
|
2015-02-19 05:24:51 +08:00
|
|
|
}
|
|
|
|
Region.setEndLoc(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.getStartLoc(), EndLoc));
|
2015-09-26 13:10:16 +08:00
|
|
|
SourceRegions.push_back(Region);
|
2017-09-09 02:44:56 +08:00
|
|
|
|
|
|
|
if (ParentOfDeferredRegion) {
|
|
|
|
ParentOfDeferredRegion = false;
|
|
|
|
|
|
|
|
// If there's an existing deferred region, keep the old one, because
|
|
|
|
// it means there are two consecutive returns (or a similar pattern).
|
|
|
|
if (!DeferredRegion.hasValue() &&
|
|
|
|
// File IDs aren't gathered within macro expansions, so it isn't
|
|
|
|
// useful to try and create a deferred region inside of one.
|
|
|
|
(SM.getFileID(EndLoc) == SM.getMainFileID()))
|
|
|
|
DeferredRegion =
|
|
|
|
SourceMappingRegion(Counter::getZero(), EndLoc, None);
|
|
|
|
}
|
|
|
|
} else if (Region.isDeferred()) {
|
|
|
|
assert(!ParentOfDeferredRegion && "Consecutive deferred regions");
|
|
|
|
ParentOfDeferredRegion = true;
|
2015-02-19 05:24:51 +08:00
|
|
|
}
|
|
|
|
RegionStack.pop_back();
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
2017-09-09 02:44:56 +08:00
|
|
|
assert(!ParentOfDeferredRegion && "Deferred region with no parent");
|
2015-02-19 05:24:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Return the currently active region.
|
|
|
|
SourceMappingRegion &getRegion() {
|
|
|
|
assert(!RegionStack.empty() && "statement has no region");
|
|
|
|
return RegionStack.back();
|
|
|
|
}
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
/// \brief Propagate counts through the children of \c S.
|
|
|
|
Counter propagateCounts(Counter TopCount, const Stmt *S) {
|
2017-07-27 10:20:20 +08:00
|
|
|
SourceLocation StartLoc = getStart(S);
|
|
|
|
SourceLocation EndLoc = getEnd(S);
|
|
|
|
size_t Index = pushRegion(TopCount, StartLoc, EndLoc);
|
2015-02-19 05:24:51 +08:00
|
|
|
Visit(S);
|
|
|
|
Counter ExitCount = getRegion().getCounter();
|
|
|
|
popRegions(Index);
|
2016-02-09 03:25:45 +08:00
|
|
|
|
|
|
|
// The statement may be spanned by an expansion. Make sure we handle a file
|
|
|
|
// exit out of this expansion before moving to the next statement.
|
2017-07-27 10:20:20 +08:00
|
|
|
if (SM.isBeforeInTranslationUnit(StartLoc, S->getLocStart()))
|
|
|
|
MostRecentLocation = EndLoc;
|
2016-02-09 03:25:45 +08:00
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
return ExitCount;
|
|
|
|
}
|
|
|
|
|
2016-05-04 23:38:26 +08:00
|
|
|
/// \brief Check whether a region with bounds \c StartLoc and \c EndLoc
|
|
|
|
/// is already added to \c SourceRegions.
|
|
|
|
bool isRegionAlreadyAdded(SourceLocation StartLoc, SourceLocation EndLoc) {
|
|
|
|
return SourceRegions.rend() !=
|
|
|
|
std::find_if(SourceRegions.rbegin(), SourceRegions.rend(),
|
|
|
|
[&](const SourceMappingRegion &Region) {
|
|
|
|
return Region.getStartLoc() == StartLoc &&
|
|
|
|
Region.getEndLoc() == EndLoc;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
/// \brief Adjust the most recently visited location to \c EndLoc.
|
|
|
|
///
|
|
|
|
/// This should be used after visiting any statements in non-source order.
|
|
|
|
void adjustForOutOfOrderTraversal(SourceLocation EndLoc) {
|
|
|
|
MostRecentLocation = EndLoc;
|
2016-05-04 23:38:26 +08:00
|
|
|
// The code region for a whole macro is created in handleFileExit() when
|
|
|
|
// it detects exiting of the virtual file of that macro. If we visited
|
|
|
|
// statements in non-source order, we might already have such a region
|
|
|
|
// added, for example, if a body of a loop is divided among multiple
|
|
|
|
// macros. Avoid adding duplicate regions in such case.
|
2015-05-02 03:23:34 +08:00
|
|
|
if (getRegion().hasEndLoc() &&
|
2016-05-04 23:38:26 +08:00
|
|
|
MostRecentLocation == getEndOfFileOrMacro(MostRecentLocation) &&
|
|
|
|
isRegionAlreadyAdded(getStartOfFileOrMacro(MostRecentLocation),
|
|
|
|
MostRecentLocation))
|
2015-02-19 05:24:51 +08:00
|
|
|
MostRecentLocation = getIncludeOrExpansionLoc(MostRecentLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Adjust regions and state when \c NewLoc exits a file.
|
|
|
|
///
|
|
|
|
/// If moving from our most recently tracked location to \c NewLoc exits any
|
|
|
|
/// files, this adjusts our current region stack and creates the file regions
|
|
|
|
/// for the exited file.
|
|
|
|
void handleFileExit(SourceLocation NewLoc) {
|
2015-06-24 04:29:09 +08:00
|
|
|
if (NewLoc.isInvalid() ||
|
|
|
|
SM.isWrittenInSameFile(MostRecentLocation, NewLoc))
|
2015-02-19 05:24:51 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// If NewLoc is not in a file that contains MostRecentLocation, walk up to
|
|
|
|
// find the common ancestor.
|
|
|
|
SourceLocation LCA = NewLoc;
|
|
|
|
FileID ParentFile = SM.getFileID(LCA);
|
|
|
|
while (!isNestedIn(MostRecentLocation, ParentFile)) {
|
|
|
|
LCA = getIncludeOrExpansionLoc(LCA);
|
|
|
|
if (LCA.isInvalid() || SM.isWrittenInSameFile(LCA, MostRecentLocation)) {
|
|
|
|
// Since there isn't a common ancestor, no file was exited. We just need
|
|
|
|
// to adjust our location to the new file.
|
|
|
|
MostRecentLocation = NewLoc;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ParentFile = SM.getFileID(LCA);
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
llvm::SmallSet<SourceLocation, 8> StartLocs;
|
|
|
|
Optional<Counter> ParentCounter;
|
2015-07-31 01:22:52 +08:00
|
|
|
for (SourceMappingRegion &I : llvm::reverse(RegionStack)) {
|
|
|
|
if (!I.hasStartLoc())
|
2015-02-19 05:24:51 +08:00
|
|
|
continue;
|
2015-07-31 01:22:52 +08:00
|
|
|
SourceLocation Loc = I.getStartLoc();
|
2015-02-19 05:24:51 +08:00
|
|
|
if (!isNestedIn(Loc, ParentFile)) {
|
2015-07-31 01:22:52 +08:00
|
|
|
ParentCounter = I.getCounter();
|
2015-02-19 05:24:51 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!SM.isInFileID(Loc, ParentFile)) {
|
|
|
|
// The most nested region for each start location is the one with the
|
|
|
|
// correct count. We avoid creating redundant regions by stopping once
|
|
|
|
// we've seen this region.
|
|
|
|
if (StartLocs.insert(Loc).second)
|
2015-07-31 01:22:52 +08:00
|
|
|
SourceRegions.emplace_back(I.getCounter(), Loc,
|
2015-02-19 05:24:51 +08:00
|
|
|
getEndOfFileOrMacro(Loc));
|
|
|
|
Loc = getIncludeOrExpansionLoc(Loc);
|
|
|
|
}
|
2015-07-31 01:22:52 +08:00
|
|
|
I.setStartLoc(getPreciseTokenLocEnd(Loc));
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
2015-02-19 05:24:51 +08:00
|
|
|
|
|
|
|
if (ParentCounter) {
|
|
|
|
// If the file is contained completely by another region and doesn't
|
|
|
|
// immediately start its own region, the whole file gets a region
|
|
|
|
// corresponding to the parent.
|
|
|
|
SourceLocation Loc = MostRecentLocation;
|
|
|
|
while (isNestedIn(Loc, ParentFile)) {
|
|
|
|
SourceLocation FileStart = getStartOfFileOrMacro(Loc);
|
|
|
|
if (StartLocs.insert(FileStart).second)
|
|
|
|
SourceRegions.emplace_back(*ParentCounter, FileStart,
|
|
|
|
getEndOfFileOrMacro(Loc));
|
|
|
|
Loc = getIncludeOrExpansionLoc(Loc);
|
|
|
|
}
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
2015-02-19 05:24:51 +08:00
|
|
|
|
|
|
|
MostRecentLocation = NewLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Ensure that \c S is included in the current region.
|
|
|
|
void extendRegion(const Stmt *S) {
|
|
|
|
SourceMappingRegion &Region = getRegion();
|
|
|
|
SourceLocation StartLoc = getStart(S);
|
|
|
|
|
|
|
|
handleFileExit(StartLoc);
|
|
|
|
if (!Region.hasStartLoc())
|
|
|
|
Region.setStartLoc(StartLoc);
|
2017-09-09 02:44:56 +08:00
|
|
|
|
|
|
|
completeDeferred(Region.getCounter(), StartLoc);
|
2015-02-19 05:24:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Mark \c S as a terminator, starting a zero region.
|
|
|
|
void terminateRegion(const Stmt *S) {
|
|
|
|
extendRegion(S);
|
|
|
|
SourceMappingRegion &Region = getRegion();
|
|
|
|
if (!Region.hasEndLoc())
|
|
|
|
Region.setEndLoc(getEnd(S));
|
|
|
|
pushRegion(Counter::getZero());
|
2017-09-09 02:44:56 +08:00
|
|
|
getRegion().setDeferred(true);
|
2015-02-19 05:24:51 +08:00
|
|
|
}
|
2014-08-05 02:41:51 +08:00
|
|
|
|
|
|
|
/// \brief Keep counts of breaks and continues inside loops.
|
|
|
|
struct BreakContinue {
|
|
|
|
Counter BreakCount;
|
|
|
|
Counter ContinueCount;
|
|
|
|
};
|
|
|
|
SmallVector<BreakContinue, 8> BreakContinueStack;
|
|
|
|
|
|
|
|
CounterCoverageMappingBuilder(
|
|
|
|
CoverageMappingModuleGen &CVM,
|
2014-10-03 00:44:01 +08:00
|
|
|
llvm::DenseMap<const Stmt *, unsigned> &CounterMap, SourceManager &SM,
|
2014-08-05 02:41:51 +08:00
|
|
|
const LangOptions &LangOpts)
|
2017-09-09 02:44:56 +08:00
|
|
|
: CoverageMappingBuilder(CVM, SM, LangOpts), CounterMap(CounterMap),
|
|
|
|
DeferredRegion(None) {}
|
2014-08-05 02:41:51 +08:00
|
|
|
|
|
|
|
/// \brief Write the mapping data to the output stream
|
|
|
|
void write(llvm::raw_ostream &OS) {
|
|
|
|
llvm::SmallVector<unsigned, 8> VirtualFileMapping;
|
2015-02-19 05:24:51 +08:00
|
|
|
gatherFileIDs(VirtualFileMapping);
|
2016-08-31 15:04:16 +08:00
|
|
|
SourceRegionFilter Filter = emitExpansionRegions();
|
2017-09-09 02:44:56 +08:00
|
|
|
assert(!DeferredRegion && "Deferred region never completed");
|
2016-08-31 15:04:16 +08:00
|
|
|
emitSourceRegions(Filter);
|
2014-08-05 02:41:51 +08:00
|
|
|
gatherSkippedRegions();
|
|
|
|
|
2016-07-26 08:24:59 +08:00
|
|
|
if (MappingRegions.empty())
|
|
|
|
return;
|
|
|
|
|
2015-02-04 05:35:49 +08:00
|
|
|
CoverageMappingWriter Writer(VirtualFileMapping, Builder.getExpressions(),
|
|
|
|
MappingRegions);
|
2014-08-05 02:41:51 +08:00
|
|
|
Writer.write(OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitStmt(const Stmt *S) {
|
2015-10-03 13:15:57 +08:00
|
|
|
if (S->getLocStart().isValid())
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(S);
|
2015-07-03 05:03:14 +08:00
|
|
|
for (const Stmt *Child : S->children())
|
|
|
|
if (Child)
|
|
|
|
this->Visit(Child);
|
2015-02-19 05:24:51 +08:00
|
|
|
handleFileExit(getEnd(S));
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 15:47:39 +08:00
|
|
|
/// Determine whether the final deferred region emitted in \p Body should be
|
|
|
|
/// discarded.
|
|
|
|
static bool discardFinalDeferredRegionInDecl(Stmt *Body) {
|
|
|
|
if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
|
|
|
|
Stmt *LastStmt = CS->body_back();
|
|
|
|
if (auto *IfElse = dyn_cast<IfStmt>(LastStmt)) {
|
|
|
|
if (auto *Else = dyn_cast_or_null<CompoundStmt>(IfElse->getElse()))
|
|
|
|
LastStmt = Else->body_back();
|
|
|
|
else
|
|
|
|
LastStmt = IfElse->getElse();
|
|
|
|
}
|
|
|
|
return dyn_cast_or_null<ReturnStmt>(LastStmt);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
void VisitDecl(const Decl *D) {
|
2017-09-09 02:44:56 +08:00
|
|
|
assert(!DeferredRegion && "Deferred region never completed");
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
Stmt *Body = D->getBody();
|
2016-07-26 08:24:59 +08:00
|
|
|
|
|
|
|
// Do not propagate region counts into system headers.
|
|
|
|
if (Body && SM.isInSystemHeader(SM.getSpellingLoc(getStart(Body))))
|
|
|
|
return;
|
|
|
|
|
2017-09-09 02:44:56 +08:00
|
|
|
Counter ExitCount = propagateCounts(getRegionCounter(Body), Body);
|
|
|
|
assert(RegionStack.empty() && "Regions entered but never exited");
|
|
|
|
|
2017-10-17 15:47:39 +08:00
|
|
|
if (DeferredRegion) {
|
|
|
|
// Complete (or discard) any deferred regions introduced by the last
|
|
|
|
// statement.
|
|
|
|
if (discardFinalDeferredRegionInDecl(Body))
|
2017-09-19 08:29:46 +08:00
|
|
|
DeferredRegion = None;
|
2017-10-17 15:47:39 +08:00
|
|
|
else
|
|
|
|
popRegions(completeDeferred(ExitCount, getEnd(Body)));
|
|
|
|
}
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitReturnStmt(const ReturnStmt *S) {
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(S);
|
2014-08-05 02:41:51 +08:00
|
|
|
if (S->getRetValue())
|
|
|
|
Visit(S->getRetValue());
|
2015-02-19 05:24:51 +08:00
|
|
|
terminateRegion(S);
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
2015-04-28 14:31:55 +08:00
|
|
|
void VisitCXXThrowExpr(const CXXThrowExpr *E) {
|
|
|
|
extendRegion(E);
|
|
|
|
if (E->getSubExpr())
|
|
|
|
Visit(E->getSubExpr());
|
|
|
|
terminateRegion(E);
|
|
|
|
}
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
void VisitGotoStmt(const GotoStmt *S) { terminateRegion(S); }
|
2014-08-05 02:41:51 +08:00
|
|
|
|
|
|
|
void VisitLabelStmt(const LabelStmt *S) {
|
2015-02-19 05:24:51 +08:00
|
|
|
SourceLocation Start = getStart(S);
|
|
|
|
// We can't extendRegion here or we risk overlapping with our new region.
|
|
|
|
handleFileExit(Start);
|
|
|
|
pushRegion(getRegionCounter(S), Start);
|
2014-08-05 02:41:51 +08:00
|
|
|
Visit(S->getSubStmt());
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitBreakStmt(const BreakStmt *S) {
|
|
|
|
assert(!BreakContinueStack.empty() && "break not in a loop or switch!");
|
|
|
|
BreakContinueStack.back().BreakCount = addCounters(
|
2015-02-19 05:24:51 +08:00
|
|
|
BreakContinueStack.back().BreakCount, getRegion().getCounter());
|
2017-08-03 07:22:50 +08:00
|
|
|
// FIXME: a break in a switch should terminate regions for all preceding
|
|
|
|
// case statements, not just the most recent one.
|
2015-02-19 05:24:51 +08:00
|
|
|
terminateRegion(S);
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitContinueStmt(const ContinueStmt *S) {
|
|
|
|
assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
|
|
|
|
BreakContinueStack.back().ContinueCount = addCounters(
|
2015-02-19 05:24:51 +08:00
|
|
|
BreakContinueStack.back().ContinueCount, getRegion().getCounter());
|
|
|
|
terminateRegion(S);
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
2017-08-09 04:10:14 +08:00
|
|
|
void VisitCallExpr(const CallExpr *E) {
|
|
|
|
VisitStmt(E);
|
|
|
|
|
|
|
|
// Terminate the region when we hit a noreturn function.
|
|
|
|
// (This is helpful dealing with switch statements.)
|
|
|
|
QualType CalleeType = E->getCallee()->getType();
|
|
|
|
if (getFunctionExtInfo(*CalleeType).getNoReturn())
|
|
|
|
terminateRegion(E);
|
|
|
|
}
|
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
void VisitWhileStmt(const WhileStmt *S) {
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(S);
|
|
|
|
|
|
|
|
Counter ParentCount = getRegion().getCounter();
|
|
|
|
Counter BodyCount = getRegionCounter(S);
|
|
|
|
|
|
|
|
// Handle the body first so that we can get the backedge count.
|
2014-08-05 02:41:51 +08:00
|
|
|
BreakContinueStack.push_back(BreakContinue());
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(S->getBody());
|
|
|
|
Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
|
2014-08-05 02:41:51 +08:00
|
|
|
BreakContinue BC = BreakContinueStack.pop_back_val();
|
2015-02-19 05:24:51 +08:00
|
|
|
|
|
|
|
// Go back to handle the condition.
|
|
|
|
Counter CondCount =
|
|
|
|
addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
|
|
|
|
propagateCounts(CondCount, S->getCond());
|
|
|
|
adjustForOutOfOrderTraversal(getEnd(S));
|
|
|
|
|
|
|
|
Counter OutCount =
|
|
|
|
addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
|
|
|
|
if (OutCount != ParentCount)
|
|
|
|
pushRegion(OutCount);
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitDoStmt(const DoStmt *S) {
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(S);
|
|
|
|
|
|
|
|
Counter ParentCount = getRegion().getCounter();
|
|
|
|
Counter BodyCount = getRegionCounter(S);
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
BreakContinueStack.push_back(BreakContinue());
|
|
|
|
extendRegion(S->getBody());
|
|
|
|
Counter BackedgeCount =
|
|
|
|
propagateCounts(addCounters(ParentCount, BodyCount), S->getBody());
|
2014-08-05 02:41:51 +08:00
|
|
|
BreakContinue BC = BreakContinueStack.pop_back_val();
|
2015-02-19 05:24:51 +08:00
|
|
|
|
|
|
|
Counter CondCount = addCounters(BackedgeCount, BC.ContinueCount);
|
|
|
|
propagateCounts(CondCount, S->getCond());
|
|
|
|
|
|
|
|
Counter OutCount =
|
|
|
|
addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
|
|
|
|
if (OutCount != ParentCount)
|
|
|
|
pushRegion(OutCount);
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitForStmt(const ForStmt *S) {
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(S);
|
2014-08-05 02:41:51 +08:00
|
|
|
if (S->getInit())
|
|
|
|
Visit(S->getInit());
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
Counter ParentCount = getRegion().getCounter();
|
|
|
|
Counter BodyCount = getRegionCounter(S);
|
|
|
|
|
|
|
|
// Handle the body first so that we can get the backedge count.
|
2014-08-05 02:41:51 +08:00
|
|
|
BreakContinueStack.push_back(BreakContinue());
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(S->getBody());
|
|
|
|
Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
|
|
|
|
BreakContinue BC = BreakContinueStack.pop_back_val();
|
2014-08-05 02:41:51 +08:00
|
|
|
|
|
|
|
// The increment is essentially part of the body but it needs to include
|
|
|
|
// the count for all the continue statements.
|
2015-02-19 05:24:51 +08:00
|
|
|
if (const Stmt *Inc = S->getInc())
|
|
|
|
propagateCounts(addCounters(BackedgeCount, BC.ContinueCount), Inc);
|
|
|
|
|
|
|
|
// Go back to handle the condition.
|
|
|
|
Counter CondCount =
|
|
|
|
addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
|
|
|
|
if (const Expr *Cond = S->getCond()) {
|
|
|
|
propagateCounts(CondCount, Cond);
|
|
|
|
adjustForOutOfOrderTraversal(getEnd(S));
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
Counter OutCount =
|
|
|
|
addCounters(BC.BreakCount, subtractCounters(CondCount, BodyCount));
|
|
|
|
if (OutCount != ParentCount)
|
|
|
|
pushRegion(OutCount);
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(S);
|
|
|
|
Visit(S->getLoopVarStmt());
|
2014-08-05 02:41:51 +08:00
|
|
|
Visit(S->getRangeStmt());
|
2015-02-19 05:24:51 +08:00
|
|
|
|
|
|
|
Counter ParentCount = getRegion().getCounter();
|
|
|
|
Counter BodyCount = getRegionCounter(S);
|
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
BreakContinueStack.push_back(BreakContinue());
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(S->getBody());
|
|
|
|
Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
|
2014-08-05 02:41:51 +08:00
|
|
|
BreakContinue BC = BreakContinueStack.pop_back_val();
|
2015-02-19 05:24:51 +08:00
|
|
|
|
2015-05-01 05:31:02 +08:00
|
|
|
Counter LoopCount =
|
|
|
|
addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
|
|
|
|
Counter OutCount =
|
|
|
|
addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
|
2015-02-19 05:24:51 +08:00
|
|
|
if (OutCount != ParentCount)
|
|
|
|
pushRegion(OutCount);
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(S);
|
2014-08-05 02:41:51 +08:00
|
|
|
Visit(S->getElement());
|
2015-02-19 05:24:51 +08:00
|
|
|
|
|
|
|
Counter ParentCount = getRegion().getCounter();
|
|
|
|
Counter BodyCount = getRegionCounter(S);
|
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
BreakContinueStack.push_back(BreakContinue());
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(S->getBody());
|
|
|
|
Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
|
2014-08-05 02:41:51 +08:00
|
|
|
BreakContinue BC = BreakContinueStack.pop_back_val();
|
2015-02-19 05:24:51 +08:00
|
|
|
|
2015-05-01 05:31:02 +08:00
|
|
|
Counter LoopCount =
|
|
|
|
addCounters(ParentCount, BackedgeCount, BC.ContinueCount);
|
|
|
|
Counter OutCount =
|
|
|
|
addCounters(BC.BreakCount, subtractCounters(LoopCount, BodyCount));
|
2015-02-19 05:24:51 +08:00
|
|
|
if (OutCount != ParentCount)
|
|
|
|
pushRegion(OutCount);
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitSwitchStmt(const SwitchStmt *S) {
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(S);
|
2016-10-15 07:38:13 +08:00
|
|
|
if (S->getInit())
|
|
|
|
Visit(S->getInit());
|
2014-08-05 02:41:51 +08:00
|
|
|
Visit(S->getCond());
|
2015-02-19 05:24:51 +08:00
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
BreakContinueStack.push_back(BreakContinue());
|
2015-02-19 05:24:51 +08:00
|
|
|
|
|
|
|
const Stmt *Body = S->getBody();
|
|
|
|
extendRegion(Body);
|
|
|
|
if (const auto *CS = dyn_cast<CompoundStmt>(Body)) {
|
|
|
|
if (!CS->body_empty()) {
|
2017-08-03 07:22:50 +08:00
|
|
|
// Make a region for the body of the switch. If the body starts with
|
|
|
|
// a case, that case will reuse this region; otherwise, this covers
|
|
|
|
// the unreachable code at the beginning of the switch body.
|
2015-02-19 05:24:51 +08:00
|
|
|
size_t Index =
|
2017-08-03 07:22:50 +08:00
|
|
|
pushRegion(Counter::getZero(), getStart(CS->body_front()));
|
2015-04-15 09:21:42 +08:00
|
|
|
for (const auto *Child : CS->children())
|
2015-02-19 05:24:51 +08:00
|
|
|
Visit(Child);
|
2017-08-03 07:22:50 +08:00
|
|
|
|
|
|
|
// Set the end for the body of the switch, if it isn't already set.
|
|
|
|
for (size_t i = RegionStack.size(); i != Index; --i) {
|
|
|
|
if (!RegionStack[i - 1].hasEndLoc())
|
|
|
|
RegionStack[i - 1].setEndLoc(getEnd(CS->body_back()));
|
|
|
|
}
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
popRegions(Index);
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
2016-06-01 04:35:12 +08:00
|
|
|
} else
|
2015-02-19 05:24:51 +08:00
|
|
|
propagateCounts(Counter::getZero(), Body);
|
2014-08-05 02:41:51 +08:00
|
|
|
BreakContinue BC = BreakContinueStack.pop_back_val();
|
2015-02-19 05:24:51 +08:00
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
if (!BreakContinueStack.empty())
|
|
|
|
BreakContinueStack.back().ContinueCount = addCounters(
|
|
|
|
BreakContinueStack.back().ContinueCount, BC.ContinueCount);
|
2015-02-19 05:24:51 +08:00
|
|
|
|
|
|
|
Counter ExitCount = getRegionCounter(S);
|
2016-06-01 02:06:19 +08:00
|
|
|
SourceLocation ExitLoc = getEnd(S);
|
2016-09-28 07:30:36 +08:00
|
|
|
pushRegion(ExitCount);
|
|
|
|
|
|
|
|
// Ensure that handleFileExit recognizes when the end location is located
|
|
|
|
// in a different file.
|
|
|
|
MostRecentLocation = getStart(S);
|
2016-06-01 02:06:19 +08:00
|
|
|
handleFileExit(ExitLoc);
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
void VisitSwitchCase(const SwitchCase *S) {
|
|
|
|
extendRegion(S);
|
|
|
|
|
|
|
|
SourceMappingRegion &Parent = getRegion();
|
|
|
|
|
|
|
|
Counter Count = addCounters(Parent.getCounter(), getRegionCounter(S));
|
|
|
|
// Reuse the existing region if it starts at our label. This is typical of
|
|
|
|
// the first case in a switch.
|
|
|
|
if (Parent.hasStartLoc() && Parent.getStartLoc() == getStart(S))
|
|
|
|
Parent.setCounter(Count);
|
|
|
|
else
|
|
|
|
pushRegion(Count, getStart(S));
|
|
|
|
|
2015-12-25 05:11:29 +08:00
|
|
|
if (const auto *CS = dyn_cast<CaseStmt>(S)) {
|
2015-02-19 05:24:51 +08:00
|
|
|
Visit(CS->getLHS());
|
|
|
|
if (const Expr *RHS = CS->getRHS())
|
|
|
|
Visit(RHS);
|
|
|
|
}
|
2014-08-05 02:41:51 +08:00
|
|
|
Visit(S->getSubStmt());
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitIfStmt(const IfStmt *S) {
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(S);
|
2016-10-15 07:38:16 +08:00
|
|
|
if (S->getInit())
|
|
|
|
Visit(S->getInit());
|
|
|
|
|
2015-06-16 14:24:15 +08:00
|
|
|
// Extend into the condition before we propagate through it below - this is
|
|
|
|
// needed to handle macros that generate the "if" but not the condition.
|
|
|
|
extendRegion(S->getCond());
|
2015-02-19 05:24:51 +08:00
|
|
|
|
|
|
|
Counter ParentCount = getRegion().getCounter();
|
|
|
|
Counter ThenCount = getRegionCounter(S);
|
|
|
|
|
2015-02-19 11:10:30 +08:00
|
|
|
// Emitting a counter for the condition makes it easier to interpret the
|
|
|
|
// counter for the body when looking at the coverage.
|
|
|
|
propagateCounts(ParentCount, S->getCond());
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(S->getThen());
|
|
|
|
Counter OutCount = propagateCounts(ThenCount, S->getThen());
|
|
|
|
|
|
|
|
Counter ElseCount = subtractCounters(ParentCount, ThenCount);
|
|
|
|
if (const Stmt *Else = S->getElse()) {
|
|
|
|
extendRegion(S->getElse());
|
|
|
|
OutCount = addCounters(OutCount, propagateCounts(ElseCount, Else));
|
|
|
|
} else
|
|
|
|
OutCount = addCounters(OutCount, ElseCount);
|
|
|
|
|
|
|
|
if (OutCount != ParentCount)
|
|
|
|
pushRegion(OutCount);
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitCXXTryStmt(const CXXTryStmt *S) {
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(S);
|
2016-06-23 03:57:58 +08:00
|
|
|
// Handle macros that generate the "try" but not the rest.
|
|
|
|
extendRegion(S->getTryBlock());
|
|
|
|
|
|
|
|
Counter ParentCount = getRegion().getCounter();
|
|
|
|
propagateCounts(ParentCount, S->getTryBlock());
|
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
for (unsigned I = 0, E = S->getNumHandlers(); I < E; ++I)
|
|
|
|
Visit(S->getHandler(I));
|
2015-02-19 05:24:51 +08:00
|
|
|
|
|
|
|
Counter ExitCount = getRegionCounter(S);
|
|
|
|
pushRegion(ExitCount);
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitCXXCatchStmt(const CXXCatchStmt *S) {
|
2015-02-19 05:24:51 +08:00
|
|
|
propagateCounts(getRegionCounter(S), S->getHandlerBlock());
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(E);
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
Counter ParentCount = getRegion().getCounter();
|
|
|
|
Counter TrueCount = getRegionCounter(E);
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2015-04-25 07:37:57 +08:00
|
|
|
Visit(E->getCond());
|
|
|
|
|
|
|
|
if (!isa<BinaryConditionalOperator>(E)) {
|
|
|
|
extendRegion(E->getTrueExpr());
|
|
|
|
propagateCounts(TrueCount, E->getTrueExpr());
|
|
|
|
}
|
|
|
|
extendRegion(E->getFalseExpr());
|
2015-02-19 05:24:51 +08:00
|
|
|
propagateCounts(subtractCounters(ParentCount, TrueCount),
|
|
|
|
E->getFalseExpr());
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitBinLAnd(const BinaryOperator *E) {
|
2017-10-17 14:51:54 +08:00
|
|
|
extendRegion(E->getLHS());
|
|
|
|
propagateCounts(getRegion().getCounter(), E->getLHS());
|
|
|
|
handleFileExit(getEnd(E->getLHS()));
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(E->getRHS());
|
|
|
|
propagateCounts(getRegionCounter(E), E->getRHS());
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
void VisitBinLOr(const BinaryOperator *E) {
|
2017-10-17 14:51:54 +08:00
|
|
|
extendRegion(E->getLHS());
|
|
|
|
propagateCounts(getRegion().getCounter(), E->getLHS());
|
|
|
|
handleFileExit(getEnd(E->getLHS()));
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(E->getRHS());
|
|
|
|
propagateCounts(getRegionCounter(E), E->getRHS());
|
2014-08-21 01:10:56 +08:00
|
|
|
}
|
2015-02-24 12:13:56 +08:00
|
|
|
|
|
|
|
void VisitLambdaExpr(const LambdaExpr *LE) {
|
|
|
|
// Lambdas are treated as their own functions for now, so we shouldn't
|
|
|
|
// propagate counts into them.
|
|
|
|
}
|
2014-08-05 02:41:51 +08:00
|
|
|
};
|
|
|
|
|
2017-04-14 12:14:29 +08:00
|
|
|
std::string getCoverageSection(const CodeGenModule &CGM) {
|
2017-04-15 08:10:05 +08:00
|
|
|
return llvm::getInstrProfSectionName(
|
|
|
|
llvm::IPSK_covmap,
|
|
|
|
CGM.getContext().getTargetInfo().getTriple().getObjectFormat());
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
2016-07-19 05:01:27 +08:00
|
|
|
std::string normalizeFilename(StringRef Filename) {
|
|
|
|
llvm::SmallString<256> Path(Filename);
|
|
|
|
llvm::sys::fs::make_absolute(Path);
|
2016-07-19 06:32:02 +08:00
|
|
|
llvm::sys::path::remove_dots(Path, /*remove_dot_dots=*/true);
|
2016-07-19 05:01:27 +08:00
|
|
|
return Path.str().str();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2015-02-03 08:20:24 +08:00
|
|
|
static void dump(llvm::raw_ostream &OS, StringRef FunctionName,
|
|
|
|
ArrayRef<CounterExpression> Expressions,
|
|
|
|
ArrayRef<CounterMappingRegion> Regions) {
|
|
|
|
OS << FunctionName << ":\n";
|
|
|
|
CounterMappingContext Ctx(Expressions);
|
|
|
|
for (const auto &R : Regions) {
|
2014-08-09 07:41:24 +08:00
|
|
|
OS.indent(2);
|
|
|
|
switch (R.Kind) {
|
|
|
|
case CounterMappingRegion::CodeRegion:
|
|
|
|
break;
|
|
|
|
case CounterMappingRegion::ExpansionRegion:
|
|
|
|
OS << "Expansion,";
|
|
|
|
break;
|
|
|
|
case CounterMappingRegion::SkippedRegion:
|
|
|
|
OS << "Skipped,";
|
|
|
|
break;
|
2017-09-19 07:37:30 +08:00
|
|
|
case CounterMappingRegion::GapRegion:
|
|
|
|
OS << "Gap,";
|
|
|
|
break;
|
2014-08-09 07:41:24 +08:00
|
|
|
}
|
|
|
|
|
2015-02-04 05:35:49 +08:00
|
|
|
OS << "File " << R.FileID << ", " << R.LineStart << ":" << R.ColumnStart
|
|
|
|
<< " -> " << R.LineEnd << ":" << R.ColumnEnd << " = ";
|
2015-01-24 07:46:13 +08:00
|
|
|
Ctx.dump(R.Count, OS);
|
2014-08-09 07:41:24 +08:00
|
|
|
if (R.Kind == CounterMappingRegion::ExpansionRegion)
|
2015-02-04 05:35:49 +08:00
|
|
|
OS << " (Expanded file = " << R.ExpandedFileID << ")";
|
|
|
|
OS << "\n";
|
2014-08-09 07:41:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
void CoverageMappingModuleGen::addFunctionMappingRecord(
|
2016-01-08 04:05:55 +08:00
|
|
|
llvm::GlobalVariable *NamePtr, StringRef NameValue, uint64_t FuncHash,
|
2016-01-19 08:49:06 +08:00
|
|
|
const std::string &CoverageMapping, bool IsUsed) {
|
2014-08-05 02:41:51 +08:00
|
|
|
llvm::LLVMContext &Ctx = CGM.getLLVMContext();
|
|
|
|
if (!FunctionRecordTy) {
|
2016-01-08 04:05:55 +08:00
|
|
|
#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) LLVMType,
|
2015-11-05 13:46:39 +08:00
|
|
|
llvm::Type *FunctionRecordTypes[] = {
|
|
|
|
#include "llvm/ProfileData/InstrProfData.inc"
|
|
|
|
};
|
2014-08-05 02:41:51 +08:00
|
|
|
FunctionRecordTy =
|
2015-07-03 04:47:25 +08:00
|
|
|
llvm::StructType::get(Ctx, makeArrayRef(FunctionRecordTypes),
|
|
|
|
/*isPacked=*/true);
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
2015-11-05 13:46:39 +08:00
|
|
|
#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Init,
|
2014-08-05 02:41:51 +08:00
|
|
|
llvm::Constant *FunctionRecordVals[] = {
|
2015-11-05 13:46:39 +08:00
|
|
|
#include "llvm/ProfileData/InstrProfData.inc"
|
|
|
|
};
|
2014-08-05 02:41:51 +08:00
|
|
|
FunctionRecords.push_back(llvm::ConstantStruct::get(
|
|
|
|
FunctionRecordTy, makeArrayRef(FunctionRecordVals)));
|
2016-01-19 08:49:06 +08:00
|
|
|
if (!IsUsed)
|
2016-01-08 04:05:55 +08:00
|
|
|
FunctionNames.push_back(
|
|
|
|
llvm::ConstantExpr::getBitCast(NamePtr, llvm::Type::getInt8PtrTy(Ctx)));
|
2016-01-22 03:25:35 +08:00
|
|
|
CoverageMappings.push_back(CoverageMapping);
|
2014-08-09 07:41:24 +08:00
|
|
|
|
|
|
|
if (CGM.getCodeGenOpts().DumpCoverageMapping) {
|
|
|
|
// Dump the coverage mapping data for this function by decoding the
|
|
|
|
// encoded data. This allows us to dump the mapping regions which were
|
|
|
|
// also processed by the CoverageMappingWriter which performs
|
|
|
|
// additional minimization operations such as reducing the number of
|
|
|
|
// expressions.
|
|
|
|
std::vector<StringRef> Filenames;
|
|
|
|
std::vector<CounterExpression> Expressions;
|
|
|
|
std::vector<CounterMappingRegion> Regions;
|
2016-11-08 01:28:04 +08:00
|
|
|
llvm::SmallVector<std::string, 16> FilenameStrs;
|
2014-08-09 07:41:24 +08:00
|
|
|
llvm::SmallVector<StringRef, 16> FilenameRefs;
|
2016-11-08 01:28:04 +08:00
|
|
|
FilenameStrs.resize(FileEntries.size());
|
2014-08-09 07:41:24 +08:00
|
|
|
FilenameRefs.resize(FileEntries.size());
|
2016-11-08 01:28:04 +08:00
|
|
|
for (const auto &Entry : FileEntries) {
|
|
|
|
auto I = Entry.second;
|
|
|
|
FilenameStrs[I] = normalizeFilename(Entry.first->getName());
|
|
|
|
FilenameRefs[I] = FilenameStrs[I];
|
|
|
|
}
|
2015-02-03 08:20:24 +08:00
|
|
|
RawCoverageMappingReader Reader(CoverageMapping, FilenameRefs, Filenames,
|
|
|
|
Expressions, Regions);
|
|
|
|
if (Reader.read())
|
2014-08-09 07:41:24 +08:00
|
|
|
return;
|
2015-11-05 13:46:39 +08:00
|
|
|
dump(llvm::outs(), NameValue, Expressions, Regions);
|
2014-08-09 07:41:24 +08:00
|
|
|
}
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CoverageMappingModuleGen::emit() {
|
|
|
|
if (FunctionRecords.empty())
|
|
|
|
return;
|
|
|
|
llvm::LLVMContext &Ctx = CGM.getLLVMContext();
|
|
|
|
auto *Int32Ty = llvm::Type::getInt32Ty(Ctx);
|
|
|
|
|
|
|
|
// Create the filenames and merge them with coverage mappings
|
|
|
|
llvm::SmallVector<std::string, 16> FilenameStrs;
|
2016-06-29 13:33:09 +08:00
|
|
|
llvm::SmallVector<StringRef, 16> FilenameRefs;
|
2014-08-05 02:41:51 +08:00
|
|
|
FilenameStrs.resize(FileEntries.size());
|
2016-06-29 13:33:09 +08:00
|
|
|
FilenameRefs.resize(FileEntries.size());
|
2014-08-05 02:41:51 +08:00
|
|
|
for (const auto &Entry : FileEntries) {
|
|
|
|
auto I = Entry.second;
|
2016-07-19 05:01:27 +08:00
|
|
|
FilenameStrs[I] = normalizeFilename(Entry.first->getName());
|
2016-06-29 13:33:09 +08:00
|
|
|
FilenameRefs[I] = FilenameStrs[I];
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
2016-06-29 13:33:09 +08:00
|
|
|
std::string FilenamesAndCoverageMappings;
|
|
|
|
llvm::raw_string_ostream OS(FilenamesAndCoverageMappings);
|
|
|
|
CoverageFilenamesSectionWriter(FilenameRefs).write(OS);
|
|
|
|
std::string RawCoverageMappings =
|
|
|
|
llvm::join(CoverageMappings.begin(), CoverageMappings.end(), "");
|
|
|
|
OS << RawCoverageMappings;
|
|
|
|
size_t CoverageMappingSize = RawCoverageMappings.size();
|
|
|
|
size_t FilenamesSize = OS.str().size() - CoverageMappingSize;
|
|
|
|
// Append extra zeroes if necessary to ensure that the size of the filenames
|
|
|
|
// and coverage mappings is a multiple of 8.
|
|
|
|
if (size_t Rem = OS.str().size() % 8) {
|
|
|
|
CoverageMappingSize += 8 - Rem;
|
|
|
|
for (size_t I = 0, S = 8 - Rem; I < S; ++I)
|
|
|
|
OS << '\0';
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
auto *FilenamesAndMappingsVal =
|
2016-06-29 13:33:09 +08:00
|
|
|
llvm::ConstantDataArray::getString(Ctx, OS.str(), false);
|
2014-08-05 02:41:51 +08:00
|
|
|
|
|
|
|
// Create the deferred function records array
|
|
|
|
auto RecordsTy =
|
|
|
|
llvm::ArrayType::get(FunctionRecordTy, FunctionRecords.size());
|
|
|
|
auto RecordsVal = llvm::ConstantArray::get(RecordsTy, FunctionRecords);
|
|
|
|
|
2016-01-04 03:25:54 +08:00
|
|
|
llvm::Type *CovDataHeaderTypes[] = {
|
|
|
|
#define COVMAP_HEADER(Type, LLVMType, Name, Init) LLVMType,
|
|
|
|
#include "llvm/ProfileData/InstrProfData.inc"
|
|
|
|
};
|
|
|
|
auto CovDataHeaderTy =
|
|
|
|
llvm::StructType::get(Ctx, makeArrayRef(CovDataHeaderTypes));
|
|
|
|
llvm::Constant *CovDataHeaderVals[] = {
|
|
|
|
#define COVMAP_HEADER(Type, LLVMType, Name, Init) Init,
|
|
|
|
#include "llvm/ProfileData/InstrProfData.inc"
|
|
|
|
};
|
|
|
|
auto CovDataHeaderVal = llvm::ConstantStruct::get(
|
|
|
|
CovDataHeaderTy, makeArrayRef(CovDataHeaderVals));
|
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
// Create the coverage data record
|
2016-01-04 03:25:54 +08:00
|
|
|
llvm::Type *CovDataTypes[] = {CovDataHeaderTy, RecordsTy,
|
|
|
|
FilenamesAndMappingsVal->getType()};
|
2014-08-05 02:41:51 +08:00
|
|
|
auto CovDataTy = llvm::StructType::get(Ctx, makeArrayRef(CovDataTypes));
|
2016-01-04 03:25:54 +08:00
|
|
|
llvm::Constant *TUDataVals[] = {CovDataHeaderVal, RecordsVal,
|
|
|
|
FilenamesAndMappingsVal};
|
2014-08-05 02:41:51 +08:00
|
|
|
auto CovDataVal =
|
|
|
|
llvm::ConstantStruct::get(CovDataTy, makeArrayRef(TUDataVals));
|
2016-01-04 03:25:54 +08:00
|
|
|
auto CovData = new llvm::GlobalVariable(
|
|
|
|
CGM.getModule(), CovDataTy, true, llvm::GlobalValue::InternalLinkage,
|
|
|
|
CovDataVal, llvm::getCoverageMappingVarName());
|
2014-08-05 02:41:51 +08:00
|
|
|
|
|
|
|
CovData->setSection(getCoverageSection(CGM));
|
|
|
|
CovData->setAlignment(8);
|
|
|
|
|
|
|
|
// Make sure the data doesn't get deleted.
|
|
|
|
CGM.addUsedGlobal(CovData);
|
2016-01-08 04:05:55 +08:00
|
|
|
// Create the deferred function records array
|
|
|
|
if (!FunctionNames.empty()) {
|
|
|
|
auto NamesArrTy = llvm::ArrayType::get(llvm::Type::getInt8PtrTy(Ctx),
|
|
|
|
FunctionNames.size());
|
|
|
|
auto NamesArrVal = llvm::ConstantArray::get(NamesArrTy, FunctionNames);
|
|
|
|
// This variable will *NOT* be emitted to the object file. It is used
|
|
|
|
// to pass the list of names referenced to codegen.
|
|
|
|
new llvm::GlobalVariable(CGM.getModule(), NamesArrTy, true,
|
|
|
|
llvm::GlobalValue::InternalLinkage, NamesArrVal,
|
2016-01-20 08:24:52 +08:00
|
|
|
llvm::getCoverageUnusedNamesVarName());
|
2016-01-08 04:05:55 +08:00
|
|
|
}
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned CoverageMappingModuleGen::getFileID(const FileEntry *File) {
|
|
|
|
auto It = FileEntries.find(File);
|
|
|
|
if (It != FileEntries.end())
|
|
|
|
return It->second;
|
|
|
|
unsigned FileID = FileEntries.size();
|
|
|
|
FileEntries.insert(std::make_pair(File, FileID));
|
|
|
|
return FileID;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoverageMappingGen::emitCounterMapping(const Decl *D,
|
|
|
|
llvm::raw_ostream &OS) {
|
|
|
|
assert(CounterMap);
|
2014-10-03 00:44:01 +08:00
|
|
|
CounterCoverageMappingBuilder Walker(CVM, *CounterMap, SM, LangOpts);
|
2014-08-05 02:41:51 +08:00
|
|
|
Walker.VisitDecl(D);
|
|
|
|
Walker.write(OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoverageMappingGen::emitEmptyMapping(const Decl *D,
|
|
|
|
llvm::raw_ostream &OS) {
|
|
|
|
EmptyCoverageMappingBuilder Walker(CVM, SM, LangOpts);
|
|
|
|
Walker.VisitDecl(D);
|
|
|
|
Walker.write(OS);
|
|
|
|
}
|