Skip to content

Commit

Permalink
Refactor ClangUserExpression::Parse [NFC]
Browse files Browse the repository at this point in the history
Summary:
This patch splits out functionality from the `Parse` method into different methods.
This benefits the code completion work (which should reuse those methods) and makes the
code a bit more readable.

Note that this patch is as minimal as possible. Some of the code in the new methods definitely
needs more refactoring.

Subscribers: lldb-commits

Differential Revision: https://reviews.llvm.org/D48339

llvm-svn: 336734
  • Loading branch information
Teemperor committed Jul 10, 2018
1 parent d5e57ed commit ba800e7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 30 deletions.
86 changes: 56 additions & 30 deletions lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,17 +322,8 @@ class OnExit {
};
} // namespace

bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager,
ExecutionContext &exe_ctx,
lldb_private::ExecutionPolicy execution_policy,
bool keep_result_in_memory,
bool generate_debug_info) {
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));

Status err;

InstallContext(exe_ctx);

bool ClangUserExpression::SetupPersistentState(DiagnosticManager &diagnostic_manager,
ExecutionContext &exe_ctx) {
if (Target *target = exe_ctx.GetTargetPtr()) {
if (PersistentExpressionState *persistent_state =
target->GetPersistentExpressionStateForLanguage(
Expand All @@ -349,26 +340,15 @@ bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager,
"error: couldn't start parsing (no target)");
return false;
}
return true;
}

ScanContext(exe_ctx, err);

if (!err.Success()) {
diagnostic_manager.PutString(eDiagnosticSeverityWarning, err.AsCString());
}

////////////////////////////////////
// Generate the expression
//

ApplyObjcCastHack(m_expr_text);

std::string prefix = m_expr_prefix;

static void SetupDeclVendor(ExecutionContext &exe_ctx, Target *target) {
if (ClangModulesDeclVendor *decl_vendor =
m_target->GetClangModulesDeclVendor()) {
target->GetClangModulesDeclVendor()) {
const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
llvm::cast<ClangPersistentVariables>(
m_target->GetPersistentExpressionStateForLanguage(
target->GetPersistentExpressionStateForLanguage(
lldb::eLanguageTypeC))
->GetHandLoadedClangModules();
ClangModulesDeclVendor::ModuleVector modules_for_macros;
Expand All @@ -377,7 +357,7 @@ bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager,
modules_for_macros.push_back(module);
}

if (m_target->GetEnableAutoImportClangModules()) {
if (target->GetEnableAutoImportClangModules()) {
if (StackFrame *frame = exe_ctx.GetFramePtr()) {
if (Block *block = frame->GetFrameBlock()) {
SymbolContext sc;
Expand All @@ -394,8 +374,13 @@ bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager,
}
}
}
}

llvm::Optional<lldb::LanguageType> ClangUserExpression::GetLanguageForExpr(
DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx) {
lldb::LanguageType lang_type = lldb::LanguageType::eLanguageTypeUnknown;

lldb::LanguageType lang_type = lldb::eLanguageTypeUnknown;
std::string prefix = m_expr_prefix;

if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
m_transformed_text = m_expr_text;
Expand All @@ -415,9 +400,50 @@ bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager,
exe_ctx)) {
diagnostic_manager.PutString(eDiagnosticSeverityError,
"couldn't construct expression body");
return false;
return llvm::Optional<lldb::LanguageType>();
}
}
return lang_type;
}

bool ClangUserExpression::PrepareForParsing(
DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx) {
InstallContext(exe_ctx);

if (!SetupPersistentState(diagnostic_manager, exe_ctx))
return false;

Status err;
ScanContext(exe_ctx, err);

if (!err.Success()) {
diagnostic_manager.PutString(eDiagnosticSeverityWarning, err.AsCString());
}

////////////////////////////////////
// Generate the expression
//

ApplyObjcCastHack(m_expr_text);

SetupDeclVendor(exe_ctx, m_target);
return true;
}

bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager,
ExecutionContext &exe_ctx,
lldb_private::ExecutionPolicy execution_policy,
bool keep_result_in_memory,
bool generate_debug_info) {
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));

if (!PrepareForParsing(diagnostic_manager, exe_ctx))
return false;

lldb::LanguageType lang_type = lldb::LanguageType::eLanguageTypeUnknown;
if (auto new_lang = GetLanguageForExpr(diagnostic_manager, exe_ctx)) {
lang_type = new_lang.getValue();
}

if (log)
log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ class ClangUserExpression : public LLVMUserExpression {
lldb::addr_t struct_address,
DiagnosticManager &diagnostic_manager) override;

llvm::Optional<lldb::LanguageType> GetLanguageForExpr(
DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx);
bool SetupPersistentState(DiagnosticManager &diagnostic_manager,
ExecutionContext &exe_ctx);
bool PrepareForParsing(DiagnosticManager &diagnostic_manager,
ExecutionContext &exe_ctx);

ClangUserExpressionHelper m_type_system_helper;

class ResultDelegate : public Materializer::PersistentVariableDelegate {
Expand Down

0 comments on commit ba800e7

Please sign in to comment.