forked from lightningdevkit/rust-lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-cfg-flags.py
More file actions
executable file
·162 lines (157 loc) · 4.64 KB
/
check-cfg-flags.py
File metadata and controls
executable file
·162 lines (157 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python3
# Rust is fairly relaxed in checking the validity of arguments passed to #[cfg].
# While it should probably be more strict when checking features, it cannot be
# strict when checking loose cfg tags, because those can be anything and are
# simply passed to rustc via unconstrained arguments.
#
# Thus, we do it for rustc manually, but scanning all our source and checking
# that all our cfg tags match a known cfg tag.
import sys, glob, re
def check_feature(feature):
if feature == "std":
pass
elif feature == "no-std":
pass
elif feature == "possiblyrandom":
pass
elif feature == "getrandom":
pass
elif feature == "hashbrown":
pass
elif feature == "backtrace":
pass
elif feature == "grind_signatures":
pass
elif feature == "unsafe_revoked_tx_signing":
pass
elif feature == "futures":
pass
elif feature == "tokio":
pass
elif feature == "rest-client":
pass
elif feature == "rpc-client":
pass
elif feature == "serde":
pass
elif feature == "esplora-blocking":
pass
elif feature == "esplora-async":
pass
elif feature == "async-interface":
pass
elif feature == "electrum":
pass
elif feature == "time":
pass
elif feature == "_test_utils":
pass
elif feature == "_test_vectors":
pass
elif feature == "afl":
pass
elif feature == "honggfuzz":
pass
elif feature == "libfuzzer_fuzz":
pass
elif feature == "stdin_fuzz":
pass
elif feature == "max_level_off":
pass
elif feature == "max_level_error":
pass
elif feature == "max_level_warn":
pass
elif feature == "max_level_info":
pass
elif feature == "max_level_debug":
pass
elif feature == "max_level_trace":
pass
else:
print("Bad feature: " + feature)
assert False
def check_target_os(os):
if os == "windows":
pass
else:
assert False
def check_cfg_tag(cfg):
if cfg == "fuzzing":
pass
elif cfg == "test":
pass
elif cfg == "debug_assertions":
pass
elif cfg == "c_bindings":
pass
elif cfg == "ldk_bench":
pass
elif cfg == "taproot":
pass
elif cfg == "async_signing":
pass
elif cfg == "require_route_graph_test":
pass
elif cfg == "dual_funding":
pass
else:
print("Bad cfg tag: " + cfg)
assert False
def check_cfg_args(cfg):
if cfg.startswith("all(") or cfg.startswith("any(") or cfg.startswith("not("):
brackets = 1
pos = 4
while pos < len(cfg):
if cfg[pos] == "(":
brackets += 1
elif cfg[pos] == ")":
brackets -= 1
if brackets == 0:
check_cfg_args(cfg[4:pos])
if pos + 1 != len(cfg):
assert cfg[pos + 1] == ","
check_cfg_args(cfg[pos + 2:].strip())
return
pos += 1
assert False
assert(cfg.endswith(")"))
check_cfg_args(cfg[4:len(cfg)-1])
else:
parts = [part.strip() for part in cfg.split(",", 1)]
if len(parts) > 1:
for part in parts:
check_cfg_args(part)
elif cfg.startswith("feature") or cfg.startswith("target_os") or cfg.startswith("target_pointer_width"):
arg = cfg
if cfg.startswith("feature"):
arg = arg[7:].strip()
elif cfg.startswith("target_os"):
arg = arg[9:].strip()
else:
arg = arg[20:].strip()
assert arg.startswith("=")
arg = arg[1:].strip()
assert arg.startswith("\"")
assert arg.endswith("\"")
arg = arg[1:len(arg)-1]
assert not "\"" in arg
if cfg.startswith("feature"):
check_feature(arg)
elif cfg.startswith("target_os"):
check_target_os(arg)
else:
assert arg == "32" or arg == "64"
else:
check_cfg_tag(cfg.strip())
cfg_regex = re.compile("#\[cfg\((.*)\)\]")
for path in glob.glob(sys.path[0] + "/../**/*.rs", recursive = True):
with open(path, "r") as file:
while True:
line = file.readline()
if not line:
break
if "#[cfg(" in line:
if not line.strip().startswith("//"):
cfg_part = cfg_regex.match(line.strip()).group(1)
check_cfg_args(cfg_part)