Skip to content

Commit f4a1bd3

Browse files
ttreyerJakeHillion
authored andcommitted
Remove Primitive::Kind::Incomplete
1 parent 3065dd1 commit f4a1bd3

11 files changed

+51
-38
lines changed

oi/type_graph/DrgnParser.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,17 @@ Type& DrgnParser::enumerateType(struct drgn_type* type) {
123123
std::to_string(kind)};
124124
}
125125
} catch (const DrgnParserError& e) {
126+
depth_--;
126127
if (isTypeIncomplete) {
127-
t = &makeType<Primitive>(type, Primitive::Kind::Incomplete);
128+
const char* typeName = "<incomplete>";
129+
if (drgn_type_has_name(type)) {
130+
typeName = drgn_type_name(type);
131+
} else if (drgn_type_has_tag(type)) {
132+
typeName = drgn_type_tag(type);
133+
}
134+
135+
return makeType<Incomplete>(nullptr, typeName);
128136
} else {
129-
depth_--;
130137
throw e;
131138
}
132139
}

oi/type_graph/EnforceCompatibility.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,6 @@ void EnforceCompatibility::visit(Class& c) {
8383
// the pointer's address in this case.
8484
return true;
8585
}
86-
87-
if (auto* primitive = dynamic_cast<Primitive*>(&ptr->pointeeType())) {
88-
if (primitive->kind() == Primitive::Kind::Incomplete) {
89-
// This is a pointer to an incomplete type. CodeGen v1 does not record
90-
// the pointer's address in this case.
91-
return true;
92-
}
93-
}
9486
}
9587

9688
return false;

oi/type_graph/Flattener.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ void flattenParent(const Parent& parent,
5656
// Create a new member to represent this parent container
5757
flattenedMembers.emplace_back(*parentContainer, Flattener::ParentPrefix,
5858
parent.bitOffset);
59-
} else if (auto* parentPrimitive = dynamic_cast<Primitive*>(&parentType);
60-
parentPrimitive &&
61-
parentPrimitive->kind() == Primitive::Kind::Incomplete) {
59+
} else if (auto* parentPrimitive = dynamic_cast<Incomplete*>(&parentType)) {
6260
// Bad DWARF can lead to us seeing incomplete parent types. Just ignore
6361
// these as there is nothing we can do to recover the missing info.
6462
} else {

oi/type_graph/Printer.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@ void Printer::print(const Type& type) {
3838

3939
void Printer::visit(const Incomplete& i) {
4040
prefix();
41-
out_ << "Incomplete:" << std::endl;
42-
print(i.underlyingType());
41+
out_ << "Incomplete";
42+
if (auto underlyingType = i.underlyingType()) {
43+
out_ << std::endl;
44+
print(underlyingType.value().get());
45+
} else {
46+
out_ << ": [" << i.inputName() << "]" << std::endl;
47+
}
4348
}
4449

4550
void Printer::visit(const Class& c) {
@@ -97,10 +102,7 @@ void Printer::visit(const Container& c) {
97102

98103
void Printer::visit(const Primitive& p) {
99104
prefix();
100-
out_ << "Primitive: " << p.name();
101-
if (p.kind() == Primitive::Kind::Incomplete)
102-
out_ << " (incomplete)";
103-
out_ << std::endl;
105+
out_ << "Primitive: " << p.name() << std::endl;
104106
}
105107

106108
void Printer::visit(const Enum& e) {

oi/type_graph/TypeGraph.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ Primitive& TypeGraph::makeType<Primitive>(Primitive::Kind kind) {
6565
case Primitive::Kind::Void:
6666
static Primitive pVoid{kind};
6767
return pVoid;
68-
case Primitive::Kind::Incomplete:
69-
static Primitive pIncomplete{kind};
70-
return pIncomplete;
7168
}
7269
}
7370

oi/type_graph/Types.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ std::string Primitive::getName(Kind kind) {
6262
case Kind::StubbedPointer:
6363
return "StubbedPointer";
6464
case Kind::Void:
65-
case Kind::Incomplete:
6665
return "void";
6766
}
6867
}
@@ -105,7 +104,6 @@ std::size_t Primitive::size() const {
105104
case Kind::StubbedPointer:
106105
return sizeof(uintptr_t);
107106
case Kind::Void:
108-
case Kind::Incomplete:
109107
return 0;
110108
}
111109
}

oi/type_graph/Types.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <optional>
3535
#include <string>
3636
#include <string_view>
37+
#include <variant>
3738
#include <vector>
3839

3940
#include "oi/ContainerInfo.h"
@@ -195,6 +196,10 @@ class Incomplete : public Type {
195196
Incomplete(Type& underlyingType) : underlyingType_(underlyingType) {
196197
}
197198

199+
Incomplete(std::string underlyingTypeName)
200+
: underlyingType_(std::move(underlyingTypeName)) {
201+
}
202+
198203
static inline constexpr bool has_node_id = false;
199204

200205
DECLARE_ACCEPT
@@ -204,7 +209,13 @@ class Incomplete : public Type {
204209
}
205210

206211
std::string_view inputName() const override {
207-
return underlyingType_.inputName();
212+
if (std::holds_alternative<std::string>(underlyingType_)) {
213+
return std::get<std::string>(underlyingType_);
214+
}
215+
216+
return std::get<std::reference_wrapper<Type>>(underlyingType_)
217+
.get()
218+
.inputName();
208219
}
209220

210221
size_t size() const override {
@@ -219,12 +230,16 @@ class Incomplete : public Type {
219230
return -1;
220231
}
221232

222-
Type& underlyingType() const {
223-
return underlyingType_;
233+
std::optional<std::reference_wrapper<Type>> underlyingType() const {
234+
if (std::holds_alternative<std::string>(underlyingType_)) {
235+
return std::nullopt;
236+
}
237+
238+
return std::get<std::reference_wrapper<Type>>(underlyingType_);
224239
}
225240

226241
private:
227-
Type& underlyingType_;
242+
std::variant<std::string, std::reference_wrapper<Type>> underlyingType_;
228243
static const std::string kName;
229244
};
230245

@@ -551,8 +566,6 @@ class Primitive : public Type {
551566

552567
StubbedPointer,
553568
Void,
554-
Incomplete, // Behaves the same as Void, but alerts us that the type was
555-
// stubbed out due to incomplete DWARF
556569
};
557570

558571
explicit Primitive(Kind kind) : kind_(kind), name_(getName(kind)) {

test/TypeGraphParser.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ Primitive::Kind getKind(std::string_view kindStr) {
4949
return Primitive::Kind::StubbedPointer;
5050
if (kindStr == "void")
5151
return Primitive::Kind::Void;
52-
if (kindStr == "void (incomplete)")
53-
return Primitive::Kind::Incomplete;
5452
throw TypeGraphParserError{"Invalid Primitive::Kind: " +
5553
std::string{kindStr}};
5654
}
@@ -212,8 +210,16 @@ Type& TypeGraphParser::parseType(std::string_view& input, size_t rootIndent) {
212210

213211
type = &it->second.get();
214212
} else if (nodeTypeName == "Incomplete") {
215-
auto& underlyingType = parseType(input, indent + 2);
216-
type = &typeGraph_.makeType<Incomplete>(underlyingType);
213+
if (line[nodeEndPos] == ':') {
214+
auto nameStartPos = line.find('[', nodeEndPos) + 1;
215+
auto nameEndPos = line.find(']', nameStartPos);
216+
auto underlyingTypeName =
217+
line.substr(nameStartPos, nameEndPos - nameStartPos);
218+
type = &typeGraph_.makeType<Incomplete>(std::string(underlyingTypeName));
219+
} else {
220+
auto& underlyingType = parseType(input, indent + 2);
221+
type = &typeGraph_.makeType<Incomplete>(underlyingType);
222+
}
217223
} else if (nodeTypeName == "Class" || nodeTypeName == "Struct" ||
218224
nodeTypeName == "Union") {
219225
// Format: "Class: MyClass (size: 12)"

test/test_drgn_parser.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,7 @@ TEST_F(DrgnParserTest, PointerNoFollow) {
367367
TEST_F(DrgnParserTest, PointerIncomplete) {
368368
test("oid_test_case_pointers_incomplete_raw", R"(
369369
[0] Pointer
370-
Incomplete:
371-
Primitive: void (incomplete)
370+
Incomplete: [IncompleteType]
372371
)");
373372
}
374373

test/test_enforce_compatibility.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ TEST(EnforceCompatibilityTest, VoidPointer) {
3636
[0] Class: MyClass (size: 8)
3737
Member: p (offset: 0)
3838
[1] Pointer
39-
Primitive: void (incomplete)
39+
Incomplete
40+
Primitive: void
4041
)",
4142
R"(
4243
[0] Class: MyClass (size: 8)

0 commit comments

Comments
 (0)