MCPcopy
hub / github.com/hwholiday/learning_tools / generate

Method generate

grpc/protoc-gen-hw/generator/generator.go:1055–1112  ·  view source on GitHub ↗

Fill the response protocol buffer with the generated output for all the files we're supposed to generate.

(file *FileDescriptor)

Source from the content-addressed store, hash-verified

1053// Fill the response protocol buffer with the generated output for all the files we're
1054// supposed to generate.
1055func (g *Generator) generate(file *FileDescriptor) {
1056 g.file = file
1057 g.usedPackages = make(map[GoImportPath]bool)
1058 g.packageNames = make(map[GoImportPath]GoPackageName)
1059 g.usedPackageNames = make(map[GoPackageName]bool)
1060 g.addedImports = make(map[GoImportPath]bool)
1061 for name := range globalPackageNames {
1062 g.usedPackageNames[name] = true
1063 }
1064
1065 g.P("// This is a compile-time assertion to ensure that this generated file")
1066 g.P("// is compatible with the proto package it is being compiled against.")
1067 g.P("// A compilation error at this line likely means your copy of the")
1068 g.P("// proto package needs to be updated.")
1069 g.P("const _ = ", g.Pkg["proto"], ".ProtoPackageIsVersion", generatedCodeVersion, " // please upgrade the proto package")
1070 g.P()
1071
1072 for _, td := range g.file.imp {
1073 g.generateImported(td)
1074 }
1075
1076 g.generateInitFunction()
1077
1078 // Run the plugins before the imports so we know which imports are necessary.
1079 g.runPlugins(file)
1080
1081 // Generate header and imports last, though they appear first in the output.
1082 rem := g.Buffer
1083 g.Buffer = new(bytes.Buffer)
1084 g.generateHeader()
1085 g.generateImports()
1086 if !g.writeOutput {
1087 return
1088 }
1089 g.Write(rem.Bytes())
1090
1091 // Reformat generated code and patch annotation locations.
1092 fset := token.NewFileSet()
1093 original := g.Bytes()
1094 fileAST, err := parser.ParseFile(fset, "", original, parser.ParseComments)
1095 if err != nil {
1096 // Print out the bad code with line numbers.
1097 // This should never happen in practice, but it can while changing generated code,
1098 // so consider this a debugging aid.
1099 var src bytes.Buffer
1100 s := bufio.NewScanner(bytes.NewReader(original))
1101 for line := 1; s.Scan(); line++ {
1102 fmt.Fprintf(&src, "%5d\t%s\n", line, s.Bytes())
1103 }
1104 g.Fail("bad Go source code was generated:", err.Error(), "\n"+src.String())
1105 }
1106 ast.SortImports(fset, fileAST)
1107 g.Reset()
1108 err = (&printer.Config{Mode: printer.TabIndent | printer.UseSpaces, Tabwidth: 8}).Fprint(g, fset, fileAST)
1109 if err != nil {
1110 g.Fail("generated Go source code could not be reformatted:", err.Error())
1111 }
1112}

Callers 1

GenerateAllFilesMethod · 0.95

Calls 12

PMethod · 0.95
generateImportedMethod · 0.95
generateInitFunctionMethod · 0.95
runPluginsMethod · 0.95
generateHeaderMethod · 0.95
generateImportsMethod · 0.95
FailMethod · 0.95
WriteMethod · 0.80
BytesMethod · 0.80
ErrorMethod · 0.65
StringMethod · 0.45
ResetMethod · 0.45

Tested by

no test coverage detected