Skip to content

Commit 4abdd23

Browse files
committed
[PATCH 7/7] [clang] improve NestedNameSpecifier: LLDB changes
Patch series starting at #147835
1 parent bcd1eeb commit 4abdd23

File tree

18 files changed

+193
-183
lines changed

18 files changed

+193
-183
lines changed

clang/include/clang/AST/DeclBase.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,12 @@ class alignas(8) Decl {
622622

623623
void setReferenced(bool R = true) { Referenced = R; }
624624

625+
/// When doing manipulations which might change the computed linkage,
626+
/// such as changing the DeclContext after the declaration has already been
627+
/// used, invalidating the cache will make sure its linkage will be
628+
/// recomputed.
629+
void invalidateCachedLinkage() { setCachedLinkage(Linkage::Invalid); }
630+
625631
/// Whether this declaration is a top-level declaration (function,
626632
/// global variable, etc.) that is lexically inside an objc container
627633
/// definition.

clang/include/clang/AST/QualTypeNames.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ std::string getFullyQualifiedName(QualType QT, const ASTContext &Ctx,
8787
/// specifier "::" should be prepended or not.
8888
QualType getFullyQualifiedType(QualType QT, const ASTContext &Ctx,
8989
bool WithGlobalNsPrefix = false);
90+
91+
/// Get the fully qualified name for the declared context of a declaration.
92+
///
93+
/// \param[in] Ctx - the ASTContext to be used.
94+
/// \param[in] Decl - the declaration for which to get the fully qualified name.
95+
/// \param[in] WithGlobalNsPrefix - If true, then the global namespace
96+
/// specifier "::" will be prepended to the fully qualified name.
97+
NestedNameSpecifier
98+
getFullyQualifiedDeclaredContext(const ASTContext &Ctx, const Decl *Decl,
99+
bool WithGlobalNsPrefix = false);
90100
} // end namespace TypeName
91101
} // end namespace clang
92102
#endif // LLVM_CLANG_AST_QUALTYPENAMES_H

clang/lib/AST/QualTypeNames.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,5 +491,12 @@ std::string getFullyQualifiedName(QualType QT,
491491
return FQQT.getAsString(Policy);
492492
}
493493

494+
NestedNameSpecifier getFullyQualifiedDeclaredContext(const ASTContext &Ctx,
495+
const Decl *Decl,
496+
bool WithGlobalNsPrefix) {
497+
return createNestedNameSpecifierForScopeOf(Ctx, Decl, /*FullyQualified=*/true,
498+
WithGlobalNsPrefix);
499+
}
500+
494501
} // end namespace TypeName
495502
} // end namespace clang

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ class DeclContextOverride {
123123

124124
decl->setDeclContext(decl->getASTContext().getTranslationUnitDecl());
125125
decl->setLexicalDeclContext(decl->getASTContext().getTranslationUnitDecl());
126+
// Changing the DeclContext might change the linkage. For example, if the
127+
// entity was previously declared inside a function, it will not be
128+
// external, but changing the declaration context to the TU will make it
129+
// external. Make sure this will recompute the linkage if it was computed
130+
// before.
131+
decl->invalidateCachedLinkage();
126132
}
127133

128134
bool ChainPassesThrough(
@@ -320,7 +326,8 @@ CompilerType ClangASTImporter::DeportType(TypeSystemClang &dst,
320326
DeclContextOverride decl_context_override;
321327

322328
if (auto *t = ClangUtil::GetQualType(src_type)->getAs<TagType>())
323-
decl_context_override.OverrideAllDeclsFromContainingFunction(t->getDecl());
329+
decl_context_override.OverrideAllDeclsFromContainingFunction(
330+
t->getOriginalDecl());
324331

325332
CompleteTagDeclsScope complete_scope(*this, &dst.getASTContext(),
326333
&src_ctxt->getASTContext());
@@ -377,8 +384,7 @@ bool ClangASTImporter::CanImport(const CompilerType &type) {
377384
} break;
378385

379386
case clang::Type::Enum: {
380-
clang::EnumDecl *enum_decl =
381-
llvm::cast<clang::EnumType>(qual_type)->getDecl();
387+
auto *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getOriginalDecl();
382388
if (enum_decl) {
383389
if (GetDeclOrigin(enum_decl).Valid())
384390
return true;
@@ -414,12 +420,6 @@ bool ClangASTImporter::CanImport(const CompilerType &type) {
414420
->getDeducedType()
415421
.getAsOpaquePtr()));
416422

417-
case clang::Type::Elaborated:
418-
return CanImport(CompilerType(type.GetTypeSystem(),
419-
llvm::cast<clang::ElaboratedType>(qual_type)
420-
->getNamedType()
421-
.getAsOpaquePtr()));
422-
423423
case clang::Type::Paren:
424424
return CanImport(CompilerType(
425425
type.GetTypeSystem(),
@@ -452,7 +452,7 @@ bool ClangASTImporter::Import(const CompilerType &type) {
452452

453453
case clang::Type::Enum: {
454454
clang::EnumDecl *enum_decl =
455-
llvm::cast<clang::EnumType>(qual_type)->getDecl();
455+
llvm::cast<clang::EnumType>(qual_type)->getOriginalDecl();
456456
if (enum_decl) {
457457
if (GetDeclOrigin(enum_decl).Valid())
458458
return CompleteAndFetchChildren(qual_type);
@@ -488,12 +488,6 @@ bool ClangASTImporter::Import(const CompilerType &type) {
488488
->getDeducedType()
489489
.getAsOpaquePtr()));
490490

491-
case clang::Type::Elaborated:
492-
return Import(CompilerType(type.GetTypeSystem(),
493-
llvm::cast<clang::ElaboratedType>(qual_type)
494-
->getNamedType()
495-
.getAsOpaquePtr()));
496-
497491
case clang::Type::Paren:
498492
return Import(CompilerType(
499493
type.GetTypeSystem(),
@@ -597,7 +591,7 @@ bool ExtractBaseOffsets(const ASTRecordLayout &record_layout,
597591
return false;
598592

599593
DeclFromUser<RecordDecl> origin_base_record(
600-
origin_base_record_type->getDecl());
594+
origin_base_record_type->getOriginalDecl());
601595

602596
if (origin_base_record.IsInvalid())
603597
return false;
@@ -728,7 +722,8 @@ bool ClangASTImporter::importRecordLayoutFromOrigin(
728722

729723
QualType base_type = bi->getType();
730724
const RecordType *base_record_type = base_type->getAs<RecordType>();
731-
DeclFromParser<RecordDecl> base_record(base_record_type->getDecl());
725+
DeclFromParser<RecordDecl> base_record(
726+
base_record_type->getOriginalDecl());
732727
DeclFromParser<CXXRecordDecl> base_cxx_record =
733728
DynCast<CXXRecordDecl>(base_record);
734729

@@ -860,7 +855,7 @@ bool ClangASTImporter::CompleteAndFetchChildren(clang::QualType type) {
860855
Log *log = GetLog(LLDBLog::Expressions);
861856

862857
if (const TagType *tag_type = type->getAs<TagType>()) {
863-
TagDecl *tag_decl = tag_type->getDecl();
858+
TagDecl *tag_decl = tag_type->getOriginalDecl();
864859

865860
DeclOrigin decl_origin = GetDeclOrigin(tag_decl);
866861

@@ -928,9 +923,9 @@ bool ClangASTImporter::RequireCompleteType(clang::QualType type) {
928923
return false;
929924

930925
if (const TagType *tag_type = type->getAs<TagType>()) {
931-
TagDecl *tag_decl = tag_type->getDecl();
926+
TagDecl *tag_decl = tag_type->getOriginalDecl();
932927

933-
if (tag_decl->getDefinition() || tag_decl->isBeingDefined())
928+
if (tag_decl->getDefinition())
934929
return true;
935930

936931
return CompleteTagDecl(tag_decl);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ TagDecl *ClangASTSource::FindCompleteType(const TagDecl *decl) {
223223
continue;
224224

225225
TagDecl *candidate_tag_decl =
226-
const_cast<TagDecl *>(tag_type->getDecl());
226+
tag_type->getOriginalDecl()->getDefinitionOrSelf();
227227

228228
if (TypeSystemClang::GetCompleteDecl(
229229
&candidate_tag_decl->getASTContext(), candidate_tag_decl))
@@ -250,7 +250,8 @@ TagDecl *ClangASTSource::FindCompleteType(const TagDecl *decl) {
250250
if (!tag_type)
251251
continue;
252252

253-
TagDecl *candidate_tag_decl = const_cast<TagDecl *>(tag_type->getDecl());
253+
TagDecl *candidate_tag_decl =
254+
tag_type->getOriginalDecl()->getDefinitionOrSelf();
254255

255256
if (TypeSystemClang::GetCompleteDecl(&candidate_tag_decl->getASTContext(),
256257
candidate_tag_decl))

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ void ClangExpressionDeclMap::LookUpLldbClass(NameSearchContext &context) {
839839

840840
clang::CXXRecordDecl *class_decl = method_decl->getParent();
841841

842-
QualType class_qual_type(class_decl->getTypeForDecl(), 0);
842+
QualType class_qual_type = m_ast_context->getCanonicalTagType(class_decl);
843843

844844
TypeFromUser class_user_type(
845845
class_qual_type.getAsOpaquePtr(),
@@ -1561,7 +1561,7 @@ ClangExpressionDeclMap::AddExpressionVariable(NameSearchContext &context,
15611561

15621562
if (const clang::Type *parser_type = parser_opaque_type.getTypePtr()) {
15631563
if (const TagType *tag_type = dyn_cast<TagType>(parser_type))
1564-
CompleteType(tag_type->getDecl());
1564+
CompleteType(tag_type->getOriginalDecl()->getDefinitionOrSelf());
15651565
if (const ObjCObjectPointerType *objc_object_ptr_type =
15661566
dyn_cast<ObjCObjectPointerType>(parser_type))
15671567
CompleteType(objc_object_ptr_type->getInterfaceDecl());

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ ClangPersistentVariables::GetCompilerTypeFromPersistentDecl(
7979
if (p.m_decl == nullptr)
8080
return std::nullopt;
8181

82+
auto ctx = std::static_pointer_cast<TypeSystemClang>(p.m_context.lock());
8283
if (clang::TypeDecl *tdecl = llvm::dyn_cast<clang::TypeDecl>(p.m_decl)) {
83-
opaque_compiler_type_t t = static_cast<opaque_compiler_type_t>(
84-
const_cast<clang::Type *>(tdecl->getTypeForDecl()));
84+
opaque_compiler_type_t t =
85+
static_cast<opaque_compiler_type_t>(const_cast<clang::Type *>(
86+
ctx->getASTContext().getTypeDeclType(tdecl).getTypePtr()));
8587
return CompilerType(p.m_context, t);
8688
}
8789
return std::nullopt;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ NameSearchContext::AddTypeDecl(const CompilerType &clang_type) {
153153

154154
return (NamedDecl *)typedef_name_decl;
155155
} else if (const TagType *tag_type = qual_type->getAs<TagType>()) {
156-
TagDecl *tag_decl = tag_type->getDecl();
156+
TagDecl *tag_decl = tag_type->getOriginalDecl()->getDefinitionOrSelf();
157157

158158
m_decls.push_back(tag_decl);
159159

lldb/source/Plugins/Language/ObjC/NSDictionary.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ static CompilerType GetLLDBNSPairType(TargetSP target_sp) {
7373

7474
static constexpr llvm::StringLiteral g_lldb_autogen_nspair("__lldb_autogen_nspair");
7575

76-
compiler_type = scratch_ts_sp->GetTypeForIdentifier<clang::CXXRecordDecl>(g_lldb_autogen_nspair);
76+
compiler_type = scratch_ts_sp->GetTypeForIdentifier<clang::CXXRecordDecl>(
77+
scratch_ts_sp->getASTContext(), g_lldb_autogen_nspair);
7778

7879
if (!compiler_type) {
7980
compiler_type = scratch_ts_sp->CreateRecordType(

lldb/source/Plugins/RegisterTypeBuilder/RegisterTypeBuilderClang.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ CompilerType RegisterTypeBuilderClang::GetRegisterType(
4747
// See if we have made this type before and can reuse it.
4848
CompilerType fields_type =
4949
type_system->GetTypeForIdentifier<clang::CXXRecordDecl>(
50-
register_type_name);
50+
type_system->getASTContext(), register_type_name);
5151

5252
if (!fields_type) {
5353
// In most ABI, a change of field type means a change in storage unit.
@@ -83,7 +83,7 @@ CompilerType RegisterTypeBuilderClang::GetRegisterType(
8383
// may have built this one already.
8484
CompilerType field_enum_type =
8585
type_system->GetTypeForIdentifier<clang::EnumDecl>(
86-
enum_type_name);
86+
type_system->getASTContext(), enum_type_name);
8787

8888
if (field_enum_type)
8989
field_type = field_enum_type;

0 commit comments

Comments
 (0)