Copy free top-level comments from headers to Rust bindings.
Comments are not traversed during normal AST visitations, so this keeps track of
all comments in the current file and emits IR for all preceding comments
whenever we visit a decl.
This uses source code locations to determine comment order. It also skips
comments that are fully contained within a decl, i.e., comments in the body of
inline functions.
PiperOrigin-RevId: 402777124
diff --git a/rs_bindings_from_cc/ir.h b/rs_bindings_from_cc/ir.h
index a52a2ee..3d6c757 100644
--- a/rs_bindings_from_cc/ir.h
+++ b/rs_bindings_from_cc/ir.h
@@ -295,6 +295,16 @@
return o << r.ToJson();
}
+struct Comment {
+ nlohmann::json ToJson() const;
+
+ std::string text;
+};
+
+inline std::ostream& operator<<(std::ostream& o, const Comment& r) {
+ return o << r.ToJson();
+}
+
// A complete intermediate representation of bindings for publicly accessible
// declarations of a single C++ library.
struct IR {
@@ -314,7 +324,7 @@
// Collection of public headers that were used to construct the AST this `IR`
// is generated from.
std::vector<HeaderName> used_headers;
- std::vector<std::variant<Func, Record, UnsupportedItem>> items;
+ std::vector<std::variant<Func, Record, UnsupportedItem, Comment>> items;
};
inline std::ostream& operator<<(std::ostream& o, const IR& ir) {