~emersion/gyosu

abd399728a05455a281ad2a412f2b799fce75e9e — Simon Ser 7 days ago bee912f master
Migrate to modernc.org/cc/v4
5 files changed, 38 insertions(+), 76 deletions(-)

M cc.go
M comment.go
M go.mod
M go.sum
M main.go
M cc.go => cc.go +0 -24
@@ 5,30 5,6 @@ import (
	"strings"
)

const builtin = `
#define __builtin_va_list void *
#define __asm(x)
#define __inline
#define __inline__
#define __signed
#define __signed__
#define __const const
#define __extension__
#define __attribute__(x)
#define __attribute(x)
#define __restrict
#define __volatile__
#define __builtin_inff() (0)
#define __builtin_infl() (0)
#define __builtin_inf() (0)
#define __builtin_fabsf(x) (0)
#define __builtin_fabsl(x) (0)
#define __builtin_fabs(x) (0)
#define __INTRINSIC_PROLOG(name)
#define __typeof
#define __typeof__
`

func generateDefines(D []string) string {
	var sb strings.Builder
	for _, def := range D {

M comment.go => comment.go +2 -2
@@ 4,7 4,7 @@ import (
	"strings"
	"unicode"

	"modernc.org/cc/v3"
	"modernc.org/cc/v4"
)

// parseParagraph parses a raw paragraph text into inline elements. The


@@ 84,7 84,7 @@ func isWordBoundary(ch rune) bool {

func extractDocComment(tok cc.Token) []paragraph {
	// Pick the last comment
	rest := tok.Sep.String()
	rest := string(tok.Sep())
	var comment string
	for {
		nextComment, nextRest := cutComment(rest)

M go.mod => go.mod +2 -2
@@ 4,13 4,13 @@ go 1.18

require (
	git.sr.ht/~sircmpwn/getopt v1.0.0
	modernc.org/cc/v3 v3.38.1
	modernc.org/cc/v4 v4.1.1-0.20221121154243-ea095dba0b2e
)

require (
	github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
	lukechampine.com/uint128 v1.2.0 // indirect
	modernc.org/mathutil v1.5.0 // indirect
	modernc.org/opt v0.1.3 // indirect
	modernc.org/strutil v1.1.3 // indirect
	modernc.org/token v1.0.1 // indirect
)

M go.sum => go.sum +6 -9
@@ 3,9 3,7 @@ git.sr.ht/~sircmpwn/getopt v1.0.0/go.mod h1:wMEGFFFNuPos7vHmWXfszqImLppbc0wEhh6J
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/google/go-cmp v0.5.3 h1:x95R7cp+rSeeqAMI2knLtQ0DKlaBhv2NrtrOvafPHRo=
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk=


@@ 13,14 11,13 @@ github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qq
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
lukechampine.com/uint128 v1.1.1/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
lukechampine.com/uint128 v1.2.0 h1:mBi/5l91vocEN8otkC5bDLhi2KdCticRiwbdB0O+rjI=
lukechampine.com/uint128 v1.2.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
modernc.org/cc/v3 v3.38.1 h1:Yu2IiiRpustRFUgMDZKwVn2RvyJzpfYSOw7zHeKtSi4=
modernc.org/cc/v3 v3.38.1/go.mod h1:vtL+3mdHx/wcj3iEGz84rQa8vEqR6XM84v5Lcvfph20=
modernc.org/cc/v4 v4.1.1-0.20221121154243-ea095dba0b2e h1:XL7f4bRyhsx5aOWfyB45FL0NlMBqyfkSerQXb+Tb4ZY=
modernc.org/cc/v4 v4.1.1-0.20221121154243-ea095dba0b2e/go.mod h1:1lT0MyTUmmbFkcZxfz38h2hIP9KKug5HaYN4htGfMfw=
modernc.org/ccorpus2 v1.2.0 h1:I+dskokQYHl+wL+Y2q9dS9B1o7JbrxBTyYvESaqmBgQ=
modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ=
modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4=
modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0=
modernc.org/strutil v1.1.3 h1:fNMm+oJklMGYfU9Ylcywl0CO5O6nTfaowNsh2wpPjzY=
modernc.org/strutil v1.1.3/go.mod h1:MEHNA7PdEnEwLvspRMtWTNnp2nnyvMfkimT1NKNAGbw=
modernc.org/token v1.0.1 h1:A3qvTqOwexpfZZeyI0FeGPDlSWX5pjZu9hF4lU+EKWg=

M main.go => main.go +28 -39
@@ 8,17 8,18 @@ import (
	"os"
	"path"
	"path/filepath"
	"runtime"
	"sort"
	"strings"

	"git.sr.ht/~sircmpwn/getopt"
	"modernc.org/cc/v3"
	"modernc.org/cc/v4"
)

func enumSpecifierPrototype(enumSpec *cc.EnumSpecifier) prototype {
	var proto prototype
	if enumSpec.Token2.Value != 0 {
		proto.Symbol(declEnum, enumSpec.Token2.Value.String())
	if enumSpec.Token2.Ch != 0 {
		proto.Symbol(declEnum, enumSpec.Token2.SrcStr())
	} else {
		proto.Keyword("enum")
	}


@@ 26,7 27,7 @@ func enumSpecifierPrototype(enumSpec *cc.EnumSpecifier) prototype {
		proto.Raw(" {")
		for el := enumSpec.EnumeratorList; el != nil; el = el.EnumeratorList {
			proto.Add(tokenSepPrototype(el.Enumerator.Token, false)...)
			proto.Raw("%v,", el.Enumerator.Token)
			proto.Raw("%v,", el.Enumerator.Token.SrcStr())
		}
		proto.Add(tokenSepPrototype(enumSpec.Token5, true)...)
		proto.Raw("}")


@@ 35,11 36,11 @@ func enumSpecifierPrototype(enumSpec *cc.EnumSpecifier) prototype {
}

func structOrUnionSpecifierPrototype(structOrUnionSpec *cc.StructOrUnionSpecifier) prototype {
	kind := structOrUnionSpec.StructOrUnion.Token.Value.String()
	kind := structOrUnionSpec.StructOrUnion.Token.SrcStr()

	var proto prototype
	if structOrUnionSpec.Token.Value != 0 {
		proto.Symbol(declKind(kind), structOrUnionSpec.Token.Value.String())
	if structOrUnionSpec.Token.Ch != 0 {
		proto.Symbol(declKind(kind), structOrUnionSpec.Token.SrcStr())
	} else {
		proto.Keyword(kind)
	}


@@ 65,7 66,7 @@ func structDeclarationPrototype(structDecl *cc.StructDeclaration) prototype {
		case cc.SpecifierQualifierListTypeSpec:
			proto.Add(typeSpecifierPrototype(sql.TypeSpecifier)...).Raw(nbsp)
		case cc.SpecifierQualifierListTypeQual:
			proto.Keyword(sql.TypeQualifier.Token.Value.String()).Raw(nbsp)
			proto.Keyword(sql.TypeQualifier.Token.SrcStr()).Raw(nbsp)
		default:
			panic(fmt.Sprintf("TODO: %v", sql.Case))
		}


@@ 95,7 96,7 @@ func directDeclaratorPrototype(directDeclarator *cc.DirectDeclarator) prototype 
	var proto prototype
	switch directDeclarator.Case {
	case cc.DirectDeclaratorIdent:
		return *proto.Raw(directDeclarator.Token.Value.String())
		return *proto.Raw(directDeclarator.Token.SrcStr())
	case cc.DirectDeclaratorArr, cc.DirectDeclaratorStaticArr:
		// TODO: TypeQualifiers, AssignmentExpression
		return *proto.Add(directDeclaratorPrototype(directDeclarator.DirectDeclarator)...).Raw("[]")


@@ 149,11 150,11 @@ func declarationSpecifiersPrototype(declSpec *cc.DeclarationSpecifiers) prototyp
		}
		switch declSpec.Case {
		case cc.DeclarationSpecifiersStorage:
			proto.Keyword(declSpec.StorageClassSpecifier.Token.Value.String())
			proto.Keyword(declSpec.StorageClassSpecifier.Token.SrcStr())
		case cc.DeclarationSpecifiersTypeSpec:
			proto.Add(typeSpecifierPrototype(declSpec.TypeSpecifier)...)
		case cc.DeclarationSpecifiersTypeQual:
			proto.Keyword(declSpec.TypeQualifier.Token.Value.String())
			proto.Keyword(declSpec.TypeQualifier.Token.SrcStr())
		default:
			panic(fmt.Sprintf("TODO: %v", declSpec.Case))
		}


@@ 174,7 175,7 @@ func typeSpecifierPrototype(typeSpec *cc.TypeSpecifier) prototype {
	case cc.TypeSpecifierBool:
		return *proto.Keyword("bool")
	default:
		return *proto.Keyword(typeSpec.Token.Value.String())
		return *proto.Keyword(typeSpec.Token.SrcStr())
	}
}



@@ 199,8 200,6 @@ func specifierQualifierListFirstToken(sql *cc.SpecifierQualifierList) cc.Token {
		return sql.TypeQualifier.Token
	case cc.SpecifierQualifierListAlignSpec:
		return sql.AlignmentSpecifier.Token
	case cc.SpecifierQualifierListAttribute:
		return sql.AttributeSpecifier.Token
	default:
		panic(fmt.Sprintf("unknown %v", sql.Case))
	}


@@ 218,15 217,13 @@ func declarationSpecifiersFirstToken(declSpec *cc.DeclarationSpecifiers) cc.Toke
		return declSpec.FunctionSpecifier.Token
	case cc.DeclarationSpecifiersAlignSpec:
		return declSpec.AlignmentSpecifier.Token
	case cc.DeclarationSpecifiersAttribute:
		return declSpec.AttributeSpecifier.Token
	default:
		panic(fmt.Sprintf("unknown %v", declSpec.Case))
	}
}

func tokenSepPrototype(tok cc.Token, last bool) prototype {
	sep := tok.Sep.String()
	sep := string(tok.Sep())
	firstLine, otherLines, _ := strings.Cut(sep, "\n")
	otherLines = "\n" + stripIndent(otherLines)
	if !last {


@@ 374,16 371,20 @@ func main() {
		}
	}

	hostPredefined, hostIncludePaths, hostSysIncludePaths, err := cc.HostConfig("")
	var cflags []string
	for _, v := range I {
		cflags = append(cflags, "-I", v)
	}

	cfg, err := cc.NewConfig(runtime.GOOS, runtime.GOARCH, cflags...)
	if err != nil {
		log.Fatalf("failed to get host config: %v", err)
		log.Fatalf("failed to create new config: %v", err)
	}

	sources := []cc.Source{
		{Name: "<predefined>", Value: hostPredefined},
		{Name: "<builtin>", Value: builtin},
		{Name: "<predefined>", Value: cfg.Predefined},
		{Name: "<builtin>", Value: cc.Builtin},
	}

	if s := generateDefines(D); s != "" {
		sources = append(sources, cc.Source{Name: "<defines>", Value: s})
	}


@@ 437,19 438,7 @@ func main() {
		}
	}

	// Priority order is relative (for #include "..." only), then -I, then the
	// usual places. See:
	// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/c99.html
	includePaths := append([]string{"@"}, I...)
	includePaths = append(includePaths, hostIncludePaths...)
	includePaths = append(includePaths, hostSysIncludePaths...)

	sysIncludePaths := append([]string(nil), I...)
	sysIncludePaths = append(sysIncludePaths, hostSysIncludePaths...)

	cfg := new(cc.Config)
	cfg.PreserveWhiteSpace = true
	ast, err := cc.Parse(cfg, includePaths, sysIncludePaths, sources)
	ast, err := cc.Parse(cfg, sources)
	if err != nil {
		log.Fatalf("parse error: %v", err)
	}


@@ 484,10 473,10 @@ func main() {
				switch typeSpec.Case {
				case cc.TypeSpecifierStructOrUnion:
					structOrUnionSpec := typeSpec.StructOrUnionSpecifier
					kind := structOrUnionSpec.StructOrUnion.Token.Value.String()
					kind := structOrUnionSpec.StructOrUnion.Token.SrcStr()
					decls = append(decls, declData{
						Kind:        declKind(kind),
						Name:        structOrUnionSpec.Token.Value.String(),
						Name:        structOrUnionSpec.Token.SrcStr(),
						Prototype:   proto,
						Description: desc,
						Filename:    origFilename,


@@ 496,7 485,7 @@ func main() {
					enumSpec := typeSpec.EnumSpecifier
					decls = append(decls, declData{
						Kind:        declEnum,
						Name:        enumSpec.Token2.Value.String(),
						Name:        enumSpec.Token2.SrcStr(),
						Prototype:   proto,
						Description: desc,
						Filename:    origFilename,


@@ 518,7 507,7 @@ func main() {
				proto.Add(declSpecsProto...).Raw(" ").Add(declaratorPrototype(idl.InitDeclarator.Declarator)...).Raw(";")
				decls = append(decls, declData{
					Kind:        kind,
					Name:        idl.InitDeclarator.Declarator.Name().String(),
					Name:        idl.InitDeclarator.Declarator.Name(),
					Prototype:   proto,
					Description: desc,
					Filename:    origFilename,