Index: include/llvm/MC/MCContext.h =================================================================== --- include/llvm/MC/MCContext.h +++ include/llvm/MC/MCContext.h @@ -11,6 +11,7 @@ #define LLVM_MC_MCCONTEXT_H #include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/Optional.h" #include "llvm/ADT/SetVector.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" @@ -116,7 +117,7 @@ bool SecureLogUsed; /// The compilation directory to use for DW_AT_comp_dir. - SmallString<128> CompilationDir; + mutable Optional> CompilationDir; /// The main file name if passed in explicitly. std::string MainFileName; @@ -392,11 +393,13 @@ /// compilation directory and have it be something other than the current /// working directory. /// Returns an empty string if the current directory cannot be determined. - StringRef getCompilationDir() const { return CompilationDir; } + StringRef getCompilationDir() const; /// \brief Set the compilation directory for DW_AT_comp_dir /// Override the default (CWD) compilation directory. - void setCompilationDir(StringRef S) { CompilationDir = S.str(); } + void setCompilationDir(StringRef S) { + CompilationDir = SmallString<128>(S.str()); + } /// \brief Get the main file name for use in error messages and debug /// info. This can be set to ensure we've got the correct file name Index: lib/MC/MCContext.cpp =================================================================== --- lib/MC/MCContext.cpp +++ lib/MC/MCContext.cpp @@ -42,11 +42,6 @@ GenDwarfForAssembly(false), GenDwarfFileNumber(0), DwarfVersion(4), AllowTemporaryLabels(true), DwarfCompileUnitID(0), AutoReset(DoAutoReset) { - - std::error_code EC = llvm::sys::fs::current_path(CompilationDir); - if (EC) - CompilationDir.clear(); - SecureLogFile = getenv("AS_SECURE_LOG_FILE"); SecureLog = nullptr; SecureLogUsed = false; @@ -67,6 +62,17 @@ delete (raw_ostream *)SecureLog; } +StringRef MCContext::getCompilationDir() const { + if (!CompilationDir) { + SmallString<128> Cwd; + std::error_code EC = llvm::sys::fs::current_path(Cwd); + if (EC) + Cwd.clear(); + CompilationDir = Cwd; + } + return *CompilationDir; +} + //===----------------------------------------------------------------------===// // Module Lifetime Management //===----------------------------------------------------------------------===// @@ -86,7 +92,7 @@ SectionSymbols.clear(); Allocator.Reset(); Instances.clear(); - CompilationDir.clear(); + CompilationDir.reset(); MainFileName.clear(); MCDwarfLineTablesCUMap.clear(); SectionsForRanges.clear();