Last active
January 14, 2017 19:13
-
-
Save orisano/1b5c8212aa5c585846023b6303371bc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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