-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcompiler_explanations.py
1078 lines (1041 loc) · 31.2 KB
/
compiler_explanations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import copy, math, re, sys
import colors
from util import explanation_url
BACKSLASH = "\\"
def get_explanation(message, colorize_output):
for e in explanations:
text = e.get(message, colorize_output)
if text:
e = copy.deepcopy(e)
e.text = text
return e
return None
#
# label - unique identifier, used as file name
#
# regex - if set, matched against text, if match fails no explanation is returned
#
# precondition - if callable, it is called with message and regex match results as arguments
# if value returned is False, no explanation is returned
# if value returned is non-empty string and explanation is not set
# the value is returned as explanation
#
# explanation - if explanation is callable, it is called with message and regex match results as arguments
# and value returned as explanation
# if explanation is a string, it is evaluated as f-string with the fields from the message object
# available as local variables,
# Note, this includes the highlighted_word and underlined_word in the compiler message (if any)
# The result of the evluation returned as the explanation
#
# show_note - print the note (if any) on a clang warning
#
# no_following_explanations - if True, don't print explanations after this one
# use where confusing parasitic errors likely
#
# reproduce - C program which should yield explanation
class Explanation:
def __init__(
self,
label=None,
precondition=None,
regex=None,
explanation=None,
no_following_explanations=False,
show_note=True,
reproduce="",
long_explanation=False,
long_explanation_url="",
):
self.label = label if label else re.sub(r"\W+", "_", regex).strip("_")
self.precondition = precondition
self.regex = regex
self.explanation = explanation
self.no_following_explanations = no_following_explanations
self.show_note = show_note
self.reproduce = reproduce
self.long_explanation = long_explanation
self.long_explanation_url = long_explanation_url
def get(self, message, colorize_output):
explanation = self.get_short_explanation(message, colorize_output)
if explanation and (self.long_explanation or self.long_explanation_url):
url = self.long_explanation_url or explanation_url(self.label)
explanation += "\n See more information here: " + url
return explanation
def get_short_explanation(self, message, colorize_output):
match = None
if self.regex:
match = re.search(
self.regex,
"\n".join(message.text_without_ansi_codes),
flags=re.I | re.DOTALL,
)
if not match:
return None
if hasattr(self.precondition, "__call__"):
r = self.precondition(message, match)
if not r:
return None
if isinstance(r, str) and not self.explanation:
return r
if hasattr(self.explanation, "__call__"):
return self.explanation(message, match)
if colorize_output:
color = colors.color
else:
color = lambda text, *args, **kwargs: text
parameters = dict(
(name, getattr(message, name))
for name in dir(message)
if not name.startswith("__")
)
parameters["match"] = match
parameters["color"] = color
parameters["emphasize"] = lambda text: color(text, style="bold")
parameters["danger"] = lambda text: color(text, "red", style="bold")
parameters["info"] = lambda text: color(text, "cyan", style="bold")
f_string = self.explanation
f_string = re.sub(r"\*\*\{(.*?)\}\*\*", r"{emphasize(\1)}", f_string)
f_string = re.sub(r"\*\*(.*?)\*\*", r"{emphasize('\1')}", f_string)
f_string = 'f"""' + f_string + '"""'
return eval(f_string, globals(), parameters)
explanations = [
Explanation(
label="two_main_functions",
regex=r"multiple definition of \W*main\b",
explanation="Your program contains more than one main function - a C program can only contain one main function.",
reproduce="""\
// hack to get 2 main functions compiled in separate files
//dcc_flags=$src_file
int main(void) {
}
""",
),
Explanation(
label="no_main_function",
regex=r"undefined reference to \W*main\b",
explanation="Your program does not contain a main function - a C program must contain a main function.",
no_following_explanations=True,
reproduce="""\
""",
),
Explanation(
label="scanf_missing_ampersand",
regex=r"format specifies type '(?P<type>int|double) \*' but the argument has type '(?P=type)'",
explanation="Perhaps you have forgotten an '&' before '**{highlighted_word}**' on line {line_number} of {file}.",
reproduce="""\
#include <stdio.h>
int main(void) {
int i = 0;
scanf("%d", i);
}
""",
),
Explanation(
label="format_type_mismatch",
regex=r"format specifies type '[^:]+' but the argument has type '[^:]+'",
explanation="make sure you are using the correct format code (e.g., `%d` for integers, `%lf` for floating-point values) in your format string on line {line_number} of {file}.",
reproduce="""\
#include <stdio.h>
int main(void) {
printf("%d", "hello!");
}
""",
),
Explanation(
label="missing_semicolon_line_before_assert",
regex=r"called object type 'int' is not a function or function pointer",
explanation="there is probably a syntax error such as missing semi-colon on line {int(line_number) - 1} of {file} or an earlier line",
precondition=lambda message, match: message.highlighted_word == "assert",
reproduce="""\
#include <assert.h>
int main(void) {
int i = 10
assert(i == 10);
}
""",
),
Explanation(
label="assert_without_closing_parenthesis",
regex=r"unterminated function-like macro invocation",
explanation="it looks like there is a missing closing bracket on the assert on line {line_number} of {file}.",
precondition=lambda message, match: message.highlighted_word == "assert",
no_following_explanations=True,
show_note=False,
reproduce="""\
#include <assert.h>
int main(int argc, char *argv[]) {
assert(argc == 1;
}
""",
),
Explanation(
label="double_int_literal_conversion",
regex=r"implicit conversion from 'double' to 'int'",
explanation="you are assigning the floating point number **{highlighted_word}** to the int variable **{underlined_word}** , if this is what you want, change **{highlighted_word}** to **{truncate_number(highlighted_word)}**",
reproduce="""\
int main(int argc, char *argv[]) {
int i = 6.7;
return i;
}
""",
),
Explanation(
label="assign_to_multidimensional_array",
regex=r"array type .*?\]\[.* is not assignable",
explanation="""\
you are trying to assign to '**{underlined_word}**' which is an array.
You can not assign to a whole array.
You can use a nested loop to assign to each array element individually.
""",
reproduce="""\
int main(int argc, char *argv[]) {
int a[3][1], b[3][1] = {0};
a = b;
}
""",
),
Explanation(
label="assign_to_array",
regex=r"array type .*?[^\]]\[(\d+)\]' is not assignable",
explanation="""\
you are trying to assign to '**{underlined_word}**' which is an array with {match.group(1)} element{'s' if match.group(1) != '1' else ''}.
You can not assign to a whole array.
You can use a loop to assign to each array element individually.
""",
long_explanation=True,
reproduce="""\
int main(void) {
int a[1], b[1] = {0};
a = b;
}
""",
),
Explanation(
label="stack_use_after_return",
regex=r"address of stack memory associated with local variable '(.*?)' returned",
explanation="""\
you are trying to return a pointer to the local variable '**{highlighted_word}**'.
You can not do this because **{highlighted_word}** will not exist after the function returns.
""",
long_explanation=True,
reproduce="""\
int *f(void) {
int i;
return &i;
}
int main(void){}
""",
),
Explanation(
label="assign_function_to_int",
regex=r"incompatible pointer to integer conversion (assigning to|initializing) '(\w+)'.*\(",
explanation="""\
you are attempting to assign **{underlined_word}** which is a function to an **{match.group(2)}** variable.
Perhaps you are trying to call the function and have forgotten the round brackets and any parameter values.
""",
long_explanation=True,
reproduce="""\
int main(int argc, char *argv[]) {
int a = main;
return a;
}
""",
),
Explanation(
label="assign_array_to_int",
regex=r"incompatible pointer to integer conversion (assigning to|initializing) '(\w+)'.*]'",
explanation="""\
you are attempting to assign **{underlined_word}** which is an array to an **{match.group(2)}** variable.""",
reproduce="""
int main(void) {
int a[3][3] = {0};
a[0][0] = a[1];
}
""",
),
Explanation(
label="assign_pointer_to_int",
regex=r"incompatible pointer to integer conversion (assigning to|initializing) '(\w+)'",
explanation="""you are attempting to assign **{underlined_word}** which is not an **{match.group(2)}** to an **{match.group(2)}** variable.""",
reproduce="""
int main(int argc, char *argv[]) {
int a;
a = &a;
}
""",
),
Explanation(
label="missing_library_include",
regex=r"(implicitly declaring library function|call to undeclared library function) '(\w+)'",
explanation="""\
you are calling **{match.group(2)}** on line {line_number} of {file} but
dcc does not recognize **{match.group(2)}** as a function.
Do you have {emphasize('#include <' + extract_system_include_file(note) + '>')} at the top of your file?
""",
show_note=False,
reproduce="""\
int main(int argc, char *argv[]) {
printf("hello");
}
""",
),
Explanation(
label="misspelt_printf",
regex=r"(implicit declaration of|call to undeclared) function '(print.?.?)'",
explanation="""\
you are calling a function named **{match.group(2)}** on line {line_number} of {file} but dcc does not recognize **{match.group(2)}** as a function.
Maybe you meant **printf**?
""",
no_following_explanations=True,
reproduce="""\
#include <stdio.h>
int main(int argc, char *argv[]) {
print("hello");
}
""",
),
Explanation(
label="implicit_function_declaration",
regex=r"(implicit declaration of function|call to undeclared function) '(\w+)'",
explanation="""\
you are calling a function named **{match.group(2)}** line {line_number} of {file} but dcc does not recognize **{match.group(2)}** as a function.
There are several possible causes:
a) You might have misspelt the function name.
b) You might need to add a #include line at the top of {file}.
c) You might need to add a prototype for **{match.group(2)}**.
""",
no_following_explanations=True,
reproduce="""\
int main(int argc, char *argv[]) {
f();
}
""",
),
Explanation(
regex=r"expression is not assignable",
explanation="""\
you are using **=** incorrectly perhaps you meant **==**.
Reminder: you use **=** to assign to a variable.
You use **==** to compare values.
""",
reproduce="""\
int main(int argc, char *argv[]) {
if (argc = 1 || argc = 2) {
return 1;
}
}
""",
),
Explanation(
label="uninitialized-local-variable",
regex=r"'(.*)' is used uninitialized in this function",
explanation="""you are using the value of the variable **{match.group(1)}** before assigning a value to **{match.group(1)}**.""",
reproduce="""\
int main(void) {
int a[1];
return a[0];
}
""",
),
Explanation(
label="function-variable-clash",
regex=r"called object type .* is not a function or function pointer",
precondition=lambda message, match: re.match(r"^\w+$", message.underlined_word),
long_explanation=True,
explanation="""\
'**{underlined_word}**' is the name of a variable but you are trying to call it as a function.
If '**{underlined_word}**' is also the name of a function, you can avoid the clash,
by changing the name of the variable '**{underlined_word}**' to something else.""",
reproduce="""\
int main(void) {
int main;
return main();
}
""",
),
Explanation(
regex=r"function definition is not allowed here",
precondition=lambda message, match: message.line_number
and int(message.line_number) > 1,
long_explanation=True,
explanation="""\
there is likely a closing brace (curly bracket) missing before line {line_number} of {file}.
Is a **} missing** in the previous function?""",
no_following_explanations=True,
reproduce="""\
int f(int a) {
return a;
int main(void) {
return f(0);
}
""",
),
Explanation(
label="indirection-requires-pointer-operand",
regex=r"indirection requires pointer operand \('(.*)' invalid\)",
explanation="""\
you are trying to use '**{underlined_word}**' as a pointer.
You can not do this because '**{underlined_word}**' is of type **{match.group(1)}**.
""",
reproduce="""\
int main(int argc, char *argv[]) {
return *argc;
}
""",
),
Explanation(
label="duplicated-cond",
regex=r"duplicated .*\bif\b.* condition",
explanation="""\
you have repeated the same condition in a chain of if statements.
Only the first if statement using the condition can be executed.
The others can never be executed.
""",
reproduce="""\
int main(int argc, char *argv[]) {
if (argc == 1)
return 42;
else if (argc == 1)
return 43;
else
return 44;
}
""",
),
Explanation(
regex=r"condition has identical branches",
explanation="""\
your if statement has identical then and else parts.
It is pointless to have an if statement which executes the same code
when its condition is true and also when its condition is false.
""",
reproduce="""\
int main(int argc, char *argv[]) {
if (argc == 1)
return 42;
else
return 42;
}
""",
),
Explanation(
label="logical-or-always-true",
regex=r"logical .?\bor\b.* is always true|logical.*or.*of collectively exhaustive tests is always true|overlapping comparisons always evaluate to true",
explanation="""Your '**||**' expression is always true, no matter what value variables have.
Perhaps you meant to use '**&&**' ?
""",
reproduce="""
int main(int argc, char *argv[]) {
if (argc > 1 || argc < 3)
return 42;
else
return 43;
}
""",
),
Explanation(
label="logical-and-always-false",
regex=r"logical .?\band\b.* is always false|overlapping comparisons always evaluate to false",
explanation="""Your '**&&**' expression is always false, no matter what value variables have.
Perhaps you meant to use '**||**' ?
""",
reproduce="""
int main(int argc, char *argv[]) {
if (argc > 1 && argc < 1)
return 42;
else
return 43;
}
""",
),
Explanation(
label="logical-equal-expressions",
regex=r"logical .?((and|or)).? of equal expressions",
explanation="""you have used '**{highlighted_word}**' with same lefthand and righthand operands.
If this what you meant, it can be simplified: **{'x ' + highlighted_word + ' x'}** can be replaced with just **x**.
""",
reproduce="""\
int main(int argc, char *argv[]) {
if (argc > 1 ||argc > 1)
return 42;
else
return 43;
}
""",
),
Explanation(
regex=r"declaration shadows a local variable",
explanation="""you already have a variable named '**{highlighted_word}**'.
It is confusing to have a second overlapping declaration of the same variable name.
""",
reproduce="""\
int main(int argc, char *argv[]) {
{
int argc = 42;
return argc;
}
}
""",
),
Explanation(
label="nonnull",
regex=r"argument (\d+) null where non-null expected",
explanation="""\
you are passing {extract_argument_variable(highlighted_word, match.group(1), emphasize)} as {emphasize('argument ' + match.group(1))} to '**{extract_function_name(highlighted_word)}**'.
{emphasize('Argument ' + match.group(1))} to '**{extract_function_name(highlighted_word)}**' should never be NULL.
""",
reproduce="""\
#include <unistd.h>
int main(void) {
char *pathname = NULL;
faccessat(0, pathname, 0, 0);
}
""",
),
Explanation(
label="indexing_one_too_far",
regex=r"array index (\d+) is past the end of the array.*(which contains \1 element|\[\1\])",
explanation="""\
remember arrays indices start at zero.
The valid array indices for an array of size n are 0..n-1.
For example, for an array of size 10 you can use 0..9 as indices.
""",
reproduce="""\
int main(void) {
int a[42] = { 0 };
return a[42];
}
""",
),
Explanation(
regex=r"array subscript is not an integer",
precondition=lambda message, match: '"' in message.highlighted_word,
explanation="""\
you are using a string as an array index. An array index has to be an integer.
""",
reproduce="""\
int main(void) {
int a[1] = { 0 };
return a["0"];
}
""",
),
Explanation(
regex=r"continue.* statement not in loop",
explanation="""\
**continue** statements can only be used inside a while or for loop.
Check the braces {{}} are correct on nearby statements.
""",
reproduce="""\
int main(void) {
continue;
}
""",
),
Explanation(
regex=r"break.* statement not in loop",
explanation="""\
**break** statements can only be used inside a while loop, for loop or switch.
Check the braces **{{}}** are correct on nearby statements.
""",
reproduce="""\
int main(void) {
break;
}
""",
),
Explanation(
label="non_void_function_does_not_return_a_value_in_all_control_paths",
regex=r"non-void function does not return a value in all control paths",
explanation="""\
Your function contains a **return** but it is possible for execution
to reach the end of the function without a **return** statment being executed.
""",
reproduce="""\
int f(int a) {
if (a) {
return 1;
}
}
int main(int argc, char *argv[]) {
f(argc);
}
""",
),
Explanation(
label="non_void_function_does_not_return_a_value",
regex=r"non-void function does not return a value \[",
explanation="""\
your function has no **return** statement.
Unless a function is of type void, it must return a value using a **return** statement.
""",
reproduce="""\
int f(int a) {
}
int main(int argc, char *argv[]) {
f(argc);
}
""",
),
Explanation(
regex=r"data argument not used by format string",
explanation="""\
you have more argument values than % codes in the format string.
You need to change the format string or change the number of arguments.
""",
reproduce="""\
#include <stdio.h>
int main(void) {
printf("%d %d", 27, 28, 29);
}
""",
),
Explanation(
regex=r"more '%' conversions than data arguments",
explanation="""\
you have less argument values than % codes in the format string.
You need to change the format string or change the number of arguments.
""",
reproduce="""\
#include <stdio.h>
int main(void) {
printf("%d %d %d %d", 27, 28, 29);
}
""",
),
Explanation(
regex=r"expected ';' in 'for' statement specifier",
explanation="""\
the three parts of a '**;**' statment should be separated with '**;**'
""",
reproduce="""\
int main(void) {
for (int i = 0; i < 10 i++) {
}
}
""",
),
Explanation(
regex=r"expression result unused",
explanation="""\
you are doing nothing with a value on line {line_number} of {file}.
Did you mean to assign it to a varable?
""",
reproduce="""\
int main(int argc, char *argv[]) {
argc;
}
""",
),
Explanation(
regex=r"extra tokens at end of #include directive",
precondition=lambda message, match: ";"
in "".join(message.text_without_ansi_codes),
explanation="""\
you have unnecessary characters on your #include statement.
Remember #include statements don't need a '**;**'.
""",
reproduce="""\
#include <stdio.h>;
int main(void) {
}
""",
),
Explanation(
regex=r"extra tokens at end of #include directive",
explanation="""\
you have unnecessary characters on your #include statement.
""",
reproduce="""\
#include <stdio.h>@
int main(void) {
}
""",
),
Explanation(
label="h_file_not_found",
regex=r"s.*o.h' file not found",
explanation="""\
you are attempting to #include a file which does not exist.
Did you mean: '**#include <stdio.h>**'
""",
reproduce="""\
#include <studio.h>
int main(void) {
}
""",
),
Explanation(
regex=r"has empty body",
precondition=lambda message, match: ";"
in "".join(message.text_without_ansi_codes),
explanation="""\
you may have an extra '**;**' that you should remove.
""",
reproduce="""\
int main(int argc, char *argv[]) {
if (argc); {
}
}
""",
),
Explanation(
regex=r"ignoring return value of function",
explanation="""\
you are not using the value returned by function **{highlighted_word}** .
Did you mean to assign it to a variable?
""",
reproduce="""\
#include <stdlib.h>
int main(int argc, char *argv[]) {
atoi(argv[0]);
}
""",
),
Explanation(
label="ignoring_return_value_of_function",
regex=r"ignoring return value of function",
explanation="""\
you are not using the value returned by function **{highlighted_word}** .
Did you mean to assign it to a variable?
""",
reproduce="""\
#include <stdlib.h>
int main(int argc, char *argv[]) {
atoi(argv[0]);
}
""",
),
Explanation(
label="invalid_equal_equal_at_end_of_declaration",
regex=r"invalid '==' at end of declaration; did you mean '='",
explanation="""\
remember '**=**' is used to assign a value to a variable, '**==**' is used to compare values,
""",
reproduce="""\
int main(void) {
int i == 0;
}
""",
),
Explanation(
regex=r"invalid preprocessing directive",
explanation="""\
you have an invalid line begining with '**#**'.
Did you mean **#include** or **#define ** ?
""",
reproduce="""\
#inclde <stdio.h>
int main(void) {
}
""",
),
Explanation(
regex=r"return type of 'main' is not 'int'",
explanation="""\
'**main**' must always have return type **int**.
""",
reproduce="""\
void main(void) {
}
""",
),
Explanation(
regex=r"multiple unsequenced modifications",
explanation="""\
you are changing a variable multiple times in the one statement.
**`++`** and **`--`** change the variable, there is no need to also assign the result to the variable.
""",
reproduce="""\
int main(int argc, char *argv[]) {
argc = argc--;
}
""",
),
Explanation(
regex=r" parameter o. 'main'",
explanation="""\
your declaration of '**main**' is incorrect.
Try either '**int main(void)**' or '**int main(int argc, char *argv[])**'
""",
reproduce="""\
int main(int argc) {
}
""",
),
Explanation(
regex=r"relational comparison result unused",
precondition=lambda message, match: ","
in "".join(message.text_without_ansi_codes),
explanation="""\
you appear to be combining combining comparison incorrectly.
Perhaps you are using '**,**' instead of '**&&**' or '**||**'.
""",
reproduce="""\
int main(int argc, char *argv[]) {
return argc < 0, argc < 23;
}
""",
),
Explanation(
regex=r"result of comparison against a string literal is unspecified",
explanation="""\
you can not compare strings with '<', '>' etc.
'string.h' has functions which can compare strings, e.g. '**strcmp**'
""",
reproduce="""\
int main(int argc, char *argv[]) {
return argv[0] < "";
}
""",
),
Explanation(
regex=r"subscripted value is not an array",
explanation="""\
you appear to be incorrectly trying to use **{underlined_word}** as an array .
""",
reproduce="""\
int main(int argc, char *argv[]) {
return argc[0];
}
""",
),
Explanation(
label="missing_function_return_type",
regex=r"type specifier missing, defaults to 'int'",
precondition=lambda message, _: re.search(
rf"\b{message.highlighted_word}\s*\(", "".join(message.text_without_ansi_codes)
),
explanation="""\
have you given a return type for **{highlighted_word}**?
You must specify the return type of a function just before its name.
""",
reproduce="""\
square (int x) {
return 1;
}
int main(void) {
return square(0);
}
""",
),
Explanation(
label="missing_parameter_type",
regex=r"type specifier missing, defaults to 'int'",
precondition=lambda message, _: not re.search(
rf"\b{message.highlighted_word}\s*\(", "".join(message.text_without_ansi_codes)
),
explanation="""\
have you given a type for **{highlighted_word}**?
You must specify the type of each function parameter.
""",
reproduce="""\
int add(int b, c) {
return 1;
}
int main(void) {
return add(1, 2);
}
""",
),
Explanation(
regex=r" warning: unknown escape sequence '\\ '",
precondition=lambda message, match: "\\ n"
in "".join(message.text_without_ansi_codes),
explanation="""\
you have a space after a backslash which is not permitted.
Did you mean '\\\\n'?
""",
reproduce="""\
int main(void) {
return "\\ n"[0];
}
""",
),
Explanation(
regex=r" warning: unknown escape sequence '\\ '",
explanation="""\
you have a space after a backslash which is not permitted.
""",
reproduce="""\
int main(void) {
return "\\ "[0];
}
""",
),
Explanation(
regex=r"using the result of an assignment as a condition without parenthese",
explanation="""\
you use '**=**' to assign to a variable, you use '**==**' to compare values.
""",
reproduce="""\
int main(int argc, char *argv[]) {
if (argc = 4) {
return 1;
}
}
""",
),
Explanation(
regex=r"use of undeclared identifier",
explanation="""\
you have used the name '**{highlighted_word}**' on line {line_number} of {file} without previously declaring it.
If you meant to use '**{highlighted_word}**' as a variable, check you have declared it by specifying its type
Also check you have spelled '**{highlighted_word}**' correctly everywhere.
""",
reproduce="""\
int main(void) {
return x;
}
""",
),
Explanation(
regex=r"unknown type name 'define'",
explanation="""\
you appear to have left out a '#'.
Use #**define** to define a constant, for example: #define PI 3.14159
""",
reproduce="""\
define X 42
int main(void) {
}
""",
),
Explanation(
regex=r"unknown type name 'include'",
explanation="""\
you appear to have left out a '#'.
Use #**include** to include a file, for example: #include <stdio.h>
""",
reproduce="""\
define X 42
int main(void) {
}
""",
),
Explanation(
regex=r"is uninitialized when used here",
explanation="""\
you are using variable '**{highlighted_word}**' before it has been assigned a value.
Be sure to assign a value to '**{highlighted_word}**' before trying to use its value.
""",
reproduce="""\
int main(void) {
int x;
}
""",
),
Explanation(
regex=r"is uninitialized when used within its own initialization",
explanation="""\
you are using variable '**{highlighted_word}**' as part of its own initialization.
You can not use a variable to initialize itself.
""",
reproduce="""\
int main(void) {
int x;
}
""",
),
Explanation(
regex=r"void function '(.*)' should not return a value",
explanation="""\
you are trying to **return** a value from function **{match.group(1)}** which is of type **void**.
You need to change the return type of **{match.group(1)}** or change the **return** statement.
""",
reproduce="""\
void f(void) {
return 1;
}
int main(void) {
}
""",
),
Explanation(
regex=r"void function '(.*)' should not return a value",
explanation="""\
you are trying to **return** a value from function **{match.group(1)}** which is of type **void**.
You need to change the return type of **{match.group(1)}** or change the **return** statement.
""",
reproduce="""\
void f(void) {
return 1;
}
int main(void) {
}
""",
),
Explanation(
regex=r"too many arguments to function call, expected (\d+), have (\d+)",
explanation="""\
function **{underlined_word+"()"}** takes **{match.group(1)}** arguments but you have given it **{match.group(2)}** arguments.
""",
reproduce="""\