35
35
#include " llvm/Support/FileSystem.h"
36
36
#include " llvm/Support/Path.h"
37
37
#include " llvm/Support/Regex.h"
38
+ #include " llvm/Support/VirtualFileSystem.h"
38
39
#include " llvm/Support/raw_ostream.h"
39
40
#include " llvm/Transforms/Instrumentation/CFGMST.h"
40
41
#include " llvm/Transforms/Instrumentation/GCOVProfiler.h"
@@ -92,8 +93,10 @@ class GCOVFunction;
92
93
93
94
class GCOVProfiler {
94
95
public:
95
- GCOVProfiler () : GCOVProfiler(GCOVOptions::getDefault()) {}
96
- GCOVProfiler (const GCOVOptions &Opts) : Options(Opts) {}
96
+ GCOVProfiler ()
97
+ : GCOVProfiler(GCOVOptions::getDefault(), *vfs::getRealFileSystem ()) {}
98
+ GCOVProfiler (const GCOVOptions &Opts, vfs::FileSystem &VFS)
99
+ : Options(Opts), VFS(VFS) {}
97
100
bool
98
101
runOnModule (Module &M, function_ref<BlockFrequencyInfo *(Function &F)> GetBFI,
99
102
function_ref<BranchProbabilityInfo *(Function &F)> GetBPI,
@@ -110,6 +113,7 @@ class GCOVProfiler {
110
113
os->write_zeros (4 - s.size () % 4 );
111
114
}
112
115
void writeBytes (const char *Bytes, int Size) { os->write (Bytes, Size); }
116
+ vfs::FileSystem &getVirtualFileSystem () const { return VFS; }
113
117
114
118
private:
115
119
// Create the .gcno files for the Module based on DebugInfo.
@@ -166,6 +170,7 @@ class GCOVProfiler {
166
170
std::vector<Regex> ExcludeRe;
167
171
DenseSet<const BasicBlock *> ExecBlocks;
168
172
StringMap<bool > InstrumentedFiles;
173
+ vfs::FileSystem &VFS;
169
174
};
170
175
171
176
struct BBInfo {
@@ -214,10 +219,10 @@ static StringRef getFunctionName(const DISubprogram *SP) {
214
219
// / Prefer relative paths in the coverage notes. Clang also may split
215
220
// / up absolute paths into a directory and filename component. When
216
221
// / the relative path doesn't exist, reconstruct the absolute path.
217
- static SmallString<128 > getFilename (const DIScope *SP) {
222
+ static SmallString<128 > getFilename (const DIScope *SP, vfs::FileSystem &VFS ) {
218
223
SmallString<128 > Path;
219
224
StringRef RelPath = SP->getFilename ();
220
- if (sys::fs:: exists (RelPath))
225
+ if (VFS. exists (RelPath))
221
226
Path = RelPath;
222
227
else
223
228
sys::path::append (Path, SP->getDirectory (), SP->getFilename ());
@@ -357,7 +362,7 @@ namespace {
357
362
358
363
void writeOut (uint32_t CfgChecksum) {
359
364
write (GCOV_TAG_FUNCTION);
360
- SmallString<128 > Filename = getFilename (SP);
365
+ SmallString<128 > Filename = getFilename (SP, P-> getVirtualFileSystem () );
361
366
uint32_t BlockLen = 3 + wordsOfString (getFunctionName (SP));
362
367
BlockLen += 1 + wordsOfString (Filename) + 4 ;
363
368
@@ -455,7 +460,7 @@ bool GCOVProfiler::isFunctionInstrumented(const Function &F) {
455
460
if (FilterRe.empty () && ExcludeRe.empty ()) {
456
461
return true ;
457
462
}
458
- SmallString<128 > Filename = getFilename (F.getSubprogram ());
463
+ SmallString<128 > Filename = getFilename (F.getSubprogram (), VFS );
459
464
auto It = InstrumentedFiles.find (Filename);
460
465
if (It != InstrumentedFiles.end ()) {
461
466
return It->second ;
@@ -467,7 +472,7 @@ bool GCOVProfiler::isFunctionInstrumented(const Function &F) {
467
472
// Path can be
468
473
// /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/*.h so for
469
474
// such a case we must get the real_path.
470
- if (sys::fs::real_path (Filename, RealPath)) {
475
+ if (VFS. getRealPath (Filename, RealPath)) {
471
476
// real_path can fail with path like "foo.c".
472
477
RealFilename = Filename;
473
478
} else {
@@ -524,9 +529,10 @@ std::string GCOVProfiler::mangleName(const DICompileUnit *CU,
524
529
SmallString<128 > Filename = CU->getFilename ();
525
530
sys::path::replace_extension (Filename, Notes ? " gcno" : " gcda" );
526
531
StringRef FName = sys::path::filename (Filename);
527
- SmallString< 128 > CurPath ;
528
- if (sys::fs::current_path (CurPath) )
532
+ ErrorOr<std::string> CWD = VFS. getCurrentWorkingDirectory () ;
533
+ if (!CWD )
529
534
return std::string (FName);
535
+ SmallString<128 > CurPath{*CWD};
530
536
sys::path::append (CurPath, FName);
531
537
return std::string (CurPath);
532
538
}
@@ -554,7 +560,7 @@ bool GCOVProfiler::runOnModule(
554
560
PreservedAnalyses GCOVProfilerPass::run (Module &M,
555
561
ModuleAnalysisManager &AM) {
556
562
557
- GCOVProfiler Profiler (GCOVOpts);
563
+ GCOVProfiler Profiler (GCOVOpts, *VFS );
558
564
FunctionAnalysisManager &FAM =
559
565
AM.getResult <FunctionAnalysisManagerModuleProxy>(M).getManager ();
560
566
@@ -789,7 +795,7 @@ bool GCOVProfiler::emitProfileNotes(
789
795
// Add the function line number to the lines of the entry block
790
796
// to have a counter for the function definition.
791
797
uint32_t Line = SP->getLine ();
792
- auto Filename = getFilename (SP);
798
+ auto Filename = getFilename (SP, VFS );
793
799
794
800
BranchProbabilityInfo *BPI = GetBPI (F);
795
801
BlockFrequencyInfo *BFI = GetBFI (F);
@@ -881,7 +887,7 @@ bool GCOVProfiler::emitProfileNotes(
881
887
if (SP != getDISubprogram (Scope))
882
888
continue ;
883
889
884
- GCOVLines &Lines = Block.getFile (getFilename (Loc->getScope ()));
890
+ GCOVLines &Lines = Block.getFile (getFilename (Loc->getScope (), VFS ));
885
891
Lines.addLine (Loc.getLine ());
886
892
}
887
893
Line = 0 ;
0 commit comments