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 == "hashbrown":
18 elif feature == "backtrace":
20 elif feature == "grind_signatures":
22 elif feature == "unsafe_revoked_tx_signing":
24 elif feature == "futures":
26 elif feature == "tokio":
28 elif feature == "rest-client":
30 elif feature == "rpc-client":
32 elif feature == "serde":
34 elif feature == "esplora-blocking":
36 elif feature == "esplora-async":
38 elif feature == "async-interface":
40 elif feature == "electrum":
42 elif feature == "time":
44 elif feature == "_test_utils":
46 elif feature == "_test_vectors":
48 elif feature == "afl":
50 elif feature == "honggfuzz":
52 elif feature == "libfuzzer_fuzz":
54 elif feature == "stdin_fuzz":
56 elif feature == "max_level_off":
58 elif feature == "max_level_error":
60 elif feature == "max_level_warn":
62 elif feature == "max_level_info":
64 elif feature == "max_level_debug":
66 elif feature == "max_level_trace":
69 print("Bad feature: " + feature)
72 def check_target_os(os):
78 def check_cfg_tag(cfg):
83 elif cfg == "debug_assertions":
85 elif cfg == "c_bindings":
87 elif cfg == "ldk_bench":
89 elif cfg == "taproot":
91 elif cfg == "async_signing":
93 elif cfg == "require_route_graph_test":
96 print("Bad cfg tag: " + cfg)
99 def check_cfg_args(cfg):
100 if cfg.startswith("all(") or cfg.startswith("any(") or cfg.startswith("not("):
103 while pos < len(cfg):
106 elif cfg[pos] == ")":
109 check_cfg_args(cfg[4:pos])
110 if pos + 1 != len(cfg):
111 assert cfg[pos + 1] == ","
112 check_cfg_args(cfg[pos + 2:].strip())
116 assert(cfg.endswith(")"))
117 check_cfg_args(cfg[4:len(cfg)-1])
119 parts = [part.strip() for part in cfg.split(",", 1)]
123 elif cfg.startswith("feature") or cfg.startswith("target_os") or cfg.startswith("target_pointer_width"):
125 if cfg.startswith("feature"):
126 arg = arg[7:].strip()
127 elif cfg.startswith("target_os"):
128 arg = arg[9:].strip()
130 arg = arg[20:].strip()
131 assert arg.startswith("=")
132 arg = arg[1:].strip()
133 assert arg.startswith("\"")
134 assert arg.endswith("\"")
135 arg = arg[1:len(arg)-1]
136 assert not "\"" in arg
137 if cfg.startswith("feature"):
139 elif cfg.startswith("target_os"):
142 assert arg == "32" or arg == "64"
144 check_cfg_tag(cfg.strip())
146 cfg_regex = re.compile("#\[cfg\((.*)\)\]")
147 for path in glob.glob(sys.path[0] + "/../**/*.rs", recursive = True):
148 with open(path, "r") as file:
150 line = file.readline()
154 if not line.strip().startswith("//"):
155 cfg_part = cfg_regex.match(line.strip()).group(1)
156 check_cfg_args(cfg_part)