Merge pull request #2876 from TheBlueMatt/2024-02-2863-doc-nits
[rust-lightning] / ci / check-cfg-flags.py
1 #!/usr/bin/env python3
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.
6 #
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.
9 import sys, glob, re
10
11 def check_feature(feature):
12     if feature == "std":
13         pass
14     elif feature == "no-std":
15         pass
16     elif feature == "ahash":
17         pass
18     elif feature == "hashbrown":
19         pass
20     elif feature == "backtrace":
21         pass
22     elif feature == "grind_signatures":
23         pass
24     elif feature == "unsafe_revoked_tx_signing":
25         pass
26     elif feature == "futures":
27         pass
28     elif feature == "tokio":
29         pass
30     elif feature == "rest-client":
31         pass
32     elif feature == "rpc-client":
33         pass
34     elif feature == "serde":
35         pass
36     elif feature == "esplora-blocking":
37         pass
38     elif feature == "esplora-async":
39         pass
40     elif feature == "async-interface":
41         pass
42     elif feature == "electrum":
43         pass
44     elif feature == "time":
45         pass
46     elif feature == "_test_utils":
47         pass
48     elif feature == "_test_vectors":
49         pass
50     elif feature == "afl":
51         pass
52     elif feature == "honggfuzz":
53         pass
54     elif feature == "libfuzzer_fuzz":
55         pass
56     elif feature == "stdin_fuzz":
57         pass
58     elif feature == "max_level_off":
59         pass
60     elif feature == "max_level_error":
61         pass
62     elif feature == "max_level_warn":
63         pass
64     elif feature == "max_level_info":
65         pass
66     elif feature == "max_level_debug":
67         pass
68     elif feature == "max_level_trace":
69         pass
70     else:
71         print("Bad feature: " + feature)
72         assert False
73
74 def check_target_os(os):
75     if os == "windows":
76         pass
77     else:
78         assert False
79
80 def check_cfg_tag(cfg):
81     if cfg == "fuzzing":
82         pass
83     elif cfg == "test":
84         pass
85     elif cfg == "debug_assertions":
86         pass
87     elif cfg == "c_bindings":
88         pass
89     elif cfg == "ldk_bench":
90         pass
91     elif cfg == "taproot":
92         pass
93     elif cfg == "async_signing":
94         pass
95     elif cfg == "require_route_graph_test":
96         pass
97     else:
98         print("Bad cfg tag: " + cfg)
99         assert False
100
101 def check_cfg_args(cfg):
102     if cfg.startswith("all(") or cfg.startswith("any(") or cfg.startswith("not("):
103         brackets = 1
104         pos = 4
105         while pos < len(cfg):
106             if cfg[pos] == "(":
107                 brackets += 1
108             elif cfg[pos] == ")":
109                 brackets -= 1
110                 if brackets == 0:
111                     check_cfg_args(cfg[4:pos])
112                     if pos + 1 != len(cfg):
113                         assert cfg[pos + 1] == ","
114                         check_cfg_args(cfg[pos + 2:].strip())
115                     return
116             pos += 1
117         assert False
118         assert(cfg.endswith(")"))
119         check_cfg_args(cfg[4:len(cfg)-1])
120     else:
121         parts = [part.strip() for part in cfg.split(",", 1)]
122         if len(parts) > 1:
123             for part in parts:
124                 check_cfg_args(part)
125         elif cfg.startswith("feature") or cfg.startswith("target_os") or cfg.startswith("target_pointer_width"):
126             arg = cfg
127             if cfg.startswith("feature"):
128                 arg = arg[7:].strip()
129             elif cfg.startswith("target_os"):
130                 arg = arg[9:].strip()
131             else:
132                 arg = arg[20:].strip()
133             assert arg.startswith("=")
134             arg = arg[1:].strip()
135             assert arg.startswith("\"")
136             assert arg.endswith("\"")
137             arg = arg[1:len(arg)-1]
138             assert not "\"" in arg
139             if cfg.startswith("feature"):
140                 check_feature(arg)
141             elif cfg.startswith("target_os"):
142                 check_target_os(arg)
143             else:
144                 assert arg == "32" or arg == "64"
145         else:
146             check_cfg_tag(cfg.strip())
147
148 cfg_regex = re.compile("#\[cfg\((.*)\)\]")
149 for path in glob.glob(sys.path[0] + "/../**/*.rs", recursive = True):
150     with open(path, "r") as file:
151         while True:
152             line = file.readline()
153             if not line:
154                 break
155             if "#[cfg(" in line:
156                 if not line.strip().startswith("//"):
157                     cfg_part = cfg_regex.match(line.strip()).group(1)
158                     check_cfg_args(cfg_part)