mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[IRGen] Update coverage mapping for version 6
The coverage format version we use is automatically set to whatever the latest version LLVM has. This resulting in us outputting version 6 as our format without having made the changes necessary to reflect the new format. Update the emission logic to take account for [the new changes in version 6][1] (we need to include the current working directory as the first element of the list of files we output). Additionally, add a `static_assert` so we don't get caught out by version bumps in the future. Note we can't pin our version, as it must stay in sync with the version Clang is using. [1]: https://llvm.org/docs/CoverageMappingFormat.html#llvm-ir-representation
This commit is contained in:
@@ -27,16 +27,24 @@
|
||||
#include "llvm/ProfileData/InstrProf.h"
|
||||
#include "llvm/Support/FileSystem.h"
|
||||
|
||||
// This selects the coverage mapping format defined when `InstrProfData.inc`
|
||||
// is textually included.
|
||||
#define COVMAP_V3
|
||||
|
||||
using namespace swift;
|
||||
using namespace irgen;
|
||||
|
||||
using llvm::coverage::CounterMappingRegion;
|
||||
using llvm::coverage::CovMapVersion;
|
||||
|
||||
// This affects the coverage mapping format defined when `InstrProfData.inc`
|
||||
// is textually included. Note that it means 'version >= 3', not 'version == 3'.
|
||||
#define COVMAP_V3
|
||||
|
||||
/// This assert is here to make sure we make all the necessary code generation
|
||||
/// changes that are needed to support the new coverage mapping format. Note we
|
||||
/// cannot pin our version, as it must remain in sync with the version Clang is
|
||||
/// using.
|
||||
/// Do not bump without at least filing a bug and pinging a coverage maintainer.
|
||||
static_assert(CovMapVersion::CurrentVersion == CovMapVersion::Version6,
|
||||
"Coverage mapping emission needs updating");
|
||||
|
||||
static std::string getInstrProfSection(IRGenModule &IGM,
|
||||
llvm::InstrProfSectKind SK) {
|
||||
return llvm::getInstrProfSectionName(SK, IGM.Triple.getObjectFormat());
|
||||
@@ -77,18 +85,26 @@ void IRGenModule::emitCoverageMaps(ArrayRef<const SILCoverageMap *> Mappings) {
|
||||
}
|
||||
|
||||
std::vector<StringRef> Files;
|
||||
for (const auto &M : Mappings)
|
||||
for (const auto &M : Mappings) {
|
||||
if (std::find(Files.begin(), Files.end(), M->getFilename()) == Files.end())
|
||||
Files.push_back(M->getFilename());
|
||||
|
||||
auto remapper = getOptions().CoveragePrefixMap;
|
||||
}
|
||||
const auto &Remapper = getOptions().CoveragePrefixMap;
|
||||
|
||||
llvm::SmallVector<std::string, 8> FilenameStrs;
|
||||
for (StringRef Name : Files) {
|
||||
llvm::SmallString<256> Path(Name);
|
||||
llvm::sys::fs::make_absolute(Path);
|
||||
FilenameStrs.push_back(remapper.remapPath(Path));
|
||||
}
|
||||
FilenameStrs.reserve(Files.size() + 1);
|
||||
|
||||
// First element needs to be the current working directory. Note if this
|
||||
// scheme ever changes, the FileID computation below will need updating.
|
||||
SmallString<256> WorkingDirectory;
|
||||
llvm::sys::fs::current_path(WorkingDirectory);
|
||||
FilenameStrs.emplace_back(Remapper.remapPath(WorkingDirectory));
|
||||
|
||||
// Following elements are the filenames present. We use their relative path,
|
||||
// which llvm-cov will turn back into absolute paths using the working
|
||||
// directory element.
|
||||
for (auto Name : Files)
|
||||
FilenameStrs.emplace_back(Remapper.remapPath(Name));
|
||||
|
||||
// Encode the filenames.
|
||||
std::string Filenames;
|
||||
@@ -112,13 +128,21 @@ void IRGenModule::emitCoverageMaps(ArrayRef<const SILCoverageMap *> Mappings) {
|
||||
const uint64_t NameHash = llvm::IndexedInstrProf::ComputeHash(NameValue);
|
||||
std::string FuncRecordName = "__covrec_" + llvm::utohexstr(NameHash);
|
||||
|
||||
unsigned FileID =
|
||||
std::find(Files.begin(), Files.end(), M->getFilename()) - Files.begin();
|
||||
// The file ID needs to be bumped by 1 to account for the working directory
|
||||
// as the first element.
|
||||
unsigned FileID = 1 +
|
||||
std::find(Files.begin(), Files.end(), M->getFilename()) -
|
||||
Files.begin();
|
||||
assert(FileID < FilenameStrs.size());
|
||||
|
||||
std::vector<CounterMappingRegion> Regions;
|
||||
for (const auto &MR : M->getMappedRegions())
|
||||
for (const auto &MR : M->getMappedRegions()) {
|
||||
// The SubFileID here is 0, because it's an index into VirtualFileMapping,
|
||||
// and we only ever have a single file associated for a function.
|
||||
Regions.emplace_back(CounterMappingRegion::makeRegion(
|
||||
MR.Counter, /*FileID=*/0, MR.StartLine, MR.StartCol, MR.EndLine,
|
||||
MR.Counter, /*SubFileID*/ 0, MR.StartLine, MR.StartCol, MR.EndLine,
|
||||
MR.EndCol));
|
||||
}
|
||||
// Append each function's regions into the encoded buffer.
|
||||
ArrayRef<unsigned> VirtualFileMapping(FileID);
|
||||
llvm::coverage::CoverageMappingWriter W(VirtualFileMapping,
|
||||
|
||||
Reference in New Issue
Block a user