Skip to content

Instantly share code, notes, and snippets.

@maciejskorski
Created May 7, 2023 10:02
Show Gist options
  • Save maciejskorski/22bed219e4d1c5e619dd5881997d5d79 to your computer and use it in GitHub Desktop.
Save maciejskorski/22bed219e4d1c5e619dd5881997d5d79 to your computer and use it in GitHub Desktop.
non-trivial clang usage example
print_lcp_config_options
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L403,C39-L403,C59 netdissect_options declared in ./tcpdump/netdissect.h:L161
const unsigned char TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L403,C61-L403,C73 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
const unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L403,C75-L403,C86 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
print_ipcp_config_options
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L404,C40-L404,C60 netdissect_options declared in ./tcpdump/netdissect.h:L161
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L404,C62-L404,C77 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L404,C79-L404,C84 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
print_ip6cp_config_options
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L405,C41-L405,C61 netdissect_options declared in ./tcpdump/netdissect.h:L161
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L405,C63-L405,C78 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L405,C80-L405,C85 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
print_ccp_config_options
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L406,C39-L406,C59 netdissect_options declared in ./tcpdump/netdissect.h:L161
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L406,C61-L406,C76 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L406,C78-L406,C83 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
print_bacp_config_options
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L407,C40-L407,C60 netdissect_options declared in ./tcpdump/netdissect.h:L161
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L407,C62-L407,C77 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L407,C79-L407,C84 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
handle_ppp
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L408,C24-L408,C44 netdissect_options declared in ./tcpdump/netdissect.h:L161
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L408,C46-L408,C57 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L408,C59-L408,C74 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L408,C76-L408,C88 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
handle_ctrl_proto
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L412,C19-L412,C42 netdissect_options declared in ./tcpdump/netdissect.h:L161
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L413,C19-L413,C30 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L413,C32-L413,C50 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L413,C52-L413,C64 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
print_lcp_config_options
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L590,C26-L590,C49 netdissect_options declared in ./tcpdump/netdissect.h:L161
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L591,C26-L591,C41 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L591,C43-L591,C56 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
handle_mlppp
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L804,C14-L804,C37 netdissect_options declared in ./tcpdump/netdissect.h:L161
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L805,C14-L805,C29 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L805,C31-L805,C43 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
handle_chap
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L828,C13-L828,C36 netdissect_options declared in ./tcpdump/netdissect.h:L161
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L829,C13-L829,C28 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L829,C30-L829,C42 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
handle_pap
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L899,C12-L899,C35 netdissect_options declared in ./tcpdump/netdissect.h:L161
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L900,C12-L900,C27 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L900,C29-L900,C41 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
handle_bap
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L997,C12-L997,C35 netdissect_options declared in ./tcpdump/netdissect.h:L161
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L998,C12-L998,C27 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L998,C33-L998,C45 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
print_ipcp_config_options
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1006,C27-L1006,C50 netdissect_options declared in ./tcpdump/netdissect.h:L161
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1007,C27-L1007,C42 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L1007,C44-L1007,C56 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
print_ip6cp_config_options
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1145,C28-L1145,C51 netdissect_options declared in ./tcpdump/netdissect.h:L161
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1146,C28-L1146,C43 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L1146,C45-L1146,C57 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
print_ccp_config_options
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1205,C26-L1205,C49 netdissect_options declared in ./tcpdump/netdissect.h:L161
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1206,C26-L1206,C41 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L1206,C43-L1206,C55 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
print_bacp_config_options
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1301,C27-L1301,C50 netdissect_options declared in ./tcpdump/netdissect.h:L161
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1302,C27-L1302,C42 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L1302,C44-L1302,C56 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
ppp_hdlc
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1359,C10-L1359,C33 netdissect_options declared in ./tcpdump/netdissect.h:L161
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1360,C10-L1360,C25 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L1360,C27-L1360,C39 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
handle_ppp
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1458,C12-L1458,C35 netdissect_options declared in ./tcpdump/netdissect.h:L161
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L1459,C12-L1459,C23 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1459,C25-L1459,C40 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L1459,C42-L1459,C54 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
ppp_print
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1520,C11-L1520,C34 netdissect_options declared in ./tcpdump/netdissect.h:L161
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1521,C11-L1521,C26 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
unsigned int TypeKind.ELABORATED arg declared in ./tcpdump/print-ppp.c:L1521,C28-L1521,C40 u_int declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_int.h:L30
ppp_if_print
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1596,C14-L1596,C37 netdissect_options declared in ./tcpdump/netdissect.h:L161
const struct pcap_pkthdr * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1597,C14-L1597,C41 pcap_pkthdr declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/pcap/pcap.h:L245
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1597,C43-L1597,C58 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
ppp_hdlc_if_print
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1665,C19-L1665,C42 netdissect_options declared in ./tcpdump/netdissect.h:L161
const struct pcap_pkthdr * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1666,C19-L1666,C46 pcap_pkthdr declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/pcap/pcap.h:L245
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1666,C48-L1666,C63 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
ppp_bsdos_if_print
struct netdissect_options * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1740,C20-L1740,C43 netdissect_options declared in ./tcpdump/netdissect.h:L161
const struct pcap_pkthdr * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1741,C20-L1741,C47 pcap_pkthdr declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/pcap/pcap.h:L245
const unsigned char * TypeKind.POINTER arg declared in ./tcpdump/print-ppp.c:L1741,C53-L1741,C68 u_char declared in /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/include/sys/_types/_u_char.h:L30
from clang.cindex import Index,Config,CursorKind,TypeKind
# check where is LLVM installed on your machine
Config.set_library_path('/usr/local/Cellar/llvm/16.0.3/lib')
SCRIPT_PATH = './tcpdump/print-ppp.c'
# C99 is a proper compiler for tcpdump, as per docs
index = Index.create()
translation_unit = index.parse(SCRIPT_PATH, args=['-std=c99'])
# filter to nodes in the root script (ignore imported!)
script_node = translation_unit.cursor
all_nodes = script_node.get_children()
all_nodes = filter(lambda c: c.location.file.name==SCRIPT_PATH, all_nodes)
# filter to function nodes
func_nodes = filter(lambda c:c.kind==CursorKind.FUNCTION_DECL, all_nodes)
# print attributes and their types for each function
for fn in func_nodes:
print(fn.spelling)
for arg in fn.get_arguments():
t = arg.type
# handle pointers by describing their pointees
if t.kind == TypeKind.POINTER:
declr = t.get_pointee().get_declaration()
else:
declr = t.get_declaration()
print('\t',
t.get_canonical().spelling,
t.kind,
f'arg declared in {arg.location.file}:L{arg.extent.start.line},C{arg.extent.start.column}-L{arg.extent.end.line},C{arg.extent.end.column}',
f'{declr.spelling} declared in {declr.location.file}:L{declr.location.line}'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment