Have driver_ast_parser track if a record is a union type.

PiperOrigin-RevId: 660480745
This commit is contained in:
Anthony Cui
2024-08-07 12:01:30 -07:00
committed by gVisor bot
parent 7e665a50ec
commit b96718ae17
3 changed files with 36 additions and 17 deletions
@@ -199,8 +199,11 @@ struct DriverStructReporter : public MatchFinder::MatchCallback {
record_decl->getLocation().printToString(ctx->getSourceManager());
// getTypeSize returns the size in bits, so we divide by 8 to get bytes.
uint64_t size = ctx->getTypeSize(record_decl->getTypeForDecl()) / 8;
RecordDefinitions[name] =
json::object({{"source", source}, {"fields", fields}, {"size", size}});
bool is_union = record_decl->isUnion();
RecordDefinitions[name] = json::object({{"source", source},
{"fields", fields},
{"size", size},
{"is_union", is_union}});
}
};
@@ -84,8 +84,9 @@ func TestParser(t *testing.T) {
parser.RecordField{Name: "e", Type: "TestStruct::e_t[4]"},
parser.RecordField{Name: "f", Type: "TestUnion"},
},
Size: 44,
Source: "test_struct.cc:25:16",
Size: 44,
IsUnion: false,
Source: "test_struct.cc:25:16",
},
"TestStruct2": parser.RecordDef{
Fields: []parser.RecordField{
@@ -94,24 +95,27 @@ func TestParser(t *testing.T) {
parser.RecordField{Name: "e", Type: "TestStruct::e_t[4]"},
parser.RecordField{Name: "f", Type: "TestUnion"},
},
Size: 44,
Source: "test_struct.cc:25:16",
Size: 44,
IsUnion: false,
Source: "test_struct.cc:25:16",
},
"TestStruct::e_t": parser.RecordDef{
Fields: []parser.RecordField{
parser.RecordField{Name: "c", Type: "OtherInt"},
parser.RecordField{Name: "d", Type: "OtherInt"},
},
Size: 8,
Source: "test_struct.cc:28:3",
Size: 8,
IsUnion: false,
Source: "test_struct.cc:28:3",
},
"TestUnion": parser.RecordDef{
Fields: []parser.RecordField{
parser.RecordField{Name: "u_a", Type: "int"},
parser.RecordField{Name: "u_b", Type: "int"},
},
Size: 4,
Source: "test_struct.cc:20:9",
Size: 4,
IsUnion: true,
Source: "test_struct.cc:20:9",
},
},
Aliases: parser.TypeAliases{
@@ -32,8 +32,8 @@ type InputJSON struct {
// OutputJSON is the format for the output of driver_ast_parser.
type OutputJSON struct {
Records RecordDefs `json:"records"`
Aliases TypeAliases `json:"aliases"`
Records RecordDefs
Aliases TypeAliases
}
// Merge merges the struct definitions from b into this OutputJSON.
@@ -60,14 +60,15 @@ func (s RecordField) String() string {
// RecordDef represents the definition of a record (struct or union).
type RecordDef struct {
Fields []RecordField
Size uint64
Source string
Fields []RecordField
Size uint64
IsUnion bool `json:"is_union"`
Source string
}
// Equals returns true if the two record definitions are equal. We ignore the source of the records.
func (s RecordDef) Equals(other RecordDef) bool {
return s.Size == other.Size && slices.Equal(s.Fields, other.Fields)
return s.IsUnion == other.IsUnion && s.Size == other.Size && slices.Equal(s.Fields, other.Fields)
}
// TypeDef represents the definition of a type.
@@ -88,7 +89,18 @@ func GetRecordDiff(name nvproxy.DriverStructName, a, b RecordDef) string {
fmt.Fprintf(&sb, "--- A: %s\n", a.Source)
fmt.Fprintf(&sb, "+++ B: %s\n", b.Source)
fmt.Fprintf(&sb, "struct %s\n", name)
switch {
case a.IsUnion && !b.IsUnion:
fmt.Fprintf(&sb, "- union %s\n", name)
fmt.Fprintf(&sb, "+ struct %s\n", name)
case !a.IsUnion && b.IsUnion:
fmt.Fprintf(&sb, "- struct %s\n", name)
fmt.Fprintf(&sb, "+ union %s\n", name)
case a.IsUnion && b.IsUnion:
fmt.Fprintf(&sb, "union %s\n", name)
case !a.IsUnion && !b.IsUnion:
fmt.Fprintf(&sb, "struct %s\n", name)
}
if a.Size != b.Size {
fmt.Fprintf(&sb, " size: %d -> %d (bytes)\n", a.Size, b.Size)
}