Skip to content

[PATCH 7/7] [clang] improve NestedNameSpecifier: LLDB changes #149949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: users/mizvekov/name-qualification-refactor-6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions clang/include/clang/AST/DeclBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,12 @@ class alignas(8) Decl {

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

/// When doing manipulations which might change the computed linkage,
/// such as changing the DeclContext after the declaration has already been
/// used, invalidating the cache will make sure its linkage will be
/// recomputed.
void invalidateCachedLinkage() { setCachedLinkage(Linkage::Invalid); }

/// Whether this declaration is a top-level declaration (function,
/// global variable, etc.) that is lexically inside an objc container
/// definition.
Expand Down
10 changes: 10 additions & 0 deletions clang/include/clang/AST/QualTypeNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ std::string getFullyQualifiedName(QualType QT, const ASTContext &Ctx,
/// specifier "::" should be prepended or not.
QualType getFullyQualifiedType(QualType QT, const ASTContext &Ctx,
bool WithGlobalNsPrefix = false);

/// Get the fully qualified name for the declared context of a declaration.
///
/// \param[in] Ctx - the ASTContext to be used.
/// \param[in] Decl - the declaration for which to get the fully qualified name.
/// \param[in] WithGlobalNsPrefix - If true, then the global namespace
/// specifier "::" will be prepended to the fully qualified name.
NestedNameSpecifier
getFullyQualifiedDeclaredContext(const ASTContext &Ctx, const Decl *Decl,
bool WithGlobalNsPrefix = false);
} // end namespace TypeName
} // end namespace clang
#endif // LLVM_CLANG_AST_QUALTYPENAMES_H
7 changes: 7 additions & 0 deletions clang/lib/AST/QualTypeNames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,5 +491,12 @@ std::string getFullyQualifiedName(QualType QT,
return FQQT.getAsString(Policy);
}

NestedNameSpecifier getFullyQualifiedDeclaredContext(const ASTContext &Ctx,
const Decl *Decl,
bool WithGlobalNsPrefix) {
return createNestedNameSpecifierForScopeOf(Ctx, Decl, /*FullyQualified=*/true,
WithGlobalNsPrefix);
}

} // end namespace TypeName
} // end namespace clang
37 changes: 16 additions & 21 deletions lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ class DeclContextOverride {

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

bool ChainPassesThrough(
Expand Down Expand Up @@ -320,7 +326,8 @@ CompilerType ClangASTImporter::DeportType(TypeSystemClang &dst,
DeclContextOverride decl_context_override;

if (auto *t = ClangUtil::GetQualType(src_type)->getAs<TagType>())
decl_context_override.OverrideAllDeclsFromContainingFunction(t->getDecl());
decl_context_override.OverrideAllDeclsFromContainingFunction(
t->getOriginalDecl());

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

case clang::Type::Enum: {
clang::EnumDecl *enum_decl =
llvm::cast<clang::EnumType>(qual_type)->getDecl();
auto *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getOriginalDecl();
if (enum_decl) {
if (GetDeclOrigin(enum_decl).Valid())
return true;
Expand Down Expand Up @@ -414,12 +420,6 @@ bool ClangASTImporter::CanImport(const CompilerType &type) {
->getDeducedType()
.getAsOpaquePtr()));

case clang::Type::Elaborated:
return CanImport(CompilerType(type.GetTypeSystem(),
llvm::cast<clang::ElaboratedType>(qual_type)
->getNamedType()
.getAsOpaquePtr()));

case clang::Type::Paren:
return CanImport(CompilerType(
type.GetTypeSystem(),
Expand Down Expand Up @@ -452,7 +452,7 @@ bool ClangASTImporter::Import(const CompilerType &type) {

case clang::Type::Enum: {
clang::EnumDecl *enum_decl =
llvm::cast<clang::EnumType>(qual_type)->getDecl();
llvm::cast<clang::EnumType>(qual_type)->getOriginalDecl();
if (enum_decl) {
if (GetDeclOrigin(enum_decl).Valid())
return CompleteAndFetchChildren(qual_type);
Expand Down Expand Up @@ -488,12 +488,6 @@ bool ClangASTImporter::Import(const CompilerType &type) {
->getDeducedType()
.getAsOpaquePtr()));

case clang::Type::Elaborated:
return Import(CompilerType(type.GetTypeSystem(),
llvm::cast<clang::ElaboratedType>(qual_type)
->getNamedType()
.getAsOpaquePtr()));

case clang::Type::Paren:
return Import(CompilerType(
type.GetTypeSystem(),
Expand Down Expand Up @@ -597,7 +591,7 @@ bool ExtractBaseOffsets(const ASTRecordLayout &record_layout,
return false;

DeclFromUser<RecordDecl> origin_base_record(
origin_base_record_type->getDecl());
origin_base_record_type->getOriginalDecl());

if (origin_base_record.IsInvalid())
return false;
Expand Down Expand Up @@ -728,7 +722,8 @@ bool ClangASTImporter::importRecordLayoutFromOrigin(

QualType base_type = bi->getType();
const RecordType *base_record_type = base_type->getAs<RecordType>();
DeclFromParser<RecordDecl> base_record(base_record_type->getDecl());
DeclFromParser<RecordDecl> base_record(
base_record_type->getOriginalDecl());
DeclFromParser<CXXRecordDecl> base_cxx_record =
DynCast<CXXRecordDecl>(base_record);

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

if (const TagType *tag_type = type->getAs<TagType>()) {
TagDecl *tag_decl = tag_type->getDecl();
TagDecl *tag_decl = tag_type->getOriginalDecl();

DeclOrigin decl_origin = GetDeclOrigin(tag_decl);

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

if (const TagType *tag_type = type->getAs<TagType>()) {
TagDecl *tag_decl = tag_type->getDecl();
TagDecl *tag_decl = tag_type->getOriginalDecl();

if (tag_decl->getDefinition() || tag_decl->isBeingDefined())
if (tag_decl->getDefinition())
return true;

return CompleteTagDecl(tag_decl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ TagDecl *ClangASTSource::FindCompleteType(const TagDecl *decl) {
continue;

TagDecl *candidate_tag_decl =
const_cast<TagDecl *>(tag_type->getDecl());
tag_type->getOriginalDecl()->getDefinitionOrSelf();

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

TagDecl *candidate_tag_decl = const_cast<TagDecl *>(tag_type->getDecl());
TagDecl *candidate_tag_decl =
tag_type->getOriginalDecl()->getDefinitionOrSelf();

if (TypeSystemClang::GetCompleteDecl(&candidate_tag_decl->getASTContext(),
candidate_tag_decl))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ void ClangExpressionDeclMap::LookUpLldbClass(NameSearchContext &context) {

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

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

TypeFromUser class_user_type(
class_qual_type.getAsOpaquePtr(),
Expand Down Expand Up @@ -1561,7 +1561,7 @@ ClangExpressionDeclMap::AddExpressionVariable(NameSearchContext &context,

if (const clang::Type *parser_type = parser_opaque_type.getTypePtr()) {
if (const TagType *tag_type = dyn_cast<TagType>(parser_type))
CompleteType(tag_type->getDecl());
CompleteType(tag_type->getOriginalDecl()->getDefinitionOrSelf());
if (const ObjCObjectPointerType *objc_object_ptr_type =
dyn_cast<ObjCObjectPointerType>(parser_type))
CompleteType(objc_object_ptr_type->getInterfaceDecl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ ClangPersistentVariables::GetCompilerTypeFromPersistentDecl(
if (p.m_decl == nullptr)
return std::nullopt;

auto ctx = std::static_pointer_cast<TypeSystemClang>(p.m_context.lock());
if (clang::TypeDecl *tdecl = llvm::dyn_cast<clang::TypeDecl>(p.m_decl)) {
opaque_compiler_type_t t = static_cast<opaque_compiler_type_t>(
const_cast<clang::Type *>(tdecl->getTypeForDecl()));
opaque_compiler_type_t t =
static_cast<opaque_compiler_type_t>(const_cast<clang::Type *>(
ctx->getASTContext().getTypeDeclType(tdecl).getTypePtr()));
return CompilerType(p.m_context, t);
}
return std::nullopt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ NameSearchContext::AddTypeDecl(const CompilerType &clang_type) {

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

m_decls.push_back(tag_decl);

Expand Down
3 changes: 2 additions & 1 deletion lldb/source/Plugins/Language/ObjC/NSDictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ static CompilerType GetLLDBNSPairType(TargetSP target_sp) {

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

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

if (!compiler_type) {
compiler_type = scratch_ts_sp->CreateRecordType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ CompilerType RegisterTypeBuilderClang::GetRegisterType(
// See if we have made this type before and can reuse it.
CompilerType fields_type =
type_system->GetTypeForIdentifier<clang::CXXRecordDecl>(
register_type_name);
type_system->getASTContext(), register_type_name);

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

if (field_enum_type)
field_type = field_enum_type;
Expand Down
11 changes: 6 additions & 5 deletions lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ bool PdbAstBuilder::CompleteTagDecl(clang::TagDecl &tag) {
->GetIndex();
lldbassert(IsTagRecord(type_id, index.tpi()));

clang::QualType tag_qt = m_clang.getASTContext().getTypeDeclType(&tag);
clang::QualType tag_qt = m_clang.getASTContext().getCanonicalTagType(&tag);
TypeSystemClang::SetHasExternalStorage(tag_qt.getAsOpaquePtr(), false);

TypeIndex tag_ti = type_id.index;
Expand Down Expand Up @@ -562,7 +562,8 @@ clang::QualType PdbAstBuilder::CreatePointerType(const PointerRecord &pointer) {
m_clang.getASTContext(), spelling));
}
return m_clang.getASTContext().getMemberPointerType(
pointee_type, /*Qualifier=*/nullptr, class_type->getAsCXXRecordDecl());
pointee_type, /*Qualifier=*/std::nullopt,
class_type->getAsCXXRecordDecl());
}

clang::QualType pointer_type;
Expand Down Expand Up @@ -859,9 +860,9 @@ PdbAstBuilder::CreateFunctionDecl(PdbCompilandSymId func_id,
SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
m_clang.GetSymbolFile()->GetBackingSymbolFile());
PdbIndex &index = pdb->GetIndex();
clang::QualType parent_qt = llvm::cast<clang::TypeDecl>(parent)
->getTypeForDecl()
->getCanonicalTypeInternal();
clang::CanQualType parent_qt =
m_clang.getASTContext().getCanonicalTypeDeclType(
llvm::cast<clang::TypeDecl>(parent));
lldb::opaque_compiler_type_t parent_opaque_ty =
ToCompilerType(parent_qt).GetOpaqueQualType();
// FIXME: Remove this workaround.
Expand Down
11 changes: 6 additions & 5 deletions lldb/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,8 @@ lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) {
// symbols in PDB for types with const or volatile modifiers, but we need
// to create only one declaration for them all.
Type::ResolveState type_resolve_state;
CompilerType clang_type =
m_ast.GetTypeForIdentifier<clang::CXXRecordDecl>(name, decl_context);
CompilerType clang_type = m_ast.GetTypeForIdentifier<clang::CXXRecordDecl>(
m_ast.getASTContext(), name, decl_context);
if (!clang_type.IsValid()) {
auto access = GetAccessibilityForUdt(*udt);

Expand Down Expand Up @@ -479,8 +479,8 @@ lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) {
uint64_t bytes = enum_type->getLength();

// Check if such an enum already exists in the current context
CompilerType ast_enum =
m_ast.GetTypeForIdentifier<clang::EnumDecl>(name, decl_context);
CompilerType ast_enum = m_ast.GetTypeForIdentifier<clang::EnumDecl>(
m_ast.getASTContext(), name, decl_context);
if (!ast_enum.IsValid()) {
auto underlying_type_up = enum_type->getUnderlyingType();
if (!underlying_type_up)
Expand Down Expand Up @@ -557,7 +557,8 @@ lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) {

// Check if such a typedef already exists in the current context
CompilerType ast_typedef =
m_ast.GetTypeForIdentifier<clang::TypedefNameDecl>(name, decl_ctx);
m_ast.GetTypeForIdentifier<clang::TypedefNameDecl>(
m_ast.getASTContext(), name, decl_ctx);
if (!ast_typedef.IsValid()) {
CompilerType target_ast_type = target_type->GetFullCompilerType();

Expand Down
Loading
Loading