From 58bd906d601cde032f83f00e29676146a2f5104a Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Thu, 24 Oct 2024 14:33:36 +0200 Subject: tools/syz-declextract: make fixed header more readable Combine all fixed header parts in a single raw string literal. --- tools/syz-declextract/run.go | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) (limited to 'tools') diff --git a/tools/syz-declextract/run.go b/tools/syz-declextract/run.go index a680ea12c..40d6497bb 100644 --- a/tools/syz-declextract/run.go +++ b/tools/syz-declextract/run.go @@ -157,6 +157,16 @@ func getMinimalOutput(out string, syscallNames map[string][]string) []string { } func makeOutput(nodes []ast.Node) []byte { + header := ` +# Code generated by syz-declextract. DO NOT EDIT. +include +include +_ = __NR_mmap2 +` + eh := ast.LoggingHandler + desc := ast.Parse([]byte(header), "", eh) + nodes = append(nodes, desc.Nodes...) + slices.SortFunc(nodes, func(a, b ast.Node) int { return strings.Compare(ast.SerializeNode(a), ast.SerializeNode(b)) }) @@ -179,7 +189,7 @@ func makeOutput(nodes []ast.Node) []byte { continue } structs = append(structs, node) - case *ast.Include, *ast.TypeDef, *ast.Resource, *ast.IntFlags, *ast.NewLine: + case *ast.Include, *ast.TypeDef, *ast.Resource, *ast.IntFlags, *ast.NewLine, *ast.Comment: continue default: _, typ, _ := node.Info() @@ -203,13 +213,6 @@ func makeOutput(nodes []ast.Node) []byte { return a.Name.Name == b.Name.Name }) - autoGeneratedNotice := "# Code generated by syz-declextract. DO NOT EDIT.\n" - commonKernelHeaders := "include \ninclude " - var netlinkNames []string - mmap2 := "_ = __NR_mmap2\n" - eh := ast.LoggingHandler - desc := ast.Parse([]byte(autoGeneratedNotice+commonKernelHeaders), "", eh) - desc.Nodes = append(desc.Nodes, nodes...) usedNetlink := make(map[string]bool) for _, node := range syscalls { if node.CallName == sendmsg && len(node.Args[1].Type.Args) == 2 && len(node.Args[1].Type.Args[1].Args) > 1 { @@ -222,11 +225,11 @@ func makeOutput(nodes []ast.Node) []byte { continue } } - desc.Nodes = append(desc.Nodes, node) + nodes = append(nodes, node) } - desc.Nodes = append(desc.Nodes, ast.Parse([]byte(mmap2), "", eh).Nodes...) + var netlinkNames []string for _, node := range structs { - desc.Nodes = append(desc.Nodes, node) + nodes = append(nodes, node) name := node.Name.Name if !usedNetlink[name] && !strings.HasSuffix(name, "$auto_record") { netlinkNames = append(netlinkNames, name) @@ -246,11 +249,11 @@ auto_union [ if netlinkUnionParsed == nil { tool.Failf("parsing error") } - desc.Nodes = append(desc.Nodes, netlinkUnionParsed.Nodes...) + nodes = append(nodes, netlinkUnionParsed.Nodes...) - // New lines are added in the parsing step. This is why we need to Format (serialize the description), Parse, then - // Format again. - return ast.Format(ast.Parse(ast.Format(desc), "", eh)) + // New lines are added in the parsing step. This is why we need to Format (serialize the description), + // Parse, then Format again. + return ast.Format(ast.Parse(ast.Format(&ast.Description{nodes}), "", eh)) } func worker(outputs chan output, files chan string, binary, compilationDatabase, format string) { -- cgit mrf-deployment