From f64e25bd9763750d32bad2b298fd61f1d06cecc8 Mon Sep 17 00:00:00 2001 From: Robert Widmann Date: Fri, 3 Jan 2020 13:47:53 -0800 Subject: [PATCH 1/2] [NFC] Hide SourceFile::Decls Supports apple/swift#28995 --- .../Swift/SwiftASTManipulator.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lldb/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.cpp b/lldb/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.cpp index c96d58a6b4f4b..e288beaa07709 100644 --- a/lldb/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.cpp +++ b/lldb/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.cpp @@ -540,7 +540,7 @@ bool SwiftASTManipulator::RewriteResult() { // First step, walk the function body converting returns to assignments to // temp variables + return: - for (swift::Decl *decl : m_source_file.Decls) { + for (swift::Decl *decl : m_source_file.getTopLevelDecls()) { if (auto top_level_code_decl = llvm::dyn_cast(decl)) { return_finder.SetDeclContext(top_level_code_decl); @@ -551,8 +551,8 @@ bool SwiftASTManipulator::RewriteResult() { // Second step, fetch the last expression, and if it is non-null, set it to // a temp result as well: - if (!m_source_file.Decls.empty()) { - swift::Decl *last_decl = *(m_source_file.Decls.end() - 1); + if (!m_source_file.getTopLevelDecls().empty()) { + swift::Decl *last_decl = *(m_source_file.getTopLevelDecls().end() - 1); if (auto last_top_level_code_decl = llvm::dyn_cast(last_decl)) { @@ -754,7 +754,7 @@ void SwiftASTManipulator::FindVariableDeclarations( }; if (m_repl) { - for (swift::Decl *decl : m_source_file.Decls) { + for (swift::Decl *decl : m_source_file.getTopLevelDecls()) { if (swift::VarDecl *var_decl = llvm::dyn_cast(decl)) { if (!var_decl->getName().str().startswith("$")) { register_one_var(var_decl); @@ -797,7 +797,7 @@ void SwiftASTManipulator::FindNonVariableDeclarations( if (!m_repl) return; // we don't do this for non-REPL expressions... yet - for (swift::Decl *decl : m_source_file.Decls) { + for (swift::Decl *decl : m_source_file.getTopLevelDecls()) { if (swift::ValueDecl *value_decl = llvm::dyn_cast(decl)) { if (!llvm::isa(value_decl) && value_decl->hasName()) { non_variables.push_back(value_decl); @@ -1125,9 +1125,8 @@ bool SwiftASTManipulator::AddExternalVariables( redirected_var_decl->setImplicit(true); - m_source_file.Decls.insert(m_source_file.Decls.begin(), top_level_code); - m_source_file.Decls.insert(m_source_file.Decls.begin(), - redirected_var_decl); + m_source_file.addTopLevelDecl(top_level_code); + m_source_file.addTopLevelDecl(redirected_var_decl); variable.m_decl = redirected_var_decl; @@ -1402,7 +1401,7 @@ swift::ValueDecl *SwiftASTManipulator::MakeGlobalTypealias( if (make_private) { type_alias_decl->overwriteAccess(swift::AccessLevel::Private); } - m_source_file.Decls.push_back(type_alias_decl); + m_source_file.addTopLevelDecl(type_alias_decl); } return type_alias_decl; From 42b0be7bdc80def7f1ad3a535fa3c0ad9892966c Mon Sep 17 00:00:00 2001 From: Robert Widmann Date: Mon, 6 Jan 2020 14:50:28 -0800 Subject: [PATCH 2/2] Add a workaround for rdar://58355191 --- .../ExpressionParser/Swift/SwiftASTManipulator.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lldb/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.cpp b/lldb/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.cpp index e288beaa07709..f52a663882d1d 100644 --- a/lldb/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.cpp +++ b/lldb/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.cpp @@ -1125,8 +1125,13 @@ bool SwiftASTManipulator::AddExternalVariables( redirected_var_decl->setImplicit(true); - m_source_file.addTopLevelDecl(top_level_code); - m_source_file.addTopLevelDecl(redirected_var_decl); + // FIXME: This should use SourceFile::addTopLevelDecl, but if these decls + // are not inserted at the beginning of the source file then + // SwiftREPL/FoundationTypes.test fails. + // + // See rdar://58355191 + m_source_file.prependTopLevelDecl(top_level_code); + m_source_file.prependTopLevelDecl(redirected_var_decl); variable.m_decl = redirected_var_decl;