Skip to content

Commit

Permalink
[Target] Remove Target::GetScratchClangASTContext
Browse files Browse the repository at this point in the history
Target doesn't really need to know about ClangASTContext more than any
other TypeSystem. We can create a method ClangASTContext::GetScratch for
anything who needs a ClangASTContext specifically instead of just a
generic TypeSystem.
  • Loading branch information
bulbazord committed Dec 12, 2019
1 parent 7a54f72 commit 3031818
Show file tree
Hide file tree
Showing 26 changed files with 138 additions and 89 deletions.
15 changes: 15 additions & 0 deletions lldb/include/lldb/Symbol/ClangASTContext.h
Expand Up @@ -30,7 +30,10 @@
#include "lldb/Expression/ExpressionVariable.h"
#include "lldb/Symbol/CompilerType.h"
#include "lldb/Symbol/TypeSystem.h"
#include "lldb/Target/Target.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/Logging.h"
#include "lldb/lldb-enumerations.h"

class DWARFASTParserClang;
Expand Down Expand Up @@ -86,6 +89,18 @@ class ClangASTContext : public TypeSystem {

static ClangASTContext *GetASTContext(clang::ASTContext *ast_ctx);

static ClangASTContext *GetScratch(Target &target,
bool create_on_demand = true) {
auto type_system_or_err = target.GetScratchTypeSystemForLanguage(
lldb::eLanguageTypeC, create_on_demand);
if (auto err = type_system_or_err.takeError()) {
LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET),
std::move(err), "Couldn't get scratch ClangASTContext");
return nullptr;
}
return llvm::dyn_cast<ClangASTContext>(&type_system_or_err.get());
}

clang::ASTContext *getASTContext();

clang::Builtin::Context *getBuiltinContext();
Expand Down
2 changes: 0 additions & 2 deletions lldb/include/lldb/Target/Target.h
Expand Up @@ -1067,8 +1067,6 @@ class Target : public std::enable_shared_from_this<Target>,
const char *name,
Status &error);

ClangASTContext *GetScratchClangASTContext(bool create_on_demand = true);

lldb::ClangASTImporterSP GetClangASTImporter();

// Install any files through the platform that need be to installed prior to
Expand Down
Expand Up @@ -1073,7 +1073,7 @@ DynamicLoaderDarwin::GetThreadLocalData(const lldb::ModuleSP module_sp,
StackFrameSP frame_sp = thread_sp->GetStackFrameAtIndex(0);
if (frame_sp) {
ClangASTContext *clang_ast_context =
target.GetScratchClangASTContext();
ClangASTContext::GetScratch(target);

if (!clang_ast_context)
return LLDB_INVALID_ADDRESS;
Expand Down
Expand Up @@ -223,7 +223,10 @@ bool DynamicLoaderMacOS::NotifyBreakpointHit(void *baton,
// get the values from the ABI:

ClangASTContext *clang_ast_context =
process->GetTarget().GetScratchClangASTContext();
ClangASTContext::GetScratch(process->GetTarget());
if (!clang_ast_context)
return false;

ValueList argument_values;

Value mode_value; // enum dyld_notify_mode { dyld_notify_adding=0,
Expand Down
Expand Up @@ -342,7 +342,10 @@ bool DynamicLoaderMacOSXDYLD::NotifyBreakpointHit(
// get the values from the ABI:

ClangASTContext *clang_ast_context =
process->GetTarget().GetScratchClangASTContext();
ClangASTContext::GetScratch(process->GetTarget());
if (!clang_ast_context)
return false;

ValueList argument_values;
Value input_value;

Expand Down
Expand Up @@ -453,7 +453,7 @@ void ASTResultSynthesizer::CommitPersistentDecls() {
ConstString name_cs(name.str().c_str());

Decl *D_scratch = m_target.GetClangASTImporter()->DeportDecl(
m_target.GetScratchClangASTContext()->getASTContext(), m_ast_context,
ClangASTContext::GetScratch(m_target)->getASTContext(), m_ast_context,
decl);

if (!D_scratch) {
Expand Down
17 changes: 10 additions & 7 deletions lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp
Expand Up @@ -120,14 +120,17 @@ void ClangASTSource::InstallASTContext(ClangASTContext &clang_ast_context,
// Update the scratch AST context's merger to reflect any new sources we
// might have come across since the last time an expression was parsed.

auto scratch_ast_context = static_cast<ClangASTContextForExpressions*>(
m_target->GetScratchClangASTContext());
if (auto *clang_ast_context = ClangASTContext::GetScratch(*m_target)) {

scratch_ast_context->GetMergerUnchecked().AddSources(sources);
auto scratch_ast_context =
static_cast<ClangASTContextForExpressions *>(clang_ast_context);

sources.push_back({*scratch_ast_context->getASTContext(),
*scratch_ast_context->getFileManager(),
scratch_ast_context->GetOriginMap()});
scratch_ast_context->GetMergerUnchecked().AddSources(sources);

sources.push_back({*scratch_ast_context->getASTContext(),
*scratch_ast_context->getFileManager(),
scratch_ast_context->GetOriginMap()});
}
}

m_merger_up =
Expand All @@ -145,7 +148,7 @@ ClangASTSource::~ClangASTSource() {
// demand by passing false to
// Target::GetScratchClangASTContext(create_on_demand).
ClangASTContext *scratch_clang_ast_context =
m_target->GetScratchClangASTContext(false);
ClangASTContext::GetScratch(*m_target, false);

if (!scratch_clang_ast_context)
return;
Expand Down
Expand Up @@ -108,7 +108,7 @@ bool ClangExpressionDeclMap::WillParse(ExecutionContext &exe_ctx,
m_parser_vars->m_persistent_vars = llvm::cast<ClangPersistentVariables>(
target->GetPersistentExpressionStateForLanguage(eLanguageTypeC));

if (!target->GetScratchClangASTContext())
if (!ClangASTContext::GetScratch(*target))
return false;
}

Expand Down Expand Up @@ -201,7 +201,7 @@ static clang::QualType ExportAllDeclaredTypes(
TypeFromUser ClangExpressionDeclMap::DeportType(ClangASTContext &target,
ClangASTContext &source,
TypeFromParser parser_type) {
assert(&target == m_target->GetScratchClangASTContext());
assert(&target == ClangASTContext::GetScratch(*m_target));
assert((TypeSystem *)&source == parser_type.GetTypeSystem());
assert(source.getASTContext() == m_ast_context);

Expand All @@ -215,7 +215,7 @@ TypeFromUser ClangExpressionDeclMap::DeportType(ClangASTContext &target,
source.getASTContext()->getSourceManager().getFileID(
source.getASTContext()->getTranslationUnitDecl()->getLocation());
auto scratch_ast_context = static_cast<ClangASTContextForExpressions *>(
m_target->GetScratchClangASTContext());
ClangASTContext::GetScratch(*m_target));
clang::QualType exported_type = ExportAllDeclaredTypes(
*m_merger_up.get(), scratch_ast_context->GetMergerUnchecked(),
*source.getASTContext(), *source.getFileManager(),
Expand Down Expand Up @@ -248,8 +248,11 @@ bool ClangExpressionDeclMap::AddPersistentVariable(const NamedDecl *decl,
if (target == nullptr)
return false;

TypeFromUser user_type =
DeportType(*target->GetScratchClangASTContext(), *ast, parser_type);
auto *clang_ast_context = ClangASTContext::GetScratch(*target);
if (!clang_ast_context)
return false;

TypeFromUser user_type = DeportType(*clang_ast_context, *ast, parser_type);

uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(
user_type, is_lvalue, m_keep_result_in_memory, m_result_delegate, err);
Expand Down Expand Up @@ -284,7 +287,9 @@ bool ClangExpressionDeclMap::AddPersistentVariable(const NamedDecl *decl,
if (target == nullptr)
return false;

ClangASTContext *context(target->GetScratchClangASTContext());
ClangASTContext *context = ClangASTContext::GetScratch(*target);
if (!context)
return false;

TypeFromUser user_type = DeportType(*context, *ast, parser_type);

Expand Down Expand Up @@ -777,7 +782,7 @@ void ClangExpressionDeclMap::SearchPersistenDecls(NameSearchContext &context,
return;

ClangASTContext *scratch_clang_ast_context =
target->GetScratchClangASTContext();
ClangASTContext::GetScratch(*target);

if (!scratch_clang_ast_context)
return;
Expand Down Expand Up @@ -1714,7 +1719,9 @@ void ClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context,
if (target == nullptr)
return;

ClangASTContext *scratch_ast_context = target->GetScratchClangASTContext();
ClangASTContext *scratch_ast_context = ClangASTContext::GetScratch(*target);
if (!scratch_ast_context)
return;

TypeFromUser user_type(scratch_ast_context->GetBasicType(eBasicTypeVoid)
.GetPointerType()
Expand Down
11 changes: 7 additions & 4 deletions lldb/source/Plugins/Language/CPlusPlus/LibCxx.cpp
Expand Up @@ -572,10 +572,13 @@ bool lldb_private::formatters::LibcxxWStringSummaryProvider(
location_sp->GetPointeeData(extractor, 0, size);

// std::wstring::size() is measured in 'characters', not bytes
auto wchar_t_size = valobj.GetTargetSP()
->GetScratchClangASTContext()
->GetBasicType(lldb::eBasicTypeWChar)
.GetByteSize(nullptr);
ClangASTContext *ast_context =
ClangASTContext::GetScratch(*valobj.GetTargetSP());
if (!ast_context)
return false;

auto wchar_t_size =
ast_context->GetBasicType(lldb::eBasicTypeWChar).GetByteSize(nullptr);
if (!wchar_t_size)
return false;

Expand Down
24 changes: 13 additions & 11 deletions lldb/source/Plugins/Language/ObjC/NSArray.cpp
Expand Up @@ -461,9 +461,8 @@ lldb_private::formatters::NSArrayMSyntheticFrontEndBase::NSArrayMSyntheticFrontE
: SyntheticChildrenFrontEnd(*valobj_sp), m_exe_ctx_ref(), m_ptr_size(8),
m_id_type() {
if (valobj_sp) {
auto *clang_ast_context = valobj_sp->GetExecutionContextRef()
.GetTargetSP()
->GetScratchClangASTContext();
auto *clang_ast_context = ClangASTContext::GetScratch(
*valobj_sp->GetExecutionContextRef().GetTargetSP());
if (clang_ast_context)
m_id_type = CompilerType(
clang_ast_context,
Expand Down Expand Up @@ -610,9 +609,8 @@ lldb_private::formatters::GenericNSArrayISyntheticFrontEnd<D32, D64, Inline>::
if (valobj_sp) {
CompilerType type = valobj_sp->GetCompilerType();
if (type) {
auto *clang_ast_context = valobj_sp->GetExecutionContextRef()
.GetTargetSP()
->GetScratchClangASTContext();
auto *clang_ast_context = ClangASTContext::GetScratch(
*valobj_sp->GetExecutionContextRef().GetTargetSP());
if (clang_ast_context)
m_id_type = CompilerType(clang_ast_context,
clang_ast_context->getASTContext()
Expand Down Expand Up @@ -780,11 +778,15 @@ lldb_private::formatters::NSArray1SyntheticFrontEnd::GetChildAtIndex(
static const ConstString g_zero("[0]");

if (idx == 0) {
CompilerType id_type(
m_backend.GetTargetSP()->GetScratchClangASTContext()->GetBasicType(
lldb::eBasicTypeObjCID));
return m_backend.GetSyntheticChildAtOffset(
m_backend.GetProcessSP()->GetAddressByteSize(), id_type, true, g_zero);
auto *clang_ast_context =
ClangASTContext::GetScratch(*m_backend.GetTargetSP());
if (clang_ast_context) {
CompilerType id_type(
clang_ast_context->GetBasicType(lldb::eBasicTypeObjCID));
return m_backend.GetSyntheticChildAtOffset(
m_backend.GetProcessSP()->GetAddressByteSize(), id_type, true,
g_zero);
}
}
return lldb::ValueObjectSP();
}
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Plugins/Language/ObjC/NSDictionary.cpp
Expand Up @@ -65,7 +65,7 @@ NSDictionary_Additionals::GetAdditionalSynthetics() {
static CompilerType GetLLDBNSPairType(TargetSP target_sp) {
CompilerType compiler_type;

ClangASTContext *target_ast_context = target_sp->GetScratchClangASTContext();
ClangASTContext *target_ast_context = ClangASTContext::GetScratch(*target_sp);

if (target_ast_context) {
ConstString g___lldb_autogen_nspair("__lldb_autogen_nspair");
Expand Down
12 changes: 6 additions & 6 deletions lldb/source/Plugins/Language/ObjC/NSError.cpp
Expand Up @@ -86,10 +86,10 @@ bool lldb_private::formatters::NSError_SummaryProvider(

ValueObjectSP domain_str_sp = ValueObject::CreateValueObjectFromData(
"domain_str", isw.GetAsData(process_sp->GetByteOrder()),
valobj.GetExecutionContextRef(), process_sp->GetTarget()
.GetScratchClangASTContext()
->GetBasicType(lldb::eBasicTypeVoid)
.GetPointerType());
valobj.GetExecutionContextRef(),
ClangASTContext::GetScratch(process_sp->GetTarget())
->GetBasicType(lldb::eBasicTypeVoid)
.GetPointerType());

if (!domain_str_sp)
return false;
Expand Down Expand Up @@ -156,8 +156,8 @@ class NSErrorSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
m_child_sp = CreateValueObjectFromData(
"_userInfo", isw.GetAsData(process_sp->GetByteOrder()),
m_backend.GetExecutionContextRef(),
process_sp->GetTarget().GetScratchClangASTContext()->GetBasicType(
lldb::eBasicTypeObjCID));
ClangASTContext::GetScratch(process_sp->GetTarget())
->GetBasicType(lldb::eBasicTypeObjCID));
return false;
}

Expand Down
10 changes: 6 additions & 4 deletions lldb/source/Plugins/Language/ObjC/NSException.cpp
Expand Up @@ -69,10 +69,12 @@ static bool ExtractFields(ValueObject &valobj, ValueObjectSP *name_sp,
InferiorSizedWord userinfo_isw(userinfo, *process_sp);
InferiorSizedWord reserved_isw(reserved, *process_sp);

CompilerType voidstar = process_sp->GetTarget()
.GetScratchClangASTContext()
->GetBasicType(lldb::eBasicTypeVoid)
.GetPointerType();
auto *clang_ast_context = ClangASTContext::GetScratch(process_sp->GetTarget());
if (!clang_ast_context)
return false;

CompilerType voidstar =
clang_ast_context->GetBasicType(lldb::eBasicTypeVoid).GetPointerType();

if (name_sp)
*name_sp = ValueObject::CreateValueObjectFromData(
Expand Down
5 changes: 2 additions & 3 deletions lldb/source/Plugins/Language/ObjC/NSIndexPath.cpp
Expand Up @@ -53,9 +53,8 @@ class NSIndexPathSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
if (!type_system)
return false;

ClangASTContext *ast = m_backend.GetExecutionContextRef()
.GetTargetSP()
->GetScratchClangASTContext();
ClangASTContext *ast = ClangASTContext::GetScratch(
*m_backend.GetExecutionContextRef().GetTargetSP());
if (!ast)
return false;

Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Plugins/Language/ObjC/NSString.cpp
Expand Up @@ -35,7 +35,7 @@ NSString_Additionals::GetAdditionalSummaries() {
static CompilerType GetNSPathStore2Type(Target &target) {
static ConstString g_type_name("__lldb_autogen_nspathstore2");

ClangASTContext *ast_ctx = target.GetScratchClangASTContext();
ClangASTContext *ast_ctx = ClangASTContext::GetScratch(target);

if (!ast_ctx)
return CompilerType();
Expand Down
Expand Up @@ -537,7 +537,10 @@ ValueObjectSP ItaniumABILanguageRuntime::GetExceptionObjectForThread(
return {};

ClangASTContext *clang_ast_context =
m_process->GetTarget().GetScratchClangASTContext();
ClangASTContext::GetScratch(m_process->GetTarget());
if (!clang_ast_context)
return {};

CompilerType voidstar =
clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();

Expand Down
Expand Up @@ -111,7 +111,10 @@ bool AppleObjCRuntime::GetObjectDescription(Stream &strm, Value &value,
}
} else {
// If it is not a pointer, see if we can make it into a pointer.
ClangASTContext *ast_context = target->GetScratchClangASTContext();
ClangASTContext *ast_context = ClangASTContext::GetScratch(*target);
if (!ast_context)
return false;

CompilerType opaque_type = ast_context->GetBasicType(eBasicTypeObjCID);
if (!opaque_type)
opaque_type = ast_context->GetBasicType(eBasicTypeVoid).GetPointerType();
Expand All @@ -123,7 +126,9 @@ bool AppleObjCRuntime::GetObjectDescription(Stream &strm, Value &value,
arg_value_list.PushValue(value);

// This is the return value:
ClangASTContext *ast_context = target->GetScratchClangASTContext();
ClangASTContext *ast_context = ClangASTContext::GetScratch(*target);
if (!ast_context)
return false;

CompilerType return_compiler_type = ast_context->GetCStringType(true);
Value ret;
Expand Down Expand Up @@ -494,9 +499,12 @@ ThreadSP AppleObjCRuntime::GetBacktraceThreadFromException(
reserved_dict = reserved_dict->GetSyntheticValue();
if (!reserved_dict) return ThreadSP();

ClangASTContext *clang_ast_context =
ClangASTContext::GetScratch(*exception_sp->GetTargetSP());
if (!clang_ast_context)
return ThreadSP();
CompilerType objc_id =
exception_sp->GetTargetSP()->GetScratchClangASTContext()->GetBasicType(
lldb::eBasicTypeObjCID);
clang_ast_context->GetBasicType(lldb::eBasicTypeObjCID);
ValueObjectSP return_addresses;

auto objc_object_from_address = [&exception_sp, &objc_id](uint64_t addr,
Expand Down

0 comments on commit 3031818

Please sign in to comment.