Skip to content

Commit d6cbcf9

Browse files
authored
[lldb][ClangExpressionParser][NFC] Factor out TargetOpts logic out of ClangExpressionParser (llvm#101681)
Same motivation as llvm#101669. We plan to eventually use the Clang driver to initialize the `CompilerInstance`. This should make refactorings of this code more straightforward. **Changes**: * Introduced `SetupTargetOpts` * Called them from `ClangExpressionParser::ClangExpressionParser` * Made `GetClangTargetABI` a file-local function since it's not using any of the state in `ClangExpressionParser`, and isn't used anywhere outside the source file
1 parent b1df554 commit d6cbcf9

File tree

2 files changed

+75
-76
lines changed

2 files changed

+75
-76
lines changed

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

Lines changed: 75 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,80 @@ static void SetupDefaultClangDiagnostics(CompilerInstance &compiler) {
351351
}
352352
}
353353

354+
/// Returns a string representing current ABI.
355+
///
356+
/// \param[in] target_arch
357+
/// The target architecture.
358+
///
359+
/// \return
360+
/// A string representing target ABI for the current architecture.
361+
static std::string GetClangTargetABI(const ArchSpec &target_arch) {
362+
std::string abi;
363+
364+
if (target_arch.IsMIPS()) {
365+
switch (target_arch.GetFlags() & ArchSpec::eMIPSABI_mask) {
366+
case ArchSpec::eMIPSABI_N64:
367+
abi = "n64";
368+
break;
369+
case ArchSpec::eMIPSABI_N32:
370+
abi = "n32";
371+
break;
372+
case ArchSpec::eMIPSABI_O32:
373+
abi = "o32";
374+
break;
375+
default:
376+
break;
377+
}
378+
}
379+
return abi;
380+
}
381+
382+
static void SetupTargetOpts(CompilerInstance &compiler,
383+
lldb_private::Target const &target) {
384+
Log *log = GetLog(LLDBLog::Expressions);
385+
ArchSpec target_arch = target.GetArchitecture();
386+
387+
const auto target_machine = target_arch.GetMachine();
388+
if (target_arch.IsValid()) {
389+
std::string triple = target_arch.GetTriple().str();
390+
compiler.getTargetOpts().Triple = triple;
391+
LLDB_LOGF(log, "Using %s as the target triple",
392+
compiler.getTargetOpts().Triple.c_str());
393+
} else {
394+
// If we get here we don't have a valid target and just have to guess.
395+
// Sometimes this will be ok to just use the host target triple (when we
396+
// evaluate say "2+3", but other expressions like breakpoint conditions and
397+
// other things that _are_ target specific really shouldn't just be using
398+
// the host triple. In such a case the language runtime should expose an
399+
// overridden options set (3), below.
400+
compiler.getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
401+
LLDB_LOGF(log, "Using default target triple of %s",
402+
compiler.getTargetOpts().Triple.c_str());
403+
}
404+
// Now add some special fixes for known architectures: Any arm32 iOS
405+
// environment, but not on arm64
406+
if (compiler.getTargetOpts().Triple.find("arm64") == std::string::npos &&
407+
compiler.getTargetOpts().Triple.find("arm") != std::string::npos &&
408+
compiler.getTargetOpts().Triple.find("ios") != std::string::npos) {
409+
compiler.getTargetOpts().ABI = "apcs-gnu";
410+
}
411+
// Supported subsets of x86
412+
if (target_machine == llvm::Triple::x86 ||
413+
target_machine == llvm::Triple::x86_64) {
414+
compiler.getTargetOpts().FeaturesAsWritten.push_back("+sse");
415+
compiler.getTargetOpts().FeaturesAsWritten.push_back("+sse2");
416+
}
417+
418+
// Set the target CPU to generate code for. This will be empty for any CPU
419+
// that doesn't really need to make a special
420+
// CPU string.
421+
compiler.getTargetOpts().CPU = target_arch.GetClangTargetCPU();
422+
423+
// Set the target ABI
424+
if (std::string abi = GetClangTargetABI(target_arch); !abi.empty())
425+
compiler.getTargetOpts().ABI = std::move(abi);
426+
}
427+
354428
//===----------------------------------------------------------------------===//
355429
// Implementation of ClangExpressionParser
356430
//===----------------------------------------------------------------------===//
@@ -395,12 +469,6 @@ ClangExpressionParser::ClangExpressionParser(
395469
// Defaults to lldb::eLanguageTypeUnknown.
396470
lldb::LanguageType frame_lang = expr.Language().AsLanguageType();
397471

398-
std::string abi;
399-
ArchSpec target_arch;
400-
target_arch = target_sp->GetArchitecture();
401-
402-
const auto target_machine = target_arch.GetMachine();
403-
404472
// If the expression is being evaluated in the context of an existing stack
405473
// frame, we introspect to see if the language runtime is available.
406474

@@ -419,45 +487,7 @@ ClangExpressionParser::ClangExpressionParser(
419487

420488
// 2. Configure the compiler with a set of default options that are
421489
// appropriate for most situations.
422-
if (target_arch.IsValid()) {
423-
std::string triple = target_arch.GetTriple().str();
424-
m_compiler->getTargetOpts().Triple = triple;
425-
LLDB_LOGF(log, "Using %s as the target triple",
426-
m_compiler->getTargetOpts().Triple.c_str());
427-
} else {
428-
// If we get here we don't have a valid target and just have to guess.
429-
// Sometimes this will be ok to just use the host target triple (when we
430-
// evaluate say "2+3", but other expressions like breakpoint conditions and
431-
// other things that _are_ target specific really shouldn't just be using
432-
// the host triple. In such a case the language runtime should expose an
433-
// overridden options set (3), below.
434-
m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
435-
LLDB_LOGF(log, "Using default target triple of %s",
436-
m_compiler->getTargetOpts().Triple.c_str());
437-
}
438-
// Now add some special fixes for known architectures: Any arm32 iOS
439-
// environment, but not on arm64
440-
if (m_compiler->getTargetOpts().Triple.find("arm64") == std::string::npos &&
441-
m_compiler->getTargetOpts().Triple.find("arm") != std::string::npos &&
442-
m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos) {
443-
m_compiler->getTargetOpts().ABI = "apcs-gnu";
444-
}
445-
// Supported subsets of x86
446-
if (target_machine == llvm::Triple::x86 ||
447-
target_machine == llvm::Triple::x86_64) {
448-
m_compiler->getTargetOpts().FeaturesAsWritten.push_back("+sse");
449-
m_compiler->getTargetOpts().FeaturesAsWritten.push_back("+sse2");
450-
}
451-
452-
// Set the target CPU to generate code for. This will be empty for any CPU
453-
// that doesn't really need to make a special
454-
// CPU string.
455-
m_compiler->getTargetOpts().CPU = target_arch.GetClangTargetCPU();
456-
457-
// Set the target ABI
458-
abi = GetClangTargetABI(target_arch);
459-
if (!abi.empty())
460-
m_compiler->getTargetOpts().ABI = abi;
490+
SetupTargetOpts(*m_compiler, *target_sp);
461491

462492
// 3. Create and install the target on the compiler.
463493
m_compiler->createDiagnostics();
@@ -1179,28 +1209,6 @@ ClangExpressionParser::ParseInternal(DiagnosticManager &diagnostic_manager,
11791209
return num_errors;
11801210
}
11811211

1182-
std::string
1183-
ClangExpressionParser::GetClangTargetABI(const ArchSpec &target_arch) {
1184-
std::string abi;
1185-
1186-
if (target_arch.IsMIPS()) {
1187-
switch (target_arch.GetFlags() & ArchSpec::eMIPSABI_mask) {
1188-
case ArchSpec::eMIPSABI_N64:
1189-
abi = "n64";
1190-
break;
1191-
case ArchSpec::eMIPSABI_N32:
1192-
abi = "n32";
1193-
break;
1194-
case ArchSpec::eMIPSABI_O32:
1195-
abi = "o32";
1196-
break;
1197-
default:
1198-
break;
1199-
}
1200-
}
1201-
return abi;
1202-
}
1203-
12041212
/// Applies the given Fix-It hint to the given commit.
12051213
static void ApplyFixIt(const FixItHint &fixit, clang::edit::Commit &commit) {
12061214
// This is cobbed from clang::Rewrite::FixItRewriter.

lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,6 @@ class ClangExpressionParser : public ExpressionParser {
119119
bool &can_interpret,
120120
lldb_private::ExecutionPolicy execution_policy) override;
121121

122-
/// Returns a string representing current ABI.
123-
///
124-
/// \param[in] target_arch
125-
/// The target architecture.
126-
///
127-
/// \return
128-
/// A string representing target ABI for the current architecture.
129-
std::string GetClangTargetABI(const ArchSpec &target_arch);
130-
131122
private:
132123
/// Parses the expression.
133124
///

0 commit comments

Comments
 (0)