Use the full path when outputting the `.gcda' file.

If we compile a single source program, the `.gcda' file will be generated where
the program was executed. This isn't desirable, because that place may be at an
unpredictable place (the program could call `chdir' for instance).

Instead, we will output the `.gcda' file in the same place we output the `.gcno'
file. I.e., the directory where the executable was generated. This matches GCC's
behavior.

<rdar://problem/13061072> & PR11809


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@178084 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Bill Wendling 2013-03-26 22:47:50 +00:00
parent f8fd883fd3
commit 39c41c3c93

View File

@ -32,6 +32,7 @@
#include "llvm/Support/CommandLine.h" #include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
#include "llvm/Support/DebugLoc.h" #include "llvm/Support/DebugLoc.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/InstIterator.h" #include "llvm/Support/InstIterator.h"
#include "llvm/Support/PathV2.h" #include "llvm/Support/PathV2.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
@ -123,7 +124,8 @@ namespace {
Function *insertFlush(ArrayRef<std::pair<GlobalVariable*, MDNode*> >); Function *insertFlush(ArrayRef<std::pair<GlobalVariable*, MDNode*> >);
void insertIndirectCounterIncrement(); void insertIndirectCounterIncrement();
std::string mangleName(DICompileUnit CU, const char *NewStem); std::string mangleName(DICompileUnit CU, const char *NewStem,
bool FullPath);
GCOVOptions Options; GCOVOptions Options;
@ -363,7 +365,8 @@ namespace {
}; };
} }
std::string GCOVProfiler::mangleName(DICompileUnit CU, const char *NewStem) { std::string GCOVProfiler::mangleName(DICompileUnit CU, const char *NewStem,
bool FullPath) {
if (NamedMDNode *GCov = M->getNamedMetadata("llvm.gcov")) { if (NamedMDNode *GCov = M->getNamedMetadata("llvm.gcov")) {
for (int i = 0, e = GCov->getNumOperands(); i != e; ++i) { for (int i = 0, e = GCov->getNumOperands(); i != e; ++i) {
MDNode *N = GCov->getOperand(i); MDNode *N = GCov->getOperand(i);
@ -381,7 +384,13 @@ std::string GCOVProfiler::mangleName(DICompileUnit CU, const char *NewStem) {
SmallString<128> Filename = CU.getFilename(); SmallString<128> Filename = CU.getFilename();
sys::path::replace_extension(Filename, NewStem); sys::path::replace_extension(Filename, NewStem);
return sys::path::filename(Filename.str()); StringRef FName = sys::path::filename(Filename);
if (!FullPath)
return FName;
SmallString<128> CurPath;
if (sys::fs::current_path(CurPath)) return FName;
sys::path::append(CurPath, FName.str());
return CurPath.str();
} }
bool GCOVProfiler::runOnModule(Module &M) { bool GCOVProfiler::runOnModule(Module &M) {
@ -404,7 +413,7 @@ void GCOVProfiler::emitProfileNotes() {
DICompileUnit CU(CU_Nodes->getOperand(i)); DICompileUnit CU(CU_Nodes->getOperand(i));
std::string ErrorInfo; std::string ErrorInfo;
raw_fd_ostream out(mangleName(CU, "gcno").c_str(), ErrorInfo, raw_fd_ostream out(mangleName(CU, "gcno", false).c_str(), ErrorInfo,
raw_fd_ostream::F_Binary); raw_fd_ostream::F_Binary);
out.write("oncg", 4); out.write("oncg", 4);
out.write(ReversedVersion, 4); out.write(ReversedVersion, 4);
@ -726,7 +735,7 @@ Function *GCOVProfiler::insertCounterWriteout(
if (CU_Nodes) { if (CU_Nodes) {
for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) { for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
DICompileUnit CU(CU_Nodes->getOperand(i)); DICompileUnit CU(CU_Nodes->getOperand(i));
std::string FilenameGcda = mangleName(CU, "gcda"); std::string FilenameGcda = mangleName(CU, "gcda", true);
Builder.CreateCall2(StartFile, Builder.CreateCall2(StartFile,
Builder.CreateGlobalStringPtr(FilenameGcda), Builder.CreateGlobalStringPtr(FilenameGcda),
Builder.CreateGlobalStringPtr(ReversedVersion)); Builder.CreateGlobalStringPtr(ReversedVersion));