Skip to content

Commit 9d92e4f

Browse files
Merge pull request #3634 from adrian-prantl/triple
Initialize the Swift CompilerInstance with the triple from the load commands.
2 parents 9b6a93a + ca320d6 commit 9d92e4f

File tree

4 files changed

+40
-43
lines changed

4 files changed

+40
-43
lines changed

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -914,8 +914,7 @@ SwiftASTContext::SwiftASTContext() {
914914
}
915915
#endif
916916

917-
SwiftASTContext::SwiftASTContext(std::string description, llvm::Triple triple,
918-
Target *target)
917+
SwiftASTContext::SwiftASTContext(std::string description, Target *target)
919918
: TypeSystemSwift(),
920919
m_compiler_invocation_ap(new swift::CompilerInvocation()) {
921920
m_description = description;
@@ -935,7 +934,6 @@ SwiftASTContext::SwiftASTContext(std::string description, llvm::Triple triple,
935934
if (target)
936935
m_target_wp = target->shared_from_this();
937936

938-
SetTriple(triple);
939937
swift::IRGenOptions &ir_gen_opts =
940938
m_compiler_invocation_ap->getIRGenOptions();
941939
ir_gen_opts.OutputKind = swift::IRGenOutputKind::Module;
@@ -1715,9 +1713,7 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
17151713
fallback ? static_cast<SwiftASTContext *>(
17161714
new SwiftASTContextForExpressions(m_description, *target))
17171715
: static_cast<SwiftASTContext *>(new SwiftASTContextForModule(
1718-
*typeref_typesystem, m_description,
1719-
target ? target->GetArchitecture().GetTriple() : triple,
1720-
target)));
1716+
*typeref_typesystem, m_description, target)));
17211717
bool suppress_config_log = false;
17221718
auto defer_log = llvm::make_scope_exit([swift_ast_sp, &suppress_config_log] {
17231719
// To avoid spamming the log with useless info, we don't log the
@@ -1734,12 +1730,8 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
17341730
swift_ast_sp->GetLanguageOptions().EnableAccessControl = false;
17351731
swift_ast_sp->GetLanguageOptions().EnableTargetOSChecking = false;
17361732

1737-
swift_ast_sp->SetTriple(triple, &module);
1738-
1739-
bool set_triple = false;
17401733
bool found_swift_modules = false;
17411734
SymbolFile *sym_file = module.GetSymbolFile();
1742-
std::string target_triple;
17431735

17441736
if (sym_file) {
17451737
bool got_serialized_options = false;
@@ -1752,17 +1744,11 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
17521744
swift_ast_sp->m_module_import_warnings.push_back(std::string(error));
17531745
}
17541746

1755-
// Some of the bits in the compiler options we keep separately, so
1756-
// we need to populate them from the serialized options:
17571747
llvm::StringRef serialized_triple =
17581748
swift_ast_sp->GetCompilerInvocation().getTargetTriple();
1759-
if (serialized_triple.empty()) {
1760-
LOG_PRINTF(LIBLLDB_LOG_TYPES, "Serialized triple was empty.");
1761-
} else {
1762-
LOG_PRINTF(LIBLLDB_LOG_TYPES, "Found serialized triple %s.",
1749+
if (!serialized_triple.empty()) {
1750+
LOG_PRINTF(LIBLLDB_LOG_TYPES, "Serialized/default triple would have been %s.",
17631751
serialized_triple.str().c_str());
1764-
swift_ast_sp->SetTriple(llvm::Triple(serialized_triple), &module);
1765-
set_triple = true;
17661752
}
17671753

17681754
// SDK path setup.
@@ -1802,24 +1788,12 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
18021788
}
18031789
}
18041790

1805-
if (!set_triple) {
1806-
llvm::Triple llvm_triple = swift_ast_sp->GetTriple();
1807-
1808-
// LLVM wants this to be set to iOS or MacOSX; if we're working on
1809-
// a bare-boards type image, change the triple for LLVM's benefit.
1810-
if (llvm_triple.getVendor() == llvm::Triple::Apple &&
1811-
llvm_triple.getOS() == llvm::Triple::UnknownOS) {
1812-
if (llvm_triple.getArch() == llvm::Triple::arm ||
1813-
llvm_triple.getArch() == llvm::Triple::thumb) {
1814-
llvm_triple.setOS(llvm::Triple::IOS);
1815-
} else {
1816-
llvm_triple.setOS(llvm::Triple::MacOSX);
1817-
}
1818-
swift_ast_sp->SetTriple(llvm_triple, &module);
1819-
}
1820-
}
1791+
// The serialized triple is the triple of the last binary
1792+
// __swiftast section that was processed. Instead of relying on
1793+
// the section contents order, we overwrite the triple in the
1794+
// CompilerInvocation with the triple recovered from the binary.
1795+
swift_ast_sp->SetTriple(triple, &module);
18211796

1822-
triple = swift_ast_sp->GetTriple();
18231797
std::string resource_dir = swift_ast_sp->GetResourceDir(triple);
18241798
ConfigureResourceDirs(swift_ast_sp->GetCompilerInvocation(),
18251799
FileSpec(resource_dir), triple);
@@ -8209,8 +8183,7 @@ SwiftASTContext::GetASTVectorForModule(const Module *module) {
82098183

82108184
SwiftASTContextForExpressions::SwiftASTContextForExpressions(
82118185
std::string description, Target &target)
8212-
: SwiftASTContext(std::move(description),
8213-
target.GetArchitecture().GetTriple(), &target),
8186+
: SwiftASTContext(std::move(description), &target),
82148187
m_typeref_typesystem(*this),
82158188
m_persistent_state_up(new SwiftPersistentExpressionState) {}
82168189

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ class SwiftASTContext : public TypeSystemSwift {
164164

165165
protected:
166166
// Constructors and destructors
167-
SwiftASTContext(std::string description, llvm::Triple triple,
168-
Target *target = nullptr);
167+
SwiftASTContext(std::string description, Target *target = nullptr);
168+
169169
public:
170170

171171
SwiftASTContext(const SwiftASTContext &rhs) = delete;
@@ -944,9 +944,8 @@ class SwiftASTContextForModule : public SwiftASTContext {
944944
/// \}
945945

946946
SwiftASTContextForModule(TypeSystemSwiftTypeRef &typeref_typesystem,
947-
std::string description, llvm::Triple triple,
948-
Target *target)
949-
: SwiftASTContext(description, triple, target),
947+
std::string description, Target *target)
948+
: SwiftASTContext(description, target),
950949
m_typeref_typesystem(typeref_typesystem) {}
951950
virtual ~SwiftASTContextForModule() {}
952951

lldb/test/API/lang/swift/deployment_target/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ include Makefile.rules
1010

1111
a.out: main.swift libNewerTarget.dylib
1212

13+
1314
dlopen_module: dlopen_module.m libNewerTarget.dylib
1415
$(MAKE) -f $(MAKEFILE_RULES) \
1516
CXX= \

lldb/test/API/lang/swift/deployment_target/TestSwiftDeploymentTarget.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from lldbsuite.test.decorators import *
1818
import lldbsuite.test.lldbutil as lldbutil
1919
import unittest2
20+
import os
2021

2122

2223
class TestSwiftDeploymentTarget(TestBase):
@@ -32,7 +33,6 @@ class TestSwiftDeploymentTarget(TestBase):
3233
@swiftTest
3334
def test_swift_deployment_target(self):
3435
self.build()
35-
3636
lldbutil.run_to_source_breakpoint(self,
3737
"break here",
3838
lldb.SBFileSpec('main.swift'))
@@ -53,3 +53,27 @@ def test_swift_deployment_target_dlopen(self):
5353
lldbutil.continue_to_breakpoint(process, bkpt)
5454
self.expect("p self", substrs=['i = 23'])
5555

56+
@skipUnlessDarwin
57+
@skipIfDarwinEmbedded # This test uses macOS triples explicitly.
58+
@skipIf(macos_version=["<", "10.11"])
59+
@swiftTest
60+
def test_swift_deployment_target_from_macho(self):
61+
self.build(dictionary={"MAKE_DSYM": "NO"})
62+
os.unlink(self.getBuildArtifact("a.swiftmodule"))
63+
log = self.getBuildArtifact("types.log")
64+
self.runCmd('log enable lldb types -f "%s"' % log)
65+
lldbutil.run_to_source_breakpoint(self,
66+
"break here",
67+
lldb.SBFileSpec('main.swift'))
68+
self.expect("p f", substrs=['i = 23'])
69+
70+
found_no_ast = False
71+
found_triple = False
72+
logfile = open(log, "r")
73+
for line in logfile:
74+
if 'SwiftASTContextForModule("a.out")::DeserializeAllCompilerFlags() -- Found 0 AST file data entries.' in line:
75+
found_no_ast = True
76+
if 'SwiftASTContextForModule("a.out")::SetTriple("x86_64-apple-macosx10.10.0")' in line:
77+
found_triple = True
78+
self.assertTrue(found_no_ast)
79+
self.assertTrue(found_triple)

0 commit comments

Comments
 (0)