2 # Rust is fairly relaxed in checking the validity of arguments passed to #[cfg].
3 # While it should probably be more strict when checking features, it cannot be
4 # strict when checking loose cfg tags, because those can be anything and are
5 # simply passed to rustc via unconstrained arguments.
7 # Thus, we do it for rustc manually, but scanning all our source and checking
8 # that all our cfg tags match a known cfg tag.
11 def check_feature(feature):
14 elif feature == "no-std":
16 elif feature == "possiblyrandom":
18 elif feature == "getrandom":
20 elif feature == "hashbrown":
22 elif feature == "backtrace":
24 elif feature == "grind_signatures":
26 elif feature == "unsafe_revoked_tx_signing":
28 elif feature == "futures":
30 elif feature == "tokio":
32 elif feature == "rest-client":
34 elif feature == "rpc-client":
36 elif feature == "serde":
38 elif feature == "esplora-blocking":
40 elif feature == "esplora-async":
42 elif feature == "async-interface":
44 elif feature == "electrum":
46 elif feature == "time":
48 elif feature == "_test_utils":
50 elif feature == "_test_vectors":
52 elif feature == "afl":
54 elif feature == "honggfuzz":
56 elif feature == "libfuzzer_fuzz":
58 elif feature == "stdin_fuzz":
60 elif feature == "max_level_off":
62 elif feature == "max_level_error":
64 elif feature == "max_level_warn":
66 elif feature == "max_level_info":
68 elif feature == "max_level_debug":
70 elif feature == "max_level_trace":
73 print("Bad feature: " + feature)
76 def check_target_os(os):
82 def check_cfg_tag(cfg):
87 elif cfg == "debug_assertions":
89 elif cfg == "c_bindings":
91 elif cfg == "ldk_bench":
93 elif cfg == "taproot":
95 elif cfg == "async_signing":
97 elif cfg == "require_route_graph_test":
99 elif cfg == "dual_funding":
102 print("Bad cfg tag: " + cfg)
105 def check_cfg_args(cfg):
106 if cfg.startswith("all(") or cfg.startswith("any(") or cfg.startswith("not("):
109 while pos < len(cfg):
112 elif cfg[pos] == ")":
115 check_cfg_args(cfg[4:pos])
116 if pos + 1 != len(cfg):
117 assert cfg[pos + 1] == ","
118 check_cfg_args(cfg[pos + 2:].strip())
122 assert(cfg.endswith(")"))
123 check_cfg_args(cfg[4:len(cfg)-1])
125 parts = [part.strip() for part in cfg.split(",", 1)]
129 elif cfg.startswith("feature") or cfg.startswith("target_os") or cfg.startswith("target_pointer_width"):
131 if cfg.startswith("feature"):
132 arg = arg[7:].strip()
133 elif cfg.startswith("target_os"):
134 arg = arg[9:].strip()
136 arg = arg[20:].strip()
137 assert arg.startswith("=")
138 arg = arg[1:].strip()
139 assert arg.startswith("\"")
140 assert arg.endswith("\"")
141 arg = arg[1:len(arg)-1]
142 assert not "\"" in arg
143 if cfg.startswith("feature"):
145 elif cfg.startswith("target_os"):
148 assert arg == "32" or arg == "64"
150 check_cfg_tag(cfg.strip())
152 cfg_regex = re.compile("#\[cfg\((.*)\)\]")
153 for path in glob.glob(sys.path[0] + "/../**/*.rs", recursive = True):
154 with open(path, "r") as file:
156 line = file.readline()
160 if not line.strip().startswith("//"):
161 cfg_part = cfg_regex.match(line.strip()).group(1)
162 check_cfg_args(cfg_part)