Skip to content

Commit

Permalink
[lldb] Merge CompilerContextKind::{Class,Struct} (#96145)
Browse files Browse the repository at this point in the history
Our dwarf parsing code treats structures and classes as interchangable.
CompilerContextKind is used when looking DIEs for types. This makes sure
we always they're treated the same way.

See also
[#95905#discussion_r1645686628](#95905 (comment)).
  • Loading branch information
labath authored Jun 24, 2024
1 parent 1c025fb commit 599ca71
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 62 deletions.
10 changes: 6 additions & 4 deletions lldb/include/lldb/lldb-private-enumerations.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLDB_LLDB_PRIVATE_ENUMERATIONS_H

#include "lldb/lldb-enumerations.h"
#include "llvm/ADT/BitmaskEnum.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FormatProviders.h"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -197,8 +198,7 @@ enum class CompilerContextKind : uint16_t {
TranslationUnit = 1,
Module = 1 << 1,
Namespace = 1 << 2,
Class = 1 << 3,
Struct = 1 << 4,
ClassOrStruct = 1 << 3,
Union = 1 << 5,
Function = 1 << 6,
Variable = 1 << 7,
Expand All @@ -210,10 +210,12 @@ enum class CompilerContextKind : uint16_t {
/// Match 0..n nested modules.
AnyModule = Any | Module,
/// Match any type.
AnyType = Any | Class | Struct | Union | Enum | Typedef | Builtin,
AnyType = Any | ClassOrStruct | Union | Enum | Typedef | Builtin,
/// Math any declaration context.
AnyDeclContext = Any | Namespace | Class | Struct | Union | Enum | Function
AnyDeclContext = Any | Namespace | ClassOrStruct | Union | Enum | Function,
LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/AnyDeclContext),
};
LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();

// Enumerations that can be used to specify the kind of metric we're looking at
// when collecting stats.
Expand Down
12 changes: 4 additions & 8 deletions lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,15 +394,13 @@ static void GetDeclContextImpl(DWARFDIE die,
case DW_TAG_namespace:
push_ctx(CompilerContextKind::Namespace, die.GetName());
break;
case DW_TAG_class_type:
case DW_TAG_structure_type:
push_ctx(CompilerContextKind::Struct, die.GetName());
push_ctx(CompilerContextKind::ClassOrStruct, die.GetName());
break;
case DW_TAG_union_type:
push_ctx(CompilerContextKind::Union, die.GetName());
break;
case DW_TAG_class_type:
push_ctx(CompilerContextKind::Class, die.GetName());
break;
case DW_TAG_enumeration_type:
push_ctx(CompilerContextKind::Enum, die.GetName());
break;
Expand Down Expand Up @@ -456,15 +454,13 @@ static void GetTypeLookupContextImpl(DWARFDIE die,
case DW_TAG_namespace:
push_ctx(CompilerContextKind::Namespace, die.GetName());
break;
case DW_TAG_class_type:
case DW_TAG_structure_type:
push_ctx(CompilerContextKind::Struct, die.GetName());
push_ctx(CompilerContextKind::ClassOrStruct, die.GetName());
break;
case DW_TAG_union_type:
push_ctx(CompilerContextKind::Union, die.GetName());
break;
case DW_TAG_class_type:
push_ctx(CompilerContextKind::Class, die.GetName());
break;
case DW_TAG_enumeration_type:
push_ctx(CompilerContextKind::Enum, die.GetName());
break;
Expand Down
6 changes: 2 additions & 4 deletions lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9173,10 +9173,8 @@ static CompilerContextKind GetCompilerKind(clang::Decl::Kind clang_kind,
if (decl_ctx) {
if (decl_ctx->isFunctionOrMethod())
return CompilerContextKind::Function;
else if (decl_ctx->isRecord())
return (CompilerContextKind)((uint16_t)CompilerContextKind::Class |
(uint16_t)CompilerContextKind::Struct |
(uint16_t)CompilerContextKind::Union);
if (decl_ctx->isRecord())
return CompilerContextKind::ClassOrStruct | CompilerContextKind::Union;
}
break;
}
Expand Down
25 changes: 10 additions & 15 deletions lldb/source/Symbol/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,18 @@ bool lldb_private::contextMatches(llvm::ArrayRef<CompilerContext> context_chain,
static CompilerContextKind ConvertTypeClass(lldb::TypeClass type_class) {
if (type_class == eTypeClassAny)
return CompilerContextKind::AnyType;
uint16_t result = 0;
if (type_class & lldb::eTypeClassClass)
result |= (uint16_t)CompilerContextKind::Class;
if (type_class & lldb::eTypeClassStruct)
result |= (uint16_t)CompilerContextKind::Struct;
CompilerContextKind result = {};
if (type_class & (lldb::eTypeClassClass | lldb::eTypeClassStruct))
result |= CompilerContextKind::ClassOrStruct;
if (type_class & lldb::eTypeClassUnion)
result |= (uint16_t)CompilerContextKind::Union;
result |= CompilerContextKind::Union;
if (type_class & lldb::eTypeClassEnumeration)
result |= (uint16_t)CompilerContextKind::Enum;
result |= CompilerContextKind::Enum;
if (type_class & lldb::eTypeClassFunction)
result |= (uint16_t)CompilerContextKind::Function;
result |= CompilerContextKind::Function;
if (type_class & lldb::eTypeClassTypedef)
result |= (uint16_t)CompilerContextKind::Typedef;
return (CompilerContextKind)result;
result |= CompilerContextKind::Typedef;
return result;
}

TypeQuery::TypeQuery(llvm::StringRef name, TypeQueryOptions options)
Expand Down Expand Up @@ -207,11 +205,8 @@ void CompilerContext::Dump(Stream &s) const {
case CompilerContextKind::Namespace:
s << "Namespace";
break;
case CompilerContextKind::Class:
s << "Class";
break;
case CompilerContextKind::Struct:
s << "Structure";
case CompilerContextKind::ClassOrStruct:
s << "ClassOrStruct";
break;
case CompilerContextKind::Union:
s << "Union";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// RUN: %clangxx_host -g -gmodules -fmodules -std=c99 -x c -include-pch %t.pch %s -c -o %t.o
// RUN: %clangxx_host %t.o -o %t.exe
// RUN: lldb-test symbols -dump-clang-ast -find type --language=C99 \
// RUN: -compiler-context 'AnyModule:*,Struct:TypeFromPCH' %t.exe | FileCheck %s
// RUN: -compiler-context 'AnyModule:*,ClassOrStruct:TypeFromPCH' %t.exe | FileCheck %s

anchor_t anchor;

Expand Down
10 changes: 5 additions & 5 deletions lldb/test/Shell/SymbolFile/DWARF/x86/compilercontext.ll
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
; Test finding types by CompilerContext.
; RUN: llc %s -filetype=obj -o %t.o
; RUN: lldb-test symbols %t.o -find=type --language=C99 \
; RUN: -compiler-context="Module:CModule,Module:SubModule,Struct:FromSubmoduleX" \
; RUN: -compiler-context="Module:CModule,Module:SubModule,ClassOrStruct:FromSubmoduleX" \
; RUN: | FileCheck %s --check-prefix=NORESULTS
; RUN: lldb-test symbols %t.o -find=type --language=C++ \
; RUN: -compiler-context="Module:CModule,Module:SubModule,Struct:FromSubmodule" \
; RUN: -compiler-context="Module:CModule,Module:SubModule,ClassOrStruct:FromSubmodule" \
; RUN: | FileCheck %s --check-prefix=NORESULTS
; RUN: lldb-test symbols %t.o -find=type --language=C99 \
; RUN: -compiler-context="Module:CModule,Module:SubModule,Struct:FromSubmodule" \
; RUN: -compiler-context="Module:CModule,Module:SubModule,ClassOrStruct:FromSubmodule" \
; RUN: | FileCheck %s
; RUN: lldb-test symbols %t.o -find=type --language=C99 \
; RUN: -compiler-context="Module:CModule,AnyModule:*,Struct:FromSubmodule" \
; RUN: -compiler-context="Module:CModule,AnyModule:*,ClassOrStruct:FromSubmodule" \
; RUN: | FileCheck %s
; RUN: lldb-test symbols %t.o -find=type --language=C99 \
; RUN: -compiler-context="AnyModule:*,Struct:FromSubmodule" \
; RUN: -compiler-context="AnyModule:*,ClassOrStruct:FromSubmodule" \
; RUN: | FileCheck %s
; RUN: lldb-test symbols %t.o -find=type --language=C99 \
; RUN: -compiler-context="Module:CModule,Module:SubModule,AnyType:FromSubmodule" \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
// RUN: FileCheck --check-prefix=FULL-MANGLED-METHOD %s
// RUN: lldb-test symbols --name=foo --context=context --find=function --function-flags=base %t | \
// RUN: FileCheck --check-prefix=CONTEXT %s
// RUN: lldb-test symbols --compiler-context=Struct:sbar,Function:foo -language=c++ -find=function -function-flags=method %t | \
// RUN: lldb-test symbols --compiler-context=ClassOrStruct:sbar,Function:foo -language=c++ -find=function -function-flags=method %t | \
// RUN: FileCheck --check-prefix=COMPILER-CONTEXT %s
// RUN: lldb-test symbols --name=not_there --find=function %t | \
// RUN: FileCheck --check-prefix=EMPTY %s
Expand Down
4 changes: 2 additions & 2 deletions lldb/test/Shell/SymbolFile/DWARF/x86/module-ownership.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

TopLevelStruct s1;
// RUN: lldb-test symbols -dump-clang-ast -find type --language=ObjC++ \
// RUN: -compiler-context 'Module:A,Struct:TopLevelStruct' %t.o \
// RUN: -compiler-context 'Module:A,ClassOrStruct:TopLevelStruct' %t.o \
// RUN: | FileCheck %s --check-prefix=CHECK-TOPLEVELSTRUCT
// CHECK-TOPLEVELSTRUCT: CXXRecordDecl {{.*}} imported in A struct TopLevelStruct
// CHECK-TOPLEVELSTRUCT: -FieldDecl {{.*}} in A a 'int'
Expand Down Expand Up @@ -45,7 +45,7 @@ @implementation SomeClass {

SomeClass *obj1;
// RUN: lldb-test symbols -dump-clang-ast -find type --language=ObjC++ \
// RUN: -compiler-context 'Module:A,Struct:SomeClass' %t.o \
// RUN: -compiler-context 'Module:A,ClassOrStruct:SomeClass' %t.o \
// RUN: | FileCheck %s --check-prefix=CHECK-OBJC
// CHECK-OBJC: ObjCInterfaceDecl {{.*}} imported in A <undeserialized declarations> SomeClass
// CHECK-OBJC-NEXT: |-ObjCIvarDecl
Expand Down
3 changes: 1 addition & 2 deletions lldb/tools/lldb-test/lldb-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,7 @@ llvm::SmallVector<CompilerContext, 4> parseCompilerContext() {
.Case("TranslationUnit", CompilerContextKind::TranslationUnit)
.Case("Module", CompilerContextKind::Module)
.Case("Namespace", CompilerContextKind::Namespace)
.Case("Class", CompilerContextKind::Class)
.Case("Struct", CompilerContextKind::Struct)
.Case("ClassOrStruct", CompilerContextKind::ClassOrStruct)
.Case("Union", CompilerContextKind::Union)
.Case("Function", CompilerContextKind::Function)
.Case("Variable", CompilerContextKind::Variable)
Expand Down
30 changes: 12 additions & 18 deletions lldb/unittests/Symbol/TestType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,26 @@ TEST(Type, GetTypeScopeAndBasename) {
}

TEST(Type, CompilerContextPattern) {
std::vector<CompilerContext> mms = {
{CompilerContextKind::Module, ConstString("A")},
{CompilerContextKind::Module, ConstString("B")},
{CompilerContextKind::Struct, ConstString("S")}};
EXPECT_TRUE(contextMatches(mms, mms));
std::vector<CompilerContext> mmc = {
{CompilerContextKind::Module, ConstString("A")},
{CompilerContextKind::Module, ConstString("B")},
{CompilerContextKind::Class, ConstString("S")}};
EXPECT_FALSE(contextMatches(mms, mmc));
std::vector<CompilerContext> ms = {
{CompilerContextKind::ClassOrStruct, ConstString("S")}};
std::vector<CompilerContext> mc = {
{CompilerContextKind::Module, ConstString("A")},
{CompilerContextKind::Struct, ConstString("S")}};
std::vector<CompilerContext> mas = {
{CompilerContextKind::ClassOrStruct, ConstString("S")}};
std::vector<CompilerContext> mac = {
{CompilerContextKind::Module, ConstString("A")},
{CompilerContextKind::AnyModule, ConstString("*")},
{CompilerContextKind::Struct, ConstString("S")}};
EXPECT_TRUE(contextMatches(mms, mas));
EXPECT_TRUE(contextMatches(ms, mas));
EXPECT_FALSE(contextMatches(mas, ms));
std::vector<CompilerContext> mmms = {
{CompilerContextKind::ClassOrStruct, ConstString("S")}};
EXPECT_TRUE(contextMatches(mmc, mac));
EXPECT_TRUE(contextMatches(mc, mac));
EXPECT_FALSE(contextMatches(mac, mc));
std::vector<CompilerContext> mmmc = {
{CompilerContextKind::Module, ConstString("A")},
{CompilerContextKind::Module, ConstString("B")},
{CompilerContextKind::Module, ConstString("C")},
{CompilerContextKind::Struct, ConstString("S")}};
EXPECT_TRUE(contextMatches(mmms, mas));
{CompilerContextKind::ClassOrStruct, ConstString("S")}};
EXPECT_TRUE(contextMatches(mmmc, mac));
std::vector<CompilerContext> mme = {
{CompilerContextKind::Module, ConstString("A")},
{CompilerContextKind::Module, ConstString("B")},
Expand All @@ -83,7 +77,7 @@ TEST(Type, CompilerContextPattern) {
{CompilerContextKind::Module, ConstString("B")},
{CompilerContextKind::AnyType, ConstString("S")}};
EXPECT_TRUE(contextMatches(mme, mma));
EXPECT_TRUE(contextMatches(mms, mma));
EXPECT_TRUE(contextMatches(mmc, mma));
std::vector<CompilerContext> mme2 = {
{CompilerContextKind::Module, ConstString("A")},
{CompilerContextKind::Module, ConstString("B")},
Expand Down
6 changes: 4 additions & 2 deletions lldb/unittests/SymbolFile/DWARF/DWARFDIETest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ TEST(DWARFDIETest, GetContext) {
return CompilerContext(CompilerContextKind::Namespace, ConstString(name));
};
auto make_struct = [](llvm::StringRef name) {
return CompilerContext(CompilerContextKind::Struct, ConstString(name));
return CompilerContext(CompilerContextKind::ClassOrStruct,
ConstString(name));
};
DWARFDIE struct_die = unit->DIE().GetFirstChild().GetFirstChild();
ASSERT_TRUE(struct_die);
Expand Down Expand Up @@ -356,7 +357,8 @@ TEST(DWARFDIETest, GetContextInFunction) {
return CompilerContext(CompilerContextKind::Namespace, ConstString(name));
};
auto make_struct = [](llvm::StringRef name) {
return CompilerContext(CompilerContextKind::Struct, ConstString(name));
return CompilerContext(CompilerContextKind::ClassOrStruct,
ConstString(name));
};
// Grab the "a::struct_t" type from the "a" namespace
DWARFDIE a_struct_die = unit->DIE().GetFirstChild().GetFirstChild();
Expand Down

0 comments on commit 599ca71

Please sign in to comment.