MCPcopy
hub / github.com/golang/protobuf / generate

Method generate

protoc-gen-go/generator/generator.go:1106–1202  ·  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

1104// Fill the response protocol buffer with the generated output for all the files we're
1105// supposed to generate.
1106func (g *Generator) generate(file *FileDescriptor) {
1107 g.file = file
1108 g.usedPackages = make(map[GoImportPath]bool)
1109 g.packageNames = make(map[GoImportPath]GoPackageName)
1110 g.usedPackageNames = make(map[GoPackageName]bool)
1111 g.addedImports = make(map[GoImportPath]bool)
1112 for name := range globalPackageNames {
1113 g.usedPackageNames[name] = true
1114 }
1115
1116 g.P("// This is a compile-time assertion to ensure that this generated file")
1117 g.P("// is compatible with the proto package it is being compiled against.")
1118 g.P("// A compilation error at this line likely means your copy of the")
1119 g.P("// proto package needs to be updated.")
1120 g.P("const _ = ", g.Pkg["proto"], ".ProtoPackageIsVersion", generatedCodeVersion, " // please upgrade the proto package")
1121 g.P()
1122
1123 for _, td := range g.file.imp {
1124 g.generateImported(td)
1125 }
1126 for _, enum := range g.file.enum {
1127 g.generateEnum(enum)
1128 }
1129 for _, desc := range g.file.desc {
1130 // Don't generate virtual messages for maps.
1131 if desc.GetOptions().GetMapEntry() {
1132 continue
1133 }
1134 g.generateMessage(desc)
1135 }
1136 for _, ext := range g.file.ext {
1137 g.generateExtension(ext)
1138 }
1139 g.generateInitFunction()
1140 g.generateFileDescriptor(file)
1141
1142 // Run the plugins before the imports so we know which imports are necessary.
1143 g.runPlugins(file)
1144
1145 // Generate header and imports last, though they appear first in the output.
1146 rem := g.Buffer
1147 remAnno := g.annotations
1148 g.Buffer = new(bytes.Buffer)
1149 g.annotations = nil
1150 g.generateHeader()
1151 g.generateImports()
1152 if !g.writeOutput {
1153 return
1154 }
1155 // Adjust the offsets for annotations displaced by the header and imports.
1156 for _, anno := range remAnno {
1157 *anno.Begin += int32(g.Len())
1158 *anno.End += int32(g.Len())
1159 g.annotations = append(g.annotations, anno)
1160 }
1161 g.Write(rem.Bytes())
1162
1163 // Reformat generated code and patch annotation locations.

Callers 1

GenerateAllFilesMethod · 0.95

Calls 15

PMethod · 0.95
generateImportedMethod · 0.95
generateEnumMethod · 0.95
generateMessageMethod · 0.95
generateExtensionMethod · 0.95
generateInitFunctionMethod · 0.95
runPluginsMethod · 0.95
generateHeaderMethod · 0.95
generateImportsMethod · 0.95
FailMethod · 0.95
ComputeFunction · 0.92

Tested by

no test coverage detected