Skip to content

Allow switching the Swift version in expressions #8775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
527 changes: 258 additions & 269 deletions lldb/bindings/python/static-binding/LLDBWrapPython.cpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lldb/bindings/python/static-binding/lldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5571,7 +5571,7 @@ def SetTrapExceptions(self, trap_exceptions=True):
def SetLanguage(self, *args):
r"""
SetLanguage(SBExpressionOptions self, lldb::LanguageType language)
SetLanguage(SBExpressionOptions self, SBSourceLanguageName name, uint32_t version)
SetLanguage(SBExpressionOptions self, lldb::SBSourceLanguageName name, uint32_t version)
Sets the language that LLDB should assume the expression is written in
"""
return _lldb.SBExpressionOptions_SetLanguage(self, *args)
Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/API/SBExpressionOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class LLDB_API SBExpressionOptions {
/// Set the language using a pair of language code and version as
/// defined by the DWARF 6 specification.
/// WARNING: These codes may change until DWARF 6 is finalized.
void SetLanguage(SBSourceLanguageName name, uint32_t version);
void SetLanguage(lldb::SBSourceLanguageName name, uint32_t version);

#ifndef SWIG
void SetCancelCallback(lldb::ExpressionCancelCallback callback, void *baton);
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/API/SBExpressionOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void SBExpressionOptions::SetLanguage(lldb::LanguageType language) {
m_opaque_up->SetLanguage(language);
}

void SBExpressionOptions::SetLanguage(SBSourceLanguageName name,
void SBExpressionOptions::SetLanguage(lldb::SBSourceLanguageName name,
uint32_t version) {
LLDB_INSTRUMENT_VA(this, name, version);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1358,16 +1358,16 @@ SwiftExpressionParser::ParseAndImport(
invocation.getFrontendOptions().ModuleName = expr_name_buf;
invocation.getIRGenOptions().ModuleName = expr_name_buf;

auto &lang_opts = invocation.getLangOptions();
bool enable_bare_slash_regex_literals =
m_sc.target_sp->GetSwiftEnableBareSlashRegex();
if (enable_bare_slash_regex_literals) {
invocation.getLangOptions().enableFeature(
swift::Feature::BareSlashRegexLiterals);
}
if (uint32_t version = m_expr.Language().version) {
invocation.getLangOptions().EffectiveLanguageVersion =
if (enable_bare_slash_regex_literals)
lang_opts.enableFeature(swift::Feature::BareSlashRegexLiterals);
if (uint32_t version = m_expr.Language().version)
lang_opts.EffectiveLanguageVersion =
llvm::VersionTuple(version / 100, version % 100);
}
if (lang_opts.EffectiveLanguageVersion >= swift::version::Version({6}))
lang_opts.StrictConcurrencyLevel = swift::StrictConcurrency::Complete;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the compiler not do this when the version is set to >=6? If lldb is doing this, but the compiler isn't, is that going to cause some issues in some cases?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, I'm confused by the title. The diff appears to affect StrictConcurrencyLevel, and the language version already appears supported.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both excellent questions. I asked, and the compiler sets this flag in an ad-hoc fashion that isn't readily available to reuse. This also seems to be the only flag that behaves this way.

The title is wrong. This just adds a test.


auto should_use_prestable_abi = [&]() {
lldb::StackFrameSP this_frame_sp(m_stack_frame_wp.lock());
Expand All @@ -1380,8 +1380,7 @@ SwiftExpressionParser::ParseAndImport(
return !runtime->IsABIStable();
};

invocation.getLangOptions().UseDarwinPreStableABIBit =
should_use_prestable_abi();
lang_opts.UseDarwinPreStableABIBit = should_use_prestable_abi();

LLDBNameLookup *external_lookup;
if (m_options.GetPlaygroundTransformEnabled() || m_options.GetREPLEnabled()) {
Expand Down
3 changes: 3 additions & 0 deletions lldb/test/API/lang/swift/expression/language_version/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SWIFT_SOURCES := main.swift

include Makefile.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import lldb
from lldbsuite.test.lldbtest import *
from lldbsuite.test.decorators import *
import lldbsuite.test.lldbutil as lldbutil
import unittest2

class TestSwiftExpressionLanguageVersion(TestBase):
NO_DEBUG_INFO_TESTCASE = True

@swiftTest
def test(self):
"""Test changing the Swift language version"""
self.build()
target, process, thread, bkpt = lldbutil.run_to_name_breakpoint(
self, 'main')

expr = """\
#if swift(>=6.0)
6
#else
5
#endif
"""

def test_version(n):
if self.TraceOn():
print("Testing version %d"%n)
options = lldb.SBExpressionOptions()
options.SetLanguage(lldb.eLanguageNameSwift, n*100 + 0)
value = self.frame().EvaluateExpression(expr, options)
self.assertEquals(value.GetValue(), "%d" % n)

test_version(5)
test_version(6)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("break here")