<div dir="ltr">Hi Gábor, Richard,<div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Hi Richard, maybe my understanding is not correct, but I believe the<br>situation is somewhat twisted and reversed...</blockquote><div><br></div><div>Is this relationship recursive? (Should it be?) I.e. When ClangASTSource uses ASTImporter to minimally import a Decl, should that Decl have another ExternalASTSource attached (possibly the original ClangASTSource) to complete the minimally imported Decl on demand?</div><div><br></div><div>-- Lang.</div><br><div class="gmail_quote"><div dir="ltr">On Tue, Aug 14, 2018 at 6:49 AM Gábor Márton <<a href="mailto:martongabesz@gmail.com">martongabesz@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">> It might be interesting to consider how this is addressed by the ExternalASTSource interface. There, we have separate "import the lexical contents of this AST node (including populating the lexical declarations list with all members, in the right order)", "import the lookup results for this name in this context (and cache them but don't add them to the list of lexical members)" and "import this specific declaration member (but don't add it to the list of lexical members)" queries. One could imagine doing something similar for the AST importer: when you're performing a minimal import but you want to import a method declaration, don't insert it into the list of lexical members of the enclosing record. Instead, defer doing that until a complete type is required (at that point, you'd need to import all the relevant members anyway, including the base classes and virtual functions in the right order).<br>
<br>
Hi Richard, maybe my understanding is not correct, but I believe the<br>
situation is somewhat twisted and reversed.<br>
Seems like LLDB already exercises the ExternalASTSource interface. The<br>
purpose of using it is exactly to load the external lexical contents<br>
of an AST node (as you suggested). However, the load is implemented by<br>
the means of the ASTImporter (in minimal mode). Consider the attached<br>
function call trace:<br>
When the user instructs LLDB to evaluate an expression then LLDB<br>
exercises the parser `clang::ParseAST`, which in turn does a lookup<br>
`clang::DeclContext::lookup`. During the lookup,<br>
`lldb_private::ClangASTSource::FindExternalVisibleDeclsByName` is<br>
called (this function overloads<br>
`ExternalASTSource::FindExternalVisibleDeclsByName`).<br>
And finally, through `lldb_private::ClangASTImporter::CopyType` we end<br>
up in `clang::ASTImporter::Import`.<br>
<br>
Gabor<br>
<br>
```<br>
#4 0x00007ffff41a72ed in clang::ASTNodeImporter::ImportDeclParts<br>
(this=this@entry=0x7fffffff8be0, D=D@entry=0x632038,<br>
DC=@0x7fffffff8ac8: 0x0,<br>
LexicalDC=@0x7fffffff8ad0: 0x0, Name=..., ToD=@0x7fffffff8ad8: 0x0, Loc=...)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/AST/ASTImporter.cpp:1062<br>
#5 0x00007ffff41a8d55 in clang::ASTNodeImporter::VisitRecordDecl<br>
(this=0x7fffffff8be0, D=0x632038)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/AST/ASTImporter.cpp:2126<br>
#6 0x00007ffff419377b in clang::ASTImporter::Import (this=0x70cc90,<br>
FromD=0x632038)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/AST/ASTImporter.cpp:7054<br>
#7 0x00007ffff41939f7 in clang::ASTNodeImporter::VisitRecordType<br>
(this=0x7fffffff8c40, T=<optimized out>)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/AST/ASTImporter.cpp:851<br>
#8 0x00007ffff4196f65 in clang::TypeVisitor<clang::ASTNodeImporter,<br>
clang::QualType>::Visit (this=this@entry=0x7fffffff8c40, T=<optimized<br>
out>)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/include/clang/AST/TypeNodes.def:92<br>
#9 0x00007ffff4197187 in clang::ASTImporter::Import (this=0x70cc90,<br>
FromT=FromT@entry=...)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/AST/ASTImporter.cpp:6983<br>
#10 0x00007ffff7351c92 in lldb_private::ClangASTImporter::CopyType<br>
(this=<optimized out>, dst_ast=<optimized out>, src_ast=<optimized<br>
out>, type=type@entry=...)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Symbol/ClangASTImporter.cpp:61<br>
#11 0x00007ffff74bdb95 in<br>
lldb_private::ClangASTSource::GuardedCopyType<br>
(this=this@entry=0x611e20, src_type=...)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp:2040<br>
#12 0x00007ffff74e9ffe in<br>
lldb_private::ClangExpressionDeclMap::GetVariableValue<br>
(this=this@entry=0x611e20, var=std::shared_ptr (count 6, weak 1)<br>
0x7fffe80020f0,<br>
var_location=..., user_type=user_type@entry=0x7fffffff8fc0,<br>
parser_type=parser_type@entry=0x7fffffff8fe0)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp:1627<br>
#13 0x00007ffff74ea3a7 in<br>
lldb_private::ClangExpressionDeclMap::AddOneVariable<br>
(this=this@entry=0x611e20, context=...,<br>
var=std::shared_ptr (count 6, weak 1) 0x7fffe80020f0, valobj=...,<br>
current_id=current_id@entry=2)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp:1679<br>
#14 0x00007ffff74ec50b in<br>
lldb_private::ClangExpressionDeclMap::FindExternalVisibleDecls<br>
(this=this@entry=0x611e20, context=..., module_sp=<br>
std::shared_ptr (empty) 0x0, namespace_decl=...,<br>
current_id=current_id@entry=2)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp:1240<br>
#15 0x00007ffff74ef4ce in<br>
lldb_private::ClangExpressionDeclMap::FindExternalVisibleDecls<br>
(this=0x611e20, context=...)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp:843<br>
#16 0x00007ffff74c71cc in<br>
lldb_private::ClangASTSource::FindExternalVisibleDeclsByName<br>
(this=0x611e20, decl_ctx=0x71d8b8, clang_decl_name=...)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp:261<br>
#17 0x00007ffff421d92d in clang::DeclContext::lookup<br>
(this=this@entry=0x71d8b8, Name=...)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/AST/DeclBase.cpp:1577<br>
#18 0x00007ffff4d4bb42 in LookupDirect (S=..., R=..., DC=DC@entry=0x71d8b8)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Sema/SemaLookup.cpp:843<br>
#19 0x00007ffff4d52f50 in CppNamespaceLookup (S=..., R=...,<br>
NS=NS@entry=0x71d8b8, UDirs=..., Context=...)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Sema/SemaLookup.cpp:942<br>
#20 0x00007ffff4d53925 in clang::Sema::CppLookupName<br>
(this=this@entry=0x7245b0, R=..., S=0x72cad0)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Sema/SemaLookup.cpp:1322<br>
#21 0x00007ffff4d53c8a in clang::Sema::LookupName (this=0x7245b0,<br>
R=..., S=0x7314f0, AllowBuiltinCreation=<optimized out>)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Sema/SemaLookup.cpp:1827<br>
#22 0x00007ffff4b6b945 in clang::Sema::ClassifyName (this=0x7245b0,<br>
S=0x7314f0, SS=..., Name=@0x7fffffffa3a8: 0x750dc8, NameLoc=...,<br>
NameLoc@entry=...,<br>
NextToken=..., IsAddressOfOperand=false,<br>
CCC=std::unique_ptr<clang::CorrectionCandidateCallback> containing<br>
0x63c250)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Sema/SemaDecl.cpp:868<br>
#23 0x00007ffff52c6a9f in clang::Parser::TryAnnotateName<br>
(this=this@entry=0x728c60,<br>
IsAddressOfOperand=IsAddressOfOperand@entry=false,<br>
CCC=std::unique_ptr<clang::CorrectionCandidateCallback> containing<br>
0x0) at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Parse/Parser.cpp:1531<br>
#24 0x00007ffff52aa56b in<br>
clang::Parser::ParseStatementOrDeclarationAfterAttributes<br>
(this=this@entry=0x728c60, Stmts=...,<br>
Allowed=Allowed@entry=clang::Parser::ACK_Any,<br>
TrailingElseLoc=<optimized out>, Attrs=...)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Parse/ParseStmt.cpp:189<br>
#25 0x00007ffff52aa843 in clang::Parser::ParseStatementOrDeclaration<br>
(this=this@entry=0x728c60, Stmts=...,<br>
Allowed=Allowed@entry=clang::Parser::ACK_Any,<br>
TrailingElseLoc=TrailingElseLoc@entry=0x0) at<br>
/home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Parse/ParseStmt.cpp:111<br>
#26 0x00007ffff52ae577 in clang::Parser::ParseCompoundStatementBody<br>
(this=this@entry=0x728c60, isStmtExpr=isStmtExpr@entry=false)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Parse/ParseStmt.cpp:1003<br>
#27 0x00007ffff52b0a49 in clang::Parser::ParseFunctionStatementBody<br>
(this=this@entry=0x728c60, Decl=Decl@entry=0x72dbd0, BodyScope=...)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Parse/ParseStmt.cpp:1972<br>
#28 0x00007ffff52cc6e0 in clang::Parser::ParseFunctionDefinition<br>
(this=this@entry=0x728c60, D=..., TemplateInfo=...,<br>
LateParsedAttrs=LateParsedAttrs@entry=0x7fffffffabe0) at<br>
/home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Parse/Parser.cpp:1248<br>
#29 0x00007ffff524aa56 in clang::Parser::ParseDeclGroup<br>
(this=this@entry=0x728c60, DS=...,<br>
Context=Context@entry=clang::DeclaratorContext::FileContext,<br>
DeclEnd=DeclEnd@entry=0x0, FRI=FRI@entry=0x0) at<br>
/home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Parse/ParseDecl.cpp:1968<br>
#30 0x00007ffff52c7f90 in<br>
clang::Parser::ParseDeclOrFunctionDefInternal<br>
(this=this@entry=0x728c60, attrs=..., DS=...,<br>
AS=AS@entry=clang::AS_none)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Parse/Parser.cpp:1012<br>
#31 0x00007ffff52c8761 in<br>
clang::Parser::ParseDeclarationOrFunctionDefinition (this=0x728c60,<br>
attrs=..., AS=clang::AS_none, DS=0x0)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Parse/Parser.cpp:1028<br>
#32 0x00007ffff52c87bf in<br>
clang::Parser::ParseDeclarationOrFunctionDefinition (this=<optimized<br>
out>, attrs=..., DS=<optimized out>, AS=<optimized out>)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Parse/Parser.cpp:1030<br>
#33 0x00007ffff52cd05d in clang::Parser::ParseExternalDeclaration<br>
(this=this@entry=0x728c60, attrs=..., DS=DS@entry=0x0)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Parse/Parser.cpp:853<br>
#34 0x00007ffff52ce05c in clang::Parser::ParseTopLevelDecl<br>
(this=this@entry=0x728c60, Result=...)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Parse/Parser.cpp:609<br>
#35 0x00007ffff522ce1b in clang::ParseAST (S=...,<br>
PrintStats=PrintStats@entry=false,<br>
SkipFunctionBodies=SkipFunctionBodies@entry=false)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Parse/ParseAST.cpp:146<br>
#36 0x00007ffff522d0e0 in clang::ParseAST (PP=..., Consumer=0x535f70,<br>
Ctx=..., PrintStats=<optimized out>, TUKind=clang::TU_Complete,<br>
CompletionConsumer=0x0,<br>
SkipFunctionBodies=false) at<br>
/home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/clang/lib/Parse/ParseAST.cpp:110<br>
#37 0x00007ffff74f0059 in lldb_private::ClangExpressionParser::Parse<br>
(this=this@entry=0x7fffffffcae0, diagnostic_manager=...)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp:619<br>
#38 0x00007ffff74d23ad in lldb_private::ClangUserExpression::Parse<br>
(this=0x6d23c0, diagnostic_manager=..., exe_ctx=...,<br>
execution_policy=lldb_private::eExecutionPolicyOnlyWhenNeeded,<br>
keep_result_in_memory=<optimized out>, generate_debug_info=<optimized<br>
out>)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp:472<br>
---Type <return> to continue, or q <return> to quit---<br>
#39 0x00007ffff72cea9e in lldb_private::UserExpression::Evaluate<br>
(exe_ctx=..., options=..., expr=..., prefix=..., result_valobj_sp=...,<br>
error=..., line_offset=0,<br>
fixed_expression=0x4bced8, jit_module_sp_ptr=0x0) at<br>
/home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Expression/UserExpression.cpp:239<br>
#40 0x00007ffff7401996 in lldb_private::Target::EvaluateExpression<br>
(this=this@entry=0x5b8d10, expr=..., exe_scope=<optimized out>,<br>
result_valobj_sp=...,<br>
options=..., fixed_expression=0x4bced8) at<br>
/home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Target/Target.cpp:2321<br>
#41 0x00007ffff77912dd in<br>
lldb_private::CommandObjectExpression::EvaluateExpression<br>
(this=this@entry=0x4bcb50, expr=expr@entry=0x7fffffffd4c0 "val.v",<br>
output_stream=output_stream@entry=0x7fffffffd5f0,<br>
error_stream=error_stream@entry=0x7fffffffd648,<br>
result=result@entry=0x7fffffffd5f0)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Commands/CommandObjectExpression.cpp:377<br>
#42 0x00007ffff7792018 in<br>
lldb_private::CommandObjectExpression::DoExecute (this=0x4bcb50,<br>
command=0x7fffffffd4c0 "val.v", result=...)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Commands/CommandObjectExpression.cpp:625<br>
#43 0x00007ffff7305baa in lldb_private::CommandObjectRaw::Execute<br>
(this=0x4bcb50, args_string=0x7fffffffd4c0 "val.v", result=...)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Interpreter/CommandObject.cpp:1013<br>
#44 0x00007ffff7302e1b in<br>
lldb_private::CommandInterpreter::HandleCommand<br>
(this=this@entry=0x4a8740, command_line=<optimized out>,<br>
lazy_add_to_history=lazy_add_to_history@entry=lldb_private::eLazyBoolCalculate,<br>
result=..., override_context=override_context@entry=0x0,<br>
repeat_on_empty_command=repeat_on_empty_command@entry=true,<br>
no_context_switching=false)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Interpreter/CommandInterpreter.cpp:1683<br>
#45 0x00007ffff73047c8 in<br>
lldb_private::CommandInterpreter::IOHandlerInputComplete<br>
(this=0x4a8740, io_handler=..., line="expr val.v")<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Interpreter/CommandInterpreter.cpp:2771<br>
#46 0x00007ffff723e241 in lldb_private::IOHandlerEditline::Run (this=0x5b4da0)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Core/IOHandler.cpp:573<br>
#47 0x00007ffff720a820 in lldb_private::Debugger::ExecuteIOHandlers<br>
(this=0x4a6f90)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Core/Debugger.cpp:961<br>
#48 0x00007ffff72f731f in<br>
lldb_private::CommandInterpreter::RunCommandInterpreter<br>
(this=0x4a8740, auto_handle_events=auto_handle_events@entry=true,<br>
spawn_thread=spawn_thread@entry=false, options=...) at<br>
/home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/Interpreter/CommandInterpreter.cpp:2971<br>
#49 0x00007ffff7094776 in lldb::SBDebugger::RunCommandInterpreter<br>
(this=this@entry=0x7fffffffda60,<br>
auto_handle_events=auto_handle_events@entry=true,<br>
spawn_thread=spawn_thread@entry=false) at<br>
/home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/source/API/SBDebugger.cpp:892<br>
#50 0x0000000000404a76 in Driver::MainLoop (this=this@entry=0x7fffffffda40)<br>
at /home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/tools/driver/Driver.cpp:1156<br>
#51 0x0000000000403724 in main (argc=2, argv=0x7fffffffdcb8) at<br>
/home/gerazo/repos/codechecker_dev_env/CTU/llvm/tools/lldb/tools/driver/Driver.cpp:1253<br>
<br>
```<br>
On Tue, Aug 14, 2018 at 2:34 AM Richard Smith via cfe-dev<br>
<<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br>
><br>
> On Mon, 13 Aug 2018 at 17:08, Lang Hames via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br>
>><br>
>> Hi Richard,<br>
>><br>
>>> Perhaps a better approach would be to make the "minimal" mode for the ASTImporter provide an ExternalASTSource to lazily complete the AST as needed (thereby avoiding violating the invariant, because you would populate the lexical declarations list whenever anyone actually asks for it).<br>
>><br>
>><br>
>> This seems worth investigating. I wonder if it might also fix another bug that I know of involving virtual methods with covariant return types. (You and I actually discussed it at one of the socials a while back, but I have not had time to dig into it further.)<br>
>><br>
>> The reproducer for the bug is:<br>
>><br>
>> class Foo {};<br>
>> class Bar : public Foo {};<br>
>><br>
>> class Base {<br>
>> public:<br>
>> virtual Foo* foo() { return nullptr; }<br>
>> };<br>
>><br>
>> class Derived : public Base {<br>
>> public:<br>
>> virtual Bar* foo() { return nullptr; }<br>
>> };<br>
>><br>
>> int main() {<br>
>> Derived D;<br>
>> D.foo(); // Evaluate 'D.foo()' here, crash LLDB.<br>
>> }<br>
>><br>
>> The issue is that since Bar's definition is not used its bases are never imported, and so the call to Bar::bases() crashes in CodeGen. If we provided an ExternalASTSource, would that be able to lazily complete the bases?<br>
><br>
><br>
> Yes, such an approach should be able to solve that problem too: when CodeGen (or indeed anything) queries any property of the definition of Foo / Bar (including whether a definition exists), you'll get a callback and a chance to provide a complete type.<br>
><br>
>><br>
>> Cheers,<br>
>> Lang.<br>
>><br>
>><br>
>> On Mon, Aug 13, 2018 at 3:30 PM Richard Smith <<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>> wrote:<br>
>>><br>
>>> On Thu, 9 Aug 2018 at 10:47, Lang Hames via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br>
>>>><br>
>>>> Hi clang-dev, lldb-dev,<br>
>>>><br>
>>>> It looks like my clang commit r305850, which modified ASTImporter to import method override tables from an external context, introduced a new bug which manifests as incorrect vtable layouts for LLDB expression code.<br>
>>>><br>
>>>> The bug itself is fairly straightforward. In r305850 I introduced the following method, which is called from ASTNodeImporter::VisitFunctionDecl:<br>
>>>><br>
>>>> void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,<br>
>>>> CXXMethodDecl *FromMethod) {<br>
>>>> for (auto *FromOverriddenMethod : FromMethod->overridden_methods())<br>
>>>> ToMethod->addOverriddenMethod(<br>
>>>> cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(<br>
>>>> FromOverriddenMethod))));<br>
>>>> }<br>
>>>><br>
>>>> This will produce the correct override table, but can also cause methods in the Base class to be visited in the wrong order. Consider:<br>
>>>><br>
>>>> class Base {<br>
>>>> public:<br>
>>>> virtual void bar() {}<br>
>>>> virtual void foo() {}<br>
>>>> };<br>
>>>><br>
>>>> class Derived : public Base {<br>
>>>> public:<br>
>>>> void foo() override {}<br>
>>>> };<br>
>>>><br>
>>>> If Derived is imported into a new context before Base, then the importer will visit Derived::foo, and (via ImportOverrides) immediately import Base::foo, but this happens before Base::bar is imported. As a consequence, the decl order on the imported Base class will end up being [ foo, bar ], instead of [ bar, foo ]. In LLDB expression evaluation this manifests as an incorrect vtable layout for Base, with foo occupying the first slot.<br>
>>>><br>
>>>> I am looking for suggestions on the right way to fix this. A brute force solution might be to always have ASTNodeImporter::VisitRecordDecl visit all base classes, then all virtual methods, which would ensure they are visited in the original decl order. However I do not know whether this covers all paths by which a CXXRecordDecl might be imported, nor whether the performance of this solution would be acceptable (it seems like it would preclude a lot of laziness).<br>
>>>><br>
>>>> Alternatively we might be able to associate an index with each imported decl and sort on that when we complete the type, but that would leave imported decls in the wrong order until the type was complete, and since I do not know all the use cases for the importer I'm concerned people may rely on the decl order before type is complete.<br>
>>>><br>
>>>> Any insight from ASTImporter experts would be greatly appreciated. :)<br>
>>><br>
>>><br>
>>> Hi Lang,<br>
>>><br>
>>> It might be interesting to consider how this is addressed by the ExternalASTSource interface. There, we have separate "import the lexical contents of this AST node (including populating the lexical declarations list with all members, in the right order)", "import the lookup results for this name in this context (and cache them but don't add them to the list of lexical members)" and "import this specific declaration member (but don't add it to the list of lexical members)" queries. One could imagine doing something similar for the AST importer: when you're performing a minimal import but you want to import a method declaration, don't insert it into the list of lexical members of the enclosing record. Instead, defer doing that until a complete type is required (at that point, you'd need to import all the relevant members anyway, including the base classes and virtual functions in the right order).<br>
>>><br>
>>> The above approach would violate AST invariants (you'd have a declaration whose lexical parent doesn't believe it lexically contains the child), but I don't know off-hand whether that would be problematic. Perhaps a better approach would be to make the "minimal" mode for the ASTImporter provide an ExternalASTSource to lazily complete the AST as needed (thereby avoiding violating the invariant, because you would populate the lexical declarations list whenever anyone actually asks for it).<br>
>><br>
>> _______________________________________________<br>
>> cfe-dev mailing list<br>
>> <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
>> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
><br>
> _______________________________________________<br>
> cfe-dev mailing list<br>
> <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div></div></div>