sys::Path WriteGraph(const GraphType &G,
const std::string& Name,
const std::string& Title = "") {
- sys::Path Filename = sys::Path::GetTemporaryDirectory();;
+ std::string ErrMsg;
+ sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
+ if (Filename.isEmpty()) {
+ std::cerr << "Error: " << ErrMsg << "\n";
+ return Filename;
+ }
Filename.appendComponent(Name + ".dot");
Filename.makeUnique();
std::cerr << "Writing '" << Filename << "'... ";
/// a "standard" place for the operating system. The directory is
/// guaranteed to be created on exit from this function. If the directory
/// cannot be created, the function will throw an exception.
- /// @throws std::string indicating why the directory could not be created.
+ /// @returns an invalid path (empty) on error
+ /// @param ErrMsg Optional place for an error message if an error occurs
/// @brief Constrct a path to an new, unique, existing temporary
/// directory.
- static Path GetTemporaryDirectory();
+ static Path GetTemporaryDirectory(std::string* ErrMsg);
/// Construct a vector of sys::Path that contains the "standard" system
/// library paths suitable for linking into programs. This function *must*
/// @throws std::string if \p unverified_path is not legal.
/// @param unverified_path The path to verify and assign.
/// @brief Construct a Path from a string.
- explicit Path(const std::string& unverified_path);
+ explicit Path(const std::string& p) : path(p) {}
/// @}
/// @name Operators
namespace llvm {
using namespace sys;
-Path::Path(const std::string& unverified_path) : path(unverified_path) {
- if (unverified_path.empty())
- return;
- if (this->isValid())
- return;
- // oops, not valid.
- path.clear();
- ThrowErrno(unverified_path + ": path is not valid");
-}
-
bool
Path::isValid() const {
// Check some obvious things
}
Path
-Path::GetTemporaryDirectory() {
+Path::GetTemporaryDirectory(std::string* ErrMsg ) {
#if defined(HAVE_MKDTEMP)
// The best way is with mkdtemp but that's not available on many systems,
// Linux and FreeBSD have it. Others probably won't.
char pathname[MAXPATHLEN];
strcpy(pathname,"/tmp/llvm_XXXXXX");
- if (0 == mkdtemp(pathname))
- ThrowErrno(std::string(pathname) + ": can't create temporary directory");
+ if (0 == mkdtemp(pathname)) {
+ MakeErrMsg(ErrMsg,
+ std::string(pathname) + ": can't create temporary directory");
+ return Path();
+ }
Path result;
result.set(pathname);
assert(result.isValid() && "mkdtemp didn't create a valid pathname!");
char pathname[MAXPATHLEN];
strcpy(pathname, "/tmp/llvm_XXXXXX");
int fd = 0;
- if (-1 == (fd = mkstemp(pathname)))
- ThrowErrno(std::string(pathname) + ": can't create temporary directory");
+ if (-1 == (fd = mkstemp(pathname))) {
+ MakeErrMsg(ErrMsg,
+ std::string(pathname) + ": can't create temporary directory");
+ return Path();
+ }
::close(fd);
::unlink(pathname); // start race condition, ignore errors
- if (-1 == ::mkdir(pathname, S_IRWXU)) // end race condition
- ThrowErrno(std::string(pathname) + ": can't create temporary directory");
+ if (-1 == ::mkdir(pathname, S_IRWXU)) { // end race condition
+ MakeErrMsg(ErrMsg,
+ std::string(pathname) + ": can't create temporary directory");
+ return Path();
+ }
Path result;
result.set(pathname);
assert(result.isValid() && "mkstemp didn't create a valid pathname!");
char pathname[MAXPATHLEN];
strcpy(pathname, "/tmp/llvm_XXXXXX");
char *TmpName = ::mktemp(pathname);
- if (TmpName == 0)
- ThrowErrno(std::string(TmpName) + ": can't create unique directory name");
- if (-1 == ::mkdir(TmpName, S_IRWXU))
- ThrowErrno(std::string(TmpName) + ": can't create temporary directory");
+ if (TmpName == 0) {
+ MakeErrMsg(ErrMsg,
+ std::string(TmpName) + ": can't create unique directory name");
+ return Path();
+ }
+ if (-1 == ::mkdir(TmpName, S_IRWXU)) {
+ MakeErrMsg(ErrMsg,
+ std::string(TmpName) + ": can't create temporary directory");
+ return Path();
+ }
Path result;
result.set(TmpName);
assert(result.isValid() && "mktemp didn't create a valid pathname!");
num++;
sprintf(pathname, "/tmp/llvm_%010u", unsigned(num));
} while ( 0 == access(pathname, F_OK ) );
- if (-1 == ::mkdir(pathname, S_IRWXU))
- ThrowErrno(std::string(pathname) + ": can't create temporary directory");
+ if (-1 == ::mkdir(pathname, S_IRWXU)) {
+ MakeErrMsg(ErrMsg,
+ std::string(pathname) + ": can't create temporary directory");
+ return Path();
Path result;
result.set(pathname);
assert(result.isValid() && "mkstemp didn't create a valid pathname!");
, TempDir()
, AdditionalArgs()
{
- TempDir = sys::Path::GetTemporaryDirectory();
- sys::RemoveDirectoryOnSignal(TempDir);
AdditionalArgs.reserve(NUM_PHASES);
StringVector emptyVec;
for (unsigned i = 0; i < NUM_PHASES; ++i)
}
sys::Path MakeTempFile(const std::string& basename,
- const std::string& suffix) {
+ const std::string& suffix,
+ std::string* ErrMsg) {
+ if (TempDir.isEmpty()) {
+ TempDir = sys::Path::GetTemporaryDirectory(ErrMsg);
+ if (TempDir.isEmpty())
+ return sys::Path();
+ sys::RemoveDirectoryOnSignal(TempDir);
+ }
sys::Path result(TempDir);
- if (!result.appendComponent(basename))
- throw basename + ": can't use this file name";
- if (!result.appendSuffix(suffix))
- throw suffix + ": can't use this file suffix";
+ if (!result.appendComponent(basename)) {
+ if (ErrMsg)
+ *ErrMsg = basename + ": can't use this file name";
+ return sys::Path();
+ }
+ if (!result.appendSuffix(suffix)) {
+ if (ErrMsg)
+ *ErrMsg = suffix + ": can't use this file suffix";
+ return sys::Path();
+ }
return result;
}
actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
}
} else {
- sys::Path TempFile(MakeTempFile(I->first.getBasename(),"E"));
+ sys::Path TempFile(
+ MakeTempFile(I->first.getBasename(),"E",&ErrMsg));
+ if (TempFile.isEmpty())
+ return 1;
actions.push_back(GetAction(cd,InFile,TempFile,
PREPROCESSING));
InFile = TempFile;
actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
}
} else {
- sys::Path TempFile(MakeTempFile(I->first.getBasename(),"trans"));
+ sys::Path TempFile(
+ MakeTempFile(I->first.getBasename(),"trans", &ErrMsg));
+ if (TempFile.isEmpty())
+ return 1;
actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
InFile = TempFile;
}
actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
}
} else {
- sys::Path TempFile(MakeTempFile(I->first.getBasename(),"opt"));
+ sys::Path TempFile(
+ MakeTempFile(I->first.getBasename(),"opt", &ErrMsg));
+ if (TempFile.isEmpty())
+ return 1;
actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
InFile = TempFile;
}