Refactor the call to llvm::sys::fs::getMainExecutable.

Taking the address of `main` is UB in C++. Reading LLVM's source, any function address can be used (that's what clang does too).
This commit is contained in:
Johan Engelen 2016-07-27 20:26:54 +02:00
parent 246c0c359c
commit c3fe8f3fdd
4 changed files with 12 additions and 5 deletions

View file

@ -19,9 +19,15 @@ namespace {
string exePath;
}
void exe_path::initialize(const char *arg0, void *mainAddress) {
void exe_path::initialize(const char *arg0) {
assert(exePath.empty());
exePath = llvm::sys::fs::getMainExecutable(arg0, mainAddress);
// Some platforms can't implement LLVM's getMainExecutable
// without being given the address of a function in the main executable.
// Thus getMainExecutable needs the address of a function;
// any function address in the main executable will do.
exePath = llvm::sys::fs::getMainExecutable(
arg0, reinterpret_cast<void *>(&exe_path::initialize));
}
const string &exe_path::getExePath() {