Skip to content

Commit 480cb99

Browse files
committed
Record where the GCOV data files should be placed.
llvm-svn: 130866
1 parent 63cc1de commit 480cb99

File tree

6 files changed

+43
-0
lines changed

6 files changed

+43
-0
lines changed

clang/include/clang/Driver/CC1Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ def femit_coverage_notes : Flag<"-femit-coverage-notes">,
140140
HelpText<"Emit a gcov coverage notes file when compiling.">;
141141
def femit_coverage_data: Flag<"-femit-coverage-data">,
142142
HelpText<"Instrument the program to emit gcov coverage data when run.">;
143+
def coverage_dir : Separate<"-coverage-dir">,
144+
HelpText<"Emit coverage data to this directory.">;
145+
def coverage_dir_EQ : Joined<"-coverage-dir=">, Alias<coverage_dir>;
143146
def relaxed_aliasing : Flag<"-relaxed-aliasing">,
144147
HelpText<"Turn off Type Based Alias Analysis">;
145148
def masm_verbose : Flag<"-masm-verbose">,

clang/include/clang/Frontend/CodeGenOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ class CodeGenOptions {
9595
/// The code model to use (-mcmodel).
9696
std::string CodeModel;
9797

98+
/// The directory in which to place coverage data files.
99+
std::string CoverageDir;
100+
98101
/// Enable additional debugging information.
99102
std::string DebugPass;
100103

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ void CodeGenModule::Release() {
132132

133133
if (getCodeGenOpts().EmitDeclMetadata)
134134
EmitDeclMetadata();
135+
136+
if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes)
137+
EmitCoverageDir();
135138
}
136139

137140
void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
@@ -2216,6 +2219,23 @@ void CodeGenFunction::EmitDeclMetadata() {
22162219
}
22172220
}
22182221

2222+
void CodeGenModule::EmitCoverageDir() {
2223+
if (!getCodeGenOpts().CoverageDir.empty()) {
2224+
if (llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu")) {
2225+
llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov");
2226+
llvm::LLVMContext &Ctx = TheModule.getContext();
2227+
llvm::MDString *CoverageDir =
2228+
llvm::MDString::get(Ctx, getCodeGenOpts().CoverageDir);
2229+
for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) {
2230+
llvm::MDNode *CU = CUNode->getOperand(i);
2231+
llvm::Value *node[] = { CoverageDir, CU };
2232+
llvm::MDNode *N = llvm::MDNode::get(Ctx, node);
2233+
GCov->addOperand(N);
2234+
}
2235+
}
2236+
}
2237+
}
2238+
22192239
///@name Custom Runtime Function Interfaces
22202240
///@{
22212241
//

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,10 @@ class CodeGenModule : public CodeGenTypeCache {
735735

736736
void EmitDeclMetadata();
737737

738+
/// EmitCoverageDir - Emit the llvm.gcov metadata used to tell LLVM where
739+
/// to emit the .gcno and .gcda files in a way that persists in .bc files.
740+
void EmitCoverageDir();
741+
738742
/// MayDeferGeneration - Determine if the given decl can be emitted
739743
/// lazily; this is only relevant for definitions. The given decl
740744
/// must be either a function or var decl.

clang/lib/Driver/Tools.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,18 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
13031303
Args.hasArg(options::OPT_coverage))
13041304
CmdArgs.push_back("-femit-coverage-data");
13051305

1306+
if (C.getArgs().hasArg(options::OPT_c) ||
1307+
C.getArgs().hasArg(options::OPT_S)) {
1308+
if (Output.isFilename()) {
1309+
llvm::StringRef CoverageDir =
1310+
llvm::sys::path::parent_path(Output.getFilename());
1311+
if (!CoverageDir.empty()) {
1312+
CmdArgs.push_back("-coverage-dir");
1313+
CmdArgs.push_back(Args.MakeArgString(CoverageDir));
1314+
}
1315+
}
1316+
}
1317+
13061318
Args.AddLastArg(CmdArgs, options::OPT_nostdinc);
13071319
Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
13081320
Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,7 @@ static void ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
977977
Opts.InstrumentForProfiling = Args.hasArg(OPT_pg);
978978
Opts.EmitGcovArcs = Args.hasArg(OPT_femit_coverage_data);
979979
Opts.EmitGcovNotes = Args.hasArg(OPT_femit_coverage_notes);
980+
Opts.CoverageDir = Args.getLastArgValue(OPT_coverage_dir);
980981

981982
if (Arg *A = Args.getLastArg(OPT_fobjc_dispatch_method_EQ)) {
982983
llvm::StringRef Name = A->getValue(Args);

0 commit comments

Comments
 (0)