Add comments to the generated header file
diff --git a/src/compiler/generator_helpers.h b/src/compiler/generator_helpers.h
index e1bb66a..7cfea9d 100644
--- a/src/compiler/generator_helpers.h
+++ b/src/compiler/generator_helpers.h
@@ -35,6 +35,9 @@
#define GRPC_INTERNAL_COMPILER_GENERATOR_HELPERS_H
#include <map>
+#include <sstream>
+#include <string>
+#include <vector>
#include "src/compiler/config.h"
@@ -165,6 +168,44 @@
}
}
+inline void Split(const grpc::string &s, char delim,
+ std::vector<grpc::string> *append_to) {
+ std::istringstream iss(s);
+ grpc::string piece;
+ while (std::getline(iss, piece)) {
+ append_to->push_back(piece);
+ }
+}
+
+enum CommentType {
+ COMMENTTYPE_LEADING,
+ COMMENTTYPE_TRAILING,
+ COMMENTTYPE_LEADING_DETACHED
+};
+
+// Get all the comments and append each line to out.
+template <typename DescriptorType>
+inline void GetComment(const DescriptorType *desc, CommentType type,
+ std::vector<grpc::string> *out) {
+ grpc::protobuf::SourceLocation location;
+ if (!desc->GetSourceLocation(&location)) {
+ return;
+ }
+ if (type == COMMENTTYPE_LEADING || type == COMMENTTYPE_TRAILING) {
+ const grpc::string &comments = type == COMMENTTYPE_LEADING
+ ? location.leading_comments
+ : location.trailing_comments;
+ Split(comments, '\n', out);
+ } else if (type == COMMENTTYPE_LEADING_DETACHED) {
+ for (int i = 0; i < location.leading_detached_comments.size(); i++) {
+ Split(location.leading_detached_comments[i], '\n', out);
+ out->push_back("");
+ }
+ } else {
+ abort();
+ }
+}
+
} // namespace grpc_generator
#endif // GRPC_INTERNAL_COMPILER_GENERATOR_HELPERS_H