5
5
import getopt
6
6
import sys
7
7
from os import path
8
+ import tempfile
9
+ import subprocess
8
10
9
11
ENCODING = "utf-8"
10
12
SOURCE_FILE_PATTERN = r"\b\d+_error\b"
11
13
14
+ # default target branch for pull requests
15
+ PR_TARGET_DEFAULT_BRANCH = 'origin/develop'
16
+
17
+ # Error IDs that were previously used but have been removed from the code. They should not be reused.
18
+ # For each ID, a file and a removal commit are provided.
19
+ REMOVED_IDS = {
20
+ "2314" , # liblangutil/ParserBase.cpp, 6965f199fd11cada51c5a97ceb72cc5e14534a17
21
+ "6635" , # liblangutil/ParserBase.cpp, 6965f199fd11cada51c5a97ceb72cc5e14534a17
22
+ # liblangutil/ParserBase.cpp, 9adbced98e49588bcc29e181f2560c2eadaa22ac
23
+ "1957" , # liblangutil/ParserBase.cpp, 6965f199fd11cada51c5a97ceb72cc5e14534a17
24
+ # liblangutil/ParserBase.cpp, 9adbced98e49588bcc29e181f2560c2eadaa22ac
25
+ "5883" , # libsolidity/analysis/OverrideChecker.cpp, 1d5350e32f04e991dbfc8fca402cbc8c7930e85d
26
+ "3881" , # libsolidity/analysis/ReferencesResolver.cpp, 66a8c7d1ab5b744736d1355ba46c9e1d5f090ac8
27
+ "6546" , # libsolidity/analysis/ReferencesResolver.cpp, 66a8c7d1ab5b744736d1355ba46c9e1d5f090ac8
28
+ "8532" , # libsolidity/analysis/ReferencesResolver.cpp, 66a8c7d1ab5b744736d1355ba46c9e1d5f090ac8
29
+ # libsolidity/analysis/DocStringAnalyser.cpp, cb5bfc7436ca11bdfa2f0543a3611079e3f4cbee
30
+ # libsolidity/analysis/DocStringAnalyser.cpp, d7899a31afd8e1132ce362c522e8a47c17fd3832
31
+ "1093" , # libsolidity/analysis/TypeChecker.cpp, 936ea6f950ffd9b4d4bfaa9cd5ae949be0df58fd
32
+ # libsolidity/formal/SMTEncoder.cpp, 11a7763f492411d1fb77841caa48fd918dca64d4
33
+ "6706" , # libsolidity/analysis/TypeChecker.cpp, 936ea6f950ffd9b4d4bfaa9cd5ae949be0df58fd
34
+ "4035" , # libsolidity/analysis/TypeChecker.cpp, 936ea6f950ffd9b4d4bfaa9cd5ae949be0df58fd
35
+ "7569" , # libyul/AsmAnalysis.cpp, 011f8a462d718f3034e72025b1d3d3916faa474d
36
+ "9595" , # libyul/AsmAnalysis.cpp, 011f8a462d718f3034e72025b1d3d3916faa474d
37
+ "9222" , # libsolidity/parsing/DocStringParser.cpp, acd42a08c1b1fe0f69dbcd9c97ac75cc0b1b352b
38
+ "7079" , # libyul/AsmAnalysis.cpp, d211a45aa4844821e03c38b1abcecb05dafd27fe
39
+ "4316" , # libyul/AsmAnalysis.cpp, 67ebb206eab4863d47f67ed219ee4dbaa985819f
40
+ "2450" , # libyul/AsmAnalysis.cpp, 9820df58abfe918c5f92c6d4fa066e2da5c3bf35
41
+ "7816" , # libsolidity/analysis/DocStringAnalyser.cpp, cb5bfc7436ca11bdfa2f0543a3611079e3f4cbee
42
+ # libsolidity/analysis/DocStringAnalyser.cpp, d7899a31afd8e1132ce362c522e8a47c17fd3832
43
+ "5380" , # libsolidity/analysis/TypeChecker.cpp, d41eaeba5686828b85279057f3a7da8be0f9a8f9
44
+ "3312" , # libsolidity/analysis/TypeChecker.cpp, d41eaeba5686828b85279057f3a7da8be0f9a8f9
45
+ "3442" , # libsolidity/analysis/TypeChecker.cpp, d41eaeba5686828b85279057f3a7da8be0f9a8f9
46
+ "9239" , # libsolidity/analysis/TypeChecker.cpp, da36400576304bc6daa924d40684b6655914e4a5
47
+ "9054" , # libsolidity/analysis/TypeChecker.cpp, da36400576304bc6daa924d40684b6655914e4a5
48
+ "9155" , # libsolidity/analysis/ReferencesResolver.cpp, fc2e9ec2ff4ca397f0aa743cc44b40352894fec2
49
+ "7698" , # libsolidity/parsing/Parser.cpp, f73b25bb78e64a57e30ade9c7982d9c2375a3b7b
50
+ "6963" , # libsolidity/analysis/TypeChecker.cpp, 93c792c696b2929da2b6e2eea31a4c5f60e9188c
51
+ "3478" , # libsolidity/analysis/TypeChecker.cpp, 93c792c696b2929da2b6e2eea31a4c5f60e9188c
52
+ "7439" , # libsolidity/parsing/Parser.cpp, 93c792c696b2929da2b6e2eea31a4c5f60e9188c
53
+ "4626" , # libsolidity/analysis/TypeChecker.cpp, 93c792c696b2929da2b6e2eea31a4c5f60e9188c
54
+ "7059" , # libsolidity/parsing/Parser.cpp, 93c792c696b2929da2b6e2eea31a4c5f60e9188c
55
+ "1719" , # libsolidity/analysis/TypeChecker.cpp, 93c792c696b2929da2b6e2eea31a4c5f60e9188c
56
+ "6983" , # libsolidity/analysis/TypeChecker.cpp, 93c792c696b2929da2b6e2eea31a4c5f60e9188c
57
+ "9843" , # libsolidity/analysis/DocStringTagParser.cpp, 8b7567f963e00ef15fd5ae97c310064658612ae9
58
+ "1147" , # libsolidity/formal/CHC.cpp, 694ec92688617a876d1f1d97beba6ca1b52e6906
59
+ "3672" , # libyul/AsmAnalysis.cpp, 291c00c3decd89f3338777ce8836a974b216cd2a
60
+ "6493" , # libsolidity/analysis/DeclarationTypeChecker.cpp, 5394435bea4e553a86f872b6b2512c50bdef1628
61
+ "4794" , # libsolidity/analysis/ReferencesResolver.cpp, ffdb0e37ff788afdf54ea1224a8c8184043322f9
62
+ "9440" , # libsolidity/analysis/DocStringTagParser.cpp, 0b45168bcbf5ed5874668f21a3c3d1476cab5620
63
+ "3299" , # libsolidity/analysis/SyntaxChecker.cpp, ad311fae1902cf3159834de6d494b46386d0cede
64
+ "1054" , # liblangutil/ParserBase.cpp, 0ee4a85a841f6494c16a93bd60f3a9ec1a7f2c6e
65
+ "9118" , # libsolidity/formal/SMTEncoder.cpp, e61b731647b88fe97ee648c4035ecb0fe210eaf0
66
+ "8182" , # libsolidity/formal/SMTEncoder.cpp, bd0c46abf55cd6d3beae62bd1618ad8463b4e88b
67
+ "6191" , # libsolidity/formal/SMTEncoder.cpp, bd0c46abf55cd6d3beae62bd1618ad8463b4e88b
68
+ "9056" , # libsolidity/formal/SMTEncoder.cpp, bd0c46abf55cd6d3beae62bd1618ad8463b4e88b
69
+ "2683" , # libsolidity/formal/SMTEncoder.cpp, bd0c46abf55cd6d3beae62bd1618ad8463b4e88b
70
+ "9599" , # libsolidity/formal/SMTEncoder.cpp, bd0c46abf55cd6d3beae62bd1618ad8463b4e88b
71
+ "3408" , # libsolidity/analysis/TypeChecker.cpp, e7a6534d4f34e1333bc34f17031997e495fc7f90
72
+ "2332" , # libsolidity/analysis/TypeChecker.cpp, e7a6534d4f34e1333bc34f17031997e495fc7f90
73
+ "9149" , # libsolidity/formal/SMTEncoder.cpp, 79f550dba94d5831be264745012cf0433556d8fb
74
+ "7989" , # libsolidity/formal/SMTEncoder.cpp, df8c6d94e30b8c66785e8c77c47a3b080270d194
75
+ "2923" , # libsolidity/formal/SMTEncoder.cpp, c8cc73c80c994f84563ac474a455521d38fbcc8f
76
+ "5084" , # libsolidity/formal/SMTEncoder.cpp, fedbea46cda1d67f00212e2d3ca51e34b8061076
77
+ "2177" , # libsolidity/formal/SMTEncoder.cpp, e23d8f559370f5aa740782d22048a1d16fd6e4aa
78
+ "1665" , # libsolidity/analysis/DeclarationTypeChecker.cpp, 3f14c904b08a358fe1b039519445439eb37d8f37
79
+ "1273" , # libsolidity/analysis/TypeChecker.cpp, 3f14c904b08a358fe1b039519445439eb37d8f37
80
+ "6084" , # libsolidity/formal/BMC.cpp, 07427c798c48061c608b1fded06c12bd296fa563
81
+ "2370" , # libsolidity/analysis/TypeChecker.cpp, 2665eaa4fac3d249fa3d892487ed7b5cfd562e49
82
+ "4639" , # libsolidity/formal/SMTEncoder.cpp, 80d743426f7c7da5e55423f6a3223145bf75b559
83
+ "6151" , # libsolidity/analysis/TypeChecker.cpp, fda352094f0e9f586004dda48daef9788068f523
84
+ "3978" , # libsolidity/analysis/TypeChecker.cpp, fda352094f0e9f586004dda48daef9788068f523
85
+ "3625" , # libsolidity/analysis/TypeChecker.cpp, 52c49aebe80ada117f3244e73eb5e643bbbfafe1
86
+ "7885" , # libsolidity/formal/SMTEncoder.cpp, fa561dbd0e839ce5e198059a0eff139e5860ea89
87
+ "3876" , # libsolidity/formal/SMTEncoder.cpp, fa561dbd0e839ce5e198059a0eff139e5860ea89
88
+ "9011" , # libsolidity/formal/SMTEncoder.cpp, fa561dbd0e839ce5e198059a0eff139e5860ea89
89
+ "3263" , # libsolidity/formal/SMTEncoder.cpp, fa561dbd0e839ce5e198059a0eff139e5860ea89
90
+ "7186" , # libsolidity/formal/SMTEncoder.cpp, fa561dbd0e839ce5e198059a0eff139e5860ea89
91
+ "3682" , # libsolidity/formal/SMTEncoder.cpp, fa561dbd0e839ce5e198059a0eff139e5860ea89
92
+ "1950" , # libsolidity/formal/SMTEncoder.cpp, fa561dbd0e839ce5e198059a0eff139e5860ea89
93
+ "9551" , # libsolidity/formal/SMTEncoder.cpp, fa561dbd0e839ce5e198059a0eff139e5860ea89
94
+ "7645" , # libsolidity/formal/SMTEncoder.cpp, 0f3924186ea02f3e335c05940dd8c16d1fb783d1
95
+ "1123" , # libsolidity/analysis/TypeChecker.cpp, fd9050614a6089168bd4565f4b49c64d04a0ef71
96
+ "1220" , # libsolidity/analysis/TypeChecker.cpp, fd9050614a6089168bd4565f4b49c64d04a0ef71
97
+ "7653" , # libsolidity/analysis/TypeChecker.cpp, fd9050614a6089168bd4565f4b49c64d04a0ef71
98
+ "9390" , # libsolidity/analysis/TypeChecker.cpp, fd9050614a6089168bd4565f4b49c64d04a0ef71
99
+ "2657" , # libyul/AsmAnalysis.cpp, ded5d721d2c97170905a536b1f996b79045c84d1
100
+ "5073" , # libsolidity/analysis/NameAndTypeResolver.cpp, efe319998110068b8bd3152230a6ff5d0a2339f1
101
+ "9085" , # libsolidity/analysis/TypeChecker.cpp, f766700000e1c50f400581fc647f71daef2bb588
102
+ "8261" , # libsolidity/analysis/NameAndTypeResolver.cpp, 78a097a012c8abd42b29b4dd2d38bf16a5b7cdc1
103
+ "4224" , # libsolidity/analysis/TypeChecker.cpp, e590a99f399e0be33dfba0ab63065d7f17e1e870
104
+ "4579" , # libsolidity/analysis/TypeChecker.cpp, e590a99f399e0be33dfba0ab63065d7f17e1e870
105
+ "3772" , # libyul/AsmParser.cpp, f04adde6641b09c8744fb1576eda87657b4d9a2d
106
+ "2837" , # libsolidity/parsing/Parser.cpp, 39e3da19053fb4d892e1aba2de9ba7a5c225f68f
107
+ "6660" , # libsolidity/formal/SMTEncoder.cpp, 4e343590635f98f7c1d0973e82f9bafc67e03466
108
+ "8195" , # libsolidity/formal/SMTEncoder.cpp, 095d33714086880ded0f2ce2c7a8e4493e379613
109
+ "5256" , # libsolidity/analysis/DocStringTagParser.cpp, 354f9d101530d38faaeaaf12382bf95e053e21e3
110
+ "9609" , # libsolidity/interface/CompilerStack.cpp, 0e8e4eacd59194f66571e136e635e1921af392e5
111
+ "5798" , # libyul/AsmParser.cpp, e5ab68ed71460ad088788ac0bc6d94c16832bcfe
112
+ "3530" , # libsolidity/analysis/DeclarationTypeChecker.cpp, 0004ad876451e28de1b3aa031ab56d08d2cdb3b1
113
+ "4130" , # libsolidity/analysis/ImmutableValidator.cpp, dad2bf64723cc83f0168bae0310714db01983a50
114
+ "6672" , # libsolidity/analysis/ImmutableValidator.cpp, dad2bf64723cc83f0168bae0310714db01983a50
115
+ "7733" , # libsolidity/analysis/ImmutableValidator.cpp, dad2bf64723cc83f0168bae0310714db01983a50
116
+ "2658" , # libsolidity/analysis/ImmutableValidator.cpp, dad2bf64723cc83f0168bae0310714db01983a50
117
+ # libsolidity/experimental/analysis/TypeInference.cpp, fb99132474c177fc861305feeafd43b9cbb93b66
118
+ "2718" , # libsolidity/analysis/ImmutableValidator.cpp, dad2bf64723cc83f0168bae0310714db01983a50
119
+ "4599" , # libsolidity/analysis/ImmutableValidator.cpp, dad2bf64723cc83f0168bae0310714db01983a50
120
+ "1574" , # libsolidity/analysis/ImmutableValidator.cpp, dad2bf64723cc83f0168bae0310714db01983a50
121
+ "3969" , # libsolidity/analysis/ImmutableValidator.cpp, dad2bf64723cc83f0168bae0310714db01983a50
122
+ "7484" , # libsolidity/analysis/ImmutableValidator.cpp, dad2bf64723cc83f0168bae0310714db01983a50
123
+ "3796" , # liblangutil/ParserBase.cpp, 9adbced98e49588bcc29e181f2560c2eadaa22ac
124
+ "3347" , # liblangutil/ParserBase.cpp, 9adbced98e49588bcc29e181f2560c2eadaa22ac
125
+ "3997" , # libsolidity/analysis/SyntaxChecker.cpp, 9adbced98e49588bcc29e181f2560c2eadaa22ac
126
+ "6156" , # libsolidity/formal/SMTEncoder.cpp, 78eb37d2596c9416b3fac9881414bf7e9c524de8
127
+ "6756" , # libsolidity/formal/SMTEncoder.cpp, 78eb37d2596c9416b3fac9881414bf7e9c524de8
128
+ "6547" , # libsolidity/experimental/analysis/Analysis.cpp, 194b114664c7daebc2ff68af3c573272f5d28913
129
+ "2138" , # libsolidity/experimental/analysis/TypeInference.cpp, fb99132474c177fc861305feeafd43b9cbb93b66
130
+ "9817" , # libsolidity/experimental/analysis/TypeInference.cpp, fb99132474c177fc861305feeafd43b9cbb93b66
131
+ "4686" , # libsolidity/experimental/analysis/TypeInference.cpp, fce70ef0e34b7e6cc8d22b0fc08784758503e8d7
132
+ "6715" , # libsolidity/analysis/TypeChecker.cpp, c5685e70544c86f25d87b93048a8bffa963177c6
133
+ "3781" , # libyul/AsmAnalysis.cpp, 0d0f2771654b14ee0e8d317a5eb22dbff8eab332
134
+ "9547" , # libyul/AsmAnalysis.cpp, 0d0f2771654b14ee0e8d317a5eb22dbff8eab332
135
+ "1733" , # libyul/AsmAnalysis.cpp, 0d0f2771654b14ee0e8d317a5eb22dbff8eab332
136
+ "3947" , # libyul/AsmAnalysis.cpp, 0d0f2771654b14ee0e8d317a5eb22dbff8eab332
137
+ "5170" , # libyul/AsmAnalysis.cpp, 0d0f2771654b14ee0e8d317a5eb22dbff8eab332
138
+ "5622" , # libsolidity/formal/BMC.cpp, eb56b4bfe6fe9996342b27d4fc6a4001725d4446
139
+ }
140
+
12
141
13
142
def read_file (file_name ):
14
143
content = None
@@ -98,13 +227,12 @@ def fix_ids_in_source_file(file_name, id_to_count, available_ids):
98
227
print (f"Fixed file: { file_name } " )
99
228
100
229
101
- def fix_ids_in_source_files (file_names , id_to_count ):
230
+ def fix_ids_in_source_files (file_names , id_to_count , available_ids ):
102
231
"""
103
232
Fixes ids in given source files;
104
233
id_to_count contains number of appearances of every id in sources
105
234
"""
106
235
107
- available_ids = {str (error_id ) for error_id in range (1000 , 10000 )} - id_to_count .keys ()
108
236
for file_name in file_names :
109
237
fix_ids_in_source_file (file_name , id_to_count , available_ids )
110
238
@@ -123,6 +251,14 @@ def find_files(top_dir, sub_dirs, extensions):
123
251
return source_file_names
124
252
125
253
254
+ def find_source_files (search_dir ):
255
+ return find_files (
256
+ search_dir ,
257
+ ["libevmasm" , "liblangutil" , "libsolc" , "libsolidity" , "libsolutil" , "libyul" , "solc" ],
258
+ [".h" , ".cpp" ]
259
+ )
260
+
261
+
126
262
def find_ids_in_test_file (file_name ):
127
263
source = read_file (file_name )
128
264
pattern = r"^// (.*Error|Warning|Info) \d\d\d\d:"
@@ -345,14 +481,77 @@ def examine_id_coverage(top_dir, source_id_to_file_names, new_ids_only=False):
345
481
346
482
return True
347
483
484
+ def find_ids_in_branch (branch_commit ):
485
+ """Returns a set of error codes present in the given branch/commit"""
486
+
487
+ with tempfile .TemporaryDirectory () as tmp_dir :
488
+ # Create a temporary worktree to examine the target branch
489
+ subprocess .run (
490
+ ['git' , 'worktree' , 'add' , tmp_dir , branch_commit ],
491
+ check = True , stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL
492
+ )
493
+
494
+ source_file_names = find_source_files (tmp_dir )
495
+ ids = find_ids_in_source_files (source_file_names ).keys ()
496
+
497
+ assert len (ids ), "Error IDs weren't found"
498
+
499
+ # Clean up the worktree
500
+ subprocess .run (
501
+ ['git' , 'worktree' , 'remove' , '--force' , tmp_dir ],
502
+ check = True , stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL
503
+ )
504
+
505
+ return set (ids )
506
+
507
+
508
+ def check_removed_error_codes_between_branches (current_ids ):
509
+ """
510
+ Checks if any error codes were removed between target branch and current branch
511
+ but not added to REMOVED_IDS. Returns True if all removed codes are properly tracked.
512
+ """
513
+
514
+ # Get the merge base (common ancestor) with the target branch
515
+ # If PR_TARGET_BRANCH env var is set (job chk_errorcodes from CI), use that, otherwise use origin/develop
516
+ target_branch = os .getenv ('PR_TARGET_BRANCH' , PR_TARGET_DEFAULT_BRANCH )
517
+ try :
518
+ merge_base = subprocess .check_output (
519
+ ['git' , 'merge-base' , target_branch , 'HEAD' ],
520
+ universal_newlines = True
521
+ ).strip ()
522
+ except subprocess .CalledProcessError :
523
+ print (f"Error: Could not find merge base with { target_branch } " )
524
+ return False
525
+
526
+ # Get error codes from target branch
527
+ try :
528
+ target_ids = find_ids_in_branch (merge_base )
529
+ except subprocess .CalledProcessError as e :
530
+ print (f"Error accessing target branch: { e } " )
531
+ return False
532
+
533
+ # Find removed error codes (present in target but not in current and not in REMOVED_IDS)
534
+ removed_ids = target_ids - set (current_ids ) - REMOVED_IDS
535
+
536
+ if len (removed_ids ) > 0 :
537
+ print (f"Error: The following error codes were removed since { target_branch } :{ merge_base } but not added to REMOVED_IDS:" )
538
+ print_ids (removed_ids )
539
+ print ("\n Please add these codes to the REMOVED_IDS set in scripts/error_codes.py" )
540
+ return False
541
+ else :
542
+ print (f"No removed IDs found since { merge_base } " )
543
+
544
+ return True
545
+
348
546
349
547
def main (argv ):
350
548
check = False
351
549
fix = False
352
550
no_confirm = False
353
551
examine_coverage = False
354
552
next_id = False
355
- opts , _args = getopt .getopt (argv , "" , ["check" , "fix" , "no-confirm" , "examine-coverage" , "next" ])
553
+ check_removed = False
554
+ opts , _args = getopt .getopt (argv , "" , ["check" , "fix" , "no-confirm" , "examine-coverage" , "next" , "check-removed" ])
356
555
357
556
for opt , _arg in opts :
358
557
if opt == "--check" :
@@ -365,20 +564,22 @@ def main(argv):
365
564
examine_coverage = True
366
565
elif opt == "--next" :
367
566
next_id = True
567
+ elif opt == "--check-removed" :
568
+ check_removed = True
368
569
369
- if [check , fix , examine_coverage , next_id ].count (True ) != 1 :
370
- print ("usage: python error_codes.py --check | --fix [--no-confirm] | --examine-coverage | --next" )
570
+ if [check , fix , examine_coverage , next_id , check_removed ].count (True ) != 1 :
571
+ print ("usage: python error_codes.py --check | --fix [--no-confirm] | --examine-coverage | --next | --check-removed " )
371
572
sys .exit (1 )
372
573
373
574
cwd = os .getcwd ()
374
575
375
- source_file_names = find_files (
376
- cwd ,
377
- ["libevmasm" , "liblangutil" , "libsolc" , "libsolidity" , "libsolutil" , "libyul" , "solc" ],
378
- [".h" , ".cpp" ]
379
- )
576
+ source_file_names = find_source_files (cwd )
380
577
source_id_to_file_names = find_ids_in_source_files (source_file_names )
381
578
579
+ if check_removed :
580
+ res = 0 if check_removed_error_codes_between_branches (source_id_to_file_names .keys ()) else 1
581
+ sys .exit (res )
582
+
382
583
ok = True
383
584
for error_id in sorted (source_id_to_file_names ):
384
585
if len (error_id ) != 4 :
@@ -402,11 +603,12 @@ def main(argv):
402
603
403
604
random .seed ()
404
605
606
+ available_ids = {str (error_id ) for error_id in range (1000 , 10000 )} - source_id_to_file_names .keys () - REMOVED_IDS
607
+
405
608
if next_id :
406
609
if not ok :
407
610
print ("Incorrect IDs have to be fixed before applying --next" )
408
611
sys .exit (1 )
409
- available_ids = {str (error_id ) for error_id in range (1000 , 10000 )} - source_id_to_file_names .keys ()
410
612
next_id = get_next_id (available_ids )
411
613
print (f"Next ID: { next_id } " )
412
614
sys .exit (0 )
@@ -434,7 +636,7 @@ def main(argv):
434
636
# number of appearances for every id
435
637
source_id_to_count = { error_id : len (file_names ) for error_id , file_names in source_id_to_file_names .items () }
436
638
437
- fix_ids_in_source_files (source_file_names , source_id_to_count )
639
+ fix_ids_in_source_files (source_file_names , source_id_to_count , available_ids )
438
640
print ("Fixing completed" )
439
641
sys .exit (2 )
440
642
0 commit comments