Skip to content

Commit 8968183

Browse files
committed
[lldb][ClangASTImporter][NFC] Factor out completion logic out of ClangASTImporterDelegate
Upstreams two helpers that make this more readable.
1 parent 8c8f328 commit 8968183

File tree

2 files changed

+44
-29
lines changed

2 files changed

+44
-29
lines changed

lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,35 @@ class CompleteTagDeclsScope : public ClangASTImporter::NewDeclListener {
235235
clang::ASTContext *m_src_ctx;
236236
ClangASTImporter &importer;
237237

238+
void CompleteDecl(
239+
Decl *decl,
240+
lldb_private::ClangASTImporter::ASTContextMetadata const &to_context_md) {
241+
// The decl that should be completed has to be imported into the target
242+
// context from some other context.
243+
assert(to_context_md.hasOrigin(decl));
244+
// We should only complete decls coming from the source context.
245+
assert(to_context_md.getOrigin(decl).ctx == m_src_ctx);
246+
247+
Decl *original_decl = to_context_md.getOrigin(decl).decl;
248+
249+
// Complete the decl now.
250+
TypeSystemClang::GetCompleteDecl(m_src_ctx, original_decl);
251+
if (auto *tag_decl = dyn_cast<TagDecl>(decl)) {
252+
if (auto *original_tag_decl = dyn_cast<TagDecl>(original_decl)) {
253+
if (original_tag_decl->isCompleteDefinition()) {
254+
m_delegate->ImportDefinitionTo(tag_decl, original_tag_decl);
255+
tag_decl->setCompleteDefinition(true);
256+
}
257+
}
258+
259+
tag_decl->setHasExternalLexicalStorage(false);
260+
tag_decl->setHasExternalVisibleStorage(false);
261+
} else if (auto *container_decl = dyn_cast<ObjCContainerDecl>(decl)) {
262+
container_decl->setHasExternalLexicalStorage(false);
263+
container_decl->setHasExternalVisibleStorage(false);
264+
}
265+
}
266+
238267
public:
239268
/// Constructs a CompleteTagDeclsScope.
240269
/// \param importer The ClangASTImporter that we should observe.
@@ -257,30 +286,7 @@ class CompleteTagDeclsScope : public ClangASTImporter::NewDeclListener {
257286
NamedDecl *decl = m_decls_to_complete.pop_back_val();
258287
m_decls_already_completed.insert(decl);
259288

260-
// The decl that should be completed has to be imported into the target
261-
// context from some other context.
262-
assert(to_context_md->hasOrigin(decl));
263-
// We should only complete decls coming from the source context.
264-
assert(to_context_md->getOrigin(decl).ctx == m_src_ctx);
265-
266-
Decl *original_decl = to_context_md->getOrigin(decl).decl;
267-
268-
// Complete the decl now.
269-
TypeSystemClang::GetCompleteDecl(m_src_ctx, original_decl);
270-
if (auto *tag_decl = dyn_cast<TagDecl>(decl)) {
271-
if (auto *original_tag_decl = dyn_cast<TagDecl>(original_decl)) {
272-
if (original_tag_decl->isCompleteDefinition()) {
273-
m_delegate->ImportDefinitionTo(tag_decl, original_tag_decl);
274-
tag_decl->setCompleteDefinition(true);
275-
}
276-
}
277-
278-
tag_decl->setHasExternalLexicalStorage(false);
279-
tag_decl->setHasExternalVisibleStorage(false);
280-
} else if (auto *container_decl = dyn_cast<ObjCContainerDecl>(decl)) {
281-
container_decl->setHasExternalLexicalStorage(false);
282-
container_decl->setHasExternalVisibleStorage(false);
283-
}
289+
CompleteDecl(decl, *to_context_md);
284290

285291
to_context_md->removeOrigin(decl);
286292
}
@@ -1370,6 +1376,18 @@ void ClangASTImporter::ASTImporterDelegate::Imported(clang::Decl *from,
13701376
from, m_source_ctx, &to->getASTContext());
13711377
}
13721378

1379+
if (auto *to_namespace_decl = dyn_cast<NamespaceDecl>(to)) {
1380+
m_main.BuildNamespaceMap(to_namespace_decl);
1381+
to_namespace_decl->setHasExternalVisibleStorage();
1382+
}
1383+
1384+
MarkDeclImported(from, to);
1385+
}
1386+
1387+
void ClangASTImporter::ASTImporterDelegate::MarkDeclImported(Decl *from,
1388+
Decl *to) {
1389+
Log *log = GetLog(LLDBLog::Expressions);
1390+
13731391
if (auto *to_tag_decl = dyn_cast<TagDecl>(to)) {
13741392
to_tag_decl->setHasExternalLexicalStorage();
13751393
to_tag_decl->getPrimaryContext()->setMustBuildLookupTable();
@@ -1384,11 +1402,6 @@ void ClangASTImporter::ASTImporterDelegate::Imported(clang::Decl *from,
13841402
(to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
13851403
}
13861404

1387-
if (auto *to_namespace_decl = dyn_cast<NamespaceDecl>(to)) {
1388-
m_main.BuildNamespaceMap(to_namespace_decl);
1389-
to_namespace_decl->setHasExternalVisibleStorage();
1390-
}
1391-
13921405
if (auto *to_container_decl = dyn_cast<ObjCContainerDecl>(to)) {
13931406
to_container_decl->setHasExternalLexicalStorage();
13941407
to_container_decl->setHasExternalVisibleStorage();

lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ class ClangASTImporter {
346346
llvm::Expected<clang::Decl *> ImportImpl(clang::Decl *From) override;
347347

348348
private:
349+
void MarkDeclImported(clang::Decl *from, clang::Decl *to);
350+
349351
/// Decls we should ignore when mapping decls back to their original
350352
/// ASTContext. Used by the CxxModuleHandler to mark declarations that
351353
/// were created from the 'std' C++ module to prevent that the Importer

0 commit comments

Comments
 (0)