Skip to content

Commit 1aca304

Browse files
committed
refactor: temp directories use output path
1 parent 691ce1d commit 1aca304

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

src/lib/Support/Path.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,28 @@ ScopedTempDirectory(
504504
}
505505
}
506506

507+
ScopedTempDirectory::
508+
ScopedTempDirectory(
509+
llvm::StringRef root,
510+
llvm::StringRef dir)
511+
{
512+
llvm::SmallString<128> tempPath(root);
513+
llvm::sys::path::append(tempPath, dir);
514+
bool const exists = llvm::sys::fs::exists(tempPath);
515+
if (exists)
516+
{
517+
ok_ = !llvm::sys::fs::remove_directories(tempPath);
518+
if (!ok_)
519+
{
520+
return;
521+
}
522+
}
523+
ok_ = !llvm::sys::fs::create_directory(tempPath);
524+
if (ok_)
525+
{
526+
path_ = tempPath;
527+
}
528+
}
507529

508530
} // mrdocs
509531
} // clang

src/lib/Support/Path.hpp

+21-1
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,37 @@ class ScopedTempDirectory
107107
<tempdir>/<prefix><random>
108108
@endcode
109109
110+
For instance, if the prefix is "mrdocs" and the operating system
111+
is Unix, the directory might be created as: "/tmp/mrdocs-1234".
112+
113+
On Windows, the directory might be created as:
114+
"C:\Users\user\AppData\Local\Temp\mrdocs-1234".
115+
110116
@param prefix The prefix for the temporary directory name.
111117
*/
112118
ScopedTempDirectory(llvm::StringRef prefix);
113119

120+
/** Constructor with a specific path
121+
122+
Creates a temporary directory with the given path.
123+
The directory is deleted when this object goes out of scope.
124+
125+
@param root The root directory for the temporary directory.
126+
@param dir The name of the temporary directory.
127+
*/
128+
ScopedTempDirectory(llvm::StringRef root, llvm::StringRef dir);
129+
114130
/** Returns `true` if the directory was created successfully.
115131
*/
116132
operator bool() const { return ok_; }
117133

118134
/** Returns the path to the temporary directory.
119135
*/
120-
llvm::StringRef path() const { return path_; }
136+
std::string_view path() const { return static_cast<llvm::StringRef>(path_); }
137+
138+
/** Convert temp directory to a std::string_view
139+
*/
140+
operator std::string_view() const { return path(); }
121141
};
122142

123143
} // mrdocs

src/tool/GenerateAction.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,16 @@ DoGenerateAction(
127127
if (compilationDatabasePath.empty())
128128
{
129129
std::string c = files::appendPath(settings.sourceRoot, "CMakeLists.txt");
130-
if (files::exists(c)) {
130+
if (files::exists(c))
131+
{
131132
compilationDatabasePath = c;
132133
}
133134
}
134135
MRDOCS_CHECK(
135136
compilationDatabasePath,
136137
"The compilation database path argument is missing");
137-
ScopedTempDirectory tempDir("mrdocs");
138-
std::string buildPath = files::appendPath(tempDir.path(), "build");
138+
ScopedTempDirectory tempDir(config->settings().output, ".temp");
139+
std::string buildPath = files::appendPath(tempDir, "build");
139140
Expected<std::string> const compileCommandsPathExp =
140141
generateCompileCommandsFile(
141142
compilationDatabasePath, settings.cmake, buildPath);

0 commit comments

Comments
 (0)