Skip to content

Commit b95359f

Browse files
author
Daniel Kroening
authored
Merge pull request #1298 from diffblue/source-locations-with-working-directory
output the working directory for source locations in XML and json
2 parents 6807bed + 964a85c commit b95359f

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

src/util/file_util.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ Date: January 2012
1313

1414
#include "file_util.h"
1515

16+
#include "invariant.h"
17+
1618
#include <cerrno>
19+
#include <cstring>
1720

1821
#if defined(__linux__) || \
1922
defined(__FreeBSD_kernel__) || \
@@ -36,29 +39,26 @@ Date: January 2012
3639
#define chdir _chdir
3740
#define popen _popen
3841
#define pclose _pclose
39-
#else
40-
#include <cstring>
4142
#endif
4243

4344
/// \return current working directory
4445
std::string get_current_working_directory()
4546
{
46-
unsigned bsize=50;
47-
48-
char *buf=reinterpret_cast<char*>(malloc(sizeof(char)*bsize));
49-
if(!buf)
50-
abort();
51-
47+
#ifndef _WIN32
5248
errno=0;
49+
char *wd=realpath(".", nullptr);
50+
INVARIANT(
51+
wd!=nullptr && errno==0,
52+
std::string("realpath failed: ")+strerror(errno));
5353

54-
while(buf && getcwd(buf, bsize-1)==nullptr && errno==ERANGE)
55-
{
56-
bsize*=2;
57-
buf=reinterpret_cast<char*>(realloc(buf, sizeof(char)*bsize));
58-
}
59-
60-
std::string working_directory=buf;
61-
free(buf);
54+
std::string working_directory=wd;
55+
free(wd);
56+
#else
57+
char buffer[4096];
58+
DWORD retval=GetCurrentDirectory(4096, buffer);
59+
CHECK_RETURN(retval>0);
60+
std::string working_directory(buffer);
61+
#endif
6262

6363
return working_directory;
6464
}

src/util/json_expr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ json_objectt json(const source_locationt &location)
8484
{
8585
json_objectt result;
8686

87+
if(!location.get_working_directory().empty())
88+
result["workingDirectory"]=
89+
json_stringt(id2string(location.get_working_directory()));
90+
8791
if(!location.get_file().empty())
8892
result["file"]=json_stringt(id2string(location.get_file()));
8993

src/util/xml_expr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ xmlt xml(const source_locationt &location)
2828

2929
result.name="location";
3030

31+
if(!location.get_working_directory().empty())
32+
result.set_attribute(
33+
"working-directory", id2string(location.get_working_directory()));
34+
3135
if(!location.get_file().empty())
3236
result.set_attribute("file", id2string(location.get_file()));
3337

0 commit comments

Comments
 (0)