Skip to content

Instantly share code, notes, and snippets.

@orisano
Last active January 14, 2017 19:13
Show Gist options
  • Save orisano/1b5c8212aa5c585846023b6303371bc7 to your computer and use it in GitHub Desktop.
Save orisano/1b5c8212aa5c585846023b6303371bc7 to your computer and use it in GitHub Desktop.
import clang.cindex
from clang.cindex import Index
def gen_stream_operator(cur):
if cur.kind.name == "STRUCT_DECL":
print("std::ostream& operator<<(std::ostream& os, const %s& rhs) {" % cur.displayname)
for child in cur.get_children():
if child.kind.name == "FIELD_DECL":
print(' os << "{member}:" << rhs.{member} << std::endl;'.format(member=child.displayname))
print(" return os;")
print("}")
else:
for child in cur.get_children():
gen_stream_operator(child)
def main():
index = Index.create()
tu = index.parse("./test.cpp")
gen_stream_operator(tu.cursor)
if __name__ == "__main__":
main()
"""
std::ostream& operator<<(std::ostream& os, const Test& rhs) {
os << "x:" << rhs.x << std::endl;
os << "y:" << rhs.y << std::endl;
os << "z:" << rhs.z << std::endl;
return os;
}
"""
struct Test {
int x;
int y;
int z;
};
int main() {
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment