From dc71a93dcf5e4822eab93d30ad9a218b488edab8 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 18 Jun 2017 10:17:09 +1000 Subject: [PATCH] Re-introduce type/bounds checking to "puffs gen". This rolls back some of: f23a808 Remove some redundant cmd/puffs/gen.go code. 3258c1e Remove check.FlagsOnlyTypeCheck. The language-specific code generators, such as puffs-gen-c, have to do type and bounds checking anyway, to generate the right types and function calls. Accordingly, I thought that the type and bounds checking in the language-agnostic cmd/puffs/gen.go was redundant, and removed it. However, puffs-gen-c just works with stdin and stdout, and the comments have been stripped from its input, so it cannot print either the right filename or right line number for any errors. We therefore need to do type and bounds checking in cmd/puffs/gen.go too, in order to produce actionable errors. --- cmd/puffs/gen.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cmd/puffs/gen.go b/cmd/puffs/gen.go index d392d248..a9193026 100644 --- a/cmd/puffs/gen.go +++ b/cmd/puffs/gen.go @@ -14,6 +14,8 @@ import ( "path/filepath" "strings" + "github.com/google/puffs/lang/ast" + "github.com/google/puffs/lang/check" "github.com/google/puffs/lang/parse" "github.com/google/puffs/lang/render" "github.com/google/puffs/lang/token" @@ -71,6 +73,7 @@ func genDir(puffsRoot string, dirname string, filenames []string, langs []string // mtime is newer than all inputs and the puffs-gen-foo command. tm := &token.Map{} + files := []*ast.File(nil) buf := &bytes.Buffer{} for _, filename := range filenames { filename = filepath.Join(puffsRoot, filepath.FromSlash(dirname), filename) @@ -82,15 +85,18 @@ func genDir(puffsRoot string, dirname string, filenames []string, langs []string if err != nil { return err } - // We don't need the AST node to pretty-print, but it's worth rejecting - // syntax errors early. This is just a parse, not a full type check. - if _, err := parse.Parse(tm, filename, tokens); err != nil { + if f, err := parse.Parse(tm, filename, tokens); err != nil { return err + } else { + files = append(files, f) } if err := render.Render(buf, tm, tokens, nil); err != nil { return err } } + if _, err := check.Check(tm, files...); err != nil { + return err + } combinedSrc := buf.Bytes() packageName := path.Base(dirname)