1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 // Author: kenton@google.com (Kenton Varda) 9 // Based on original Protocol Buffers design by 10 // Sanjay Ghemawat, Jeff Dean, and others. 11 // 12 // Generates C++ code for a given .proto file. 13 14 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_GENERATOR_H__ 15 #define GOOGLE_PROTOBUF_COMPILER_CPP_GENERATOR_H__ 16 17 #include <cstdint> 18 #include <string> 19 #include <utility> 20 #include <vector> 21 22 #include "absl/status/status.h" 23 #include "absl/strings/string_view.h" 24 #include "google/protobuf/compiler/code_generator.h" 25 #include "google/protobuf/cpp_features.pb.h" 26 #include "google/protobuf/descriptor.pb.h" 27 #include "google/protobuf/port.h" 28 29 // Must be included last. 30 #include "google/protobuf/port_def.inc" 31 32 namespace google { 33 namespace protobuf { 34 namespace compiler { 35 namespace cpp { 36 // CodeGenerator implementation which generates a C++ source file and 37 // header. If you create your own protocol compiler binary and you want 38 // it to support C++ output, you can do so by registering an instance of this 39 // CodeGenerator with the CommandLineInterface in your main() function. 40 class PROTOC_EXPORT CppGenerator : public CodeGenerator { 41 public: 42 CppGenerator() = default; 43 CppGenerator(const CppGenerator&) = delete; 44 CppGenerator& operator=(const CppGenerator&) = delete; 45 ~CppGenerator() override = default; 46 47 enum class Runtime { 48 kGoogle3, // Use the internal google3 runtime. 49 kOpensource, // Use the open-source runtime. 50 51 // Use the open-source runtime with google3 #include paths. We make these 52 // absolute to avoid ambiguity, so the runtime will be #included like: 53 // #include "third_party/protobuf/.../google/protobuf/message.h" 54 kOpensourceGoogle3 55 }; 56 set_opensource_runtime(bool opensource)57 void set_opensource_runtime(bool opensource) { 58 opensource_runtime_ = opensource; 59 } 60 61 // If set to a non-empty string, generated code will do: 62 // #include "<BASE>/google/protobuf/message.h" 63 // instead of: 64 // #include "google/protobuf/message.h" 65 // This has no effect if opensource_runtime = false. set_runtime_include_base(std::string base)66 void set_runtime_include_base(std::string base) { 67 runtime_include_base_ = std::move(base); 68 } 69 70 bool Generate(const FileDescriptor* file, const std::string& parameter, 71 GeneratorContext* generator_context, 72 std::string* error) const override; 73 GetSupportedFeatures()74 uint64_t GetSupportedFeatures() const override { 75 return FEATURE_PROTO3_OPTIONAL | FEATURE_SUPPORTS_EDITIONS; 76 } 77 GetMinimumEdition()78 Edition GetMinimumEdition() const override { return Edition::EDITION_PROTO2; } GetMaximumEdition()79 Edition GetMaximumEdition() const override { return Edition::EDITION_2023; } 80 GetFeatureExtensions()81 std::vector<const FieldDescriptor*> GetFeatureExtensions() const override { 82 return {GetExtensionReflection(pb::cpp)}; 83 } 84 85 using CodeGenerator::GetEdition; 86 using CodeGenerator::GetResolvedSourceFeatures; 87 88 private: 89 bool opensource_runtime_ = google::protobuf::internal::IsOss(); 90 std::string runtime_include_base_; 91 92 absl::Status ValidateFeatures(const FileDescriptor* file) const; 93 }; 94 } // namespace cpp 95 } // namespace compiler 96 } // namespace protobuf 97 } // namespace google 98 99 #include "google/protobuf/port_undef.inc" 100 101 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_GENERATOR_H__ 102