@@ -832,12 +832,14 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
832
832
}
833
833
}
834
834
835
- if (Args.hasArg (options::OPT_ftest_coverage) ||
836
- Args.hasArg (options::OPT_coverage))
835
+ bool EmitCovNotes = Args.hasArg (options::OPT_ftest_coverage) ||
836
+ Args.hasArg (options::OPT_coverage);
837
+ bool EmitCovData = Args.hasFlag (options::OPT_fprofile_arcs,
838
+ options::OPT_fno_profile_arcs, false ) ||
839
+ Args.hasArg (options::OPT_coverage);
840
+ if (EmitCovNotes)
837
841
CmdArgs.push_back (" -femit-coverage-notes" );
838
- if (Args.hasFlag (options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
839
- false ) ||
840
- Args.hasArg (options::OPT_coverage))
842
+ if (EmitCovData)
841
843
CmdArgs.push_back (" -femit-coverage-data" );
842
844
843
845
if (Args.hasFlag (options::OPT_fcoverage_mapping,
@@ -873,40 +875,48 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
873
875
CmdArgs.push_back (Args.MakeArgString (Twine (" -fprofile-filter-files=" + v)));
874
876
}
875
877
876
- if (C.getArgs ().hasArg (options::OPT_c) ||
877
- C.getArgs ().hasArg (options::OPT_S)) {
878
- if (Output.isFilename ()) {
879
- CmdArgs.push_back (" -coverage-notes-file" );
880
- SmallString<128 > OutputFilename;
881
- if (Arg *FinalOutput = C.getArgs ().getLastArg (options::OPT_o))
882
- OutputFilename = FinalOutput->getValue ();
883
- else
884
- OutputFilename = llvm::sys::path::filename (Output.getBaseInput ());
885
- SmallString<128 > CoverageFilename = OutputFilename;
886
- if (llvm::sys::path::is_relative (CoverageFilename)) {
887
- SmallString<128 > Pwd;
888
- if (!llvm::sys::fs::current_path (Pwd)) {
889
- llvm::sys::path::append (Pwd, CoverageFilename);
890
- CoverageFilename.swap (Pwd);
891
- }
878
+ // Leave -fprofile-dir= an unused argument unless .gcda emission is
879
+ // enabled. To be polite, with '-fprofile-arcs -fno-profile-arcs' consider
880
+ // the flag used. There is no -fno-profile-dir, so the user has no
881
+ // targeted way to suppress the warning.
882
+ Arg *FProfileDir = nullptr ;
883
+ if (Args.hasArg (options::OPT_fprofile_arcs) ||
884
+ Args.hasArg (options::OPT_coverage))
885
+ FProfileDir = Args.getLastArg (options::OPT_fprofile_dir);
886
+
887
+ // Put the .gcno and .gcda files (if needed) next to the object file or
888
+ // bitcode file in the case of LTO.
889
+ // FIXME: There should be a simpler way to find the object file for this
890
+ // input, and this code probably does the wrong thing for commands that
891
+ // compile and link all at once.
892
+ if ((Args.hasArg (options::OPT_c) || Args.hasArg (options::OPT_S)) &&
893
+ (EmitCovNotes || EmitCovData) && Output.isFilename ()) {
894
+ SmallString<128 > OutputFilename;
895
+ if (Arg *FinalOutput = C.getArgs ().getLastArg (options::OPT_o))
896
+ OutputFilename = FinalOutput->getValue ();
897
+ else
898
+ OutputFilename = llvm::sys::path::filename (Output.getBaseInput ());
899
+ SmallString<128 > CoverageFilename = OutputFilename;
900
+ if (llvm::sys::path::is_relative (CoverageFilename)) {
901
+ SmallString<128 > Pwd;
902
+ if (!llvm::sys::fs::current_path (Pwd)) {
903
+ llvm::sys::path::append (Pwd, CoverageFilename);
904
+ CoverageFilename.swap (Pwd);
892
905
}
893
- llvm::sys::path::replace_extension (CoverageFilename, " gcno " );
894
- CmdArgs. push_back (Args. MakeArgString ( CoverageFilename) );
906
+ }
907
+ llvm::sys::path::replace_extension ( CoverageFilename, " gcno " );
895
908
896
- // Leave -fprofile-dir= an unused argument unless .gcda emission is
897
- // enabled. To be polite, with '-fprofile-arcs -fno-profile-arcs' consider
898
- // the flag used. There is no -fno-profile-dir, so the user has no
899
- // targeted way to suppress the warning.
900
- if (Args.hasArg (options::OPT_fprofile_arcs) ||
901
- Args.hasArg (options::OPT_coverage)) {
902
- CmdArgs.push_back (" -coverage-data-file" );
903
- if (Arg *FProfileDir = Args.getLastArg (options::OPT_fprofile_dir)) {
904
- CoverageFilename = FProfileDir->getValue ();
905
- llvm::sys::path::append (CoverageFilename, OutputFilename);
906
- }
907
- llvm::sys::path::replace_extension (CoverageFilename, " gcda" );
908
- CmdArgs.push_back (Args.MakeArgString (CoverageFilename));
909
+ CmdArgs.push_back (" -coverage-notes-file" );
910
+ CmdArgs.push_back (Args.MakeArgString (CoverageFilename));
911
+
912
+ if (EmitCovData) {
913
+ if (FProfileDir) {
914
+ CoverageFilename = FProfileDir->getValue ();
915
+ llvm::sys::path::append (CoverageFilename, OutputFilename);
909
916
}
917
+ llvm::sys::path::replace_extension (CoverageFilename, " gcda" );
918
+ CmdArgs.push_back (" -coverage-data-file" );
919
+ CmdArgs.push_back (Args.MakeArgString (CoverageFilename));
910
920
}
911
921
}
912
922
}
0 commit comments