Skip to content

Commit 1d9bbf2

Browse files
Merge pull request #3716 from adrian-prantl/cherry-pick56
Cherry picks
2 parents 64a4d67 + 200098b commit 1d9bbf2

File tree

16 files changed

+227
-133
lines changed

16 files changed

+227
-133
lines changed

lldb/include/lldb/Expression/UserExpression.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,8 @@ class UserExpression : public Expression {
266266
0x1001; ///< ValueObject::GetError() returns this if there is no result
267267
/// from the expression.
268268

269-
const char *GetFixedText() {
270-
if (m_fixed_text.empty())
271-
return nullptr;
272-
return m_fixed_text.c_str();
269+
llvm::StringRef GetFixedText() {
270+
return m_fixed_text;
273271
}
274272

275273
protected:

lldb/packages/Python/lldbsuite/test/make/Makefile.rules

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,20 @@ ifneq "$(DYLIB_HIDE_SWIFTMODULE)" ""
708708
-emit-library $(DYLIB_SWIFT_FLAGS) -o $@ \
709709
$(patsubst %.swiftmodule.o,,$(DYLIB_OBJECTS))
710710
else
711+
ifneq "$(FRAMEWORK)" ""
712+
mkdir -p $(FRAMEWORK).framework/Versions/A/Headers
713+
mkdir -p $(FRAMEWORK).framework/Versions/A/Modules
714+
mkdir -p $(FRAMEWORK).framework/Versions/A/Resources
715+
ifneq "$(MODULENAME)" ""
716+
mkdir -p $(FRAMEWORK).framework/Versions/A/Modules/$(MODULENAME).swiftmodule
717+
cp -r $(MODULENAME).swiftmodule $(FRAMEWORK).framework/Versions/A/Modules/$(MODULENAME).swiftmodule/$(ARCH).swiftmodule
718+
endif
719+
(cd $(FRAMEWORK).framework/Versions; ln -sf A Current)
720+
(cd $(FRAMEWORK).framework/; ln -sf Versions/A/Headers Headers)
721+
(cd $(FRAMEWORK).framework/; ln -sf Versions/A/Modules Modules)
722+
(cd $(FRAMEWORK).framework/; ln -sf Versions/A/Resources Resources)
723+
(cd $(FRAMEWORK).framework/; ln -sf Versions/A/$(FRAMEWORK) $(FRAMEWORK))
724+
endif
711725
$(SWIFTC) $(patsubst -g,,$(SWIFTFLAGS)) \
712726
-emit-library $(DYLIB_SWIFT_FLAGS) -o $@ $(DYLIB_OBJECTS)
713727
endif

lldb/source/Expression/UserExpression.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,13 @@ UserExpression::Evaluate(ExecutionContext &exe_ctx,
262262
if (fixed_expression == nullptr)
263263
fixed_expression = &tmp_fixed_expression;
264264

265-
const char *fixed_text = user_expression_sp->GetFixedText();
266-
if (fixed_text != nullptr)
267-
fixed_expression->append(fixed_text);
265+
*fixed_expression = user_expression_sp->GetFixedText().str();
266+
267+
// Swift Playgrounds disable fixits, but SwiftASTContext may get
268+
// poisoned (see SwiftASTContextForExpressions::ModulesDidLoad())
269+
// during expression evaluation. When this happens we want to re-run
270+
// the same expression with a freshly initialized SwiftASTContext.
271+
bool is_replay = !options.GetAutoApplyFixIts() && expr == *fixed_expression;
268272

269273
// If there is a fixed expression, try to parse it:
270274
if (!parse_success) {
@@ -273,9 +277,9 @@ UserExpression::Evaluate(ExecutionContext &exe_ctx,
273277
user_expression_sp.reset();
274278

275279
execution_results = lldb::eExpressionParseError;
276-
if (fixed_expression && !fixed_expression->empty() &&
277-
options.GetAutoApplyFixIts()) {
278-
const uint64_t max_fix_retries = options.GetRetriesWithFixIts();
280+
if (!fixed_expression->empty() &&
281+
(options.GetAutoApplyFixIts() || is_replay)) {
282+
const uint64_t max_fix_retries = is_replay ? 1 : options.GetRetriesWithFixIts();
279283
for (uint64_t i = 0; i < max_fix_retries; ++i) {
280284
// Try parsing the fixed expression.
281285
lldb::UserExpressionSP fixed_expression_sp(
@@ -293,8 +297,8 @@ UserExpression::Evaluate(ExecutionContext &exe_ctx,
293297
} else {
294298
// The fixed expression also didn't parse. Let's check for any new
295299
// Fix-Its we could try.
296-
if (fixed_expression_sp->GetFixedText()) {
297-
*fixed_expression = fixed_expression_sp->GetFixedText();
300+
if (!fixed_expression_sp->GetFixedText().empty()) {
301+
*fixed_expression = fixed_expression_sp->GetFixedText().str();
298302
} else {
299303
// Fixed expression didn't compile without a fixit, don't retry and
300304
// don't tell the user about it.

lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,10 +1221,18 @@ struct SwiftASTContextError : public llvm::ErrorInfo<SwiftASTContextError> {
12211221
/// This indicates an error in the SwiftASTContext.
12221222
struct ModuleImportError : public llvm::ErrorInfo<ModuleImportError> {
12231223
static char ID;
1224-
std::string Message;
1225-
1226-
ModuleImportError(llvm::Twine Message) : Message(Message.str()) {}
1227-
void log(llvm::raw_ostream &OS) const override { OS << "ModuleImport"; }
1224+
std::string msg;
1225+
bool is_explicit;
1226+
1227+
ModuleImportError(llvm::Twine message, bool is_explicit = false)
1228+
: msg(message.str()), is_explicit(is_explicit) {}
1229+
void log(llvm::raw_ostream &OS) const override {
1230+
if (is_explicit)
1231+
OS << "error while processing import statement:";
1232+
else
1233+
OS << "error while importing implicit dependency:";
1234+
OS << msg;
1235+
}
12281236
std::error_code convertToErrorCode() const override {
12291237
return inconvertibleErrorCode();
12301238
}
@@ -1321,8 +1329,7 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
13211329
const char *msg = implicit_import_error.AsCString();
13221330
if (!msg)
13231331
msg = "error status positive, but import still failed";
1324-
return make_error<ModuleImportError>(llvm::Twine("in implicit-import:\n") +
1325-
msg);
1332+
return make_error<ModuleImportError>(msg);
13261333
}
13271334

13281335
swift::ImplicitImportInfo importInfo;
@@ -1471,8 +1478,7 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
14711478
const char *msg = auto_import_error.AsCString();
14721479
if (!msg)
14731480
msg = "error status positive, but import still failed";
1474-
return make_error<ModuleImportError>(
1475-
llvm::Twine("in user-import:\n") + msg);
1481+
return make_error<ModuleImportError>(msg, /*explicit=*/true);
14761482
}
14771483
}
14781484

@@ -1524,13 +1530,19 @@ unsigned SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
15241530
m_sc, *m_exe_scope, m_options, repl, playground);
15251531

15261532
if (!parsed_expr) {
1533+
bool user_import = false;
15271534
bool retry = false;
15281535
handleAllErrors(parsed_expr.takeError(),
15291536
[&](const ModuleImportError &MIE) {
1537+
if (MIE.is_explicit) {
1538+
// A new dylib may have poisoned the context.
1539+
retry = true;
1540+
user_import = true;
1541+
}
15301542
if (swift_ast_ctx->GetClangImporter())
15311543
// Already on backup power.
15321544
diagnostic_manager.PutString(eDiagnosticSeverityError,
1533-
MIE.Message);
1545+
MIE.message());
15341546
else
15351547
// Discard the shared scratch context and retry.
15361548
retry = true;
@@ -1552,7 +1564,8 @@ unsigned SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
15521564
// fresh SwiftASTContext initialized with the flags from the
15531565
// current lldb::Module / Swift dylib to avoid header search
15541566
// mismatches.
1555-
m_sc.target_sp->SetUseScratchTypesystemPerModule(true);
1567+
if (!user_import)
1568+
m_sc.target_sp->SetUseScratchTypesystemPerModule(true);
15561569
return 2;
15571570
}
15581571

0 commit comments

Comments
 (0)