Skip to content

Commit 924a7d8

Browse files
authored
Use CLANG_RESOURCE_DIR more consistently (#103388)
When Clang is consumed as a library, the CLANG_RESOURCE_DIR definition is not exported from the CMake system, so external clients will be unable to compute the same resource dir as Clang itself would, because they don't know what to pass for the optional CustomResourceDir argument. All call sites except one would pass CLANG_RESOURCE_DIR to Driver::GetResourcesPath. It seems the one exception in libclang CIndexer was an oversight. Move the use of CLANG_RESOURCE_DIR into GetResourcesPath and remove the optional argument to avoid this inconsistency between internal and external clients.
1 parent 1387ba4 commit 924a7d8

File tree

5 files changed

+12
-13
lines changed

5 files changed

+12
-13
lines changed

clang/include/clang/Driver/Driver.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,7 @@ class Driver {
379379

380380
/// Takes the path to a binary that's either in bin/ or lib/ and returns
381381
/// the path to clang's resource directory.
382-
static std::string GetResourcesPath(StringRef BinaryPath,
383-
StringRef CustomResourceDir = "");
382+
static std::string GetResourcesPath(StringRef BinaryPath);
384383

385384
Driver(StringRef ClangExecutable, StringRef TargetTriple,
386385
DiagnosticsEngine &Diags, std::string Title = "clang LLVM compiler",

clang/lib/Driver/Driver.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,18 +171,18 @@ getHIPOffloadTargetTriple(const Driver &D, const ArgList &Args) {
171171
}
172172

173173
// static
174-
std::string Driver::GetResourcesPath(StringRef BinaryPath,
175-
StringRef CustomResourceDir) {
174+
std::string Driver::GetResourcesPath(StringRef BinaryPath) {
176175
// Since the resource directory is embedded in the module hash, it's important
177176
// that all places that need it call this function, so that they get the
178177
// exact same string ("a/../b/" and "b/" get different hashes, for example).
179178

180179
// Dir is bin/ or lib/, depending on where BinaryPath is.
181-
std::string Dir = std::string(llvm::sys::path::parent_path(BinaryPath));
182-
180+
StringRef Dir = llvm::sys::path::parent_path(BinaryPath);
183181
SmallString<128> P(Dir);
184-
if (CustomResourceDir != "") {
185-
llvm::sys::path::append(P, CustomResourceDir);
182+
183+
StringRef ConfiguredResourceDir(CLANG_RESOURCE_DIR);
184+
if (!ConfiguredResourceDir.empty()) {
185+
llvm::sys::path::append(P, ConfiguredResourceDir);
186186
} else {
187187
// On Windows, libclang.dll is in bin/.
188188
// On non-Windows, libclang.so/.dylib is in lib/.
@@ -239,7 +239,7 @@ Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple,
239239
#endif
240240

241241
// Compute the path to the resource directory.
242-
ResourceDir = GetResourcesPath(ClangExecutable, CLANG_RESOURCE_DIR);
242+
ResourceDir = GetResourcesPath(ClangExecutable);
243243
}
244244

245245
void Driver::setDriverMode(StringRef Value) {

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3130,7 +3130,7 @@ std::string CompilerInvocation::GetResourcesPath(const char *Argv0,
31303130
void *MainAddr) {
31313131
std::string ClangExecutable =
31323132
llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
3133-
return Driver::GetResourcesPath(ClangExecutable, CLANG_RESOURCE_DIR);
3133+
return Driver::GetResourcesPath(ClangExecutable);
31343134
}
31353135

31363136
static void GenerateHeaderSearchArgs(const HeaderSearchOptions &Opts,

lldb/source/Plugins/ExpressionParser/Clang/ClangHost.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static bool DefaultComputeClangResourceDirectory(FileSpec &lldb_shlib_spec,
5353
std::string raw_path = lldb_shlib_spec.GetPath();
5454
llvm::StringRef parent_dir = llvm::sys::path::parent_path(raw_path);
5555
static const std::string clang_resource_path =
56-
clang::driver::Driver::GetResourcesPath("bin/lldb", CLANG_RESOURCE_DIR);
56+
clang::driver::Driver::GetResourcesPath("bin/lldb");
5757

5858
static const llvm::StringRef kResourceDirSuffixes[] = {
5959
// LLVM.org's build of LLDB uses the clang resource directory placed

lldb/unittests/Expression/ClangParserTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ TEST_F(ClangHostTest, ComputeClangResourceDirectory) {
4242
#else
4343
std::string path_to_liblldb = "C:\\foo\\bar\\lib\\";
4444
#endif
45-
std::string path_to_clang_dir = clang::driver::Driver::GetResourcesPath(
46-
path_to_liblldb + "liblldb", CLANG_RESOURCE_DIR);
45+
std::string path_to_clang_dir =
46+
clang::driver::Driver::GetResourcesPath(path_to_liblldb + "liblldb");
4747
llvm::SmallString<256> path_to_clang_lib_dir_real;
4848
llvm::sys::fs::real_path(path_to_clang_dir, path_to_clang_lib_dir_real);
4949

0 commit comments

Comments
 (0)