/// directory, recursively collect all of the paths within the directory.
void collectPaths(const std::string &Path);
+ /// Check if the two given files are the same file.
+ bool isEquivalentFile(StringRef FilePath1, StringRef FilePath2);
+
+ /// Retrieve a file status with a cache.
+ Optional<sys::fs::file_status> getFileStatus(StringRef FilePath);
+
/// Return a memory buffer for the given source file.
ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
/// remapped to, when using -path-equivalence.
Optional<std::pair<std::string, std::string>> PathRemapping;
+ /// File status cache used when finding the same file.
+ StringMap<Optional<sys::fs::file_status>> FileStatusCache;
+
/// The architecture the coverage mapping data targets.
std::vector<StringRef> CoverageArches;
}
}
+Optional<sys::fs::file_status>
+CodeCoverageTool::getFileStatus(StringRef FilePath) {
+ auto It = FileStatusCache.try_emplace(FilePath);
+ auto &CachedStatus = It.first->getValue();
+ if (!It.second)
+ return CachedStatus;
+
+ sys::fs::file_status Status;
+ if (!sys::fs::status(FilePath, Status))
+ CachedStatus = Status;
+ return CachedStatus;
+}
+
+bool CodeCoverageTool::isEquivalentFile(StringRef FilePath1,
+ StringRef FilePath2) {
+ auto Status1 = getFileStatus(FilePath1);
+ auto Status2 = getFileStatus(FilePath2);
+ return Status1.hasValue() && Status2.hasValue() &&
+ sys::fs::equivalent(Status1.getValue(), Status2.getValue());
+}
+
ErrorOr<const MemoryBuffer &>
CodeCoverageTool::getSourceFile(StringRef SourceFile) {
// If we've remapped filenames, look up the real location for this file.
SourceFile = Loc->second;
}
for (const auto &Files : LoadedSourceFiles)
- if (sys::fs::equivalent(SourceFile, Files.first))
+ if (isEquivalentFile(SourceFile, Files.first))
return *Files.second;
auto Buffer = MemoryBuffer::getFile(SourceFile);
if (auto EC = Buffer.getError()) {