cpp_styleguide.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. #include <google/protobuf/compiler/code_generator.h>
  2. #include <google/protobuf/compiler/cpp/helpers.h>
  3. #include <google/protobuf/descriptor.h>
  4. #include <google/protobuf/descriptor.pb.h>
  5. #include <google/protobuf/io/printer.h>
  6. #include <google/protobuf/io/zero_copy_stream.h>
  7. #include "cpp_styleguide.h"
  8. #include <util/stream/output.h>
  9. namespace NProtobuf {
  10. namespace NCompiler {
  11. namespace NPlugins {
  12. using namespace google::protobuf;
  13. using namespace google::protobuf::compiler;
  14. using namespace google::protobuf::compiler::cpp;
  15. typedef std::map<TProtoStringType, TProtoStringType> TVariables;
  16. bool GenerateYaStyle(const FileDescriptor* fileDescriptor) {
  17. const auto& extension = fileDescriptor->FindExtensionByName("GenerateYaStyle");
  18. return extension;
  19. }
  20. bool GenerateYaStyle(const FieldDescriptor* descriptor) {
  21. const auto& fileDescriptor = descriptor->file();
  22. return GenerateYaStyle(fileDescriptor);
  23. }
  24. void SetCommonFieldVariables(const FieldDescriptor* descriptor, TVariables* variables) {
  25. const auto& name = descriptor->name();
  26. if (GenerateYaStyle(descriptor))
  27. (*variables)["rname"] = UnderscoresToCamelCase(name, true);
  28. else
  29. (*variables)["rname"] = name;
  30. (*variables)["name"] = FieldName(descriptor);
  31. }
  32. TProtoStringType HeaderFileName(const FileDescriptor* file) {
  33. TProtoStringType basename = compiler::StripProto(file->name());
  34. bool use_proto_h = !!getenv("PROTOC_PLUGINS_LITE_HEADERS");
  35. return use_proto_h ? basename.append(".proto.h") : basename.append(".pb.h");
  36. }
  37. TProtoStringType SourceFileName(const FileDescriptor* file) {
  38. TProtoStringType basename = compiler::StripProto(file->name());
  39. return basename.append(".pb.cc");
  40. }
  41. bool IsLiteRuntimeMessage(const Descriptor* desc) {
  42. return desc->file() != NULL && desc->file()->options().optimize_for() == google::protobuf::FileOptions::LITE_RUNTIME;
  43. }
  44. bool IsAutogeneratedNestedType(const Descriptor* desc) {
  45. return desc->options().map_entry();
  46. }
  47. class TFieldExtGenerator {
  48. public:
  49. TFieldExtGenerator(const FieldDescriptor* field)
  50. : Field_(field)
  51. {
  52. SetCommonFieldVariables(Field_, &Variables_);
  53. }
  54. virtual ~TFieldExtGenerator() {
  55. }
  56. virtual void GenerateAccessorDeclarations(io::Printer* printer) = 0;
  57. virtual void GenerateJSONPrinting(io::Printer* printer) = 0;
  58. protected:
  59. void GenerateRepeatedJSONPrinting(io::Printer* printer, const char* itemPrinter) {
  60. printer->Print("out << '[';\n");
  61. printer->Print("{\n");
  62. printer->Indent();
  63. printer->Print("const char* separator = \"\";\n");
  64. printer->Print(Variables_, "for (size_t _index = 0; _index < $rname$Size(); ++_index) {\n");
  65. printer->Indent();
  66. printer->Print("out << separator;\n");
  67. printer->Print(Variables_, itemPrinter);
  68. printer->Print(";\n");
  69. printer->Print("separator = \",\";\n");
  70. printer->Outdent();
  71. printer->Print("}\n");
  72. printer->Outdent();
  73. printer->Print("}\n");
  74. printer->Print("out << ']';\n");
  75. }
  76. protected:
  77. const FieldDescriptor* Field_;
  78. TVariables Variables_;
  79. };
  80. class TMessageFieldExtGenerator: public TFieldExtGenerator {
  81. public:
  82. TMessageFieldExtGenerator(const FieldDescriptor* field)
  83. : TFieldExtGenerator(field)
  84. {
  85. }
  86. void GenerateAccessorDeclarations(io::Printer* printer) override {
  87. Variables_["type"] = QualifiedClassName(Field_->message_type());
  88. printer->Print(Variables_,
  89. "inline const $type$& Get$rname$() const { return $name$(); }\n"
  90. "inline $type$* Mutable$rname$() { return mutable_$name$(); }\n");
  91. }
  92. void GenerateJSONPrinting(io::Printer* printer) override {
  93. printer->Print(Variables_, "Get$rname$().PrintJSON(out);\n");
  94. }
  95. };
  96. class TMapFieldExtGenerator: public TFieldExtGenerator {
  97. public:
  98. TMapFieldExtGenerator(const FieldDescriptor* field)
  99. : TFieldExtGenerator(field)
  100. , Key_(field->message_type()->FindFieldByName("key"))
  101. , Val_(field->message_type()->FindFieldByName("value"))
  102. {
  103. Variables_["key_cpp"] = PrimitiveTypeName(Key_->cpp_type());
  104. switch (Val_->cpp_type()) {
  105. case FieldDescriptor::CPPTYPE_MESSAGE:
  106. Variables_["val_cpp"] = QualifiedClassName(Val_->message_type());
  107. break;
  108. case FieldDescriptor::CPPTYPE_ENUM:
  109. Variables_["val_cpp"] = ClassName(Val_->enum_type(), true);
  110. break;
  111. default:
  112. Variables_["val_cpp"] = PrimitiveTypeName(Val_->cpp_type());
  113. }
  114. }
  115. void GenerateAccessorDeclarations(io::Printer* printer) {
  116. printer->Print(Variables_,
  117. "inline const ::google::protobuf::Map<$key_cpp$, $val_cpp$>& Get$rname$() const { return $name$(); }\n"
  118. "inline ::google::protobuf::Map<$key_cpp$, $val_cpp$>* Mutable$rname$() { return mutable_$name$(); }\n");
  119. }
  120. void GenerateKeyValuePrinting(io::Printer* printer, const char* scopeName, bool isKey) {
  121. const FieldDescriptor* desc = isKey ? Key_ : Val_;
  122. switch(desc->cpp_type()) {
  123. case FieldDescriptor::CPPTYPE_STRING:
  124. printer->Print(TString::Join("::google::protobuf::io::PrintJSONString(out, ", scopeName , ");\n").data());
  125. break;
  126. case FieldDescriptor::CPPTYPE_ENUM:
  127. printer->Print(TString::Join("out << int(", scopeName, ");\n").data());
  128. break;
  129. case FieldDescriptor::CPPTYPE_MESSAGE:
  130. printer->Print(TString::Join(scopeName, ".PrintJSON(out);\n").data());
  131. break;
  132. default:
  133. if (isKey) {
  134. printer->Print(TString::Join("out << '\"' << ", scopeName, " << '\"';\n").data());
  135. } else {
  136. printer->Print(TString::Join("out << ", scopeName, ";\n").data());
  137. }
  138. }
  139. }
  140. void GenerateJSONPrinting(io::Printer* printer) {
  141. printer->Print(Variables_,
  142. "out << '{';\n"
  143. "const ::google::protobuf::Map<$key_cpp$, $val_cpp$>& map = Get$rname$();\n"
  144. "for (auto it = map.begin(); it != map.end(); ++it) {\n"
  145. );
  146. printer->Indent();
  147. printer->Print("if (it != map.begin()) { out << ','; }\n");
  148. GenerateKeyValuePrinting(printer, "it->first", true);
  149. printer->Print("out << ':';\n");
  150. GenerateKeyValuePrinting(printer, "it->second", false);
  151. printer->Outdent();
  152. printer->Print("}\n");
  153. printer->Print("out << '}';\n");
  154. }
  155. private:
  156. const FieldDescriptor* Key_;
  157. const FieldDescriptor* Val_;
  158. };
  159. class TRepeatedMessageFieldExtGenerator: public TFieldExtGenerator {
  160. public:
  161. TRepeatedMessageFieldExtGenerator(const FieldDescriptor* field)
  162. : TFieldExtGenerator(field)
  163. {
  164. }
  165. void GenerateAccessorDeclarations(io::Printer* printer) override {
  166. Variables_["type"] = QualifiedClassName(Field_->message_type());
  167. printer->Print(Variables_,
  168. "inline const $type$& Get$rname$(size_t _index) const {Y_ASSERT(_index < static_cast<size_t>(::Max<int>())); return $name$(int(_index)); }\n"
  169. "inline $type$* Mutable$rname$(size_t _index) {Y_ASSERT(_index < static_cast<size_t>(::Max<int>())); return mutable_$name$(int(_index)); }\n"
  170. "inline $type$* Add$rname$() { return add_$name$(); }\n"
  171. "inline const $type$& get_idx_$name$(int _index) const { return $name$(_index); }\n"
  172. "inline const ::google::protobuf::RepeatedPtrField< $type$ >&\n"
  173. " get_arr_$name$() const { return $name$(); }\n"
  174. "inline const ::google::protobuf::RepeatedPtrField< $type$ >&\n"
  175. " Get$rname$() const { return $name$(); }\n"
  176. "inline ::google::protobuf::RepeatedPtrField< $type$ >*\n"
  177. " Mutable$rname$() { return mutable_$name$(); }\n");
  178. }
  179. void GenerateJSONPrinting(io::Printer* printer) override {
  180. GenerateRepeatedJSONPrinting(printer, "Get$rname$(_index).PrintJSON(out)");
  181. }
  182. };
  183. class TStringFieldExtGenerator: public TFieldExtGenerator {
  184. public:
  185. TStringFieldExtGenerator(const FieldDescriptor* field)
  186. : TFieldExtGenerator(field)
  187. {
  188. }
  189. void GenerateAccessorDeclarations(io::Printer* printer) override {
  190. Variables_["pointer_type"] = Field_->type() == FieldDescriptor::TYPE_BYTES ? "void" : "char";
  191. if (Field_->options().ctype() != FieldOptions::STRING) {
  192. printer->Outdent();
  193. printer->Print(
  194. " private:\n"
  195. " // Hidden due to unknown ctype option.\n");
  196. printer->Indent();
  197. }
  198. printer->Print(Variables_,
  199. "inline const TProtoStringType& Get$rname$() const { return $name$(); }\n"
  200. "inline void Set$rname$(const TProtoStringType& value) { set_$name$(value); }\n"
  201. "inline void Set$rname$(TProtoStringType&& value) { set_$name$(std::move(value)); }\n"
  202. "inline void Set$rname$(const char* value) { set_$name$(value); }\n"
  203. "inline void Set$rname$(const $pointer_type$* value, size_t size) { set_$name$(value, size); }\n"
  204. "inline TProtoStringType* Mutable$rname$() { return mutable_$name$(); }\n");
  205. if (Field_->options().ctype() != FieldOptions::STRING) {
  206. printer->Outdent();
  207. printer->Print(" public:\n");
  208. printer->Indent();
  209. }
  210. }
  211. void GenerateJSONPrinting(io::Printer* printer) override {
  212. printer->Print(Variables_, "::google::protobuf::io::PrintJSONString(out, Get$rname$());\n");
  213. }
  214. };
  215. class TRepeatedStringFieldExtGenerator: public TFieldExtGenerator {
  216. public:
  217. TRepeatedStringFieldExtGenerator(const FieldDescriptor* field)
  218. : TFieldExtGenerator(field)
  219. {
  220. }
  221. void GenerateAccessorDeclarations(io::Printer* printer) override {
  222. Variables_["pointer_type"] = Field_->type() == FieldDescriptor::TYPE_BYTES ? "void" : "char";
  223. if (Field_->options().ctype() != FieldOptions::STRING) {
  224. printer->Outdent();
  225. printer->Print(
  226. " private:\n"
  227. " // Hidden due to unknown ctype option.\n");
  228. printer->Indent();
  229. }
  230. printer->Print(Variables_,
  231. "inline const TProtoStringType& Get$rname$(size_t _index) const {Y_ASSERT(_index < static_cast<size_t>(::Max<int>())); return $name$(_index); }\n"
  232. "inline TProtoStringType* Mutable$rname$(size_t _index) {Y_ASSERT(_index < static_cast<size_t>(::Max<int>())); return mutable_$name$(_index); }\n"
  233. "inline void Set$rname$(size_t _index, const TProtoStringType& value) {Y_ASSERT(_index < static_cast<size_t>(::Max<int>())); set_$name$(_index, value); }\n"
  234. "inline void Set$rname$(size_t _index, TProtoStringType&& value) {Y_ASSERT(_index < static_cast<size_t>(::Max<int>())); set_$name$(_index, std::move(value)); }\n"
  235. "inline void Set$rname$(size_t _index, const char* value) {Y_ASSERT(_index < static_cast<size_t>(::Max<int>())); set_$name$(_index, value); }\n"
  236. "inline void Set$rname$(size_t _index, const $pointer_type$* value, size_t size) {Y_ASSERT(_index < static_cast<size_t>(::Max<int>())); set_$name$(_index, value, size); }\n"
  237. "inline TProtoStringType* Add$rname$() { return add_$name$(); }\n"
  238. "inline void Add$rname$(const TProtoStringType& value) { add_$name$(value); }\n"
  239. "inline void Add$rname$(TProtoStringType&& value) { add_$name$(std::move(value)); }\n"
  240. "inline void Add$rname$(const char* value) { add_$name$(value); }\n"
  241. "inline void Add$rname$(const $pointer_type$* value, size_t size) { add_$name$(value, size); }\n"
  242. "inline const TProtoStringType& get_idx_$name$(int _index) const { return $name$(_index); }\n"
  243. "inline const ::google::protobuf::RepeatedPtrField<TProtoStringType>& get_arr_$name$() const"
  244. "{ return $name$(); }\n"
  245. "inline const ::google::protobuf::RepeatedPtrField<TProtoStringType>& Get$rname$() const"
  246. "{ return $name$(); }\n"
  247. "inline ::google::protobuf::RepeatedPtrField<TProtoStringType>* Mutable$rname$()"
  248. "{ return mutable_$name$(); }\n");
  249. if (Field_->options().ctype() != FieldOptions::STRING) {
  250. printer->Outdent();
  251. printer->Print(" public:\n");
  252. printer->Indent();
  253. }
  254. }
  255. void GenerateJSONPrinting(io::Printer* printer) override {
  256. GenerateRepeatedJSONPrinting(
  257. printer,
  258. "::google::protobuf::io::PrintJSONString(out, Get$rname$(_index))"
  259. );
  260. }
  261. };
  262. class TEnumFieldExtGenerator: public TFieldExtGenerator {
  263. public:
  264. TEnumFieldExtGenerator(const FieldDescriptor* field)
  265. : TFieldExtGenerator(field)
  266. {
  267. }
  268. void GenerateAccessorDeclarations(io::Printer* printer) override {
  269. Variables_["type"] = ClassName(Field_->enum_type(), true);
  270. printer->Print(Variables_,
  271. "inline $type$ Get$rname$() const { return $name$(); }\n"
  272. "inline void Set$rname$($type$ value) { set_$name$(value); }\n");
  273. }
  274. void GenerateJSONPrinting(io::Printer* printer) override {
  275. printer->Print(Variables_, "out << (int)Get$rname$();\n");
  276. }
  277. };
  278. class TRepeatedEnumFieldExtGenerator: public TFieldExtGenerator {
  279. public:
  280. TRepeatedEnumFieldExtGenerator(const FieldDescriptor* field)
  281. : TFieldExtGenerator(field)
  282. {
  283. }
  284. void GenerateAccessorDeclarations(io::Printer* printer) override {
  285. Variables_["type"] = ClassName(Field_->enum_type(), true);
  286. printer->Print(Variables_,
  287. "inline $type$ Get$rname$(size_t _index) const {Y_ASSERT(_index < static_cast<size_t>(::Max<int>())); return $name$(_index); }\n"
  288. "inline void Set$rname$(size_t _index, $type$ value) {Y_ASSERT(_index < static_cast<size_t>(::Max<int>())); set_$name$(_index, value); }\n"
  289. "inline void Add$rname$($type$ value) { add_$name$(value); }\n"
  290. "inline $type$ get_idx_$name$(int _index) const {return $name$(_index); }\n"
  291. "inline const ::google::protobuf::RepeatedField<int>& get_arr_$name$() const { return $name$(); }\n"
  292. "inline const ::google::protobuf::RepeatedField<int>& Get$rname$() const { return $name$(); }\n"
  293. "inline ::google::protobuf::RepeatedField<int>* Mutable$rname$() { return mutable_$name$(); }\n");
  294. }
  295. void GenerateJSONPrinting(io::Printer* printer) override {
  296. GenerateRepeatedJSONPrinting(printer, "out << (int)Get$rname$(_index)");
  297. }
  298. };
  299. class TPrimitiveFieldExtGenerator: public TFieldExtGenerator {
  300. public:
  301. TPrimitiveFieldExtGenerator(const FieldDescriptor* field)
  302. : TFieldExtGenerator(field)
  303. {
  304. }
  305. void GenerateAccessorDeclarations(io::Printer* printer) override {
  306. Variables_["type"] = PrimitiveTypeName(Field_->cpp_type());
  307. printer->Print(Variables_,
  308. "inline $type$ Get$rname$() const { return $name$();}\n"
  309. "inline void Set$rname$($type$ value) { set_$name$(value); }\n");
  310. }
  311. void GenerateJSONPrinting(io::Printer* printer) override {
  312. printer->Print(Variables_, "out << Get$rname$();\n");
  313. }
  314. };
  315. class TRepeatedPrimitiveFieldExtGenerator: public TFieldExtGenerator {
  316. public:
  317. TRepeatedPrimitiveFieldExtGenerator(const FieldDescriptor* field)
  318. : TFieldExtGenerator(field)
  319. {
  320. }
  321. void GenerateAccessorDeclarations(io::Printer* printer) override {
  322. Variables_["type"] = PrimitiveTypeName(Field_->cpp_type());
  323. printer->Print(Variables_,
  324. "inline $type$ Get$rname$(size_t _index) const {Y_ASSERT(_index < static_cast<size_t>(::Max<int>())); return $name$(_index); }\n"
  325. "inline void Set$rname$(size_t _index, $type$ value) {Y_ASSERT(_index < static_cast<size_t>(::Max<int>())); set_$name$(_index, value); }\n"
  326. "inline void Add$rname$($type$ value) { add_$name$(value); }\n"
  327. "inline $type$ get_idx_$name$(int _index) const { return $name$(_index); }\n"
  328. "inline const ::google::protobuf::RepeatedField< $type$ >&\n"
  329. " get_arr_$name$() const { return $name$(); }\n"
  330. "inline const ::google::protobuf::RepeatedField< $type$ >&\n"
  331. " Get$rname$() const { return $name$(); }\n"
  332. "inline ::google::protobuf::RepeatedField< $type$ >*\n"
  333. " Mutable$rname$() { return mutable_$name$(); }\n");
  334. }
  335. void GenerateJSONPrinting(io::Printer* printer) override {
  336. GenerateRepeatedJSONPrinting(printer, "out << Get$rname$(_index)");
  337. }
  338. };
  339. class TBoolFieldExtGenerator: public TPrimitiveFieldExtGenerator {
  340. public:
  341. TBoolFieldExtGenerator(const FieldDescriptor* field)
  342. : TPrimitiveFieldExtGenerator(field)
  343. {
  344. }
  345. void GenerateJSONPrinting(io::Printer* printer) override {
  346. printer->Print(Variables_, "out << (Get$rname$() ? \"true\" : \"false\");\n");
  347. }
  348. };
  349. class TRepeatedBoolFieldExtGenerator: public TRepeatedPrimitiveFieldExtGenerator {
  350. public:
  351. TRepeatedBoolFieldExtGenerator(const FieldDescriptor* field)
  352. : TRepeatedPrimitiveFieldExtGenerator(field)
  353. {
  354. }
  355. void GenerateJSONPrinting(io::Printer* printer) override {
  356. GenerateRepeatedJSONPrinting(printer, "out << (Get$rname$(_index) ? \"true\" : \"false\")");
  357. }
  358. };
  359. class TFloatFieldExtGenerator: public TPrimitiveFieldExtGenerator {
  360. public:
  361. TFloatFieldExtGenerator(const FieldDescriptor* field)
  362. : TPrimitiveFieldExtGenerator(field)
  363. {
  364. }
  365. void GenerateJSONPrinting(io::Printer* printer) override {
  366. printer->Print(Variables_, "out << double(Get$rname$());\n");
  367. }
  368. };
  369. class TRepeatedFloatFieldExtGenerator: public TRepeatedPrimitiveFieldExtGenerator {
  370. public:
  371. TRepeatedFloatFieldExtGenerator(const FieldDescriptor* field)
  372. : TRepeatedPrimitiveFieldExtGenerator(field)
  373. {
  374. }
  375. void GenerateJSONPrinting(io::Printer* printer) override {
  376. GenerateRepeatedJSONPrinting(printer, "out << double(Get$rname$(_index))");
  377. }
  378. };
  379. // borrowed mostly from protobuf/compiler/cpp/cpp_extension.cc
  380. class TExtensionGenerator {
  381. public:
  382. TExtensionGenerator(const FieldDescriptor* descriptor)
  383. : Descriptor_(descriptor)
  384. {
  385. if (Descriptor_->is_repeated()) {
  386. type_traits_ = "Repeated";
  387. }
  388. TProtoStringType clsName;
  389. switch (Descriptor_->cpp_type()) {
  390. case FieldDescriptor::CPPTYPE_ENUM:
  391. type_traits_.append("EnumTypeTraits< ");
  392. clsName = ClassName(Descriptor_->enum_type(), true);
  393. type_traits_.append(clsName);
  394. type_traits_.append(", ");
  395. type_traits_.append(clsName);
  396. type_traits_.append("_IsValid>");
  397. break;
  398. case FieldDescriptor::CPPTYPE_STRING:
  399. type_traits_.append("StringTypeTraits");
  400. break;
  401. case FieldDescriptor::CPPTYPE_MESSAGE:
  402. type_traits_.append("MessageTypeTraits< ");
  403. type_traits_.append(ClassName(Descriptor_->message_type(), true));
  404. type_traits_.append(" >");
  405. break;
  406. default:
  407. type_traits_.append("PrimitiveTypeTraits< ");
  408. type_traits_.append(PrimitiveTypeName(Descriptor_->cpp_type()));
  409. type_traits_.append(" >");
  410. break;
  411. }
  412. }
  413. void GenerateDeclaration(io::Printer* printer) const
  414. {
  415. TVariables vars;
  416. vars["extendee" ] = ClassName(Descriptor_->containing_type(), true);
  417. vars["type_traits" ] = type_traits_;
  418. vars["name" ] = Descriptor_->name();
  419. vars["field_type" ] = std::to_string(static_cast<int>(Descriptor_->type()));
  420. vars["packed" ] = Descriptor_->options().packed() ? "true" : "false";
  421. printer->Print(vars,
  422. "typedef ::google::protobuf::internal::ExtensionIdentifier< $extendee$,\n"
  423. " ::google::protobuf::internal::$type_traits$, $field_type$, $packed$ >\n"
  424. " Td$name$;\n"
  425. );
  426. }
  427. private:
  428. const FieldDescriptor* Descriptor_;
  429. TProtoStringType type_traits_;
  430. };
  431. class TOneofGenerator {
  432. public:
  433. TOneofGenerator(const OneofDescriptor* Descriptor_)
  434. : Descriptor_(Descriptor_)
  435. {
  436. Variables_["camel_oneof_name"] = UnderscoresToCamelCase(Descriptor_->name(), true);
  437. Variables_["rname"] = Descriptor_->name();
  438. }
  439. void GenerateDeclarations(io::Printer* printer) const {
  440. printer->Print(Variables_, "$camel_oneof_name$Case Get$rname$Case() const { return $rname$_case(); }\n");
  441. printer->Print(Variables_, "void Clear$rname$() { clear_$rname$(); }\n");
  442. if (Descriptor_->name() != UnderscoresToCamelCase(Descriptor_->name(), true)) {
  443. printer->Print(Variables_, "$camel_oneof_name$Case Get$camel_oneof_name$Case() const { return $rname$_case(); }\n");
  444. printer->Print(Variables_, "void Clear$camel_oneof_name$() { clear_$rname$(); }\n");
  445. }
  446. }
  447. private:
  448. const OneofDescriptor* Descriptor_;
  449. TVariables Variables_;
  450. };
  451. TFieldExtGenerator* MakeGenerator(const FieldDescriptor* field) {
  452. if (field->is_repeated()) {
  453. switch (field->cpp_type()) {
  454. case FieldDescriptor::CPPTYPE_MESSAGE:
  455. if (field->is_map()) {
  456. return new TMapFieldExtGenerator(field);
  457. }
  458. return new TRepeatedMessageFieldExtGenerator(field);
  459. case FieldDescriptor::CPPTYPE_BOOL:
  460. return new TRepeatedBoolFieldExtGenerator(field);
  461. case FieldDescriptor::CPPTYPE_FLOAT:
  462. return new TRepeatedFloatFieldExtGenerator(field);
  463. case FieldDescriptor::CPPTYPE_STRING:
  464. switch (field->options().ctype()) {
  465. default: // RepeatedStringFieldExtGenerator handles unknown ctypes.
  466. case FieldOptions::STRING:
  467. return new TRepeatedStringFieldExtGenerator(field);
  468. }
  469. case FieldDescriptor::CPPTYPE_ENUM:
  470. return new TRepeatedEnumFieldExtGenerator(field);
  471. default:
  472. return new TRepeatedPrimitiveFieldExtGenerator(field);
  473. }
  474. } else {
  475. switch (field->cpp_type()) {
  476. case FieldDescriptor::CPPTYPE_MESSAGE:
  477. return new TMessageFieldExtGenerator(field);
  478. case FieldDescriptor::CPPTYPE_BOOL:
  479. return new TBoolFieldExtGenerator(field);
  480. case FieldDescriptor::CPPTYPE_FLOAT:
  481. return new TFloatFieldExtGenerator(field);
  482. case FieldDescriptor::CPPTYPE_STRING:
  483. switch (field->options().ctype()) {
  484. default: // StringFieldGenerator handles unknown ctypes.
  485. case FieldOptions::STRING:
  486. return new TStringFieldExtGenerator(field);
  487. }
  488. case FieldDescriptor::CPPTYPE_ENUM:
  489. return new TEnumFieldExtGenerator(field);
  490. default:
  491. return new TPrimitiveFieldExtGenerator(field);
  492. }
  493. }
  494. }
  495. class TMessageExtGenerator {
  496. public:
  497. TMessageExtGenerator(const Descriptor* descriptor, OutputDirectory* outputDirectory)
  498. : Descriptor_(descriptor)
  499. , Classname_(ClassName(descriptor, false))
  500. , OutputDirectory_(outputDirectory)
  501. {
  502. for (int i = 0; i < descriptor->nested_type_count(); i++) {
  503. if (!IsAutogeneratedNestedType(descriptor->nested_type(i))) {
  504. NestedGenerators_.emplace_back(descriptor->nested_type(i), OutputDirectory_);
  505. }
  506. }
  507. FieldGenerators_.reserve(descriptor->field_count());
  508. for (int i = 0; i < descriptor->field_count(); i++) {
  509. FieldGenerators_.emplace_back(MakeGenerator(descriptor->field(i)));
  510. }
  511. ExtensionGenerators_.reserve(descriptor->extension_count());
  512. for (int i = 0; i < descriptor->extension_count(); i++) {
  513. ExtensionGenerators_.emplace_back(descriptor->extension(i));
  514. }
  515. OneofGenerators_.reserve(descriptor->real_oneof_decl_count());
  516. for (int i = 0; i < descriptor->real_oneof_decl_count(); i++) {
  517. OneofGenerators_.emplace_back(descriptor->oneof_decl(i));
  518. }
  519. }
  520. void GenerateClassDefinitionExtension() {
  521. GenerateSaveLoadImplementation();
  522. GenerateJSONImplementation();
  523. for (auto& nestedGenerator: NestedGenerators_) {
  524. nestedGenerator.GenerateClassDefinitionExtension();
  525. }
  526. }
  527. void GenerateDebugOutputExtension() {
  528. GenerateDebugOutput();
  529. for (auto& nestedGenerator: NestedGenerators_) {
  530. nestedGenerator.GenerateDebugOutputExtension();
  531. }
  532. }
  533. void GenerateTypedefOutputExtension(bool nested) {
  534. GenerateTypedefOutput(nested);
  535. for (auto& nestedGenerator: NestedGenerators_) {
  536. nestedGenerator.GenerateTypedefOutputExtension(true);
  537. }
  538. }
  539. void GenerateClassExtension() {
  540. GenerateDebugStringImplementation();
  541. for (auto& nestedGenerator: NestedGenerators_) {
  542. nestedGenerator.GenerateClassExtension();
  543. }
  544. }
  545. void GenerateDeclarations() {
  546. GenerateFieldAccessorDeclarations();
  547. for (auto& nestedGenerator: NestedGenerators_) {
  548. nestedGenerator.GenerateDeclarations();
  549. }
  550. }
  551. void GenerateDefinitions() {
  552. GenerateClassExtension();
  553. GenerateDebugOutputExtension();
  554. GenerateClassDefinitionExtension();
  555. }
  556. private:
  557. void GenerateFieldAccessorDeclarations() {
  558. TProtoStringType fileName = HeaderFileName(Descriptor_->file());
  559. TProtoStringType scope = "class_scope:" + Descriptor_->full_name();
  560. std::unique_ptr<io::ZeroCopyOutputStream> output(
  561. OutputDirectory_->OpenForInsert(fileName, scope));
  562. io::Printer printer(output.get(), '$');
  563. printer.Print("// Yandex cpp-styleguide extension\n");
  564. for (int i = 0; i < Descriptor_->field_count(); i++) {
  565. const FieldDescriptor* field = Descriptor_->field(i);
  566. TVariables vars;
  567. SetCommonFieldVariables(field, &vars);
  568. if (field->is_repeated()) {
  569. printer.Print(vars,
  570. "inline size_t $rname$Size() const { return (size_t)$name$_size(); }\n");
  571. } else if (field->has_presence()) {
  572. printer.Print(vars,
  573. "inline bool Has$rname$() const { return has_$name$(); }\n");
  574. }
  575. printer.Print(vars, "inline void Clear$rname$() { clear_$name$(); }\n");
  576. // Generate type-specific accessor declarations.
  577. FieldGenerators_[i]->GenerateAccessorDeclarations(&printer);
  578. printer.Print("\n");
  579. }
  580. for (auto& extensionGenerator: ExtensionGenerators_) {
  581. extensionGenerator.GenerateDeclaration(&printer);
  582. }
  583. for (auto& oneofGenerator: OneofGenerators_) {
  584. oneofGenerator.GenerateDeclarations(&printer);
  585. }
  586. TVariables vars;
  587. vars["class"] = ClassName(Descriptor_, false);
  588. if (!IsLiteRuntimeMessage(Descriptor_)) {
  589. printer.Print("TProtoStringType ShortUtf8DebugString() const;\n");
  590. printer.Print("void PrintJSON(IOutputStream&) const override;\n");
  591. printer.Print(vars, "::google::protobuf::io::TAsJSON<$class$> AsJSON() const {\n");
  592. printer.Print(vars, " return ::google::protobuf::io::TAsJSON<$class$>(*this);\n");
  593. printer.Print("}\n");
  594. printer.Print("void Save(IOutputStream* output) const;\n");
  595. printer.Print("void Load(IInputStream* input);\n");
  596. }
  597. printer.Print("// End of Yandex-specific extension\n");
  598. }
  599. void GenerateSaveLoadImplementation() {
  600. TProtoStringType fileName = SourceFileName(Descriptor_->file());
  601. TProtoStringType scope = "namespace_scope";
  602. std::unique_ptr<io::ZeroCopyOutputStream> output(
  603. OutputDirectory_->OpenForInsert(fileName, scope));
  604. io::Printer printer(output.get(), '$');
  605. TVariables vars;
  606. vars["class"] = Classname_;
  607. if (!IsLiteRuntimeMessage(Descriptor_)) {
  608. printer.Print("// Yandex-specific extension\n");
  609. printer.Print(vars, "void $class$::Save(IOutputStream* output) const {\n");
  610. printer.Print(" ::Save(output, static_cast<const ::google::protobuf::Message&>(*this));\n");
  611. printer.Print("}\n");
  612. printer.Print(vars, "void $class$::Load(IInputStream* input) {\n");
  613. printer.Print(" ::Load(input, static_cast<::google::protobuf::Message&>(*this));\n");
  614. printer.Print("}\n");
  615. printer.Print("// End of Yandex-specific extension\n");
  616. }
  617. }
  618. void GenerateDebugStringImplementation() {
  619. TProtoStringType fileName = SourceFileName(Descriptor_->file());
  620. TProtoStringType scope = "namespace_scope";
  621. std::unique_ptr<io::ZeroCopyOutputStream> output(
  622. OutputDirectory_->OpenForInsert(fileName, scope));
  623. io::Printer printer(output.get(), '$');
  624. TVariables vars;
  625. vars["class"] = Classname_;
  626. if (!IsLiteRuntimeMessage(Descriptor_)) {
  627. printer.Print("// Yandex-specific extension\n");
  628. printer.Print(vars, "TProtoStringType $class$::ShortUtf8DebugString() const {\n");
  629. printer.Print(" return ::ShortUtf8DebugString(*this);\n");
  630. printer.Print("}\n");
  631. printer.Print("// End of Yandex-specific extension\n");
  632. }
  633. }
  634. void GenerateJSONImplementation() {
  635. if (IsLiteRuntimeMessage(Descriptor_)) {
  636. return;
  637. }
  638. TProtoStringType fileName = SourceFileName(Descriptor_->file());
  639. TProtoStringType scope = "namespace_scope";
  640. std::unique_ptr<io::ZeroCopyOutputStream> output(
  641. OutputDirectory_->OpenForInsert(fileName, scope));
  642. io::Printer printer(output.get(), '$');
  643. printer.Print("// Yandex JSON extension\n");
  644. TVariables vars;
  645. vars["class"] = ClassName(Descriptor_, true);
  646. printer.Print(vars, "inline void $class$::PrintJSON(IOutputStream& out) const {\n");
  647. printer.Indent();
  648. printer.Print("out << '{';\n");
  649. if (Descriptor_->field_count() > 0) {
  650. printer.Print("const char* sep = \"\";\n");
  651. }
  652. for (int i = 0; i < Descriptor_->field_count(); i++) {
  653. const FieldDescriptor* field = Descriptor_->field(i);
  654. TVariables vars;
  655. SetCommonFieldVariables(field, &vars);
  656. if (field->is_repeated()) {
  657. // map or repeated field in both proto3 and proto2 syntax
  658. printer.Print(vars, "if ($rname$Size() > 0) {\n");
  659. } else if (field->has_presence()) {
  660. // any optional or required field in proto2 syntax
  661. // message-field or any oneof field in proto3 syntax
  662. printer.Print(vars, "if (Has$rname$()) {\n");
  663. } else {
  664. // string, enum or primitive field in proto3 syntax
  665. printer.Print(vars, "if (Get$rname$()) {\n");
  666. }
  667. printer.Indent();
  668. printer.Print("out << sep;\n");
  669. printer.Print(vars, "out << \"\\\"$rname$\\\":\";\n");
  670. FieldGenerators_[i]->GenerateJSONPrinting(&printer);
  671. printer.Print(vars, "sep = \",\";\n");
  672. printer.Outdent();
  673. printer.Print("}\n");
  674. }
  675. printer.Print("out << '}';\n");
  676. printer.Outdent();
  677. printer.Print("}\n");
  678. printer.Print("// End of Yandex JSON extension\n");
  679. }
  680. void GenerateDebugOutput() {
  681. TProtoStringType fileName = SourceFileName(Descriptor_->file());
  682. TProtoStringType scope = "global_scope";
  683. std::unique_ptr<io::ZeroCopyOutputStream> output(
  684. OutputDirectory_->OpenForInsert(fileName, scope));
  685. io::Printer printer(output.get(), '$');
  686. if (!IsLiteRuntimeMessage(Descriptor_)) {
  687. printer.Print("// Yandex debug output extension\n");
  688. TVariables vars;
  689. vars["class"] = ClassName(Descriptor_, true);
  690. printer.Print("template<>\n");
  691. printer.Print(vars, "void Out< $class$>(IOutputStream& out, const $class$& msg) {\n");
  692. printer.Print(" out << \"{ \" << msg.ShortUtf8DebugString() << \" }\";\n");
  693. printer.Print("}\n");
  694. printer.Print("// End of Yandex debug output extension\n");
  695. }
  696. }
  697. void GenerateTypedefOutput(bool nested) {
  698. if (!GenerateYaStyle(Descriptor_->file()))
  699. return;
  700. TProtoStringType fileName = HeaderFileName(Descriptor_->file());
  701. TProtoStringType scope = nested ? "class_scope:" + Descriptor_->full_name().substr(0,
  702. Descriptor_->full_name().size() - Descriptor_->name().size() - 1)
  703. : "namespace_scope";
  704. std::unique_ptr<io::ZeroCopyOutputStream> output(
  705. OutputDirectory_->OpenForInsert(fileName, scope));
  706. io::Printer printer(output.get(), '$');
  707. TString name = Descriptor_->name();
  708. bool isOk = name.size() >= 2 && name[0] == 'T' && name[1] >= 'A' && name[1] <= 'Z';
  709. if (!isOk) {
  710. printer.Print("// Yandex typedef extension\n");
  711. TVariables vars;
  712. vars["class"] = name;
  713. vars["base_class"] = ClassName(Descriptor_, true);
  714. printer.Print(vars, "typedef $base_class$ T$class$;\n");
  715. printer.Print("// End of Yandex typedef extension\n");
  716. }
  717. }
  718. private:
  719. const Descriptor* Descriptor_;
  720. TProtoStringType Classname_;
  721. OutputDirectory* OutputDirectory_;
  722. std::vector<std::unique_ptr<TFieldExtGenerator>> FieldGenerators_;
  723. std::vector<TMessageExtGenerator> NestedGenerators_;
  724. std::vector<TExtensionGenerator> ExtensionGenerators_;
  725. std::vector<TOneofGenerator> OneofGenerators_;
  726. };
  727. class TFileExtGenerator {
  728. public:
  729. TFileExtGenerator(const FileDescriptor* file, OutputDirectory* output_directory)
  730. : File_(file)
  731. , OutputDirectory_(output_directory)
  732. {
  733. MessageGenerators_.reserve(file->message_type_count());
  734. for (int i = 0; i < file->message_type_count(); i++) {
  735. MessageGenerators_.emplace_back(file->message_type(i), OutputDirectory_);
  736. }
  737. }
  738. void GenerateHeaderExtensions() {
  739. GenerateHeaderIncludeExtensions();
  740. for (auto& messageGenerator: MessageGenerators_) {
  741. messageGenerator.GenerateTypedefOutputExtension(false);
  742. messageGenerator.GenerateDeclarations();
  743. }
  744. }
  745. void GenerateSourceExtensions() {
  746. GenerateSourceIncludeExtensions();
  747. for (auto& messageGenerator: MessageGenerators_) {
  748. messageGenerator.GenerateDefinitions();
  749. }
  750. }
  751. private:
  752. void GenerateSourceIncludeExtensions() {
  753. TProtoStringType fileName = SourceFileName(File_);
  754. TProtoStringType scope = "includes";
  755. std::unique_ptr<io::ZeroCopyOutputStream> output(
  756. OutputDirectory_->OpenForInsert(fileName, scope));
  757. io::Printer printer(output.get(), '$');
  758. printer.Print("#include <google/protobuf/messagext.h>\n");
  759. }
  760. void GenerateHeaderIncludeExtensions() {
  761. TProtoStringType fileName = HeaderFileName(File_);
  762. TProtoStringType scope = "includes";
  763. std::unique_ptr<io::ZeroCopyOutputStream> output(
  764. OutputDirectory_->OpenForInsert(fileName, scope));
  765. io::Printer printer(output.get(), '$');
  766. printer.Print("#include <google/protobuf/json_util.h>\n");
  767. }
  768. private:
  769. const FileDescriptor* File_;
  770. OutputDirectory* OutputDirectory_;
  771. size_t MessageTypeCount_;
  772. std::vector<TMessageExtGenerator> MessageGenerators_;
  773. };
  774. bool TCppStyleGuideExtensionGenerator::Generate(const FileDescriptor* file,
  775. const TProtoStringType&,
  776. OutputDirectory* outputDirectory,
  777. TProtoStringType*) const {
  778. TFileExtGenerator fileGenerator(file, outputDirectory);
  779. // Generate header.
  780. fileGenerator.GenerateHeaderExtensions();
  781. // Generate cc file.
  782. fileGenerator.GenerateSourceExtensions();
  783. return true;
  784. }
  785. }
  786. }
  787. }
  788. int main(int argc, char* argv[]) {
  789. #ifdef _MSC_VER
  790. // Don't print a silly message or stick a modal dialog box in my face,
  791. // please.
  792. _set_abort_behavior(0, ~0);
  793. #endif // !_MSC_VER
  794. NProtobuf::NCompiler::NPlugins::TCppStyleGuideExtensionGenerator generator;
  795. return google::protobuf::compiler::PluginMain(argc, argv, &generator);
  796. }