2014-08-05 02:41:51 +08:00
|
|
|
//===--- CoverageMappingGen.cpp - Coverage mapping generation ---*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-08-05 02:41:51 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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 {
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 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;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The region's starting location.
|
2015-02-19 05:24:51 +08:00
|
|
|
Optional<SourceLocation> LocStart;
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 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
|
|
|
|
2018-08-10 04:05:18 +08:00
|
|
|
SourceLocation getBeginLoc() 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
|
|
|
|
2018-01-18 02:53:51 +08:00
|
|
|
void setEndLoc(SourceLocation Loc) {
|
|
|
|
assert(Loc.isValid() && "Setting an invalid end location");
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-11-30 06:25:14 +08:00
|
|
|
SpellingRegion(SourceManager &SM, SourceMappingRegion &R)
|
2018-08-10 05:05:56 +08:00
|
|
|
: SpellingRegion(SM, R.getBeginLoc(), R.getEndLoc()) {}
|
2017-11-30 06:25:14 +08:00
|
|
|
|
2017-07-27 10:20:25 +08:00
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Provides the common functionality for the different
|
2014-08-05 02:41:51 +08:00
|
|
|
/// coverage mapping region builders.
|
|
|
|
class CoverageMappingBuilder {
|
|
|
|
public:
|
|
|
|
CoverageMappingModuleGen &CVM;
|
|
|
|
SourceManager &SM;
|
|
|
|
const LangOptions &LangOpts;
|
|
|
|
|
|
|
|
private:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Map of clang's FileIDs to IDs used for coverage mapping.
|
2015-02-19 05:24:51 +08:00
|
|
|
llvm::SmallDenseMap<FileID, std::pair<unsigned, SourceLocation>, 8>
|
|
|
|
FileIDMapping;
|
2014-08-05 02:41:51 +08:00
|
|
|
|
|
|
|
public:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The coverage mapping regions for this function
|
2014-08-05 02:41:51 +08:00
|
|
|
llvm::SmallVector<CounterMappingRegion, 32> MappingRegions;
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 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
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// A set of regions which can be used as a filter.
|
2016-08-31 15:04:16 +08:00
|
|
|
///
|
|
|
|
/// 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
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Return the precise end location for the given token.
|
2014-08-05 02:41:51 +08:00
|
|
|
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
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Return the start location of an included file or expanded macro.
|
2015-02-19 05:24:51 +08:00
|
|
|
SourceLocation getStartOfFileOrMacro(SourceLocation Loc) {
|
|
|
|
if (Loc.isMacroID())
|
|
|
|
return Loc.getLocWithOffset(-SM.getFileOffset(Loc));
|
|
|
|
return SM.getLocForStartOfFile(SM.getFileID(Loc));
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Return the end location of an included file or expanded macro.
|
2015-02-19 05:24:51 +08:00
|
|
|
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
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Find out where the current file is included or macro is expanded.
|
2015-02-19 05:24:51 +08:00
|
|
|
SourceLocation getIncludeOrExpansionLoc(SourceLocation Loc) {
|
2018-04-30 13:25:48 +08:00
|
|
|
return Loc.isMacroID() ? SM.getImmediateExpansionRange(Loc).getBegin()
|
2015-02-19 05:24:51 +08:00
|
|
|
: SM.getIncludeLoc(SM.getFileID(Loc));
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Return true if \c Loc is a location in a built-in macro.
|
2015-05-15 06:14:10 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Check whether \c Loc is included or expanded from \c Parent.
|
2016-06-07 18:07:51 +08:00
|
|
|
bool isNestedIn(SourceLocation Loc, FileID Parent) {
|
|
|
|
do {
|
|
|
|
Loc = getIncludeOrExpansionLoc(Loc);
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
return false;
|
|
|
|
} while (!SM.isInFileID(Loc, Parent));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 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) {
|
2018-08-10 05:08:08 +08:00
|
|
|
SourceLocation Loc = S->getBeginLoc();
|
2015-05-15 06:14:10 +08:00
|
|
|
while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
|
2018-04-30 13:25:48 +08:00
|
|
|
Loc = SM.getImmediateExpansionRange(Loc).getBegin();
|
2015-02-19 05:24:51 +08:00
|
|
|
return Loc;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 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) {
|
2018-08-10 05:09:38 +08:00
|
|
|
SourceLocation Loc = S->getEndLoc();
|
2015-05-15 06:14:10 +08:00
|
|
|
while (SM.isMacroArgExpansion(Loc) || isInBuiltin(Loc))
|
2018-04-30 13:25:48 +08:00
|
|
|
Loc = SM.getImmediateExpansionRange(Loc).getBegin();
|
2015-03-25 12:13:49 +08:00
|
|
|
return getPreciseTokenLocEnd(Loc);
|
2015-02-19 05:24:51 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Find the set of files we have regions for and assign IDs
|
2015-02-19 05:24:51 +08:00
|
|
|
///
|
|
|
|
/// 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) {
|
2018-08-10 05:05:56 +08:00
|
|
|
SourceLocation Loc = Region.getBeginLoc();
|
2015-02-19 05:24:51 +08:00
|
|
|
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));
|
|
|
|
}
|
2019-04-24 22:43:05 +08:00
|
|
|
llvm::stable_sort(FileLocs, llvm::less_second());
|
2015-02-19 05:24:51 +08:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Get the coverage mapping file ID for \c Loc.
|
2015-02-19 05:24:51 +08:00
|
|
|
///
|
|
|
|
/// 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
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Gather all the regions that were skipped by the preprocessor
|
2014-08-05 02:41:51 +08:00
|
|
|
/// 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
|
2018-04-06 23:14:32 +08:00
|
|
|
// the source 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Generate the coverage counter mapping regions from collected
|
2014-08-05 02:41:51 +08:00
|
|
|
/// 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");
|
|
|
|
|
2018-08-10 05:05:56 +08:00
|
|
|
SourceLocation LocStart = Region.getBeginLoc();
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Creates unreachable coverage regions for the functions that
|
2014-08-05 02:41:51 +08:00
|
|
|
/// 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
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Write the mapping data to the output stream
|
2014-08-05 02:41:51 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// A StmtVisitor that creates coverage mapping regions which map
|
2014-08-05 02:41:51 +08:00
|
|
|
/// from the source code locations to the PGO counters.
|
|
|
|
struct CounterCoverageMappingBuilder
|
|
|
|
: public CoverageMappingBuilder,
|
|
|
|
public ConstStmtVisitor<CounterCoverageMappingBuilder> {
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The map of statements to count values.
|
2014-08-05 02:41:51 +08:00
|
|
|
llvm::DenseMap<const Stmt *, unsigned> &CounterMap;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// A stack of currently live regions.
|
2015-02-19 05:24:51 +08:00
|
|
|
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;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// A location in the most recently visited file or macro.
|
2015-02-19 05:24:51 +08:00
|
|
|
///
|
|
|
|
/// This is used to adjust the active source regions appropriately when
|
|
|
|
/// expressions cross file or macro boundaries.
|
|
|
|
SourceLocation MostRecentLocation;
|
|
|
|
|
2017-11-09 10:33:39 +08:00
|
|
|
/// Location of the last terminated region.
|
|
|
|
Optional<std::pair<SourceLocation, size_t>> LastTerminatedRegion;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// 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
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Push a region onto the stack.
|
2015-02-19 05:24:51 +08:00
|
|
|
///
|
|
|
|
/// 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.
|
2018-08-10 05:05:56 +08:00
|
|
|
FileID StartFile = SM.getFileID(DR.getBeginLoc());
|
2017-11-09 10:33:40 +08:00
|
|
|
if (SM.getFileID(DeferredEndLoc) != StartFile) {
|
2017-09-09 02:44:56 +08:00
|
|
|
if (isNestedIn(DeferredEndLoc, StartFile)) {
|
|
|
|
do {
|
|
|
|
DeferredEndLoc = getIncludeOrExpansionLoc(DeferredEndLoc);
|
|
|
|
} while (StartFile != SM.getFileID(DeferredEndLoc));
|
2017-11-09 10:33:40 +08:00
|
|
|
} else {
|
|
|
|
return Index;
|
2017-09-09 02:44:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The parent of this deferred region ends where the containing decl ends,
|
|
|
|
// so the region isn't useful.
|
2018-08-10 05:05:56 +08:00
|
|
|
if (DR.getBeginLoc() == DeferredEndLoc)
|
2017-09-09 02:44:56 +08:00
|
|
|
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.
|
2018-08-10 05:05:56 +08:00
|
|
|
if (!SpellingRegion(SM, DR.getBeginLoc(), DeferredEndLoc).isInSourceOrder())
|
2017-09-09 02:44:56 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-11-09 10:33:39 +08:00
|
|
|
/// Complete a deferred region created after a terminated region at the
|
|
|
|
/// top-level.
|
|
|
|
void completeTopLevelDeferredRegion(Counter Count,
|
|
|
|
SourceLocation DeferredEndLoc) {
|
|
|
|
if (DeferredRegion || !LastTerminatedRegion)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (LastTerminatedRegion->second != RegionStack.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SourceLocation Start = LastTerminatedRegion->first;
|
|
|
|
if (SM.getFileID(Start) != SM.getMainFileID())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SourceMappingRegion DR = RegionStack.back();
|
|
|
|
DR.setStartLoc(Start);
|
|
|
|
DR.setDeferred(false);
|
|
|
|
DeferredRegion = DR;
|
|
|
|
completeDeferred(Count, DeferredEndLoc);
|
|
|
|
}
|
|
|
|
|
2018-11-20 04:10:22 +08:00
|
|
|
size_t locationDepth(SourceLocation Loc) {
|
|
|
|
size_t Depth = 0;
|
|
|
|
while (Loc.isValid()) {
|
|
|
|
Loc = getIncludeOrExpansionLoc(Loc);
|
|
|
|
Depth++;
|
|
|
|
}
|
|
|
|
return Depth;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Pop regions from the stack into the function's list of regions.
|
2015-02-19 05:24:51 +08:00
|
|
|
///
|
|
|
|
/// 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()) {
|
2018-08-10 05:05:56 +08:00
|
|
|
SourceLocation StartLoc = Region.getBeginLoc();
|
2015-02-19 05:24:51 +08:00
|
|
|
SourceLocation EndLoc = Region.hasEndLoc()
|
|
|
|
? Region.getEndLoc()
|
|
|
|
: RegionStack[ParentIndex].getEndLoc();
|
2018-11-20 04:10:22 +08:00
|
|
|
size_t StartDepth = locationDepth(StartLoc);
|
|
|
|
size_t EndDepth = locationDepth(EndLoc);
|
2015-02-19 05:24:51 +08:00
|
|
|
while (!SM.isWrittenInSameFile(StartLoc, EndLoc)) {
|
2018-11-20 04:10:22 +08:00
|
|
|
bool UnnestStart = StartDepth >= EndDepth;
|
|
|
|
bool UnnestEnd = EndDepth >= StartDepth;
|
|
|
|
if (UnnestEnd) {
|
|
|
|
// The region ends in a nested file or macro expansion. Create a
|
|
|
|
// separate region for each expansion.
|
|
|
|
SourceLocation NestedLoc = getStartOfFileOrMacro(EndLoc);
|
|
|
|
assert(SM.isWrittenInSameFile(NestedLoc, EndLoc));
|
|
|
|
|
|
|
|
if (!isRegionAlreadyAdded(NestedLoc, EndLoc))
|
|
|
|
SourceRegions.emplace_back(Region.getCounter(), NestedLoc, EndLoc);
|
|
|
|
|
|
|
|
EndLoc = getPreciseTokenLocEnd(getIncludeOrExpansionLoc(EndLoc));
|
|
|
|
if (EndLoc.isInvalid())
|
|
|
|
llvm::report_fatal_error("File exit not handled before popRegions");
|
|
|
|
EndDepth--;
|
|
|
|
}
|
|
|
|
if (UnnestStart) {
|
|
|
|
// The region begins in a nested file or macro expansion. Create a
|
|
|
|
// separate region for each expansion.
|
|
|
|
SourceLocation NestedLoc = getEndOfFileOrMacro(StartLoc);
|
|
|
|
assert(SM.isWrittenInSameFile(StartLoc, NestedLoc));
|
|
|
|
|
|
|
|
if (!isRegionAlreadyAdded(StartLoc, NestedLoc))
|
|
|
|
SourceRegions.emplace_back(Region.getCounter(), StartLoc, NestedLoc);
|
|
|
|
|
|
|
|
StartLoc = getIncludeOrExpansionLoc(StartLoc);
|
|
|
|
if (StartLoc.isInvalid())
|
|
|
|
llvm::report_fatal_error("File exit not handled before popRegions");
|
|
|
|
StartDepth--;
|
|
|
|
}
|
2015-02-19 05:24:51 +08:00
|
|
|
}
|
2018-11-20 04:10:22 +08:00
|
|
|
Region.setStartLoc(StartLoc);
|
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);
|
|
|
|
|
2018-08-10 05:05:56 +08:00
|
|
|
assert(SM.isWrittenInSameFile(Region.getBeginLoc(), EndLoc));
|
2017-11-30 06:25:14 +08:00
|
|
|
assert(SpellingRegion(SM, Region).isInSourceOrder());
|
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.
|
2017-11-09 10:33:40 +08:00
|
|
|
!EndLoc.isMacroID())
|
2017-09-09 02:44:56 +08:00
|
|
|
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();
|
2017-11-09 10:33:39 +08:00
|
|
|
|
|
|
|
// If the zero region pushed after the last terminated region no longer
|
|
|
|
// exists, clear its cached information.
|
|
|
|
if (LastTerminatedRegion &&
|
|
|
|
RegionStack.size() < LastTerminatedRegion->second)
|
|
|
|
LastTerminatedRegion = None;
|
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
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Return the currently active region.
|
2015-02-19 05:24:51 +08:00
|
|
|
SourceMappingRegion &getRegion() {
|
|
|
|
assert(!RegionStack.empty() && "statement has no region");
|
|
|
|
return RegionStack.back();
|
|
|
|
}
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2018-11-29 04:48:07 +08:00
|
|
|
/// Propagate counts through the children of \p S if \p VisitChildren is true.
|
|
|
|
/// Otherwise, only emit a count for \p S itself.
|
|
|
|
Counter propagateCounts(Counter TopCount, const Stmt *S,
|
|
|
|
bool VisitChildren = true) {
|
2017-07-27 10:20:20 +08:00
|
|
|
SourceLocation StartLoc = getStart(S);
|
|
|
|
SourceLocation EndLoc = getEnd(S);
|
|
|
|
size_t Index = pushRegion(TopCount, StartLoc, EndLoc);
|
2018-11-29 04:48:07 +08:00
|
|
|
if (VisitChildren)
|
|
|
|
Visit(S);
|
2015-02-19 05:24:51 +08:00
|
|
|
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.
|
2018-08-10 05:08:08 +08:00
|
|
|
if (SM.isBeforeInTranslationUnit(StartLoc, S->getBeginLoc()))
|
2017-07-27 10:20:20 +08:00
|
|
|
MostRecentLocation = EndLoc;
|
2016-02-09 03:25:45 +08:00
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
return ExitCount;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Check whether a region with bounds \c StartLoc and \c EndLoc
|
2016-05-04 23:38:26 +08:00
|
|
|
/// 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) {
|
2018-08-10 05:05:56 +08:00
|
|
|
return Region.getBeginLoc() == StartLoc &&
|
2016-05-04 23:38:26 +08:00
|
|
|
Region.getEndLoc() == EndLoc;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Adjust the most recently visited location to \c EndLoc.
|
2015-02-19 05:24:51 +08:00
|
|
|
///
|
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Adjust regions and state when \c NewLoc exits a file.
|
2015-02-19 05:24:51 +08:00
|
|
|
///
|
|
|
|
/// 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;
|
2018-08-10 05:05:56 +08:00
|
|
|
SourceLocation Loc = I.getBeginLoc();
|
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);
|
2017-11-30 06:25:14 +08:00
|
|
|
if (StartLocs.insert(FileStart).second) {
|
2015-02-19 05:24:51 +08:00
|
|
|
SourceRegions.emplace_back(*ParentCounter, FileStart,
|
|
|
|
getEndOfFileOrMacro(Loc));
|
2017-11-30 06:25:14 +08:00
|
|
|
assert(SpellingRegion(SM, SourceRegions.back()).isInSourceOrder());
|
|
|
|
}
|
2015-02-19 05:24:51 +08:00
|
|
|
Loc = getIncludeOrExpansionLoc(Loc);
|
|
|
|
}
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
2015-02-19 05:24:51 +08:00
|
|
|
|
|
|
|
MostRecentLocation = NewLoc;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Ensure that \c S is included in the current region.
|
2015-02-19 05:24:51 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Mark \c S as a terminator, starting a zero region.
|
2015-02-19 05:24:51 +08:00
|
|
|
void terminateRegion(const Stmt *S) {
|
|
|
|
extendRegion(S);
|
|
|
|
SourceMappingRegion &Region = getRegion();
|
2017-11-09 10:33:39 +08:00
|
|
|
SourceLocation EndLoc = getEnd(S);
|
2015-02-19 05:24:51 +08:00
|
|
|
if (!Region.hasEndLoc())
|
2017-11-09 10:33:39 +08:00
|
|
|
Region.setEndLoc(EndLoc);
|
2015-02-19 05:24:51 +08:00
|
|
|
pushRegion(Counter::getZero());
|
2017-11-09 10:33:39 +08:00
|
|
|
auto &ZeroRegion = getRegion();
|
|
|
|
ZeroRegion.setDeferred(true);
|
|
|
|
LastTerminatedRegion = {EndLoc, RegionStack.size()};
|
2015-02-19 05:24:51 +08:00
|
|
|
}
|
2014-08-05 02:41:51 +08:00
|
|
|
|
2017-11-30 06:25:14 +08:00
|
|
|
/// Find a valid gap range between \p AfterLoc and \p BeforeLoc.
|
|
|
|
Optional<SourceRange> findGapAreaBetween(SourceLocation AfterLoc,
|
|
|
|
SourceLocation BeforeLoc) {
|
|
|
|
// If the start and end locations of the gap are both within the same macro
|
|
|
|
// file, the range may not be in source order.
|
|
|
|
if (AfterLoc.isMacroID() || BeforeLoc.isMacroID())
|
|
|
|
return None;
|
|
|
|
if (!SM.isWrittenInSameFile(AfterLoc, BeforeLoc))
|
|
|
|
return None;
|
|
|
|
return {{AfterLoc, BeforeLoc}};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Find the source range after \p AfterStmt and before \p BeforeStmt.
|
|
|
|
Optional<SourceRange> findGapAreaBetween(const Stmt *AfterStmt,
|
|
|
|
const Stmt *BeforeStmt) {
|
|
|
|
return findGapAreaBetween(getPreciseTokenLocEnd(getEnd(AfterStmt)),
|
|
|
|
getStart(BeforeStmt));
|
|
|
|
}
|
|
|
|
|
2017-11-09 10:33:38 +08:00
|
|
|
/// Emit a gap region between \p StartLoc and \p EndLoc with the given count.
|
|
|
|
void fillGapAreaWithCount(SourceLocation StartLoc, SourceLocation EndLoc,
|
|
|
|
Counter Count) {
|
2017-11-30 06:25:14 +08:00
|
|
|
if (StartLoc == EndLoc)
|
2017-11-09 10:33:38 +08:00
|
|
|
return;
|
2017-11-30 06:25:14 +08:00
|
|
|
assert(SpellingRegion(SM, StartLoc, EndLoc).isInSourceOrder());
|
2017-11-09 10:33:38 +08:00
|
|
|
handleFileExit(StartLoc);
|
|
|
|
size_t Index = pushRegion(Count, StartLoc, EndLoc);
|
|
|
|
getRegion().setGap(true);
|
|
|
|
handleFileExit(EndLoc);
|
|
|
|
popRegions(Index);
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Keep counts of breaks and continues inside loops.
|
2014-08-05 02:41:51 +08:00
|
|
|
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
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Write the mapping data to the output stream
|
2014-08-05 02:41:51 +08:00
|
|
|
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) {
|
2018-08-10 05:08:08 +08:00
|
|
|
if (S->getBeginLoc().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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-11-29 04:48:07 +08:00
|
|
|
// Do not visit the artificial children nodes of defaulted methods. The
|
|
|
|
// lexer may not be able to report back precise token end locations for
|
|
|
|
// these children nodes (llvm.org/PR39822), and moreover users will not be
|
|
|
|
// able to see coverage for them.
|
|
|
|
bool Defaulted = false;
|
|
|
|
if (auto *Method = dyn_cast<CXXMethodDecl>(D))
|
|
|
|
Defaulted = Method->isDefaulted();
|
|
|
|
|
|
|
|
propagateCounts(getRegionCounter(Body), Body,
|
|
|
|
/*VisitChildren=*/!Defaulted);
|
2017-09-09 02:44:56 +08:00
|
|
|
assert(RegionStack.empty() && "Regions entered but never exited");
|
|
|
|
|
2018-05-31 07:35:44 +08:00
|
|
|
// Discard the last uncompleted deferred region in a decl, if one exists.
|
|
|
|
// This prevents lines at the end of a function containing only whitespace
|
|
|
|
// or closing braces from being marked as uncovered.
|
|
|
|
DeferredRegion = None;
|
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) {
|
2017-11-09 10:33:39 +08:00
|
|
|
Counter LabelCount = getRegionCounter(S);
|
2015-02-19 05:24:51 +08:00
|
|
|
SourceLocation Start = getStart(S);
|
2017-11-09 10:33:39 +08:00
|
|
|
completeTopLevelDeferredRegion(LabelCount, Start);
|
2018-06-01 08:37:13 +08:00
|
|
|
completeDeferred(LabelCount, Start);
|
2015-02-19 05:24:51 +08:00
|
|
|
// We can't extendRegion here or we risk overlapping with our new region.
|
|
|
|
handleFileExit(Start);
|
2017-11-09 10:33:39 +08:00
|
|
|
pushRegion(LabelCount, 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));
|
|
|
|
|
2017-11-30 06:25:14 +08:00
|
|
|
// The body count applies to the area immediately after the increment.
|
|
|
|
auto Gap = findGapAreaBetween(S->getCond(), S->getBody());
|
|
|
|
if (Gap)
|
|
|
|
fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
|
|
|
|
|
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 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);
|
|
|
|
|
2018-02-16 15:59:43 +08:00
|
|
|
// The loop increment may contain a break or continue.
|
|
|
|
if (S->getInc())
|
|
|
|
BreakContinueStack.emplace_back();
|
|
|
|
|
2015-02-19 05:24:51 +08:00
|
|
|
// Handle the body first so that we can get the backedge count.
|
2018-02-16 15:59:43 +08:00
|
|
|
BreakContinueStack.emplace_back();
|
2015-02-19 05:24:51 +08:00
|
|
|
extendRegion(S->getBody());
|
|
|
|
Counter BackedgeCount = propagateCounts(BodyCount, S->getBody());
|
2018-02-16 15:59:43 +08:00
|
|
|
BreakContinue BodyBC = 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.
|
2018-02-16 15:59:43 +08:00
|
|
|
BreakContinue IncrementBC;
|
|
|
|
if (const Stmt *Inc = S->getInc()) {
|
|
|
|
propagateCounts(addCounters(BackedgeCount, BodyBC.ContinueCount), Inc);
|
|
|
|
IncrementBC = BreakContinueStack.pop_back_val();
|
|
|
|
}
|
2015-02-19 05:24:51 +08:00
|
|
|
|
|
|
|
// Go back to handle the condition.
|
2018-02-16 15:59:43 +08:00
|
|
|
Counter CondCount = addCounters(
|
|
|
|
addCounters(ParentCount, BackedgeCount, BodyBC.ContinueCount),
|
|
|
|
IncrementBC.ContinueCount);
|
2015-02-19 05:24:51 +08:00
|
|
|
if (const Expr *Cond = S->getCond()) {
|
|
|
|
propagateCounts(CondCount, Cond);
|
|
|
|
adjustForOutOfOrderTraversal(getEnd(S));
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
|
|
|
|
2017-11-30 06:25:14 +08:00
|
|
|
// The body count applies to the area immediately after the increment.
|
|
|
|
auto Gap = findGapAreaBetween(getPreciseTokenLocEnd(S->getRParenLoc()),
|
|
|
|
getStart(S->getBody()));
|
|
|
|
if (Gap)
|
|
|
|
fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
|
|
|
|
|
2018-02-16 15:59:43 +08:00
|
|
|
Counter OutCount = addCounters(BodyBC.BreakCount, IncrementBC.BreakCount,
|
|
|
|
subtractCounters(CondCount, BodyCount));
|
2015-02-19 05:24:51 +08:00
|
|
|
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);
|
2018-09-29 02:44:09 +08:00
|
|
|
if (S->getInit())
|
|
|
|
Visit(S->getInit());
|
2015-02-19 05:24:51 +08:00
|
|
|
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
|
|
|
|
2017-11-30 06:25:14 +08:00
|
|
|
// The body count applies to the area immediately after the range.
|
|
|
|
auto Gap = findGapAreaBetween(getPreciseTokenLocEnd(S->getRParenLoc()),
|
|
|
|
getStart(S->getBody()));
|
|
|
|
if (Gap)
|
|
|
|
fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
|
|
|
|
|
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
|
|
|
|
2017-11-30 06:25:14 +08:00
|
|
|
// The body count applies to the area immediately after the collection.
|
|
|
|
auto Gap = findGapAreaBetween(getPreciseTokenLocEnd(S->getRParenLoc()),
|
|
|
|
getStart(S->getBody()));
|
|
|
|
if (Gap)
|
|
|
|
fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), BodyCount);
|
|
|
|
|
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.
|
2018-08-10 05:05:56 +08:00
|
|
|
if (Parent.hasStartLoc() && Parent.getBeginLoc() == getStart(S))
|
2015-02-19 05:24:51 +08:00
|
|
|
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());
|
|
|
|
|
2017-11-09 10:33:38 +08:00
|
|
|
// The 'then' count applies to the area immediately after the condition.
|
2017-11-30 06:25:14 +08:00
|
|
|
auto Gap = findGapAreaBetween(S->getCond(), S->getThen());
|
|
|
|
if (Gap)
|
|
|
|
fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), ThenCount);
|
2017-11-09 10:33:38 +08:00
|
|
|
|
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()) {
|
2017-11-09 10:33:38 +08:00
|
|
|
// The 'else' count applies to the area immediately after the 'then'.
|
2017-11-30 06:25:14 +08:00
|
|
|
Gap = findGapAreaBetween(S->getThen(), Else);
|
|
|
|
if (Gap)
|
|
|
|
fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), ElseCount);
|
2017-11-09 10:33:38 +08:00
|
|
|
extendRegion(Else);
|
2015-02-19 05:24:51 +08:00
|
|
|
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)) {
|
2017-11-09 10:33:38 +08:00
|
|
|
// The 'then' count applies to the area immediately after the condition.
|
2017-11-30 06:25:14 +08:00
|
|
|
auto Gap =
|
|
|
|
findGapAreaBetween(E->getQuestionLoc(), getStart(E->getTrueExpr()));
|
|
|
|
if (Gap)
|
|
|
|
fillGapAreaWithCount(Gap->getBegin(), Gap->getEnd(), TrueCount);
|
2017-11-09 10:33:38 +08:00
|
|
|
|
2015-04-25 07:37:57 +08:00
|
|
|
extendRegion(E->getTrueExpr());
|
|
|
|
propagateCounts(TrueCount, E->getTrueExpr());
|
|
|
|
}
|
2017-11-09 10:33:38 +08:00
|
|
|
|
2015-04-25 07:37:57 +08:00
|
|
|
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;
|
2018-05-18 06:11:43 +08:00
|
|
|
OS.write_zeros(8 - Rem);
|
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);
|
|
|
|
}
|