Просмотр исходного кода

Print out malformed generated code in a useful way.

While working on the code generator, I occasionally cause it to make
malformed Go code. It can often be very hard to trace the problem,
since the only error message that is printed is the compiler-style
error from go/parser; without the corresponding source code it is
usually a useless message. So, if we generate bad source code, print
out that source code at the same time.
David Symonds 10 лет назад
Родитель
Сommit
1e35a3a79e
1 измененных файлов с 11 добавлено и 2 удалено
  1. 11 2
      protoc-gen-go/generator/generator.go

+ 11 - 2
protoc-gen-go/generator/generator.go

@@ -37,6 +37,7 @@
 package generator
 
 import (
+	"bufio"
 	"bytes"
 	"fmt"
 	"go/parser"
@@ -1064,10 +1065,18 @@ func (g *Generator) generate(file *FileDescriptor) {
 
 	// Reformat generated code.
 	fset := token.NewFileSet()
+	raw := g.Bytes()
 	ast, err := parser.ParseFile(fset, "", g, parser.ParseComments)
 	if err != nil {
-		g.Fail("bad Go source code was generated:", err.Error())
-		return
+		// Print out the bad code with line numbers.
+		// This should never happen in practice, but it can while changing generated code,
+		// so consider this a debugging aid.
+		var src bytes.Buffer
+		s := bufio.NewScanner(bytes.NewReader(raw))
+		for line := 1; s.Scan(); line++ {
+			fmt.Fprintf(&src, "%5d\t%s\n", line, s.Bytes())
+		}
+		g.Fail("bad Go source code was generated:", err.Error(), "\n"+src.String())
 	}
 	g.Reset()
 	err = (&printer.Config{Mode: printer.TabIndent | printer.UseSpaces, Tabwidth: 8}).Fprint(g, fset, ast)