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 == "ahash":
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":
100 print("Bad cfg tag: " + cfg)
103 def check_cfg_args(cfg):
104 if cfg.startswith("all(") or cfg.startswith("any(") or cfg.startswith("not("):
107 while pos < len(cfg):
110 elif cfg[pos] == ")":
113 check_cfg_args(cfg[4:pos])
114 if pos + 1 != len(cfg):
115 assert cfg[pos + 1] == ","
116 check_cfg_args(cfg[pos + 2:].strip())
120 assert(cfg.endswith(")"))
121 check_cfg_args(cfg[4:len(cfg)-1])
123 parts = [part.strip() for part in cfg.split(",", 1)]
127 elif cfg.startswith("feature") or cfg.startswith("target_os") or cfg.startswith("target_pointer_width"):
129 if cfg.startswith("feature"):
130 arg = arg[7:].strip()
131 elif cfg.startswith("target_os"):
132 arg = arg[9:].strip()
134 arg = arg[20:].strip()
135 assert arg.startswith("=")
136 arg = arg[1:].strip()
137 assert arg.startswith("\"")
138 assert arg.endswith("\"")
139 arg = arg[1:len(arg)-1]
140 assert not "\"" in arg
141 if cfg.startswith("feature"):
143 elif cfg.startswith("target_os"):
146 assert arg == "32" or arg == "64"
148 check_cfg_tag(cfg.strip())
150 cfg_regex = re.compile("#\[cfg\((.*)\)\]")
151 for path in glob.glob(sys.path[0] + "/../**/*.rs", recursive = True):
152 with open(path, "r") as file:
154 line = file.readline()
158 if not line.strip().startswith("//"):
159 cfg_part = cfg_regex.match(line.strip()).group(1)
160 check_cfg_args(cfg_part)