Skip to content

Commit 26a940e

Browse files
committed
Move GetResourcesPath from clangDriver into clangOptions
1 parent 458e1e0 commit 26a940e

File tree

14 files changed

+73
-68
lines changed

14 files changed

+73
-68
lines changed

clang-tools-extra/clangd/CompileCommands.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ std::optional<std::string> detectSysroot() {
132132

133133
std::string detectStandardResourceDir() {
134134
static int StaticForMainAddr; // Just an address in this process.
135-
return driver::Driver::GetResourcesPathForInvocation(
136-
"clangd", (void *)&StaticForMainAddr);
135+
return GetResourcesPath("clangd", (void *)&StaticForMainAddr);
137136
}
138137

139138
// The path passed to argv[0] is important:

clang/include/clang/Driver/Driver.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -406,21 +406,6 @@ class Driver {
406406
SmallString<128> &CrashDiagDir);
407407

408408
public:
409-
410-
/// Takes the path to a binary that's either in bin/ or lib/ and returns
411-
/// the path to clang's resource directory.
412-
static std::string GetResourcesPath(StringRef BinaryPath);
413-
414-
/// Get the directory where the compiler headers reside, relative to the
415-
/// compiler binary (found by the passed in arguments).
416-
///
417-
/// \param Argv0 - The program path (from argv[0]), for finding the builtin
418-
/// compiler path.
419-
/// \param MainAddr - The address of main (or some other function in the main
420-
/// executable), for finding the builtin compiler path.
421-
static std::string GetResourcesPathForInvocation(const char *Argv0,
422-
void *MainAddr);
423-
424409
Driver(StringRef ClangExecutable, StringRef TargetTriple,
425410
DiagnosticsEngine &Diags, std::string Title = "clang LLVM compiler",
426411
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr);

clang/include/clang/Options/OptionUtils.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ArgList;
2828
} // namespace llvm
2929

3030
namespace clang {
31+
3132
/// Return the value of the last argument as an integer, or a default. If Diags
3233
/// is non-null, emits an error if the argument is given, but non-integral.
3334
int getLastArgIntValue(const llvm::opt::ArgList &Args,
@@ -63,6 +64,19 @@ StringRef parseMPreferVectorWidthOption(clang::DiagnosticsEngine &Diags,
6364
StringRef parseMRecipOption(clang::DiagnosticsEngine &Diags,
6465
const llvm::opt::ArgList &Args);
6566

67+
/// Get the directory where the compiler headers reside, relative to the
68+
/// compiler binary path \p BinaryPath.
69+
std::string GetResourcesPath(StringRef BinaryPath);
70+
71+
/// Get the directory where the compiler headers reside, relative to the
72+
/// compiler binary path (found by the passed in arguments).
73+
///
74+
/// \param Argv0 The program path (from argv[0]), for finding the builtin
75+
/// compiler path.
76+
/// \param MainAddr The address of main (or some other function in the main
77+
/// executable), for finding the builtin compiler path.
78+
std::string GetResourcesPath(const char *Argv0, void *MainAddr);
79+
6680
} // namespace clang
6781

6882
#endif // LLVM_CLANG_OPTIONS_OPTIONUTILS_H

clang/lib/Driver/Driver.cpp

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#include "clang/Driver/ToolChain.h"
6767
#include "clang/Driver/Types.h"
6868
#include "clang/Lex/DependencyDirectivesScanner.h"
69+
#include "clang/Options/OptionUtils.h"
6970
#include "clang/Options/Options.h"
7071
#include "llvm/ADT/ArrayRef.h"
7172
#include "llvm/ADT/STLExtras.h"
@@ -123,46 +124,6 @@ template <typename F> static bool usesInput(const ArgList &Args, F &&Fn) {
123124
});
124125
}
125126

126-
std::string Driver::GetResourcesPath(StringRef BinaryPath) {
127-
// Since the resource directory is embedded in the module hash, it's important
128-
// that all places that need it call this function, so that they get the
129-
// exact same string ("a/../b/" and "b/" get different hashes, for example).
130-
131-
// Dir is bin/ or lib/, depending on where BinaryPath is.
132-
StringRef Dir = llvm::sys::path::parent_path(BinaryPath);
133-
SmallString<128> P(Dir);
134-
135-
StringRef ConfiguredResourceDir(CLANG_RESOURCE_DIR);
136-
if (!ConfiguredResourceDir.empty()) {
137-
// FIXME: We should fix the behavior of llvm::sys::path::append so we don't
138-
// need to check for absolute paths here.
139-
if (llvm::sys::path::is_absolute(ConfiguredResourceDir))
140-
P = ConfiguredResourceDir;
141-
else
142-
llvm::sys::path::append(P, ConfiguredResourceDir);
143-
} else {
144-
// On Windows, libclang.dll is in bin/.
145-
// On non-Windows, libclang.so/.dylib is in lib/.
146-
// With a static-library build of libclang, LibClangPath will contain the
147-
// path of the embedding binary, which for LLVM binaries will be in bin/.
148-
// ../lib gets us to lib/ in both cases.
149-
P = llvm::sys::path::parent_path(Dir);
150-
// This search path is also created in the COFF driver of lld, so any
151-
// changes here also needs to happen in lld/COFF/Driver.cpp
152-
llvm::sys::path::append(P, CLANG_INSTALL_LIBDIR_BASENAME, "clang",
153-
CLANG_VERSION_MAJOR_STRING);
154-
}
155-
156-
return std::string(P);
157-
}
158-
159-
std::string Driver::GetResourcesPathForInvocation(const char *Argv0,
160-
void *MainAddr) {
161-
std::string ClangExecutable =
162-
llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
163-
return GetResourcesPath(ClangExecutable);
164-
}
165-
166127
CUIDOptions::CUIDOptions(llvm::opt::DerivedArgList &Args, const Driver &D)
167128
: UseCUID(Kind::Hash) {
168129
if (Arg *A = Args.getLastArg(options::OPT_fuse_cuid_EQ)) {

clang/lib/Interpreter/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ add_clang_library(clangInterpreter
4646
clangFrontend
4747
clangFrontendTool
4848
clangLex
49+
clangOptions
4950
clangParse
5051
clangSema
5152
clangSerialization

clang/lib/Interpreter/Interpreter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "clang/Interpreter/Interpreter.h"
4343
#include "clang/Interpreter/Value.h"
4444
#include "clang/Lex/PreprocessorOptions.h"
45+
#include "clang/Options/OptionUtils.h"
4546
#include "clang/Options/Options.h"
4647
#include "clang/Sema/Lookup.h"
4748
#include "clang/Serialization/ObjectFilePCHContainerReader.h"
@@ -105,7 +106,7 @@ CreateCI(const llvm::opt::ArgStringList &Argv) {
105106
if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
106107
Clang->getHeaderSearchOpts().ResourceDir.empty())
107108
Clang->getHeaderSearchOpts().ResourceDir =
108-
driver::Driver::GetResourcesPathForInvocation(Argv[0], nullptr);
109+
GetResourcesPath(Argv[0], nullptr);
109110

110111
Clang->createVirtualFileSystem();
111112

clang/lib/Options/OptionUtils.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
#include "clang/Options/OptionUtils.h"
1010
#include "clang/Basic/Diagnostic.h"
1111
#include "clang/Basic/DiagnosticDriver.h"
12+
#include "clang/Basic/Version.h"
13+
#include "clang/Config/config.h"
1214
#include "clang/Options/Options.h"
1315
#include "llvm/Option/ArgList.h"
16+
#include "llvm/Support/FileSystem.h"
17+
#include "llvm/Support/Path.h"
1418

1519
using namespace clang;
1620
using namespace llvm::opt;
@@ -204,4 +208,43 @@ StringRef parseMRecipOption(clang::DiagnosticsEngine &Diags,
204208
return Out;
205209
}
206210

211+
std::string GetResourcesPath(StringRef BinaryPath) {
212+
// Since the resource directory is embedded in the module hash, it's important
213+
// that all places that need it call this function, so that they get the
214+
// exact same string ("a/../b/" and "b/" get different hashes, for example).
215+
216+
// Dir is bin/ or lib/, depending on where BinaryPath is.
217+
StringRef Dir = llvm::sys::path::parent_path(BinaryPath);
218+
SmallString<128> P(Dir);
219+
220+
StringRef ConfiguredResourceDir(CLANG_RESOURCE_DIR);
221+
if (!ConfiguredResourceDir.empty()) {
222+
// FIXME: We should fix the behavior of llvm::sys::path::append so we don't
223+
// need to check for absolute paths here.
224+
if (llvm::sys::path::is_absolute(ConfiguredResourceDir))
225+
P = ConfiguredResourceDir;
226+
else
227+
llvm::sys::path::append(P, ConfiguredResourceDir);
228+
} else {
229+
// On Windows, libclang.dll is in bin/.
230+
// On non-Windows, libclang.so/.dylib is in lib/.
231+
// With a static-library build of libclang, LibClangPath will contain the
232+
// path of the embedding binary, which for LLVM binaries will be in bin/.
233+
// ../lib gets us to lib/ in both cases.
234+
P = llvm::sys::path::parent_path(Dir);
235+
// This search path is also created in the COFF driver of lld, so any
236+
// changes here also needs to happen in lld/COFF/Driver.cpp
237+
llvm::sys::path::append(P, CLANG_INSTALL_LIBDIR_BASENAME, "clang",
238+
CLANG_VERSION_MAJOR_STRING);
239+
}
240+
241+
return std::string(P);
242+
}
243+
244+
std::string GetResourcesPath(const char *Argv0, void *MainAddr) {
245+
const std::string ClangExecutable =
246+
llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
247+
return GetResourcesPath(ClangExecutable);
248+
}
249+
207250
} // namespace clang

clang/lib/Tooling/Tooling.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "clang/Frontend/TextDiagnosticPrinter.h"
3232
#include "clang/Lex/HeaderSearchOptions.h"
3333
#include "clang/Lex/PreprocessorOptions.h"
34+
#include "clang/Options/OptionUtils.h"
3435
#include "clang/Options/Options.h"
3536
#include "clang/Tooling/ArgumentsAdjusters.h"
3637
#include "clang/Tooling/CompilationDatabase.h"
@@ -510,9 +511,7 @@ static void injectResourceDir(CommandLineArguments &Args, const char *Argv0,
510511

511512
// If there's no override in place add our resource dir.
512513
Args = getInsertArgumentAdjuster(
513-
("-resource-dir=" +
514-
driver::Driver::GetResourcesPathForInvocation(Argv0, MainAddr))
515-
.c_str())(Args, "");
514+
("-resource-dir=" + GetResourcesPath(Argv0, MainAddr)).c_str())(Args, "");
516515
}
517516

518517
int ClangTool::run(ToolAction *Action) {

clang/tools/driver/cc1_main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
270270
if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
271271
Clang->getHeaderSearchOpts().ResourceDir.empty())
272272
Clang->getHeaderSearchOpts().ResourceDir =
273-
driver::Driver::GetResourcesPathForInvocation(Argv0, MainAddr);
273+
GetResourcesPath(Argv0, MainAddr);
274274

275275
/// Create the actual file system.
276276
Clang->createVirtualFileSystem(llvm::vfs::getRealFileSystem(), DiagsBuffer);

clang/tools/libclang/CIndexer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "clang/Basic/Version.h"
1717
#include "clang/Config/config.h"
1818
#include "clang/Driver/Driver.h"
19+
#include "clang/Options/OptionUtils.h"
1920
#include "llvm/ADT/STLExtras.h"
2021
#include "llvm/ADT/SmallString.h"
2122
#include "llvm/Support/FileSystem.h"
@@ -137,7 +138,7 @@ const std::string &CIndexer::getClangResourcesPath() {
137138
#endif
138139

139140
// Cache our result.
140-
ResourcesPath = driver::Driver::GetResourcesPath(LibClangPath);
141+
ResourcesPath = GetResourcesPath(LibClangPath);
141142
return ResourcesPath;
142143
}
143144

0 commit comments

Comments
 (0)