Skip to content

Commit e440d23

Browse files
committed
Only pass -coverage-notes-file when emitting coverage
The only functional change here is that -coverage-notes-file is not passed to -cc1 in some situations. This code appears to be trying to put the gcno and gcda output next to the final object file, but it's doing that in a really convoluted way that needs to be re-examined. It looks for -c or -S in the original command, and then looks at the -o argument if present in order to handle the -fno-integrated-as case. However, this doesn't work if this is a link command with multiple inputs. I looked into fixing this, but the check-profile test suite has a lot of dependencies on this behavior, so I left it all alone. llvm-svn: 373004
1 parent 2fce113 commit e440d23

File tree

2 files changed

+52
-36
lines changed

2 files changed

+52
-36
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -832,12 +832,14 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
832832
}
833833
}
834834

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)
837841
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)
841843
CmdArgs.push_back("-femit-coverage-data");
842844

843845
if (Args.hasFlag(options::OPT_fcoverage_mapping,
@@ -873,40 +875,48 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
873875
CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-filter-files=" + v)));
874876
}
875877

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);
892905
}
893-
llvm::sys::path::replace_extension(CoverageFilename, "gcno");
894-
CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
906+
}
907+
llvm::sys::path::replace_extension(CoverageFilename, "gcno");
895908

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);
909916
}
917+
llvm::sys::path::replace_extension(CoverageFilename, "gcda");
918+
CmdArgs.push_back("-coverage-data-file");
919+
CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
910920
}
911921
}
912922
}

clang/test/Driver/coverage_no_integrated_as.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// REQUIRES: clang-driver
22

3+
// RUN: %clang -### -c %s 2>&1 | FileCheck -check-prefix=CHECK-NO-COV %s
4+
35
// RUN: %clang -### -S -fprofile-arcs %s 2>&1 | FileCheck -check-prefix=CHECK-GCNO-DEFAULT-LOCATION %s
46
// RUN: %clang -### -S -fprofile-arcs --target=%itanium_abi_triple -no-integrated-as %s 2>&1 | FileCheck -check-prefix=CHECK-GCNO-DEFAULT-LOCATION %s
57
// RUN: %clang -### -c -fprofile-arcs %s 2>&1 | FileCheck -check-prefix=CHECK-GCNO-DEFAULT-LOCATION %s
@@ -15,6 +17,10 @@
1517
// RUN: %clang -### -c -fprofile-arcs %s -o foo/bar.o 2>&1 | FileCheck -check-prefix=CHECK-GCNO-LOCATION-REL-PATH %s
1618
// RUN: %clang -### -c -fprofile-arcs --target=%itanium_abi_triple -no-integrated-as %s -o foo/bar.o 2>&1 | FileCheck -check-prefix=CHECK-GCNO-LOCATION-REL-PATH %s
1719

20+
// These should only get passed if any of --coverage, -ftest-coverage, or
21+
// -fprofile-arcs is passed.
22+
// CHECK-NO-COV-NOT: "-coverage-notes-file"
23+
// CHECK-NO-COV-NOT: "-coverage-data-file"
1824

1925
// CHECK-GCNO-DEFAULT-LOCATION: "-coverage-notes-file" "{{.*}}{{/|\\\\}}coverage_no_integrated_as.c"
2026
// CHECK-GCNO-DEFAULT-LOCATION-NOT: "-coverage-notes-file" "/tmp/{{.*}}/coverage_no_integrated_as.c"

0 commit comments

Comments
 (0)